diff --git a/harness/README.md b/harness/README.md index 38e415e..55e20fb 100644 --- a/harness/README.md +++ b/harness/README.md @@ -107,6 +107,34 @@ over the link. A new capability and two fields; one prompt text made plainer; no person in that turn, unasked, and never tells the person to ask again. A session keeps the system prompt it was started with (`context.md` §10), so only sessions started after the change read it. +Amended before any release carried it, after a review of the CLI's implementation found work that +could be lost around such turns. No capability, field or method is added by these; a client that +follows 3.10.0 as first written follows this. + +- **A schedule's run waits for its subagents** (`acp.md`, *Scheduled work* → *The run waits for its + subagents*; `reports.md` §2). A scheduled turn on a device does not end while a subagent it + started is still out: the turn stays open (`waiting_for_subagents`), each report reaches the main + agent in the same turn, and the answer's `text` and `report` come from the last reply. Before, + the run answered with the reply that ended the main agent's first turn ("the subagents are on + it") and the real answer came in a turn `report_write` was not offered in. A run is one turn; + background commands are not waited for; Stop on a scheduled turn stops the run's subagents too, + and so does any other end of the turn with subagents still out. +- **A session that still has work out** (`acp.md`, section of that name; `conformance/outstanding.json`, + 8 cases): with no turn running, a subagent that works or waits, a report about to wake the main + agent, or a background command makes a session *not idle* for the device — the link is not + closed for a change of limits, a service does not start again for an update, and a terminal + opening the session is refused in words. `status.busy` still means "a turn runs". +- **Stop, said whole** (`session/cancel`; *A turn the harness starts*): it stops the turn and no + subagent (the scheduled turn apart); a turn stopped while it waited for a model slot is in the + history like any stopped turn, its note with it; notes a stopped turn had not delivered are kept + with the session, across a restart. +- **A steer nobody could be handed** (*A turn the harness starts*, Steer): kept with the session, + across a restart, and taken in at the next turn's first step in every mode — said with `steered` + and its id — and never also handed back as `unsent`. And the `task` end that carries `unsent` + comes before the `inbox` event that says the queue is empty. +- **A wake that may not start a turn** (a schedule's session on an interactive-only model): the + session is idle again and the note is kept for the person's next message. + ## 3.9.0 Written ahead of the implementations; the CLI carries it in the same round. A new capability and diff --git a/harness/acp.md b/harness/acp.md index 6a18c51..2c5a076 100644 --- a/harness/acp.md +++ b/harness/acp.md @@ -219,7 +219,11 @@ Params: `{ sessionId, modeId }`, lowered to `remote.max_mode`. Result `{}`. ### `session/cancel` — client → agent (notification) Params: `{ sessionId }`. Stops the running turn, whoever started it: the client's prompt, a turn -typed in a terminal, or one the harness started. Prompts waiting behind it still run. +typed in a terminal, or one the harness started. Prompts waiting behind it still run. **It stops +the turn, not the team**: subagents that work go on, and a report that arrives later starts a turn +as any does (*A turn the harness starts*) — a subagent is stopped by `_lembas/team/stop`. The one +exception is a scheduled turn, where Stop stops the run and its subagents with it (*Scheduled +work*, *The run waits for its subagents*). ### `session/update` — agent → client (notification) @@ -446,6 +450,35 @@ In a scheduled turn: - Subagents as in an agent chat (`agents`, the tree, findings); a subagent's call only the person can allow is an approval like the main agent's, with `scheduled` beside `subagent`. +**The run waits for its subagents** (spec 3.10.0; `conformance/outstanding.json`, `run_waits`). In +a chat the main agent's turn may end while subagents work, and each report wakes it with a turn +of its own (`subagents.md` §9). A run has nobody to read those turns and one answer to give, so +**a scheduled turn does not end while a subagent it started is still out** — a team's subagent +that is `working`, `waiting` or `paused`, a background `task` subagent still running — or while a +report waits for the main agent to get a slot: + +- When the main agent's reply ends with subagents out, the turn stays open: its progress is + `waiting_for_subagents` (`progress.md` §4), it holds no slot of its model meanwhile (`team.md` + §8, and takes one again before it goes on), and **each report reaches it in the same turn**, as + a note at a step boundary (`team.md` §7) — never as a turn of its own. The model reads exactly + what a woken turn would have given it. +- So a run is **one turn**, from its `task` start to its `task` end, with the run's `turnId` on + every event; `report_write` is offered throughout; and the answer's `text` and `report` are + from the turn's last reply — given after the last report came in. +- **Background commands are not waited for** — one may never end. A command the run left running + is still the run's (`interactive.md` §2): its note later wakes a turn of the session, which is + scheduled work, and in which `report_write` answers its `unavailable` text: the run has + answered, and there is no answer left to carry a report. +- **What bounds the wait**: nothing new. Every subagent's own turn ends by its step limit and its + budgets; the session's time budget, where one is set, counts the wait (looked at when the + next report comes in, as between any two steps); and a run that says + nothing for the client's inactivity limit is stopped by the client, as any run is. While it + waits, the tree (`team`), the subagents' tool calls (`sub_tool`) and their phases are still said. +- **Stop (`session/cancel`) on a scheduled turn stops the run**: the turn, and every subagent of + it that still works or waits. The same when the turn ends any other way with subagents still + out — an error, the step limit, a budget: they are stopped with it, and none of them leaves a + note. **Nothing of a run's subagents works after its answer.** + ### How the turn ends and reports `session/prompt`'s answer carries the run's outcome in `_meta.lembas.scheduled`: @@ -637,10 +670,19 @@ with the note as its user message (`loop.json` `jobs`, `team.md` §7). Nobody ty - **Steer** (`_lembas/steer`): accepted while it runs, taken in at its next step, said with `steered`. A message it ended before taking in comes back as `unsent` on its `task` end event — it has no prompt's answer to carry it — and the client sends it next, as it does a prompt's - `unsent`. With no client listening the message stays, and goes in at the next turn's first step; - so it does in a session a terminal runs, where it stays in the terminal's own queue. -- **Stop** (`session/cancel`): stops it. Notes it had not given the model yet start no further turn: - they go in with the person's next message. + `unsent`. That `task` end comes **before** any `inbox` event that says the queue is empty, so an + emptied queue is never read as messages taken in. With no client listening (no link is up) the + message is **kept with the session — across a restart of the device — and goes in at the next + turn's first step**, whoever starts that turn and whatever the device's own setting for messages + typed mid-task: it is said with `steered` and its id there, and is never handed back as `unsent` + as well. In a session a terminal runs it stays in the terminal's own queue. +- **Stop** (`session/cancel`): stops it — and nothing else: no subagent is stopped with it, and a + report that arrives afterwards starts a turn as any does. Notes it had not given the model yet + start no further turn: they are kept with the session — across a restart — and go in with the + person's next message. A turn stopped **while it still waits for a model slot** (`team.md` §8) + had already begun for the client — its `task` start and its `prompt` were said — so it is a + stopped turn like any other: its user turn is in the history under its id, the note with it, + and the model reads it with the next message. Nothing of it is lost. - **A prompt** (`session/prompt`) that arrives while it runs waits its turn — it is in `queued` — and runs after the turn's `task` end. A prompt carrying the harness turn's own id does not run: the id is known, and the answer is `null` (the turn has none of this side's). @@ -649,9 +691,13 @@ with the note as its user message (`loop.json` `jobs`, `team.md` §7). Nobody ty - **History** (`_lembas/session/history`): its user turn is there, with its `turnId`, `machine: true` and the whole note as `text`; the reply follows it. - **The link** is not closed for a change of limits while one runs (*`_lembas/hello`*: "once no - session is working"). + session is working"), nor while one is about to: see *A session that still has work out*. - **Scheduled sessions.** A turn a schedule's job wakes is the schedule's work: it does not run on - an interactive-only model (`interactive.md`), and the device says so in a `notice` instead. + an interactive-only model (`interactive.md`), and the device says so in a `notice` instead. No + turn starts, so the session is idle again — the main agent holds no slot for it — and the note + is kept, as after a Stop: it goes in with the person's next message. During a schedule's run + itself no such turn starts at all: the run's turn takes its subagents' reports in (*The run + waits for its subagents*). **The client's side.** The row it keeps for such a turn is the harness's (`machine`), keyed by the turn's id; the reply after it is filled from the events between that id's `prompt` and its `task` @@ -666,6 +712,26 @@ with `machine` and both `task` events with the id, but its `status` says `busy: turn while one runs, it refuses a steer then, and a prompt sent then fails (`-32603`); a client follows such a turn by its `task` events alone and sends nothing to the session until its end. +## A session that still has work out (spec 3.10.0) + +A session's turn may end with work of it still going on **in the device's process**: a subagent +that works or waits for a slot, a report about to wake the main agent, a background command. +`_lembas/session/status` then says `busy: false` — no turn runs — and shows the work in `tree` and +`jobs`. The device treats such a session as **not idle** wherever letting go of it would lose +that work (`conformance/outstanding.json`, `has_work`): + +- it does not close the link for a change of limits (*`_lembas/hello`*), and a service does not + start again on a newly installed release, until no session has a turn running or waiting **and + none has work out**; +- **a terminal on the device that opens the session is refused, in words that name what still + works**, as it is while a turn runs. A terminal is another process: it could carry the + conversation on from the store, but not the subagents and commands, and closing the service's + copy would stop them with no report ever arriving. So the session stays where its work is; the + person waits, or stops the subagents and jobs (`_lembas/team/stop`, `_lembas/job/stop`), and + opens it then. + +Deleting the session (`_lembas/session/delete`) still stops everything, as it says. + ## `_lembas/*` methods Direction is who sends it. "→ result" marks a request; the others are notifications. @@ -685,7 +751,8 @@ that is not linked. The device's limits apply to every request as they are at that moment. When what the hello and `initialize` said changes (the device's `remote:` block was edited — a folder added, remote work -turned on), the device closes the link once no session is working and dials again at once, so the +turned on), the device closes the link once no session is working — no turn runs or waits, and no +session has work out (*A session that still has work out*) — and dials again at once, so the client hears the new limits. ### `_lembas/event` — agent → client diff --git a/harness/conformance/outstanding.json b/harness/conformance/outstanding.json new file mode 100644 index 0000000..7420926 --- /dev/null +++ b/harness/conformance/outstanding.json @@ -0,0 +1,224 @@ +{ + "about": "Spec 3.10.0 (acp.md, *A session that still has work out* and *The run waits for its subagents*): what a session still has out when no turn of it runs. `agents`: the team as its tree has it — the main agent (`lead`: `working`, `waiting` or `idle`) and every live subagent with its state (subagents.md §9); an ended subagent is not in it. `jobs`: the session's background jobs that are not a team's subagents (a team's subagent counts once, by its state), each `kind` (`command` or `subagent`, a background `task`) and `state` as in JobInfo. The result: `subagents` — who still works for the session and will report: the team's subagents that are `working`, `waiting` or `paused`, by name in tree order, then the running background `task` subagents by job id; `commands` — the running background commands by job id; `run_waits` — a schedule's run does not end yet: a subagent is out, or the main agent is `waiting` (a report waits for it to get a slot) — never for a command, which may not end; `has_work` — the session is not idle for the device (the link is not closed for a change of limits, a service does not start again for an update, a terminal opening the session is refused): `run_waits`, or a command runs. LLeMbas: where its own scheduled runs with a team end; CLI: src/session/outstanding.ts.", + "cases": [ + { + "about": "Nothing out: a team whose subagents have all reported, and no jobs.", + "agents": [ + { + "name": "lead", + "state": "idle" + }, + { + "name": "api", + "state": "finished" + } + ], + "jobs": [], + "expect": { + "subagents": [], + "commands": [], + "run_waits": false, + "has_work": false + } + }, + { + "about": "A subagent works after the main agent's reply ended.", + "agents": [ + { + "name": "lead", + "state": "idle" + }, + { + "name": "api", + "state": "working" + } + ], + "jobs": [], + "expect": { + "subagents": [ + "api" + ], + "commands": [], + "run_waits": true, + "has_work": true + } + }, + { + "about": "One waits for a slot, one is in its last turn after its context filled, one has finished: the first two are out, in tree order.", + "agents": [ + { + "name": "lead", + "state": "idle" + }, + { + "name": "web", + "state": "finished" + }, + { + "name": "api", + "state": "paused" + }, + { + "name": "docs", + "state": "waiting" + } + ], + "jobs": [], + "expect": { + "subagents": [ + "api", + "docs" + ], + "commands": [], + "run_waits": true, + "has_work": true + } + }, + { + "about": "Every subagent has finished, and the last report waits for the main agent to get a slot: the run is not over.", + "agents": [ + { + "name": "lead", + "state": "waiting" + }, + { + "name": "api", + "state": "finished" + } + ], + "jobs": [], + "expect": { + "subagents": [], + "commands": [], + "run_waits": true, + "has_work": true + } + }, + { + "about": "A background task subagent still runs: out, named by its job.", + "agents": [ + { + "name": "lead", + "state": "idle" + } + ], + "jobs": [ + { + "id": "job2", + "kind": "subagent", + "state": "running" + }, + { + "id": "job1", + "kind": "subagent", + "state": "done" + } + ], + "expect": { + "subagents": [ + "job2" + ], + "commands": [], + "run_waits": true, + "has_work": true + } + }, + { + "about": "A background command runs: the session has work out, but a run does not wait for it — it may never end.", + "agents": [ + { + "name": "lead", + "state": "idle" + } + ], + "jobs": [ + { + "id": "job1", + "kind": "command", + "state": "running" + } + ], + "expect": { + "subagents": [], + "commands": [ + "job1" + ], + "run_waits": false, + "has_work": true + } + }, + { + "about": "Finished, stopped and lost jobs are not out.", + "agents": [ + { + "name": "lead", + "state": "idle" + } + ], + "jobs": [ + { + "id": "job1", + "kind": "command", + "state": "done" + }, + { + "id": "job2", + "kind": "command", + "state": "stopped" + }, + { + "id": "job3", + "kind": "subagent", + "state": "lost" + }, + { + "id": "job4", + "kind": "command", + "state": "error" + } + ], + "expect": { + "subagents": [], + "commands": [], + "run_waits": false, + "has_work": false + } + }, + { + "about": "Both kinds: subagents first by name, then the task subagent by job; the command beside them.", + "agents": [ + { + "name": "lead", + "state": "working" + }, + { + "name": "api", + "state": "working" + } + ], + "jobs": [ + { + "id": "job3", + "kind": "command", + "state": "running" + }, + { + "id": "job4", + "kind": "subagent", + "state": "running" + } + ], + "expect": { + "subagents": [ + "api", + "job4" + ], + "commands": [ + "job3" + ], + "run_waits": true, + "has_work": true + } + } + ] +} diff --git a/harness/reports.md b/harness/reports.md index bf100bf..d85d545 100644 --- a/harness/reports.md +++ b/harness/reports.md @@ -40,6 +40,13 @@ not in the list at all. A subagent of a run is never offered it: the run's own agent files the report. +**The run is the schedule's turn, whole** (spec 3.10.0). On a linked computer that turn stays +open while the run's subagents work and takes their reports in (`acp.md`, *The run waits for its +subagents*), so the tool is offered until the run answers — after the last report. A turn the +session starts by itself *after* that answer — a background command the run left ended — is the +schedule's work (`interactive.md` §2) but not its run: the tool answers `unavailable` there, +because no answer is left to carry a report. + ## 3. Notify when A schedule says when its owner wants to hear from it (`scheduled.notify.values`): diff --git a/src/session/outstanding.ts b/src/session/outstanding.ts new file mode 100644 index 0000000..6894a45 --- /dev/null +++ b/src/session/outstanding.ts @@ -0,0 +1,49 @@ +// What a session still has out when no turn of it runs (harness/acp.md, *A session that still has +// work out*; conformance/outstanding.json): subagents that work or wait, a report that waits for +// the main agent, background commands that run. One rule, asked by three things: a schedule's run +// (it is not over while its subagents work), the service (it does not start again, close the link +// or let a terminal take the session while any of it is out), and whoever says why. + +export interface OutstandingInput { + /** The team as its tree has it: the main agent (`lead`) and every live subagent, with its state. */ + agents: { name: string; state: string }[] + /** The session's background jobs that are not a team's members (those count once, by their + * state in `agents`): `kind` and `state` as in JobInfo. */ + jobs: { id: string; kind: string; state: string }[] +} + +export interface Outstanding { + /** Who still works for the session and will report: the subagents working, waiting for a slot + * or in their last turn (`paused`), by name in tree order, then the background `task` + * subagents still running, by job id. */ + subagents: string[] + /** The background commands still running, by job id. */ + commands: string[] + /** A schedule's run does not end yet: a subagent is out, or a report waits for the main agent + * (it is `waiting` for a slot). Background commands do not hold a run: one may never end. */ + run_waits: boolean + /** The session is not idle, though no turn runs: anything above. */ + has_work: boolean +} + +const LEAD = "lead" +const OUT = new Set(["working", "waiting", "paused"]) + +export function outstanding(o: OutstandingInput): Outstanding { + const members = o.agents.filter((a) => a.name !== LEAD && OUT.has(a.state)).map((a) => a.name) + const running = o.jobs.filter((j) => j.state === "running") + const subagents = [...members, ...running.filter((j) => j.kind === "subagent").map((j) => j.id)] + const commands = running.filter((j) => j.kind === "command").map((j) => j.id) + const run_waits = subagents.length > 0 || o.agents.some((a) => a.name === LEAD && a.state === "waiting") + return { subagents, commands, run_waits, has_work: run_waits || commands.length > 0 } +} + +/** Why a session with work out is not let go of, in words a person at a terminal reads. */ +export function outstandingReason(out: Outstanding): string | undefined { + if (!out.has_work) return undefined + const parts: string[] = [] + if (out.subagents.length) parts.push(`its subagent${out.subagents.length === 1 ? "" : "s"} still work${out.subagents.length === 1 ? "s" : ""} (${out.subagents.join(", ")})`) + else if (out.run_waits) parts.push("a subagent's report is about to start its next turn") + if (out.commands.length) parts.push(`${out.commands.length === 1 ? "a background command still runs" : "background commands still run"} (${out.commands.join(", ")})`) + return `this session is not idle in the service: ${parts.join(", and ")}. A terminal cannot take them over — wait for them to finish, or stop them in the web UI (the Team panel, the job strip), then try again` +} diff --git a/tests/conformance.ts b/tests/conformance.ts index 2915766..52de8c7 100644 --- a/tests/conformance.ts +++ b/tests/conformance.ts @@ -40,6 +40,7 @@ import { isSilent, judgeCase, monitorHash, notifyInstruction, reportResult, repo import { clientModel, records, startModel, type SessionKind } from "../src/session/lastmodel.ts" import { reattach } from "../src/acp/turns.ts" +import { outstanding, type OutstandingInput } from "../src/session/outstanding.ts" import { background, scheduledRefusal, twin, type BackgroundCall, type Listed } from "../src/provider/interactive.ts" type Case = Record @@ -57,6 +58,8 @@ function readableUnder(grants: Record | undefined, path: string) } export const AREAS: Record unknown> = { + // Spec 3.10.0: what a session still has out when no turn of it runs (src/session/outstanding.ts). + outstanding: (c) => outstanding(c as unknown as OutstandingInput), // Spec 3.10.0: a row reattached after a restart or a dropped link — the client's rule (src/acp/turns.ts). reattach: (c) => reattach({ turn: c.turn as string, machine: c.machine === true, status: c.status as { turnId?: string; queued?: string[]; lastTurnId?: string } }), // Spec 3.9.0: the model last used in a directory (src/session/lastmodel.ts).