diff --git a/apps/staged/docs/note-delivery.md b/apps/staged/docs/note-delivery.md index d2f6e3d96..52fd28152 100644 --- a/apps/staged/docs/note-delivery.md +++ b/apps/staged/docs/note-delivery.md @@ -76,13 +76,24 @@ then call write_note again.`` on both; the cause is the platform's own message. Each call republishes the file; the last call of the turn wins. The result -text is `Saved note "" (<n> bytes from the draft file). The note -is stored; end your turn now without repeating it in your reply.` (`<n>` is +text is `Saved note "<title>" (<n> bytes from the draft file). If the user +asked you a question this turn and you have not answered it in chat yet, +answer it now, briefly. Otherwise send a simple "Done" message with no other +text to end your turn, rather than summarising the work done.` (`<n>` is the body's UTF-8 length on both transports, which is why the text says -bytes and not characters.) The message -writer treats a completed `write_note` call like the old separator: the -session finishes after the response that follows it instead of holding for -background work. +bytes and not characters.) The text is word for word the same on both +transports (`note_tool::saved_message`, `savedMessage` in +`staged-mcp.cjs`). The tool description ends with the same request: send +"Done" after the saved result, and answer a question before making the +call. + +The call does not end the turn. Neither MCP nor ACP lets a tool declare +that it ends one, so the agent always gets one more response after the +result. The wording asks for that response to be the one word "Done", and +the chat pane hides it (_Ending a saved turn: the hidden "Done"_ below). +The message writer still treats a completed `write_note` call like the old +separator: the session finishes after the response that follows it instead +of holding for background work. That response is now the "Done" message. No call, no note. A draft that changed without a `write_note` call is never stored, whatever it holds: the call is the one signal that the note is @@ -134,7 +145,10 @@ once and states the contract in one sentence: "Your note draft file is call `write_note`." It adds the amendment rule ("If the file already exists, it holds the note as last saved: Read it and edit only what changes instead of rewriting it"), and says that assistant text is not saved and that the -call takes no body. One wording serves a fresh note and an amendment because +call takes no body. Its last paragraph asks for the ending: once the tool +reports the note is saved, send a simple "Done" message with no other text, +which Staged hides, and answer a user's question in the reply before the +call. One wording serves a fresh note and an amendment because the seed rule above decides whether the file exists. When the path is the `$HOME/…` spelling of a workstation whose home could not be resolved (see _Remote (Blox) sessions_), the guidance adds that `$HOME` there is a @@ -196,24 +210,246 @@ trip is accepted; the rejected empty call is what the wording avoids. The Goose wording is unchanged. In the chat pane a `write_note` call (`mcp__staged__write_note` from Claude, -`staged: write note` from Goose) is not shown as a generic "Ran" row: it gets -its own "Saved note" / "Saving note" / "Note rejected" row named after the -title, with the title and the suggested next steps in its details -(`noteToolCall.ts`, `NoteToolDetails.svelte`). The row has no body preview: -the call carries no body, and the stored note is where the body is read. A -`body` on a row recorded before this change is ignored. Tool calls still `pending` or -`in_progress` when the session has settled render by the session's outcome -(`AcpTranscriptOptions`): in a session that ended `error` or `cancelled` -they were cut off and read "Interrupted" in the collapsed header as well as -in the status; in a session that ended `completed` — a remote note session -completed from the workstation outbox — their work stood, so the `write_note` -row reads "Saved note" and other calls "Finished with the session". The -backend does not rewrite their status; the transcript builder derives it -from the session status. +`staged: write note` from Goose, `mcp.staged.write_note` from Codex) is not +shown as a generic "Ran" row. A call that saved the note is shown as the +note card, "Note created" or "Note updated", in the row's place (_The note +card_ below). Every other call gets its own row named after the title, with +the title and the suggested next steps in its details (`noteToolCall.ts`, +`NoteToolDetails.svelte`): "Saving note" while the call runs, "Note +rejected" when the tool refused it, and "Saved note" for the one saved call +that stays a row, a final-turn call that did not change the stored note. +The row has no body preview: the call carries no body, and the stored note +is where the body is read. A `body` on a row recorded before this change is +ignored. + +Tool calls still `pending` or `in_progress` when the session has settled +render by the session's outcome (`AcpTranscriptOptions`): in a session that +ended `error` or `cancelled` they were cut off and read "Interrupted" in +the collapsed header as well as in the status; in a session that ended +`completed` — a remote note session completed from the workstation outbox — +their work stood, so other calls read "Finished with the session" and the +`write_note` call resolves to `completed`. It is then a saved call like any +other: the note card, or the "Saved note" row under the final-turn rule. +The backend does not rewrite their status; the transcript builder derives +it from the session status. Source: `src-tauri/src/note_tool.rs` (schema, handler, in-process server), `src-tauri/src/note_draft.rs` (paths, validation, error text, seed). +## Ending a saved turn: the hidden "Done" + +A saved `write_note` call leaves the agent one more response, and Claude +spent it on a recap of the note it had just published. The texts the agent +reads about the tool now ask for one word in its place, and the chat pane +hides that word. A saved turn therefore shows the note card and nothing +under it. No hook, cancel or driver change is involved, and the mechanism +is the same for Claude, Goose and Codex. + +The request is made in three places: + +| Text | Where | What it asks | +| ---------------- | ----------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- | +| Tool result | `note_tool::saved_message`, `savedMessage` in `staged-mcp.cjs` | Answer the user's question now if it is still unanswered. Otherwise send a simple "Done" message with no other text. | +| Tool description | The `#[tool]` attribute, `WRITE_NOTE_DESCRIPTION` and the script's copy | Send "Done" after the saved result. Answer a question before making the call. | +| Session prompt | The last paragraph of `session_commands::note_delivery_guidance` | The same request, and that Staged hides the message. | + +The result text carries the "answer it now" clause on purpose. In live runs +a wording that only said "answer before the call, then Done" lost the +answer in 8 of 38 turns where the user had asked a question. This wording +lost none in 27. A reply the agent writes after the call is real text, and +the pane shows it under the card. + +Nothing forces the agent to comply. A turn that ignores the wording renders +as it did before the change, with its text shown, and is counted by the +drift log (_The drift log_ below). + +The hiding rule is a pure module, `noteDoneMessage.ts`. + +`isNoteDoneText` decides what a bare "Done" is. It trims the text, removes +one pair of wrapping `**`, `*`, `_`, backticks or quotes, removes one +trailing `.` or `!`, and compares the rest with `done`, ignoring case. So +`Done`, `Done.`, `done` and `**Done**` match. `Done ✅`, a sentence that +starts with the word, and an answer whose last line is `Done` do not. + +`hiddenNoteDoneMessageIds` takes the transcript groups and returns the +assistant messages to hide. A message is hidden only when all of these +hold: + +1. **A saved call comes directly before it.** The nearest earlier group is + a tool group whose last `write_note` item resolved to `completed`. The + item is found by tool name (`writeNoteToolCall`), not by its label, and + the status is the resolved one. A rejected, running or interrupted call + hides nothing, and a remote call completed from the outbox counts as + saved. Other items in the group do not matter: Codex completes a review + item after the call, and Claude sometimes issues a shell call beside it. +2. **Only such messages follow the call.** Every assistant message between + that tool group and the end of the turn is a bare "Done". A background + task that wakes the session can add a second one, and both are hidden. + A recap after the first shows both messages. +3. **The turn ends there.** What follows is the next user message or the + end of the transcript, not another tool call. + +The rule fails open. On any mismatch the message renders whole, exactly as +the agent wrote it, and nothing is trimmed from it. The worst case is a +visible "Done". + +The row stays stored. `SessionChatPane` derives `renderedGroups` from the +transcript groups and leaves a hidden group out of the list. It does not +render the group empty, because an empty group would take a second gap in +the column and be picked up as a scroll anchor. The rule runs on every +rebuild, so a message that grows past the word while streaming comes back +with all of its text. + +The stored row is still read elsewhere, and none of these readers filters +it: + +- **Unread chat and note freshness.** The backend queries behind a note + row's chat button count visible assistant rows newer than the note + (`VISIBLE_ASSISTANT_FILTER`), and they count a "Done" row. A turn that + changed the note stores it after the turn, so the note is newer than its + own "Done" and nothing changes. A turn that republished an identical + note keeps the note's `updated_at`, so its "Done" is newer than the note + and the chat reads as unread, as the recap row did before. The forced + republish after a chat answer was the main source of an identical + republish, and the fallback rule removed it (_Fallback: no `write_note` + call_ below). +- **`wait_for_repo_session`.** It returns the last assistant message as + `output` beside the note. A project agent reads "Done" there in place of + a recap. The note itself is in the same payload. +- **The live activity hint.** It shows the latest assistant text, so it can + read "Done" for the moment between the message and the end of the turn. + +Remote sessions follow the same rule over the same rows. The script's +result text is compared with the laptop's by test, and `SERVER_VERSION` is +7 for this wording (see [`staged-mcp-on-blox.md`](./staged-mcp-on-blox.md)). +The upload is gated on a checksum, so a workstation gets the new text on +its next session, together with the app that hides the word. + +### The note card + +The chat pane marks each point where a note was delivered with a card: +"Note created" for the first delivery in the transcript and "Note updated" +for every later one. `listNoteDeliveries` in `noteDeliveries.ts` decides +where they go, from the transcript groups alone. Nothing in the backend +records "the note was updated". A delivery has one of two shapes: + +- **A `write_note` call that resolved to `completed`.** This is the tool + path. The card is drawn in place of the call's tool row, since a "Saved + note" row with a card under it would say the same thing twice. The call + is found by tool name and by its resolved status, as in the hiding rule. +- **An assistant message that holds the `---` separator.** This is the + shape of a run with no tool attached and of every session recorded before + the tool. The card sits under the text above the separator, and reads + "Writing note" while the session runs and the message is the last one in + the transcript. A call is never "Writing note": its "Saving note" row + says so while it runs. + +The first delivery is "Note created" across both shapes, so a session that +began on the separator and continued on the tool has one "Note created". + +A call that was rejected, interrupted or is still running is not a +delivery. It keeps its tool row, with its title and details. A rejected +call followed by a retry shows a "Note rejected" row and then the card. Two +saved calls in a row are two cards: `groupRichToolsByVerb` takes a +predicate for the items the pane renders on its own, so they do not fold +into one collapsed "Saved note" row. + +**The final-turn rule.** One saved call is not a card. The rule applies in +the final turn of a session that is no longer running, when the pane is +opened on its note. There a saved call is a card only when `updatedAt` on +the note is at or after the call row. The note is stored after the turn, so +a turn that changed it stamps it later than its own call. An older note was +not changed by that call: the agent republished an identical note, which +keeps its timestamp, or the run failed before the note was stored. That +call keeps its "Saved note" row. In a completed session the "Ask for the +note to be updated" button shows under it, because `shouldAskForNoteUpdate` +reads the same timestamp. The card and the button therefore never show +together for one turn. + +The final turn is everything after the last user message, as in the +transcript builder. Calls of earlier turns are not compared, because a +later turn has moved the note on since. A pane with no note context +compares nothing, and every saved call there is a card. + +The comparison waits for the session to settle. The note cannot be stored +before the run ends, and a run can wait on background work after the call. +A strict reading would hold the row for the whole turn and swap it for the +card at the end. So while the session runs, a saved call is a card as soon +as it completes. + +### The drift log + +A model that stops following the wording goes back to writing a recap under +the card, and no test or error says so. After each settled turn of a note +session the run loop therefore calls +`note_done::log_settled_turn_note_done_drift`. It logs one line at info +level when the turn saved a note and did not end in a bare "Done": + +```text +Session <id>: note-done drift: the turn saved a note and did not end in a bare "Done": 1 message after the saved write_note call, the last 742 chars +``` + +The line holds counts and lengths only, never the text. After the fixed +phrase `note-done drift` it says what followed the last saved call: + +| Ending of the line | What happened | +| -------------------------------------------------------------------- | ----------------------------------------------------------------------------- | +| `no message after the saved write_note call` | The turn ended at the call, with no closing message. | +| `<n> message(s) after the saved write_note call, the last <m> chars` | Text followed the call. `<m>` is the trimmed length of the last message. | +| `…, ending in a "Done" line` | The last message is a longer answer that the agent closed with a `Done` line. | +| `…, with a tool call after the text` | A tool call came after the closing text, so the turn did not end there. | + +The decision is `note_done_drift`, a pure function over the turn's stored +rows. It follows the pane's rule: + +- The turn is everything after the last user row. A message steered in + after the call starts a new turn, and what the agent wrote after it is + not checked. +- The turn saved a note when the run's submission slot is filled and the + turn holds a `write_note` call. The slot alone outlives a turn, so on its + own it would match a repair turn that never called the tool. +- After the last such call the tool rows beside it are stepped over. Then + at least one assistant message must follow, each must be a bare "Done", + and the turn must end there. + +The call sits after the outbox collection, so a remote note is in the slot, +and before the Pikchr check, so a turn that goes on to a repair is counted. +The log and the pane differ in one rare case: a saved call followed by a +rejected one in the same turn. The pane then shows the closing word and the +log reports nothing. + +**What a rising count means.** Count the lines that hold `note-done drift` +and read the count against the number of saved turns. A single line is not +an error. The result text itself asks for an answer in place of "Done" when +a user's question is still open, and that turn is logged like any other. A +steady low count is those turns. A count that rises with no change in how +people use note sessions means the wording has stopped working: an agent or +a new model is writing after the saved call again, and users see a recap +under the card. The lengths tell the two apart, since an answer is short +and a recap ran to a median of 186 words in stored sessions. When the count +rises: + +- Run the ignored real-agent test against each bridge (_How to test + manually_ below) to see which agent stopped complying. +- Change the wording in all three texts together. `noteDoneWording.test.ts` + fails when one side changes alone or quotes a word the filter does not + accept. +- If no wording holds for Claude, add the `PostToolUse` hook that was + planned as the hard stop and is held in reserve. The wording and the + filter stay in place under it. + +The log and the filter cannot disagree about what "Done" is or which call +is `write_note`. `note_done/rules.rs` holds Rust copies of the pane's +`isNoteDoneText` and `isWriteNoteToolName`, with JavaScript's white space +set for the trim. Both languages run the same case table, +`src/lib/features/sessions/testStubs/noteDone.cases.json`, so a change to +one side alone fails a test. + +Source: `src/lib/features/sessions/noteDoneMessage.ts` (the hiding rule), +`src/lib/features/sessions/noteDeliveries.ts` (the card), +`src-tauri/src/note_done.rs` and `src-tauri/src/note_done/rules.rs` (the +drift log). + ## Local and project sessions Locally the server is an in-process rmcp streamable-HTTP server bound to @@ -467,7 +703,8 @@ fills. From there it feeds, with no further round trip: - the "did this turn deliver a note" decision behind the `write_note` follow-up prompt, so a remote turn that did call the tool is never asked to call it again — the follow-up fires only when the outbox has no `done` - for this turn _and_ the `---` fallback found nothing; + for this turn _and_ the `---` fallback found nothing _and_ the note has + no stored body (see _Fallback: no `write_note` call_); - the post-completion hooks (`resolve_note_title_and_body`, `ingest_note_media`), unchanged. @@ -654,10 +891,11 @@ read had just put in the slot, which the loop never checked. A quit that lands during the read is not a Stop: the run settles from the read, and the sweep recovers a parked row after the relaunch. -A failed read inside the loop is not final while the `write_note` -follow-up has an attempt left: the turn counts as one that delivered no -note, the follow-up runs, and its turn is read again. The rule above -applies to the read after the run's last turn. +A failed read inside the loop is not final while a `write_note` follow-up +is due (a note with no stored body, and an attempt left): the turn counts as +one that delivered no note, the follow-up runs, and its turn is read again. +An amendment gets no follow-up, so its failed read is the run's last. The +rule above applies to the read after the run's last turn. The outbox is never cleared at start, which is what lets that recovery tell an uncollected note from an old one. @@ -772,13 +1010,20 @@ Two mechanisms cover that: Locally, a `write_note` call fills the slot while the turn is still running, and the message writer's `should_finish_after_response` ends a background -hold as soon as the response after it completes. Remote sessions have no such +hold as soon as the response after it completes. That response is the +closing "Done" message, or the answer the result text asks for in its +place. On the Claude bridge the finish is taken at a successful result with +non-empty final text (`response_completion.rs`), which the one word +supplies. Remote sessions have no such mid-turn signal: the outbox is only read after the turn settles, and the prompt tells the agent not to write `---` text, so `should_finish_after_response` never fires for them. A remote note session under a background hold therefore runs to the hold's quiescence timeout (or its cap) even when the note was saved early. Polling the outbox during the turn would cost a `ws exec` per -poll; this is a known limitation, not an oversight. A hold the user _stops_ +poll; this is a known limitation, not an oversight. Background work that +wakes the agent during such a hold can draw a second "Done" message after +the first. The chat pane hides a run of them (_Ending a saved turn: the +hidden "Done"_ above). A hold the user _stops_ before the agent has written is a different matter: the agent carries on, and its late note is picked up by the sweep (see _Polling a hold-stopped row_ below). @@ -799,13 +1044,29 @@ never routes file or terminal access through the laptop. ## Fallback: no `write_note` call If a note session's turn ends with no `write_note` call (and no `---` -separator text, see below), the session runner sends one follow-up prompt -(`note_write_followup_prompt`): "Write or finish the draft at `<path>` (the -full note body in markdown, without the title), then call the `write_note` -tool with `title` and `suggested_next_steps`". A draft the agent wrote but -never published is covered by the same prompt: it only has to call the -tool. It does this once; a second silent turn stores the empty note as -before, so a session cannot loop. +separator text, see below) and the note has no stored body yet, the session +runner sends one follow-up prompt (`note_write_followup_prompt`): "Write or +finish the draft at `<path>` (the full note body in markdown, without the +title), then call the `write_note` tool with `title` and +`suggested_next_steps`". A draft the agent wrote but never published is +covered by the same prompt: it only has to call the tool. It does this once; +a second silent turn stores the empty note as before, so a session cannot +loop. + +The follow-up is a guard against storing an empty note, so a note that +already has a stored body gets none (`note_write_followup_due`, +`stored_note_has_body`). A turn there that publishes nothing is a chat +answer, or an amendment the agent chose not to make. The post-completion +hooks keep the stored note for it, and its `updated_at` does not move. The +chat pane reads that as a note older than the chat and offers "Ask for the +note to be updated" (`shouldAskForNoteUpdate`); the user decides whether the +answer belongs in the note. A forced republish would cost an agent turn for +every chat question and restamp a note that did not change. "Has a stored +body" is the reading the draft seed uses (`stored_note_seed`): a run whose +draft was seeded with the stored note is a run that gets no follow-up. One +gap is accepted: an agent that edits a seeded draft and never publishes it +is not prompted, and the next run seeds the draft from the stored note +again. A run with no `write_note` tool attached (the local server did not start, or the Staged MCP on Blox could not be installed) gets a different @@ -1185,7 +1446,48 @@ them by accident: sessions, with the commit detection and media ingest that come with them; nothing asked for that. Revisit only if users start pressing Stop _because_ the note is saved and the agent is still running — the chat - pane's "Saved note" row makes that moment visible. + pane's note card makes that moment visible. The moment is short now. The + turn ends with the "Done" message about 2 s after the call (2.3 s + measured on Fable, 1.4 s on Opus and Sonnet), where the recap took 4.5 s + in the same runs and 7 s in stored sessions. +- **A saved turn ends with a hidden "Done", not a hard stop.** Neither MCP + nor ACP lets a tool end the turn, so a real stop needs a mechanism per + agent: a Claude `PostToolUse` hook, a `session/cancel` for Goose, nothing + for Codex, and driver changes so a hook-stopped turn still finishes. + Asking for one word needs none of that, costs one short model call per + saved turn, and works for every agent. In live runs the text after the + last saved call was exactly `Done` in 146 of 146 Claude turns with no + user question, and in 4 of 4 turns each on Goose and Codex. It also keeps + a reply where the user asked for one, which a hard stop would cut. The + hook stays in reserve for Claude. Add it if the drift log shows the recap + coming back, or if the 2 s and the Stop window above prove to matter. +- **The "Done" is hidden when the pane renders, and the row is stored.** + The other choice was to drop the row in the message writer. A wrong match + there loses the text, where a later release can show a hidden row again. + The writer also sees only a call's raw status, and a remote call + completed from the outbox never gets `completed` there, so only the + transcript builder knows that call was saved. The cost is that backend + comparisons count the row (_Ending a saved turn: the hidden "Done"_). + Making them exact needs the dropped row or a per-row marker, which is a + data-model change. +- **A `Done` line at the end of a real answer is shown.** Four turns in the + live runs ended an answer with a last line `Done`. Stripping that line + would hide it at the cost of editing text the agent wrote, so the pane + shows the message whole and the drift log marks it in its line. +- **The hiding rule is keyed on the call, not on the text.** The old `---` + scan was a text test applied to every assistant message, and it had to + agree with a backend parser by convention. When delivery moved to the + tool the scan matched nothing and nothing failed, so the note card + disappeared unnoticed. The text test for "Done" runs only directly after + a completed `write_note` call, it fails open, and the word has one source + that tests on both sides read. +- **The note card replaces the saved call's row.** With "Done" hidden a + saved turn ends at the call. A card at the end of the turn would sit + directly under the "Saved note" row and repeat it, so the completed call + is the card. The final-turn rule keeps the row for a call that did not + change the stored note, so the card never claims an update that did not + happen and the "Ask for the note to be updated" button has a row to sit + under. - **A Stop the agent never acknowledges is a lost connection, not a Stop.** The user pressing Stop on a hung proxy is not a statement about the turn's output: it is the only button they have when the session looks stuck. @@ -1320,9 +1622,13 @@ Local: `note draft <temp dir>/staged-note-draft-<session-id>.md absent for a fresh note`. The agent writes that file with its file tool, then calls `write_note` with a title and next steps only. In the chat pane the call - shows as a "Saved note" row named after the title, with the title and the - suggested next steps in its details and no body preview; the note row - updates when the turn ends. After the run the draft file is gone. + shows as a "Saving note" row named after the title while it runs, with + the title and the suggested next steps in its details and no body + preview. When the call completes the row becomes the "Note created" + card. Nothing shows under the card: the agent's closing "Done" is + hidden, and the log has no `note-done drift` line for the session. The + note row updates when the turn ends. After the run the draft file is + gone. 2. Check the log for `note delivered via write_note (Tool)`. If you see the warn line about the `---` fallback, the agent ignored the tool: check the prompt reached it (`note_delivery_guidance` text, which names the draft @@ -1334,10 +1640,13 @@ fresh note`. The agent writes that file with its file tool, then calls and the stored note must be the repaired one. (Applies to remote sessions too: the outbox is collected after every settled turn, so the same correction prompt and re-collection happen there.) -4. Ask the agent (in the prompt) to not call any tools: after the turn, one - follow-up user message asking it to write or finish the draft and call - `write_note` must appear, and the note must be stored after the second - turn. +4. On a fresh note, ask the agent (in the prompt) to not call any tools: + after the turn, one follow-up user message asking it to write or finish + the draft and call `write_note` must appear, and the note must be stored + after the second turn. Then, on that stored note, ask a chat question + that needs no note change ("no need to change the note: …"): no + follow-up message must appear, the note's content and timestamp must be + unchanged, and the chat pane must offer "Ask for the note to be updated". 5. Local Claude amendment: on a Claude note session whose note is stored and holds an embedded image, send "Please update the note: add a short Risks section". The log shows `note draft … seeded with the stored note @@ -1356,7 +1665,38 @@ for amendment`, and before the agent's first tool call the file holds the draft to see the rejection: ask the agent, in a new session, to call `write_note` before writing anything. The tool result must read `write_note rejected: the draft file … does not exist …` with the - absolute path, and the agent must recover within the turn. + absolute path, and the agent must recover within the turn. The rejected + call keeps its "Note rejected" row, and the retry below it is the note + card. +7. The card and the button. On a stored note, ask for a change ("add a + short Risks section"): the new call shows as a "Note updated" card, the + first one still reads "Note created", and no "Ask for the note to be + updated" button shows. Then ask the agent to publish the note again + without changing it ("call `write_note` again with the draft as it is, + and the same title and next steps"). After the turn settles that call + shows as a "Saved note" row, not a card, the note's timestamp is + unchanged, and the button shows under the row. +8. A question in a note turn. On a stored note, ask for a change and a + chat answer in one message ("add the file name to the note, and tell me + here which line holds the bug"). The answer must be visible, above the + card or under it, and no bare "Done" may show. An answer under the card + is logged as drift, which is expected for this turn. +9. Drift. Ask the agent to "write a three-sentence summary in chat after + you save the note". The summary shows under the card, and the log holds + one line for the session that contains `note-done drift` and ends with + the count and the length, for example + `1 message after the saved write_note call, the last 412 chars`. A turn + that ends in the hidden "Done" logs no such line. +10. A real agent. From the repository root run + `cargo test -p acp-client real_bridge_saved_note_turn_ends_with_a_bare_done -- --ignored --nocapture --test-threads=1`. + It needs the bridge installed, a signed-in CLI, network and `node`. It + attaches the shipped `staged-mcp.cjs` with a temporary home directory, + asks for a short note without saying how to end the turn, and asserts + that every row after the saved call is a bare "Done". Set + `ACP_REAL_BRIDGE=codex` or `ACP_REAL_BRIDGE=goose` for the other + bridges (Goose under a headless runner also needs + `GOOSE_DISABLE_KEYRING=1`). Run it for each bridge when the drift count + rises or a new model ships. Blox: @@ -1371,7 +1711,9 @@ note draft at <path>` names the same file. After the `write_note` call, `done` holds 32 hex characters, and a second `write_note` call changes them. The commands before the agent starts are the ones that ran before the draft existed (the install probe, the outbox read, the clear); none was added - for the draft. + for the draft. The chat pane shows the "Note created" card with nothing + under it, as it does locally: the result text and the hidden "Done" are + the same on both transports. Goose amendment on Blox: on a Goose remote note session whose note is stored, send a follow-up asking for one added paragraph. Before the agent's first tool call, `cat ~/.staged/sessions/<session-id>/draft.md` @@ -1382,7 +1724,8 @@ note draft at <path>` names the same file. After the `write_note` call, `staged: write note` with a title and next steps only. The stored note has the new paragraph and every earlier section byte-for-byte. 3. Disconnect tests. Use a prompt that keeps the turn open after the note - is published, and wait for the "Saved note" row before each kill. A bare + is published, and wait for the note card ("Note created") before each + kill. A bare `sleep 120` does not do it on Claude: its harness blocks the long sleep, so the turn does not stay open. A shell loop of short sleeps works, for example "write the note, call `write_note`, then run @@ -1651,12 +1994,62 @@ when it honours it), `blox-cli` follow-up paths (`session_commands::tests`: every note prompt names the draft once with the amendment wording and no `body`; the grammar upload returns the workstation home from its last command), and on the frontend -`noteToolCall.test.ts` (title and next steps, no body), +`noteToolCall.test.ts` (title and next steps, no body, and the tool name in +each bridge's spelling from the shared case table), `NoteToolDetails.component.test.ts` (no body preview or "No note body" row), `toolCallViewModel.test.ts` / -`acpTranscript.test.ts` (the `write_note` row for both agents, interrupted -pending calls), `sessionModalHelpers.test.ts` (the detached, storing and unread-outbox copy and which +`acpTranscript.test.ts` (the `write_note` row for all three bridges, +interrupted pending calls, and the grouping predicate that keeps note cards +out of a collapsed group), `sessionModalHelpers.test.ts` (the detached, storing and unread-outbox copy and which end-of-session notice the chat pane renders), `acpTranscript.test.ts` (only the final turn's unsettled calls are relabelled in a completed session) and `sessionFailureCopy.test.ts` (the timeline copy says "yet" only while `isPollingForNote` is set). + +For the ending of a saved turn: `note_tool::tests` and +`session_commands::tests` pin the result text, the description's last +sentences and the last paragraph of the guidance in every note prompt. +`saved_result_text_is_identical_for_local_and_remote_sessions` +(`remote_staged_mcp::note_outbox::tests`) gives the local handler and the +script the same title and draft and compares the two result texts. +`noteDoneWording.test.ts` reads `note_tool.rs`, `staged-mcp.cjs` and +`session_commands.rs` as text: each result text, each description and the +session prompt must quote exactly one closing word, and it must be the +`NOTE_DONE_WORD` that `isNoteDoneText` accepts. `noteDoneMessage.test.ts` +runs `isNoteDoneText` over the shared case table and +`hiddenNoteDoneMessageIds` over groups built by `buildAcpTranscriptGroups`: +a match in each bridge's spelling, a rejected, running and interrupted +call, a tool call after the text, more text after "Done", a reply between +the call and "Done", an answer ending in a `Done` line, two "Done" +messages, Codex's extra item, a remote call completed from the outbox, +several turns, and another server's `write_note`. +`SessionChatPane.noteDone.component.test.ts` mounts the real pane and +asserts the message is absent for each bridge, present after a rejected or +interrupted call, and back when more text or a tool call follows it. + +For the note card: `noteDeliveries.test.ts` (each bridge's spelling, calls +that are not deliveries, a remote call completed from the outbox, a +rejected call and its retry, the created and updated labels, the separator +shape alone and mixed with calls, and a table for the final-turn rule +checked against `shouldAskForNoteUpdate`) and +`SessionChatPane.noteCard.component.test.ts` (the real pane, card and tool +row: the card for each bridge, the two labels, a reply under the card, the +kept row for a rejected and an interrupted call, two cards in a row, a +running call turning into the card, the text shape, and with a note context +the card without the button, the row with the button, the swap when the +note refreshes, and a running session that settles with the note +unchanged). + +For the follow-up rule and the drift log: `session_runner::tests::note_draft` +(a table for `note_write_followup_due`, and a chat-only turn on fresh and +amended branch and project notes) and `note_done::tests` (both rules over +the shared case table, the drift decision for each bridge's spelling, every +accepted and rejected text, a recap, a turn that ends at the call, an empty +slot, a turn with no call, earlier turns, a steered message, other tool +rows beside the call, a second "Done", a recap after "Done", an answer +ending in a `Done` line, a tool call after the text, two calls in one turn, +the wording of the log line, and three store-backed tests with rows the +real message writer stored). The ignored +`real_bridge_saved_note_turn_ends_with_a_bare_done` in `crates/acp-client` +holds a real agent to the same rules: it compiles `note_done/rules.rs` by +`#[path]`. diff --git a/apps/staged/docs/staged-mcp-on-blox.md b/apps/staged/docs/staged-mcp-on-blox.md index 33df5bd97..1afcb01d3 100644 --- a/apps/staged/docs/staged-mcp-on-blox.md +++ b/apps/staged/docs/staged-mcp-on-blox.md @@ -223,7 +223,8 @@ a rejected call, and the saved-note text counts UTF-8 bytes as the laptop does; version 5: the saved-note text calls that count bytes, not characters; version 6: `write_note` writes a random token into `done`, which was an empty file before, so the collector can tell two calls' markers -apart). +apart; version 7: the saved-note text and the tool description ask the agent +to close the turn with a bare "Done" message, which the chat pane hides). On the laptop: diff --git a/apps/staged/src-tauri/resources/remote/staged-mcp.cjs b/apps/staged/src-tauri/resources/remote/staged-mcp.cjs index bf5bd273f..2c4cb6229 100644 --- a/apps/staged/src-tauri/resources/remote/staged-mcp.cjs +++ b/apps/staged/src-tauri/resources/remote/staged-mcp.cjs @@ -60,7 +60,7 @@ const readline = require("readline"); // Reported as `serverInfo.version` and stamped by tools into what they write // (`write_note`'s `meta.json` keeps it under its original `helperVersion` key). -const SERVER_VERSION = 6; +const SERVER_VERSION = 7; const SERVER_NAME = "staged"; // MCP protocol revisions this server implements (newest first). `initialize` @@ -173,8 +173,9 @@ const WRITE_NOTE_DESCRIPTION = "Staged stores the current contents of your note draft file (the path is in your instructions) " + "as the note body, under the title you pass here, and does not read your assistant text. " + "Write the whole note body into the draft file first; calling again republishes the file, " + - "and the last call of the turn wins. After the result says the note is saved, end your turn " + - "without repeating the note."; + "and the last call of the turn wins. After the result says the note is saved, send a simple " + + '"Done" message with no other text to end your turn, rather than summarising the work done. ' + + "If the user asked you a question, answer it before making this call."; // Mirrors the schema rmcp derives from `WriteNoteParams`; the Rust test // `write_note_schema_matches_the_rust_tool_schema` checks property names and @@ -393,13 +394,18 @@ function normalizeSteps(raw) { } function savedMessage(title, bodyLength) { - // Same wording as `note_tool::saved_message` on the laptop. + // Must stay word-for-word in step with `note_tool::saved_message` on the + // laptop. The chat pane hides the quoted closing word after a saved call + // (`noteDoneMessage.ts`), so that word has to stay the one its filter + // accepts. return ( 'Saved note "' + title + '" (' + bodyLength + - " bytes from the draft file). The note is stored; end your turn now without repeating it in your reply." + " bytes from the draft file). If the user asked you a question this turn and you have not " + + "answered it in chat yet, answer it now, briefly. Otherwise send a simple " + + '"Done" message with no other text to end your turn, rather than summarising the work done.' ); } diff --git a/apps/staged/src-tauri/src/agent/writer.rs b/apps/staged/src-tauri/src/agent/writer.rs index 3c58fdcb3..182ef35a3 100644 --- a/apps/staged/src-tauri/src/agent/writer.rs +++ b/apps/staged/src-tauri/src/agent/writer.rs @@ -94,7 +94,7 @@ fn format_tool_call_content(title: &str, raw_input: Option<&serde_json::Value>) /// The title a stored tool call row was written with: the inverse of /// [`format_tool_call_content`]. -fn stored_tool_call_title(content: &str) -> String { +pub(crate) fn stored_tool_call_title(content: &str) -> String { serde_json::from_str::<serde_json::Value>(content) .ok() .and_then(|value| value.get("name")?.as_str().map(str::to_string)) diff --git a/apps/staged/src-tauri/src/lib.rs b/apps/staged/src-tauri/src/lib.rs index a969c52b1..eadb9be86 100644 --- a/apps/staged/src-tauri/src/lib.rs +++ b/apps/staged/src-tauri/src/lib.rs @@ -27,6 +27,7 @@ pub mod managed_node; mod mcp_progress; pub mod migrations; pub mod note_commands; +mod note_done; mod note_draft; mod note_media; pub mod note_tool; diff --git a/apps/staged/src-tauri/src/note_done.rs b/apps/staged/src-tauri/src/note_done.rs new file mode 100644 index 000000000..b0c4897bd --- /dev/null +++ b/apps/staged/src-tauri/src/note_done.rs @@ -0,0 +1,219 @@ +//! Whether a note session's turn ended the way the `write_note` result asks. +//! +//! A saved `write_note` call tells the agent to close its turn with the one +//! word "Done" (`note_tool::saved_message`), and the chat pane hides that +//! message (`hiddenNoteDoneMessageIds` in +//! `src/lib/features/sessions/noteDoneMessage.ts`). Nothing forces the agent +//! to comply: a model that stops following the wording goes back to writing a +//! recap under the note card, and nothing fails. So each settled turn that +//! saved a note and did not end in a bare "Done" is logged, as one line with +//! lengths only. A rising count of those lines means the wording has stopped +//! working. +//! +//! The decision is [`note_done_drift`], a pure function over the turn's +//! stored rows. It follows the chat pane's rule, so a logged turn is one +//! where the pane shows text after the saved call, or where the agent sent +//! no closing message at all: +//! +//! - what counts as "Done" is [`is_note_done_text`], the Rust copy of the +//! pane's `isNoteDoneText`; +//! - the call is found by tool name with [`is_write_note_tool_name`], the +//! Rust copy of the pane's `isWriteNoteToolName`. +//! +//! Both live in `note_done/rules.rs`. Each pair is held together by the case +//! table both test suites run, +//! `src/lib/features/sessions/testStubs/noteDone.cases.json`. + +mod rules; + +use std::fmt; + +use crate::note_tool::NoteSubmissionSlot; +use crate::store::{MessageRole, Store}; + +use rules::{is_note_done_text, is_write_note_tool_name, trim_js, NOTE_DONE_WORD}; + +/// One stored transcript row, as the drift check reads it. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct TurnRow<'a> { + pub role: MessageRole, + pub content: &'a str, +} + +/// How a turn that saved a note ended, when it did not end in a bare "Done". +/// It holds counts and lengths only, never the text. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) struct NoteDoneDrift { + /// Assistant messages after the last `write_note` call, to the end of + /// the turn. Zero when the turn ended at the call. + pub messages: usize, + /// Length in characters of the last of those messages, trimmed. Zero + /// when there is none. + pub last_message_chars: usize, + /// The last message is longer text whose final line is a bare "Done": + /// an answer the agent closed with the word. The pane shows it whole. + pub trailing_done_line: bool, + /// A tool call came after the first of those messages. The pane then + /// shows even a bare "Done", since the turn did not end there. + pub tool_call_after_text: bool, +} + +impl fmt::Display for NoteDoneDrift { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.messages == 0 { + return f.write_str("no message after the saved write_note call"); + } + write!( + f, + "{} {} after the saved write_note call, the last {} chars", + self.messages, + if self.messages == 1 { + "message" + } else { + "messages" + }, + self.last_message_chars + )?; + if self.trailing_done_line { + write!(f, ", ending in a \"{NOTE_DONE_WORD}\" line")?; + } + if self.tool_call_after_text { + f.write_str(", with a tool call after the text")?; + } + Ok(()) + } +} + +/// How the turn at the end of `rows` drifted from the ending the +/// `write_note` result asks for, or `None` when it did not, or saved no note. +/// +/// `rows` are a session's stored rows in order, ending with the turn that +/// just settled. The turn is everything after the last user row, as in the +/// chat pane, so a message steered into the turn starts a new one: what the +/// agent wrote after a steer answers the user, and is not checked. +/// +/// A turn saved a note when the run's submission slot is filled +/// (`slot_filled`) and the turn holds a `write_note` call. The slot is the +/// backend's record of a saved note. It covers a remote call whose result +/// never arrived, which the pane resolves from the outbox. The call row +/// places the save in this turn: the slot outlives a turn, so on its own it +/// would also match a repair turn that never called the tool. The pane +/// reads the call's own status instead, so the two differ in one rare case: +/// a saved call followed by a rejected one in the same turn. The pane then +/// shows the closing word and this reports nothing. +/// +/// From the last `write_note` call on, the rule is the pane's +/// (`hiddenNoteDoneMessageIds`): +/// +/// 1. The tool rows directly after the call belong to its tool group and are +/// stepped over. Codex completes a review item there, and Claude +/// sometimes issues a shell call beside the note call. +/// 2. Then comes a run of assistant messages. Each must be a bare "Done" +/// ([`is_note_done_text`]), and there must be at least one. +/// 3. The turn must end with that run, not at another tool call. +/// +/// Anything else is drift. An empty run is drift too: the pane has nothing +/// to hide, but the agent ended the turn without the message it was asked +/// for. +pub(crate) fn note_done_drift(slot_filled: bool, rows: &[TurnRow<'_>]) -> Option<NoteDoneDrift> { + if !slot_filled { + return None; + } + let turn = match rows.iter().rposition(|row| row.role == MessageRole::User) { + Some(last_user) => &rows[last_user + 1..], + None => rows, + }; + let call = turn.iter().rposition(|row| { + row.role == MessageRole::ToolCall + && is_write_note_tool_name(&crate::agent::writer::stored_tool_call_title(row.content)) + })?; + let after_call = &turn[call + 1..]; + let is_tool_row = + |row: &TurnRow<'_>| matches!(row.role, MessageRole::ToolCall | MessageRole::ToolResult); + let group_end = after_call + .iter() + .position(|row| !is_tool_row(row)) + .unwrap_or(after_call.len()); + let closing = &after_call[group_end..]; + let run_end = closing + .iter() + .position(|row| row.role != MessageRole::Assistant) + .unwrap_or(closing.len()); + let (run, rest) = closing.split_at(run_end); + + let ends_in_done = + !run.is_empty() && rest.is_empty() && run.iter().all(|row| is_note_done_text(row.content)); + if ends_in_done { + return None; + } + + let messages: Vec<&str> = closing + .iter() + .filter(|row| row.role == MessageRole::Assistant) + .map(|row| row.content) + .collect(); + let last = messages.last().copied().map(trim_js).unwrap_or_default(); + Some(NoteDoneDrift { + messages: messages.len(), + last_message_chars: last.chars().count(), + trailing_done_line: !is_note_done_text(last) + && last.lines().next_back().is_some_and(is_note_done_text), + tool_call_after_text: !rest.is_empty(), + }) +} + +/// [`note_done_drift`] for the turn of `session_id` that just settled, read +/// from the store. `turn_start_message_id` is the run's first prompt row; a +/// later turn of the run (the `write_note` follow-up, a Pikchr repair) starts +/// at its own stored prompt, which [`note_done_drift`] finds. +/// +/// A failed read reports nothing: this is a diagnostic, and the run goes on. +pub(crate) fn settled_turn_note_done_drift( + store: &Store, + session_id: &str, + turn_start_message_id: i64, + slot: &NoteSubmissionSlot, +) -> Option<NoteDoneDrift> { + if !crate::note_tool::has_submission(slot) { + return None; + } + let messages = match store.get_session_messages_since(session_id, turn_start_message_id, &[]) { + Ok(messages) => messages, + Err(e) => { + log::debug!( + "Session {session_id}: could not read the turn for the note-done check: {e}" + ); + return None; + } + }; + let rows: Vec<TurnRow<'_>> = messages + .iter() + .map(|message| TurnRow { + role: message.role.clone(), + content: &message.content, + }) + .collect(); + note_done_drift(true, &rows) +} + +/// Log one line when the turn of `session_id` that just settled saved a note +/// and did not end in a bare "Done" (see the module docs). The line starts +/// with a fixed phrase, `note-done drift`, so the lines can be counted. +pub(crate) fn log_settled_turn_note_done_drift( + store: &Store, + session_id: &str, + turn_start_message_id: i64, + slot: &NoteSubmissionSlot, +) { + if let Some(drift) = + settled_turn_note_done_drift(store, session_id, turn_start_message_id, slot) + { + log::info!( + "Session {session_id}: note-done drift: the turn saved a note and did not end in a \ + bare \"{NOTE_DONE_WORD}\": {drift}" + ); + } +} + +#[cfg(test)] +mod tests; diff --git a/apps/staged/src-tauri/src/note_done/rules.rs b/apps/staged/src-tauri/src/note_done/rules.rs new file mode 100644 index 000000000..281efa9d5 --- /dev/null +++ b/apps/staged/src-tauri/src/note_done/rules.rs @@ -0,0 +1,97 @@ +//! The two string rules the backend shares with the chat pane: what counts +//! as the closing word of a saved note turn, and which tool name is the +//! `staged` server's `write_note`. +//! +//! They are the Rust copies of `isNoteDoneText` +//! (`src/lib/features/sessions/noteDoneMessage.ts`) and `isWriteNoteToolName` +//! (`src/lib/features/sessions/noteToolCall.ts`), which decide what the chat +//! pane hides. Each pair is kept equal by a shared case table, +//! `src/lib/features/sessions/testStubs/noteDone.cases.json`, that both test +//! suites run (see `note_done/tests.rs`). +//! +//! The file uses nothing outside `std` and has no tests of its own, on +//! purpose: the real-bridge test in +//! `crates/acp-client/src/driver/tests/real_bridge.rs` compiles this same +//! file by path, so a live agent is held to the rules the app ships. Keep it +//! that way, and update that `#[path]` if the file moves. + +/// The closing message the `write_note` result asks for +/// (`note_tool::saved_message`, and `savedMessage` in `staged-mcp.cjs`). +pub(crate) const NOTE_DONE_WORD: &str = "Done"; + +/// Wrappers an agent may put around the word: Markdown emphasis, code, +/// quotes. `**` comes before `*`, so bold is taken off whole. +const WRAPPERS: [(&str, &str); 8] = [ + ("**", "**"), + ("*", "*"), + ("_", "_"), + ("`", "`"), + ("\"", "\""), + ("'", "'"), + ("“", "”"), + ("‘", "’"), +]; + +/// Whether JavaScript counts `c` as white space, in `String.prototype.trim` +/// and in a regular expression's `\s`. That is Unicode `White_Space` without +/// U+0085 and with the byte order mark, U+FEFF. +fn is_js_whitespace(c: char) -> bool { + (c.is_whitespace() && c != '\u{85}') || c == '\u{feff}' +} + +/// `text` without leading and trailing white space, as JavaScript's `trim` +/// removes it. +pub(crate) fn trim_js(text: &str) -> &str { + text.trim_matches(is_js_whitespace) +} + +/// Whether an assistant message's text is nothing but the closing word: +/// `Done`, in any case, optionally in one pair of wrappers and with one +/// trailing `.` or `!`. Anything more is real text: `Done ✅`, a sentence +/// that starts with the word, an answer whose last line is `Done`. +pub(crate) fn is_note_done_text(text: &str) -> bool { + let mut word = trim_js(text); + for (open, close) in WRAPPERS { + if word.len() >= open.len() + close.len() && word.starts_with(open) && word.ends_with(close) + { + word = &word[open.len()..word.len() - close.len()]; + break; + } + } + if let Some(stripped) = word.strip_suffix(['.', '!']) { + word = stripped; + } + word.to_lowercase() == NOTE_DONE_WORD.to_lowercase() +} + +/// Whether a tool call's title names the `staged` server's `write_note`, in +/// any of the spellings the bridges use: Claude Code's +/// `mcp__staged__write_note`, Goose's `staged: write note`, Codex's +/// `mcp.staged.write_note`, or a bare `staged.write_note`. The `mcp` prefix +/// is optional and the server must be `staged`, so another server's +/// `write_note` does not count. +pub(crate) fn is_write_note_tool_name(name: &str) -> bool { + // Runs of white space and hyphens become one underscore. + let mut normalized = String::with_capacity(name.len()); + let mut in_separator = false; + for c in trim_js(name).to_lowercase().chars() { + if is_js_whitespace(c) || c == '-' { + if !in_separator { + normalized.push('_'); + } + in_separator = true; + } else { + normalized.push(c); + in_separator = false; + } + } + let unprefixed = normalized + .strip_prefix("mcp__") + .or_else(|| normalized.strip_prefix("mcp.")) + .unwrap_or(&normalized); + unprefixed.strip_prefix("staged").is_some_and(|rest| { + ["__", ":_", "_", "."] + .iter() + .any(|separator| rest.strip_prefix(separator) == Some("write_note")) + }) +} diff --git a/apps/staged/src-tauri/src/note_done/tests.rs b/apps/staged/src-tauri/src/note_done/tests.rs new file mode 100644 index 000000000..20c603be2 --- /dev/null +++ b/apps/staged/src-tauri/src/note_done/tests.rs @@ -0,0 +1,598 @@ +use super::*; +use crate::agent::MessageWriter; +use crate::note_tool::{ + new_submission_slot, store_submission, NoteSubmission, NoteSubmissionSource, +}; +use crate::store::Session; +use std::sync::Arc; + +// --------------------------------------------------------------------------- +// The two rules shared with the chat pane +// --------------------------------------------------------------------------- + +/// The case table the chat pane's tests run too (`noteDoneMessage.test.ts` +/// and `noteToolCall.test.ts`). +const SHARED_CASES: &str = + include_str!("../../../src/lib/features/sessions/testStubs/noteDone.cases.json"); + +/// The `[label, input]` rows under `rule.verdict` in the shared table. +fn shared_cases(rule: &str, verdict: &str) -> Vec<(String, String)> { + let table: serde_json::Value = serde_json::from_str(SHARED_CASES).unwrap(); + let rows = table[rule][verdict] + .as_array() + .unwrap_or_else(|| panic!("{rule}.{verdict} is missing from the shared cases")); + assert!(!rows.is_empty(), "{rule}.{verdict} has no cases"); + rows.iter() + .map(|row| { + let field = |index: usize| { + row[index] + .as_str() + .unwrap_or_else(|| panic!("{rule}.{verdict}: {row} is not [label, input]")) + .to_string() + }; + (field(0), field(1)) + }) + .collect() +} + +#[test] +fn done_text_matches_the_chat_pane_on_every_shared_case() { + for (label, text) in shared_cases("doneText", "accept") { + assert!(is_note_done_text(&text), "should accept {label}: {text:?}"); + } + for (label, text) in shared_cases("doneText", "reject") { + assert!(!is_note_done_text(&text), "should reject {label}: {text:?}"); + } +} + +#[test] +fn write_note_tool_name_matches_the_chat_pane_on_every_shared_case() { + for (label, name) in shared_cases("writeNoteToolName", "accept") { + assert!( + is_write_note_tool_name(&name), + "should accept {label}: {name:?}" + ); + } + for (label, name) in shared_cases("writeNoteToolName", "reject") { + assert!( + !is_write_note_tool_name(&name), + "should reject {label}: {name:?}" + ); + } +} + +#[test] +fn the_word_the_tool_result_asks_for_is_a_bare_done() { + assert_eq!(NOTE_DONE_WORD, "Done"); + assert!(is_note_done_text(NOTE_DONE_WORD)); + // The saved result quotes the word: the request and the check cannot + // drift apart inside the backend either. + let result = crate::note_tool::saved_message("Plan", 10); + assert!( + result.contains(&format!("send a simple \"{NOTE_DONE_WORD}\" message")), + "{result}" + ); +} + +// --------------------------------------------------------------------------- +// The drift decision +// --------------------------------------------------------------------------- + +const CLAUDE_CALL: &str = r#"{"name":"mcp__staged__write_note","input":{"title":"Plan"}}"#; +const GOOSE_CALL: &str = r#"{"name":"staged: write note","input":{"title":"Plan"}}"#; +const CODEX_CALL: &str = r#"{"name":"mcp.staged.write_note","input":{"title":"Plan"}}"#; +const SHELL_CALL: &str = r#"{"name":"Bash","input":{"command":"git status"}}"#; +const SAVED: &str = "Saved note \"Plan\" (10 bytes from the draft file)."; + +fn user(content: &str) -> TurnRow<'_> { + TurnRow { + role: MessageRole::User, + content, + } +} + +fn assistant(content: &str) -> TurnRow<'_> { + TurnRow { + role: MessageRole::Assistant, + content, + } +} + +fn call(content: &str) -> TurnRow<'_> { + TurnRow { + role: MessageRole::ToolCall, + content, + } +} + +fn result(content: &str) -> TurnRow<'_> { + TurnRow { + role: MessageRole::ToolResult, + content, + } +} + +/// A drift of `messages` messages whose last is `last_message_chars` long. +fn text_drift(messages: usize, last_message_chars: usize) -> NoteDoneDrift { + NoteDoneDrift { + messages, + last_message_chars, + trailing_done_line: false, + tool_call_after_text: false, + } +} + +const NO_CLOSING_MESSAGE: NoteDoneDrift = NoteDoneDrift { + messages: 0, + last_message_chars: 0, + trailing_done_line: false, + tool_call_after_text: false, +}; + +#[test] +fn a_turn_that_ends_in_a_bare_done_is_not_drift() { + for (bridge, note_call) in [ + ("Claude Code", CLAUDE_CALL), + ("Goose", GOOSE_CALL), + ("Codex", CODEX_CALL), + ("a row that holds only the name", "mcp.staged.write_note"), + ] { + let rows = [ + user("Write the note"), + assistant("I will look at the code first."), + call(SHELL_CALL), + result("clean"), + call(note_call), + result(SAVED), + assistant("Done"), + ]; + assert_eq!(note_done_drift(true, &rows), None, "{bridge}"); + } +} + +#[test] +fn every_spelling_the_filter_hides_is_not_drift() { + for (label, text) in shared_cases("doneText", "accept") { + let rows = [user("Write the note"), call(CLAUDE_CALL), assistant(&text)]; + assert_eq!(note_done_drift(true, &rows), None, "{label}: {text:?}"); + } +} + +#[test] +fn every_text_the_filter_shows_is_drift() { + for (label, text) in shared_cases("doneText", "reject") { + let rows = [user("Write the note"), call(CLAUDE_CALL), assistant(&text)]; + let drift = note_done_drift(true, &rows) + .unwrap_or_else(|| panic!("{label} should be drift: {text:?}")); + assert_eq!(drift.messages, 1, "{label}"); + assert_eq!( + drift.last_message_chars, + trim_js(&text).chars().count(), + "{label}" + ); + assert!(!drift.tool_call_after_text, "{label}"); + } +} + +#[test] +fn a_recap_after_the_saved_call_is_drift_with_its_length() { + let recap = " I wrote the note. It covers the root cause and the fix.\n"; + let rows = [ + user("Write the note"), + call(CLAUDE_CALL), + result(SAVED), + assistant(recap), + ]; + assert_eq!( + note_done_drift(true, &rows), + Some(text_drift(1, recap.trim().chars().count())) + ); +} + +#[test] +fn the_length_counts_characters_not_bytes() { + let rows = [ + user("Write the note"), + call(CLAUDE_CALL), + assistant("Fertig ✅ é"), + ]; + assert_eq!(note_done_drift(true, &rows), Some(text_drift(1, 10))); +} + +#[test] +fn a_turn_that_ends_at_the_call_is_drift_with_no_message() { + // Text before the call is not the closing message. + let rows = [ + user("Write the note"), + assistant("Saving the note now."), + call(CLAUDE_CALL), + result(SAVED), + ]; + assert_eq!(note_done_drift(true, &rows), Some(NO_CLOSING_MESSAGE)); +} + +#[test] +fn an_empty_slot_is_never_drift() { + // The call was rejected, or the outbox held nothing: no note was saved, + // so the result never asked for the word. + let rows = [ + user("Write the note"), + call(CLAUDE_CALL), + result("The draft file is empty."), + assistant("The draft was empty, so I could not save the note."), + ]; + assert_eq!(note_done_drift(false, &rows), None); +} + +#[test] +fn a_turn_with_no_write_note_call_is_never_drift() { + // The slot outlives a turn. A Pikchr repair turn that never called the + // tool, or a chat answer, did not save anything itself. + let no_tool = [ + user("Why does the test pass?"), + assistant("Because it mocks the page size."), + ]; + assert_eq!(note_done_drift(true, &no_tool), None); + + let other_tools = [ + user("Fix the diagram"), + call(SHELL_CALL), + result("ok"), + assistant("I fixed the diagram in the draft."), + ]; + assert_eq!(note_done_drift(true, &other_tools), None); + + let other_server = [ + user("Write the note"), + call(r#"{"name":"mcp__other__write_note","input":{}}"#), + assistant("Saved it to the other server."), + ]; + assert_eq!(note_done_drift(true, &other_server), None); +} + +#[test] +fn only_the_last_turn_is_read() { + // An earlier turn's recap is not this turn's ending. + let rows = [ + user("Write the note"), + call(CLAUDE_CALL), + assistant("Here is a long recap of the note."), + user("Add a section on the empty case"), + call(CLAUDE_CALL), + result(SAVED), + assistant("Done"), + ]; + assert_eq!(note_done_drift(true, &rows), None); + + // And an earlier turn's "Done" does not excuse this one. + let rows = [ + user("Write the note"), + call(CLAUDE_CALL), + assistant("Done"), + user("Add a section on the empty case"), + call(CLAUDE_CALL), + assistant("Added the section."), + ]; + assert_eq!(note_done_drift(true, &rows), Some(text_drift(1, 18))); +} + +#[test] +fn a_message_steered_in_after_the_call_starts_a_new_turn() { + // What follows a steer answers the user. The pane shows it as a reply + // under the user's message, and it is not checked. + let rows = [ + user("Write the note"), + call(CLAUDE_CALL), + result(SAVED), + user("Which line holds the bug?"), + assistant("Line 42 of src/page.js."), + ]; + assert_eq!(note_done_drift(true, &rows), None); +} + +#[test] +fn rows_with_no_user_row_are_one_turn() { + let rows = [call(CLAUDE_CALL), result(SAVED), assistant("All saved.")]; + assert_eq!(note_done_drift(true, &rows), Some(text_drift(1, 10))); +} + +#[test] +fn other_tool_rows_beside_the_call_do_not_matter() { + // Codex completes a review item after the call; Claude sometimes issues + // a shell call in the same message. + let rows = [ + user("Write the note"), + call(CODEX_CALL), + call(r#"{"name":"Guardian Review","input":{}}"#), + result("approved"), + result(SAVED), + call(SHELL_CALL), + result("clean"), + assistant("Done"), + ]; + assert_eq!(note_done_drift(true, &rows), None); +} + +#[test] +fn a_second_done_from_a_background_wake_is_not_drift() { + let rows = [ + user("Write the note"), + call(CLAUDE_CALL), + result(SAVED), + assistant("Done"), + assistant("Done."), + ]; + assert_eq!(note_done_drift(true, &rows), None); +} + +#[test] +fn a_recap_after_done_is_drift() { + let rows = [ + user("Write the note"), + call(CLAUDE_CALL), + assistant("Done"), + assistant("One more thing: the test is flaky."), + ]; + assert_eq!(note_done_drift(true, &rows), Some(text_drift(2, 34))); + + // A reply between the call and the word shows both messages too. The + // last one is the bare word, so its length is the word's. + let rows = [ + user("Does the fix cover zero orders?"), + call(CLAUDE_CALL), + assistant("Yes, the empty page is returned."), + assistant("Done"), + ]; + assert_eq!(note_done_drift(true, &rows), Some(text_drift(2, 4))); +} + +#[test] +fn an_answer_that_ends_in_a_done_line_is_drift_and_says_so() { + let answer = "The bug is in `src/main.rs`.\n\nDone"; + let rows = [ + user("Where is the bug?"), + call(CLAUDE_CALL), + assistant(answer), + ]; + assert_eq!( + note_done_drift(true, &rows), + Some(NoteDoneDrift { + trailing_done_line: true, + ..text_drift(1, answer.chars().count()) + }) + ); + + // A wrapped or punctuated last line counts, as the bare message would. + let rows = [ + user("Where is the bug?"), + call(CLAUDE_CALL), + assistant("It is on line 42.\n**Done.**\n"), + ]; + assert!(note_done_drift(true, &rows).unwrap().trailing_done_line); + + // The word inside a last sentence does not. + let rows = [ + user("Where is the bug?"), + call(CLAUDE_CALL), + assistant("It is on line 42.\nAll done"), + ]; + assert!(!note_done_drift(true, &rows).unwrap().trailing_done_line); +} + +#[test] +fn a_tool_call_after_the_closing_text_is_drift() { + // The pane shows the word here: the turn did not end at it. + let rows = [ + user("Write the note"), + call(CLAUDE_CALL), + result(SAVED), + assistant("Done"), + call(SHELL_CALL), + result("clean"), + ]; + assert_eq!( + note_done_drift(true, &rows), + Some(NoteDoneDrift { + tool_call_after_text: true, + ..text_drift(1, 4) + }) + ); + + // Text after that tool call is the turn's last message. + let rows = [ + user("Write the note"), + call(CLAUDE_CALL), + assistant("Done"), + call(SHELL_CALL), + assistant("The tree is clean."), + ]; + assert_eq!( + note_done_drift(true, &rows), + Some(NoteDoneDrift { + tool_call_after_text: true, + ..text_drift(2, 18) + }) + ); +} + +#[test] +fn the_last_write_note_call_of_the_turn_is_the_one_that_counts() { + // A recap between two calls is not the ending: the agent republished + // and then closed the turn as asked. + let rows = [ + user("Write the note"), + call(CLAUDE_CALL), + result(SAVED), + assistant("I should add the edge case too."), + call(CLAUDE_CALL), + result(SAVED), + assistant("Done"), + ]; + assert_eq!(note_done_drift(true, &rows), None); + + // A rejected first call and a saved retry. + let rows = [ + user("Write the note"), + call(CLAUDE_CALL), + result("The title is empty."), + call(CLAUDE_CALL), + result(SAVED), + assistant("Saved on the second try."), + ]; + assert_eq!(note_done_drift(true, &rows), Some(text_drift(1, 24))); +} + +#[test] +fn the_log_line_names_what_followed_the_call() { + assert_eq!( + NO_CLOSING_MESSAGE.to_string(), + "no message after the saved write_note call" + ); + assert_eq!( + text_drift(1, 742).to_string(), + "1 message after the saved write_note call, the last 742 chars" + ); + assert_eq!( + NoteDoneDrift { + trailing_done_line: true, + ..text_drift(2, 90) + } + .to_string(), + "2 messages after the saved write_note call, the last 90 chars, ending in a \"Done\" line" + ); + assert_eq!( + NoteDoneDrift { + tool_call_after_text: true, + ..text_drift(1, 4) + } + .to_string(), + "1 message after the saved write_note call, the last 4 chars, with a tool call after \ + the text" + ); +} + +// --------------------------------------------------------------------------- +// Read from the store, with rows the real writer stored +// --------------------------------------------------------------------------- + +fn saved_slot() -> crate::note_tool::NoteSubmissionSlot { + let slot = new_submission_slot(); + store_submission( + &slot, + NoteSubmission { + title: "Plan".to_string(), + body: "The body.".to_string(), + suggested_next_steps: None, + source: NoteSubmissionSource::Tool, + }, + ); + slot +} + +/// A session with one prompt row; returns the store, the session id and the +/// prompt's row id, which is where the run's first turn starts. +fn session_with_prompt() -> (Arc<Store>, String, i64) { + let store = Arc::new(Store::in_memory().unwrap()); + let session = Session::new_running("write a note", std::path::Path::new(".")); + store.create_session(&session).unwrap(); + let turn_start = store + .add_session_message(&session.id, MessageRole::User, "Write a note") + .unwrap(); + (store, session.id, turn_start) +} + +/// Store a `write_note` call and its saved result through the real writer, +/// titled as `title`, then `closing` as the assistant message after it. +async fn write_saved_turn(store: &Arc<Store>, session_id: &str, title: &str, closing: &str) { + let writer = MessageWriter::new(session_id.to_string(), Arc::clone(store)); + writer.append_text("Writing the note.").await; + let input = serde_json::json!({ "title": "Plan" }); + writer.record_tool_call("call-1", title, Some(&input)).await; + writer.record_tool_result("call-1", SAVED).await; + if !closing.is_empty() { + writer.append_text(closing).await; + } + writer.finalize().await; +} + +#[tokio::test] +async fn a_stored_turn_is_read_as_each_bridge_titles_the_call() { + for title in [ + "mcp__staged__write_note", + "staged: write note", + "mcp.staged.write_note", + ] { + let (store, session_id, turn_start) = session_with_prompt(); + write_saved_turn(&store, &session_id, title, "Done").await; + assert_eq!( + settled_turn_note_done_drift(&store, &session_id, turn_start, &saved_slot()), + None, + "{title}" + ); + + let (store, session_id, turn_start) = session_with_prompt(); + write_saved_turn( + &store, + &session_id, + title, + "The note is saved. It covers the fix.", + ) + .await; + assert_eq!( + settled_turn_note_done_drift(&store, &session_id, turn_start, &saved_slot()), + Some(text_drift(1, 37)), + "{title}" + ); + + let (store, session_id, turn_start) = session_with_prompt(); + write_saved_turn(&store, &session_id, title, "").await; + assert_eq!( + settled_turn_note_done_drift(&store, &session_id, turn_start, &saved_slot()), + Some(NO_CLOSING_MESSAGE), + "{title}" + ); + } +} + +#[tokio::test] +async fn a_stored_turn_with_an_empty_slot_reports_nothing() { + let (store, session_id, turn_start) = session_with_prompt(); + write_saved_turn(&store, &session_id, "mcp__staged__write_note", "A recap.").await; + assert_eq!( + settled_turn_note_done_drift(&store, &session_id, turn_start, &new_submission_slot()), + None + ); +} + +#[tokio::test] +async fn a_later_turn_of_the_run_is_read_from_its_own_prompt() { + // The run's first turn ended with a recap. The automatic follow-up (or a + // Pikchr repair) is a stored user row, and its turn ended as asked. The + // check still starts at the run's first prompt. + let (store, session_id, turn_start) = session_with_prompt(); + write_saved_turn( + &store, + &session_id, + "mcp__staged__write_note", + "A long recap.", + ) + .await; + let slot = saved_slot(); + assert_eq!( + settled_turn_note_done_drift(&store, &session_id, turn_start, &slot), + Some(text_drift(1, 13)) + ); + + store + .add_session_message( + &session_id, + MessageRole::User, + "Fix the diagram and republish", + ) + .unwrap(); + write_saved_turn(&store, &session_id, "mcp__staged__write_note", "Done").await; + assert_eq!( + settled_turn_note_done_drift(&store, &session_id, turn_start, &slot), + None + ); +} diff --git a/apps/staged/src-tauri/src/note_tool.rs b/apps/staged/src-tauri/src/note_tool.rs index 772fa6940..97a94fbca 100644 --- a/apps/staged/src-tauri/src/note_tool.rs +++ b/apps/staged/src-tauri/src/note_tool.rs @@ -56,7 +56,8 @@ pub(crate) const WRITE_NOTE_DESCRIPTION: &str = "Publish the note this session i Staged stores the current contents of your note draft file (the path is in your instructions) as the note body, \ under the title you pass here, and does not read your assistant text. Write the whole note body into the draft \ file first; calling again republishes the file, and the last call of the turn wins. After the result says the \ -note is saved, end your turn without repeating the note."; +note is saved, send a simple \"Done\" message with no other text to end your turn, rather than summarising the \ +work done. If the user asked you a question, answer it before making this call."; /// Structured suggested next steps, the same shape as the former /// `suggested-next-steps` JSON block (camelCase keys). Both fields are @@ -176,10 +177,19 @@ impl NoteSubmission { /// The `write_note` result text. Shared with the Staged MCP on Blox's wording so /// the agent sees the same instruction on both transports. `body_len` is the /// body's UTF-8 length, so the text says bytes. +/// +/// The text asks the agent to close the turn with the one word "Done" in +/// place of a recap of the note, unless the user asked a question it has not +/// answered yet. The chat pane hides that word after a saved call +/// (`noteDoneMessage.ts`), so the quoted word here and the word the filter +/// accepts have to stay the same; `noteDoneWording.test.ts` reads this +/// function to check it. pub(crate) fn saved_message(title: &str, body_len: usize) -> String { format!( - "Saved note \"{title}\" ({body_len} bytes from the draft file). The note is stored; \ -end your turn now without repeating it in your reply." + "Saved note \"{title}\" ({body_len} bytes from the draft file). If the user asked you a \ +question this turn and you have not answered it in chat yet, answer it now, briefly. Otherwise \ +send a simple \"Done\" message with no other text to end your turn, rather than summarising the \ +work done." ) } @@ -250,7 +260,8 @@ impl NoteToolsHandler { Staged stores the current contents of your note draft file (the path is in your instructions) as the note body, \ under the title you pass here, and does not read your assistant text. Write the whole note body into the draft \ file first; calling again republishes the file, and the last call of the turn wins. After the result says the \ -note is saved, end your turn without repeating the note." +note is saved, send a simple \"Done\" message with no other text to end your turn, rather than summarising the \ +work done. If the user asked you a question, answer it before making this call." )] async fn write_note( &self, @@ -376,6 +387,15 @@ mod tests { Some(WRITE_NOTE_DESCRIPTION), "the #[tool] description must match the shared constant" ); + // How the turn ends after a saved call, word for word. + assert!( + WRITE_NOTE_DESCRIPTION.ends_with( + "After the result says the note is saved, send a simple \"Done\" message with no \ +other text to end your turn, rather than summarising the work done. If the user asked you a \ +question, answer it before making this call." + ), + "{WRITE_NOTE_DESCRIPTION}" + ); } #[test] @@ -491,11 +511,20 @@ mod tests { assert!(text.contains("Saved note \\\"First\\\""), "{text}"); assert!(text.contains("from the draft file"), "{text}"); assert!(text.contains("end your turn"), "{text}"); - // The count is the body's UTF-8 length, and the text says so. + // The handler's text is `saved_message` for the stored title and body. + let content = serde_json::to_value(&first.content).unwrap(); + assert_eq!( + content[0]["text"], + saved_message("First", "First body ![Shot](staged-media://abc.png)".len()) + ); + // The count is the body's UTF-8 length, and the text says so. The + // rest asks for a bare "Done" unless a question is still unanswered. assert_eq!( saved_message("Né", "é→".len()), - "Saved note \"Né\" (5 bytes from the draft file). The note is stored; end your \ -turn now without repeating it in your reply." + "Saved note \"Né\" (5 bytes from the draft file). If the user asked you a question \ +this turn and you have not answered it in chat yet, answer it now, briefly. Otherwise send a \ +simple \"Done\" message with no other text to end your turn, rather than summarising the work \ +done." ); let current = current_submission(&slot).unwrap(); assert_eq!(current.body, "First body ![Shot](staged-media://abc.png)"); diff --git a/apps/staged/src-tauri/src/remote_staged_mcp.rs b/apps/staged/src-tauri/src/remote_staged_mcp.rs index 6e45ca8fe..95071c0ef 100644 --- a/apps/staged/src-tauri/src/remote_staged_mcp.rs +++ b/apps/staged/src-tauri/src/remote_staged_mcp.rs @@ -522,7 +522,7 @@ mod tests { let init = &responses[0]["result"]; assert_eq!(init["serverInfo"]["name"], SERVER_NAME); - assert_eq!(init["serverInfo"]["version"], "6"); + assert_eq!(init["serverInfo"]["version"], "7"); assert_eq!(init["capabilities"], serde_json::json!({"tools": {}})); // The instructions name the session's draft file by absolute path: // the one spelling of it an agent that expands neither `~` nor diff --git a/apps/staged/src-tauri/src/remote_staged_mcp/note_outbox.rs b/apps/staged/src-tauri/src/remote_staged_mcp/note_outbox.rs index 3221e89d2..816ce1a33 100644 --- a/apps/staged/src-tauri/src/remote_staged_mcp/note_outbox.rs +++ b/apps/staged/src-tauri/src/remote_staged_mcp/note_outbox.rs @@ -2048,6 +2048,67 @@ mod tests { ); } + /// The saved result is the instruction that ends a note turn: it asks for + /// a bare "Done", which the chat pane hides. A local session gets the text + /// from `note_tool::handle_write_note` and a Blox session from the script, + /// so the two copies must read the same for the same title and draft, or + /// the two kinds of session end their turns differently. + #[test] + fn saved_result_text_is_identical_for_local_and_remote_sessions() { + let Some(node) = node() else { + eprintln!("node not available; skipping saved result text comparison"); + return; + }; + // Titles and drafts that would expose a difference in quoting, + // trimming or counting: the count is UTF-8 bytes of the trimmed body. + let cases = [ + ("Plan", "Body"), + ("# Plan with a heading mark", " Body line\n---\nmore \n"), + ("Fix \"quoted\" titles", "Body with \"quotes\" and a `tick`"), + ("Né: résumé → done", "é→ in the body, 日本語 too"), + (" Padded title ", "\u{feff}Body after a byte order mark"), + ]; + for (title, draft) in cases { + let home = tempfile::tempdir().unwrap(); + write_draft(home.path(), draft); + + let slot = crate::note_tool::new_submission_slot(); + let local = crate::note_tool::handle_write_note( + "sess-1", + &slot, + &home.path().join(".staged/sessions/sess-1/draft.md"), + serde_json::from_value(serde_json::json!({ "title": title })).unwrap(), + ); + assert_ne!(local.is_error, Some(true), "{title:?}: {local:?}"); + let local = serde_json::to_value(&local.content).unwrap(); + let local = local[0]["text"].as_str().expect("a text result"); + + let responses = run_script( + home.path(), + &node, + &[ + serde_json::json!({"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"write_note","arguments":{"title":title}}}), + ], + ); + assert_eq!(responses[0]["result"]["isError"], false, "{title:?}"); + let remote = responses[0]["result"]["content"][0]["text"] + .as_str() + .expect("a text result"); + + assert_eq!(remote, local, "title {title:?}, draft {draft:?}"); + // And both are the shared wording, not two copies that drifted + // the same way: the instruction after the "Saved note" sentence. + assert!( + local.ends_with( + " bytes from the draft file). If the user asked you a question this turn \ +and you have not answered it in chat yet, answer it now, briefly. Otherwise send a simple \ +\"Done\" message with no other text to end your turn, rather than summarising the work done." + ), + "{local}" + ); + } + } + #[test] fn write_note_schema_matches_the_rust_tool_schema() { let Some(node) = node() else { diff --git a/apps/staged/src-tauri/src/session_commands.rs b/apps/staged/src-tauri/src/session_commands.rs index cd598781b..7d95baa53 100644 --- a/apps/staged/src-tauri/src/session_commands.rs +++ b/apps/staged/src-tauri/src/session_commands.rs @@ -555,8 +555,10 @@ title.\n\ - If the note is pure research or informational with no clear next action: \ {{\"suggestedNextCommitStep\": null, \"suggestedNextNoteStep\": null}}\n\ \n\ -Once the tool reports the note is saved, end your turn without repeating the note. If the turn \ -ends without a `write_note` call, no note is saved, whatever the draft file holds." +Once the tool reports the note is saved, send a simple \"Done\" message with no other text to \ +end your turn, rather than summarising the work done. Staged hides that message. If the user \ +asked you a question, answer it in your reply before you call `write_note`. If the turn ends \ +without a `write_note` call, no note is saved, whatever the draft file holds." ) } @@ -14718,6 +14720,20 @@ it, and when it is ready call `write_note`." prompt.contains("Your assistant text is not saved"), "{prompt}" ); + // How the turn ends after a saved call: the guidance's last + // paragraph, word for word. It matches the tool's description and + // result text. + assert!( + prompt.contains( + "\n\nOnce the tool reports the note is saved, send a simple \"Done\" message with \ +no other text to end your turn, rather than summarising the work done. Staged hides that \ +message. If the user asked you a question, answer it in your reply before you call \ +`write_note`. If the turn ends without a `write_note` call, no note is saved, whatever the \ +draft file holds." + ), + "{prompt}" + ); + assert!(!prompt.contains("without repeating"), "{prompt}"); assert!(prompt.contains("suggestedNextCommitStep"), "{prompt}"); assert!(prompt.contains("`suggested_next_steps`"), "{prompt}"); assert!(!prompt.contains("```suggested-next-steps"), "{prompt}"); diff --git a/apps/staged/src-tauri/src/session_runner.rs b/apps/staged/src-tauri/src/session_runner.rs index e9260c765..3473417f3 100644 --- a/apps/staged/src-tauri/src/session_runner.rs +++ b/apps/staged/src-tauri/src/session_runner.rs @@ -2564,12 +2564,13 @@ pub fn start_session( // exactly as they would a local one — and a repair turn's // rewrite is picked up the same way on the next pass. A // collection failure is logged and, for the rest of the - // loop, treated like a turn that delivered nothing: the - // follow-up prompt gives the agent a second chance, and the - // read after that turn may go through. It is remembered, - // though: a run that would complete with its last turn's - // read failed does not store an empty note (see - // `completed_run_outbox_unread`). + // loop, treated like a turn that delivered nothing: on a + // note with no stored body the follow-up prompt gives the + // agent a second chance, and the read after that turn may + // go through. It is remembered, though: a run that would + // complete with its last turn's read failed does not store + // an empty note, or keep a stored one over a published + // amendment (see `completed_run_outbox_unread`). if let (Some(turn_id), Some(ws_name)) = (remote_note_turn.get(), config.workspace_name.clone()) { @@ -2626,6 +2627,23 @@ pub fn start_session( } } + // The `write_note` result asks the agent to close a saved + // turn with a bare "Done", which the chat pane hides. A turn + // that saved a note and ended any other way is logged, as + // one line with lengths only: a rising count means the + // wording has stopped working. This sits after the outbox + // collection so a remote note is in the slot, and before the + // Pikchr check so a turn that goes on to a repair is counted + // too. + if is_note_session { + crate::note_done::log_settled_turn_note_done_drift( + &store, + &config.session_id, + turn_start_message_id, + ¬e_slot, + ); + } + // Diagrams are checked where the note landed. With a // `write_note` tool attached and a published note in the slot // — filled locally by the tool, remotely by the outbox @@ -2650,14 +2668,20 @@ pub fn start_session( // `write_note` call (and without the `---` fallback) // gets one follow-up asking for the call before an // empty note is stored. Mirrors Buzz's stance: if it - // wasn't posted, it didn't happen. + // wasn't posted, it didn't happen. A note that + // already has a stored body gets none: the turn was + // a chat answer, and the hooks keep the note as it + // is (`note_write_followup_due`). if is_note_session - && note_followup_attempts < NOTE_WRITE_FOLLOWUP_MAX_ATTEMPTS - && !note_turn_delivered( - &store, - &config.session_id, - turn_start_message_id, - ¬e_slot, + && note_write_followup_due( + note_turn_delivered( + &store, + &config.session_id, + turn_start_message_id, + ¬e_slot, + ), + stored_note_has_body(&store, &config.session_id), + note_followup_attempts, ) { note_followup_attempts += 1; @@ -4077,6 +4101,33 @@ impl std::fmt::Display for AgentRunError { /// session cannot loop. const NOTE_WRITE_FOLLOWUP_MAX_ATTEMPTS: u32 = 1; +/// Whether a note session's completed turn is followed by the automatic +/// prompt asking for the note ([`note_write_followup_prompt`]). +/// +/// The follow-up exists so that an empty note is not stored. It is therefore +/// sent only when all three hold: +/// +/// - the turn delivered no note (`turn_delivered`, see +/// [`note_turn_delivered`]); +/// - the note has no stored body yet (`stored_note_has_body`, see +/// [`stored_note_has_body`]); +/// - the run has an attempt left (`attempts_used` against +/// [`NOTE_WRITE_FOLLOWUP_MAX_ATTEMPTS`]). +/// +/// A turn on a note that already has a body and that delivered nothing is a +/// chat answer, or an amendment the agent chose not to make. The +/// post-completion hooks keep the stored note for it, timestamp included, +/// which is what lets the chat pane offer to ask for the note to be updated. +/// Forcing a republish there would cost an agent turn and restamp a note +/// that did not change. +fn note_write_followup_due( + turn_delivered: bool, + stored_note_has_body: bool, + attempts_used: u32, +) -> bool { + !turn_delivered && !stored_note_has_body && attempts_used < NOTE_WRITE_FOLLOWUP_MAX_ATTEMPTS +} + /// Follow-up prompt for a note session whose turn ended without delivering /// a note. With a `write_note` tool attached (`note_draft_path` is its /// draft): write or finish the draft, then call the tool. With none attached @@ -4186,6 +4237,16 @@ fn stored_note_seed(store: &Store, session_id: &str) -> Option<String> { .and_then(crate::note_draft::seed_body) } +/// Whether the note this session writes already has a stored body: the +/// session is amending a note, not writing a fresh one. This is the reading +/// the draft seed uses ([`stored_note_seed`]), so a run whose draft was +/// seeded is a run that gets no automatic `write_note` follow-up +/// ([`note_write_followup_due`]). A note the start-of-run outbox recovery +/// stored counts, since it is stored by the time a turn ends. +fn stored_note_has_body(store: &Store, session_id: &str) -> bool { + stored_note_seed(store, session_id).is_some() +} + /// Put a local note session's draft in its run-start state, before the agent /// starts: seeded with the stored body for an amendment or a Pikchr repair, /// absent for a fresh note (a stale draft from an earlier run is removed). @@ -4619,10 +4680,12 @@ fn debug_string_fields(text: &str, prefix: &str) -> Vec<String> { /// turn's output). /// /// The turn loop carries on after a failed read as if nothing was delivered -/// (it sends the `write_note` follow-up while it has one), so a run can come -/// out `Completed` with an empty submission slot while a published note sits -/// on the workstation. Storing an empty note there would also leave the row -/// `completed`, with nothing looking for the note until a Resume. +/// (it sends the `write_note` follow-up when one is due, see +/// [`note_write_followup_due`]), so a run can come out `Completed` with an +/// empty submission slot while a published note sits on the workstation. +/// Storing an empty note there, or keeping the stored note of an amendment, +/// would also leave the row `completed`, with nothing looking for the note +/// until a Resume. fn completed_run_outbox_unread( result: &Result<AgentRunOutcome, AgentRunError>, stop_requested: bool, diff --git a/apps/staged/src-tauri/src/session_runner/tests/note_draft.rs b/apps/staged/src-tauri/src/session_runner/tests/note_draft.rs index de4358524..e013cc8e0 100644 --- a/apps/staged/src-tauri/src/session_runner/tests/note_draft.rs +++ b/apps/staged/src-tauri/src/session_runner/tests/note_draft.rs @@ -211,6 +211,7 @@ fn a_session_without_a_note_has_no_seed() { store.create_session(&session).unwrap(); assert_eq!(stored_note_seed(&store, &session.id), None); assert_eq!(remote_note_draft_seed(&store, &session.id), None); + assert!(!stored_note_has_body(&store, &session.id)); } /// Remote run start is the outbox recovery: collect, then clear. The draft @@ -326,6 +327,90 @@ fn a_failed_draft_seed_leaves_the_recovery_settled() { )); } +/// The automatic follow-up guards against storing an empty note, so it is +/// due only for a turn that delivered nothing on a note with no stored +/// body, and only while the run has its one attempt left. +#[test] +fn the_automatic_follow_up_is_due_only_for_an_undelivered_note_with_no_stored_body() { + // (turn delivered, stored note has a body, attempts used) -> due + let cases = [ + // A fresh note's silent turn: asked once, then left alone. + (false, false, 0, true), + (false, false, NOTE_WRITE_FOLLOWUP_MAX_ATTEMPTS, false), + // A chat answer on a note that has a body: the stored note stands. + (false, true, 0, false), + (false, true, NOTE_WRITE_FOLLOWUP_MAX_ATTEMPTS, false), + // A delivered note needs no prompt, fresh or amended. + (true, false, 0, false), + (true, true, 0, false), + (true, false, NOTE_WRITE_FOLLOWUP_MAX_ATTEMPTS, false), + (true, true, NOTE_WRITE_FOLLOWUP_MAX_ATTEMPTS, false), + ]; + for (turn_delivered, stored_note_has_body, attempts_used, due) in cases { + assert_eq!( + note_write_followup_due(turn_delivered, stored_note_has_body, attempts_used), + due, + "delivered={turn_delivered} stored_body={stored_note_has_body} \ + attempts={attempts_used}" + ); + } +} + +/// The decision as the run loop makes it, from the store: a turn that +/// answered in chat and called nothing. On a fresh note (a blank stored +/// body) the follow-up is sent; on an amended one it is not, so the hooks +/// keep the stored note and its timestamp. A branch note and a project note +/// are read the same way. +#[test] +fn a_chat_only_turn_gets_the_follow_up_on_a_fresh_note_and_none_on_an_amended_one() { + // (stored body, follow-up due) + let notes = [("", true), (" \n", true), (STORED_NOTE, false)]; + for project_note in [false, true] { + for (content, due) in notes { + let store = Arc::new(Store::in_memory().unwrap()); + let session_id = note_session(&store, project_note, content); + let turn_start = store + .add_session_message(&session_id, MessageRole::User, "Why does the test pass?") + .unwrap(); + store + .add_session_message( + &session_id, + MessageRole::Assistant, + "It never asks for a second page.", + ) + .unwrap(); + let slot = new_submission_slot(); + let follow_up = |attempts_used| { + note_write_followup_due( + note_turn_delivered(&store, &session_id, turn_start, &slot), + stored_note_has_body(&store, &session_id), + attempts_used, + ) + }; + + assert_eq!( + stored_note_has_body(&store, &session_id), + !due, + "project_note={project_note} content={content:?}" + ); + assert_eq!( + follow_up(0), + due, + "project_note={project_note} content={content:?}" + ); + // Fresh or amended, the run never asks twice. + assert!(!follow_up(NOTE_WRITE_FOLLOWUP_MAX_ATTEMPTS)); + + // A turn that did publish is not asked on either kind of note. + crate::note_tool::store_submission(&slot, tool_submission(VALID_NOTE_BODY)); + assert!( + !follow_up(0), + "project_note={project_note} content={content:?}" + ); + } + } +} + #[test] fn the_note_follow_up_and_pikchr_repair_name_the_draft_file() { let draft = "/tmp/staged-note-draft-sess-1.md"; diff --git a/apps/staged/src/lib/features/sessions/SessionChatPane.noteCard.component.test.ts b/apps/staged/src/lib/features/sessions/SessionChatPane.noteCard.component.test.ts new file mode 100644 index 000000000..cf7b62d6e --- /dev/null +++ b/apps/staged/src/lib/features/sessions/SessionChatPane.noteCard.component.test.ts @@ -0,0 +1,509 @@ +import { flushSync, mount, tick, unmount } from 'svelte'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import type { Session, SessionMessage, SessionPollRequest } from '../../types'; +import type { LinkedNoteContext } from './noteFreshness'; + +/** + * The note card a note session's chat shows where a note was delivered: + * "Note created" for the first delivery, "Note updated" for later ones. A + * note delivered with the `write_note` tool shows the card in place of the + * call's "Saved note" row (see `noteDeliveries.ts`). + * + * These tests mount the real pane with the real card and the real tool row, + * on rows shaped as the bridges store them. The card went missing once + * because every pane test stubbed it and the helper tests used fixtures in + * the old text shape, so neither is replaced here. + */ + +const SESSION_ID = 'note-session'; + +/** What the backend holds: the pane loads it, and polls it while `running`. */ +const { transcript, sessionState, sessionRow, pollSessionMock } = vi.hoisted(() => { + const transcript = { current: [] as SessionMessage[] }; + const sessionState = { status: 'completed', completionReason: 'turn_complete' as string | null }; + const sessionRow = (id: string) => + ({ + id, + status: sessionState.status, + completionReason: sessionState.completionReason, + provider: null, + workingDir: null, + }) as unknown as Session; + return { + transcript, + sessionState, + sessionRow, + // One tick of a running session: the last row the pane holds, which may + // have changed, and any after it. + pollSessionMock: vi.fn(async (id: string, request: SessionPollRequest) => ({ + session: sessionRow(id), + queuedMessages: [], + messages: transcript.current.filter((message) => message.id >= request.sinceId), + acpMetadataMessages: [], + })), + }; +}); + +// The composer and the pickers are not under test. `NoteActivityCard` and +// `ToolCallCard` are, and are the real components. +vi.mock('./ChatComposer.svelte', async () => { + const { default: PromptInputStub } = await import('./testStubs/PromptInputStub.svelte'); + return { default: PromptInputStub, composerControlClass: '' }; +}); +vi.mock('../agents/AcpFixedConfigPicker.svelte', () => import('./testStubs/EmptyStub.svelte')); +vi.mock('./PlanCard.svelte', () => import('./testStubs/EmptyStub.svelte')); +vi.mock('./PipelineSteps.svelte', () => import('./testStubs/EmptyStub.svelte')); +vi.mock( + '../../shared/markdown/DiagramViewerModal.svelte', + () => import('./testStubs/EmptyStub.svelte') +); +vi.mock('../branches/dragDrop', () => ({ subscribeDragDrop: () => () => {} })); +vi.mock('../agents/agent.svelte', () => ({ agentState: { providers: [] } })); +vi.mock('../settings/preferences.svelte', () => ({ + // Read by the send control the pane hands the composer stub. + preferences: { composerSendMode: 'queue' }, + setAcpConfigPref: vi.fn(), +})); +vi.mock('../../shared/draftCache', () => ({ + createDraftWriteScheduler: () => ({ + scheduleSave: vi.fn(), + saveNow: vi.fn(async () => {}), + clearNow: vi.fn(async () => {}), + flush: vi.fn(async () => {}), + }), + loadDraft: async () => null, + loadDraftContent: async () => null, +})); +vi.mock('../../transport', async (importOriginal) => ({ + ...(await importOriginal<typeof import('../../transport')>()), + listenToEvent: () => () => {}, +})); +vi.mock('../../commands', async (importOriginal) => ({ + ...(await importOriginal<typeof import('../../commands')>()), + getSession: async (id: string) => sessionRow(id), + pollSession: pollSessionMock, + getSessionLiveDelivery: async () => ({ supported: false, retryable: false }), + getLatestSessionMessages: async () => ({ data: transcript.current }), + getSessionMessages: async () => ({ data: [] }), + getSessionMessagesSince: async () => [], + getSessionAcpMetadataMessages: async () => [], + getSessionAcpMetadataMessagesSince: async () => [], + listQueuedSessionMessages: async () => [], +})); + +import SessionChatPane from './SessionChatPane.svelte'; +import { reactiveProps } from './testStubs/reactiveProps.svelte'; + +/** A row created at the time its id names, so ids read as a clock. */ +function row(id: number, role: SessionMessage['role'], content: string): SessionMessage { + return { id, sessionId: SESSION_ID, role, createdAt: id, content }; +} + +const NOTE_TITLE = 'Plan: fix the flaky login test'; + +/** A `write_note` call row as a bridge stores it, under that bridge's tool name. */ +function noteCall(id: number, name: string, status = 'completed'): SessionMessage { + const input = { title: NOTE_TITLE }; + return { + ...row(id, 'tool_call', JSON.stringify({ name, input })), + acpEventKind: 'tool_call', + acpToolCallId: `tc-${id}`, + acpToolStatus: status, + acpRawInput: input, + }; +} + +const CLAUDE = 'mcp__staged__write_note'; +const GOOSE = 'staged: write note'; +const CODEX = 'mcp.staged.write_note'; + +/** The note the pane is opened on, as the note dialog hands it over. */ +function noteContext(updatedAt: number): LinkedNoteContext { + return { + id: 'note-1', + title: NOTE_TITLE, + content: 'Wait for the session cookie before asserting.', + updatedAt, + hasParsedNote: true, + }; +} + +async function settle() { + for (let i = 0; i < 8; i++) { + await tick(); + } + flushSync(); +} + +describe('SessionChatPane note card', () => { + let pane: ReturnType<typeof mount> | null = null; + /** The pane's props, reactive so a test can hand it a refreshed note. */ + let props: { + active: boolean; + sessionId: string; + hashtagItems: []; + noteInfo: LinkedNoteContext | null; + }; + + beforeEach(() => { + document.body.innerHTML = ''; + sessionState.status = 'completed'; + sessionState.completionReason = 'turn_complete'; + pollSessionMock.mockClear(); + vi.spyOn(console, 'error').mockImplementation(() => {}); + vi.stubGlobal('requestAnimationFrame', (cb: FrameRequestCallback) => { + cb(0); + return 0; + }); + // A group that enters a mounted transcript slides in, and jsdom has no + // Web Animations. The slide itself is beside the point here. + Element.prototype.animate = () => + ({ onfinish: null, cancel() {}, playState: 'finished' }) as unknown as Animation; + }); + + afterEach(() => { + if (pane) unmount(pane); + pane = null; + delete (Element.prototype as Partial<Element>).animate; + vi.useRealTimers(); + vi.unstubAllGlobals(); + vi.restoreAllMocks(); + }); + + /** Mounts the pane, on the note when one is given, as the note dialog does. */ + async function mountPane(noteInfo: LinkedNoteContext | null = null) { + props = reactiveProps({ active: true, sessionId: SESSION_ID, hashtagItems: [], noteInfo }); + const target = document.createElement('div'); + document.body.appendChild(target); + pane = mount(SessionChatPane, { target, props }); + await settle(); + } + + /** One poll of a running session, which picks up the rows and the status. */ + async function pollOnce() { + const calls = pollSessionMock.mock.calls.length; + vi.advanceTimersByTime(500); + expect(pollSessionMock).toHaveBeenCalledTimes(calls + 1); + await settle(); + } + + /** The label of each note card the transcript shows, in order. */ + const noteCards = () => + [...document.querySelectorAll('.messages .note-activity-card')].map((card) => + card.textContent?.trim() + ); + + /** The header of each tool row: its verb and what it acted on. */ + const toolRows = () => + [...document.querySelectorAll('.tool-group .tool-header')].map((header) => [ + header.querySelector('.tool-name')?.textContent?.trim(), + header.querySelector('.tool-args-preview')?.textContent?.trim(), + ]); + + /** The label of the follow-up button under the transcript, if it is shown. */ + const followupButton = () => + document.querySelector('.note-followup-row button')?.textContent?.trim() ?? null; + + /** What the transcript shows, top to bottom: replies, tool rows and cards. */ + const shown = () => + [ + ...document.querySelectorAll( + '.messages .human-text, .messages .markdown-content, .messages .tool-name, .messages .note-activity-card' + ), + ].map((el) => el.textContent?.trim()); + + it.each([ + ['Claude Code', CLAUDE], + ['Goose', GOOSE], + ['Codex', CODEX], + ])('shows the card in place of a saved call from %s', async (_agent, name) => { + transcript.current = [ + row(1, 'user', 'Plan a fix for the flaky login test'), + row(2, 'assistant', 'The test races the session cookie.'), + noteCall(3, name), + row(4, 'assistant', 'Done'), + ]; + await mountPane(); + + expect(noteCards()).toEqual(['Note created']); + // The call has no row of its own, and no title beside the card. + expect(toolRows()).toEqual([]); + expect(document.querySelector('.messages')?.textContent).not.toContain(NOTE_TITLE); + // The card is where the row was: in the call's tool group, after the + // reply, and it ends the turn since the closing "Done" is hidden. + expect(document.querySelectorAll('.tool-group > .note-activity-card')).toHaveLength(1); + expect(shown()).toEqual([ + 'Plan a fix for the flaky login test', + 'The test races the session cookie.', + 'Note created', + ]); + }); + + it('labels the first saved call as created and later ones as updated', async () => { + transcript.current = [ + row(1, 'user', 'Plan a fix for the flaky login test'), + noteCall(2, CLAUDE), + row(3, 'assistant', 'Done'), + row(4, 'user', 'Add the file name'), + noteCall(5, CLAUDE), + row(6, 'assistant', 'Done'), + ]; + await mountPane(); + + expect(shown()).toEqual([ + 'Plan a fix for the flaky login test', + 'Note created', + 'Add the file name', + 'Note updated', + ]); + }); + + it('shows a reply that follows the call under the card', async () => { + transcript.current = [ + row(1, 'user', 'Which file holds the bug? Add it to the note.'), + noteCall(2, CLAUDE), + row(3, 'assistant', 'It is `src/login.ts`, line 12.'), + ]; + await mountPane(); + + expect(shown()).toEqual([ + 'Which file holds the bug? Add it to the note.', + 'Note created', + 'It is src/login.ts, line 12.', + ]); + }); + + it('keeps the tool row of a rejected call', async () => { + transcript.current = [ + row(1, 'user', 'Plan a fix for the flaky login test'), + noteCall(2, CLAUDE, 'failed'), + row(3, 'assistant', 'The diagram did not render. Fixing it.'), + noteCall(4, CLAUDE), + row(5, 'assistant', 'Done'), + ]; + await mountPane(); + + // The rejected call stays a row with its title; the retry is the card, + // and the first delivery, since the rejected call delivered nothing. + expect(toolRows()).toEqual([['Note rejected', NOTE_TITLE]]); + expect(shown()).toEqual([ + 'Plan a fix for the flaky login test', + 'Note rejected', + 'The diagram did not render. Fixing it.', + 'Note created', + ]); + }); + + it('keeps the tool row of an interrupted call', async () => { + // The call never settled and the session ended in error. + sessionState.status = 'error'; + sessionState.completionReason = 'crashed'; + transcript.current = [ + row(1, 'user', 'Plan a fix for the flaky login test'), + noteCall(2, CLAUDE, 'in_progress'), + ]; + await mountPane(); + + expect(toolRows()).toEqual([['Note not saved', NOTE_TITLE]]); + expect(noteCards()).toEqual([]); + }); + + it('shows the card for a remote call completed from the outbox', async () => { + // The call's update was lost with the connection; the session completed + // from the workstation outbox, so the call resolves to saved. + transcript.current = [ + row(1, 'user', 'Plan a fix for the flaky login test'), + noteCall(2, CLAUDE, 'in_progress'), + ]; + await mountPane(); + + expect(toolRows()).toEqual([]); + expect(noteCards()).toEqual(['Note created']); + }); + + it("keeps Codex's extra item as a row beside the card", async () => { + transcript.current = [ + row(1, 'user', 'Plan a fix for the flaky login test'), + noteCall(2, CODEX), + { + ...row(3, 'tool_call', 'Guardian Review'), + acpEventKind: 'tool_call', + acpToolCallId: 'tc-review', + acpToolStatus: 'completed', + }, + row(4, 'assistant', 'Done'), + ]; + await mountPane(); + + expect(noteCards()).toEqual(['Note created']); + expect(toolRows()).toHaveLength(1); + // Both are in the one tool group, the card first. + const group = document.querySelector('.tool-group'); + expect(group?.children).toHaveLength(2); + expect(group?.firstElementChild?.classList.contains('note-activity-card')).toBe(true); + }); + + it('shows two saved calls in a row as two cards', async () => { + // Same verb, same tool group: as rows they would fold into one collapsed + // "Saved note" row. + transcript.current = [ + row(1, 'user', 'Plan a fix for the flaky login test'), + noteCall(2, CLAUDE), + noteCall(3, CLAUDE), + row(4, 'assistant', 'Done'), + ]; + await mountPane(); + + expect(noteCards()).toEqual(['Note created', 'Note updated']); + expect(toolRows()).toEqual([]); + }); + + it('turns the running call from a row into the card when it completes', async () => { + // Only the poll's interval: Svelte's own scheduling stays real. + vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] }); + sessionState.status = 'running'; + sessionState.completionReason = null; + transcript.current = [ + row(1, 'user', 'Plan a fix for the flaky login test'), + noteCall(2, CLAUDE, 'in_progress'), + ]; + await mountPane(); + expect(toolRows()).toEqual([['Saving note', NOTE_TITLE]]); + expect(noteCards()).toEqual([]); + + transcript.current = [transcript.current[0], noteCall(2, CLAUDE)]; + await pollOnce(); + + expect(toolRows()).toEqual([]); + expect(noteCards()).toEqual(['Note created']); + }); + + it('still shows the card for a note delivered as text', async () => { + // The fallback for a run with no tool, and every session recorded before + // the tool: the reply holds the separator and the note after it. + transcript.current = [ + row(1, 'user', 'Plan a fix for the flaky login test'), + row(2, 'assistant', 'Here is the plan.\n\n---\n\n# Plan\n\nWait for the cookie.'), + row(3, 'user', 'Add the file name'), + noteCall(4, CLAUDE), + row(5, 'assistant', 'Done'), + ]; + await mountPane(); + + // The text after the separator is the note, not chat; the saved call + // that follows it is an update to the note that text created. + expect(shown()).toEqual([ + 'Plan a fix for the flaky login test', + 'Here is the plan.', + 'Note created', + 'Add the file name', + 'Note updated', + ]); + }); + + describe('opened on its note', () => { + const savedTurn = () => [ + row(10, 'user', 'Plan a fix for the flaky login test'), + noteCall(20, CLAUDE), + row(30, 'assistant', 'Done'), + ]; + + it('shows the card and no button when the turn changed the note', async () => { + transcript.current = savedTurn(); + // Stored once the turn ended, after its last message. + await mountPane(noteContext(40)); + + expect(noteCards()).toEqual(['Note created']); + expect(toolRows()).toEqual([]); + expect(followupButton()).toBeNull(); + }); + + it('keeps the row and offers the button when the note is older than the call', async () => { + // The agent republished an identical note, which keeps its timestamp, + // or the run failed before the note was stored. + transcript.current = savedTurn(); + await mountPane(noteContext(5)); + + expect(noteCards()).toEqual([]); + expect(toolRows()).toEqual([['Saved note', NOTE_TITLE]]); + expect(followupButton()).toBe('Ask for the note to be updated'); + // The closing "Done" stays hidden either way. + expect(shown()).toEqual(['Plan a fix for the flaky login test', 'Saved note']); + }); + + it('swaps the row for the card when the refreshed note is newer', async () => { + transcript.current = savedTurn(); + await mountPane(noteContext(5)); + expect(toolRows()).toEqual([['Saved note', NOTE_TITLE]]); + + // The dialog refetched the note: it was stored after the turn. + props.noteInfo = noteContext(40); + await settle(); + + expect(noteCards()).toEqual(['Note created']); + expect(toolRows()).toEqual([]); + expect(followupButton()).toBeNull(); + }); + + it('shows the card while the session runs, before the note is stored', async () => { + vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] }); + sessionState.status = 'running'; + sessionState.completionReason = null; + transcript.current = savedTurn(); + // The stored note is still the one from before the turn. + await mountPane(noteContext(5)); + + expect(noteCards()).toEqual(['Note created']); + expect(toolRows()).toEqual([]); + expect(followupButton()).toBeNull(); + + // The session settles and the note did not change: the row comes back + // and the button is offered. + sessionState.status = 'completed'; + sessionState.completionReason = 'turn_complete'; + await pollOnce(); + + expect(noteCards()).toEqual([]); + expect(toolRows()).toEqual([['Saved note', NOTE_TITLE]]); + expect(followupButton()).toBe('Ask for the note to be updated'); + }); + + it('keeps an earlier card and offers the button after a chat-only turn', async () => { + transcript.current = [ + ...savedTurn(), + row(50, 'user', 'Why wait for the cookie?'), + row(60, 'assistant', 'The assertion reads it before the redirect sets it.'), + ]; + // Stored after the first turn; the second turn left it alone. + await mountPane(noteContext(40)); + + expect(shown()).toEqual([ + 'Plan a fix for the flaky login test', + 'Note created', + 'Why wait for the cookie?', + 'The assertion reads it before the redirect sets it.', + ]); + expect(followupButton()).toBe('Ask for the note to be updated'); + }); + + it('compares only the final turn', async () => { + // The first turn stored the note at 40. The second republished it + // unchanged: its call keeps the row, the first turn's stays a card. + transcript.current = [ + ...savedTurn(), + row(50, 'user', 'Is the plan still right?'), + noteCall(60, CLAUDE), + row(70, 'assistant', 'Done'), + ]; + await mountPane(noteContext(40)); + + expect(shown()).toEqual([ + 'Plan a fix for the flaky login test', + 'Note created', + 'Is the plan still right?', + 'Saved note', + ]); + expect(followupButton()).toBe('Ask for the note to be updated'); + }); + }); +}); diff --git a/apps/staged/src/lib/features/sessions/SessionChatPane.noteDone.component.test.ts b/apps/staged/src/lib/features/sessions/SessionChatPane.noteDone.component.test.ts new file mode 100644 index 000000000..eb9703446 --- /dev/null +++ b/apps/staged/src/lib/features/sessions/SessionChatPane.noteDone.component.test.ts @@ -0,0 +1,333 @@ +import { flushSync, mount, tick, unmount } from 'svelte'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import type { Session, SessionMessage, SessionPollRequest } from '../../types'; + +/** + * The bare "Done" a note session sends after a saved `write_note` call. The + * pane leaves that message out of the transcript (see `noteDoneMessage.ts`); + * any other text after the call is shown as written. These tests go through + * the real pane, the real tool row and the real note card, so the rule is + * checked where the user sees it, on rows shaped as the bridges store them. + * A saved call shows as the note card and any other as its tool row (see + * `SessionChatPane.noteCard.component.test.ts`). + */ + +const SESSION_ID = 'note-session'; + +/** What the backend holds: the pane loads it, and polls it while `running`. */ +const { transcript, sessionStatus, sessionRow, pollSessionMock } = vi.hoisted(() => { + const transcript = { current: [] as SessionMessage[] }; + const sessionStatus = { current: 'completed' }; + const sessionRow = (id: string) => + ({ id, status: sessionStatus.current, provider: null, workingDir: null }) as unknown as Session; + return { + transcript, + sessionStatus, + sessionRow, + // One tick of a running session: the last row the pane holds, which may + // have grown, and any after it. + pollSessionMock: vi.fn(async (id: string, request: SessionPollRequest) => ({ + session: sessionRow(id), + queuedMessages: [], + messages: transcript.current.filter((message) => message.id >= request.sinceId), + acpMetadataMessages: [], + })), + }; +}); + +// The composer and the pickers are not under test. The tool row and the note +// card are: neither is stubbed, so the saved call renders as the user sees it. +vi.mock('./ChatComposer.svelte', async () => { + const { default: PromptInputStub } = await import('./testStubs/PromptInputStub.svelte'); + return { default: PromptInputStub, composerControlClass: '' }; +}); +vi.mock('../agents/AcpFixedConfigPicker.svelte', () => import('./testStubs/EmptyStub.svelte')); +vi.mock('./PlanCard.svelte', () => import('./testStubs/EmptyStub.svelte')); +vi.mock('./PipelineSteps.svelte', () => import('./testStubs/EmptyStub.svelte')); +vi.mock( + '../../shared/markdown/DiagramViewerModal.svelte', + () => import('./testStubs/EmptyStub.svelte') +); +vi.mock('../branches/dragDrop', () => ({ subscribeDragDrop: () => () => {} })); +vi.mock('../agents/agent.svelte', () => ({ agentState: { providers: [] } })); +vi.mock('../settings/preferences.svelte', () => ({ + // Read by the send control the pane hands the composer stub. + preferences: { composerSendMode: 'queue' }, + setAcpConfigPref: vi.fn(), +})); +vi.mock('../../shared/draftCache', () => ({ + createDraftWriteScheduler: () => ({ + scheduleSave: vi.fn(), + saveNow: vi.fn(async () => {}), + clearNow: vi.fn(async () => {}), + flush: vi.fn(async () => {}), + }), + loadDraft: async () => null, + loadDraftContent: async () => null, +})); +vi.mock('../../transport', async (importOriginal) => ({ + ...(await importOriginal<typeof import('../../transport')>()), + listenToEvent: () => () => {}, +})); +vi.mock('../../commands', async (importOriginal) => ({ + ...(await importOriginal<typeof import('../../commands')>()), + getSession: async (id: string) => sessionRow(id), + pollSession: pollSessionMock, + getSessionLiveDelivery: async () => ({ supported: false, retryable: false }), + getLatestSessionMessages: async () => ({ data: transcript.current }), + getSessionMessages: async () => ({ data: [] }), + getSessionMessagesSince: async () => [], + getSessionAcpMetadataMessages: async () => [], + getSessionAcpMetadataMessagesSince: async () => [], + listQueuedSessionMessages: async () => [], +})); + +import SessionChatPane from './SessionChatPane.svelte'; +import { reactiveProps } from './testStubs/reactiveProps.svelte'; + +function row(id: number, role: SessionMessage['role'], content: string): SessionMessage { + return { id, sessionId: SESSION_ID, role, createdAt: id, content }; +} + +const NOTE_TITLE = 'Plan: fix the flaky login test'; + +/** A `write_note` call row as a bridge stores it, under that bridge's tool name. */ +function noteCall(id: number, name: string, status = 'completed'): SessionMessage { + const input = { title: NOTE_TITLE }; + return { + ...row(id, 'tool_call', JSON.stringify({ name, input })), + acpEventKind: 'tool_call', + acpToolCallId: `tc-${id}`, + acpToolStatus: status, + acpRawInput: input, + }; +} + +const CLAUDE = 'mcp__staged__write_note'; +const GOOSE = 'staged: write note'; +const CODEX = 'mcp.staged.write_note'; + +async function settle() { + for (let i = 0; i < 8; i++) { + await tick(); + } + flushSync(); +} + +describe('SessionChatPane after a saved note', () => { + let pane: ReturnType<typeof mount> | null = null; + + beforeEach(() => { + document.body.innerHTML = ''; + sessionStatus.current = 'completed'; + pollSessionMock.mockClear(); + vi.spyOn(console, 'error').mockImplementation(() => {}); + vi.stubGlobal('requestAnimationFrame', (cb: FrameRequestCallback) => { + cb(0); + return 0; + }); + // A group that enters a mounted transcript slides in, and jsdom has no + // Web Animations. The slide itself is beside the point here. + Element.prototype.animate = () => + ({ onfinish: null, cancel() {}, playState: 'finished' }) as unknown as Animation; + }); + + afterEach(() => { + if (pane) unmount(pane); + pane = null; + delete (Element.prototype as Partial<Element>).animate; + vi.useRealTimers(); + vi.unstubAllGlobals(); + vi.restoreAllMocks(); + }); + + async function mountPane() { + const props = reactiveProps({ active: true, sessionId: SESSION_ID, hashtagItems: [] }); + const target = document.createElement('div'); + document.body.appendChild(target); + pane = mount(SessionChatPane, { target, props }); + await settle(); + } + + /** The text of each assistant message the transcript shows, in order. */ + const assistantTexts = () => + [...document.querySelectorAll('.assistant-message .markdown-content')].map((el) => + el.textContent?.trim() + ); + + /** The label of each note card the transcript shows, in order. */ + const noteCards = () => + [...document.querySelectorAll('.messages .note-activity-card')].map((card) => + card.textContent?.trim() + ); + + /** The header of each tool row: its verb and what it acted on. */ + const toolRows = () => + [...document.querySelectorAll('.tool-group .tool-header')].map((header) => [ + header.querySelector('.tool-name')?.textContent?.trim(), + header.querySelector('.tool-args-preview')?.textContent?.trim(), + ]); + + /** The keys of the groups the transcript rendered, in order. */ + const groupKeys = () => + [...document.querySelectorAll<HTMLElement>('.transcript-group')].map( + (group) => group.dataset.groupKey + ); + + it.each([ + ['Claude Code', CLAUDE], + ['Goose', GOOSE], + ['Codex', CODEX], + ])('leaves out the bare Done after a call from %s', async (_agent, name) => { + transcript.current = [ + row(1, 'user', 'Plan a fix for the flaky login test'), + row(2, 'assistant', 'The test races the session cookie.'), + noteCall(3, name), + row(4, 'assistant', 'Done'), + ]; + await mountPane(); + + // The reply before the call and the saved call are there; the Done is not. + expect(assistantTexts()).toEqual(['The test races the session cookie.']); + expect(noteCards()).toEqual(['Note created']); + expect(toolRows()).toEqual([]); + // Nothing is rendered for it at all: no empty group to take a gap in the + // column or to anchor the scroll position. + expect(groupKeys()).toEqual(['m-1', 'm-2', 't-tool:tc-3']); + expect(document.querySelector('.messages')?.textContent).not.toMatch(/done/i); + }); + + it("leaves it out beside Codex's extra item, and when it is said twice", async () => { + transcript.current = [ + row(1, 'user', 'Plan a fix for the flaky login test'), + noteCall(2, CODEX), + { + ...row(3, 'tool_call', 'Guardian Review'), + acpEventKind: 'tool_call', + acpToolCallId: 'tc-review', + acpToolStatus: 'completed', + }, + row(4, 'assistant', 'Done'), + row(5, 'assistant', '**Done.**'), + ]; + await mountPane(); + + expect(assistantTexts()).toEqual([]); + expect(noteCards()).toEqual(['Note created']); + expect(toolRows()).toEqual([['Ran', 'Guardian Review']]); + expect(groupKeys()).toHaveLength(2); + }); + + it('shows any other text after the call, whole', async () => { + transcript.current = [ + row(1, 'user', 'Which file holds the bug?'), + noteCall(2, CLAUDE), + row(3, 'assistant', 'It is `src/login.ts`, line 12.\n\nDone'), + row(4, 'user', 'Add that to the note'), + noteCall(5, CLAUDE), + row(6, 'assistant', 'Done, the note now names the file.'), + ]; + await mountPane(); + + // An answer that ends with a Done line keeps the line; a sentence that + // starts with the word keeps the word. + expect(assistantTexts()).toEqual([ + 'It is src/login.ts, line 12.\nDone', + 'Done, the note now names the file.', + ]); + }); + + it('shows the Done after a rejected call', async () => { + transcript.current = [ + row(1, 'user', 'Plan a fix for the flaky login test'), + noteCall(2, CLAUDE, 'failed'), + row(3, 'assistant', 'Done'), + ]; + await mountPane(); + + expect(toolRows()).toEqual([['Note rejected', NOTE_TITLE]]); + expect(assistantTexts()).toEqual(['Done']); + }); + + it('shows the Done after a call that was interrupted', async () => { + // The call never settled and the session ended in error. + sessionStatus.current = 'error'; + transcript.current = [ + row(1, 'user', 'Plan a fix for the flaky login test'), + noteCall(2, CLAUDE, 'in_progress'), + row(3, 'assistant', 'Done'), + ]; + await mountPane(); + + expect(toolRows()).toEqual([['Note not saved', NOTE_TITLE]]); + expect(assistantTexts()).toEqual(['Done']); + }); + + it('leaves it out after a remote call completed from the outbox', async () => { + // The call's update was lost with the connection; the session completed + // from the workstation outbox, so the call resolves to saved. + transcript.current = [ + row(1, 'user', 'Plan a fix for the flaky login test'), + noteCall(2, CLAUDE, 'in_progress'), + row(3, 'assistant', 'Done'), + ]; + await mountPane(); + + expect(noteCards()).toEqual(['Note created']); + expect(toolRows()).toEqual([]); + expect(assistantTexts()).toEqual([]); + }); + + it('brings the message back when more text streams in after the word', async () => { + // Only the poll's interval: Svelte's own scheduling stays real. + vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] }); + sessionStatus.current = 'running'; + transcript.current = [ + row(1, 'user', 'Which file holds the bug?'), + noteCall(2, CLAUDE), + row(3, 'assistant', 'Done'), + ]; + await mountPane(); + expect(assistantTexts()).toEqual([]); + expect(pollSessionMock).not.toHaveBeenCalled(); + + // The next chunk: the same row, longer. + transcript.current = [ + ...transcript.current.slice(0, 2), + row(3, 'assistant', 'Done. The bug is in `src/login.ts`.'), + ]; + vi.advanceTimersByTime(500); + expect(pollSessionMock).toHaveBeenCalledTimes(1); + await settle(); + + expect(assistantTexts()).toEqual(['Done. The bug is in src/login.ts.']); + expect(groupKeys()).toEqual(['m-1', 't-tool:tc-2', 'm-3']); + }); + + it('shows the message again once a tool call follows it', async () => { + vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] }); + sessionStatus.current = 'running'; + transcript.current = [ + row(1, 'user', 'Plan a fix for the flaky login test'), + noteCall(2, CLAUDE), + row(3, 'assistant', 'Done'), + ]; + await mountPane(); + expect(assistantTexts()).toEqual([]); + + transcript.current = [ + ...transcript.current, + { + ...row(4, 'tool_call', JSON.stringify({ name: 'Bash', input: { command: 'git status' } })), + acpEventKind: 'tool_call', + acpToolCallId: 'tc-4', + acpToolStatus: 'in_progress', + }, + ]; + vi.advanceTimersByTime(500); + expect(pollSessionMock).toHaveBeenCalledTimes(1); + await settle(); + + expect(assistantTexts()).toEqual(['Done']); + }); +}); diff --git a/apps/staged/src/lib/features/sessions/SessionChatPane.svelte b/apps/staged/src/lib/features/sessions/SessionChatPane.svelte index a2dc72e14..f4b07c900 100644 --- a/apps/staged/src/lib/features/sessions/SessionChatPane.svelte +++ b/apps/staged/src/lib/features/sessions/SessionChatPane.svelte @@ -192,7 +192,17 @@ type AcpCommand, type AcpTranscriptEvent, type AcpTranscriptGroup, + type RichToolItem, } from './acpTranscript'; + import { + isFirstNoteDeliveryMessage, + isNoteCardItem, + listNoteDeliveries, + noteCallCardLabel, + stabilizeNoteDeliveries, + type NoteDeliveries, + } from './noteDeliveries'; + import { hiddenNoteDoneMessageIds } from './noteDoneMessage'; import PlanCard from './PlanCard.svelte'; import { viewport } from '../../shared/viewport.svelte'; import { autofocusOnOpen } from '../../shared/focusAfterDialogEnter'; @@ -779,10 +789,43 @@ ); return previousGrouped; }); + /** + * The groups the transcript shows: all of them, less the bare "Done" a note + * session sends to close a turn after a saved `write_note` call (see + * `noteDoneMessage.ts`). The saved-note row above already says it. The + * group is left out of the list rather than rendered empty, so it takes no + * gap in the column and is not a scroll anchor. The rule runs on every + * rebuild: a message that grows past the word comes back whole. + */ + let renderedGroups = $derived.by(() => { + const hidden = hiddenNoteDoneMessageIds(grouped); + if (hidden.size === 0) return grouped; + return grouped.filter((group) => group.type !== 'assistant' || !hidden.has(group.message.id)); + }); /** Number-valued so per-group template expressions can depend on "am I the * last group?" without re-running whenever the grouped array identity * changes — deriveds cut propagation when the value is equal. */ - let lastGroupIndex = $derived(grouped.length - 1); + let lastGroupIndex = $derived(renderedGroups.length - 1); + /** + * Where the transcript delivered a note: the saved `write_note` calls shown + * as the note card in place of their row, and the assistant messages that + * hold the `---` separator (see `noteDeliveries.ts`). In the final turn of + * a settled session a saved call is a card only when the stored note is at + * least as new as the call; otherwise its row stays and the follow-up + * button below the transcript offers the update. The same object is kept + * across rebuilds that change no delivery, so the tool groups that read it + * are not re-evaluated on every poll tick. + */ + let previousNoteDeliveries: NoteDeliveries | null = null; + let noteDeliveries = $derived.by(() => { + previousNoteDeliveries = stabilizeNoteDeliveries( + previousNoteDeliveries, + listNoteDeliveries(grouped, { noteUpdatedAt: noteInfo?.updatedAt, sessionLive: isLive }) + ); + return previousNoteDeliveries; + }); + /** Whether a tool item is shown as the note card, so it never folds into a verb group. */ + const isNoteCard = (item: RichToolItem) => isNoteCardItem(noteDeliveries, item); /** Latest ACP plan, pinned above the transcript rather than rendered in it. */ let plan = $derived.by(() => latestPlan(acpMetadataMessages)); /** Pikchr sources of successful render_pikchr tool calls, shown inline as diagrams. */ @@ -3265,25 +3308,6 @@ .slice(0, 6); }); - /** Note-indicator scans keyed on the message object: rows keep identity - * across polls except the re-fetched tail, so stale entries self-evict. */ - const noteIndicatorCache = new WeakMap<SessionMessage, boolean>(); - - function messageHasNoteIndicator(message: SessionMessage): boolean { - let hasNote = noteIndicatorCache.get(message); - if (hasNote === undefined) { - hasNote = splitAtNoteIndicator(message.content).hasNote; - noteIndicatorCache.set(message, hasNote); - } - return hasNote; - } - - let noteBearingMessageIds = $derived.by(() => { - return messages - .filter((message) => message.role === 'assistant' && messageHasNoteIndicator(message)) - .map((message) => message.id); - }); - let isPipelineRunning = $derived(isLive && !!session?.pipeline); function insertSlashCommand(command: AcpCommand) { @@ -3458,7 +3482,7 @@ {/if} </div> {/if} - {#each grouped as group, groupIdx (transcriptGroupKey(group))} + {#each renderedGroups as group, groupIdx (transcriptGroupKey(group))} <div in:messageSlide class={group.type === 'user' ? 'transcript-group user-group' : 'transcript-group'} @@ -3563,7 +3587,6 @@ {@const split = splitAtNoteIndicator(group.message.content, { streaming: isLive && groupIdx === lastGroupIndex, })} - {@const firstNoteMessageId = noteBearingMessageIds[0]} {@const hasPreamble = split.preamble.trim().length > 0} {@const showAssistantCopy = hasPreamble || !split.hasNote} <div class="message-row assistant-message"> @@ -3582,7 +3605,10 @@ label={noteActivityLabel({ isLive, isLastGroup: groupIdx === lastGroupIndex, - isFirstNoteMessage: group.message.id === firstNoteMessageId, + isFirstNoteMessage: isFirstNoteDeliveryMessage( + noteDeliveries, + group.message.id + ), })} /> {/if} @@ -3606,8 +3632,14 @@ </div> {:else if group.type === 'tools'} <div class="message-row tool-group"> - {#each groupRichToolsByVerb(group.items) as verbGroup (verbGroup.key)} - {#if verbGroup.items.length === 1} + {#each groupRichToolsByVerb(group.items, isNoteCard) as verbGroup (verbGroup.key)} + {#if verbGroup.items.length === 1 && isNoteCard(verbGroup.items[0])} + <!-- A saved `write_note` call: the card says the note was + delivered, in the row's place. --> + <NoteActivityCard + label={noteCallCardLabel(noteDeliveries, verbGroup.items[0])} + /> + {:else if verbGroup.items.length === 1} <ToolCallCard item={verbGroup.items[0]} {displayRoots} @@ -4358,6 +4390,23 @@ min-width: 0; } + /* The note card shown in place of a saved `write_note` row. It keeps its + own width in the column instead of stretching like a tool row, and is set + off from the rows beside it. Alone, first or last in the group, the gap + between transcript groups is its spacing. */ + .tool-group :global(.note-activity-card) { + align-self: flex-start; + margin: 6px 0; + } + + .tool-group :global(.note-activity-card:first-child) { + margin-top: 0; + } + + .tool-group :global(.note-activity-card:last-child) { + margin-bottom: 0; + } + /* Inline diagram from a successful render_pikchr call — rendered through the markdown pipeline, so it picks up the shared diagram styles (fullscreen zoom affordance included); only the message margins are tightened to sit diff --git a/apps/staged/src/lib/features/sessions/acpTranscript.test.ts b/apps/staged/src/lib/features/sessions/acpTranscript.test.ts index d62ae1d64..2aadbbf00 100644 --- a/apps/staged/src/lib/features/sessions/acpTranscript.test.ts +++ b/apps/staged/src/lib/features/sessions/acpTranscript.test.ts @@ -797,6 +797,32 @@ describe('groupRichToolsByVerb', () => { expect(groups.map((group) => group.items)).toHaveLength(3); }); + it('keeps an item the caller renders on its own out of a verb group', () => { + // The pane shows a saved `write_note` call as the note card. Two in a row + // would otherwise fold into one collapsed "Saved note" row. + const items = [ + richTool({ key: 'tool:1', verb: 'Saved note' }), + richTool({ key: 'tool:2', verb: 'Saved note' }), + richTool({ key: 'tool:3', verb: 'Saved note' }), + ]; + + expect(groupRichToolsByVerb(items).map((group) => group.items.length)).toEqual([3]); + + const alone = groupRichToolsByVerb(items, (item) => item.key === 'tool:2'); + expect(alone.map((group) => group.items.map((item) => item.key))).toEqual([ + ['tool:1'], + ['tool:2'], + ['tool:3'], + ]); + // An item named standalone neither joins the group before it nor lets + // the next item join it; the others still fold as before. + const first = groupRichToolsByVerb(items, (item) => item.key === 'tool:1'); + expect(first.map((group) => group.items.map((item) => item.key))).toEqual([ + ['tool:1'], + ['tool:2', 'tool:3'], + ]); + }); + it('does not merge same verbs across transcript group boundaries', () => { const transcript = buildAcpTranscriptGroups( [ diff --git a/apps/staged/src/lib/features/sessions/acpTranscript.ts b/apps/staged/src/lib/features/sessions/acpTranscript.ts index c88fd56bf..106895321 100644 --- a/apps/staged/src/lib/features/sessions/acpTranscript.ts +++ b/apps/staged/src/lib/features/sessions/acpTranscript.ts @@ -402,11 +402,24 @@ function rendersStandalone(item: RichToolItem | undefined): boolean { return !!item && (item.isPikchrDiagramTool || item.pikchrRenderSource !== null); } -export function groupRichToolsByVerb(items: RichToolItem[]): RichToolVerbGroup[] { +/** + * Folds consecutive tool items with the same verb into one group, which the + * pane shows as a single collapsed row. + * + * `standsAlone` names further items the caller renders as their own block, + * beside the ones this module knows: the pane passes the saved `write_note` + * calls it shows as the note card. Such an item is always a group of one. + */ +export function groupRichToolsByVerb( + items: RichToolItem[], + standsAlone?: (item: RichToolItem) => boolean +): RichToolVerbGroup[] { + const alone = (item: RichToolItem | undefined) => + rendersStandalone(item) || (!!item && !!standsAlone?.(item)); const groups: Array<Omit<RichToolVerbGroup, 'summary' | 'statusTone'>> = []; for (const item of items) { const last = groups[groups.length - 1]; - if (!rendersStandalone(item) && last?.verb === item.verb && !rendersStandalone(last.items[0])) { + if (!alone(item) && last?.verb === item.verb && !alone(last.items[0])) { last.items.push(item); } else { groups.push({ diff --git a/apps/staged/src/lib/features/sessions/noteDeliveries.test.ts b/apps/staged/src/lib/features/sessions/noteDeliveries.test.ts new file mode 100644 index 000000000..dab84e833 --- /dev/null +++ b/apps/staged/src/lib/features/sessions/noteDeliveries.test.ts @@ -0,0 +1,389 @@ +import { describe, expect, it } from 'vitest'; +import type { Session, SessionMessage } from '../../types'; +import { + buildAcpTranscriptGroups, + type AcpTranscriptOptions, + type ToolStatus, +} from './acpTranscript'; +import { + isFirstNoteDeliveryMessage, + isNoteCardItem, + listNoteDeliveries, + noteCallCardLabel, + noteMessageKey, + stabilizeNoteDeliveries, + type NoteDeliveryOptions, +} from './noteDeliveries'; +import { shouldAskForNoteUpdate, type LinkedNoteContext } from './noteFreshness'; + +/** A row created at the time its id names, so ids read as a clock. */ +function message( + overrides: Partial<SessionMessage> & Pick<SessionMessage, 'id' | 'role'> +): SessionMessage { + return { + sessionId: 'session-1', + content: overrides.content ?? '', + createdAt: overrides.id, + ...overrides, + }; +} + +const user = (id: number, content = 'Write the note') => message({ id, role: 'user', content }); +const assistant = (id: number, content: string) => message({ id, role: 'assistant', content }); + +const NOTE_INPUT = { title: 'Plan' }; + +/** A `write_note` call row, in Claude Code's spelling unless `name` says otherwise. */ +function noteCall(id: number, status: ToolStatus = 'completed', name = 'mcp__staged__write_note') { + return message({ + id, + role: 'tool_call', + content: JSON.stringify({ name, input: NOTE_INPUT }), + acpEventKind: 'tool_call', + acpToolCallId: `tc-${id}`, + acpToolStatus: status, + acpRawInput: NOTE_INPUT, + }); +} + +/** Any other tool call row. */ +function otherCall(id: number, name = 'Bash') { + return message({ + id, + role: 'tool_call', + content: JSON.stringify({ name, input: { command: 'git status' } }), + acpEventKind: 'tool_call', + acpToolCallId: `tc-${id}`, + acpToolStatus: 'completed', + }); +} + +/** The key the transcript builder gives the call row with this id. */ +const callKey = (id: number) => `tool:tc-${id}`; + +/** A reply in the old shape: text, the separator, then the note. */ +const SEPARATOR_NOTE = 'Here is the plan.\n\n---\n\n# Plan\n\nFix the test.'; + +function deliveries( + rows: SessionMessage[], + options: NoteDeliveryOptions = {}, + transcript: AcpTranscriptOptions = {} +) { + return listNoteDeliveries(buildAcpTranscriptGroups(rows, [], undefined, transcript), options); +} + +/** The keys of the calls shown as cards, in transcript order. */ +function cards( + rows: SessionMessage[], + options: NoteDeliveryOptions = {}, + transcript: AcpTranscriptOptions = {} +): string[] { + return [...deliveries(rows, options, transcript).cardToolKeys]; +} + +describe('listNoteDeliveries', () => { + it('finds nothing in a transcript with no note', () => { + const none = deliveries([user(1), otherCall(2), assistant(3, 'The tree is clean.')]); + expect(none.firstKey).toBeNull(); + expect(none.cardToolKeys.size).toBe(0); + }); + + it('shows a saved call as a card', () => { + const found = deliveries([user(1), noteCall(2), assistant(3, 'Done')]); + expect([...found.cardToolKeys]).toEqual([callKey(2)]); + expect(found.firstKey).toBe(callKey(2)); + }); + + it.each([ + ['Claude Code', 'mcp__staged__write_note'], + ['Goose', 'staged: write note'], + ['Codex', 'mcp.staged.write_note'], + ])('finds the call as %s names it', (_agent, name) => { + expect(cards([user(1), noteCall(2, 'completed', name)])).toEqual([callKey(2)]); + }); + + it('finds a call whose row holds only the tool name', () => { + // No arguments on the row: recognised by name, not by its label. + const call = message({ + id: 2, + role: 'tool_call', + content: 'mcp.staged.write_note', + acpEventKind: 'tool_call', + acpToolCallId: 'tc-2', + acpToolStatus: 'completed', + }); + expect(cards([user(1), call])).toEqual([callKey(2)]); + }); + + it.each([ + ['rejected', 'failed'], + ['cancelled by the user', 'cancelled'], + ['waiting to run', 'pending'], + ['still running', 'in_progress'], + ] as const)('keeps the row of a call that was %s', (_label, status) => { + const found = deliveries([user(1), noteCall(2, status), assistant(3, 'Done')]); + expect(found.cardToolKeys.size).toBe(0); + expect(found.firstKey).toBeNull(); + }); + + it('keeps the row of an interrupted call', () => { + // The call never settled and the session ended in error or was cancelled: + // the row reads "Note not saved". + const rows = [user(1), noteCall(2, 'in_progress')]; + expect(cards(rows, {}, { sessionSettled: true, sessionCompleted: false })).toEqual([]); + }); + + it('shows a remote call completed from the outbox as a card', () => { + // The call's update was lost with the connection, but the session + // completed from the workstation outbox: the call resolves to saved. + const rows = [user(1), noteCall(2, 'in_progress')]; + expect(cards(rows, {}, { sessionSettled: true, sessionCompleted: true })).toEqual([callKey(2)]); + }); + + it("ignores another server's write_note", () => { + const rows = [user(1), noteCall(2, 'completed', 'mcp__other__write_note')]; + expect(cards(rows)).toEqual([]); + }); + + it('picks the note call out of a tool group with other items', () => { + // Codex completes a review item after the call; Claude sometimes issues a + // shell call beside it. They share the tool group and keep their rows. + const rows = [user(1), otherCall(2), noteCall(3), otherCall(4, 'Guardian Review')]; + expect(cards(rows)).toEqual([callKey(3)]); + }); + + it('shows a rejected call as a row and the retry that saved as a card', () => { + const rows = [user(1), noteCall(2, 'failed'), assistant(3, 'Fixing the diagram.'), noteCall(4)]; + const found = deliveries(rows); + expect([...found.cardToolKeys]).toEqual([callKey(4)]); + expect(found.firstKey).toBe(callKey(4)); + }); + + it('names the first delivery the one that created the note', () => { + const rows = [ + user(1), + noteCall(2), + assistant(3, 'Done'), + user(4, 'Add a section on the fix'), + noteCall(5), + assistant(6, 'Done'), + ]; + const groups = buildAcpTranscriptGroups(rows, []); + const found = listNoteDeliveries(groups); + const items = groups.flatMap((group) => (group.type === 'tools' ? group.items : [])); + + expect(items.map((item) => isNoteCardItem(found, item))).toEqual([true, true]); + expect(items.map((item) => noteCallCardLabel(found, item))).toEqual([ + 'Note created', + 'Note updated', + ]); + }); + + it('never labels a saved call as still being written', () => { + // The pane's live flag does not reach the label: a running call has its + // own "Saving note" row, and a completed one is saved. + const groups = buildAcpTranscriptGroups([user(1), noteCall(2)], []); + const found = listNoteDeliveries(groups, { sessionLive: true }); + const [item] = groups.flatMap((group) => (group.type === 'tools' ? group.items : [])); + expect(noteCallCardLabel(found, item)).toBe('Note created'); + }); + + describe('the separator fallback', () => { + it('finds an assistant message that holds the separator', () => { + const found = deliveries([user(1), assistant(2, SEPARATOR_NOTE)]); + expect(found.firstKey).toBe(noteMessageKey(2)); + expect(isFirstNoteDeliveryMessage(found, 2)).toBe(true); + expect(found.cardToolKeys.size).toBe(0); + }); + + it('does not take a plain reply for a note', () => { + const found = deliveries([user(1), assistant(2, 'The test races the cookie.')]); + expect(found.firstKey).toBeNull(); + expect(isFirstNoteDeliveryMessage(found, 2)).toBe(false); + }); + + it('orders both shapes in one list', () => { + // A session recorded before the tool, continued after it: the note was + // created by text, so the saved call is an update. + const rows = [ + user(1), + assistant(2, SEPARATOR_NOTE), + user(3, 'Add a section on the fix'), + noteCall(4), + user(5, 'And one on the tests'), + assistant(6, SEPARATOR_NOTE), + ]; + const groups = buildAcpTranscriptGroups(rows, []); + const found = listNoteDeliveries(groups); + const [item] = groups.flatMap((group) => (group.type === 'tools' ? group.items : [])); + + expect(found.firstKey).toBe(noteMessageKey(2)); + expect(isFirstNoteDeliveryMessage(found, 2)).toBe(true); + expect(isFirstNoteDeliveryMessage(found, 6)).toBe(false); + expect(noteCallCardLabel(found, item)).toBe('Note updated'); + }); + + it('is not compared with the stored note', () => { + // The rule below is about saved calls. The text path is as it was. + const found = deliveries([user(1), assistant(2, SEPARATOR_NOTE)], { noteUpdatedAt: 0 }); + expect(found.firstKey).toBe(noteMessageKey(2)); + }); + }); + + describe('the final turn, against the stored note', () => { + const turn = [user(1), noteCall(2), assistant(3, 'Done')]; + + it.each([ + ['newer than the call: the turn changed the note', 4, [callKey(2)]], + ['as old as the call', 2, [callKey(2)]], + ['older than the call: the note was not changed by it', 1, []], + ['never stored', 0, []], + ])('reads a note %s', (_label, noteUpdatedAt, expected) => { + expect(cards(turn, { noteUpdatedAt })).toEqual(expected); + }); + + it('shows the card when the pane has no note to compare with', () => { + expect(cards(turn)).toEqual([callKey(2)]); + expect(cards(turn, { noteUpdatedAt: null })).toEqual([callKey(2)]); + }); + + it('shows the card while the session is running', () => { + // The note is stored after the turn, and the turn may then wait on + // background work. Until the session settles there is nothing to + // compare, so the saved call is a card as soon as it completes. + expect(cards(turn, { noteUpdatedAt: 1, sessionLive: true })).toEqual([callKey(2)]); + }); + + it('keeps the row once that session settles with the note unchanged', () => { + expect(cards(turn, { noteUpdatedAt: 1, sessionLive: false })).toEqual([]); + }); + + it('does not compare the calls of earlier turns', () => { + // The first turn stored the note at 4. The second republished it + // unchanged, which keeps the timestamp: only that call keeps its row. + const rows = [ + ...turn, + user(5, 'Is the plan still right?'), + noteCall(6), + assistant(7, 'Done'), + ]; + const found = deliveries(rows, { noteUpdatedAt: 4 }); + expect([...found.cardToolKeys]).toEqual([callKey(2)]); + expect(found.firstKey).toBe(callKey(2)); + }); + + it('leaves an earlier card alone when the final turn has no call', () => { + // A chat question after the note: the note is older than everything in + // the final turn, and the earlier call is still the card it was. + const rows = [...turn, user(5, 'Why that order?'), assistant(6, 'The cookie is set first.')]; + expect(cards(rows, { noteUpdatedAt: 4 })).toEqual([callKey(2)]); + }); + + it('treats a transcript with no user message as one turn', () => { + // The window can start after the prompt of a long turn. + const rows = [noteCall(2), assistant(3, 'Done')]; + expect(cards(rows, { noteUpdatedAt: 1 })).toEqual([]); + expect(cards(rows, { noteUpdatedAt: 4 })).toEqual([callKey(2)]); + }); + + it('reads each saved call of the turn on its own', () => { + const rows = [user(1), noteCall(2), assistant(3, 'One more section.'), noteCall(4)]; + expect(cards(rows, { noteUpdatedAt: 5 })).toEqual([callKey(2), callKey(4)]); + expect(cards(rows, { noteUpdatedAt: 1 })).toEqual([]); + }); + + it('has no first delivery when the only call kept its row', () => { + // A first run that failed before the note was stored. + expect(deliveries(turn, { noteUpdatedAt: 0 }).firstKey).toBeNull(); + }); + + /** Whether the pane offers "Ask for the note to be updated" under the turn. */ + function buttonShown(rows: SessionMessage[], noteUpdatedAt: number): boolean { + const session = { status: 'completed', completionReason: 'turn_complete' } as Session; + const note: LinkedNoteContext = { + id: 'note-1', + title: 'Plan', + content: 'Fix the test.', + updatedAt: noteUpdatedAt, + hasParsedNote: true, + }; + return shouldAskForNoteUpdate(session, rows, note); + } + + it.each([ + ['a turn that changed the note', 4], + ['a note stored with the closing message', 3], + ['an identical republish', 1], + ['a note that was never stored', 0], + ])('is the complement of the follow-up button for %s', (_label, noteUpdatedAt) => { + // The button reads the same timestamp: it is offered when the newest + // assistant message is newer than the note. The note is stored after + // the turn, so a turn that ends with a saved call shows one of the two. + const card = cards(turn, { noteUpdatedAt }).length > 0; + expect(card).toBe(!buttonShown(turn, noteUpdatedAt)); + }); + + it('shows both when the agent wrote more after the note was stored', () => { + // Not a state a turn reaches by itself: the note is stored once the + // run ends. A message that arrives later, with no prompt before it, is + // newer than the note. The call did change the note, so it is a card, + // and the new text is not in the note, so the button is offered. + const rows = [...turn, assistant(5, 'The background check has finished: all green.')]; + expect(cards(rows, { noteUpdatedAt: 4 })).toEqual([callKey(2)]); + expect(buttonShown(rows, 4)).toBe(true); + }); + }); +}); + +describe('stabilizeNoteDeliveries', () => { + const saved = [user(1), noteCall(2), assistant(3, 'Done')]; + + it('takes the first result as it is', () => { + const next = deliveries(saved); + expect(stabilizeNoteDeliveries(null, next)).toBe(next); + }); + + it('keeps the earlier object when a rebuild changes no delivery', () => { + const previous = deliveries(saved); + // The same deliveries, from a transcript that grew by a reply. + const next = deliveries([...saved, user(4, 'Thanks'), assistant(5, 'Any time.')]); + expect(next).not.toBe(previous); + expect(stabilizeNoteDeliveries(previous, next)).toBe(previous); + }); + + it('hands every empty transcript the same object', () => { + expect(deliveries([user(1)])).toBe(deliveries([user(1), assistant(2, 'Hello')])); + }); + + it('takes the new result when a call becomes a card', () => { + const previous = deliveries([user(1), noteCall(2, 'in_progress')]); + const next = deliveries(saved); + expect(stabilizeNoteDeliveries(previous, next)).toBe(next); + }); + + it('takes the new result when a card goes back to a row', () => { + const previous = deliveries(saved, { noteUpdatedAt: 1, sessionLive: true }); + const next = deliveries(saved, { noteUpdatedAt: 1, sessionLive: false }); + expect(stabilizeNoteDeliveries(previous, next)).toBe(next); + }); + + it('takes the new result when another call is saved', () => { + const previous = deliveries(saved); + const next = deliveries([...saved, user(4), noteCall(5)]); + expect(stabilizeNoteDeliveries(previous, next)).toBe(next); + }); + + it('takes the new result when the same number of cards has other keys', () => { + const previous = deliveries([user(1), noteCall(2)]); + const next = deliveries([user(1), noteCall(3)]); + expect(stabilizeNoteDeliveries(previous, next)).toBe(next); + }); + + it('takes the new result when only the first delivery differs', () => { + // An older page of the transcript loads and brings an earlier delivery. + const previous = deliveries([user(3), noteCall(4)]); + const next = deliveries([user(1), assistant(2, SEPARATOR_NOTE), user(3), noteCall(4)]); + expect([...next.cardToolKeys]).toEqual([...previous.cardToolKeys]); + expect(stabilizeNoteDeliveries(previous, next)).toBe(next); + }); +}); diff --git a/apps/staged/src/lib/features/sessions/noteDeliveries.ts b/apps/staged/src/lib/features/sessions/noteDeliveries.ts new file mode 100644 index 000000000..8a4929ed6 --- /dev/null +++ b/apps/staged/src/lib/features/sessions/noteDeliveries.ts @@ -0,0 +1,191 @@ +import type { SessionMessage } from '../../types'; +import type { AcpTranscriptGroup, RichToolItem } from './acpTranscript'; +import { noteActivityLabel } from './noteFreshness'; +import { splitAtNoteIndicator } from './noteIndicators'; +import { writeNoteToolCall } from './noteToolCall'; + +/** + * The points in a note session's transcript where a note was delivered, which + * the chat pane marks with the note card ("Note created", "Note updated"). + * + * A note reaches Staged in one of two shapes (see `docs/note-delivery.md`): + * + * - a `write_note` call that resolved to `completed`: the tool path, which + * every session with the tool attached uses; + * - an assistant message that holds the `---` note separator: the fallback + * for a run with no tool, and every session recorded before the tool. + * + * The card is drawn from the stored rows alone. Nothing in the backend records + * "the note was updated", so this list is the one place that decides it. + */ +export interface NoteDeliveries { + /** + * The key of the first delivery the transcript holds, the one that created + * the note: a tool item key, or `noteMessageKey` of an assistant message. + * `null` when the transcript holds no delivery. + */ + firstKey: string | null; + /** + * The keys of the tool items the pane shows as the note card in place of + * their "Saved note" row. + */ + cardToolKeys: ReadonlySet<string>; +} + +export interface NoteDeliveryOptions { + /** + * `updatedAt` of the note the pane is opened on. Absent when the pane has + * no note context, and then every saved call is a card. + */ + noteUpdatedAt?: number | null; + /** + * The session is running. Its final turn has not reached the hooks that + * store the note, so the stored note cannot yet be compared with the call. + */ + sessionLive?: boolean; +} + +const NO_DELIVERIES: NoteDeliveries = { firstKey: null, cardToolKeys: new Set() }; + +/** The delivery key of an assistant message that holds the note separator. */ +export function noteMessageKey(messageId: number): string { + return `message:${messageId}`; +} + +/** + * Lists the deliveries in the transcript groups `buildAcpTranscriptGroups` + * produces. + * + * A saved call is a card, with one exception, the final-turn rule. In the + * final turn of a session that is no longer running, and with a note context + * to compare with, the call is a card only when the stored note is at least + * as new as the call row. The note is stored after the turn, so a turn that + * changed it stamps it later than its own call. A note that is older was not + * changed by that call: the agent republished an identical note, which keeps + * its timestamp, or the run failed before the note was stored. Such a call + * keeps its "Saved note" row, and the pane's "Ask for the note to be updated" + * button, which reads the same timestamp, shows under it. A turn that ends + * with a saved call therefore shows the card or the button, not both. + * + * The final turn is everything after the last user message, the reading the + * transcript builder uses for calls that never settled. Calls of earlier + * turns are not compared: a later turn has moved the note on since. + * + * A call that was rejected, interrupted or is still running is never a + * delivery and keeps its tool row. + */ +export function listNoteDeliveries( + groups: readonly AcpTranscriptGroup[], + options: NoteDeliveryOptions = {} +): NoteDeliveries { + // The stored note's time, when the final turn's calls are compared with it. + const storedAt = + typeof options.noteUpdatedAt === 'number' && !options.sessionLive + ? options.noteUpdatedAt + : null; + let finalTurnStart = -1; + if (storedAt !== null) { + for (let index = groups.length - 1; index >= 0; index--) { + if (groups[index].type === 'user') { + finalTurnStart = index; + break; + } + } + } + + let firstKey: string | null = null; + const cardToolKeys = new Set<string>(); + for (let index = 0; index < groups.length; index++) { + const group = groups[index]; + if (group.type === 'assistant') { + if (messageHasNoteIndicator(group.message)) firstKey ??= noteMessageKey(group.message.id); + } else if (group.type === 'tools') { + for (const item of group.items) { + if (!isSavedNoteCall(item)) continue; + const unchangedByCall = + storedAt !== null && index > finalTurnStart && storedAt < item.call.createdAt; + if (unchangedByCall) continue; + cardToolKeys.add(item.key); + firstKey ??= item.key; + } + } + } + + return firstKey === null ? NO_DELIVERIES : { firstKey, cardToolKeys }; +} + +/** + * `previous` when `next` says the same thing, so a rebuild that changed no + * delivery hands the pane the object it already has. The transcript rebuilds + * twice a second while a session streams, and every tool group reads this. + */ +export function stabilizeNoteDeliveries( + previous: NoteDeliveries | null, + next: NoteDeliveries +): NoteDeliveries { + if (!previous || previous === next) return next; + if (previous.firstKey !== next.firstKey) return next; + if (previous.cardToolKeys.size !== next.cardToolKeys.size) return next; + for (const key of next.cardToolKeys) { + if (!previous.cardToolKeys.has(key)) return next; + } + return previous; +} + +/** Whether a tool item is shown as the note card. */ +export function isNoteCardItem(deliveries: NoteDeliveries, item: RichToolItem): boolean { + return deliveries.cardToolKeys.has(item.key); +} + +/** + * The label of the card shown in place of a saved call's row. A saved call is + * never "Writing note": while the call runs, its "Saving note" row says so. + */ +export function noteCallCardLabel( + deliveries: NoteDeliveries, + item: RichToolItem +): ReturnType<typeof noteActivityLabel> { + return noteActivityLabel({ + isLive: false, + isLastGroup: false, + isFirstNoteMessage: deliveries.firstKey === item.key, + }); +} + +/** Whether an assistant message is the first delivery in the transcript. */ +export function isFirstNoteDeliveryMessage(deliveries: NoteDeliveries, messageId: number): boolean { + return deliveries.firstKey === noteMessageKey(messageId); +} + +/** + * Scans keyed on the row object. Rows keep their identity across polls except + * the re-fetched tail, so stale entries evict themselves. + */ +const noteIndicatorCache = new WeakMap<SessionMessage, boolean>(); +const writeNoteCallCache = new WeakMap<SessionMessage, boolean>(); + +function messageHasNoteIndicator(message: SessionMessage): boolean { + let hasNote = noteIndicatorCache.get(message); + if (hasNote === undefined) { + hasNote = splitAtNoteIndicator(message.content).hasNote; + noteIndicatorCache.set(message, hasNote); + } + return hasNote; +} + +/** + * Whether a tool item is a `write_note` call that resolved to `completed`. + * The call is recognised by its tool name, not by its row label, and the + * status is the resolved one, so a remote call completed from the outbox + * counts as saved. + */ +function isSavedNoteCall(item: RichToolItem): boolean { + if (item.status !== 'completed') return false; + let isNoteCall = writeNoteCallCache.get(item.call); + if (isNoteCall === undefined) { + // The name is in the row's content; `rawInput` only fills in arguments. + isNoteCall = writeNoteToolCall(item.call.content, undefined) !== null; + writeNoteCallCache.set(item.call, isNoteCall); + } + return isNoteCall; +} diff --git a/apps/staged/src/lib/features/sessions/noteDoneMessage.test.ts b/apps/staged/src/lib/features/sessions/noteDoneMessage.test.ts new file mode 100644 index 000000000..824e22319 --- /dev/null +++ b/apps/staged/src/lib/features/sessions/noteDoneMessage.test.ts @@ -0,0 +1,303 @@ +import { describe, expect, it } from 'vitest'; +import type { SessionMessage } from '../../types'; +import { + buildAcpTranscriptGroups, + type AcpTranscriptGroup, + type AcpTranscriptOptions, + type ToolStatus, +} from './acpTranscript'; +import { doneTextCases } from './testStubs/noteDoneCases'; +import { NOTE_DONE_WORD, hiddenNoteDoneMessageIds, isNoteDoneText } from './noteDoneMessage'; + +describe('isNoteDoneText', () => { + // The cases live in testStubs/noteDone.cases.json, which the backend's copy of this + // rule runs too (src-tauri/src/note_done/tests.rs): the drift log and this + // filter must agree on what counts as "Done". + it.each(doneTextCases.accept)('accepts %s', (_label, text) => { + expect(isNoteDoneText(text)).toBe(true); + }); + + it.each(doneTextCases.reject)('rejects %s', (_label, text) => { + expect(isNoteDoneText(text)).toBe(false); + }); + + it('rejects a missing text', () => { + expect(isNoteDoneText(null)).toBe(false); + expect(isNoteDoneText(undefined)).toBe(false); + }); + + it('accepts the word exactly as it is exported', () => { + // The tool result quotes this word; the wording tests read it from here. + expect(NOTE_DONE_WORD).toBe('Done'); + expect(isNoteDoneText(NOTE_DONE_WORD)).toBe(true); + }); +}); + +function message( + overrides: Partial<SessionMessage> & Pick<SessionMessage, 'id' | 'role'> +): SessionMessage { + return { + sessionId: 'session-1', + content: overrides.content ?? '', + createdAt: overrides.id, + ...overrides, + }; +} + +const user = (id: number, content = 'Write the note') => message({ id, role: 'user', content }); +const assistant = (id: number, content: string) => message({ id, role: 'assistant', content }); + +const NOTE_INPUT = { title: 'Plan' }; + +/** A `write_note` call row, in Claude Code's spelling unless `name` says otherwise. */ +function noteCall(id: number, status: ToolStatus = 'completed', name = 'mcp__staged__write_note') { + return message({ + id, + role: 'tool_call', + content: JSON.stringify({ name, input: NOTE_INPUT }), + acpEventKind: 'tool_call', + acpToolCallId: `tc-${id}`, + acpToolStatus: status, + acpRawInput: NOTE_INPUT, + }); +} + +/** Any other tool call row. */ +function otherCall(id: number, status: ToolStatus = 'completed', name = 'Bash') { + return message({ + id, + role: 'tool_call', + content: JSON.stringify({ name, input: { command: 'git status' } }), + acpEventKind: 'tool_call', + acpToolCallId: `tc-${id}`, + acpToolStatus: status, + }); +} + +function hiddenIds(rows: SessionMessage[], options: AcpTranscriptOptions = {}): number[] { + return [...hiddenNoteDoneMessageIds(buildAcpTranscriptGroups(rows, [], undefined, options))]; +} + +describe('hiddenNoteDoneMessageIds', () => { + it('hides a bare Done after a saved call', () => { + expect(hiddenIds([user(1), noteCall(2), assistant(3, 'Done')])).toEqual([3]); + }); + + it.each([ + ['Claude Code', 'mcp__staged__write_note'], + ['Goose', 'staged: write note'], + ['Codex', 'mcp.staged.write_note'], + ])('finds the call as %s names it', (_agent, name) => { + expect(hiddenIds([user(1), noteCall(2, 'completed', name), assistant(3, 'Done')])).toEqual([3]); + }); + + it('finds a call whose row holds only the tool name', () => { + // No arguments on the row: recognised by name, not by its label. + const call = message({ + id: 2, + role: 'tool_call', + content: 'mcp.staged.write_note', + acpEventKind: 'tool_call', + acpToolCallId: 'tc-2', + acpToolStatus: 'completed', + }); + expect(hiddenIds([user(1), call, assistant(3, 'Done')])).toEqual([3]); + }); + + it('hides the tolerated spellings of the word', () => { + expect(hiddenIds([user(1), noteCall(2), assistant(3, '**Done.**\n')])).toEqual([3]); + }); + + it('keeps the reply written before the call', () => { + const rows = [ + user(1, 'Which file holds the bug?'), + assistant(2, 'The bug is in `src/main.rs`.'), + noteCall(3), + assistant(4, 'Done'), + ]; + expect(hiddenIds(rows)).toEqual([4]); + }); + + it('shows the message after a rejected call', () => { + expect(hiddenIds([user(1), noteCall(2, 'failed'), assistant(3, 'Done')])).toEqual([]); + }); + + it('shows the message while the call is still running', () => { + expect(hiddenIds([user(1), noteCall(2, 'in_progress'), assistant(3, 'Done')])).toEqual([]); + expect(hiddenIds([user(1), noteCall(2, 'pending'), assistant(3, 'Done')])).toEqual([]); + }); + + it('shows the message after an interrupted call', () => { + // The call never settled and the session ended in error or was cancelled: + // the row reads "Note not saved". + const rows = [user(1), noteCall(2, 'in_progress'), assistant(3, 'Done')]; + expect(hiddenIds(rows, { sessionSettled: true, sessionCompleted: false })).toEqual([]); + }); + + it('shows the message after a call the user cancelled', () => { + expect(hiddenIds([user(1), noteCall(2, 'cancelled'), assistant(3, 'Done')])).toEqual([]); + }); + + it('hides it after a remote call completed from the outbox', () => { + // The call's update was lost with the connection, but the session + // completed from the workstation outbox: the row resolves to "Saved note". + const rows = [user(1), noteCall(2, 'in_progress'), assistant(3, 'Done')]; + expect(hiddenIds(rows, { sessionSettled: true, sessionCompleted: true })).toEqual([3]); + }); + + it('shows the message when a tool call follows it', () => { + const rows = [user(1), noteCall(2), assistant(3, 'Done'), otherCall(4)]; + expect(hiddenIds(rows)).toEqual([]); + }); + + it('shows the message when a tool call and more text follow it', () => { + const rows = [user(1), noteCall(2), assistant(3, 'Done'), otherCall(4), assistant(5, 'Done')]; + // The last one follows a tool group with no `write_note` item. + expect(hiddenIds(rows)).toEqual([]); + }); + + it('shows both messages when more text follows the Done', () => { + const rows = [ + user(1), + noteCall(2), + assistant(3, 'Done'), + assistant(4, 'The note covers the three failing tests.'), + ]; + expect(hiddenIds(rows)).toEqual([]); + }); + + it('shows both messages when a reply comes between the call and the Done', () => { + const rows = [ + user(1), + noteCall(2), + assistant(3, 'The bug is on line 12 of `src/main.rs`.'), + assistant(4, 'Done'), + ]; + expect(hiddenIds(rows)).toEqual([]); + }); + + it('shows text that continues after the word in the same message', () => { + expect(hiddenIds([user(1), noteCall(2), assistant(3, 'Done, the note is saved.')])).toEqual([]); + }); + + it('shows an answer that ends with a Done line, whole', () => { + const rows = [user(1), noteCall(2), assistant(3, 'The bug is in `src/main.rs`.\n\nDone')]; + expect(hiddenIds(rows)).toEqual([]); + }); + + it('hides two Done messages in a row', () => { + // A background task woke the session after the turn and the agent + // answered with a second one. + const rows = [user(1), noteCall(2), assistant(3, 'Done'), assistant(4, 'Done.')]; + expect(hiddenIds(rows)).toEqual([3, 4]); + }); + + it("ignores Codex's extra item after the call in the same tool group", () => { + // Codex announces a review item after `write_note` and completes it + // before the call's own result; both land in one tool group. + const review = message({ + id: 3, + role: 'tool_call', + content: 'Guardian Review', + acpEventKind: 'tool_call', + acpToolCallId: 'tc-review', + acpToolStatus: 'completed', + }); + const rows = [ + user(1), + noteCall(2, 'completed', 'mcp.staged.write_note'), + review, + assistant(4, 'Done'), + ]; + const groups = buildAcpTranscriptGroups(rows, []); + expect(groups.map((group) => group.type)).toEqual(['user', 'tools', 'assistant']); + expect([...hiddenNoteDoneMessageIds(groups)]).toEqual([4]); + }); + + it('ignores a shell call issued beside the note call', () => { + const rows = [user(1), otherCall(2), noteCall(3), otherCall(4, 'failed'), assistant(5, 'Done')]; + expect(hiddenIds(rows)).toEqual([5]); + }); + + it('goes by the last write_note item of the tool group', () => { + // Rejected, fixed, saved: the last call stands. + expect(hiddenIds([user(1), noteCall(2, 'failed'), noteCall(3), assistant(4, 'Done')])).toEqual([ + 4, + ]); + // Saved, then a second call rejected: the note row above reads "Note rejected". + expect(hiddenIds([user(1), noteCall(2), noteCall(3, 'failed'), assistant(4, 'Done')])).toEqual( + [] + ); + }); + + it('shows a Done that no note call precedes', () => { + expect(hiddenIds([user(1), assistant(2, 'Done')])).toEqual([]); + expect(hiddenIds([user(1), otherCall(2), assistant(3, 'Done')])).toEqual([]); + expect(hiddenIds([assistant(1, 'Done')])).toEqual([]); + }); + + it("shows a Done after another server's write_note", () => { + const rows = [ + user(1), + noteCall(2, 'completed', 'mcp__other__write_note'), + assistant(3, 'Done'), + ]; + expect(hiddenIds(rows)).toEqual([]); + }); + + it('shows a Done when a saved call is separated from it by a user message', () => { + const rows = [user(1), noteCall(2), user(3, 'Are you finished?'), assistant(4, 'Done')]; + expect(hiddenIds(rows)).toEqual([]); + }); + + it('hides the Done of every turn that ends with one', () => { + const rows = [ + user(1), + noteCall(2), + assistant(3, 'Done'), + user(4, 'Which file holds the bug?'), + assistant(5, 'It is `src/main.rs`.'), + user(6, 'Add that to the note'), + noteCall(7), + assistant(8, 'done'), + ]; + expect(hiddenIds(rows)).toEqual([3, 8]); + }); + + it('judges each turn on its own', () => { + const rows = [ + user(1), + noteCall(2), + assistant(3, 'Done'), + user(4, 'Add the file name'), + noteCall(5), + assistant(6, 'Done. I added the file name under "Cause".'), + ]; + expect(hiddenIds(rows)).toEqual([3]); + }); + + it('hides nothing in a turn that ends at the call', () => { + expect(hiddenIds([user(1), noteCall(2)])).toEqual([]); + expect(hiddenIds([])).toEqual([]); + }); + + it('steps over acp groups, which render nothing', () => { + const [userGroup, toolsGroup, doneGroup] = buildAcpTranscriptGroups( + [user(1), noteCall(2), assistant(4, 'Done')], + [] + ); + const acp = (id: number): AcpTranscriptGroup => ({ + type: 'acp', + event: { + id, + kind: 'usage_update', + title: 'Usage', + content: null, + message: message({ id, role: 'assistant' }), + }, + }); + expect([ + ...hiddenNoteDoneMessageIds([userGroup, toolsGroup, acp(3), doneGroup, acp(5)]), + ]).toEqual([4]); + }); +}); diff --git a/apps/staged/src/lib/features/sessions/noteDoneMessage.ts b/apps/staged/src/lib/features/sessions/noteDoneMessage.ts new file mode 100644 index 000000000..437750395 --- /dev/null +++ b/apps/staged/src/lib/features/sessions/noteDoneMessage.ts @@ -0,0 +1,102 @@ +import type { AcpTranscriptGroup } from './acpTranscript'; +import { writeNoteToolCall } from './noteToolCall'; + +/** + * The closing message a note session is asked to send once `write_note` has + * saved its note. The tool result asks for this one word so the turn ends + * without a recap of the note; the chat pane hides the message, since the + * "Saved note" row above it already says what happened. The row itself stays + * stored: hiding is a rendering decision, so a wrong match loses nothing. + */ +export const NOTE_DONE_WORD = 'Done'; + +/** Wrappers an agent may put around the word: Markdown emphasis, code, quotes. */ +const WRAPPERS: ReadonlyArray<readonly [open: string, close: string]> = [ + // `**` before `*`, so bold is taken off whole rather than as one asterisk. + ['**', '**'], + ['*', '*'], + ['_', '_'], + ['`', '`'], + ['"', '"'], + ["'", "'"], + ['“', '”'], + ['‘', '’'], +]; + +/** + * Whether an assistant message's text is nothing but the closing word: + * `Done`, in any case, optionally in one pair of wrappers and with one + * trailing `.` or `!`. Anything more is real text: `Done ✅`, a sentence that + * starts with the word, an answer whose last line is `Done`. + */ +export function isNoteDoneText(text: string | null | undefined): boolean { + if (typeof text !== 'string') return false; + let word = text.trim(); + for (const [open, close] of WRAPPERS) { + if ( + word.length >= open.length + close.length && + word.startsWith(open) && + word.endsWith(close) + ) { + word = word.slice(open.length, word.length - close.length); + break; + } + } + if (word.endsWith('.') || word.endsWith('!')) word = word.slice(0, -1); + return word.toLowerCase() === NOTE_DONE_WORD.toLowerCase(); +} + +/** + * The ids of the assistant messages the chat pane hides: the bare "Done" that + * closes a turn after a saved `write_note` call. + * + * A message is hidden only when all of these hold: + * + * 1. A saved call comes directly before it: the nearest earlier group is a + * tool group whose last `write_note` item resolved to `completed`. Other + * items in that group do not matter. Codex completes a review item after + * the call and Claude sometimes issues a shell call beside it. + * 2. Every assistant message between that tool group and the end of the turn + * is a bare "Done" (see `isNoteDoneText`). A background task that wakes + * the session can add a second one, and both are hidden. A recap after + * the first shows both. + * 3. The turn ends there: what follows is the next user message or the end + * of the transcript, not a tool call. + * + * `acp` groups render nothing and are stepped over throughout. + * + * The test is keyed on the call, so no other message can match, and it fails + * open: on any mismatch the text is shown whole, exactly as written. + */ +export function hiddenNoteDoneMessageIds(groups: readonly AcpTranscriptGroup[]): Set<number> { + const hidden = new Set<number>(); + for (let index = 0; index < groups.length; index++) { + const group = groups[index]; + if (group.type !== 'tools' || !endsWithSavedNote(group)) continue; + + const run: number[] = []; + let next = index + 1; + let allDone = true; + for (; next < groups.length; next++) { + const follower = groups[next]; + if (follower.type === 'acp') continue; + if (follower.type !== 'assistant') break; + if (!isNoteDoneText(follower.message.content)) allDone = false; + run.push(follower.message.id); + } + const endsTurn = next >= groups.length || groups[next].type === 'user'; + if (allDone && endsTurn) { + for (const id of run) hidden.add(id); + } + } + return hidden; +} + +/** Whether the last `write_note` item of a tool group is a saved call. */ +function endsWithSavedNote(group: Extract<AcpTranscriptGroup, { type: 'tools' }>): boolean { + for (let index = group.items.length - 1; index >= 0; index--) { + const item = group.items[index]; + if (writeNoteToolCall(item.call.content, item.rawInput)) return item.status === 'completed'; + } + return false; +} diff --git a/apps/staged/src/lib/features/sessions/noteDoneWording.test.ts b/apps/staged/src/lib/features/sessions/noteDoneWording.test.ts new file mode 100644 index 000000000..3907653ce --- /dev/null +++ b/apps/staged/src/lib/features/sessions/noteDoneWording.test.ts @@ -0,0 +1,118 @@ +import { describe, expect, it } from 'vitest'; +import remoteScriptSource from '../../../../src-tauri/resources/remote/staged-mcp.cjs?raw'; +import noteToolSource from '../../../../src-tauri/src/note_tool.rs?raw'; +import sessionCommandsSource from '../../../../src-tauri/src/session_commands.rs?raw'; +import { NOTE_DONE_WORD, isNoteDoneText } from './noteDoneMessage'; + +// The backend asks a note session to close its turn with one quoted word, and +// the chat pane hides a message that is nothing but that word. Nothing at +// runtime connects the two: the request is text in Rust and in the script +// uploaded to Blox, and the filter is `isNoteDoneText` here. These tests read +// the backend sources as text, so rewording either side without the other +// fails here instead of leaving a visible closing word in every note chat. + +/** + * The phrase that carries the closing word, in each source's own spelling: + * `"Done"` in a single-quoted JavaScript string, `\"Done\"` in a Rust or + * double-quoted JavaScript one. + */ +const CLOSING_REQUEST = /send a simple \\?"([^"\\]*)\\?" message/g; + +/** + * Source text with its string literals joined back up, so a phrase wrapped + * across lines still reads as one: Rust's `\` line continuations and + * JavaScript's `"a " + "b"` joins. + */ +function joinStringLiterals(source: string): string { + return source.replace(/\\\r?\n[ \t]*/g, '').replace(/(["'])\s*\+\s*(["'])/g, ''); +} + +/** The text from `start` up to the first `end` after it. */ +function sourceBetween(source: string, start: string, end: string): string { + const from = source.indexOf(start); + expect(from, `"${start}" was not found; update this test with the rename`).toBeGreaterThan(-1); + const to = source.indexOf(end, from + start.length); + expect(to, `no "${end}" after "${start}"`).toBeGreaterThan(from); + return joinStringLiterals(source.slice(from, to)); +} + +/** Every word a text asks the agent to close its turn with. */ +function closingWords(text: string): string[] { + return [...text.matchAll(CLOSING_REQUEST)].map((match) => match[1]); +} + +const resultTexts: ReadonlyArray<readonly [label: string, text: string]> = [ + [ + 'the local tool result (note_tool.rs saved_message)', + sourceBetween(noteToolSource, 'pub(crate) fn saved_message(title: &str', '\n}\n'), + ], + [ + 'the Blox tool result (staged-mcp.cjs savedMessage)', + sourceBetween(remoteScriptSource, 'function savedMessage(title', '\n}\n'), + ], +]; + +const guidanceTexts: ReadonlyArray<readonly [label: string, text: string]> = [ + [ + 'the local tool description (note_tool.rs #[tool] attribute)', + sourceBetween(noteToolSource, '#[tool(\n description = ', '\n )]'), + ], + [ + 'the shared tool description (note_tool.rs WRITE_NOTE_DESCRIPTION)', + sourceBetween(noteToolSource, 'pub(crate) const WRITE_NOTE_DESCRIPTION', ';\n'), + ], + [ + 'the Blox tool description (staged-mcp.cjs WRITE_NOTE_DESCRIPTION)', + sourceBetween(remoteScriptSource, 'const WRITE_NOTE_DESCRIPTION =', ';\n'), + ], + [ + 'the session prompt (session_commands.rs note_delivery_guidance)', + sourceBetween(sessionCommandsSource, 'pub(crate) fn note_delivery_guidance(', '\n}\n'), + ], +]; + +describe('the closing word the backend asks for', () => { + it.each(resultTexts)('%s quotes the one word the filter hides', (_label, text) => { + expect(closingWords(text)).toEqual([NOTE_DONE_WORD]); + expect(isNoteDoneText(closingWords(text)[0])).toBe(true); + }); + + it.each(guidanceTexts)('%s asks for the same word', (_label, text) => { + expect(closingWords(text)).toEqual([NOTE_DONE_WORD]); + expect(isNoteDoneText(closingWords(text)[0])).toBe(true); + }); + + it('gives local and Blox sessions the same instruction after the saved sentence', () => { + // Everything from the byte count on is fixed text; what comes before it + // is built from the title and length, in each language's own syntax. + const instructions = resultTexts.map(([, text]) => { + const match = /bytes from the draft file\)\. (.*?)\\?["']\s*\)/s.exec(text); + expect(match, text).not.toBeNull(); + return match![1].replace(/\\"/g, '"'); + }); + expect(instructions[0]).toBe( + 'If the user asked you a question this turn and you have not answered it in chat yet, ' + + `answer it now, briefly. Otherwise send a simple "${NOTE_DONE_WORD}" message with no ` + + 'other text to end your turn, rather than summarising the work done.' + ); + expect(instructions[1]).toBe(instructions[0]); + }); +}); + +describe('reading the request out of source text', () => { + it('finds the word in each spelling of the quotes', () => { + expect(closingWords('send a simple "Done" message')).toEqual(['Done']); + expect(closingWords('send a simple \\"Done\\" message')).toEqual(['Done']); + expect(closingWords('send a simple "Finished" message')).toEqual(['Finished']); + expect(closingWords('end your turn without repeating the note')).toEqual([]); + }); + + it('reads a phrase wrapped across lines as one', () => { + expect(joinStringLiterals('send a \\\n simple \\"Done\\" message')).toBe( + 'send a simple \\"Done\\" message' + ); + expect(joinStringLiterals('"send a simple " +\n \'"Done" message\'')).toBe( + '"send a simple "Done" message\'' + ); + }); +}); diff --git a/apps/staged/src/lib/features/sessions/noteToolCall.test.ts b/apps/staged/src/lib/features/sessions/noteToolCall.test.ts index 13c5c9c32..96fa0d7a7 100644 --- a/apps/staged/src/lib/features/sessions/noteToolCall.test.ts +++ b/apps/staged/src/lib/features/sessions/noteToolCall.test.ts @@ -1,4 +1,5 @@ import { describe, expect, it } from 'vitest'; +import { writeNoteToolNameCases } from './testStubs/noteDoneCases'; import { isWriteNoteToolName, writeNoteCallFromInput, @@ -12,21 +13,21 @@ const steps = { }; describe('isWriteNoteToolName', () => { - it.each([ - 'mcp__staged__write_note', - 'staged: write note', - 'staged.write_note', - 'staged_write_note', - ])('recognises %s', (name) => { + // The cases live in testStubs/noteDone.cases.json, which the backend's copy of this + // rule runs too (src-tauri/src/note_done/tests.rs): the drift log finds the + // call by the same names as the chat pane. + it.each(writeNoteToolNameCases.accept)('recognises the %s spelling: %j', (_, name) => { expect(isWriteNoteToolName(name)).toBe(true); }); - it.each(['mcp__other__write_note', 'write_note', 'Write', '', null, undefined])( - 'rejects %s', - (name) => { - expect(isWriteNoteToolName(name)).toBe(false); - } - ); + it.each(writeNoteToolNameCases.reject)('rejects %s: %j', (_, name) => { + expect(isWriteNoteToolName(name)).toBe(false); + }); + + it('rejects a missing name', () => { + expect(isWriteNoteToolName(null)).toBe(false); + expect(isWriteNoteToolName(undefined)).toBe(false); + }); }); describe('writeNoteCallFromInput', () => { @@ -109,10 +110,24 @@ describe('writeNoteToolCall', () => { }); }); + it('reads a Codex call, whose title is `mcp.staged.write_note`', () => { + expect( + writeNoteToolCall(content('mcp.staged.write_note', { title: 'From Codex' }), undefined) + ).toEqual({ + title: 'From Codex', + suggestedNextCommitStep: null, + suggestedNextNoteStep: null, + }); + expect(writeNoteToolCall('mcp.staged.write_note', undefined)?.title).toBe(''); + }); + it('is null for any other tool', () => { expect(writeNoteToolCall(content('mcp__other__write_note', { title: 'Plan' }), undefined)).toBe( null ); + expect( + writeNoteToolCall(content('mcp.other.write_note', { title: 'Plan' }), undefined) + ).toBeNull(); expect(writeNoteToolCall('Bash', { title: 'Plan' })).toBeNull(); }); }); diff --git a/apps/staged/src/lib/features/sessions/noteToolCall.ts b/apps/staged/src/lib/features/sessions/noteToolCall.ts index b9556fdae..8c46af4b3 100644 --- a/apps/staged/src/lib/features/sessions/noteToolCall.ts +++ b/apps/staged/src/lib/features/sessions/noteToolCall.ts @@ -17,7 +17,9 @@ export interface WriteNoteCall { /** * Whether a tool name is the `staged` server's `write_note`, in any of the * spellings the bridges use: Claude Code's `mcp__staged__write_note`, - * Goose's `staged: write note`, or a bare `staged.write_note`. + * Goose's `staged: write note`, Codex's `mcp.staged.write_note`, or a bare + * `staged.write_note`. The `mcp` prefix is optional and the server must be + * `staged`, so another server's `write_note` is not a note row. */ export function isWriteNoteToolName(name: string | null | undefined): boolean { if (typeof name !== 'string') return false; @@ -25,7 +27,7 @@ export function isWriteNoteToolName(name: string | null | undefined): boolean { .trim() .toLowerCase() .replace(/[\s-]+/g, '_') - .replace(/^mcp__/, ''); + .replace(/^mcp(?:__|\.)/, ''); return /^staged(?:__|:_|_|\.)write_note$/.test(normalized); } diff --git a/apps/staged/src/lib/features/sessions/testStubs/noteDone.cases.json b/apps/staged/src/lib/features/sessions/testStubs/noteDone.cases.json new file mode 100644 index 000000000..caf725f68 --- /dev/null +++ b/apps/staged/src/lib/features/sessions/testStubs/noteDone.cases.json @@ -0,0 +1,82 @@ +{ + "about": "Shared cases for the two rules that decide whether a note turn ended in a bare \"Done\" after a saved write_note call. The chat pane's filter (noteDoneMessage.ts, noteToolCall.ts) and the backend's drift log (src-tauri/src/note_done.rs) implement each rule separately, in TypeScript and in Rust. Both test suites run every row here, so a change to one side that the other does not make fails a test. Each row is [label, input].", + "doneText": { + "accept": [ + ["the word as asked for", "Done"], + ["a trailing full stop", "Done."], + ["a trailing exclamation mark", "Done!"], + ["lower case", "done"], + ["upper case", "DONE"], + ["surrounding whitespace", " Done \n"], + ["a trailing newline, as a streamed chunk ends", "Done\n\n"], + ["a no-break space on each side", " Done "], + ["a leading byte order mark", "Done"], + ["bold", "**Done**"], + ["italic, asterisk", "*Done*"], + ["italic, underscore", "_Done_"], + ["inline code", "`Done`"], + ["double quotes", "\"Done\""], + ["single quotes", "'Done'"], + ["curly double quotes", "“Done”"], + ["curly single quotes", "‘Done’"], + ["a wrapper around the punctuation", "**Done.**"], + ["quotes around the punctuation", "\"Done!\""], + ["a wrapper, lower case, padded", "\n `done` "] + ], + "reject": [ + ["an empty message", ""], + ["whitespace only", " \n "], + ["only punctuation", "."], + ["only a wrapper", "****"], + ["an emoji after the word", "Done ✅"], + ["a sentence that starts with the word", "Done, the note is saved"], + ["a sentence after the word", "Done. The note is saved."], + ["a sentence that ends with the word", "The note is done"], + ["an answer that ends with a Done line", "The bug is in `src/main.rs`.\n\nDone"], + ["a Done line before more text", "Done\n\nOne more thing: the test is flaky."], + ["the word twice", "Done Done"], + ["two trailing marks", "Done.."], + ["an ellipsis", "Done…"], + ["another trailing mark", "Done?"], + ["two pairs of wrappers", "**`Done`**"], + ["punctuation outside the wrapper", "**Done**."], + ["a doubled underscore", "__Done__"], + ["an unclosed wrapper", "**Done"], + ["mismatched quotes", "\"Done'"], + ["whitespace inside the wrapper", "** Done **"], + ["a next-line control character after the word", "Done\u0085"], + ["a zero-width space after the word", "Done​"], + ["a longer word", "Donee"], + ["another word", "Finished"], + ["a heading", "# Done"], + ["a list item", "- Done"] + ] + }, + "writeNoteToolName": { + "accept": [ + ["Claude Code", "mcp__staged__write_note"], + ["Goose", "staged: write note"], + ["Codex", "mcp.staged.write_note"], + ["a bare dotted name", "staged.write_note"], + ["a bare underscored name", "staged_write_note"], + ["Codex in another case, padded", " MCP.Staged.Write_Note "], + ["Goose in another case, hyphenated", "Staged: Write-Note"], + ["Goose with a run of spaces", "staged: write note"] + ], + "reject": [ + ["another server's tool, Claude Code form", "mcp__other__write_note"], + ["another server's tool, Codex form", "mcp.other.write_note"], + ["another server's tool, Goose form", "other: write note"], + ["a server whose name only ends in staged", "mcp.unstaged.write_note"], + ["another tool on the staged server, Codex form", "mcp.staged.read_note"], + ["a longer tool name, Codex form", "mcp.staged.write_notes"], + ["a doubled prefix", "mcp.mcp.staged.write_note"], + ["a prefix with no separator", "mcpstaged.write_note"], + ["the tool with no server", "write_note"], + ["the Codex prefix with no server", "mcp.write_note"], + ["the name inside a longer title", "Ran mcp.staged.write_note"], + ["a built-in tool", "Write"], + ["an empty string", ""] + ] + } +} diff --git a/apps/staged/src/lib/features/sessions/testStubs/noteDoneCases.ts b/apps/staged/src/lib/features/sessions/testStubs/noteDoneCases.ts new file mode 100644 index 000000000..02ffb178a --- /dev/null +++ b/apps/staged/src/lib/features/sessions/testStubs/noteDoneCases.ts @@ -0,0 +1,39 @@ +import casesSource from './noteDone.cases.json?raw'; + +/** + * The case table shared with the backend's copies of two rules: what counts + * as a bare "Done" (`isNoteDoneText`, and `is_note_done_text` in + * `src-tauri/src/note_done/text.rs`) and which tool name is `write_note` + * (`isWriteNoteToolName`, and `is_write_note_tool_name` in + * `src-tauri/src/note_done.rs`). The chat pane hides a closing "Done" by the + * first pair's TypeScript half, and the backend logs a turn that did not end + * in one by the Rust half. Both test suites run every row, so the two cannot + * come to disagree without a failing test. Test-only: nothing at runtime + * imports this. + */ +export type SharedCase = readonly [label: string, input: string]; + +interface SharedCases { + accept: SharedCase[]; + reject: SharedCase[]; +} + +function readCases(rule: string): SharedCases { + const table = JSON.parse(casesSource) as Record<string, unknown>; + const cases = table[rule] as Partial<SharedCases> | undefined; + for (const verdict of ['accept', 'reject'] as const) { + const rows = cases?.[verdict]; + if (!Array.isArray(rows) || rows.length === 0) { + throw new Error(`noteDone.cases.json has no ${rule}.${verdict} cases`); + } + for (const row of rows) { + if (!Array.isArray(row) || row.length !== 2 || row.some((cell) => typeof cell !== 'string')) { + throw new Error(`${rule}.${verdict}: ${JSON.stringify(row)} is not [label, input]`); + } + } + } + return cases as SharedCases; +} + +export const doneTextCases = readCases('doneText'); +export const writeNoteToolNameCases = readCases('writeNoteToolName'); diff --git a/apps/staged/src/lib/features/sessions/toolCallViewModel.test.ts b/apps/staged/src/lib/features/sessions/toolCallViewModel.test.ts index 37dd36c91..4670bc96a 100644 --- a/apps/staged/src/lib/features/sessions/toolCallViewModel.test.ts +++ b/apps/staged/src/lib/features/sessions/toolCallViewModel.test.ts @@ -671,7 +671,7 @@ describe('write_note tool rows', () => { type: 'content', content: { type: 'text', - text: 'Saved note "README.md: Sandbox repository purpose" (82 bytes from the draft file). The note is stored; end your turn now without repeating it in your reply.', + text: 'Saved note "README.md: Sandbox repository purpose" (82 bytes from the draft file). If the user asked you a question this turn and you have not answered it in chat yet, answer it now, briefly. Otherwise send a simple "Done" message with no other text to end your turn, rather than summarising the work done.', }, }, ]; @@ -679,6 +679,7 @@ describe('write_note tool rows', () => { it.each([ ['Claude', 'mcp__staged__write_note'], ['Goose', 'staged: write note'], + ['Codex', 'mcp.staged.write_note'], ])( 'renders the %s form as a note with its title and next steps instead of a command', (_, name) => { diff --git a/crates/acp-client/src/driver/tests/real_bridge.rs b/crates/acp-client/src/driver/tests/real_bridge.rs index 31f937f86..f9cde1cbf 100644 --- a/crates/acp-client/src/driver/tests/real_bridge.rs +++ b/crates/acp-client/src/driver/tests/real_bridge.rs @@ -23,6 +23,11 @@ //! lines are always captured for assertions). The checks are the invariants, //! not the agent's wording: one user row, in transcript order, the turn //! resolving once, the same child across an interrupt, no hang. +//! +//! One scenario is about the agent's wording, on purpose: after Staged's +//! `write_note` tool saves a note, the agent is asked to close the turn with +//! a bare "Done" (scenario 8). It attaches the Staged app's own MCP script +//! and so also needs `node` and the `apps/staged` tree beside this crate. use super::*; use crate::driver::{SessionConnection, SessionSettled, SteerOutcome, TurnControlHandle}; @@ -108,6 +113,12 @@ enum Ev { id: String, title: String, }, + /// A later title for a tool call: bridges announce some calls under a + /// placeholder and name them in an update. + ToolTitle { + id: String, + title: String, + }, ToolResult { id: String, }, @@ -151,16 +162,24 @@ struct Trace { /// it, or [`PERMISSION_HOLD`] passes. hold_next_permission: AtomicBool, release_permission: tokio::sync::Notify, + /// The caller's answer to `should_finish_after_response`: whether the + /// response that just completed is the finished artifact. `None` never + /// finishes early, like a writer with no artifact to recognise. + finish_after_response: Option<FinishAfterResponse>, } +/// See [`Trace::finish_after_response`]. +type FinishAfterResponse = Box<dyn Fn() -> bool + Send + Sync>; + impl Trace { - fn new(label: &str) -> Self { + fn new(label: &str, finish_after_response: Option<FinishAfterResponse>) -> Self { Self { label: label.to_string(), started: Instant::now(), events: std::sync::Mutex::new(Vec::new()), hold_next_permission: AtomicBool::new(false), release_permission: tokio::sync::Notify::new(), + finish_after_response, } } @@ -208,35 +227,7 @@ impl Trace { } fn rows(&self) -> Vec<Row> { - let mut rows = Vec::new(); - let mut open = String::new(); - let flush = |open: &mut String, rows: &mut Vec<Row>| { - if !open.trim().is_empty() { - rows.push(Row::Assistant(std::mem::take(open))); - } else { - open.clear(); - } - }; - for (_, ev) in self.events() { - match ev { - Ev::Append(text) => open.push_str(&text), - Ev::Finalize => flush(&mut open, &mut rows), - Ev::User(text) => { - flush(&mut open, &mut rows); - rows.push(Row::User(text)); - } - Ev::ToolCall { title, .. } => { - flush(&mut open, &mut rows); - rows.push(Row::Tool(title)); - } - Ev::ToolResult { .. } - | Ev::Permission { .. } - | Ev::PermissionCancelled { .. } - | Ev::Event(_) => {} - } - } - flush(&mut open, &mut rows); - rows + rows_from(self.events().into_iter().map(|(_, ev)| ev)) } fn dump_rows(&self) { @@ -270,12 +261,56 @@ impl Trace { } } +/// The transcript rows `events` make: assistant text between boundaries, +/// user rows, tool calls under the title they were announced with. +fn rows_from(events: impl IntoIterator<Item = Ev>) -> Vec<Row> { + let mut rows = Vec::new(); + let mut open = String::new(); + let flush = |open: &mut String, rows: &mut Vec<Row>| { + if !open.trim().is_empty() { + rows.push(Row::Assistant(std::mem::take(open))); + } else { + open.clear(); + } + }; + for ev in events { + match ev { + Ev::Append(text) => open.push_str(&text), + Ev::Finalize => flush(&mut open, &mut rows), + Ev::User(text) => { + flush(&mut open, &mut rows); + rows.push(Row::User(text)); + } + Ev::ToolCall { title, .. } => { + flush(&mut open, &mut rows); + rows.push(Row::Tool(title)); + } + Ev::ToolTitle { .. } + | Ev::ToolResult { .. } + | Ev::Permission { .. } + | Ev::PermissionCancelled { .. } + | Ev::Event(_) => {} + } + } + flush(&mut open, &mut rows); + rows +} + #[async_trait::async_trait] impl MessageWriter for Trace { async fn append_text(&self, text: &str) { self.push(Ev::Append(text.to_string())); } + async fn should_finish_after_response(&self) -> bool { + let Some(finish) = self.finish_after_response.as_ref() else { + return false; + }; + let finish = finish(); + self.push(Ev::Event(format!("finish-after-response={finish}"))); + finish + } + async fn finalize(&self) { self.push(Ev::Finalize); } @@ -303,10 +338,16 @@ impl MessageWriter for Trace { async fn update_tool_call_title( &self, - _tool_call_id: &str, - _title: Option<&str>, + tool_call_id: &str, + title: Option<&str>, _raw_input: Option<&serde_json::Value>, ) { + if let Some(title) = title { + self.push(Ev::ToolTitle { + id: tool_call_id.to_string(), + title: title.to_string(), + }); + } } async fn record_tool_result(&self, tool_call_id: &str, _content: &str) { @@ -454,6 +495,10 @@ struct ConnectOptions { /// Environment set over the process's own for the bridge. env: Vec<(&'static str, &'static str)>, resume: Option<Resume>, + /// MCP servers handed to the agent with the session. + mcp_servers: Vec<McpServer>, + /// See [`Trace::finish_after_response`]. + finish_after_response: Option<FinishAfterResponse>, } struct Session { @@ -481,7 +526,7 @@ impl Session { .await } - async fn connect_with(label: &str, options: ConnectOptions) -> Self { + async fn connect_with(label: &str, mut options: ConnectOptions) -> Self { install_logger(); pin_bridge_dir(); let provider = provider_id(); @@ -494,6 +539,7 @@ impl Session { .unwrap_or_else(|e| panic!("{e}: put the bridge on PATH (see the module docs)")) .with_env_snapshot(env) .with_background_hold(Some(BackgroundHoldConfig::default())) + .with_mcp_servers(std::mem::take(&mut options.mcp_servers)) .with_steering_disabled(options.steering_disabled); let dir = match &options.resume { Some(resume) => resume.dir.clone(), @@ -504,7 +550,7 @@ impl Session { )), }; std::fs::create_dir_all(&dir).expect("temp working dir"); - let trace = Arc::new(Trace::new(label)); + let trace = Arc::new(Trace::new(label, options.finish_after_response.take())); let writer: Arc<dyn MessageWriter> = Arc::clone(&trace) as Arc<dyn MessageWriter>; let store = Arc::new(BridgeStore::default()); if let Some(resume) = &options.resume { @@ -1381,3 +1427,197 @@ async fn real_bridge_resume_loads_the_session_on_a_new_child() { }) .await; } + +// ----------------------------------------------------------------------------- +// Scenario 8: a saved note ends the turn with a bare "Done" +// ----------------------------------------------------------------------------- + +/// The Staged app's own rules for this scenario, compiled from its source: +/// what counts as a bare "Done", and which tool call is `write_note`. The +/// chat pane hides the closing message by the first rule and the app logs a +/// turn that fails it, so a live agent is held here to exactly what ships. +/// The file is kept free of dependencies for this include. +#[path = "../../../../../apps/staged/src-tauri/src/note_done/rules.rs"] +mod staged_note_done_rules; +use staged_note_done_rules::{is_note_done_text, is_write_note_tool_name}; + +/// The Staged session the MCP script is started for. It names the scratch +/// directory the script keeps the draft and the saved note in. +const NOTE_SESSION_ID: &str = "real-bridge-note"; +const NOTE_ORDERS_FILE: &str = "id,customer,total\n1,Ada,12.50\n2,Grace,8.00\n3,Linus,0.00\n"; + +/// A first note turn, in the words Staged uses for the draft file and the +/// call. It says nothing about how to end the turn: the closing word is asked +/// for by the tool's description and by its saved result, which are the +/// script's own, so this checks the wording that ships and not this prompt. +fn note_prompt(draft: &Path) -> String { + format!( + "Read the file `orders.txt` in the current working directory. Then write a short note \ + about it: put the note body, three short markdown bullet points on what the file \ + holds, into your note draft file at `{}` using your file tools. Then publish the note \ + by calling the `write_note` tool from the `staged` MCP server with the title `Orders \ + file`. In Claude Code the tool's full name is `mcp__staged__write_note` and it may be \ + deferred: if it is not in your loaded tools, load it with `ToolSearch \ + select:mcp__staged__write_note` first, then call it with its schema. Do not pass the \ + note body to the call, and do not write the note into your reply.", + draft.display() + ) +} + +/// Staged's MCP server as it runs on a Blox workstation, attached here over +/// stdio: its `write_note` has the description and the saved result the app +/// ships (the local tool's texts are tested equal to them in the app). +/// `home` stands in for the home directory, through `env`, so the script's +/// scratch directory is `home/.staged/sessions/<session id>` and nothing is +/// written under the real `~/.staged`. +fn staged_mcp_server(home: &Path) -> McpServer { + let script = Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../apps/staged/src-tauri/resources/remote/staged-mcp.cjs"); + let script = script + .canonicalize() + .unwrap_or_else(|e| panic!("the Staged MCP script {}: {e}", script.display())); + let node = crate::types::find_command("node") + .expect("node should be on PATH to run the Staged MCP script"); + McpServer::Stdio( + McpServerStdio::new("staged", PathBuf::from("/usr/bin/env")).args(vec![ + format!("HOME={}", home.display()), + node.to_string_lossy().into_owned(), + script.to_string_lossy().into_owned(), + NOTE_SESSION_ID.to_string(), + "turn-1".to_string(), + ]), + ) +} + +/// The transcript rows after the last `write_note` call and its tool group: +/// what the agent wrote once the note was saved. `None` when the turn holds +/// no `write_note` call. A call counts under any title it was given, since a +/// bridge may announce it under a placeholder and name it in an update. +fn rows_after_write_note_call(events: &[Ev]) -> Option<Vec<Row>> { + let named_write_note = |call_id: &str| { + events.iter().any(|ev| match ev { + Ev::ToolCall { id, title } | Ev::ToolTitle { id, title } => { + id == call_id && is_write_note_tool_name(title) + } + _ => false, + }) + }; + let call = events + .iter() + .rposition(|ev| matches!(ev, Ev::ToolCall { id, .. } if named_write_note(id)))?; + let rows = rows_from(events[call + 1..].iter().cloned()); + // Other calls beside it are the same tool group: Codex completes a review + // item there, and Claude sometimes issues a shell call in the same message. + let group_end = rows + .iter() + .position(|row| !matches!(row, Row::Tool(_))) + .unwrap_or(rows.len()); + Some(rows[group_end..].to_vec()) +} + +#[tokio::test(flavor = "current_thread")] +#[ignore = "spawns the installed ACP bridge and calls the model"] +async fn real_bridge_saved_note_turn_ends_with_a_bare_done() { + tokio::task::LocalSet::new() + .run_until(async { + pin_bridge_dir(); + let home = std::env::temp_dir().join(format!( + "acp-real-bridge-note-home-{}-{}", + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_or(0, |elapsed| elapsed.as_nanos()) + )); + let outbox = home.join(".staged/sessions").join(NOTE_SESSION_ID); + std::fs::create_dir_all(&outbox).expect("the note session's scratch dir"); + let draft = outbox.join("draft.md"); + // The script writes this marker last, once the note is saved. + let saved_marker = outbox.join("done"); + + // Like the app's writer, which finishes a response once the + // session's note is saved, whatever the assistant text says. + let marker = saved_marker.clone(); + let mut session = Session::connect_with( + "note-done", + ConnectOptions { + mcp_servers: vec![staged_mcp_server(&home)], + finish_after_response: Some(Box::new(move || marker.is_file())), + ..ConnectOptions::default() + }, + ) + .await; + std::fs::write(session.dir.join("orders.txt"), NOTE_ORDERS_FILE).expect("orders.txt"); + + let prompt = note_prompt(&draft); + let turn = tokio::time::timeout(TURN_TIMEOUT, session.connection.prompt(&prompt, &[])) + .await + .expect("the note turn should end"); + eprintln!( + "[note-done] prompt() resolved {turn:?} at {:.2}s", + session.trace.elapsed().as_secs_f64() + ); + assert_eq!(turn, Ok(AgentRunOutcome::Completed)); + + // Without a background hold in the way, the connection settles on + // its own; how it settled is read before the teardown below. + let settled = match session.settled_rx.take() { + Some(rx) => tokio::time::timeout(SETTLE_WAIT, rx) + .await + .ok() + .and_then(Result::ok), + None => None, + }; + let reason = settled.as_ref().map(|settled| settled.reason); + + let events: Vec<Ev> = session + .trace + .events() + .into_iter() + .map(|(_, ev)| ev) + .collect(); + let note = std::fs::read_to_string(outbox.join("note.md")).unwrap_or_default(); + let closing = rows_after_write_note_call(&events); + eprintln!( + "SCENARIO 8: {} saved a {}-byte note; after the write_note call: {closing:?}; \ + settled {reason:?}", + provider_id(), + note.len() + ); + assert!( + saved_marker.is_file() && !note.trim().is_empty(), + "the turn should save a note through write_note; rows: {:#?}", + session.trace.rows() + ); + let closing = closing.unwrap_or_else(|| { + panic!( + "no write_note call in the turn; rows: {:#?}", + session.trace.rows() + ) + }); + assert!( + !closing.is_empty(), + "the turn ended at the saved call, with no closing message; rows: {:#?}", + session.trace.rows() + ); + for row in &closing { + assert!( + matches!(row, Row::Assistant(text) if is_note_done_text(text)), + "after the saved call the agent should send a bare \"Done\" and nothing \ + else, but the turn went on with {row:?}; all of it: {closing:#?}" + ); + } + // The finish mark is driven by the Claude SDK's result frame, so + // only that bridge is held to it; the others end their turn the + // ordinary way, and the reason is printed above. + if provider_id() == "claude" { + assert_eq!( + reason, + Some(SessionSettleReason::ResponseComplete), + "a saved note and a completed response should release the session" + ); + } + session.finish().await; + let _ = std::fs::remove_dir_all(&home); + }) + .await; +}