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
13 changes: 13 additions & 0 deletions pkgs/fleet/book/concepts/bridge.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ commands):
| `inbox [--agent A] [--groups g1,g2] [--commands \| --messages] [--all] [--since-id N] [--limit N] [--take] [--wait SECS] [--interval SECS]` | `GET /fleet/<fleet-token>/inbox?…` | Unread items by default; `--all` includes read ones; `--take` marks what it returns as read; `--wait` polls (default interval 15 s; wait capped at 24 h in source) until something arrives or the seconds pass. |
| `post [--agent A] [--reply-to ID] [--to ADDR] [--instruction \| --kind K] <text \| ->` | `POST /fleet/<fleet-token>/send` | A message to central by default. With `--reply-to` it answers that row (and marks it read). `--to` sends to any address instead: `drew`, `claude`, `chatgpt`, `connectors`, `agent:<name>` (or a bare name), `group:<name>`, `agents`, `all`; repeat or comma-separate. `--instruction` (or `--kind instruction`) makes it an instruction, which requires `--to`. |
| `ack [--agent A] (--up-to ID \| ID...)` | `POST /fleet/<fleet-token>/ack` | Mark messages read: either a list of ids, or everything up to an id. |
| `work [--agent A] --state acted\|queued\|done [--task ID] ID...` | `POST /fleet/<fleet-token>/work` | Record the work-item lifecycle of your own instructions: `acted` (handled in-session), `queued` (became queue task `--task`), `done`. Forward only, from `received`; only the receiving agent can mark its own copy. |
| `health` | `GET /healthz` | The bridge's health probe. No token. |

Defaults and config: the agent name defaults to `$FLEET_AGENT`, then
Expand All @@ -63,6 +64,18 @@ operations change read state:
- `post --reply-to ID` answers a row and marks that row read.
- `ack` marks explicit ids, or everything up to an id with `--up-to`.

Read state is not the whole story for instructions. An instruction is a
work item with a lifecycle the receiving agent records with `work`:
`received` → `acted` (handled in-session) or `queued` (it became a named
fleet queue task) → `done`, forward only, each recipient's copy
independent on a group send. The owner's pipeline shows the state and
the queue task id, so an instruction that is neither acted on nor
queued stays visible at `received` — the defect the task-it-or-queue-it
rule (in the reference chapter, and in the text the intake ladder hands
a woken agent) exists to prevent. Notes instructions carry the same
fields on the notes surface; connector-channel instructions store none —
their read state stands in for `acted`.

The registry side is equally simple: there is no separate enrolment store
on the bridge. Saying `hello` _is_ registering — name, groups, and last-seen
are refreshed on every hello — and `agents` reports the registry as the set
Expand Down
34 changes: 33 additions & 1 deletion pkgs/fleet/book/reference/bridge.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Two token families, both resolved **by name** through fleet-secrets; values are
| Family | Secret name | Opens | URL shape |
| --------------------------- | ------------------------------------------------------------------------- | ---------------------------------------------------------------------------- | ---------------------------- |
| Owner token (central verbs) | `message-bridge.access-token` (renamable via `[bridge] owner_token_name`) | `send`, `agents`, `log` | `/api/<owner-token>/fleet/…` |
| Fleet token (agent verbs) | `message-bridge.fleet-token` | `hello`, `inbox`, `post`, `ack` — agent routes only; it cannot send commands | `/fleet/<fleet-token>/…` |
| Fleet token (agent verbs) | `message-bridge.fleet-token` | `hello`, `inbox`, `post`, `ack`, `work` — agent routes only; it cannot send commands | `/fleet/<fleet-token>/…` |
| Per-agent credential | `bridge.<agent>-fleet-token` (the name the agent profile records) | The same agent routes, as that one agent only; its groups are the ones bound when the owner minted its bootstrap token | `/fleet/<credential>/…` |
| Notes token (notes family) | `bridge.<agent>-token` (renamable via `[bridge] notes_token_name`) | `notes send`, `notes inbox`, `notes ack` for the token's class/name identity | `Authorization: Bearer` header on `/notes/…` |

Expand Down Expand Up @@ -346,6 +346,38 @@ $ fleet bridge ack --agent archer --up-to 412

Marks all of `archer`'s messages up to id 412 as read.

## fleet bridge work

