Skip to content

Commit 6a6c182

Browse files
TymekTMclaude
andcommitted
perf(server): retry a failed OpenCode usage cache write on the next scan
Remember cache files whose last write failed, so the rows are saved on the next scan instead of waiting for a row to change. A test fails one write and checks the retry. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 8ed7c81 commit 6a6c182

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

‎packages/provider-opencode/src/server/usage.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,65 @@ describe("openCodeUsageReader", () => {
405405
}
406406
}).pipe(Effect.provide(NodeServices.layer)),
407407
);
408+
409+
it.effect("retries a failed cache write on the next unchanged scan", () => {
410+
let failures = 1;
411+
const flakyFileSystem = Layer.effect(
412+
FileSystem.FileSystem,
413+
Effect.map(FileSystem.FileSystem, (inner) => ({
414+
...inner,
415+
// Writing under a missing directory fails with a real PlatformError.
416+
writeFileString: (target: string, contents: string) =>
417+
failures-- > 0
418+
? inner.writeFileString(`${target}/missing/file`, contents)
419+
: inner.writeFileString(target, contents),
420+
})),
421+
).pipe(Layer.provide(NodeServices.layer));
422+
return Effect.gen(function* () {
423+
const fileSystem = yield* FileSystem.FileSystem;
424+
const path = yield* Path.Path;
425+
const dir = yield* fileSystem.makeTempDirectoryScoped({
426+
prefix: "usage-reader-retry-test-",
427+
});
428+
const db = yield* openDatabase(path.join(dir, "opencode.db"));
429+
yield* db.unsafe(
430+
"CREATE TABLE message (id TEXT, session_id TEXT, time_created INTEGER, time_updated INTEGER, data TEXT)",
431+
);
432+
yield* db.unsafe("INSERT INTO message VALUES ('a', 's', 1, 1, ?)", [
433+
JSON.stringify({ role: "assistant", modelID: "m", tokens: { input: 7 } }),
434+
]);
435+
if (openCodeUsageReader.kind !== "scan") throw new Error("Expected an OpenCode scan reader");
436+
const reader = openCodeUsageReader;
437+
const host = yield* ProviderHost.ProviderHost;
438+
const cachePath = path.join(host.paths.stateDir, "usage-opencode-cache-v1.json");
439+
const scan = reader.scan({
440+
instances: [
441+
{
442+
instanceId: ProviderInstanceId.make("opencode"),
443+
config: undefined,
444+
environment: { OPENCODE_DATA_DIR: dir },
445+
configured: true,
446+
},
447+
],
448+
settings: DEFAULT_SERVER_SETTINGS,
449+
windowStartMs: 0,
450+
retentionCutoffMs: 0,
451+
awaitRefresh: true,
452+
});
453+
yield* scan;
454+
assert.isFalse(yield* fileSystem.exists(cachePath));
455+
// The rows are unchanged since the failed write, so only the retry can save them.
456+
yield* scan;
457+
assert.isTrue(yield* fileSystem.exists(cachePath));
458+
}).pipe(
459+
Effect.provide(
460+
TestProviderHost.layer().pipe(
461+
Layer.provideMerge(flakyFileSystem),
462+
Layer.provideMerge(NodeServices.layer),
463+
),
464+
),
465+
);
466+
});
408467
});
409468

410469
describe("readOpenCodeUsage cache", () => {

‎packages/provider-opencode/src/server/usage.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,8 @@ const decodeCacheFile = Schema.decodeUnknownEffect(CacheFile);
395395
const encodeCacheFile = Schema.encodeEffect(CacheFile);
396396
// Parsed rows by cache file, so refreshes and restarts re-read only rows that changed.
397397
const rowCaches = new Map<string, OpenCodeUsageCache>();
398+
// Cache files whose last write failed; the next scan writes them again.
399+
const unsavedCachePaths = new Set<string>();
398400

399401
export const openCodeUsageReader: ProviderUsageReader<OpenCodeSettings, OpenCodeUsageReaderEnv> = {
400402
kind: "scan",
@@ -440,9 +442,11 @@ export const openCodeUsageReader: ProviderUsageReader<OpenCodeSettings, OpenCode
440442
{ concurrency: "unbounded" },
441443
);
442444
if (
445+
unsavedCachePaths.has(cachePath) ||
443446
rowCache.size !== before.size ||
444447
rowCache.entries().some(([key, rows]) => before.get(key) !== rows)
445448
) {
449+
unsavedCachePaths.add(cachePath);
446450
const tables = rowCache.entries().map(([key, rows]) => {
447451
const entries = rows
448452
.entries()
@@ -451,6 +455,7 @@ export const openCodeUsageReader: ProviderUsageReader<OpenCodeSettings, OpenCode
451455
});
452456
yield* encodeCacheFile(Object.fromEntries(tables)).pipe(
453457
Effect.flatMap((contents) => writeFileStringAtomically({ filePath: cachePath, contents })),
458+
Effect.tap(() => Effect.sync(() => unsavedCachePaths.delete(cachePath))),
454459
Effect.ignore({ log: true }),
455460
);
456461
}

0 commit comments

Comments
 (0)