Public: anyone can read this; the crew can change it.
The Apple app can list and answer the actions Talk is holding for a person #18342
Merged 42m ago, live in production
scout/forge:scout/18338-the-apple-app-can-list-and-answer-the-ac → main 0 commits fork
7 files
changed +133
−5
docs/features/talk.md +1 −1
| @@ -87,7 +87,7 @@ A turn can be ended mid-reply (`stopTalk` in `src/actions/talk.ts`, `stopTurn` i | ||
| 87 | 87 | |
| 88 | 88 | - **Web:** the Stop button beside Send while an answer is running, and `Escape` in the composer. Send still posts the form without the script. |
| 89 | 89 | - **API:** `POST /api/v1/conversation/stop` answers 200, or 409 when nothing runs; a read-only token is 403. |
| 90 | - **Pin, answer a held action, redo (#18003):** `PATCH /api/v1/conversations/:id` reads `pinned` (true or false; an archived conversation is 409 `archived`); `POST /api/v1/conversation-confirms/:id` reads `{answer: "yes"|"no"}` (no body is yes) from the person's session or own rw token, never the agent's; `POST /api/v1/conversation/again` reads `{how: "retry"|"regenerate"|"edit", text?, conversation?, turn?}` (a retry names the failed turn in `turn`) and answers 202 through `askAgain`, the code behind `/talk/again/:how`. The terminal has all three already: `src/tui/screens/talk_chats.ts` (pin), `src/tui/ssh-talk.ts` (again), `src/tui/screens/confirms.ts` (yes and no). | |
| 90 | - **Pin, answer a held action, redo (#18003):** `PATCH /api/v1/conversations/:id` reads `pinned` (true or false; an archived conversation is 409 `archived`); `GET /api/v1/conversation-confirms` lists the caller's own waiting confirms (`id`, `tool`, `does`, `arguments`, `createdAt`, `expiresAt`: the web card's words), and `POST /api/v1/conversation-confirms/:id` reads `{answer: "yes"|"no"}` (no body is yes) from the person's session or own rw token, never the agent's; `POST /api/v1/conversation/again` reads `{how: "retry"|"regenerate"|"edit", text?, conversation?, turn?}` (a retry names the failed turn in `turn`) and answers 202 through `askAgain`, the code behind `/talk/again/:how`. The terminal has all three already: `src/tui/screens/talk_chats.ts` (pin), `src/tui/ssh-talk.ts` (again), `src/tui/screens/confirms.ts` (yes and no). | |
| 91 | 91 | - **MCP:** `stop` with no `repo` and no `run` stops the forge's agent; with both it stops that crew run; one without the other is an error. The agent's own token is refused. |
| 92 | 92 | - **Terminal:** `ssh <forge> talk /stop` (or `--stop`) exits 0 when stopped, 1 with "no answer is running". |
| 93 | 93 |
src/contract/openapi/routes/getApiV1ConversationConfirms.ts added +11
| @@ -0,0 +1,11 @@ | ||
| 1 | import type { Route } from "../shared.ts"; | |
| 2 | ||
| 3 | export const route: Route = { | |
| 4 | method: "get", | |
| 5 | path: "/api/v1/conversation-confirms", | |
| 6 | summary: "The confirms waiting on the signed-in person's yes or no, oldest first", | |
| 7 | detail: "The held actions the forge's agent asked the person to confirm and nobody has answered, oldest first; each has `does`, the sentence the web card shows, and the `arguments` a yes will send; only the caller's own, by their browser session or their own token (the forge's agent's token is refused)", | |
| 8 | auth: true, | |
| 9 | role: "member", | |
| 10 | refuses: [401], | |
| 11 | }; |
src/contract/openapi/routes/index.ts +1
| @@ -97,6 +97,7 @@ export { route as getApiV1Codes } from "./getApiV1Codes.ts"; | ||
| 97 | 97 | export { route as getApiV1Config } from "./getApiV1Config.ts"; |
| 98 | 98 | export { route as getApiV1Connections } from "./getApiV1Connections.ts"; |
| 99 | 99 | export { route as getApiV1Contribute } from "./getApiV1Contribute.ts"; |
| 100 | export { route as getApiV1ConversationConfirms } from "./getApiV1ConversationConfirms.ts"; | |
| 100 | 101 | export { route as getApiV1ConversationFilesId } from "./getApiV1ConversationFilesId.ts"; |
| 101 | 102 | export { route as getApiV1ConversationTurns } from "./getApiV1ConversationTurns.ts"; |
| 102 | 103 | export { route as getApiV1Conversations } from "./getApiV1Conversations.ts"; |
src/http/api/conversation_confirms.ts +18 −4
| @@ -7,19 +7,33 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | import { scopeAllows } from "../../core/tokens.ts"; |
| 10 | import { REFUSED, sayNo, sayYes } from "../../actions/conversation_confirms.ts"; | |
| 11 | import { crossOrigin, type Handler } from "../context.ts"; | |
| 10 | import { REFUSED, sayNo, sayYes, waitingConfirms } from "../../actions/conversation_confirms.ts"; | |
| 11 | import { crossOrigin, type Ctx, type Handler } from "../context.ts"; | |
| 12 | 12 | import { apiError, boundedBody, forbidden, json, unauthorized } from "../respond.ts"; |
| 13 | 13 | import type { Router } from "../router.ts"; |
| 14 | 14 | |
| 15 | /** | |
| 16 | * The person's own session or own token: never the forge's agent, a run, a contribute or an app | |
| 17 | * token. | |
| 18 | */ | |
| 19 | const ownAuth = (auth: Ctx["auth"]): boolean => | |
| 20 | auth.via === "session" || (auth.via === "token" && auth.through === undefined && auth.run === undefined && auth.contribute === undefined && auth.appId === undefined); | |
| 21 | ||
| 15 | 22 | export function conversationConfirmRoutes(router: Router<Handler>): void { |
| 23 | // What waits on the person's yes, oldest first, in the web card's own words: `does` is the | |
| 24 | // sentence the card shows, `arguments` everything it will send. Only the caller's own. | |
| 25 | router.add("GET", "/api/v1/conversation-confirms", (ctx) => { | |
| 26 | const user = ctx.auth.user; | |
| 27 | if (user === undefined) return unauthorized(); | |
| 28 | if (!ownAuth(ctx.auth)) return unauthorized(); | |
| 29 | return json({ confirms: waitingConfirms(ctx, user) }); | |
| 30 | }); | |
| 16 | 31 | router.add("POST", "/api/v1/conversation-confirms/:id", async (ctx) => { |
| 17 | 32 | const user = ctx.auth.user; |
| 18 | 33 | if (user === undefined) return unauthorized(); |
| 19 | 34 | // The person's own session, or their own rw token: never the forge's agent (a via token reaches |
| 20 | 35 | // /mcp alone) nor a run, contribute or app token. |
| 21 | const own = ctx.auth.via === "session" || (ctx.auth.via === "token" && ctx.auth.through === undefined && ctx.auth.run === undefined && ctx.auth.contribute === undefined && ctx.auth.appId === undefined); | |
| 22 | if (!own) return unauthorized(); | |
| 36 | if (!ownAuth(ctx.auth)) return unauthorized(); | |
| 23 | 37 | if (crossOrigin(ctx.request, ctx.url, ctx.app.log)) return forbidden(); |
| 24 | 38 | if (ctx.auth.via === "token" && !scopeAllows(ctx.auth.scope ?? "ro", "rw")) return forbidden(); |
| 25 | 39 | // No body is a yes, as before; a body says `answer`: yes or no. |
src/http/lazy_lists.ts +1
| @@ -1523,6 +1523,7 @@ export const API_ROUTES = [ | ||
| 1523 | 1523 | ["POST", "/api/v1/repos/:owner/:name/changes/:room"], |
| 1524 | 1524 | ["POST", "/api/v1/admin/releases/:sha/rollback"], |
| 1525 | 1525 | ["POST", "/api/v1/repos/:owner/:name/pulls/:number/take-back"], |
| 1526 | ["GET", "/api/v1/conversation-confirms"], | |
| 1526 | 1527 | ["POST", "/api/v1/conversation-confirms/:id"], |
| 1527 | 1528 | ["POST", "/api/v1/conversation/messages"], |
| 1528 | 1529 | ["POST", "/api/v1/conversation/again"], |
test/apple-held.test.ts added +100
| @@ -0,0 +1,100 @@ | ||
| 1 | // The Apple app lists and answers the actions Talk holds for a person (#18338): the same words as | |
| 2 | // the web card, only for the person it was held for, and a second answer is a 409 with no second | |
| 3 | // effect. | |
| 4 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 5 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 6 | import { tmpdir } from "node:os"; | |
| 7 | import { join } from "node:path"; | |
| 8 | import { createApp, type App } from "../src/app.ts"; | |
| 9 | import { createRepo } from "../src/actions/repos.ts"; | |
| 10 | import { waitingConfirms } from "../src/actions/conversation_confirms.ts"; | |
| 11 | import { hold } from "../src/core/fence.ts"; | |
| 12 | import { createIssue, getIssue } from "../src/core/issues.ts"; | |
| 13 | import { createToken } from "../src/core/tokens.ts"; | |
| 14 | import { createUser, type User } from "../src/core/users.ts"; | |
| 15 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 16 | ||
| 17 | let dir = ""; | |
| 18 | let app: App; | |
| 19 | let servers: Servers; | |
| 20 | let ada: User; | |
| 21 | let bob: User; | |
| 22 | let adaToken = ""; | |
| 23 | let bobToken = ""; | |
| 24 | let n = 0; | |
| 25 | ||
| 26 | beforeAll(async () => { | |
| 27 | dir = await mkdtemp(join(tmpdir(), "apple-held-")); | |
| 28 | app = createApp({ dataDir: dir, port: 0 }); | |
| 29 | servers = await serve(app); | |
| 30 | const people: User[] = []; | |
| 31 | for (const username of ["ada", "bob"]) { | |
| 32 | const made = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", ...(username === "ada" ? { adminIfFirst: true } : {}) }); | |
| 33 | if (!made.ok) throw new Error(made.error); | |
| 34 | people.push(made.value); | |
| 35 | } | |
| 36 | [ada, bob] = people as [User, User]; | |
| 37 | adaToken = createToken(app.db, ada.id, "ios", "rw").token; | |
| 38 | bobToken = createToken(app.db, bob.id, "ios", "rw").token; | |
| 39 | }, 30_000); | |
| 40 | afterAll(async () => { | |
| 41 | await servers.stop(); | |
| 42 | app.close(); | |
| 43 | await rm(dir, { recursive: true, force: true }); | |
| 44 | }); | |
| 45 | ||
| 46 | const url = "/api/v1/conversation-confirms"; | |
| 47 | const get = (token: string) => fetch(`${servers.url}${url}`, { headers: { authorization: `Bearer ${token}` } }); | |
| 48 | const answer = (token: string, id: string, body?: unknown) => | |
| 49 | fetch(`${servers.url}${url}/${id}`, { method: "POST", headers: { authorization: `Bearer ${token}`, origin: servers.url, "content-type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body) }); | |
| 50 | ||
| 51 | /** A held `close` of a fresh issue: said yes to, the issue is closed, so the effect is visible. */ | |
| 52 | async function held() { | |
| 53 | n++; | |
| 54 | const made = await createRepo(app, ada, { name: `held${n}`, visibility: "public" }); | |
| 55 | if (!made.ok) throw new Error(String(made.error)); | |
| 56 | const issue = createIssue(app.db, { repoId: made.value.id, authorId: ada.id, title: `Held ${n}` }); | |
| 57 | if (!issue.ok) throw new Error(issue.error); | |
| 58 | const id = hold(app.db, `conversation:c${n}`, ada.id, "close", { repo: `ada/held${n}`, number: issue.value.number }); | |
| 59 | if (id === null) throw new Error("not held"); | |
| 60 | const state = () => getIssue(app.db, made.value.id, issue.value.number)!.state; | |
| 61 | return { id, state }; | |
| 62 | } | |
| 63 | ||
| 64 | test("lists the caller's held actions in the web card's words, and only theirs", async () => { | |
| 65 | const { id } = await held(); | |
| 66 | const res = await get(adaToken); | |
| 67 | expect(res.status).toBe(200); | |
| 68 | const body = (await res.json()) as { confirms: { id: string; does: string }[] }; | |
| 69 | const mine = body.confirms.find((c) => c.id === id)!; | |
| 70 | expect(mine.does).toBe(waitingConfirms({ app }, ada).find((w) => w.id === id)!.does); | |
| 71 | expect(((await (await get(bobToken)).json()) as { confirms: unknown[] }).confirms).toEqual([]); | |
| 72 | expect((await fetch(`${servers.url}${url}`)).status).toBe(401); | |
| 73 | }); | |
| 74 | ||
| 75 | test("approve does the call once; answering again is a 409 and does nothing more", async () => { | |
| 76 | const { id, state } = await held(); | |
| 77 | expect((await answer(adaToken, id, { answer: "yes" })).status).toBe(200); | |
| 78 | expect(state()).toBe("closed"); | |
| 79 | const again = await answer(adaToken, id, { answer: "yes" }); | |
| 80 | expect(again.status).toBe(409); | |
| 81 | expect(state()).toBe("closed"); | |
| 82 | expect(((await (await get(adaToken)).json()) as { confirms: { id: string }[] }).confirms.some((c) => c.id === id)).toBe(false); | |
| 83 | }); | |
| 84 | ||
| 85 | test("decline does nothing and spends the confirm", async () => { | |
| 86 | const { id, state } = await held(); | |
| 87 | expect((await answer(adaToken, id, { answer: "no" })).status).toBe(200); | |
| 88 | expect(state()).toBe("open"); | |
| 89 | expect((await answer(adaToken, id, { answer: "yes" })).status).toBe(409); | |
| 90 | expect(state()).toBe("open"); | |
| 91 | }); | |
| 92 | ||
| 93 | test("another person gets 404, and the action still waits for the right one", async () => { | |
| 94 | const { id, state } = await held(); | |
| 95 | expect((await answer(bobToken, id, { answer: "yes" })).status).toBe(404); | |
| 96 | expect((await answer(bobToken, id, { answer: "no" })).status).toBe(404); | |
| 97 | expect(state()).toBe("open"); | |
| 98 | expect((await answer(adaToken, id)).status).toBe(200); | |
| 99 | expect(state()).toBe("closed"); | |
| 100 | }); |
test/route-timing-coverage.test.ts +1
| @@ -22,6 +22,7 @@ import { seedAdmin } from "./helpers/admin.ts"; | ||
| 22 | 22 | /** Routes that answer 200 without a `session` phase → why. */ |
| 23 | 23 | const UNTIMED: Record<string, string> = { |
| 24 | 24 | "/api/v1/models": "the models offered to a person, two indexed reads; no page to phase", |
| 25 | "/api/v1/conversation-confirms": "the caller's held confirms, one indexed read; no page to phase", | |
| 25 | 26 | "/api/v1/conversations": "a person's conversation list, one indexed query; no page to phase", |
| 26 | 27 | "/api/v1/admin/machines/:machine/disks": "an owner's per-machine disk summary, one indexed read; no page to phase", |
| 27 | 28 | "/api/v1/repos/:owner/:name/issues/:number/estimates": "an issue's base rate from one indexed read of its history; no page to phase", |
new above
offline