diff --git a/src/loop/adapter-openai.ts b/src/loop/adapter-openai.ts index 282910787b..29f8654e44 100644 --- a/src/loop/adapter-openai.ts +++ b/src/loop/adapter-openai.ts @@ -163,11 +163,14 @@ function stripFinishReasonChunk(buf: Buffer): Buffer { // standard field is prompt_tokens_details.cached_tokens, while DeepSeek reports // KV-cache hits as top-level prompt_cache_hit_tokens (#779). Both mean "input // tokens served from cache", so normalize to one number. -function openaiCachedTokens(u: Record): number | undefined { +function openaiCachedTokens(u: Record): number | null { const pd = u.prompt_tokens_details as Record | undefined; + if (typeof u.prompt_tokens === "number" + && typeof pd?.cached_tokens !== "number" + && typeof u.prompt_cache_hit_tokens !== "number") return 0; if (typeof pd?.cached_tokens === "number") return pd.cached_tokens; if (typeof u.prompt_cache_hit_tokens === "number") return u.prompt_cache_hit_tokens; - return undefined; + return null; } export function createOpenaiAdapter(requestBody: Record, clientSystem?: string, absorbName?: string, notes?: string[], errorShape: "protocol" | "completion" = "protocol"): CompressLoopAdapter { diff --git a/src/storage/store.ts b/src/storage/store.ts index 4e42316ec8..24bc12cdbf 100644 --- a/src/storage/store.ts +++ b/src/storage/store.ts @@ -1044,10 +1044,10 @@ export class UnifiedStore { next_run_id = excluded.next_run_id, meta_json = excluded.meta_json, stats_json = excluded.stats_json, - metadata_json = excluded.metadata_json, - kstate_json = excluded.kstate_json, + metadata_json = CASE WHEN metadata_json IS excluded.metadata_json THEN sessions.metadata_json ELSE excluded.metadata_json END, + kstate_json = CASE WHEN kstate_json IS excluded.kstate_json THEN sessions.kstate_json ELSE excluded.kstate_json END, tail_json = excluded.tail_json, - ccr_json = excluded.ccr_json, + ccr_json = CASE WHEN ccr_json IS excluded.ccr_json THEN sessions.ccr_json ELSE excluded.ccr_json END, last_ingested_seq = excluded.last_ingested_seq, orphans_json = excluded.orphans_json`, ) diff --git a/src/update.ts b/src/update.ts index 2886deeb12..e3c493bcff 100644 --- a/src/update.ts +++ b/src/update.ts @@ -2099,21 +2099,47 @@ export async function installViaTarball( } }; - // Copy staging over install dir using Node's built-in fs.cp (Node 16.7+). - // Cross-platform — no dependency on the `cp` binary (absent on Windows). - // `recursive: true` + the trailing `/.` semantics: fs.cp copies the - // *contents* of stagingDir into installDir, merging without nesting. - let copyError: string | null = null; + // #6309: prefer an atomic directory swap. Copy staging next to the + // install dir on the same volume, then rename the current install aside + // and rename the prepared copy into place. The old copy is the rollback + // source and is only removed after verification. If rename is not + // possible (common on Windows while the running install holds handles), + // fall back to the previous merge-copy path. + const swapCandidate = `${installDir}.update-new-${Date.now()}`; + let copiedForSwap = false; try { - await cp(stagingDir, installDir, { recursive: true, force: true }); - } catch (e) { - copyError = `failed to copy to install dir: ${String(e)}`; - } finally { + await cp(stagingDir, swapCandidate, { recursive: true, force: true }); + copiedForSwap = true; + await rename(installDir, backupDir); + try { + await rename(swapCandidate, installDir); + } catch (e) { + const swapError = String(e); + await rename(backupDir, installDir); + throw new Error(swapError); + } await rm(stagingDir, { recursive: true, force: true }); - } - if (copyError !== null) { - const rb = await restoreFromBackup(); - return { ok: false, error: rb ?? copyError }; + } catch { + if (copiedForSwap) await rm(swapCandidate, { recursive: true, force: true }); + if (!pnpmOldLink) { + try { + await cp(installDir, backupDir, { recursive: true, force: true }); + } catch (e) { + return { ok: false, error: `backup of current install failed (install left untouched): ${String(e)}` }; + } + } + let copyError: string | null = null; + try { + await cp(stagingDir, installDir, { recursive: true, force: true }); + } catch (e) { + copyError = `failed to copy to install dir: ${String(e)}`; + } finally { + await rm(stagingDir, { recursive: true, force: true }); + } + if (copyError !== null) { + const rb = await restoreFromBackup(); + return { ok: false, error: rb ?? copyError }; + } } // Final verification: version must match and every declared entry must @@ -2151,7 +2177,7 @@ export async function installViaTarball( // Success: the backup is no longer needed, and neither is the displaced // pnpm link (the store copy underneath it was left byte-identical). - await rm(backupDir, { recursive: true, force: true }); + if (!pnpmOldLink) await rm(backupDir, { recursive: true, force: true }); if (pnpmOldLink) { try { await unlink(pnpmOldLink); diff --git a/tests/golden/storage-render/chat.json b/tests/golden/storage-render/chat.json index e0f1b4aa96..ae4923dd78 100644 --- a/tests/golden/storage-render/chat.json +++ b/tests/golden/storage-render/chat.json @@ -95,13 +95,13 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "97b9991ae7a398fea5d620994df0101001d9c3f246615daac95e6afb4cee14a0", - "stateDigestAligned": "fc632ea30ecb84eeb10e2e0bb4d525f06b17ebd60c6296625d71f398b569ed0b", + "stateDigest": "a7b5a67f54a2cbd7978e2a57ad78f522f7af8f94a4e2402898aee4d93479541a", + "stateDigestAligned": "76f701f5ab1631fe1615a3f1cd8fa0fcd0a3fa19c02658cc5d077ae565f4897a", "sectionDigests": { "state": "90857fbf82001a2b", "blockContents": "94a58f96d7c5cfc3", - "stats": "3e6858671f4b9a9d", - "metadata": "faa2b34b71823646" + "stats": "5c2f0fe203d710e8", + "metadata": "0c7d1819e0d4c0f6" }, "stateKeyDigests": { "blocks": "d9770feee91dafc6", diff --git a/tests/issue3250-wal-checkpoint.test.ts b/tests/issue3250-wal-checkpoint.test.ts index e989760ae6..d5affc667d 100644 --- a/tests/issue3250-wal-checkpoint.test.ts +++ b/tests/issue3250-wal-checkpoint.test.ts @@ -334,3 +334,12 @@ test("#3250: a configured cap flows open() → journal_size_limit (custom limit rmBestEffort(dir); } }); + +test("#6323: auto_vacuum precedes WAL so fresh databases honor INCREMENTAL", () => { + const { db } = openSqliteDatabase(":memory:"); + for (const pragma of STORAGE_PRAGMAS) db.exec(pragma); + migrateSchema(db); + const row = db.prepare("PRAGMA auto_vacuum").get() as Record; + assert.equal(Object.values(row)[0], 2, "auto_vacuum should be INCREMENTAL (2)"); + db.close(); +}); diff --git a/tests/loop-core.test.ts b/tests/loop-core.test.ts index b0012ca38e..e420605678 100644 --- a/tests/loop-core.test.ts +++ b/tests/loop-core.test.ts @@ -4,6 +4,7 @@ import type { Config, CoreMessage } from "acp-kernel"; import { createCore, createInitialState, assignRefs, emptyRefMap, defaultConfig } from "acp-kernel"; import type { Session } from "../src/session.ts"; import { runCompressLoop, createResponsesAdapter, createOpenaiAdapter, MAX_LOOP_ROUNDS } from "../src/loop/index.ts"; +import { buildSessionCacheReport } from "../src/cache-ledger.ts"; import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; import { createHandoffReply } from "../src/server/handoff-response.ts"; import type { WireProtocol } from "../src/util.ts"; @@ -410,6 +411,28 @@ for (const completion of ["text", "tool"] as const) { } } +test("#6329: OpenAI cache-field omission means zero cached tokens, not unmeasured", async () => { + const ctx = makeCtx([], "openai"); + const body = { model: "m", messages: [], stream: true, stream_options: { include_usage: true } }; + const raw = [ + `data: ${JSON.stringify({ choices: [{ index: 0, delta: { content: "hello" } }] })}\n\n`, + `data: ${JSON.stringify({ choices: [{ index: 0, delta: {}, finish_reason: "stop" }], usage: { prompt_tokens: 1000, completion_tokens: 10, total_tokens: 1010 } })}\n\n`, + "data: [DONE]\n\n", + ].join(""); + for await (const _chunk of runCompressLoop( + new Response(raw).body!, ctx, body, { url: "http://mock", headers: {} }, + createOpenaiAdapter(body), buildCompressSystemPrompt(), + )) {} + assert.equal(ctx.session.stats.inputTokens, 1000); + assert.equal(ctx.session.stats.cachedTokens, 0); + assert.equal(ctx.session.stats.cacheSamples, 1); + const report = buildSessionCacheReport(ctx.session); + assert.equal(report.unmeasured.samples, 0); + assert.equal(report.unmeasured.inputTokens, 0); + assert.equal(report.totals.input, 1000); + assert.equal(report.totals.cached, 0); +}); + test("loop usage: protocol unset → legacy additive behavior (prompt + cached)", async () => { const ctx = makeCtx(); await drain( diff --git a/tests/storage-archive-gc.test.ts b/tests/storage-archive-gc.test.ts index 4180076f30..d60fa6d7c3 100644 --- a/tests/storage-archive-gc.test.ts +++ b/tests/storage-archive-gc.test.ts @@ -5,6 +5,7 @@ import * as os from "node:os"; import * as path from "node:path"; import { planIngest, type LegacySessionLike } from "../src/storage/ingest.ts"; import { UnifiedStore } from "../src/storage/store.ts"; +import { openSqliteDatabase } from "../src/storage/driver.ts"; import { resolveArchiveAfterDays } from "../src/knobs.ts"; /** @@ -170,3 +171,43 @@ test("gc(): age sweep archives only past-retention sessions; dormant shared rows try { fs.rmSync(dir, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }); } catch { /* Windows stray handles */ } } }); + +test("#6317: failed archive sweep leaves no archive line; restore uses the newest entry", () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "storage-archive-failed-")); + try { + const store = UnifiedStore.open(path.join(dir, "index.db")); + store.ingestSession(planIngest(mkSession("sess-A", [mkMsg("m-a", "old body ".repeat(50))]))); + const archiveDir = path.join(dir, "archive"); + const logPath = path.join(archiveDir, UnifiedStore.ARCHIVE_LOG_NAME); + fs.mkdirSync(archiveDir, { recursive: true }); + + const second = openSqliteDatabase(path.join(dir, "index.db")).db; + second.exec("BEGIN IMMEDIATE"); + try { + assert.throws( + () => store.archiveSession("sess-A", { archiveDir }), + /database is locked/, + "archive fails while another writer holds the lock", + ); + } finally { + second.exec("ROLLBACK"); + second.close(); + } + assert.ok(!fs.existsSync(logPath), "failed archive must not append a stale line"); + + store.ingestSession(planIngest(mkSession("sess-A", [mkMsg("m-a", "old body ".repeat(50)), mkMsg("m-a2", "new body ".repeat(50))]))); + const archived = store.archiveSession("sess-A", { archiveDir }); + assert.ok(archived.ok); + const lines = fs.readFileSync(logPath, "utf8").split("\n").filter((l) => l.trim()); + assert.equal(lines.length, 1, "only the successful archive writes a line"); + const dump = JSON.parse(lines[0]) as { messages?: { msg_id?: string }[] }; + assert.deepEqual(dump.messages?.map((m) => m.msg_id), ["m-a", "m-a2"]); + + const restored = store.restoreArchived("sess-A", { archiveDir }); + assert.ok(restored.ok); + assert.deepEqual(store.getMessages("sess-A").map((m) => m.msgId), ["m-a", "m-a2"], "restore returns the newest dump"); + store.close(); + } finally { + try { fs.rmSync(dir, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }); } catch { /* Windows stray handles */ } + } +}); diff --git a/tests/storage-unified.test.ts b/tests/storage-unified.test.ts index 15d2d1a04c..8e4908ba87 100644 --- a/tests/storage-unified.test.ts +++ b/tests/storage-unified.test.ts @@ -960,3 +960,36 @@ test("#4313: fork clone copies ts verbatim (inherited ledger rows keep their sta fs.rmSync(dir, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }); } }); + +test("#6325: unchanged large session columns are not rewritten on delta ingest", () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "storage-6325-")); + try { + const store = UnifiedStore.open(path.join(dir, "index.db")); + const now = Date.now(); + const makeSession = (id: string, text: string): LegacySessionLike => ({ + id, + createdAt: now, + lastSeen: now, + meta: { protocol: "openai" }, + stats: {}, + metadata: { cacheLedger: { keep: "x".repeat(4096) } }, + state: { blocks: [], nextBlockId: 1, nextRunId: 1, messageRefs: { byRaw: {}, byRef: {} } }, + blockContents: new Map(), + lastMessages: [{ id: "m1", role: "user", contentType: "text", text } as unknown as CoreMessage], + lastMessagesFolded: false, + pluginSnapshot: undefined, + contentStore: undefined, + } as unknown as LegacySessionLike); + const session = makeSession("sess-6325", "hello"); + const before = store.ingestSession(planIngest(session)); + assert.ok(before); + const first = store.getSessionRow("sess-6325"); + assert.ok(first?.metadataJson?.includes("cacheLedger")); + store.ingestSession(planIngest({ ...session, lastMessages: [{ id: "m2", role: "assistant", contentType: "text", text: "world" } as unknown as CoreMessage] })); + const second = store.getSessionRow("sess-6325"); + assert.equal(second?.metadataJson, first?.metadataJson, "unchanged metadata_json should reuse the stored value"); + store.close(); + } finally { + try { fs.rmSync(dir, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }); } catch { /* Windows stray handles */ } + } +});