A desktop coding agent with free access to frontier models.
TypeScript
fix: fewer server round trips at chat start
ogsamrat07b24887de6c
diff --git a/apps/server/src/session-archive/index.ts b/apps/server/src/session-archive/index.tsindex 0390f7f7..e923e411 100644--- a/apps/server/src/session-archive/index.ts+++ b/apps/server/src/session-archive/index.ts@@ -1402,7 +1402,12 @@ export class SessionArchiver {*/async settleCapturePolicy(): Promise<RedactionPolicy | null> {const kept = this.policy;- if (kept && !kept.outage && this.now().getTime() - kept.at < POLICY_TTL_MS) return defaultRedactionPolicy();+ if (kept && !kept.outage && this.now().getTime() - kept.at < POLICY_TTL_MS) {+ // The cached answer's own key response goes to the next base too, so the pin and the base share it.+ const answer = this.policyAnswer;+ if (answer && answer.generation === this.generation) this.settleAnswer = { fetched: answer.fetched, at: this.now().getTime(), generation: answer.generation };+ return defaultRedactionPolicy();+ }// Not known, or known only as an outage: ask now (one probe shared with the folder policy).let probe = this.policyProbe;if (!probe) {@@ -1429,6 +1434,7 @@ export class SessionArchiver {}private settleAnswer: { fetched: KeyResult; at: number; generation: number } | null = null;+ private policyAnswer: { fetched: KeyResult; generation: number } | null = null;private async folderPolicy(generation: number): Promise<{ policy: ArchivePolicy; fetched: KeyResult | null }> {const kept = this.policy;@@ -1491,6 +1497,8 @@ export class SessionArchiver {// without git that starts during a blip is asked again at its next turn instead of going without a base.const at = this.now().getTime() - (fetched.status === "unavailable" ? Math.max(0, POLICY_TTL_MS - POLICY_OUTAGE_TTL_MS) : 0);this.policy = { value: fetched.status === "ok" ? fetched.policy : POLICY_OFF, at, ...(fetched.status === "unavailable" ? { outage: true } : {}) };+ // The key response behind a real answer, for a later settleCapturePolicy that finds the policy cached.+ this.policyAnswer = fetched.status === "unavailable" ? null : { fetched, generation };// The uploader's rules follow the server's switch; anything but a yes keeps the old rules.const rulesBefore = defaultRedactionPolicy();setRedactionPolicy(fetched.status !== "ok" ? "legacy" : fetched.policy.captureRaw === true ? CLIENT_RAW : fetched.policy.redactionV3 === true ? CLIENT_V3 : "legacy");diff --git a/apps/server/src/session-archive/settle-answer.test.ts b/apps/server/src/session-archive/settle-answer.test.tsnew file mode 100644index 00000000..9d5bfb10--- /dev/null+++ b/apps/server/src/session-archive/settle-answer.test.ts@@ -0,0 +1,53 @@+import { afterEach, describe, expect, test } from "bun:test";+import { execFile } from "node:child_process";+import { writeFile } from "node:fs/promises";+import { join } from "node:path";+import { promisify } from "node:util";++import { setRedactionPolicy } from "../context/redact-policy.js";+import { FakeArchiveServer } from "./fake-archive-server.js";+import { SessionArchiver } from "./index.js";+import { cleanupTempDirs, tempDir } from "./test-helpers.js";++const execFileAsync = promisify(execFile);+afterEach(async () => {+ setRedactionPolicy("legacy");+ await cleanupTempDirs();+});++async function gitProject(): Promise<string> {+ const root = await tempDir("project");+ const git = (...args: string[]) => execFileAsync("git", ["-C", root, "-c", "user.name=t", "-c", "[email protected]", "-c", "commit.gpgsign=false", ...args]);+ await git("init", "-q", "-b", "main");+ await writeFile(join(root, "app.ts"), "export const app = 1;\n");+ await git("add", "app.ts");+ await git("commit", "-q", "-m", "initial");+ return root;+}++function subjectFor(server: FakeArchiveServer, stateDir: string) {+ return new SessionArchiver({ gatewayUrl: server.gatewayUrl, accessToken: server.token, fetch: server.respond, stateDir, retry: { baseMs: 1, maxMs: 2, attempts: 2 } });+}++describe("the capture switch's answer and the first base use the same key response", () => {+ test("a fresh answer: the chat's pin and its base share one key read", async () => {+ const server = new FakeArchiveServer();+ server.policy = { redaction_v3: true };+ const subject = subjectFor(server, await tempDir("state"));+ expect(await subject.settleCapturePolicy()).toBe("client-v3");+ expect(await subject.captureBase("ses_settle_fresh", await gitProject())).toMatchObject({ status: "queued", kind: "base" });+ expect(server.calls.filter((call) => call.path === "archives/key")).toHaveLength(1);+ });++ test("a cached answer: the next chat's pin and base reuse the cached response, no new key read", async () => {+ const server = new FakeArchiveServer();+ server.policy = { redaction_v3: true };+ const subject = subjectFor(server, await tempDir("state"));+ expect(await subject.settleCapturePolicy()).toBe("client-v3");+ await subject.captureBase("ses_settle_first", await gitProject());+ // A second chat: the policy is cached now; its pin and its base must still share one answer.+ expect(await subject.settleCapturePolicy()).toBe("client-v3");+ expect(await subject.captureBase("ses_settle_second", await gitProject())).toMatchObject({ status: "queued", kind: "base" });+ expect(server.calls.filter((call) => call.path === "archives/key")).toHaveLength(1);+ });+});