@devries-cameron20 / t3code Mirror · read-only / commits / bc91203bc3
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 12:15 UTC

fix(server): count only newly imported agent threads (#37025)

Co-authored-by: Federico Lomurno <275704276+flomurno@users.noreply.github.com>

Julius Marminge committed Nov 14, 2026 at 19:05 UTC bc91203bc326eeba47a38dbd9cbd38957d000c6d
2 files changed +49 -6
apps/server/src/project/AgentSessionImporter.test.ts
+47 -4
@@ -185,7 +185,7 @@ const seedOwner = Effect.fn(function* (
185 });
186
187 const makeImporter = Effect.fn(function* (
188 - candidates: ReadonlyArray<ReturnType<typeof candidate>>,
188 + candidates: ReadonlyArray<AgentSessionScanner.AgentSessionRecentThread>,
189 beforeTransaction: Effect.Effect<void> = Effect.void,
190 getAdapter?: ProviderAdapterRegistry.ProviderAdapterRegistryV2Shape["get"],
191 threadCommands?: KeyedLock.KeyedLock<ThreadId>,
@@ -484,8 +484,8 @@ it.effect(
484 expect(yield* runtimes.list()).toEqual([]);
485 yield* sql`DROP TRIGGER fail_runtime`;
486
487 - expect(yield* importer.run).toEqual({ importedCount: 1, skippedCount: 0 });
488 - expect(yield* importer.run).toEqual({ importedCount: 1, skippedCount: 0 });
487 + expect(yield* importer.run).toEqual({ importedCount: 0, skippedCount: 0 });
488 + expect(yield* importer.run).toEqual({ importedCount: 0, skippedCount: 0 });
489 expect(yield* events.read({ threadId }).pipe(Stream.runCollect)).toEqual(committed);
490 expect(importer.published).toHaveLength(6);
491 expect(
@@ -501,6 +501,49 @@ it.effect(
501 }).pipe(Effect.scoped, Effect.provide(persistence)),
502 );
503
504 +it.effect("counts only new threads when re-importing cached and recovered transcripts", () =>
505 + Effect.gen(function* () {
506 + const events = yield* EventStore.EventStoreV2;
507 + const runtimes = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository;
508 + const cached = candidate(
509 + CLAUDE_MODEL_SELECTION,
510 + "020e7f00-dce6-406b-8ec6-ed90498945d6",
511 + "claudeAgent",
512 + );
513 + const recovered = candidate(CODEX_MODEL_SELECTION, "recovered");
514 + const initial = yield* makeImporter([cached, recovered]);
515 + expect(yield* initial.run).toEqual({ importedCount: 2, skippedCount: 0 });
516 + const before = yield* events.read({ threadId: importId(recovered) }).pipe(Stream.runCollect);
517 + // The file changed on disk but still holds the imported messages.
518 + recovered.source.size += 100;
519 + recovered.source.mtimeMs += 1;
520 + const duplicate = { ...recovered.source, filePath: "/tmp/duplicate.jsonl" };
521 + const fresh = candidate(CODEX_MODEL_SELECTION, "fresh");
522 + const repeated = yield* makeImporter([
523 + { _tag: "AlreadyImported", source: cached.source },
524 + recovered,
525 + { _tag: "Duplicate", source: duplicate },
526 + fresh,
527 + { _tag: "Skipped" },
528 + ]);
529 +
530 + expect(yield* repeated.run).toEqual({ importedCount: 1, skippedCount: 1 });
531 + expect(yield* events.read({ threadId: importId(recovered) }).pipe(Stream.runCollect)).toEqual(
532 + before,
533 + );
534 + expect(
535 + new Set(
536 + repeated.published.flatMap((stored) => ("event" in stored ? [stored.event.threadId] : [])),
537 + ),
538 + ).toEqual(new Set([importId(fresh)]));
539 + expect(
540 + Option.getOrThrow(yield* runtimes.getByThreadId({ threadId: importId(recovered) })),
541 + ).toMatchObject({
542 + runtimePayload: { importedTranscripts: [recovered.source, duplicate] },
543 + });
544 + }).pipe(Effect.scoped, Effect.provide(persistence)),
545 +);
546 +
547 it.effect("does not certify new native history after a partial import retry", () =>
548 Effect.gen(function* () {
549 const sql = yield* SqlClient.SqlClient;
@@ -1000,7 +1043,7 @@ it.effect("repeated imports reach linked-worktree sessions behind the scan budge
1043 });
1044
1045 expect(yield* importer.run).toEqual({ importedCount: 100, skippedCount: 1 });
1003 - expect(yield* importer.run).toEqual({ importedCount: 101, skippedCount: 0 });
1046 + expect(yield* importer.run).toEqual({ importedCount: 1, skippedCount: 0 });
1047 const oldest = yield* ProjectionStore.ProjectionStoreV2.pipe(
1048 Effect.flatMap((projections) =>
1049 projections.getThread(ThreadId.make("import:codex:session-100")),
apps/server/src/project/AgentSessionImporter.ts
+2 -2
@@ -279,7 +279,6 @@ const make = Effect.gen(function* () {
279 );
280 if (outcome._tag === "AlreadyImported") {
281 importedThreadIds.add(threadId);
282 - importedCount += 1;
282 return;
283 }
284 if (outcome._tag === "Duplicate") {
@@ -350,7 +349,8 @@ const make = Effect.gen(function* () {
349 }
350 yield* persistRuntime;
351 yield* runtimes.recordImportedTranscript({ threadId, source });
353 - return true;
352 + importedThreadIds.add(threadId);
353 + return null;
354 }
355
356 const model = thread.model ?? DEFAULT_MODEL_BY_PROVIDER[driver] ?? DEFAULT_MODEL;