```
fleet bridge work [--agent A] --state acted|queued|done [--task ID] ID...
```

| Flag | Argument | Default | Meaning |
| -------------------- | --------------------- | ------------------------------------- | ------------------------------------------------------------------ |
| `--agent` | `A` | `$FLEET_AGENT`, then `[bridge] agent` | Agent whose mailbox copy to mark. |
| `--state` | `acted\|queued\|done` | required | The lifecycle state to record. |
| `--task` | `ID` | — | The fleet queue task the instruction became; required for `queued`. |
| `ID...` (positional) | ids | required | The instruction rows to mark; several ids are marked one by one. |

### Behaviour

Agent verb (fleet token). Records the **work-item lifecycle** of instructions in this agent's own mailbox. Route: `POST /fleet/<fleet-token>/work` with `{agent, id, state, task_id?}` per id. An instruction starts at `received`; the receiving agent moves its own copy forward — `acted` (handled in-session), `queued` (it became a fleet queue task; `--task` names it), then `done`. States move only forward (`received` → `acted` | `queued` → `done`); re-marking the current state is a no-op. Only the receiving agent can mark a row: with a per-agent credential, naming another agent or another agent's row is the bridge's plain 404. The owner sees every row's state and task id in the pipeline, so an instruction that is neither acted on nor queued stays visible at `received`. Notes instructions carry the same lifecycle on the notes surface (`POST /notes/work`, same body shape); connector-channel instructions store no lifecycle fields — their read state stands in for `acted` in the pipeline.

The rule the lifecycle serves, carried verbatim by this book, the verb's help text, and the woken-agent text the intake ladder hands out with a pushed command:

> Task it or queue it. An instruction is a work item: do the work now, in this session, and reply — or queue it as a repo-task/v1 packet whose context_refs carry the bridge surface, the message id and the batch, and reply with the task id. Record the outcome with fleet bridge work: --state acted for work done in-session, --state queued --task <id> for queued work, --state done when the work is finished. An instruction that is neither acted on nor queued is a defect: it stays at received, where the owner can see it stuck in the pipeline.

### Exit codes

`0` ok · `2` usage (including `--state queued` without `--task`) · `3` transport or credentials · `4` the bridge refused the request.

### Example

```console
$ fleet bridge work --agent archer --state queued --task 23500 412
```

Records that archer queued instruction 412 as fleet queue task 23500.
## fleet bridge exchange

