Public: anyone can read this; the crew can change it.
A person deletes one of their imported sessions from its page, by typing its name #18371
Merged 1h 40m ago, live in production
scout/forge:scout/18359-a-person-deletes-one-of-their-imported-s → main 0 commits fork 3 threads · 3 unresolved
9 files
changed +245
−10
docs/features/traces.md +1 −1
| @@ -58,7 +58,7 @@ For Codex, point the same command at a rollout: `--data-binary @"$(ls -t ~/.code | ||
| 58 | 58 | |
| 59 | 59 | A new source is one file implementing `ImportAdapter` (`src/trace/import_adapter.ts`, see `import_claude_code.ts`) and one line in `ADAPTERS`; Codex rollouts are taken the same way (`import_codex.ts`). |
| 60 | 60 | |
| 61 | `DELETE /api/v1/traces/<id>` (#18388) deletes one imported session, with `{ "confirm": "<the session's title>" }` as its body (at most 4 KiB; `traceDelete` in `src/kernel/body_caps.ts`). Nothing is removed unless the title is sent exactly: a wrong or missing one is a 422 (`name_mismatch`) and the file and rows stay. Only the importer may: another person's or an unknown id is a 404, a crew run or Talk turn is a 403 (`record_kept`, its record is kept), a session caller's cross-origin request is a 403, and a session with a live share link is a 409 (`shared`: stop sharing first). A right name removes `traces/imported/<id>.jsonl.gz`, the index row and its touched tools and files (`deleteImported` in `src/trace/import.ts`), frees the quota, and answers `{ removed, title, bytes }`. Not yet: a button on the page and a terminal screen. | |
| 61 | `DELETE /api/v1/traces/<id>` (#18388) deletes one imported session, with `{ "confirm": "<the session's title>" }` as its body (at most 4 KiB; `traceDelete` in `src/kernel/body_caps.ts`). Nothing is removed unless the title is sent exactly: a wrong or missing one is a 422 (`name_mismatch`) and the file and rows stay. Only the importer may: another person's or an unknown id is a 404, a crew run or Talk turn is a 403 (`record_kept`, its record is kept), a session caller's cross-origin request is a 403, and a session with a live share link is a 409 (`shared`: stop sharing first). A right name removes `traces/imported/<id>.jsonl.gz`, the index row and its touched tools and files (`deleteImported` in `src/trace/import.ts`), frees the quota, and answers `{ removed, title, bytes }`. The red Delete on the session's page (the title typed exactly, a plain click does nothing) and `X` in the terminal's Traces list do the same and answer a receipt (#18359); on a shared session they say to stop sharing first, so deleting never leaves a link that a re-import of the same session would bring back. Not yet: an undo. | |
| 62 | 62 | |
| 63 | 63 | `/traces/<id>` of an imported trace (#18357) is the same tree and strip a crew run has, read from the stored file (`src/trace/imported_read.ts`, `src/web/pages/trace_imported.ts`), with `#step-N` permalinks and each step's raw lines (the file's lines that name it) on demand at `/traces/<id>/steps/<n>`. Only the importer reads it; anyone else gets the not-found an unknown id gets. Each step line shows the tokens its file held for it (`5 in, 7 cached, 9 out`; a reply split across records is counted once) and the total is their sum; a session holds no cost, and sometimes no tokens: the page says "not recorded in this file" rather than zero. Steps are capped at 20,000. `GET /api/v1/traces/<id>` answers the same tree for an imported id. |
| 64 | 64 | ## One trace as a tree (#18022) |
src/http/lazy_lists.ts +1
src/http/routes/operations.ts +2
| @@ -24,6 +24,7 @@ import { resiliencePage, nightlyOutputPage } from "../../web/pages/resilience.ts | ||
| 24 | 24 | import { runRedirect } from "../../web/pages/runs.ts"; |
| 25 | 25 | import { tracePage, traceStep } from "../../web/pages/trace.ts"; |
| 26 | 26 | import { traceComparePage } from "../../web/pages/trace_compare.ts"; |
| 27 | import { traceDelete } from "../../web/pages/trace_imported.ts"; | |
| 27 | 28 | import { traceShare, traceShared, traceUnshare } from "../../web/pages/trace_share.ts"; |
| 28 | 29 | import { tracesPage } from "../../web/pages/traces.ts"; |
| 29 | 30 | import { traceImport } from "../../web/pages/trace_import.ts"; |
| @@ -135,5 +136,6 @@ export function operationsRoutes(router: Router<Handler>, knownPatterns: () => s | ||
| 135 | 136 | router.add("GET", "/traces/:id/compare/:other", traceComparePage); |
| 136 | 137 | router.add("POST", "/traces/:id/share", traceShare); |
| 137 | 138 | router.add("POST", "/traces/:id/unshare", traceUnshare); |
| 139 | router.add("POST", "/traces/:id/delete", traceDelete); | |
| 138 | 140 | router.add("GET", "/traces/shared/:token", traceShared); |
| 139 | 141 | } |
src/trace/import.ts +9
| @@ -60,6 +60,15 @@ export function removeImported(dataDir: string, ids: readonly string[]): void { | ||
| 60 | 60 | for (const id of ids) rmSync(fileOf(dataDir, id), { force: true }); |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | /** The words a deleted session's receipt says, the page's and the terminal's too. */ | |
| 64 | export const deletedWords = (title: string, bytes: number): string => `Removed the imported session "${title}" (${bytes} bytes freed).`; | |
| 65 | ||
| 66 | /** Why a crew run or Talk turn is never deleted. */ | |
| 67 | export const KEPT_WORDS = "a crew run's or Talk turn's record is kept forever: only an imported session can be deleted"; | |
| 68 | ||
| 69 | /** Why a shared session is not deleted yet. */ | |
| 70 | export const SHARED_WORDS = "this session has a live share link: stop sharing it first"; | |
| 71 | ||
| 63 | 72 | /** What a removal answers: the session's title and the stored bytes it frees. */ |
| 64 | 73 | export type Removal = |
| 65 | 74 | | { readonly ok: true; readonly title: string; readonly bytes: number } |
src/tui/screens/traces.ts +31 −6
| @@ -10,9 +10,10 @@ | ||
| 10 | 10 | * page's own `readFilter`. `b` types the name of a saved search (the page's links) and opens the |
| 11 | 11 | * first that starts with it, `w` names the filters on screen and keeps them, `x` removes the saved |
| 12 | 12 | * search last opened (the search only: no trace is touched); a facet a saved search holds that no |
| 13 | * longer exists is dropped and said. On an imported session `s` makes its share link (shown once; | |
| 14 | * a second press confirms when one is live, as it ends the old) and `z` ends it (#18377). Esc | |
| 15 | * closes. The lines are `traceLine`, the page's own row. | |
| 13 | * longer exists is dropped and said. `X` on an imported session asks for its title typed and | |
| 14 | * deletes it (#18359). On an imported session `s` makes its share link (shown once; a second press | |
| 15 | * confirms when one is live, as it ends the old) and `z` ends it (#18377). Esc closes. The lines | |
| 16 | * are `traceLine`, the page's own row. | |
| 16 | 17 | */ |
| 17 | 18 | import { binding } from "../app/keys.ts"; |
| 18 | 19 | import type { Ctx, Screen } from "../app/app.ts"; |
| @@ -23,7 +24,8 @@ import { openedSaved, removeSaved, SAVE_WORDS, saveSearch, savedWords, NAME_MAX, | ||
| 23 | 24 | import { sameIssue } from "../../trace/compare.ts"; |
| 24 | 25 | import { traceScreen } from "./trace.ts"; |
| 25 | 26 | import { importedSharedNow, sharedHref, shareImported, unshareImported } from "../../actions/trace_share.ts"; |
| 26 | import { Scroll, openOnWeb } from "./common.ts"; | |
| 27 | import { Scroll, openOnWeb, promptScreen } from "./common.ts"; | |
| 28 | import { deletedWords, deleteImported, SHARED_WORDS } from "../../trace/import.ts"; | |
| 27 | 29 | import { style } from "../term/style.ts"; |
| 28 | 30 | import { truncate } from "../term/width.ts"; |
| 29 | 31 | import { emptyState } from "../ui/widgets.ts"; |
| @@ -31,6 +33,8 @@ import type { Session } from "../session.ts"; | ||
| 31 | 33 | import type { PaletteEntry } from "./entries.ts"; |
| 32 | 34 | |
| 33 | 35 | function tracesScreen(f: Session, ctx: Ctx): Screen { |
| 36 | // eslint-disable-next-line prefer-const | |
| 37 | let screen: Screen; | |
| 34 | 38 | const scroll = new Scroll(); |
| 35 | 39 | // What the page holds in its URL, as strings; `readFilter` is the one validator of them. |
| 36 | 40 | const facet: { [K in "q" | "kind" | "repo" | "model" | "outcome" | "by" | "tool" | "file" | "from" | "to"]?: string } = {}; |
| @@ -83,9 +87,9 @@ function tracesScreen(f: Session, ctx: Ctx): Screen { | ||
| 83 | 87 | const held = marked.length === 0 ? "" : ` marked ${marked.length}/2 (*)`; |
| 84 | 88 | return `${on.length === 0 ? "" : ` [${on.map(([k, v]) => `${k}=${v}`).join(" ")}]`}${held}`; |
| 85 | 89 | }; |
| 86 | return { | |
| 90 | screen = { | |
| 87 | 91 | title: "Traces", |
| 88 | keys: [binding(["j", "k"], "move"), binding(["enter"], "open the trace"), binding(["v"], "mark"), binding(["d"], "compare marked"), binding(["n"], "older"), binding(["/"], "search"), binding(["i", "r", "m", "o", "p"], "kind, repo, model, outcome, person"), binding(["u", "e"], "tool used, file touched"), binding(["f", "t"], "from, to day"), binding(["b", "w", "x"], "open, save, remove a saved search"), binding(["y"], "import a session on the web"), binding(["s", "z"], "share an imported session, stop sharing"), binding(["c"], "clear filters"), binding(["esc"], "close")], | |
| 92 | keys: [binding(["j", "k"], "move"), binding(["enter"], "open the trace"), binding(["v"], "mark"), binding(["d"], "compare marked"), binding(["X"], "delete an imported session"), binding(["n"], "older"), binding(["/"], "search"), binding(["i", "r", "m", "o", "p"], "kind, repo, model, outcome, person"), binding(["u", "e"], "tool used, file touched"), binding(["f", "t"], "from, to day"), binding(["b", "w", "x"], "open, save, remove a saved search"), binding(["y"], "import a session on the web"), binding(["s", "z"], "share an imported session, stop sharing"), binding(["c"], "clear filters"), binding(["esc"], "close")], | |
| 89 | 93 | render(area, s) { |
| 90 | 94 | const c = ctx.theme.c; |
| 91 | 95 | s.text(area.x, area.y, truncate(`${typing === undefined ? "/ to search" : typing === "q" ? "Search" : typing === "from" ? "From (YYYY-MM-DD)" : typing === "to" ? "To (YYYY-MM-DD)" : typing === "tool" ? "Tool (Edit, Bash…)" : "File (path)"}: ${typing === undefined || typing === "q" ? (facet.q ?? "") : (facet[typing] ?? "")}${typing === undefined ? "" : "_"}${summary()}`, area.w), style(c.fg, c.bg), area.x + area.w); |
| @@ -160,6 +164,26 @@ function tracesScreen(f: Session, ctx: Ctx): Screen { | ||
| 160 | 164 | if (repo === undefined || !sameIssue(ra, rb)) ctx.toast("those two are not runs of one issue", "attn"); |
| 161 | 165 | else ctx.push(traceScreen(f, ctx, repo, ra.id, rb.id)); |
| 162 | 166 | } |
| 167 | } else if (e.key === "X") { | |
| 168 | // The page's red Delete: the title typed exactly, then the session goes (#18359). | |
| 169 | const r = list.rows[at]; | |
| 170 | if (r === undefined || r.source !== "imported") ctx.toast("only an imported session can be deleted: a run's or Talk turn's record is kept forever", "attn"); | |
| 171 | else { | |
| 172 | const row = screen; | |
| 173 | ctx.push( | |
| 174 | promptScreen(ctx, row, { | |
| 175 | title: "Delete this session for good", | |
| 176 | label: `type its title exactly: ${r.title}`, | |
| 177 | onSubmit: (typed) => { | |
| 178 | const done = deleteImported(f.app.db, f.app.config.dataDir, f.user?.id ?? "", r.id, typed); | |
| 179 | if (!done.ok) return done.error === "shared" ? SHARED_WORDS : "that is not its title: nothing was removed"; | |
| 180 | ctx.toast(deletedWords(done.title, done.bytes), "success"); | |
| 181 | read(); | |
| 182 | return undefined; | |
| 183 | }, | |
| 184 | }), | |
| 185 | ); | |
| 186 | } | |
| 163 | 187 | } else if (e.key === "y") openOnWeb(f, ctx, "/traces#traces-import", "the import form (a file input)"); |
| 164 | 188 | else if (e.key === "s" || e.key === "z") { |
| 165 | 189 | const r = list.rows[at]; |
| @@ -190,6 +214,7 @@ function tracesScreen(f: Session, ctx: Ctx): Screen { | ||
| 190 | 214 | return true; |
| 191 | 215 | }, |
| 192 | 216 | }; |
| 217 | return screen; | |
| 193 | 218 | } |
| 194 | 219 | |
| 195 | 220 | export const entry: PaletteEntry = { |
src/web/pages/trace_imported.ts +27 −3
| @@ -7,16 +7,17 @@ | ||
| 7 | 7 | import { importedSharedNow } from "../../actions/trace_share.ts"; |
| 8 | 8 | import type { Ctx } from "../../http/context.ts"; |
| 9 | 9 | import { securityFloor } from "../../http/respond.ts"; |
| 10 | import { importedLines } from "../../trace/import.ts"; | |
| 10 | import { deletedWords, deleteImported, importedLines, KEPT_WORDS, SHARED_WORDS } from "../../trace/import.ts"; | |
| 11 | 11 | import { parseImported, rawLinesOf } from "../../trace/imported_read.ts"; |
| 12 | 12 | import type { TraceRow } from "../../trace/index_table.ts"; |
| 13 | 13 | import { stepsOf } from "../../trace/model.ts"; |
| 14 | 14 | import { spanWords, stepLine } from "../../trace/read.ts"; |
| 15 | 15 | import { stepNumber } from "../../trace/run_log.ts"; |
| 16 | 16 | import { seenTraceRow } from "../../trace/tree.ts"; |
| 17 | import { notFoundPage, pageHead } from "../chrome.ts"; | |
| 17 | import { forbiddenPage, notFoundPage, pageHead } from "../chrome.ts"; | |
| 18 | import { readForm } from "../form.ts"; | |
| 18 | 19 | import { html } from "../html.ts"; |
| 19 | import { page } from "../layout.ts"; | |
| 20 | import { flashTo, page } from "../layout.ts"; | |
| 20 | 21 | import { behaviorsJs } from "../behaviors.ts"; |
| 21 | 22 | import { strip, TRACE_CSS, TRACE_JS, treeRows } from "./trace.ts"; |
| 22 | 23 | |
| @@ -47,6 +48,11 @@ export function importedTracePage(ctx: Ctx, row: TraceRow): Response { | ||
| 47 | 48 | <div><dt>Cost</dt><dd id="trace-cost">${row.costUsd > 0 ? `$${row.costUsd.toFixed(2)}` : "not recorded in this file"}</dd></div> |
| 48 | 49 | </dl> |
| 49 | 50 | <nav class="bar small" id="trace-links" aria-label="Where this trace is"><a href="/traces">All traces</a></nav> |
| 51 | <form method="post" class="bar small" id="trace-delete" action="${base}/delete" autocomplete="off"> | |
| 52 | <label for="trace-delete-name" class="muted">Delete this session for good: type its title exactly to confirm.</label> | |
| 53 | <input class="input" id="trace-delete-name" name="name" required autocomplete="off" spellcheck="false" placeholder="${row.title}" /> | |
| 54 | <button type="submit" class="btn" data-variant="danger">Delete</button> | |
| 55 | </form> | |
| 50 | 56 | ${shared === undefined ? null : html`<form method="post" class="bar small" id="trace-share" action="${base}/${shared ? "unshare" : "share"}"> |
| 51 | 57 | <span class="muted">${shared ? "A share link is live: anyone holding it reads this session, secrets redacted." : "Not shared. A link lets anyone who holds it read this session. The forge hides the credentials it knows, but a file can hold others, so share only what you have read."}</span> |
| 52 | 58 | <button type="submit">${shared ? "Stop sharing" : "Make a share link"}</button> |
| @@ -71,3 +77,21 @@ export function importedStep(ctx: Ctx, row: TraceRow): Response { | ||
| 71 | 77 | }), |
| 72 | 78 | ); |
| 73 | 79 | } |
| 80 | ||
| 81 | // POST /traces/:id/delete — the form on an imported session's page: the title typed, then it goes. | |
| 82 | export async function traceDelete(ctx: Ctx): Promise<Response> { | |
| 83 | const form = await readForm(ctx, "rw"); | |
| 84 | if (form instanceof Response) return form; | |
| 85 | const user = ctx.auth.user; | |
| 86 | const id = ctx.params.id ?? ""; | |
| 87 | const seen = user === undefined ? undefined : seenTraceRow(ctx.app, user, id); | |
| 88 | if (user === undefined || seen === undefined) return notFoundPage(ctx); | |
| 89 | if (seen.kind !== "imported") return forbiddenPage(ctx, `${KEPT_WORDS[0]!.toUpperCase()}${KEPT_WORDS.slice(1)}.`); | |
| 90 | const name = form.get("name"); | |
| 91 | const done = deleteImported(ctx.app.db, ctx.app.config.dataDir, user.id, id, name); | |
| 92 | if (!done.ok) { | |
| 93 | const message = done.error === "shared" ? `${SHARED_WORDS[0]!.toUpperCase()}${SHARED_WORDS.slice(1)}.` : "That is not the session's title: nothing was removed."; | |
| 94 | return flashTo(ctx, `/traces/${id}`, { message, tone: "danger" }); | |
| 95 | } | |
| 96 | return flashTo(ctx, "/traces", { message: deletedWords(done.title, done.bytes) }); | |
| 97 | } |
test/trace-delete.test.ts added +121
| @@ -0,0 +1,121 @@ | ||
| 1 | /** | |
| 2 | * An imported session is deleted by its owner, with its title typed, and by no one else (#18359). | |
| 3 | */ | |
| 4 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 5 | import { existsSync } from "node:fs"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { createSession } from "../src/core/sessions.ts"; | |
| 11 | import { createToken } from "../src/core/tokens.ts"; | |
| 12 | import { createUser, type User } from "../src/core/users.ts"; | |
| 13 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 14 | import { createRepo } from "../src/actions/repos.ts"; | |
| 15 | import { indexRun, putTraceRow } from "../src/trace/index_table.ts"; | |
| 16 | import { seedAdmin } from "./helpers/admin.ts"; | |
| 17 | ||
| 18 | const rec = (o: object): string => JSON.stringify({ sessionId: "sess-d", cwd: "/w", ...o }); | |
| 19 | const SESSION = [ | |
| 20 | rec({ type: "user", timestamp: "2026-12-01T09:00:00Z", message: { role: "user", content: "fix the login bug" } }), | |
| 21 | rec({ type: "assistant", timestamp: "2026-12-01T09:00:01Z", message: { id: "m1", model: "claude-x", role: "assistant", content: [{ type: "text", text: "Done" }] } }), | |
| 22 | ].join("\n"); | |
| 23 | const TITLE = "fix the login bug"; | |
| 24 | ||
| 25 | let dir = ""; | |
| 26 | let app: App, servers: Servers; | |
| 27 | let alice0: User; | |
| 28 | let alice = "", bob = "", id = "", aliceCookie = ""; | |
| 29 | ||
| 30 | const api = (path: string, token: string, init: RequestInit = {}) => fetch(`${servers.url}${path}`, { ...init, headers: { authorization: `Bearer ${token}`, ...(init.headers ?? {}) } }); | |
| 31 | const del = (token: string, name: unknown, target = id) => api(`/api/v1/traces/${target}`, token, { method: "DELETE", body: JSON.stringify({ confirm: name }) }); | |
| 32 | const list = async (token: string) => ((await (await api("/api/v1/traces?kind=imported", token)).json()) as { items: { id: string }[] }).items.map((i) => i.id); | |
| 33 | const stored = () => existsSync(join(dir, "traces", "imported", `${id}.jsonl.gz`)); | |
| 34 | ||
| 35 | beforeAll(async () => { | |
| 36 | dir = await mkdtemp(join(tmpdir(), "forge-trace-delete-")); | |
| 37 | app = createApp({ dataDir: dir, port: 0 }); | |
| 38 | servers = await serve(app); | |
| 39 | const a = await seedAdmin(app, { username: "alice", email: "[email protected]", password: "correct-horse-battery" }); | |
| 40 | if (!a.ok) throw new Error(a.error); | |
| 41 | const b = await createUser(app.db, { origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-good-passphrase" }); | |
| 42 | if (!b.ok) throw new Error(b.error); | |
| 43 | alice0 = a.value; | |
| 44 | alice = createToken(app.db, a.value.id, "t", "rw").token; | |
| 45 | bob = createToken(app.db, b.value.id, "t", "rw").token; | |
| 46 | aliceCookie = `forge_session=${createSession(app.db, a.value.id).token}`; | |
| 47 | const sent = await api("/api/v1/traces/import", alice, { method: "POST", body: SESSION }); | |
| 48 | id = ((await sent.json()) as { id: string }).id; | |
| 49 | putTraceRow(app.db, { id: "f".repeat(32), kind: "talk", repository: "", title: "a talk turn", model: "", account: "", steps: 0, errors: 0, tokensIn: 0, tokensCached: 0, tokensOut: 0, costUsd: 0, outcome: "ok", started: "2026-12-01T00:00:00.000Z", ended: "2026-12-01T00:00:00.000Z" }, ""); | |
| 50 | }, 30_000); | |
| 51 | ||
| 52 | afterAll(async () => { | |
| 53 | await servers.stop(); | |
| 54 | await rm(dir, { recursive: true, force: true }); | |
| 55 | }); | |
| 56 | ||
| 57 | test("the page offers a Delete asking for the title; a wrong name, a cross-site post and a stranger remove nothing", async () => { | |
| 58 | const page = await (await fetch(`${servers.url}/traces/${id}`, { headers: { cookie: aliceCookie } })).text(); | |
| 59 | expect(page).toContain(`action="/traces/${id}/delete"`); | |
| 60 | expect(page).toContain('data-variant="danger"'); | |
| 61 | ||
| 62 | const form = (name: string) => ({ method: "POST", redirect: "manual" as const, headers: { cookie: aliceCookie, "content-type": "application/x-www-form-urlencoded", origin: servers.url }, body: new URLSearchParams({ name }) }); | |
| 63 | const wrong = await fetch(`${servers.url}/traces/${id}/delete`, form("fix the login")); | |
| 64 | expect(wrong.status).toBe(303); | |
| 65 | expect(stored()).toBe(true); | |
| 66 | expect(await list(alice)).toContain(id); | |
| 67 | ||
| 68 | const cross = await fetch(`${servers.url}/traces/${id}/delete`, { ...form(TITLE), headers: { cookie: aliceCookie, "content-type": "application/x-www-form-urlencoded", origin: "https://evil.example" } }); | |
| 69 | expect(cross.status).toBe(403); | |
| 70 | expect(stored()).toBe(true); | |
| 71 | ||
| 72 | const crossApi = await fetch(`${servers.url}/api/v1/traces/${id}`, { method: "DELETE", headers: { cookie: aliceCookie, origin: "https://evil.example" }, body: JSON.stringify({ confirm: TITLE }) }); | |
| 73 | expect(crossApi.status).toBe(403); | |
| 74 | expect(stored()).toBe(true); | |
| 75 | }); | |
| 76 | ||
| 77 | test("a wrong or missing name is a 422 and removes nothing; another user gets 404; a crew or Talk record is a 403 with the reason", async () => { | |
| 78 | expect((await del(alice, "fix")).status).toBe(422); | |
| 79 | expect((await del(alice, "")).status).toBe(422); | |
| 80 | expect((await del(alice, 7)).status).toBe(422); | |
| 81 | expect((await api(`/api/v1/traces/${id}`, alice, { method: "DELETE" })).status).toBe(422); | |
| 82 | expect(stored()).toBe(true); | |
| 83 | expect(await list(alice)).toContain(id); | |
| 84 | ||
| 85 | expect((await del(bob, TITLE)).status).toBe(404); | |
| 86 | expect((await del(alice, TITLE, "0".repeat(32))).status).toBe(404); | |
| 87 | expect(stored()).toBe(true); | |
| 88 | ||
| 89 | // A crew run alice may read is kept: the 403 says why, and the row stays. | |
| 90 | const repo = await createRepo(app, alice0, { name: "mix", visibility: "public", description: "" }); | |
| 91 | if (!repo.ok) throw new Error("repo failed"); | |
| 92 | app.db.prepare("INSERT INTO run (id, repo_id, kind, state, summary, model, actor_id, cost_usd, created_at) VALUES ('run1', ?1, 'agent', 'ok', 'crew run', 'm', ?2, 0, '2026-12-03T10:00:00.000Z')").run(repo.value.id, alice0.id); | |
| 93 | expect(indexRun(app.db, dir, "run1")).toBe(true); | |
| 94 | const kept = await del(alice, "crew run", "run1"); | |
| 95 | expect(kept.status).toBe(403); | |
| 96 | expect(JSON.stringify(await kept.json())).toContain("record_kept"); | |
| 97 | expect(app.db.query("SELECT 1 FROM trace_index WHERE id = 'run1'").get()).not.toBeNull(); | |
| 98 | }); | |
| 99 | ||
| 100 | test("the right name removes the file and the row, frees the quota, and answers a receipt; the list no longer shows it", async () => { | |
| 101 | const res = await del(alice, TITLE); | |
| 102 | expect(res.status).toBe(200); | |
| 103 | const body = (await res.json()) as { removed: boolean; title: string; bytes: number }; | |
| 104 | expect(body.removed).toBe(true); | |
| 105 | expect(body.bytes).toBeGreaterThan(0); | |
| 106 | expect(body.title).toBe(TITLE); | |
| 107 | expect(stored()).toBe(false); | |
| 108 | expect(await list(alice)).not.toContain(id); | |
| 109 | expect((await api(`/api/v1/traces/${id}`, alice)).status).toBe(404); | |
| 110 | expect((await del(alice, TITLE)).status).toBe(404); | |
| 111 | // The same session can be imported again: the quota and the id are free. | |
| 112 | expect((await api("/api/v1/traces/import", alice, { method: "POST", body: SESSION })).status).toBe(201); | |
| 113 | }); | |
| 114 | ||
| 115 | test("the page's form removes it too and says what went", async () => { | |
| 116 | const again = ((await (await api("/api/v1/traces?kind=imported", alice)).json()) as { items: { id: string }[] }).items[0]!.id; | |
| 117 | const res = await fetch(`${servers.url}/traces/${again}/delete`, { method: "POST", redirect: "manual", headers: { cookie: aliceCookie, "content-type": "application/x-www-form-urlencoded", origin: servers.url }, body: new URLSearchParams({ name: TITLE }) }); | |
| 118 | expect(res.status).toBe(303); | |
| 119 | expect(res.headers.get("location")).toContain("/traces"); | |
| 120 | expect(await list(alice)).not.toContain(again); | |
| 121 | }); |
test/traces-tui.test.ts +40
| @@ -88,6 +88,46 @@ test("i cycles the kind: runs, then only Talk, then only imported; Enter on an i | ||
| 88 | 88 | app.stop(); |
| 89 | 89 | }); |
| 90 | 90 | |
| 91 | test("X on an imported session asks for its title: a wrong one removes nothing, the right one does; on a run it says it is kept (#18359)", async () => { | |
| 92 | const gone = "a".repeat(32); | |
| 93 | putTraceRow( | |
| 94 | forge.db, | |
| 95 | { id: gone, kind: "imported", repository: "", title: "doomed session", model: "m", account: "", steps: 1, errors: 0, tokensIn: 0, tokensCached: 0, tokensOut: 0, costUsd: 0, outcome: "ok", started: "2026-12-05T10:00:00.000Z", ended: "2026-12-05T10:01:00.000Z" }, | |
| 96 | alice.id, | |
| 97 | ); | |
| 98 | const { app, session } = createForgeApp(forge, { sshUsername: "alice", remoteAddress: "127.0.0.1", user: alice }, { terminal: headlessTerminal(120, 30), exitHooks: false }); | |
| 99 | app.start(); | |
| 100 | await settleApp(app); | |
| 101 | entry.run(session, app.ctx); | |
| 102 | await settleApp(app); | |
| 103 | const press = async (key: string): Promise<string> => { | |
| 104 | app.dispatch({ type: "key", key, ctrl: false, alt: false, shift: false }); | |
| 105 | await settleApp(app); | |
| 106 | return app.screen.toText(); | |
| 107 | }; | |
| 108 | const type = async (text: string): Promise<void> => { | |
| 109 | for (const ch of text) await press(ch); | |
| 110 | }; | |
| 111 | await press("j"); | |
| 112 | await press("j"); | |
| 113 | await press("j"); // the crew run | |
| 114 | expect(await press("X")).toContain("is kept forever"); | |
| 115 | await press("k"); | |
| 116 | await press("k"); | |
| 117 | await press("k"); // the newest is "doomed session" | |
| 118 | const asked = await press("X"); | |
| 119 | expect(asked).toContain("doomed session"); | |
| 120 | await type("doomed"); | |
| 121 | expect(await press("enter")).toContain("nothing was removed"); | |
| 122 | expect(forge.db.query("SELECT 1 FROM trace_index WHERE id = ?1").get(gone)).not.toBeNull(); | |
| 123 | await type(" session"); | |
| 124 | const after = await press("enter"); | |
| 125 | expect(forge.db.query("SELECT 1 FROM trace_index WHERE id = ?1").get(gone)).toBeNull(); | |
| 126 | expect(after).toContain("Removed the imported session"); | |
| 127 | expect(after).not.toContain("doomed session · imported"); | |
| 128 | app.stop(); | |
| 129 | }); | |
| 130 | ||
| 91 | 131 | test("y says where the import form is on the web, and the key is listed", async () => { |
| 92 | 132 | const { app, session } = createForgeApp(forge, { sshUsername: "alice", remoteAddress: "127.0.0.1", user: alice }, { terminal: headlessTerminal(120, 30), exitHooks: false }); |
| 93 | 133 | app.start(); |
new above
offline
"Imported sessions are never shared by link, so there is none to end" is false. Say that deleting a session also ends its share link.