diff --git a/apps/server/src/project/AgentSessionImporter.test.ts b/apps/server/src/project/AgentSessionImporter.test.ts index c659622a2f1e8..a4dfe91371bd7 100644 --- a/apps/server/src/project/AgentSessionImporter.test.ts +++ b/apps/server/src/project/AgentSessionImporter.test.ts @@ -179,7 +179,7 @@ const seedOwner = Effect.fn(function* ( }); const makeImporter = Effect.fn(function* ( - candidates: ReadonlyArray>, + candidates: ReadonlyArray, beforeTransaction: Effect.Effect = Effect.void, getAdapter?: ProviderAdapterRegistry.ProviderAdapterRegistryV2Shape["get"], threadCommands?: KeyedLock.KeyedLock, @@ -192,6 +192,7 @@ const makeImporter = Effect.fn(function* ( const events = yield* EventStore.EventStoreV2; const projections = yield* ProjectionStore.ProjectionStoreV2; const published: Array = []; + const ownedSourceCalls: Array> = []; const withTransaction: typeof sql.withTransaction = (effect) => beforeTransaction.pipe(Effect.andThen(sql.withTransaction(effect))); const importSql = new Proxy(sql, { @@ -229,7 +230,10 @@ const makeImporter = Effect.fn(function* ( yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository, ), Layer.mock(AgentSessionScanner.AgentSessionScanner)({ - recentThreads: () => Stream.fromIterable(candidates), + recentThreads: (_workspaceRoot, _completedSources, ownedSources = []) => { + ownedSourceCalls.push(ownedSources); + return Stream.fromIterable(candidates); + }, }), Layer.mock(ProjectService.ProjectService)({ getById: () => @@ -270,6 +274,7 @@ const makeImporter = Effect.fn(function* ( run: importer.importRecentAgentThreads({ projectId }), importNativeHistory: importer.importNativeHistory, published, + ownedSourceCalls, }; }); @@ -311,11 +316,14 @@ it.effect( deleted, ownedPi, otherInstance, + { _tag: "Deferred" }, unowned, + { _tag: "Skipped" }, unownedPi, + { _tag: "Deferred" }, ]); - expect(yield* importer.run).toEqual({ importedCount: 3, skippedCount: 0 }); + expect(yield* importer.run).toEqual({ importedCount: 3, skippedCount: 3, deferredCount: 2 }); for (const source of [active, archived, deleted, ownedPi]) { const threadId = importId(source); expect(yield* Effect.option(projections.getThread(threadId))).toEqual(Option.none()); @@ -367,6 +375,12 @@ it.effect( runtimePayload: { importedTranscripts: [source.source] }, }); } + + yield* importer.run; + expect(importer.ownedSourceCalls).toEqual([ + [], + [active.source, archived.source, deleted.source, ownedPi.source], + ]); }).pipe(Effect.scoped, Effect.provide(persistence)), ); @@ -386,8 +400,12 @@ it.effect("rechecks ownership after the import reaches the commit transaction", // Commit ownership after all pre-transaction reads, before the import acquires SQLite. const ownerId = yield* seedOwner(source); yield* Deferred.succeed(continueTransaction, undefined); - expect(yield* Fiber.join(pending)).toEqual({ importedCount: 0, skippedCount: 0 }); - expect(yield* importer.run).toEqual({ importedCount: 0, skippedCount: 0 }); + expect(yield* Fiber.join(pending)).toEqual({ + importedCount: 0, + skippedCount: 0, + deferredCount: 0, + }); + expect(yield* importer.run).toEqual({ importedCount: 0, skippedCount: 0, deferredCount: 0 }); const projections = yield* ProjectionStore.ProjectionStoreV2; const events = yield* EventStore.EventStoreV2; const runtimes = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository; @@ -414,7 +432,7 @@ it.effect( const threadId = importId(source); const importer = yield* makeImporter([source]); yield* sql`CREATE TEMP TRIGGER fail_import BEFORE INSERT ON orchestration_events WHEN NEW.event_type = 'message.updated' BEGIN SELECT RAISE(ABORT, 'test publication failure'); END`; - expect(yield* importer.run).toEqual({ importedCount: 0, skippedCount: 1 }); + expect(yield* importer.run).toEqual({ importedCount: 0, skippedCount: 1, deferredCount: 0 }); expect(yield* events.read({ threadId }).pipe(Stream.runCollect)).toEqual([]); expect(yield* Effect.option(projections.getThread(threadId))).toEqual(Option.none()); expect(yield* runtimes.list()).toEqual([]); @@ -422,15 +440,15 @@ it.effect( yield* sql`DROP TRIGGER fail_import`; yield* sql`CREATE TEMP TRIGGER fail_runtime BEFORE INSERT ON provider_session_runtime BEGIN SELECT RAISE(ABORT, 'test runtime failure'); END`; - expect(yield* importer.run).toEqual({ importedCount: 0, skippedCount: 1 }); + expect(yield* importer.run).toEqual({ importedCount: 0, skippedCount: 1, deferredCount: 0 }); const committed = yield* events.read({ threadId }).pipe(Stream.runCollect); expect(committed).toHaveLength(6); expect(importer.published).toHaveLength(6); expect(yield* runtimes.list()).toEqual([]); yield* sql`DROP TRIGGER fail_runtime`; - expect(yield* importer.run).toEqual({ importedCount: 1, skippedCount: 0 }); - expect(yield* importer.run).toEqual({ importedCount: 1, skippedCount: 0 }); + expect(yield* importer.run).toEqual({ importedCount: 1, skippedCount: 0, deferredCount: 0 }); + expect(yield* importer.run).toEqual({ importedCount: 1, skippedCount: 0, deferredCount: 0 }); expect(yield* events.read({ threadId }).pipe(Stream.runCollect)).toEqual(committed); expect(importer.published).toHaveLength(6); expect( @@ -781,7 +799,7 @@ it.effect("imports under a policy ceiling in the project's default, kept under t }, ), ); - expect(yield* importer.run).toEqual({ importedCount: 1, skippedCount: 0 }); + expect(yield* importer.run).toEqual({ importedCount: 1, skippedCount: 0, deferredCount: 0 }); expect((yield* projections.getThread(threadId)).runtimeMode).toBe("auto-accept-edits"); expect(Option.getOrThrow(yield* runtimes.getByThreadId({ threadId })).runtimeMode).toBe( "auto-accept-edits", diff --git a/apps/server/src/project/AgentSessionImporter.ts b/apps/server/src/project/AgentSessionImporter.ts index ec3e9e3bbe21a..a9f8e54900351 100644 --- a/apps/server/src/project/AgentSessionImporter.ts +++ b/apps/server/src/project/AgentSessionImporter.ts @@ -207,6 +207,8 @@ const make = Effect.gen(function* () { const threadCommands = yield* ThreadCommandExecutor.ThreadCommandExecutor; const settingsService = yield* ServerSettings.ServerSettingsService; const ceiling = settingsService.managedPolicy?.limits?.maxRuntimeMode; + /** Transcripts whose native session a T3 thread owns, so later attempts skip them for free. */ + const ownedTranscripts = new Map(); const importRecentAgentThreads = Effect.fn("importRecentAgentThreadsV2")(function* ( input: AgentSessionImportInput, ) { @@ -261,10 +263,13 @@ const make = Effect.gen(function* () { } return payload.value.importedTranscripts ?? []; }); - const outcomes = scanner.recentThreads(project.workspaceRoot, completedSources); + const outcomes = scanner.recentThreads(project.workspaceRoot, completedSources, [ + ...ownedTranscripts.values(), + ]); const importedThreadIds = new Set(); let importedCount = 0; let skippedCount = 0; + let deferredCount = 0; yield* Stream.runForEach(outcomes, (outcome) => Effect.gen(function* () { @@ -272,6 +277,11 @@ const make = Effect.gen(function* () { skippedCount += 1; return; } + if (outcome._tag === "Deferred") { + skippedCount += 1; + deferredCount += 1; + return; + } const source = outcome.source; const threadId = ThreadId.make( `import:${source.providerInstanceId}:${source.providerSessionId}`, @@ -457,7 +467,10 @@ const make = Effect.gen(function* () { }, ], }); - if (committed.length === 0) return null; + if (committed.length === 0) { + ownedTranscripts.set(`${source.providerInstanceId}\0${source.filePath}`, source); + return null; + } yield* persistRuntime; yield* runtimes.recordImportedTranscript({ threadId, source }); return true; @@ -479,7 +492,7 @@ const make = Effect.gen(function* () { }), ); - return { importedCount, skippedCount } satisfies AgentSessionImportResult; + return { importedCount, skippedCount, deferredCount } satisfies AgentSessionImportResult; }); /** diff --git a/apps/server/src/project/AgentSessionScanner.test.ts b/apps/server/src/project/AgentSessionScanner.test.ts index f672768e7ac58..2e42ffe55fd66 100644 --- a/apps/server/src/project/AgentSessionScanner.test.ts +++ b/apps/server/src/project/AgentSessionScanner.test.ts @@ -1710,34 +1710,71 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { const claudeHomePath = yield* makeTempDir("t3code-record-budget-claude-"); const codexHomePath = yield* makeTempDir("t3code-record-budget-codex-"); const workspace = yield* makeTempDir("t3code-record-budget-workspace-"); - const exactLimit = makeRecordLimitTranscript(workspace, false); + const rolloutPath = (name: string) => + path.join(codexHomePath, "sessions", "2026", "08", "24", `rollout-${name}.jsonl`); + for (let index = 0; index < 99; index += 1) { + yield* writeTranscript({ + filePath: rolloutPath(`missing-base-${String(index).padStart(2, "0")}`), + contents: encodeTranscriptRecord({ + type: "session_meta", + payload: { + id: `missing-base-${index}`, + cwd: workspace, + history_base: { thread_id: "missing-parent", end_byte_offset: 1 }, + }, + }), + mtimeMs: nowMs - index, + }); + } // The newer transcript has 100,001 records; the older one has exactly 100,000. + const exactLimit = makeRecordLimitTranscript(workspace, false); for (const [sessionId, contents, mtimeMs] of [ - ["excessive", exactLimit.replace("record-limit-session", "excessive") + "{}\n", nowMs], + [ + "excessive", + exactLimit.replace("record-limit-session", "excessive") + "{}\n", + nowMs - 500, + ], ["older", exactLimit, nowMs - 1_000], + [ + "oldest", + [ + encodeTranscriptRecord({ + type: "session_meta", + payload: { id: "oldest", cwd: workspace }, + }), + encodeTranscriptRecord({ + type: "event_msg", + payload: { type: "user_message", message: "Oldest prompt" }, + }), + ].join("\n"), + nowMs - 2_000, + ], ] as const) { - yield* writeTranscript({ - filePath: path.join( - codexHomePath, - "sessions", - "2026", - "08", - "24", - `rollout-${sessionId}.jsonl`, - ), - contents, - mtimeMs, - }); + yield* writeTranscript({ filePath: rolloutPath(sessionId), contents, mtimeMs }); } - const outcomes = yield* runRecentThreadOutcomes({ - claudeHomePath, - codexHomePath, - workspaceRoot: workspace, - }); - expect(outcomes.map((outcome) => outcome._tag)).toEqual(["Skipped", "Importable"]); - expect(outcomes[1]).toMatchObject({ - thread: { providerSessionId: "record-limit-session" }, - }); + const missingBaseFailures = Array.from({ length: 99 }, () => "Skipped"); + + yield* Effect.gen(function* () { + const scanner = yield* AgentSessionScanner.AgentSessionScanner; + const first = yield* Stream.runCollect(scanner.recentThreads(workspace)); + expect(Array.from(first, (outcome) => outcome._tag)).toEqual([ + ...missingBaseFailures, + "Skipped", + "Deferred", + "Deferred", + ]); + + const second = Array.from(yield* Stream.runCollect(scanner.recentThreads(workspace))); + expect(second.map((outcome) => outcome._tag)).toEqual([ + ...missingBaseFailures, + "Skipped", + "Importable", + "Deferred", + ]); + expect(second[100]).toMatchObject({ + thread: { providerSessionId: "record-limit-session" }, + }); + }).pipe(Effect.provide(layerScannerTest({ claudeHomePath, codexHomePath }))); }), ); @@ -1790,6 +1827,87 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { }), ); + it.effect( + "defers transcripts past the per-attempt budget and skips known failures for free", + () => + Effect.gen(function* () { + const path = yield* Path.Path; + const nowMs = Date.parse("2026-08-24T12:00:00.000Z"); + yield* TestClock.setTime(nowMs); + const claudeHomePath = yield* makeTempDir("t3code-transcript-budget-claude-"); + const codexHomePath = yield* makeTempDir("t3code-transcript-budget-codex-"); + const workspace = yield* makeTempDir("t3code-transcript-budget-workspace-"); + const writeRollout = (index: number, withPrompt: boolean) => + writeTranscript({ + filePath: path.join( + codexHomePath, + "sessions", + "2026", + "08", + "24", + `rollout-${String(index).padStart(3, "0")}.jsonl`, + ), + contents: [ + { type: "session_meta", payload: { id: `session-${index}`, cwd: workspace } }, + ...(withPrompt + ? [ + { + type: "event_msg", + payload: { type: "user_message", message: "Import this prompt" }, + }, + ] + : []), + ] + .map((record) => encodeTranscriptRecord(record)) + .join("\n"), + mtimeMs: nowMs - index * 60_000, + }); + for (let index = 0; index <= 200; index += 1) { + yield* writeRollout(index, index >= 100); + } + const tags = (outcomes: Iterable) => + Array.from(outcomes, (outcome) => outcome._tag); + const repeat = (count: number, tag: string) => Array.from({ length: count }, () => tag); + + yield* Effect.gen(function* () { + const scanner = yield* AgentSessionScanner.AgentSessionScanner; + const first = yield* Stream.runCollect(scanner.recentThreads(workspace)); + expect(tags(first)).toEqual([...repeat(100, "Skipped"), ...repeat(101, "Deferred")]); + + const second = yield* Stream.runCollect(scanner.recentThreads(workspace)); + expect(tags(second)).toEqual([ + ...repeat(100, "Skipped"), + ...repeat(100, "Importable"), + "Deferred", + ]); + const completed = Array.from(second).flatMap((outcome) => + outcome._tag === "Importable" ? [outcome.source] : [], + ); + + yield* writeRollout(0, true); + const third = Array.from( + yield* Stream.runCollect(scanner.recentThreads(workspace, completed)), + ); + expect(tags(third)).toEqual([ + "Importable", + ...repeat(99, "Skipped"), + ...repeat(100, "AlreadyImported"), + "Importable", + ]); + expect([third[0], third[200]]).toMatchObject([ + { thread: { providerSessionId: "session-0" } }, + { thread: { providerSessionId: "session-200" } }, + ]); + + const fourth = Array.from( + yield* Stream.runCollect(scanner.recentThreads(workspace, [], completed)), + ); + expect(tags(fourth)).toEqual(["Importable", ...repeat(99, "Skipped"), "Importable"]); + expect(fourth.at(-1)).toMatchObject({ thread: { providerSessionId: "session-200" } }); + }).pipe(Effect.provide(layerScannerTest({ claudeHomePath, codexHomePath }))); + }), + ); + for (const source of ["claudeAgent", "codex"] as const) { for (const replacement of [ "same root", @@ -2105,6 +2223,66 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { }), ); + it.effect("retries a transcript after a read error", () => + Effect.gen(function* () { + const path = yield* Path.Path; + const fileSystem = yield* FileSystem.FileSystem; + const nowMs = Date.parse("2026-08-24T12:00:00.000Z"); + yield* TestClock.setTime(nowMs); + const claudeHomePath = yield* makeTempDir("t3code-claude-home-"); + const codexHomePath = yield* makeTempDir("t3code-codex-home-"); + const workspace = yield* makeTempDir("t3code-workspace-"); + const transcriptPath = path.join( + codexHomePath, + "sessions", + "2026", + "08", + "24", + "rollout-flaky.jsonl", + ); + yield* writeTranscript({ + filePath: transcriptPath, + contents: [ + encodeTranscriptRecord({ + type: "session_meta", + payload: { id: "flaky-session", cwd: workspace }, + }), + encodeTranscriptRecord({ + type: "event_msg", + payload: { type: "user_message", message: "Import after the error" }, + }), + ].join("\n"), + mtimeMs: nowMs, + }); + const firstImportOpen = 2; + let transcriptOpenCount = 0; + const simulatedFileSystem = FileSystem.FileSystem.of({ + ...fileSystem, + open: (filePath, options) => { + if (filePath !== transcriptPath) return fileSystem.open(filePath, options); + transcriptOpenCount += 1; + return fileSystem.open( + transcriptOpenCount === firstImportOpen ? `${filePath}.unavailable` : filePath, + options, + ); + }, + }); + + yield* Effect.gen(function* () { + const scanner = yield* AgentSessionScanner.AgentSessionScanner; + const first = yield* Stream.runCollect(scanner.recentThreads(workspace)); + expect(Array.from(first)).toEqual([{ _tag: "Skipped" }]); + const second = Array.from(yield* Stream.runCollect(scanner.recentThreads(workspace))); + expect(second).toMatchObject([ + { _tag: "Importable", thread: { providerSessionId: "flaky-session" } }, + ]); + }).pipe( + Effect.provide(layerScannerTest({ claudeHomePath, codexHomePath })), + Effect.provideService(FileSystem.FileSystem, simulatedFileSystem), + ); + }), + ); + it.effect("uses configured order when custom instances share a session home", () => Effect.gen(function* () { const path = yield* Path.Path; diff --git a/apps/server/src/project/AgentSessionScanner.ts b/apps/server/src/project/AgentSessionScanner.ts index 1b32de22a8878..40b7710d2ed38 100644 --- a/apps/server/src/project/AgentSessionScanner.ts +++ b/apps/server/src/project/AgentSessionScanner.ts @@ -212,15 +212,18 @@ export type AgentSessionRecentThread = } | { readonly _tag: "AlreadyImported"; readonly source: AgentSessionImportSource } | { readonly _tag: "Duplicate"; readonly source: AgentSessionImportSource } - | { readonly _tag: "Skipped" }; + | { readonly _tag: "Skipped" } + | { readonly _tag: "Deferred" }; export class AgentSessionScanner extends Context.Service< AgentSessionScanner, { readonly scan: Effect.Effect; + /** Owned sources belong to native sessions T3 already has; they are dropped for free. */ readonly recentThreads: ( workspaceRoot: string, completedSources?: ReadonlyArray, + ownedSources?: ReadonlyArray, ) => Stream.Stream; } >()("t3/project/AgentSessionScanner") {} @@ -1039,6 +1042,10 @@ export const make = Effect.gen(function* () { ).pipe(Effect.orElseSucceed(() => null)); }); + /** + * Returns "unimportable" when an unchanged file would fail the same way again (read limits, + * parse errors) and null when a later read may succeed (I/O errors, a file changing mid-read). + */ const readTranscript = Effect.fn("AgentSessionScanner.readTranscript")(function* ( filePath: string, expected: ReturnType, @@ -1046,7 +1053,9 @@ export const make = Effect.gen(function* () { prefix?: { readonly endByteOffset: number; readonly historyBytes: number }, ) { const readBytes = prefix?.endByteOffset ?? expected.size; - if (readBytes > expected.size || readBytes > MAX_IMPORTED_TRANSCRIPT_BYTES) return null; + if (readBytes > expected.size || readBytes > MAX_IMPORTED_TRANSCRIPT_BYTES) { + return "unimportable" as const; + } return yield* Effect.scoped( fileSystem.open(filePath, { flag: "r" }).pipe( @@ -1135,7 +1144,7 @@ export const make = Effect.gen(function* () { }, catch: (cause) => new TranscriptParseError({ filePath, cause }), }); - if (!withinBudget) return null; + if (!withinBudget) return "unimportable" as const; } if ( @@ -1145,7 +1154,7 @@ export const make = Effect.gen(function* () { catch: (cause) => new TranscriptParseError({ filePath, cause }), })) ) - return null; + return "unimportable" as const; return sameTranscriptIdentity(expected, transcriptIdentity(filePath, yield* file.stat)) ? { records, cwd, claudeFields, historyBytes } : null; @@ -1153,6 +1162,12 @@ export const make = Effect.gen(function* () { ), ), ).pipe( + Effect.catchTags({ + TranscriptParseError: (cause) => + Effect.logWarning("Could not read imported transcript", { filePath, cause }).pipe( + Effect.as("unimportable" as const), + ), + }), Effect.catch((cause) => Effect.logWarning("Could not read imported transcript", { filePath, cause }).pipe( Effect.as(null), @@ -1589,6 +1604,8 @@ export const make = Effect.gen(function* () { }); let cachedCandidates: ReadonlyArray | null = null; + /** Unimportable transcripts by file, so later attempts skip them without spending the budget. */ + const unimportableTranscripts = new Map(); const scan: AgentSessionScanner["Service"]["scan"] = Effect.gen(function* () { const { candidates: raw, truncated } = yield* collectCandidates(); @@ -1724,6 +1741,7 @@ export const make = Effect.gen(function* () { const prepareRecentThreads = Effect.fn("AgentSessionScanner.prepareRecentThreads")(function* ( workspaceRoot: string, completedSources: ReadonlyArray, + ownedSources: ReadonlyArray, ) { const root = path.resolve(expandHomePath(workspaceRoot, homeDir)); const realRoot = yield* fileSystem.realPath(root).pipe(Effect.orElseSucceed(() => root)); @@ -1771,6 +1789,10 @@ export const make = Effect.gen(function* () { completedSources, (source) => `${source.providerInstanceId}\0${source.filePath}`, ); + const ownedByFile = Map.groupBy( + ownedSources, + (source) => `${source.providerInstanceId}\0${source.filePath}`, + ); const importedSessions = new Set(); const codexThreadNamesByHome = new Map>(); let bytesRemaining = MAX_IMPORT_BYTES; @@ -1818,7 +1840,7 @@ export const make = Effect.gen(function* () { "codex", { endByteOffset: base.end_byte_offset, historyBytes }, ); - if (inherited === null) return null; + if (inherited === null || inherited === "unimportable") return null; historyBytes = inherited.historyBytes; // The rollout's own session_meta names the thread; inherited ones may name a fork parent. prefixes.unshift(inherited.records.filter((record) => record.type !== "session_meta")); @@ -1829,11 +1851,17 @@ export const make = Effect.gen(function* () { return Stream.fromIteratorSucceed(eligibleTranscripts.values(), 1).pipe( Stream.mapEffect(({ candidate, transcript }) => Effect.gen(function* () { - const completed = completedByFile.get( - `${candidate.providerInstanceId}\0${transcript.filePath}`, - ); - if (completed === undefined && (transcriptsRemaining === 0 || bytesRemaining === 0)) { - return Option.some({ _tag: "Skipped" }); + const fileKey = `${candidate.providerInstanceId}\0${transcript.filePath}`; + const completed = completedByFile.get(fileKey); + const unimportable = unimportableTranscripts.get(fileKey); + const owned = ownedByFile.get(fileKey); + if ( + completed === undefined && + unimportable === undefined && + owned === undefined && + (transcriptsRemaining === 0 || bytesRemaining === 0) + ) { + return Option.some({ _tag: "Deferred" }); } const stats = yield* statOption(transcript.filePath); if (Option.isNone(stats) || stats.value.type !== "File") { @@ -1854,12 +1882,22 @@ export const make = Effect.gen(function* () { }); } if ( - transcriptsRemaining === 0 || + owned?.some( + (source) => + source.provider === candidate.source && sameTranscriptIdentity(source, identity), + ) + ) { + return Option.none(); + } + if ( identity.size > MAX_IMPORTED_TRANSCRIPT_BYTES || - identity.size > bytesRemaining + (unimportable !== undefined && sameTranscriptIdentity(unimportable, identity)) ) { return Option.some({ _tag: "Skipped" }); } + if (transcriptsRemaining === 0 || identity.size > bytesRemaining) { + return Option.some({ _tag: "Deferred" }); + } transcriptsRemaining -= 1; bytesRemaining -= identity.size; const snapshot = yield* readTranscript(transcript.filePath, identity, candidate.source); @@ -1867,7 +1905,8 @@ export const make = Effect.gen(function* () { return Option.some({ _tag: "Skipped" }); } - if (Option.isNone(snapshot.cwd)) { + if (snapshot === "unimportable" || Option.isNone(snapshot.cwd)) { + unimportableTranscripts.set(fileKey, identity); return Option.some({ _tag: "Skipped" }); } const expandedCwd = expandHomePath( @@ -1902,6 +1941,8 @@ export const make = Effect.gen(function* () { snapshot.claudeFields, ); if (parsedThread === null) { + const parsedWithFullHistory = candidate.source !== "codex" || inheritedRecords !== null; + if (parsedWithFullHistory) unimportableTranscripts.set(fileKey, identity); return Option.some({ _tag: "Skipped" }); } let savedTitle: string | undefined; @@ -1940,7 +1981,8 @@ export const make = Effect.gen(function* () { const recentThreads: AgentSessionScanner["Service"]["recentThreads"] = ( workspaceRoot, completedSources = [], - ) => Stream.unwrap(prepareRecentThreads(workspaceRoot, completedSources)); + ownedSources = [], + ) => Stream.unwrap(prepareRecentThreads(workspaceRoot, completedSources, ownedSources)); return AgentSessionScanner.of({ scan, recentThreads }); }); diff --git a/apps/web/src/components/onboarding/WelcomePage.import.browser.test.tsx b/apps/web/src/components/onboarding/WelcomePage.import.browser.test.tsx index d8099e3907bfb..01d2b91d8371f 100644 --- a/apps/web/src/components/onboarding/WelcomePage.import.browser.test.tsx +++ b/apps/web/src/components/onboarding/WelcomePage.import.browser.test.tsx @@ -14,6 +14,7 @@ import { type EnvironmentCloudLinkStateResult, type ServerProvider, } from "@t3tools/contracts"; +import * as Cause from "effect/Cause"; import { AsyncResult, Atom } from "effect/reactivity"; import { expect, it, vi } from "vite-plus/test"; @@ -150,6 +151,7 @@ vi.mock(import("../../state/use-atom-command"), async (original) => ({ state[command]) as never, })); +import { formatOnboardingImportWarning } from "../../onboarding/projectImport.logic"; import { welcomeImportSelectionKey } from "../../onboarding/welcomeState"; import { appAtomRegistry } from "../../rpc/atomRegistry"; import { toastManager } from "../ui/toast"; @@ -739,3 +741,181 @@ it("says the phone is set up only once this computer is shared with the signed-i await vi.waitFor(() => expect(pageText()).toContain("Your phone can reach this computer.")); expect(pageText()).not.toContain("Check T3 Connect"); }); + +/** The summary wording is covered by projectImport.logic.test.ts; these check the counts. */ +function expectImportSummary( + counts: Parameters[0], + continueImport: boolean, +) { + const summary = vi + .mocked(toastManager.add) + .mock.calls.map(([toast]) => toast) + .find((toast) => toast.title === "Some history was not imported"); + expect(summary?.description).toBe(formatOnboardingImportWarning(counts)); + expect(summary?.actionProps?.children).toBe(continueImport ? "Continue import" : undefined); +} + +it("imports in batches and stops after the current batch without starting the next project", async () => { + using _now = setup(); + const secondBatch = deferred<{ + importedCount: number; + skippedCount: number; + deferredCount: number; + }>(); + state.importThreads + .mockResolvedValueOnce( + AsyncResult.success({ importedCount: 2, skippedCount: 5, deferredCount: 5 }), + ) + .mockImplementationOnce(async () => AsyncResult.success(await secondBatch.promise)); + await using _screen = await mountWelcome(); + using _toastAdd = vi.spyOn(toastManager, "add"); + + await click("Import 2 projects and start"); + await vi.waitFor(() => expect(state.importThreads).toHaveBeenCalledTimes(2)); + await vi.waitFor(() => + expect(pageText()).toContain("Importing first (1 of 2)… 2 threads imported, 5 left"), + ); + + await click("Stop importing"); + expect(button("Stopping…").disabled).toBe(true); + await act(async () => { + secondBatch.resolve({ importedCount: 4, skippedCount: 3, deferredCount: 3 }); + await secondBatch.promise; + }); + await vi.waitFor(() => expect(state.onDone).toHaveBeenCalledOnce()); + expect(state.importThreads).toHaveBeenCalledTimes(2); + expect(state.createProject).toHaveBeenCalledOnce(); + expectImportSummary({ imported: 4, failed: 0, deferred: 3, stopped: true }, true); +}); + +it("stops when a batch makes no progress and reports the rest as not imported", async () => { + using _now = setup(); + appAtomRegistry.set(candidatesAtom(), [candidate("/projects/first")]); + state.importThreads.mockResolvedValue( + AsyncResult.success({ importedCount: 2, skippedCount: 5, deferredCount: 5 }), + ); + await using _screen = await mountWelcome(); + using _toastAdd = vi.spyOn(toastManager, "add"); + + await click("Import 1 project and start"); + await vi.waitFor(() => expect(state.onDone).toHaveBeenCalledOnce()); + expect(state.importThreads).toHaveBeenCalledTimes(2); + expectImportSummary({ imported: 2, failed: 5, deferred: 0, stopped: false }, false); +}); + +it("keeps the last successful batch when a later batch fails", async () => { + using _now = setup(); + appAtomRegistry.set(candidatesAtom(), [candidate("/projects/first")]); + state.importThreads + .mockResolvedValueOnce( + AsyncResult.success({ importedCount: 2, skippedCount: 5, deferredCount: 5 }), + ) + .mockResolvedValueOnce(AsyncResult.failure(Cause.fail(new Error("offline")))); + await using _screen = await mountWelcome(); + using _toastAdd = vi.spyOn(toastManager, "add"); + + await click("Import 1 project and start"); + await vi.waitFor(() => expect(state.onDone).toHaveBeenCalledOnce()); + expect(state.importThreads).toHaveBeenCalledTimes(2); + expect(state.refreshScan).toHaveBeenCalledOnce(); + expectImportSummary({ imported: 2, failed: 0, deferred: 5, stopped: false }, true); +}); + +it("counts each project's last batch and treats servers without deferred counts as one batch", async () => { + using _now = setup(); + state.importThreads + .mockResolvedValueOnce(AsyncResult.success({ importedCount: 1, skippedCount: 2 })) + .mockResolvedValueOnce( + AsyncResult.success({ importedCount: 2, skippedCount: 3, deferredCount: 3 }), + ) + .mockResolvedValueOnce( + AsyncResult.success({ importedCount: 5, skippedCount: 0, deferredCount: 0 }), + ); + await using _screen = await mountWelcome(); + using _toastAdd = vi.spyOn(toastManager, "add"); + + await click("Import 2 projects and start"); + await vi.waitFor(() => expect(state.onDone).toHaveBeenCalledOnce()); + expect(state.importThreads).toHaveBeenCalledTimes(3); + expectImportSummary({ imported: 6, failed: 2, deferred: 0, stopped: false }, false); +}); + +it("reopens the page on the unfinished projects through Continue import", async () => { + using _now = setup(); + appAtomRegistry.set(candidatesAtom(), [ + candidate("/projects/first"), + candidate("/projects/lonely", { threadCount: 1 }), + ]); + state.importThreads + .mockResolvedValueOnce(AsyncResult.success({ importedCount: 1, skippedCount: 0 })) + .mockResolvedValueOnce( + AsyncResult.success({ importedCount: 1, skippedCount: 3, deferredCount: 3 }), + ) + .mockResolvedValueOnce(AsyncResult.failure(Cause.fail(new Error("offline")))); + { + await using screen = await mountWelcome(); + using toastAdd = vi.spyOn(toastManager, "add"); + await click("Review all 2"); + await click("Select all"); + await click("Import 2 projects and start"); + await vi.waitFor(() => expect(state.onDone).toHaveBeenCalledOnce()); + const summary = toastAdd.mock.calls + .map(([toast]) => toast) + .find((toast) => toast.actionProps?.children === "Continue import"); + await act(async () => summary?.actionProps?.onClick?.(new MouseEvent("click") as never)); + expect(screen.router?.state.location.pathname).toBe("/welcome"); + } + + await using _reopened = await mountWelcome(); + await vi.waitFor(() => expect(pageText()).toContain("Import 1 project and their threads")); + await click("Import threads and start"); + await vi.waitFor(() => expect(state.importThreads).toHaveBeenCalledTimes(4)); + expect(state.importThreads).toHaveBeenLastCalledWith( + expect.objectContaining({ + input: expect.objectContaining({ expectedWorkspaceRoot: "/projects/lonely" }), + }), + ); + expect(state.createProject).toHaveBeenCalledTimes(2); +}); + +it("does not count a project's threads twice when Start runs its import again", async () => { + using _now = setup(); + appAtomRegistry.set(candidatesAtom(), [candidate("/projects/first")]); + state.importThreads.mockResolvedValue(AsyncResult.success({ importedCount: 1, skippedCount: 1 })); + state.completeOnboarding.mockRejectedValueOnce(new Error("offline")); + await using _screen = await mountWelcome(); + using _toastAdd = vi.spyOn(toastManager, "add"); + + await click("Import 1 project and start"); + await vi.waitFor(() => expect(button("Import threads and start").disabled).toBe(false)); + await click("Import threads and start"); + await vi.waitFor(() => expect(state.onDone).toHaveBeenCalledOnce()); + expect(state.importThreads).toHaveBeenCalledTimes(2); + expectImportSummary({ imported: 1, failed: 1, deferred: 0, stopped: false }, false); +}); + +it("does not reuse an earlier summary or resume when Start runs again without projects", async () => { + using _now = setup(); + appAtomRegistry.set(candidatesAtom(), [candidate("/projects/first")]); + state.importThreads + .mockResolvedValueOnce( + AsyncResult.success({ importedCount: 1, skippedCount: 3, deferredCount: 3 }), + ) + .mockResolvedValueOnce(AsyncResult.failure(Cause.fail(new Error("offline")))); + state.completeOnboarding.mockRejectedValueOnce(new Error("offline")); + { + await using _screen = await mountWelcome(); + using toastAdd = vi.spyOn(toastManager, "add"); + await click("Import 1 project and start"); + await vi.waitFor(() => expect(button("Import threads and start").disabled).toBe(false)); + const importProjects = document.querySelector('[role="checkbox"]'); + await act(async () => importProjects?.click()); + await click("Start"); + await vi.waitFor(() => expect(state.onDone).toHaveBeenCalledOnce()); + expect(toastAdd).not.toHaveBeenCalledWith( + expect.objectContaining({ title: "Some history was not imported" }), + ); + } + await using _reopened = await mountWelcome(); + expect(pageText()).toContain("Import projects and their threads"); +}); diff --git a/apps/web/src/components/onboarding/WelcomePage.tsx b/apps/web/src/components/onboarding/WelcomePage.tsx index 48171ccf94457..09fb9dc56d9f2 100644 --- a/apps/web/src/components/onboarding/WelcomePage.tsx +++ b/apps/web/src/components/onboarding/WelcomePage.tsx @@ -1,6 +1,7 @@ import { useAuth } from "@clerk/react"; import { useAtomValue } from "@effect/atom-react"; import type { + AgentSessionImportResult, AgentSessionProjectCandidate, EnvironmentId, ProjectId, @@ -27,7 +28,7 @@ import { ProviderDriverKind, ThreadId, } from "@t3tools/contracts"; -import { Link, useBlocker } from "@tanstack/react-router"; +import { Link, useBlocker, useNavigate } from "@tanstack/react-router"; import * as Schema from "effect/Schema"; import { CheckIcon, @@ -51,12 +52,15 @@ import { workspaceAreaProps } from "../../lib/workspaceFocus"; import { useT3ConnectAuthPrompt } from "../clerk/useT3ConnectAuthPrompt"; import { completeOnboarding } from "../../onboarding/firstRun"; import { + formatOnboardingImportProgress, + formatOnboardingImportWarning, groupOnboardingProjects, partitionOnboardingProjects, onboardingProjectKey, resolveOnboardingLandingProject, isNewOnboardingProject, resolveOnboardingProjectId, + shouldImportNextBatch, type OnboardingProjectGroup, splitOnboardingProjectPath, } from "../../onboarding/projectImport.logic"; @@ -165,8 +169,12 @@ export function WelcomePage({ toastManager.add({ id: "welcome-finishing", type: "info", - title: "Finishing setup", - description: "One moment.", + ...(projectImport.progress === null + ? { title: "Finishing setup", description: "One moment." } + : { + title: "Importing projects", + description: "Select Stop importing to finish after the current batch.", + }), }); return true; }, @@ -177,10 +185,12 @@ export function WelcomePage({ // Starting before the project list loads keeps saved picks, such as an import a quit cut short. const keepChoicesRef = useRef(false); const completionErrorToastIdRef = useRef | null>(null); + const navigate = useNavigate(); const finish = ( projectRef?: ScopedProjectRef, importWarning?: string, importedThreadCount = 0, + resume?: WelcomeImportSelection, ) => { if (finishingPromiseRef.current !== null) return finishingPromiseRef.current; if (completionErrorToastIdRef.current !== null) { @@ -198,13 +208,29 @@ export function WelcomePage({ // Release before onDone so its own navigation is not blocked. holdPageRef.current = false; await onDone(projectRef); - if (!keepChoicesRef.current) setImportSelection(null); + if (resume) setImportSelection(resume); + else if (!keepChoicesRef.current) setImportSelection(null); if (importWarning) { - toastManager.add({ + const toastId = toastManager.add({ type: "warning", title: "Some history was not imported", description: importWarning, timeout: 0, + ...(resume + ? { + actionProps: { + children: "Continue import", + onClick: () => { + toastManager.close(toastId); + void navigate( + isPrimary + ? { to: "/welcome" } + : { to: "/welcome", hash: `agents:${environmentId}` }, + ); + }, + }, + } + : {}), }); } else if (importedThreadCount > 0) { toastManager.add({ @@ -291,7 +317,15 @@ export function WelcomePage({ {showPhone ? : null}
- {agentReady ? ( + {projectImport.progress !== null ? ( + + ) : agentReady ? ( ) : ( ) : null}
+ {progress !== null ? ( +

+ {formatOnboardingImportProgress(progress)} +

+ ) : null} {reviewing && canWrite ? ( // The negative margin lines the list's checkboxes up with the one above it.
@@ -1376,12 +1416,12 @@ function useProjectImport({ projectRef?: ScopedProjectRef, importWarning?: string, importedThreadCount?: number, + resume?: WelcomeImportSelection, ) => Promise; }) { const writableEnvironments = useEnvironmentsWithScope(scans, AuthOrchestrationOperateScope); const createProject = useAtomCommand(projectEnvironment.create, { reportFailure: false }); const importThreads = useAtomCommand(agentSessionImport, { reportFailure: false }); - const importWarningRef = useRef(""); // Per project, so a retry that skips finished projects still counts their threads. const importedThreadsRef = useRef(new Map()); const importedProjectsRef = useRef(new Map()); @@ -1391,6 +1431,14 @@ function useProjectImport({ new Map(), ); const importGenerationRef = useRef(0); + const stopRequestedRef = useRef(false); + const [isStopping, setIsStopping] = useState(false); + const [progress, setProgress] = useState<{ + readonly title: string; + readonly position: number; + readonly total: number; + readonly batch: AgentSessionImportResult | null; + } | null>(null); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- bumps the import generation on mount and unmount so in-flight imports drop stale results useEffect(() => { @@ -1445,7 +1493,7 @@ function useProjectImport({ 0, ); - const finishAfterImport = () => { + const finishAfterImport = (importWarning = "", resume?: WelcomeImportSelection) => { const projectRef = resolveOnboardingLandingProject( lastImportSelectionRef.current, projectsWithImportedHistoryRef.current, @@ -1454,7 +1502,7 @@ function useProjectImport({ // Busy until setup is saved, so Start cannot run the import again meanwhile. setIsImporting(true); if (projectRef === undefined) { - void onDone(undefined, importWarningRef.current, importedThreadTotal()).then((completed) => { + void onDone(undefined, importWarning, importedThreadTotal(), resume).then((completed) => { if (!completed) setIsImporting(false); }); return; @@ -1473,7 +1521,7 @@ function useProjectImport({ holdPageRef.current = false; return; } - const completed = await onDone(landing, importWarningRef.current, importedThreadTotal()); + const completed = await onDone(landing, importWarning, importedThreadTotal(), resume); if (!completed) setIsImporting(false); }); }; @@ -1485,13 +1533,18 @@ function useProjectImport({ readEnvironmentScope(candidate.environmentId, AuthOrchestrationOperateScope), ); // The permission message shows from the session's scopes. - const stopForDeniedAccess = () => setIsImporting(false); + const stopForDeniedAccess = () => { + setIsImporting(false); + setProgress(null); + setIsStopping(false); + }; if (!hasAccess()) { stopForDeniedAccess(); return; } setIsImporting(true); - importWarningRef.current = ""; + stopRequestedRef.current = false; + setIsStopping(false); lastImportSelectionRef.current = selection.map((candidate) => candidate.key); const importGeneration = importGenerationRef.current; const importedThreads = importedThreadsRef.current; @@ -1501,18 +1554,30 @@ function useProjectImport({ importedProjects.size > 0 ? selection.filter((candidate) => importedProjects.has(candidate.key)).length : 0; - let skippedThreadCount = 0; + let failedThreadCount = 0; + let deferredThreadCount = 0; + let stopped = false; const refreshEnvironments = new Set(); // Resolves false when the run stopped early: a newer run, an unmount, or lost access. const importEach = async () => { - for (const candidate of selection) { + for (const [index, candidate] of selection.entries()) { const { environmentId } = candidate; if (importGeneration !== importGenerationRef.current) return false; + if (stopRequestedRef.current) { + stopped = true; + return true; + } if (!hasAccess()) { stopForDeniedAccess(); return false; } if (importedProjects.has(candidate.key)) continue; + const projectProgress = { + title: candidate.title, + position: index + 1, + total: selection.length, + }; + setProgress({ ...projectProgress, batch: null }); let projectId = resolveOnboardingProjectId(readProjects(), environmentId, candidate); if (projectId === null) { let attempt = projectAttempts.get(candidate.key); @@ -1546,34 +1611,52 @@ function useProjectImport({ } } - if (!hasAccess()) { - stopForDeniedAccess(); - return false; + let previousBatch: AgentSessionImportResult | undefined; + let lastBatch: AgentSessionImportResult | undefined; + let stalled = false; + while (true) { + if (stopRequestedRef.current) { + stopped = true; + break; + } + if (!hasAccess()) { + stopForDeniedAccess(); + return false; + } + const threadImportResult = await importThreads({ + environmentId, + input: { projectId, expectedWorkspaceRoot: candidate.path }, + }); + if (importGeneration !== importGenerationRef.current) return false; + if (threadImportResult._tag !== "Success") { + if (!isAtomCommandInterrupted(threadImportResult)) { + projectAttempts.delete(candidate.key); + refreshEnvironments.add(environmentId); + } + break; + } + previousBatch = lastBatch; + lastBatch = threadImportResult.value; + setProgress({ ...projectProgress, batch: lastBatch }); + if (!shouldImportNextBatch(previousBatch, lastBatch)) { + stalled = (lastBatch.deferredCount ?? 0) > 0; + break; + } } - const threadImportResult = await importThreads({ - environmentId, - input: { projectId, expectedWorkspaceRoot: candidate.path }, - }); - if (importGeneration !== importGenerationRef.current) return false; - if (threadImportResult._tag === "Success") { - importedThreads.set( + if (lastBatch === undefined) continue; + const deferred = stalled ? 0 : (lastBatch.deferredCount ?? 0); + importedThreads.set(candidate.key, lastBatch.importedCount); + deferredThreadCount += deferred; + failedThreadCount += lastBatch.skippedCount - deferred; + if (lastBatch.importedCount > 0) { + projectsWithImportedHistoryRef.current.set( candidate.key, - (importedThreads.get(candidate.key) ?? 0) + threadImportResult.value.importedCount, + scopeProjectRef(environmentId, projectId), ); - skippedThreadCount += threadImportResult.value.skippedCount; - if (threadImportResult.value.importedCount > 0) { - projectsWithImportedHistoryRef.current.set( - candidate.key, - scopeProjectRef(environmentId, projectId), - ); - } - if (threadImportResult.value.skippedCount === 0) { - importedProjectsCount += 1; - importedProjects.set(candidate.key, scopeProjectRef(environmentId, projectId)); - } - } else if (!isAtomCommandInterrupted(threadImportResult)) { - projectAttempts.delete(candidate.key); - refreshEnvironments.add(environmentId); + } + if (lastBatch.skippedCount === 0) { + importedProjectsCount += 1; + importedProjects.set(candidate.key, scopeProjectRef(environmentId, projectId)); } } return true; @@ -1587,19 +1670,33 @@ function useProjectImport({ if (refreshEnvironments.has(scan.environmentId)) scan.refresh(); } setIsImporting(false); - const importedThreadCount = importedThreadTotal(); - if (importedProjectsCount < selection.length) { - if (importedThreadCount > 0 && skippedThreadCount > 0) { - importWarningRef.current = `Imported ${importedThreadCount} ${importedThreadCount === 1 ? "thread" : "threads"}. ${skippedThreadCount} ${skippedThreadCount === 1 ? "thread" : "threads"} could not be imported.`; - } else if (skippedThreadCount > 0) { - importWarningRef.current = `${skippedThreadCount} ${skippedThreadCount === 1 ? "thread could" : "threads could"} not be imported.`; - } else if (importedThreadCount > 0) { - importWarningRef.current = `Imported ${importedThreadCount} ${importedThreadCount === 1 ? "thread" : "threads"}. Some thread history could not be imported.`; - } else { - importWarningRef.current = "Could not import thread history."; - } + setProgress(null); + setIsStopping(false); + if (importedProjectsCount === selection.length) { + finishAfterImport(); + return; } - finishAfterImport(); + const unfinished = selection.filter((candidate) => !importedProjects.has(candidate.key)); + finishAfterImport( + formatOnboardingImportWarning({ + imported: importedThreadTotal(), + failed: failedThreadCount, + deferred: deferredThreadCount, + stopped, + }), + unfinished.length > 0 && (stopped || deferredThreadCount > 0) + ? { + knownKeys: candidates.map((candidate) => candidate.key), + keys: unfinished.map((candidate) => candidate.key), + optedOut: false, + } + : undefined, + ); + }; + + const stopImport = () => { + stopRequestedRef.current = true; + setIsStopping(true); }; return { @@ -1613,6 +1710,9 @@ function useProjectImport({ visibleImportError, runImport, finishAfterImport, + progress, + isStopping, + stopImport, }; } diff --git a/apps/web/src/onboarding/projectImport.logic.test.ts b/apps/web/src/onboarding/projectImport.logic.test.ts index 10cb0d731ee75..d363df435d413 100644 --- a/apps/web/src/onboarding/projectImport.logic.test.ts +++ b/apps/web/src/onboarding/projectImport.logic.test.ts @@ -1,13 +1,21 @@ -import { EnvironmentId, ProjectId, type AgentSessionProjectCandidate } from "@t3tools/contracts"; +import { + EnvironmentId, + ProjectId, + type AgentSessionImportResult, + type AgentSessionProjectCandidate, +} from "@t3tools/contracts"; import { describe, expect, it } from "vite-plus/test"; import { + formatOnboardingImportProgress, + formatOnboardingImportWarning, groupOnboardingProjects, isNewOnboardingProject, partitionOnboardingProjects, onboardingProjectKey, resolveOnboardingLandingProject, resolveOnboardingProjectId, + shouldImportNextBatch, } from "./projectImport.logic"; const now = Date.parse("2026-08-22T12:00:00.000Z"); @@ -252,3 +260,75 @@ describe("projects on multiple computers", () => { expect(partitionOnboardingProjects([first, second], now).recent).toEqual([first, second]); }); }); + +describe("shouldImportNextBatch", () => { + const batch = (skippedCount: number, deferredCount?: number): AgentSessionImportResult => ({ + importedCount: 1, + skippedCount, + ...(deferredCount === undefined ? {} : { deferredCount }), + }); + + it.each([ + ["old server without deferredCount", batch(5), batch(5), false], + ["first batch with deferred threads", undefined, batch(5, 5), true], + ["deferred threads shrinking", batch(5, 5), batch(3, 3), true], + ["deferred threads unchanged", batch(5, 5), batch(5, 5), false], + ["nothing deferred", batch(5, 5), batch(2, 0), false], + ["first batch with nothing deferred", undefined, batch(0, 0), false], + ] as const)("%s", (_name, previous, current, expected) => { + expect(shouldImportNextBatch(previous, current)).toBe(expected); + }); +}); + +describe("formatOnboardingImportProgress", () => { + const project = { title: "web", position: 2, total: 3 }; + + it.each([ + [{ ...project, total: 1, batch: null }, "Importing web…"], + [{ ...project, batch: null }, "Importing web (2 of 3)…"], + [ + { ...project, batch: { importedCount: 100, skippedCount: 153, deferredCount: 153 } }, + "Importing web (2 of 3)… 100 threads imported, 153 left", + ], + [ + { ...project, batch: { importedCount: 1, skippedCount: 0, deferredCount: 0 } }, + "Importing web (2 of 3)… 1 thread imported", + ], + [ + { ...project, batch: { importedCount: 4, skippedCount: 2 } }, + "Importing web (2 of 3)… 4 threads imported", + ], + ] as const)("%j", (input, expected) => { + expect(formatOnboardingImportProgress(input)).toBe(expected); + }); +}); + +describe("formatOnboardingImportWarning", () => { + it.each([ + [ + { imported: 3, failed: 2, deferred: 0, stopped: false }, + "Imported 3 threads. 2 threads could not be imported.", + ], + [{ imported: 0, failed: 1, deferred: 0, stopped: false }, "1 thread could not be imported."], + [ + { imported: 1, failed: 0, deferred: 0, stopped: false }, + "Imported 1 thread. Some thread history could not be imported.", + ], + [{ imported: 0, failed: 0, deferred: 0, stopped: false }, "Could not import thread history."], + [ + { imported: 4, failed: 0, deferred: 1, stopped: false }, + "Imported 4 threads. 1 thread was not imported yet.", + ], + [ + { imported: 4, failed: 2, deferred: 3, stopped: false }, + "Imported 4 threads. 2 threads could not be imported. 3 threads were not imported yet.", + ], + [ + { imported: 4, failed: 0, deferred: 3, stopped: true }, + "Import stopped. Imported 4 threads. 3 threads were not imported yet.", + ], + [{ imported: 0, failed: 0, deferred: 0, stopped: true }, "Import stopped."], + ] as const)("%j", (input, expected) => { + expect(formatOnboardingImportWarning(input)).toBe(expected); + }); +}); diff --git a/apps/web/src/onboarding/projectImport.logic.ts b/apps/web/src/onboarding/projectImport.logic.ts index 1be9c7b824b2a..c99501d294ac6 100644 --- a/apps/web/src/onboarding/projectImport.logic.ts +++ b/apps/web/src/onboarding/projectImport.logic.ts @@ -2,7 +2,12 @@ import { findProjectByPath, normalizeProjectPathForComparison, } from "@t3tools/client-runtime/state/projects"; -import type { AgentSessionProjectCandidate, EnvironmentId, ProjectId } from "@t3tools/contracts"; +import type { + AgentSessionImportResult, + AgentSessionProjectCandidate, + EnvironmentId, + ProjectId, +} from "@t3tools/contracts"; const RECENT_PROJECT_WINDOW_MS = 30 * 24 * 60 * 60 * 1000; const DEFAULT_SELECTION_MIN_THREADS = 3; @@ -163,3 +168,59 @@ export function splitOnboardingProjectPath(path: string): { export function onboardingProjectKey(environmentId: EnvironmentId, path: string): string { return JSON.stringify([environmentId, path]); } + +/** Servers that omit deferredCount get one batch; the strict decrease guarantees the loop ends. */ +export function shouldImportNextBatch( + previous: AgentSessionImportResult | undefined, + current: AgentSessionImportResult, +): boolean { + const deferred = current.deferredCount ?? 0; + return deferred > 0 && (previous === undefined || deferred < (previous.deferredCount ?? 0)); +} + +export function formatOnboardingImportProgress({ + title, + position, + total, + batch, +}: { + readonly title: string; + readonly position: number; + readonly total: number; + readonly batch: AgentSessionImportResult | null; +}): string { + const label = `Importing ${title}${total > 1 ? ` (${position} of ${total})` : ""}…`; + if (batch === null) return label; + const imported = `${batch.importedCount} ${batch.importedCount === 1 ? "thread" : "threads"} imported`; + const left = batch.deferredCount ?? 0; + return `${label} ${imported}${left > 0 ? `, ${left} left` : ""}`; +} + +export function formatOnboardingImportWarning({ + imported, + failed, + deferred, + stopped, +}: { + readonly imported: number; + readonly failed: number; + readonly deferred: number; + readonly stopped: boolean; +}): string { + const threads = (count: number) => `${count} ${count === 1 ? "thread" : "threads"}`; + const sentences: Array = []; + if (stopped) sentences.push("Import stopped."); + if (imported > 0) sentences.push(`Imported ${threads(imported)}.`); + if (failed > 0) sentences.push(`${threads(failed)} could not be imported.`); + if (deferred > 0) { + sentences.push(`${threads(deferred)} ${deferred === 1 ? "was" : "were"} not imported yet.`); + } + if (!stopped && failed === 0 && deferred === 0) { + sentences.push( + imported > 0 + ? "Some thread history could not be imported." + : "Could not import thread history.", + ); + } + return sentences.join(" "); +} diff --git a/docs/user/welcome-page.md b/docs/user/welcome-page.md index f280ace079147..238c97cd5d915 100644 --- a/docs/user/welcome-page.md +++ b/docs/user/welcome-page.md @@ -101,10 +101,14 @@ does not count toward that limit. It skips a conversation with more than 100,000 records every time, so opening the page again does not import it. It ignores malformed records and skips unreadable or unparseable conversations. -Each import attempt reads up to 100 conversation files and 4 GiB per project. To -continue a large batch, open the Welcome page again and pick the project in -**Review all**. Completed conversations are not imported again. You can continue -without the remaining history. +Import works through each project in batches of up to 100 conversation files and +4 GiB, and starts the next batch until nothing is left. If a batch makes no +progress, it stops and counts what is left as not imported. Completed +conversations are not imported again. Select **Stop importing** to start with what +is imported after the current batch. **Continue import** on the summary opens the +Welcome page again with the unfinished projects checked, and they stay checked +when you open it from **Settings → About**. A computer running an older T3 Code +version imports one batch per run. You can start without importing. While an import runs, T3 Code stays on the Welcome page. If you quit, open the Welcome page again from **Settings → About**; the diff --git a/packages/contracts/src/agentSessions.ts b/packages/contracts/src/agentSessions.ts index 93e0e36e4fbd4..c82e693eb5661 100644 --- a/packages/contracts/src/agentSessions.ts +++ b/packages/contracts/src/agentSessions.ts @@ -73,8 +73,12 @@ export class AgentSessionImportProjectChangedError extends Schema.TaggedError