Public: anyone can read this; the crew can change it.
A kept message is a successful send at every door: 202 kept, with why and when (#16471) #16593
Check · Review · Land · Live done — Landed and live in production.
fix/talk-kept-is-202 → main 0 commits 3 threads · 3 unresolved
21 files
changed +263
−91
- docs/features/talk-kept-message.md
- src/actions/build.ts
- src/actions/talk.ts
- src/contract/openapi/routes/postApiV1ConversationMessages.ts
- src/crew/conversation.ts
- src/http/api/conversation_build.ts
- src/http/api/conversation_talk.ts
- src/http/mcp/tools/say.ts
- src/tui/screens/talk_build.ts
- src/tui/screens/talk_chats.ts
- src/tui/ssh-talk.ts
- src/web/pages/talk.ts
- src/web/talk_build.ts
- test/conversation-files.test.ts
- test/conversation-talk.test.ts
- test/conversation-turn.test.ts
- test/crew-sandbox-required.test.ts
- test/talk-kept-message.test.ts
- test/talk-starting.test.ts
- test/tui-talk-chats.test.ts
- test/ui-663-talk-and-merges.test.ts
docs/features/talk-kept-message.md +30 −8
| @@ -17,9 +17,9 @@ draws **Retry**: | ||
| 17 | 17 | - the agent cannot run on this forge (no sandbox): an admin sees the cause; |
| 18 | 18 | - the forge is starting (a woken cell waits for the forge it replaces before its agent runs, which |
| 19 | 19 | has taken minutes): "This forge is starting; its agent answers within a few minutes. Your message |
| 20 | is kept; Retry then." The API answers `starting` (503) with that line and the seconds left of the | |
| 21 | boot as `Retry-After`; a Retry pressed before the agent is up is refused as `starting` and stores | |
| 22 | nothing more; | |
| 20 | is kept; Retry then." The API answers it as a kept send (202, `why` `starting`) with the seconds left | |
| 21 | of the boot as `Retry-After`; a Retry pressed before the agent is up is refused as `starting` and | |
| 22 | stores nothing more; | |
| 23 | 23 | - the forge's agent is not running (off, with no agent coming): Retry once it runs; |
| 24 | 24 | - a forge its parent lends model credit to has spent it (before the turn, or mid-answer): who can raise it (the parent forge's admin), never the raw "API Error: 402" (#16455). |
| 25 | 25 | |
| @@ -28,9 +28,31 @@ Messages queued behind one that cannot start are kept too, in the order they wer | ||
| 28 | 28 | **Retry** sends the kept message again, once: the failed line is set aside and the same words start a |
| 29 | 29 | new turn. The page says "Your message is kept. Tap Retry under it when you are ready." (`src/web/pages/talk.ts`). |
| 30 | 30 | In the terminal's Talk (`src/tui/screens/talk_chats.ts`) the line shows with " · failed", and |
| 31 | `ssh <forge> talk-again retry <turn>` retries it; the API keeps its codes (`resting` 429, `busy` | |
| 32 | 429, ...), and the kept turns are in `GET /api/v1/conversation/turns`. | |
| 31 | `ssh <forge> talk-again retry <turn>` retries it. | |
| 33 | 32 | |
| 34 | Code: `heldLine` and `isHeld` in `src/crew/conversation.ts`; the starting line is | |
| 35 | `src/crew/starting_line.ts`, kept by `keepUnstarted`, as an off one is. Tests: `test/conversation-turn.test.ts`, | |
| 36 | `test/talk-kept-message.test.ts`, `test/talk-starting.test.ts`. | |
| 33 | ## What a client gets | |
| 34 | ||
| 35 | A kept message is a send that succeeded, so every door answers it as one: | |
| 36 | ||
| 37 | - `POST /api/v1/conversation/messages` answers **202** with `{ "how": "kept", "why", "message", | |
| 38 | "turn", "until" }`: `message` is the line, `turn` the kept message's own turn. `why` is | |
| 39 | `resting` (a spent lent credit too), `busy`, `answering`, `no_account`, `sandbox`, | |
| 40 | `unauthorized`, `off` or `starting`; `starting` adds `Retry-After`, the seconds left of the | |
| 41 | boot. `until` is there only when the account said when it wakes. With `no_account` the body | |
| 42 | also carries `says` and `ask`, the call that asks an admin for one. | |
| 43 | - The MCP `say` tool returns a result (not an error) whose `says` is the line, with `kept: true`, | |
| 44 | `why`, `turn` and `until`. | |
| 45 | - `ssh <forge> talk <words>` prints `kept: <the line>` and exits 0. | |
| 46 | - A build's first message (`POST /api/v1/builds`, MCP `say` with `action: "build"`) answers | |
| 47 | `said`, `message` and `kept`. | |
| 48 | ||
| 49 | A refusal that keeps nothing stays an error: an empty or overlong message, no such conversation, | |
| 50 | more than 12 a minute, a full queue, a file already waiting, or no signed-in person. | |
| 51 | ||
| 52 | **Retry, edit and regenerate** keep nothing new: one that cannot start puts the exchange back as it | |
| 53 | was and is refused with why (the page's flash, `{ ok: false, message }` with its status, and | |
| 54 | `ssh <forge> talk-again` exiting 1), never "Asked again" (#16593). | |
| 55 | ||
| 56 | Code: `heldLine`, `Kept` and `keepUnstarted` in `src/crew/conversation.ts`; the starting line | |
| 57 | is `src/crew/starting_line.ts`. Tests: `test/conversation-turn.test.ts`, | |
| 58 | `test/talk-kept-message.test.ts`, `test/conversation-talk.test.ts`, `test/talk-starting.test.ts`. |
src/actions/build.ts +17 −5
| @@ -31,7 +31,7 @@ import { createBranch } from "./branches.ts"; | ||
| 31 | 31 | import { ensureBuildPull, type Version, versionsIn } from "./build_pull.ts"; |
| 32 | 32 | import { mergePull } from "./pulls.ts"; |
| 33 | 33 | import { deleteRepo } from "./repos.ts"; |
| 34 | import { talk, type TalkRefusal } from "./talk.ts"; | |
| 34 | import { TALK_WORDS, talk } from "./talk.ts"; | |
| 35 | 35 | |
| 36 | 36 | export { VERSIONS_MAX, type Version } from "./build_pull.ts"; |
| 37 | 37 | |
| @@ -81,16 +81,27 @@ export const BUILD_WORDS: Readonly<Record<string, string>> = { | ||
| 81 | 81 | /** A prompt as a one-line title. */ |
| 82 | 82 | const titleOf = (prompt: string): string => prompt.replace(/\s+/g, " ").trim().slice(0, TITLE_MAX); |
| 83 | 83 | |
| 84 | /** Why a build's first message did not start, and the words for it. */ | |
| 85 | export interface BuildSaid { | |
| 86 | readonly why: string; | |
| 87 | readonly message: string; | |
| 88 | /** Kept with its line (Retry sends it), rather than refused. */ | |
| 89 | readonly kept: boolean; | |
| 90 | } | |
| 91 | ||
| 92 | /** The sentence a page or the terminal says after a build whose first message did not start. */ | |
| 93 | export const buildSaidWords = (said: BuildSaid): string => (said.kept ? `The app is made. ${said.message}` : `The app is made, but ${said.message}.`); | |
| 94 | ||
| 84 | 95 | /** |
| 85 | 96 | * Make the repository from the template, start the chat on its branch and |
| 86 | * send the prompt. `said` is the refusal when the agent could not take the | |
| 87 | * words (no account, resting): the build still exists and the person can send again. | |
| 97 | * send the prompt. `said` is set when the agent did not start on it: kept with its line (resting, | |
| 98 | * busy, no account…; Retry sends it), or refused (rate limited…). The build exists either way. | |
| 88 | 99 | */ |
| 89 | 100 | export async function startBuild( |
| 90 | 101 | app: App, |
| 91 | 102 | actor: User | undefined, |
| 92 | 103 | input: { readonly prompt: string; readonly template?: string }, |
| 93 | ): Promise<Result<{ build: ChatBuild; repo: Repo; said: TalkRefusal | undefined }, BuildError>> { | |
| 104 | ): Promise<Result<{ build: ChatBuild; repo: Repo; said: BuildSaid | undefined }, BuildError>> { | |
| 94 | 105 | const user = requireUser(actor); |
| 95 | 106 | if (!user.ok) return user; |
| 96 | 107 | const template = input.template === undefined || input.template === "" ? undefined : input.template; |
| @@ -117,7 +128,8 @@ export async function startBuild( | ||
| 117 | 128 | const build = startChatBuild(app.db, conversation.value.id, made.repo.id); |
| 118 | 129 | renameConversation(app.db, user.value.id, conversation.value.id, titleOf(input.prompt)); |
| 119 | 130 | const said = talk(app, user.value, input.prompt, [], "queue", conversation.value.id); |
| 120 | return ok({ build, repo: made.repo, said: said.ok ? undefined : said.error }); | |
| 131 | const kept = said.ok ? said.value.kept : undefined; | |
| 132 | return ok({ build, repo: made.repo, said: kept !== undefined ? { why: kept.why, message: kept.line, kept: true } : said.ok ? undefined : { why: said.error, message: TALK_WORDS[said.error][1], kept: false } }); | |
| 121 | 133 | } |
| 122 | 134 | |
| 123 | 135 | /** The build of one of the person's own conversations, with the repository it is in. */ |
src/actions/talk.ts +3 −3
| @@ -32,7 +32,6 @@ export const TALK_WORDS: Readonly<Record<TalkRefusal, readonly [number, string]> | ||
| 32 | 32 | too_long: [422, "the message is too long"], |
| 33 | 33 | no_account: [403, storedText(NO_TALK_ACCOUNT, "an admin of this forge", "grant one")], |
| 34 | 34 | resting: [429, "the account that serves you is resting or out of budget; try again later"], |
| 35 | answering: [409, "the forge is still answering; send pictures when it has finished"], | |
| 36 | 35 | busy: [429, "the forge's agent is answering as many people as it can at once; try again in a minute"], |
| 37 | 36 | off: [503, "the forge's agent is not running here"], |
| 38 | 37 | starting: [503, STARTING_LINE], |
| @@ -71,10 +70,11 @@ export function talk(app: App, user: User | undefined, text: string, files: read | ||
| 71 | 70 | const options = conversationOptions(app); |
| 72 | 71 | if (options === undefined) { |
| 73 | 72 | // No agent yet: a woken forge configures it after waiting for the one it replaces (starting), |
| 74 | // or none is coming (off). Either way the message is kept with its line, never lost. | |
| 73 | // or none is coming (off). Either way the message is kept with its line, never lost: a send | |
| 74 | // that succeeded, as every kept one is. | |
| 75 | 75 | const why = startingIn(app) === undefined ? "off" : "starting"; |
| 76 | 76 | const kept = keepUnstarted(app, user.id, text, files, conversationId, why); |
| 77 | return kept.ok ? err(why) : kept; | |
| 77 | return kept.ok ? ok({ how: "kept", kept: kept.value }) : kept; | |
| 78 | 78 | } |
| 79 | 79 | const said = say(app, options, user.id, text, files, during, conversationId, about); |
| 80 | 80 | if (said.ok) watch(app, said.value); |
src/contract/openapi/routes/postApiV1ConversationMessages.ts +1 −4
| @@ -4,7 +4,7 @@ export const route: Route = { | ||
| 4 | 4 | method: "post", |
| 5 | 5 | path: "/api/v1/conversation/messages", |
| 6 | 6 | summary: "Say something to the forge's own agent, as the signed-in person", |
| 7 | detail: "Say something to the forge's own agent, as the signed-in person: it answers through the forge's MCP with that person's permissions, resuming their conversation; a message sent while it answers is handed to the turn, or with `during` (optional) set to `queue` waits for it (at most 5), or set to `steer` stops it and goes at once; answers 202 at once, read the answer from the turns; at most 12 a minute per person", | |
| 7 | detail: "Say something to the forge's own agent, as the signed-in person: it answers through the forge's MCP with that person's permissions, resuming their conversation; a message sent while it answers is handed to the turn, or with `during` (optional) set to `queue` waits for it (at most 5), or set to `steer` stops it and goes at once; answers 202 at once, read the answer from the turns; at most 12 a minute per person. A message that cannot start now (the account resting or out of credit, the forge busy, no model account, the agent not running) is kept with a line saying why and answers 202 too, as { how: \"kept\", why, message, turn, until? } (until: when the account wakes, where known; with no account the body also carries says and ask); a refusal that keeps nothing (an empty or overlong message, no such conversation, too many a minute) stays an error", | |
| 8 | 8 | auth: true, |
| 9 | 9 | role: "member", |
| 10 | 10 | status: 202, |
| @@ -17,10 +17,7 @@ export const route: Route = { | ||
| 17 | 17 | file_taken: "an attached file already waits in a queued message (409)", |
| 18 | 18 | queue_full: "five messages already wait (429)", |
| 19 | 19 | too_large: "the body is over 128000 bytes (413)", |
| 20 | no_account: "no model account is granted to the person (403); the body carries `says` and `ask`, the call that asks an admin for one", | |
| 21 | resting: "the account that serves the person is resting or out of budget (429)", | |
| 22 | 20 | rate_limited: "more than 12 messages in a minute (429)", |
| 23 | off: "the forge's agent is not running here (503)", | |
| 24 | 21 | forbidden: "the request is cross-origin", |
| 25 | 22 | }, |
| 26 | 23 | refuses: [401, 422], |
src/crew/conversation.ts +33 −13
| @@ -337,20 +337,36 @@ export function mcpConfig(localUrl: string): string { | ||
| 337 | 337 | }); |
| 338 | 338 | } |
| 339 | 339 | |
| 340 | export type SayRefusal = "not_found" | "unauthorized" | "empty" | "too_long" | "no_account" | "resting" | "busy" | "answering" | "queue_full" | "file_taken" | "sandbox"; | |
| 340 | /** | |
| 341 | * What `say` refuses: only what keeps nothing. A message that cannot start now is kept (`Kept`). | |
| 342 | */ | |
| 343 | export type SayRefusal = "not_found" | "unauthorized" | "empty" | "too_long" | "queue_full" | "file_taken"; | |
| 341 | 344 | |
| 342 | 345 | export interface Said { |
| 343 | 346 | /** |
| 344 | 347 | * `started` a new turn, `steered` words into the one running, `queued` for after it, or |
| 345 | 348 | * `interrupted` it to send these at once. |
| 346 | 349 | */ |
| 347 | readonly how: "started" | "steered" | "queued" | "interrupted" | "answered"; | |
| 350 | readonly how: "started" | "steered" | "queued" | "interrupted" | "answered" | "kept"; | |
| 348 | 351 | /** Settles when the turn does; only a started turn has one. */ |
| 349 | 352 | readonly done?: Promise<void>; |
| 350 | 353 | /** The queue id of a `queued` message, to cancel it by. */ |
| 351 | 354 | readonly queued?: string; |
| 352 | 355 | /** The text of an `answered` message, so a door with no page (the terminal) prints it at once. */ |
| 353 | 356 | readonly answer?: string; |
| 357 | /** A `kept` message: why no turn started, the line under it, its turn, and until when. */ | |
| 358 | readonly kept?: Kept; | |
| 359 | } | |
| 360 | ||
| 361 | /** | |
| 362 | * A message kept with its line because no turn could start now (#16431): the send succeeded, the | |
| 363 | * answer waits on Retry. `why` is the door's code; `until` when the account wakes, where known. | |
| 364 | */ | |
| 365 | export interface Kept { | |
| 366 | readonly why: HeldRefusal | "off" | "starting"; | |
| 367 | readonly line: string; | |
| 368 | readonly turn: string; | |
| 369 | readonly until?: string; | |
| 354 | 370 | } |
| 355 | 371 | |
| 356 | 372 | /** |
| @@ -507,9 +523,12 @@ export function say( | ||
| 507 | 523 | const refuse = (why: HeldRefusal | NotStarted): Result<Said, SayRefusal> => { |
| 508 | 524 | const reason = typeof why === "string" ? { code: why } : why; |
| 509 | 525 | release(app.db, stored.value.id); |
| 510 | appendTurn(app.db, userId, conversation.id, { role: "agent", text: heldLine(reason), failed: true }); | |
| 511 | // The doors keep their codes: a spent lent credit is a resting account to them. | |
| 512 | return err(reason.code === "credit_spent" ? "resting" : reason.code); | |
| 526 | const line = heldLine(reason); | |
| 527 | appendTurn(app.db, userId, conversation.id, { role: "agent", text: line, failed: true }); | |
| 528 | // Kept is a success of the send: the doors answer it as such, with why (a spent lent credit is | |
| 529 | // a resting account to them), the line, the turn and, where known, until when. | |
| 530 | const until = "until" in reason && typeof reason.until === "string" ? { until: reason.until } : {}; | |
| 531 | return ok({ how: "kept", kept: { why: reason.code === "credit_spent" ? "resting" : reason.code, line, turn: stored.value.id, ...until } }); | |
| 513 | 532 | }; |
| 514 | 533 | const delivery = deliver(app.config.dataDir, userId, files); |
| 515 | 534 | const running = live.get(conversation.id); |
| @@ -541,7 +560,11 @@ export const CONTEXT_FULL = 0.85; | ||
| 541 | 560 | export const CONTEXT_RENEWED_LINE = "This thread's context was nearly full, so I continue in a fresh session from its record and your standing rules."; |
| 542 | 561 | |
| 543 | 562 | export type RedoHow = "edit" | "regenerate" | "retry"; |
| 544 | export type RedoRefusal = SayRefusal | "running" | "nothing"; | |
| 563 | /** | |
| 564 | * What `redo` refuses. A Retry, edit or regenerate that cannot start puts the exchange back as it | |
| 565 | * was and says why (#16593): nothing new is kept, so it is never answered as a success. | |
| 566 | */ | |
| 567 | export type RedoRefusal = SayRefusal | "running" | "nothing" | "busy" | Exclude<NotStarted["code"], "credit_spent">; | |
| 545 | 568 | |
| 546 | 569 | /** |
| 547 | 570 | * Ask again, in place of the last exchange (#8817). `edit` replaces the |
| @@ -637,10 +660,6 @@ export type HeldRefusal = "resting" | "busy" | "answering" | "no_account" | "san | ||
| 637 | 660 | |
| 638 | 661 | /** The model door answered 402, as Claude Code prints it: the credit a parent lends is spent. */ |
| 639 | 662 | const creditSpent = (text: string): boolean => /\bAPI Error: 402\b/.test(text); |
| 640 | const HELD: ReadonlySet<string> = new Set<HeldRefusal>(["resting", "busy", "answering", "no_account", "sandbox", "unauthorized"]); | |
| 641 | ||
| 642 | /** Whether a refusal is one a message is kept through, with its line and Retry (#16431). */ | |
| 643 | export const isHeld = (code: string): code is HeldRefusal => HELD.has(code); | |
| 644 | 663 | |
| 645 | 664 | /** |
| 646 | 665 | * A message the forge cannot hand to its agent at all right now is kept, as any message that cannot |
| @@ -648,7 +667,7 @@ export const isHeld = (code: string): code is HeldRefusal => HELD.has(code); | ||
| 648 | 667 | * why and when to Retry. `starting`: a woken forge has yet to configure its agent (#16486); |
| 649 | 668 | * `off`: no agent is coming (#16455). Nothing is kept for an empty or overlong message. |
| 650 | 669 | */ |
| 651 | export function keepUnstarted(app: App, userId: string, text: string, files: readonly ConversationFile[], conversationId: string | undefined, why: "starting" | "off"): Result<true, "empty" | "too_long" | "not_found"> { | |
| 670 | export function keepUnstarted(app: App, userId: string, text: string, files: readonly ConversationFile[], conversationId: string | undefined, why: "starting" | "off"): Result<Kept, "empty" | "too_long" | "not_found"> { | |
| 652 | 671 | if (text.trim() === "") return err("empty"); |
| 653 | 672 | if (text.length > WORDS_MAX) return err("too_long"); |
| 654 | 673 | const conversation = conversationId === undefined ? openConversation(app.db, userId) : getConversation(app.db, userId, conversationId); |
| @@ -657,8 +676,9 @@ export function keepUnstarted(app: App, userId: string, text: string, files: rea | ||
| 657 | 676 | if (!stored.ok) return err("too_long"); |
| 658 | 677 | attach(app.db, files, stored.value.id); |
| 659 | 678 | release(app.db, stored.value.id); |
| 660 | appendTurn(app.db, userId, conversation.id, { role: "agent", text: why === "starting" ? STARTING_LINE : heldLine({ code: "off" }), failed: true }); | |
| 661 | return ok(true); | |
| 679 | const line = why === "starting" ? STARTING_LINE : heldLine({ code: "off" }); | |
| 680 | appendTurn(app.db, userId, conversation.id, { role: "agent", text: line, failed: true }); | |
| 681 | return ok({ why, line, turn: stored.value.id }); | |
| 662 | 682 | } |
| 663 | 683 | |
| 664 | 684 | /** Why a turn did not start: the refusal, and what the account said and until when, where known. */ |
src/http/api/conversation_build.ts +1 −2
| @@ -8,7 +8,6 @@ | ||
| 8 | 8 | |
| 9 | 9 | import { BUILD_WORDS, buildState, publishBuild, restoreVersion, startBuild, versionDiff, type BuildError } from "../../actions/build.ts"; |
| 10 | 10 | import { BUILD_ID as ID, promptRefusal, templateRefusal, versionRefusal } from "../../actions/build_input.ts"; |
| 11 | import { TALK_WORDS } from "../../actions/talk.ts"; | |
| 12 | 11 | import { apiError, boundedBody, json, tryAgain, unauthorized } from "../respond.ts"; |
| 13 | 12 | import type { Router } from "../router.ts"; |
| 14 | 13 | import type { Handler } from "../context.ts"; |
| @@ -38,7 +37,7 @@ export function conversationBuildRoutes(router: Router<Handler>): void { | ||
| 38 | 37 | const started = await startBuild(ctx.app, user, { prompt, ...(template === undefined ? {} : { template }) }); |
| 39 | 38 | if (!started.ok) return fail(started.error); |
| 40 | 39 | const { build, repo, said } = started.value; |
| 41 | return json({ conversation: build.conversationId, repo: `${repo.ownerName}/${repo.name}`, branch: build.branch, ...(said === undefined ? {} : { said, message: TALK_WORDS[said][1] }) }, 201); | |
| 40 | return json({ conversation: build.conversationId, repo: `${repo.ownerName}/${repo.name}`, branch: build.branch, ...(said === undefined ? {} : { said: said.why, message: said.message, kept: said.kept }) }, 201); | |
| 42 | 41 | }); |
| 43 | 42 | |
| 44 | 43 | const named = (id: string | undefined): string | undefined => (id !== undefined && ID.test(id) ? id : undefined); |
src/http/api/conversation_talk.ts +17 −4
| @@ -101,12 +101,25 @@ export function conversationTalkRoutes(router: Router<Handler>): void { | ||
| 101 | 101 | const said = talk(ctx.app, user, text, files.value, during === "queue" ? "queue" : during === "steer" ? "interrupt" : "inject", named, typeof input["about"] === "string" ? input["about"] : null); |
| 102 | 102 | if (!said.ok) { |
| 103 | 103 | const [status, words] = TALK_WORDS[said.error]; |
| 104 | if (said.error === "no_account") return json({ error: said.error, message: words, ...blockedFields(noTalkAccountBlocked(ctx.app, user)) }, status); | |
| 105 | // While the forge starts, the line is the kept one and the wait is what is left of its boot, | |
| 106 | // read once, as Retry-After alone. | |
| 107 | if (said.error === "starting") return tryAgain(said.error, 503, words, startingIn(ctx.app) ?? 10); | |
| 108 | 104 | return status === 429 || status === 503 ? tryAgain(said.error, status, words, talkWait(said.error)) : json({ error: said.error, message: words }, status); |
| 109 | 105 | } |
| 106 | // Kept is a success: the message is in the conversation; the body says why it did not start, | |
| 107 | // the line under it, its turn and until when, so a client never reads it as lost. | |
| 108 | const kept = said.value.kept; | |
| 109 | if (kept !== undefined) | |
| 110 | return json( | |
| 111 | { | |
| 112 | how: "kept", | |
| 113 | why: kept.why, | |
| 114 | message: kept.line, | |
| 115 | turn: kept.turn, | |
| 116 | ...(kept.until === undefined ? {} : { until: kept.until }), | |
| 117 | ...(kept.why === "no_account" ? blockedFields(noTalkAccountBlocked(ctx.app, user)) : {}), | |
| 118 | }, | |
| 119 | 202, | |
| 120 | // While the forge starts, about when to Retry: what is left of its boot, read once. | |
| 121 | kept.why === "starting" ? { "retry-after": String(startingIn(ctx.app) ?? 10) } : {}, | |
| 122 | ); | |
| 110 | 123 | return json(said.value.queued === undefined ? { how: said.value.how } : { how: said.value.how, queued: said.value.queued }, 202); |
| 111 | 124 | }); |
| 112 | 125 |
src/http/mcp/tools/say.ts +4 −4
| @@ -1,6 +1,4 @@ | ||
| 1 | 1 | import { mayView } from "../../../core/sealed.ts"; |
| 2 | import { badBlocked } from "../shared.ts"; | |
| 3 | import { noTalkAccountBlocked } from "../../../actions/blocked_words.ts"; | |
| 4 | 2 | import { addCommentThrough } from "../../../actions/issues.ts"; |
| 5 | 3 | import { getIssue } from "../../../core/issues.ts"; |
| 6 | 4 | import { archiveConversation, createConversation, findConversations, getConversation, listConversations, renameConversation, TOO_MANY_WORDS } from "../../../core/conversations.ts"; |
| @@ -67,7 +65,7 @@ export const tool: Tool = { | ||
| 67 | 65 | const started = await startBuild(ctx.app, user, { prompt, ...(template === undefined ? {} : { template }) }); |
| 68 | 66 | if (!started.ok) return buildBad(started.error); |
| 69 | 67 | const { build, repo, said } = started.value; |
| 70 | return good({ conversation: build.conversationId, repo: `${repo.ownerName}/${repo.name}`, branch: build.branch, ...(said === undefined ? {} : { said, message: TALK_WORDS[said][1] }) }); | |
| 68 | return good({ conversation: build.conversationId, repo: `${repo.ownerName}/${repo.name}`, branch: build.branch, ...(said === undefined ? {} : { said: said.why, message: said.message, kept: said.kept }) }); | |
| 71 | 69 | } |
| 72 | 70 | const id = str(args["conversation"]); |
| 73 | 71 | if (!BUILD_ID.test(id)) return buildBad("not_found"); |
| @@ -123,8 +121,10 @@ export const tool: Tool = { | ||
| 123 | 121 | if (text.trim() === "" || text.length > WORDS_MAX) return bad("invalid_text", `text is 1 to ${WORDS_MAX} characters`); |
| 124 | 122 | const named = args["conversation"] === undefined ? undefined : str(args["conversation"]); |
| 125 | 123 | const talked = talk(ctx.app, user, text, [], "inject", named); |
| 126 | if (!talked.ok && talked.error === "no_account") return badBlocked("no_account", noTalkAccountBlocked(ctx.app, user)); | |
| 127 | 124 | if (!talked.ok) return bad(talked.error, TALK_WORDS[talked.error][1]); |
| 125 | // Kept: the message is in the conversation, and its line says why it did not start. | |
| 126 | const kept = talked.value.kept; | |
| 127 | if (kept !== undefined) return good({ says: kept.line, kept: true, why: kept.why, turn: kept.turn, ...(kept.until === undefined ? {} : { until: kept.until }) }); | |
| 128 | 128 | return good({ says: talked.value.how === "steered" ? "Said to the forge's agent, mid-answer." : "The forge's agent is answering; read it in GET /api/v1/conversation/turns." }); |
| 129 | 129 | } |
| 130 | 130 | const repo = repoArg(ctx, user, args); |
src/tui/screens/talk_build.ts +2 −3
| @@ -6,8 +6,7 @@ | ||
| 6 | 6 | * read shows: Restore announces its push and Publish merges the pull. |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | import { buildState, BUILD_WORDS, publishBuild, restoreVersion, startBuild, versionDiff, type BuildState } from "../../actions/build.ts"; | |
| 10 | import { TALK_WORDS } from "../../actions/talk.ts"; | |
| 9 | import { buildSaidWords, buildState, BUILD_WORDS, publishBuild, restoreVersion, startBuild, versionDiff, type BuildState } from "../../actions/build.ts"; | |
| 11 | 10 | import { offeredTemplates } from "../../core/app_templates.ts"; |
| 12 | 11 | import { buildsOf } from "../../core/chat_build.ts"; |
| 13 | 12 | import { getRepoById } from "../../core/repos.ts"; |
| @@ -192,7 +191,7 @@ export function talkBuildScreen(f: Session, ctx: Ctx): Screen { | ||
| 192 | 191 | if (prompt.trim() === "") return "Say what to build."; |
| 193 | 192 | runAsync(ctx, async () => { |
| 194 | 193 | const started = await startBuild(f.app, user, { prompt, ...(template === "" ? {} : { template }) }); |
| 195 | note = started.ok ? (started.value.said === undefined ? "Started; Enter opens it." : `The app is made, but ${TALK_WORDS[started.value.said][1]}.`) : words(started.error); | |
| 194 | note = started.ok ? (started.value.said === undefined ? "Started; Enter opens it." : buildSaidWords(started.value.said)) : words(started.error); | |
| 196 | 195 | ctx.invalidate(); |
| 197 | 196 | }); |
| 198 | 197 | return undefined; |
src/tui/screens/talk_chats.ts +2 −2
| @@ -524,7 +524,7 @@ function chatScreen(f: Session, ctx: Ctx, chat: Conversation, hit?: string): Scr | ||
| 524 | 524 | const starter = turns.length === 0 && !pane && around === undefined ? STARTER_PROMPTS[Number(e.key) - 1] : undefined; |
| 525 | 525 | if (starter !== undefined && /^[1-9]$/.test(e.key)) { |
| 526 | 526 | const said = talk(f.app, user, starter, [], "inject", chat.id); |
| 527 | note = said.ok ? `Sent "${starter}"; r reloads to see the answer.` : TALK_WORDS[said.error][1]; | |
| 527 | note = said.ok ? (said.value.kept?.line ?? `Sent "${starter}"; r reloads to see the answer.`) : TALK_WORDS[said.error][1]; | |
| 528 | 528 | ctx.invalidate(); |
| 529 | 529 | return true; |
| 530 | 530 | } |
| @@ -628,7 +628,7 @@ function chatScreen(f: Session, ctx: Ctx, chat: Conversation, hit?: string): Scr | ||
| 628 | 628 | onSubmit: (words) => { |
| 629 | 629 | const said = talk(f.app, user, words, [], "inject", chat.id); |
| 630 | 630 | if (!said.ok) return TALK_WORDS[said.error][1]; |
| 631 | note = "Sent; r reloads to see the answer."; | |
| 631 | note = said.value.kept?.line ?? "Sent; r reloads to see the answer."; | |
| 632 | 632 | ctx.invalidate(); |
| 633 | 633 | return undefined; |
| 634 | 634 | }, |
src/tui/ssh-talk.ts +5
| @@ -162,6 +162,11 @@ export function serveTalk(app: App, session: SshSession, user: User | undefined, | ||
| 162 | 162 | } |
| 163 | 163 | const finish = (said: ReturnType<typeof talk>): void => { |
| 164 | 164 | if (!said.ok) return refuse(TALK_WORDS[said.error][1]); |
| 165 | // Kept: the message is in the conversation; the line says why it did not start, and Retry. | |
| 166 | if (said.value.kept !== undefined) { | |
| 167 | session.write(`kept: ${said.value.kept.line}${CRLF}`); | |
| 168 | return session.exit(0); | |
| 169 | } | |
| 165 | 170 | if (said.value.how === "answered") { |
| 166 | 171 | session.write(terminalAnswer(said.value.answer ?? "")); |
| 167 | 172 | return session.exit(0); |
src/web/pages/talk.ts +5 −4
| @@ -51,7 +51,7 @@ import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.t | ||
| 51 | 51 | import { ASKING } from "../../crew/conversation_confirm.ts"; |
| 52 | 52 | import { INVERSES, readReceipt, type Receipt } from "../../crew/receipt.ts"; |
| 53 | 53 | import { pastTime } from "../../core/fence.ts"; |
| 54 | import { isHeld, turnRunning, type RedoHow } from "../../crew/conversation.ts"; | |
| 54 | import { turnRunning, type RedoHow } from "../../crew/conversation.ts"; | |
| 55 | 55 | import { liveBlock, subscribe } from "../../crew/conversation_live.ts"; |
| 56 | 56 | import { during } from "../../perf/jobs.ts"; |
| 57 | 57 | import type { Ctx } from "../../http/context.ts"; |
| @@ -730,9 +730,10 @@ export async function talkSend(ctx: Ctx): Promise<Response> { | ||
| 730 | 730 | return flashTo(ctx, `${here}`, { tone: "danger", message: `Say something, up to ${WORDS_MAX} characters.` }); |
| 731 | 731 | const about = form.get("about"); |
| 732 | 732 | const said = talk(ctx.app, user, text, [], "queue", conversation.id, typeof about === "string" ? about : null); |
| 733 | if (!said.ok && said.error === "no_account") return flashTo(ctx, `${here}`, blockedFlash(noTalkAccountBlocked(ctx.app, user), "danger")); | |
| 734 | // Kept: the line under the message says why it could not start and when to Retry. | |
| 735 | if (!said.ok && (isHeld(said.error) || said.error === "starting" || said.error === "off")) return flashTo(ctx, `${here}`, { tone: "attn", message: KEPT_FLASH }); | |
| 733 | // Kept: the line under the message says why it could not start and when to Retry; with no | |
| 734 | // account, the ask for one beside it. | |
| 735 | if (said.ok && said.value.kept?.why === "no_account") return flashTo(ctx, `${here}`, blockedFlash(noTalkAccountBlocked(ctx.app, user), "attn")); | |
| 736 | if (said.ok && said.value.kept !== undefined) return flashTo(ctx, `${here}`, { tone: "attn", message: KEPT_FLASH }); | |
| 736 | 737 | if (!said.ok) return flashTo(ctx, `${here}`, { tone: "danger", message: TALK_WORDS[said.error][1] }); |
| 737 | 738 | return flashTo(ctx, `${here}`, { tone: "success", message: "Sent. Reload to see the answer." }); |
| 738 | 739 | } |
src/web/talk_build.ts +2 −3
| @@ -6,8 +6,7 @@ | ||
| 6 | 6 | * ships no script. |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | import { buildState, BUILD_WORDS, publishBuild, restoreVersion, startBuild, versionDiff, type BuildState } from "../actions/build.ts"; | |
| 10 | import { TALK_WORDS } from "../actions/talk.ts"; | |
| 9 | import { buildSaidWords, buildState, BUILD_WORDS, publishBuild, restoreVersion, startBuild, versionDiff, type BuildState } from "../actions/build.ts"; | |
| 11 | 10 | import { offeredTemplates } from "../core/app_templates.ts"; |
| 12 | 11 | import { WORDS_MAX } from "../core/fence.ts"; |
| 13 | 12 | import { getConversation } from "../core/conversations.ts"; |
| @@ -111,7 +110,7 @@ export async function talkBuild(ctx: Ctx): Promise<Response> { | ||
| 111 | 110 | const started = await startBuild(ctx.app, user, { prompt, ...(from === undefined ? {} : { template: from }) }); |
| 112 | 111 | if (!started.ok) return flashTo(ctx, "/talk", { tone: "danger", message: BUILD_WORDS[started.error] ?? "Not started." }); |
| 113 | 112 | const to = `/talk/${started.value.build.conversationId}`; |
| 114 | return started.value.said === undefined ? redirect(to) : flashTo(ctx, to, { tone: "danger", message: `The app is made, but ${TALK_WORDS[started.value.said][1]}.` }); | |
| 113 | return started.value.said === undefined ? redirect(to) : flashTo(ctx, to, { tone: started.value.said.kept ? "attn" : "danger", message: buildSaidWords(started.value.said) }); | |
| 115 | 114 | } |
| 116 | 115 | |
| 117 | 116 | /** The conversation a build request names, when it is the person's. */ |
test/conversation-files.test.ts +2 −2
| @@ -89,8 +89,8 @@ describe("attachments", () => { | ||
| 89 | 89 | const png = storeFile(app.db, dir, alice.id, "a.png", PNG); |
| 90 | 90 | if (!png.ok) throw new Error(png.error); |
| 91 | 91 | const refused = say(app, options, alice.id, "look", [png.value]); |
| 92 | expect(refused.ok).toBe(false); | |
| 93 | expect(!refused.ok && refused.error).toBe("answering"); | |
| 92 | expect(refused.ok && refused.value.how).toBe("kept"); | |
| 93 | expect(refused.ok && refused.value.kept?.why).toBe("answering"); | |
| 94 | 94 | // Not attached to anything: it can go with the next message. |
| 95 | 95 | expect(pendingAmong(app.db, alice.id, [png.value.id]).ok).toBe(true); |
| 96 | 96 | const note = text("n.md", "steer me"); |
test/conversation-talk.test.ts +58 −10
| @@ -32,6 +32,7 @@ import { waitFor } from "./helpers/poll.ts"; | ||
| 32 | 32 | quietLogs(); |
| 33 | 33 | |
| 34 | 34 | const KEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB2S7bF0R4NmuTUvVJXPPjdI1AF8XHXeYSNfr8DJ5eDo alice"; |
| 35 | const KIT_KEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHRugSiAU1FLP6WOEA7pZQJ4a/kFVu+eOyKjsd6yZALb kit"; | |
| 35 | 36 | |
| 36 | 37 | describe("talking to the forge's agent", () => { |
| 37 | 38 | let dir: string; |
| @@ -63,15 +64,15 @@ describe("talking to the forge's agent", () => { | ||
| 63 | 64 | return { error: body.result.isError === true, text: body.result.content[0]!.text }; |
| 64 | 65 | }; |
| 65 | 66 | |
| 66 | const exec = async (command: string) => { | |
| 67 | const parsed = parseAuthorizedKey(KEY); | |
| 67 | const exec = async (command: string, key = KEY) => { | |
| 68 | const parsed = parseAuthorizedKey(key); | |
| 68 | 69 | if (!parsed.ok) throw new Error("key"); |
| 69 | 70 | let out = ""; |
| 70 | 71 | let err = ""; |
| 71 | 72 | let code: number | undefined; |
| 72 | 73 | await new Promise<void>((resolve) => { |
| 73 | 74 | const session = { |
| 74 | key: { fingerprint: fingerprint(parsed.value.blob), line: KEY }, | |
| 75 | key: { fingerprint: fingerprint(parsed.value.blob), line: key }, | |
| 75 | 76 | username: "whoever", |
| 76 | 77 | remoteAddress: "203.0.113.9", |
| 77 | 78 | kind: "exec" as const, |
| @@ -188,9 +189,14 @@ echo '{"type":"result","subtype":"success","is_error":false,"result":"Heard.","t | ||
| 188 | 189 | const send = () => |
| 189 | 190 | fetch(`${servers.url}/api/v1/conversation/messages`, { method: "POST", headers: headers(dana), body: JSON.stringify({ text: "hi" }) }); |
| 190 | 191 | for (let i = 0; i < TALK_PER_MINUTE; i++) { |
| 192 | // Kept, so the send succeeded: 202 with why, the line, its turn and the ask for an account. | |
| 191 | 193 | const res = await send(); |
| 192 | expect(res.status).toBe(403); | |
| 193 | expect(((await res.json()) as { error: string }).error).toBe("no_account"); | |
| 194 | expect(res.status).toBe(202); | |
| 195 | const body = (await res.json()) as { how: string; why: string; message: string; turn: string; ask?: unknown }; | |
| 196 | expect([body.how, body.why]).toEqual(["kept", "no_account"]); | |
| 197 | expect(body.message).toContain("Your message is kept for Retry"); | |
| 198 | expect(body.turn).toMatch(/\S/); | |
| 199 | expect(body.ask).toBeDefined(); | |
| 194 | 200 | } |
| 195 | 201 | const over = await send(); |
| 196 | 202 | expect(over.status).toBe(429); |
| @@ -209,7 +215,7 @@ echo '{"type":"result","subtype":"success","is_error":false,"result":"Heard.","t | ||
| 209 | 215 | const erin = createToken(app.db, made.value.id, "personal", "rw").token; |
| 210 | 216 | const send = () => fetch(`${servers.url}/api/v1/conversation/messages`, { method: "POST", headers: headers(erin), body: JSON.stringify({ text: "hi" }) }); |
| 211 | 217 | const refused = await send(); |
| 212 | expect(refused.status).toBe(403); | |
| 218 | expect(refused.status).toBe(202); | |
| 213 | 219 | expect(JSON.stringify(await refused.json())).toContain("An admin of this forge can grant one"); |
| 214 | 220 | expect(setModelGrant(app.db, made.value.id, 5, made.value.id, new Date())).toBe(true); |
| 215 | 221 | const ok = await send(); |
| @@ -219,18 +225,23 @@ echo '{"type":"result","subtype":"success","is_error":false,"result":"Heard.","t | ||
| 219 | 225 | expect(JSON.stringify(await read.json())).toContain('"hi"'); |
| 220 | 226 | }); |
| 221 | 227 | |
| 222 | test("a spent dollar grant is resting (429), and the host's lent credit alone lets a turn through", async () => { | |
| 228 | test("a spent dollar grant is resting (kept, 202), and the host's lent credit alone lets a turn through", async () => { | |
| 223 | 229 | const made = await createUser(app.db, { origin: "seed", trusted: true, username: "finn", email: "[email protected]", password: "a-good-passphrase" }); |
| 224 | 230 | if (!made.ok) throw new Error(made.error); |
| 225 | 231 | const finn = createToken(app.db, made.value.id, "personal", "rw").token; |
| 226 | 232 | const send = () => fetch(`${servers.url}/api/v1/conversation/messages`, { method: "POST", headers: headers(finn), body: JSON.stringify({ text: "hi" }) }); |
| 227 | 233 | expect(setModelGrant(app.db, made.value.id, 0.1, made.value.id, new Date())).toBe(true); |
| 228 | expect((await send()).status).toBe(429); | |
| 234 | const how = async (): Promise<[number, string]> => { | |
| 235 | const res = await send(); | |
| 236 | const body = (await res.json()) as { how: string; why?: string }; | |
| 237 | return [res.status, body.why === undefined ? body.how : `${body.how} ${body.why}`]; | |
| 238 | }; | |
| 239 | expect(await how()).toEqual([202, "kept resting"]); | |
| 229 | 240 | setModelGrant(app.db, made.value.id, null, made.value.id, new Date()); |
| 230 | expect((await send()).status).toBe(403); | |
| 241 | expect(await how()).toEqual([202, "kept no_account"]); | |
| 231 | 242 | writeFileSync(join(dir, "parent.json"), JSON.stringify({ credit: { token: "bearer-token-1", capUsd: 5, spentUsd: 1 } })); |
| 232 | 243 | try { |
| 233 | expect((await send()).status).toBe(202); | |
| 244 | expect((await how())[1]).not.toStartWith("kept"); | |
| 234 | 245 | } finally { |
| 235 | 246 | rmSync(join(dir, "parent.json")); |
| 236 | 247 | } |
| @@ -494,6 +505,43 @@ echo '{"type":"result","subtype":"success","is_error":false,"result":"Heard.","t | ||
| 494 | 505 | expect((await exec("talk-again edit bad\u0007words")).code).toBe(1); |
| 495 | 506 | }); |
| 496 | 507 | |
| 508 | test("a kept message is a success at every door: 202 kept, an MCP result, ssh exit 0, each with its line", async () => { | |
| 509 | // At the live-turn cap nothing can start; each message is kept with the line saying why. | |
| 510 | const busy = "The forge is answering as many people as it can at once. Your message is kept; Retry in a minute."; | |
| 511 | // Kit, a person of their own, so the per-minute count of earlier tests is not theirs. | |
| 512 | const made = await createUser(app.db, { origin: "seed", trusted: true, username: "kit", email: "[email protected]", password: "a-good-passphrase" }); | |
| 513 | if (!made.ok) throw new Error(made.error); | |
| 514 | setModelGrant(app.db, made.value.id, 5, made.value.id, new Date()); | |
| 515 | const parsed = parseAuthorizedKey(KIT_KEY); | |
| 516 | if (!parsed.ok) throw new Error("key"); | |
| 517 | addKey(app.db, made.value.id, "laptop", parsed.value); | |
| 518 | const kit = createToken(app.db, made.value.id, "personal", "rw").token; | |
| 519 | const standing = async () => { | |
| 520 | const read = (await (await fetch(`${servers.url}/api/v1/conversation/turns`, { headers: headers(kit) })).json()) as { turns: { id: string; role: string; text: string }[] }; | |
| 521 | return read.turns; | |
| 522 | }; | |
| 523 | configureConversation(app, { binary: join(dir, "never-runs"), localUrl: servers.url, budgetUsd: 1, timeoutMs: 30_000, signal: new AbortController().signal, liveMax: 0 }); | |
| 524 | try { | |
| 525 | const res = await fetch(`${servers.url}/api/v1/conversation/messages`, { method: "POST", headers: headers(kit), body: JSON.stringify({ text: "kept by api" }) }); | |
| 526 | expect(res.status).toBe(202); | |
| 527 | const body = (await res.json()) as { how: string; why: string; message: string; turn: string }; | |
| 528 | expect({ how: body.how, why: body.why, message: body.message }).toEqual({ how: "kept", why: "busy", message: busy }); | |
| 529 | // `turn` is the kept message's own turn, the one Retry answers after. | |
| 530 | expect((await standing()).find((t) => t.id === body.turn)?.text).toBe("kept by api"); | |
| 531 | const said = await mcp(kit, { text: "kept by mcp" }); | |
| 532 | expect(said).toEqual({ error: false, text: expect.stringContaining(busy) }); | |
| 533 | const r = await exec("talk kept by ssh", KIT_KEY); | |
| 534 | expect(r.code).toBe(0); | |
| 535 | expect(r.out).toContain(`kept: ${busy}`); | |
| 536 | expect((await standing()).map((t) => `${t.role}:${t.text}`).slice(-6)).toEqual(["person:kept by api", `agent:${busy}`, "person:kept by mcp", `agent:${busy}`, "person:kept by ssh", `agent:${busy}`]); | |
| 537 | // A message that keeps nothing is still an error at each door. | |
| 538 | expect((await fetch(`${servers.url}/api/v1/conversation/messages`, { method: "POST", headers: headers(kit), body: JSON.stringify({ text: " " }) })).status).toBe(422); | |
| 539 | expect((await exec("talk ", KIT_KEY)).code).toBe(1); | |
| 540 | } finally { | |
| 541 | withBinary(answers("Back.")); | |
| 542 | } | |
| 543 | }); | |
| 544 | ||
| 497 | 545 | test("ssh talk /stop stops instead of speaking; with nothing running it says so (#8818)", async () => { |
| 498 | 546 | expect(isStop("/stop")).toBe(true); |
| 499 | 547 | expect(isStop(" --stop ")).toBe(true); |
test/conversation-turn.test.ts +9 −5
| @@ -355,7 +355,7 @@ echo '{"type":"result","subtype":"success","is_error":false,"result":"Back.","to | ||
| 355 | 355 | const b = await createUser(app.db, { origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-good-passphrase" }); |
| 356 | 356 | if (!b.ok) throw new Error("user"); |
| 357 | 357 | const said = say(app, options(stub("bobs", "s", "answer")), b.value.id, "hi"); |
| 358 | expect(!said.ok && said.error).toBe("no_account"); | |
| 358 | expect(said.ok && said.value.kept?.why).toBe("no_account"); | |
| 359 | 359 | expect(existsSync(join(dir, "bobs.argv"))).toBe(false); |
| 360 | 360 | // A message is never lost (#16431): kept, with the line saying why, flagged failed for Retry. |
| 361 | 361 | expect(turns(b.value)).toEqual(["person:hi", "agent:No account answers for you on this forge yet. Your message is kept for Retry. An admin of this forge can grant one."]); |
| @@ -370,7 +370,7 @@ echo '{"type":"result","subtype":"success","is_error":false,"result":"Back.","to | ||
| 370 | 370 | expect(setAccountCeiling(app.db, alice.id, 0.5, 0.5, alice.id, new Date())).toBe(true); |
| 371 | 371 | const said = say(app, options(stub("ceiling", "s", "answer")), alice.id, "over the day"); |
| 372 | 372 | clearAccountCeiling(app.db, alice.id); |
| 373 | expect(!said.ok && said.error).toBe("resting"); | |
| 373 | expect(said.ok && said.value.kept?.why).toBe("resting"); | |
| 374 | 374 | expect(existsSync(join(dir, "ceiling.argv"))).toBe(false); |
| 375 | 375 | const added = turns(alice).slice(-2); |
| 376 | 376 | expect(added[0]).toBe("person:over the day"); |
| @@ -381,7 +381,7 @@ echo '{"type":"result","subtype":"success","is_error":false,"result":"Back.","to | ||
| 381 | 381 | test("past the live-turn cap a new turn is busy; a steer into a running one is not counted", async () => { |
| 382 | 382 | expect(TURNS_LIVE_MAX).toBe(4); |
| 383 | 383 | const none = say(app, { ...options(stub("capped", "sess-a", "answer")), liveMax: 0 }, alice.id, "too many"); |
| 384 | expect(!none.ok && none.error).toBe("busy"); | |
| 384 | expect(none.ok && none.value.kept?.why).toBe("busy"); | |
| 385 | 385 | expect(existsSync(join(dir, "capped.argv"))).toBe(false); |
| 386 | 386 | expect(turns(alice).slice(-2)).toEqual(["person:too many", "agent:The forge is answering as many people as it can at once. Your message is kept; Retry in a minute."]); |
| 387 | 387 | const bin = stub("one-live", "sess-a", "steer"); |
| @@ -401,7 +401,11 @@ echo '{"type":"result","subtype":"success","is_error":false,"result":"Back.","to | ||
| 401 | 401 | const until = new Date(Date.now() + 15 * 60_000).toISOString(); |
| 402 | 402 | restSubscription(app.db, sub.value.id, until, "window full"); |
| 403 | 403 | const said = say(app, options(stub("rested", "s", "answer")), alice.id, "what is happening on voxel?"); |
| 404 | expect(!said.ok && said.error).toBe("resting"); | |
| 404 | expect(said.ok && said.value.kept?.why).toBe("resting"); | |
| 405 | // The send succeeded: what a client gets back names why, the line, the message's turn and when. | |
| 406 | expect(said.ok && said.value.kept?.until).toBe(until); | |
| 407 | expect(said.ok && said.value.kept?.line).toMatch(/^The account that answers you is resting until /); | |
| 408 | expect(said.ok && said.value.done).toBeUndefined(); | |
| 405 | 409 | expect(existsSync(join(dir, "rested.argv"))).toBe(false); |
| 406 | 410 | const added = turns(alice).slice(-2); |
| 407 | 411 | expect(added[0]).toBe("person:what is happening on voxel?"); |
| @@ -474,7 +478,7 @@ echo '{"type":"result","subtype":"success","is_error":true,"result":"You have hi | ||
| 474 | 478 | |
| 475 | 479 | const before = turns(alice).length; |
| 476 | 480 | const refused = say(app, options(stub("while-resting", "s", "answer")), alice.id, "again?"); |
| 477 | expect(refused.ok ? "started" : refused.error).toBe("resting"); | |
| 481 | expect(refused.ok ? (refused.value.kept?.why ?? "started") : refused.error).toBe("resting"); | |
| 478 | 482 | expect(turns(alice)).toHaveLength(before); |
| 479 | 483 | |
| 480 | 484 | wakeSubscription(app.db, account.value.id); |
test/crew-sandbox-required.test.ts +8 −6
| @@ -153,7 +153,7 @@ describe("required and unavailable: nothing of the agent runs, a turn and a one- | ||
| 153 | 153 | try { |
| 154 | 154 | const runs = () => (app.db.query("SELECT count(*) AS n FROM conversation_run").get() as { n: number }).n; |
| 155 | 155 | const before = runs(); |
| 156 | expect(say(app, options, userId, "hello")).toEqual({ ok: false, error: "sandbox" }); | |
| 156 | expect(say(app, options, userId, "hello")).toEqual({ ok: true, value: { how: "kept", kept: { why: "sandbox", line: expect.any(String), turn: expect.any(String) } } }); | |
| 157 | 157 | expect(await oneShot(app, userId, "s", "p")).toEqual({ ok: false, error: "sandbox" }); |
| 158 | 158 | expect(runs()).toBe(before); |
| 159 | 159 | } finally { |
| @@ -185,7 +185,7 @@ describe("required and unavailable: nothing of the agent runs, a turn and a one- | ||
| 185 | 185 | const runs = () => (app.db.query("SELECT count(*) AS n FROM conversation_run").get() as { n: number }).n; |
| 186 | 186 | const spends = () => (app.db.query("SELECT count(*) AS n FROM one_shot_spend").get() as { n: number }).n; |
| 187 | 187 | const [r, s] = [runs(), spends()]; |
| 188 | expect(say(app, options, userId, "hello again")).toEqual({ ok: false, error: "sandbox" }); | |
| 188 | expect(say(app, options, userId, "hello again")).toEqual({ ok: true, value: { how: "kept", kept: { why: "sandbox", line: expect.any(String), turn: expect.any(String) } } }); | |
| 189 | 189 | expect(await oneShot(app, userId, "s", "p")).toEqual({ ok: false, error: "sandbox" }); |
| 190 | 190 | expect(runs()).toBe(r); |
| 191 | 191 | expect(spends()).toBe(s); |
| @@ -264,7 +264,7 @@ describe("the probe is taken again, at most once a minute and one at a time", () | ||
| 264 | 264 | }); |
| 265 | 265 | |
| 266 | 266 | describe("Talk's doors answer a refused turn in the forge's own words", () => { |
| 267 | test("the API says 503 with the sentence, and nothing is started or kept", async () => { | |
| 267 | test("the API keeps the message with the sentence (202 kept), and nothing is started", async () => { | |
| 268 | 268 | const dir = await mkdtemp(join(tmpdir(), "forge-sandbox-door-")); |
| 269 | 269 | const door = createApp({ dataDir: dir, port: 0, host: "forge.test" }); |
| 270 | 270 | const servers = await serve(door); |
| @@ -275,9 +275,11 @@ describe("Talk's doors answer a refused turn in the forge's own words", () => { | ||
| 275 | 275 | configureConversation(door, { binary: "/nonexistent-agent", localUrl: servers.url, budgetUsd: 1, timeoutMs: 60_000, signal: new AbortController().signal }); |
| 276 | 276 | noteSandbox(door, true, { ok: false, reason: "bwrap is not installed" }, () => {}); |
| 277 | 277 | const res = await fetch(`${servers.url}/api/v1/conversation/messages`, { method: "POST", headers: { "content-type": "application/json", authorization: `Bearer ${token}` }, body: JSON.stringify({ text: "hello" }) }); |
| 278 | expect(res.status).toBe(503); | |
| 279 | expect(await res.text()).toContain("requires the agent sandbox"); | |
| 280 | expect(talk(door, made.value, "hello")).toEqual({ ok: false, error: "sandbox" }); | |
| 278 | expect(res.status).toBe(202); | |
| 279 | const body = (await res.json()) as { how: string; why: string; message: string }; | |
| 280 | expect([body.how, body.why]).toEqual(["kept", "sandbox"]); | |
| 281 | expect(body.message).toContain("it requires a sandbox that is unavailable. Your message is kept"); | |
| 282 | expect(talk(door, made.value, "hello")).toEqual({ ok: true, value: { how: "kept", kept: { why: "sandbox", line: expect.any(String), turn: expect.any(String) } } }); | |
| 281 | 283 | expect((door.db.query("SELECT count(*) AS n FROM conversation_run").get() as { n: number }).n).toBe(0); |
| 282 | 284 | } finally { |
| 283 | 285 | await servers.stop(); |
test/talk-kept-message.test.ts +43 −1
| @@ -9,7 +9,9 @@ import { chmodSync, existsSync, writeFileSync } from "node:fs"; | ||
| 9 | 9 | import { mkdtemp, rm } from "node:fs/promises"; |
| 10 | 10 | import { tmpdir } from "node:os"; |
| 11 | 11 | import { join } from "node:path"; |
| 12 | import { talkAgain } from "../src/actions/talk.ts"; | |
| 12 | import { TALK_WORDS, talkAgain } from "../src/actions/talk.ts"; | |
| 13 | import { serveTalkAgain } from "../src/tui/ssh-talk.ts"; | |
| 14 | import type { SshSession } from "../src/ssh/server.ts"; | |
| 13 | 15 | import { createApp, type App } from "../src/app.ts"; |
| 14 | 16 | import { listTurns, openConversation } from "../src/core/conversations.ts"; |
| 15 | 17 | import { createSession } from "../src/core/sessions.ts"; |
| @@ -163,6 +165,46 @@ describe("a kept message on every door", () => { | ||
| 163 | 165 | tui.stop?.(); |
| 164 | 166 | }); |
| 165 | 167 | |
| 168 | test("a Retry that cannot start yet says why on the page, in JSON and in the terminal, never 'Asked again' (#16593)", async () => { | |
| 169 | configureConversation(app, options({ liveMax: 0 })); | |
| 170 | const conversation = openConversation(app.db, alice.id); | |
| 171 | const before = said(alice); | |
| 172 | const failed = listTurns(app.db, alice.id, conversation.id, { limit: 100 }); | |
| 173 | if (!failed.ok) throw new Error("turns"); | |
| 174 | const turn = failed.value.at(-1)!.id; | |
| 175 | const busy = TALK_WORDS.busy[1]; | |
| 176 | const cookie = `forge_session=${createSession(app.db, alice.id).token}`; | |
| 177 | const post = (accept?: string) => | |
| 178 | fetch(`${servers.url}/talk/again/retry`, { | |
| 179 | method: "POST", | |
| 180 | headers: { cookie, origin: new URL(servers.url).origin, ...(accept === undefined ? {} : { accept }) }, | |
| 181 | body: new URLSearchParams({ turn }), | |
| 182 | redirect: "manual", | |
| 183 | }); | |
| 184 | const page = await post(); | |
| 185 | expect(page.status).toBe(303); | |
| 186 | const flash = (page.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 187 | const shown = await (await fetch(`${servers.url}${page.headers.get("location")}`, { headers: { cookie: `${cookie}; ${flash}` } })).text(); | |
| 188 | expect(shown).toContain("as many people as it can at once; try again in a minute"); | |
| 189 | expect(shown).not.toContain("Asked again"); | |
| 190 | const asJson = await post("application/json"); | |
| 191 | expect(asJson.status).toBe(429); | |
| 192 | expect(await asJson.json()).toMatchObject({ ok: false, message: busy }); | |
| 193 | // The terminal's talk-again: the refusal, exit 1, nothing said of an answer. | |
| 194 | let out = "", errOut = "", code: number | undefined; | |
| 195 | const session: SshSession = { | |
| 196 | key: { algo: "ssh-ed25519", blob: new Uint8Array(), fingerprint: "", line: "" }, username: "alice", remoteAddress: "203.0.113.9", kind: "exec", command: "talk-again retry", term: undefined, env: {}, | |
| 197 | cols: 0, rows: 0, widthPx: 0, heightPx: 0, ended: false, pending: 0, | |
| 198 | write: (d: Uint8Array | string) => void (out += typeof d === "string" ? d : Buffer.from(d).toString()), | |
| 199 | writeStderr: (d: string) => void (errOut += d), onData() {}, onResize() {}, onEof() {}, onClose() {}, exit: (c: number) => void (code = c), drain: async () => {}, | |
| 200 | }; | |
| 201 | serveTalkAgain(app, session, alice, "retry", turn); | |
| 202 | expect([code, out]).toEqual([1, ""]); | |
| 203 | expect(errOut).toContain(busy); | |
| 204 | // And the exchange is as it was: the kept line is still there to Retry. | |
| 205 | expect(said(alice)).toEqual(before); | |
| 206 | }); | |
| 207 | ||
| 166 | 208 | test("Retry sends the kept words once: no second copy of the message", async () => { |
| 167 | 209 | configureConversation(app, options()); |
| 168 | 210 | const failed = listTurns( |
test/talk-starting.test.ts +11 −6
| @@ -45,10 +45,12 @@ const send = (text: string) => | ||
| 45 | 45 | body: JSON.stringify({ text }), |
| 46 | 46 | }); |
| 47 | 47 | |
| 48 | test("with no agent expected, Talk says off, as before", async () => { | |
| 48 | test("with no agent expected, Talk keeps the message as off (202 kept)", async () => { | |
| 49 | 49 | expect(startingIn(app)).toBeUndefined(); |
| 50 | 50 | const res = await send("hello"); |
| 51 | expect([res.status, ((await res.json()) as { error: string }).error]).toEqual([503, "off"]); | |
| 51 | const body = (await res.json()) as { how: string; why: string }; | |
| 52 | expect([res.status, body.how, body.why]).toEqual([202, "kept", "off"]); | |
| 53 | expect(res.headers.get("retry-after")).toBeNull(); | |
| 52 | 54 | }); |
| 53 | 55 | |
| 54 | 56 | test("while the boot has yet to configure the agent, a message is kept and the wait is said and sent", async () => { |
| @@ -57,12 +59,13 @@ test("while the boot has yet to configure the agent, a message is kept and the w | ||
| 57 | 59 | const left = startingIn(app, now)!; |
| 58 | 60 | expect(left).toBe(Math.round((STARTING_MS - 60_000) / 1000)); |
| 59 | 61 | const res = await send("what shipped today?"); |
| 60 | expect(res.status).toBe(503); | |
| 62 | // Kept, so the send succeeded: 202 kept, why starting, and the wait left of the boot. | |
| 63 | expect(res.status).toBe(202); | |
| 61 | 64 | const wait = Number(res.headers.get("retry-after")); |
| 62 | 65 | expect(wait).toBeGreaterThan(150); |
| 63 | 66 | expect(wait).toBeLessThanOrEqual(180); |
| 64 | const body = (await res.json()) as { error: string; message: string }; | |
| 65 | expect(body.error).toBe("starting"); | |
| 67 | const body = (await res.json()) as { how: string; why: string; message: string; turn: string }; | |
| 68 | expect([body.how, body.why]).toEqual(["kept", "starting"]); | |
| 66 | 69 | // The line carries no number: the seconds are Retry-After's alone, read once. |
| 67 | 70 | expect(body.message).toBe(STARTING_LINE); |
| 68 | 71 | // Kept: the words, and the line under them flagged failed so the page draws Retry. |
| @@ -75,6 +78,7 @@ test("while the boot has yet to configure the agent, a message is kept and the w | ||
| 75 | 78 | ["agent", body.message], |
| 76 | 79 | ]); |
| 77 | 80 | expect(last[1]!.failed).toBe(true); |
| 81 | expect(body.turn).toBe(last[0]!.id); | |
| 78 | 82 | // The page's plain form keeps it too, and says so rather than "not running". |
| 79 | 83 | const session = `forge_session=${createSession(app.db, alice.id).token}`; |
| 80 | 84 | const posted = await fetch(`${servers.url}/talk`, { |
| @@ -108,7 +112,8 @@ test("once the agent is configured, it is no longer starting; a boot with no cre | ||
| 108 | 112 | // Off, the message is kept too, with its own line (#16455). |
| 109 | 113 | const bea = await createUser(fresh.db, { origin: "seed", trusted: true, username: "bea", email: "[email protected]", password: "a-good-passphrase" }); |
| 110 | 114 | if (!bea.ok) throw new Error(bea.error); |
| 111 | expect(talk(fresh, bea.value, "hi")).toEqual({ ok: false, error: "off" }); | |
| 115 | const off = talk(fresh, bea.value, "hi"); | |
| 116 | expect(off.ok && off.value.kept?.why).toBe("off"); | |
| 112 | 117 | const kept = listTurns(fresh.db, bea.value.id, openConversation(fresh.db, bea.value.id).id, { limit: 10 }); |
| 113 | 118 | expect(kept.ok && kept.value.map((t) => `${t.role}${t.failed ? "!" : ""}:${t.text}`)).toEqual(["person:hi", "agent!:This forge's agent is not running right now. Your message is kept; Retry once it runs."]); |
| 114 | 119 | } finally { |
test/tui-talk-chats.test.ts +2 −2
| @@ -118,7 +118,7 @@ describe("the terminal's conversations screen", () => { | ||
| 118 | 118 | // The crew is off in this app: the prompt says why, and the message is kept with its line |
| 119 | 119 | // (#16455). |
| 120 | 120 | const said = await drive(bob, [...open, "\r", "s", "hello", "\r"]); |
| 121 | expect(said).toContain("not running here"); | |
| 121 | expect(said).toContain("This forge's agent is not running right now. Your message is kept; Retry once it runs."); | |
| 122 | 122 | const turns = listTurns(app.db, bob.id, made.value.id, { limit: 50 }); |
| 123 | 123 | expect(turns.ok && turns.value.slice(2).map((t) => `${t.role}${t.failed ? "!" : ""}:${t.text}`)).toEqual(["person:hello", "agent!:This forge's agent is not running right now. Your message is kept; Retry once it runs."]); |
| 124 | 124 | }); |
| @@ -148,7 +148,7 @@ describe("the terminal's conversations screen", () => { | ||
| 148 | 148 | STARTER_PROMPTS.forEach((p, i) => expect(text).toContain(`${String(i + 1)} ${p}`)); |
| 149 | 149 | // The crew is off in this app: the header says why, and the starter is kept with its line. |
| 150 | 150 | const sent = await drive(carol.value, [...open, "\r", "2"]); |
| 151 | expect(sent).toContain("not running here"); | |
| 151 | expect(sent).toContain("This forge's agent is not running right now. Your message is kept; Retry once it runs."); | |
| 152 | 152 | const turns = listTurns(app.db, carol.value.id, made.value.id, { limit: 50 }); |
| 153 | 153 | expect(turns.ok && turns.value.map((t) => `${t.role}${t.failed ? "!" : ""}`)).toEqual(["person", "agent!"]); |
| 154 | 154 | expect(turns.ok && turns.value[0]?.text).toBe(STARTER_PROMPTS[1]); |
test/ui-663-talk-and-merges.test.ts +8 −4
| @@ -181,7 +181,7 @@ describe("a cell whose lent credit is spent, and a forge whose agent is starting | ||
| 181 | 181 | chmodSync(bin, 0o755); |
| 182 | 182 | try { |
| 183 | 183 | const res = say(app, { binary: bin, localUrl: servers.url, budgetUsd: 1, timeoutMs: 30_000, signal: new AbortController().signal }, alice.id, "quick check: can you answer now?"); |
| 184 | expect(!res.ok && res.error).toBe("resting"); | |
| 184 | expect(res.ok && res.value.kept?.why).toBe("resting"); | |
| 185 | 185 | const turns = listTurns(app.db, alice.id, openConversation(app.db, alice.id).id, { limit: 50 }); |
| 186 | 186 | const shown = turns.ok ? turns.value.map((t) => `${t.role}${t.failed ? "!" : ""}:${t.text}`) : []; |
| 187 | 187 | expect(shown.slice(-2)).toEqual([ |
| @@ -197,7 +197,7 @@ describe("a cell whose lent credit is spent, and a forge whose agent is starting | ||
| 197 | 197 | // Before the crew is up, `conversationOptions` is undefined: `talk` answered "off" and kept |
| 198 | 198 | // nothing. |
| 199 | 199 | const sent = talk(app, alice, "capped send: are you there?"); |
| 200 | expect(!sent.ok && sent.error).toBe("off"); | |
| 200 | expect(sent.ok && sent.value.kept?.why).toBe("off"); | |
| 201 | 201 | const turns = listTurns(app.db, alice.id, openConversation(app.db, alice.id).id, { limit: 50 }); |
| 202 | 202 | const shown = turns.ok ? turns.value.map((t) => `${t.role}${t.failed ? "!" : ""}:${t.text}`) : []; |
| 203 | 203 | expect(shown.slice(-2)).toEqual([ |
| @@ -209,7 +209,10 @@ describe("a cell whose lent credit is spent, and a forge whose agent is starting | ||
| 209 | 209 | test("while the agent is off, the per-minute cap holds: past it nothing more is kept", async () => { |
| 210 | 210 | const made = await createUser(app.db, { origin: "seed", trusted: true, username: "flood", email: "[email protected]", password: "a-good-passphrase" }); |
| 211 | 211 | if (!made.ok) throw new Error(made.error); |
| 212 | for (let i = 0; i < TALK_PER_MINUTE; i++) expect(talk(app, made.value, `kept ${String(i)}`)).toEqual({ ok: false, error: "off" }); | |
| 212 | for (let i = 0; i < TALK_PER_MINUTE; i++) { | |
| 213 | const sent = talk(app, made.value, `kept ${String(i)}`); | |
| 214 | expect(sent.ok && sent.value.kept?.why).toBe("off"); | |
| 215 | } | |
| 213 | 216 | expect(talk(app, made.value, "one too many")).toEqual({ ok: false, error: "rate_limited" }); |
| 214 | 217 | const turns = listTurns(app.db, made.value.id, openConversation(app.db, made.value.id).id, { limit: 100 }); |
| 215 | 218 | expect(turns.ok && turns.value.length).toBe(TALK_PER_MINUTE * 2); |
| @@ -221,7 +224,8 @@ describe("a cell whose lent credit is spent, and a forge whose agent is starting | ||
| 221 | 224 | if (!made.ok) throw new Error(made.error); |
| 222 | 225 | promote(app.db, made.value); |
| 223 | 226 | setModelGrant(app.db, made.value.id, 5, alice.id, new Date()); |
| 224 | expect(talk(app, made.value, "build me a clock")).toEqual({ ok: false, error: "off" }); | |
| 227 | const sent = talk(app, made.value, "build me a clock"); | |
| 228 | expect(sent.ok && sent.value.kept?.why).toBe("off"); | |
| 225 | 229 | const conversation = openConversation(app.db, made.value.id); |
| 226 | 230 | const before = listTurns(app.db, made.value.id, conversation.id, { limit: 10 }); |
| 227 | 231 | const kept = before.ok ? before.value.at(-1) : undefined; |
new above
offline
refuseis shared byredo, so every caller ofredonow gets ok for a kept message. Each one must be taught to readkept, orredomust keep returning the refusal for a retry. A test must pin whichever you choose.