```
Expand Down
24 changes: 24 additions & 0 deletions pkgs/fleet/crates/fleet-agent/src/next.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ use crate::{
DEFAULT_REPO, EXIT_NO_WORK, EXIT_OK, Out, R, World, argv, check_name, check_repo, parse, usage,
};

/// The task-it-or-queue-it rule handed to an agent woken by a pushed
/// command (design §4.6, #14372). Verbatim copy of fleet-bridge's
/// WORK_RULE; the same text stands in the fleet book
/// (pkgs/fleet/book/reference/bridge.md). Edit all three together.
pub const PUSH_WORK_RULE: &str = "\
Task it or queue it. An instruction is a work item: do the work now, in \
this session, and reply — or queue it as a repo-task/v1 packet whose \
context_refs carry the bridge surface, the message id and the batch, and \
reply with the task id. Record the outcome with fleet bridge work: \
--state acted for work done in-session, --state queued --task <id> for \
queued work, --state done when the work is finished. An instruction \
that is neither acted on nor queued is a defect: it stays at received, \
where the owner can see it stuck in the pipeline.";

/// Labels that hold an issue (FDR-0012): a person set them on purpose.
pub const HOLD_LABELS: [&str; 3] = ["held", "user/owned", "do-not-auto-edit"];
/// Labels on an issue that indexes other work and is not itself a
Expand Down Expand Up @@ -603,6 +617,11 @@ pub fn next(world: &dyn World, args: &[String]) -> R<(Value, i32)> {
"command_id": id,
"instructions": message.get("body").and_then(Value::as_str).unwrap_or(""),
"message": message,
// The task-it-or-queue-it rule (design §4.6, #14372):
// the same text the work verb's help and the fleet
// book carry (fleet-bridge's WORK_RULE) — keep all
// three verbatim-consistent.
"rule": PUSH_WORK_RULE,
});
let then = vec![
format!(
Expand All @@ -611,6 +630,11 @@ pub fn next(world: &dyn World, args: &[String]) -> R<(Value, i32)> {
format!(
"fleet bridge post --agent {agent} --reply-to {id} \"could not: <reason>\""
),
format!("fleet bridge work --agent {agent} --state acted {id}"),
format!(
"fleet bridge work --agent {agent} --state queued --task <queue task id> {id}"
),
format!("fleet bridge work --agent {agent} --state done {id}"),
];
return Ok(answer("push", task, then, log));
}
Expand Down
87 changes: 87 additions & 0 deletions pkgs/fleet/crates/fleet-bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ const FLEET_TOKEN_LABEL: &str = "message-bridge/fleet-token/v1";
const DEFAULT_INTERVAL_SECS: u64 = 15;
const MAX_WAIT_SECS: u64 = 24 * 3600;

/// The task-it-or-queue-it rule for instructions (design §4.6,
/// #14372). This exact text is carried in three places that must stay
/// verbatim-consistent: here (the work verb's help), the fleet book
/// (pkgs/fleet/book/reference/bridge.md), and the woken-agent text the
/// intake ladder hands out with a pushed command
/// (fleet-agent/src/next.rs). Edit all three together.
pub const WORK_RULE: &str = "\
Task it or queue it. An instruction is a work item: do the work now, in \
this session, and reply — or queue it as a repo-task/v1 packet whose \
context_refs carry the bridge surface, the message id and the batch, and \
reply with the task id. Record the outcome with fleet bridge work: \
--state acted for work done in-session, --state queued --task <id> for \
queued work, --state done when the work is finished. An instruction \
that is neither acted on nor queued is a defect: it stays at received, \
where the owner can see it stuck in the pipeline.";

pub const USAGE: &str = "\
usage: fleet bridge <verb> [options]

Expand Down Expand Up @@ -118,6 +134,10 @@ agent (fleet token):
--instruction (or --kind instruction) makes it an instruction,
which needs --to.
ack [--agent A] (--up-to ID | ID...) mark messages read
work [--agent A] --state acted|queued|done [--task ID] ID...
record the work-item lifecycle of your own instructions:
acted (handled in-session), queued (became queue task --task),
done. States move only forward, from received.

provisioning (single-use bootstrap token, minted by the owner):
exchange --agent A --bootstrap TOKEN
Expand Down Expand Up @@ -298,6 +318,22 @@ usage: fleet bridge ack [--agent A] (--up-to ID | ID...)

agent (fleet token). Mark messages read."
}
"work" => {
return Some(format!(
"\
usage: fleet bridge work [--agent A] --state acted|queued|done [--task ID] ID...

agent (fleet token). Record the work-item lifecycle of instructions in
your own mailbox: acted for work handled in-session, queued for work
that became a fleet queue task (give its id with --task), done when the
work is finished. States move only forward, from received; a row can
only be marked by the agent it was sent to.

{WORK_RULE}

{USAGE_FOOTER}"
));
}
"exchange" => {
"\
usage: fleet bridge exchange --agent A --bootstrap TOKEN
Expand Down Expand Up @@ -753,6 +789,8 @@ const VALUE_FLAGS: &[&str] = &[
"--interval",
"--reply-to",
"--up-to",
"--state",
"--task",
"--bootstrap",
"--scopes",
"--ttl",
Expand Down Expand Up @@ -1432,6 +1470,55 @@ pub fn execute(ctx: &mut Ctx, args: &[String]) -> R<Value> {
let token = fleet_token(ctx)?;
client_with(ctx, &token).call("POST", &format!("/fleet/{token}/ack"), Some(&payload))
}
"work" => {
let a = parse(rest, &[], &["--agent", "--state", "--task"])?;
let agent = agent_of(&a, &ctx.settings)?;
let state = a
.last("--state")
.ok_or_else(|| usage("work needs --state acted, queued or done"))?;
if !matches!(state.as_str(), "acted" | "queued" | "done") {
return Err(usage(format!(
"--state is acted, queued or done, not '{state}'"
)));
}
let task = a.uint("--task")?;
if state == "queued" && task.is_none() {
return Err(usage(
"queued work needs --task: the fleet queue task id it became",
));
}
let ids: Vec<u64> =
a.positional
.iter()
.map(|p| {
p.parse::<u64>().ok().filter(|n| *n > 0).ok_or_else(|| {
usage(format!("message id '{p}' is not a positive number"))
})
})
.collect::<R<_>>()?;
if ids.is_empty() {
return Err(usage("work needs at least one message ID"));
}
let token = fleet_token(ctx)?;
let client = client_with(ctx, &token);
let mut marked = Vec::new();
for id in &ids {
let mut payload = json!({ "agent": &agent, "id": id, "state": &state });
if let Some(t) = task {
payload["task_id"] = json!(t);
}
marked.push(client.call(
"POST",
&format!("/fleet/{token}/work"),
Some(&payload),
)?);
}
if marked.len() == 1 {
Ok(marked.pop().unwrap())
} else {
Ok(json!({ "marked": marked }))
}
}
"exchange" => verb_exchange(ctx, rest),
"notes" => verb_notes(ctx, rest),
"health" => {
Expand Down
59 changes: 59 additions & 0 deletions pkgs/fleet/crates/fleet-bridge/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ fn every_verb_has_its_own_help() {
"inbox",
"post",
"ack",
"work",
"health",
"exchange",
"notes",
Expand Down Expand Up @@ -235,6 +236,13 @@ fn usage_errors_exit_2_before_any_request() {
],
vec!["ack", "--agent", "a"],
vec!["ack", "--agent", "a", "0"],
vec!["work", "--agent", "a"],
vec!["work", "--agent", "a", "--state", "done"],
vec!["work", "--agent", "a", "--state", "sideways", "3"],
vec!["work", "--agent", "a", "--state", "received", "3"],
vec!["work", "--agent", "a", "--state", "queued", "3"],
vec!["work", "--agent", "a", "--state", "done", "0"],
vec!["work", "--agent", "a", "--state", "done", "--task", "x", "3"],
vec!["log", "--limit", "many"],
vec!["pipeline", "--limit", "many"],
vec!["pipeline", "--bogus", "x"],
Expand Down Expand Up @@ -471,6 +479,57 @@ fn ack_log_and_agents_shapes() {
);
}

#[test]
fn work_marks_the_agents_own_rows_and_names_the_queue_task() {
let (url, seen) = fake_bridge(|_, _, _| {
(200, r#"{"message":{"id":3,"work_state":"queued","work_task_id":23500}}"#.into())
});
let s = secrets(&[(DEFAULT_BRIDGE_FLEET_TOKEN_NAME, "f".repeat(40).as_str())]);
let out = run_with(
&url,
&s,
&["work", "--agent", "muse-1", "--state", "queued", "--task", "23500", "3"],
"",
)
.unwrap();
assert_eq!(out["message"]["work_state"], "queued");
run_with(
&url,
&s,
&["work", "--agent", "muse-1", "--state", "acted", "4", "5"],
"",
)
.unwrap();
let seen = seen.lock().unwrap();
assert_eq!(seen[0].method, "POST");
assert_eq!(seen[0].url, format!("/fleet/{}/work", "f".repeat(40)));
assert_eq!(
seen[0].body.as_ref().unwrap(),
&json!({"agent": "muse-1", "id": 3, "state": "queued", "task_id": 23500})
);
// Several ids are one request each, in order.
assert_eq!(seen.len(), 3);
assert_eq!(
seen[1].body.as_ref().unwrap(),
&json!({"agent": "muse-1", "id": 4, "state": "acted"})
);
assert_eq!(
seen[2].body.as_ref().unwrap(),
&json!({"agent": "muse-1", "id": 5, "state": "acted"})
);
}

#[test]
fn work_help_carries_the_task_it_or_queue_it_rule() {
// #14372 acceptance: the help text and the book carry the same
// rule, verbatim. The book half is pinned by the text itself
// (pkgs/fleet/book/reference/bridge.md quotes WORK_RULE).
let text = verb_usage("work").unwrap();
assert!(text.contains(WORK_RULE), "{text}");
assert!(WORK_RULE.contains("repo-task/v1"), "{WORK_RULE}");
assert!(WORK_RULE.contains("context_refs"), "{WORK_RULE}");
}

#[test]
fn pipeline_directory_credentials_and_revoke_shapes() {
// #14374: the console's unified views and its revoke control, as
Expand Down
Loading