From 57d8ade21345814257792cc8229359b34b3cdcad Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 15 Nov 2026 09:50:42 -0800 Subject: [PATCH 1/3] chore(quest): claim quest/m0/benchmark/resend-sessions.md Quest-Claim: ba2062a31b845486 From 2b57f90778b3cb52bf44464e9ab8c4e5e5bffc75 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 15 Nov 2026 10:01:57 -0800 Subject: [PATCH 2/3] fix(records): resend a session once its branch's PR opens The sender uploaded a session whose branch headed no PR yet once, with `pulls: []`, and settled its stop, so the PR that opened later never counted it. It now remembers each sent session whose tree (it and its sub-agents) has a latest branch that headed no PR, under `remembered/` beside `pending/`, and queues it again when a later pass for its group reads a PR heading one of those branches, by the rule `attribute` uses, now shared. Each upload decides afresh; a session is forgotten once its transcript is gone, and dropped silently if token-max no longer lists it on resend. Co-Authored-By: Claude Opus 5.5 --- README.md | 6 +- quest/m0/benchmark/resend-sessions.md | 79 ------------- src/cli/main.ts | 4 +- src/cli/send.ts | 155 +++++++++++++++++++++++--- src/cli/usage.ts | 79 ++++++++++--- test/send.test.ts | 154 ++++++++++++++++++++++++- 6 files changed, 364 insertions(+), 113 deletions(-) delete mode 100644 quest/m0/benchmark/resend-sessions.md diff --git a/README.md b/README.md index d940db86..4b92152e 100644 --- a/README.md +++ b/README.md @@ -1729,7 +1729,11 @@ latest upload, since one sender sends a session's uploads in order. The type is - `pulls` are the PRs it worked on, attributed as `quest usage` attributes them; a session with none yet, such as planning before its PR opens, keeps - its `branch`, and the next record names the PR. + its `branch`, and the next record names the PR. The sender remembers a sent + session while its latest branch, or a sub-agent's, heads no PR, and resends + it, with its sub-agents, once one opens: with the next session it sends from + that repository, since only then does it read the repository's PRs. It + forgets the session once the harness prunes its transcript. - `role` is classifying when a PR's `## Review` record names it as the [risk classifier](#risk-tiers), reviewing when it names it as a reviewer or Codex marks it a review, else the one the first quest skill it ran gives: planning diff --git a/quest/m0/benchmark/resend-sessions.md b/quest/m0/benchmark/resend-sessions.md deleted file mode 100644 index d90dd689..00000000 --- a/quest/m0/benchmark/resend-sessions.md +++ /dev/null @@ -1,79 +0,0 @@ -# [M] Resend a session once its branch's PR opens - -## Goal - -A session whose last stop came before its branch's PR opened reaches Kixel -Code's store with that PR in its record's `pulls`, so the PR's cost and -time count it. Today the sender uploads such a session once with -`pulls: []` and settles its queued stop, so it is never rebuilt. `quest -start` opens the PR at claim time, so this mostly hits ad hoc sessions -outside quests. - -Check: an ad hoc session on a fresh branch stops, then its PR opens; the -next session sent from the same checkout's repository resends the first, -and the store's copy names the PR, equal to what `quest usage --json` -prints for it. - -## Plan - -Found in [kixel-ai/kixel.quest#36](https://github.com/kixel-ai/kixel.quest/pull/36), -the record store; the maintainer decided there to fix it (✅ keep, -rejected: drop). The gap dates from -[moq-dev/quest#718](https://github.com/moq-dev/quest/pull/718), which -settles each queued stop after one upload: list it under `## Caused by`. - -How it works today, in `src/cli/send.ts`: each pass rebuilds only queued -sessions and their sub-agents, then settles their stops. It reads the PRs -updated since the group's cursor (`read/`), so a PR opened after an -upload is always read by the group's next pass, with no gap. The store -keeps each session's latest upload, so a resend replaces, never doubles. - -Decided 2026-11-14 (/quest-plan, unattended, so each is a recommendation -the PR's review can overturn): - -- Which sessions does the sender remember after uploading? - - ✅ Each queued session, as the root of the tree it emits, when any - session in that tree has a latest branch that headed no PR in the - read. `attribute` prefers the latest branch, so this catches a session - that moved to a new branch whose PR opens later. The whole tree is - remembered because `buildRecords` (`src/cli/records.ts`) passes PRs - from parent to sub-agent and back, so a parent on `main` takes its - PR from a sub-agent's fresh branch - - Rejected: only records with `pulls: []`, which misses the moved - session; only the session whose own branch lacks a PR, which leaves a - parent that inherits it at `pulls: []` -- When is a remembered tree resent? - - ✅ On a pass for its group (endpoint, origin, prefix) whose PR read - holds a PR that some member's unresolved latest branch heads, by the - rule `attribute` uses, shared rather than copied. A PR on an older - branch triggers nothing, since `attribute` falling back to it is - already what was sent. The sender queues the root again, so the tree - goes through the normal path, rebuilding every member's record, and - widens token-max's window only once, when it is due. Waiting for the - group's next queued session is the "next `quest` run" the record - store's PR recommended - - Rejected: polling each remembered tree's group on a timer, which - spends GitHub quota every pass for a resend that can wait for the - repository's next session -- When is a remembered tree forgotten? - - ✅ Each upload of a root decides afresh by the rule above, so a resend - that still leaves a member's latest branch without a PR keeps the - tree; it is dropped when token-max no longer lists the root (its - transcript was pruned), silently, since the first upload already sent - everything but the PR. The harness's transcript retention bounds what - is kept, with no constant of our own - - Rejected: forgetting after one resend, which loses a branch whose PR - opens later still; a fixed age; keeping it until its branch is - deleted, which costs a git or GitHub read per session -- Milestone? ✅ m0 under the benchmark, whose end-to-end check needs every - PR's cost to be whole (rejected: m1). - -Keep what is remembered in the sender's state directory beside `pending/`, -one small file per root, holding its `Pending` and, per unresolved member, -what matching needs: its latest branch with its times and the -repositories token-max resolved for it. - -## Related - -- [Record endpoint](/quest/m0/benchmark/records-endpoint.md) - turns the - sender on for each repository; this fixes what it sends diff --git a/src/cli/main.ts b/src/cli/main.ts index 5dd21bb7..6efae0c5 100644 --- a/src/cli/main.ts +++ b/src/cli/main.ts @@ -1252,7 +1252,9 @@ export function main(argv: string[], out: Output): number | Promise { `${tokensPath()} pairs with that endpoint as a bearer token, one upload per endpoint and repository, ` + `then again every ${SEND_INTERVAL / 1000} seconds until stopped, at low priority. A record is built as quest usage ` + "builds it, from token-max's sessions on this host since the earliest queued session's first instant and " + - "the PRs updated since then, and replaces its earlier copy, so a resend never doubles. An endpoint that " + + "the PRs updated since then, and replaces its earlier copy, so a resend never doubles. A sent session whose " + + "latest branch, or a sub-agent's, heads no PR yet is sent again, with its sub-agents, once a pass for its " + + "repository reads a PR opened on that branch, until its transcript is gone. An endpoint that " + "refuses the token (401 or 403) is sent nothing more until the token changes; any other failure keeps the " + "sessions queued, retried after a minute and then twice as long each time up to six hours, and the next " + "stop hook says so once. Before a record is built, a cheap model, " + diff --git a/src/cli/send.ts b/src/cli/send.ts index e6d70dd9..f970ff85 100644 --- a/src/cli/send.ts +++ b/src/cli/send.ts @@ -1,6 +1,7 @@ // Sending each session's record, and the breakages its PRs name, to the Kixel Code endpoint its checkout names. The stop hook only queues the session; // one sender per user, run by its service manager, rebuilds each queued session's record, with its sub-agents', and -// posts them, keeping what failed for the next pass. A record replaces its earlier copy, so a resend never doubles. +// posts them, keeping what failed for the next pass, and queues a sent session again once a PR opens on a branch its +// record could not name a PR for. A record replaces its earlier copy, so a resend never doubles. import { spawnSync } from "node:child_process"; import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs"; import { homedir, setPriority } from "node:os"; @@ -16,7 +17,19 @@ import { readRemote, remoteRepository } from "./github"; import { eachLine } from "./messages"; import { questRoot } from "./rules"; import { type Labeled, type SplitDependencies, type Steps, labelSession, readSteps, splitOf } from "./split"; -import { type Pull, type Session, fixBreakages, fold, key, readPull, sessionRecords, updatedPulls } from "./usage"; +import { + type Pull, + type Reached, + type Session, + fixBreakages, + fold, + heading, + key, + readPull, + sessionRecords, + unheaded, + updatedPulls, +} from "./usage"; import { type LoggedWait } from "./waits"; /** The checkout's file naming its record endpoint, as `{"records": "https://..."}`. */ @@ -184,26 +197,18 @@ export function queue(stopped: Stopped, env: NodeJS.ProcessEnv = process.env): Q `quest: ${tokensPath(env)} holds no token for ${endpoint}, so this host sends it no session records.`, ); if (existsSync(refusal(env, endpoint, token))) return { notices, queued: false }; - const dir = join(stateDirectory(env), "pending"); const start = transcriptStart(stopped.transcript); if (start === null) throw new Error(`${stopped.transcript} records no instant, so its session cannot be read`); - const queued = randomUUID(); - const pending: Pending = { + const dir = enqueue(env, { harness: stopped.harness, session: stopped.session, start, - queued, + queued: randomUUID(), root, origin: repository, prefix: run(["rev-parse", "--show-prefix"]).stdout.trim(), endpoint, - }; - mkdirSync(dir, { recursive: true, mode: 0o700 }); - // Each stop gets a file of its own, never rewritten, so a sender removes only the stops it read. Written whole, - // then renamed, so the sender never reads half a file. - const path = join(dir, `${queued}.json`); - writeFileSync(`${path}.new`, JSON.stringify(pending)); - renameSync(`${path}.new`, path); + }); const sender = senderRuns(env); if (!sender.runs) // Said once per outage: the PID of the sender that stopped names it, or none for a host that never ran one. @@ -214,6 +219,19 @@ export function queue(stopped: Stopped, env: NodeJS.ProcessEnv = process.env): Q return { notices, queued: true }; } +/** + * Write `pending` to the queue, returning the queue's directory. Each stop gets a file of its own, never rewritten, so + * a sender removes only the stops it read. Written whole, then renamed, so the sender never reads half a file. + */ +function enqueue(env: NodeJS.ProcessEnv, pending: Pending): string { + const dir = join(stateDirectory(env), "pending"); + mkdirSync(dir, { recursive: true, mode: 0o700 }); + const path = join(dir, `${pending.queued}.json`); + writeFileSync(`${path}.new`, JSON.stringify(pending)); + renameSync(`${path}.new`, path); + return dir; +} + /** The sender's last failure when the person has not seen it yet, marking it seen. */ function unseenFailure(env: NodeJS.ProcessEnv): string | null { const path = join(stateDirectory(env), "failure"); @@ -383,9 +401,11 @@ async function sendOnce(deps: SendDependencies, dir: string): Promise { else missing.push(q); } // A session token-max no longer lists, as when its transcript was deleted, can never be built: it is - // dropped and said, so it does not hold back the rest of its group forever. + // dropped and said, so it does not hold back the rest of its group forever. One remembered to name a PR + // later is dropped silently, since its first upload sent everything but that PR. for (const q of missing) { settle(q); + if (forget(deps.env, id, q.pending)) continue; notes.push( `token-max lists no ${q.pending.harness} session ${q.pending.session} on this host, so its record was dropped`, ); @@ -495,6 +515,8 @@ async function sendOnce(deps: SendDependencies, dir: string): Promise { throw new Error(`${endpoint} answered ${response.status}: ${(await response.text()).slice(0, 500)}`); clearBackoff(deps.env, id); writeCursor(deps.env, id, read); + remember(deps.env, id, { group, emit, byId, waiting: unheaded([...emit], exported, pulls) }); + resendDue(deps.env, id, pulls, group); for (const q of group) settle(q); sent.push({ endpoint, @@ -669,6 +691,111 @@ function clearBackoff(env: NodeJS.ProcessEnv, id: string): void { rmSync(backoffPath(env, id), { force: true }); } +/** + * A sent session whose tree's record could not name a PR one of its branches may yet head: the latest branch of it or + * a sub-agent sent with it headed no PR. It is queued again once one does, so that PR's cost and time count it. + */ +interface Remembered { + /** Its stop as last queued, queued again under a new ID to resend it. */ + pending: Pending; + /** Its transcript's files: once none is left, token-max lists it no more, so it is forgotten. */ + files: string[]; + /** Each latest branch in its tree that headed no PR when it was last sent. */ + waiting: Reached[]; +} + +/** Group `id`'s remembered sessions, a file each, so a pass reads only its own group's. */ +const rememberedDir = (env: NodeJS.ProcessEnv, id: string) => join(stateDirectory(env), "remembered", digest(id)); +const rememberedPath = (env: NodeJS.ProcessEnv, id: string, pending: Pending) => + join(rememberedDir(env, id), `${digest(`${pending.harness}\0${pending.session}`)}.json`); + +/** + * Decide afresh, for each session of `group` the upload sent, whether to remember it: when the latest branch of any + * session in the tree it was sent with, it and its sub-agents, headed no PR. The whole tree counts, since + * `buildRecords` passes PRs between parent and sub-agent, so a PR opening on a sub-agent's branch changes its parent's + * record too. + */ +function remember( + env: NodeJS.ProcessEnv, + id: string, + sent: { + group: { pending: Pending }[]; + emit: Set; + byId: Map; + waiting: Map; + }, +): void { + const children = new Map(); + for (const s of sent.emit) { + if (s.parent === null) continue; + const parent = `${s.harness}\0${s.parent}`; + children.set(parent, [...(children.get(parent) ?? []), s]); + } + // A session that stopped twice is queued twice; its latest stop stands for it. + const roots = new Map(sent.group.map((q) => [`${q.pending.harness}\0${q.pending.session}`, q.pending])); + for (const [name, pending] of roots) { + const root = sent.byId.get(name); + if (root === undefined || !sent.emit.has(root)) continue; + const tree = new Set([root]); + for (const s of tree) for (const child of children.get(`${s.harness}\0${s.id}`) ?? []) tree.add(child); + const waiting = [ + ...new Map( + [...tree].flatMap((s) => { + const reached = sent.waiting.get(s); + return reached ? [[JSON.stringify(reached), reached] as const] : []; + }), + ).values(), + ]; + const path = rememberedPath(env, id, pending); + if (waiting.length === 0) { + rmSync(path, { force: true }); + continue; + } + mkdirSync(dirname(path), { recursive: true, mode: 0o700 }); + writeFileSync(`${path}.new`, JSON.stringify({ pending, files: root.files, waiting } satisfies Remembered)); + renameSync(`${path}.new`, path); + } +} + +/** Forget `pending`'s session, returning whether it was remembered. */ +function forget(env: NodeJS.ProcessEnv, id: string, pending: Pending): boolean { + const path = rememberedPath(env, id, pending); + if (!existsSync(path)) return false; + rmSync(path, { force: true }); + return true; +} + +/** + * Queue again each session group `id` remembers once a PR among `pulls` heads one of its waiting branches, so the + * group's next pass rebuilds its tree, and forget each whose transcript is gone. The group's sessions in `sent` were + * just rebuilt, so they are left as `remember` decided. + */ +function resendDue(env: NodeJS.ProcessEnv, id: string, pulls: Pull[], sent: { pending: Pending }[]): void { + const dir = rememberedDir(env, id); + if (!existsSync(dir)) return; + const fresh = new Set(sent.map((q) => rememberedPath(env, id, q.pending))); + const heads = heading(pulls); + for (const name of readdirSync(dir)) { + const path = join(dir, name); + if (!name.endsWith(".json") || fresh.has(path)) continue; + const kept = readRemembered(path); + // An unreadable file loses only the resend of a record already sent once, so it never blocks the group. + if (kept === null || !kept.files.some((file) => existsSync(file))) rmSync(path, { force: true }); + else if (kept.waiting.some(heads)) enqueue(env, { ...kept.pending, queued: randomUUID() }); + } +} + +function readRemembered(path: string): Remembered | null { + try { + const value = JSON.parse(readFileSync(path, "utf8")) as Partial | null; + return typeof value?.pending === "object" && Array.isArray(value.files) && Array.isArray(value.waiting) + ? (value as Remembered) + : null; + } catch { + return null; + } +} + /** Whether a checkout still holds `dir` or the nearest of its parents that exists, as `questRoot` finds it. */ function insideCheckout(dir: string): boolean { for (let at = dir; ; at = dirname(at)) { diff --git a/src/cli/usage.ts b/src/cli/usage.ts index 09b632f3..fd3736ae 100644 --- a/src/cli/usage.ts +++ b/src/cli/usage.ts @@ -579,36 +579,62 @@ function owners(sessions: Session[]): (session: Session) => string[] | null { }; } +/** `pulls` by head branch, oldest first, as {@link headed} takes them. */ +function byBranch(pulls: Pull[]): Map { + const branches = new Map(); + for (const each of [...pulls].sort((a, b) => a.created - b.created)) + branches.set(each.branch, [...(branches.get(each.branch) ?? []), each]); + return branches; +} + +/** A branch a session reached at `first`, epoch ms, in one of `repositories`, as matching it to a PR needs. */ +export interface Reached { + name: string; + first: number; + repositories: string[]; +} + +/** + * The PR `reached` heads: the oldest still open when the session reached the branch and headed from one of its + * repositories, or null when none is, or when such PRs come from several repositories, since a checkout with remotes + * for both a fork and its upstream cannot tell which one's branch it was on. + */ +function headed(branches: Map, reached: Reached): Pull | null { + const open = (branches.get(reached.name) ?? []).filter( + (each) => + (each.closed === null || each.closed >= reached.first) && + each.repository !== null && + reached.repositories.includes(each.repository), + ); + if (new Set(open.map((each) => each.repository)).size > 1) return null; + return open[0] ?? null; +} + +/** Whether some PR among `pulls` heads a branch, by the rule that attributes a session's own PR. */ +export function heading(pulls: Pull[]): (reached: Reached) => boolean { + const branches = byBranch(pulls); + return (reached) => headed(branches, reached) !== null; +} + /** - * The PR each session's own branch heads: the one whose head branch it last named, among those still open when the - * session reached the branch and headed from a repository its host resolved for it. `evidence` holds every exported - * copy, folded ones included, since a dropped copy may be the only one naming its directory's repository. + * The PR each session's own branch heads: the one {@link headed} finds for the branch it last named that heads one. + * Another repository may use the same branch name, so a session counts only where its repository is known. `evidence` + * holds every exported copy, folded ones included, since a dropped copy may be the only one naming its directory's + * repository. */ function attribute( sessions: Session[], evidence: Session[], pulls: Pull[], ): { own: Map; known: (session: Session) => string[] | null } { - const byBranch = new Map(); - for (const each of [...pulls].sort((a, b) => a.created - b.created)) - byBranch.set(each.branch, [...(byBranch.get(each.branch) ?? []), each]); + const branches = byBranch(pulls); const own = new Map(); const owned = owners(evidence); for (const session of sessions) { - // Another repository may use the same branch name, so a session counts only where its repository is known. const known = owned(session); if (known === null) continue; for (const seen of [...session.branches].sort((a, b) => b.last - a.last)) { - // A branch can head a new PR after an earlier one closed: take the one open when the session reached it. - const open = (byBranch.get(seen.name) ?? []).filter( - (each) => - (each.closed === null || each.closed >= seen.first) && - each.repository !== null && - known.includes(each.repository), - ); - // A checkout with remotes for both a fork and its upstream cannot tell which one's branch it was on. - if (new Set(open.map((each) => each.repository)).size > 1) continue; - const found = open[0]; + const found = headed(branches, { name: seen.name, first: seen.first, repositories: known }); if (found) { own.set(session, found); break; @@ -618,6 +644,25 @@ function attribute( return { own, known: owned }; } +/** + * Each of `sessions` whose latest branch heads no PR among `pulls`, by the rule {@link attribute} uses, with that + * branch, so a PR that opens on it later can be noticed. A session whose repository is unknown, or that named no + * branch, has none to wait for. + */ +export function unheaded(sessions: Session[], evidence: Session[], pulls: Pull[]): Map { + const branches = byBranch(pulls); + const owned = owners(evidence); + const waiting = new Map(); + for (const session of sessions) { + const repositories = owned(session); + const latest = [...session.branches].sort((a, b) => b.last - a.last)[0]; + if (repositories === null || latest === undefined) continue; + const reached = { name: latest.name, first: latest.first, repositories }; + if (headed(branches, reached) === null) waiting.set(session, reached); + } + return waiting; +} + /** What `sessionRecords` builds from. */ export interface RecordSources { /** The base repository as lowercase `owner/repo`. */ diff --git a/test/send.test.ts b/test/send.test.ts index 67dc911b..62cdb252 100644 --- a/test/send.test.ts +++ b/test/send.test.ts @@ -1,5 +1,5 @@ import { execFileSync } from "node:child_process"; -import { mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs"; +import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { expect, test } from "vitest"; @@ -687,3 +687,155 @@ test("an incident's --at must be a date and time with a Z or offset, on a real c ]) expect(parseInstant(text), text).toBeNull(); }); + +/** A PR of acme/proj from `branch`, opened and last updated at `at`. */ +const opened = (number: number, branch: string, at: string) => ({ + number, + html_url: `https://github.com/acme/proj/pull/${number}`, + title: "B", + head: { ref: branch, repo: { full_name: "acme/proj" } }, + state: "open", + draft: true, + merged_at: null, + created_at: at, + updated_at: at, + closed_at: null, + body: "", +}); + +const remembered = (env: NodeJS.ProcessEnv) => { + const dir = join(env.XDG_STATE_HOME!, "quest", "records", "remembered"); + if (!existsSync(dir)) return []; + return readdirSync(dir) + .flatMap((group) => readdirSync(join(dir, group)).map((name) => join(dir, group, name))) + .map((path) => (JSON.parse(readFileSync(path, "utf8")) as { pending: { session: string } }).pending.session) + .sort(); +}; + +/** A later session on main in `root`, queued, which only brings its group's next pass. */ +function later(env: NodeJS.ProcessEnv, root: string, id: string): Session { + writeFileSync(join(root, `${id}.jsonl`), `{"type":"user","timestamp":"2026-11-06T13:00:00Z"}\n`); + queue({ ...stopped(root), session: id, transcript: join(root, `${id}.jsonl`) }, env); + const at = Date.parse("2026-11-06T13:00:00Z"); + return session(id, "main", { + start: at, + end: at + 60_000, + branches: [{ name: "main", first: at, last: at + 60_000 }], + files: [join(root, `${id}.jsonl`)], + }); +} + +test("a session sent before its branch's PR opened is resent with the PR on its group's next pass", async () => { + const env = home(); + const root = checkout(ENDPOINT); + queue(stopped(root), env); + const first = session("s1", "fix/x", { files: [transcript()] }); + const before = sender(env, 200, [first]); + await sendPass(before.deps); + expect(parseUpload(before.uploads[0]!.body).records.map((r) => [r.session, r.pulls])).toEqual([["s1", []]]); + expect(remembered(env)).toEqual(["s1"]); + // The PR opens; nothing reads it until the repository's next session is sent, which queues s1 again. + const s2 = later(env, root, "s2"); + const pr = opened(9, "fix/x", "2026-11-06T13:30:00Z"); + const next = sender(env, 200, [first, s2], () => {}, [pr]); + next.clock.now = Date.parse("2026-11-06T14:00:00Z"); + await sendPass(next.deps); + expect(parseUpload(next.uploads[0]!.body).records.map((r) => r.session)).toEqual(["s2"]); + expect(pending(env)).toHaveLength(1); + // Its resend widens token-max's window back to its start, once, and names the PR; nothing waits after it. + const resend = sender(env, 200, [first, s2], () => {}, [pr]); + await sendPass(resend.deps); + expect(resend.windows).toEqual([new Date("2026-11-06T10:00:00Z")]); + expect(parseUpload(resend.uploads[0]!.body).records.map((r) => [r.session, r.pulls, r.role])).toEqual([ + ["s1", [9], "implementing"], + ]); + expect(pending(env)).toEqual([]); + // s2 stays remembered, as main heads no PR either. + expect(remembered(env)).toEqual(["s2"]); +}); + +test("a moved session and a sub-agent's fresh branch wait for their own PRs; a PR on an older branch triggers nothing", async () => { + const env = home(); + const root = checkout(ENDPOINT); + queue(stopped(root), env); + const at = (time: string) => Date.parse(`2026-11-06T${time}:00Z`); + // s1 moved from quest/m0/a, whose PR 7 is open, to fix/y; its sub-agent works on fix/sub. + const s1 = session("s1", "quest/m0/a", { + files: [transcript()], + branches: [ + { name: "quest/m0/a", first: at("10:00"), last: at("10:10") }, + { name: "fix/y", first: at("10:20"), last: at("10:30") }, + ], + }); + const sub = session("sub", "fix/sub", { parent: "s1" }); + const first = sender(env, 200, [s1, sub]); + await sendPass(first.deps); + expect(parseUpload(first.uploads[0]!.body).records.map((r) => [r.session, r.pulls])).toEqual([ + ["s1", [7]], + ["sub", [7]], + ]); + const pass = async (id: string, now: string, pulls: object[]) => { + const s = later(env, root, id); + const each = sender(env, 200, [s1, sub, s], () => {}, pulls); + each.clock.now = Date.parse(`2026-11-06T${now}:00Z`); + await sendPass(each.deps); + const resend = sender(env, 200, [s1, sub, s], () => {}, pulls); + await sendPass(resend.deps); + return resend.uploads.map((u) => parseUpload(u.body).records.map((r) => [r.session, r.pulls])); + }; + // Newest update first, as GitHub lists them. + const pulls = [ + opened(12, "fix/y", "2026-11-06T15:30:00Z"), + opened(11, "fix/sub", "2026-11-06T14:30:00Z"), + opened(10, "quest/m0/a", "2026-11-06T13:30:00Z"), + ]; + // Another PR on quest/m0/a changes nothing attribution would send. + expect(await pass("s2", "14:00", pulls.slice(2))).toEqual([]); + // A PR on the sub-agent's branch resends the tree; s1 still waits for fix/y. + expect(await pass("s3", "15:00", pulls.slice(1))).toEqual([ + [ + ["s1", [7]], + ["sub", [11]], + ], + ]); + expect(remembered(env)).toEqual(["s1", "s2", "s3"]); + expect(await pass("s4", "16:00", pulls)).toEqual([ + [ + ["s1", [12]], + ["sub", [11]], + ], + ]); + expect(remembered(env)).toEqual(["s2", "s3", "s4"]); +}); + +test("a remembered session is forgotten once its transcript is gone, unreadable, or unlisted, without a notice", async () => { + const env = home(); + const root = checkout(ENDPOINT); + const files = [transcript(), transcript(), transcript()]; + for (const id of ["gone", "garbled", "unlisted"]) queue({ ...stopped(root), session: id }, env); + const sessions = ["gone", "garbled", "unlisted"].map((id, i) => session(id, `fix/${id}`, { files: [files[i]!] })); + await sendPass(sender(env, 200, sessions).deps); + expect(remembered(env)).toEqual(["garbled", "gone", "unlisted"]); + rmSync(files[0]!); + const dir = join(env.XDG_STATE_HOME!, "quest", "records", "remembered"); + const [group] = readdirSync(dir); + for (const name of readdirSync(join(dir, group!))) { + const path = join(dir, group!, name); + if (readFileSync(path, "utf8").includes('"garbled"')) writeFileSync(path, '{"pending":'); + } + // Every branch's PR opens; only the session whose transcript is still there is queued again. + const pulls = ["gone", "garbled", "unlisted"].map((id, i) => opened(20 + i, `fix/${id}`, "2026-11-06T13:30:00Z")); + const s2 = later(env, root, "s2"); + const next = sender(env, 200, [...sessions, s2], () => {}, pulls); + next.clock.now = Date.parse("2026-11-06T14:00:00Z"); + await sendPass(next.deps); + expect(pending(env)).toHaveLength(1); + expect(remembered(env)).toEqual(["s2", "unlisted"]); + // token-max no longer lists it, as once its transcript is pruned: it is dropped without a notice. + const resend = sender(env, 200, [s2], () => {}, pulls); + await sendPass(resend.deps); + expect(resend.uploads).toEqual([]); + expect(pending(env)).toEqual([]); + expect(remembered(env)).toEqual(["s2"]); + expect(queue(stopped(root), env).notices).toEqual([]); +}); From 6437cb82934484651a3c2606e0481d18fdd9c448 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 00:30:03 -0800 Subject: [PATCH 3/3] fix(records): keep a resend's PRs through a failed pass and its earlier read Write the group's cursor only after remember and resendDue, so a pass that fails before queueing a resend reads its PR again. A resend reads PRs from its earlier upload's read, if earlier, so it names every PR that upload did. readRemembered rejects malformed waiting entries. Co-Authored-By: Claude Opus 5.5 --- src/cli/send.ts | 48 +++++++++++++++++++++------ test/send.test.ts | 84 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 120 insertions(+), 12 deletions(-) diff --git a/src/cli/send.ts b/src/cli/send.ts index f970ff85..48da98b8 100644 --- a/src/cli/send.ts +++ b/src/cli/send.ts @@ -48,6 +48,11 @@ export interface Pending { origin: string; prefix: string; endpoint: string; + /** + * On a resend, the instant, epoch ms, its earlier upload read PRs from, so the resend reads at least as far back and + * names every PR that upload did. + */ + read?: number; } /** Where the sender keeps its queue and what it has said. */ @@ -386,13 +391,15 @@ async function sendOnce(deps: SendDependencies, dir: string): Promise { const [owner, repo] = origin.split("/") as [string, string]; const repository = await baseRepository(client, { owner, repo }, prefix); // PRs are read from the last upload's read, if earlier, so a fix that merged after its sessions were sent, - // as through the merge queue, still reaches the store with the next session. + // as through the merge queue, still reaches the store with the next session. A resend reads from its earlier + // upload's read too, if earlier, so it drops no PR that upload named. const read = deps.now() - CLOCK_SKEW; - const pulls: Pull[] = await updatedPulls( - client, - repository, - Math.min(since, readCursor(deps.env, id) ?? since), + const from = Math.min( + since, + readCursor(deps.env, id) ?? since, + ...group.map((q) => q.pending.read ?? Number.POSITIVE_INFINITY), ); + const pulls: Pull[] = await updatedPulls(client, repository, from); const emit = new Set(); const missing: typeof group = []; for (const q of group) { @@ -514,9 +521,10 @@ async function sendOnce(deps: SendDependencies, dir: string): Promise { if (!response.ok) throw new Error(`${endpoint} answered ${response.status}: ${(await response.text()).slice(0, 500)}`); clearBackoff(deps.env, id); - writeCursor(deps.env, id, read); - remember(deps.env, id, { group, emit, byId, waiting: unheaded([...emit], exported, pulls) }); + remember(deps.env, id, { group, emit, byId, from, waiting: unheaded([...emit], exported, pulls) }); resendDue(deps.env, id, pulls, group); + // Only once every resend these PRs call for is queued, so a pass that fails before then reads them again. + writeCursor(deps.env, id, read); for (const q of group) settle(q); sent.push({ endpoint, @@ -696,7 +704,7 @@ function clearBackoff(env: NodeJS.ProcessEnv, id: string): void { * a sub-agent sent with it headed no PR. It is queued again once one does, so that PR's cost and time count it. */ interface Remembered { - /** Its stop as last queued, queued again under a new ID to resend it. */ + /** Its stop as last queued, with where that upload read PRs from, queued again under a new ID to resend it. */ pending: Pending; /** Its transcript's files: once none is left, token-max lists it no more, so it is forgotten. */ files: string[]; @@ -722,6 +730,8 @@ function remember( group: { pending: Pending }[]; emit: Set; byId: Map; + /** Where the upload's PR read began. */ + from: number; waiting: Map; }, ): void { @@ -752,7 +762,14 @@ function remember( continue; } mkdirSync(dirname(path), { recursive: true, mode: 0o700 }); - writeFileSync(`${path}.new`, JSON.stringify({ pending, files: root.files, waiting } satisfies Remembered)); + writeFileSync( + `${path}.new`, + JSON.stringify({ + pending: { ...pending, read: sent.from }, + files: root.files, + waiting, + } satisfies Remembered), + ); renameSync(`${path}.new`, path); } } @@ -788,7 +805,18 @@ function resendDue(env: NodeJS.ProcessEnv, id: string, pulls: Pull[], sent: { pe function readRemembered(path: string): Remembered | null { try { const value = JSON.parse(readFileSync(path, "utf8")) as Partial | null; - return typeof value?.pending === "object" && Array.isArray(value.files) && Array.isArray(value.waiting) + return typeof value?.pending === "object" && + value.pending !== null && + Array.isArray(value.files) && + value.files.every((file) => typeof file === "string") && + Array.isArray(value.waiting) && + value.waiting.every( + (each) => + typeof each?.name === "string" && + typeof each.first === "number" && + Array.isArray(each.repositories) && + each.repositories.every((r) => typeof r === "string"), + ) ? (value as Remembered) : null; } catch { diff --git a/test/send.test.ts b/test/send.test.ts index 62cdb252..ea436482 100644 --- a/test/send.test.ts +++ b/test/send.test.ts @@ -1,5 +1,5 @@ import { execFileSync } from "node:child_process"; -import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { expect, test } from "vitest"; @@ -754,6 +754,84 @@ test("a session sent before its branch's PR opened is resent with the PR on its expect(remembered(env)).toEqual(["s2"]); }); +test("a pass that fails before its resends are queued reads their PRs again, even ones older than its sessions", async () => { + const env = home(); + const root = checkout(ENDPOINT); + queue(stopped(root), env); + const first = session("s1", "fix/x", { files: [transcript()] }); + await sendPass(sender(env, 200, [first]).deps); + expect(remembered(env)).toEqual(["s1"]); + // The PR opens at 12:30, before s2 starts, so only the cursor's read still reaches it. + const s2 = later(env, root, "s2"); + const pr = opened(9, "fix/x", "2026-11-06T12:30:00Z"); + const queueDir = join(env.XDG_STATE_HOME!, "quest", "records", "pending"); + // The upload succeeds, then queueing s1's resend fails: the queue's directory is a file for the rest of the pass. + const failing = sender( + env, + 200, + [first, s2], + () => { + renameSync(queueDir, `${queueDir}.away`); + writeFileSync(queueDir, ""); + }, + [pr], + ); + failing.clock.now = Date.parse("2026-11-06T14:00:00Z"); + await expect(sendPass(failing.deps)).rejects.toThrow("EEXIST"); + rmSync(queueDir); + renameSync(`${queueDir}.away`, queueDir); + expect(pending(env)).toHaveLength(1); + const retry = sender(env, 200, [first, s2], () => {}, [pr]); + retry.clock.now += 120_000; + await sendPass(retry.deps); + expect(pending(env)).toHaveLength(1); + const resend = sender(env, 200, [first, s2], () => {}, [pr]); + await sendPass(resend.deps); + expect(parseUpload(resend.uploads[0]!.body).records.map((r) => [r.session, r.pulls])).toEqual([["s1", [9]]]); +}); + +test("a resend names every PR its earlier upload did, even one its own window misses", async () => { + const env = home(); + const root = checkout(ENDPOINT); + const at = (time: string) => Date.parse(`2026-11-06T${time}:00Z`); + // s0 starts at 10:00, so the first upload reads PR 7, last updated at 11:00; s1 starts after that update. + queue({ ...stopped(root), session: "s0" }, env); + writeFileSync(join(root, "s1.jsonl"), `{"type":"user","timestamp":"2026-11-06T11:30:00Z"}\n`); + queue({ ...stopped(root), transcript: join(root, "s1.jsonl") }, env); + const s0 = session("s0", "main", { files: [transcript()] }); + const late = { start: at("11:30"), end: at("11:50") }; + const s1 = session("s1", "quest/m0/a", { + ...late, + files: [join(root, "s1.jsonl")], + branches: [{ name: "quest/m0/a", first: at("11:30"), last: at("11:40") }], + }); + const sub = session("sub", "fix/sub", { + ...late, + parent: "s1", + branches: [{ name: "fix/sub", first: at("11:30"), last: at("11:40") }], + }); + const first = sender(env, 200, [s0, s1, sub]); + await sendPass(first.deps); + expect(parseUpload(first.uploads[0]!.body).records.map((r) => [r.session, r.pulls])).toEqual([ + ["s0", []], + ["s1", [7]], + ["sub", [7]], + ]); + // The sub-agent's PR opens, so the tree is resent, alone in its group's pass. + const s2 = later(env, root, "s2"); + const pr = opened(11, "fix/sub", "2026-11-06T13:30:00Z"); + const next = sender(env, 200, [s0, s1, sub, s2], () => {}, [pr]); + next.clock.now = at("14:00"); + await sendPass(next.deps); + const resend = sender(env, 200, [s0, s1, sub, s2], () => {}, [pr]); + await sendPass(resend.deps); + expect(resend.windows).toEqual([new Date(late.start)]); + expect(parseUpload(resend.uploads[0]!.body).records.map((r) => [r.session, r.pulls])).toEqual([ + ["s1", [7]], + ["sub", [11]], + ]); +}); + test("a moved session and a sub-agent's fresh branch wait for their own PRs; a PR on an older branch triggers nothing", async () => { const env = home(); const root = checkout(ENDPOINT); @@ -821,7 +899,9 @@ test("a remembered session is forgotten once its transcript is gone, unreadable, const [group] = readdirSync(dir); for (const name of readdirSync(join(dir, group!))) { const path = join(dir, group!, name); - if (readFileSync(path, "utf8").includes('"garbled"')) writeFileSync(path, '{"pending":'); + // Its JSON parses, but a waiting entry is null, which would throw on every pass if read as a branch. + const kept = readFileSync(path, "utf8"); + if (kept.includes('"garbled"')) writeFileSync(path, JSON.stringify({ ...JSON.parse(kept), waiting: [null] })); } // Every branch's PR opens; only the session whose transcript is still there is queued again. const pulls = ["gone", "garbled", "unlisted"].map((id, i) => opened(20 + i, `fix/${id}`, "2026-11-06T13:30:00Z"));