Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .agents/skills/layerfs-js-sdk/references/methods/store.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
store.list({ machine, with: ["stats"] })
store.get(store, { with: ["live", "stats"] })
store.create(name, { machine })
store.connect(store, url, { user, password, synchronous_commit })
store.connect(store, url, { user, password, synchronous_commit, dry_run })
store.disconnect(store)
store.attach(store, { machine })
store.attach(store, { machine, dry_run })
store.detach(store, { machine })
store.move(store, machine, { on_progress })
store.remove(store)
Expand Down Expand Up @@ -78,9 +78,9 @@ $ layerfs store get shop --with live,stats #
| Call | What it does | Notes |
| --- | --- | --- |
| `create(name)` | An empty sqlite Store | `{"ok":true}` (seen). A taken name is `Exists` (seen: `a Store named shop exists`). `machine` makes it on that running machine through its worker |
| `connect(store, url, …)` | Connects a PostgreSQL database as a Store (`user` and `password` required the first time, checked before anything is saved; `synchronous_commit` defaults to `local`); on a connected Store, changes its address, user, password or `synchronous_commit` (an omitted user or password keeps the saved one; the same Store must be reached) | The URL holds host, port, database and TLS settings only: one naming a user, password or `passfile` is `InvalidInput` with the `fix` `layerfs store connect <store> <url> --user <user> --password-stdin`. Shell: `--user U --password-stdin`; the password is never answered, logged or traced. Short `{ projects, projects_count? }`, or `{"ok":true}`; long `outcome` `connected` or `updated` |
| `connect(store, url, …)` | Connects a PostgreSQL database as a Store (`user` and `password` required the first time, checked before anything is saved; `synchronous_commit` defaults to `local`); on a connected Store, changes its address, user, password or `synchronous_commit` (an omitted user or password keeps the saved one; the same Store must be reached) | The URL holds host, port, database and TLS settings only: one naming a user, password or `passfile` is `InvalidInput` with the `fix` `layerfs store connect <store> <url> --user <user> --password-stdin`. Shell: `--user U --password-stdin`; the password is never answered, logged or traced. Short `{ projects, projects_count? }`, or `{"ok":true}`; long `outcome` `connected` or `updated`. A new address, user or password is checked from each attached machine first: one that fails refuses `Conflict` with `machines` and a detach `fix`, nothing saved. `dry_run: true` checks and saves nothing: `{ ok, outcome: "tested", here, machines }`. A refused check names its `step` |
| `disconnect(store)` | Forgets a PostgreSQL Store with its saved password and attachments, its data staying | `InUse` while a Sandbox or Workspace uses it |
| `attach(store, { machine })` | Lets a machine run Sandboxes of a PostgreSQL Store, checked from that machine with the saved URL and login first | Short `{ ok, outcome, latency_ms? }`; `outcome` `attached`, or `checked` when already attached. A sqlite Store is `InvalidInput`; machine native is attached like any other. `sandbox.create` on a machine not attached is refused with `layerfs store attach <store> --machine <machine>` as its `fix` |
| `attach(store, { machine })` | Lets a machine run Sandboxes of a PostgreSQL Store, checked from that machine with the saved URL and login first | Short `{ ok, outcome, latency_ms? }`; `outcome` `attached`, or `checked` when already attached, or `tested` for `dry_run: true` (records nothing). A refused check names its `step` and `machine`. A sqlite Store is `InvalidInput`; machine native is attached like any other. `sandbox.create` on a machine not attached is refused with `layerfs store attach <store> --machine <machine>` as its `fix` |
| `detach(store, { machine })` | Stops a machine running Sandboxes of a PostgreSQL Store | `InUse` while a Sandbox of the Store's projects is on it; `NotFound` when not attached |
| `move(store, machine)` | Moves a sqlite Store between this computer and a machine | To or from `native` only (seen when already there: `store move, check: Store shop is already on native`, `InvalidInput`; moving off a machine whose record lacks `vm_name` is `Unavailable` with `blocked_by`). Progress `check`, `snapshot`, `send`, `verify`, `install`, `record`, `retire`. Short `{ moved_bytes }`. A sqlite Store's Sandboxes run only on its machine |
| `remove(store)` | Deletes a sqlite Store with every project in it | `{"ok":true}`. `NotFound` for an unknown name (seen). A PostgreSQL Store is disconnected instead. A record this build does not read, or a Store on a machine whose record has no 2.0 name, goes by its records alone, with `left` saying what may remain |
Expand Down
10 changes: 10 additions & 0 deletions api/crates/layerfs-api/src/calls/guide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,11 +487,21 @@ const ARGS: &[(&str, &str, &str)] = &[
"password",
"the role's password, read from stdin; needed on a Store's first connect",
),
(
"store.connect",
"dry_run",
"check from this computer and each attached machine, and save nothing",
),
(
"store.attach",
"machine",
"the machine that may run the Store's Sandboxes",
),
(
"store.attach",
"dry_run",
"check from the machine as attach does, and record nothing",
),
(
"store.detach",
"machine",
Expand Down
13 changes: 8 additions & 5 deletions api/crates/layerfs-api/src/store/attach.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
//! `store.attach`: let a machine run Sandboxes of a PostgreSQL Store,
//! dialling the URL and login this computer saved; checked from it first.
use crate::calls::table::Kind::Name;
//! `--dry-run` makes the check alone and records nothing.
use crate::calls::table::Kind::{Bool, Name};
use crate::calls::table::{a, call, opt, Call};
use crate::calls::{Context, Form};
use layerfs_bridge::control::SdkValue;
use layerfs_home::names::{MachineName, StoreName};
use layerfs_home::outcome::Outcome;

/// Its answer: `ok` and the outcome (`attached`, or `checked` for an
/// attached machine checked again), with what the check measured.
/// Its answer: `ok` and the outcome (`attached`, `checked` for an
/// attached machine checked again, or `tested` for a dry run), with what
/// the check measured.
const FORM: Form = Form::of(
&["ok", "outcome"],
&["latency_ms"],
Expand All @@ -27,7 +29,7 @@ pub(crate) const CALL: Call = call(
"store",
"attach",
&[a("store", Name)],
&[opt("machine", Name).required()],
&[opt("machine", Name).required(), opt("dry_run", Bool)],
"let a machine run Sandboxes of a PostgreSQL Store, checked from that machine first",
run,
)
Expand All @@ -38,5 +40,6 @@ fn run(context: &mut Context<'_>, args: &SdkValue) -> Outcome {
let store = StoreName::parse(args.get_text("store").unwrap_or(""))?;
let machine = MachineName::parse(args.get_text("machine").unwrap_or(""))?;
let (home, events) = context.parts();
layerfs_control::store::attach(home, events, &store, &machine)
let dry_run = args.get_bool("dry_run");
layerfs_control::store::attach(home, events, &store, &machine, dry_run)
}
57 changes: 51 additions & 6 deletions api/crates/layerfs-api/src/store/connect.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! `store.connect`: connect a PostgreSQL database as a Store, or change a
//! connected one's address, login or `synchronous_commit`. This computer
//! is the one place a Store's URL and login are saved; machines attach to
//! the Store (`store.attach`) and dial both.
use crate::calls::table::Kind::{Content, Name, Text};
//! the Store (`store.attach`) and dial both. `--dry-run` makes the checks
//! alone, from this computer and each attached machine, and saves nothing.
use crate::calls::table::Kind::{Bool, Content, Name, Text};
use crate::calls::table::{a, call, opt, Call};
use crate::calls::{Context, Form};
use layerfs_bridge::control::SdkValue;
Expand All @@ -11,10 +12,18 @@ use layerfs_home::outcome::{refused, Outcome};
use layerfs_store::postgresql::connect::StoreConnect;

/// Its answer: the projects a first connect found, else `ok`; never the
/// password.
/// password. A dry run's (`outcome: tested`) shows the checks in the
/// short form too: `here`, this computer's, and `machines`, one row each.
const FORM: Form = Form::of(
&[],
&["ok", "projects", "projects_count"],
&[
"ok",
"outcome",
"projects",
"projects_count",
"here",
"machines",
],
&[
"ok",
"projects",
Expand All @@ -26,9 +35,42 @@ const FORM: Form = Form::of(
"user",
"synchronous_commit",
"machines",
"here",
],
)
.parts(&[("machines", crate::calls::Part::Scalars("machine"))]);
.parts(&[
("machines", crate::calls::Part::Scalars("machine")),
("here", crate::calls::Part::Rows(&TESTED)),
])
.short_when(&[("outcome", tested), ("here", tested), ("machines", tested)]);

/// One check of a dry run: `ok` (false kept in the short form), what it
/// measured, or the step it stopped at, the refusal's code and why.
const TESTED: Form = Form::of(
&["ok"],
&[
"latency_ms",
"server_version",
"empty",
"step",
"code",
"message",
],
&[
"ok",
"latency_ms",
"server_version",
"empty",
"step",
"code",
"message",
],
);

/// Whether the answer is a dry run's.
fn tested(fields: &[(String, SdkValue)], _asked: &[&str]) -> bool {
(fields.iter()).any(|(key, value)| key == "outcome" && *value == SdkValue::text("tested"))
}

/// Its table entry: the password is a secret the shell reads from stdin.
pub(crate) const CALL: Call = call(
Expand All @@ -39,6 +81,7 @@ pub(crate) const CALL: Call = call(
opt("user", Text),
opt("password", Content).from_stdin("--password-stdin"),
opt("synchronous_commit", Text),
opt("dry_run", Bool),
],
"connect a PostgreSQL database as a Store, or change a connected one's address, login or setting",
run,
Expand All @@ -61,6 +104,8 @@ fn run(context: &mut Context<'_>, args: &SdkValue) -> Outcome {
user: text("user"),
password,
synchronous_commit: text("synchronous_commit"),
dry_run: args.get_bool("dry_run"),
};
layerfs_control::store::connect(&context.home, &request)
let (home, events) = context.parts();
layerfs_control::store::connect(home, events, &request)
}
19 changes: 13 additions & 6 deletions api/crates/layerfs-api/tests/postgres_connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ fn connect_read_and_disconnect() {
}

/// A database the server does not have is a session the server refused,
/// not a server that took no connection (#535 terminal pass, lane K3).
/// not a server that took no connection (#535 terminal pass, lane K3): it
/// is refused at the connection step `database`, in the server's words.
#[test]
fn a_missing_database_is_a_refused_session() {
let (Ok(url), Ok(_)) = (
Expand All @@ -288,12 +289,18 @@ fn a_missing_database_is_a_refused_session() {
let missing = format!("{server}/lfs_no_such_database");
let mut args = vec![("store", t("gone")), ("url", t(&missing))];
args.extend(login);
let (code, message) = call(&home, "store.connect", &args).unwrap_err();
assert_eq!(code, "Unavailable", "{message}");
assert!(
message.contains("the server refused the session"),
"{message}"
let args = SdkValue::Map(
args.iter()
.map(|(n, v)| ((*n).to_owned(), v.clone()))
.collect(),
);
let refusal = layerfs_api::call(Some(&home), "store.connect", args, &mut |_| {}).unwrap_err();
let message = &refusal.message;
assert_eq!(refusal.code, "Unavailable", "{message}");
assert!(message.contains("refused the connection"), "{message}");
assert!(message.contains("lfs_no_such_database"), "{message}");
let next = refusal.detail.get("next").cloned().unwrap_or_default();
assert_eq!(next.get_text("step"), Some("database"), "{refusal:?}");
assert!(!paths::store_dir(&home, "gone").exists());
std::fs::remove_dir_all(&home).unwrap();
}
Loading