Public: anyone can read this; the crew can change it.
The planner's forge token is refused on every write, so it opens no issues #16109
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/16106-the-planner-s-forge-token-is-refused-on → main 0 commits fork 2 threads · 2 unresolved
5 files
changed +177
−0
src/crew/runner.ts +19
| @@ -98,6 +98,7 @@ import { | ||
| 98 | 98 | type Run, |
| 99 | 99 | } from "../core/runs.ts"; |
| 100 | 100 | import { mintRunToken } from "../core/run_token.ts"; |
| 101 | import { keepToken, refusedToken } from "./token_refused.ts"; | |
| 101 | 102 | import { deleteToken } from "../core/tokens.ts"; |
| 102 | 103 | import { runGit } from "../git/env.ts"; |
| 103 | 104 | import { foreignGitDir, runCloneGit } from "./clone_git.ts"; |
| @@ -1642,6 +1643,8 @@ async function work( | ||
| 1642 | 1643 | // One deadline for the run, whatever it takes to get there: the token |
| 1643 | 1644 | // dies ten minutes after it, so a second invocation gets what is left. |
| 1644 | 1645 | const deadline = Date.now() + options.runTimeoutMs; |
| 1646 | // The token was minted before the setup above spent time: hold it to the deadline (#16106). | |
| 1647 | keepToken(app.db, token.id, deadline); | |
| 1645 | 1648 | // The agent's own model, unless the work asks for Opus (#7570). A |
| 1646 | 1649 | // planner's plain pass runs on Sonnet; one handed work the crew could |
| 1647 | 1650 | // not finish runs on Opus (`model_choice.ts`). |
| @@ -1963,6 +1966,22 @@ async function work( | ||
| 1963 | 1966 | contextTokens: result.contextTokens, |
| 1964 | 1967 | }; |
| 1965 | 1968 | |
| 1969 | // A token the forge stopped honouring mid-run refused every write: that is a failed run, | |
| 1970 | // never an ok one that opened nothing (#16106). | |
| 1971 | const refused = refusedToken(app.db, token.id, deadline); | |
| 1972 | if (refused !== undefined) { | |
| 1973 | write(refused); | |
| 1974 | return { | |
| 1975 | state: "failed", | |
| 1976 | summary: refused, | |
| 1977 | sessionId: result.sessionId, | |
| 1978 | costUsd: result.costUsd, | |
| 1979 | turns: result.turns, | |
| 1980 | contextTokens: result.contextTokens, | |
| 1981 | pushed: false, | |
| 1982 | }; | |
| 1983 | } | |
| 1984 | ||
| 1966 | 1985 | // The decline contract: an agent that should not do the task says so |
| 1967 | 1986 | // instead of guessing, and nothing it touched becomes a proposal. |
| 1968 | 1987 | const why = declineOf(result.result); |
src/crew/token_refused.ts added +41
| @@ -0,0 +1,41 @@ | ||
| 1 | /** | |
| 2 | * A crew run's token, kept to the run's deadline and judged when the session ends (#16106). | |
| 3 | * | |
| 4 | * The token is minted before the clone, the sandbox and the grant are made, and dies at | |
| 5 | * `runTimeoutMs + 10 min` after that minting: a slow setup spent the run's own time, and the | |
| 6 | * planner's calls (`POST /plan`, `GET /api/v1/user`) came back 401 while the run went on to report | |
| 7 | * ok. `keepToken` moves the expiry to the deadline once the deadline is known; `refusedToken` says, | |
| 8 | * when the session ends before its deadline, that the token no longer works (expired, revoked or | |
| 9 | * swept), so the run fails with that sentence instead of reporting ok. Plain data in, plain data | |
| 10 | * out. | |
| 11 | */ | |
| 12 | ||
| 13 | import type { Database } from "bun:sqlite"; | |
| 14 | import { renewToken, tokenLive } from "../core/tokens.ts"; | |
| 15 | ||
| 16 | /** Ten minutes past the deadline, as the minting allows, so a last push still has a credential. */ | |
| 17 | export const TOKEN_GRACE_MS = 10 * 60_000; | |
| 18 | ||
| 19 | /** Make the run's token last until `deadline` and the grace after it; never shorten it. */ | |
| 20 | export function keepToken(db: Database, tokenId: string, deadline: number): boolean { | |
This sets the expiry unconditionally, so it can shorten a longer-lived token. Only move it forward (take the later of the old and new expiry). |
||
| 21 | const row = db | |
| 22 | .query<{ expires_at: string | null }, [string]>("SELECT expires_at FROM token WHERE id = ?1") | |
| 23 | .get(tokenId); | |
| 24 | const have = row?.expires_at ? Date.parse(row.expires_at) : 0; | |
| 25 | const want = Math.max(have, deadline + TOKEN_GRACE_MS); | |
| 26 | return renewToken(db, tokenId, new Date(want).toISOString()); | |
| 27 | } | |
| 28 | ||
| 29 | /** | |
| 30 | * The sentence for a run whose token stopped working before its deadline, or undefined when it is | |
| 31 | * still good (or the deadline passed, when an expired token is the run's own end). | |
| 32 | */ | |
| 33 | export function refusedToken( | |
| 34 | db: Database, | |
| 35 | tokenId: string, | |
| 36 | deadline: number, | |
| 37 | now: number = Date.now(), | |
| 38 | ): string | undefined { | |
| 39 | if (now >= deadline || tokenLive(db, tokenId)) return undefined; | |
| 40 | return "the run's forge token was refused (401 unauthorized): expired or revoked before the run ended, so its writes did not land"; | |
| 41 | } |
test/crew-token-refused.test.ts added +73
| @@ -0,0 +1,73 @@ | ||
| 1 | /** A run whose token stopped working fails with that sentence instead of reporting ok (#16106). */ | |
| 2 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { createToken, deleteToken, verifyToken } from "../src/core/tokens.ts"; | |
| 8 | import { createUser } from "../src/core/users.ts"; | |
| 9 | import { keepToken, refusedToken, TOKEN_GRACE_MS } from "../src/crew/token_refused.ts"; | |
| 10 | ||
| 11 | describe("a crew run's token", () => { | |
| 12 | let dir: string; | |
| 13 | let app: App; | |
| 14 | let userId: string; | |
| 15 | beforeAll(async () => { | |
| 16 | dir = await mkdtemp(join(tmpdir(), "forge-token-refused-")); | |
| 17 | app = createApp({ dataDir: dir, port: 0, host: "forge.test" }); | |
| 18 | const made = await createUser(app.db, { | |
| 19 | origin: "seed", trusted: true, username: "planner", email: "[email protected]", | |
| 20 | password: "a-good-passphrase", adminIfFirst: true, | |
| 21 | }); | |
| 22 | if (!made.ok) throw new Error(made.error); | |
| 23 | userId = made.value.id; | |
| 24 | }); | |
| 25 | afterAll(async () => { | |
| 26 | app.db.close(); | |
| 27 | await rm(dir, { recursive: true, force: true }); | |
| 28 | }); | |
| 29 | ||
| 30 | test("minted to expire before the deadline, it is kept to the deadline and its grace", () => { | |
| 31 | const deadline = Date.now() + 60 * 60_000; | |
| 32 | const t = createToken(app.db, userId, "run:a", "rw", { | |
| 33 | expiresAt: new Date(Date.now() + 1000).toISOString(), | |
| 34 | }); | |
| 35 | expect(keepToken(app.db, t.id, deadline)).toBe(true); | |
| 36 | const row = app.db | |
| 37 | .query<{ expires_at: string }, [string]>("SELECT expires_at FROM token WHERE id = ?1") | |
| 38 | .get(t.id); | |
| 39 | expect(Date.parse(row!.expires_at)).toBe(deadline + TOKEN_GRACE_MS); | |
| 40 | expect(verifyToken(app.db, t.token).ok).toBe(true); | |
| 41 | }); | |
| 42 | ||
| 43 | test("a token minted to outlast the deadline is never shortened", () => { | |
| 44 | const long = Date.now() + 5 * 60 * 60_000; | |
| 45 | const t = createToken(app.db, userId, "run:e", "rw", { expiresAt: new Date(long).toISOString() }); | |
| 46 | expect(keepToken(app.db, t.id, Date.now() + 60_000)).toBe(true); | |
| 47 | const row = app.db | |
| 48 | .query<{ expires_at: string }, [string]>("SELECT expires_at FROM token WHERE id = ?1") | |
| 49 | .get(t.id); | |
| 50 | expect(Date.parse(row!.expires_at)).toBe(long); | |
| 51 | }); | |
| 52 | ||
| 53 | test("a token gone or lapsed before the deadline is named as refused", () => { | |
| 54 | const deadline = Date.now() + 60_000; | |
| 55 | const live = createToken(app.db, userId, "run:b", "rw", { | |
| 56 | expiresAt: new Date(deadline + TOKEN_GRACE_MS).toISOString(), | |
| 57 | }); | |
| 58 | expect(refusedToken(app.db, live.id, deadline)).toBeUndefined(); | |
| 59 | deleteToken(app.db, userId, live.id); | |
| 60 | expect(refusedToken(app.db, live.id, deadline)).toContain("401"); | |
| 61 | const lapsed = createToken(app.db, userId, "run:c", "rw", { | |
| 62 | expiresAt: new Date(Date.now() - 1000).toISOString(), | |
| 63 | }); | |
| 64 | expect(refusedToken(app.db, lapsed.id, deadline)).toContain("refused"); | |
| 65 | }); | |
| 66 | ||
| 67 | test("after the deadline an expired token is the run's own end, not a refusal", () => { | |
| 68 | const t = createToken(app.db, userId, "run:d", "rw", { | |
| 69 | expiresAt: new Date(Date.now() - 1000).toISOString(), | |
| 70 | }); | |
| 71 | expect(refusedToken(app.db, t.id, Date.now() - 5000)).toBeUndefined(); | |
| 72 | }); | |
| 73 | }); |
test/crew-token-revoked.test.ts added +38
| @@ -0,0 +1,38 @@ | ||
| 1 | /** | |
| 2 | * Through the runner: a run whose forge token is revoked mid-session fails with the refusal | |
| 3 | * sentence, and while the token stands the session is not judged refused (#16106). | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 6 | import { existsSync, writeFileSync } from "node:fs"; | |
| 7 | import { join } from "node:path"; | |
| 8 | import { assignIssue, openIssue } from "../src/actions/issues.ts"; | |
| 9 | import { deleteToken, tokenLive } from "../src/core/tokens.ts"; | |
| 10 | import { pollUntil } from "./helpers/poll.ts"; | |
| 11 | import { agentRunSettled, startStandInCrew, stopStandInCrew, type StandInCrew } from "./helpers/crew-standin.ts"; | |
| 12 | ||
| 13 | describe("a crew run whose token dies mid-session", () => { | |
| 14 | let standIn: StandInCrew; | |
| 15 | beforeAll(async () => { | |
| 16 | standIn = await startStandInCrew(); | |
| 17 | }, 30_000); | |
| 18 | afterAll(() => stopStandInCrew(standIn)); | |
| 19 | ||
| 20 | test("fails with the refusal sentence, never ok", async () => { | |
| 21 | const { app, alice, bot, repo, dataDir } = standIn; | |
| 22 | const issue = openIssue(app, alice, repo, { title: "Open nothing", body: "REVOKE-ME" }); | |
| 23 | if (!issue.ok) throw new Error(String(issue.error)); | |
| 24 | expect(assignIssue(app, alice, repo, issue.value, bot).ok).toBe(true); | |
| 25 | await pollUntil(() => existsSync(join(dataDir, "revoke-me")), "the session to start"); | |
| 26 | const tokens = app.db | |
| 27 | .query<{ id: string }, [string]>("SELECT id FROM token WHERE user_id = ?1") | |
| 28 | .all(bot.id); | |
| 29 | expect(tokens.length).toBeGreaterThan(0); | |
| 30 | // Setup is over and the token is still good: kept to the deadline, not lapsed. | |
| 31 | for (const t of tokens) expect(tokenLive(app.db, t.id)).toBe(true); | |
| 32 | for (const t of tokens) deleteToken(app.db, bot.id, t.id); | |
| 33 | writeFileSync(join(dataDir, "revoked"), ""); | |
| 34 | const run = await agentRunSettled(app, repo, (r) => r.issueId === issue.value.id, 60_000); | |
| 35 | expect(run.state).toBe("failed"); | |
| 36 | expect(run.summary).toContain("forge token was refused"); | |
| 37 | }, 90_000); | |
| 38 | }); |
test/helpers/crew-standin.ts +6
| @@ -210,6 +210,12 @@ touch "${dataDir}/down" | ||
| 210 | 210 | echo '{"type":"result","subtype":"error_during_execution","is_error":true,"result":"API Error: Can'"'"'t reach the API server","total_cost_usd":0,"num_turns":0,"session_id":"fake-1","terminal_reason":"api_error"}' |
| 211 | 211 | exit 0 |
| 212 | 212 | fi |
| 213 | if grep -q REVOKE-ME "${promptFile}"; then | |
| 214 | touch "${dataDir}/revoke-me" | |
| 215 | n=0; while [ ! -f "${dataDir}/revoked" ] && [ $n -lt 300 ]; do sleep 0.1; n=$((n+1)); done | |
| 216 | echo '{"type":"result","subtype":"success","is_error":false,"result":"All done.","total_cost_usd":0.01,"num_turns":1,"session_id":"fake-1","terminal_reason":"completed"}' | |
| 217 | exit 0 | |
| 218 | fi | |
| 213 | 219 | if grep -q NET-DOWN "${promptFile}"; then touch "${dataDir}/down"; fi |
| 214 | 220 | if [ -f "${dataDir}/down" ] && grep -q "Continue where you stopped\\|NET-DOWN" "${promptFile}"; then |
| 215 | 221 | echo '{"type":"result","subtype":"error_during_execution","is_error":true,"result":"API Error: Can'"'"'t reach the API server","total_cost_usd":0,"num_turns":0,"session_id":"fake-1","terminal_reason":"api_error"}' |
new above
offline
No test would fail without this branch or the
keepTokencall above. Add a runner-seam test where the token dies mid-session and the result isfailedwith this sentence.