TypeScript
feat(web): first-run welcome wizard with agent setup and project import (#5362)
Theo Browne09aac71563c6
diff --git a/apps/desktop/src/settings/DesktopClientSettings.diagnostics.test.ts b/apps/desktop/src/settings/DesktopClientSettings.diagnostics.test.tsindex 5034df44cf7..d2fd166e878 100644--- a/apps/desktop/src/settings/DesktopClientSettings.diagnostics.test.ts+++ b/apps/desktop/src/settings/DesktopClientSettings.diagnostics.test.ts@@ -54,7 +54,7 @@ const readWithLogs = (fileSystemLayer: Layer.Layer<FileSystem.FileSystem>) => {const environment = yield* DesktopEnvironment.DesktopEnvironment;const settings = yield* DesktopClientSettings.DesktopClientSettings;return {- result: yield* settings.get,+ result: yield* Effect.result(settings.get),settingsPath: environment.clientSettingsPath,records,};@@ -73,12 +73,13 @@ describe("DesktopClientSettings diagnostics", () => {Effect.gen(function* () {const result = yield* readWithLogs(FileSystem.layerNoop({}));- assert.isTrue(Option.isNone(result.result));+ if (result.result._tag !== "Success") return assert.fail("expected a successful read");+ assert.isTrue(Option.isNone(result.result.success));assert.deepEqual(result.records, []);}),);- it.effect("logs non-missing filesystem failures with the settings path", () => {+ it.effect("reports non-missing filesystem failures and logs the settings path", () => {const permissionError = PlatformError.systemError({_tag: "PermissionDenied",module: "FileSystem",@@ -93,7 +94,12 @@ describe("DesktopClientSettings diagnostics", () => {}),);- assert.isTrue(Option.isNone(result.result));+ if (result.result._tag !== "Failure") return assert.fail("expected a read failure");+ assert.instanceOf(+ result.result.failure,+ DesktopClientSettings.DesktopClientSettingsReadError,+ );+ assert.strictEqual(result.result.failure.cause, permissionError);assert.equal(result.records.length, 1);assert.deepEqual(result.records[0]?.message, ["Could not read desktop client settings.",@@ -103,7 +109,7 @@ describe("DesktopClientSettings diagnostics", () => {});});- it.effect("logs malformed settings documents with the settings path", () =>+ it.effect("reports malformed settings documents and logs the settings path", () =>Effect.gen(function* () {const result = yield* readWithLogs(FileSystem.layerNoop({@@ -111,7 +117,12 @@ describe("DesktopClientSettings diagnostics", () => {}),);- assert.isTrue(Option.isNone(result.result));+ if (result.result._tag !== "Failure") return assert.fail("expected a decode failure");+ assert.instanceOf(+ result.result.failure,+ DesktopClientSettings.DesktopClientSettingsReadError,+ );+ assert.equal(result.result.failure.operation, "decode-document");assert.equal(result.records.length, 1);const message = result.records[0]?.message;if (!Array.isArray(message)) {diff --git a/apps/desktop/src/settings/DesktopClientSettings.test.ts b/apps/desktop/src/settings/DesktopClientSettings.test.tsindex 0d9ddc8fde9..9fbacc832a9 100644--- a/apps/desktop/src/settings/DesktopClientSettings.test.ts+++ b/apps/desktop/src/settings/DesktopClientSettings.test.ts@@ -44,6 +44,7 @@ const clientSettings: ClientSettings = {fontSizeTerminal: 12,fontSmoothing: true,glassOpacity: 80,+ onboardingCompletedAt: null,panelAnimationDurationMs: 0,planModeEnabled: false,proactivePanelsEnabled: true,@@ -136,6 +137,59 @@ describe("DesktopClientSettings", () => {),);+ for (const failure of [+ { label: "permission", reason: "PermissionDenied" },+ { label: "I/O", reason: "Unknown" },+ ] as const) {+ it.effect(`preserves saved preferences across ${failure.label} read failures and retries`, () =>+ withClientSettings(+ Effect.gen(function* () {+ const environment = yield* DesktopEnvironment.DesktopEnvironment;+ const fileSystem = yield* FileSystem.FileSystem;+ const settings = yield* DesktopClientSettings.DesktopClientSettings;+ const savedSettings = {+ ...clientSettings,+ onboardingCompletedAt: "2026-09-05T12:00:00.000Z",+ };+ yield* settings.set(savedSettings);+ const savedContents = yield* fileSystem.readFileString(environment.clientSettingsPath);+ const cause = PlatformError.systemError({+ _tag: failure.reason,+ module: "FileSystem",+ method: "readFileString",+ pathOrDescriptor: environment.clientSettingsPath,+ });+ let failRead = true;+ const retryableSettings = yield* DesktopClientSettings.make.pipe(+ Effect.provideService(+ FileSystem.FileSystem,+ FileSystem.FileSystem.of({+ ...fileSystem,+ readFileString: (path) =>+ Effect.suspend(() =>+ failRead ? Effect.fail(cause) : fileSystem.readFileString(path),+ ),+ }),+ ),+ );++ const error = yield* retryableSettings.get.pipe(Effect.flip);+ assert.instanceOf(error, DesktopClientSettings.DesktopClientSettingsReadError);+ assert.equal(error.operation, "read-file");+ assert.equal(error.path, environment.clientSettingsPath);+ assert.strictEqual(error.cause, cause);+ assert.equal(+ yield* fileSystem.readFileString(environment.clientSettingsPath),+ savedContents,+ );++ failRead = false;+ assert.deepEqual(yield* retryableSettings.get, Option.some(savedSettings));+ }),+ ),+ );+ }+it.effect("reports the failed client settings write operation and path", () =>withClientSettings(Effect.gen(function* () {@@ -222,17 +276,31 @@ describe("DesktopClientSettings", () => {),);- it.effect("treats malformed client settings documents as absent", () =>- withClientSettings(- Effect.gen(function* () {- const environment = yield* DesktopEnvironment.DesktopEnvironment;- const fileSystem = yield* FileSystem.FileSystem;- const settings = yield* DesktopClientSettings.DesktopClientSettings;- yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });- yield* fileSystem.writeFileString(environment.clientSettingsPath, "{not-json");+ for (const document of [+ { label: "malformed JSON", contents: "{not-json" },+ { label: "invalid direct settings", contents: '{"fontSizeCode":"large"}' },+ { label: "invalid legacy settings", contents: '{"settings":{"fontSizeCode":"large"}}' },+ ]) {+ it.effect(`reports ${document.label} without treating the settings file as absent`, () =>+ withClientSettings(+ Effect.gen(function* () {+ const environment = yield* DesktopEnvironment.DesktopEnvironment;+ const fileSystem = yield* FileSystem.FileSystem;+ const settings = yield* DesktopClientSettings.DesktopClientSettings;+ yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });+ yield* fileSystem.writeFileString(environment.clientSettingsPath, document.contents);- assert.isTrue(Option.isNone(yield* settings.get));- }),- ),- );+ const error = yield* settings.get.pipe(Effect.flip);+ assert.instanceOf(error, DesktopClientSettings.DesktopClientSettingsReadError);+ assert.equal(error.operation, "decode-document");+ assert.equal(error.path, environment.clientSettingsPath);+ assert.instanceOf(error.cause, Schema.SchemaError);+ assert.equal(+ yield* fileSystem.readFileString(environment.clientSettingsPath),+ document.contents,+ );+ }),+ ),+ );+ }});diff --git a/apps/desktop/src/settings/DesktopClientSettings.ts b/apps/desktop/src/settings/DesktopClientSettings.tsindex 4ff091e27a2..5eadd27d545 100644--- a/apps/desktop/src/settings/DesktopClientSettings.ts+++ b/apps/desktop/src/settings/DesktopClientSettings.ts@@ -12,25 +12,33 @@ import * as Ref from "effect/Ref";import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";-const ClientSettingsDocumentSchema = Schema.Struct({- settings: ClientSettingsSchema,-});-const ClientSettingsJson = fromLenientJson(ClientSettingsSchema);-const LegacyClientSettingsDocumentJson = fromLenientJson(ClientSettingsDocumentSchema);-const decodeLegacyClientSettingsDocumentJson = Schema.decodeEffect(- LegacyClientSettingsDocumentJson,+const decodeClientSettingsDocument = Schema.decodeEffect(+ fromLenientJson(Schema.Record(Schema.String, Schema.Unknown)),);-const decodeClientSettingsJsonValue = Schema.decodeEffect(ClientSettingsJson);-const decodeClientSettingsJson = (raw: string): Effect.Effect<ClientSettings, Schema.SchemaError> =>- decodeLegacyClientSettingsDocumentJson(raw).pipe(- Effect.map((document) => document.settings),- Effect.catchTags({- SchemaError: () => decodeClientSettingsJsonValue(raw),- }),+const decodeClientSettingsValue = Schema.decodeUnknownEffect(ClientSettingsSchema);+const decodeClientSettingsJson = Effect.fnUntraced(function* (raw: string) {+ const document = yield* decodeClientSettingsDocument(raw);+ // Select the shape before validation so invalid legacy settings cannot become defaults.+ return yield* decodeClientSettingsValue(+ Object.hasOwn(document, "settings") ? document.settings : document,);+});const encodeClientSettingsJson = Schema.encodeEffect(ClientSettingsJson);+export class DesktopClientSettingsReadError extends Schema.TaggedErrorClass<DesktopClientSettingsReadError>()(+ "DesktopClientSettingsReadError",+ {+ operation: Schema.Literals(["read-file", "decode-document"]),+ path: Schema.String,+ cause: Schema.Defect(),+ },+) {+ override get message(): string {+ return `Desktop client settings read failed during ${this.operation} at ${this.path}.`;+ }+}+const DesktopClientSettingsWriteOperation = Schema.Literals(["create-temporary-file-name","encode-document",@@ -55,7 +63,7 @@ export class DesktopClientSettingsWriteError extends Schema.TaggedErrorClass<Desexport class DesktopClientSettings extends Context.Service<DesktopClientSettings,{- readonly get: Effect.Effect<Option.Option<ClientSettings>>;+ readonly get: Effect.Effect<Option.Option<ClientSettings>, DesktopClientSettingsReadError>;readonly set: (settings: ClientSettings,) => Effect.Effect<void, DesktopClientSettingsWriteError>;@@ -65,7 +73,7 @@ export class DesktopClientSettings extends Context.Service<const readClientSettings = (fileSystem: FileSystem.FileSystem,settingsPath: string,-): Effect.Effect<Option.Option<ClientSettings>> =>+): Effect.Effect<Option.Option<ClientSettings>, DesktopClientSettingsReadError> =>fileSystem.readFileString(settingsPath).pipe(Effect.map(Option.some),Effect.catchTags({@@ -74,7 +82,15 @@ const readClientSettings = (? Effect.succeed(Option.none<string>()): Effect.logWarning("Could not read desktop client settings.", cause).pipe(Effect.annotateLogs({ settingsPath }),- Effect.as(Option.none<string>()),+ Effect.andThen(+ Effect.fail(+ new DesktopClientSettingsReadError({+ operation: "read-file",+ path: settingsPath,+ cause,+ }),+ ),+ ),),}),Effect.flatMap(@@ -87,7 +103,15 @@ const readClientSettings = (SchemaError: (cause) =>Effect.logWarning("Could not decode desktop client settings.", cause).pipe(Effect.annotateLogs({ settingsPath }),- Effect.as(Option.none<ClientSettings>()),+ Effect.andThen(+ Effect.fail(+ new DesktopClientSettingsReadError({+ operation: "decode-document",+ path: settingsPath,+ cause,+ }),+ ),+ ),),}),),diff --git a/apps/server/src/auth/RpcAuthorization.test.ts b/apps/server/src/auth/RpcAuthorization.test.tsindex 25971b0c0ae..7262239577b 100644--- a/apps/server/src/auth/RpcAuthorization.test.ts+++ b/apps/server/src/auth/RpcAuthorization.test.ts@@ -43,6 +43,15 @@ describe("RPC authorization scopes", () => {);});+ it("requires write access to import agent session history", () => {+ expect(requiredScopeForRpcMethod(WS_METHODS.agentSessionsScan)).toBe(+ AuthOrchestrationReadScope,+ );+ expect(requiredScopeForRpcMethod(WS_METHODS.agentSessionsImport)).toBe(+ AuthOrchestrationOperateScope,+ );+ });+it("reads the reviewer menu under the same scope as the pull request it belongs to", () => {// The candidate list is a read like the detail beside it, and asking somebody for a review is// a write like every other pull request operation.diff --git a/apps/server/src/auth/RpcAuthorization.ts b/apps/server/src/auth/RpcAuthorization.tsindex de6661f4588..7bd1ed6c45f 100644--- a/apps/server/src/auth/RpcAuthorization.ts+++ b/apps/server/src/auth/RpcAuthorization.ts@@ -99,6 +99,8 @@ export const RPC_REQUIRED_SCOPES = {[WS_METHODS.projectsWriteFile]: AuthOrchestrationOperateScope,[WS_METHODS.shellOpenInEditor]: AuthOrchestrationOperateScope,[WS_METHODS.filesystemBrowse]: AuthOrchestrationReadScope,+ [WS_METHODS.agentSessionsScan]: AuthOrchestrationReadScope,+ [WS_METHODS.agentSessionsImport]: AuthOrchestrationOperateScope,[WS_METHODS.assetsCreateUrl]: AuthOrchestrationReadScope,[WS_METHODS.attachmentsCreateUploadUrl]: AuthOrchestrationOperateScope,[WS_METHODS.attachmentsDelete]: AuthOrchestrationOperateScope,diff --git a/apps/server/src/checkpointing/CheckpointDiffQuery.test.ts b/apps/server/src/checkpointing/CheckpointDiffQuery.test.tsindex c8b143f87eb..d05ca5ec854 100644--- a/apps/server/src/checkpointing/CheckpointDiffQuery.test.ts+++ b/apps/server/src/checkpointing/CheckpointDiffQuery.test.ts@@ -90,6 +90,7 @@ describe("CheckpointDiffQuery.layer", () => {getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),getProjectShellById: () => Effect.succeed(Option.none()),getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),+ getImportedAgentSessionSources: () => Effect.die("unused"),getThreadCheckpointContext: () =>Effect.sync(() => {getThreadCheckpointContextCalls += 1;@@ -202,6 +203,7 @@ describe("CheckpointDiffQuery.layer", () => {getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),getProjectShellById: () => Effect.succeed(Option.none()),getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),+ getImportedAgentSessionSources: () => Effect.die("unused"),getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),getFullThreadDiffContext: () => Effect.die("unused"),getThreadRuntimeContext: () => Effect.die("unused"),@@ -289,6 +291,7 @@ describe("CheckpointDiffQuery.layer", () => {getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),getProjectShellById: () => Effect.succeed(Option.none()),getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),+ getImportedAgentSessionSources: () => Effect.die("unused"),getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),getFullThreadDiffContext: () => Effect.die("unused"),getThreadRuntimeContext: () => Effect.die("unused"),@@ -361,6 +364,7 @@ describe("CheckpointDiffQuery.layer", () => {getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),getProjectShellById: () => Effect.succeed(Option.none()),getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),+ getImportedAgentSessionSources: () => Effect.die("unused"),getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),getFullThreadDiffContext: () => Effect.die("unused"),getThreadRuntimeContext: () => Effect.die("unused"),@@ -418,6 +422,7 @@ describe("CheckpointDiffQuery.layer", () => {getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),getProjectShellById: () => Effect.succeed(Option.none()),getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),+ getImportedAgentSessionSources: () => Effect.die("unused"),getThreadCheckpointContext: () => Effect.succeed(Option.none()),getFullThreadDiffContext: () => Effect.succeed(Option.none()),getThreadRuntimeContext: () => Effect.die("unused"),diff --git a/apps/server/src/orchestration/Layers/CheckpointReactor.test.ts b/apps/server/src/orchestration/Layers/CheckpointReactor.test.tsindex cb1725882fb..1dae23cdccb 100644--- a/apps/server/src/orchestration/Layers/CheckpointReactor.test.ts+++ b/apps/server/src/orchestration/Layers/CheckpointReactor.test.ts@@ -1342,6 +1342,36 @@ describe("CheckpointReactor", () => {).toBe("v1\n");});+ it("does not create checkpoints while importing historical user messages", async () => {+ const harness = await createHarness({+ hasSession: false,+ seedFilesystemCheckpoints: false,+ threadWorktreePath: null,+ });+ if (runtime === null) throw new Error("Checkpoint test runtime was not initialized.");++ await runtime.runPromise(+ harness.engine.dispatch({+ type: "thread.history.import",+ commandId: CommandId.make("cmd-import-history-without-checkpoint"),+ threadId: ThreadId.make("thread-1"),+ messages: [+ {+ messageId: MessageId.make("imported-user-message"),+ role: "user",+ text: "A message from an existing agent session",+ createdAt: "2026-01-01T00:00:00.000Z",+ },+ ],+ }),+ );+ await harness.drain();++ expect(+ gitRefExists(harness.cwd, checkpointRefForThreadTurn(ThreadId.make("thread-1"), 0)),+ ).toBe(false);+ });+it("captures turn completion checkpoint from project workspace root when provider session cwd is unavailable", async () => {const harness = await createHarness({hasSession: false,diff --git a/apps/server/src/orchestration/Layers/CheckpointReactor.ts b/apps/server/src/orchestration/Layers/CheckpointReactor.tsindex de1cb29c5cd..0331b0141fb 100644--- a/apps/server/src/orchestration/Layers/CheckpointReactor.ts+++ b/apps/server/src/orchestration/Layers/CheckpointReactor.ts@@ -615,6 +615,7 @@ const make = Effect.gen(function* () {) {if (event.type === "thread.message-sent") {if (+ event.metadata.historyImport === true ||event.payload.role !== "user" ||event.payload.streaming ||event.payload.turnId !== nulldiff --git a/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts b/apps/server/src/orchestration/Layers/OrchestrationEngine.test.tsindex 866e56ac037..abfb49b5305 100644--- a/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts+++ b/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts@@ -418,6 +418,7 @@ describe("OrchestrationEngine", () => {getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),getProjectShellById: () => Effect.succeed(Option.none()),getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),+ getImportedAgentSessionSources: () => Effect.die("unused"),getThreadCheckpointContext: () => Effect.succeed(Option.none()),getFullThreadDiffContext: () => Effect.succeed(Option.none()),getThreadRuntimeContext: () => Effect.die("unused"),diff --git a/apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts b/apps/server/src/orchestration/Layers/ProjectionPipeline.test.tsindex 0249844864f..d81d9b11b1f 100644--- a/apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts+++ b/apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts@@ -108,6 +108,102 @@ it.layer(Layer.fresh(makeProjectionPipelinePrefixedTestLayer("t3-projection-curs},);+it.layer(Layer.fresh(makeProjectionPipelinePrefixedTestLayer("t3-import-shell-")))(+ "imported thread shell projection",+ (it) => {+ it.effect("does not mark imported user messages as queued work in thread shells", () =>+ Effect.gen(function* () {+ const projectionPipeline = yield* OrchestrationProjectionPipeline;+ const eventStore = yield* OrchestrationEventStore;+ const sql = yield* SqlClient.SqlClient;+ const createdAt = "2026-08-24T10:00:00.000Z";+ const threadId = ThreadId.make("import:codex:shell-session");++ yield* eventStore.append({+ type: "thread.created",+ eventId: EventId.make("evt-import-shell-thread"),+ aggregateKind: "thread",+ aggregateId: threadId,+ occurredAt: createdAt,+ commandId: CommandId.make("cmd-import-shell-thread"),+ causationEventId: null,+ correlationId: CommandId.make("cmd-import-shell-thread"),+ metadata: {},+ payload: {+ threadId,+ projectId: ProjectId.make("project-import-shell"),+ title: "Imported thread",+ modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },+ runtimeMode: "full-access",+ interactionMode: "default",+ branch: null,+ worktreePath: null,+ createdAt,+ updatedAt: createdAt,+ },+ });+ yield* eventStore.append({+ type: "thread.message-sent",+ eventId: EventId.make("evt-import-shell-message"),+ aggregateKind: "thread",+ aggregateId: threadId,+ occurredAt: createdAt,+ commandId: CommandId.make("cmd-import-shell-message"),+ causationEventId: null,+ correlationId: CommandId.make("cmd-import-shell-message"),+ metadata: { historyImport: true },+ payload: {+ threadId,+ messageId: MessageId.make("import:codex:shell-session:0"),+ role: "user",+ text: "Imported user prompt",+ turnId: null,+ streaming: false,+ createdAt,+ updatedAt: createdAt,+ },+ });++ yield* projectionPipeline.bootstrap;++ const readLatestUserMessageAt = sql<{ readonly latestUserMessageAt: string | null }>`+ SELECT latest_user_message_at AS "latestUserMessageAt"+ FROM projection_threads+ WHERE thread_id = ${threadId}+ `;+ assert.deepEqual(yield* readLatestUserMessageAt, [{ latestUserMessageAt: null }]);++ const sessionEvent = yield* eventStore.append({+ type: "thread.session-set",+ eventId: EventId.make("evt-import-shell-session"),+ aggregateKind: "thread",+ aggregateId: threadId,+ occurredAt: createdAt,+ commandId: CommandId.make("cmd-import-shell-session"),+ causationEventId: null,+ correlationId: CommandId.make("cmd-import-shell-session"),+ metadata: {},+ payload: {+ threadId,+ session: {+ threadId,+ status: "ready",+ providerName: "codex",+ providerInstanceId: ProviderInstanceId.make("codex"),+ runtimeMode: "full-access",+ activeTurnId: null,+ lastError: null,+ updatedAt: createdAt,+ },+ },+ });+ yield* projectionPipeline.projectEvent(sessionEvent);+ assert.deepEqual(yield* readLatestUserMessageAt, [{ latestUserMessageAt: null }]);+ }),+ );+ },+);+it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {it.effect("bootstraps all projection states and writes projection rows", () =>Effect.gen(function* () {diff --git a/apps/server/src/orchestration/Layers/ProjectionPipeline.ts b/apps/server/src/orchestration/Layers/ProjectionPipeline.tsindex cd028d50223..b338596993d 100644--- a/apps/server/src/orchestration/Layers/ProjectionPipeline.ts+++ b/apps/server/src/orchestration/Layers/ProjectionPipeline.ts@@ -1,10 +1,12 @@import {ApprovalRequestId,+ isImportedAgentSessionMessageId,type ChatAttachment,type OrchestrationEvent,type OrchestrationSessionStatus,ThreadId,} from "@t3tools/contracts";+import { compareDateTimeStrings } from "@t3tools/shared/dateTime";import * as Effect from "effect/Effect";import * as FileSystem from "effect/FileSystem";import * as Layer from "effect/Layer";@@ -248,7 +250,7 @@ function retainProjectionMessagesAfterRevert(}for (const message of messages) {- if (message.role === "system") {+ if (message.role === "system" || isImportedAgentSessionMessageId(message.messageId)) {retainedMessageIds.add(message.messageId);continue;}@@ -258,7 +260,10 @@ function retainProjectionMessagesAfterRevert(}const retainedUserCount = messages.filter(- (message) => message.role === "user" && retainedMessageIds.has(message.messageId),+ (message) =>+ message.role === "user" &&+ !isImportedAgentSessionMessageId(message.messageId) &&+ retainedMessageIds.has(message.messageId),).length;const missingUserCount = Math.max(0, turnCount - retainedUserCount);if (missingUserCount > 0) {@@ -271,7 +276,7 @@ function retainProjectionMessagesAfterRevert().toSorted((left, right) =>- left.createdAt.localeCompare(right.createdAt) ||+ compareDateTimeStrings(left.createdAt, right.createdAt) ||left.messageId.localeCompare(right.messageId),).slice(0, missingUserCount);@@ -281,7 +286,10 @@ function retainProjectionMessagesAfterRevert(}const retainedAssistantCount = messages.filter(- (message) => message.role === "assistant" && retainedMessageIds.has(message.messageId),+ (message) =>+ message.role === "assistant" &&+ !isImportedAgentSessionMessageId(message.messageId) &&+ retainedMessageIds.has(message.messageId),).length;const missingAssistantCount = Math.max(0, turnCount - retainedAssistantCount);if (missingAssistantCount > 0) {@@ -294,7 +302,7 @@ function retainProjectionMessagesAfterRevert().toSorted((left, right) =>- left.createdAt.localeCompare(right.createdAt) ||+ compareDateTimeStrings(left.createdAt, right.createdAt) ||left.messageId.localeCompare(right.messageId),).slice(0, missingAssistantCount);@@ -903,6 +911,7 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjectiupdatedAt: event.occurredAt,latestUserMessageAt:event.payload.role === "user" &&+ !isImportedAgentSessionMessageId(event.payload.messageId) &&(previousLatest === null || event.payload.createdAt > previousLatest)? event.payload.createdAt: previousLatest,diff --git a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.tsindex 6330c38ca0c..a1b351de353 100644--- a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts+++ b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts@@ -1,4 +1,5 @@import {+ type AgentSessionImportSource,CheckpointRef,EventId,MessageId,@@ -11,6 +12,7 @@ import { assert, it } from "@effect/vitest";import * as NodeServices from "@effect/platform-node/NodeServices";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";+import * as Schema from "effect/Schema";import * as SqlClient from "effect/unstable/sql/SqlClient";import { SqlitePersistenceMemory } from "../../persistence/Layers/Sqlite.ts";@@ -2134,7 +2136,9 @@ projectionSnapshotLayer("ProjectionSnapshotQuery windowed thread detail", (it) =//// Straggler user message at T03.5 (turn_id NULL, not any pending_message_id)// and a turnless activity at T03.6 — both belong to the page containing T03+.- const seedFanOutThread = Effect.fnUntraced(function* () {+ const seedFanOutThread = Effect.fnUntraced(function* (options?: {+ readonly importedMessageCount?: number;+ }) {const sql = yield* SqlClient.SqlClient;// Tests in this block share one in-memory database; reset before seeding.@@ -2163,6 +2167,20 @@ projectionSnapshotLayer("ProjectionSnapshotQuery windowed thread detail", (it) ='turn-5', 0, 0, 0, '2026-03-01T00:00:00.000Z', '2026-03-01T00:00:10.000Z', NULL)`;+ if (options?.importedMessageCount) {+ for (let index = 0; index < options.importedMessageCount; index += 1) {+ const messageId = `import:codex:session-w:${String(index).padStart(6, "0")}`;+ const role = index % 2 === 0 ? "user" : "assistant";+ yield* sql`+ INSERT INTO projection_thread_messages (+ message_id, thread_id, turn_id, role, text, is_streaming, created_at, updated_at+ )+ VALUES (${messageId}, 'thread-w', NULL, ${role}, ${"imported message " + index}, 0,+ '2026-02-28T00:00:00.000Z', '2026-02-28T00:00:00.000Z')+ `;+ }+ }+const turns: ReadonlyArray<{turn: string;pendingMessage: string | null;@@ -2396,6 +2414,51 @@ projectionSnapshotLayer("ProjectionSnapshotQuery windowed thread detail", (it) =}),);+ it.effect("keeps imported history on the oldest page after resumed turns", () =>+ Effect.gen(function* () {+ yield* seedFanOutThread({ importedMessageCount: 12 });+ const snapshotQuery = yield* ProjectionSnapshotQuery;++ const completePage = yield* snapshotQuery.getThreadDetailSnapshot(threadW, { turnLimit: 50 });+ assert.equal(completePage._tag, "Some");+ if (completePage._tag !== "Some") return;+ assert.equal(+ completePage.value.thread.messages.filter((message) => message.id.startsWith("import:"))+ .length,+ 12,+ );+ assert.equal(completePage.value.page?.hasMore, false);+ assert.equal(completePage.value.page?.beforeCursor, null);++ const recentPage = yield* snapshotQuery.getThreadDetailSnapshot(threadW, { turnLimit: 2 });+ assert.equal(recentPage._tag, "Some");+ if (recentPage._tag !== "Some") return;+ assert.equal(+ recentPage.value.thread.messages.some((message) => message.id.startsWith("import:")),+ false,+ );+ const cursor = recentPage.value.page?.beforeCursor;+ assert.notEqual(cursor, null);+ assert.notEqual(cursor, undefined);+ if (cursor === null || cursor === undefined) return;++ const oldestPage = yield* snapshotQuery.getThreadDetailSnapshot(threadW, {+ turnLimit: 1,+ beforeCursor: cursor,+ });+ assert.equal(oldestPage._tag, "Some");+ if (oldestPage._tag !== "Some") return;++ const importedIds = oldestPage.value.thread.messages+ .map((message) => message.id)+ .filter((messageId) => messageId.startsWith("import:"));+ assert.equal(importedIds.length, 12);+ assert.equal(new Set(importedIds).size, 12);+ assert.equal(oldestPage.value.page?.hasMore, false);+ assert.equal(oldestPage.value.page?.beforeCursor, null);+ }),+ );+it.effect("a cursor for a different thread degrades to the first page", () =>Effect.gen(function* () {yield* seedFanOutThread();@@ -2779,3 +2842,237 @@ projectionSnapshotLayer("ProjectionSnapshotQuery windowed thread detail", (it) =}),);});++projectionSnapshotLayer("ProjectionSnapshotQuery imported sources", (it) => {+ const encodeJson = Schema.encodeUnknownSync(Schema.fromJsonString(Schema.Unknown));+ const source: AgentSessionImportSource = {+ provider: "codex",+ providerInstanceId: ProviderInstanceId.make("codex-home"),+ providerSessionId: "native-session",+ filePath: "/tmp/transcript.jsonl",+ size: 128,+ mtimeMs: 1_700_000_000_000,+ device: 1,+ inode: 2,+ birthtimeMs: 1_699_000_000_000,+ };++ const seedImportedSession = Effect.fn("seedImportedSession")(function* (+ projectId: ProjectId,+ source: AgentSessionImportSource,+ ) {+ const sql = yield* SqlClient.SqlClient;+ const threadId = ThreadId.make(+ `import:${source.providerInstanceId}:${source.providerSessionId}`,+ );+ const timestamp = "2026-03-02T00:00:00.000Z";+ yield* sql`+ INSERT OR IGNORE INTO projection_projects (+ project_id, title, workspace_root, scripts_json, created_at, updated_at+ ) VALUES (${projectId}, 'Imported project', '/tmp/imported-project', '[]',+ ${timestamp}, ${timestamp})+ `;+ yield* sql`+ INSERT INTO projection_threads (+ thread_id, project_id, title, model_selection_json, runtime_mode, interaction_mode,+ created_at, updated_at+ ) VALUES (${threadId}, ${projectId}, 'Imported thread',+ ${encodeJson({ instanceId: source.providerInstanceId, model: "gpt-5-codex" })},+ 'full-access', 'default',+ ${timestamp}, ${timestamp})+ `;+ yield* sql`+ INSERT INTO provider_session_runtime (+ thread_id, provider_name, provider_instance_id, adapter_key, runtime_mode, status,+ last_seen_at, resume_cursor_json, runtime_payload_json+ ) VALUES (${threadId}, ${source.provider}, ${source.providerInstanceId},+ ${source.provider}, 'full-access', 'stopped', ${timestamp},+ ${encodeJson({ threadId: source.providerSessionId })},+ ${encodeJson({ importedTranscripts: [source] })})+ `;+ yield* sql`+ INSERT INTO projection_thread_messages (+ message_id, thread_id, role, text, is_streaming, created_at, updated_at+ ) VALUES (${`${threadId}:000000`}, ${threadId}, 'user', 'Imported history', 0,+ ${timestamp}, ${timestamp})+ `;+ return { threadId, source };+ });++ it.effect("reads completed source copies without decoding message bodies", () =>+ Effect.gen(function* () {+ const sql = yield* SqlClient.SqlClient;+ const query = yield* ProjectionSnapshotQuery;+ const projectId = ProjectId.make("project-import-metadata");+ const imported = yield* seedImportedSession(projectId, source);+ const copiedSource = {+ ...source,+ filePath: "/tmp/transcript-copy.jsonl",+ mtimeMs: null,+ inode: null,+ birthtimeMs: null,+ };+ yield* sql`+ UPDATE provider_session_runtime+ SET runtime_payload_json = ${encodeJson({+ cwd: "/tmp/imported-project",+ importedTranscripts: [source, copiedSource],+ })}+ WHERE thread_id = ${imported.threadId}+ `;+ yield* sql`+ UPDATE projection_thread_messages SET attachments_json = 'not-json'+ WHERE thread_id = ${imported.threadId}+ `;++ const counter = makeSqlStatementCounter();+ const sources = yield* query+ .getImportedAgentSessionSources(projectId)+ .pipe(Effect.withTracer(counter.tracer));+ assert.deepEqual(sources, [imported, { threadId: imported.threadId, source: copiedSource }]);+ assert.equal(counter.count(), 1);+ }),+ );++ it.effect("requires active project threads, a binding, and an imported message", () =>+ Effect.gen(function* () {+ const sql = yield* SqlClient.SqlClient;+ const query = yield* ProjectionSnapshotQuery;+ const projectId = ProjectId.make("project-import-completion");+ const completed = yield* seedImportedSession(projectId, {+ ...source,+ providerSessionId: "completed",+ });+ yield* sql`+ UPDATE projection_thread_messages SET message_id = ${`${completed.threadId}:legacy`}+ WHERE thread_id = ${completed.threadId}+ `;+ const partials = yield* Effect.forEach(+ [+ "no-binding",+ "no-history",+ "no-imported-message",+ "wrong-message-thread",+ "archived",+ "deleted",+ ],+ (providerSessionId) => seedImportedSession(projectId, { ...source, providerSessionId }),+ );+ const [noBinding, noHistory, noImportedMessage, wrongMessageThread, archived, deleted] =+ partials;+ assert.isDefined(noBinding);+ assert.isDefined(noHistory);+ assert.isDefined(noImportedMessage);+ assert.isDefined(wrongMessageThread);+ assert.isDefined(archived);+ assert.isDefined(deleted);+ yield* sql`DELETE FROM provider_session_runtime WHERE thread_id = ${noBinding.threadId}`;+ yield* sql`DELETE FROM projection_thread_messages WHERE thread_id = ${noHistory.threadId}`;+ yield* sql`+ UPDATE projection_thread_messages SET message_id = ${`normal:${noImportedMessage.threadId}`}+ WHERE thread_id = ${noImportedMessage.threadId}+ `;+ yield* sql`+ UPDATE projection_thread_messages SET thread_id = 'unrelated-thread'+ WHERE thread_id = ${wrongMessageThread.threadId}+ `;+ yield* sql`+ UPDATE projection_threads SET archived_at = '2026-03-03T00:00:00.000Z'+ WHERE thread_id = ${archived.threadId}+ `;+ yield* sql`+ UPDATE projection_threads SET deleted_at = '2026-03-03T00:00:00.000Z'+ WHERE thread_id = ${deleted.threadId}+ `;+ const otherProjectId = ProjectId.make("project-import-other");+ const otherProject = yield* seedImportedSession(otherProjectId, {+ ...source,+ providerSessionId: "other-project",+ });+ const deletedProjectId = ProjectId.make("project-import-deleted");+ yield* seedImportedSession(deletedProjectId, {+ ...source,+ providerSessionId: "deleted-project",+ });+ yield* sql`+ UPDATE projection_projects SET deleted_at = '2026-03-03T00:00:00.000Z'+ WHERE project_id = ${deletedProjectId}+ `;++ assert.deepEqual(yield* query.getImportedAgentSessionSources(projectId), [completed]);+ assert.deepEqual(yield* query.getImportedAgentSessionSources(otherProjectId), [otherProject]);+ assert.deepEqual(yield* query.getImportedAgentSessionSources(deletedProjectId), []);+ assert.deepEqual(+ yield* query.getImportedAgentSessionSources(ProjectId.make("project-import-missing")),+ [],+ );+ }),+ );++ it.effect("keeps original sources when the current runtime provider and cursor change", () =>+ Effect.gen(function* () {+ const sql = yield* SqlClient.SqlClient;+ const query = yield* ProjectionSnapshotQuery;+ const projectId = ProjectId.make("project-import-switched");+ const imported = yield* seedImportedSession(projectId, {+ ...source,+ provider: "claudeAgent",+ providerInstanceId: ProviderInstanceId.make("claude-original"),+ providerSessionId: "original-session",+ });+ yield* sql`+ UPDATE provider_session_runtime+ SET provider_name = 'codex', provider_instance_id = 'codex-new', adapter_key = 'codex',+ resume_cursor_json = '{"threadId":"new-session"}'+ WHERE thread_id = ${imported.threadId}+ `;++ assert.deepEqual(yield* query.getImportedAgentSessionSources(projectId), [imported]);+ }),+ );++ it.effect("skips invalid source payloads and entries without dropping valid sources", () =>+ Effect.gen(function* () {+ const sql = yield* SqlClient.SqlClient;+ const query = yield* ProjectionSnapshotQuery;+ const projectId = ProjectId.make("project-import-invalid");+ const imported = yield* seedImportedSession(projectId, {+ ...source,+ providerSessionId: "a-invalid",+ });+ const valid = yield* seedImportedSession(projectId, {+ ...source,+ providerSessionId: "z-valid",+ });+ for (const payload of [null, "not-json", "null", "[]", "{}", '{"importedTranscripts":{}}']) {+ yield* sql`+ UPDATE provider_session_runtime SET runtime_payload_json = ${payload}+ WHERE thread_id = ${imported.threadId}+ `;+ assert.deepEqual(yield* query.getImportedAgentSessionSources(projectId), [valid]);+ }+ yield* sql`+ UPDATE provider_session_runtime SET runtime_payload_json = X'FF'+ WHERE thread_id = ${imported.threadId}+ `;+ assert.deepEqual(yield* query.getImportedAgentSessionSources(projectId), [valid]);++ yield* sql`+ UPDATE provider_session_runtime+ SET runtime_payload_json = ${encodeJson({+ importedTranscripts: [+ null,+ {},+ { ...imported.source, size: -1 },+ { ...imported.source, provider: "cursor" },+ { ...imported.source, providerInstanceId: "wrong-instance" },+ { ...imported.source, providerSessionId: "wrong-session" },+ imported.source,+ ],+ })}+ WHERE thread_id = ${imported.threadId}+ `;+ assert.deepEqual(yield* query.getImportedAgentSessionSources(projectId), [imported, valid]);+ }),+ );+});diff --git a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.tsindex 91d09bf5719..86e0b94573d 100644--- a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts+++ b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts@@ -1,4 +1,5 @@import {+ AgentSessionImportSource,ApprovalRequestId,ChatAttachment,CheckpointRef,@@ -75,6 +76,14 @@ import {const decodeReadModel = Schema.decodeUnknownEffect(OrchestrationReadModel);const decodeShellSnapshot = Schema.decodeUnknownEffect(OrchestrationShellSnapshot);const decodeThread = Schema.decodeUnknownEffect(OrchestrationThread);+const decodeImportedTranscriptsPayload = Schema.decodeUnknownOption(+ Schema.fromJsonString(+ Schema.Struct({+ importedTranscripts: Schema.Array(Schema.Unknown),+ }),+ ),+);+const decodeAgentSessionImportSource = Schema.decodeUnknownOption(AgentSessionImportSource);// Keep detail reads consistent with the in-memory projector's retained// activity window. Applying the limit in SQL avoids decoding an unbounded// payload_json set before the projector can enforce that invariant.@@ -164,6 +173,10 @@ const WorkspaceRootLookupInput = Schema.Struct({const ProjectIdLookupInput = Schema.Struct({projectId: ProjectId,});+const ProjectionImportedAgentSessionSourcesRowSchema = Schema.Struct({+ threadId: ThreadId,+ runtimePayload: Schema.Unknown,+});const ThreadIdLookupInput = Schema.Struct({threadId: ThreadId,});@@ -983,6 +996,33 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {`,});+ const listImportedAgentSessionSourceRows = SqlSchema.findAll({+ Request: ProjectIdLookupInput,+ Result: ProjectionImportedAgentSessionSourcesRowSchema,+ execute: ({ projectId }) =>+ sql`+ SELECT+ threads.thread_id AS "threadId",+ runtime.runtime_payload_json AS "runtimePayload"+ FROM projection_threads AS threads+ INNER JOIN projection_projects AS projects+ ON projects.project_id = threads.project_id+ INNER JOIN provider_session_runtime AS runtime+ ON runtime.thread_id = threads.thread_id+ WHERE threads.project_id = ${projectId}+ AND threads.deleted_at IS NULL+ AND threads.archived_at IS NULL+ AND projects.deleted_at IS NULL+ AND EXISTS (+ SELECT 1+ FROM projection_thread_messages AS messages+ WHERE messages.thread_id = threads.thread_id+ AND messages.message_id GLOB 'import:*'+ )+ ORDER BY threads.thread_id ASC+ `,+ });+const getThreadCheckpointContextThreadRow = SqlSchema.findOneOption({Request: ThreadIdLookupInput,Result: ProjectionThreadCheckpointContextThreadRowSchema,@@ -2663,6 +2703,33 @@ pending_approval_requests AS (Effect.map(Option.map((row) => row.threadId)),);+ const getImportedAgentSessionSources: ProjectionSnapshotQueryShape["getImportedAgentSessionSources"] =+ Effect.fn("ProjectionSnapshotQuery.getImportedAgentSessionSources")(function* (projectId) {+ const rows = yield* listImportedAgentSessionSourceRows({ projectId }).pipe(+ Effect.mapError(+ toPersistenceSqlOrDecodeError(+ "ProjectionSnapshotQuery.getImportedAgentSessionSources:query",+ "ProjectionSnapshotQuery.getImportedAgentSessionSources:decodeRows",+ ),+ ),+ );+ return rows.flatMap((row) => {+ const payload = decodeImportedTranscriptsPayload(row.runtimePayload);+ if (Option.isNone(payload)) return [];+ return payload.value.importedTranscripts.flatMap((entry) => {+ const source = decodeAgentSessionImportSource(entry);+ if (+ Option.isNone(source) ||+ row.threadId !==+ `import:${source.value.providerInstanceId}:${source.value.providerSessionId}`+ ) {+ return [];+ }+ return [{ threadId: row.threadId, source: source.value }];+ });+ });+ });+const getThreadCheckpointContext: ProjectionSnapshotQueryShape["getThreadCheckpointContext"] = (threadId,) =>@@ -3151,17 +3218,35 @@ pending_approval_requests AS ();const oldest = windowRows[0];+ const hasMore =+ oldest !== undefined &&+ (yield* listTurnWindowRows({+ threadId,+ beforeAnchorAt: oldest.anchorAt,+ beforeTurnKey: oldest.turnKey,+ userTurnLimit: 1,+ maxRawTurns: 1,+ }).pipe(+ Effect.mapError(+ toPersistenceSqlOrDecodeError(+ "ProjectionSnapshotQuery.getThreadDetailSnapshot:probeOlder:query",+ "ProjectionSnapshotQuery.getThreadDetailSnapshot:probeOlder:decodeRows",+ ),+ ),+ )).length > 0;// An empty window (no turns before the cursor, or a thread with no// turns at all) still returns thread metadata with empty collections// for turn-linked rows; turnless rows are bounded to the same empty// range. The first page of a turnless thread stays unwindowed so- // pre-turn content (e.g. a just-created thread) is not hidden.+ // pre-turn content (e.g. a just-created thread) is not hidden. Once+ // paging reaches the oldest turn, include turnless messages before+ // the first turn, such as history imported from a provider session.const bounds: ThreadDetailBounds | undefined =oldest === undefined && cursor === null? undefined: {- minAnchorAt: oldest?.anchorAt ?? "",- minTurnKey: oldest?.turnKey ?? "",+ minAnchorAt: hasMore ? (oldest?.anchorAt ?? "") : "",+ minTurnKey: hasMore ? (oldest?.turnKey ?? "") : "",beforeAnchorAt: cursor?.beforeAnchorAt ?? ANCHOR_UNBOUNDED,beforeTurnKey: cursor?.beforeTurnId ?? "",};@@ -3178,23 +3263,6 @@ pending_approval_requests AS (return Option.none<OrchestrationThreadDetailSnapshot>();}- const hasMore =- oldest !== undefined &&- (yield* listTurnWindowRows({- threadId,- beforeAnchorAt: oldest.anchorAt,- beforeTurnKey: oldest.turnKey,- userTurnLimit: 1,- maxRawTurns: 1,- }).pipe(- Effect.mapError(- toPersistenceSqlOrDecodeError(- "ProjectionSnapshotQuery.getThreadDetailSnapshot:probeOlder:query",- "ProjectionSnapshotQuery.getThreadDetailSnapshot:probeOlder:decodeRows",- ),- ),- )).length > 0;-const { snapshotSequence } = yield* getSnapshotSequence();const watermarkRow = yield* getThreadEventWatermarkRow({threadId,@@ -3253,6 +3321,7 @@ pending_approval_requests AS (getActiveProjectByWorkspaceRoot,getProjectShellById,getFirstActiveThreadIdByProjectId,+ getImportedAgentSessionSources,getThreadCheckpointContext,getFullThreadDiffContext,getThreadShellById,diff --git a/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts b/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.tsindex 96072b26631..37bcc1ae8f3 100644--- a/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts+++ b/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts@@ -7,6 +7,7 @@* @module ProjectionSnapshotQuery*/import type {+ AgentSessionImportSource,ApprovalRequestId,CheckpointRef,OrchestrationCheckpointSummary,@@ -170,6 +171,15 @@ export interface ProjectionSnapshotQueryShape {projectId: ProjectId,) => Effect.Effect<Option.Option<ThreadId>, ProjectionRepositoryError>;+ /** Read completed import sources without loading thread history. */+ readonly getImportedAgentSessionSources: (projectId: ProjectId) => Effect.Effect<+ ReadonlyArray<{+ readonly threadId: ThreadId;+ readonly source: AgentSessionImportSource;+ }>,+ ProjectionRepositoryError+ >;+/*** Read the checkpoint context needed to resolve a single thread diff.*/diff --git a/apps/server/src/orchestration/decider.import.test.ts b/apps/server/src/orchestration/decider.import.test.tsnew file mode 100644index 00000000000..c809c733800--- /dev/null+++ b/apps/server/src/orchestration/decider.import.test.ts@@ -0,0 +1,509 @@+import * as NodeServices from "@effect/platform-node/NodeServices";+import { expect, it } from "@effect/vitest";+import {+ CommandId,+ EventId,+ MessageId,+ ProjectId,+ ProviderInstanceId,+ ThreadId,+} from "@t3tools/contracts";+import * as Effect from "effect/Effect";+import * as TestClock from "effect/testing/TestClock";++import { decideOrchestrationCommand } from "./decider.ts";+import { createEmptyReadModel, projectEvent } from "./projector.ts";++it.layer(NodeServices.layer)("thread history import", (it) => {+ it.effect("marks imported thread creation without changing live creation", () =>+ Effect.gen(function* () {+ const createdAt = "2026-08-24T10:00:00.000Z";+ const projectId = ProjectId.make("project-1");+ const readModel = yield* projectEvent(createEmptyReadModel(createdAt), {+ sequence: 1,+ eventId: EventId.make("event-project-created"),+ aggregateKind: "project",+ aggregateId: projectId,+ type: "project.created",+ occurredAt: createdAt,+ commandId: CommandId.make("command-project-created"),+ causationEventId: null,+ correlationId: CommandId.make("command-project-created"),+ metadata: {},+ payload: {+ projectId,+ title: "Project",+ workspaceRoot: "/tmp/project",+ defaultModelSelection: null,+ scripts: [],+ createdAt,+ updatedAt: createdAt,+ },+ });+ const makeCreateCommand = (threadId: ThreadId) => ({+ type: "thread.create" as const,+ commandId: CommandId.make(`command-create-${threadId}`),+ threadId,+ projectId,+ title: "Imported thread",+ modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },+ runtimeMode: "full-access" as const,+ interactionMode: "default" as const,+ branch: null,+ worktreePath: null,+ createdAt,+ });++ const imported = yield* decideOrchestrationCommand({+ command: {+ ...makeCreateCommand(ThreadId.make("import:codex:session-1")),+ historyImport: true,+ },+ readModel,+ });+ const live = yield* decideOrchestrationCommand({+ command: makeCreateCommand(ThreadId.make("live-thread")),+ readModel,+ });++ expect(imported).toMatchObject({+ type: "thread.created",+ metadata: { historyImport: true },+ });+ expect(live).toMatchObject({ type: "thread.created" });+ expect(live).not.toMatchObject({ metadata: { historyImport: true } });+ }),+ );++ it.effect("settles imported messages at the latest absolute timestamp", () =>+ Effect.gen(function* () {+ const createdAt = "2026-08-24T10:30:00.000+02:00";+ const threadId = ThreadId.make("import:codex:session-1");+ const readModel = yield* projectEvent(createEmptyReadModel(createdAt), {+ sequence: 1,+ eventId: EventId.make("event-thread-created"),+ aggregateKind: "thread",+ aggregateId: threadId,+ type: "thread.created",+ occurredAt: createdAt,+ commandId: CommandId.make("command-thread-created"),+ causationEventId: null,+ correlationId: CommandId.make("command-thread-created"),+ metadata: {},+ payload: {+ threadId,+ projectId: ProjectId.make("project-1"),+ title: "Imported thread",+ modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },+ runtimeMode: "full-access",+ interactionMode: "default",+ branch: null,+ worktreePath: null,+ createdAt,+ updatedAt: createdAt,+ },+ });++ const events = yield* decideOrchestrationCommand({+ command: {+ type: "thread.history.import",+ commandId: CommandId.make("command-import-history"),+ threadId,+ messages: [+ {+ messageId: MessageId.make(`${threadId}:000000`),+ role: "user",+ text: "Fix the bug",+ createdAt,+ },+ {+ messageId: MessageId.make(`${threadId}:000001`),+ role: "assistant",+ text: "Fixed",+ createdAt: "2026-08-24T09:00:00.000Z",+ },+ ],+ },+ readModel,+ });++ expect(events).toMatchObject([+ {+ type: "thread.message-sent",+ metadata: { historyImport: true },+ payload: { role: "user", text: "Fix the bug", turnId: null, streaming: false },+ },+ {+ type: "thread.message-sent",+ metadata: { historyImport: true },+ payload: { role: "assistant", text: "Fixed", turnId: null, streaming: false },+ },+ {+ type: "thread.settled",+ metadata: { historyImport: true },+ occurredAt: "2026-08-24T09:00:00.000Z",+ payload: {+ settledAt: "2026-08-24T09:00:00.000Z",+ updatedAt: "2026-08-24T09:00:00.000Z",+ },+ },+ ]);++ let projected = readModel;+ const plannedEvents = Array.isArray(events) ? events : [events];+ for (const [index, event] of plannedEvents.entries()) {+ projected = yield* projectEvent(projected, { ...event, sequence: index + 2 });+ }+ projected = yield* projectEvent(projected, {+ sequence: 5,+ eventId: EventId.make("event-import-reverted"),+ aggregateKind: "thread",+ aggregateId: threadId,+ type: "thread.reverted",+ occurredAt: "2026-08-24T10:02:00.000Z",+ commandId: CommandId.make("command-import-reverted"),+ causationEventId: null,+ correlationId: CommandId.make("command-import-reverted"),+ metadata: {},+ payload: { threadId, turnCount: 0 },+ });+ expect(projected.threads[0]?.messages.map((message) => message.text)).toEqual([+ "Fix the bug",+ "Fixed",+ ]);+ }),+ );++ it.effect("allows a thread with a newly imported user message to be settled", () =>+ Effect.gen(function* () {+ const createdAt = "2026-08-24T10:00:00.000Z";+ yield* TestClock.setTime(Date.parse("2026-08-24T10:00:30.000Z"));+ const threadId = ThreadId.make("import:codex:session-1");+ const withThread = yield* projectEvent(createEmptyReadModel(createdAt), {+ sequence: 1,+ eventId: EventId.make("event-import-thread-created"),+ aggregateKind: "thread",+ aggregateId: threadId,+ type: "thread.created",+ occurredAt: createdAt,+ commandId: CommandId.make("command-import-thread-created"),+ causationEventId: null,+ correlationId: CommandId.make("command-import-thread-created"),+ metadata: {},+ payload: {+ threadId,+ projectId: ProjectId.make("project-1"),+ title: "Imported thread",+ modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },+ runtimeMode: "full-access",+ interactionMode: "default",+ branch: null,+ worktreePath: null,+ createdAt,+ updatedAt: createdAt,+ },+ });+ const readModel = yield* projectEvent(withThread, {+ sequence: 2,+ eventId: EventId.make("event-import-user-message"),+ aggregateKind: "thread",+ aggregateId: threadId,+ type: "thread.message-sent",+ occurredAt: createdAt,+ commandId: CommandId.make("command-import-user-message"),+ causationEventId: null,+ correlationId: CommandId.make("command-import-user-message"),+ metadata: { historyImport: true },+ payload: {+ threadId,+ messageId: MessageId.make("import:codex:session-1:0"),+ role: "user",+ text: "Existing prompt",+ turnId: null,+ streaming: false,+ createdAt,+ updatedAt: createdAt,+ },+ });++ const result = yield* decideOrchestrationCommand({+ command: {+ type: "thread.settle",+ commandId: CommandId.make("command-settle-imported-thread"),+ threadId,+ },+ readModel,+ });++ expect(result).toMatchObject({ type: "thread.settled" });+ }),+ );++ it.effect("rejects history import after a client message reaches the thread", () =>+ Effect.gen(function* () {+ const createdAt = "2026-08-24T10:00:00.000Z";+ const liveMessageAt = "2026-08-24T10:02:00.000Z";+ const threadId = ThreadId.make("import:codex:client-race");+ const withThread = yield* projectEvent(createEmptyReadModel(createdAt), {+ sequence: 1,+ eventId: EventId.make("event-client-race-thread-created"),+ aggregateKind: "thread",+ aggregateId: threadId,+ type: "thread.created",+ occurredAt: createdAt,+ commandId: CommandId.make("command-client-race-thread-created"),+ causationEventId: null,+ correlationId: CommandId.make("command-client-race-thread-created"),+ metadata: {},+ payload: {+ threadId,+ projectId: ProjectId.make("project-1"),+ title: "Imported thread",+ modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },+ runtimeMode: "full-access",+ interactionMode: "default",+ branch: null,+ worktreePath: null,+ createdAt,+ updatedAt: createdAt,+ },+ });+ const readModel = yield* projectEvent(withThread, {+ sequence: 2,+ eventId: EventId.make("event-client-race-message"),+ aggregateKind: "thread",+ aggregateId: threadId,+ type: "thread.message-sent",+ occurredAt: liveMessageAt,+ commandId: CommandId.make("command-client-race-message"),+ causationEventId: null,+ correlationId: CommandId.make("command-client-race-message"),+ metadata: {},+ payload: {+ threadId,+ messageId: MessageId.make("client-race-message"),+ role: "user",+ text: "Start live work",+ turnId: null,+ streaming: false,+ createdAt: liveMessageAt,+ updatedAt: liveMessageAt,+ },+ });++ const error = yield* Effect.flip(+ decideOrchestrationCommand({+ command: {+ type: "thread.history.import",+ commandId: CommandId.make("command-client-race-import"),+ threadId,+ messages: [+ {+ messageId: MessageId.make(`${threadId}:000000`),+ role: "user",+ text: "Old work",+ createdAt,+ },+ ],+ },+ readModel,+ }),+ );++ expect(error._tag).toBe("OrchestrationCommandInvariantError");+ expect(error.message).toContain("must be active and empty");+ expect(readModel.threads[0]?.updatedAt).toBe(liveMessageAt);+ }),+ );++ for (const requestKind of ["approval.requested", "user-input.requested"] as const) {+ it.effect(`rejects history import with an open ${requestKind} activity`, () =>+ Effect.gen(function* () {+ const createdAt = "2026-08-24T10:00:00.000Z";+ const threadId = ThreadId.make(`import:codex:${requestKind}`);+ const withThread = yield* projectEvent(createEmptyReadModel(createdAt), {+ sequence: 1,+ eventId: EventId.make(`event-${requestKind}-thread-created`),+ aggregateKind: "thread",+ aggregateId: threadId,+ type: "thread.created",+ occurredAt: createdAt,+ commandId: CommandId.make(`command-${requestKind}-thread-created`),+ causationEventId: null,+ correlationId: CommandId.make(`command-${requestKind}-thread-created`),+ metadata: {},+ payload: {+ threadId,+ projectId: ProjectId.make("project-1"),+ title: "Imported thread",+ modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },+ runtimeMode: "full-access",+ interactionMode: "default",+ branch: null,+ worktreePath: null,+ createdAt,+ updatedAt: createdAt,+ },+ });+ const readModel = yield* projectEvent(withThread, {+ sequence: 2,+ eventId: EventId.make(`event-${requestKind}`),+ aggregateKind: "thread",+ aggregateId: threadId,+ type: "thread.activity-appended",+ occurredAt: createdAt,+ commandId: CommandId.make(`command-${requestKind}`),+ causationEventId: null,+ correlationId: CommandId.make(`command-${requestKind}`),+ metadata: {},+ payload: {+ threadId,+ activity: {+ id: EventId.make(`activity-${requestKind}`),+ tone: "approval",+ kind: requestKind,+ summary: "Pending request",+ payload: { requestId: "request-1" },+ turnId: null,+ createdAt,+ },+ },+ });++ const error = yield* Effect.flip(+ decideOrchestrationCommand({+ command: {+ type: "thread.history.import",+ commandId: CommandId.make(`command-import-${requestKind}`),+ threadId,+ messages: [+ {+ messageId: MessageId.make(`${threadId}:000000`),+ role: "user",+ text: "Old work",+ createdAt,+ },+ ],+ },+ readModel,+ }),+ );++ expect(error._tag).toBe("OrchestrationCommandInvariantError");+ expect(error.message).toContain("must be active and empty");+ }),+ );+ }++ it.effect("rejects a live user message in the imported-session namespace", () =>+ Effect.gen(function* () {+ const createdAt = "2026-08-24T10:00:00.000Z";+ const threadId = ThreadId.make("thread-live-message");+ const readModel = yield* projectEvent(createEmptyReadModel(createdAt), {+ sequence: 1,+ eventId: EventId.make("event-live-thread-created"),+ aggregateKind: "thread",+ aggregateId: threadId,+ type: "thread.created",+ occurredAt: createdAt,+ commandId: CommandId.make("command-live-thread-created"),+ causationEventId: null,+ correlationId: CommandId.make("command-live-thread-created"),+ metadata: {},+ payload: {+ threadId,+ projectId: ProjectId.make("project-1"),+ title: "Live thread",+ modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },+ runtimeMode: "full-access",+ interactionMode: "default",+ branch: null,+ worktreePath: null,+ createdAt,+ updatedAt: createdAt,+ },+ });++ const error = yield* Effect.flip(+ decideOrchestrationCommand({+ command: {+ type: "thread.turn.start",+ commandId: CommandId.make("command-live-import-id"),+ threadId,+ message: {+ messageId: MessageId.make("import:forged-live-message"),+ role: "user",+ text: "Live work",+ attachments: [],+ },+ runtimeMode: "full-access",+ interactionMode: "default",+ createdAt,+ },+ readModel,+ }),+ );++ expect(error._tag).toBe("OrchestrationCommandInvariantError");+ expect(error.message).toContain("reserved imported-session namespace");+ }),+ );++ it.effect("rejects live assistant messages in the imported-session namespace", () =>+ Effect.gen(function* () {+ const createdAt = "2026-08-24T10:00:00.000Z";+ const threadId = ThreadId.make("thread-live-assistant-message");+ const readModel = yield* projectEvent(createEmptyReadModel(createdAt), {+ sequence: 1,+ eventId: EventId.make("event-live-assistant-thread-created"),+ aggregateKind: "thread",+ aggregateId: threadId,+ type: "thread.created",+ occurredAt: createdAt,+ commandId: CommandId.make("command-live-assistant-thread-created"),+ causationEventId: null,+ correlationId: CommandId.make("command-live-assistant-thread-created"),+ metadata: {},+ payload: {+ threadId,+ projectId: ProjectId.make("project-1"),+ title: "Live thread",+ modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },+ runtimeMode: "full-access",+ interactionMode: "default",+ branch: null,+ worktreePath: null,+ createdAt,+ updatedAt: createdAt,+ },+ });++ for (const commandType of [+ "thread.message.assistant.delta",+ "thread.message.assistant.complete",+ ] as const) {+ const command =+ commandType === "thread.message.assistant.delta"+ ? {+ type: commandType,+ commandId: CommandId.make("command-live-assistant-delta-import-id"),+ threadId,+ messageId: MessageId.make("import:forged-live-assistant-message"),+ delta: "Live work",+ createdAt,+ }+ : {+ type: commandType,+ commandId: CommandId.make("command-live-assistant-complete-import-id"),+ threadId,+ messageId: MessageId.make("import:forged-live-assistant-message"),+ createdAt,+ };+ const error = yield* Effect.flip(decideOrchestrationCommand({ command, readModel }));++ expect(error._tag).toBe("OrchestrationCommandInvariantError");+ expect(error.message).toContain("reserved imported-session namespace");+ }+ }),+ );+});diff --git a/apps/server/src/orchestration/decider.ts b/apps/server/src/orchestration/decider.tsindex b336053ac9e..1fd9feba7c4 100644--- a/apps/server/src/orchestration/decider.ts+++ b/apps/server/src/orchestration/decider.ts@@ -2,12 +2,14 @@ import {EventId,MessageId,UserInputRequestedPayload,+ isImportedAgentSessionMessageId,type OrchestrationCommand,type OrchestrationEvent,type OrchestrationReadModel,type OrchestrationThread,type OrchestrationThreadActivity,} from "@t3tools/contracts";+import { compareDateTimeStrings } from "@t3tools/shared/dateTime";import * as DateTime from "effect/DateTime";import * as Crypto from "effect/Crypto";import * as Effect from "effect/Effect";@@ -98,7 +100,7 @@ function hasQueuedTurnStartForThread(let latestUserMessageAt: string | null = null;let latestUserMessageAtMs = Number.NEGATIVE_INFINITY;for (const message of thread.messages) {- if (message.role !== "user") continue;+ if (message.role !== "user" || isImportedAgentSessionMessageId(message.id)) continue;const messageAtMs = Date.parse(message.createdAt);latestUserMessageAtMs = Math.max(latestUserMessageAtMs, messageAtMs);if (messageAtMs === latestUserMessageAtMs) {@@ -347,6 +349,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"aggregateId: command.threadId,occurredAt: command.createdAt,commandId: command.commandId,+ ...(command.historyImport === true ? { metadata: { historyImport: true } } : {}),})),type: "thread.created",payload: {@@ -905,6 +908,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"}case "thread.turn.start": {+ if (isImportedAgentSessionMessageId(command.message.messageId)) {+ return yield* new OrchestrationCommandInvariantError({+ commandType: command.type,+ detail: `Message id '${command.message.messageId}' uses the reserved imported-session namespace.`,+ });+ }const targetThread = yield* requireThread({readModel,command,@@ -1272,6 +1281,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"}case "thread.message.assistant.delta": {+ if (isImportedAgentSessionMessageId(command.messageId)) {+ return yield* new OrchestrationCommandInvariantError({+ commandType: command.type,+ detail: `Message id '${command.messageId}' uses the reserved imported-session namespace.`,+ });+ }yield* requireThread({readModel,command,@@ -1299,6 +1314,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"}case "thread.message.assistant.complete": {+ if (isImportedAgentSessionMessageId(command.messageId)) {+ return yield* new OrchestrationCommandInvariantError({+ commandType: command.type,+ detail: `Message id '${command.messageId}' uses the reserved imported-session namespace.`,+ });+ }yield* requireThread({readModel,command,@@ -1325,6 +1346,79 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"};}+ case "thread.history.import": {+ const thread = yield* requireThread({+ readModel,+ command,+ threadId: command.threadId,+ });+ if (+ thread.deletedAt !== null ||+ thread.archivedAt !== null ||+ thread.messages.length > 0 ||+ thread.latestTurn !== null ||+ thread.session !== null ||+ hasOpenBlockingRequest(thread)+ ) {+ return yield* new OrchestrationCommandInvariantError({+ commandType: command.type,+ detail: `Thread '${command.threadId}' must be active and empty before history can be imported.`,+ });+ }+ const firstMessage = command.messages[0];+ if (firstMessage === undefined) {+ return yield* new OrchestrationCommandInvariantError({+ commandType: command.type,+ detail: "Thread history imports require at least one message.",+ });+ }++ const events: Array<PlannedOrchestrationEvent> = [];+ for (const message of command.messages) {+ events.push({+ ...(yield* withEventBase({+ aggregateKind: "thread",+ aggregateId: command.threadId,+ occurredAt: message.createdAt,+ commandId: command.commandId,+ metadata: { historyImport: true },+ })),+ type: "thread.message-sent",+ payload: {+ threadId: command.threadId,+ messageId: message.messageId,+ role: message.role,+ text: message.text,+ turnId: null,+ streaming: false,+ createdAt: message.createdAt,+ updatedAt: message.createdAt,+ },+ });+ }+ const settledAt = command.messages.reduce(+ (latest, message) =>+ compareDateTimeStrings(message.createdAt, latest) > 0 ? message.createdAt : latest,+ firstMessage.createdAt,+ );+ events.push({+ ...(yield* withEventBase({+ aggregateKind: "thread",+ aggregateId: command.threadId,+ occurredAt: settledAt,+ commandId: command.commandId,+ metadata: { historyImport: true },+ })),+ type: "thread.settled",+ payload: {+ threadId: command.threadId,+ settledAt,+ updatedAt: settledAt,+ },+ });+ return events;+ }+case "thread.proposed-plan.upsert": {yield* requireThread({readModel,diff --git a/apps/server/src/orchestration/projector.ts b/apps/server/src/orchestration/projector.tsindex a558e0ad7af..77dbe51b954 100644--- a/apps/server/src/orchestration/projector.ts+++ b/apps/server/src/orchestration/projector.ts@@ -1,10 +1,12 @@import type { OrchestrationEvent, OrchestrationReadModel, ThreadId } from "@t3tools/contracts";import {+ isImportedAgentSessionMessageId,OrchestrationCheckpointSummary,OrchestrationMessage,OrchestrationSession,OrchestrationThread,} from "@t3tools/contracts";+import { compareDateTimeStrings } from "@t3tools/shared/dateTime";import * as Effect from "effect/Effect";import * as Schema from "effect/Schema";import * as Predicate from "effect/Predicate";@@ -117,7 +119,7 @@ function retainThreadMessagesAfterRevert(): ReadonlyArray<OrchestrationMessage> {const retainedMessageIds = new Set<string>();for (const message of messages) {- if (message.role === "system") {+ if (message.role === "system" || isImportedAgentSessionMessageId(message.id)) {retainedMessageIds.add(message.id);continue;}@@ -127,7 +129,10 @@ function retainThreadMessagesAfterRevert(}const retainedUserCount = messages.filter(- (message) => message.role === "user" && retainedMessageIds.has(message.id),+ (message) =>+ message.role === "user" &&+ !isImportedAgentSessionMessageId(message.id) &&+ retainedMessageIds.has(message.id),).length;const missingUserCount = Math.max(0, turnCount - retainedUserCount);if (missingUserCount > 0) {@@ -140,7 +145,8 @@ function retainThreadMessagesAfterRevert().toSorted((left, right) =>- left.createdAt.localeCompare(right.createdAt) || left.id.localeCompare(right.id),+ compareDateTimeStrings(left.createdAt, right.createdAt) ||+ left.id.localeCompare(right.id),).slice(0, missingUserCount);for (const message of fallbackUserMessages) {@@ -149,7 +155,10 @@ function retainThreadMessagesAfterRevert(}const retainedAssistantCount = messages.filter(- (message) => message.role === "assistant" && retainedMessageIds.has(message.id),+ (message) =>+ message.role === "assistant" &&+ !isImportedAgentSessionMessageId(message.id) &&+ retainedMessageIds.has(message.id),).length;const missingAssistantCount = Math.max(0, turnCount - retainedAssistantCount);if (missingAssistantCount > 0) {@@ -162,7 +171,8 @@ function retainThreadMessagesAfterRevert().toSorted((left, right) =>- left.createdAt.localeCompare(right.createdAt) || left.id.localeCompare(right.id),+ compareDateTimeStrings(left.createdAt, right.createdAt) ||+ left.id.localeCompare(right.id),).slice(0, missingAssistantCount);for (const message of fallbackAssistantMessages) {diff --git a/apps/server/src/persistence/Layers/ProjectionThreadMessages.test.ts b/apps/server/src/persistence/Layers/ProjectionThreadMessages.test.tsindex c8fa16158ba..d4a70af59be 100644--- a/apps/server/src/persistence/Layers/ProjectionThreadMessages.test.ts+++ b/apps/server/src/persistence/Layers/ProjectionThreadMessages.test.ts@@ -12,12 +12,24 @@ const layer = it.layer();layer("ProjectionThreadMessageRepository", (it) => {- it.effect("finds the latest user-message time within one thread", () =>+ it.effect("finds the latest live user-message time within one thread", () =>Effect.gen(function* () {const repository = yield* ProjectionThreadMessageRepository;const threadId = ThreadId.make("thread-latest-user-message");assert.isNull(yield* repository.getLatestUserMessageAt({ threadId }));+ yield* repository.upsert({+ messageId: MessageId.make("import:codex:latest-user-message:000000"),+ threadId,+ turnId: null,+ role: "user",+ text: "Imported prompt",+ isStreaming: false,+ createdAt: "2026-02-28T19:05:06.000Z",+ updatedAt: "2026-02-28T19:05:06.000Z",+ });+ assert.isNull(yield* repository.getLatestUserMessageAt({ threadId }));+const messages = [{ role: "user", createdAt: "2026-02-28T19:05:02.000Z" },{ role: "user", createdAt: "2026-02-28T19:05:01.000Z" },diff --git a/apps/server/src/persistence/Layers/ProjectionThreadMessages.ts b/apps/server/src/persistence/Layers/ProjectionThreadMessages.tsindex ce28e11b860..be20fb37f36 100644--- a/apps/server/src/persistence/Layers/ProjectionThreadMessages.ts+++ b/apps/server/src/persistence/Layers/ProjectionThreadMessages.ts@@ -191,6 +191,7 @@ const makeProjectionThreadMessageRepository = Effect.gen(function* () {SELECT MAX(created_at) AS "latestUserMessageAt"FROM projection_thread_messagesWHERE thread_id = ${threadId} AND role = 'user'+ AND message_id NOT GLOB 'import:*'`,});diff --git a/apps/server/src/persistence/ProviderSessionRuntime.ts b/apps/server/src/persistence/ProviderSessionRuntime.tsindex 2ccdd862522..d73f56aab9e 100644--- a/apps/server/src/persistence/ProviderSessionRuntime.ts+++ b/apps/server/src/persistence/ProviderSessionRuntime.ts@@ -10,6 +10,7 @@ import * as SqlClient from "effect/unstable/sql/SqlClient";import * as SqlSchema from "effect/unstable/sql/SqlSchema";import {+ AgentSessionImportSource,IsoDateTime,ProviderInstanceId,ProviderSessionRuntimeStatus,@@ -58,6 +59,16 @@ export type GetProviderSessionRuntimeInput = typeof GetProviderSessionRuntimeInpexport const DeleteProviderSessionRuntimeInput = Schema.Struct({ threadId: ThreadId });export type DeleteProviderSessionRuntimeInput = typeof DeleteProviderSessionRuntimeInput.Type;+export const RecordImportedTranscriptInput = Schema.Struct({+ threadId: ThreadId,+ source: AgentSessionImportSource,+});+export type RecordImportedTranscriptInput = typeof RecordImportedTranscriptInput.Type;++export interface ProviderSessionRuntimeUpsertOptions {+ readonly onConflict?: "update" | "ignore";+}+/*** ProviderSessionRuntimeRepository - Service tag for provider runtime persistence.*/@@ -67,10 +78,17 @@ export class ProviderSessionRuntimeRepository extends Context.Service</*** Insert or replace a provider runtime row.*- * Upserts by canonical `threadId`, including JSON payload/cursor fields.+ * Upserts by canonical `threadId`, retaining imported transcript records+ * from the current database row.*/readonly upsert: (runtime: ProviderSessionRuntime,+ options?: ProviderSessionRuntimeUpsertOptions,+ ) => Effect.Effect<void, ProviderSessionRuntimeRepositoryError>;++ /** Record one source file without replacing the current session state. */+ readonly recordImportedTranscript: (+ input: RecordImportedTranscriptInput,) => Effect.Effect<void, ProviderSessionRuntimeRepositoryError>;/**@@ -129,6 +147,10 @@ const GetRuntimeRequestSchema = Schema.Struct({const DeleteRuntimeRequestSchema = GetRuntimeRequestSchema;+const RecordImportedTranscriptRequestSchema = RecordImportedTranscriptInput.mapFields(+ Struct.assign({ source: Schema.fromJsonString(AgentSessionImportSource) }),+);+function toPersistenceSqlOrDecodeError(sqlOperation: string,decodeOperation: string,@@ -147,6 +169,8 @@ function toPersistenceSqlOrDecodeError(export const make = Effect.gen(function* () {const sql = yield* SqlClient.SqlClient;+ // Runtime writes can carry stale payloads. Only recordImportedTranscript may+ // change source records, so restore that field from the row being updated.const upsertRuntimeRow = SqlSchema.void({Request: ProviderSessionRuntimeDbRowSchema,execute: (runtime) =>@@ -171,7 +195,11 @@ export const make = Effect.gen(function* () {${runtime.status},${runtime.lastSeenAt},${runtime.resumeCursor},- ${runtime.runtimePayload}+ CASE+ WHEN json_type(${runtime.runtimePayload}) = 'object'+ THEN json_remove(${runtime.runtimePayload}, '$.importedTranscripts')+ ELSE ${runtime.runtimePayload}+ END)ON CONFLICT (thread_id)DO UPDATE SET@@ -182,7 +210,107 @@ export const make = Effect.gen(function* () {status = excluded.status,last_seen_at = excluded.last_seen_at,resume_cursor_json = excluded.resume_cursor_json,- runtime_payload_json = excluded.runtime_payload_json+ runtime_payload_json = CASE+ WHEN json_type(+ CASE+ WHEN json_valid(provider_session_runtime.runtime_payload_json)+ THEN provider_session_runtime.runtime_payload_json+ ELSE '{}'+ END,+ '$.importedTranscripts'+ ) IS NOT NULL+ THEN json_set(+ CASE+ WHEN json_type(excluded.runtime_payload_json) = 'object'+ THEN excluded.runtime_payload_json+ ELSE '{}'+ END,+ '$.importedTranscripts',+ json_extract(provider_session_runtime.runtime_payload_json, '$.importedTranscripts')+ )+ ELSE excluded.runtime_payload_json+ END+ `,+ });++ const insertRuntimeRow = SqlSchema.void({+ Request: ProviderSessionRuntimeDbRowSchema,+ execute: (runtime) =>+ sql`+ INSERT INTO provider_session_runtime (+ thread_id,+ provider_name,+ provider_instance_id,+ adapter_key,+ runtime_mode,+ status,+ last_seen_at,+ resume_cursor_json,+ runtime_payload_json+ )+ VALUES (+ ${runtime.threadId},+ ${runtime.providerName},+ ${runtime.providerInstanceId},+ ${runtime.adapterKey},+ ${runtime.runtimeMode},+ ${runtime.status},+ ${runtime.lastSeenAt},+ ${runtime.resumeCursor},+ CASE+ WHEN json_type(${runtime.runtimePayload}) = 'object'+ THEN json_remove(${runtime.runtimePayload}, '$.importedTranscripts')+ ELSE ${runtime.runtimePayload}+ END+ )+ ON CONFLICT (thread_id) DO NOTHING+ `,+ });++ const recordImportedTranscriptRow = SqlSchema.void({+ Request: RecordImportedTranscriptRequestSchema,+ execute: ({ threadId, source }) =>+ sql`+ WITH current_runtime AS (+ SELECT CASE+ WHEN json_valid(runtime_payload_json) THEN CASE+ WHEN json_type(runtime_payload_json) = 'object' THEN runtime_payload_json+ ELSE '{}'+ END+ ELSE '{}'+ END AS payload+ FROM provider_session_runtime+ WHERE thread_id = ${threadId}+ )+ UPDATE provider_session_runtime+ SET runtime_payload_json = (+ SELECT json_set(+ payload,+ '$.importedTranscripts',+ json((+ SELECT json_group_array(json(value))+ FROM (+ SELECT value+ FROM json_each(CASE+ WHEN json_type(payload, '$.importedTranscripts') = 'array'+ THEN json_extract(payload, '$.importedTranscripts')+ ELSE '[]'+ END)+ WHERE CASE+ WHEN type = 'object' THEN+ json_extract(value, '$.providerInstanceId')+ IS NOT json_extract(${source}, '$.providerInstanceId')+ OR json_extract(value, '$.filePath') IS NOT json_extract(${source}, '$.filePath')+ ELSE 0+ END+ UNION ALL+ SELECT ${source} AS value+ )+ ))+ )+ FROM current_runtime+ )+ WHERE thread_id = ${threadId}`,});@@ -235,8 +363,8 @@ export const make = Effect.gen(function* () {`,});- const upsert: ProviderSessionRuntimeRepository["Service"]["upsert"] = (runtime) =>- upsertRuntimeRow(runtime).pipe(+ const upsert: ProviderSessionRuntimeRepository["Service"]["upsert"] = (runtime, options) =>+ (options?.onConflict === "ignore" ? insertRuntimeRow(runtime) : upsertRuntimeRow(runtime)).pipe(Effect.mapError(toPersistenceSqlOrDecodeError("ProviderSessionRuntimeRepository.upsert:query",@@ -246,6 +374,18 @@ export const make = Effect.gen(function* () {),);+ const recordImportedTranscript: ProviderSessionRuntimeRepository["Service"]["recordImportedTranscript"] =+ (input) =>+ recordImportedTranscriptRow(input).pipe(+ Effect.mapError(+ toPersistenceSqlOrDecodeError(+ "ProviderSessionRuntimeRepository.recordImportedTranscript:query",+ "ProviderSessionRuntimeRepository.recordImportedTranscript:encodeRequest",+ { threadId: input.threadId },+ ),+ ),+ );+const getByThreadId: ProviderSessionRuntimeRepository["Service"]["getByThreadId"] = (input) =>getRuntimeRowByThreadId(input).pipe(Effect.mapError(@@ -324,6 +464,7 @@ export const make = Effect.gen(function* () {return {upsert,+ recordImportedTranscript,getByThreadId,list,deleteByThreadId,diff --git a/apps/server/src/project/AgentSessionImporter.test.ts b/apps/server/src/project/AgentSessionImporter.test.tsnew file mode 100644index 00000000000..38d6ad1d433--- /dev/null+++ b/apps/server/src/project/AgentSessionImporter.test.ts@@ -0,0 +1,1213 @@+import * as NodeServices from "@effect/platform-node/NodeServices";+import { describe, expect, it, vi } from "@effect/vitest";+import {+ AgentSessionImportProjectChangedError,+ CommandId,+ MessageId,+ ProjectId,+ ProviderDriverKind,+ ProviderInstanceId,+ ThreadId,+ type OrchestrationCommand,+ type OrchestrationProjectShell,+ type OrchestrationThread,+ type ProviderSendTurnInput,+} from "@t3tools/contracts";+import * as Effect from "effect/Effect";+import * as Deferred from "effect/Deferred";+import * as Fiber from "effect/Fiber";+import * as FileSystem from "effect/FileSystem";+import * as Layer from "effect/Layer";+import * as Option from "effect/Option";+import * as Path from "effect/Path";+import * as Schema from "effect/Schema";+import * as Stream from "effect/Stream";+import * as TestClock from "effect/testing/TestClock";++import { makeTestProviderAdapterHarness } from "../../integration/TestProviderAdapter.integration.ts";+import { ServerConfig } from "../config.ts";+import { GitWorkflowService } from "../git/GitWorkflowService.ts";+import { OrchestrationCommandReceiptRepositoryLive } from "../persistence/Layers/OrchestrationCommandReceipts.ts";+import { OrchestrationEventStoreLive } from "../persistence/Layers/OrchestrationEventStore.ts";+import { SqlitePersistenceMemory } from "../persistence/Layers/Sqlite.ts";+import * as ProviderSessionRuntime from "../persistence/ProviderSessionRuntime.ts";+import { OrchestrationEngineLive } from "../orchestration/Layers/OrchestrationEngine.ts";+import { OrchestrationProjectionPipelineLive } from "../orchestration/Layers/ProjectionPipeline.ts";+import { OrchestrationProjectionSnapshotQueryLive } from "../orchestration/Layers/ProjectionSnapshotQuery.ts";+import { ProviderCommandReactorLive } from "../orchestration/Layers/ProviderCommandReactor.ts";+import { OrchestrationCommandInvariantError } from "../orchestration/Errors.ts";+import * as ThreadBackgroundLiveness from "../orchestration/ThreadBackgroundLiveness.ts";+import * as ThreadPlanProgress from "../orchestration/ThreadPlanProgress.ts";+import * as OrchestrationEngine from "../orchestration/Services/OrchestrationEngine.ts";+import * as ProjectionSnapshotQuery from "../orchestration/Services/ProjectionSnapshotQuery.ts";+import { ProviderCommandReactor } from "../orchestration/Services/ProviderCommandReactor.ts";+import { ProviderSessionDirectoryLive } from "../provider/Layers/ProviderSessionDirectory.ts";+import { makeProviderServiceLive } from "../provider/Layers/ProviderService.ts";+import {+ NoOpProviderEventLoggers,+ ProviderEventLoggers,+} from "../provider/Layers/ProviderEventLoggers.ts";+import { ProviderSessionDirectoryPersistenceError } from "../provider/Errors.ts";+import { ProviderAdapterRegistry } from "../provider/Services/ProviderAdapterRegistry.ts";+import { ProviderAuthService } from "../provider/Services/ProviderAuthService.ts";+import * as ProviderSessionDirectory from "../provider/Services/ProviderSessionDirectory.ts";+import { makeAdapterRegistryMock } from "../provider/testUtils/providerAdapterRegistryMock.ts";+import { makeProviderRegistryLayer } from "../provider/testUtils/providerRegistryMock.ts";+import { ServerSettingsService } from "../serverSettings.ts";+import * as AnalyticsService from "../telemetry/AnalyticsService.ts";+import { TextGeneration } from "../textGeneration/TextGeneration.ts";+import { VcsStatusBroadcaster } from "../vcs/VcsStatusBroadcaster.ts";+import * as RepositoryIdentityResolver from "./RepositoryIdentityResolver.ts";+import { importRecentAgentThreads } from "./AgentSessionImporter.ts";+import * as AgentSessionScanner from "./AgentSessionScanner.ts";++const PROJECT_ID = ProjectId.make("project-1");+const WORKSPACE_ROOT = "/tmp/project-from-server";+const CLAUDE_SESSION_ID = "123e4567-e89b-42d3-a456-426614174000";+const encodeTranscriptRecord = Schema.encodeUnknownSync(Schema.fromJsonString(Schema.Unknown));++const makeThread = (source: "codex" | "claudeAgent"): AgentSessionScanner.AgentSessionThread => ({+ source,+ providerInstanceId: ProviderInstanceId.make(source),+ providerSessionId: source === "codex" ? "codex-session" : CLAUDE_SESSION_ID,+ title: `Imported ${source} thread`,+ model: null,+ createdAt: "2026-08-24T10:00:00.000Z",+ updatedAt: "2026-08-24T10:01:00.000Z",+ messages: [+ { role: "user", text: "Fix the bug", createdAt: "2026-08-24T10:00:00.000Z" },+ { role: "assistant", text: "Fixed", createdAt: "2026-08-24T10:01:00.000Z" },+ ],+});++const makeThreadOutcome = (thread: AgentSessionScanner.AgentSessionThread) =>+ ({+ _tag: "Importable",+ thread,+ source: {+ provider: thread.source,+ providerInstanceId: thread.providerInstanceId,+ providerSessionId: thread.providerSessionId,+ filePath: `/tmp/transcripts/${thread.providerInstanceId}/${thread.providerSessionId}.jsonl`,+ size: 0,+ mtimeMs: 0,+ device: 0,+ inode: 0,+ birthtimeMs: 0,+ },+ }) satisfies AgentSessionScanner.AgentSessionRecentThread;++const makeProject = (): OrchestrationProjectShell => ({+ id: PROJECT_ID,+ title: "Project",+ workspaceRoot: WORKSPACE_ROOT,+ defaultModelSelection: null,+ scripts: [],+ createdAt: "2026-08-24T09:00:00.000Z",+ updatedAt: "2026-08-24T09:00:00.000Z",+});++const makeProjectedThread = (input: {+ readonly source: "codex" | "claudeAgent";+ readonly projectId?: ProjectId;+ readonly imported?: boolean;+ readonly includeFollowup?: boolean;+}): OrchestrationThread => {+ const sourceThread = makeThread(input.source);+ const threadId = ThreadId.make(+ `import:${sourceThread.providerInstanceId}:${sourceThread.providerSessionId}`,+ );+ return {+ id: threadId,+ projectId: input.projectId ?? PROJECT_ID,+ title: sourceThread.title,+ modelSelection: { instanceId: sourceThread.providerInstanceId, model: "default" },+ runtimeMode: "full-access",+ interactionMode: "default",+ branch: null,+ worktreePath: null,+ latestTurn: null,+ createdAt: sourceThread.createdAt,+ updatedAt: sourceThread.updatedAt,+ archivedAt: null,+ settledOverride: null,+ settledAt: null,+ deletedAt: null,+ messages: input.imported+ ? [+ {+ id: MessageId.make(`${threadId}:000000`),+ role: "user",+ text: "Fix the bug",+ turnId: null,+ streaming: false,+ createdAt: "2026-08-24T10:00:00.000Z",+ updatedAt: "2026-08-24T10:00:00.000Z",+ },+ ...(input.includeFollowup+ ? [+ {+ id: MessageId.make("user-followup"),+ role: "user" as const,+ text: "Keep going",+ turnId: null,+ streaming: false,+ createdAt: "2026-08-24T10:02:00.000Z",+ updatedAt: "2026-08-24T10:02:00.000Z",+ },+ ]+ : []),+ ]+ : [],+ proposedPlans: [],+ activities: [],+ checkpoints: [],+ session: null,+ };+};++const makeSnapshotsLayer = (input: {+ readonly project?: OrchestrationProjectShell;+ readonly getThread?: (threadId: ThreadId) => Option.Option<OrchestrationThread>;+}) =>+ Layer.mock(ProjectionSnapshotQuery.ProjectionSnapshotQuery)({+ getProjectShellById: () =>+ Effect.succeed(input.project === undefined ? Option.none() : Option.some(input.project)),+ getImportedAgentSessionSources: () => Effect.succeed([]),+ getThreadDetailById: (threadId) => Effect.succeed(input.getThread?.(threadId) ?? Option.none()),+ });++const runImport = (input: {+ readonly scanner: AgentSessionScanner.AgentSessionScanner["Service"];+ readonly engine: OrchestrationEngine.OrchestrationEngineService["Service"];+ readonly directory: ProviderSessionDirectory.ProviderSessionDirectory["Service"];+ readonly snapshots: ReturnType<typeof makeSnapshotsLayer>;+ readonly expectedWorkspaceRoot?: string;+}) =>+ importRecentAgentThreads({+ projectId: PROJECT_ID,+ ...(input.expectedWorkspaceRoot === undefined+ ? {}+ : { expectedWorkspaceRoot: input.expectedWorkspaceRoot }),+ }).pipe(+ Effect.provideService(AgentSessionScanner.AgentSessionScanner, input.scanner),+ Effect.provideService(OrchestrationEngine.OrchestrationEngineService, input.engine),+ Effect.provideService(ProviderSessionDirectory.ProviderSessionDirectory, input.directory),+ Effect.provide(input.snapshots),+ );++it.layer(NodeServices.layer)("AgentSessionImporter", (it) => {+ describe("importRecentAgentThreads", () => {+ it.effect("uses the project root and stores provider-specific resume cursors", () =>+ Effect.gen(function* () {+ const commands: Array<OrchestrationCommand> = [];+ const bindings: Array<ProviderSessionDirectory.ProviderRuntimeBinding> = [];+ let scannedRoot: string | undefined;+ const scanner = AgentSessionScanner.AgentSessionScanner.of({+ scan: Effect.die("unused"),+ recentThreads: (workspaceRoot) => {+ scannedRoot = workspaceRoot;+ return Stream.concat(+ Stream.succeed(makeThreadOutcome(makeThread("codex"))),+ Stream.fromEffect(+ Effect.sync(() => {+ expect(bindings).toHaveLength(1);+ return makeThreadOutcome(makeThread("claudeAgent"));+ }),+ ),+ );+ },+ });+ const engine = OrchestrationEngine.OrchestrationEngineService.of({+ dispatch: (command) => Effect.sync(() => ({ sequence: commands.push(command) })),+ readEvents: () => Stream.empty,+ readThreadEvents: () => Stream.empty,+ getThreadReplayStats: () => Effect.die("unused"),+ streamDomainEvents: Stream.empty,+ subscribeDomainEvents: Effect.succeed(Stream.empty),+ latestSequence: Effect.succeed(0),+ });+ const directory = ProviderSessionDirectory.ProviderSessionDirectory.of({+ upsert: (binding) => Effect.sync(() => void bindings.push(binding)),+ getProvider: () => Effect.die("unused"),+ recordImportedTranscript: () => Effect.void,+ getBinding: () => Effect.succeed(Option.none()),+ listThreadIds: () => Effect.die("unused"),+ listBindings: () => Effect.die("unused"),+ });++ const result = yield* runImport({+ scanner,+ engine,+ directory,+ snapshots: makeSnapshotsLayer({ project: makeProject() }),+ expectedWorkspaceRoot: `${WORKSPACE_ROOT}/`,+ });++ expect(result).toEqual({ importedCount: 2, skippedCount: 0 });+ expect(scannedRoot).toBe(WORKSPACE_ROOT);+ expect(commands.map((command) => command.type)).toEqual([+ "thread.create",+ "thread.history.import",+ "thread.create",+ "thread.history.import",+ ]);+ expect(commands.filter((command) => command.type === "thread.create")).toMatchObject([+ { historyImport: true },+ { historyImport: true },+ ]);+ expect(+ commands+ .filter((command) => command.type === "thread.history.import")+ .flatMap((command) => command.messages.map((message) => message.messageId)),+ ).toEqual([+ "import:codex:codex-session:000000",+ "import:codex:codex-session:000001",+ `import:claudeAgent:${CLAUDE_SESSION_ID}:000000`,+ `import:claudeAgent:${CLAUDE_SESSION_ID}:000001`,+ ]);+ expect(bindings).toMatchObject([+ {+ provider: "codex",+ providerInstanceId: "codex",+ resumeCursor: { threadId: "codex-session" },+ runtimePayload: { cwd: WORKSPACE_ROOT },+ },+ {+ provider: "claudeAgent",+ providerInstanceId: "claudeAgent",+ resumeCursor: {+ threadId: `import:claudeAgent:${CLAUDE_SESSION_ID}`,+ resume: CLAUDE_SESSION_ID,+ },+ runtimePayload: { cwd: WORKSPACE_ROOT },+ },+ ]);+ }),+ );++ it.effect("rejects a changed project root before scanning or writing", () =>+ Effect.gen(function* () {+ const recentThreads = vi.fn(() => Stream.empty);+ const error = yield* importRecentAgentThreads({+ projectId: PROJECT_ID,+ expectedWorkspaceRoot: WORKSPACE_ROOT,+ }).pipe(+ Effect.provideService(+ AgentSessionScanner.AgentSessionScanner,+ AgentSessionScanner.AgentSessionScanner.of({+ scan: Effect.die("must not scan a changed project"),+ recentThreads,+ }),+ ),+ Effect.provide(+ Layer.mergeAll(+ Layer.mock(OrchestrationEngine.OrchestrationEngineService)({}),+ Layer.mock(ProviderSessionDirectory.ProviderSessionDirectory)({}),+ makeSnapshotsLayer({+ project: { ...makeProject(), workspaceRoot: "/tmp/project-moved" },+ }),+ ),+ ),+ Effect.flip,+ );++ expect(error).toEqual(new AgentSessionImportProjectChangedError({ projectId: PROJECT_ID }));+ expect(recentThreads).not.toHaveBeenCalled();+ }),+ );++ it.effect("counts scanner skips without writing a thread or binding", () =>+ Effect.gen(function* () {+ const scanner = AgentSessionScanner.AgentSessionScanner.of({+ scan: Effect.die("unused"),+ recentThreads: () => Stream.succeed({ _tag: "Skipped" }),+ });+ const engine = OrchestrationEngine.OrchestrationEngineService.of({+ dispatch: () => Effect.die("must not dispatch for a scanner skip"),+ readEvents: () => Stream.empty,+ readThreadEvents: () => Stream.empty,+ getThreadReplayStats: () => Effect.die("unused"),+ streamDomainEvents: Stream.empty,+ subscribeDomainEvents: Effect.succeed(Stream.empty),+ latestSequence: Effect.succeed(0),+ });+ const directory = ProviderSessionDirectory.ProviderSessionDirectory.of({+ upsert: () => Effect.die("must not bind a scanner skip"),+ getProvider: () => Effect.die("unused"),+ recordImportedTranscript: () => Effect.die("unused"),+ getBinding: () => Effect.die("must not read a scanner skip binding"),+ listThreadIds: () => Effect.die("unused"),+ listBindings: () => Effect.die("unused"),+ });++ const result = yield* runImport({+ scanner,+ engine,+ directory,+ snapshots: makeSnapshotsLayer({ project: makeProject() }),+ });++ expect(result).toEqual({ importedCount: 0, skippedCount: 1 });+ }),+ );++ it.effect("recovers after a rejected history receipt and a failed binding write", () =>+ Effect.gen(function* () {+ let threadCreated = false;+ let historyImported = false;+ let historyAttemptCount = 0;+ let bindingAttemptCount = 0;+ const rejectedCommandIds = new Set<string>();+ const bindings: Array<ProviderSessionDirectory.ProviderRuntimeBinding> = [];+ const scanner = AgentSessionScanner.AgentSessionScanner.of({+ scan: Effect.die("unused"),+ recentThreads: () => Stream.fromIterable([makeThreadOutcome(makeThread("codex"))]),+ });+ const engine = OrchestrationEngine.OrchestrationEngineService.of({+ dispatch: (command) => {+ if (rejectedCommandIds.has(command.commandId)) {+ return Effect.fail(+ new OrchestrationCommandInvariantError({+ commandType: command.type,+ detail: "Previously rejected.",+ }),+ );+ }+ if (command.type === "thread.create") threadCreated = true;+ if (command.type === "thread.history.import") {+ historyAttemptCount += 1;+ if (historyAttemptCount === 1) {+ rejectedCommandIds.add(command.commandId);+ return Effect.fail(+ new OrchestrationCommandInvariantError({+ commandType: command.type,+ detail: "Temporary history import failure.",+ }),+ );+ }+ historyImported = true;+ }+ return Effect.succeed({ sequence: 1 });+ },+ readEvents: () => Stream.empty,+ readThreadEvents: () => Stream.empty,+ getThreadReplayStats: () => Effect.die("unused"),+ streamDomainEvents: Stream.empty,+ subscribeDomainEvents: Effect.succeed(Stream.empty),+ latestSequence: Effect.succeed(0),+ });+ const directory = ProviderSessionDirectory.ProviderSessionDirectory.of({+ upsert: (binding) => {+ bindingAttemptCount += 1;+ if (bindingAttemptCount === 1) {+ return Effect.fail(+ new ProviderSessionDirectoryPersistenceError({+ operation: "upsert",+ detail: "Temporary session storage failure.",+ }),+ );+ }+ bindings.push(binding);+ return Effect.void;+ },+ getProvider: () => Effect.die("unused"),+ recordImportedTranscript: () => Effect.void,+ getBinding: () =>+ Effect.succeed(bindings[0] === undefined ? Option.none() : Option.some(bindings[0])),+ listThreadIds: () => Effect.die("unused"),+ listBindings: () => Effect.die("unused"),+ });+ const snapshots = makeSnapshotsLayer({+ project: makeProject(),+ getThread: () =>+ threadCreated+ ? Option.some(makeProjectedThread({ source: "codex", imported: historyImported }))+ : Option.none(),+ });+ const importOnce = () => runImport({ scanner, engine, directory, snapshots });++ expect(yield* importOnce()).toEqual({ importedCount: 0, skippedCount: 1 });+ expect(yield* importOnce()).toEqual({ importedCount: 0, skippedCount: 1 });+ expect(yield* importOnce()).toEqual({ importedCount: 1, skippedCount: 0 });+ const historyAttemptsAfterCompletion = historyAttemptCount;+ expect(yield* importOnce()).toEqual({ importedCount: 1, skippedCount: 0 });+ expect(historyAttemptCount).toBe(historyAttemptsAfterCompletion);+ expect(historyAttemptCount).toBe(2);+ expect(bindings).toHaveLength(1);+ }),+ );++ it.effect("does not replace completed history or an active binding on retry", () =>+ Effect.gen(function* () {+ const scanner = AgentSessionScanner.AgentSessionScanner.of({+ scan: Effect.die("unused"),+ recentThreads: () => Stream.fromIterable([makeThreadOutcome(makeThread("codex"))]),+ });+ const runningBinding: ProviderSessionDirectory.ProviderRuntimeBinding = {+ threadId: ThreadId.make("import:codex:codex-session"),+ provider: ProviderDriverKind.make("codex"),+ providerInstanceId: ProviderInstanceId.make("codex"),+ status: "running",+ resumeCursor: { threadId: "newer-codex-session" },+ };+ const directory = ProviderSessionDirectory.ProviderSessionDirectory.of({+ upsert: () => Effect.die("must not replace an active binding"),+ getProvider: () => Effect.die("unused"),+ recordImportedTranscript: () => Effect.void,+ getBinding: () => Effect.succeed(Option.some(runningBinding)),+ listThreadIds: () => Effect.die("unused"),+ listBindings: () => Effect.die("unused"),+ });+ const engine = OrchestrationEngine.OrchestrationEngineService.of({+ dispatch: () => Effect.die("must not replay history or settle active work"),+ readEvents: () => Stream.empty,+ readThreadEvents: () => Stream.empty,+ getThreadReplayStats: () => Effect.die("unused"),+ streamDomainEvents: Stream.empty,+ subscribeDomainEvents: Effect.succeed(Stream.empty),+ latestSequence: Effect.succeed(0),+ });++ const result = yield* runImport({+ scanner,+ engine,+ directory,+ snapshots: makeSnapshotsLayer({+ project: makeProject(),+ getThread: () =>+ Option.some(+ makeProjectedThread({ source: "codex", imported: true, includeFollowup: true }),+ ),+ }),+ });++ expect(result).toEqual({ importedCount: 1, skippedCount: 0 });+ }),+ );++ it.effect("skips malformed Claude ids and wrong-project thread collisions", () =>+ Effect.gen(function* () {+ const scanner = AgentSessionScanner.AgentSessionScanner.of({+ scan: Effect.die("unused"),+ recentThreads: () =>+ Stream.fromIterable([+ makeThreadOutcome({ ...makeThread("claudeAgent"), providerSessionId: "not-a-uuid" }),+ makeThreadOutcome(makeThread("codex")),+ ]),+ });+ const commands: Array<OrchestrationCommand> = [];+ const engine = OrchestrationEngine.OrchestrationEngineService.of({+ dispatch: (command) => Effect.sync(() => ({ sequence: commands.push(command) })),+ readEvents: () => Stream.empty,+ readThreadEvents: () => Stream.empty,+ getThreadReplayStats: () => Effect.die("unused"),+ streamDomainEvents: Stream.empty,+ subscribeDomainEvents: Effect.succeed(Stream.empty),+ latestSequence: Effect.succeed(0),+ });+ const directory = ProviderSessionDirectory.ProviderSessionDirectory.of({+ upsert: () => Effect.die("must not bind malformed or wrong-project sessions"),+ getProvider: () => Effect.die("unused"),+ recordImportedTranscript: () => Effect.die("unused"),+ getBinding: () => Effect.succeed(Option.none()),+ listThreadIds: () => Effect.die("unused"),+ listBindings: () => Effect.die("unused"),+ });++ const result = yield* runImport({+ scanner,+ engine,+ directory,+ snapshots: makeSnapshotsLayer({+ project: makeProject(),+ getThread: (threadId) =>+ threadId === "import:codex:codex-session"+ ? Option.some(+ makeProjectedThread({+ source: "codex",+ projectId: ProjectId.make("project-other"),+ }),+ )+ : Option.none(),+ }),+ });++ expect(result).toEqual({ importedCount: 0, skippedCount: 2 });+ expect(commands).toHaveLength(0);+ }),+ );+ });+});++const integrationThread = {+ ...makeThread("codex"),+ updatedAt: "2026-08-24T10:00:00.000Z",+ messages: Array.from({ length: 12 }, (_, index) => ({+ role: index % 2 === 0 ? ("user" as const) : ("assistant" as const),+ text: `Message ${index}`,+ createdAt: "2026-08-24T10:00:00.000Z",+ })),+};+const integrationScanner = AgentSessionScanner.AgentSessionScanner.of({+ scan: Effect.die("unused"),+ recentThreads: () => Stream.fromIterable([makeThreadOutcome(integrationThread)]),+});+const integrationServerConfig = ServerConfig.layerTest(process.cwd(), {+ prefix: "t3-agent-session-importer-test-",+});+const integrationRuntimeRepository = ProviderSessionRuntime.layer.pipe(+ Layer.provide(SqlitePersistenceMemory),+);+const integrationLayer = Layer.mergeAll(+ OrchestrationEngineLive.pipe(+ Layer.provide(OrchestrationProjectionSnapshotQueryLive),+ Layer.provide(OrchestrationProjectionPipelineLive),+ ),+ OrchestrationProjectionSnapshotQueryLive,+ integrationRuntimeRepository,+ ProviderSessionDirectoryLive.pipe(Layer.provide(integrationRuntimeRepository)),+ Layer.succeed(AgentSessionScanner.AgentSessionScanner, integrationScanner),+).pipe(+ Layer.provide(ThreadBackgroundLiveness.layer),+ Layer.provide(ThreadPlanProgress.layer),+ Layer.provide(OrchestrationEventStoreLive),+ Layer.provide(OrchestrationCommandReceiptRepositoryLive),+ Layer.provide(RepositoryIdentityResolver.layer),+ Layer.provide(SqlitePersistenceMemory),+ Layer.provideMerge(integrationServerConfig),+ Layer.provideMerge(NodeServices.layer),+);++it.layer(integrationLayer)("AgentSessionImporter integration", (it) => {+ it.effect("imports once after the real engine persists an old rejected receipt", () =>+ Effect.gen(function* () {+ const engine = yield* OrchestrationEngine.OrchestrationEngineService;+ const snapshots = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;+ const directory = yield* ProviderSessionDirectory.ProviderSessionDirectory;+ const threadId = ThreadId.make("import:codex:codex-session");++ yield* engine.dispatch({+ type: "project.create",+ commandId: CommandId.make("create-import-integration-project"),+ projectId: PROJECT_ID,+ title: "Project",+ workspaceRoot: WORKSPACE_ROOT,+ defaultModelSelection: null,+ createdAt: "2026-08-24T09:00:00.000Z",+ });+ const rejected = yield* Effect.result(+ engine.dispatch({+ type: "thread.history.import",+ commandId: CommandId.make(`agent-session:history:${threadId}`),+ threadId,+ messages: [+ {+ messageId: MessageId.make(`${threadId}:000000`),+ role: "user",+ text: "Fix the bug",+ createdAt: "2026-08-24T10:00:00.000Z",+ },+ ],+ }),+ );+ expect(rejected._tag).toBe("Failure");++ const result = yield* importRecentAgentThreads({ projectId: PROJECT_ID });+ const importedThread = yield* snapshots.getThreadDetailById(threadId);+ const binding = yield* directory.getBinding(threadId);++ expect(result).toEqual({ importedCount: 1, skippedCount: 0 });+ expect(Option.getOrThrow(importedThread).messages.map((message) => message.text)).toEqual(+ integrationThread.messages.map((message) => message.text),+ );+ expect(Option.getOrThrow(importedThread).settledOverride).toBe("settled");+ expect(Option.getOrThrow(importedThread).updatedAt).toBe("2026-08-24T10:00:00.000Z");+ expect(Option.getOrThrow(binding)).toMatchObject({+ provider: "codex",+ providerInstanceId: "codex",+ resumeCursor: { threadId: "codex-session" },+ runtimePayload: { cwd: WORKSPACE_ROOT },+ });++ yield* engine.dispatch({+ type: "thread.revert.complete",+ commandId: CommandId.make("revert-imported-thread-to-baseline"),+ threadId,+ turnCount: 0,+ createdAt: "2026-08-24T10:05:00.000Z",+ });+ const afterRevert = yield* snapshots.getThreadDetailById(threadId);+ expect(Option.getOrThrow(afterRevert).messages.map((message) => message.text)).toEqual(+ integrationThread.messages.map((message) => message.text),+ );+ }),+ );++ it.effect(+ "retries a bounded import after scanner restart without rereading completed transcripts",+ () =>+ Effect.gen(function* () {+ const engine = yield* OrchestrationEngine.OrchestrationEngineService;+ const snapshots = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;+ const directory = yield* ProviderSessionDirectory.ProviderSessionDirectory;+ const fileSystem = yield* FileSystem.FileSystem;+ const path = yield* Path.Path;+ const nowMs = Date.parse("2026-08-24T12:00:00.000Z");+ yield* TestClock.setTime(nowMs);+ const fixtureDir = yield* fileSystem.makeTempDirectoryScoped({+ prefix: "t3-import-retry-",+ });+ const workspaceRoot = path.join(fixtureDir, "workspace");+ const claudeHomePath = path.join(fixtureDir, "claude");+ const codexHomePath = path.join(fixtureDir, "codex");+ const sessionsDir = path.join(codexHomePath, "sessions", "2026", "08", "24");+ yield* fileSystem.makeDirectory(workspaceRoot);+ yield* fileSystem.makeDirectory(claudeHomePath);+ yield* fileSystem.makeDirectory(sessionsDir, { recursive: true });++ const projectId = ProjectId.make("project-bounded-import-retry");+ const transcripts = Array.from({ length: 101 }, (_, index) => {+ const providerSessionId = `bounded-session-${String(index).padStart(3, "0")}`;+ return {+ providerSessionId,+ threadId: ThreadId.make(`import:codex:${providerSessionId}`),+ filePath: path.join(sessionsDir, `rollout-${providerSessionId}.jsonl`),+ };+ });+ for (const [index, transcript] of transcripts.entries()) {+ yield* fileSystem.writeFileString(+ transcript.filePath,+ [+ encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: transcript.providerSessionId, cwd: workspaceRoot },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: {+ type: "user_message",+ message: `Prompt ${transcript.providerSessionId}`,+ },+ }),+ ].join("\n"),+ );+ const seconds = nowMs / 1_000 - index;+ yield* fileSystem.utimes(transcript.filePath, seconds, seconds);+ }+ const legacy = transcripts[0]!;+ const failed = transcripts[1]!;+ const remaining = transcripts[100]!;+ yield* engine.dispatch({+ type: "project.create",+ commandId: CommandId.make("create-bounded-import-project"),+ projectId,+ title: "Bounded import",+ workspaceRoot,+ defaultModelSelection: null,+ createdAt: "2026-08-24T09:00:00.000Z",+ });++ // This completed import predates persisted transcript source metadata.+ yield* directory.upsert({+ threadId: legacy.threadId,+ provider: ProviderDriverKind.make("codex"),+ providerInstanceId: ProviderInstanceId.make("codex"),+ status: "stopped",+ resumeCursor: { threadId: "legacy-current-session" },+ runtimePayload: { cwd: workspaceRoot },+ });+ yield* engine.dispatch({+ type: "thread.create",+ commandId: CommandId.make("create-legacy-bounded-import"),+ threadId: legacy.threadId,+ projectId,+ title: "Legacy import",+ modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "default" },+ runtimeMode: "full-access",+ interactionMode: "default",+ branch: null,+ worktreePath: null,+ createdAt: "2026-08-24T10:00:00.000Z",+ historyImport: true,+ });+ yield* engine.dispatch({+ type: "thread.history.import",+ commandId: CommandId.make("import-legacy-bounded-history"),+ threadId: legacy.threadId,+ messages: [+ {+ messageId: MessageId.make(`${legacy.threadId}:000000`),+ role: "user",+ text: "Legacy imported history",+ createdAt: "2026-08-24T10:00:00.000Z",+ },+ ],+ });+ expect(yield* snapshots.getImportedAgentSessionSources(projectId)).toEqual([]);++ let failHistory = true;+ const importerEngine = OrchestrationEngine.OrchestrationEngineService.of({+ ...engine,+ dispatch: (command) => {+ if (+ failHistory &&+ command.type === "thread.history.import" &&+ command.threadId === failed.threadId+ ) {+ failHistory = false;+ return Effect.fail(+ new OrchestrationCommandInvariantError({+ commandType: command.type,+ detail: "Injected history import failure.",+ }),+ );+ }+ return engine.dispatch(command);+ },+ });+ const settingsLayer = ServerSettingsService.layerTest({+ providers: {+ claudeAgent: { homePath: claudeHomePath },+ codex: { homePath: codexHomePath },+ },+ });+ const transcriptPaths = new Set(transcripts.map((transcript) => transcript.filePath));+ const runAttempt = Effect.fn("runBoundedImportAttempt")(function* (+ completedPaths: ReadonlySet<string>,+ ) {+ const openCounts = new Map<string, number>();+ const fullReads: string[] = [];+ const observedFileSystem = FileSystem.FileSystem.of({+ ...fileSystem,+ open: (filePath, options) =>+ Effect.suspend(() => {+ if (transcriptPaths.has(filePath)) {+ const count = (openCounts.get(filePath) ?? 0) + 1;+ openCounts.set(filePath, count);+ // A fresh scanner first opens each file for project discovery.+ if (count > 1) {+ fullReads.push(filePath);+ if (completedPaths.has(filePath)) {+ return Effect.die(new Error(`Completed transcript reopened: ${filePath}`));+ }+ }+ }+ return fileSystem.open(filePath, options);+ }),+ });+ const result = yield* importRecentAgentThreads({ projectId }).pipe(+ Effect.provide(+ Layer.fresh(AgentSessionScanner.layer).pipe(+ Layer.provide(settingsLayer),+ Layer.provide(Layer.succeed(FileSystem.FileSystem, observedFileSystem)),+ ),+ ),+ Effect.provideService(OrchestrationEngine.OrchestrationEngineService, importerEngine),+ );+ return { result, fullReads, openCounts };+ });++ const first = yield* runAttempt(new Set());+ expect(first.result).toEqual({ importedCount: 99, skippedCount: 2 });+ expect(failHistory).toBe(false);+ expect(first.fullReads).toEqual(transcripts.slice(0, 100).map((entry) => entry.filePath));+ expect(first.openCounts.get(remaining.filePath)).toBe(1);+ const completedSources = yield* snapshots.getImportedAgentSessionSources(projectId);+ expect(completedSources).toHaveLength(99);+ expect(completedSources).toContainEqual({+ threadId: legacy.threadId,+ source: expect.objectContaining({ filePath: legacy.filePath }),+ });+ expect(+ Option.getOrThrow(yield* snapshots.getThreadDetailById(failed.threadId)).messages,+ ).toEqual([]);+ expect(Option.getOrThrow(yield* directory.getBinding(failed.threadId))).toMatchObject({+ status: "stopped",+ resumeCursor: { threadId: failed.providerSessionId },+ });+ expect(Option.isNone(yield* snapshots.getThreadDetailById(remaining.threadId))).toBe(true);++ const completedPaths = new Set(completedSources.map((entry) => entry.source.filePath));+ const second = yield* runAttempt(completedPaths);+ expect(second.result).toEqual({ importedCount: 101, skippedCount: 0 });+ expect(second.fullReads).toEqual([failed.filePath, remaining.filePath]);+ for (const transcript of transcripts) {+ expect(second.openCounts.get(transcript.filePath)).toBe(+ completedPaths.has(transcript.filePath) ? 1 : 2,+ );+ }+ expect(yield* snapshots.getImportedAgentSessionSources(projectId)).toHaveLength(101);+ expect(+ Option.getOrThrow(yield* snapshots.getThreadDetailById(legacy.threadId)).messages.map(+ (message) => message.text,+ ),+ ).toEqual(["Legacy imported history"]);+ expect(+ Option.getOrThrow(yield* directory.getBinding(legacy.threadId)).resumeCursor,+ ).toEqual({+ threadId: "legacy-current-session",+ });+ for (const transcript of [failed, remaining]) {+ expect(+ Option.getOrThrow(+ yield* snapshots.getThreadDetailById(transcript.threadId),+ ).messages.map((message) => message.text),+ ).toEqual([`Prompt ${transcript.providerSessionId}`]);+ }+ }),+ );++ for (const source of ["codex", "claudeAgent"] as const) {+ it.effect(`resumes imported ${source} history only after the first prompt`, () =>+ Effect.gen(function* () {+ const engine = yield* OrchestrationEngine.OrchestrationEngineService;+ const snapshots = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;+ const directory = yield* ProviderSessionDirectory.ProviderSessionDirectory;+ const fileSystem = yield* FileSystem.FileSystem;+ const workspaceRoot = yield* fileSystem.makeTempDirectoryScoped();+ const projectId = ProjectId.make(`project-import-resume-${source}`);+ const sourceThread = {+ ...makeThread(source),+ providerSessionId: source === "codex" ? "codex-first-resume" : CLAUDE_SESSION_ID,+ };+ const threadId = ThreadId.make(+ `import:${sourceThread.providerInstanceId}:${sourceThread.providerSessionId}`,+ );+ const resumeCursor =+ source === "codex"+ ? { threadId: sourceThread.providerSessionId }+ : { threadId, resume: sourceThread.providerSessionId };+ const provider = ProviderDriverKind.make(source);+ const harness = yield* makeTestProviderAdapterHarness({ provider });+ const importSettled = yield* Deferred.make<void>();+ const turnSent = yield* Deferred.make<void>();+ const startSession = vi.fn(harness.adapter.startSession);+ const sendTurn = vi.fn((input: ProviderSendTurnInput) =>+ harness.adapter+ .sendTurn(input)+ .pipe(Effect.tap(() => Deferred.succeed(turnSent, undefined))),+ );+ const providerLayer = makeProviderServiceLive().pipe(+ Layer.provide(+ Layer.succeed(+ ProviderAdapterRegistry,+ makeAdapterRegistryMock({+ [provider]: { ...harness.adapter, startSession, sendTurn },+ }),+ ),+ ),+ Layer.provide(+ Layer.succeed(ProviderSessionDirectory.ProviderSessionDirectory, directory),+ ),+ Layer.provide(Layer.succeed(ProviderEventLoggers, NoOpProviderEventLoggers)),+ Layer.provide(AnalyticsService.layerTest),+ );+ const reactorLayer = ProviderCommandReactorLive.pipe(+ Layer.provideMerge(providerLayer),+ Layer.provide(+ Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {+ ...snapshots,+ // Acknowledge the imported settlement before draining the reactor.+ getThreadShellById: (requestedThreadId) =>+ snapshots+ .getThreadShellById(requestedThreadId)+ .pipe(+ Effect.tap(() =>+ requestedThreadId === threadId+ ? Deferred.succeed(importSettled, undefined)+ : Effect.void,+ ),+ ),+ }),+ ),+ Layer.provide(+ Layer.mock(ProviderAuthService)({+ tryHandlePromptCommand: () => Effect.succeed(false),+ }),+ ),+ Layer.provide(makeProviderRegistryLayer()),+ Layer.provide(Layer.mock(GitWorkflowService)({})),+ Layer.provide(Layer.mock(VcsStatusBroadcaster)({})),+ Layer.provide(Layer.mock(TextGeneration)({})),+ Layer.provide(ServerSettingsService.layerTest()),+ );++ yield* engine.dispatch({+ type: "project.create",+ commandId: CommandId.make(`create-import-resume-project-${source}`),+ projectId,+ title: "Import resume",+ workspaceRoot,+ defaultModelSelection: null,+ createdAt: "2026-08-24T09:00:00.000Z",+ });+ yield* harness.queueTurnResponseForNextSession({ events: [] });++ yield* Effect.gen(function* () {+ const reactor = yield* ProviderCommandReactor;+ yield* reactor.start();+ expect(yield* importRecentAgentThreads({ projectId })).toEqual({+ importedCount: 1,+ skippedCount: 0,+ });+ yield* Deferred.await(importSettled);+ yield* reactor.drain;+ expect(startSession).not.toHaveBeenCalled();+ expect(sendTurn).not.toHaveBeenCalled();+ const importedThread = Option.getOrThrow(yield* snapshots.getThreadDetailById(threadId));+ expect(importedThread.session).toBeNull();+ expect(importedThread.latestTurn).toBeNull();++ yield* engine.dispatch({+ type: "thread.turn.start",+ commandId: CommandId.make(`resume-imported-${source}`),+ threadId,+ message: {+ messageId: MessageId.make(`resume-imported-message-${source}`),+ role: "user",+ text: "Continue this session",+ attachments: [],+ },+ modelSelection: importedThread.modelSelection,+ runtimeMode: importedThread.runtimeMode,+ interactionMode: importedThread.interactionMode,+ createdAt: "2026-08-24T10:02:00.000Z",+ });+ yield* Deferred.await(turnSent);+ yield* reactor.drain;+ expect(startSession).toHaveBeenCalledExactlyOnceWith(+ expect.objectContaining({+ threadId,+ provider,+ providerInstanceId: sourceThread.providerInstanceId,+ resumeCursor,+ cwd: workspaceRoot,+ }),+ );+ expect(sendTurn).toHaveBeenCalledExactlyOnceWith(+ expect.objectContaining({ threadId, input: "Continue this session" }),+ );+ expect(Option.getOrThrow(yield* directory.getBinding(threadId))).toMatchObject({+ provider,+ providerInstanceId: sourceThread.providerInstanceId,+ resumeCursor,+ });+ }).pipe(+ Effect.provide(reactorLayer),+ Effect.provideService(+ AgentSessionScanner.AgentSessionScanner,+ AgentSessionScanner.AgentSessionScanner.of({+ scan: Effect.die("unused"),+ recentThreads: () => Stream.succeed(makeThreadOutcome(sourceThread)),+ }),+ ),+ );+ }),+ );+ }++ it.effect("persists the resume cursor before publishing a new imported thread", () =>+ Effect.gen(function* () {+ const engine = yield* OrchestrationEngine.OrchestrationEngineService;+ const snapshots = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;+ const directory = yield* ProviderSessionDirectory.ProviderSessionDirectory;+ const repository = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository;+ const projectId = ProjectId.make("project-import-binding-race");+ const workspaceRoot = "/tmp/project-import-binding-race";+ const providerSessionId = "codex-binding-race";+ const threadId = ThreadId.make(`import:codex:${providerSessionId}`);+ const scanner = AgentSessionScanner.AgentSessionScanner.of({+ scan: Effect.die("unused"),+ recentThreads: () =>+ Stream.succeed(+ makeThreadOutcome({ ...integrationThread, providerSessionId, title: "Binding race" }),+ ),+ });+ const importerAtBindingWrite = yield* Deferred.make<void>();+ const releaseImporter = yield* Deferred.make<void>();+ const importerRepository = ProviderSessionRuntime.ProviderSessionRuntimeRepository.of({+ ...repository,+ upsert: (runtime, options) =>+ options?.onConflict === "ignore"+ ? Deferred.succeed(importerAtBindingWrite, undefined).pipe(+ Effect.andThen(Deferred.await(releaseImporter)),+ Effect.andThen(repository.upsert(runtime, options)),+ )+ : repository.upsert(runtime, options),+ });+ const importerDirectory = yield* ProviderSessionDirectory.ProviderSessionDirectory.pipe(+ Effect.provide(+ Layer.fresh(ProviderSessionDirectoryLive).pipe(+ Layer.provide(+ Layer.succeed(+ ProviderSessionRuntime.ProviderSessionRuntimeRepository,+ importerRepository,+ ),+ ),+ ),+ ),+ );++ yield* engine.dispatch({+ type: "project.create",+ commandId: CommandId.make("create-import-binding-race-project"),+ projectId,+ title: "Binding race",+ workspaceRoot,+ defaultModelSelection: null,+ createdAt: "2026-08-24T09:00:00.000Z",+ });++ const importFiber = yield* importRecentAgentThreads({ projectId }).pipe(+ Effect.provideService(AgentSessionScanner.AgentSessionScanner, scanner),+ Effect.provideService(ProviderSessionDirectory.ProviderSessionDirectory, importerDirectory),+ Effect.forkChild,+ );++ yield* Effect.raceFirst(+ Deferred.await(importerAtBindingWrite),+ Fiber.join(importFiber).pipe(+ Effect.flatMap((result) =>+ Effect.die(+ new Error(`Import completed before the binding write: ${JSON.stringify(result)}`),+ ),+ ),+ ),+ );+ expect(Option.isNone(yield* snapshots.getThreadDetailById(threadId))).toBe(true);++ yield* directory.upsert({+ threadId,+ provider: ProviderDriverKind.make("codex"),+ providerInstanceId: ProviderInstanceId.make("codex"),+ status: "running",+ resumeCursor: { threadId: "active-client-session" },+ runtimePayload: { cwd: workspaceRoot, activeTurnId: "turn-active" },+ });+ yield* Deferred.succeed(releaseImporter, undefined);++ expect(yield* Fiber.join(importFiber)).toEqual({ importedCount: 1, skippedCount: 0 });+ expect(+ Option.getOrThrow(yield* snapshots.getThreadDetailById(threadId)).messages.map(+ (message) => message.text,+ ),+ ).toEqual(integrationThread.messages.map((message) => message.text));+ expect(Option.getOrThrow(yield* directory.getBinding(threadId))).toMatchObject({+ status: "running",+ resumeCursor: { threadId: "active-client-session" },+ runtimePayload: { cwd: workspaceRoot, activeTurnId: "turn-active" },+ });+ }),+ );++ it.effect("does not import history over a turn started on a partial thread", () =>+ Effect.gen(function* () {+ const engine = yield* OrchestrationEngine.OrchestrationEngineService;+ const snapshots = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;+ const directory = yield* ProviderSessionDirectory.ProviderSessionDirectory;+ const repository = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository;+ const projectId = ProjectId.make("project-import-turn-race");+ const workspaceRoot = "/tmp/project-import-turn-race";+ const providerSessionId = "codex-turn-race";+ const threadId = ThreadId.make(`import:codex:${providerSessionId}`);+ const scanner = AgentSessionScanner.AgentSessionScanner.of({+ scan: Effect.die("unused"),+ recentThreads: () =>+ Stream.succeed(+ makeThreadOutcome({ ...integrationThread, providerSessionId, title: "Turn race" }),+ ),+ });+ const importerAtBindingWrite = yield* Deferred.make<void>();+ const releaseImporter = yield* Deferred.make<void>();+ const importerRepository = ProviderSessionRuntime.ProviderSessionRuntimeRepository.of({+ ...repository,+ upsert: (runtime, options) =>+ options?.onConflict === "ignore"+ ? Deferred.succeed(importerAtBindingWrite, undefined).pipe(+ Effect.andThen(Deferred.await(releaseImporter)),+ Effect.andThen(repository.upsert(runtime, options)),+ )+ : repository.upsert(runtime, options),+ });+ const importerDirectory = yield* ProviderSessionDirectory.ProviderSessionDirectory.pipe(+ Effect.provide(+ Layer.fresh(ProviderSessionDirectoryLive).pipe(+ Layer.provide(+ Layer.succeed(+ ProviderSessionRuntime.ProviderSessionRuntimeRepository,+ importerRepository,+ ),+ ),+ ),+ ),+ );++ yield* engine.dispatch({+ type: "project.create",+ commandId: CommandId.make("create-import-turn-race-project"),+ projectId,+ title: "Turn race",+ workspaceRoot,+ defaultModelSelection: null,+ createdAt: "2026-08-24T09:00:00.000Z",+ });+ yield* engine.dispatch({+ type: "thread.create",+ commandId: CommandId.make("create-import-turn-race-thread"),+ threadId,+ projectId,+ title: "Turn race",+ modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "default" },+ runtimeMode: "full-access",+ interactionMode: "default",+ branch: null,+ worktreePath: null,+ createdAt: "2026-08-24T10:00:00.000Z",+ });++ const importFiber = yield* importRecentAgentThreads({ projectId }).pipe(+ Effect.provideService(AgentSessionScanner.AgentSessionScanner, scanner),+ Effect.provideService(ProviderSessionDirectory.ProviderSessionDirectory, importerDirectory),+ Effect.forkChild,+ );+ yield* Deferred.await(importerAtBindingWrite);++ yield* directory.upsert({+ threadId,+ provider: ProviderDriverKind.make("codex"),+ providerInstanceId: ProviderInstanceId.make("codex"),+ status: "running",+ resumeCursor: { threadId: "active-client-session" },+ runtimePayload: { cwd: workspaceRoot, activeTurnId: "turn-active" },+ });+ yield* engine.dispatch({+ type: "thread.turn.start",+ commandId: CommandId.make("start-turn-during-import"),+ threadId,+ message: {+ messageId: MessageId.make("message-during-import"),+ role: "user",+ text: "Continue while import waits",+ attachments: [],+ },+ runtimeMode: "full-access",+ interactionMode: "default",+ createdAt: "2026-08-24T10:02:00.000Z",+ });+ yield* Deferred.succeed(releaseImporter, undefined);++ expect(yield* Fiber.join(importFiber)).toEqual({ importedCount: 0, skippedCount: 1 });+ expect(Option.getOrThrow(yield* directory.getBinding(threadId))).toMatchObject({+ status: "running",+ resumeCursor: { threadId: "active-client-session" },+ runtimePayload: { cwd: workspaceRoot, activeTurnId: "turn-active" },+ });+ expect(+ Option.getOrThrow(yield* snapshots.getThreadDetailById(threadId)).messages.map(+ (message) => message.text,+ ),+ ).toEqual(["Continue while import waits"]);+ }),+ );+});diff --git a/apps/server/src/project/AgentSessionImporter.ts b/apps/server/src/project/AgentSessionImporter.tsnew file mode 100644index 00000000000..3820c2cf411--- /dev/null+++ b/apps/server/src/project/AgentSessionImporter.ts@@ -0,0 +1,297 @@+import {+ CommandId,+ DEFAULT_MODEL,+ DEFAULT_MODEL_BY_PROVIDER,+ DEFAULT_PROVIDER_INTERACTION_MODE,+ DEFAULT_RUNTIME_MODE,+ AgentSessionImportProjectChangedError,+ AgentSessionImportProjectNotFoundError,+ AgentSessionSource,+ AgentSessionScanError,+ isImportedAgentSessionMessageId,+ MessageId,+ ProjectId,+ ProviderDriverKind,+ ThreadId,+ type AgentSessionImportInput,+ type AgentSessionImportResult,+ type OrchestrationThread,+} from "@t3tools/contracts";+import { normalizeProjectPathForComparison } from "@t3tools/shared/path";+import * as Crypto from "effect/Crypto";+import * as Effect from "effect/Effect";+import * as Option from "effect/Option";+import * as Schema from "effect/Schema";+import * as Stream from "effect/Stream";++import * as OrchestrationEngine from "../orchestration/Services/OrchestrationEngine.ts";+import * as ProjectionSnapshotQuery from "../orchestration/Services/ProjectionSnapshotQuery.ts";+import * as ProviderSessionDirectory from "../provider/Services/ProviderSessionDirectory.ts";+import * as AgentSessionScanner from "./AgentSessionScanner.ts";++const CLAUDE_SESSION_ID_PATTERN =+ /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;++class AgentSessionUnresumableSessionError extends Schema.TaggedErrorClass<AgentSessionUnresumableSessionError>()(+ "AgentSessionUnresumableSessionError",+ {+ source: AgentSessionSource,+ providerSessionId: Schema.String,+ },+) {+ override get message(): string {+ return `Session '${this.providerSessionId}' from '${this.source}' cannot be resumed.`;+ }+}++class AgentSessionThreadProjectConflictError extends Schema.TaggedErrorClass<AgentSessionThreadProjectConflictError>()(+ "AgentSessionThreadProjectConflictError",+ {+ threadId: ThreadId,+ expectedProjectId: ProjectId,+ actualProjectId: ProjectId,+ },+) {+ override get message(): string {+ return `Imported thread '${this.threadId}' belongs to project '${this.actualProjectId}', not '${this.expectedProjectId}'.`;+ }+}++class AgentSessionThreadModifiedError extends Schema.TaggedErrorClass<AgentSessionThreadModifiedError>()(+ "AgentSessionThreadModifiedError",+ { threadId: ThreadId },+) {+ override get message(): string {+ return `Imported thread '${this.threadId}' changed before its history import completed.`;+ }+}++function hasImportedHistory(thread: OrchestrationThread): boolean {+ return thread.messages.some((message) => isImportedAgentSessionMessageId(message.id));+}++function hasImportBlockingActivity(+ thread: OrchestrationThread,+ importedHistoryPresent: boolean,+): boolean {+ return (+ thread.archivedAt !== null ||+ thread.deletedAt !== null ||+ thread.latestTurn !== null ||+ thread.session !== null ||+ thread.messages.some((message) => !isImportedAgentSessionMessageId(message.id)) ||+ thread.proposedPlans.length > 0 ||+ thread.activities.length > 0 ||+ thread.checkpoints.length > 0 ||+ thread.snoozedUntil != null ||+ thread.snoozedAt != null ||+ thread.pinnedAt != null ||+ thread.pinOrderKey != null ||+ thread.titleRegeneration != null ||+ thread.linkedPullRequest != null ||+ thread.unsettledAt != null ||+ (importedHistoryPresent+ ? thread.settledOverride !== "settled"+ : thread.settledOverride !== null || thread.settledAt !== null)+ );+}++/** Import recent transcript text and persist the cursor needed to resume its provider session. */+export const importRecentAgentThreads = Effect.fn("importRecentAgentThreads")(function* (+ input: AgentSessionImportInput,+) {+ const scanner = yield* AgentSessionScanner.AgentSessionScanner;+ const engine = yield* OrchestrationEngine.OrchestrationEngineService;+ const snapshots = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;+ const directory = yield* ProviderSessionDirectory.ProviderSessionDirectory;+ const crypto = yield* Crypto.Crypto;+ const project = yield* snapshots.getProjectShellById(input.projectId).pipe(+ Effect.mapError((cause) => new AgentSessionScanError({ operation: "read-projects", cause })),+ Effect.flatMap(+ Option.match({+ onNone: () =>+ Effect.fail(new AgentSessionImportProjectNotFoundError({ projectId: input.projectId })),+ onSome: Effect.succeed,+ }),+ ),+ );+ const workspaceRoot = project.workspaceRoot;+ if (+ input.expectedWorkspaceRoot !== undefined &&+ normalizeProjectPathForComparison(workspaceRoot) !==+ normalizeProjectPathForComparison(input.expectedWorkspaceRoot)+ ) {+ return yield* new AgentSessionImportProjectChangedError({ projectId: input.projectId });+ }+ const completedSources = yield* snapshots+ .getImportedAgentSessionSources(input.projectId)+ .pipe(+ Effect.mapError((cause) => new AgentSessionScanError({ operation: "read-projects", cause })),+ );+ const threads = scanner.recentThreads(+ workspaceRoot,+ completedSources.map((entry) => entry.source),+ );+ const importedThreadIds = new Set<ThreadId>();+ let importedCount = 0;+ let skippedCount = 0;++ yield* Stream.runForEach(threads, (outcome) =>+ Effect.gen(function* () {+ if (outcome._tag === "Skipped") {+ skippedCount += 1;+ return;+ }+ if (outcome._tag === "AlreadyImported" || outcome._tag === "Duplicate") {+ const threadId = ThreadId.make(+ `import:${outcome.source.providerInstanceId}:${outcome.source.providerSessionId}`,+ );+ if (outcome._tag === "AlreadyImported") {+ importedThreadIds.add(threadId);+ importedCount += 1;+ } else if (importedThreadIds.has(threadId)) {+ const recorded = yield* directory+ .recordImportedTranscript({ threadId, source: outcome.source })+ .pipe(Effect.result);+ if (recorded._tag === "Failure") {+ skippedCount += 1;+ yield* Effect.logWarning("Could not record an imported transcript copy", {+ threadId,+ cause: recorded.failure,+ });+ }+ }+ return;+ }+ const thread = outcome.thread;+ const threadId = ThreadId.make(+ `import:${thread.providerInstanceId}:${thread.providerSessionId}`,+ );+ const imported = yield* Effect.gen(function* () {+ const provider = ProviderDriverKind.make(thread.source);+ const model = thread.model ?? DEFAULT_MODEL_BY_PROVIDER[provider] ?? DEFAULT_MODEL;+ const existingThread = yield* snapshots.getThreadDetailById(threadId);+ const existingBinding = yield* directory.getBinding(threadId);++ if (+ thread.source === "claudeAgent" &&+ !CLAUDE_SESSION_ID_PATTERN.test(thread.providerSessionId)+ ) {+ return yield* new AgentSessionUnresumableSessionError({+ source: thread.source,+ providerSessionId: thread.providerSessionId,+ });+ }++ if (Option.isSome(existingThread) && existingThread.value.projectId !== input.projectId) {+ return yield* new AgentSessionThreadProjectConflictError({+ threadId,+ expectedProjectId: input.projectId,+ actualProjectId: existingThread.value.projectId,+ });+ }++ const importedHistoryPresent = Option.isSome(existingThread)+ ? hasImportedHistory(existingThread.value)+ : false;+ if (+ Option.isSome(existingThread) &&+ importedHistoryPresent &&+ Option.isSome(existingBinding)+ ) {+ yield* directory.recordImportedTranscript({ threadId, source: outcome.source });+ return true;+ }++ if (+ Option.isSome(existingThread) &&+ hasImportBlockingActivity(existingThread.value, importedHistoryPresent)+ ) {+ return yield* new AgentSessionThreadModifiedError({ threadId });+ }++ if (+ Option.isSome(existingBinding) &&+ (existingBinding.value.provider !== provider ||+ existingBinding.value.providerInstanceId !== thread.providerInstanceId ||+ existingBinding.value.status !== "stopped")+ ) {+ return yield* new AgentSessionThreadModifiedError({ threadId });+ }++ // Install the cursor before the thread becomes visible. A concurrent+ // real session can replace it, while insert-ignore keeps this import+ // from replacing that newer binding.+ if (Option.isNone(existingBinding)) {+ yield* directory.upsert(+ {+ threadId,+ provider,+ providerInstanceId: thread.providerInstanceId,+ status: "stopped",+ runtimeMode: DEFAULT_RUNTIME_MODE,+ resumeCursor:+ thread.source === "codex"+ ? { threadId: thread.providerSessionId }+ : { threadId, resume: thread.providerSessionId },+ runtimePayload: { cwd: workspaceRoot },+ },+ { onConflict: "ignore" },+ );+ }++ if (Option.isNone(existingThread)) {+ yield* engine.dispatch({+ type: "thread.create",+ commandId: CommandId.make(yield* crypto.randomUUIDv4),+ threadId,+ projectId: input.projectId,+ title: thread.title,+ modelSelection: { instanceId: thread.providerInstanceId, model },+ runtimeMode: DEFAULT_RUNTIME_MODE,+ interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,+ branch: null,+ worktreePath: null,+ createdAt: thread.createdAt,+ historyImport: true,+ });+ }++ if (!importedHistoryPresent) {+ yield* engine.dispatch({+ type: "thread.history.import",+ commandId: CommandId.make(yield* crypto.randomUUIDv4),+ threadId,+ messages: thread.messages.map((message, index) => ({+ messageId: MessageId.make(`${threadId}:${String(index).padStart(6, "0")}`),+ role: message.role,+ text: message.text,+ createdAt: message.createdAt,+ })),+ });+ }++ yield* directory.recordImportedTranscript({ threadId, source: outcome.source });++ return true;+ }).pipe(+ Effect.catch((cause) =>+ Effect.logWarning("Could not import an agent session", {+ provider: thread.source,+ sessionId: thread.providerSessionId,+ cause,+ }).pipe(Effect.as(false)),+ ),+ );++ if (imported) {+ importedThreadIds.add(threadId);+ importedCount += 1;+ } else {+ skippedCount += 1;+ }+ }),+ );++ return { importedCount, skippedCount } satisfies AgentSessionImportResult;+});diff --git a/apps/server/src/project/AgentSessionScanner.test.ts b/apps/server/src/project/AgentSessionScanner.test.tsnew file mode 100644index 00000000000..dc6d72a0ce6--- /dev/null+++ b/apps/server/src/project/AgentSessionScanner.test.ts@@ -0,0 +1,3088 @@+import * as NodeServices from "@effect/platform-node/NodeServices";+import * as NodeOS from "node:os";+import { describe, expect, it } from "@effect/vitest";+import {+ type OrchestrationProjectShell,+ ProjectId,+ ProviderDriverKind,+ ProviderInstanceId,+ type ServerSettings as ContractServerSettings,+} from "@t3tools/contracts";+import { symlinksSupported } from "@t3tools/shared/testing/symlinks";+import * as Effect from "effect/Effect";+import * as FileSystem from "effect/FileSystem";+import * as Layer from "effect/Layer";+import * as Option from "effect/Option";+import * as Path from "effect/Path";+import * as Schema from "effect/Schema";+import * as Stream from "effect/Stream";+import * as TestClock from "effect/testing/TestClock";++import * as ServerConfig from "../config.ts";+import * as ProjectionSnapshotQuery from "../orchestration/Services/ProjectionSnapshotQuery.ts";+import * as ServerSettings from "../serverSettings.ts";+import * as AgentSessionScanner from "./AgentSessionScanner.ts";++const makeProjectShell = (workspaceRoot: string): OrchestrationProjectShell => ({+ id: ProjectId.make("project-1"),+ title: "Imported",+ workspaceRoot,+ defaultModelSelection: null,+ scripts: [],+ createdAt: "2026-01-01T00:00:00.000Z",+ updatedAt: "2026-01-01T00:00:00.000Z",+});++/** Only `getShellSnapshot` is exercised; the rest must not be called. */+const makeProjectionSnapshotQueryLayer = (importedWorkspaceRoots: ReadonlyArray<string>) =>+ Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {+ getCommandReadModel: () => Effect.die("unused"),+ getUserInputActivity: () => Effect.die("unused"),+ getSnapshot: () => Effect.die("unused"),+ getShellSnapshot: () =>+ Effect.succeed({+ snapshotSequence: 0,+ projects: importedWorkspaceRoots.map((workspaceRoot) => makeProjectShell(workspaceRoot)),+ threads: [],+ updatedAt: "2026-01-01T00:00:00.000Z",+ }),+ getArchivedShellSnapshot: () => Effect.die("unused"),+ getSnapshotSequence: () => Effect.die("unused"),+ getCounts: () => Effect.die("unused"),+ getEventReplayStats: () => Effect.die("unused"),+ getActiveProjectByWorkspaceRoot: () => Effect.die("unused"),+ getProjectShellById: () => Effect.die("unused"),+ getImportedAgentSessionSources: () => Effect.succeed([]),+ getFirstActiveThreadIdByProjectId: () => Effect.die("unused"),+ getThreadCheckpointContext: () => Effect.die("unused"),+ getFullThreadDiffContext: () => Effect.die("unused"),+ getThreadShellById: () => Effect.die("unused"),+ getThreadRuntimeContext: () => Effect.die("unused"),+ getThreadDetailById: () => Effect.die("unused"),+ getThreadDetailSnapshot: () => Effect.die("unused"),+ searchThreads: () => Effect.die("unused"),+ });++/**+ * Run a scan against the given homes. Homes are temp dirs created inside the+ * test, so the layer is built per run rather than shared.+ */+interface ScannerTestInput {+ readonly claudeHomePath: string;+ readonly codexHomePath: string;+ readonly importedWorkspaceRoots?: ReadonlyArray<string>;+ /** Base dir for the test ServerConfig; worktreesDir derives from it. */+ readonly configBaseDir?: string;+ readonly providerInstances?: ContractServerSettings["providerInstances"];+}++const makeScannerTestLayer = (input: ScannerTestInput) =>+ AgentSessionScanner.layer.pipe(+ Layer.provide(+ Layer.mergeAll(+ ServerSettings.layerTest({+ providers: {+ claudeAgent: { homePath: input.claudeHomePath },+ codex: { homePath: input.codexHomePath },+ },+ ...(input.providerInstances === undefined+ ? {}+ : { providerInstances: input.providerInstances }),+ }),+ ServerConfig.layerTest(+ input.claudeHomePath,+ input.configBaseDir ?? { prefix: "t3code-scanner-config-" },+ ),+ makeProjectionSnapshotQueryLayer(input.importedWorkspaceRoots ?? []),+ ),+ ),+ );++const runScan = (input: ScannerTestInput) =>+ Effect.gen(function* () {+ const scanner = yield* AgentSessionScanner.AgentSessionScanner;+ return yield* scanner.scan;+ }).pipe(Effect.provide(makeScannerTestLayer(input)));++const runRecentThreadOutcomes = (input: ScannerTestInput & { readonly workspaceRoot: string }) =>+ Effect.gen(function* () {+ const scanner = yield* AgentSessionScanner.AgentSessionScanner;+ return yield* scanner.recentThreads(input.workspaceRoot).pipe(+ Stream.runCollect,+ Effect.map((outcomes) => Array.from(outcomes)),+ );+ }).pipe(Effect.provide(makeScannerTestLayer(input)));++const runRecentThreads = (input: ScannerTestInput & { readonly workspaceRoot: string }) =>+ runRecentThreadOutcomes(input).pipe(+ Effect.map((outcomes) =>+ outcomes.flatMap((outcome) => (outcome._tag === "Importable" ? [outcome.thread] : [])),+ ),+ );++const makeTempDir = Effect.fn("AgentSessionScanner.test.makeTempDir")(function* (prefix: string) {+ const fileSystem = yield* FileSystem.FileSystem;+ return yield* fileSystem.makeTempDirectoryScoped({ prefix });+});++const writeTranscript = Effect.fn("AgentSessionScanner.test.writeTranscript")(function* (input: {+ readonly filePath: string;+ readonly contents: string;+ /** Epoch millis, so ordering assertions never depend on write timing. */+ readonly mtimeMs: number;+}) {+ const fileSystem = yield* FileSystem.FileSystem;+ const path = yield* Path.Path;+ yield* fileSystem.makeDirectory(path.dirname(input.filePath), { recursive: true });+ yield* fileSystem.writeFileString(input.filePath, input.contents);+ // Numeric utimes arguments are seconds, not milliseconds.+ const seconds = input.mtimeMs / 1000;+ yield* fileSystem.utimes(input.filePath, seconds, seconds);+});++/** Claude session line: the first record carries the real `cwd`. */+const claudeSessionLine = (cwd: string) =>+ `${JSON.stringify({ type: "user", cwd, sessionId: "s1" })}\n${JSON.stringify({ type: "assistant" })}\n`;++/** Codex rollout line: session metadata is nested under `payload`. */+const codexRolloutLine = (cwd: string) =>+ `${JSON.stringify({ timestamp: "2026-01-01T00:00:00.000Z", type: "session_meta", payload: { id: "r1", cwd } })}\n`;++const encodeTranscriptRecord = Schema.encodeUnknownSync(Schema.fromJsonString(Schema.Unknown));++function makeRecordLimitTranscript(cwd: string, overflow: boolean): string {+ const records =+ [+ encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: "record-limit-session", cwd },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: { type: "user_message", message: "First prompt" },+ }),+ ].join("\n") ++ "\n" ++ "{}\n".repeat(99_998);+ return overflow+ ? records ++ "\n" ++ encodeTranscriptRecord({+ type: "event_msg",+ payload: { type: "user_message", message: "Overflow prompt" },+ }) ++ "\n"+ : records;+}++it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {+ describe("scan", () => {+ it.effect("reads Claude project cwds from transcripts, newest first", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const claudeHomePath = yield* makeTempDir("t3code-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const olderWorkspace = yield* makeTempDir("t3code-workspace-older-");+ const newerWorkspace = yield* makeTempDir("t3code-workspace-newer-");++ // Slugs are intentionally lossy; the scanner must not decode them.+ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-slug-older", "a.jsonl"),+ contents: claudeSessionLine(olderWorkspace),+ mtimeMs: Date.parse("2026-01-01T00:00:00.000Z"),+ });+ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-slug-older", "b.jsonl"),+ contents: claudeSessionLine(olderWorkspace),+ mtimeMs: Date.parse("2026-01-02T00:00:00.000Z"),+ });+ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-slug-newer", "c.jsonl"),+ contents: claudeSessionLine(newerWorkspace),+ mtimeMs: Date.parse("2026-03-01T00:00:00.000Z"),+ });++ const result = yield* runScan({ claudeHomePath, codexHomePath });++ expect(result.candidates).toEqual([+ {+ path: newerWorkspace,+ title: path.basename(newerWorkspace),+ sources: ["claudeAgent"],+ threadCount: 1,+ lastActiveAt: "2026-03-01T00:00:00.000Z",+ alreadyImported: false,+ },+ {+ path: olderWorkspace,+ title: path.basename(olderWorkspace),+ sources: ["claudeAgent"],+ threadCount: 2,+ lastActiveAt: "2026-01-02T00:00:00.000Z",+ alreadyImported: false,+ },+ ]);+ }),+ );++ it.effect("groups Codex rollouts by cwd across date directories", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const claudeHomePath = yield* makeTempDir("t3code-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const workspace = yield* makeTempDir("t3code-workspace-");+ const otherWorkspace = yield* makeTempDir("t3code-workspace-other-");++ const rollout = (year: string, month: string, day: string, name: string) =>+ path.join(codexHomePath, "sessions", year, month, day, name);++ yield* writeTranscript({+ filePath: rollout("2026", "01", "05", "rollout-2026-01-05T10-00-00-aaa.jsonl"),+ contents: codexRolloutLine(workspace),+ mtimeMs: Date.parse("2026-01-05T10:00:00.000Z"),+ });+ yield* writeTranscript({+ filePath: rollout("2026", "02", "09", "rollout-2026-02-09T10-00-00-bbb.jsonl"),+ contents: codexRolloutLine(workspace),+ mtimeMs: Date.parse("2026-02-09T10:00:00.000Z"),+ });+ yield* writeTranscript({+ filePath: rollout("2026", "02", "09", "rollout-2026-02-09T11-00-00-ccc.jsonl"),+ contents: codexRolloutLine(otherWorkspace),+ mtimeMs: Date.parse("2026-02-09T11:00:00.000Z"),+ });++ const result = yield* runScan({ claudeHomePath, codexHomePath });++ expect(result.candidates).toEqual([+ {+ path: otherWorkspace,+ title: path.basename(otherWorkspace),+ sources: ["codex"],+ threadCount: 1,+ lastActiveAt: "2026-02-09T11:00:00.000Z",+ alreadyImported: false,+ },+ {+ path: workspace,+ title: path.basename(workspace),+ sources: ["codex"],+ threadCount: 2,+ lastActiveAt: "2026-02-09T10:00:00.000Z",+ alreadyImported: false,+ },+ ]);+ }),+ );++ it.effect.each(["claudeAgent", "codex"] as const)(+ "does not open a non-file %s transcript",+ (source) =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const fileSystem = yield* FileSystem.FileSystem;+ const claudeHomePath = yield* makeTempDir("t3code-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const transcriptPath =+ source === "claudeAgent"+ ? path.join(claudeHomePath, "projects", "-slug", "session.jsonl")+ : path.join(codexHomePath, "sessions", "2026", "08", "24", "rollout-session.jsonl");+ yield* fileSystem.makeDirectory(transcriptPath, { recursive: true });++ let transcriptOpenCount = 0;+ const simulatedFileSystem = FileSystem.FileSystem.of({+ ...fileSystem,+ open: (filePath, options) => {+ if (filePath === transcriptPath) transcriptOpenCount += 1;+ return fileSystem.open(filePath, options);+ },+ });++ const result = yield* runScan({ claudeHomePath, codexHomePath }).pipe(+ Effect.provideService(FileSystem.FileSystem, simulatedFileSystem),+ );++ expect(result.candidates).toEqual([]);+ expect(transcriptOpenCount).toBe(0);+ }),+ );++ it.effect.each(["claudeAgent", "codex"] as const)(+ "stops %s directory reads at the discovery operation budget",+ (source) =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const fileSystem = yield* FileSystem.FileSystem;+ const claudeHomePath = yield* makeTempDir("t3code-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const discoveryRoot =+ source === "claudeAgent"+ ? path.join(claudeHomePath, "projects")+ : path.join(codexHomePath, "sessions");+ const emptyDirectories = Array.from(+ { length: 20_001 },+ (_, index) => `empty-${index.toString().padStart(5, "0")}`,+ );+ let directoryReadCount = 0;+ const simulatedFileSystem = FileSystem.FileSystem.of({+ ...fileSystem,+ readDirectory: (directory, options) => {+ if (directory === discoveryRoot) {+ directoryReadCount += 1;+ return Effect.succeed(emptyDirectories);+ }+ if (path.dirname(directory) === discoveryRoot) {+ directoryReadCount += 1;+ return Effect.succeed([]);+ }+ return fileSystem.readDirectory(directory, options);+ },+ });++ const result = yield* runScan({ claudeHomePath, codexHomePath }).pipe(+ Effect.provideService(FileSystem.FileSystem, simulatedFileSystem),+ );++ expect(result.candidates).toEqual([]);+ expect(directoryReadCount).toBe(20_000);+ }),+ );++ it.effect("merges the same cwd seen by both agents and flags imported projects", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const claudeHomePath = yield* makeTempDir("t3code-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const workspace = yield* makeTempDir("t3code-workspace-");++ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-slug", "a.jsonl"),+ contents: claudeSessionLine(workspace),+ mtimeMs: Date.parse("2026-01-01T00:00:00.000Z"),+ });+ yield* writeTranscript({+ filePath: path.join(+ codexHomePath,+ "sessions",+ "2026",+ "04",+ "01",+ "rollout-2026-04-01T09-00-00-aaa.jsonl",+ ),+ contents: codexRolloutLine(workspace),+ mtimeMs: Date.parse("2026-04-01T09:00:00.000Z"),+ });++ const result = yield* runScan({+ claudeHomePath,+ codexHomePath,+ importedWorkspaceRoots: [workspace],+ });++ expect(result.candidates).toEqual([+ {+ path: workspace,+ title: path.basename(workspace),+ projectId: ProjectId.make("project-1"),+ sources: ["claudeAgent", "codex"],+ threadCount: 2,+ lastActiveAt: "2026-04-01T09:00:00.000Z",+ alreadyImported: true,+ },+ ]);+ }),+ );++ it.effect("returns the imported project ID through a realpath alias", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const fileSystem = yield* FileSystem.FileSystem;+ const claudeHomePath = yield* makeTempDir("t3code-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const workspace = yield* makeTempDir("t3code-workspace-");+ const linkParent = yield* makeTempDir("t3code-scanner-links-");+ const workspaceAlias = path.join(linkParent, "workspace-alias");+ yield* fileSystem.symlink(workspace, workspaceAlias);++ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-slug", "a.jsonl"),+ contents: claudeSessionLine(workspaceAlias),+ mtimeMs: Date.parse("2026-01-01T00:00:00.000Z"),+ });++ const result = yield* runScan({+ claudeHomePath,+ codexHomePath,+ importedWorkspaceRoots: [workspace],+ });++ expect(result.candidates[0]).toMatchObject({+ path: workspace,+ projectId: ProjectId.make("project-1"),+ alreadyImported: true,+ });+ }),+ );++ it.effect("matches a persisted project alias to a transcript realpath", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const fileSystem = yield* FileSystem.FileSystem;+ const claudeHomePath = yield* makeTempDir("t3code-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const workspace = yield* makeTempDir("t3code-workspace-");+ const linkParent = yield* makeTempDir("t3code-scanner-links-");+ const workspaceAlias = path.join(linkParent, "workspace-alias");+ yield* fileSystem.symlink(workspace, workspaceAlias);++ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-slug", "a.jsonl"),+ contents: claudeSessionLine(workspace),+ mtimeMs: Date.parse("2026-01-01T00:00:00.000Z"),+ });++ const result = yield* runScan({+ claudeHomePath,+ codexHomePath,+ importedWorkspaceRoots: [workspaceAlias],+ });++ expect(result.candidates[0]).toMatchObject({+ path: workspaceAlias,+ projectId: ProjectId.make("project-1"),+ alreadyImported: true,+ });+ }),+ );++ it.effect("merges case aliases and preserves the persisted project path", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const fileSystem = yield* FileSystem.FileSystem;+ const claudeHomePath = yield* makeTempDir("t3code-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const workspace = yield* makeTempDir("t3code-workspace-");+ const workspaceAlias = path.join(+ path.dirname(workspace),+ path.basename(workspace).toUpperCase(),+ );++ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-slug", "a.jsonl"),+ contents: claudeSessionLine(workspaceAlias),+ mtimeMs: Date.parse("2026-01-01T00:00:00.000Z"),+ });+ yield* writeTranscript({+ filePath: path.join(codexHomePath, "sessions", "2026", "01", "02", "rollout-b.jsonl"),+ contents: codexRolloutLine(workspace),+ mtimeMs: Date.parse("2026-01-02T00:00:00.000Z"),+ });++ const simulatedFileSystem = FileSystem.FileSystem.of({+ ...fileSystem,+ stat: (filePath) => fileSystem.stat(filePath === workspaceAlias ? workspace : filePath),+ });+ const result = yield* runScan({+ claudeHomePath,+ codexHomePath,+ importedWorkspaceRoots: [workspace],+ }).pipe(Effect.provideService(FileSystem.FileSystem, simulatedFileSystem));++ expect(result.candidates).toEqual([+ {+ path: workspace,+ title: path.basename(workspace),+ projectId: ProjectId.make("project-1"),+ sources: ["claudeAgent", "codex"],+ threadCount: 2,+ lastActiveAt: "2026-01-02T00:00:00.000Z",+ alreadyImported: true,+ },+ ]);+ }),+ );++ it.effect("keeps case variants distinct when the filesystem identities differ", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const fileSystem = yield* FileSystem.FileSystem;+ const claudeHomePath = yield* makeTempDir("t3code-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const backingUpper = yield* makeTempDir("t3code-backing-upper-");+ const backingLower = yield* makeTempDir("t3code-backing-lower-");+ const aliasParent = yield* makeTempDir("t3code-case-aliases-");+ const upperWorkspace = path.join(aliasParent, "Repo");+ const lowerWorkspace = path.join(aliasParent, "repo");++ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-upper", "a.jsonl"),+ contents: claudeSessionLine(upperWorkspace),+ mtimeMs: Date.parse("2026-01-02T00:00:00.000Z"),+ });+ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-lower", "b.jsonl"),+ contents: claudeSessionLine(lowerWorkspace),+ mtimeMs: Date.parse("2026-01-01T00:00:00.000Z"),+ });++ const simulatedFileSystem = FileSystem.FileSystem.of({+ ...fileSystem,+ stat: (filePath) =>+ fileSystem.stat(+ filePath === upperWorkspace+ ? backingUpper+ : filePath === lowerWorkspace+ ? backingLower+ : filePath,+ ),+ });+ const result = yield* runScan({ claudeHomePath, codexHomePath }).pipe(+ Effect.provideService(FileSystem.FileSystem, simulatedFileSystem),+ );++ expect(result.candidates.map((candidate) => candidate.path)).toEqual([+ upperWorkspace,+ lowerWorkspace,+ ]);+ }),+ );++ it.effect("uses explicit provider instance homes instead of overridden legacy homes", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const claudeHomePath = yield* makeTempDir("t3code-claude-legacy-");+ const codexHomePath = yield* makeTempDir("t3code-codex-legacy-");+ const claudeInstanceHome = yield* makeTempDir("t3code-claude-instance-");+ const codexInstanceHome = yield* makeTempDir("t3code-codex-instance-");+ const legacyWorkspace = yield* makeTempDir("t3code-workspace-legacy-");+ const claudeWorkspace = yield* makeTempDir("t3code-workspace-claude-");+ const codexWorkspace = yield* makeTempDir("t3code-workspace-codex-");++ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-legacy", "session.jsonl"),+ contents: claudeSessionLine(legacyWorkspace),+ mtimeMs: Date.parse("2026-01-01T00:00:00.000Z"),+ });+ yield* writeTranscript({+ filePath: path.join(claudeInstanceHome, "projects", "-actual", "session.jsonl"),+ contents: claudeSessionLine(claudeWorkspace),+ mtimeMs: Date.parse("2026-02-01T00:00:00.000Z"),+ });+ yield* writeTranscript({+ filePath: path.join(+ codexInstanceHome,+ "sessions",+ "2026",+ "03",+ "01",+ "rollout-instance.jsonl",+ ),+ contents: codexRolloutLine(codexWorkspace),+ mtimeMs: Date.parse("2026-03-01T00:00:00.000Z"),+ });++ const result = yield* runScan({+ claudeHomePath,+ codexHomePath,+ providerInstances: {+ [ProviderInstanceId.make("claudeAgent")]: {+ driver: ProviderDriverKind.make("claudeAgent"),+ config: { homePath: claudeInstanceHome },+ },+ [ProviderInstanceId.make("codex")]: {+ driver: ProviderDriverKind.make("codex"),+ config: { homePath: codexInstanceHome },+ },+ },+ });++ expect(result.candidates.map((candidate) => candidate.path)).toEqual([+ codexWorkspace,+ claudeWorkspace,+ ]);+ }),+ );++ it.effect("scans each distinct home across multiple instances once", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const claudeHomePath = yield* makeTempDir("t3code-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const otherCodexHome = yield* makeTempDir("t3code-codex-other-");+ const workspace = yield* makeTempDir("t3code-workspace-");+ const otherWorkspace = yield* makeTempDir("t3code-workspace-other-");++ for (const [home, cwd] of [+ [codexHomePath, workspace],+ [otherCodexHome, otherWorkspace],+ ] as const) {+ yield* writeTranscript({+ filePath: path.join(home, "sessions", "2026", "01", "01", "rollout-session.jsonl"),+ contents: codexRolloutLine(cwd),+ mtimeMs: Date.parse("2026-01-01T00:00:00.000Z"),+ });+ }++ const result = yield* runScan({+ claudeHomePath,+ codexHomePath,+ providerInstances: {+ [ProviderInstanceId.make("codex-personal")]: {+ driver: ProviderDriverKind.make("codex"),+ config: { homePath: codexHomePath },+ },+ [ProviderInstanceId.make("codex-work")]: {+ driver: ProviderDriverKind.make("codex"),+ config: { homePath: otherCodexHome },+ },+ },+ });++ expect(result.candidates).toHaveLength(2);+ expect(result.candidates.map((candidate) => candidate.threadCount)).toEqual([1, 1]);+ expect(result.candidates.map((candidate) => candidate.path).sort()).toEqual(+ [workspace, otherWorkspace].sort(),+ );+ }),+ );++ it.effect("honors provider instance home directory environment variables", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const claudeHomePath = yield* makeTempDir("t3code-claude-legacy-");+ const codexHomePath = yield* makeTempDir("t3code-codex-legacy-");+ const claudeEnvironmentHome = yield* makeTempDir("t3code-claude-env-");+ const codexEnvironmentHome = yield* makeTempDir("t3code-codex-env-");+ const claudeWorkspace = yield* makeTempDir("t3code-workspace-claude-");+ const codexWorkspace = yield* makeTempDir("t3code-workspace-codex-");++ yield* writeTranscript({+ filePath: path.join(claudeEnvironmentHome, "projects", "-actual", "session.jsonl"),+ contents: claudeSessionLine(claudeWorkspace),+ mtimeMs: Date.parse("2026-01-01T00:00:00.000Z"),+ });+ yield* writeTranscript({+ filePath: path.join(+ codexEnvironmentHome,+ "sessions",+ "2026",+ "01",+ "01",+ "rollout-session.jsonl",+ ),+ contents: codexRolloutLine(codexWorkspace),+ mtimeMs: Date.parse("2026-01-02T00:00:00.000Z"),+ });++ const result = yield* runScan({+ claudeHomePath,+ codexHomePath,+ providerInstances: {+ [ProviderInstanceId.make("claudeAgent")]: {+ driver: ProviderDriverKind.make("claudeAgent"),+ environment: [+ { name: "CLAUDE_CONFIG_DIR", value: claudeEnvironmentHome, sensitive: false },+ ],+ config: {},+ },+ [ProviderInstanceId.make("codex")]: {+ driver: ProviderDriverKind.make("codex"),+ environment: [{ name: "CODEX_HOME", value: codexEnvironmentHome, sensitive: false }],+ config: {},+ },+ },+ });++ expect(result.candidates.map((candidate) => candidate.path)).toEqual([+ codexWorkspace,+ claudeWorkspace,+ ]);+ }),+ );++ it.effect("ignores invalid provider instances while scanning the remaining providers", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const claudeHomePath = yield* makeTempDir("t3code-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const workspace = yield* makeTempDir("t3code-workspace-");++ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-actual", "session.jsonl"),+ contents: claudeSessionLine(workspace),+ mtimeMs: Date.parse("2026-01-01T00:00:00.000Z"),+ });++ const result = yield* runScan({+ claudeHomePath,+ codexHomePath,+ providerInstances: {+ [ProviderInstanceId.make("codex")]: {+ driver: ProviderDriverKind.make("codex"),+ config: { homePath: 123 },+ },+ },+ });++ expect(result.candidates.map((candidate) => candidate.path)).toEqual([workspace]);+ }),+ );++ it.effect("does not scan provider instances disabled by the envelope or config", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const claudeHomePath = yield* makeTempDir("t3code-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const envelopeDisabledHome = yield* makeTempDir("t3code-codex-disabled-envelope-");+ const configDisabledHome = yield* makeTempDir("t3code-codex-disabled-config-");+ const envelopeWorkspace = yield* makeTempDir("t3code-workspace-disabled-envelope-");+ const configWorkspace = yield* makeTempDir("t3code-workspace-disabled-config-");++ for (const [home, workspace, session] of [+ [envelopeDisabledHome, envelopeWorkspace, "envelope-disabled"],+ [configDisabledHome, configWorkspace, "config-disabled"],+ ] as const) {+ yield* writeTranscript({+ filePath: path.join(home, "sessions", "2026", "08", "24", `rollout-${session}.jsonl`),+ contents: codexRolloutLine(workspace),+ mtimeMs: Date.parse("2026-08-24T12:00:00.000Z"),+ });+ }++ const result = yield* runScan({+ claudeHomePath,+ codexHomePath,+ providerInstances: {+ [ProviderInstanceId.make("codex-envelope-disabled")]: {+ driver: ProviderDriverKind.make("codex"),+ enabled: false,+ config: { homePath: envelopeDisabledHome },+ },+ [ProviderInstanceId.make("codex-config-disabled")]: {+ driver: ProviderDriverKind.make("codex"),+ config: { enabled: false, homePath: configDisabledHome },+ },+ },+ });++ expect(result.candidates).toEqual([]);+ }),+ );++ it.effect("ignores relative working directories from malformed transcripts", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const claudeHomePath = yield* makeTempDir("t3code-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const workspace = yield* makeTempDir("t3code-workspace-");++ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-relative", "session.jsonl"),+ contents: claudeSessionLine(path.relative(path.resolve(), workspace)),+ mtimeMs: Date.parse("2026-01-01T00:00:00.000Z"),+ });++ const result = yield* runScan({ claudeHomePath, codexHomePath });++ expect(result.candidates).toEqual([]);+ }),+ );++ it.effect("drops candidates whose directory no longer exists", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const claudeHomePath = yield* makeTempDir("t3code-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");++ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-slug", "a.jsonl"),+ contents: claudeSessionLine(path.join(claudeHomePath, "does-not-exist")),+ mtimeMs: Date.parse("2026-01-01T00:00:00.000Z"),+ });++ const result = yield* runScan({ claudeHomePath, codexHomePath });++ expect(result.candidates).toEqual([]);+ }),+ );++ it.effect("excludes the home directory, temporary root, and T3 data directory", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const claudeHomePath = yield* makeTempDir("t3code-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const configBaseDir = yield* makeTempDir("t3code-scanner-base-");+ const workspace = yield* makeTempDir("t3code-workspace-");++ for (const [index, cwd] of [+ NodeOS.homedir(),+ NodeOS.tmpdir(),+ configBaseDir,+ workspace,+ ].entries()) {+ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", `-slug-${index}`, "session.jsonl"),+ contents: claudeSessionLine(cwd),+ mtimeMs: Date.parse("2026-01-01T00:00:00.000Z") + index,+ });+ }++ const result = yield* runScan({ claudeHomePath, codexHomePath, configBaseDir });++ expect(result.candidates.map((candidate) => candidate.path)).toEqual([workspace]);+ }),+ );++ it.effect("excludes T3-managed worktree sandboxes", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const claudeHomePath = yield* makeTempDir("t3code-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const fileSystem = yield* FileSystem.FileSystem;++ const worktreeCwd = path.join(claudeHomePath, ".t3", "worktrees", "t3code", "wt-1");+ yield* fileSystem.makeDirectory(worktreeCwd, { recursive: true });+ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-slug", "a.jsonl"),+ contents: claudeSessionLine(worktreeCwd),+ mtimeMs: Date.parse("2026-01-01T00:00:00.000Z"),+ });++ const result = yield* runScan({ claudeHomePath, codexHomePath });++ expect(result.candidates).toEqual([]);+ }),+ );++ it.effect("excludes sandboxes under the configured worktrees dir without .t3 in the path", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const claudeHomePath = yield* makeTempDir("t3code-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const configBaseDir = yield* makeTempDir("t3code-scanner-base-");+ const fileSystem = yield* FileSystem.FileSystem;++ // worktreesDir derives as `<baseDir>/worktrees`, and the temp base+ // dir contains no `.t3` segment — only the config-based prefix match+ // can exclude this one.+ const worktreeCwd = path.join(configBaseDir, "worktrees", "t3code", "wt-2");+ yield* fileSystem.makeDirectory(worktreeCwd, { recursive: true });+ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-slug", "a.jsonl"),+ contents: claudeSessionLine(worktreeCwd),+ mtimeMs: Date.parse("2026-01-01T00:00:00.000Z"),+ });++ const result = yield* runScan({ claudeHomePath, codexHomePath, configBaseDir });++ expect(result.candidates).toEqual([]);+ }),+ );++ it.effect("excludes sandboxes reached through a symlink into the worktrees dir", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const claudeHomePath = yield* makeTempDir("t3code-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const configBaseDir = yield* makeTempDir("t3code-scanner-base-");+ const linkParent = yield* makeTempDir("t3code-scanner-links-");+ const fileSystem = yield* FileSystem.FileSystem;++ // The recorded cwd is a symlink whose own spelling looks harmless;+ // only its realpath reveals the managed sandbox.+ const worktreeCwd = path.join(configBaseDir, "worktrees", "t3code", "wt-3");+ yield* fileSystem.makeDirectory(worktreeCwd, { recursive: true });+ const symlinkCwd = path.join(linkParent, "innocent-project");+ yield* fileSystem.symlink(worktreeCwd, symlinkCwd);+ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-slug", "a.jsonl"),+ contents: claudeSessionLine(symlinkCwd),+ mtimeMs: Date.parse("2026-01-01T00:00:00.000Z"),+ });++ const result = yield* runScan({ claudeHomePath, codexHomePath, configBaseDir });++ expect(result.candidates).toEqual([]);+ }),+ );++ it.effect("finds the cwd on a later line when the first records carry none", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const claudeHomePath = yield* makeTempDir("t3code-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const workspace = yield* makeTempDir("t3code-workspace-");++ // Claude transcripts often open with records that have no cwd.+ const contents = `{"type":"file-history-snapshot","messageId":"m1"}\n{"type":"queue-operation","operation":"enqueue"}\n${claudeSessionLine(workspace)}`;+ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-slug", "a.jsonl"),+ contents,+ mtimeMs: Date.parse("2026-01-01T00:00:00.000Z"),+ });++ const result = yield* runScan({ claudeHomePath, codexHomePath });++ expect(result.candidates.map((candidate) => candidate.path)).toEqual([workspace]);+ }),+ );++ it.effect("reads a complete transcript record at the exact chunk boundary", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const claudeHomePath = yield* makeTempDir("t3code-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const workspace = yield* makeTempDir("t3code-workspace-");+ const record = claudeSessionLine(workspace).split("\n")[0]!;+ const prefix = '{"padding":"';+ const suffix = `",${record.slice(1)}`;+ const contents = `${prefix}${"x".repeat(32 * 1024 - prefix.length - suffix.length)}${suffix}`;++ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-exact", "session.jsonl"),+ contents,+ mtimeMs: Date.parse("2026-01-01T00:00:00.000Z"),+ });++ const result = yield* runScan({ claudeHomePath, codexHomePath });++ expect(contents).toHaveLength(32 * 1024);+ expect(result.candidates.map((candidate) => candidate.path)).toEqual([workspace]);+ }),+ );++ it.effect("finds session metadata after a first record larger than one chunk", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const claudeHomePath = yield* makeTempDir("t3code-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const workspace = yield* makeTempDir("t3code-workspace-");+ const history = `{"type":"file-history-snapshot","data":"${"x".repeat(32 * 1024)}"}\n`;++ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-large", "session.jsonl"),+ contents: `${history}${claudeSessionLine(workspace)}`,+ mtimeMs: Date.parse("2026-01-01T00:00:00.000Z"),+ });++ const result = yield* runScan({ claudeHomePath, codexHomePath });++ expect(result.candidates.map((candidate) => candidate.path)).toEqual([workspace]);+ }),+ );++ it.effect.each([64, 65])("shares metadata bytes across homes for %s one-MiB files", (count) =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const fileSystem = yield* FileSystem.FileSystem;+ const claudeHomePath = yield* makeTempDir("t3code-metadata-home-");+ const secondHome = yield* makeTempDir("t3code-metadata-second-");+ const codexHomePath = yield* makeTempDir("t3code-metadata-codex-");+ const firstWorkspace = yield* makeTempDir("t3code-metadata-first-project-");+ const secondWorkspace = yield* makeTempDir("t3code-metadata-second-project-");+ const directories = [+ path.join(claudeHomePath, "projects", "p"),+ path.join(secondHome, "projects", "p"),+ ];+ const templates = directories.map((directory) => path.join(directory, "template.jsonl"));+ for (const [index, workspace] of [firstWorkspace, secondWorkspace].entries()) {+ const record = encodeTranscriptRecord({ cwd: workspace });+ yield* writeTranscript({+ filePath: templates[index]!,+ contents:+ " ".repeat(1024 * 1024 - new TextEncoder().encode(record).byteLength) + record,+ mtimeMs: Date.parse("2026-01-01T00:00:00.000Z") - index * 1_000,+ });+ }+ const resolveFile = (filePath: string) => {+ const index = directories.indexOf(path.dirname(filePath));+ return index === -1 ? filePath : templates[index]!;+ };+ let reservedBytes = 0;+ let opens = 0;+ const requests: number[] = [];+ const observedFileSystem = FileSystem.FileSystem.of({+ ...fileSystem,+ readDirectory: (directory, options) => {+ const index = directories.indexOf(directory);+ return index === -1+ ? fileSystem.readDirectory(directory, options)+ : Effect.succeed(+ Array.from(+ { length: index === 0 ? 32 : count - 32 },+ (_, item) => `session-${item}.jsonl`,+ ),+ );+ },+ stat: (filePath) => fileSystem.stat(resolveFile(filePath)),+ open: (filePath, options) => {+ if (!directories.includes(path.dirname(filePath)))+ return fileSystem.open(filePath, options);+ opens += 1;+ return fileSystem.open(resolveFile(filePath), options).pipe(+ Effect.map((file) => ({+ ...file,+ stat: file.stat,+ readAlloc: (size: FileSystem.SizeInput) => {+ reservedBytes += Number(size);+ requests.push(Number(size));+ return file.readAlloc(size);+ },+ })),+ );+ },+ });+ const result = yield* runScan({+ claudeHomePath,+ codexHomePath,+ providerInstances: {+ [ProviderInstanceId.make("claude-work")]: {+ driver: ProviderDriverKind.make("claudeAgent"),+ config: { homePath: secondHome },+ },+ },+ }).pipe(Effect.provideService(FileSystem.FileSystem, observedFileSystem));+ expect(result.candidates.map((candidate) => candidate.path)).toEqual([+ firstWorkspace,+ secondWorkspace,+ ]);+ expect(result.candidates.map((candidate) => candidate.threadCount)).toEqual([32, 32]);+ expect(result.truncated).toBe(count === 65 ? true : undefined);+ expect(opens).toBe(64);+ expect(reservedBytes).toBe(64 * 1024 * 1024);+ expect(requests[0]).toBe(8 * 1024);+ expect(Math.max(...requests)).toBe(8 * 1024);+ }),+ );++ it.effect.each([50, 51])("bounds metadata open/read calls for %s short-read files", (count) =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const fileSystem = yield* FileSystem.FileSystem;+ const claudeHomePath = yield* makeTempDir("t3code-short-metadata-home-");+ const codexHomePath = yield* makeTempDir("t3code-short-metadata-codex-");+ const workspace = yield* makeTempDir("t3code-short-metadata-project-");+ const directory = path.join(claudeHomePath, "projects", "p");+ const template = path.join(directory, "template.jsonl");+ const record = encodeTranscriptRecord({ cwd: workspace });+ const contents = " ".repeat(399 - new TextEncoder().encode(record).byteLength) + record;+ const bytes = new TextEncoder().encode(contents);+ yield* writeTranscript({+ filePath: template,+ contents,+ mtimeMs: Date.parse("2026-01-01T00:00:00.000Z"),+ });+ let operations = 0;+ const observedFileSystem = FileSystem.FileSystem.of({+ ...fileSystem,+ readDirectory: (target, options) =>+ target === directory+ ? Effect.succeed(+ Array.from({ length: count }, (_, index) => `session-${index}.jsonl`),+ )+ : fileSystem.readDirectory(target, options),+ stat: (filePath) =>+ fileSystem.stat(path.dirname(filePath) === directory ? template : filePath),+ open: (filePath, options) => {+ if (path.dirname(filePath) !== directory) return fileSystem.open(filePath, options);+ operations += 1;+ let offset = 0;+ return fileSystem.open(template, options).pipe(+ Effect.map((file) => ({+ ...file,+ stat: file.stat,+ readAlloc: () =>+ Effect.sync(() => {+ operations += 1;+ if (offset === bytes.length) return Option.none<Uint8Array>();+ return Option.some(bytes.subarray(offset, ++offset));+ }),+ })),+ );+ },+ });+ const result = yield* runScan({ claudeHomePath, codexHomePath }).pipe(+ Effect.provideService(FileSystem.FileSystem, observedFileSystem),+ );+ expect(operations).toBe(20_000);+ expect(result.candidates[0]?.threadCount).toBe(50);+ expect(result.truncated).toBe(count === 51 ? true : undefined);+ }),+ );++ it.effect("bounds malformed metadata records without excluding another account", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const fileSystem = yield* FileSystem.FileSystem;+ const claudeHomePath = yield* makeTempDir("t3code-record-metadata-home-");+ const secondHome = yield* makeTempDir("t3code-record-metadata-second-");+ const codexHomePath = yield* makeTempDir("t3code-record-metadata-codex-");+ const workspace = yield* makeTempDir("t3code-record-metadata-project-");+ const directory = path.join(claudeHomePath, "projects", "p");+ const template = path.join(directory, "template.jsonl");+ yield* writeTranscript({+ filePath: template,+ contents: "x\n".repeat(1_001),+ mtimeMs: Date.parse("2026-01-02T00:00:00.000Z"),+ });+ yield* writeTranscript({+ filePath: path.join(secondHome, "projects", "p", "session.jsonl"),+ contents: encodeTranscriptRecord({ cwd: workspace }),+ mtimeMs: Date.parse("2026-01-01T00:00:00.000Z"),+ });+ let malformedOpens = 0;+ const observedFileSystem = FileSystem.FileSystem.of({+ ...fileSystem,+ readDirectory: (target, options) =>+ target === directory+ ? Effect.succeed(Array.from({ length: 102 }, (_, index) => `session-${index}.jsonl`))+ : fileSystem.readDirectory(target, options),+ stat: (filePath) =>+ fileSystem.stat(path.dirname(filePath) === directory ? template : filePath),+ open: (filePath, options) => {+ if (path.dirname(filePath) !== directory) return fileSystem.open(filePath, options);+ malformedOpens += 1;+ return fileSystem.open(template, options);+ },+ });+ const result = yield* runScan({+ claudeHomePath,+ codexHomePath,+ providerInstances: {+ [ProviderInstanceId.make("claude-work")]: {+ driver: ProviderDriverKind.make("claudeAgent"),+ config: { homePath: secondHome },+ },+ },+ }).pipe(Effect.provideService(FileSystem.FileSystem, observedFileSystem));+ expect(result.candidates.map((candidate) => candidate.path)).toEqual([workspace]);+ expect(malformedOpens).toBe(100);+ expect(result.truncated).toBe(true);+ }),+ );++ it.effect.each([19_999, 20_000])(+ "reports unfinished directory work for %s project directories",+ (count) =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const fileSystem = yield* FileSystem.FileSystem;+ const claudeHomePath = yield* makeTempDir("t3code-directory-budget-home-");+ const codexHomePath = yield* makeTempDir("t3code-directory-budget-codex-");+ const projectsDir = path.join(claudeHomePath, "projects");+ let reads = 0;+ const observedFileSystem = FileSystem.FileSystem.of({+ ...fileSystem,+ readDirectory: (directory, options) => {+ if (directory === projectsDir) {+ reads += 1;+ return Effect.succeed(+ Array.from({ length: count }, (_, index) => `project-${index}`),+ );+ }+ if (path.dirname(directory) === projectsDir) {+ reads += 1;+ return Effect.succeed([]);+ }+ return fileSystem.readDirectory(directory, options);+ },+ });+ const result = yield* runScan({ claudeHomePath, codexHomePath }).pipe(+ Effect.provideService(FileSystem.FileSystem, observedFileSystem),+ );+ expect(reads).toBe(20_000);+ expect(result.candidates).toEqual([]);+ expect(result.truncated).toBe(count === 20_000 ? true : undefined);+ }),+ );++ it.effect("skips malformed transcripts without failing the scan", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const claudeHomePath = yield* makeTempDir("t3code-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const workspace = yield* makeTempDir("t3code-workspace-");++ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-broken", "a.jsonl"),+ contents: "not json at all\n",+ mtimeMs: Date.parse("2026-05-01T00:00:00.000Z"),+ });+ // Valid JSON, but no cwd anywhere in the record.+ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-no-cwd", "a.jsonl"),+ contents: `{"type":"summary"}\n`,+ mtimeMs: Date.parse("2026-05-02T00:00:00.000Z"),+ });+ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-good", "a.jsonl"),+ contents: claudeSessionLine(workspace),+ mtimeMs: Date.parse("2026-05-03T00:00:00.000Z"),+ });++ const result = yield* runScan({ claudeHomePath, codexHomePath });++ expect(result.candidates).toEqual([+ {+ path: workspace,+ title: path.basename(workspace),+ sources: ["claudeAgent"],+ threadCount: 1,+ lastActiveAt: "2026-05-03T00:00:00.000Z",+ alreadyImported: false,+ },+ ]);+ }),+ );++ it.effect("returns an empty result when neither home directory exists", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const root = yield* makeTempDir("t3code-missing-homes-");++ const result = yield* runScan({+ claudeHomePath: path.join(root, "no-claude"),+ codexHomePath: path.join(root, "no-codex"),+ });++ expect(result.candidates).toEqual([]);+ expect(result.scannedAt).toMatch(/^\d{4}-\d{2}-\d{2}T/);+ }),+ );+ });++ describe("recentThreads", () => {+ it.effect.each([false, true])(+ "counts terminal newlines correctly with record overflow=%s",+ (overflow) =>+ 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-record-limit-claude-");+ const codexHomePath = yield* makeTempDir("t3code-record-limit-codex-");+ const workspace = yield* makeTempDir("t3code-record-limit-project-");+ const directory = path.join(codexHomePath, "sessions", "2026", "08", "24");+ yield* writeTranscript({+ filePath: path.join(directory, "rollout-records.jsonl"),+ contents: makeRecordLimitTranscript(workspace, overflow),+ mtimeMs: nowMs,+ });+ yield* writeTranscript({+ filePath: path.join(directory, "rollout-older.jsonl"),+ contents: [+ encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: "older-session", cwd: workspace },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: { type: "user_message", message: "Older prompt" },+ }),+ ].join("\n"),+ mtimeMs: nowMs - 1_000,+ });+ const outcomes = yield* runRecentThreadOutcomes({+ claudeHomePath,+ codexHomePath,+ workspaceRoot: workspace,+ });+ expect(outcomes.map((outcome) => outcome._tag)).toEqual(+ overflow ? ["Skipped", "Importable"] : ["Importable", "Skipped"],+ );+ expect(+ outcomes.flatMap((outcome) =>+ outcome._tag === "Importable"+ ? outcome.thread.messages.map((message) => message.text)+ : [],+ ),+ ).toEqual([overflow ? "Older prompt" : "First prompt"]);+ }),+ );++ it.effect("imports recent Claude and Codex sessions for the selected project only", () =>+ 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-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const workspace = yield* makeTempDir("t3code-workspace-");+ const otherWorkspace = yield* makeTempDir("t3code-workspace-other-");++ const claudeTranscript = (cwd: string, sessionId: string) =>+ `${JSON.stringify({+ type: "user",+ cwd,+ sessionId,+ timestamp: "2026-08-23T12:00:00.000Z",+ message: { role: "user", content: "Fix the project" },+ })}\n${JSON.stringify({+ type: "assistant",+ sessionId,+ timestamp: "2026-08-23T12:01:00.000Z",+ message: { role: "assistant", content: [{ type: "text", text: "Done" }] },+ })}\n`;++ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-selected", "claude-recent.jsonl"),+ contents: claudeTranscript(workspace, "claude-recent"),+ mtimeMs: nowMs - 24 * 60 * 60 * 1000,+ });+ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-selected", "claude-old.jsonl"),+ contents: claudeTranscript(workspace, "claude-old"),+ mtimeMs: nowMs - 31 * 24 * 60 * 60 * 1000,+ });+ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-other", "claude-other.jsonl"),+ contents: claudeTranscript(otherWorkspace, "claude-other"),+ mtimeMs: nowMs - 24 * 60 * 60 * 1000,+ });+ yield* writeTranscript({+ filePath: path.join(+ codexHomePath,+ "sessions",+ "2026",+ "08",+ "24",+ "rollout-codex-recent.jsonl",+ ),+ contents: [+ encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: "codex-recent", cwd: workspace },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ timestamp: "2026-08-24T10:00:00.000Z",+ payload: { type: "user_message", message: "Review this code" },+ }),+ encodeTranscriptRecord({+ type: "response_item",+ timestamp: "2026-08-24T10:01:00.000Z",+ payload: {+ type: "message",+ role: "assistant",+ content: [{ type: "output_text", text: "Looks good" }],+ },+ }),+ ].join("\n"),+ mtimeMs: nowMs - 60 * 60 * 1000,+ });++ const threads = yield* runRecentThreads({+ claudeHomePath,+ codexHomePath,+ workspaceRoot: workspace,+ });++ expect(threads.map((thread) => thread.providerSessionId)).toEqual([+ "codex-recent",+ "claude-recent",+ ]);+ expect(threads.map((thread) => thread.messages.map((message) => message.text))).toEqual([+ ["Review this code", "Looks good"],+ ["Fix the project", "Done"],+ ]);+ }),+ );++ it.effect("imports history recorded with a case alias", () =>+ 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 workspaceAlias = path.join(+ path.dirname(workspace),+ path.basename(workspace).toUpperCase(),+ );++ yield* writeTranscript({+ filePath: path.join(claudeHomePath, "projects", "-alias", "case-session.jsonl"),+ contents: [+ encodeTranscriptRecord({+ type: "user",+ cwd: workspaceAlias,+ sessionId: "case-session",+ timestamp: "2026-08-24T10:00:00.000Z",+ message: { role: "user", content: "Import case alias history" },+ }),+ encodeTranscriptRecord({+ type: "assistant",+ sessionId: "case-session",+ timestamp: "2026-08-24T10:01:00.000Z",+ message: { role: "assistant", content: "Imported" },+ }),+ ].join("\n"),+ mtimeMs: nowMs,+ });++ const simulatedFileSystem = FileSystem.FileSystem.of({+ ...fileSystem,+ stat: (filePath) => fileSystem.stat(filePath === workspaceAlias ? workspace : filePath),+ });+ const threads = yield* runRecentThreads({+ claudeHomePath,+ codexHomePath,+ workspaceRoot: workspace,+ }).pipe(Effect.provideService(FileSystem.FileSystem, simulatedFileSystem));++ expect(threads.map((thread) => thread.providerSessionId)).toEqual(["case-session"]);+ }),+ );++ it.effect("keeps the provider instance that owns a custom session home", () =>+ 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-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const customHome = yield* makeTempDir("t3code-codex-custom-");+ const workspace = yield* makeTempDir("t3code-workspace-");++ yield* writeTranscript({+ filePath: path.join(customHome, "sessions", "2026", "08", "24", "rollout-custom.jsonl"),+ contents: [+ encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: "custom-session", cwd: workspace },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: { type: "user_message", message: "Use my work account" },+ }),+ ].join("\n"),+ mtimeMs: nowMs,+ });++ const threads = yield* runRecentThreads({+ claudeHomePath,+ codexHomePath,+ workspaceRoot: workspace,+ providerInstances: {+ [ProviderInstanceId.make("codex-work")]: {+ driver: ProviderDriverKind.make("codex"),+ config: { homePath: customHome },+ },+ },+ });++ expect(threads[0]?.providerInstanceId).toBe("codex-work");+ }),+ );++ it.effect("suppresses duplicate session copies without reporting a skipped import", () =>+ 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-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const workspace = yield* makeTempDir("t3code-workspace-");+ const contents = [+ encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: "copied-session", cwd: workspace },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: { type: "user_message", message: "Import this session once" },+ }),+ ].join("\n");++ for (const [name, mtimeMs] of [+ ["rollout-copy-a.jsonl", nowMs],+ ["rollout-copy-b.jsonl", nowMs - 1],+ ] as const) {+ yield* writeTranscript({+ filePath: path.join(codexHomePath, "sessions", "2026", "08", "24", name),+ contents,+ mtimeMs,+ });+ }++ const outcomes = yield* runRecentThreadOutcomes({+ claudeHomePath,+ codexHomePath,+ workspaceRoot: workspace,+ });++ expect(outcomes.map((outcome) => outcome._tag)).toEqual(["Importable", "Duplicate"]);+ expect(outcomes[0]).toMatchObject({+ _tag: "Importable",+ thread: { providerSessionId: "copied-session" },+ });+ }),+ );++ it.effect("shares a 64 MiB full-read budget across providers without hiding projects", () =>+ 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-budget-claude-");+ const codexHomePath = yield* makeTempDir("t3code-budget-codex-");+ const workspace = yield* makeTempDir("t3code-budget-workspace-");+ const transcriptPaths = new Set<string>();+ for (const [index, source] of [+ "codex",+ "claudeAgent",+ "codex",+ "claudeAgent",+ "codex",+ ].entries()) {+ const sessionId = `budget-session-${index}`;+ const filePath =+ source === "codex"+ ? path.join(+ codexHomePath,+ "sessions",+ "2026",+ "08",+ "24",+ `rollout-${sessionId}.jsonl`,+ )+ : path.join(claudeHomePath, "projects", "selected", `${sessionId}.jsonl`);+ const contents =+ source === "codex"+ ? [+ encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: sessionId, cwd: workspace },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: { type: "user_message", message: "Imported prompt" },+ }),+ ].join("\n")+ : encodeTranscriptRecord({+ type: "user",+ cwd: workspace,+ sessionId,+ message: { content: "Imported prompt" },+ });+ transcriptPaths.add(filePath);+ yield* writeTranscript({+ filePath,+ contents: `${contents}\n`.padEnd(16 * 1024 * 1024, " "),+ mtimeMs: nowMs - index * 1_000,+ });+ }++ const opens = new Map<string, number>();+ let fullReadBytes = 0;+ const trackedFileSystem = FileSystem.FileSystem.of({+ ...fileSystem,+ open: (filePath, options) => {+ const count = (opens.get(filePath) ?? 0) + 1;+ opens.set(filePath, count);+ return fileSystem.open(filePath, options).pipe(+ Effect.map((file) =>+ !transcriptPaths.has(filePath) || count === 1+ ? file+ : {+ ...file,+ stat: file.stat,+ readAlloc: (size: FileSystem.SizeInput) =>+ file.readAlloc(size).pipe(+ Effect.tap((chunk) =>+ Effect.sync(() => {+ if (chunk._tag === "Some") fullReadBytes += chunk.value.byteLength;+ }),+ ),+ ),+ },+ ),+ );+ },+ });+ const outcomes = yield* Effect.gen(function* () {+ const scanner = yield* AgentSessionScanner.AgentSessionScanner;+ const scan = yield* scanner.scan;+ expect(scan.candidates[0]?.threadCount).toBe(5);+ return yield* scanner.recentThreads(workspace).pipe(Stream.runCollect);+ }).pipe(+ Effect.provide(makeScannerTestLayer({ claudeHomePath, codexHomePath })),+ Effect.provideService(FileSystem.FileSystem, trackedFileSystem),+ );++ expect(outcomes.map((outcome) => outcome._tag)).toEqual([+ "Importable",+ "Importable",+ "Importable",+ "Importable",+ "Skipped",+ ]);+ expect(fullReadBytes).toBe(64 * 1024 * 1024);+ }),+ );++ it.effect("skips excessive records without blocking an older valid transcript", () =>+ 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-record-budget-claude-");+ const codexHomePath = yield* makeTempDir("t3code-record-budget-codex-");+ const workspace = yield* makeTempDir("t3code-record-budget-workspace-");+ for (const [sessionId, padding, mtimeMs] of [+ ["excessive", "\n".repeat(100_001), nowMs],+ ["older", "", nowMs - 1_000],+ ] as const) {+ yield* writeTranscript({+ filePath: path.join(+ codexHomePath,+ "sessions",+ "2026",+ "08",+ "24",+ `rollout-${sessionId}.jsonl`,+ ),+ contents:+ [+ encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: sessionId, cwd: workspace },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: { type: "user_message", message: "Imported prompt" },+ }),+ ].join("\n") + padding,+ mtimeMs,+ });+ }+ const outcomes = yield* runRecentThreadOutcomes({+ claudeHomePath,+ codexHomePath,+ workspaceRoot: workspace,+ });+ expect(outcomes.map((outcome) => outcome._tag)).toEqual(["Skipped", "Importable"]);+ expect(outcomes[1]).toMatchObject({ thread: { providerSessionId: "older" } });+ }),+ );++ for (const source of ["claudeAgent", "codex"] as const) {+ for (const replacement of [+ "same root",+ "other root",+ "symlink alias",+ "other then same",+ ] as const) {+ it.effect.skipIf(replacement === "symlink alias" && !symlinksSupported)(+ `rechecks ${source} snapshot cwd after replacement with ${replacement}`,+ () =>+ 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 fixture = yield* makeTempDir("t3code-replaced-cwd-");+ const workspace = path.join(fixture, "original");+ const otherWorkspace = path.join(fixture, "other");+ const alias = path.join(fixture, "alias");+ const claudeHomePath = path.join(fixture, "claude");+ const codexHomePath = path.join(fixture, "codex");+ yield* fileSystem.makeDirectory(workspace);+ yield* fileSystem.makeDirectory(otherWorkspace);+ if (replacement === "symlink alias") yield* fileSystem.symlink(workspace, alias);+ const filePath =+ source === "codex"+ ? path.join(+ codexHomePath,+ "sessions",+ "2026",+ "08",+ "24",+ "rollout-replaced.jsonl",+ )+ : path.join(claudeHomePath, "projects", "p", "replaced.jsonl");+ const makeContents = (cwd: string, text: string, laterCwd?: string) =>+ [+ ...(source === "codex"+ ? [+ { type: "session_meta", payload: { id: "replacement-session", cwd } },+ { type: "event_msg", payload: { type: "user_message", message: text } },+ ]+ : [+ {+ type: "user",+ cwd,+ sessionId: "replacement-session",+ message: { content: text },+ },+ ]),+ ...(laterCwd === undefined ? [] : [{ cwd: laterCwd }]),+ ]+ .map((record) => encodeTranscriptRecord(record))+ .join("\n");+ yield* writeTranscript({+ filePath,+ contents: makeContents(workspace, "Original prompt"),+ mtimeMs: nowMs,+ });++ yield* Effect.gen(function* () {+ const scanner = yield* AgentSessionScanner.AgentSessionScanner;+ const scan = yield* scanner.scan;+ expect(scan.candidates.map((candidate) => candidate.path)).toEqual([workspace]);+ const replacementCwd =+ replacement === "symlink alias"+ ? alias+ : replacement === "same root"+ ? workspace+ : otherWorkspace;+ yield* fileSystem.remove(filePath);+ yield* writeTranscript({+ filePath,+ contents: makeContents(+ replacementCwd,+ "Replacement prompt",+ replacement === "other then same" ? workspace : undefined,+ ),+ mtimeMs: nowMs,+ });+ const outcomes = yield* scanner.recentThreads(workspace).pipe(Stream.runCollect);+ if (replacement === "same root" || replacement === "symlink alias") {+ expect(outcomes).toHaveLength(1);+ expect(outcomes[0]).toMatchObject({+ _tag: "Importable",+ thread: { messages: [{ text: "Replacement prompt" }] },+ });+ } else {+ expect(outcomes).toEqual([{ _tag: "Skipped" }]);+ }+ }).pipe(Effect.provide(makeScannerTestLayer({ claudeHomePath, codexHomePath })));+ }),+ );+ }+ }++ it.effect("checks file identity and provider before skipping completed history", () =>+ 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-completed-claude-");+ const codexHomePath = yield* makeTempDir("t3code-completed-codex-");+ const workspace = yield* makeTempDir("t3code-completed-workspace-");+ const filePath = path.join(+ codexHomePath,+ "sessions",+ "2026",+ "08",+ "24",+ "rollout-replaced.jsonl",+ );+ const contents = (sessionId: string) =>+ [+ encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: sessionId, cwd: workspace },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: { type: "user_message", message: "Imported prompt" },+ }),+ ].join("\n");+ yield* writeTranscript({+ filePath,+ contents: contents("original-session"),+ mtimeMs: nowMs,+ });++ yield* Effect.gen(function* () {+ const scanner = yield* AgentSessionScanner.AgentSessionScanner;+ const initial = yield* scanner.recentThreads(workspace).pipe(Stream.runCollect);+ const imported = initial[0];+ expect(imported?._tag).toBe("Importable");+ if (imported?._tag !== "Importable") return;+ const completed = yield* scanner+ .recentThreads(workspace, [imported.source])+ .pipe(Stream.runCollect);+ expect(completed[0]?._tag).toBe("AlreadyImported");+ const wrongProvider = yield* scanner+ .recentThreads(workspace, [{ ...imported.source, provider: "claudeAgent" }])+ .pipe(Stream.runCollect);+ expect(wrongProvider[0]?._tag).toBe("Importable");++ // Keep the old inode allocated while replacing the path with an equal-size file.+ yield* fileSystem.open(filePath);+ yield* fileSystem.remove(filePath);+ yield* writeTranscript({+ filePath,+ contents: contents("replaced-session"),+ mtimeMs: nowMs,+ });+ const replaced = yield* scanner+ .recentThreads(workspace, [imported.source])+ .pipe(Stream.runCollect);+ expect(replaced[0]).toMatchObject({+ _tag: "Importable",+ thread: { providerSessionId: "replaced-session" },+ source: { size: imported.source.size, mtimeMs: imported.source.mtimeMs },+ });+ }).pipe(Effect.provide(makeScannerTestLayer({ claudeHomePath, codexHomePath })));+ }),+ );++ it.effect("reports an eligible transcript over 16 MiB as skipped", () =>+ 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-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const workspace = yield* makeTempDir("t3code-workspace-");+ const transcript = [+ encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: "large-session", cwd: workspace },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: { type: "user_message", message: "Import this large session" },+ }),+ ]+ .join("\n")+ .padEnd(16 * 1024 * 1024 + 1, " ");+ yield* writeTranscript({+ filePath: path.join(codexHomePath, "sessions", "2026", "08", "24", "rollout-large.jsonl"),+ contents: transcript,+ mtimeMs: nowMs,+ });++ const outcomes = yield* runRecentThreadOutcomes({+ claudeHomePath,+ codexHomePath,+ workspaceRoot: workspace,+ });++ expect(outcomes).toEqual([{ _tag: "Skipped" }]);+ }),+ );++ it.effect("reports stat, read, and parse failures as skipped", () =>+ 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 missingPath = path.join(codexHomePath, "missing.jsonl");+ const transcriptPaths = {+ stat: path.join(codexHomePath, "sessions", "2026", "08", "24", "rollout-stat.jsonl"),+ read: path.join(codexHomePath, "sessions", "2026", "08", "24", "rollout-read.jsonl"),+ parse: path.join(codexHomePath, "sessions", "2026", "08", "24", "rollout-parse.jsonl"),+ };+ const transcriptContents = (sessionId: string) =>+ [+ encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: sessionId, cwd: workspace },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: { type: "user_message", message: "Import this session" },+ }),+ ].join("\n");++ yield* writeTranscript({+ filePath: transcriptPaths.stat,+ contents: transcriptContents("stat-session"),+ mtimeMs: nowMs,+ });+ yield* writeTranscript({+ filePath: transcriptPaths.read,+ contents: transcriptContents("read-session"),+ mtimeMs: nowMs,+ });+ yield* writeTranscript({+ filePath: transcriptPaths.parse,+ contents: encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: "parse-session", cwd: workspace },+ }),+ mtimeMs: nowMs,+ });++ let statCount = 0;+ let readOpenCount = 0;+ const simulatedFileSystem = FileSystem.FileSystem.of({+ ...fileSystem,+ stat: (filePath) => {+ if (filePath !== transcriptPaths.stat) return fileSystem.stat(filePath);+ statCount += 1;+ return fileSystem.stat(statCount === 1 ? filePath : missingPath);+ },+ open: (filePath, options) => {+ if (filePath !== transcriptPaths.read) return fileSystem.open(filePath, options);+ readOpenCount += 1;+ return fileSystem.open(readOpenCount === 1 ? filePath : missingPath, options);+ },+ });++ const outcomes = yield* runRecentThreadOutcomes({+ claudeHomePath,+ codexHomePath,+ workspaceRoot: workspace,+ }).pipe(Effect.provideService(FileSystem.FileSystem, simulatedFileSystem));++ expect(outcomes).toEqual([{ _tag: "Skipped" }, { _tag: "Skipped" }, { _tag: "Skipped" }]);+ }),+ );++ it.effect("does not reopen a transcript that becomes a non-file after discovery", () =>+ 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 nonFilePath = yield* makeTempDir("t3code-non-file-");+ const transcriptPath = path.join(+ codexHomePath,+ "sessions",+ "2026",+ "08",+ "24",+ "rollout-changed.jsonl",+ );+ yield* writeTranscript({+ filePath: transcriptPath,+ contents: [+ encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: "changed-session", cwd: workspace },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: { type: "user_message", message: "Do not import this session" },+ }),+ ].join("\n"),+ mtimeMs: nowMs,+ });++ let transcriptStatCount = 0;+ let transcriptOpenCount = 0;+ const simulatedFileSystem = FileSystem.FileSystem.of({+ ...fileSystem,+ stat: (filePath) => {+ if (filePath !== transcriptPath) return fileSystem.stat(filePath);+ transcriptStatCount += 1;+ return fileSystem.stat(transcriptStatCount === 1 ? transcriptPath : nonFilePath);+ },+ open: (filePath, options) => {+ if (filePath === transcriptPath) transcriptOpenCount += 1;+ return fileSystem.open(filePath, options);+ },+ });++ const outcomes = yield* runRecentThreadOutcomes({+ claudeHomePath,+ codexHomePath,+ workspaceRoot: workspace,+ }).pipe(Effect.provideService(FileSystem.FileSystem, simulatedFileSystem));++ expect(transcriptStatCount).toBe(2);+ expect(transcriptOpenCount).toBe(1);+ expect(outcomes).toEqual([{ _tag: "Skipped" }]);+ }),+ );++ it.effect("does not import a transcript dated after the current time", () =>+ 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-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const workspace = yield* makeTempDir("t3code-workspace-");+ yield* writeTranscript({+ filePath: path.join(+ codexHomePath,+ "sessions",+ "2026",+ "08",+ "24",+ "rollout-future.jsonl",+ ),+ contents: [+ encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: "future-session", cwd: workspace },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: { type: "user_message", message: "Future work" },+ }),+ ].join("\n"),+ mtimeMs: nowMs + 1,+ });++ const outcomes = yield* runRecentThreadOutcomes({+ claudeHomePath,+ codexHomePath,+ workspaceRoot: workspace,+ });++ expect(outcomes).toEqual([]);+ }),+ );++ it.effect("skips growth during reading without exceeding the reserved bytes", () =>+ 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-growing.jsonl",+ );+ const contents = [+ encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: "growing-session", cwd: workspace },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: { type: "user_message", message: "Do not import a changing file" },+ }),+ ].join("\n");+ yield* writeTranscript({ filePath: transcriptPath, contents, mtimeMs: nowMs });+ let transcriptOpenCount = 0;+ let fullReadBytes = 0;+ let grew = false;+ const simulatedFileSystem = FileSystem.FileSystem.of({+ ...fileSystem,+ open: (filePath, options) => {+ if (filePath !== transcriptPath) return fileSystem.open(filePath, options);+ transcriptOpenCount += 1;+ if (transcriptOpenCount === 1) return fileSystem.open(filePath, options);+ return fileSystem.open(filePath, options).pipe(+ Effect.map((file) => ({+ ...file,+ stat: file.stat,+ readAlloc: (size: FileSystem.SizeInput) =>+ file.readAlloc(size).pipe(+ Effect.tap((chunk) =>+ Effect.gen(function* () {+ if (chunk._tag === "None") return;+ fullReadBytes += chunk.value.byteLength;+ if (!grew) {+ grew = true;+ yield* fileSystem.writeFileString(filePath, `${contents}\nchanged`);+ }+ }),+ ),+ ),+ })),+ );+ },+ });++ const outcomes = yield* runRecentThreadOutcomes({+ claudeHomePath,+ codexHomePath,+ workspaceRoot: workspace,+ }).pipe(Effect.provideService(FileSystem.FileSystem, simulatedFileSystem));++ expect(transcriptOpenCount).toBe(2);+ expect(fullReadBytes).toBe(new TextEncoder().encode(contents).byteLength);+ expect(outcomes).toEqual([{ _tag: "Skipped" }]);+ }),+ );++ it.effect("skips a transcript that shrinks after its size check", () =>+ 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-shrinking.jsonl",+ );+ const shrunkPath = path.join(codexHomePath, "shrunk.jsonl");+ const contents = [+ encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: "shrinking-session", cwd: workspace },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: { type: "user_message", message: "Do not import a changing file" },+ }),+ ].join("\n");+ yield* writeTranscript({+ filePath: transcriptPath,+ contents: `${contents}\n${"padding".repeat(100)}`,+ mtimeMs: nowMs,+ });+ yield* writeTranscript({ filePath: shrunkPath, contents, mtimeMs: nowMs });++ 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 === 1 ? transcriptPath : shrunkPath,+ options,+ );+ },+ });++ const outcomes = yield* runRecentThreadOutcomes({+ claudeHomePath,+ codexHomePath,+ workspaceRoot: workspace,+ }).pipe(Effect.provideService(FileSystem.FileSystem, simulatedFileSystem));++ expect(transcriptOpenCount).toBe(2);+ expect(outcomes).toEqual([{ _tag: "Skipped" }]);+ }),+ );++ it.effect("does not read the second transcript when the consumer takes one thread", () =>+ 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 makeCodexTranscript = (sessionId: string, text: string) =>+ [+ encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: sessionId, cwd: workspace },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: { type: "user_message", message: text },+ }),+ ].join("\n");+ const olderPath = path.join(+ codexHomePath,+ "sessions",+ "2026",+ "08",+ "23",+ "rollout-older.jsonl",+ );+ const newerPath = path.join(+ codexHomePath,+ "sessions",+ "2026",+ "08",+ "24",+ "rollout-newer.jsonl",+ );+ yield* writeTranscript({+ filePath: olderPath,+ contents: makeCodexTranscript("older-session", "Older prompt"),+ mtimeMs: nowMs - 1_000,+ });+ yield* writeTranscript({+ filePath: newerPath,+ contents: makeCodexTranscript("newer-session", "Newer prompt"),+ mtimeMs: nowMs,+ });++ const openCounts = new Map<string, number>();+ const contentReads: Array<string> = [];+ const trackedPaths = new Set([olderPath, newerPath]);+ const simulatedFileSystem = FileSystem.FileSystem.of({+ ...fileSystem,+ open: (filePath, options) => {+ if (trackedPaths.has(filePath)) {+ const count = (openCounts.get(filePath) ?? 0) + 1;+ openCounts.set(filePath, count);+ if (count === 2) contentReads.push(filePath);+ }+ return fileSystem.open(filePath, options);+ },+ });++ const threads = yield* Effect.gen(function* () {+ const scanner = yield* AgentSessionScanner.AgentSessionScanner;+ return yield* scanner.recentThreads(workspace).pipe(+ Stream.take(1),+ Stream.runCollect,+ Effect.map((items) => Array.from(items)),+ );+ }).pipe(+ Effect.provide(makeScannerTestLayer({ claudeHomePath, codexHomePath })),+ Effect.provideService(FileSystem.FileSystem, simulatedFileSystem),+ );++ expect(+ threads.flatMap((outcome) =>+ outcome._tag === "Importable" ? [outcome.thread.providerSessionId] : [],+ ),+ ).toEqual(["newer-session"]);+ expect(contentReads).toEqual([newerPath]);+ expect(openCounts.get(olderPath)).toBe(1);+ }),+ );++ it.effect("does not import sessions from a T3-managed worktree", () =>+ 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 configBaseDir = yield* makeTempDir("t3code-scanner-base-");+ const workspace = path.join(configBaseDir, "worktrees", "t3code", "managed-worktree");+ yield* fileSystem.makeDirectory(workspace, { recursive: true });++ yield* writeTranscript({+ filePath: path.join(+ codexHomePath,+ "sessions",+ "2026",+ "08",+ "24",+ "rollout-managed.jsonl",+ ),+ contents: [+ encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: "managed-session", cwd: workspace },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: { type: "user_message", message: "Do not import this session" },+ }),+ ].join("\n"),+ mtimeMs: nowMs,+ });++ const threads = yield* runRecentThreads({+ claudeHomePath,+ codexHomePath,+ configBaseDir,+ workspaceRoot: workspace,+ });++ expect(threads).toEqual([]);+ }),+ );++ it.effect("uses one deterministic provider instance for a shared session home", () =>+ 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-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const sharedHome = yield* makeTempDir("t3code-codex-shared-");+ const workspace = yield* makeTempDir("t3code-workspace-");++ yield* writeTranscript({+ filePath: path.join(sharedHome, "sessions", "2026", "08", "24", "rollout-shared.jsonl"),+ contents: [+ encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: "shared-session", cwd: workspace },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: { type: "user_message", message: "Use the shared session" },+ }),+ ].join("\n"),+ mtimeMs: nowMs,+ });++ const threads = yield* runRecentThreads({+ claudeHomePath,+ codexHomePath,+ workspaceRoot: workspace,+ providerInstances: {+ [ProviderInstanceId.make("codex")]: {+ driver: ProviderDriverKind.make("codex"),+ config: { homePath: sharedHome },+ },+ [ProviderInstanceId.make("codex-personal")]: {+ driver: ProviderDriverKind.make("codex"),+ config: { homePath: sharedHome },+ },+ [ProviderInstanceId.make("codex-work")]: {+ driver: ProviderDriverKind.make("codex"),+ config: { homePath: sharedHome },+ },+ },+ });++ expect(threads.map((thread) => thread.providerInstanceId)).toEqual(["codex"]);+ }),+ );++ it.effect("uses configured order when custom instances share a session home", () =>+ 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-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const sharedHome = yield* makeTempDir("t3code-codex-shared-");+ const workspace = yield* makeTempDir("t3code-workspace-");++ yield* writeTranscript({+ filePath: path.join(sharedHome, "sessions", "2026", "08", "24", "rollout-shared.jsonl"),+ contents: [+ encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: "shared-session", cwd: workspace },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: { type: "user_message", message: "Use the first account" },+ }),+ ].join("\n"),+ mtimeMs: nowMs,+ });++ const threads = yield* runRecentThreads({+ claudeHomePath,+ codexHomePath,+ workspaceRoot: workspace,+ providerInstances: {+ [ProviderInstanceId.make("codex-work")]: {+ driver: ProviderDriverKind.make("codex"),+ config: { homePath: sharedHome },+ },+ [ProviderInstanceId.make("codex-personal")]: {+ driver: ProviderDriverKind.make("codex"),+ config: { homePath: sharedHome },+ },+ },+ });++ expect(threads.map((thread) => thread.providerInstanceId)).toEqual(["codex-work"]);+ }),+ );++ it.effect("keeps a second account when the first has 5000 newer files", () =>+ 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 oldWorkspace = yield* makeTempDir("t3code-workspace-old-");+ const recentWorkspace = yield* makeTempDir("t3code-workspace-recent-");+ const recentHome = yield* makeTempDir("t3code-claude-recent-home-");+ const oldDirectory = path.join(claudeHomePath, "projects", "-aaa-old");+ const oldTranscript = path.join(oldDirectory, "old.jsonl");+ const recentDirectory = path.join(recentHome, "projects", "-zzz-recent");++ yield* writeTranscript({+ filePath: oldTranscript,+ contents: encodeTranscriptRecord({+ type: "user",+ cwd: oldWorkspace,+ sessionId: "old-session",+ message: { role: "user", content: "Old work" },+ }),+ mtimeMs: nowMs,+ });+ yield* writeTranscript({+ filePath: path.join(recentDirectory, "recent.jsonl"),+ contents: encodeTranscriptRecord({+ type: "user",+ cwd: recentWorkspace,+ sessionId: "recent-session",+ message: { role: "user", content: "Recent work" },+ }),+ mtimeMs: nowMs - 1_000,+ });++ const simulatedOldTranscripts = Array.from(+ { length: 5_000 },+ (_, index) => `old-${index}.jsonl`,+ );+ const resolveTranscript = (filePath: string) =>+ path.dirname(filePath) === oldDirectory && path.basename(filePath).startsWith("old-")+ ? oldTranscript+ : filePath;+ const simulatedFileSystem = FileSystem.FileSystem.of({+ ...fileSystem,+ readDirectory: (directory, options) =>+ directory === oldDirectory+ ? Effect.succeed(simulatedOldTranscripts)+ : fileSystem.readDirectory(directory, options),+ stat: (filePath) => fileSystem.stat(resolveTranscript(filePath)),+ open: (filePath, options) => fileSystem.open(resolveTranscript(filePath), options),+ });++ const input = {+ claudeHomePath,+ codexHomePath,+ providerInstances: {+ [ProviderInstanceId.make("claude-work")]: {+ driver: ProviderDriverKind.make("claudeAgent"),+ config: { homePath: recentHome },+ },+ },+ };+ const threads = yield* Effect.gen(function* () {+ const scanner = yield* AgentSessionScanner.AgentSessionScanner;+ const scan = yield* scanner.scan;+ expect(scan.truncated).toBe(true);+ return yield* scanner.recentThreads(recentWorkspace).pipe(Stream.runCollect);+ }).pipe(+ Effect.provide(makeScannerTestLayer(input)),+ Effect.provideService(FileSystem.FileSystem, simulatedFileSystem),+ );++ expect(+ threads.flatMap((outcome) =>+ outcome._tag === "Importable" ? [outcome.thread.providerSessionId] : [],+ ),+ ).toEqual(["recent-session"]);+ }),+ );+ });+});++describe("parseAgentSessionTranscript", () => {+ it.each([false, true])(+ "handles the exact record limit and an interior blank overflow=%s",+ (overflow) => {+ const thread = AgentSessionScanner.parseAgentSessionTranscript({+ contents: makeRecordLimitTranscript("/project", overflow),+ source: "codex",+ providerInstanceId: ProviderInstanceId.make("codex"),+ fallbackSessionId: "unused",+ lastActiveAtMs: Date.parse("2026-08-24T12:00:00.000Z"),+ });+ if (overflow) expect(thread).toBeNull();+ else expect(thread?.messages.map((message) => message.text)).toEqual(["First prompt"]);+ },+ );++ it("keeps Claude text and titles while dropping malformed and tool records", () => {+ const thread = AgentSessionScanner.parseAgentSessionTranscript({+ contents: [+ "not valid json",+ JSON.stringify({ type: "ai-title", aiTitle: "Fix authentication" }),+ JSON.stringify({+ type: "user",+ sessionId: "claude-session",+ isMeta: true,+ message: { role: "user", content: "Injected skill instructions" },+ }),+ JSON.stringify({+ type: "user",+ sessionId: "claude-session",+ isCompactSummary: true,+ message: { role: "user", content: "Injected compaction summary" },+ }),+ JSON.stringify({+ type: "user",+ sessionId: "claude-session",+ timestamp: "2026-08-24T10:00:00.000Z",+ message: { role: "user", content: [{ type: "text", text: "Fix authentication" }] },+ }),+ JSON.stringify({+ type: "user",+ sessionId: "claude-session",+ message: { role: "user", content: [{ type: "tool_result", text: "hidden" }] },+ }),+ JSON.stringify({+ type: "assistant",+ sessionId: "claude-session",+ message: {+ role: "assistant",+ model: "claude-sonnet-5",+ content: [{ type: "text", text: "Updated the login flow" }],+ },+ }),+ JSON.stringify({+ type: "assistant",+ sessionId: "claude-session",+ message: {+ role: "assistant",+ model: "<synthetic>",+ content: [{ type: "text", text: "The provider request failed" }],+ },+ }),+ ].join("\n"),+ source: "claudeAgent",+ providerInstanceId: ProviderInstanceId.make("claudeAgent"),+ fallbackSessionId: "fallback",+ lastActiveAtMs: Date.parse("2026-08-24T12:00:00.000Z"),+ });++ expect(thread).toMatchObject({+ providerSessionId: "claude-session",+ title: "Fix authentication",+ model: "claude-sonnet-5",+ messages: [+ { role: "user", text: "Fix authentication" },+ { role: "assistant", text: "Updated the login flow" },+ { role: "assistant", text: "The provider request failed" },+ ],+ });+ });++ it("drops injected Codex instructions while keeping the visible user event", () => {+ const thread = AgentSessionScanner.parseAgentSessionTranscript({+ contents: [+ JSON.stringify({ type: "session_meta", payload: { id: "codex-session" } }),+ JSON.stringify({+ type: "response_item",+ payload: {+ type: "message",+ role: "user",+ internal_chat_message_metadata_passthrough: { turn_id: "turn-1" },+ content: [+ {+ type: "input_text",+ text: "<user_instructions>\nInternal setup instructions\n</user_instructions>",+ },+ ],+ },+ }),+ JSON.stringify({+ type: "event_msg",+ payload: { type: "user_message", message: "Fix the actual bug" },+ }),+ JSON.stringify({+ type: "response_item",+ payload: {+ type: "message",+ role: "user",+ internal_chat_message_metadata_passthrough: { turn_id: "turn-1" },+ content: [{ type: "input_text", text: "Fix the actual bug" }],+ },+ }),+ JSON.stringify({+ type: "response_item",+ payload: {+ type: "message",+ role: "assistant",+ content: [{ type: "output_text", text: "Fixed" }],+ },+ }),+ ].join("\n"),+ source: "codex",+ providerInstanceId: ProviderInstanceId.make("codex"),+ fallbackSessionId: "fallback",+ lastActiveAtMs: Date.parse("2026-08-24T12:00:00.000Z"),+ });++ expect(thread?.messages.map((message) => message.text)).toEqual([+ "Fix the actual bug",+ "Fixed",+ ]);+ });++ it("keeps the canonical first prompt after long Codex transcripts are capped", () => {+ const canonicalPrompt = "\n Keep the canonical prompt \n";+ const canonicalTimestamp = "2026-08-24T10:01:00.000Z";+ const laterAssistantMessages = Array.from({ length: 200 }, (_, index) =>+ encodeTranscriptRecord({+ type: "response_item",+ timestamp: `2026-08-24T11:${String(index % 60).padStart(2, "0")}:00.000Z`,+ payload: {+ type: "message",+ role: "assistant",+ content: [{ type: "output_text", text: `Assistant message ${index}` }],+ },+ }),+ );+ const thread = AgentSessionScanner.parseAgentSessionTranscript({+ contents: [+ encodeTranscriptRecord({ type: "session_meta", payload: { id: "codex-session" } }),+ encodeTranscriptRecord({+ type: "response_item",+ timestamp: "2026-08-24T10:00:00.000Z",+ payload: {+ type: "message",+ role: "user",+ content: [{ type: "input_text", text: "Keep the canonical prompt" }],+ },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ timestamp: canonicalTimestamp,+ payload: { type: "user_message", message: canonicalPrompt },+ }),+ ...laterAssistantMessages,+ ].join("\n"),+ source: "codex",+ providerInstanceId: ProviderInstanceId.make("codex"),+ fallbackSessionId: "fallback",+ lastActiveAtMs: Date.parse("2026-08-24T12:00:00.000Z"),+ });++ expect(thread?.messages).toHaveLength(200);+ expect(thread?.messages[0]).toMatchObject({+ role: "user",+ text: canonicalPrompt,+ createdAt: canonicalTimestamp,+ });+ });++ it("restores the canonical first prompt when a later user message remains", () => {+ const canonicalPrompt = "\n Keep the canonical prompt \n";+ const canonicalTimestamp = "2026-08-24T10:01:00.000Z";+ const assistantMessages = Array.from({ length: 198 }, (_, index) =>+ encodeTranscriptRecord({+ type: "response_item",+ timestamp: `2026-08-24T11:${String(index % 60).padStart(2, "0")}:00.000Z`,+ payload: {+ type: "message",+ role: "assistant",+ content: [{ type: "output_text", text: `Assistant message ${index}` }],+ },+ }),+ );+ const thread = AgentSessionScanner.parseAgentSessionTranscript({+ contents: [+ encodeTranscriptRecord({ type: "session_meta", payload: { id: "codex-session" } }),+ encodeTranscriptRecord({+ type: "response_item",+ timestamp: "2026-08-24T10:00:00.000Z",+ payload: {+ type: "message",+ role: "user",+ internal_chat_message_metadata_passthrough: { turn_id: "turn-1" },+ content: [{ type: "input_text", text: "Keep the canonical prompt" }],+ },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ timestamp: canonicalTimestamp,+ payload: { type: "user_message", message: canonicalPrompt },+ }),+ ...assistantMessages,+ encodeTranscriptRecord({+ type: "event_msg",+ timestamp: "2026-08-24T11:58:30.000Z",+ payload: { type: "user_message", message: "Keep this later prompt" },+ }),+ encodeTranscriptRecord({+ type: "response_item",+ timestamp: "2026-08-24T11:59:00.000Z",+ payload: {+ type: "message",+ role: "assistant",+ content: [{ type: "output_text", text: "Keep this latest response" }],+ },+ }),+ ].join("\n"),+ source: "codex",+ providerInstanceId: ProviderInstanceId.make("codex"),+ fallbackSessionId: "fallback",+ lastActiveAtMs: Date.parse("2026-08-24T12:00:00.000Z"),+ });++ expect(thread?.messages).toHaveLength(200);+ expect(thread?.messages[0]).toMatchObject({+ role: "user",+ text: canonicalPrompt,+ createdAt: canonicalTimestamp,+ });+ expect(+ thread?.messages.filter((message) => message.text.trim() === canonicalPrompt.trim()),+ ).toHaveLength(1);+ expect(thread?.messages.some((message) => message.text === "Keep this later prompt")).toBe(+ true,+ );+ expect(thread?.messages.at(-1)?.text).toBe("Keep this latest response");+ });++ it("keeps mixed-format response users when turn IDs repeat after an assistant", () => {+ const thread = AgentSessionScanner.parseAgentSessionTranscript({+ contents: [+ encodeTranscriptRecord({ type: "session_meta", payload: { id: "codex-session" } }),+ encodeTranscriptRecord({+ type: "response_item",+ payload: {+ type: "message",+ role: "user",+ internal_chat_message_metadata_passthrough: { turn_id: "turn-older" },+ content: [{ type: "input_text", text: "Keep this older prompt" }],+ },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: { type: "user_message", message: "Keep this newer prompt" },+ }),+ encodeTranscriptRecord({+ type: "response_item",+ payload: {+ type: "message",+ role: "user",+ internal_chat_message_metadata_passthrough: { turn_id: "turn-newer" },+ content: [{ type: "input_text", text: "Keep this newer prompt" }],+ },+ }),+ encodeTranscriptRecord({+ type: "response_item",+ payload: {+ type: "message",+ role: "assistant",+ content: [{ type: "output_text", text: "Ask again when needed" }],+ },+ }),+ encodeTranscriptRecord({+ type: "response_item",+ payload: {+ type: "message",+ role: "user",+ internal_chat_message_metadata_passthrough: { turn_id: "turn-newer" },+ content: [{ type: "input_text", text: "Keep this newer prompt" }],+ },+ }),+ ].join("\n"),+ source: "codex",+ providerInstanceId: ProviderInstanceId.make("codex"),+ fallbackSessionId: "fallback",+ lastActiveAtMs: Date.parse("2026-08-24T12:00:00.000Z"),+ });++ expect(thread?.messages.map((message) => message.text)).toEqual([+ "Keep this older prompt",+ "Keep this newer prompt",+ "Ask again when needed",+ "Keep this newer prompt",+ ]);+ });++ it("preserves response user text when Codex turn metadata is ambiguous", () => {+ const thread = AgentSessionScanner.parseAgentSessionTranscript({+ contents: [+ encodeTranscriptRecord({ type: "session_meta", payload: { id: "codex-session" } }),+ encodeTranscriptRecord({+ type: "response_item",+ payload: {+ type: "message",+ role: "user",+ internal_chat_message_metadata_passthrough: ["unexpected"],+ content: [{ type: "input_text", text: "Keep this legacy prompt" }],+ },+ }),+ encodeTranscriptRecord({+ type: "response_item",+ payload: {+ type: "message",+ role: "user",+ internal_chat_message_metadata_passthrough: { turn_id: " " },+ content: [{ type: "input_text", text: "Keep this prompt with a blank turn ID" }],+ },+ }),+ ].join("\n"),+ source: "codex",+ providerInstanceId: ProviderInstanceId.make("codex"),+ fallbackSessionId: "fallback",+ lastActiveAtMs: Date.parse("2026-08-24T12:00:00.000Z"),+ });++ expect(thread?.messages.map((message) => message.text)).toEqual([+ "Keep this legacy prompt",+ "Keep this prompt with a blank turn ID",+ ]);+ });++ it("uses the first valid Codex session ID when a fork copies ancestor metadata", () => {+ const thread = AgentSessionScanner.parseAgentSessionTranscript({+ contents: [+ encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: "fork-session", forked_from_id: "parent-session" },+ }),+ encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: "parent-session" },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: { type: "user_message", message: "Continue in the fork" },+ }),+ ].join("\n"),+ source: "codex",+ providerInstanceId: ProviderInstanceId.make("codex"),+ fallbackSessionId: "fallback",+ lastActiveAtMs: Date.parse("2026-08-24T12:00:00.000Z"),+ });++ expect(thread?.providerSessionId).toBe("fork-session");+ });++ it("skips Codex transcripts without a resumable session ID", () => {+ const thread = AgentSessionScanner.parseAgentSessionTranscript({+ contents: encodeTranscriptRecord({+ type: "event_msg",+ payload: { type: "user_message", message: "This transcript has no session metadata" },+ }),+ source: "codex",+ providerInstanceId: ProviderInstanceId.make("codex"),+ fallbackSessionId: "rollout-2026-08-24T12-00-00-not-a-session-id",+ lastActiveAtMs: Date.parse("2026-08-24T12:00:00.000Z"),+ });++ expect(thread).toBeNull();+ });++ it("uses the canonical Codex event when its turn has generated response context", () => {+ const thread = AgentSessionScanner.parseAgentSessionTranscript({+ contents: [+ encodeTranscriptRecord({ type: "session_meta", payload: { id: "codex-session" } }),+ encodeTranscriptRecord({+ type: "response_item",+ payload: {+ type: "message",+ role: "user",+ internal_chat_message_metadata_passthrough: { turn_id: "turn-1" },+ content: [+ {+ type: "input_text",+ text: "<environment_context>\n<cwd>/tmp/project</cwd>\n<shell>zsh</shell>\n</environment_context>",+ },+ ],+ },+ }),+ encodeTranscriptRecord({+ type: "response_item",+ payload: {+ type: "message",+ role: "user",+ internal_chat_message_metadata_passthrough: { turn_id: "turn-1" },+ content: [+ {+ type: "input_text",+ text: "# AGENTS.md instructions for /tmp/project\n\n<INSTRUCTIONS>\nPrivate project rules\n</INSTRUCTIONS>",+ },+ ],+ },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: {+ type: "user_message",+ message: "Do something here so it looks like a real project.",+ },+ }),+ encodeTranscriptRecord({+ type: "response_item",+ payload: {+ type: "message",+ role: "user",+ internal_chat_message_metadata_passthrough: { turn_id: "turn-1" },+ content: [+ {+ type: "input_text",+ text: "Do something here so it looks like a real project.",+ },+ ],+ },+ }),+ encodeTranscriptRecord({+ type: "response_item",+ payload: {+ type: "message",+ role: "assistant",+ content: [{ type: "output_text", text: "Created the project." }],+ },+ }),+ ].join("\n"),+ source: "codex",+ providerInstanceId: ProviderInstanceId.make("codex"),+ fallbackSessionId: "fallback",+ lastActiveAtMs: Date.parse("2026-08-25T08:00:00.000Z"),+ });++ expect(thread?.title).toBe("Do something here so it looks like a real project.");+ expect(thread?.messages.map((message) => message.text)).toEqual([+ "Do something here so it looks like a real project.",+ "Created the project.",+ ]);+ });++ it("preserves context markup in response-only Codex messages", () => {+ const context = "<environment_context>\n<cwd>/tmp/project</cwd>\n</environment_context>";+ const thread = AgentSessionScanner.parseAgentSessionTranscript({+ contents: [+ encodeTranscriptRecord({ type: "session_meta", payload: { id: "codex-session" } }),+ encodeTranscriptRecord({+ type: "response_item",+ payload: {+ type: "message",+ role: "user",+ content: [+ {+ type: "input_text",+ text: context,+ },+ ],+ },+ }),+ encodeTranscriptRecord({+ type: "response_item",+ payload: {+ type: "message",+ role: "user",+ content: [{ type: "input_text", text: "Initialize Git and add a README." }],+ },+ }),+ ].join("\n"),+ source: "codex",+ providerInstanceId: ProviderInstanceId.make("codex"),+ fallbackSessionId: "fallback",+ lastActiveAtMs: Date.parse("2026-08-25T08:00:00.000Z"),+ });++ expect(thread?.title).toBe("<environment_context>");+ expect(thread?.messages.map((message) => message.text)).toEqual([+ context,+ "Initialize Git and add a README.",+ ]);+ });++ it("preserves a canonical Codex event that starts with context markup", () => {+ const prompt =+ "<environment_context>\n<cwd>/tmp/project</cwd>\n</environment_context>\n\nCreate a useful project.";+ const thread = AgentSessionScanner.parseAgentSessionTranscript({+ contents: [+ encodeTranscriptRecord({ type: "session_meta", payload: { id: "codex-session" } }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: {+ type: "user_message",+ message: prompt,+ },+ }),+ ].join("\n"),+ source: "codex",+ providerInstanceId: ProviderInstanceId.make("codex"),+ fallbackSessionId: "fallback",+ lastActiveAtMs: Date.parse("2026-08-25T08:00:00.000Z"),+ });++ expect(thread?.title).toBe("<environment_context>");+ expect(thread?.messages.map((message) => message.text)).toEqual([prompt]);+ });++ it("preserves a Codex request heading in a canonical event", () => {+ const prompt = "\n ## My request for Codex:\n\nFix the visible bug";+ const thread = AgentSessionScanner.parseAgentSessionTranscript({+ contents: [+ encodeTranscriptRecord({ type: "session_meta", payload: { id: "codex-session" } }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: {+ type: "user_message",+ message: prompt,+ },+ }),+ ].join("\n"),+ source: "codex",+ providerInstanceId: ProviderInstanceId.make("codex"),+ fallbackSessionId: "fallback",+ lastActiveAtMs: Date.parse("2026-08-25T08:00:00.000Z"),+ });++ expect(thread?.title).toBe("## My request for Codex:");+ expect(thread?.messages.map((message) => message.text)).toEqual([prompt]);+ });++ it("keeps context markup quoted inside visible Codex user text", () => {+ const quoted =+ "Do not remove this example:\n<environment_context>\n<cwd>/tmp/example</cwd>\n</environment_context>";+ const thread = AgentSessionScanner.parseAgentSessionTranscript({+ contents: [+ encodeTranscriptRecord({ type: "session_meta", payload: { id: "codex-session" } }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: { type: "user_message", message: quoted },+ }),+ ].join("\n"),+ source: "codex",+ providerInstanceId: ProviderInstanceId.make("codex"),+ fallbackSessionId: "fallback",+ lastActiveAtMs: Date.parse("2026-08-25T08:00:00.000Z"),+ });++ expect(thread?.messages.map((message) => message.text)).toEqual([quoted]);+ });++ it("skips sessions without a visible user message", () => {+ const thread = AgentSessionScanner.parseAgentSessionTranscript({+ contents: JSON.stringify({+ type: "assistant",+ message: { role: "assistant", content: "Done" },+ }),+ source: "claudeAgent",+ providerInstanceId: ProviderInstanceId.make("claudeAgent"),+ fallbackSessionId: "claude-session",+ lastActiveAtMs: Date.parse("2026-08-24T12:00:00.000Z"),+ });++ expect(thread).toBeNull();+ });++ it("keeps the first prompt when later assistant output exceeds the message limit", () => {+ const transcript = [+ encodeTranscriptRecord({+ type: "user",+ sessionId: "claude-session",+ message: { role: "user", content: "Keep this prompt" },+ }),+ ...Array.from({ length: 250 }, (_, index) =>+ encodeTranscriptRecord({+ type: "assistant",+ message: { role: "assistant", content: `Assistant update ${index}` },+ }),+ ),+ ].join("\n");++ const thread = AgentSessionScanner.parseAgentSessionTranscript({+ contents: transcript,+ source: "claudeAgent",+ providerInstanceId: ProviderInstanceId.make("claudeAgent"),+ fallbackSessionId: "fallback",+ lastActiveAtMs: Date.parse("2026-08-24T12:00:00.000Z"),+ });++ expect(thread?.messages).toHaveLength(200);+ expect(thread?.messages[0]?.text).toBe("Keep this prompt");+ expect(thread?.messages.at(-1)?.text).toBe("Assistant update 249");+ });+});diff --git a/apps/server/src/project/AgentSessionScanner.ts b/apps/server/src/project/AgentSessionScanner.tsnew file mode 100644index 00000000000..bc6d093d42a--- /dev/null+++ b/apps/server/src/project/AgentSessionScanner.ts@@ -0,0 +1,1313 @@+/**+ * AgentSessionScanner - discovery of projects a user already works on.+ *+ * Claude Code and Codex both keep a per-session transcript on disk, and each+ * transcript records the directory the session ran in. Reading those `cwd`+ * values gives us the set of directories worth offering as projects during+ * onboarding, without asking the user to browse the filesystem.+ *+ * The scan is read-only and best-effort: an unreadable home, a malformed+ * transcript, or a directory that has since been deleted is skipped rather+ * than failing the scan. Project creation stays with the client, which+ * dispatches `project.create` for whichever candidates the user picks.+ *+ * @module project/AgentSessionScanner+ */+import * as NodeOS from "node:os";++import {+ AgentSessionScanError,+ ClaudeSettings,+ CodexSettings,+ ProviderDriverKind,+ ProviderInstanceId,+ resolveProviderInstanceEnabled,+ type AgentSessionImportSource,+ type AgentSessionProjectCandidate,+ type AgentSessionScanResult,+ type ProviderInstanceConfig,+} from "@t3tools/contracts";+import * as Context from "effect/Context";+import * as DateTime from "effect/DateTime";+import * as Effect from "effect/Effect";+import * as FileSystem from "effect/FileSystem";+import * as Layer from "effect/Layer";+import * as Option from "effect/Option";+import * as Path from "effect/Path";+import * as Schema from "effect/Schema";+import * as Stream from "effect/Stream";++import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hostProcess";+import { normalizeProjectPathForComparison } from "@t3tools/shared/path";++import * as ServerConfig from "../config.ts";+import * as ProjectionSnapshotQuery from "../orchestration/Services/ProjectionSnapshotQuery.ts";+import { resolveCodexHomeLayout } from "../provider/Drivers/CodexHomeLayout.ts";+import { expandHomePath } from "../pathExpansion.ts";+import * as ServerSettings from "../serverSettings.ts";++/** Chunk size for full transcript reads. */+const TRANSCRIPT_PREFIX_BYTES = 32 * 1024;+/** Small reads avoid wasting the metadata budget on long Codex instruction headers. */+const METADATA_READ_BYTES = 8 * 1024;+/** Prevent malformed transcripts from turning project discovery into a full file scan. */+const MAX_TRANSCRIPT_SCAN_BYTES = 1024 * 1024;++/**+ * Upper bound on transcripts inspected (first line read) per source.+ * Newest-first ordering means the cap drops only stale sessions when a home+ * directory is unusually large.+ */+const MAX_TRANSCRIPTS_PER_SOURCE = 5000;++/**+ * Upper bound on discovery filesystem operations per source. Newest-first+ * ordering needs mtimes before the read cap can be applied, so directory reads+ * and candidate stats share a larger budget. Once it runs out the scan stops.+ */+const MAX_DISCOVERY_OPERATIONS_PER_SOURCE = MAX_TRANSCRIPTS_PER_SOURCE * 4;+const MAX_METADATA_BYTES_PER_SOURCE = 64 * 1024 * 1024;+const MAX_METADATA_OPERATIONS_PER_SOURCE = MAX_TRANSCRIPTS_PER_SOURCE * 4;+const MAX_METADATA_RECORDS_PER_SOURCE = 100_000;+const MAX_METADATA_RECORDS_PER_TRANSCRIPT = 1_000;+const RECENT_THREAD_WINDOW_MS = 30 * 24 * 60 * 60 * 1000;+const MAX_IMPORTED_TRANSCRIPT_BYTES = 16 * 1024 * 1024;+const MAX_IMPORTED_MESSAGES = 200;+const MAX_IMPORT_BYTES = 64 * 1024 * 1024;+const MAX_IMPORT_TRANSCRIPTS = 100;+const MAX_IMPORT_RECORDS = 100_000;++const TranscriptContentBlock = Schema.Struct({+ type: Schema.optional(Schema.String),+ text: Schema.optional(Schema.String),+});++const TranscriptMessage = Schema.Struct({+ role: Schema.optional(Schema.String),+ content: Schema.optional(Schema.Union([Schema.String, Schema.Array(TranscriptContentBlock)])),+ model: Schema.optional(Schema.String),+});++const CodexTurnMetadata = Schema.Struct({+ turn_id: Schema.optional(Schema.Union([Schema.String, Schema.Null])),+});++const TranscriptRecord = Schema.Struct({+ type: Schema.optional(Schema.String),+ timestamp: Schema.optional(Schema.String),+ sessionId: Schema.optional(Schema.String),+ aiTitle: Schema.optional(Schema.String),+ isSidechain: Schema.optional(Schema.Boolean),+ isMeta: Schema.optional(Schema.Boolean),+ isCompactSummary: Schema.optional(Schema.Boolean),+ message: Schema.optional(TranscriptMessage),+ payload: Schema.optional(+ Schema.Struct({+ id: Schema.optional(Schema.String),+ session_id: Schema.optional(Schema.String),+ type: Schema.optional(Schema.String),+ role: Schema.optional(Schema.String),+ message: Schema.optional(Schema.String),+ model: Schema.optional(Schema.String),+ content: Schema.optional(Schema.Array(TranscriptContentBlock)),+ internal_chat_message_metadata_passthrough: Schema.optional(Schema.Unknown),+ }),+ ),+});++const decodeClaudeSettings = Schema.decodeUnknownOption(ClaudeSettings);+const decodeCodexSettings = Schema.decodeUnknownOption(CodexSettings);+const decodeTranscriptRecord = Schema.decodeUnknownOption(Schema.fromJsonString(TranscriptRecord));+const decodeCodexTurnMetadata = Schema.decodeUnknownOption(CodexTurnMetadata);++export interface AgentSessionThreadMessage {+ readonly role: "user" | "assistant";+ readonly text: string;+ readonly createdAt: string;+}++export interface AgentSessionThread {+ readonly source: AgentSessionSource;+ readonly providerInstanceId: ProviderInstanceId;+ readonly providerSessionId: string;+ readonly title: string;+ readonly model: string | null;+ readonly createdAt: string;+ readonly updatedAt: string;+ readonly messages: ReadonlyArray<AgentSessionThreadMessage>;+}++export type AgentSessionRecentThread =+ | {+ readonly _tag: "Importable";+ readonly thread: AgentSessionThread;+ readonly source: AgentSessionImportSource;+ }+ | { readonly _tag: "AlreadyImported"; readonly source: AgentSessionImportSource }+ | { readonly _tag: "Duplicate"; readonly source: AgentSessionImportSource }+ | { readonly _tag: "Skipped" };++/** Service tag for agent session discovery. */+export class AgentSessionScanner extends Context.Service<+ AgentSessionScanner,+ {+ /**+ * Discover every directory the configured Claude and Codex homes have run+ * a session in. Candidates are returned newest-first; the client decides+ * which ones to import and how far back to look. Fails with the contract+ * error directly — there is no server-local context worth wrapping.+ */+ readonly scan: Effect.Effect<AgentSessionScanResult, AgentSessionScanError>;+ readonly recentThreads: (+ workspaceRoot: string,+ completedSources?: ReadonlyArray<AgentSessionImportSource>,+ ) => Stream.Stream<AgentSessionRecentThread, AgentSessionScanError>;+ }+>()("t3/project/AgentSessionScanner") {}++type AgentSessionSource = AgentSessionProjectCandidate["sources"][number];++/** A single directory's worth of evidence from one source. */+interface RawCandidate {+ readonly cwd: string;+ readonly source: AgentSessionSource;+ readonly providerInstanceId: ProviderInstanceId;+ readonly threadCount: number;+ readonly lastActiveAtMs: number | null;+ readonly transcripts: ReadonlyArray<{+ readonly filePath: string;+ readonly mtimeMs: number | null;+ }>;+}++interface TranscriptCandidate {+ readonly filePath: string;+ readonly mtimeMs: number;+ readonly providerInstanceId: ProviderInstanceId;+ readonly size: number;+}++interface MetadataReadBudget {+ bytesRemaining: number;+ operationsRemaining: number;+ recordsRemaining: number;+ truncated: boolean;+}++function selectMetadataTranscripts(transcripts: ReadonlyArray<TranscriptCandidate>) {+ const selected: Array<TranscriptCandidate> = [];+ let pending = Array.from(+ Map.groupBy(transcripts, (transcript) => transcript.providerInstanceId).values(),+ (entries) => entries.values(),+ );+ while (pending.length > 0 && selected.length < MAX_TRANSCRIPTS_PER_SOURCE) {+ const nextRound: typeof pending = [];+ for (const iterator of pending) {+ if (selected.length === MAX_TRANSCRIPTS_PER_SOURCE) break;+ const next = iterator.next();+ if (next.done) continue;+ selected.push(next.value);+ nextRound.push(iterator);+ }+ pending = nextRound;+ }+ return selected;+}++function splitTranscriptRecords(contents: string, limit: number): string[] {+ const records = contents.endsWith("\n") ? contents.slice(0, -1) : contents;+ return records.split("\n", limit);+}++function extractText(+ content: string | ReadonlyArray<typeof TranscriptContentBlock.Type> | undefined,+): string {+ if (typeof content === "string") return content.trim();+ if (content === undefined) return "";+ return content+ .filter(+ (block) =>+ block.type === "text" || block.type === "input_text" || block.type === "output_text",+ )+ .map((block) => block.text?.trim() ?? "")+ .filter((text) => text.length > 0)+ .join("\n");+}++function normalizeTimestamp(value: string | undefined, fallback: string): string {+ if (value === undefined) return fallback;+ const parsed = DateTime.make(value);+ return Option.isSome(parsed) ? DateTime.formatIso(parsed.value) : fallback;+}++function codexTurnId(metadata: unknown): string | null {+ const decoded = decodeCodexTurnMetadata(metadata);+ if (+ Option.isNone(decoded) ||+ typeof decoded.value.turn_id !== "string" ||+ decoded.value.turn_id.trim().length === 0+ ) {+ return null;+ }+ return decoded.value.turn_id;+}++/** Keep visible user and assistant text while ignoring tools, reasoning, and malformed records. */+export function parseAgentSessionTranscript(+ input: {+ readonly contents: string;+ readonly source: AgentSessionSource;+ readonly providerInstanceId: ProviderInstanceId;+ readonly fallbackSessionId: string;+ readonly lastActiveAtMs: number;+ },+ lines = splitTranscriptRecords(input.contents, MAX_IMPORT_RECORDS + 1),+): AgentSessionThread | null {+ if (lines.length > MAX_IMPORT_RECORDS) return null;+ const fallbackTimestamp = DateTime.formatIso(DateTime.makeUnsafe(input.lastActiveAtMs));+ // Claude filenames are session IDs. Codex rollout filenames include extra+ // timestamp text, so only transcript metadata can provide a resumable ID.+ let providerSessionId = input.source === "codex" ? "" : input.fallbackSessionId;+ let title: string | null = null;+ let model: string | null = null;+ let hasCodexSessionId = false;+ const messages: Array<AgentSessionThreadMessage & { readonly codexResponseUser: boolean }> = [];+ let firstUserMessage:+ | (AgentSessionThreadMessage & { readonly codexResponseUser: boolean })+ | undefined;+ function* decodedRecords() {+ for (const line of lines) {+ const decoded = decodeTranscriptRecord(line);+ if (Option.isSome(decoded)) yield decoded.value;+ }+ }++ // A Codex response item can include generated setup text beside the real+ // prompt. Suppress response-user records only when the shared turn ID and a+ // verbatim event copy prove which prompt the user submitted.+ const canonicalCodexResponseUserIndices = new Set<number>();+ let canonicalUserTextsInTurn = new Set<string>();+ let responseUsersInTurn: Array<{+ readonly index: number;+ readonly turnId: string;+ readonly text: string;+ }> = [];+ const finishCodexTurn = () => {+ const canonicalTurnIds = new Set(+ responseUsersInTurn.flatMap((responseUser) =>+ canonicalUserTextsInTurn.has(responseUser.text) ? [responseUser.turnId] : [],+ ),+ );+ for (const responseUser of responseUsersInTurn) {+ if (canonicalTurnIds.has(responseUser.turnId)) {+ canonicalCodexResponseUserIndices.add(responseUser.index);+ }+ }+ canonicalUserTextsInTurn = new Set();+ responseUsersInTurn = [];+ };+ if (input.source === "codex") {+ let recordIndex = -1;+ for (const record of decodedRecords()) {+ recordIndex += 1;+ if (+ record.type === "response_item" &&+ record.payload?.type === "message" &&+ record.payload.role === "assistant"+ ) {+ finishCodexTurn();+ continue;+ }+ if (record.type === "event_msg" && record.payload?.type === "user_message") {+ const text = record.payload.message?.trim() ?? "";+ if (text.length > 0) canonicalUserTextsInTurn.add(text);+ continue;+ }+ if (+ record.type === "response_item" &&+ record.payload?.type === "message" &&+ record.payload.role === "user"+ ) {+ const turnId = codexTurnId(record.payload.internal_chat_message_metadata_passthrough);+ const text = extractText(record.payload.content);+ if (turnId !== null && text.length > 0) {+ responseUsersInTurn.push({ index: recordIndex, turnId, text });+ }+ }+ }+ finishCodexTurn();+ }++ const retainMessage = (+ message: AgentSessionThreadMessage & { readonly codexResponseUser: boolean },+ ) => {+ if (firstUserMessage === undefined && message.role === "user") {+ firstUserMessage = message;+ }+ messages.push(message);+ if (messages.length > MAX_IMPORTED_MESSAGES) messages.shift();+ };++ const hasMatchingCodexEventInTurn = (text: string) => {+ const comparisonText = text.trim();+ for (let index = messages.length - 1; index >= 0; index--) {+ const message = messages[index];+ if (message?.role === "assistant") return false;+ if (+ message?.role === "user" &&+ !message.codexResponseUser &&+ message.text.trim() === comparisonText+ ) {+ return true;+ }+ }+ return false;+ };++ let recordIndex = -1;+ for (const record of decodedRecords()) {+ recordIndex += 1;+ if (input.source === "claudeAgent") {+ if (+ record.isSidechain === true ||+ record.isMeta === true ||+ record.isCompactSummary === true+ ) {+ continue;+ }+ if (record.sessionId?.trim()) providerSessionId = record.sessionId.trim();+ if (record.aiTitle?.trim()) title = record.aiTitle.trim();+ const messageModel = record.message?.model?.trim();+ // Claude uses this sentinel for local error responses. It is not a+ // model ID that can be selected when the imported session resumes.+ if (messageModel && messageModel !== "<synthetic>") model = messageModel;+ if (record.type !== "user" && record.type !== "assistant") {+ continue;+ }++ const text = extractText(record.message?.content);+ if (text.length === 0) continue;+ retainMessage({+ role: record.type,+ text,+ createdAt: normalizeTimestamp(record.timestamp, fallbackTimestamp),+ codexResponseUser: false,+ });+ continue;+ }++ if (record.type === "session_meta") {+ const sessionId = record.payload?.id?.trim() || record.payload?.session_id?.trim();+ if (!hasCodexSessionId && sessionId) {+ providerSessionId = sessionId;+ hasCodexSessionId = true;+ }+ continue;+ }+ if (record.type === "turn_context" && record.payload?.model?.trim()) {+ model = record.payload.model.trim();+ continue;+ }+ if (record.type === "event_msg" && record.payload?.type === "user_message") {+ const text = record.payload.message ?? "";+ if (text.trim().length === 0) continue;+ // Codex can write the same prompt as both a response item and an event.+ // Remove only the matching response copy so mixed-format logs keep every+ // distinct user message.+ for (let index = messages.length - 1; index >= 0; index--) {+ const message = messages[index];+ if (message?.role === "assistant") break;+ if (message?.codexResponseUser === true && message.text.trim() === text.trim()) {+ if (firstUserMessage === message) firstUserMessage = undefined;+ messages.splice(index, 1);+ break;+ }+ }+ retainMessage({+ role: "user",+ text,+ createdAt: normalizeTimestamp(record.timestamp, fallbackTimestamp),+ codexResponseUser: false,+ });+ continue;+ }+ if (+ record.type !== "response_item" ||+ record.payload?.type !== "message" ||+ (record.payload.role !== "user" && record.payload.role !== "assistant")+ ) {+ continue;+ }++ const extractedText = extractText(record.payload.content);+ if (extractedText.length === 0) continue;+ if (record.payload.role === "user" && canonicalCodexResponseUserIndices.has(recordIndex)) {+ continue;+ }+ if (record.payload.role === "user" && hasMatchingCodexEventInTurn(extractedText)) {+ continue;+ }+ retainMessage({+ role: record.payload.role,+ text: extractedText,+ createdAt: normalizeTimestamp(record.timestamp, fallbackTimestamp),+ codexResponseUser: record.payload.role === "user",+ });+ }++ const visibleMessages = messages.map(+ ({ codexResponseUser: _codexResponseUser, ...message }) => message,+ );+ if (providerSessionId.trim().length === 0 || firstUserMessage === undefined) return null;+ const firstUserMessageRetained = messages.includes(firstUserMessage);+ const { codexResponseUser: _codexResponseUser, ...visibleFirstUserMessage } = firstUserMessage;+ const retainedMessages = firstUserMessageRetained+ ? visibleMessages+ : [visibleFirstUserMessage, ...visibleMessages.slice(-(MAX_IMPORTED_MESSAGES - 1))];+ const derivedTitle = visibleFirstUserMessage.text.trim().split("\n")[0]?.slice(0, 100).trim();++ return {+ source: input.source,+ providerInstanceId: input.providerInstanceId,+ providerSessionId,+ title: title ?? (derivedTitle && derivedTitle.length > 0 ? derivedTitle : "Imported thread"),+ model,+ createdAt: retainedMessages[0]?.createdAt ?? fallbackTimestamp,+ updatedAt: fallbackTimestamp,+ messages: retainedMessages,+ };+}++/**+ * T3 Code runs its own agent sessions inside disposable worktrees. Their+ * transcripts look exactly like user sessions, but re-importing the app's own+ * sandboxes as projects is never right. Matches this server's configured+ * worktrees directory plus the conventional `.t3/worktrees` layout, which+ * also catches sandboxes from other T3 homes on the same machine. Separators+ * are normalized (and, on Windows, case folded) so the prefix match holds+ * there too. Callers check both the recorded spelling and its realpath so a+ * symlink into the worktrees directory cannot bypass the filter.+ */+function normalizeForWorktreeMatch(value: string, caseFold: boolean): string {+ const normalized = `${value.replaceAll("\\", "/")}/`;+ return caseFold ? normalized.toLowerCase() : normalized;+}++function isT3ManagedWorktree(+ candidatePath: string,+ worktreesDir: string,+ caseFold: boolean,+): boolean {+ const normalized = normalizeForWorktreeMatch(candidatePath, caseFold);+ return (+ normalized.startsWith(normalizeForWorktreeMatch(worktreesDir, caseFold)) ||+ normalized.includes("/.t3/worktrees/")+ );+}++/** Extract `cwd` from a session-meta record, tolerating the shapes each CLI writes. */+function extractCwd(line: string): string | null {+ let parsed: unknown;+ try {+ parsed = JSON.parse(line);+ } catch {+ return null;+ }+ if (typeof parsed !== "object" || parsed === null) return null;++ const record = parsed as Record<string, unknown>;+ if (typeof record.cwd === "string" && record.cwd.trim().length > 0) {+ return record.cwd;+ }+ // Codex nests session metadata under `payload`.+ const payload = record.payload;+ if (typeof payload === "object" && payload !== null) {+ const nested = (payload as Record<string, unknown>).cwd;+ if (typeof nested === "string" && nested.trim().length > 0) {+ return nested;+ }+ }+ return null;+}++function transcriptIdentity(filePath: string, stats: FileSystem.File.Info) {+ return {+ filePath,+ size: Number(stats.size),+ mtimeMs: Option.match(stats.mtime, { onNone: () => null, onSome: (date) => date.getTime() }),+ device: stats.dev,+ inode: Option.getOrNull(stats.ino),+ birthtimeMs: Option.match(stats.birthtime, {+ onNone: () => null,+ onSome: (date) => date.getTime(),+ }),+ };+}++function sameTranscriptIdentity(+ left: ReturnType<typeof transcriptIdentity>,+ right: ReturnType<typeof transcriptIdentity>,+): boolean {+ return (+ left.filePath === right.filePath &&+ left.size === right.size &&+ left.mtimeMs === right.mtimeMs &&+ left.device === right.device &&+ left.inode === right.inode &&+ left.birthtimeMs === right.birthtimeMs+ );+}++export const make = Effect.gen(function* () {+ const fileSystem = yield* FileSystem.FileSystem;+ const path = yield* Path.Path;+ const serverConfig = yield* ServerConfig.ServerConfig;+ const serverSettings = yield* ServerSettings.ServerSettingsService;+ const projectionSnapshotQuery = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;+ const baseDir = path.resolve(serverConfig.baseDir);+ const worktreesDir = path.resolve(serverConfig.worktreesDir);+ // Windows filesystems are case-insensitive, so path prefix checks there+ // must case fold.+ const foldWorktreeCase = (yield* HostProcessPlatform) === "win32";+ const hostEnvironment = yield* HostProcessEnvironment;+ const excludedProjectRoots = new Set(+ [NodeOS.homedir(), NodeOS.tmpdir()].map((directory) =>+ normalizeProjectPathForComparison(path.resolve(directory)),+ ),+ );++ const isExcludedProjectPath = (candidatePath: string) =>+ excludedProjectRoots.has(normalizeProjectPathForComparison(candidatePath)) ||+ normalizeForWorktreeMatch(candidatePath, foldWorktreeCase).startsWith(+ normalizeForWorktreeMatch(baseDir, foldWorktreeCase),+ ) ||+ isT3ManagedWorktree(candidatePath, worktreesDir, foldWorktreeCase);++ const listDirectory = (directory: string) =>+ fileSystem.readDirectory(directory).pipe(Effect.orElseSucceed((): ReadonlyArray<string> => []));++ const statOption = (target: string) =>+ fileSystem.stat(target).pipe(Effect.map(Option.some), Effect.orElseSucceed(Option.none));++ /** Match directory aliases without assuming the host volume is case-insensitive. */+ const directoryIdentity = Effect.fn("AgentSessionScanner.directoryIdentity")(function* (+ target: string,+ knownStats?: FileSystem.File.Info,+ ) {+ const resolved = path.resolve(target);+ const stats = knownStats === undefined ? yield* statOption(resolved) : Option.some(knownStats);+ if (+ Option.isSome(stats) &&+ Option.isSome(stats.value.ino) &&+ Number.isSafeInteger(stats.value.ino.value) &&+ stats.value.ino.value > 0+ ) {+ return `inode:${stats.value.dev}:${stats.value.ino.value}`;+ }+ const realPath = yield* fileSystem+ .realPath(resolved)+ .pipe(Effect.orElseSucceed(() => resolved));+ return `path:${normalizeProjectPathForComparison(realPath)}`;+ });++ // A large history snapshot can precede session metadata. Read bounded+ // chunks until a complete record names its cwd or the safety budget ends.+ const readCwd = Effect.fn("AgentSessionScanner.readCwd")(function* (+ transcript: TranscriptCandidate,+ budget: MetadataReadBudget,+ ) {+ if (transcript.size === 0) return null;+ if (+ budget.bytesRemaining === 0 ||+ budget.operationsRemaining < 2 ||+ budget.recordsRemaining === 0+ ) {+ budget.truncated = true;+ return null;+ }+ budget.operationsRemaining -= 1;+ return yield* Effect.scoped(+ fileSystem.open(transcript.filePath, { flag: "r" }).pipe(+ Effect.flatMap((file) =>+ Effect.gen(function* () {+ const decoder = new TextDecoder();+ let remaining = "";+ let bytesRead = 0;+ let recordsRead = 0;+ const maxBytes = Math.min(MAX_TRANSCRIPT_SCAN_BYTES, transcript.size);+ const reserveRecord = () => {+ if (+ recordsRead === MAX_METADATA_RECORDS_PER_TRANSCRIPT ||+ budget.recordsRemaining === 0+ ) {+ budget.truncated = true;+ return false;+ }+ recordsRead += 1;+ budget.recordsRemaining -= 1;+ return true;+ };+ const readLastRecord = () => {+ const record = remaining + decoder.decode();+ return record.length === 0 || !reserveRecord() ? null : extractCwd(record.trim());+ };++ while (bytesRead < maxBytes) {+ if (budget.bytesRemaining === 0 || budget.operationsRemaining === 0) {+ budget.truncated = true;+ return null;+ }+ const readSize = Math.min(+ METADATA_READ_BYTES,+ maxBytes - bytesRead,+ budget.bytesRemaining,+ );+ budget.operationsRemaining -= 1;+ budget.bytesRemaining -= readSize;+ const next = yield* file.readAlloc(readSize);+ if (Option.isNone(next)) {+ return readLastRecord();+ }++ bytesRead += next.value.byteLength;+ remaining += decoder.decode(next.value, { stream: true });+ const lines = remaining.split("\n");+ remaining = lines.pop() ?? "";++ for (const line of lines) {+ if (!reserveRecord()) return null;+ const cwd = extractCwd(line.trim());+ if (cwd !== null) return cwd;+ }+ }++ if (bytesRead < transcript.size) {+ budget.truncated = true;+ return null;+ }+ return readLastRecord();+ }),+ ),+ ),+ ).pipe(Effect.orElseSucceed(() => null));+ });++ /** Check the open file before and after reading, without reading past its reserved byte budget. */+ const readTranscript = Effect.fn("AgentSessionScanner.readTranscript")(function* (+ filePath: string,+ expected: ReturnType<typeof transcriptIdentity>,+ ) {+ if (expected.size > MAX_IMPORTED_TRANSCRIPT_BYTES) return null;++ return yield* Effect.scoped(+ fileSystem.open(filePath, { flag: "r" }).pipe(+ Effect.flatMap((file) =>+ Effect.gen(function* () {+ if (!sameTranscriptIdentity(expected, transcriptIdentity(filePath, yield* file.stat))) {+ return null;+ }+ const decoder = new TextDecoder();+ let contents = "";+ let bytesRead = 0;++ while (bytesRead < expected.size) {+ const next = yield* file.readAlloc(+ Math.min(TRANSCRIPT_PREFIX_BYTES, expected.size - bytesRead),+ );+ if (Option.isNone(next)) {+ return null;+ }++ bytesRead += next.value.byteLength;+ contents += decoder.decode(next.value, { stream: true });+ }++ return sameTranscriptIdentity(expected, transcriptIdentity(filePath, yield* file.stat))+ ? contents + decoder.decode()+ : null;+ }),+ ),+ ),+ ).pipe(Effect.orElseSucceed(() => null));+ });++ /**+ * Resolve the Claude config directory the CLI would use, matching the+ * precedence the spawned CLI sees: the instance's `homePath` (exported as+ * `CLAUDE_CONFIG_DIR`), then a `CLAUDE_CONFIG_DIR` already in the+ * environment, then `~/.claude`.+ */+ const resolveClaudeConfigDir = (homePath: string, environmentHome?: string): string => {+ const configured = homePath.trim();+ if (configured.length > 0) {+ return path.resolve(expandHomePath(configured));+ }+ const fromEnvironment = environmentHome?.trim() ?? "";+ if (fromEnvironment.length > 0) {+ return path.resolve(expandHomePath(fromEnvironment));+ }+ return path.join(NodeOS.homedir(), ".claude");+ };++ const discoverClaudeTranscripts = Effect.fn("AgentSessionScanner.discoverClaudeTranscripts")(+ function* (homePath: string, providerInstanceId: ProviderInstanceId, operationBudget: number) {+ const projectsDir = path.join(homePath, "projects");+ let operationsRemaining = operationBudget;+ let truncated = false;+ const readDirectory = (directory: string) => {+ if (operationsRemaining <= 0) {+ truncated = true;+ return Effect.succeed<ReadonlyArray<string>>([]);+ }+ operationsRemaining -= 1;+ return listDirectory(directory);+ };+ const projectDirectories = yield* readDirectory(projectsDir);+ const transcripts: Array<TranscriptCandidate> = [];++ for (const projectDirectory of projectDirectories) {+ if (operationsRemaining <= 0) {+ truncated = true;+ break;+ }+ const directory = path.join(projectsDir, projectDirectory);+ const directoryTranscripts = (yield* readDirectory(directory))+ .filter((entry) => entry.endsWith(".jsonl"))+ .map((entry) => path.join(directory, entry));++ for (const filePath of directoryTranscripts) {+ if (operationsRemaining <= 0) {+ truncated = true;+ break;+ }+ operationsRemaining -= 1;+ const stats = yield* statOption(filePath);+ if (+ Option.isNone(stats) ||+ stats.value.type !== "File" ||+ Option.isNone(stats.value.mtime)+ ) {+ continue;+ }+ transcripts.push({+ filePath,+ mtimeMs: stats.value.mtime.value.getTime(),+ providerInstanceId,+ size: Number(stats.value.size),+ });+ }+ }+ return { transcripts, truncated };+ },+ );++ const discoverCodexTranscripts = Effect.fn("AgentSessionScanner.discoverCodexTranscripts")(+ function* (homePath: string, providerInstanceId: ProviderInstanceId, operationBudget: number) {+ const sessionsDir = path.join(homePath, "sessions");++ const transcripts: Array<TranscriptCandidate> = [];+ let operationsRemaining = operationBudget;+ let truncated = false;+ const readDirectory = (directory: string) => {+ if (operationsRemaining <= 0) {+ truncated = true;+ return Effect.succeed<ReadonlyArray<string>>([]);+ }+ operationsRemaining -= 1;+ return listDirectory(directory);+ };+ // Date-partitioned directories sort chronologically, so walking them in+ // reverse spends each home's share of the operation budget on recent sessions.+ for (const year of (yield* readDirectory(sessionsDir)).toSorted().toReversed()) {+ if (operationsRemaining <= 0) {+ truncated = true;+ break;+ }+ for (const month of (yield* readDirectory(path.join(sessionsDir, year)))+ .toSorted()+ .toReversed()) {+ if (operationsRemaining <= 0) {+ truncated = true;+ break;+ }+ for (const day of (yield* readDirectory(path.join(sessionsDir, year, month)))+ .toSorted()+ .toReversed()) {+ if (operationsRemaining <= 0) {+ truncated = true;+ break;+ }+ const directory = path.join(sessionsDir, year, month, day);+ for (const entry of (yield* readDirectory(directory)).toSorted().toReversed()) {+ if (!entry.startsWith("rollout-") || !entry.endsWith(".jsonl")) continue;+ if (operationsRemaining <= 0) {+ truncated = true;+ break;+ }+ const filePath = path.join(directory, entry);+ operationsRemaining -= 1;+ const stats = yield* statOption(filePath);+ if (+ Option.isSome(stats) &&+ stats.value.type === "File" &&+ Option.isSome(stats.value.mtime)+ ) {+ transcripts.push({+ filePath,+ mtimeMs: stats.value.mtime.value.getTime(),+ providerInstanceId,+ size: Number(stats.value.size),+ });+ }+ }+ }+ }+ }+ return { transcripts, truncated };+ },+ );++ const groupTranscriptsByCwd = Effect.fn("AgentSessionScanner.groupTranscriptsByCwd")(function* (+ source: AgentSessionSource,+ transcripts: ReadonlyArray<TranscriptCandidate>,+ budget: MetadataReadBudget,+ ) {+ const byOwnerAndCwd = new Map<+ string,+ {+ cwd: string;+ providerInstanceId: ProviderInstanceId;+ lastActiveAtMs: number;+ transcripts: Array<{ filePath: string; mtimeMs: number }>;+ }+ >();++ for (const transcript of transcripts) {+ const cwd = yield* readCwd(transcript, budget);+ if (cwd === null) continue;+ const key = `${transcript.providerInstanceId}\0${cwd}`;+ const existing = byOwnerAndCwd.get(key);+ if (existing) {+ existing.lastActiveAtMs = Math.max(existing.lastActiveAtMs, transcript.mtimeMs);+ existing.transcripts.push(transcript);+ } else {+ byOwnerAndCwd.set(key, {+ cwd,+ providerInstanceId: transcript.providerInstanceId,+ lastActiveAtMs: transcript.mtimeMs,+ transcripts: [transcript],+ });+ }+ }++ return Array.from(byOwnerAndCwd.values(), (group): RawCandidate => ({+ cwd: group.cwd,+ source,+ providerInstanceId: group.providerInstanceId,+ threadCount: group.transcripts.length,+ lastActiveAtMs: group.lastActiveAtMs,+ transcripts: group.transcripts,+ }));+ });++ const collectCandidates = Effect.fn("AgentSessionScanner.collectCandidates")(function* () {+ const settings = yield* serverSettings.getSettings.pipe(+ Effect.mapError((cause) => new AgentSessionScanError({ operation: "read-settings", cause })),+ );++ const raw: Array<RawCandidate> = [];+ let truncated = false;++ for (const source of ["claudeAgent", "codex"] as const) {+ const instances: Array<{+ readonly instanceId: ProviderInstanceId;+ readonly config: ProviderInstanceConfig;+ }> = Object.entries(settings.providerInstances)+ .filter(+ ([, instance]) => instance.driver === source && resolveProviderInstanceEnabled(instance),+ )+ .map(([instanceId, config]) => ({+ instanceId: ProviderInstanceId.make(instanceId),+ config,+ }));+ if (!Object.hasOwn(settings.providerInstances, source)) {+ const legacyInstance = {+ instanceId: ProviderInstanceId.make(source),+ config: {+ driver: ProviderDriverKind.make(source),+ config: settings.providers[source],+ },+ };+ if (resolveProviderInstanceEnabled(legacyInstance.config)) {+ instances.push(legacyInstance);+ }+ }++ // A shared home contains one copy of each session. Prefer the built-in+ // instance as its owner, then keep configured order for custom accounts.+ instances.sort((left, right) => {+ const leftDefault = left.instanceId === source ? 0 : 1;+ const rightDefault = right.instanceId === source ? 0 : 1;+ return leftDefault - rightDefault;+ });+ const homes: Array<{ homePath: string; providerInstanceId: ProviderInstanceId }> = [];+ const seenHomes = new Set<string>();+ for (const { instanceId, config: instance } of instances) {+ const homeVariable = source === "claudeAgent" ? "CLAUDE_CONFIG_DIR" : "CODEX_HOME";+ const environmentHome =+ instance.environment?.findLast((variable) => variable.name === homeVariable)?.value ??+ hostEnvironment[homeVariable];++ let homePath: string;+ if (source === "claudeAgent") {+ const config = decodeClaudeSettings(instance.config ?? {});+ if (Option.isNone(config)) continue;+ homePath = resolveClaudeConfigDir(config.value.homePath, environmentHome);+ } else {+ const config = decodeCodexSettings(instance.config ?? {});+ if (Option.isNone(config)) continue;+ const codexSettings =+ config.value.homePath.trim().length === 0 &&+ config.value.shadowHomePath.trim().length === 0 &&+ environmentHome?.trim()+ ? { ...config.value, homePath: environmentHome }+ : config.value;+ const layout = yield* resolveCodexHomeLayout(codexSettings).pipe(+ Effect.provideService(Path.Path, path),+ );+ homePath = layout.sharedHomePath;+ }++ const homeKey = `${source}\0${yield* directoryIdentity(homePath)}`;+ if (seenHomes.has(homeKey)) continue;+ seenHomes.add(homeKey);+ homes.push({ homePath, providerInstanceId: instanceId });+ }++ const transcriptCandidates: Array<TranscriptCandidate> = [];+ const baseOperationBudget = Math.floor(+ MAX_DISCOVERY_OPERATIONS_PER_SOURCE / Math.max(1, homes.length),+ );+ const extraOperationBudgets = MAX_DISCOVERY_OPERATIONS_PER_SOURCE % Math.max(1, homes.length);+ for (const [index, home] of homes.entries()) {+ const operationBudget = baseOperationBudget + (index < extraOperationBudgets ? 1 : 0);+ if (operationBudget === 0) {+ truncated = true;+ continue;+ }+ const discovered = yield* source === "claudeAgent"+ ? discoverClaudeTranscripts(home.homePath, home.providerInstanceId, operationBudget)+ : discoverCodexTranscripts(home.homePath, home.providerInstanceId, operationBudget);+ truncated ||= discovered.truncated;+ transcriptCandidates.push(...discovered.transcripts);+ }++ transcriptCandidates.sort(+ (left, right) =>+ right.mtimeMs - left.mtimeMs || left.filePath.localeCompare(right.filePath),+ );+ if (transcriptCandidates.length > MAX_TRANSCRIPTS_PER_SOURCE) {+ truncated = true;+ }+ // Give each account a turn before taking another file from the same home.+ const selectedTranscripts = selectMetadataTranscripts(transcriptCandidates);+ const metadataBudget: MetadataReadBudget = {+ bytesRemaining: MAX_METADATA_BYTES_PER_SOURCE,+ operationsRemaining: MAX_METADATA_OPERATIONS_PER_SOURCE,+ recordsRemaining: MAX_METADATA_RECORDS_PER_SOURCE,+ truncated: false,+ };+ raw.push(...(yield* groupTranscriptsByCwd(source, selectedTranscripts, metadataBudget)));+ truncated ||= metadataBudget.truncated;+ }++ return { candidates: raw, truncated };+ });++ let cachedCandidates: ReadonlyArray<RawCandidate> | null = null;++ const scan: AgentSessionScanner["Service"]["scan"] = Effect.gen(function* () {+ const { candidates: raw, truncated } = yield* collectCandidates();+ cachedCandidates = raw;++ // Filesystem identity merges symlinks and case aliases without collapsing+ // distinct case-sensitive directories.+ const merged = new Map<+ string,+ {+ path: string;+ sources: Array<AgentSessionSource>;+ threadCount: number;+ lastActiveAtMs: number | null;+ }+ >();+ const directoryKeys = new Map<string, string>();++ for (const candidate of raw) {+ const expanded = expandHomePath(candidate.cwd.trim());+ if (!path.isAbsolute(expanded)) continue;+ const resolved = path.resolve(expanded);+ if (isExcludedProjectPath(resolved)) continue;+ let key = directoryKeys.get(resolved);+ if (key === undefined) {+ const stats = yield* statOption(resolved);+ // Directories that no longer exist can't be imported.+ if (Option.isNone(stats) || stats.value.type !== "Directory") {+ directoryKeys.set(resolved, "");+ continue;+ }+ const realPath = yield* fileSystem+ .realPath(resolved)+ .pipe(Effect.orElseSucceed(() => resolved));+ // A symlink can point into the worktrees directory even when its own+ // spelling doesn't; check again with links resolved.+ if (isExcludedProjectPath(realPath)) {+ key = "";+ } else {+ key = yield* directoryIdentity(resolved, stats.value);+ }+ directoryKeys.set(resolved, key);+ }+ if (key === "") continue;++ const existing = merged.get(key);+ if (!existing) {+ merged.set(key, {+ path: resolved,+ sources: [candidate.source],+ threadCount: candidate.threadCount,+ lastActiveAtMs: candidate.lastActiveAtMs,+ });+ continue;+ }+ if (!existing.sources.includes(candidate.source)) {+ existing.sources.push(candidate.source);+ }+ existing.threadCount += candidate.threadCount;+ existing.lastActiveAtMs =+ existing.lastActiveAtMs === null || candidate.lastActiveAtMs === null+ ? (existing.lastActiveAtMs ?? candidate.lastActiveAtMs)+ : Math.max(existing.lastActiveAtMs, candidate.lastActiveAtMs);+ }++ // Resolve persisted roots too. A project and a transcript can name+ // different symlinks to the same directory.+ const shellSnapshot = yield* projectionSnapshotQuery+ .getShellSnapshot()+ .pipe(+ Effect.mapError(+ (cause) => new AgentSessionScanError({ operation: "read-projects", cause }),+ ),+ );+ const importedProjectsByRoot = new Map<string, (typeof shellSnapshot.projects)[number]>();+ for (const project of shellSnapshot.projects) {+ const projectRoot = path.resolve(expandHomePath(project.workspaceRoot));+ importedProjectsByRoot.set(normalizeProjectPathForComparison(projectRoot), project);+ importedProjectsByRoot.set(yield* directoryIdentity(projectRoot), project);+ }++ const candidates: Array<AgentSessionProjectCandidate> = [];+ for (const [key, entry] of merged.entries()) {+ // Keep the path key for missing roots and use filesystem identity for+ // aliases that resolve to the same directory.+ const importedProject =+ importedProjectsByRoot.get(normalizeProjectPathForComparison(entry.path)) ??+ importedProjectsByRoot.get(key);+ const candidatePath = importedProject?.workspaceRoot ?? entry.path;+ candidates.push({+ path: candidatePath,+ title: path.basename(candidatePath) || candidatePath,+ ...(importedProject === undefined ? {} : { projectId: importedProject.id }),+ sources: entry.sources,+ threadCount: entry.threadCount,+ lastActiveAt:+ entry.lastActiveAtMs === null+ ? null+ : DateTime.formatIso(DateTime.makeUnsafe(entry.lastActiveAtMs)),+ alreadyImported: importedProject !== undefined,+ });+ }++ // Newest first, undated candidates last.+ candidates.sort((left, right) => {+ if (left.lastActiveAt === right.lastActiveAt) return left.path.localeCompare(right.path);+ if (left.lastActiveAt === null) return 1;+ if (right.lastActiveAt === null) return -1;+ return right.lastActiveAt.localeCompare(left.lastActiveAt);+ });++ return {+ candidates,+ scannedAt: DateTime.formatIso(yield* DateTime.now),+ ...(truncated ? { truncated: true } : {}),+ };+ });++ const prepareRecentThreads = Effect.fn("AgentSessionScanner.prepareRecentThreads")(function* (+ workspaceRoot: string,+ completedSources: ReadonlyArray<AgentSessionImportSource>,+ ) {+ const root = path.resolve(expandHomePath(workspaceRoot));+ const realRoot = yield* fileSystem.realPath(root).pipe(Effect.orElseSucceed(() => root));+ if (isExcludedProjectPath(root) || isExcludedProjectPath(realRoot)) return Stream.empty;+ const rootIdentity = yield* directoryIdentity(root);+ const nowMs = DateTime.toEpochMillis(yield* DateTime.now);+ const cutoffMs = nowMs - RECENT_THREAD_WINDOW_MS;++ const candidates = cachedCandidates ?? (yield* collectCandidates()).candidates;+ cachedCandidates = candidates;++ const eligibleTranscripts: Array<{+ readonly candidate: RawCandidate;+ readonly transcript: RawCandidate["transcripts"][number] & { readonly mtimeMs: number };+ }> = [];+ for (const candidate of candidates) {+ const expanded = expandHomePath(candidate.cwd.trim());+ if (!path.isAbsolute(expanded)) continue;+ const resolved = path.resolve(expanded);+ if ((yield* directoryIdentity(resolved)) !== rootIdentity) continue;++ for (const transcript of candidate.transcripts) {+ if (+ transcript.mtimeMs === null ||+ transcript.mtimeMs < cutoffMs ||+ transcript.mtimeMs > nowMs+ ) {+ continue;+ }+ eligibleTranscripts.push({+ candidate,+ transcript: { ...transcript, mtimeMs: transcript.mtimeMs },+ });+ }+ }++ eligibleTranscripts.sort((left, right) => {+ if (left.transcript.mtimeMs !== right.transcript.mtimeMs) {+ return right.transcript.mtimeMs - left.transcript.mtimeMs;+ }+ return left.transcript.filePath.localeCompare(right.transcript.filePath);+ });++ const completedByFile = Map.groupBy(+ completedSources,+ (source) => `${source.providerInstanceId}\0${source.filePath}`,+ );+ const importedSessions = new Set<string>();+ let bytesRemaining = MAX_IMPORT_BYTES;+ let transcriptsRemaining = MAX_IMPORT_TRANSCRIPTS;+ let recordsRemaining = MAX_IMPORT_RECORDS;+ 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 || recordsRemaining === 0)+ ) {+ return Option.some<AgentSessionRecentThread>({ _tag: "Skipped" });+ }+ const stats = yield* statOption(transcript.filePath);+ if (Option.isNone(stats) || stats.value.type !== "File") {+ return Option.some<AgentSessionRecentThread>({ _tag: "Skipped" });+ }+ const identity = transcriptIdentity(transcript.filePath, stats.value);+ const completedSource = completed?.find(+ (source) =>+ source.provider === candidate.source && sameTranscriptIdentity(source, identity),+ );+ if (completedSource !== undefined) {+ const sessionKey = `${completedSource.providerInstanceId}\0${completedSource.providerSessionId}`;+ if (importedSessions.has(sessionKey)) return Option.none<AgentSessionRecentThread>();+ importedSessions.add(sessionKey);+ return Option.some<AgentSessionRecentThread>({+ _tag: "AlreadyImported",+ source: completedSource,+ });+ }+ if (+ transcriptsRemaining === 0 ||+ recordsRemaining === 0 ||+ identity.size > MAX_IMPORTED_TRANSCRIPT_BYTES ||+ identity.size > bytesRemaining+ ) {+ return Option.some<AgentSessionRecentThread>({ _tag: "Skipped" });+ }+ // Reserve the whole file even if its read or parse fails.+ transcriptsRemaining -= 1;+ bytesRemaining -= identity.size;+ const contents = yield* readTranscript(transcript.filePath, identity);+ if (contents === null) {+ return Option.some<AgentSessionRecentThread>({ _tag: "Skipped" });+ }+ const lines = splitTranscriptRecords(contents, recordsRemaining + 1);+ if (lines.length > recordsRemaining) {+ return Option.some<AgentSessionRecentThread>({ _tag: "Skipped" });+ }+ recordsRemaining -= lines.length;++ // A stable replacement file can belong to a different project than the cached candidate.+ let snapshotCwd: string | null = null;+ for (const line of lines) {+ snapshotCwd = extractCwd(line);+ if (snapshotCwd !== null) break;+ }+ if (snapshotCwd === null) {+ return Option.some<AgentSessionRecentThread>({ _tag: "Skipped" });+ }+ const expandedCwd = expandHomePath(snapshotCwd.trim());+ if (+ !path.isAbsolute(expandedCwd) ||+ (yield* directoryIdentity(path.resolve(expandedCwd))) !== rootIdentity+ ) {+ return Option.some<AgentSessionRecentThread>({ _tag: "Skipped" });+ }++ const parsedThread = parseAgentSessionTranscript(+ {+ contents,+ source: candidate.source,+ providerInstanceId: candidate.providerInstanceId,+ fallbackSessionId: path.basename(transcript.filePath, ".jsonl"),+ lastActiveAtMs: transcript.mtimeMs,+ },+ lines,+ );+ if (parsedThread === null) {+ return Option.some<AgentSessionRecentThread>({ _tag: "Skipped" });+ }++ const source: AgentSessionImportSource = {+ ...identity,+ provider: parsedThread.source,+ providerInstanceId: parsedThread.providerInstanceId,+ providerSessionId: parsedThread.providerSessionId,+ };+ const sessionKey = `${parsedThread.providerInstanceId}\0${parsedThread.providerSessionId}`;+ if (importedSessions.has(sessionKey)) {+ return Option.some<AgentSessionRecentThread>({ _tag: "Duplicate", source });+ }+ importedSessions.add(sessionKey);+ return Option.some<AgentSessionRecentThread>({+ _tag: "Importable",+ thread: parsedThread,+ source,+ });+ }),+ ),+ Stream.map(Option.toArray),+ Stream.flattenIterable,+ );+ });++ const recentThreads: AgentSessionScanner["Service"]["recentThreads"] = (+ workspaceRoot,+ completedSources = [],+ ) => Stream.unwrap(prepareRecentThreads(workspaceRoot, completedSources));++ return AgentSessionScanner.of({ scan, recentThreads });+});++export const layer = Layer.effect(AgentSessionScanner, make);diff --git a/apps/server/src/project/ProjectSetupScriptRunner.test.ts b/apps/server/src/project/ProjectSetupScriptRunner.test.tsindex 46fee8d8add..6cc399dd4de 100644--- a/apps/server/src/project/ProjectSetupScriptRunner.test.ts+++ b/apps/server/src/project/ProjectSetupScriptRunner.test.ts@@ -41,6 +41,7 @@ const makeProjectionSnapshotQueryLayer = (project: OrchestrationProject) =>getProjectShellById: (projectId) =>Effect.succeed(projectId === project.id ? Option.some(project) : Option.none()),getFirstActiveThreadIdByProjectId: () => Effect.die("unused"),+ getImportedAgentSessionSources: () => Effect.die("unused"),getThreadCheckpointContext: () => Effect.die("unused"),getFullThreadDiffContext: () => Effect.die("unused"),getThreadRuntimeContext: () => Effect.die("unused"),diff --git a/apps/server/src/provider/Drivers/ClaudeDriver.ts b/apps/server/src/provider/Drivers/ClaudeDriver.tsindex 56cb1cb06f0..324284a3a4c 100644--- a/apps/server/src/provider/Drivers/ClaudeDriver.ts+++ b/apps/server/src/provider/Drivers/ClaudeDriver.ts@@ -26,6 +26,7 @@ import { ChildProcessSpawner } from "effect/unstable/process";import { makeClaudeTextGeneration } from "../../textGeneration/ClaudeTextGeneration.ts";import * as BackgroundPolicy from "../../background/BackgroundPolicy.ts";import { ServerConfig } from "../../config.ts";+import { expandHomePath } from "../../pathExpansion.ts";import { ServerSettingsService } from "../../serverSettings.ts";import { ProviderDriverError } from "../Errors.ts";import { makeClaudeAdapter } from "../Layers/ClaudeAdapter.ts";@@ -119,7 +120,11 @@ export const ClaudeDriver: ProviderDriver<ClaudeSettings, ClaudeDriverEnv> = {driverKind: DRIVER_KIND,instanceId,});- const effectiveConfig = { ...config, enabled } satisfies ClaudeSettings;+ const effectiveConfig = {+ ...config,+ enabled,+ binaryPath: expandHomePath(config.binaryPath),+ } satisfies ClaudeSettings;const resolveMaintenance = yield* makeCachedProviderMaintenanceResolution(resolveProviderMaintenanceCapabilitiesEffect(UPDATE, {binaryPath: effectiveConfig.binaryPath,diff --git a/apps/server/src/provider/Drivers/CodexDriver.ts b/apps/server/src/provider/Drivers/CodexDriver.tsindex d7fd1e9c569..071fb20674a 100644--- a/apps/server/src/provider/Drivers/CodexDriver.ts+++ b/apps/server/src/provider/Drivers/CodexDriver.ts@@ -33,6 +33,7 @@ import { ChildProcessSpawner } from "effect/unstable/process";import { makeCodexTextGeneration } from "../../textGeneration/CodexTextGeneration.ts";import * as BackgroundPolicy from "../../background/BackgroundPolicy.ts";import { ServerConfig } from "../../config.ts";+import { expandHomePath } from "../../pathExpansion.ts";import { ServerSettingsService } from "../../serverSettings.ts";import { ProviderDriverError } from "../Errors.ts";import { makeCodexAdapter } from "../Layers/CodexAdapter.ts";@@ -157,6 +158,7 @@ export const CodexDriver: ProviderDriver<CodexSettings, CodexDriverEnv> = {const effectiveConfig = {...config,enabled,+ binaryPath: expandHomePath(config.binaryPath),homePath: homeLayout.effectiveHomePath ?? "",} satisfies CodexSettings;const resolveMaintenance = yield* makeCachedProviderMaintenanceResolution(diff --git a/apps/server/src/provider/Layers/CodexAdapter.test.ts b/apps/server/src/provider/Layers/CodexAdapter.test.tsindex 2ca44a2a5f0..4676d780a53 100644--- a/apps/server/src/provider/Layers/CodexAdapter.test.ts+++ b/apps/server/src/provider/Layers/CodexAdapter.test.ts@@ -222,6 +222,7 @@ function makeScopedRuntimeFactory(options?: { readonly failConstruction?: booleaconst providerSessionDirectoryTestLayer = Layer.succeed(ProviderSessionDirectory, {upsert: () => Effect.void,+ recordImportedTranscript: () => Effect.die("unused"),getProvider: () =>Effect.die(new Error("ProviderSessionDirectory.getProvider is not used in test")),getBinding: () => Effect.succeed(Option.none()),diff --git a/apps/server/src/provider/Layers/OpenCodeAdapter.test.ts b/apps/server/src/provider/Layers/OpenCodeAdapter.test.tsindex 81e799c9095..082e20d7cb5 100644--- a/apps/server/src/provider/Layers/OpenCodeAdapter.test.ts+++ b/apps/server/src/provider/Layers/OpenCodeAdapter.test.ts@@ -516,6 +516,7 @@ const OpenCodeRuntimeTestDouble: OpenCodeRuntimeShape = {const providerSessionDirectoryTestLayer = Layer.succeed(ProviderSessionDirectory, {upsert: () => Effect.void,+ recordImportedTranscript: () => Effect.die("unused"),getProvider: () =>Effect.die(new Error("ProviderSessionDirectory.getProvider is not used in test")),getBinding: () => Effect.succeed(Option.none()),diff --git a/apps/server/src/provider/Layers/ProviderInstanceRegistryLive.test.ts b/apps/server/src/provider/Layers/ProviderInstanceRegistryLive.test.tsindex af43e039e65..25dafa5ba04 100644--- a/apps/server/src/provider/Layers/ProviderInstanceRegistryLive.test.ts+++ b/apps/server/src/provider/Layers/ProviderInstanceRegistryLive.test.ts@@ -24,7 +24,6 @@*/import { describe, expect, it } from "@effect/vitest";import * as NodeServices from "@effect/platform-node/NodeServices";-import * as Path from "effect/Path";import {type ClaudeSettings,type CodexSettings,@@ -35,9 +34,12 @@ import {type ProviderInstanceConfigMap,ProviderInstanceId,} from "@t3tools/contracts";+import { isHostWindows } from "@t3tools/shared/hostProcess";import * as DateTime from "effect/DateTime";import * as Effect from "effect/Effect";+import * as FileSystem from "effect/FileSystem";import * as Layer from "effect/Layer";+import * as Path from "effect/Path";import * as Stream from "effect/Stream";import { HttpClient, HttpClientResponse } from "effect/unstable/http";@@ -45,6 +47,7 @@ import * as BackgroundPolicy from "../../background/BackgroundPolicy.ts";import type { BuiltInDriversEnv } from "../builtInDrivers.ts";import { AntigravityInstallation } from "../AntigravityInstallation.ts";import { ServerConfig } from "../../config.ts";+import { expandHomePath } from "../../pathExpansion.ts";import { ServerSettingsService } from "../../serverSettings.ts";import { ClaudeDriver } from "../Drivers/ClaudeDriver.ts";import { CodexDriver } from "../Drivers/CodexDriver.ts";@@ -139,6 +142,80 @@ const makeOpenCodeConfig = (overrides: Partial<OpenCodeSettings>): OpenCodeSetti...overrides,});+const makeTildeProviderFixtures = Effect.fn(+ "ProviderInstanceRegistryLive.test.makeTildeProviderFixtures",+)(function* () {+ const fileSystem = yield* FileSystem.FileSystem;+ const path = yield* Path.Path;+ const homePath = expandHomePath("~");+ const fixtureDir = yield* fileSystem.makeTempDirectoryScoped({+ directory: homePath,+ prefix: ".t3-provider-path-test-",+ });+ const codexPath = path.join(fixtureDir, "codex");+ const claudePath = path.join(fixtureDir, "claude");+ const claudeHomePath = path.join(fixtureDir, "claude-home");+ const codexScriptPath = path.join(fixtureDir, "codex-script.json");+ const codexFixtureDir = path.join(import.meta.dirname, "../testFixtures");++ yield* fileSystem.copyFile(path.join(codexFixtureDir, "codexCollabMockPeer.sh"), codexPath);+ yield* fileSystem.copyFile(+ path.join(codexFixtureDir, "codexCollabMockPeer.mjs"),+ path.join(fixtureDir, "codexCollabMockPeer.mjs"),+ );+ yield* fileSystem.copyFile(+ path.join(codexFixtureDir, "codexMultiAgentWire.json"),+ path.join(fixtureDir, "codexMultiAgentWire.json"),+ );+ yield* fileSystem.writeFileString(+ codexScriptPath,+ // @effect-diagnostics-next-line preferSchemaOverJson:off - fixed script document read by the external Codex mock peer.+ JSON.stringify({ rootThreadId: "probe-thread", notifications: [] }),+ );+ yield* fileSystem.chmod(codexPath, 0o755);++ yield* fileSystem.writeFileString(+ claudePath,+ [+ "#!/usr/bin/env node",+ 'import * as NodeReadline from "node:readline";',+ 'if (process.argv.includes("--version")) {',+ ' process.stdout.write("claude 2.1.219\\n");',+ " process.exit(0);",+ "}",+ "const lines = NodeReadline.createInterface({ input: process.stdin });",+ 'lines.on("line", (line) => {',+ " const message = JSON.parse(line);",+ ' if (message.type !== "control_request" || message.request?.subtype !== "initialize") return;',+ " process.stdout.write(JSON.stringify({",+ ' type: "control_response",',+ " response: {",+ ' subtype: "success",',+ " request_id: message.request_id,",+ " response: {",+ " commands: [], agents: [], models: [],",+ ' output_style: "default", available_output_styles: ["default"],',+ ' account: { email: "[email protected]", subscriptionType: "pro", tokenSource: "oauth" },',+ " },",+ " },",+ ' }) + "\\n");',+ "});",+ "setInterval(() => {}, 1_000);",+ "",+ ].join("\n"),+ );+ yield* fileSystem.chmod(claudePath, 0o755);+ yield* fileSystem.makeDirectory(claudeHomePath);++ const asTildePath = (filePath: string) => `~/${path.relative(homePath, filePath)}`;+ return {+ codexBinaryPath: asTildePath(codexPath),+ claudeBinaryPath: asTildePath(claudePath),+ claudeHomePath,+ codexScriptPath,+ };+});+describe("ProviderInstanceRegistryLive — multi-instance codex slice", () => {// `ServerConfig.layerTest` needs `FileSystem` to materialize its scratch// directory. `Layer.merge` just unions requirements, so we have to push@@ -261,6 +338,60 @@ describe("ProviderInstanceRegistryLive — multi-instance codex slice", () => {}).pipe(Effect.provide(testLayer)),);+ it.live("runs Codex and Claude readiness probes from configured tilde paths", () =>+ Effect.gen(function* () {+ if (yield* isHostWindows) return;++ const fixtures = yield* makeTildeProviderFixtures();++ const codexId = ProviderInstanceId.make("codex_tilde");+ const claudeId = ProviderInstanceId.make("claude_tilde");+ const configMap: ProviderInstanceConfigMap = {+ [codexId]: {+ driver: ProviderDriverKind.make("codex"),+ enabled: true,+ environment: [+ {+ name: "T3_CODEX_COLLAB_SCRIPT",+ value: fixtures.codexScriptPath,+ sensitive: false,+ },+ ],+ config: makeCodexConfig({ enabled: true, binaryPath: fixtures.codexBinaryPath }),+ },+ [claudeId]: {+ driver: ProviderDriverKind.make("claudeAgent"),+ enabled: true,+ config: makeClaudeConfig({+ enabled: true,+ binaryPath: fixtures.claudeBinaryPath,+ homePath: fixtures.claudeHomePath,+ }),+ },+ };++ const { registry } = yield* makeProviderInstanceRegistry({+ drivers: [CodexDriver, ClaudeDriver],+ configMap,+ });+ const codex = yield* registry.getInstance(codexId);+ const claude = yield* registry.getInstance(claudeId);+ expect(codex).toBeDefined();+ expect(claude).toBeDefined();++ const [codexSnapshot, claudeSnapshot] = yield* Effect.all(+ [codex!.snapshot.refresh, claude!.snapshot.refresh],+ { concurrency: "unbounded" },+ );+ expect(codexSnapshot).toMatchObject({ status: "ready", installed: true, version: "0.0.0" });+ expect(claudeSnapshot).toMatchObject({+ status: "ready",+ installed: true,+ version: "2.1.219",+ });+ }).pipe(Effect.provide(testLayer)),+ );+it.live("shadows instances whose driver is not registered in this build without failing boot",() =>diff --git a/apps/server/src/provider/Layers/ProviderService.test.ts b/apps/server/src/provider/Layers/ProviderService.test.tsindex f5b9be91650..238265ec5a4 100644--- a/apps/server/src/provider/Layers/ProviderService.test.ts+++ b/apps/server/src/provider/Layers/ProviderService.test.ts@@ -4271,6 +4271,7 @@ const getBinding = vi.fn((threadId: ThreadId) =>const boundedListing = makeProviderServiceLayer({directory: {upsert: () => Effect.void,+ recordImportedTranscript: () => Effect.die("unused"),getProvider: () => Effect.die("ProviderService.listSessions does not use getProvider"),getBinding,listThreadIds,diff --git a/apps/server/src/provider/Layers/ProviderSessionDirectory.test.ts b/apps/server/src/provider/Layers/ProviderSessionDirectory.test.tsindex 079b7f10ebf..8b41bd3e518 100644--- a/apps/server/src/provider/Layers/ProviderSessionDirectory.test.ts+++ b/apps/server/src/provider/Layers/ProviderSessionDirectory.test.ts@@ -4,9 +4,13 @@ import * as NodeOS from "node:os";import * as NodePath from "node:path";import * as NodeServices from "@effect/platform-node/NodeServices";-import { ProviderDriverKind, ThreadId } from "@t3tools/contracts";-import { it, assert } from "@effect/vitest";-import { assertSome } from "@effect/vitest/utils";+import {+ ProviderDriverKind,+ ProviderInstanceId,+ ThreadId,+ type AgentSessionImportSource,+} from "@t3tools/contracts";+import { assert, expect, it } from "@effect/vitest";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";import * as Option from "effect/Option";@@ -20,9 +24,22 @@ import * as ProviderSessionRuntime from "../../persistence/ProviderSessionRuntimimport { ProviderSessionDirectory } from "../Services/ProviderSessionDirectory.ts";import { ProviderSessionDirectoryLive } from "./ProviderSessionDirectory.ts";+const importedSource = {+ provider: "codex",+ providerInstanceId: ProviderInstanceId.make("codex"),+ providerSessionId: "provider-session",+ filePath: "/tmp/provider-session.jsonl",+ size: 100,+ mtimeMs: 1_000,+ device: 1,+ inode: 123,+ birthtimeMs: 500,+} satisfies AgentSessionImportSource;+function makeDirectoryLayer<E, R>(persistenceLayer: Layer.Layer<SqlClient.SqlClient, E, R>) {const runtimeRepositoryLayer = ProviderSessionRuntime.layer.pipe(Layer.provide(persistenceLayer));return Layer.mergeAll(+ persistenceLayer,runtimeRepositoryLayer,ProviderSessionDirectoryLive.pipe(Layer.provide(runtimeRepositoryLayer)),NodeServices.layer,@@ -30,7 +47,7 @@ function makeDirectoryLayer<E, R>(persistenceLayer: Layer.Layer<SqlClient.SqlCli}it.layer(makeDirectoryLayer(SqlitePersistenceMemory))("ProviderSessionDirectoryLive", (it) => {- it("upserts and reads thread bindings", () =>+ it.effect("upserts and reads thread bindings", () =>Effect.gen(function* () {const directory = yield* ProviderSessionDirectory;const runtimeRepository = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository;@@ -39,13 +56,14 @@ it.layer(makeDirectoryLayer(SqlitePersistenceMemory))("ProviderSessionDirectoryLyield* directory.upsert({provider: ProviderDriverKind.make("codex"),+ providerInstanceId: ProviderInstanceId.make("codex"),threadId: initialThreadId,});const provider = yield* directory.getProvider(initialThreadId);assert.equal(provider, "codex");const resolvedBinding = yield* directory.getBinding(initialThreadId);- assertSome(resolvedBinding, {+ expect(Option.getOrThrow(resolvedBinding)).toMatchObject({threadId: initialThreadId,provider: ProviderDriverKind.make("codex"),});@@ -57,6 +75,7 @@ it.layer(makeDirectoryLayer(SqlitePersistenceMemory))("ProviderSessionDirectoryLyield* directory.upsert({provider: ProviderDriverKind.make("codex"),+ providerInstanceId: ProviderInstanceId.make("codex"),threadId: nextThreadId,});const updatedBinding = yield* directory.getBinding(nextThreadId);@@ -74,10 +93,11 @@ it.layer(makeDirectoryLayer(SqlitePersistenceMemory))("ProviderSessionDirectoryL}const threadIds = yield* directory.listThreadIds();- assert.deepEqual(threadIds, [nextThreadId]);- }));+ expect(threadIds).toEqual(expect.arrayContaining([initialThreadId, nextThreadId]));+ }),+ );- it("persists runtime fields and merges payload updates", () =>+ it.effect("persists runtime fields and merges payload updates", () =>Effect.gen(function* () {const directory = yield* ProviderSessionDirectory;const runtimeRepository = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository;@@ -86,6 +106,7 @@ it.layer(makeDirectoryLayer(SqlitePersistenceMemory))("ProviderSessionDirectoryLyield* directory.upsert({provider: ProviderDriverKind.make("codex"),+ providerInstanceId: ProviderInstanceId.make("codex"),threadId,status: "starting",resumeCursor: {@@ -99,6 +120,7 @@ it.layer(makeDirectoryLayer(SqlitePersistenceMemory))("ProviderSessionDirectoryLyield* directory.upsert({provider: ProviderDriverKind.make("codex"),+ providerInstanceId: ProviderInstanceId.make("codex"),threadId,status: "running",runtimePayload: {@@ -120,9 +142,158 @@ it.layer(makeDirectoryLayer(SqlitePersistenceMemory))("ProviderSessionDirectoryLactiveTurnId: "turn-1",});}- }));+ }),+ );++ it.effect("keeps the existing binding when an insert conflicts", () =>+ Effect.gen(function* () {+ const directory = yield* ProviderSessionDirectory;+ const threadId = ThreadId.make("thread-insert-conflict");++ yield* directory.upsert({+ provider: ProviderDriverKind.make("codex"),+ providerInstanceId: ProviderInstanceId.make("codex"),+ threadId,+ status: "running",+ resumeCursor: { threadId: "active-provider-thread" },+ });++ yield* directory.upsert(+ {+ provider: ProviderDriverKind.make("codex"),+ providerInstanceId: ProviderInstanceId.make("codex"),+ threadId,+ status: "stopped",+ resumeCursor: { threadId: "stale-provider-thread" },+ },+ { onConflict: "ignore" },+ );++ const binding = yield* directory.getBinding(threadId);+ expect(Option.getOrThrow(binding)).toMatchObject({+ threadId,+ status: "running",+ resumeCursor: { threadId: "active-provider-thread" },+ });+ }),+ );++ it.effect("records source files without replacing the current provider session", () =>+ Effect.gen(function* () {+ const directory = yield* ProviderSessionDirectory;+ const repository = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository;+ const source = { ...importedSource, providerSessionId: "record-source" };+ const threadId = ThreadId.make(+ `import:${source.providerInstanceId}:${source.providerSessionId}`,+ );+ const runtimePayload = { cwd: "/tmp/project", activeTurnId: "active-turn" };+ yield* directory.upsert({+ threadId,+ provider: ProviderDriverKind.make("claudeAgent"),+ providerInstanceId: ProviderInstanceId.make("claude-current"),+ status: "running",+ resumeCursor: { resume: "current-native-session" },+ runtimePayload,+ });+ const before = Option.getOrThrow(yield* repository.getByThreadId({ threadId }));++ yield* directory.recordImportedTranscript({ threadId, source });+ const replacement = { ...source, size: 200, mtimeMs: 2_000 };+ yield* directory.recordImportedTranscript({ threadId, source: replacement });+ const secondFile = { ...source, filePath: "/tmp/provider-session-copy.jsonl" };+ yield* directory.recordImportedTranscript({ threadId, source: secondFile });++ expect(Option.getOrThrow(yield* repository.getByThreadId({ threadId }))).toEqual({+ ...before,+ runtimePayload: { ...runtimePayload, importedTranscripts: [replacement, secondFile] },+ });+ }),+ );++ it.effect("does not create a binding when recording an imported transcript", () =>+ Effect.gen(function* () {+ const directory = yield* ProviderSessionDirectory;+ const threadId = ThreadId.make("import:codex:missing-source-binding");++ yield* directory.recordImportedTranscript({ threadId, source: importedSource });++ expect(Option.isNone(yield* directory.getBinding(threadId))).toBe(true);+ }),+ );++ it.effect("keeps newly recorded sources when a runtime write uses a stale payload", () =>+ Effect.gen(function* () {+ const directory = yield* ProviderSessionDirectory;+ const repository = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository;+ const firstSource = { ...importedSource, providerSessionId: "stale-source" };+ const threadId = ThreadId.make(+ `import:${firstSource.providerInstanceId}:${firstSource.providerSessionId}`,+ );+ yield* directory.upsert({+ threadId,+ provider: ProviderDriverKind.make("codex"),+ providerInstanceId: ProviderInstanceId.make("codex"),+ status: "stopped",+ resumeCursor: { threadId: "original-native-session" },+ runtimePayload: { cwd: "/tmp/stale-source-project" },+ });+ yield* directory.recordImportedTranscript({ threadId, source: firstSource });+ const stale = Option.getOrThrow(yield* repository.getByThreadId({ threadId }));+ const secondSource = { ...firstSource, filePath: "/tmp/stale-source-copy.jsonl" };+ yield* directory.recordImportedTranscript({ threadId, source: secondSource });++ yield* repository.upsert({+ ...stale,+ status: "running",+ resumeCursor: { threadId: "new-native-session" },+ lastSeenAt: "2026-08-24T10:00:00.000Z",+ });++ expect(Option.getOrThrow(yield* repository.getByThreadId({ threadId }))).toEqual({+ ...stale,+ status: "running",+ resumeCursor: { threadId: "new-native-session" },+ lastSeenAt: "2026-08-24T10:00:00.000Z",+ runtimePayload: {+ cwd: "/tmp/stale-source-project",+ importedTranscripts: [firstSource, secondSource],+ },+ });+ }),+ );++ it.effect("reserves imported source records for the atomic recording method", () =>+ Effect.gen(function* () {+ const directory = yield* ProviderSessionDirectory;+ for (const onConflict of ["update", "ignore"] as const) {+ const source = { ...importedSource, providerSessionId: `reserved-source-${onConflict}` };+ const threadId = ThreadId.make(+ `import:${source.providerInstanceId}:${source.providerSessionId}`,+ );+ const binding = {+ threadId,+ provider: ProviderDriverKind.make("codex"),+ providerInstanceId: ProviderInstanceId.make("codex"),+ };+ yield* directory.upsert(+ { ...binding, runtimePayload: { cwd: "/tmp/project", importedTranscripts: [source] } },+ { onConflict },+ );+ expect(Option.getOrThrow(yield* directory.getBinding(threadId)).runtimePayload).toEqual({+ cwd: "/tmp/project",+ });++ yield* directory.recordImportedTranscript({ threadId, source });+ yield* directory.upsert({ ...binding, runtimePayload: null });++ expect(Option.getOrThrow(yield* directory.getBinding(threadId)).runtimePayload).toEqual({+ importedTranscripts: [source],+ });+ }+ }),+ );- it("lists persisted bindings with metadata in oldest-first order", () =>+ it.effect("lists persisted bindings with metadata in oldest-first order", () =>Effect.gen(function* () {const directory = yield* ProviderSessionDirectory;const runtimeRepository = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository;@@ -162,12 +333,15 @@ it.layer(makeDirectoryLayer(SqlitePersistenceMemory))("ProviderSessionDirectoryL},});- const bindings = yield* directory.listBindings();+ const bindings = (yield* directory.listBindings()).filter(+ (binding) => binding.threadId === olderThreadId || binding.threadId === newerThreadId,+ );assert.deepEqual(bindings, [{threadId: olderThreadId,provider: ProviderDriverKind.make("claudeAgent"),+ providerInstanceId: ProviderInstanceId.make("claudeAgent"),adapterKey: "claudeAgent",runtimeMode: "approval-required",status: "starting",@@ -182,6 +356,7 @@ it.layer(makeDirectoryLayer(SqlitePersistenceMemory))("ProviderSessionDirectoryL{threadId: newerThreadId,provider: ProviderDriverKind.make("codex"),+ providerInstanceId: ProviderInstanceId.make("codex"),adapterKey: "codex",runtimeMode: "full-access",status: "running",@@ -194,40 +369,45 @@ it.layer(makeDirectoryLayer(SqlitePersistenceMemory))("ProviderSessionDirectoryL},},]);- }));+ }),+ );- it("resets adapterKey to the new provider when provider changes without an explicit adapter key", () =>- Effect.gen(function* () {- const directory = yield* ProviderSessionDirectory;- const runtimeRepository = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository;- const threadId = ThreadId.make("thread-provider-change");+ it.effect(+ "resets adapterKey to the new provider when provider changes without an explicit adapter key",+ () =>+ Effect.gen(function* () {+ const directory = yield* ProviderSessionDirectory;+ const runtimeRepository = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository;+ const threadId = ThreadId.make("thread-provider-change");- yield* runtimeRepository.upsert({- threadId,- providerName: "claudeAgent",- providerInstanceId: null,- adapterKey: "claudeAgent",- runtimeMode: "full-access",- status: "running",- lastSeenAt: "2026-01-01T00:00:00.000Z",- resumeCursor: null,- runtimePayload: null,- });+ yield* runtimeRepository.upsert({+ threadId,+ providerName: "claudeAgent",+ providerInstanceId: null,+ adapterKey: "claudeAgent",+ runtimeMode: "full-access",+ status: "running",+ lastSeenAt: "2026-01-01T00:00:00.000Z",+ resumeCursor: null,+ runtimePayload: null,+ });- yield* directory.upsert({- provider: ProviderDriverKind.make("codex"),- threadId,- });+ yield* directory.upsert({+ provider: ProviderDriverKind.make("codex"),+ providerInstanceId: ProviderInstanceId.make("codex"),+ threadId,+ });- const runtime = yield* runtimeRepository.getByThreadId({ threadId });- assert.equal(Option.isSome(runtime), true);- if (Option.isSome(runtime)) {- assert.equal(runtime.value.providerName, "codex");- assert.equal(runtime.value.adapterKey, "codex");- }- }));+ const runtime = yield* runtimeRepository.getByThreadId({ threadId });+ assert.equal(Option.isSome(runtime), true);+ if (Option.isSome(runtime)) {+ assert.equal(runtime.value.providerName, "codex");+ assert.equal(runtime.value.adapterKey, "codex");+ }+ }),+ );- it("rehydrates persisted mappings across layer restart", () =>+ it.effect("rehydrates persisted mappings across layer restart", () =>Effect.gen(function* () {const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-provider-directory-"));const dbPath = NodePath.join(tempDir, "orchestration.sqlite");@@ -239,6 +419,7 @@ it.layer(makeDirectoryLayer(SqlitePersistenceMemory))("ProviderSessionDirectoryLconst directory = yield* ProviderSessionDirectory;yield* directory.upsert({provider: ProviderDriverKind.make("codex"),+ providerInstanceId: ProviderInstanceId.make("codex"),threadId,});}).pipe(Effect.provide(directoryLayer));@@ -250,7 +431,7 @@ it.layer(makeDirectoryLayer(SqlitePersistenceMemory))("ProviderSessionDirectoryLassert.equal(provider, "codex");const resolvedBinding = yield* directory.getBinding(threadId);- assertSome(resolvedBinding, {+ expect(Option.getOrThrow(resolvedBinding)).toMatchObject({threadId,provider: ProviderDriverKind.make("codex"),});@@ -267,5 +448,6 @@ it.layer(makeDirectoryLayer(SqlitePersistenceMemory))("ProviderSessionDirectoryL}).pipe(Effect.provide(directoryLayer));NodeFS.rmSync(tempDir, { recursive: true, force: true });- }));+ }),+ );});diff --git a/apps/server/src/provider/Layers/ProviderSessionDirectory.ts b/apps/server/src/provider/Layers/ProviderSessionDirectory.tsindex 253a954d210..29ec8d2ed16 100644--- a/apps/server/src/provider/Layers/ProviderSessionDirectory.ts+++ b/apps/server/src/provider/Layers/ProviderSessionDirectory.ts@@ -100,7 +100,7 @@ const makeProviderSessionDirectory = Effect.gen(function* () {),);- const upsert: ProviderSessionDirectoryShape["upsert"] = Effect.fn(function* (binding) {+ const upsert: ProviderSessionDirectoryShape["upsert"] = Effect.fn(function* (binding, options) {const existing = yield* repository.getByThreadId({ threadId: binding.threadId }).pipe(Effect.mapError(toPersistenceError("ProviderSessionDirectory.upsert:getByThreadId")));@@ -126,25 +126,30 @@ const makeProviderSessionDirectory = Effect.gen(function* () {});}yield* repository- .upsert({- threadId: resolvedThreadId,- providerName: binding.provider,- providerInstanceId,- adapterKey:- binding.adapterKey ??- (providerChanged ? binding.provider : (existingRuntime?.adapterKey ?? binding.provider)),- runtimeMode: binding.runtimeMode ?? existingRuntime?.runtimeMode ?? "full-access",- status: binding.status ?? existingRuntime?.status ?? "running",- lastSeenAt: now,- resumeCursor:- binding.resumeCursor !== undefined- ? binding.resumeCursor- : (existingRuntime?.resumeCursor ?? null),- runtimePayload: mergeRuntimePayload(- existingRuntime?.runtimePayload ?? null,- binding.runtimePayload,- ),- })+ .upsert(+ {+ threadId: resolvedThreadId,+ providerName: binding.provider,+ providerInstanceId,+ adapterKey:+ binding.adapterKey ??+ (providerChanged+ ? binding.provider+ : (existingRuntime?.adapterKey ?? binding.provider)),+ runtimeMode: binding.runtimeMode ?? existingRuntime?.runtimeMode ?? "full-access",+ status: binding.status ?? existingRuntime?.status ?? "running",+ lastSeenAt: now,+ resumeCursor:+ binding.resumeCursor !== undefined+ ? binding.resumeCursor+ : (existingRuntime?.resumeCursor ?? null),+ runtimePayload: mergeRuntimePayload(+ existingRuntime?.runtimePayload ?? null,+ binding.runtimePayload,+ ),+ },+ options,+ ).pipe(Effect.mapError(toPersistenceError("ProviderSessionDirectory.upsert:upsert")));});@@ -164,6 +169,15 @@ const makeProviderSessionDirectory = Effect.gen(function* () {),);+ const recordImportedTranscript: ProviderSessionDirectoryShape["recordImportedTranscript"] = (+ input,+ ) =>+ repository+ .recordImportedTranscript(input)+ .pipe(+ Effect.mapError(toPersistenceError("ProviderSessionDirectory.recordImportedTranscript")),+ );+const listThreadIds: ProviderSessionDirectoryShape["listThreadIds"] = () =>repository.list().pipe(Effect.mapError(toPersistenceError("ProviderSessionDirectory.listThreadIds:list")),@@ -184,6 +198,7 @@ const makeProviderSessionDirectory = Effect.gen(function* () {return {upsert,+ recordImportedTranscript,getProvider,getBinding,listThreadIds,diff --git a/apps/server/src/provider/Layers/ProviderSessionReaper.test.ts b/apps/server/src/provider/Layers/ProviderSessionReaper.test.tsindex 1777544d8fb..c680a8228e3 100644--- a/apps/server/src/provider/Layers/ProviderSessionReaper.test.ts+++ b/apps/server/src/provider/Layers/ProviderSessionReaper.test.ts@@ -217,6 +217,7 @@ describe("ProviderSessionReaper", () => {getActiveProjectByWorkspaceRoot: () => Effect.die("unused"),getProjectShellById: () => Effect.die("unused"),getFirstActiveThreadIdByProjectId: () => Effect.die("unused"),+ getImportedAgentSessionSources: () => Effect.die("unused"),getThreadCheckpointContext: () => Effect.die("unused"),getFullThreadDiffContext: () => Effect.die("unused"),getThreadRuntimeContext: () => Effect.die("unused"),diff --git a/apps/server/src/provider/ProviderInstanceEnvironment.test.ts b/apps/server/src/provider/ProviderInstanceEnvironment.test.tsindex 7ac3f2f2837..7d6bbe61a2a 100644--- a/apps/server/src/provider/ProviderInstanceEnvironment.test.ts+++ b/apps/server/src/provider/ProviderInstanceEnvironment.test.ts@@ -1,8 +1,55 @@-import { describe, expect, it } from "vite-plus/test";+import * as NodeOS from "node:os";++import * as NodeServices from "@effect/platform-node/NodeServices";+import { describe, expect, it } from "@effect/vitest";+import * as Effect from "effect/Effect";+import * as Path from "effect/Path";import { mergeProviderInstanceEnvironment } from "./ProviderInstanceEnvironment.ts";describe("mergeProviderInstanceEnvironment", () => {+ it.effect.each([+ { value: "~/.account", tail: ".account" },+ { value: "~\\.account\\work", tail: ".account\\work" },+ ])("expands configured provider homes set to $value", ({ value, tail }) =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const baseEnv = {+ CODEX_HOME: "~/.inherited-codex",+ CLAUDE_CONFIG_DIR: "~/.inherited-claude",+ };+ const environment = mergeProviderInstanceEnvironment(+ [+ { name: "CODEX_HOME", value, sensitive: false },+ { name: "CLAUDE_CONFIG_DIR", value, sensitive: false },+ { name: "CUSTOM_VALUE", value, sensitive: false },+ ],+ baseEnv,+ );++ expect(environment).toEqual({+ CODEX_HOME: path.join(NodeOS.homedir(), tail),+ CLAUDE_CONFIG_DIR: path.join(NodeOS.homedir(), tail),+ CUSTOM_VALUE: value,+ });+ expect(baseEnv).toEqual({+ CODEX_HOME: "~/.inherited-codex",+ CLAUDE_CONFIG_DIR: "~/.inherited-claude",+ });+ }).pipe(Effect.provide(NodeServices.layer)),+ );++ it("leaves inherited provider homes unchanged", () => {+ const baseEnv = { CODEX_HOME: "~/.codex", CLAUDE_CONFIG_DIR: "~\\.claude" };++ expect(+ mergeProviderInstanceEnvironment(+ [{ name: "CUSTOM_VALUE", value: "~/.custom", sensitive: false }],+ baseEnv,+ ),+ ).toEqual({ ...baseEnv, CUSTOM_VALUE: "~/.custom" });+ });+it("overrides inherited environment values and preserves empty strings", () => {expect(mergeProviderInstanceEnvironment(diff --git a/apps/server/src/provider/ProviderInstanceEnvironment.ts b/apps/server/src/provider/ProviderInstanceEnvironment.tsindex e469253604e..77c0c6c2dc8 100644--- a/apps/server/src/provider/ProviderInstanceEnvironment.ts+++ b/apps/server/src/provider/ProviderInstanceEnvironment.ts@@ -1,5 +1,7 @@import type { ProviderInstanceEnvironment } from "@t3tools/contracts";+import { expandHomePath } from "../pathExpansion.ts";+export function mergeProviderInstanceEnvironment(environment: ProviderInstanceEnvironment | undefined,baseEnv: NodeJS.ProcessEnv = process.env,@@ -10,7 +12,11 @@ export function mergeProviderInstanceEnvironment(const next: NodeJS.ProcessEnv = { ...baseEnv };for (const variable of environment) {- next[variable.name] = variable.value;+ // Child processes do not apply shell expansion to environment values.+ next[variable.name] =+ variable.name === "CODEX_HOME" || variable.name === "CLAUDE_CONFIG_DIR"+ ? expandHomePath(variable.value)+ : variable.value;}return next;}diff --git a/apps/server/src/provider/Services/ProviderSessionDirectory.ts b/apps/server/src/provider/Services/ProviderSessionDirectory.tsindex f2dd4323f7a..9dbafd3e804 100644--- a/apps/server/src/provider/Services/ProviderSessionDirectory.ts+++ b/apps/server/src/provider/Services/ProviderSessionDirectory.ts@@ -1,4 +1,5 @@import type {+ AgentSessionImportSource,ProviderInstanceId,ProviderDriverKind,ProviderSessionRuntimeStatus,@@ -40,11 +41,22 @@ export type ProviderSessionDirectoryWriteError =| ProviderValidationError| ProviderSessionDirectoryPersistenceError;+export interface ProviderSessionDirectoryUpsertOptions {+ readonly onConflict?: "update" | "ignore";+}+export interface ProviderSessionDirectoryShape {readonly upsert: (binding: ProviderRuntimeBinding,+ options?: ProviderSessionDirectoryUpsertOptions,) => Effect.Effect<void, ProviderSessionDirectoryWriteError>;+ /** Record an imported file without changing the current provider session. */+ readonly recordImportedTranscript: (input: {+ readonly threadId: ThreadId;+ readonly source: AgentSessionImportSource;+ }) => Effect.Effect<void, ProviderSessionDirectoryPersistenceError>;+readonly getProvider: (threadId: ThreadId,) => Effect.Effect<ProviderDriverKind, ProviderSessionDirectoryReadError>;diff --git a/apps/server/src/provider/testFixtures/codexCollabMockPeer.mjs b/apps/server/src/provider/testFixtures/codexCollabMockPeer.mjsindex 4e6d1b26194..fa567d75cf8 100644--- a/apps/server/src/provider/testFixtures/codexCollabMockPeer.mjs+++ b/apps/server/src/provider/testFixtures/codexCollabMockPeer.mjs@@ -57,6 +57,14 @@ rl.on("line", (line) => {});return;}+ if (method === "account/read") {+ write({ id, result: { account: { type: "apiKey" }, requiresOpenaiAuth: false } });+ return;+ }+ if (method === "skills/list" || method === "model/list") {+ write({ id, result: { data: [] } });+ return;+ }if (method === "thread/start") {write({ id, result: fixture.responses.threadStart });return;diff --git a/apps/server/src/relay/AgentAwarenessRelay.test.ts b/apps/server/src/relay/AgentAwarenessRelay.test.tsindex ee23cbffaf0..59049500729 100644--- a/apps/server/src/relay/AgentAwarenessRelay.test.ts+++ b/apps/server/src/relay/AgentAwarenessRelay.test.ts@@ -138,7 +138,7 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {expect(AgentAwarenessRelay.eventThreadId(event)).toBe(threadId);});- it("does not publish start intents, streaming content, or non-awareness activity events", () => {+ it("does not publish imported, start-intent, streaming, or non-awareness events", () => {const now = "2026-05-25T00:00:00.000Z";const base = {sequence: 1,@@ -147,6 +147,7 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {aggregateKind: "thread",aggregateId: "thread-1" as ThreadId,occurredAt: now,+ metadata: {},};expect(@@ -202,6 +203,36 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {},} as unknown as OrchestrationEvent),).toBe(false);+ expect(+ AgentAwarenessRelay.shouldPublishAgentAwarenessEvent({+ ...base,+ type: "thread.created",+ metadata: { historyImport: true },+ payload: { threadId: "thread-1" as ThreadId },+ } as unknown as OrchestrationEvent),+ ).toBe(false);+ expect(+ AgentAwarenessRelay.shouldPublishAgentAwarenessEvent({+ ...base,+ type: "thread.settled",+ metadata: { historyImport: true },+ payload: { threadId: "thread-1" as ThreadId },+ } as unknown as OrchestrationEvent),+ ).toBe(false);+ expect(+ AgentAwarenessRelay.shouldPublishAgentAwarenessEvent({+ ...base,+ type: "thread.created",+ payload: { threadId: "thread-1" as ThreadId },+ } as unknown as OrchestrationEvent),+ ).toBe(true);+ expect(+ AgentAwarenessRelay.shouldPublishAgentAwarenessEvent({+ ...base,+ type: "thread.settled",+ payload: { threadId: "thread-1" as ThreadId },+ } as unknown as OrchestrationEvent),+ ).toBe(true);});it("deduplicates awareness state updates whose only change is their event timestamp", () => {@@ -400,17 +431,32 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {}),);- it.effect("keeps the orchestration listener armed until relay config is installed", () =>+ it.effect("keeps the listener armed and skips imported thread work", () =>Effect.scoped(Effect.gen(function* () {const events = yield* Queue.unbounded<OrchestrationEvent>();const threadShellRequested = yield* Deferred.make<void>();+ const releaseThreadShell = yield* Deferred.make<void>();+ const threadShellRequests: Array<ThreadId> = [];+ let fetchCallCount = 0;const secrets = makeMemorySecretStore();const now = "2026-05-25T00:00:00.000Z";const projectId = "project-1" as ProjectId;const threadId = "thread-1" as ThreadId;+ const importedThreadId = "import:codex:session-1" as ThreadId;const environmentId = "env-1" as EnvironmentId;+ const originalFetch = globalThis.fetch;+ globalThis.fetch = (() => {+ fetchCallCount += 1;+ return Promise.resolve(Response.json({ ok: true, deliveries: [] }));+ }) as unknown as typeof fetch;+ yield* Effect.addFinalizer(() =>+ Effect.sync(() => {+ globalThis.fetch = originalFetch;+ }),+ );+const project = {id: projectId,title: "T3 Code",@@ -473,15 +519,18 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {getShellSnapshot: () =>Effect.succeed({snapshotSequence: 1,- projects: [project],- threads: [thread],+ projects: [],+ threads: [],updatedAt: now,} satisfies OrchestrationShellSnapshot),- getThreadShellById: () =>- Deferred.succeed(threadShellRequested, undefined).pipe(- Effect.ignore,- Effect.as(Option.some(thread)),- ),+ getThreadShellById: (requestedThreadId: ThreadId) =>+ Effect.gen(function* () {+ threadShellRequests.push(requestedThreadId);+ if (requestedThreadId !== threadId) return Option.none();+ yield* Deferred.succeed(threadShellRequested, undefined);+ yield* Deferred.await(releaseThreadShell);+ return Option.some(thread);+ }),getProjectShellById: () => Effect.succeed(Option.some(project)),} as unknown as ProjectionSnapshotQueryShape;@@ -511,17 +560,40 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {yield* Effect.gen(function* () {const relay = yield* AgentAwarenessRelay.AgentAwarenessRelay;yield* relay.start();- yield* secrets.setString(RELAY_URL_SECRET, "http://127.0.0.1:1");+ yield* secrets.setString(RELAY_URL_SECRET, "https://relay.example.test");yield* secrets.setString(RELAY_ENVIRONMENT_CREDENTIAL_SECRET, "relay-credential");yield* secrets.setString(PUBLISH_AGENT_ACTIVITY_SECRET, "true");yield* Queue.offer(events, {- type: "thread.activity-appended",+ type: "thread.created",sequence: 1,+ eventId: "evt-import-created",+ commandId: CommandId.make("cmd-import-created"),+ aggregateKind: "thread",+ aggregateId: importedThreadId,+ metadata: { historyImport: true },+ payload: { threadId: importedThreadId },+ occurredAt: now,+ } as unknown as OrchestrationEvent);+ yield* Queue.offer(events, {+ type: "thread.settled",+ sequence: 2,+ eventId: "evt-import-settled",+ commandId: CommandId.make("cmd-import-settled"),+ aggregateKind: "thread",+ aggregateId: importedThreadId,+ metadata: { historyImport: true },+ payload: { threadId: importedThreadId },+ occurredAt: now,+ } as unknown as OrchestrationEvent);+ yield* Queue.offer(events, {+ type: "thread.activity-appended",+ sequence: 3,eventId: "evt-1",commandId: CommandId.make("cmd-1"),aggregateKind: "thread",aggregateId: threadId,actor: { kind: "server" },+ metadata: {},payload: {threadId,activity: {@@ -532,6 +604,9 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {} as unknown as OrchestrationEvent);yield* Deferred.await(threadShellRequested).pipe(Effect.timeout("2 seconds"));+ expect(threadShellRequests).toEqual([threadId]);+ expect(fetchCallCount).toBe(0);+ yield* Deferred.succeed(releaseThreadShell, undefined);}).pipe(Effect.provide(AgentAwarenessRelay.layer.pipe(@@ -692,6 +767,7 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {aggregateKind: "thread",aggregateId: threadId,actor: { kind: "server" },+ metadata: {},payload: {threadId,activity: {diff --git a/apps/server/src/relay/AgentAwarenessRelay.ts b/apps/server/src/relay/AgentAwarenessRelay.tsindex 3dd0df642ce..8d7b9e98361 100644--- a/apps/server/src/relay/AgentAwarenessRelay.ts+++ b/apps/server/src/relay/AgentAwarenessRelay.ts@@ -67,6 +67,9 @@ export function eventThreadId(event: OrchestrationEvent): ThreadId | null {}export function shouldPublishAgentAwarenessEvent(event: OrchestrationEvent): boolean {+ if (event.metadata.historyImport === true) {+ return false;+ }switch (event.type) {case "thread.message-sent":case "thread.turn-start-requested":diff --git a/apps/server/src/server.test.ts b/apps/server/src/server.test.tsindex bebde7eded7..cfebfcf157c 100644--- a/apps/server/src/server.test.ts+++ b/apps/server/src/server.test.ts@@ -36,6 +36,7 @@ import {type ProviderInstallState,ProviderSetupError,ResolvedKeybindingRule,+ type ServerLifecycleStreamEvent,ThreadId,TurnId,WS_METHODS,@@ -91,6 +92,7 @@ const decodeTransferThreadSnapshot = Schema.decodeUnknownEffect(const decodeTransferShellSnapshot = Schema.decodeUnknownEffect(Schema.fromJsonString(OrchestrationShellSnapshot),);+const encodeTestJson = Schema.encodeUnknownSync(Schema.fromJsonString(Schema.Unknown));import * as BackgroundPolicy from "./background/BackgroundPolicy.ts";import * as ServerConfig from "./config.ts";@@ -127,6 +129,7 @@ import {AntigravityInstallationError,} from "./provider/AntigravityInstallation.ts";import type { ProviderInstance } from "./provider/ProviderDriver.ts";+import * as ProviderSessionDirectory from "./provider/Services/ProviderSessionDirectory.ts";import { ProviderAdapterRequestError } from "./provider/Errors.ts";import { makeManualOnlyProviderMaintenanceCapabilities } from "./provider/providerMaintenance.ts";import * as ServerLifecycleEvents from "./serverLifecycleEvents.ts";@@ -513,6 +516,9 @@ const buildAppUnderTest = (options?: {projectSetupScriptRunner?: Partial<ProjectSetupScriptRunner.ProjectSetupScriptRunner["Service"]>;+ providerSessionDirectory?: Partial<+ ProviderSessionDirectory.ProviderSessionDirectory["Service"]+ >;terminalManager?: Partial<TerminalManager.TerminalManager["Service"]>;orchestrationEngine?: Partial<OrchestrationEngine.OrchestrationEngineService["Service"]>;threadDeletionReactor?: Partial<ThreadDeletionReactor["Service"]>;@@ -785,6 +791,13 @@ const buildAppUnderTest = (options?: {managedDirectory: "unused-test-antigravity-runtime",...options?.layers?.antigravityInstallation,}),+ Layer.mock(ProviderSessionDirectory.ProviderSessionDirectory)({+ upsert: () => Effect.void,+ getBinding: () => Effect.succeed(Option.none()),+ listThreadIds: () => Effect.succeed([]),+ listBindings: () => Effect.succeed([]),+ ...options?.layers?.providerSessionDirectory,+ }),),),Layer.provide(@@ -973,6 +986,7 @@ const buildAppUnderTest = (options?: {}),getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),+ getImportedAgentSessionSources: () => Effect.succeed([]),getThreadCheckpointContext: () => Effect.succeed(Option.none()),...options?.layers?.projectionSnapshotQuery,}),@@ -5332,6 +5346,103 @@ it.layer(NodeServices.layer)("server router seam", (it) => {}).pipe(Effect.provide(NodeHttpServer.layerTest)),);+ it.effect("keeps agent session import project failures structured over websocket rpc", () =>+ Effect.gen(function* () {+ yield* buildAppUnderTest();++ const projectId = ProjectId.make("missing-import-project");+ const wsUrl = yield* getWsServerUrl("/ws");+ const error = yield* Effect.scoped(+ withWsRpcClient(wsUrl, (client) =>+ client[WS_METHODS.agentSessionsImport]({ projectId }).pipe(Effect.flip),+ ),+ );++ assert.equal(error._tag, "AgentSessionImportProjectNotFoundError");+ if (error._tag === "AgentSessionImportProjectNotFoundError") {+ assert.equal(error.projectId, projectId);+ }+ }).pipe(Effect.provide(NodeHttpServer.layerTest)),+ );++ it.effect("returns scanner skip counts over websocket rpc", () =>+ Effect.gen(function* () {+ const fileSystem = yield* FileSystem.FileSystem;+ const path = yield* Path.Path;+ const codexHome = yield* fileSystem.makeTempDirectoryScoped({+ prefix: "t3-agent-import-rpc-codex-",+ });+ const workspaceRoot = yield* fileSystem.makeTempDirectoryScoped({+ prefix: "t3-agent-import-rpc-workspace-",+ });+ const transcriptDirectory = path.join(codexHome, "sessions", "2026", "08", "31");+ const transcriptPath = path.join(transcriptDirectory, "rollout-skipped.jsonl");+ yield* fileSystem.makeDirectory(transcriptDirectory, { recursive: true });+ yield* fileSystem.writeFileString(+ transcriptPath,+ encodeTestJson({+ timestamp: "2026-08-31T12:00:00.000Z",+ type: "session_meta",+ payload: { id: "rpc-skipped-session", cwd: workspaceRoot },+ }),+ );+ yield* fileSystem.utimes(transcriptPath, 0, 0);++ const projectId = ProjectId.make("agent-import-rpc-project");+ const project = {+ id: projectId,+ title: "Agent import RPC",+ workspaceRoot,+ defaultModelSelection: null,+ scripts: [],+ createdAt: "2026-08-31T12:00:00.000Z",+ updatedAt: "2026-08-31T12:00:00.000Z",+ } as const;+ yield* buildAppUnderTest({+ layers: {+ serverSettings: {+ getSettings: Effect.succeed({+ ...DEFAULT_SERVER_SETTINGS,+ providerInstances: {+ [ProviderInstanceId.make("codex")]: {+ driver: ProviderDriverKind.make("codex"),+ config: { homePath: codexHome },+ },+ [ProviderInstanceId.make("claudeAgent")]: {+ driver: ProviderDriverKind.make("claudeAgent"),+ enabled: false,+ config: {},+ },+ },+ }),+ },+ projectionSnapshotQuery: {+ getProjectShellById: (requestedProjectId) =>+ Effect.succeed(+ requestedProjectId === projectId ? Option.some(project) : Option.none(),+ ),+ },+ },+ });++ const wsUrl = yield* getWsServerUrl("/ws");+ const result = yield* Effect.scoped(+ withWsRpcClient(wsUrl, (client) =>+ Effect.gen(function* () {+ const scan = yield* client[WS_METHODS.agentSessionsScan]({});+ assert.deepEqual(+ scan.candidates.map((candidate) => candidate.path),+ [workspaceRoot],+ );+ return yield* client[WS_METHODS.agentSessionsImport]({ projectId });+ }),+ ),+ );++ assert.deepEqual(result, { importedCount: 0, skippedCount: 1 });+ }).pipe(Effect.provide(NodeHttpServer.layerTest)),+ );+it.effect("uploads Codex thread feedback through websocket rpc", () =>Effect.gen(function* () {const input = {@@ -6239,6 +6350,98 @@ it.layer(NodeServices.layer)("server router seam", (it) => {}).pipe(Effect.provide(NodeHttpServer.layerTest)),);+ it.effect("subscribeServerLifecycle buffers updates published during snapshot capture", () =>+ Effect.gen(function* () {+ const pubsub = yield* PubSub.unbounded<ServerLifecycleStreamEvent>();+ const streamSubscribed = yield* Deferred.make<void>();+ const snapshotPublished = yield* Deferred.make<void>();+ const bootstrapProjectId = ProjectId.make("project-bootstrap");+ const bootstrapThreadId = ThreadId.make("thread-bootstrap");+ const snapshotEvent = {+ version: 1 as const,+ sequence: 1,+ type: "welcome" as const,+ payload: {+ environment: testEnvironmentDescriptor,+ cwd: "/tmp/project",+ projectName: "project",+ bootstrapStatus: "pending" as const,+ },+ };+ const gapEvent = {+ version: 1 as const,+ sequence: 2,+ type: "welcome" as const,+ payload: {+ environment: testEnvironmentDescriptor,+ cwd: "/tmp/project",+ projectName: "project",+ bootstrapStatus: "complete" as const,+ bootstrapProjectId,+ bootstrapThreadId,+ bootstrapProjectCreated: true,+ bootstrapThreadCreated: true,+ },+ };+ const sentinelEvent = {+ version: 1 as const,+ sequence: 3,+ type: "ready" as const,+ payload: { at: "2026-01-01T00:00:01.000Z", environment: testEnvironmentDescriptor },+ };+ const liveStream = Stream.unwrap(+ Effect.gen(function* () {+ const subscription = yield* PubSub.subscribe(pubsub);+ yield* Deferred.succeed(streamSubscribed, undefined);+ return Stream.fromSubscription(subscription);+ }),+ );++ yield* buildAppUnderTest({+ layers: {+ serverLifecycleEvents: {+ snapshot: PubSub.publish(pubsub, gapEvent).pipe(+ Effect.andThen(Deferred.succeed(snapshotPublished, undefined)),+ Effect.as({ sequence: 1, events: [snapshotEvent] }),+ ),+ stream: liveStream,+ },+ },+ });++ yield* Effect.gen(function* () {+ yield* Deferred.await(snapshotPublished);+ yield* Deferred.await(streamSubscribed);+ yield* PubSub.publish(pubsub, sentinelEvent);+ }).pipe(Effect.forkScoped);++ const wsUrl = yield* getWsServerUrl("/ws");+ const events = yield* Effect.scoped(+ withWsRpcClient(wsUrl, (client) =>+ client[WS_METHODS.subscribeServerLifecycle]({}).pipe(Stream.take(2), Stream.runCollect),+ ),+ );++ const [first, second] = Array.from(events);+ assert.equal(first?.type, "welcome");+ assert.equal(first?.sequence, 1);+ if (first?.type !== "welcome") {+ assert.fail("expected the pending bootstrap event");+ }+ assert.equal(first.payload.bootstrapStatus, "pending");+ assert.equal(second?.type, "welcome");+ assert.equal(second?.sequence, 2);+ if (second?.type !== "welcome") {+ assert.fail("expected the bootstrap completion event");+ }+ assert.equal(second.payload.bootstrapStatus, "complete");+ assert.equal(second.payload.bootstrapProjectId, bootstrapProjectId);+ assert.equal(second.payload.bootstrapThreadId, bootstrapThreadId);+ assert.equal(second.payload.bootstrapProjectCreated, true);+ assert.equal(second.payload.bootstrapThreadCreated, true);+ }).pipe(Effect.provide(NodeHttpServer.layerTest)),+ );+it.effect("routes websocket rpc projects.searchEntries", () =>Effect.gen(function* () {const fs = yield* FileSystem.FileSystem;diff --git a/apps/server/src/serverRuntimeStartup.reconcile.test.ts b/apps/server/src/serverRuntimeStartup.reconcile.test.tsindex 2c95a6163ac..e46c2a8ca01 100644--- a/apps/server/src/serverRuntimeStartup.reconcile.test.ts+++ b/apps/server/src/serverRuntimeStartup.reconcile.test.ts@@ -151,6 +151,7 @@ it.effect("marks active running sessions that have persisted resume state", () =),),upsert: (binding) => Effect.sync(() => upserts.push(binding)),+ recordImportedTranscript: () => Effect.die("unused"),getProvider: () => Effect.die("unused"),listThreadIds: () => Effect.die("unused"),listBindings: () => Effect.succeed([]),@@ -265,6 +266,7 @@ it.effect.each(["marked update", "opt-in restart"] as const)(firstMarkerCleared ? Deferred.succeed(continuationCleared, undefined) : Effect.void,),),+ recordImportedTranscript: () => Effect.die("unused"),getProvider: () => Effect.die("unused"),listThreadIds: () => Effect.die("unused"),listBindings: () => Effect.succeed([]),@@ -391,6 +393,7 @@ it.effect("does not continue archived or deleted marked sessions", () => {);},upsert: () => Effect.void,+ recordImportedTranscript: () => Effect.die("unused"),getProvider: () => Effect.die("unused"),listThreadIds: () => Effect.die("unused"),listBindings: () => Effect.succeed([]),@@ -446,6 +449,7 @@ it.effect("retries continuation preparation before settling a persistent failure}),),upsert: () => Effect.void,+ recordImportedTranscript: () => Effect.die("unused"),getProvider: () => Effect.die("unused"),listThreadIds: () => Effect.die("unused"),listBindings: () => Effect.succeed([]),@@ -517,6 +521,7 @@ it.effect("reconciles multiple active and archived orphans but skips live sessio),),upsert: (binding) => Effect.sync(() => upserts.push(binding)),+ recordImportedTranscript: () => Effect.die("unused"),getProvider: () => Effect.die("unused"),listThreadIds: () => Effect.die("unused"),listBindings: () => Effect.succeed([]),@@ -596,6 +601,7 @@ it.effect(}),),upsert: () => Effect.fail(writeFailure),+ recordImportedTranscript: () => Effect.die("unused"),getProvider: () => Effect.die("unused"),listThreadIds: () => Effect.die("unused"),listBindings: () => Effect.succeed([]),@@ -633,6 +639,7 @@ it.effect("retries failed projections and continues after a persistent failure",directory: {getBinding: () => Effect.succeed(Option.none()),upsert: () => Effect.void,+ recordImportedTranscript: () => Effect.die("unused"),getProvider: () => Effect.die("unused"),listThreadIds: () => Effect.die("unused"),listBindings: () => Effect.succeed([]),@@ -682,6 +689,7 @@ it.effect("does not fail startup when the live provider session inventory cannotEffect.provideService(ProviderSessionDirectory.ProviderSessionDirectory, {getBinding: () => Effect.die("unused"),upsert: () => Effect.die("unused"),+ recordImportedTranscript: () => Effect.die("unused"),getProvider: () => Effect.die("unused"),listThreadIds: () => Effect.die("unused"),listBindings: () => Effect.succeed([]),@@ -754,6 +762,7 @@ for (const scenario of [Effect.sync(() => {upserts.push(binding);}),+ recordImportedTranscript: () => Effect.die("unused"),getProvider: () => Effect.die("unused"),listThreadIds: () => Effect.die("unused"),listBindings: () => Effect.succeed([]),@@ -827,6 +836,7 @@ for (const preparedStatus of [if (binding.status !== "starting" || sends.length === 0) return;yield* Deferred.succeed(cleared, undefined);}),+ recordImportedTranscript: () => Effect.die("unused"),getProvider: () => Effect.die("unused"),listThreadIds: () => Effect.die("unused"),listBindings: () =>@@ -932,6 +942,7 @@ it.effect("settles failed opt-in recovery without retrying the provider turn", (Effect.sync(() => {binding = next;}),+ recordImportedTranscript: () => Effect.die("unused"),getProvider: () => Effect.die("unused"),listThreadIds: () => Effect.die("unused"),listBindings: () => Effect.succeed([]),diff --git a/apps/server/src/serverRuntimeStartup.test.ts b/apps/server/src/serverRuntimeStartup.test.tsindex fddf618cb13..8cba5255227 100644--- a/apps/server/src/serverRuntimeStartup.test.ts+++ b/apps/server/src/serverRuntimeStartup.test.ts@@ -155,6 +155,7 @@ it.effect("resolveAutoBootstrapWelcomeTargets returns existing project and threa),getProjectShellById: () => Effect.die("unused"),getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.some(bootstrapThreadId)),+ getImportedAgentSessionSources: () => Effect.die("unused"),getThreadCheckpointContext: () => Effect.succeed(Option.none()),getFullThreadDiffContext: () => Effect.succeed(Option.none()),getThreadRuntimeContext: () => Effect.die("unused"),@@ -181,6 +182,8 @@ it.effect("resolveAutoBootstrapWelcomeTargets returns existing project and threaassert.deepStrictEqual(targets, {bootstrapProjectId,bootstrapThreadId,+ bootstrapProjectCreated: false,+ bootstrapThreadCreated: false,});assert.deepStrictEqual(yield* Ref.get(dispatchCalls), []);});@@ -212,6 +215,7 @@ it.effect("resolveAutoBootstrapWelcomeTargets creates a project and thread whengetActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),getProjectShellById: () => Effect.die("unused"),getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),+ getImportedAgentSessionSources: () => Effect.die("unused"),getThreadCheckpointContext: () => Effect.succeed(Option.none()),getFullThreadDiffContext: () => Effect.succeed(Option.none()),getThreadRuntimeContext: () => Effect.die("unused"),@@ -237,6 +241,8 @@ it.effect("resolveAutoBootstrapWelcomeTargets creates a project and thread whenassert.equal(typeof targets.bootstrapProjectId, "string");assert.equal(typeof targets.bootstrapThreadId, "string");+ assert.equal(targets.bootstrapProjectCreated, true);+ assert.equal(targets.bootstrapThreadCreated, true);const commands = yield* Ref.get(dispatchCalls);assert.deepStrictEqual(commands.map((command) => command.type),@@ -250,6 +256,60 @@ it.effect("resolveAutoBootstrapWelcomeTargets creates a project and thread when}),);+it.effect(+ "resolveAutoBootstrapWelcomeTargets preserves a project created before thread failure",+ () =>+ Effect.gen(function* () {+ const dispatchCalls = yield* Ref.make<ReadonlyArray<string>>([]);+ const targets = yield* ServerRuntimeStartup.resolveAutoBootstrapWelcomeTargets.pipe(+ Effect.provideService(ServerConfig.ServerConfig, {+ cwd: "/tmp/startup-project",+ autoBootstrapProjectFromCwd: true,+ } as never),+ Effect.provideService(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {+ getUserInputActivity: () => Effect.die("unused"),+ getCommandReadModel: () => Effect.die("unused"),+ getSnapshot: () => Effect.die("unused"),+ getShellSnapshot: () => Effect.die("unused"),+ getArchivedShellSnapshot: () => Effect.die("unused"),+ getSnapshotSequence: () => Effect.die("unused"),+ getCounts: () => Effect.die("unused"),+ getEventReplayStats: () => Effect.die("unused"),+ getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),+ getProjectShellById: () => Effect.die("unused"),+ getFirstActiveThreadIdByProjectId: () => Effect.die("thread lookup failed"),+ getImportedAgentSessionSources: () => Effect.die("unused"),+ getThreadCheckpointContext: () => Effect.succeed(Option.none()),+ getFullThreadDiffContext: () => Effect.succeed(Option.none()),+ getThreadRuntimeContext: () => Effect.die("unused"),+ getThreadShellById: () => Effect.die("unused"),+ getThreadDetailById: () => Effect.die("unused"),+ getThreadDetailSnapshot: () => Effect.die("unused"),+ searchThreads: () => Effect.succeed({ matches: [] }),+ }),+ Effect.provideService(OrchestrationEngine.OrchestrationEngineService, {+ readEvents: () => Stream.empty,+ readThreadEvents: () => Stream.empty,+ getThreadReplayStats: () => Effect.die("unused thread replay stats"),+ dispatch: (command) =>+ Ref.update(dispatchCalls, (calls) => [...calls, command.type]).pipe(+ Effect.as({ sequence: 1 }),+ ),+ streamDomainEvents: Stream.empty,+ subscribeDomainEvents: Effect.succeed(Stream.empty),+ latestSequence: Effect.succeed(0),+ } satisfies OrchestrationEngine.OrchestrationEngineService["Service"]),+ Effect.provide(NodeServices.layer),+ );++ assert.equal(typeof targets.bootstrapProjectId, "string");+ assert.equal(targets.bootstrapProjectCreated, true);+ assert.equal(targets.bootstrapThreadId, undefined);+ assert.equal(targets.bootstrapThreadCreated, undefined);+ assert.deepStrictEqual(yield* Ref.get(dispatchCalls), ["project.create"]);+ }),+);+it.effect("resolveAutoBootstrapWelcomeTargets preserves typed UUID generation failures", () =>Effect.gen(function* () {const crypto = yield* Crypto.Crypto;@@ -278,6 +338,7 @@ it.effect("resolveAutoBootstrapWelcomeTargets preserves typed UUID generation fagetActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),getProjectShellById: () => Effect.die("unused"),getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),+ getImportedAgentSessionSources: () => Effect.die("unused"),getThreadCheckpointContext: () => Effect.succeed(Option.none()),getFullThreadDiffContext: () => Effect.succeed(Option.none()),getThreadRuntimeContext: () => Effect.die("unused"),@@ -309,3 +370,31 @@ it.effect("resolveAutoBootstrapWelcomeTargets preserves typed UUID generation faassert.deepStrictEqual(yield* Ref.get(dispatchCalls), []);}).pipe(Effect.provide(NodeServices.layer)),);++it.effect("completeAutoBootstrapWelcome settles failures without bootstrap targets", () =>+ Effect.gen(function* () {+ const completion = yield* ServerRuntimeStartup.completeAutoBootstrapWelcome(+ Effect.fail("bootstrap failed"),+ );++ assert.deepStrictEqual(completion, { bootstrapStatus: "complete" });+ }),+);++it.effect("completeAutoBootstrapWelcome settles unexpected defects", () =>+ Effect.gen(function* () {+ const completion = yield* ServerRuntimeStartup.completeAutoBootstrapWelcome(+ Effect.die("bootstrap defect"),+ );++ assert.deepStrictEqual(completion, { bootstrapStatus: "complete" });+ }),+);++it.effect("completeAutoBootstrapWelcome settles an empty bootstrap result", () =>+ Effect.gen(function* () {+ const completion = yield* ServerRuntimeStartup.completeAutoBootstrapWelcome(Effect.succeed({}));++ assert.deepStrictEqual(completion, { bootstrapStatus: "complete" });+ }),+);diff --git a/apps/server/src/serverRuntimeStartup.ts b/apps/server/src/serverRuntimeStartup.tsindex ea3670f08c9..2a12dbb3637 100644--- a/apps/server/src/serverRuntimeStartup.ts+++ b/apps/server/src/serverRuntimeStartup.ts@@ -194,6 +194,8 @@ export const resolveAutoBootstrapWelcomeTargets = Effect.gen(function* () {let bootstrapProjectId: ProjectId | undefined;let bootstrapThreadId: ThreadId | undefined;+ let bootstrapProjectCreated = false;+ let bootstrapThreadCreated = false;if (serverConfig.autoBootstrapProjectFromCwd) {yield* Effect.gen(function* () {@@ -216,45 +218,79 @@ export const resolveAutoBootstrapWelcomeTargets = Effect.gen(function* () {workspaceRoot: serverConfig.cwd,createdAt,});+ bootstrapProjectId = nextProjectId;+ bootstrapProjectCreated = true;} else {nextProjectId = existingProject.value.id;+ bootstrapProjectId = nextProjectId;nextThreadModelSelection =existingProject.value.defaultModelSelection ?? getAutoBootstrapThreadModelSelection();}- const existingThreadId =- yield* projectionReadModelQuery.getFirstActiveThreadIdByProjectId(nextProjectId);- if (Option.isNone(existingThreadId)) {- const createdAt = DateTime.formatIso(yield* DateTime.now);- const createdThreadId = ThreadId.make(yield* randomUUID);- yield* orchestrationEngine.dispatch({- type: "thread.create",- commandId: CommandId.make(yield* randomUUID),- threadId: createdThreadId,- projectId: nextProjectId,- title: "New thread",- modelSelection: nextThreadModelSelection,- interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,- runtimeMode: "full-access",- branch: null,- worktreePath: null,- createdAt,- });- bootstrapProjectId = nextProjectId;- bootstrapThreadId = createdThreadId;- } else {- bootstrapProjectId = nextProjectId;- bootstrapThreadId = existingThreadId.value;- }+ yield* Effect.gen(function* () {+ const existingThreadId =+ yield* projectionReadModelQuery.getFirstActiveThreadIdByProjectId(nextProjectId);+ if (Option.isNone(existingThreadId)) {+ const createdAt = DateTime.formatIso(yield* DateTime.now);+ const createdThreadId = ThreadId.make(yield* randomUUID);+ yield* orchestrationEngine.dispatch({+ type: "thread.create",+ commandId: CommandId.make(yield* randomUUID),+ threadId: createdThreadId,+ projectId: nextProjectId,+ title: "New thread",+ modelSelection: nextThreadModelSelection,+ interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,+ runtimeMode: "full-access",+ branch: null,+ worktreePath: null,+ createdAt,+ });+ bootstrapThreadId = createdThreadId;+ bootstrapThreadCreated = true;+ } else {+ bootstrapThreadId = existingThreadId.value;+ }+ }).pipe(+ Effect.catchCause((cause) =>+ Cause.hasInterrupts(cause)+ ? Effect.failCause(cause)+ : Effect.logWarning("startup thread auto-bootstrap failed", {+ bootstrapProjectId: nextProjectId,+ cause,+ }),+ ),+ );});}return {...(bootstrapProjectId ? { bootstrapProjectId } : {}),...(bootstrapThreadId ? { bootstrapThreadId } : {}),+ ...(bootstrapProjectId ? { bootstrapProjectCreated } : {}),+ ...(bootstrapThreadId ? { bootstrapThreadCreated } : {}),} as const;});+export const completeAutoBootstrapWelcome = <A extends object, E, R>(+ bootstrap: Effect.Effect<A, E, R>,+) =>+ bootstrap.pipe(+ Effect.matchCauseEffect({+ onFailure: (cause) =>+ Cause.hasInterrupts(cause)+ ? Effect.failCause(cause)+ : Effect.logWarning("startup auto-bootstrap failed", { cause }).pipe(+ Effect.as({ bootstrapStatus: "complete" as const }),+ ),+ onSuccess: (targets) =>+ Effect.succeed({+ ...targets,+ bootstrapStatus: "complete" as const,+ }),+ }),+ );+const resolveStartupBrowserTarget = Effect.gen(function* () {const serverConfig = yield* ServerConfig.ServerConfig;const serverAuth = yield* EnvironmentAuth.EnvironmentAuth;@@ -839,36 +875,31 @@ export const make = (options?: StartupOptions) =>runStartupPhase("welcome.autobootstrap",Effect.gen(function* () {- const bootstrapTargets = yield* resolveAutoBootstrapWelcomeTargets.pipe(- Effect.provideService(Crypto.Crypto, crypto),+ const bootstrapCompletion = yield* completeAutoBootstrapWelcome(+ resolveAutoBootstrapWelcomeTargets.pipe(+ Effect.provideService(Crypto.Crypto, crypto),+ ),+ );++ yield* Effect.logDebug(+ "startup phase: publishing completed bootstrap welcome event",+ {+ environmentId: environment.environmentId,+ cwd: welcomeBase.cwd,+ projectName: welcomeBase.projectName,+ ...bootstrapCompletion,+ },);- if (!bootstrapTargets.bootstrapProjectId && !bootstrapTargets.bootstrapThreadId) {- return;- }-- yield* Effect.logDebug("startup phase: publishing bootstrapped welcome event", {- environmentId: environment.environmentId,- cwd: welcomeBase.cwd,- projectName: welcomeBase.projectName,- bootstrapProjectId: bootstrapTargets.bootstrapProjectId,- bootstrapThreadId: bootstrapTargets.bootstrapThreadId,- });yield* lifecycleEvents.publish({version: 1,type: "welcome",payload: {environment,...welcomeBase,- ...bootstrapTargets,+ ...bootstrapCompletion,},});- }).pipe(- Effect.catch((cause) =>- Effect.logWarning("startup auto-bootstrap welcome failed", {- cause,- }),- ),- ),+ }).pipe(Effect.ignoreCause({ log: true })),),);}@@ -914,7 +945,11 @@ export const make = (options?: StartupOptions) =>lifecycleEvents.publish({version: 1,type: "welcome",- payload: { environment, ...welcomeBase },+ payload: {+ environment,+ ...welcomeBase,+ bootstrapStatus: serverConfig.autoBootstrapProjectFromCwd ? "pending" : "complete",+ },}),);yield* options?.activate ?? Effect.void;diff --git a/apps/server/src/serverSettings.test.ts b/apps/server/src/serverSettings.test.tsindex 4526e2c988a..6e837308e8c 100644--- a/apps/server/src/serverSettings.test.ts+++ b/apps/server/src/serverSettings.test.ts@@ -14,6 +14,7 @@ import * as Duration from "effect/Duration";import * as FileSystem from "effect/FileSystem";import * as Layer from "effect/Layer";import * as Option from "effect/Option";+import * as Path from "effect/Path";import * as PlatformError from "effect/PlatformError";import * as Schema from "effect/Schema";import * as Stream from "effect/Stream";@@ -22,6 +23,7 @@ import * as ServerSecretStore from "./auth/ServerSecretStore.ts";import * as ServerConfig from "./config.ts";import { SqlitePersistenceMemory } from "./persistence/Layers/Sqlite.ts";import * as ServerSettingsModule from "./serverSettings.ts";+import { resolveProviderInstanceTerminalEnvironment } from "./terminal/Manager.ts";const decodeSettingsPatch = Schema.decodeUnknownEffect(ServerSettingsPatch);const decodeServerSettings = Schema.decodeUnknownEffect(ServerSettings);@@ -1102,4 +1104,39 @@ it.layer(NodeServices.layer)("server settings", (it) => {);}).pipe(Effect.provide(makeServerSettingsLayer())),);++ it.effect("materializes provider secrets for terminal environment resolution", () =>+ Effect.gen(function* () {+ const serverSettings = yield* ServerSettingsModule.ServerSettingsService;+ const serverConfig = yield* ServerConfig.ServerConfig;+ const fileSystem = yield* FileSystem.FileSystem;+ const path = yield* Path.Path;+ const instanceId = ProviderInstanceId.make("codex_terminal");++ yield* serverSettings.updateSettings({+ providerInstances: {+ [instanceId]: {+ driver: ProviderDriverKind.make("codex"),+ environment: [+ { name: "OPENROUTER_API_KEY", value: "sk-terminal-secret", sensitive: true },+ ],+ config: { homePath: "~/.codex-terminal" },+ },+ },+ });++ const environment = yield* resolveProviderInstanceTerminalEnvironment({+ serverSettings,+ path,+ rawProviderInstanceId: instanceId,+ env: undefined,+ });+ const persisted = yield* fileSystem.readFileString(serverConfig.settingsPath);++ assert.equal(environment.OPENROUTER_API_KEY, "sk-terminal-secret");+ assert.match(environment.CODEX_HOME ?? "", /[\\/][.]codex-terminal$/);+ assert.notInclude(persisted, "sk-terminal-secret");+ assert.include(persisted, '"valueRedacted": true');+ }).pipe(Effect.provide(makeServerSettingsLayer())),+ );});diff --git a/apps/server/src/terminal/Manager.test.ts b/apps/server/src/terminal/Manager.test.tsindex deea3963178..e480e11588b 100644--- a/apps/server/src/terminal/Manager.test.ts+++ b/apps/server/src/terminal/Manager.test.ts@@ -7,9 +7,14 @@ import {type TerminalMetadataStreamEvent,type TerminalOpenInput,type TerminalRestartInput,+ ProviderDriverKind,+ ProviderInstanceId,+ ServerSettingsError,+ TerminalProviderInstanceNotFoundError,} from "@t3tools/contracts";import { HostProcessPlatform } from "@t3tools/shared/hostProcess";import * as Data from "effect/Data";+import * as Deferred from "effect/Deferred";import * as Duration from "effect/Duration";import * as Effect from "effect/Effect";import * as Encoding from "effect/Encoding";@@ -23,11 +28,16 @@ import * as Path from "effect/Path";import * as Ref from "effect/Ref";import * as Schedule from "effect/Schedule";import * as Scope from "effect/Scope";+import * as Stream from "effect/Stream";import * as TestClock from "effect/testing/TestClock";import { ChildProcessSpawner } from "effect/unstable/process";import { expect } from "vite-plus/test";+import * as ServerSecretStore from "../auth/ServerSecretStore.ts";+import * as ServerConfig from "../config.ts";+import { SqlitePersistenceMemory } from "../persistence/Layers/Sqlite.ts";import * as ProcessRunner from "../processRunner.ts";+import * as ServerSettings from "../serverSettings.ts";import * as TerminalManager from "./Manager.ts";import * as PtyAdapter from "./PtyAdapter.ts";@@ -215,6 +225,9 @@ interface CreateManagerOptions {maxRetainedInactiveSessions?: number;historyByteLimit?: number;ptyAdapter?: FakePtyAdapter;+ resolveProviderInstanceEnvironment?: Parameters<+ typeof TerminalManager.makeWithOptions+ >[0]["resolveProviderInstanceEnvironment"];}interface ManagerFixture {@@ -259,6 +272,9 @@ const createManager = (...(options.maxRetainedInactiveSessions !== undefined? { maxRetainedInactiveSessions: options.maxRetainedInactiveSessions }: {}),+ ...(options.resolveProviderInstanceEnvironment !== undefined+ ? { resolveProviderInstanceEnvironment: options.resolveProviderInstanceEnvironment }+ : {}),});const eventsRef = yield* Ref.make<ReadonlyArray<TerminalEvent>>([]);const unsubscribe = yield* manager.subscribe((event) =>@@ -1736,6 +1752,26 @@ it.layer(}),);+ it.effect("expands provider home paths passed to setup terminals", () =>+ Effect.gen(function* () {+ const { manager, ptyAdapter } = yield* createManager(5);++ yield* manager.open({+ ...openInput(),+ env: {+ CODEX_HOME: "~/.codex-work",+ CLAUDE_CONFIG_DIR: "~/.claude-work",+ CUSTOM_ACCOUNT: "~/leave-this-value-alone",+ },+ });++ const environment = ptyAdapter.spawnInputs[0]?.env;+ expect(environment?.CODEX_HOME).toMatch(/[\\/][.]codex-work$/);+ expect(environment?.CLAUDE_CONFIG_DIR).toMatch(/[\\/][.]claude-work$/);+ expect(environment?.CUSTOM_ACCOUNT).toBe("~/leave-this-value-alone");+ }),+ );+it.effect("strips AppImage runtime env from terminal sessions", () =>Effect.gen(function* () {const appDir = "/tmp/.mount_T3Codeabc123";@@ -1822,6 +1858,382 @@ it.layer(}),);+ it.effect("resolves a provider instance environment before spawning", () =>+ Effect.gen(function* () {+ const providerInstanceId = ProviderInstanceId.make("codex_work");+ const { manager, ptyAdapter } = yield* createManager(5, {+ env: { T3CODE_SECRET: "server-only" },+ resolveProviderInstanceEnvironment: (requestedId, env) =>+ Effect.succeed({+ ...env,+ PROVIDER_SECRET: requestedId === providerInstanceId ? "secret-value" : "wrong",+ CODEX_HOME: "/accounts/codex-work",+ }),+ });++ const snapshot = yield* manager.open(+ openInput({ providerInstanceId, env: { CLIENT_FLAG: "1" } }),+ );++ expect(ptyAdapter.spawnInputs[0]?.env.PROVIDER_SECRET).toBe("secret-value");+ expect(ptyAdapter.spawnInputs[0]?.env.CODEX_HOME).toBe("/accounts/codex-work");+ expect(ptyAdapter.spawnInputs[0]?.env.CLIENT_FLAG).toBe("1");+ expect(ptyAdapter.spawnInputs[0]?.env.T3CODE_SECRET).toBeUndefined();+ expect(snapshot).not.toHaveProperty("env");+ expect(snapshot).not.toHaveProperty("providerInstanceId");+ }),+ );++ it.effect("fails closed when a provider instance is missing", () =>+ Effect.gen(function* () {+ const providerInstanceId = ProviderInstanceId.make("deleted_instance");+ const { manager, ptyAdapter } = yield* createManager(5, {+ resolveProviderInstanceEnvironment: (requestedId) =>+ Effect.fail(+ new TerminalProviderInstanceNotFoundError({+ providerInstanceId: ProviderInstanceId.make(requestedId),+ }),+ ),+ });++ const error = yield* manager.open(openInput({ providerInstanceId })).pipe(Effect.flip);++ assert.deepStrictEqual(+ error,+ new TerminalProviderInstanceNotFoundError({ providerInstanceId }),+ );+ expect(ptyAdapter.spawnInputs).toHaveLength(0);+ }),+ );++ it.effect("preserves the settings failure when provider environment resolution fails", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const providerInstanceId = ProviderInstanceId.make("codex_work");+ const settingsCause = new Error("secret store read failed");+ const settingsError = new ServerSettingsError({+ settingsPath: "/test/settings.json",+ operation: "read-secret",+ providerInstanceId,+ environmentVariable: "OPENROUTER_API_KEY",+ cause: settingsCause,+ });+ const serverSettings = ServerSettings.ServerSettingsService.of({+ start: Effect.void,+ ready: Effect.void,+ getSettings: Effect.fail(settingsError),+ updateSettings: () => Effect.fail(settingsError),+ streamChanges: Stream.empty,+ subscribeChanges: Effect.succeed(Stream.empty),+ });++ const error = yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({+ serverSettings,+ path,+ rawProviderInstanceId: providerInstanceId,+ env: undefined,+ }).pipe(Effect.flip);++ expect(error).toMatchObject({+ _tag: "TerminalProviderEnvironmentError",+ providerInstanceId,+ });+ expect(error.cause).toBe(settingsError);+ expect(error.message).not.toContain(settingsError.message);+ expect(error.message).not.toContain("OPENROUTER_API_KEY");+ }),+ );++ it.effect.each([+ {+ name: "Codex home",+ driver: "codex",+ variable: "CODEX_HOME",+ config: { homePath: "/configured/codex" },+ expectedHome: "/configured/codex",+ },+ {+ name: "Codex shadow home",+ driver: "codex",+ variable: "CODEX_HOME",+ config: { homePath: "/configured/codex", shadowHomePath: "/configured/codex-shadow" },+ expectedHome: "/configured/codex-shadow",+ },+ {+ name: "Claude home",+ driver: "claudeAgent",+ variable: "CLAUDE_CONFIG_DIR",+ config: { homePath: "/configured/claude" },+ expectedHome: "/configured/claude",+ },+ ])("prefers $name over the instance environment", ({ driver, variable, config, expectedHome }) =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const serverSettings = yield* ServerSettings.ServerSettingsService;+ const environment = yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({+ serverSettings,+ path,+ rawProviderInstanceId: "configured_home",+ env: undefined,+ });++ expect(environment[variable]).toBe(path.resolve(expectedHome));+ }).pipe(+ Effect.provide(+ ServerSettings.layerTest({+ providerInstances: {+ [ProviderInstanceId.make("configured_home")]: {+ driver: ProviderDriverKind.make(driver),+ environment: [{ name: variable, value: "~/.environment-account", sensitive: false }],+ config,+ },+ },+ }),+ ),+ ),+ );++ it.effect("resolves the legacy Codex default instance", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const serverSettings = yield* ServerSettings.ServerSettingsService;+ const environment = yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({+ serverSettings,+ path,+ rawProviderInstanceId: "codex",+ env: undefined,+ });++ expect(environment.CODEX_HOME).toMatch(/[\\/][.]codex-legacy$/);+ }).pipe(+ Effect.provide(+ ServerSettings.ServerSettingsService.layerTest({+ providerInstances: {},+ providers: { codex: { homePath: "~/.codex-legacy" } },+ }),+ ),+ ),+ );++ it.effect("resolves the legacy Claude default instance", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const serverSettings = yield* ServerSettings.ServerSettingsService;+ const environment = yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({+ serverSettings,+ path,+ rawProviderInstanceId: "claudeAgent",+ env: undefined,+ });++ expect(environment.CLAUDE_CONFIG_DIR).toMatch(/[\\/][.]claude-legacy$/);+ }).pipe(+ Effect.provide(+ ServerSettings.ServerSettingsService.layerTest({+ providerInstances: {},+ providers: { claudeAgent: { homePath: "~/.claude-legacy" } },+ }),+ ),+ ),+ );++ it.effect("prefers an explicit default instance over legacy provider settings", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const serverSettings = yield* ServerSettings.ServerSettingsService;+ const environment = yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({+ serverSettings,+ path,+ rawProviderInstanceId: "codex",+ env: undefined,+ });++ expect(environment.CODEX_HOME).toMatch(/[\\/][.]codex-explicit$/);+ }).pipe(+ Effect.provide(+ ServerSettings.ServerSettingsService.layerTest({+ providers: { codex: { homePath: "~/.codex-legacy" } },+ providerInstances: {+ [ProviderInstanceId.make("codex")]: {+ driver: "codex",+ config: { homePath: "~/.codex-explicit" },+ },+ },+ }),+ ),+ ),+ );++ it.effect("keeps unknown provider instance ids unavailable after legacy hydration", () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const serverSettings = yield* ServerSettings.ServerSettingsService;+ const error = yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({+ serverSettings,+ path,+ rawProviderInstanceId: "codex_unknown",+ env: undefined,+ }).pipe(Effect.flip);++ expect(error).toMatchObject({+ _tag: "TerminalProviderInstanceNotFoundError",+ providerInstanceId: "codex_unknown",+ });+ }).pipe(Effect.provide(ServerSettings.ServerSettingsService.layerTest())),+ );++ it.effect("restarts a running terminal when the resolved provider environment changes", () =>+ Effect.gen(function* () {+ const providerInstanceId = ProviderInstanceId.make("codex_work");+ let providerSecret = "first-secret";+ const { manager, ptyAdapter } = yield* createManager(5, {+ resolveProviderInstanceEnvironment: () =>+ Effect.succeed({ PROVIDER_SECRET: providerSecret }),+ });++ yield* manager.open(openInput({ providerInstanceId }));+ providerSecret = "second-secret";+ yield* manager.open(openInput({ providerInstanceId }));++ expect(ptyAdapter.processes[0]?.killed).toBe(true);+ expect(ptyAdapter.spawnInputs).toHaveLength(2);+ expect(ptyAdapter.spawnInputs[1]?.env.PROVIDER_SECRET).toBe("second-secret");+ }),+ );++ it.effect("restarts with current provider secrets and clears bounded history", () =>+ Effect.gen(function* () {+ const serverSettings = yield* ServerSettings.ServerSettingsService;+ const path = yield* Path.Path;+ const providerInstanceId = ProviderInstanceId.make("codex_restart");+ const { manager, ptyAdapter, logsDir } = yield* createManager(2, {+ historyByteLimit: 8,+ resolveProviderInstanceEnvironment: (rawProviderInstanceId, env) =>+ TerminalManager.resolveProviderInstanceTerminalEnvironment({+ serverSettings,+ path,+ rawProviderInstanceId,+ env,+ }),+ });+ const homePath = path.join(logsDir, "codex");+ const updateSecret = (value: string) =>+ serverSettings.updateSettings({+ providerInstances: {+ [providerInstanceId]: {+ driver: ProviderDriverKind.make("codex"),+ config: { homePath },+ environment: [{ name: "PROVIDER_SECRET", value, sensitive: true }],+ },+ },+ });+ const input = {+ providerInstanceId,+ env: { CLIENT_FLAG: "1", PROVIDER_SECRET: "client-value" },+ };+ const outputProcessed = yield* Deferred.make<void>();+ const unsubscribe = yield* manager.subscribe((event) =>+ event.type === "output"+ ? Deferred.succeed(outputProcessed, undefined).pipe(Effect.asVoid)+ : Effect.void,+ );+ yield* Effect.addFinalizer(() => Effect.sync(unsubscribe));++ yield* updateSecret("first-secret");+ yield* manager.restart(restartInput(input));+ const firstProcess = ptyAdapter.processes[0]!;+ expect(ptyAdapter.spawnInputs[0]?.env.PROVIDER_SECRET).toBe("first-secret");+ firstProcess.emitData("discarded\nold-one\nold-two\n");+ yield* Deferred.await(outputProcessed);+ expect((yield* manager.open(openInput(input))).history).toBe("old-two\n");++ yield* updateSecret("second-secret");+ const restarted = yield* manager.restart(restartInput(input));++ expect(firstProcess.killed).toBe(true);+ expect(ptyAdapter.spawnInputs).toHaveLength(2);+ expect(ptyAdapter.spawnInputs[1]?.env).toMatchObject({+ PROVIDER_SECRET: "second-secret",+ CODEX_HOME: homePath,+ CLIENT_FLAG: "1",+ });+ expect(restarted.history).toBe("");+ expect(restarted.status).toBe("running");+ expect(restarted).not.toHaveProperty("env");+ expect(restarted).not.toHaveProperty("providerInstanceId");+ const logPath = yield* historyLogPath(logsDir);+ expect(yield* readFileString(logPath)).toBe("");++ ptyAdapter.processes[1]!.emitData("discarded again\nnew-one\nnew-two\n");+ yield* manager.close({ threadId: "thread-1" });+ expect(yield* readFileString(logPath)).toBe("new-two\n");+ }).pipe(+ Effect.provide(+ ServerSettings.layer.pipe(+ Layer.provide(ServerSecretStore.layer),+ Layer.provide(SqlitePersistenceMemory),+ Layer.provide(+ ServerConfig.layerTest(process.cwd(), { prefix: "t3code-terminal-provider-restart-" }),+ ),+ ),+ ),+ ),+ );++ it.effect("attaches to a running provider terminal without resolving the provider again", () =>+ Effect.gen(function* () {+ const providerInstanceId = ProviderInstanceId.make("codex_work");+ let providerAvailable = true;+ const { manager, ptyAdapter } = yield* createManager(5, {+ resolveProviderInstanceEnvironment: (requestedId) =>+ providerAvailable+ ? Effect.succeed({ PROVIDER_SECRET: "secret-value" })+ : Effect.fail(+ new TerminalProviderInstanceNotFoundError({+ providerInstanceId: ProviderInstanceId.make(requestedId),+ }),+ ),+ });+ yield* manager.open(openInput({ providerInstanceId }));+ providerAvailable = false;+ const events: TerminalAttachStreamEvent[] = [];++ const unsubscribe = yield* manager.attachStream(+ { ...openInput({ providerInstanceId }), restartIfNotRunning: true },+ (event) => Effect.sync(() => events.push(event)),+ );+ unsubscribe();++ expect(events[0]?.type).toBe("snapshot");+ expect(ptyAdapter.spawnInputs).toHaveLength(1);+ expect(ptyAdapter.processes[0]?.killed).toBe(false);+ }),+ );++ it.effect("fails closed when attaching would create a missing provider terminal", () =>+ Effect.gen(function* () {+ const providerInstanceId = ProviderInstanceId.make("deleted_instance");+ const { manager, ptyAdapter } = yield* createManager(5, {+ resolveProviderInstanceEnvironment: (requestedId) =>+ Effect.fail(+ new TerminalProviderInstanceNotFoundError({+ providerInstanceId: ProviderInstanceId.make(requestedId),+ }),+ ),+ });++ const error = yield* manager+ .attachStream(openInput({ providerInstanceId }), () => Effect.void)+ .pipe(Effect.flip);++ assert.deepStrictEqual(+ error,+ new TerminalProviderInstanceNotFoundError({ providerInstanceId }),+ );+ expect(ptyAdapter.spawnInputs).toHaveLength(0);+ }),+ );+it.effect("starts zsh with prompt spacer disabled to avoid `%` end markers", () =>Effect.gen(function* () {if ((yield* HostProcessPlatform) === "win32") return;diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.tsindex f04e3c2d897..d9bdc6bcd92 100644--- a/apps/server/src/terminal/Manager.ts+++ b/apps/server/src/terminal/Manager.ts@@ -15,6 +15,8 @@ import {TerminalError,TerminalHistoryError,TerminalNotRunningError,+ TerminalProviderInstanceNotFoundError,+ TerminalProviderEnvironmentError,TerminalResizeError,TerminalSessionLookupError,TerminalWriteError,@@ -31,6 +33,9 @@ import {type TerminalSessionStatus,type TerminalSummary,type TerminalWriteInput,+ ClaudeSettings,+ CodexSettings,+ ProviderInstanceId,} from "@t3tools/contracts";import { makeKeyedCoalescingWorker } from "@t3tools/shared/KeyedCoalescingWorker";import { HostProcessPlatform } from "@t3tools/shared/hostProcess";@@ -52,11 +57,17 @@ import * as Semaphore from "effect/Semaphore";import * as SynchronizedRef from "effect/SynchronizedRef";import * as ServerConfig from "../config.ts";+import { mergeProviderInstanceEnvironment } from "../provider/ProviderInstanceEnvironment.ts";+import { resolveCodexHomeLayout } from "../provider/Drivers/CodexHomeLayout.ts";+import { makeClaudeEnvironment } from "../provider/Drivers/ClaudeHome.ts";+import { deriveProviderInstanceConfigMap } from "../provider/Layers/ProviderInstanceRegistryHydration.ts";+import * as ServerSettings from "../serverSettings.ts";import {increment,terminalRestartsTotal,terminalSessionsTotal,} from "../observability/Metrics.ts";+import { expandHomePath } from "../pathExpansion.ts";import * as ProcessRunner from "../processRunner.ts";import * as PortScanner from "../preview/PortScanner.ts";import * as PtyAdapter from "./PtyAdapter.ts";@@ -69,6 +80,8 @@ export {TerminalError,TerminalHistoryError,TerminalNotRunningError,+ TerminalProviderInstanceNotFoundError,+ TerminalProviderEnvironmentError,TerminalResizeError,TerminalSessionLookupError,TerminalWriteError,@@ -86,6 +99,8 @@ const DEFAULT_OPEN_ROWS = 30;const TERMINAL_ENV_BLOCKLIST = new Set(["PORT", "ELECTRON_RENDERER_PORT", "ELECTRON_RUN_AS_NODE"]);const nowIso = Effect.map(DateTime.now, DateTime.formatIso);const MAX_TERMINAL_LABEL_LENGTH = 128;+const decodeClaudeSettings = Schema.decodeUnknownOption(ClaudeSettings);+const decodeCodexSettings = Schema.decodeUnknownOption(CodexSettings);class TerminalSubprocessCheckError extends Schema.TaggedErrorClass<TerminalSubprocessCheckError>()("TerminalSubprocessCheckError",@@ -1267,7 +1282,8 @@ function createTerminalSpawnEnv(}if (runtimeEnv) {for (const [key, value] of Object.entries(runtimeEnv)) {- spawnEnv[key] = value;+ spawnEnv[key] =+ key === "CODEX_HOME" || key === "CLAUDE_CONFIG_DIR" ? expandHomePath(value) : value;}}// Both PTY backends feed truecolor-capable terminal clients.@@ -1306,17 +1322,78 @@ interface TerminalManagerOptions {readonly threadId: string;readonly terminalId: string;}) => Effect.Effect<void>;+ resolveProviderInstanceEnvironment?: (+ providerInstanceId: string,+ env: Record<string, string> | undefined,+ ) => Effect.Effect<+ Record<string, string>,+ TerminalProviderInstanceNotFoundError | TerminalProviderEnvironmentError+ >;}+export const resolveProviderInstanceTerminalEnvironment = Effect.fn(+ "terminal.resolveProviderInstanceTerminalEnvironment",+)(function* (input: {+ readonly serverSettings: ServerSettings.ServerSettingsService["Service"];+ readonly path: Path.Path;+ readonly rawProviderInstanceId: string;+ readonly env: Record<string, string> | undefined;+}) {+ const providerInstanceId = ProviderInstanceId.make(input.rawProviderInstanceId);+ const settings = yield* input.serverSettings.getSettings.pipe(+ Effect.mapError((cause) => new TerminalProviderEnvironmentError({ providerInstanceId, cause })),+ );+ const instance = deriveProviderInstanceConfigMap(settings)[providerInstanceId];+ if (instance === undefined) {+ return yield* new TerminalProviderInstanceNotFoundError({ providerInstanceId });+ }++ let resolved = mergeProviderInstanceEnvironment(instance.environment, input.env ?? {});+ if (instance.driver === "codex") {+ const config = decodeCodexSettings(instance.config ?? {});+ if (Option.isSome(config)) {+ const layout = yield* resolveCodexHomeLayout(config.value).pipe(+ Effect.provideService(Path.Path, input.path),+ );+ if (layout.effectiveHomePath)+ resolved = { ...resolved, CODEX_HOME: layout.effectiveHomePath };+ }+ } else if (instance.driver === "claudeAgent") {+ const config = decodeClaudeSettings(instance.config ?? {});+ if (Option.isSome(config)) {+ resolved = yield* makeClaudeEnvironment(config.value, resolved).pipe(+ Effect.provideService(Path.Path, input.path),+ );+ }+ }++ return Object.fromEntries(+ Object.entries(resolved).filter((entry): entry is [string, string] => entry[1] !== undefined),+ );+});+export const make = Effect.fn("TerminalManager.make")(function* () {const { terminalLogsDir } = yield* ServerConfig.ServerConfig;const ptyAdapter = yield* PtyAdapter.PtyAdapter;const portDiscovery = yield* PortScanner.PortDiscovery;+ const serverSettings = yield* ServerSettings.ServerSettingsService;+ const path = yield* Path.Path;+ const resolveProviderInstanceEnvironment = Effect.fn(+ "terminal.resolveProviderInstanceEnvironment",+ )((rawProviderInstanceId: string, env: Record<string, string> | undefined) =>+ resolveProviderInstanceTerminalEnvironment({+ serverSettings,+ path,+ rawProviderInstanceId,+ env,+ }),+ );return yield* makeWithOptions({logsDir: terminalLogsDir,ptyAdapter,registerTerminalProcesses: portDiscovery.registerTerminalProcesses,unregisterTerminal: portDiscovery.unregisterTerminal,+ resolveProviderInstanceEnvironment,});});@@ -1339,6 +1416,24 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(funcconst baseEnv = options.env ?? process.env;const shellResolver = options.shellResolver ?? (() => defaultShellResolver(platform, baseEnv));const processRunner = yield* ProcessRunner.ProcessRunner;+ const resolveLaunchInputEnvironment = Effect.fn("terminal.resolveLaunchInputEnvironment")(+ function* <Input extends TerminalOpenInput | TerminalAttachInput | TerminalRestartInput>(+ input: Input,+ ): Effect.fn.Return<+ Input,+ TerminalProviderInstanceNotFoundError | TerminalProviderEnvironmentError+ > {+ if (input.providerInstanceId === undefined) return input;+ const resolver = options.resolveProviderInstanceEnvironment;+ if (resolver === undefined) {+ return yield* new TerminalProviderInstanceNotFoundError({+ providerInstanceId: ProviderInstanceId.make(input.providerInstanceId),+ });+ }+ const env = yield* resolver(input.providerInstanceId, input.env);+ return { ...input, env };+ },+ );// One process-table snapshot per poll tick, shared across every terminal.// Per-terminal `pgrep`/`ps` calls multiply spawn load by terminal count and// can exhaust the PID space on hosts with many sessions (#6332).@@ -2468,7 +2563,10 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func});const open: TerminalManager["Service"]["open"] = (input) =>- withThreadLock(input.threadId, openLocked(input));+ withThreadLock(+ input.threadId,+ resolveLaunchInputEnvironment(input).pipe(Effect.flatMap(openLocked)),+ );const openOrAttachForStream = (input: TerminalAttachInput) =>withThreadLock(@@ -2485,11 +2583,12 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func});}- return yield* openLocked({+ const resolvedInput = yield* resolveLaunchInputEnvironment({...input,terminalId,cwd: input.cwd,});+ return yield* openLocked(resolvedInput);}const session = existing.value;@@ -2497,11 +2596,12 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(funcconst targetRows = input.rows ?? session.rows;if (!session.process && input.cwd && input.restartIfNotRunning === true) {- return yield* openLocked({+ const resolvedInput = yield* resolveLaunchInputEnvironment({...input,terminalId,cwd: input.cwd,});+ return yield* openLocked(resolvedInput);}if (@@ -2753,84 +2853,87 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func}),);+ const restartResolved = (input: TerminalRestartInput) =>+ Effect.gen(function* () {+ yield* increment(terminalRestartsTotal, { scope: "thread" });+ const terminalId = input.terminalId;+ yield* assertValidCwd(input.cwd);++ const sessionKey = toSessionKey(input.threadId, terminalId);+ const existingSession = yield* getSession(input.threadId, terminalId);+ let session: TerminalSessionState;+ if (Option.isNone(existingSession)) {+ const cols = input.cols ?? DEFAULT_OPEN_COLS;+ const rows = input.rows ?? DEFAULT_OPEN_ROWS;+ session = {+ threadId: input.threadId,+ terminalId,+ cwd: input.cwd,+ worktreePath: input.worktreePath ?? null,+ status: "starting",+ pid: null,+ history: new BoundedTerminalHistory(historyLineLimit, "", historyByteLimit),+ pendingHistoryControlSequence: "",+ pendingProcessEvents: [],+ pendingProcessEventIndex: 0,+ processEventDrainRunning: false,+ exitCode: null,+ exitSignal: null,+ updatedAt: yield* nowIso,+ eventSequence: 0,+ cols,+ rows,+ process: null,+ unsubscribeData: null,+ unsubscribeExit: null,+ hasRunningSubprocess: false,+ childCommandLabel: null,+ runtimeEnv: normalizedRuntimeEnv(input.env),+ };+ const createdSession = session;+ yield* modifyManagerState((state) => {+ const sessions = new Map(state.sessions);+ sessions.set(sessionKey, createdSession);+ return [undefined, { ...state, sessions }] as const;+ });+ yield* evictInactiveSessionsIfNeeded();+ } else {+ session = existingSession.value;+ yield* stopProcess(session);+ session.cwd = input.cwd;+ session.worktreePath = input.worktreePath ?? null;+ session.runtimeEnv = normalizedRuntimeEnv(input.env);+ }++ const cols = input.cols ?? session.cols;+ const rows = input.rows ?? session.rows;++ session.history.clear();+ session.pendingHistoryControlSequence = "";+ session.pendingProcessEvents = [];+ session.pendingProcessEventIndex = 0;+ session.processEventDrainRunning = false;+ yield* persistHistory(input.threadId, terminalId, session.history);+ yield* startSession(+ session,+ {+ threadId: input.threadId,+ terminalId,+ cwd: input.cwd,+ ...(input.worktreePath !== undefined ? { worktreePath: input.worktreePath } : {}),+ cols,+ rows,+ ...(input.env ? { env: input.env } : {}),+ },+ "restarted",+ );+ return snapshot(session);+ });+const restart: TerminalManager["Service"]["restart"] = (input) =>withThreadLock(input.threadId,- Effect.gen(function* () {- yield* increment(terminalRestartsTotal, { scope: "thread" });- const terminalId = input.terminalId;- yield* assertValidCwd(input.cwd);-- const sessionKey = toSessionKey(input.threadId, terminalId);- const existingSession = yield* getSession(input.threadId, terminalId);- let session: TerminalSessionState;- if (Option.isNone(existingSession)) {- const cols = input.cols ?? DEFAULT_OPEN_COLS;- const rows = input.rows ?? DEFAULT_OPEN_ROWS;- session = {- threadId: input.threadId,- terminalId,- cwd: input.cwd,- worktreePath: input.worktreePath ?? null,- status: "starting",- pid: null,- history: new BoundedTerminalHistory(historyLineLimit, "", historyByteLimit),- pendingHistoryControlSequence: "",- pendingProcessEvents: [],- pendingProcessEventIndex: 0,- processEventDrainRunning: false,- exitCode: null,- exitSignal: null,- updatedAt: yield* nowIso,- eventSequence: 0,- cols,- rows,- process: null,- unsubscribeData: null,- unsubscribeExit: null,- hasRunningSubprocess: false,- childCommandLabel: null,- runtimeEnv: normalizedRuntimeEnv(input.env),- };- const createdSession = session;- yield* modifyManagerState((state) => {- const sessions = new Map(state.sessions);- sessions.set(sessionKey, createdSession);- return [undefined, { ...state, sessions }] as const;- });- yield* evictInactiveSessionsIfNeeded();- } else {- session = existingSession.value;- yield* stopProcess(session);- session.cwd = input.cwd;- session.worktreePath = input.worktreePath ?? null;- session.runtimeEnv = normalizedRuntimeEnv(input.env);- }-- const cols = input.cols ?? session.cols;- const rows = input.rows ?? session.rows;-- session.history.clear();- session.pendingHistoryControlSequence = "";- session.pendingProcessEvents = [];- session.pendingProcessEventIndex = 0;- session.processEventDrainRunning = false;- yield* persistHistory(input.threadId, terminalId, session.history);- yield* startSession(- session,- {- threadId: input.threadId,- terminalId,- cwd: input.cwd,- ...(input.worktreePath !== undefined ? { worktreePath: input.worktreePath } : {}),- cols,- rows,- ...(input.env ? { env: input.env } : {}),- },- "restarted",- );- return snapshot(session);- }),+ resolveLaunchInputEnvironment(input).pipe(Effect.flatMap(restartResolved)),);const close: TerminalManager["Service"]["close"] = (input) =>diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.tsindex 5ecdd341c95..6261f7bc528 100644--- a/apps/server/src/ws.ts+++ b/apps/server/src/ws.ts@@ -53,6 +53,7 @@ import {type RelayClientInstallProgressEvent,ServerSelfUpdateError,type ServerSelfUpdateProgressEvent,+ type ServerLifecycleStreamEvent,type FilesystemBrowseFailure,FilesystemBrowseError,AssetWorkspaceContextNotFoundError,@@ -96,6 +97,7 @@ import {} from "./observability/RpcInstrumentation.ts";import * as ProviderRegistry from "./provider/Services/ProviderRegistry.ts";import * as ProviderService from "./provider/Services/ProviderService.ts";+import * as ProviderSessionDirectory from "./provider/Services/ProviderSessionDirectory.ts";import * as ProviderMaintenanceRunner from "./provider/providerMaintenanceRunner.ts";import { ProviderAuthService } from "./provider/Services/ProviderAuthService.ts";import { ProviderInstanceRegistry } from "./provider/Services/ProviderInstanceRegistry.ts";@@ -119,6 +121,8 @@ import * as VcsProvisioningService from "./vcs/VcsProvisioningService.ts";import * as GitWorkflowService from "./git/GitWorkflowService.ts";import * as ReviewService from "./review/ReviewService.ts";import * as ProjectSetupScriptRunner from "./project/ProjectSetupScriptRunner.ts";+import * as AgentSessionScanner from "./project/AgentSessionScanner.ts";+import { importRecentAgentThreads } from "./project/AgentSessionImporter.ts";import * as ServerEnvironment from "./environment/ServerEnvironment.ts";import * as RemoteOpenTargets from "./environment/RemoteOpenTargets.ts";import * as BackgroundPolicy from "./background/BackgroundPolicy.ts";@@ -512,6 +516,7 @@ const makeWsRpcLayer = (const portDiscovery = yield* PortScanner.PortDiscovery;const providerRegistry = yield* ProviderRegistry.ProviderRegistry;const providerService = yield* ProviderService.ProviderService;+ const providerSessionDirectory = yield* ProviderSessionDirectory.ProviderSessionDirectory;const providerMaintenanceRunner = yield* ProviderMaintenanceRunner.ProviderMaintenanceRunner;const providerAuth = yield* ProviderAuthService;const providerInstances = yield* ProviderInstanceRegistry;@@ -560,6 +565,7 @@ const makeWsRpcLayer = (return true;});const projectSetupScriptRunner = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;+ const agentSessionScanner = yield* AgentSessionScanner.AgentSessionScanner;const serverEnvironment = yield* ServerEnvironment.ServerEnvironment;const backgroundPolicy = yield* BackgroundPolicy.BackgroundPolicy;const rpcClientIds = yield* Ref.make(new Set<RpcClientId>());@@ -2332,6 +2338,31 @@ const makeWsRpcLayer = (deletePendingAttachment(input.attachmentId),{ "rpc.aggregate": "workspace" },),+ [WS_METHODS.agentSessionsScan]: () =>+ observeRpcEffect(WS_METHODS.agentSessionsScan, agentSessionScanner.scan, {+ "rpc.aggregate": "workspace",+ }),+ [WS_METHODS.agentSessionsImport]: (input) =>+ observeRpcEffect(+ WS_METHODS.agentSessionsImport,+ importRecentAgentThreads(input).pipe(+ Effect.provideService(AgentSessionScanner.AgentSessionScanner, agentSessionScanner),+ Effect.provideService(+ OrchestrationEngine.OrchestrationEngineService,+ orchestrationEngine,+ ),+ Effect.provideService(+ ProjectionSnapshotQuery.ProjectionSnapshotQuery,+ projectionSnapshotQuery,+ ),+ Effect.provideService(Crypto.Crypto, crypto),+ Effect.provideService(+ ProviderSessionDirectory.ProviderSessionDirectory,+ providerSessionDirectory,+ ),+ ),+ { "rpc.aggregate": "workspace" },+ ),[WS_METHODS.assetsCreateUrl]: (input) =>observeRpcEffect(WS_METHODS.assetsCreateUrl,@@ -2746,11 +2777,18 @@ const makeWsRpcLayer = (observeRpcStreamEffect(WS_METHODS.subscribeServerLifecycle,Effect.gen(function* () {+ const liveBuffer = yield* Queue.unbounded<ServerLifecycleStreamEvent>();+ yield* Effect.forkScoped(+ lifecycleEvents.stream.pipe(+ Stream.runForEach((event) => Queue.offer(liveBuffer, event)),+ ),+ { startImmediately: true },+ );const snapshot = yield* lifecycleEvents.snapshot;const snapshotEvents = Array.from(snapshot.events).toSorted((left, right) => left.sequence - right.sequence,);- const liveEvents = lifecycleEvents.stream.pipe(+ const liveEvents = Stream.fromQueue(liveBuffer).pipe(Stream.filter((event) => event.sequence > snapshot.sequence),);return Stream.concat(Stream.fromIterable(snapshotEvents), liveEvents);@@ -2877,6 +2915,7 @@ export const websocketRpcRouteLayer = Layer.unwrap(previewAutomationBroker,).pipe(Layer.provideMerge(RpcSerialization.layerJson),+ Layer.provide(AgentSessionScanner.layer),Layer.provide(ProviderMaintenanceRunner.layer),Layer.provide(Layer.succeed(ServerSelfUpdate.ServerSelfUpdate, serverSelfUpdate)),// One server-lifetime service means clients share the same PR caches, and a WSdiff --git a/apps/web/src/authBootstrap.test.ts b/apps/web/src/authBootstrap.test.tsindex 1a79f729bb0..dfe2b51d040 100644--- a/apps/web/src/authBootstrap.test.ts+++ b/apps/web/src/authBootstrap.test.ts@@ -310,6 +310,63 @@ describe("resolveInitialServerAuthGateState", () => {expect(testApi.calls.session).toBe(2);});+ it("keeps manual token submission pending until the session is authenticated", async () => {+ vi.useFakeTimers();+ let authenticated = false;+ let settled = false;+ try {+ const testApi = await installAuthApi({+ session: () =>+ authenticated+ ? authenticatedSession(LOOPBACK_AUTH)+ : unauthenticatedSession(LOOPBACK_AUTH),+ browserSession: () => Effect.succeed(browserSession(["orchestration:read"])),+ });+ const { submitServerAuthCredential } = await import("./environments/primary");++ const submission = submitServerAuthCredential("retry-token").finally(() => {+ settled = true;+ });+ await vi.advanceTimersByTimeAsync(0);++ expect(testApi.calls.browserSession).toEqual([{ credential: "retry-token" }]);+ expect(testApi.calls.session).toBe(1);+ expect(settled).toBe(false);++ authenticated = true;+ await vi.advanceTimersByTimeAsync(100);+ await expect(submission).resolves.toBeUndefined();+ expect(testApi.calls.session).toBe(2);+ } finally {+ vi.useRealTimers();+ }+ });++ it("fails manual token submission when the session is not established", async () => {+ vi.useFakeTimers();+ try {+ const testApi = await installAuthApi({+ session: () => unauthenticatedSession(LOOPBACK_AUTH),+ browserSession: () => Effect.succeed(browserSession(["orchestration:read"])),+ });+ const { PrimaryEnvironmentAuthSessionTimeoutError, submitServerAuthCredential } =+ await import("./environments/primary/auth");++ const submission = submitServerAuthCredential("retry-token");+ const failure = submission.then(+ () => null,+ (error: unknown) => error,+ );+ await vi.advanceTimersByTimeAsync(2_000);++ await expect(failure).resolves.toBeInstanceOf(PrimaryEnvironmentAuthSessionTimeoutError);+ expect(testApi.calls.browserSession).toEqual([{ credential: "retry-token" }]);+ expect(testApi.calls.session).toBeGreaterThan(1);+ } finally {+ vi.useRealTimers();+ }+ });+it("rejects a blank pairing token with a structured validation error", async () => {const { PrimaryEnvironmentPairingCredentialRequiredError, submitServerAuthCredential } =await import("./environments/primary/auth");diff --git a/apps/web/src/browser/HostedBrowserWebview.test.tsx b/apps/web/src/browser/HostedBrowserWebview.test.tsxnew file mode 100644index 00000000000..4a241befef7--- /dev/null+++ b/apps/web/src/browser/HostedBrowserWebview.test.tsx@@ -0,0 +1,200 @@+import {+ DEFAULT_CLIENT_SETTINGS,+ EnvironmentId,+ FILL_PREVIEW_VIEWPORT,+ ThreadId,+ type ClientSettings,+ type DesktopPreviewBridge,+} from "@t3tools/contracts";+import { act } from "react";+import { create, type ReactTestRenderer } from "react-test-renderer";+import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";++const mocks = vi.hoisted(() => ({+ getClientSettings: vi.fn<() => Promise<ClientSettings | null>>(),+ setClientSettings: vi.fn<(settings: ClientSettings) => Promise<void>>(),+ createTab: vi.fn<DesktopPreviewBridge["createTab"]>(),+ closeTab: vi.fn<DesktopPreviewBridge["closeTab"]>(),+ registerWebview: vi.fn<DesktopPreviewBridge["registerWebview"]>(),+ getPreviewConfig: vi.fn<DesktopPreviewBridge["getPreviewConfig"]>(),+ activeRecordings: new Set<string>(),+}));++vi.mock("~/localApi", () => ({+ ensureLocalApi: () => ({ persistence: mocks }),+}));++vi.mock("~/components/preview/previewBridge", () => ({+ previewBridge: {+ createTab: mocks.createTab,+ closeTab: mocks.closeTab,+ registerWebview: mocks.registerWebview,+ getPreviewConfig: mocks.getPreviewConfig,+ },+}));++vi.mock("~/components/preview/usePreviewBridge", () => ({+ usePreviewBridge: () => undefined,+}));++vi.mock("./browserRecording", () => ({+ useActiveBrowserRecordingTabIds: () => mocks.activeRecordings,+ stopBrowserRecording: async () => null,+}));++import {+ __resetClientSettingsPersistenceForTests,+ ensureClientSettingsHydrated,+} from "~/hooks/useSettings";+import { useBrowserSurfaceStore } from "./browserSurfaceStore";+import * as desktopTabLifetime from "./desktopTabLifetime";+import { HostedBrowserWebview } from "./HostedBrowserWebview";++let renderer: ReactTestRenderer | undefined;++function deferred<A>() {+ let resolve!: (value: A) => void;+ let reject!: (error: unknown) => void;+ const promise = new Promise<A>((resolvePromise, rejectPromise) => {+ resolve = resolvePromise;+ reject = rejectPromise;+ });+ return { promise, resolve, reject };+}++beforeEach(() => {+ __resetClientSettingsPersistenceForTests();+ useBrowserSurfaceStore.setState({ activityByTabId: {}, byTabId: {} });+ mocks.getClientSettings.mockReset();+ mocks.setClientSettings.mockReset().mockResolvedValue(undefined);+ mocks.createTab.mockReset().mockResolvedValue(undefined);+ mocks.closeTab.mockReset().mockResolvedValue(undefined);+ mocks.registerWebview.mockReset().mockResolvedValue(undefined);+ mocks.getPreviewConfig.mockReset().mockResolvedValue({+ partition: "persist:t3-preview-work",+ webPreferences: "contextIsolation=yes",+ preloadUrl: null,+ });+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);+ vi.stubGlobal("window", globalThis);+ vi.stubGlobal("navigator", { platform: "Linux" });+ vi.stubGlobal(+ "requestAnimationFrame",+ vi.fn(() => 0),+ );+ vi.stubGlobal("cancelAnimationFrame", vi.fn());+ vi.spyOn(console, "error").mockImplementation(() => undefined);+});++afterEach(async () => {+ vi.useFakeTimers();+ await act(() => renderer?.unmount());+ renderer = undefined;+ await vi.advanceTimersByTimeAsync(0);+ vi.useRealTimers();+ __resetClientSettingsPersistenceForTests();+ useBrowserSurfaceStore.setState({ activityByTabId: {}, byTabId: {} });+ vi.restoreAllMocks();+ vi.unstubAllGlobals();+});++describe("HostedBrowserWebview settings hydration", () => {+ it("starts a retained background tab only after a settings read succeeds on retry", async () => {+ const firstRead = deferred<ClientSettings | null>();+ const retryRead = deferred<ClientSettings | null>();+ const tabCreation = deferred<void>();+ mocks.getClientSettings+ .mockReturnValueOnce(firstRead.promise)+ .mockReturnValueOnce(retryRead.promise);+ mocks.createTab.mockReturnValueOnce(tabCreation.promise);+ const acquire = vi.spyOn(desktopTabLifetime, "acquireDesktopTab");+ const createGuest = vi.fn((_attributes: unknown) =>+ Object.assign(new EventTarget(), { getWebContentsId: () => 41 }),+ );+ const threadRef = {+ environmentId: EnvironmentId.make("host-settings-retry"),+ threadId: ThreadId.make("thread-settings-retry"),+ };+ const runtimeTabId = "retained-background-tab";+ useBrowserSurfaceStore.getState().acquireActivity(runtimeTabId);++ await act(() => {+ renderer = create(+ <HostedBrowserWebview+ threadRef={threadRef}+ tabId="server-tab"+ runtimeTabId={runtimeTabId}+ initialUrl="https://example.com"+ viewport={FILL_PREVIEW_VIEWPORT}+ pictureInPicture={false}+ profileId="work"+ zoomFactor={1.25}+ />,+ {+ createNodeMock: (element) =>+ element.type === "webview"+ ? createGuest(element.props)+ : { scrollLeft: 0, scrollTop: 0, scrollTo: () => undefined },+ },+ );+ });++ expect(mocks.getClientSettings).toHaveBeenCalledOnce();+ expect(acquire).not.toHaveBeenCalled();+ expect(createGuest).not.toHaveBeenCalled();+ expect(mocks.createTab).not.toHaveBeenCalled();++ const failure = new Error("Saved settings are unavailable");+ await act(async () => {+ const hydration = ensureClientSettingsHydrated();+ firstRead.reject(failure);+ await expect(hydration).rejects.toBe(failure);+ });+ expect(acquire).not.toHaveBeenCalled();+ expect(createGuest).not.toHaveBeenCalled();+ expect(mocks.createTab).not.toHaveBeenCalled();++ let retry!: Promise<void>;+ await act(() => {+ retry = ensureClientSettingsHydrated();+ });+ expect(mocks.getClientSettings).toHaveBeenCalledTimes(2);+ expect(acquire).not.toHaveBeenCalled();+ expect(createGuest).not.toHaveBeenCalled();+ expect(mocks.createTab).not.toHaveBeenCalled();++ await act(async () => {+ retryRead.resolve({+ ...DEFAULT_CLIENT_SETTINGS,+ browserDefaultZoomFactor: 1.25,+ browserDefaultAppearance: "dark",+ browserProfiles: [{ id: "work", name: "Work", kind: "persistent" }],+ browserDefaultProfileId: "work",+ });+ await retry;+ });++ expect(acquire).toHaveBeenCalledExactlyOnceWith(runtimeTabId);+ expect(mocks.getPreviewConfig).toHaveBeenCalledExactlyOnceWith(threadRef.environmentId, "work");+ expect(createGuest).toHaveBeenCalledOnce();+ expect(createGuest).toHaveBeenCalledWith(+ expect.objectContaining({+ partition: "persist:t3-preview-work",+ src: "https://example.com",+ }),+ );+ expect(mocks.createTab).toHaveBeenCalledExactlyOnceWith(runtimeTabId, {+ zoomFactor: 1.25,+ colorScheme: "dark",+ });+ expect(mocks.registerWebview).not.toHaveBeenCalled();++ await act(async () => {+ tabCreation.resolve();+ await tabCreation.promise;+ });+ expect(mocks.registerWebview).toHaveBeenCalledExactlyOnceWith(runtimeTabId, 41);+ expect(mocks.closeTab).not.toHaveBeenCalled();+ expect(mocks.setClientSettings).not.toHaveBeenCalled();+ });+});diff --git a/apps/web/src/browser/HostedBrowserWebview.tsx b/apps/web/src/browser/HostedBrowserWebview.tsxindex 0f01960ce52..42d5bcfb35b 100644--- a/apps/web/src/browser/HostedBrowserWebview.tsx+++ b/apps/web/src/browser/HostedBrowserWebview.tsx@@ -6,6 +6,7 @@ import { useCallback, useEffect, useRef, useState } from "react";import { previewBridge } from "~/components/preview/previewBridge";import { usePreviewBridge } from "~/components/preview/usePreviewBridge";+import { useClientSettingsHydrated } from "~/hooks/useSettings";import { cn, isMacPlatform } from "~/lib/utils";import { resolveBrowserSurfacePanelRect, useBrowserSurfaceStore } from "./browserSurfaceStore";@@ -66,6 +67,7 @@ export function HostedBrowserWebview(props: {zoomFactor,profileId,} = props;+ const clientSettingsHydrated = useClientSettingsHydrated();const config = usePreviewWebviewConfig(threadRef.environmentId, profileId);const [initialSrc] = useState(() => initialUrl ?? "about:blank");const tabLeaseRef = useRef<AcquiredDesktopTab | null>(null);@@ -94,6 +96,7 @@ export function HostedBrowserWebview(props: {usePreviewBridge({ threadRef, tabId, runtimeTabId });useEffect(() => {+ if (!clientSettingsHydrated) return;crashRecoveryRef.current = INITIAL_WEBVIEW_CRASH_RECOVERY_STATE;const lease = acquireDesktopTab(runtimeTabId);tabLeaseRef.current = lease;@@ -101,7 +104,7 @@ export function HostedBrowserWebview(props: {if (tabLeaseRef.current === lease) tabLeaseRef.current = null;lease.release();};- }, [runtimeTabId]);+ }, [clientSettingsHydrated, runtimeTabId]);const [webviewGeneration, setWebviewGeneration] = useState(0);const [recoverySrc, setRecoverySrc] = useState(initialSrc);@@ -118,7 +121,7 @@ export function HostedBrowserWebview(props: {useEffect(() => {const webview = webviewRef.current;const bridge = previewBridge;- if (!webview || !config || !bridge) return;+ if (!clientSettingsHydrated || !webview || !config || !bridge) return;let disposed = false;let recoveryTimeout: ReturnType<typeof setTimeout> | null = null;const register = () => {@@ -164,7 +167,7 @@ export function HostedBrowserWebview(props: {webview.removeEventListener("dom-ready", register);webview.removeEventListener("render-process-gone", recoverGuest);};- }, [config, initialSrc, runtimeTabId, webviewGeneration]);+ }, [clientSettingsHydrated, config, initialSrc, runtimeTabId, webviewGeneration]);const active = presentation.visible && presentation.rect !== null;const lastRect = presentation.rect;@@ -249,7 +252,7 @@ export function HostedBrowserWebview(props: {wrapper.scrollTo({ left: 0, top: 0 });}, [runtimeTabId, viewport._tag, viewportHeight, viewportWidth]);- if (!config) return null;+ if (!clientSettingsHydrated || !config) return null;const renderingActive = active || backgroundActivity || pictureInPicture || recordingActive;const wrapperStyle = resolveHostedBrowserWebviewWrapperStyle({diff --git a/apps/web/src/browser/browserDefaults.test.ts b/apps/web/src/browser/browserDefaults.test.tsindex bac9600c182..ed86cde1c9c 100644--- a/apps/web/src/browser/browserDefaults.test.ts+++ b/apps/web/src/browser/browserDefaults.test.ts@@ -1,15 +1,17 @@import { describe, expect, it, vi } from "vite-plus/test";import { DEFAULT_BROWSER_PROFILE_ID, INCOGNITO_BROWSER_PROFILE_ID } from "@t3tools/contracts";+import { ensureClientSettingsHydrated } from "~/hooks/useSettings";+const settings = vi.hoisted(() => ({ current: {} as Record<string, unknown> }));vi.mock("~/hooks/useSettings", () => ({getClientSettings: () => settings.current,useClientSettings: () => undefined,- ensureClientSettingsHydrated: () => Promise.resolve(),+ ensureClientSettingsHydrated: vi.fn(async () => undefined),}));-const { getBrowserDefaults } = await import("./browserDefaults");+const { getBrowserDefaults, resolveBrowserDefaults } = await import("./browserDefaults");const withDefaultProfile = (browserDefaultProfileId: string) => {settings.current = {@@ -41,3 +43,22 @@ describe("getBrowserDefaults profile resolution", () => {);});});++describe("resolveBrowserDefaults", () => {+ it("rejects failed reads and uses the saved profile after a successful retry", async () => {+ withDefaultProfile("work");+ settings.current.browserDefaultZoomFactor = 1.25;+ settings.current.browserDefaultAppearance = "dark";+ const failure = new Error("Settings read failed");+ vi.mocked(ensureClientSettingsHydrated).mockRejectedValueOnce(failure);++ await expect(resolveBrowserDefaults()).rejects.toBe(failure);+ await expect(resolveBrowserDefaults()).resolves.toMatchObject({+ viewport: { _tag: "fill" },+ zoomFactor: 1.25,+ appearance: "dark",+ autoShowFloatingPreview: true,+ profileId: "work",+ });+ });+});diff --git a/apps/web/src/browser/browserDefaults.ts b/apps/web/src/browser/browserDefaults.tsindex eaae409568a..6141b1a52fa 100644--- a/apps/web/src/browser/browserDefaults.ts+++ b/apps/web/src/browser/browserDefaults.ts@@ -79,6 +79,7 @@ export function getBrowserDefaults(): BrowserDefaults {* Opening a preview is asynchronous anyway, and before hydration the snapshot* is the schema defaults rather than the user's — a tab opened in that window* would be born at the wrong viewport, zoom and appearance and never corrected.+ * Read failures reject so a new tab cannot use the wrong profile or viewport.*/export async function resolveBrowserDefaults(): Promise<BrowserDefaults> {await ensureClientSettingsHydrated();diff --git a/apps/web/src/browser/browserLinkTarget.test.ts b/apps/web/src/browser/browserLinkTarget.test.tsindex 94f97001c96..a60362c43bd 100644--- a/apps/web/src/browser/browserLinkTarget.test.ts+++ b/apps/web/src/browser/browserLinkTarget.test.ts@@ -1,6 +1,16 @@-import { describe, expect, it } from "vite-plus/test";+import type { BrowserLinkTarget } from "@t3tools/contracts";+import { describe, expect, it, vi } from "vite-plus/test";-import { resolveLinkTarget } from "./browserLinkTarget";+import { ensureClientSettingsHydrated } from "~/hooks/useSettings";++import { resolveBrowserLinkTargetPreference, resolveLinkTarget } from "./browserLinkTarget";++const settings = vi.hoisted(() => ({ browserLinkTarget: "system" as BrowserLinkTarget }));++vi.mock("~/hooks/useSettings", () => ({+ ensureClientSettingsHydrated: vi.fn(async () => undefined),+ getClientSettings: () => settings,+}));const click = { metaKey: false, ctrlKey: false };@@ -67,3 +77,17 @@ describe("resolveLinkTarget", () => {}});});++describe("resolveBrowserLinkTargetPreference", () => {+ it.each(["system", "app"] as const)(+ "rejects failed reads instead of using the current %s preference",+ async (preference) => {+ settings.browserLinkTarget = preference;+ const failure = new Error("Settings read failed");+ vi.mocked(ensureClientSettingsHydrated).mockRejectedValueOnce(failure);++ await expect(resolveBrowserLinkTargetPreference()).rejects.toBe(failure);+ await expect(resolveBrowserLinkTargetPreference()).resolves.toBe(preference);+ },+ );+});diff --git a/apps/web/src/browser/browserLinkTarget.ts b/apps/web/src/browser/browserLinkTarget.tsindex d0377557274..7ecffb4593d 100644--- a/apps/web/src/browser/browserLinkTarget.ts+++ b/apps/web/src/browser/browserLinkTarget.ts@@ -55,6 +55,7 @@ export function isWebUrl(url: string): boolean {* hydration the snapshot is the schema default ("system"), so a link clicked* in the first moments after launch would ignore a persisted "app" — opening* is asynchronous anyway, so waiting costs nothing the user can see.+ * Read failures reject rather than choosing a browser without the saved preference.*/export async function resolveBrowserLinkTargetPreference(): Promise<BrowserLinkTarget> {await ensureClientSettingsHydrated();diff --git a/apps/web/src/browser/browserRecording.test.ts b/apps/web/src/browser/browserRecording.test.tsindex 49145f314e9..5cfe614f298 100644--- a/apps/web/src/browser/browserRecording.test.ts+++ b/apps/web/src/browser/browserRecording.test.ts@@ -5,6 +5,8 @@ import {} from "@t3tools/contracts";import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";+import { ensureClientSettingsHydrated } from "~/hooks/useSettings";+const {clientSettings,events,@@ -241,6 +243,35 @@ describe("browser recording", () => {await stopBrowserRecording("recording-tab");});+ it("clears a failed settings read before retrying recording", async () => {+ const tabId = "settings-read-failure-tab";+ const error = new Error("Settings read failed");+ vi.mocked(ensureClientSettingsHydrated).mockRejectedValueOnce(error);++ await expect(startBrowserRecording(tabId)).rejects.toBe(error);++ expect(readActiveBrowserRecordingTabIds()).toEqual(new Set());+ expect(useBrowserSurfaceStore.getState().activityByTabId[tabId]).toBeUndefined();+ expect(animationFrameCount).toBe(0);+ expect(startScreencast).not.toHaveBeenCalled();+ expect(stopScreencast).not.toHaveBeenCalled();+ expect(getDisplayMedia).not.toHaveBeenCalled();+ expect(FakeMediaRecorder.instances).toHaveLength(0);++ clientSettings.browserRecordingFrameRate = 60;+ await startBrowserRecording(tabId);++ expect(getDisplayMedia).toHaveBeenCalledWith({+ audio: false,+ video: { frameRate: { max: 60 } },+ });+ await stopBrowserRecording(tabId);++ expect(startScreencast).toHaveBeenCalledOnce();+ expect(readActiveBrowserRecordingTabIds()).toEqual(new Set());+ expect(useBrowserSurfaceStore.getState().activityByTabId[tabId]).toBeUndefined();+ });+it("stops the native stream when MediaRecorder cleanup fails", async () => {const stopTrack = vi.fn();getDisplayMedia.mockResolvedValueOnce({diff --git a/apps/web/src/browser/browserRecording.ts b/apps/web/src/browser/browserRecording.tsindex 73bc2708ddf..c7825961abb 100644--- a/apps/web/src/browser/browserRecording.ts+++ b/apps/web/src/browser/browserRecording.ts@@ -516,10 +516,12 @@ export async function startBrowserRecording(activeRecordings.set(tabId, recording);publishActiveRecordingTabIds();try {- const frameRatePromise = ensureClientSettingsHydrated().then(- () => getClientSettings().browserRecordingFrameRate,- );- const [frameRate] = await Promise.all([frameRatePromise, waitForBrowserRecordingPaint()]);+ await ensureClientSettingsHydrated().catch((cause: unknown) => {+ clearActiveRecording(recording);+ throw cause;+ });+ const frameRate = getClientSettings().browserRecordingFrameRate;+ await waitForBrowserRecordingPaint();const throwIfStartupCancelled = async (): Promise<void> => {// Once a grant starts, a stop lets startup finish so the caller receives an artifact.// Only a contended start can be cancelled before it reaches native capture.diff --git a/apps/web/src/browser/desktopTabLifetime.test.ts b/apps/web/src/browser/desktopTabLifetime.test.tsindex 80bfa0d275d..c5338ecf4cc 100644--- a/apps/web/src/browser/desktopTabLifetime.test.ts+++ b/apps/web/src/browser/desktopTabLifetime.test.ts@@ -1,6 +1,7 @@import {DEFAULT_PREVIEW_APPEARANCE,DEFAULT_PREVIEW_ZOOM_FACTOR,+ DEFAULT_CLIENT_SETTINGS,EnvironmentId,ThreadId,} from "@t3tools/contracts";@@ -21,8 +22,10 @@ vi.mock("./browserRecording", () => ({}));import { acquireDesktopTab } from "./desktopTabLifetime";+import * as browserDefaults from "./browserDefaults";+import { __setClientSettingsForTests } from "~/hooks/useSettings";-/** Client settings are unset in tests, so creation carries the schema defaults. */+/** Tests load default settings unless they select other preferences. */const DEFAULT_TAB_STATE = {zoomFactor: DEFAULT_PREVIEW_ZOOM_FACTOR,colorScheme: DEFAULT_PREVIEW_APPEARANCE,@@ -31,6 +34,7 @@ import { previewRuntimeTabId } from "./previewRuntimeTabId";describe("desktopTabLifetime", () => {beforeEach(() => {+ __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS);closeTab.mockClear();createTab.mockClear();stopBrowserRecording.mockClear();@@ -40,6 +44,35 @@ describe("desktopTabLifetime", () => {afterEach(() => {vi.useRealTimers();vi.unstubAllGlobals();+ vi.restoreAllMocks();+ });++ it("does not create a desktop tab after a failed settings read and permits a later retry", async () => {+ vi.useFakeTimers();+ const failure = new Error("Settings read failed");+ vi.spyOn(browserDefaults, "resolveBrowserDefaults").mockRejectedValueOnce(failure);+ const failed = acquireDesktopTab("tab_settings_retry");++ await expect(failed.ready).rejects.toBe(failure);+ expect(createTab).not.toHaveBeenCalled();+ failed.release();+ await vi.advanceTimersByTimeAsync(0);++ __setClientSettingsForTests({+ ...DEFAULT_CLIENT_SETTINGS,+ browserDefaultZoomFactor: 1.25,+ browserDefaultAppearance: "dark",+ });+ createTab.mockResolvedValueOnce(undefined);+ const retry = acquireDesktopTab("tab_settings_retry");+ await retry.ready;++ expect(createTab).toHaveBeenCalledExactlyOnceWith("tab_settings_retry", {+ zoomFactor: 1.25,+ colorScheme: "dark",+ });+ retry.release();+ await vi.advanceTimersByTimeAsync(0);});it("shares tab creation readiness across concurrent leases", async () => {diff --git a/apps/web/src/browser/openFileInPreview.ts b/apps/web/src/browser/openFileInPreview.tsindex f506e42e73e..a320e3ba34d 100644--- a/apps/web/src/browser/openFileInPreview.ts+++ b/apps/web/src/browser/openFileInPreview.ts@@ -38,6 +38,14 @@ export class BrowserPreviewUnavailableError extends Data.TaggedError(readonly message: string;}> {}+export class BrowserSettingsReadError extends Data.TaggedError("BrowserSettingsReadError")<{+ readonly cause: unknown;+}> {+ override get message(): string {+ return "Saved browser settings could not be loaded.";+ }+}+export type OpenPreviewMutation<E = unknown> = (input: {readonly environmentId: EnvironmentId;readonly input: PreviewOpenInput;@@ -47,8 +55,13 @@ export async function openUrlInPreview<E>(input: {readonly threadRef: ScopedThreadRef;readonly url: string;readonly openPreview: OpenPreviewMutation<E>;-}): Promise<AtomCommandResult<void, E>> {- const defaults = await resolveBrowserDefaults();+}): Promise<AtomCommandResult<void, E | BrowserSettingsReadError>> {+ const defaults = await resolveBrowserDefaults().catch(+ (cause: unknown) => new BrowserSettingsReadError({ cause }),+ );+ if (defaults instanceof BrowserSettingsReadError) {+ return AsyncResult.failure(Cause.fail(defaults));+ }const result = await input.openPreview({environmentId: input.threadRef.environmentId,input: {@@ -82,7 +95,12 @@ export async function openFileInPreview<AssetError, PreviewError>(input: {readonly input: { readonly resource: AssetResource };}) => Promise<AtomCommandResult<AssetCreateUrlResult, AssetError>>;readonly openPreview: OpenPreviewMutation<PreviewError>;-}): Promise<AtomCommandResult<void, AssetError | PreviewError | BrowserPreviewUnavailableError>> {+}): Promise<+ AtomCommandResult<+ void,+ AssetError | PreviewError | BrowserPreviewUnavailableError | BrowserSettingsReadError+ >+> {if (!isPreviewSupportedInRuntime()) {return AsyncResult.failure(Cause.fail(diff --git a/apps/web/src/browser/useOpenLink.ts b/apps/web/src/browser/useOpenLink.tsindex 0e9bf721f82..2a1d122eedb 100644--- a/apps/web/src/browser/useOpenLink.ts+++ b/apps/web/src/browser/useOpenLink.ts@@ -1,5 +1,8 @@import type { ScopedThreadRef } from "@t3tools/contracts";-import { isAtomCommandInterrupted } from "@t3tools/client-runtime/state/runtime";+import {+ isAtomCommandInterrupted,+ squashAtomCommandFailure,+} from "@t3tools/client-runtime/state/runtime";import { useCallback } from "react";import { recordVisitForThread } from "~/browserHistoryStore";@@ -12,7 +15,7 @@ import {resolveBrowserLinkTargetPreference,resolveLinkTarget,} from "./browserLinkTarget";-import { openUrlInPreview } from "./openFileInPreview";+import { BrowserSettingsReadError, openUrlInPreview } from "./openFileInPreview";const NO_MODIFIER = { metaKey: false, ctrlKey: false } as const;@@ -24,8 +27,8 @@ const NO_MODIFIER = { metaKey: false, ctrlKey: false } as const;** An in-app open that fails falls back to the system browser rather than* dropping the click: the user asked for the link, and the setting only says- * where it should go first. The returned promise rejects only when that- * fallback fails too, the same way `shell.openExternal` does.+ * where it should go first. Failed settings reads reject without opening a+ * browser. The promise also rejects if the system-browser fallback fails.*/export function useOpenLink(threadRef: ScopedThreadRef | null | undefined): (url: string,@@ -52,6 +55,8 @@ export function useOpenLink(threadRef: ScopedThreadRef | null | undefined): (recordVisitForThread(targetThreadRef, url);return;}+ const failure = squashAtomCommandFailure(result);+ if (failure instanceof BrowserSettingsReadError) throw failure;console.error(result.cause);}const api = readLocalApi();diff --git a/apps/web/src/clientPersistenceStorage.test.ts b/apps/web/src/clientPersistenceStorage.test.tsindex db69fe96c80..a86177b48eb 100644--- a/apps/web/src/clientPersistenceStorage.test.ts+++ b/apps/web/src/clientPersistenceStorage.test.ts@@ -52,22 +52,45 @@ describe("clientPersistenceStorage", () => {expect(readBrowserClientSettings()).toEqual(settings);});- it("reports structured decode failures while preserving the fallback", async () => {+ it.each(["not-json", '{"wordWrap":"invalid"}'])(+ "does not treat invalid saved settings as absent: %s",+ async (value) => {+ const testWindow = getTestWindow();+ testWindow.localStorage.setItem("t3code:client-settings:v1", value);+ const { readBrowserClientSettings } = await import("./clientPersistenceStorage");++ expect(() => readBrowserClientSettings()).toThrow(+ expect.objectContaining({+ _tag: "LocalStorageOperationError",+ operation: "decode",+ storageKey: "t3code:client-settings:v1",+ }),+ );+ expect(testWindow.localStorage.getItem("t3code:client-settings:v1")).toBe(value);+ },+ );++ it("preserves saved settings across a transient read failure", async () => {const testWindow = getTestWindow();- testWindow.localStorage.setItem("t3code:client-settings:v1", "not-json");- const consoleError = vi.spyOn(console, "error").mockImplementation(() => undefined);+ const settings = { ...DEFAULT_CLIENT_SETTINGS, timestampFormat: "12-hour" as const };+ testWindow.localStorage.setItem("t3code:client-settings:v1", JSON.stringify(settings));+ const write = vi.spyOn(testWindow.localStorage, "setItem");+ const failure = new Error("storage unavailable");+ vi.spyOn(testWindow.localStorage, "getItem").mockImplementationOnce(() => {+ throw failure;+ });const { readBrowserClientSettings } = await import("./clientPersistenceStorage");- expect(readBrowserClientSettings()).toBeNull();- expect(consoleError).toHaveBeenCalledWith(- "Could not read persisted client settings.",+ expect(() => readBrowserClientSettings()).toThrow(expect.objectContaining({_tag: "LocalStorageOperationError",- operation: "decode",+ operation: "read",storageKey: "t3code:client-settings:v1",- cause: expect.anything(),+ cause: failure,}),);+ expect(readBrowserClientSettings()).toEqual(settings);+ expect(write).not.toHaveBeenCalled();});it("defaults word wrap on and discards obsolete wrapping preferences", async () => {diff --git a/apps/web/src/clientPersistenceStorage.ts b/apps/web/src/clientPersistenceStorage.tsindex 5c0ba7c6ecc..f39ea63c5a7 100644--- a/apps/web/src/clientPersistenceStorage.ts+++ b/apps/web/src/clientPersistenceStorage.ts@@ -13,12 +13,7 @@ export function readBrowserClientSettings(): ClientSettings | null {return null;}- try {- return getLocalStorageItem(CLIENT_SETTINGS_STORAGE_KEY, ClientSettingsSchema);- } catch (error) {- console.error("Could not read persisted client settings.", error);- return null;- }+ return getLocalStorageItem(CLIENT_SETTINGS_STORAGE_KEY, ClientSettingsSchema);}export function writeBrowserClientSettings(settings: ClientSettings): void {diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsxindex af41324be6a..55de831a52e 100644--- a/apps/web/src/components/ChatMarkdown.tsx+++ b/apps/web/src/components/ChatMarkdown.tsx@@ -176,6 +176,7 @@ import {openFileInPreview,openUrlInPreview,BrowserPreviewUnavailableError,+ BrowserSettingsReadError,} from "../browser/openFileInPreview";import { resolveLinkTarget } from "../browser/browserLinkTarget";import { PullRequestLinkPreview } from "./pullRequest/PullRequestLinkPreview";@@ -2357,6 +2358,18 @@ function useChatMarkdownState({}return openUrlInPreview({ threadRef, url, openPreview }).then((result) => {if (result._tag === "Success") recordVisitForThread(threadRef, url);+ else if (!isAtomCommandInterrupted(result)) {+ const error = squashAtomCommandFailure(result);+ if (error instanceof BrowserSettingsReadError) {+ toastManager.add(+ stackedThreadToast({+ type: "error",+ title: "Unable to open link in browser",+ description: error.message,+ }),+ );+ }+ }return result;});},@@ -2789,14 +2802,14 @@ const CHAT_MARKDOWN_COMPONENTS = {}event.preventDefault();event.stopPropagation();- // The click was taken from the shell, so an in-app open that fails- // hands the link to the system browser instead of dropping it.+ // Keep the link here if saved settings could not be read.void openExternalLinkInPreview(href).then((result) => {if (result._tag === "Success" || isAtomCommandInterrupted(result)) return;reportMarkdownActionFailure({ operation: "open-link-in-preview", target: href },result.cause,);+ if (squashAtomCommandFailure(result) instanceof BrowserSettingsReadError) return;void readLocalApi()?.shell.openExternal(href);});}}diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsxindex b27d66c7611..11226371172 100644--- a/apps/web/src/components/ChatView.tsx+++ b/apps/web/src/components/ChatView.tsx@@ -162,6 +162,7 @@ import {useThreadPreviewState,} from "../previewStateStore";import { previewRuntimeTabId } from "../browser/previewRuntimeTabId";+import { BrowserSettingsReadError } from "../browser/openFileInPreview";import { addBrowserSurface } from "./preview/addBrowserSurface";import { closePreviewSession } from "./preview/closePreviewSession";import { ThreadPreviewMiniPlayer } from "./preview/ThreadPreviewMiniPlayer";@@ -3718,6 +3719,18 @@ export default function ChatView(props: ChatViewProps) {threadRef: activeThreadRef,openPreview,...(profileId === undefined ? {} : { profileId }),+ }).then((result) => {+ if (result._tag !== "Failure" || isAtomCommandInterrupted(result)) return;+ const error = squashAtomCommandFailure(result);+ if (error instanceof BrowserSettingsReadError) {+ toastManager.add(+ stackedThreadToast({+ type: "error",+ title: "Unable to open browser",+ description: error.message,+ }),+ );+ }});},[activeThreadRef, openPreview],diff --git a/apps/web/src/components/ThreadTerminalDrawer.test.ts b/apps/web/src/components/ThreadTerminalDrawer.test.tsindex d9dcd6e7993..1624a739bb1 100644--- a/apps/web/src/components/ThreadTerminalDrawer.test.ts+++ b/apps/web/src/components/ThreadTerminalDrawer.test.ts@@ -1,11 +1,98 @@-import { describe, expect, it } from "vite-plus/test";+import { afterEach, describe, expect, it, vi } from "vite-plus/test";import {shouldClearTerminalSelectionAction,shouldHandleTerminalExit,+ terminalContextMenuItems,terminalSelectionLineRange,+ terminalSelectionMenuItems,+ terminalThemeFromApp,} from "./ThreadTerminalDrawer";+describe("terminal selection menus", () => {+ it("omits Add to chat when the terminal has no chat target", () => {+ expect(terminalSelectionMenuItems().map(({ id }) => id)).toEqual(["add-to-chat", "copy"]);+ expect(terminalContextMenuItems({ hasSelection: true }).map(({ id }) => id)).toEqual([+ "add-to-chat",+ "copy",+ "paste",+ ]);++ expect(terminalSelectionMenuItems({ canAddToChat: false }).map(({ id }) => id)).toEqual([+ "copy",+ ]);+ expect(+ terminalContextMenuItems({ hasSelection: true, canAddToChat: false }).map(({ id }) => id),+ ).toEqual(["copy", "paste"]);+ });+});++describe("terminalThemeFromApp", () => {+ afterEach(() => {+ vi.unstubAllGlobals();+ });++ it("uses terminal colors inherited by the mount instead of a light document theme", () => {+ const root = { classList: { contains: () => false } };+ const body = {};+ const drawer = {};+ let canvasColor = "#000";+ const colors: Record<string, [number, number, number, number]> = {+ "#000": [0, 0, 0, 255],+ "#fff": [255, 255, 255, 255],+ "#ddd": [221, 221, 221, 255],+ "#111": [17, 17, 17, 255],+ };++ vi.stubGlobal("document", {+ documentElement: root,+ body,+ querySelector: () => drawer,+ createElement: () => ({+ width: 0,+ height: 0,+ getContext: () => ({+ clearRect: () => undefined,+ fillRect: () => undefined,+ get fillStyle() {+ return canvasColor;+ },+ set fillStyle(value: string) {+ canvasColor = value;+ },+ getImageData: () => ({ data: colors[canvasColor] ?? [0, 0, 0, 0] }),+ }),+ }),+ });+ vi.stubGlobal("getComputedStyle", (element: object) => {+ const local = element === drawer;+ const values = local+ ? {+ "--terminal-background": "#000",+ "--terminal-foreground": "#fff",+ "--terminal-cursor": "#ddd",+ "--terminal-selection-background": "rgba(255, 255, 255, 0.2)",+ }+ : {+ "--terminal-background": "#fff",+ "--terminal-foreground": "#111",+ };+ return {+ backgroundColor: local ? "#000" : "#fff",+ color: local ? "#fff" : "#111",+ colorScheme: local ? "dark" : "light",+ getPropertyValue: (name: string) => values[name as keyof typeof values] ?? "",+ };+ });++ const theme = terminalThemeFromApp();++ expect(theme.background).toEqual({ r: 0, g: 0, b: 0 });+ expect(theme.foreground).toEqual({ r: 255, g: 255, b: 255 });+ expect(theme.cursor).toEqual({ r: 221, g: 221, b: 221 });+ });+});+describe("terminal selection actions", () => {it("clears a pending or currently owned menu when the selection disappears", () => {expect(diff --git a/apps/web/src/components/ThreadTerminalDrawer.tsx b/apps/web/src/components/ThreadTerminalDrawer.tsxindex 9f4956aae68..d9ddf9225bd 100644--- a/apps/web/src/components/ThreadTerminalDrawer.tsx+++ b/apps/web/src/components/ThreadTerminalDrawer.tsx@@ -20,6 +20,7 @@ import {} from "lucide-react";import {type ContextMenuItem,+ type ProviderInstanceId,type ResolvedKeybindingsConfig,type ScopedThreadRef,type ThreadId,@@ -41,6 +42,7 @@ import {import { Popover, PopoverPopup, PopoverTrigger } from "~/components/ui/popover";import { Button } from "~/components/ui/button";import { PanelTabCloseButton } from "~/components/ui/panel-tab-close-button";+import { stackedThreadToast, toastManager } from "~/components/ui/toast";import { readTextFromClipboard, writeTextToClipboard } from "~/hooks/useCopyToClipboard";import { cn } from "~/lib/utils";import { type TerminalContextSelection } from "~/lib/terminalContext";@@ -172,16 +174,23 @@ function terminalFontOptions(family: string, size: number): { family?: string; s}export function terminalThemeFromApp(mountElement?: HTMLElement | null): GhosttyTheme {- const isDark = document.documentElement.classList.contains("dark");- const fallbackBackground = isDark ? "rgb(14, 18, 24)" : "rgb(255, 255, 255)";- const fallbackForeground = isDark ? "rgb(237, 241, 247)" : "rgb(28, 33, 41)";const drawerSurface =mountElement?.closest(".thread-terminal-drawer") ??document.querySelector(".thread-terminal-drawer") ??document.body;const drawerStyles = getComputedStyle(drawerSurface);+ const themeStyles = mountElement ? getComputedStyle(mountElement) : drawerStyles;+ const colorScheme = themeStyles.colorScheme;+ const isDark =+ colorScheme === "dark"+ ? true+ : colorScheme === "light"+ ? false+ : document.documentElement.classList.contains("dark");+ const fallbackBackground = isDark ? "rgb(14, 18, 24)" : "rgb(255, 255, 255)";+ const fallbackForeground = isDark ? "rgb(237, 241, 247)" : "rgb(28, 33, 41)";const bodyStyles = getComputedStyle(document.body);- const themeStyles = getComputedStyle(document.documentElement);+ const rootThemeStyles = getComputedStyle(document.documentElement);const background = normalizeComputedColor(drawerStyles.backgroundColor,normalizeComputedColor(bodyStyles.backgroundColor, fallbackBackground),@@ -190,8 +199,16 @@ export function terminalThemeFromApp(mountElement?: HTMLElement | null): GhosttydrawerStyles.color,normalizeComputedColor(bodyStyles.color, fallbackForeground),);- const terminalBackground = readThemeColor(themeStyles, "--terminal-background", background);- const terminalForeground = readThemeColor(themeStyles, "--terminal-foreground", foreground);+ const terminalBackground = readThemeColor(+ themeStyles,+ "--terminal-background",+ readThemeColor(rootThemeStyles, "--terminal-background", background),+ );+ const terminalForeground = readThemeColor(+ themeStyles,+ "--terminal-foreground",+ readThemeColor(rootThemeStyles, "--terminal-foreground", foreground),+ );const terminalCursor = readThemeColor(themeStyles,"--terminal-cursor",@@ -232,10 +249,14 @@ export function terminalSelectionLineRange(position: {export type TerminalContextMenuAction = "add-to-chat" | "copy" | "paste";-/** Post-selection popup: just the two selection actions, always enabled. */-export function terminalSelectionMenuItems(): ContextMenuItem<"add-to-chat" | "copy">[] {+/** Post-selection popup: available selection actions, always enabled. */+export function terminalSelectionMenuItems(options?: {+ canAddToChat?: boolean;+}): ContextMenuItem<"add-to-chat" | "copy">[] {return [- { id: "add-to-chat", label: "Add to chat" },+ ...(options?.canAddToChat === false+ ? []+ : ([{ id: "add-to-chat", label: "Add to chat" }] satisfies ContextMenuItem<"add-to-chat">[])),{ id: "copy", label: "Copy" },];}@@ -248,11 +269,13 @@ export function terminalSelectionMenuItems(): ContextMenuItem<"add-to-chat" | "c*/export function terminalContextMenuItems(options: {hasSelection: boolean;+ canAddToChat?: boolean;}): ContextMenuItem<TerminalContextMenuAction>[] {+ const { hasSelection, canAddToChat = true } = options;return [- ...terminalSelectionMenuItems().map((item) => ({+ ...terminalSelectionMenuItems({ canAddToChat }).map((item) => ({...item,- disabled: !options.hasSelection,+ disabled: !hasSelection,})),{ id: "paste", label: "Paste" },];@@ -292,8 +315,9 @@ interface TerminalViewportProps {cwd: string;worktreePath?: string | null;runtimeEnv?: Record<string, string>;+ providerInstanceId?: ProviderInstanceId;onSessionExited: () => void;- onAddTerminalContext: (selection: TerminalContextSelection) => void;+ onAddTerminalContext?: (selection: TerminalContextSelection) => void;focusRequestId: number;autoFocus: boolean;visible: boolean;@@ -317,6 +341,7 @@ export function TerminalViewport({cwd,worktreePath,runtimeEnv,+ providerInstanceId,onSessionExited,onAddTerminalContext,focusRequestId,@@ -357,8 +382,9 @@ export function TerminalViewport({onSessionExited();});const handleAddTerminalContext = useEffectEvent((selection: TerminalContextSelection) => {- onAddTerminalContext(selection);+ onAddTerminalContext?.(selection);});+ const canAddSelectionToChat = useEffectEvent(() => onAddTerminalContext !== undefined);const readTerminalLabel = useEffectEvent(() => terminalLabel);const terminalFontFamily = useClientSettings((settings) =>resolveTerminalFontPreference({@@ -383,6 +409,7 @@ export function TerminalViewport({cwd,...(worktreePath !== undefined ? { worktreePath } : {}),...(runtimeEnv ? { env: runtimeEnv } : {}),+ ...(providerInstanceId ? { providerInstanceId } : {}),},});const writeTerminal = useEffectEvent((data: string) =>@@ -635,7 +662,10 @@ export function TerminalViewport({let clicked: TerminalContextMenuAction | null;try {clicked = await localApi.contextMenu.show(- terminalContextMenuItems({ hasSelection: selectionAction !== null }),+ terminalContextMenuItems({+ hasSelection: selectionAction !== null,+ canAddToChat: canAddSelectionToChat(),+ }),{ x: event.clientX, y: event.clientY },);} catch (error) {@@ -648,7 +678,9 @@ export function TerminalViewport({}switch (clicked) {case "add-to-chat":- if (selectionAction) addSelectionToChat(selectionAction.selection);+ if (selectionAction && canAddSelectionToChat()) {+ addSelectionToChat(selectionAction.selection);+ }return;case "copy":if (selectionAction) await copySelection(selectionAction.clipboardText, requestId);@@ -675,7 +707,10 @@ export function TerminalViewport({const requestId = ++selectionActionRequestIdRef.current;openSelectionMenuRequestIdRef.current = requestId;const clicked = await localApi.contextMenu- .show(terminalSelectionMenuItems(), nextAction.position)+ .show(+ terminalSelectionMenuItems({ canAddToChat: canAddSelectionToChat() }),+ nextAction.position,+ ).finally(() => {if (openSelectionMenuRequestIdRef.current === requestId) {openSelectionMenuRequestIdRef.current = null;@@ -686,7 +721,7 @@ export function TerminalViewport({}switch (clicked) {case "add-to-chat":- addSelectionToChat(nextAction.selection);+ if (canAddSelectionToChat()) addSelectionToChat(nextAction.selection);return;case "copy":await copySelection(nextAction.clipboardText, requestId);@@ -768,6 +803,14 @@ export function TerminalViewport({threadRef,openPreview,fallbackToBrowser,+ }).catch((error: unknown) => {+ toastManager.add(+ stackedThreadToast({+ type: "error",+ title: "Unable to open link",+ description: error instanceof Error ? error.message : "An error occurred.",+ }),+ );});return;}diff --git a/apps/web/src/components/cloud/CloudEnvironmentConnectList.test.tsx b/apps/web/src/components/cloud/CloudEnvironmentConnectList.test.tsxnew file mode 100644index 00000000000..e82135dc44b--- /dev/null+++ b/apps/web/src/components/cloud/CloudEnvironmentConnectList.test.tsx@@ -0,0 +1,214 @@+import type { Discovery } from "@t3tools/client-runtime/relay";+import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime";+import { EnvironmentId } from "@t3tools/contracts";+import * as Option from "effect/Option";+import { AsyncResult } from "effect/unstable/reactivity";+import { act, type ButtonHTMLAttributes } from "react";+import { create, type ReactTestRenderer } from "react-test-renderer";+import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";++type DiscoveredEnvironments = Discovery.RelayEnvironmentDiscoveryState["environments"];++const discovery = vi.hoisted(() => ({+ state: null as Discovery.RelayEnvironmentDiscoveryState | null,+ listeners: new Set<() => void>(),+ refreshCommand: Symbol("refresh"),+ registerCommand: Symbol("register"),+ refresh: vi.fn<() => Promise<AtomCommandResult<void, never>>>(),+ register: vi.fn(),+ listEnvironments: vi.fn<() => Promise<DiscoveredEnvironments>>(),+}));++vi.mock("~/state/relay", () => ({+ relayEnvironmentDiscovery: { refresh: discovery.refreshCommand },+}));+vi.mock("~/connection/catalog", () => ({+ environmentCatalog: { register: discovery.registerCommand },+}));+vi.mock("~/state/use-atom-command", () => ({+ useAtomCommand: (command: unknown) =>+ command === discovery.refreshCommand ? discovery.refresh : discovery.register,+}));+vi.mock("~/state/environments", async () => {+ const { useSyncExternalStore } = await import("react");+ const subscribe = (listener: () => void) => {+ discovery.listeners.add(listener);+ return () => discovery.listeners.delete(listener);+ };+ const read = () => {+ if (discovery.state === null) throw new Error("Discovery fixture is not initialized");+ return discovery.state;+ };+ return { useRelayEnvironmentDiscovery: () => useSyncExternalStore(subscribe, read, read) };+});+vi.mock("../ConnectionStatusDot", () => ({ ConnectionStatusDot: () => null }));+vi.mock("../ui/button", () => ({+ Button: ({ children, ...props }: ButtonHTMLAttributes<HTMLButtonElement>) => (+ <button {...props}>{children}</button>+ ),+}));+vi.mock("../ui/toast", () => ({ toastManager: { add: vi.fn() } }));++import { CloudEnvironmentConnectRows } from "./CloudEnvironmentConnectList";++const newMachineId = EnvironmentId.make("new-computer");+const linkedMachines: DiscoveredEnvironments = new Map([+ [+ newMachineId,+ {+ environment: {+ environmentId: newMachineId,+ label: "Work laptop",+ endpoint: {+ httpBaseUrl: "https://relay.example.test",+ wsBaseUrl: "wss://relay.example.test/ws",+ providerKind: "manual",+ },+ linkedAt: "2026-09-05T12:00:00.000Z",+ },+ availability: "online",+ status: Option.none(),+ error: Option.none(),+ },+ ],+]);++let renderer: ReactTestRenderer | null;+let page: EventTarget & { visibilityState: DocumentVisibilityState };+let browserWindow: EventTarget;++function publish(state: Discovery.RelayEnvironmentDiscoveryState) {+ discovery.state = state;+ for (const listener of discovery.listeners) listener();+}++async function mount(refreshWhileEmpty = true) {+ await act(async () => {+ renderer = create(+ <CloudEnvironmentConnectRows+ primaryEnvironmentId={null}+ savedEnvironments={[]}+ showSavedEnvironments+ refreshWhileEmpty={refreshWhileEmpty}+ empty={<p>Waiting for your computer to connect.</p>}+ />,+ );+ });+}++async function advance(milliseconds: number) {+ await act(async () => {+ await vi.advanceTimersByTimeAsync(milliseconds);+ });+}++beforeEach(() => {+ vi.useFakeTimers();+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);+ page = Object.assign(new EventTarget(), { visibilityState: "visible" as const });+ browserWindow = new EventTarget();+ vi.stubGlobal("document", page);+ vi.stubGlobal("window", browserWindow);+ renderer = null;+ discovery.listeners.clear();+ discovery.state = {+ environments: new Map(),+ refreshing: false,+ offline: false,+ error: Option.none(),+ };+ discovery.listEnvironments.mockReset().mockResolvedValue(new Map());+ discovery.refresh.mockReset().mockImplementation(async () => {+ publish({ environments: new Map(), refreshing: true, offline: false, error: Option.none() });+ const environments = await discovery.listEnvironments();+ publish({ environments, refreshing: false, offline: false, error: Option.none() });+ return AsyncResult.success(undefined);+ });+});++afterEach(async () => {+ await act(async () => renderer?.unmount());+ vi.useRealTimers();+ vi.unstubAllGlobals();+});++describe("cloud onboarding discovery", () => {+ it("shows a newly linked computer without remounting and stops polling once found", async () => {+ discovery.listEnvironments+ .mockResolvedValueOnce(new Map())+ .mockResolvedValueOnce(linkedMachines);+ await mount();+ expect(renderer!.root.findByType("p").children).toEqual([+ "Waiting for your computer to connect.",+ ]);++ await advance(5_000);++ expect(renderer!.root.findAllByType("p").map((node) => node.children)).toContainEqual([+ "Work laptop",+ ]);+ expect(renderer!.root.findByType("button").children).toEqual(["Connect"]);+ await advance(30_000);+ expect(discovery.listEnvironments).toHaveBeenCalledTimes(2);+ });++ it("waits while hidden and refreshes immediately when visible again", async () => {+ page.visibilityState = "hidden";+ await mount();+ await advance(30_000);+ expect(discovery.listEnvironments).not.toHaveBeenCalled();++ await act(async () => {+ page.visibilityState = "visible";+ page.dispatchEvent(new Event("visibilitychange"));+ });+ expect(discovery.listEnvironments).toHaveBeenCalledTimes(1);++ page.visibilityState = "hidden";+ page.dispatchEvent(new Event("visibilitychange"));+ browserWindow.dispatchEvent(new Event("focus"));+ await advance(30_000);+ expect(discovery.listEnvironments).toHaveBeenCalledTimes(1);+ });++ it("does not overlap a slow refresh or restart polling after unmount", async () => {+ let resolveRefresh!: (environments: DiscoveredEnvironments) => void;+ const pending = new Promise<DiscoveredEnvironments>((resolve) => {+ resolveRefresh = resolve;+ });+ discovery.listEnvironments.mockResolvedValueOnce(new Map()).mockReturnValueOnce(pending);+ await mount();+ await advance(5_000);+ expect(renderer!.root.findByType("p").children).toEqual([+ "Waiting for your computer to connect.",+ ]);++ browserWindow.dispatchEvent(new Event("focus"));+ page.dispatchEvent(new Event("visibilitychange"));+ await advance(30_000);+ expect(discovery.listEnvironments).toHaveBeenCalledTimes(2);++ await act(async () => renderer!.unmount());+ renderer = null;+ await act(async () => resolveRefresh(new Map()));+ await advance(30_000);+ expect(discovery.listEnvironments).toHaveBeenCalledTimes(2);+ });++ it("pauses while offline and resumes when discovery is online", async () => {+ await mount();+ await act(async () => publish({ ...discovery.state!, offline: true }));+ await advance(30_000);+ expect(discovery.listEnvironments).toHaveBeenCalledTimes(1);++ await act(async () => publish({ ...discovery.state!, offline: false }));+ await advance(5_000);+ expect(discovery.listEnvironments).toHaveBeenCalledTimes(2);+ });++ it("does not add polling to other cloud lists", async () => {+ await mount(false);+ await advance(30_000);+ expect(discovery.listEnvironments).toHaveBeenCalledTimes(1);+ });+});diff --git a/apps/web/src/components/cloud/CloudEnvironmentConnectList.tsx b/apps/web/src/components/cloud/CloudEnvironmentConnectList.tsxindex 460a253812a..7f29c69c320 100644--- a/apps/web/src/components/cloud/CloudEnvironmentConnectList.tsx+++ b/apps/web/src/components/cloud/CloudEnvironmentConnectList.tsx@@ -11,7 +11,7 @@ import {import type { EnvironmentId } from "@t3tools/contracts";import type { RelayClientEnvironmentRecord } from "@t3tools/contracts/relay";import * as Option from "effect/Option";-import { type ReactNode, useCallback, useEffect, useState } from "react";+import { type ReactNode, useCallback, useEffect, useEffectEvent, useState } from "react";import { environmentCatalog } from "~/connection/catalog";import { cn } from "~/lib/utils";@@ -25,6 +25,8 @@ import { Skeleton } from "../ui/skeleton";import { toastManager } from "../ui/toast";import { presentSavedCloudEnvironmentConnection } from "./cloudEnvironmentConnectionPresentation";+const EMPTY_DISCOVERY_REFRESH_INTERVAL_MS = 5_000;+export interface SavedCloudEnvironmentConnection {readonly environmentId: EnvironmentId;readonly connection: EnvironmentConnectionPresentation;@@ -55,11 +57,13 @@ export function CloudEnvironmentConnectRows({primaryEnvironmentId,savedEnvironments,showSavedEnvironments = false,+ refreshWhileEmpty = false,empty = null,}: {readonly primaryEnvironmentId: EnvironmentId | null;readonly savedEnvironments: ReadonlyArray<SavedCloudEnvironmentConnection>;readonly showSavedEnvironments?: boolean;+ readonly refreshWhileEmpty?: boolean;readonly empty?: ReactNode;}) {const environmentsState = useRelayEnvironmentDiscovery();@@ -69,6 +73,10 @@ export function CloudEnvironmentConnectRows({const refreshRelayEnvironments = useAtomCommand(relayEnvironmentDiscovery.refresh, {reportFailure: false,});+ const refreshDiscoveryWhenIdle = useEffectEvent(async () => {+ if (environmentsState.refreshing || environmentsState.offline) return;+ await refreshRelayEnvironments();+ });const connectRelayEnvironment = useCallback((environment: RelayClientEnvironmentRecord) =>registerEnvironment(@@ -89,8 +97,10 @@ export function CloudEnvironmentConnectRows({);useEffect(() => {- void refreshRelayEnvironments();- }, [refreshRelayEnvironments]);+ if (!refreshWhileEmpty || document.visibilityState === "visible") {+ void refreshRelayEnvironments();+ }+ }, [refreshRelayEnvironments, refreshWhileEmpty]);const connectEnvironment = async (environment: RelayClientEnvironmentRecord) => {setConnectingEnvironmentId(environment.environmentId);@@ -132,10 +142,54 @@ export function CloudEnvironmentConnectRows({environment.environmentId !== primaryEnvironmentId &&(showSavedEnvironments || !savedById.has(environment.environmentId)),);+ // Discovery clears its list on refresh, so poll only until a machine appears.+ const shouldRefreshWhileEmpty =+ refreshWhileEmpty && visibleEnvironments.length === 0 && !environmentsState.offline;++ useEffect(() => {+ if (!shouldRefreshWhileEmpty) return;+ let timer: ReturnType<typeof setTimeout> | undefined;+ let disposed = false;+ let pending = false;+ const visible = () => document.visibilityState === "visible";+ const schedule = () => {+ clearTimeout(timer);+ if (!disposed && visible()) {+ timer = setTimeout(() => void refresh(), EMPTY_DISCOVERY_REFRESH_INTERVAL_MS);+ }+ };+ const refresh = async () => {+ if (disposed || pending || !visible()) return;+ clearTimeout(timer);+ pending = true;+ try {+ await refreshDiscoveryWhenIdle();+ } finally {+ pending = false;+ schedule();+ }+ };+ const onFocus = () => void refresh();+ const onVisibilityChange = () => {+ clearTimeout(timer);+ if (visible()) void refresh();+ };++ schedule();+ window.addEventListener("focus", onFocus);+ document.addEventListener("visibilitychange", onVisibilityChange);+ return () => {+ disposed = true;+ clearTimeout(timer);+ window.removeEventListener("focus", onFocus);+ document.removeEventListener("visibilitychange", onVisibilityChange);+ };+ }, [shouldRefreshWhileEmpty]);const standalone = showSavedEnvironments || savedEnvironments.length === 0;if (+ !refreshWhileEmpty &&standalone &&visibleEnvironments.length === 0 &&environmentsState.refreshing &&diff --git a/apps/web/src/components/onboarding/FirstRunGate.tsx b/apps/web/src/components/onboarding/FirstRunGate.tsxnew file mode 100644index 00000000000..a9df5cb00b5--- /dev/null+++ b/apps/web/src/components/onboarding/FirstRunGate.tsx@@ -0,0 +1,244 @@+import { useAtomValue } from "@effect/atom-react";+import { useLocation, useNavigate } from "@tanstack/react-router";+import { Atom } from "effect/unstable/reactivity";+import { RotateCcwIcon } from "lucide-react";+import { useEffect, useLayoutEffect, useState } from "react";++import {+ ensureClientSettingsHydrated,+ useClientSettings,+ useClientSettingsHydrationStatus,+} from "../../hooks/useSettings";+import { mountOnboardingTheme } from "../../hooks/useTheme";+import { useCompleteOnboarding } from "../../onboarding/firstRun";+import {+ isFirstRunWorkspaceProvenanceAuthoritative,+ isFreshFirstRunWorkspace,+ resolveFirstRunDecision,+ resolveHostedFirstRunDecision,+ transitionFirstRunGateState,+ type FirstRunGateState,+} from "../../onboarding/firstRun.logic";+import {+ useAllEnvironmentShellsBootstrapped,+ useProjects,+ useThreadShells,+} from "../../state/entities";+import { useEnvironments } from "../../state/environments";+import { environmentProjects } from "../../state/projects";+import { primaryServerConfigAtom, primaryServerWelcomeAtom } from "../../state/server";+import { environmentShell } from "../../state/shell";+import { environmentThreadShells } from "../../state/threads";+import { Button } from "../ui/button";++/**+ * Holds back authenticated and hosted app trees until the first-run decision+ * is known, so a fresh install never flashes the main screen before the wizard.+ * Nothing renders while pending — no shell, no EventRouter (whose welcome+ * payload would otherwise navigate into a thread), no dialogs.+ *+ * Decision order: a set `onboardingCompletedAt` resolves to the app as soon as+ * settings hydrate (the common case, no server round-trip). A `null` flag also+ * covers installs that predate the field, so it alone is not enough — the gate+ * waits for environment shells to bootstrap and inspects the workspace.+ * Hosted mode instead checks its saved environment catalog. A timeout shows+ * recovery for an unreachable primary server without mounting the app tree.+ */++const FIRST_RUN_DECISION_TIMEOUT_MS = 4_000;++const primaryShellLiveAtom = Atom.make((get) => {+ const serverConfig = get(primaryServerConfigAtom);+ return (+ serverConfig !== null &&+ get(environmentShell.stateValueAtom(serverConfig.environment.environmentId)).status === "live"+ );+}).pipe(Atom.withLabel("web-onboarding-primary-shell-live"));++const workspaceEvidenceLiveAtom = Atom.make((get) => {+ const environmentIds = new Set([+ ...get(environmentProjects.projectsAtom).map((project) => project.environmentId),+ ...get(environmentThreadShells.threadShellsAtom).map((thread) => thread.environmentId),+ ]);++ for (const environmentId of environmentIds) {+ if (get(environmentShell.stateValueAtom(environmentId)).status !== "live") {+ return false;+ }+ }++ return true;+}).pipe(Atom.withLabel("web-onboarding-workspace-evidence-live"));++export function FirstRunGate({+ enabled,+ hostedStatic,+ children,+}: {+ readonly enabled: boolean;+ readonly hostedStatic: boolean;+ readonly children: React.ReactNode;+}) {+ const navigate = useNavigate();+ const pathname = useLocation({ select: (location) => location.pathname });+ const hydrationStatus = useClientSettingsHydrationStatus();+ const hydrated = hydrationStatus === "ready";+ const completeOnboarding = useCompleteOnboarding();+ const onboardingCompletedAt = useClientSettings((settings) => settings.onboardingCompletedAt);+ const bootstrapped = useAllEnvironmentShellsBootstrapped();+ const { environments, isReady: environmentCatalogReady } = useEnvironments();+ const projects = useProjects();+ const threads = useThreadShells();+ const serverConfig = useAtomValue(primaryServerConfigAtom);+ const serverWelcome = useAtomValue(primaryServerWelcomeAtom);+ const primaryShellLive = useAtomValue(primaryShellLiveAtom);+ const workspaceEvidenceLive = useAtomValue(workspaceEvidenceLiveAtom);+ // Within a session settings stay hydrated, so remounts (e.g. returning from+ // the wizard) resolve synchronously instead of blanking a frame.+ const [gateState, setGateState] = useState<FirstRunGateState>(() => ({+ decision:+ (!enabled && !hostedStatic) || (hydrated && onboardingCompletedAt !== null)+ ? "app"+ : "pending",+ stalled: false,+ }));+ const { decision, stalled } = gateState;+ const settingsReadFailed = hydrationStatus === "failed" || hydrationStatus === "retrying";+ const ownsOnboardingTheme = settingsReadFailed || stalled || decision === "wizard";++ useLayoutEffect(() => {+ if (!ownsOnboardingTheme) return;+ return mountOnboardingTheme();+ }, [ownsOnboardingTheme]);++ // A workspace still counts as fresh when its only content is the server's+ // own cwd auto-bootstrap: web mode creates a project + thread from cwd at+ // startup (`autoBootstrapProjectFromCwd` defaults on there), so "no+ // projects at all" would mean `npx t3` users never see the wizard. Any+ // other project, more than one thread, or state in a non-primary+ // environment is real user state — the aggregate hooks span every+ // environment, and a saved remote's project must never read as "the+ // bootstrap project" just because its root string matches the primary cwd.+ const serverCwd = serverConfig?.cwd ?? null;+ const primaryEnvironmentId = serverConfig?.environment.environmentId ?? null;+ const workspaceFresh = isFreshFirstRunWorkspace({+ primaryEnvironmentId,+ serverCwd,+ bootstrapProjectId: serverWelcome?.bootstrapProjectId,+ bootstrapThreadId: serverWelcome?.bootstrapThreadId,+ bootstrapProjectCreated: serverWelcome?.bootstrapProjectCreated,+ bootstrapThreadCreated: serverWelcome?.bootstrapThreadCreated,+ projects,+ threads,+ });++ const { decision: nextDecision, persistCompletion } = hostedStatic+ ? resolveHostedFirstRunDecision({+ hydrated,+ completed: onboardingCompletedAt !== null,+ catalogReady: environmentCatalogReady,+ environmentCount: environments.length,+ })+ : resolveFirstRunDecision({+ enabled,+ hydrated,+ completed: onboardingCompletedAt !== null,+ bootstrapped,+ authoritative: primaryShellLive,+ workspaceAuthoritative: workspaceEvidenceLive,+ workspaceProvenanceAuthoritative: isFirstRunWorkspaceProvenanceAuthoritative({+ welcomeReceived: serverWelcome !== null,+ bootstrapStatus: serverWelcome?.bootstrapStatus ?? null,+ }),+ catalogReady: environmentCatalogReady,+ serverConfigAvailable: serverConfig !== null,+ workspaceFresh,+ projectCount: projects.length,+ threadCount: threads.length,+ });++ useEffect(() => {+ if (decision === "wizard" || !hydrated) return;++ if (persistCompletion && onboardingCompletedAt === null) {+ void completeOnboarding().catch(() => undefined);+ }++ setGateState((state) =>+ transitionFirstRunGateState(state, { type: "evidence", decision: nextDecision }),+ );+ }, [+ completeOnboarding,+ decision,+ hydrated,+ nextDecision,+ onboardingCompletedAt,+ persistCompletion,+ ]);++ // A stalled server read gets a recovery screen, but never mounts the app.+ // The timer starts after settings hydrate so slow local hydration does not+ // show a false connection failure.+ useEffect(() => {+ if (!enabled || decision !== "pending" || !hydrated) return;+ const timer = window.setTimeout(+ () => setGateState((state) => transitionFirstRunGateState(state, { type: "timeout" })),+ FIRST_RUN_DECISION_TIMEOUT_MS,+ );+ return () => window.clearTimeout(timer);+ }, [decision, enabled, hydrated]);++ useEffect(() => {+ if (decision === "wizard" && pathname !== "/welcome") {+ void navigate({ to: "/welcome", replace: true });+ }+ }, [decision, navigate, pathname]);++ if (settingsReadFailed) {+ return <FirstRunRecovery reason="settings" retrying={hydrationStatus === "retrying"} />;+ }+ if (decision !== "app") {+ return stalled ? <FirstRunRecovery reason="connection" /> : null;+ }+ return children;+}++function FirstRunRecovery({+ reason,+ retrying = false,+}: {+ readonly reason: "settings" | "connection";+ readonly retrying?: boolean;+}) {+ const settingsReadFailed = reason === "settings";+ return (+ <main className="flex h-dvh min-h-0 items-center justify-center bg-background px-6 text-foreground">+ <div className="flex max-w-sm flex-col items-center text-center">+ <h1 className="text-lg font-semibold">+ {settingsReadFailed ? "Could not read settings" : "Still connecting"}+ </h1>+ <p className="mt-2 text-sm text-muted-foreground">+ {settingsReadFailed+ ? "Your saved settings could not be loaded."+ : "T3 Code could not confirm this workspace."}+ </p>+ <Button+ className="mt-5"+ size="sm"+ variant="outline"+ disabled={retrying}+ onClick={() => {+ if (settingsReadFailed) {+ void ensureClientSettingsHydrated().catch(() => undefined);+ } else {+ window.location.reload();+ }+ }}+ >+ <RotateCcwIcon />+ {settingsReadFailed ? "Retry" : "Reload"}+ </Button>+ </div>+ </main>+ );+}diff --git a/apps/web/src/components/onboarding/WelcomeWizard.tsx b/apps/web/src/components/onboarding/WelcomeWizard.tsxnew file mode 100644index 00000000000..cb31ef25966--- /dev/null+++ b/apps/web/src/components/onboarding/WelcomeWizard.tsx@@ -0,0 +1,1478 @@+import { useAuth } from "@clerk/react";+import { useAtomValue } from "@effect/atom-react";+import type {+ AgentSessionProjectCandidate,+ EnvironmentId,+ ProjectId,+ ScopedProjectRef,+ ServerConfig,+ ServerProvider,+} from "@t3tools/contracts";+import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";+import {+ isAtomCommandInterrupted,+ squashAtomCommandFailure,+} from "@t3tools/client-runtime/state/runtime";+import { CommandId, ProviderDriverKind, ThreadId } from "@t3tools/contracts";+import * as Schema from "effect/Schema";+import {+ ArrowRightIcon,+ CheckIcon,+ ChevronLeftIcon,+ ChevronRightIcon,+ CloudIcon,+ CopyIcon,+ LinkIcon,+ MonitorIcon,+ TerminalIcon,+ type LucideIcon,+} from "lucide-react";+import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";++import { TYPOGRAPHY_ADVANCED_STORAGE_KEY } from "../../appearanceFonts";+import { useLocalStorage } from "../../hooks/useLocalStorage";+import { mountOnboardingTheme } from "../../hooks/useTheme";+import { hasCloudPublicConfig } from "../../cloud/publicConfig";+import { useT3ConnectAuthPrompt } from "../clerk/useT3ConnectAuthPrompt";+import { useCompleteOnboarding } from "../../onboarding/firstRun";+import {+ partitionOnboardingProjects,+ resolveOnboardingLandingProject,+ resolveOnboardingProjectId,+} from "../../onboarding/projectImport.logic";+import {+ getOnboardingProviderState,+ resolveOnboardingProviderLoginCommand,+ selectOnboardingProvidersByDriver,+} from "../../onboarding/providerReadiness.logic";+import {+ isOnboardingRelayEnvironment,+ resolveOnboardingTargetEnvironment,+} from "../../onboarding/targetEnvironment.logic";+import { useCopyToClipboard } from "../../hooks/useCopyToClipboard";+import { newProjectId, randomUUID } from "../../lib/utils";+import { resolveDefaultProviderModelSelection } from "../../providerInstances";+import { agentSessionImport, agentSessionScan } from "../../state/agentSessions";+import { readProjects, useProjects } from "../../state/entities";+import { useEnvironments, usePrimaryEnvironment } from "../../state/environments";+import { useEnvironmentQuery } from "../../state/query";+import { projectEnvironment } from "../../state/projects";+import { serverEnvironment } from "../../state/server";+import { terminalEnvironment } from "../../state/terminal";+import { useAtomCommand } from "../../state/use-atom-command";+import { connectPairing } from "../../connection/onboarding";+import { isElectron } from "../../env";+import { formatRelativeTimeLabel } from "../../timestampFormat";+import { getProviderSummary } from "../settings/providerStatus";+import { getDriverOption } from "../settings/providerDriverMeta";+import { CloudEnvironmentConnectRows } from "../cloud/CloudEnvironmentConnectList";+import { TerminalViewport } from "../ThreadTerminalDrawer";+import { Button } from "../ui/button";+import { Checkbox } from "../ui/checkbox";+import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible";+import { Input } from "../ui/input";+import { toastManager } from "../ui/toast";+import { cn } from "../../lib/utils";++/**+ * First-run welcome wizard. Rendered as the full-screen `/welcome` route on a+ * fresh install (no completed-onboarding flag, empty workspace). Flow per the+ * onboarding overhaul spec: connection choice → sign-in/pair (remote paths) →+ * agent setup with inline install terminal → project import → main screen.+ * Every step past the connection gate is skippable; the whole wizard is+ * re-runnable by clearing the flag.+ */++type WizardStep = "connection" | "connect-machines" | "pair-direct" | "agents" | "import";++type ConnectionMode = "local" | "connect" | "direct";++/**+ * The machine the agent and import steps run against. Local mode targets the+ * primary environment; the remote modes prefer the machine the user just+ * connected (the most recently added connected non-primary environment), so+ * probing and import happen where their code lives rather than on the local+ * server that happens to serve the app. Deliberately not a persisted+ * "primary machine" concept — just whichever machine fits the chosen path+ * right now, labeled inline on each step.+ */+function useOnboardingTargetEnvironment(+ mode: ConnectionMode,+ pairedEnvironmentId: EnvironmentId | null,+) {+ const { environments } = useEnvironments();+ const primaryEnvironment = usePrimaryEnvironment();+ return resolveOnboardingTargetEnvironment({+ mode,+ environments,+ primaryEnvironment,+ pairedEnvironmentId,+ });+}++const AGENT_ONBOARDING_THREAD_ID = ThreadId.make("onboarding-agent-setup");+const ONBOARDING_STAGES = ["Connect", "Agents", "Projects"] as const;+const SCAN_LIMIT_MESSAGE = "Scan limit reached. Some projects or conversations may be missing.";++export function WelcomeWizard({+ localAvailable,+ onDone,+}: {+ /**+ * Whether the "Local Only" card is offered. True whenever the app is served+ * by an authenticated primary server — desktop, `npx t3`, or a dev server —+ * since that server is "this machine" regardless of the hostname the app+ * was opened from. Only hosted-static (app.t3.codes) has no local server.+ */+ readonly localAvailable: boolean;+ readonly onDone: (projectRef?: ScopedProjectRef) => void;+}) {+ useLayoutEffect(() => mountOnboardingTheme(), []);+ const completeOnboarding = useCompleteOnboarding();+ const [step, setStep] = useState<WizardStep>("connection");+ const [mode, setMode] = useState<ConnectionMode>("local");+ const [pairedEnvironmentId, setPairedEnvironmentId] = useState<EnvironmentId | null>(null);+ const finishingPromiseRef = useRef<Promise<boolean> | null>(null);+ const completionErrorToastIdRef = useRef<ReturnType<typeof toastManager.add> | null>(null);+ const targetEnvironment = useOnboardingTargetEnvironment(mode, pairedEnvironmentId);+ const stageIndex = step === "agents" ? 1 : step === "import" ? 2 : 0;+ const finish = useCallback(+ (projectRef?: ScopedProjectRef) => {+ if (finishingPromiseRef.current !== null) return finishingPromiseRef.current;+ if (completionErrorToastIdRef.current !== null) {+ toastManager.close(completionErrorToastIdRef.current);+ completionErrorToastIdRef.current = null;+ }++ const completion = completeOnboarding()+ .then(() => {+ if (completionErrorToastIdRef.current !== null) {+ toastManager.close(completionErrorToastIdRef.current);+ completionErrorToastIdRef.current = null;+ }+ onDone(projectRef);+ return true;+ })+ .catch(() => {+ const errorToast = {+ type: "error",+ title: "Could not finish setup",+ description: "Your settings could not be saved. Try again.",+ } as const;+ if (completionErrorToastIdRef.current === null) {+ completionErrorToastIdRef.current = toastManager.add(errorToast);+ } else {+ toastManager.update(completionErrorToastIdRef.current, errorToast);+ }+ return false;+ })+ .finally(() => {+ if (finishingPromiseRef.current === completion) {+ finishingPromiseRef.current = null;+ }+ });+ finishingPromiseRef.current = completion;+ return completion;+ },+ [completeOnboarding, onDone],+ );++ return (+ <div className="flex h-dvh min-h-0 flex-col overflow-hidden bg-black text-foreground">+ {isElectron ? (+ <div+ aria-hidden+ className="drag-region h-[var(--workspace-topbar-height)] min-h-[var(--workspace-topbar-height)] shrink-0 wco:pr-[var(--workspace-native-controls-inset)]"+ />+ ) : null}+ <main className="min-h-0 flex-1 overflow-x-hidden overflow-y-auto">+ <div className="mx-auto grid min-h-full w-full max-w-5xl content-center gap-10 px-6 py-12 sm:grid-cols-[170px_minmax(0,1fr)] sm:gap-14 sm:px-10 lg:px-12">+ <aside className="flex min-w-0 flex-col justify-between sm:min-h-72">+ <nav+ aria-label="Setup progress"+ className="flex flex-wrap gap-x-5 gap-y-1 sm:flex-col sm:gap-1"+ >+ {ONBOARDING_STAGES.map((stage, index) => (+ <div+ key={stage}+ aria-current={index === stageIndex ? "step" : undefined}+ className={cn(+ "flex min-h-9 items-center gap-2.5 text-sm",+ index < stageIndex+ ? "text-success-foreground"+ : index === stageIndex+ ? "text-foreground"+ : "text-muted-foreground/60",+ )}+ >+ {index < stageIndex ? (+ <CheckIcon className="size-3.5" />+ ) : (+ <span className="w-3.5 font-mono text-xs">0{index + 1}</span>+ )}+ <span>{stage}</span>+ </div>+ ))}+ </nav>+ {targetEnvironment ? (+ <div className="mt-8 hidden items-center gap-2 text-xs text-muted-foreground sm:flex">+ <span className="size-1.5 rounded-full bg-success" />+ <span className="truncate">{targetEnvironment.label}</span>+ </div>+ ) : null}+ </aside>++ <section className="min-w-0">+ {step === "connection" ? (+ <ConnectionStep+ localAvailable={localAvailable}+ localLabel={targetEnvironment?.label ?? "This computer"}+ onLocal={() => {+ setMode("local");+ setPairedEnvironmentId(null);+ setStep("agents");+ }}+ onConnect={() => {+ setMode("connect");+ setPairedEnvironmentId(null);+ setStep("connect-machines");+ }}+ onDirect={() => {+ setMode("direct");+ setPairedEnvironmentId(null);+ setStep("pair-direct");+ }}+ />+ ) : step === "connect-machines" ? (+ <ConnectMachinesStep+ onBack={() => setStep("connection")}+ onContinue={() => setStep("agents")}+ />+ ) : step === "pair-direct" ? (+ <PairDirectStep+ onBack={() => setStep("connection")}+ onPaired={(environmentId) => {+ setPairedEnvironmentId(environmentId);+ setStep("agents");+ }}+ />+ ) : step === "agents" ? (+ <AgentsStep+ mode={mode}+ pairedEnvironmentId={pairedEnvironmentId}+ onBack={() =>+ setStep(+ mode === "local"+ ? "connection"+ : mode === "connect"+ ? "connect-machines"+ : "pair-direct",+ )+ }+ onContinue={() => setStep("import")}+ onSkip={() => setStep("import")}+ />+ ) : (+ <ImportStep+ mode={mode}+ pairedEnvironmentId={pairedEnvironmentId}+ onBack={() => setStep("agents")}+ onDone={finish}+ />+ )}+ </section>+ </div>+ </main>+ </div>+ );+}++// ── Step 1: connection choice ────────────────────────────────++function ConnectionStep({+ localAvailable,+ localLabel,+ onLocal,+ onConnect,+ onDirect,+}: {+ readonly localAvailable: boolean;+ readonly localLabel: string;+ readonly onLocal: () => void;+ readonly onConnect: () => void;+ readonly onDirect: () => void;+}) {+ const cloudEnabled = hasCloudPublicConfig();+ const [choice, setChoice] = useState<"local" | "connect" | "direct">(+ localAvailable ? "local" : cloudEnabled ? "connect" : "direct",+ );++ const advance = () => {+ if (choice === "local") onLocal();+ else if (choice === "connect") onConnect();+ else onDirect();+ };++ return (+ <>+ <h1 className="text-3xl font-semibold text-foreground sm:text-[34px]">Where is your code?</h1>+ <p className="mt-2.5 text-sm text-muted-foreground">Choose where your agents will run.</p>+ <div className="mt-8 border-t border-border">+ {localAvailable ? (+ <ConnectionOption+ icon={MonitorIcon}+ title="This computer"+ description={localLabel}+ truncateDescription+ detail="No account"+ selected={choice === "local"}+ onSelect={() => setChoice("local")}+ />+ ) : null}+ {cloudEnabled ? (+ <ConnectionOption+ icon={CloudIcon}+ title="T3 Connect"+ description="Your computers, wherever you are"+ detail="Sign in"+ selected={choice === "connect"}+ onSelect={() => setChoice("connect")}+ />+ ) : null}+ <ConnectionOption+ icon={LinkIcon}+ title="Pair a server"+ description="Local network or Tailscale"+ detail="Pairing link"+ selected={choice === "direct"}+ onSelect={() => setChoice("direct")}+ />+ </div>+ <div className="mt-7 flex justify-end">+ <Button className="gap-2" onClick={advance}>+ Continue+ <ArrowRightIcon className="size-3.5" />+ </Button>+ </div>+ </>+ );+}++function ConnectionOption({+ icon: Icon,+ title,+ description,+ truncateDescription = false,+ detail,+ selected,+ onSelect,+}: {+ readonly icon: LucideIcon;+ readonly title: string;+ readonly description: string;+ readonly truncateDescription?: boolean;+ readonly detail: string;+ readonly selected: boolean;+ readonly onSelect: () => void;+}) {+ return (+ <button+ type="button"+ aria-pressed={selected}+ onClick={onSelect}+ className={cn(+ "group flex min-h-20 w-full cursor-pointer items-center gap-4 border-b border-border px-1 py-3 text-left transition-colors",+ "outline-none focus-visible:bg-accent focus-visible:ring-1 focus-visible:ring-ring",+ selected+ ? "text-foreground"+ : "text-muted-foreground hover:bg-accent/50 hover:text-foreground",+ )}+ >+ <Icon+ className={cn("size-[18px]", selected ? "text-success-foreground" : "text-icon-muted")}+ />+ <span className="min-w-0 flex-1">+ <span className="block text-sm font-medium">{title}</span>+ <span+ className={cn(+ "mt-1 block text-xs text-muted-foreground",+ truncateDescription ? "truncate" : "break-words whitespace-normal",+ )}+ >+ {description}+ </span>+ </span>+ <span className="hidden text-xs text-muted-foreground sm:block">{detail}</span>+ {selected ? (+ <CheckIcon className="size-4 text-success-foreground" />+ ) : (+ <span className="size-4" />+ )}+ </button>+ );+}++// ── Step 2: T3 Connect (sign in, then connect machines) ──────++const CONNECT_LOGIN_COMMAND = "npx t3 connect";++/**+ * Sign-in and machine-connection combined: signed out shows the Clerk prompt,+ * signed in forks on account state — zero connected machines blocks on the+ * `npx t3 connect` command and auto-advance is left to the user pressing+ * Continue once their machine appears; existing machines show a confirmation+ * list with the command folded away. There is deliberately no "primary+ * machine" selection.+ */+function ConnectMachinesStep({+ onBack,+ onContinue,+}: {+ readonly onBack: () => void;+ readonly onContinue: () => void;+}) {+ // Mirrors ManagedRelayAuthProvider: a pending Clerk session must not read+ // as signed-out mid-transition.+ const { isLoaded, isSignedIn } = useAuth({ treatPendingAsSignedOut: false });+ const { openAuthPrompt } = useT3ConnectAuthPrompt();+ const { environments } = useEnvironments();+ const primaryEnvironment = usePrimaryEnvironment();+ const savedEnvironments = environments.filter(isOnboardingRelayEnvironment);+ // Only a live connection counts: a saved-but-offline machine must not show+ // the "connected" confirmation (the agents step would find nothing to+ // probe). Its row still renders in the list either way.+ const hasRemoteMachines = savedEnvironments.some(+ (environment) => environment.connection.phase === "connected",+ );++ if (!isLoaded) {+ return <StepShell title="Sign in to T3 Connect" onBack={onBack} />;+ }++ if (!isSignedIn) {+ return (+ <StepShell+ title="Sign in to T3 Connect"+ description="Connect your computers with one account."+ onBack={onBack}+ >+ <div className="mt-6">+ <Button onClick={openAuthPrompt}>Sign in</Button>+ </div>+ </StepShell>+ );+ }++ return (+ <StepShell+ title={hasRemoteMachines ? "Your computers" : "Connect your computer"}+ description={+ hasRemoteMachines+ ? "Connected to your T3 account."+ : "Run this command on the computer with your code."+ }+ onBack={onBack}+ >+ {hasRemoteMachines ? (+ <>+ <div className="mt-6 overflow-hidden border-y border-border">+ <CloudEnvironmentConnectRows+ primaryEnvironmentId={primaryEnvironment?.environmentId ?? null}+ savedEnvironments={savedEnvironments}+ showSavedEnvironments+ empty={null}+ />+ </div>+ <Collapsible className="mt-4">+ <CollapsibleTrigger className="group flex items-center gap-1 text-xs text-muted-foreground transition-colors hover:text-foreground">+ <ChevronRightIcon className="size-3.5 transition-transform duration-200 group-data-panel-open:rotate-90" />+ Add another machine+ </CollapsibleTrigger>+ <CollapsiblePanel>+ <CommandBlock command={CONNECT_LOGIN_COMMAND} className="mt-2" />+ <p className="mt-2 text-xs text-muted-foreground">+ Keep T3 Code running on that computer. If it is not running, open T3 Code or run{" "}+ <code className="font-mono">npx t3 serve</code>.+ </p>+ </CollapsiblePanel>+ </Collapsible>+ <div className="mt-7 flex justify-end">+ <Button onClick={onContinue}>Continue</Button>+ </div>+ </>+ ) : (+ <>+ <CommandBlock command={CONNECT_LOGIN_COMMAND} className="mt-7" prominent />+ <p className="mt-2 text-xs text-muted-foreground">+ Keep T3 Code running on that computer. If it is not running, open T3 Code or run{" "}+ <code className="font-mono">npx t3 serve</code>.+ </p>+ <div className="mt-5 overflow-hidden border-y border-border">+ <CloudEnvironmentConnectRows+ primaryEnvironmentId={primaryEnvironment?.environmentId ?? null}+ savedEnvironments={savedEnvironments}+ showSavedEnvironments+ refreshWhileEmpty+ empty={+ <p className="px-1 py-4 text-xs text-muted-foreground">+ Waiting for your computer to connect.+ </p>+ }+ />+ </div>+ <div className="mt-7 flex flex-wrap items-center justify-between gap-3">+ <Button variant="ghost-muted" onClick={onContinue}>+ Skip for now+ </Button>+ <div className="flex flex-wrap items-center justify-end gap-3">+ <span className="hidden text-xs text-muted-foreground sm:block">+ Waiting for connection+ </span>+ <Button disabled>Continue</Button>+ </div>+ </div>+ </>+ )}+ </StepShell>+ );+}++// ── Step 2′: Direct pairing ──────────────────────────────────++/**+ * Server-minted pairing, D-B treatment: numbered steps, `t3 pair` on the+ * server, paste the URL here. Registers the remote environment in this+ * browser's catalog (same path the hosted /pair surface uses).+ */+function PairDirectStep({+ onBack,+ onPaired,+}: {+ readonly onBack: () => void;+ readonly onPaired: (environmentId: EnvironmentId) => void;+}) {+ const connectPairingEnvironment = useAtomCommand(connectPairing, { reportFailure: false });+ const [pairingUrl, setPairingUrl] = useState("");+ const [errorMessage, setErrorMessage] = useState("");+ const [isPairing, setIsPairing] = useState(false);+ const mountedRef = useRef(true);++ useEffect(() => {+ mountedRef.current = true;+ return () => {+ mountedRef.current = false;+ };+ }, []);++ const submit = async () => {+ setIsPairing(true);+ setErrorMessage("");+ const result = await connectPairingEnvironment({ pairingUrl });+ if (!mountedRef.current) return;+ setIsPairing(false);+ if (result._tag === "Success") {+ onPaired(result.value);+ return;+ }+ if (isAtomCommandInterrupted(result)) return;+ const cause = squashAtomCommandFailure(result);+ setErrorMessage(cause instanceof Error ? cause.message : "Pairing failed.");+ };++ return (+ <StepShell+ title="Pair a server"+ description="Connect with a one-time pairing link."+ onBack={onBack}+ >+ <div className="mt-7 space-y-5">+ <div>+ <p className="text-sm text-muted-foreground">+ <span className="font-mono text-muted-foreground/70">01</span> Run this on your server+ </p>+ <CommandBlock command="npx t3 pair" className="mt-2" />+ <p className="mt-2 text-xs text-muted-foreground">+ Start the server with <code className="font-mono">npx t3 serve</code> first. Add{" "}+ <code className="font-mono">--tailscale</code> to use your tailnet.+ </p>+ </div>+ <div>+ <label className="block text-sm text-muted-foreground" htmlFor="onboarding-pairing-url">+ <span className="font-mono text-muted-foreground/70">02</span> Paste the pairing link+ </label>+ <Input+ id="onboarding-pairing-url"+ className="mt-2"+ size="lg"+ autoCapitalize="none"+ autoComplete="off"+ autoCorrect="off"+ spellCheck={false}+ nativeInput+ disabled={isPairing}+ placeholder="https://your-server:5230/pair#token=…"+ value={pairingUrl}+ onChange={(event) => setPairingUrl(event.currentTarget.value)}+ onKeyDown={(event) => {+ if (event.nativeEvent.isComposing || event.keyCode === 229) return;+ if (event.key === "Enter" && pairingUrl.trim().length > 0) void submit();+ }}+ />+ </div>+ {errorMessage ? (+ <div className="rounded-lg border border-destructive/30 bg-destructive/6 px-3 py-2 text-sm text-destructive">+ {errorMessage}+ </div>+ ) : null}+ </div>+ <div className="mt-7 flex justify-end">+ <Button+ disabled={isPairing || pairingUrl.trim().length === 0}+ onClick={() => void submit()}+ >+ {isPairing ? "Pairing..." : "Connect"}+ </Button>+ </div>+ </StepShell>+ );+}++// ── Step 3: agents ───────────────────────────────────────────++const PRIMARY_AGENT_DRIVERS = ["claudeAgent", "codex"] as const;+type OnboardingAgentDriver = (typeof PRIMARY_AGENT_DRIVERS)[number];++const AGENT_INSTALL_COMMANDS: Record<OnboardingAgentDriver, string> = {+ claudeAgent: "npm install -g @anthropic-ai/claude-code",+ codex: "npm install -g @openai/codex",+};++/** Setup values stay fixed while provider probes refresh the surrounding cards. */+interface AgentTerminalSession {+ readonly environmentId: EnvironmentId;+ readonly driver: OnboardingAgentDriver;+ readonly providerInstanceId: ServerProvider["instanceId"];+ readonly cwd: string;+ readonly command: string;+ readonly keybindings: ServerConfig["keybindings"];+}++/**+ * Claude Code and Codex use live probe status. Install opens the built-in terminal inline+ * with the command pre-typed — the update RPC can't install a binary that+ * isn't there yet (it infers the package manager from the installed binary's+ * path), and the terminal also handles the interactive login that follows.+ */+function AgentsStep({+ mode,+ pairedEnvironmentId,+ onBack,+ onContinue,+ onSkip,+}: {+ readonly mode: ConnectionMode;+ readonly pairedEnvironmentId: EnvironmentId | null;+ readonly onBack: () => void;+ readonly onContinue: () => void;+ readonly onSkip: () => void;+}) {+ const targetEnvironment = useOnboardingTargetEnvironment(mode, pairedEnvironmentId);+ if (targetEnvironment === null) {+ return (+ <StepShell+ title="Your agents"+ description="Waiting for this computer to connect."+ onBack={onBack}+ >+ <div className="mt-6 flex justify-end">+ <Button variant="ghost-muted" onClick={onSkip}>+ Skip for now+ </Button>+ </div>+ </StepShell>+ );+ }+ return (+ <ConnectedAgentsStep+ key={targetEnvironment.environmentId}+ environmentId={targetEnvironment.environmentId}+ machineLabel={targetEnvironment.label}+ onBack={onBack}+ onContinue={onContinue}+ onSkip={onSkip}+ />+ );+}++function ConnectedAgentsStep({+ environmentId,+ machineLabel,+ onBack,+ onContinue,+ onSkip,+}: {+ readonly environmentId: EnvironmentId;+ readonly machineLabel: string;+ readonly onBack: () => void;+ readonly onContinue: () => void;+ readonly onSkip: () => void;+}) {+ const providers = useAtomValue(serverEnvironment.providersValueAtom(environmentId));+ const refreshProviders = useAtomCommand(serverEnvironment.refreshProviders, {+ reportFailure: false,+ });+ const serverConfig = useAtomValue(serverEnvironment.configValueAtom(environmentId));+ const [terminalSession, setTerminalSession] = useState<AgentTerminalSession | null>(null);++ // Re-probe on entry so freshly installed CLIs show up without a manual+ // refresh; harmless when nothing changed (single-flighted per environment).+ useEffect(() => {+ void refreshProviders({ environmentId, input: {} });+ }, [environmentId, refreshProviders]);++ const byDriver = useMemo(() => selectOnboardingProvidersByDriver(providers), [providers]);++ const primaryAgents = PRIMARY_AGENT_DRIVERS.map((driver) => ({+ driver,+ provider: byDriver.get(driver),+ }));+ const readyCount = primaryAgents.filter(+ ({ provider }) => getOnboardingProviderState(provider) === "ready",+ ).length;+ return (+ <StepShell title="Your agents" description={`Detected on ${machineLabel}.`} onBack={onBack}>+ <div className="mt-7 border-t border-border">+ {primaryAgents.map(({ driver, provider }) => (+ <AgentCard+ key={driver}+ driver={driver}+ provider={provider}+ terminalOpen={terminalSession?.driver === driver}+ terminalAvailable={serverConfig !== null}+ onOpenTerminal={() => {+ if (provider === undefined || serverConfig === null) return;+ setTerminalSession({+ environmentId,+ driver,+ providerInstanceId: provider.instanceId,+ cwd: serverConfig.cwd,+ command: provider.installed+ ? resolveOnboardingProviderLoginCommand(+ provider,+ serverConfig.settings,+ serverConfig.environment.platform.os,+ )+ : AGENT_INSTALL_COMMANDS[driver],+ keybindings: serverConfig.keybindings,+ });+ }}+ />+ ))}+ </div>+ {terminalSession !== null ? (+ <AgentInstallTerminal+ key={`${terminalSession.environmentId}:${terminalSession.providerInstanceId}:${terminalSession.driver}`}+ session={terminalSession}+ onClose={() => {+ setTerminalSession(null);+ void refreshProviders({ environmentId, input: {} });+ }}+ />+ ) : null}+ <div className="mt-7 flex flex-wrap items-center justify-between gap-3">+ <Button variant="ghost-muted" onClick={onSkip}>+ Skip+ </Button>+ <div className="flex flex-wrap items-center justify-end gap-3">+ <span className="text-xs text-muted-foreground">+ {readyCount} of {primaryAgents.length} ready+ </span>+ <Button className="gap-2" onClick={onContinue}>+ Continue+ <ArrowRightIcon className="size-3.5" />+ </Button>+ </div>+ </div>+ </StepShell>+ );+}++function AgentCard({+ driver,+ provider,+ terminalOpen,+ terminalAvailable,+ onOpenTerminal,+}: {+ readonly driver: OnboardingAgentDriver;+ readonly provider: ServerProvider | undefined;+ readonly terminalOpen: boolean;+ readonly terminalAvailable: boolean;+ readonly onOpenTerminal: () => void;+}) {+ const meta = getDriverOption(ProviderDriverKind.make(driver));+ const Icon = meta?.icon;+ const displayName = driver === "claudeAgent" ? "Claude Code" : (meta?.label ?? driver);+ const summary = getProviderSummary(provider);+ const providerState = getOnboardingProviderState(provider);++ return (+ <div className="flex min-h-20 items-center gap-4 border-b border-border py-3">+ {Icon ? (+ <Icon className={cn("size-6 shrink-0", driver !== "claudeAgent" && "fill-foreground")} />+ ) : null}+ <div className="min-w-0 flex-1">+ <span className="block text-sm font-medium text-foreground">{displayName}</span>+ <p className="mt-1 truncate text-xs text-muted-foreground">+ {summary.headline}+ {summary.detail ? ` · ${summary.detail}` : ""}+ </p>+ </div>+ <div className="shrink-0">+ {providerState === "ready" ? (+ <span className="inline-flex items-center gap-1.5 text-xs font-medium text-success-foreground">+ <CheckIcon className="size-3.5" />+ Ready+ </span>+ ) : providerState === "checking" ? (+ <span className="text-xs text-muted-foreground">Checking...</span>+ ) : providerState === "disabled" ? (+ <span className="text-xs text-muted-foreground">Disabled</span>+ ) : providerState === "attention" ? (+ <span className="text-xs text-muted-foreground">{summary.headline}</span>+ ) : (+ <Button+ size="xs"+ variant="ghost"+ onClick={onOpenTerminal}+ disabled={terminalOpen || !terminalAvailable}+ >+ <TerminalIcon className="size-3.5" />+ {providerState === "signIn" ? "Sign in" : "Install"}+ </Button>+ )}+ </div>+ </div>+ );+}++/**+ * Inline install terminal. Opens a PTY on the connected environment under a+ * synthetic onboarding thread id (terminals are keyed by free-form thread id;+ * the server validates only the cwd) and pre-types the install or login+ * command without submitting, so the user reviews and presses Enter.+ */+function AgentInstallTerminal({+ session,+ onClose,+}: {+ readonly session: AgentTerminalSession;+ readonly onClose: () => void;+}) {+ const { command, cwd, driver, environmentId, keybindings, providerInstanceId } = session;+ // Same terminal typography preference the thread drawer honors.+ const [advancedTypography] = useLocalStorage(+ TYPOGRAPHY_ADVANCED_STORAGE_KEY,+ false,+ Schema.Boolean,+ );+ const openTerminal = useAtomCommand(terminalEnvironment.open, { reportFailure: false });+ const writeTerminal = useAtomCommand(terminalEnvironment.write, { reportFailure: false });+ const closeTerminal = useAtomCommand(terminalEnvironment.close, { reportFailure: false });+ const setupQueueRef = useRef(Promise.resolve());+ const setupGenerationRef = useRef(0);+ const activeSetupGenerationRef = useRef<number | null>(null);+ const [terminalId] = useState(() => `onboarding-${driver}-${randomUUID()}`);+ const threadRef = useMemo(+ () => scopeThreadRef(environmentId, AGENT_ONBOARDING_THREAD_ID),+ [environmentId],+ );+ const [setupAttempt, setSetupAttempt] = useState(0);+ const [setupState, setSetupState] = useState<+ "preparing" | "ready" | "openFailed" | "writeFailed"+ >("preparing");+ const terminalReady = setupState === "ready" || setupState === "writeFailed";++ // Keep each setup generation distinct. In Strict Mode, a canceled open can+ // finish after the replacement setup starts; it must not close or pre-type+ // into the replacement session that shares this terminal id.+ useEffect(() => {+ const generation = setupGenerationRef.current + 1;+ setupGenerationRef.current = generation;+ activeSetupGenerationRef.current = generation;+ setSetupState("preparing");++ setupQueueRef.current = setupQueueRef.current.then(async () => {+ if (activeSetupGenerationRef.current !== generation) return;+ const opened = await openTerminal({+ environmentId,+ input: {+ threadId: AGENT_ONBOARDING_THREAD_ID,+ terminalId,+ cwd,+ providerInstanceId,+ },+ });+ if (opened._tag !== "Success") {+ if (activeSetupGenerationRef.current === generation) setSetupState("openFailed");+ return;+ }++ if (activeSetupGenerationRef.current !== generation) return;++ const wrote = await writeTerminal({+ environmentId,+ input: { threadId: AGENT_ONBOARDING_THREAD_ID, terminalId, data: command },+ });+ if (activeSetupGenerationRef.current !== generation) return;+ setSetupState(wrote._tag === "Success" ? "ready" : "writeFailed");+ });++ // Every exit path unmounts the drawer (Done, Continue/Skip, card switch,+ // session exit), so this cleanup is the single place the PTY dies —+ // nothing is left running behind the wizard. An interrupted install is+ // re-runnable from the card.+ return () => {+ if (activeSetupGenerationRef.current === generation) {+ activeSetupGenerationRef.current = null;+ }+ setupQueueRef.current = setupQueueRef.current.then(async () => {+ await closeTerminal({+ environmentId,+ input: { threadId: AGENT_ONBOARDING_THREAD_ID, terminalId, deleteHistory: true },+ });+ });+ };+ }, [+ closeTerminal,+ command,+ cwd,+ environmentId,+ openTerminal,+ providerInstanceId,+ setupAttempt,+ terminalId,+ writeTerminal,+ ]);++ return (+ <div className="thread-terminal-drawer mt-4 overflow-hidden rounded-lg border border-border/70 bg-background text-foreground">+ <div className="flex items-center justify-between border-b border-border/60 bg-background/60 px-3 py-1.5">+ <span className="text-[11px] font-medium text-muted-foreground">+ {setupState === "writeFailed" ? (+ <>+ Run <code className="rounded bg-muted px-1 font-mono">{command}</code> in this+ terminal.+ </>+ ) : setupState === "ready" ? (+ "Review the command, then press Enter to run it."+ ) : setupState === "openFailed" ? (+ "Could not open the setup terminal."+ ) : (+ "Preparing command..."+ )}+ </span>+ <div className="flex items-center gap-1">+ {setupState === "openFailed" ? (+ <Button size="xs" variant="ghost" onClick={() => setSetupAttempt((value) => value + 1)}>+ Retry+ </Button>+ ) : null}+ <Button size="xs" variant="ghost-muted" onClick={onClose}>+ Close+ </Button>+ </div>+ </div>+ <div className="h-64">+ {terminalReady ? (+ <TerminalViewport+ threadRef={threadRef}+ threadId={AGENT_ONBOARDING_THREAD_ID}+ terminalId={terminalId}+ terminalLabel={`Install ${driver}`}+ cwd={cwd}+ providerInstanceId={providerInstanceId}+ advancedTypography={advancedTypography}+ onSessionExited={onClose}+ focusRequestId={1}+ autoFocus+ visible+ resizeEpoch={0}+ drawerHeight={256}+ keybindings={keybindings}+ />+ ) : null}+ </div>+ </div>+ );+}++// ── Step 4: import ───────────────────────────────────────────++/**+ * One-decision import (4B): a summary line with Import recent / Choose /+ * Skip. The default imports only projects touched in the last 30 days;+ * Choose expands a checklist including older ones. Imported projects also+ * receive Codex and Claude threads active within the last 30 days.+ */+function ImportStep({+ mode,+ pairedEnvironmentId,+ onBack,+ onDone,+}: {+ readonly mode: ConnectionMode;+ readonly pairedEnvironmentId: EnvironmentId | null;+ readonly onBack: () => void;+ readonly onDone: (projectRef?: ScopedProjectRef) => Promise<boolean>;+}) {+ const targetEnvironment = useOnboardingTargetEnvironment(mode, pairedEnvironmentId);+ const environmentId = targetEnvironment?.environmentId ?? null;+ const machineLabel = targetEnvironment?.label ?? "this machine";+ const providers = useAtomValue(+ serverEnvironment.providersValueAtom(environmentId ?? ("" as EnvironmentId)),+ );+ const scan = useEnvironmentQuery(+ environmentId === null ? null : agentSessionScan({ environmentId, input: {} }),+ );+ const createProject = useAtomCommand(projectEnvironment.create, { reportFailure: false });+ const importThreads = useAtomCommand(agentSessionImport, { reportFailure: false });+ const projects = useProjects();+ const [choosing, setChoosing] = useState(false);+ const [deselected, setDeselected] = useState<ReadonlySet<string>>(new Set());+ const [isImporting, setIsImporting] = useState(false);+ const [importError, setImportError] = useState("");+ const [landingProject, setLandingProject] = useState<ScopedProjectRef | null>(null);+ // Keep project creation attempts separate from completed history imports so both can retry.+ const importedProjectsRef = useRef(new Map<string, ScopedProjectRef>());+ const projectsWithImportedHistoryRef = useRef(new Map<string, ScopedProjectRef>());+ const lastImportSelectionRef = useRef<ReadonlyArray<string>>([]);+ const projectAttemptsRef = useRef(+ new Map<string, { readonly projectId: ProjectId; readonly commandId: CommandId }>(),+ );+ const importGenerationRef = useRef(0);++ // Candidate paths are per-environment; a target switch would otherwise+ // leave stale entries in the deselection set (and stale success records).+ useEffect(() => {+ importGenerationRef.current += 1;+ setDeselected(new Set());+ setIsImporting(false);+ setImportError("");+ setLandingProject(null);+ importedProjectsRef.current = new Map();+ projectsWithImportedHistoryRef.current = new Map();+ lastImportSelectionRef.current = [];+ projectAttemptsRef.current = new Map();+ return () => {+ importGenerationRef.current += 1;+ };+ }, [environmentId]);++ useEffect(() => {+ if (+ landingProject !== null &&+ landingProject.environmentId === environmentId &&+ projects.some(+ (project) =>+ project.id === landingProject.projectId &&+ project.environmentId === landingProject.environmentId,+ )+ ) {+ setLandingProject(null);+ void onDone(landingProject).then((completed) => {+ if (!completed) setIsImporting(false);+ });+ }+ }, [environmentId, landingProject, onDone, projects]);++ const { available: candidates, recent } = useMemo(+ () => partitionOnboardingProjects(scan.data?.candidates ?? []),+ [scan.data],+ );+ const more = candidates.length - recent.length;+ const scanTruncated = scan.data?.truncated === true;+ const scanLimitNotice = scanTruncated ? (+ <p className="mt-3 text-sm text-muted-foreground" role="status">+ {SCAN_LIMIT_MESSAGE}+ </p>+ ) : null;++ const finishAfterImport = () => {+ const projectRef = resolveOnboardingLandingProject(+ lastImportSelectionRef.current,+ projectsWithImportedHistoryRef.current,+ importedProjectsRef.current,+ );+ if (projectRef === undefined) {+ void onDone();+ return;+ }+ setIsImporting(true);+ setLandingProject(projectRef);+ };++ const runImport = async (selection: ReadonlyArray<AgentSessionProjectCandidate>) => {+ if (environmentId === null || selection.length === 0) {+ void onDone();+ return;+ }+ setIsImporting(true);+ setImportError("");+ lastImportSelectionRef.current = selection.map((candidate) => candidate.path);+ const importGeneration = importGenerationRef.current;+ const importedProjects = importedProjectsRef.current;+ const projectAttempts = projectAttemptsRef.current;+ const defaultModelSelection = resolveDefaultProviderModelSelection(providers ?? [], null);+ // Interrupted imports are neither failures nor successes — the command was+ // superseded or the environment dropped — but they still didn't land, so+ // they must not read as "imported everything". Retries skip paths that+ // already landed this session (re-creating them would only trip the+ // duplicate-root invariant and read as a failure).+ let importedProjectsCount =+ importedProjects.size > 0+ ? selection.filter((candidate) => importedProjects.has(candidate.path)).length+ : 0;+ let importedThreadCount = 0;+ let skippedThreadCount = 0;+ let shouldRefreshScan = false;+ for (const candidate of selection) {+ if (+ importGeneration !== importGenerationRef.current ||+ importedProjects !== importedProjectsRef.current+ ) {+ return;+ }+ if (importedProjects.has(candidate.path)) continue;+ let projectId = resolveOnboardingProjectId(readProjects(), environmentId, candidate);+ if (projectId === null) {+ let attempt = projectAttempts.get(candidate.path);+ if (attempt === undefined) {+ const nextProjectId = newProjectId();+ attempt = {+ projectId: nextProjectId,+ commandId: CommandId.make(`onboarding:project:create:${nextProjectId}`),+ };+ projectAttempts.set(candidate.path, attempt);+ }+ projectId = attempt.projectId;+ const result = await createProject({+ environmentId,+ input: {+ projectId,+ commandId: attempt.commandId,+ title: candidate.title,+ workspaceRoot: candidate.path,+ createWorkspaceRootIfMissing: false,+ defaultModelSelection,+ },+ });+ if (+ importGeneration !== importGenerationRef.current ||+ importedProjects !== importedProjectsRef.current+ ) {+ return;+ }+ if (result._tag !== "Success") {+ if (!isAtomCommandInterrupted(result)) {+ projectAttempts.delete(candidate.path);+ shouldRefreshScan = true;+ }+ continue;+ }+ }++ const threadImportResult = await importThreads({+ environmentId,+ input: { projectId, expectedWorkspaceRoot: candidate.path },+ });+ if (+ importGeneration !== importGenerationRef.current ||+ importedProjects !== importedProjectsRef.current+ ) {+ return;+ }+ if (threadImportResult._tag === "Success") {+ importedThreadCount += threadImportResult.value.importedCount;+ skippedThreadCount += threadImportResult.value.skippedCount;+ if (threadImportResult.value.importedCount > 0) {+ projectsWithImportedHistoryRef.current.set(+ candidate.path,+ scopeProjectRef(environmentId, projectId),+ );+ }+ if (threadImportResult.value.skippedCount === 0) {+ importedProjectsCount += 1;+ importedProjects.set(candidate.path, scopeProjectRef(environmentId, projectId));+ }+ } else if (!isAtomCommandInterrupted(threadImportResult)) {+ projectAttempts.delete(candidate.path);+ shouldRefreshScan = true;+ }+ }+ if (shouldRefreshScan) scan.refresh();+ setIsImporting(false);+ if (importedProjectsCount < selection.length) {+ if (importedThreadCount > 0 && skippedThreadCount > 0) {+ setImportError(+ `Imported ${importedThreadCount} ${importedThreadCount === 1 ? "thread" : "threads"}. ${skippedThreadCount} ${skippedThreadCount === 1 ? "thread" : "threads"} could not be imported.`,+ );+ } else if (skippedThreadCount > 0) {+ setImportError(+ `${skippedThreadCount} ${skippedThreadCount === 1 ? "thread could" : "threads could"} not be imported.`,+ );+ } else if (importedThreadCount > 0) {+ setImportError(+ `Imported ${importedThreadCount} ${importedThreadCount === 1 ? "thread" : "threads"}. Some thread history could not be imported.`,+ );+ } else {+ setImportError("Could not import thread history.");+ }+ return;+ }+ finishAfterImport();+ };++ if (environmentId === null || (scan.isPending && scan.data === null)) {+ return (+ <StepShell+ title="Your projects"+ description="Looking for projects from Claude Code and Codex."+ onBack={onBack}+ >+ <div className="mt-6 flex justify-end">+ <Button variant="ghost-muted" onClick={() => void onDone()}>+ Skip+ </Button>+ </div>+ </StepShell>+ );+ }++ if (scan.error !== null || candidates.length === 0) {+ return (+ <StepShell+ title="Your projects"+ description={+ scan.error !== null+ ? "Could not check this computer for projects."+ : scanTruncated+ ? SCAN_LIMIT_MESSAGE+ : "No existing Claude Code or Codex projects found."+ }+ onBack={onBack}+ >+ {scan.error !== null ? (+ <p className="mt-3 text-xs text-muted-foreground">You can add projects later.</p>+ ) : null}+ <div className="mt-6 flex justify-end gap-2">+ {scan.error !== null ? (+ <Button variant="ghost" onClick={scan.refresh}>+ Retry+ </Button>+ ) : null}+ <Button+ variant={scan.error !== null || scanTruncated ? "ghost-muted" : "default"}+ onClick={() => void onDone()}+ >+ {scan.error !== null || scanTruncated ? "Skip" : "Start coding"}+ </Button>+ </div>+ </StepShell>+ );+ }++ if (choosing) {+ const selected = candidates.filter((candidate) => !deselected.has(candidate.path));+ return (+ <StepShell+ title="Choose your projects"+ onBack={() => setChoosing(false)}+ backDisabled={isImporting}+ description={`${candidates.length} found on ${machineLabel}.`}+ >+ {scanLimitNotice}+ <div className="mt-6 max-h-72 overflow-x-hidden overflow-y-auto border-y border-border">+ {candidates.map((candidate) => (+ <label+ key={candidate.path}+ className="flex min-h-12 cursor-pointer items-center gap-3 border-b border-border/60 px-1 py-2 last:border-b-0 hover:bg-accent/50"+ >+ <Checkbox+ checked={!deselected.has(candidate.path)}+ onCheckedChange={(checked) => {+ setDeselected((previous) => {+ const next = new Set(previous);+ if (checked === true) next.delete(candidate.path);+ else next.add(candidate.path);+ return next;+ });+ }}+ />+ <span className="min-w-0 flex-1 truncate font-mono text-xs text-foreground">+ {candidate.path}+ </span>+ <span className="hidden shrink-0 whitespace-nowrap text-[11px] text-muted-foreground sm:block">+ {candidate.sources.map(formatSource).join(", ")} · {candidate.threadCount}{" "}+ {candidate.threadCount === 1 ? "thread" : "threads"}+ {candidate.lastActiveAt+ ? ` · ${formatRelativeTimeLabel(candidate.lastActiveAt)}`+ : ""}+ </span>+ </label>+ ))}+ </div>+ {importError ? <p className="mt-3 text-sm text-destructive">{importError}</p> : null}+ <div className="mt-7 flex flex-wrap items-center justify-between gap-3">+ <Button+ variant="ghost-muted"+ disabled={isImporting}+ onClick={importError ? finishAfterImport : () => void onDone()}+ >+ {importError ? "Continue without the rest" : "Skip"}+ </Button>+ <Button+ disabled={isImporting || selected.length === 0}+ onClick={() => void runImport(selected)}+ >+ {isImporting ? "Importing..." : `Import ${selected.length}`}+ </Button>+ </div>+ </StepShell>+ );+ }++ return (+ <StepShell+ title="Your recent projects"+ description={`${recent.length} ${recent.length === 1 ? "project" : "projects"} found on ${machineLabel}.${more > 0 ? ` ${more} more available.` : ""}`}+ onBack={onBack}+ backDisabled={isImporting}+ >+ {scanLimitNotice}+ <div className="mt-6 border-y border-border">+ {recent.slice(0, 4).map((candidate) => (+ <div+ key={candidate.path}+ className="flex min-h-12 items-center gap-3 border-b border-border/60 px-1 py-2 last:border-b-0"+ >+ <CheckIcon className="size-3.5 shrink-0 text-success-foreground" />+ <span className="min-w-0 flex-1 truncate font-mono text-xs text-foreground">+ {candidate.path}+ </span>+ <span className="hidden shrink-0 whitespace-nowrap text-[11px] text-muted-foreground sm:block">+ {candidate.sources.map(formatSource).join(", ")}+ </span>+ </div>+ ))}+ {recent.length > 4 ? (+ <p className="px-1 py-3 text-xs text-muted-foreground">+ {recent.length - 4} more projects+ </p>+ ) : null}+ </div>+ {importError ? <p className="mt-3 text-sm text-destructive">{importError}</p> : null}+ <div className="mt-7 flex flex-wrap items-center justify-between gap-3">+ <Button+ variant="ghost-muted"+ disabled={isImporting}+ onClick={importError ? finishAfterImport : () => void onDone()}+ >+ {importError ? "Continue without the rest" : "Skip"}+ </Button>+ <div className="flex items-center gap-2">+ <Button variant="ghost" disabled={isImporting} onClick={() => setChoosing(true)}>+ Choose+ </Button>+ <Button+ disabled={isImporting || recent.length === 0}+ onClick={() => void runImport(recent)}+ >+ {isImporting+ ? "Importing..."+ : `Import ${recent.length} ${recent.length === 1 ? "project" : "projects"}`}+ </Button>+ </div>+ </div>+ </StepShell>+ );+}++// ── Shared bits ──────────────────────────────────────────────++function StepShell({+ title,+ description,+ onBack,+ backDisabled = false,+ children,+}: {+ readonly title: string;+ readonly description?: string;+ readonly onBack?: () => void;+ readonly backDisabled?: boolean;+ readonly children?: React.ReactNode;+}) {+ return (+ <>+ {onBack ? (+ <Button+ className="mb-5 -ml-2"+ disabled={backDisabled}+ onClick={onBack}+ size="xs"+ variant="ghost-muted"+ >+ <ChevronLeftIcon className="size-3.5" />+ Back+ </Button>+ ) : null}+ <h1 className="text-3xl font-semibold text-foreground sm:text-[34px]">{title}</h1>+ {description ? (+ <p className="mt-2.5 text-sm leading-relaxed text-muted-foreground">{description}</p>+ ) : null}+ {children}+ </>+ );+}++function CommandBlock({+ command,+ className,+ prominent = false,+}: {+ readonly command: string;+ readonly className?: string;+ readonly prominent?: boolean;+}) {+ const { copyToClipboard, isCopied } = useCopyToClipboard({+ timeout: 1500,+ target: "command",+ });+ return (+ <div+ className={cn(+ "flex items-center justify-between gap-3 border border-border bg-accent/50 font-mono",+ prominent ? "px-4 py-3.5 text-base" : "px-3 py-2.5 text-sm",+ className,+ )}+ >+ <span className="min-w-0 truncate">+ <span className="mr-2 text-muted-foreground">$</span>+ {command}+ </span>+ <Button+ size="icon-xs"+ variant="ghost"+ aria-label="Copy command"+ onClick={() => copyToClipboard(command, undefined)}+ >+ {isCopied ? <CheckIcon className="size-3.5" /> : <CopyIcon className="size-3.5" />}+ </Button>+ </div>+ );+}++function formatSource(source: "claudeAgent" | "codex"): string {+ return source === "claudeAgent" ? "Claude" : "Codex";+}diff --git a/apps/web/src/components/preview/PreviewAutomationHosts.test.tsx b/apps/web/src/components/preview/PreviewAutomationHosts.test.tsxnew file mode 100644index 00000000000..75ed20cc4fe--- /dev/null+++ b/apps/web/src/components/preview/PreviewAutomationHosts.test.tsx@@ -0,0 +1,190 @@+import {+ DEFAULT_CLIENT_SETTINGS,+ EnvironmentId,+ ThreadId,+ type ClientSettings,+ type PreviewAutomationResponse,+ type PreviewAutomationStreamEvent,+ type PreviewOpenInput,+ type PreviewSessionSnapshot,+} from "@t3tools/contracts";+import { AsyncResult, Atom } from "effect/unstable/reactivity";+import { act } from "react";+import { create, type ReactTestRenderer } from "react-test-renderer";+import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";++import { __resetClientSettingsPersistenceForTests } from "~/hooks/useSettings";+import { readThreadPreviewState, resetPreviewStateForTests } from "~/previewStateStore";+import { appAtomRegistry, AppAtomRegistryProvider } from "~/rpc/atomRegistry";++import { PreviewAutomationHosts } from "./PreviewAutomationHosts";++const mocks = vi.hoisted(() => ({+ getClientSettings: vi.fn<() => Promise<ClientSettings | null>>(),+ setClientSettings: vi.fn(),+ open: vi.fn(async (_target: { environmentId: EnvironmentId; input: PreviewOpenInput }) =>+ AsyncResult.success(snapshot),+ ),+ list: vi.fn(async () => AsyncResult.success(emptyList)),+ resize: vi.fn(),+ respond:+ vi.fn<+ (target: { environmentId: EnvironmentId; input: PreviewAutomationResponse }) => Promise<void>+ >(),+ focus: vi.fn(async () => undefined),+}));++vi.mock("~/localApi", () => ({+ ensureLocalApi: () => ({ persistence: mocks }),+}));+vi.mock("~/env", () => ({ isElectron: true }));+vi.mock("~/state/environments", () => ({+ useEnvironments: () => ({ environments: [{ environmentId }] }),+}));+vi.mock("~/state/preview", () => ({+ previewEnvironment: {+ automationRequests: () => requestsAtom,+ list: () => listAtom,+ open: mocks.open,+ resize: mocks.resize,+ respondToAutomation: mocks.respond,+ focusAutomationHost: mocks.focus,+ },+}));+vi.mock("~/state/use-atom-command", () => ({+ useAtomCommand: (command: unknown) => command,+}));+vi.mock("~/state/use-atom-query-runner", () => ({+ useAtomQueryRunner: () => mocks.list,+}));+vi.mock("./previewBridge", () => ({ previewBridge: { automation: {} } }));++const environmentId = EnvironmentId.make("automation-environment");+const threadId = ThreadId.make("automation-thread");+const threadRef = { environmentId, threadId };+const viewport = { _tag: "freeform", width: 1440, height: 900 } as const;+const savedSettings: ClientSettings = {+ ...DEFAULT_CLIENT_SETTINGS,+ browserDefaultViewport: viewport,+ browserDefaultProfileId: "work",+ browserProfiles: [{ id: "work", name: "Work", kind: "persistent" }],+};+const snapshot: PreviewSessionSnapshot = {+ threadId,+ tabId: "automation-tab",+ navStatus: { _tag: "Idle" },+ canGoBack: false,+ canGoForward: false,+ viewport,+ profileId: "work",+ updatedAt: "2026-09-05T00:00:00.000Z",+};+const emptyList = { sessions: [], serverEpoch: "test-server", revision: 0 };+const listAtom = Atom.make(AsyncResult.success(emptyList));+const requestsAtom = Atom.make<AsyncResult.AsyncResult<PreviewAutomationStreamEvent, Error>>(+ AsyncResult.initial(false),+);+const requestEvent: PreviewAutomationStreamEvent = {+ type: "request",+ connectionId: "automation-connection",+ request: {+ requestId: "open-request",+ threadId,+ operation: "open",+ input: { open: false, reuseExistingTab: false },+ timeoutMs: 15_000,+ },+};++function deferred<A>() {+ let resolve!: (value: A) => void;+ const promise = new Promise<A>((complete) => {+ resolve = complete;+ });+ return { promise, resolve };+}++let renderer: ReactTestRenderer | null = null;++beforeEach(async () => {+ vi.clearAllMocks();+ mocks.getClientSettings.mockReset().mockResolvedValue(savedSettings);+ mocks.respond.mockReset();+ __resetClientSettingsPersistenceForTests();+ resetPreviewStateForTests();+ appAtomRegistry.set(requestsAtom, AsyncResult.initial(false));+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);+ vi.stubGlobal("window", { addEventListener: vi.fn(), removeEventListener: vi.fn() });+ vi.stubGlobal("document", { hasFocus: () => false, querySelectorAll: () => [] });+ await act(() => {+ renderer = create(+ <AppAtomRegistryProvider>+ <PreviewAutomationHosts />+ </AppAtomRegistryProvider>,+ );+ });+});++afterEach(async () => {+ await act(() => renderer?.unmount());+ renderer = null;+ resetPreviewStateForTests();+ __resetClientSettingsPersistenceForTests();+ vi.unstubAllGlobals();+ vi.restoreAllMocks();+});++describe("PreviewAutomationHosts open", () => {+ it("waits for saved settings before opening a tab with the configured profile and viewport", async () => {+ const readStarted = deferred<void>();+ const read = deferred<ClientSettings>();+ const response = deferred<PreviewAutomationResponse>();+ mocks.getClientSettings.mockImplementationOnce(() => {+ readStarted.resolve();+ return read.promise;+ });+ mocks.respond.mockImplementationOnce(async ({ input }) => response.resolve(input));++ await act(async () => {+ appAtomRegistry.set(requestsAtom, AsyncResult.success(requestEvent));+ await readStarted.promise;+ });+ expect(mocks.open).not.toHaveBeenCalled();++ await act(async () => {+ read.resolve(savedSettings);+ await response.promise;+ });++ expect(mocks.open).toHaveBeenCalledExactlyOnceWith({+ environmentId,+ input: { threadId, viewport, profileId: "work" },+ });+ expect(mocks.getClientSettings).toHaveBeenCalledOnce();+ await expect(response.promise).resolves.toMatchObject({ requestId: "open-request", ok: true });+ expect(readThreadPreviewState(threadRef).snapshot).toEqual(snapshot);+ expect(mocks.setClientSettings).not.toHaveBeenCalled();+ });++ it("reports a settings read failure without opening a tab", async () => {+ vi.spyOn(console, "error").mockImplementation(() => undefined);+ mocks.getClientSettings.mockRejectedValueOnce(new Error("Settings read failed"));+ const response = deferred<PreviewAutomationResponse>();+ mocks.respond.mockImplementationOnce(async ({ input }) => response.resolve(input));++ await act(async () => {+ appAtomRegistry.set(requestsAtom, AsyncResult.success(requestEvent));+ await response.promise;+ });++ await expect(response.promise).resolves.toMatchObject({+ requestId: "open-request",+ ok: false,+ error: { _tag: "PreviewAutomationExecutionError" },+ });+ expect(mocks.getClientSettings).toHaveBeenCalledOnce();+ expect(mocks.open).not.toHaveBeenCalled();+ expect(readThreadPreviewState(threadRef).snapshot).toBeNull();+ expect(mocks.setClientSettings).not.toHaveBeenCalled();+ });+});diff --git a/apps/web/src/components/preview/PreviewAutomationHosts.tsx b/apps/web/src/components/preview/PreviewAutomationHosts.tsxindex 08b31640906..fd87f7e80c7 100644--- a/apps/web/src/components/preview/PreviewAutomationHosts.tsx+++ b/apps/web/src/components/preview/PreviewAutomationHosts.tsx@@ -41,7 +41,11 @@ import {acquireBrowserSurfaceActivity,useBrowserSurfaceStore,} from "~/browser/browserSurfaceStore";-import { browserDefaultOpenViewport, resolveBrowserDefaults } from "~/browser/browserDefaults";+import {+ browserDefaultOpenProfileId,+ browserDefaultOpenViewport,+ resolveBrowserDefaults,+} from "~/browser/browserDefaults";import { runBrowserViewportMutation } from "~/browser/browserViewportActions";import { previewRuntimeTabId } from "~/browser/previewRuntimeTabId";import { isElectron } from "~/env";@@ -412,6 +416,7 @@ function PreviewAutomationHost(props: { readonly environmentId: EnvironmentId })const reusedExistingTab = activeTabId !== null;tabId = activeTabId;if (!activeTabId) {+ const defaults = await resolveBrowserDefaults();const result = await open({environmentId,input: {@@ -419,7 +424,8 @@ function PreviewAutomationHost(props: { readonly environmentId: EnvironmentId })...(resolvedInputUrl ? { url: resolvedInputUrl } : {}),// An agent that didn't state a size gets the user's// configured default, same as a hand-opened tab.- viewport: browserDefaultOpenViewport(await resolveBrowserDefaults()),+ viewport: browserDefaultOpenViewport(defaults),+ profileId: browserDefaultOpenProfileId(defaults),},});if (result._tag === "Failure") {diff --git a/apps/web/src/components/preview/PreviewView.tsx b/apps/web/src/components/preview/PreviewView.tsxindex 7e0bf2dfb54..e6ad2758bc4 100644--- a/apps/web/src/components/preview/PreviewView.tsx+++ b/apps/web/src/components/preview/PreviewView.tsx@@ -1,7 +1,10 @@"use client";import { scopedThreadKey } from "@t3tools/client-runtime/environment";-import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";+import {+ isAtomCommandInterrupted,+ squashAtomCommandFailure,+} from "@t3tools/client-runtime/state/runtime";import {DEFAULT_BROWSER_PROFILE_ID,FILL_PREVIEW_VIEWPORT,@@ -46,6 +49,7 @@ import {} from "~/browser/browserViewportActions";import { browserResponsiveViewportForToggle, useBrowserDefaults } from "~/browser/browserDefaults";import { previewRuntimeTabId } from "~/browser/previewRuntimeTabId";+import { BrowserSettingsReadError } from "~/browser/openFileInPreview";import { PreviewUnreachable } from "./PreviewUnreachable";import { revealInFileExplorerLabel } from "./fileExplorerLabel";import { shouldShowPreviewEmptyState } from "./previewEmptyStateLogic";@@ -186,6 +190,16 @@ export function PreviewView({return true;}const result = await openPreviewSession({ openPreview: open, threadRef, url: resolvedUrl });+ if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {+ const error = squashAtomCommandFailure(result);+ if (error instanceof BrowserSettingsReadError) {+ toastManager.add({+ type: "error",+ title: "Unable to open browser",+ description: error.message,+ });+ }+ }return result._tag === "Success";},[open, runtimeTabId, threadRef],diff --git a/apps/web/src/components/preview/addBrowserSurface.test.ts b/apps/web/src/components/preview/addBrowserSurface.test.tsindex f26cb0fff9e..d34de83a23b 100644--- a/apps/web/src/components/preview/addBrowserSurface.test.ts+++ b/apps/web/src/components/preview/addBrowserSurface.test.ts@@ -1,5 +1,6 @@import {DEFAULT_BROWSER_PROFILE_ID,+ DEFAULT_CLIENT_SETTINGS,FILL_PREVIEW_VIEWPORT,type PreviewOpenInput,type PreviewSessionSnapshot,@@ -14,6 +15,7 @@ import {resetPreviewStateForTests,} from "~/previewStateStore";import { selectThreadRightPanelState, useRightPanelStore } from "~/rightPanelStore";+import { __setClientSettingsForTests } from "~/hooks/useSettings";import { addBrowserSurface } from "./addBrowserSurface";@@ -32,6 +34,7 @@ const snapshot = (tabId: string): PreviewSessionSnapshot => ({});beforeEach(() => {+ __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS);resetPreviewStateForTests();useRightPanelStore.setState({ byThreadKey: {} });});diff --git a/apps/web/src/components/preview/addBrowserSurface.ts b/apps/web/src/components/preview/addBrowserSurface.tsindex 622cdbec2f1..e0cd8350120 100644--- a/apps/web/src/components/preview/addBrowserSurface.ts+++ b/apps/web/src/components/preview/addBrowserSurface.ts@@ -4,7 +4,7 @@ import {} from "@t3tools/client-runtime/state/runtime";import type { ScopedThreadRef } from "@t3tools/contracts";-import type { OpenPreviewMutation } from "~/browser/openFileInPreview";+import type { BrowserSettingsReadError, OpenPreviewMutation } from "~/browser/openFileInPreview";import { useRightPanelStore } from "~/rightPanelStore";import { openPreviewSession } from "./openPreviewSession";@@ -15,7 +15,7 @@ export async function addBrowserSurface<E>(input: {readonly openPreview: OpenPreviewMutation<E>;/** Omit to use the configured default profile. */readonly profileId?: string | undefined;-}): Promise<AtomCommandResult<void, E>> {+}): Promise<AtomCommandResult<void, E | BrowserSettingsReadError>> {const result = await openPreviewSession({openPreview: input.openPreview,threadRef: input.threadRef,diff --git a/apps/web/src/components/preview/openDiscoveredPort.ts b/apps/web/src/components/preview/openDiscoveredPort.tsindex a49acbd8610..288db101e7a 100644--- a/apps/web/src/components/preview/openDiscoveredPort.ts+++ b/apps/web/src/components/preview/openDiscoveredPort.ts@@ -5,7 +5,7 @@ import {} from "@t3tools/client-runtime/state/runtime";import { resolveDiscoveredServerUrl } from "~/browser/browserTargetResolver";-import type { OpenPreviewMutation } from "~/browser/openFileInPreview";+import type { BrowserSettingsReadError, OpenPreviewMutation } from "~/browser/openFileInPreview";import { recordVisitForThread } from "~/browserHistoryStore";import { useRightPanelStore } from "~/rightPanelStore";import { openPreviewSession } from "./openPreviewSession";@@ -14,7 +14,7 @@ export async function openDiscoveredPort<E>(input: {readonly threadRef: ScopedThreadRef;readonly port: DiscoveredLocalServer;readonly openPreview: OpenPreviewMutation<E>;-}): Promise<AtomCommandResult<void, E>> {+}): Promise<AtomCommandResult<void, E | BrowserSettingsReadError>> {const resolvedUrl = resolveDiscoveredServerUrl(input.threadRef.environmentId, input.port.url);const result = await openPreviewSession({openPreview: input.openPreview,diff --git a/apps/web/src/components/preview/openPreviewSession.test.ts b/apps/web/src/components/preview/openPreviewSession.test.tsindex ef3d51a9e7f..fe14211280c 100644--- a/apps/web/src/components/preview/openPreviewSession.test.ts+++ b/apps/web/src/components/preview/openPreviewSession.test.ts@@ -1,5 +1,6 @@import {DEFAULT_BROWSER_PROFILE_ID,+ DEFAULT_CLIENT_SETTINGS,FILL_PREVIEW_VIEWPORT,type PreviewOpenInput,type PreviewSessionSnapshot,@@ -7,8 +8,11 @@ import {} from "@t3tools/contracts";import * as Cause from "effect/Cause";import { AsyncResult } from "effect/unstable/reactivity";-import { beforeEach, describe, expect, it, vi } from "vite-plus/test";+import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";+import * as browserDefaults from "~/browser/browserDefaults";+import { BrowserSettingsReadError, openUrlInPreview } from "~/browser/openFileInPreview";+import { __setClientSettingsForTests } from "~/hooks/useSettings";import { readThreadPreviewState, resetPreviewStateForTests } from "~/previewStateStore";import { openPreviewSession } from "./openPreviewSession";@@ -31,7 +35,14 @@ const snapshot: PreviewSessionSnapshot = {updatedAt: "2026-06-11T23:00:00.000Z",};-beforeEach(resetPreviewStateForTests);+beforeEach(() => {+ resetPreviewStateForTests();+ __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS);+});++afterEach(() => {+ vi.restoreAllMocks();+});describe("openPreviewSession", () => {it("creates an idle tab without recording a recently visited URL", async () => {@@ -88,4 +99,44 @@ describe("openPreviewSession", () => {expect(readThreadPreviewState(threadRef).snapshot).toBeNull();expect(readThreadPreviewState(threadRef).recentlySeenUrls).toEqual([]);});++ it.each(["session", "link"] as const)(+ "does not open a %s with unread settings and uses the saved profile on retry",+ async (entryPoint) => {+ const failure = new Error("Settings read failed");+ vi.spyOn(browserDefaults, "resolveBrowserDefaults").mockRejectedValueOnce(failure);+ const viewport = { _tag: "freeform", width: 1280, height: 720 } as const;+ __setClientSettingsForTests({+ ...DEFAULT_CLIENT_SETTINGS,+ browserDefaultViewport: viewport,+ browserDefaultProfileId: "work",+ browserProfiles: [{ id: "work", name: "Work", kind: "persistent" }],+ });+ const openPreview = vi.fn(async () => AsyncResult.success(snapshot));+ const input = { openPreview, threadRef, url: "https://t3.chat/" };+ const open = entryPoint === "session" ? openPreviewSession : openUrlInPreview;++ const result = await open(input);++ expect(result._tag).toBe("Failure");+ if (result._tag === "Failure") {+ expect(Cause.squash(result.cause)).toBeInstanceOf(BrowserSettingsReadError);+ expect(Cause.squash(result.cause)).toMatchObject({ cause: failure });+ }+ expect(openPreview).not.toHaveBeenCalled();+ expect(readThreadPreviewState(threadRef).snapshot).toBeNull();+ expect(readThreadPreviewState(threadRef).recentlySeenUrls).toEqual([]);++ await expect(open(input)).resolves.toMatchObject({ _tag: "Success" });+ expect(openPreview).toHaveBeenCalledExactlyOnceWith({+ environmentId: threadRef.environmentId,+ input: {+ threadId: threadRef.threadId,+ url: input.url,+ viewport,+ profileId: "work",+ },+ });+ },+ );});diff --git a/apps/web/src/components/preview/openPreviewSession.ts b/apps/web/src/components/preview/openPreviewSession.tsindex deb5465ebc2..07dab9a0b36 100644--- a/apps/web/src/components/preview/openPreviewSession.ts+++ b/apps/web/src/components/preview/openPreviewSession.ts@@ -6,12 +6,15 @@ import type {ScopedThreadRef,} from "@t3tools/contracts";import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime";+import * as Cause from "effect/Cause";+import { AsyncResult } from "effect/unstable/reactivity";import {browserDefaultOpenProfileId,browserDefaultOpenViewport,resolveBrowserDefaults,} from "~/browser/browserDefaults";+import { BrowserSettingsReadError } from "~/browser/openFileInPreview";import { applyPreviewServerSnapshot, rememberPreviewUrl } from "~/previewStateStore";interface OpenPreviewSessionInput<E> {@@ -29,10 +32,15 @@ interface OpenPreviewSessionInput<E> {export async function openPreviewSession<E>(input: OpenPreviewSessionInput<E>,-): Promise<AtomCommandResult<PreviewSessionSnapshot, E>> {+): Promise<AtomCommandResult<PreviewSessionSnapshot, E | BrowserSettingsReadError>> {// Resolved once: a tab opened before client settings hydrate would otherwise// be born at the schema defaults and never corrected.- const defaults = await resolveBrowserDefaults();+ const defaults = await resolveBrowserDefaults().catch(+ (cause: unknown) => new BrowserSettingsReadError({ cause }),+ );+ if (defaults instanceof BrowserSettingsReadError) {+ return AsyncResult.failure(Cause.fail(defaults));+ }const result = await input.openPreview({environmentId: input.threadRef.environmentId,input: {diff --git a/apps/web/src/components/preview/openTerminalLinkInPreview.test.ts b/apps/web/src/components/preview/openTerminalLinkInPreview.test.tsindex 46dd33f7beb..2ce81cb06af 100644--- a/apps/web/src/components/preview/openTerminalLinkInPreview.test.ts+++ b/apps/web/src/components/preview/openTerminalLinkInPreview.test.ts@@ -68,6 +68,33 @@ afterEach(() => {});describe("openTerminalLinkInPreview", () => {+ it.each(["target", "defaults"] as const)(+ "does not open either browser when reading %s fails",+ async (setting) => {+ const failure = new Error("Settings read failed");+ if (setting === "target") {+ linkTargetMocks.preference.mockImplementationOnce(() => {+ throw failure;+ });+ } else {+ browserDefaultsMocks.resolve.mockRejectedValueOnce(failure);+ }+ const fallbackToBrowser = vi.fn();+ const openPreview = vi.fn(async () => AsyncResult.success(snapshot));++ await expect(+ openTerminalLinkInPreview({+ url: "https://example.com/docs",+ threadRef,+ openPreview,+ fallbackToBrowser,+ }),+ ).rejects.toBe(failure);+ expect(fallbackToBrowser).not.toHaveBeenCalled();+ expect(openPreview).not.toHaveBeenCalled();+ },+ );+it("opens in the system browser while that is the configured target", async () => {linkTargetMocks.preference.mockReturnValue("system");const fallbackToBrowser = vi.fn();diff --git a/apps/web/src/components/settings/providerStatus.test.ts b/apps/web/src/components/settings/providerStatus.test.tsnew file mode 100644index 00000000000..46dc7e26251--- /dev/null+++ b/apps/web/src/components/settings/providerStatus.test.ts@@ -0,0 +1,71 @@+import { ProviderDriverKind, ProviderInstanceId, type ServerProvider } from "@t3tools/contracts";+import { describe, expect, it } from "vite-plus/test";++import { getProviderSummary } from "./providerStatus";++const provider: ServerProvider = {+ instanceId: ProviderInstanceId.make("codex"),+ driver: ProviderDriverKind.make("codex"),+ enabled: true,+ installed: true,+ version: "1.0.0",+ status: "ready",+ auth: { status: "authenticated", label: "ChatGPT" },+ checkedAt: "2026-08-23T00:00:00.000Z",+ models: [],+ slashCommands: [],+ skills: [],+};++describe("getProviderSummary", () => {+ it("reports ready providers with unknown authentication as available", () => {+ expect(getProviderSummary({ ...provider, auth: { status: "unknown" } })).toEqual({+ headline: "Available",+ detail: null,+ });+ });++ it("does not hide a provider error behind a previous authenticated state", () => {+ expect(+ getProviderSummary({+ ...provider,+ status: "error",+ message: "The provider process failed to start.",+ }),+ ).toEqual({+ headline: "Unavailable",+ detail: "The provider process failed to start.",+ });+ });++ it("does not hide a provider warning behind an authenticated state", () => {+ expect(+ getProviderSummary({+ ...provider,+ status: "warning",+ message: "The provider version is unsupported.",+ }),+ ).toEqual({+ headline: "Needs attention",+ detail: "The provider version is unsupported.",+ });+ });++ it("keeps authentication failures actionable when their provider status is error", () => {+ expect(+ getProviderSummary({+ ...provider,+ status: "error",+ auth: { status: "unauthenticated" },+ message: "Run codex login.",+ }),+ ).toEqual({+ headline: "Not authenticated",+ detail: "Run codex login.",+ });+ });++ it("treats a disabled provider status as disabled even before its enabled flag updates", () => {+ expect(getProviderSummary({ ...provider, status: "disabled" }).headline).toBe("Disabled");+ });+});diff --git a/apps/web/src/components/settings/providerStatus.ts b/apps/web/src/components/settings/providerStatus.tsindex 0f39f643f5c..90c618f5daa 100644--- a/apps/web/src/components/settings/providerStatus.ts+++ b/apps/web/src/components/settings/providerStatus.ts@@ -26,7 +26,8 @@ export type ProviderStatusKey = keyof typeof PROVIDER_STATUS_STYLES;* settings page. Prefers `provider.message` for server-supplied detail and* falls back to generic phrasing when the server has not yet reported any* state — which happens before the first probe or when an instance names a- * driver this build does not ship.+ * driver this build does not ship. A ready provider without account metadata+ * remains available and does not imply an authentication failure.*/export function getProviderSummary(provider: ServerProvider | undefined) {if (!provider) {@@ -35,7 +36,7 @@ export function getProviderSummary(provider: ServerProvider | undefined) {detail: "Waiting for the server to report installation and authentication details.",};}- if (!provider.enabled) {+ if (!provider.enabled || provider.status === "disabled") {return {headline: "Disabled",detail:@@ -48,13 +49,6 @@ export function getProviderSummary(provider: ServerProvider | undefined) {detail: provider.message ?? "CLI not detected on PATH.",};}- if (provider.auth.status === "authenticated") {- const authLabel = provider.auth.label ?? provider.auth.type;- return {- headline: authLabel ? `Authenticated · ${authLabel}` : "Authenticated",- detail: provider.message ?? null,- };- }if (provider.auth.status === "unauthenticated") {return {headline: "Not authenticated",@@ -74,9 +68,16 @@ export function getProviderSummary(provider: ServerProvider | undefined) {detail: provider.message ?? "The provider failed its startup checks.",};}+ if (provider.auth.status === "authenticated") {+ const authLabel = provider.auth.label ?? provider.auth.type;+ return {+ headline: authLabel ? `Authenticated · ${authLabel}` : "Authenticated",+ detail: provider.message ?? null,+ };+ }return {headline: "Available",- detail: provider.message ?? "Installed and ready, but authentication could not be verified.",+ detail: provider.message ?? null,};}diff --git a/apps/web/src/environments/primary/auth.ts b/apps/web/src/environments/primary/auth.tsindex fe0345e4152..d76002a9329 100644--- a/apps/web/src/environments/primary/auth.ts+++ b/apps/web/src/environments/primary/auth.ts@@ -353,6 +353,8 @@ export async function submitServerAuthCredential(credential: string): Promise<voresolvedAuthenticatedGateState = null;await exchangeBootstrapCredential(trimmedCredential);+ await waitForAuthenticatedSessionAfterBootstrap();+ resolvedAuthenticatedGateState = { status: "authenticated" };bootstrapPromise = null;stripPairingTokenFromUrl();}diff --git a/apps/web/src/hooks/useLocalStorage.test.ts b/apps/web/src/hooks/useLocalStorage.test.tsindex 27627a36e4b..495365dfdcc 100644--- a/apps/web/src/hooks/useLocalStorage.test.ts+++ b/apps/web/src/hooks/useLocalStorage.test.ts@@ -55,9 +55,28 @@ describe("local storage errors", () => {}});- it("preserves decode failure context", async () => {+ it("retries when access to browser storage becomes available", async () => {+ const storage = createStorage();+ storage.setItem("read-key", JSON.stringify("saved value"));+ let blocked = true;+ vi.stubGlobal("window", {+ get localStorage() {+ if (blocked) throw new Error("storage unavailable");+ return storage;+ },+ });+ const { getLocalStorageItem, LocalStorageOperationError } = await import("./useLocalStorage");++ expect(() => getLocalStorageItem("read-key", Schema.String)).toThrow(+ LocalStorageOperationError,+ );+ blocked = false;+ expect(getLocalStorageItem("read-key", Schema.String)).toBe("saved value");+ });++ it.each(["", "not-json"])("preserves decode failure context for %j", async (value) => {const { getLocalStorageItem, LocalStorageOperationError } = await loadWithStorage(- createStorage({ getItem: () => "not-json" }),+ createStorage({ getItem: () => value }),);try {diff --git a/apps/web/src/hooks/useLocalStorage.ts b/apps/web/src/hooks/useLocalStorage.tsindex 3099e73ff43..11271559948 100644--- a/apps/web/src/hooks/useLocalStorage.ts+++ b/apps/web/src/hooks/useLocalStorage.ts@@ -15,26 +15,26 @@ export class LocalStorageOperationError extends Schema.TaggedErrorClass<LocalSto}}-const isomorphicLocalStorage: Storage =- typeof window !== "undefined"- ? window.localStorage- : (function () {- const store = new Map<string, string>();- return {- clear: () => store.clear(),- getItem: (_) => store.get(_) ?? null,- key: (_) => Record.keys(store).at(_) ?? null,- get length() {- return store.size;- },- removeItem: (_) => store.delete(_),- setItem: (_, value) => store.set(_, value),- };- })();+const fallbackStorage: Storage = (() => {+ const store = new Map<string, string>();+ return {+ clear: () => store.clear(),+ getItem: (_) => store.get(_) ?? null,+ key: (_) => Record.keys(store).at(_) ?? null,+ get length() {+ return store.size;+ },+ removeItem: (_) => store.delete(_),+ setItem: (_, value) => store.set(_, value),+ };+})();++const getStorage = (): Storage =>+ typeof window !== "undefined" ? window.localStorage : fallbackStorage;const read = (key: string) => {try {- return isomorphicLocalStorage.getItem(key);+ return getStorage().getItem(key);} catch (cause) {throw new LocalStorageOperationError({ operation: "read", storageKey: key, cause });}@@ -58,13 +58,13 @@ const encode = <T, E>(key: string, schema: Schema.Codec<T, E>, value: T) => {export const getLocalStorageItem = <T, E>(key: string, schema: Schema.Codec<T, E>): T | null => {const item = read(key);- return item ? decode(key, schema, item) : null;+ return item === null ? null : decode(key, schema, item);};export const setLocalStorageItem = <T, E>(key: string, value: T, schema: Schema.Codec<T, E>) => {const valueToSet = encode(key, schema, value);try {- isomorphicLocalStorage.setItem(key, valueToSet);+ getStorage().setItem(key, valueToSet);} catch (cause) {throw new LocalStorageOperationError({ operation: "write", storageKey: key, cause });}@@ -72,7 +72,7 @@ export const setLocalStorageItem = <T, E>(key: string, value: T, schema: Schema.export const removeLocalStorageItem = (key: string) => {try {- isomorphicLocalStorage.removeItem(key);+ getStorage().removeItem(key);} catch (cause) {throw new LocalStorageOperationError({ operation: "remove", storageKey: key, cause });}diff --git a/apps/web/src/hooks/useSettings.test.ts b/apps/web/src/hooks/useSettings.test.tsindex 200e14241e1..d5542476688 100644--- a/apps/web/src/hooks/useSettings.test.ts+++ b/apps/web/src/hooks/useSettings.test.ts@@ -3,12 +3,22 @@ import {ProviderDriverKind,ProviderInstanceId,} from "@t3tools/contracts";-import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";-import { beforeEach, describe, expect, it, vi } from "vite-plus/test";+import { DEFAULT_CLIENT_SETTINGS, type ClientSettings } from "@t3tools/contracts/settings";+import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";++const persistenceMocks = vi.hoisted(() => ({+ getClientSettings: vi.fn<() => Promise<ClientSettings | null>>(),+ setClientSettings: vi.fn<(settings: ClientSettings) => Promise<void>>(),+}));++vi.mock("~/localApi", () => ({+ ensureLocalApi: () => ({ persistence: persistenceMocks }),+}));import {__resetClientSettingsPersistenceForTests,__setClientSettingsForTests,+ ensureClientSettingsHydrated,getClientSettings,mergeEnvironmentSettings,persistClientSettingsPatch,@@ -17,9 +27,138 @@ import {} from "./useSettings";beforeEach(() => {+ persistenceMocks.getClientSettings.mockReset().mockResolvedValue(null);+ persistenceMocks.setClientSettings.mockReset().mockResolvedValue(undefined);__resetClientSettingsPersistenceForTests();});+afterEach(() => {+ vi.restoreAllMocks();+});++describe("client settings hydration", () => {+ const savedSettings = {+ ...DEFAULT_CLIENT_SETTINGS,+ timestampFormat: "12-hour" as const,+ favorites: [{ provider: ProviderInstanceId.make("codex_work"), model: "gpt-5.6" }],+ };+ const onboardingCompletedAt = "2026-09-05T12:00:00.000Z";+ const complete = (current: ClientSettings) => ({ ...current, onboardingCompletedAt });++ it("rejects completion after a failed read and preserves saved preferences on retry", async () => {+ const failure = new Error("storage unavailable");+ vi.spyOn(console, "error").mockImplementation(() => undefined);+ persistenceMocks.getClientSettings+ .mockRejectedValueOnce(failure)+ .mockResolvedValue(savedSettings);++ await expect(persistClientSettingsUpdate(complete)).rejects.toBe(failure);+ expect(persistenceMocks.setClientSettings).not.toHaveBeenCalled();+ expect(getClientSettings()).toBe(DEFAULT_CLIENT_SETTINGS);++ const completedSettings = { ...savedSettings, onboardingCompletedAt };+ await expect(persistClientSettingsUpdate(complete)).resolves.toEqual(completedSettings);+ expect(persistenceMocks.setClientSettings).toHaveBeenCalledExactlyOnceWith(completedSettings);+ expect(persistenceMocks.getClientSettings).toHaveBeenCalledTimes(2);+ });++ it("uses defaults only after storage confirms no saved settings exist", async () => {+ const completedSettings = { ...DEFAULT_CLIENT_SETTINGS, onboardingCompletedAt };++ await expect(persistClientSettingsUpdate(complete)).resolves.toEqual(completedSettings);+ expect(persistenceMocks.getClientSettings).toHaveBeenCalledOnce();+ expect(persistenceMocks.setClientSettings).toHaveBeenCalledExactlyOnceWith(completedSettings);+ });++ it("holds patches until a pending read supplies the saved preferences", async () => {+ let finishRead!: (settings: ClientSettings) => void;+ persistenceMocks.getClientSettings.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ finishRead = resolve;+ }),+ );+ const persisted = new Promise<ClientSettings>((resolve) => {+ persistenceMocks.setClientSettings.mockImplementationOnce(async (settings) => {+ resolve(settings);+ });+ });++ const hydration = ensureClientSettingsHydrated();+ persistClientSettingsPatch({ wordWrap: false });+ expect(getClientSettings()).toBe(DEFAULT_CLIENT_SETTINGS);+ expect(persistenceMocks.setClientSettings).not.toHaveBeenCalled();++ finishRead(savedSettings);+ await hydration;+ await expect(persisted).resolves.toEqual({ ...savedSettings, wordWrap: false });+ expect(persistenceMocks.getClientSettings).toHaveBeenCalledOnce();+ });++ it("handles failed patch reads without writing and retries with the saved preferences", async () => {+ const failure = new Error("storage unavailable");+ vi.spyOn(console, "error").mockImplementation(() => undefined);+ persistenceMocks.getClientSettings.mockRejectedValue(failure);++ const hydration = ensureClientSettingsHydrated();+ persistClientSettingsPatch({ wordWrap: false });+ await expect(hydration).rejects.toBe(failure);+ expect(persistenceMocks.setClientSettings).not.toHaveBeenCalled();++ persistenceMocks.getClientSettings.mockResolvedValue(savedSettings);+ const persisted = new Promise<ClientSettings>((resolve) => {+ persistenceMocks.setClientSettings.mockImplementationOnce(async (settings) => {+ resolve(settings);+ });+ });+ persistClientSettingsPatch({ wordWrap: false });++ await expect(persisted).resolves.toEqual({ ...savedSettings, wordWrap: false });+ });++ it("preserves patch order across hydration and a blocked completion write", async () => {+ let finishRead!: (settings: ClientSettings) => void;+ const read = new Promise<ClientSettings>((resolve) => {+ finishRead = resolve;+ });+ persistenceMocks.getClientSettings.mockReturnValue(read);+ let finishCompletionWrite!: () => void;+ const blockedWrite = new Promise<void>((resolve) => {+ finishCompletionWrite = resolve;+ });+ let signalCompletionWrite!: () => void;+ const completionWriteStarted = new Promise<void>((resolve) => {+ signalCompletionWrite = resolve;+ });+ let durableSettings: ClientSettings = savedSettings;+ const persist = vi+ .fn<(settings: ClientSettings) => Promise<void>>()+ .mockImplementationOnce(async (settings) => {+ signalCompletionWrite();+ await blockedWrite;+ durableSettings = settings;+ })+ .mockImplementation(async (settings) => {+ durableSettings = settings;+ });++ const completion = persistClientSettingsUpdate(complete, persist);+ persistClientSettingsPatch({ wordWrap: false }, persist);+ finishRead(savedSettings);+ await completionWriteStarted;+ persistClientSettingsPatch({ wordWrap: true }, persist);+ const finalWrite = persistClientSettingsUpdate((current) => current, persist);++ finishCompletionWrite();+ await completion;+ await finalWrite;++ const expected = { ...savedSettings, onboardingCompletedAt, wordWrap: true };+ expect(getClientSettings()).toEqual(expected);+ expect(durableSettings).toEqual(expected);+ });+});+describe("persistClientSettingsUpdate", () => {it("publishes the update only after persistence succeeds", async () => {let finishPersistence!: () => void;@@ -245,3 +384,40 @@ describe("mergeEnvironmentSettings", () => {expect(settings.sidebarAutoSettleOnMerge).toBe(false);});});++describe("onboarding completion persistence", () => {+ it("keeps onboarding incomplete after a failed save and preserves preferences on retry", async () => {+ const failure = new Error("disk full");+ const persist = vi+ .fn<(settings: typeof DEFAULT_CLIENT_SETTINGS) => Promise<void>>()+ .mockRejectedValueOnce(failure)+ .mockResolvedValue(undefined);+ const existingSettings = {+ ...DEFAULT_CLIENT_SETTINGS,+ timestampFormat: "12-hour" as const,+ favorites: [+ {+ provider: ProviderInstanceId.make("codex_work"),+ model: "gpt-5.6",+ },+ ],+ };+ __setClientSettingsForTests(existingSettings);+ const onboardingCompletedAt = "2026-09-01T12:00:00.000Z";+ const complete = (current: typeof DEFAULT_CLIENT_SETTINGS) => ({+ ...current,+ onboardingCompletedAt,+ });++ await expect(persistClientSettingsUpdate(complete, persist)).rejects.toBe(failure);+ expect(getClientSettings()).toBe(existingSettings);+ expect(getClientSettings().onboardingCompletedAt).toBeNull();++ const completedSettings = { ...existingSettings, onboardingCompletedAt };+ await expect(persistClientSettingsUpdate(complete, persist)).resolves.toEqual(+ completedSettings,+ );+ expect(getClientSettings()).toEqual(completedSettings);+ expect(persist).toHaveBeenLastCalledWith(completedSettings);+ });+});diff --git a/apps/web/src/hooks/useSettings.ts b/apps/web/src/hooks/useSettings.tsindex 52a6fec12f0..9b428b08ea5 100644--- a/apps/web/src/hooks/useSettings.ts+++ b/apps/web/src/hooks/useSettings.ts@@ -54,11 +54,13 @@ type UnifiedSettingsPatch = ServerSettingsPatch & ClientSettingsPatch;const clientSettingsListeners = new Set<() => void>();const clientSettingsHydrationListeners = new Set<() => void>();+type ClientSettingsHydrationStatus = "pending" | "ready" | "failed" | "retrying";let clientSettingsSnapshot = DEFAULT_CLIENT_SETTINGS;-let clientSettingsHydrated = false;+let clientSettingsHydrationStatus: ClientSettingsHydrationStatus = "pending";let clientSettingsHydrationPromise: Promise<void> | null = null;let clientSettingsHydrationGeneration = 0;let clientSettingsPersistenceQueue: Promise<void> = Promise.resolve();+let deferredClientSettingsPatchCount = 0;function emitClientSettingsChange() {for (const listener of clientSettingsListeners) {@@ -81,36 +83,40 @@ function replaceClientSettingsSnapshot(settings: ClientSettings): void {emitClientSettingsChange();}-function setClientSettingsHydrated(nextHydrated: boolean): void {- if (clientSettingsHydrated === nextHydrated) {+function setClientSettingsHydrationStatus(nextStatus: ClientSettingsHydrationStatus): void {+ if (clientSettingsHydrationStatus === nextStatus) {return;}- clientSettingsHydrated = nextHydrated;+ clientSettingsHydrationStatus = nextStatus;emitClientSettingsHydrationChange();}function subscribeClientSettings(listener: () => void): () => void {clientSettingsListeners.add(listener);- void hydrateClientSettings();+ void hydrateClientSettings().catch(() => undefined);return () => {clientSettingsListeners.delete(listener);};}function getClientSettingsHydratedSnapshot(): boolean {- return clientSettingsHydrated;+ return clientSettingsHydrationStatus === "ready";+}++function getClientSettingsHydrationStatusSnapshot(): ClientSettingsHydrationStatus {+ return clientSettingsHydrationStatus;}function subscribeClientSettingsHydration(listener: () => void): () => void {clientSettingsHydrationListeners.add(listener);- void hydrateClientSettings();+ void hydrateClientSettings().catch(() => undefined);return () => {clientSettingsHydrationListeners.delete(listener);};}async function hydrateClientSettings(): Promise<void> {- if (clientSettingsHydrated) {+ if (clientSettingsHydrationStatus === "ready") {return;}if (clientSettingsHydrationPromise) {@@ -118,6 +124,11 @@ async function hydrateClientSettings(): Promise<void> {}const hydrationGeneration = clientSettingsHydrationGeneration;+ setClientSettingsHydrationStatus(+ clientSettingsHydrationStatus === "failed" || clientSettingsHydrationStatus === "retrying"+ ? "retrying"+ : "pending",+ );const nextHydration = (async () => {try {const persistedSettings = await ensureLocalApi().persistence.getClientSettings();@@ -127,15 +138,16 @@ async function hydrateClientSettings(): Promise<void> {if (persistedSettings) {replaceClientSettingsSnapshot({ ...DEFAULT_CLIENT_SETTINGS, ...persistedSettings });}+ setClientSettingsHydrationStatus("ready");} catch (error) {+ if (hydrationGeneration === clientSettingsHydrationGeneration) {+ setClientSettingsHydrationStatus("failed");+ }console.error(`${CLIENT_SETTINGS_PERSISTENCE_ERROR_SCOPE} hydrate failed`, {operation: "hydrate",...safeErrorLogAttributes(error),});- } finally {- if (hydrationGeneration === clientSettingsHydrationGeneration) {- setClientSettingsHydrated(true);- }+ throw error;}})();@@ -165,15 +177,32 @@ export function persistClientSettingsPatch(patch: ClientSettingsPatch,persist: (settings: ClientSettings) => Promise<void> = defaultClientSettingsPersistence,): void {- replaceClientSettingsSnapshot({ ...getClientSettingsSnapshot(), ...patch });- void enqueueClientSettingsPersistence(() => persist(getClientSettingsSnapshot())).catch(- (error) => {- console.error(`${CLIENT_SETTINGS_PERSISTENCE_ERROR_SCOPE} persist failed`, {- operation: "persist",- ...safeErrorLogAttributes(error),- });- },- );+ // Patches queued before hydration must publish before newer optimistic patches.+ const deferPatch =+ clientSettingsHydrationStatus !== "ready" || deferredClientSettingsPatchCount > 0;+ if (deferPatch) {+ deferredClientSettingsPatchCount += 1;+ } else {+ replaceClientSettingsSnapshot({ ...getClientSettingsSnapshot(), ...patch });+ }+ void enqueueClientSettingsPersistence(async () => {+ if (deferPatch) {+ try {+ if (clientSettingsHydrationStatus !== "ready") {+ await hydrateClientSettings();+ }+ replaceClientSettingsSnapshot({ ...getClientSettingsSnapshot(), ...patch });+ } finally {+ deferredClientSettingsPatchCount -= 1;+ }+ }+ await persist(getClientSettingsSnapshot());+ }).catch((error) => {+ console.error(`${CLIENT_SETTINGS_PERSISTENCE_ERROR_SCOPE} persist failed`, {+ operation: "persist",+ ...safeErrorLogAttributes(error),+ });+ });}/**@@ -187,6 +216,9 @@ export async function persistClientSettingsUpdate(persist: (settings: ClientSettings) => Promise<void> = defaultClientSettingsPersistence,): Promise<ClientSettings> {return enqueueClientSettingsPersistence(async () => {+ if (clientSettingsHydrationStatus !== "ready") {+ await hydrateClientSettings();+ }for (;;) {const current = getClientSettingsSnapshot();const next = update(current);@@ -234,7 +266,9 @@ export function getClientSettings(): ClientSettings {}/**- * Resolves once client settings have been read from disk.+ * Resolves after settings load or storage confirms no saved settings exist.+ * Failed reads reject and remain retryable. They must not allow defaults to+ * overwrite saved preferences.** The pre-hydration snapshot is just the schema defaults, so imperative paths* that open a preview must await this or they bake the built-in viewport, zoom@@ -252,6 +286,14 @@ export function useClientSettingsHydrated(): boolean {);}+export function useClientSettingsHydrationStatus(): ClientSettingsHydrationStatus {+ return useSyncExternalStore(+ subscribeClientSettingsHydration,+ getClientSettingsHydrationStatusSnapshot,+ () => "pending",+ );+}+function useClientSettingsValue(): ClientSettings {return useSyncExternalStore(subscribeClientSettings,@@ -524,9 +566,10 @@ export function useUpdateClientSettings() {export function __resetClientSettingsPersistenceForTests(): void {clientSettingsHydrationGeneration += 1;clientSettingsSnapshot = DEFAULT_CLIENT_SETTINGS;- clientSettingsHydrated = false;+ clientSettingsHydrationStatus = "pending";clientSettingsHydrationPromise = null;clientSettingsPersistenceQueue = Promise.resolve();+ deferredClientSettingsPatchCount = 0;clientSettingsListeners.clear();clientSettingsHydrationListeners.clear();}@@ -534,6 +577,6 @@ export function __resetClientSettingsPersistenceForTests(): void {export function __setClientSettingsForTests(settings: ClientSettings): void {clientSettingsHydrationGeneration += 1;clientSettingsSnapshot = settings;- clientSettingsHydrated = true;+ clientSettingsHydrationStatus = "ready";clientSettingsHydrationPromise = null;}diff --git a/apps/web/src/hooks/useTheme.test.ts b/apps/web/src/hooks/useTheme.test.tsindex ab87388ff29..9a1748dc4d2 100644--- a/apps/web/src/hooks/useTheme.test.ts+++ b/apps/web/src/hooks/useTheme.test.ts@@ -204,3 +204,200 @@ describe("theme failure handling", () => {}});});++describe("onboarding theme", () => {+ it("clears custom palettes and restores the latest selected theme", async () => {+ const storage = createStorage();+ const classes = new Set<string>();+ const styleValues = new Map<string, string>();+ const root = {+ classList: {+ add: (name: string) => classes.add(name),+ remove: (name: string) => classes.delete(name),+ toggle: (name: string, force?: boolean) => {+ const next = force ?? !classes.has(name);+ if (next) classes.add(name);+ else classes.delete(name);+ return next;+ },+ },+ dataset: {} as Record<string, string>,+ offsetHeight: 0,+ style: {+ backgroundColor: "",+ removeProperty: (name: string) => styleValues.delete(name),+ setProperty: (name: string, value: string) => styleValues.set(name, value),+ },+ };+ vi.doMock("react", () => ({+ useCallback: <A>(callback: A) => callback,+ useEffect: () => undefined,+ useSyncExternalStore: (+ subscribe: (listener: () => void) => () => void,+ getSnapshot: () => unknown,+ ) => {+ subscribe(() => undefined);+ return getSnapshot();+ },+ }));+ vi.stubGlobal("window", {+ addEventListener: () => undefined,+ localStorage: storage,+ matchMedia: () => ({+ matches: false,+ addEventListener: () => undefined,+ removeEventListener: () => undefined,+ }),+ removeEventListener: () => undefined,+ });+ vi.stubGlobal("document", {+ body: { style: { backgroundColor: "" } },+ createElement: () => ({ name: "", setAttribute: () => undefined }),+ documentElement: root,+ head: { append: () => undefined },+ querySelector: () => null,+ querySelectorAll: () => [],+ });+ vi.stubGlobal("getComputedStyle", () => ({+ backgroundColor: "rgb(0, 0, 0)",+ getPropertyValue: () => "",+ }));+ vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => {+ callback(0);+ return 0;+ });++ const { EMBER_THEME, installCustomTheme } = await import("../themePalette");+ const firstTheme = installCustomTheme({+ ...EMBER_THEME,+ id: "first-custom",+ label: "First Custom",+ });+ const secondTheme = installCustomTheme({+ ...EMBER_THEME,+ id: "second-custom",+ label: "Second Custom",+ colors: { ...EMBER_THEME.colors, error: "#123456" },+ });+ storage.setItem("t3code:theme", firstTheme.id);++ const { mountOnboardingTheme, useTheme } = await import("./useTheme");+ expect(root.dataset.themeId).toBe(firstTheme.id);+ expect(styleValues.get("--app-theme-error")).toBe(firstTheme.colors.error);++ const cleanup = mountOnboardingTheme();+ expect(root.dataset.themeId).toBeUndefined();+ expect(styleValues.size).toBe(0);++ expect(useTheme().setTheme(secondTheme.id)).toBe(true);+ expect(root.dataset.themeId).toBeUndefined();+ expect(styleValues.size).toBe(0);++ cleanup();+ expect(root.dataset.themeId).toBe(secondTheme.id);+ expect(styleValues.get("--app-theme-error")).toBe(secondTheme.colors.error);+ });++ it("stays dark during storage changes and restores the latest saved theme", async () => {+ const storage = createStorage();+ storage.setItem("t3code:theme", "light");+ const classes = new Set<string>();+ const styleValues = new Map<string, string>();+ const style = {+ backgroundColor: "",+ removeProperty: (name: string) => styleValues.delete(name),+ setProperty: (name: string, value: string) => styleValues.set(name, value),+ };+ const root = {+ classList: {+ add: (name: string) => classes.add(name),+ contains: (name: string) => classes.has(name),+ remove: (name: string) => classes.delete(name),+ toggle: (name: string, force?: boolean) => {+ const next = force ?? !classes.has(name);+ if (next) classes.add(name);+ else classes.delete(name);+ return next;+ },+ },+ dataset: {} as Record<string, string>,+ offsetHeight: 0,+ style,+ };+ const body = { style: { backgroundColor: "" } };+ let storageHandler: ((event: StorageEvent) => void) | undefined;+ const setDesktopTheme = vi.fn().mockResolvedValue(undefined);+ vi.doMock("react", () => ({+ useCallback: <A>(callback: A) => callback,+ useEffect: () => undefined,+ useSyncExternalStore: (+ subscribe: (listener: () => void) => () => void,+ getSnapshot: () => unknown,+ ) => {+ subscribe(() => undefined);+ return getSnapshot();+ },+ }));+ vi.stubGlobal("window", {+ addEventListener: (type: string, listener: (event: StorageEvent) => void) => {+ if (type === "storage") storageHandler = listener;+ },+ localStorage: storage,+ matchMedia: () => ({+ matches: false,+ addEventListener: () => undefined,+ removeEventListener: () => undefined,+ }),+ removeEventListener: () => undefined,+ desktopBridge: { setTheme: setDesktopTheme },+ });+ vi.stubGlobal("document", {+ body,+ createElement: () => ({ name: "", setAttribute: () => undefined }),+ documentElement: root,+ head: { append: () => undefined },+ querySelector: () => null,+ querySelectorAll: () => [],+ });+ vi.stubGlobal("getComputedStyle", () => ({+ backgroundColor:+ root.dataset.onboardingSurface !== undefined+ ? "rgb(0, 0, 0)"+ : classes.has("dark")+ ? "rgb(10, 10, 10)"+ : "rgb(255, 255, 255)",+ getPropertyValue: () => "",+ }));+ vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => {+ callback(0);+ return 0;+ });++ const { mountOnboardingTheme, useTheme } = await import("./useTheme");+ expect(useTheme().resolvedTheme).toBe("light");+ const cleanup = mountOnboardingTheme();++ expect(root.dataset.onboardingSurface).toBe("");+ expect(classes.has("dark")).toBe(true);+ expect(root.style.backgroundColor).toBe("#000");+ expect(body.style.backgroundColor).toBe("#000");+ expect(useTheme().resolvedTheme).toBe("dark");+ expect(setDesktopTheme).toHaveBeenLastCalledWith("dark");++ storage.setItem("t3code:theme", "dark");+ storageHandler?.({ key: "t3code:theme" } as StorageEvent);+ storage.setItem("t3code:theme", "light");+ storageHandler?.({ key: "t3code:theme" } as StorageEvent);+ expect(classes.has("dark")).toBe(true);+ expect(useTheme().resolvedTheme).toBe("dark");++ cleanup();+ expect(root.dataset.onboardingSurface).toBeUndefined();+ expect(classes.has("dark")).toBe(false);+ expect(root.style.backgroundColor).toBe("rgb(255, 255, 255)");+ expect(body.style.backgroundColor).toBe("rgb(255, 255, 255)");+ expect(storage.getItem("t3code:theme")).toBe("light");+ expect(useTheme().resolvedTheme).toBe("light");+ expect(setDesktopTheme).toHaveBeenLastCalledWith("light");+ });+});diff --git a/apps/web/src/hooks/useTheme.ts b/apps/web/src/hooks/useTheme.tsindex 726a03dac7b..01928552acb 100644--- a/apps/web/src/hooks/useTheme.ts+++ b/apps/web/src/hooks/useTheme.ts@@ -98,6 +98,14 @@ function readStoredThemeHalvesRaw(): { light?: string; dark?: string } {function themeHalvesSignature(halves: ThemeHalves | null): string {return `${halves?.light ?? ""}|${halves?.dark ?? ""}`;}++function isOnboardingThemeActive(): boolean {+ return (+ typeof document !== "undefined" &&+ document.documentElement.dataset?.onboardingSurface !== undefined+ );+}+const THEME_COLOR_META_NAME = "theme-color";const DYNAMIC_THEME_COLOR_SELECTOR = `meta[name="${THEME_COLOR_META_NAME}"][data-dynamic-theme-color="true"]`;@@ -292,15 +300,19 @@ function resolveBrowserChromeSurface(): HTMLElement {export function syncBrowserChromeTheme() {if (typeof document === "undefined" || typeof getComputedStyle === "undefined") return;+ const onboardingActive = isOnboardingThemeActive();const rootStyles = getComputedStyle(document.documentElement);- const themeChromeColor = document.documentElement.dataset.themeId- ? normalizeThemeColor(rootStyles.getPropertyValue("--app-chrome-background"))- : null;+ const themeChromeColor =+ !onboardingActive && document.documentElement.dataset.themeId+ ? normalizeThemeColor(rootStyles.getPropertyValue("--app-chrome-background"))+ : null;const surfaceColor = normalizeThemeColor(getComputedStyle(resolveBrowserChromeSurface()).backgroundColor,);const fallbackColor = normalizeThemeColor(getComputedStyle(document.body).backgroundColor);- const backgroundColor = themeChromeColor ?? surfaceColor ?? fallbackColor;+ const backgroundColor = onboardingActive+ ? "#000"+ : (themeChromeColor ?? surfaceColor ?? fallbackColor);if (!backgroundColor) return;document.documentElement.style.backgroundColor = backgroundColor;@@ -321,8 +333,15 @@ export function syncBrowserChromeTheme() {function applyTheme(theme: Theme, { suppressTransitions = false, preservePreview = true } = {}) {if (typeof document === "undefined" || typeof window === "undefined") return;+ const onboardingActive = isOnboardingThemeActive();// Keep the editor's draft visible until an explicit refresh restores the selection.- if (preservePreview && document.documentElement.dataset?.themeId === THEME_PREVIEW_ID) return;+ if (+ preservePreview &&+ !onboardingActive &&+ document.documentElement.dataset?.themeId === THEME_PREVIEW_ID+ ) {+ return;+ }const appearanceMode = readAppearanceModePreference(theme);const followSystem = appearanceMode === "system";const systemDark = followSystem ? getSystemDark() : false;@@ -334,7 +353,13 @@ function applyTheme(theme: Theme, { suppressTransitions = false, preservePreviewlastAppliedTheme.appearanceMode === appearanceMode &&themeHalvesSignature(lastAppliedTheme.themeHalves) === themeHalvesSignature(themeHalves)) {- syncDesktopTheme(theme, followSystem, appearanceMode);+ if (onboardingActive) {+ document.documentElement.classList.add("dark");+ syncBrowserChromeTheme();+ syncDesktopTheme("dark", false, "dark");+ } else {+ syncDesktopTheme(theme, followSystem, appearanceMode);+ }return;}@@ -348,12 +373,19 @@ function applyTheme(theme: Theme, { suppressTransitions = false, preservePreviewappearanceMode,themeHalves,);- applyThemePalette(resolveThemeHalf(theme, themeHalves, resolvedAppearance), resolvedAppearance);- const isDark = resolvedAppearance === "dark";- document.documentElement.classList.toggle("dark", isDark);+ if (onboardingActive) {+ document.documentElement.classList.add("dark");+ } else {+ applyThemePalette(resolveThemeHalf(theme, themeHalves, resolvedAppearance), resolvedAppearance);+ document.documentElement.classList.toggle("dark", resolvedAppearance === "dark");+ }lastAppliedTheme = { theme, systemDark, followSystem, appearanceMode, themeHalves };syncBrowserChromeTheme();- syncDesktopTheme(theme, followSystem, appearanceMode);+ if (onboardingActive) {+ syncDesktopTheme("dark", false, "dark");+ } else {+ syncDesktopTheme(theme, followSystem, appearanceMode);+ }if (suppressTransitions) {// Force a reflow so the no-transitions class takes effect before removalvoid document.documentElement.offsetHeight;@@ -363,6 +395,28 @@ function applyTheme(theme: Theme, { suppressTransitions = false, preservePreview}}+/** Own the document-wide dark palette used by the first-run wizard and its portals. */+export function mountOnboardingTheme(): () => void {+ if (typeof document === "undefined" || typeof window === "undefined") return () => {};++ const root = document.documentElement;+ applyThemePalette("dark", "dark");+ root.dataset.onboardingSurface = "";+ root.classList.add("dark");+ syncBrowserChromeTheme();+ syncDesktopTheme("dark", false, "dark");+ emitChange();++ return () => {+ delete root.dataset.onboardingSurface;+ root.style.backgroundColor = "";+ document.body.style.backgroundColor = "";+ lastAppliedTheme = null;+ applyTheme(getStored(), { suppressTransitions: true, preservePreview: false });+ emitChange();+ };+}+export async function syncDesktopThemePreference(bridge: DesktopThemeBridge,theme: Theme,@@ -424,13 +478,9 @@ function getSnapshot(): ThemeSnapshot {const systemDark = followSystem ? getSystemDark() : false;const themeHalves = readStoredThemeHalves();- const resolvedTheme = resolveThemeAppearance(- theme,- systemDark,- followSystem,- appearanceMode,- themeHalves,- );+ const resolvedTheme = isOnboardingThemeActive()+ ? "dark"+ : resolveThemeAppearance(theme, systemDark, followSystem, appearanceMode, themeHalves);if (lastSnapshot &&lastSnapshot.theme === theme &&diff --git a/apps/web/src/index.css b/apps/web/src/index.cssindex 68b0101be28..45d10f28d73 100644--- a/apps/web/src/index.css+++ b/apps/web/src/index.css@@ -1184,6 +1184,39 @@ html[data-theme-id]:not([data-theme-id=""]) {--terminal-selection-background: var(--app-theme-terminal-selection-background);}+/* The first-run flow owns the whole document so portaled menus and tooltips+ use the same fixed palette as the wizard. This follows the theme mapping so+ saved custom themes cannot override it while onboarding is mounted. */+html[data-onboarding-surface]:root {+ color-scheme: dark;+ --accent: #262626;+ --accent-foreground: #fff;+ --appearance-contrast-target: #fff;+ --app-chrome-background: #000;+ --background: #000;+ --border: #262626;+ --card: #000;+ --card-foreground: #fff;+ --destructive: var(--color-red-400);+ --foreground: #fff;+ --icon-muted: #a1a1aa;+ --input: #262626;+ --muted: #171717;+ --muted-foreground: #a1a1aa;+ --placeholder: #71717a;+ --popover: #171717;+ --popover-foreground: #fff;+ --ring: #737373;+ --secondary: #171717;+ --secondary-foreground: #fff;+ --secondary-label: #a1a1aa;+ --success-foreground: var(--color-emerald-400);+ --terminal-background: #000;+ --terminal-cursor: #fff;+ --terminal-foreground: #fff;+ --terminal-selection-background: rgb(255 255 255 / 20%);+}+/* Theme-token dependency probes are restored synchronously, before paint. Keeptransitions from observing the temporary sentinel color in between. */html[data-theme-token-probe],@@ -1385,11 +1418,10 @@ html[data-theme-id="t3-chat"] [data-app-sidebar] {}}-/* Contrast stays in ordinary custom properties so both Tailwind utilities and- global/imperative chrome styles resolve the same adjusted role. Redeclare on- the sidebar because it owns a local semantic palette. */+/* Recompute contrast wherever a subtree owns its own semantic color palette. */:root,-[data-app-sidebar] {+[data-app-sidebar],+[data-onboarding-surface] {--contrast-toolbar-foreground: color-mix(in oklab,color-mix(diff --git a/apps/web/src/onboarding/firstRun.logic.test.ts b/apps/web/src/onboarding/firstRun.logic.test.tsnew file mode 100644index 00000000000..ca35f6322e3--- /dev/null+++ b/apps/web/src/onboarding/firstRun.logic.test.ts@@ -0,0 +1,514 @@+import { describe, expect, it } from "vite-plus/test";++import {+ isFirstRunWorkspaceProvenanceAuthoritative,+ isFreshFirstRunWorkspace,+ resolveFirstRunDecision,+ resolveHostedFirstRunDecision,+ transitionFirstRunGateState,+} from "./firstRun.logic";++const freshWorkspace = {+ enabled: true,+ hydrated: true,+ completed: false,+ bootstrapped: true,+ authoritative: true,+ workspaceAuthoritative: true,+ workspaceProvenanceAuthoritative: true,+ catalogReady: true,+ serverConfigAvailable: true,+ workspaceFresh: true,+ projectCount: 1,+ threadCount: 1,+} as const;++describe("resolveFirstRunDecision", () => {+ it("opens the wizard for an authoritative fresh workspace", () => {+ expect(resolveFirstRunDecision(freshWorkspace)).toEqual({+ decision: "wizard",+ persistCompletion: false,+ });+ });++ it("does not permanently complete onboarding from cached project counts", () => {+ expect(+ resolveFirstRunDecision({+ ...freshWorkspace,+ authoritative: false,+ projectCount: 3,+ workspaceFresh: false,+ }),+ ).toEqual({+ decision: "app",+ persistCompletion: false,+ });+ });++ it("backfills completion once existing projects are confirmed by the server", () => {+ expect(+ resolveFirstRunDecision({+ ...freshWorkspace,+ projectCount: 3,+ workspaceFresh: false,+ }),+ ).toEqual({+ decision: "app",+ persistCompletion: true,+ });+ });++ it("does not complete onboarding while another environment is still bootstrapping", () => {+ expect(+ resolveFirstRunDecision({+ ...freshWorkspace,+ bootstrapped: false,+ projectCount: 3,+ workspaceFresh: false,+ }),+ ).toEqual({+ decision: "app",+ persistCompletion: false,+ });+ });++ it("waits for managed environments before treating a workspace as new", () => {+ expect(resolveFirstRunDecision({ ...freshWorkspace, catalogReady: false })).toEqual({+ decision: "pending",+ persistCompletion: false,+ });+ });++ it("does not complete onboarding before the environment catalog is ready", () => {+ expect(+ resolveFirstRunDecision({+ ...freshWorkspace,+ catalogReady: false,+ projectCount: 3,+ workspaceFresh: false,+ }),+ ).toEqual({+ decision: "app",+ persistCompletion: false,+ });+ });++ it("does not complete onboarding before the server configuration is available", () => {+ expect(+ resolveFirstRunDecision({+ ...freshWorkspace,+ projectCount: 3,+ serverConfigAvailable: false,+ workspaceFresh: false,+ }),+ ).toEqual({+ decision: "app",+ persistCompletion: false,+ });+ });++ it("does not complete onboarding from cached remote projects", () => {+ expect(+ resolveFirstRunDecision({+ ...freshWorkspace,+ projectCount: 3,+ workspaceAuthoritative: false,+ workspaceFresh: false,+ }),+ ).toEqual({+ decision: "app",+ persistCompletion: false,+ });+ });++ it("does not complete onboarding from a single cached remote project", () => {+ expect(+ resolveFirstRunDecision({+ ...freshWorkspace,+ workspaceAuthoritative: false,+ workspaceFresh: false,+ }),+ ).toEqual({+ decision: "app",+ persistCompletion: false,+ });+ });++ it("waits for live data before judging a single cached project", () => {+ expect(+ resolveFirstRunDecision({+ ...freshWorkspace,+ authoritative: false,+ workspaceFresh: false,+ }),+ ).toEqual({+ decision: "pending",+ persistCompletion: false,+ });+ });++ it("waits for the completed bootstrap welcome before judging a nonempty workspace", () => {+ expect(+ resolveFirstRunDecision({+ ...freshWorkspace,+ workspaceProvenanceAuthoritative: false,+ }),+ ).toEqual({+ decision: "pending",+ persistCompletion: false,+ });+ });++ it("waits when the initial welcome is pending and opens the wizard after completion", () => {+ const pendingProvenance = isFirstRunWorkspaceProvenanceAuthoritative({+ welcomeReceived: true,+ bootstrapStatus: "pending",+ });+ expect(+ resolveFirstRunDecision({+ ...freshWorkspace,+ workspaceProvenanceAuthoritative: pendingProvenance,+ }),+ ).toEqual({ decision: "pending", persistCompletion: false });++ const completedProvenance = isFirstRunWorkspaceProvenanceAuthoritative({+ welcomeReceived: true,+ bootstrapStatus: "complete",+ });+ expect(+ resolveFirstRunDecision({+ ...freshWorkspace,+ workspaceProvenanceAuthoritative: completedProvenance,+ }),+ ).toEqual({ decision: "wizard", persistCompletion: false });+ });++ it("does not wait for server data after onboarding is already complete", () => {+ expect(+ resolveFirstRunDecision({+ ...freshWorkspace,+ authoritative: false,+ bootstrapped: false,+ completed: true,+ serverConfigAvailable: false,+ }),+ ).toEqual({+ decision: "app",+ persistCompletion: false,+ });+ });+});++describe("isFirstRunWorkspaceProvenanceAuthoritative", () => {+ it("waits for cwd bootstrap when the initial catalog is empty", () => {+ expect(+ isFirstRunWorkspaceProvenanceAuthoritative({+ welcomeReceived: true,+ bootstrapStatus: "pending",+ }),+ ).toBe(false);+ });++ it("accepts an empty catalog after cwd bootstrap completes", () => {+ expect(+ isFirstRunWorkspaceProvenanceAuthoritative({+ welcomeReceived: true,+ bootstrapStatus: "complete",+ }),+ ).toBe(true);+ });++ it("waits for a welcome before treating an empty catalog as final", () => {+ expect(+ isFirstRunWorkspaceProvenanceAuthoritative({+ welcomeReceived: false,+ bootstrapStatus: null,+ }),+ ).toBe(false);+ });++ it("accepts a legacy welcome without bootstrap status", () => {+ expect(+ isFirstRunWorkspaceProvenanceAuthoritative({+ welcomeReceived: true,+ bootstrapStatus: null,+ }),+ ).toBe(true);+ });+});++describe("transitionFirstRunGateState", () => {+ it("shows recovery without mounting the app when evidence stalls", () => {+ expect(+ transitionFirstRunGateState({ decision: "pending", stalled: false }, { type: "timeout" }),+ ).toEqual({ decision: "pending", stalled: true });+ });++ it.each(["app", "wizard"] as const)(+ "resolves stalled recovery to %s only after authoritative evidence",+ (decision) => {+ expect(+ transitionFirstRunGateState(+ { decision: "pending", stalled: true },+ { type: "evidence", decision },+ ),+ ).toEqual({ decision, stalled: false });+ },+ );++ it("keeps recovery visible while evidence remains pending", () => {+ const state = { decision: "pending", stalled: true } as const;+ expect(transitionFirstRunGateState(state, { type: "evidence", decision: "pending" })).toBe(+ state,+ );+ });++ it("allows authoritative wizard evidence to replace an app decision", () => {+ expect(+ transitionFirstRunGateState(+ { decision: "app", stalled: false },+ { type: "evidence", decision: "wizard" },+ ),+ ).toEqual({ decision: "wizard", stalled: false });+ });+});++describe("resolveHostedFirstRunDecision", () => {+ it("keeps the shell hidden until client settings are hydrated", () => {+ expect(+ resolveHostedFirstRunDecision({+ hydrated: false,+ completed: false,+ catalogReady: true,+ environmentCount: 0,+ }),+ ).toEqual({+ decision: "pending",+ persistCompletion: false,+ });+ });++ it("waits for the saved environment catalog before judging a hosted install", () => {+ expect(+ resolveHostedFirstRunDecision({+ hydrated: true,+ completed: false,+ catalogReady: false,+ environmentCount: 0,+ }),+ ).toEqual({+ decision: "pending",+ persistCompletion: false,+ });+ });++ it("opens onboarding when a hosted install has no saved environments", () => {+ expect(+ resolveHostedFirstRunDecision({+ hydrated: true,+ completed: false,+ catalogReady: true,+ environmentCount: 0,+ }),+ ).toEqual({+ decision: "wizard",+ persistCompletion: false,+ });+ });++ it("backfills onboarding for a hosted install with saved environments", () => {+ expect(+ resolveHostedFirstRunDecision({+ hydrated: true,+ completed: false,+ catalogReady: true,+ environmentCount: 1,+ }),+ ).toEqual({+ decision: "app",+ persistCompletion: true,+ });+ });++ it("opens the app immediately after hosted onboarding is complete", () => {+ expect(+ resolveHostedFirstRunDecision({+ hydrated: true,+ completed: true,+ catalogReady: false,+ environmentCount: 0,+ }),+ ).toEqual({+ decision: "app",+ persistCompletion: false,+ });+ });+});++const primaryEnvironmentId = "primary-environment";+const bootstrapProject = {+ id: "bootstrap-project",+ environmentId: primaryEnvironmentId,+ workspaceRoot: "/projects/current",+};+const bootstrapThread = {+ id: "bootstrap-thread",+ projectId: bootstrapProject.id,+ environmentId: primaryEnvironmentId,+ latestTurn: null,+ latestUserMessageAt: null,+ session: null,+};++describe("isFreshFirstRunWorkspace", () => {+ it("accepts an empty workspace", () => {+ expect(+ isFreshFirstRunWorkspace({+ primaryEnvironmentId,+ serverCwd: "/projects/current",+ projects: [],+ threads: [],+ }),+ ).toBe(true);+ });++ it("accepts only the unused project and thread created from the server cwd", () => {+ expect(+ isFreshFirstRunWorkspace({+ primaryEnvironmentId,+ serverCwd: "/projects/current/",+ bootstrapProjectId: bootstrapProject.id,+ bootstrapThreadId: bootstrapThread.id,+ bootstrapProjectCreated: true,+ bootstrapThreadCreated: true,+ projects: [bootstrapProject],+ threads: [bootstrapThread],+ }),+ ).toBe(true);+ });++ it("rejects an existing unused cwd project and thread reused by startup", () => {+ expect(+ isFreshFirstRunWorkspace({+ primaryEnvironmentId,+ serverCwd: "/projects/current",+ bootstrapProjectId: bootstrapProject.id,+ bootstrapThreadId: bootstrapThread.id,+ bootstrapProjectCreated: false,+ bootstrapThreadCreated: false,+ projects: [bootstrapProject],+ threads: [bootstrapThread],+ }),+ ).toBe(false);+ });++ it("rejects a nonempty workspace when an older server omits creation provenance", () => {+ expect(+ isFreshFirstRunWorkspace({+ primaryEnvironmentId,+ serverCwd: "/projects/current",+ bootstrapProjectId: bootstrapProject.id,+ bootstrapThreadId: bootstrapThread.id,+ projects: [bootstrapProject],+ threads: [bootstrapThread],+ }),+ ).toBe(false);+ });++ it("normalizes Windows project paths before checking the bootstrap workspace", () => {+ expect(+ isFreshFirstRunWorkspace({+ primaryEnvironmentId,+ serverCwd: "C:\\Projects\\Current\\",+ bootstrapProjectId: bootstrapProject.id,+ bootstrapThreadId: bootstrapThread.id,+ bootstrapProjectCreated: true,+ bootstrapThreadCreated: true,+ projects: [{ ...bootstrapProject, workspaceRoot: "c:/projects/current" }],+ threads: [bootstrapThread],+ }),+ ).toBe(true);+ });++ it("rejects projects from another environment even when their paths match", () => {+ expect(+ isFreshFirstRunWorkspace({+ primaryEnvironmentId,+ serverCwd: "/projects/current",+ projects: [{ ...bootstrapProject, environmentId: "remote-environment" }],+ threads: [],+ }),+ ).toBe(false);+ });++ it("rejects threads from another environment", () => {+ expect(+ isFreshFirstRunWorkspace({+ primaryEnvironmentId,+ serverCwd: "/projects/current",+ projects: [bootstrapProject],+ threads: [{ ...bootstrapThread, environmentId: "remote-environment" }],+ }),+ ).toBe(false);+ });++ it("rejects a thread that does not belong to the bootstrap project", () => {+ expect(+ isFreshFirstRunWorkspace({+ primaryEnvironmentId,+ serverCwd: "/projects/current",+ projects: [bootstrapProject],+ threads: [{ ...bootstrapThread, projectId: "another-project" }],+ }),+ ).toBe(false);+ });++ it("rejects a thread when there is no bootstrap project", () => {+ expect(+ isFreshFirstRunWorkspace({+ primaryEnvironmentId,+ serverCwd: "/projects/current",+ projects: [],+ threads: [bootstrapThread],+ }),+ ).toBe(false);+ });++ it("rejects a bootstrap thread that already has a user message", () => {+ expect(+ isFreshFirstRunWorkspace({+ primaryEnvironmentId,+ serverCwd: "/projects/current",+ projects: [bootstrapProject],+ threads: [+ {+ ...bootstrapThread,+ latestUserMessageAt: "2026-08-23T12:00:00.000Z",+ },+ ],+ }),+ ).toBe(false);+ });++ it("rejects a bootstrap thread that has started a turn", () => {+ expect(+ isFreshFirstRunWorkspace({+ primaryEnvironmentId,+ serverCwd: "/projects/current",+ projects: [bootstrapProject],+ threads: [{ ...bootstrapThread, latestTurn: { id: "first-turn" } }],+ }),+ ).toBe(false);+ });++ it("rejects a bootstrap thread that has a provider session", () => {+ expect(+ isFreshFirstRunWorkspace({+ primaryEnvironmentId,+ serverCwd: "/projects/current",+ projects: [bootstrapProject],+ threads: [{ ...bootstrapThread, session: { status: "ready" } }],+ }),+ ).toBe(false);+ });+});diff --git a/apps/web/src/onboarding/firstRun.logic.ts b/apps/web/src/onboarding/firstRun.logic.tsnew file mode 100644index 00000000000..013dbb02d52--- /dev/null+++ b/apps/web/src/onboarding/firstRun.logic.ts@@ -0,0 +1,184 @@+import { normalizeProjectPathForComparison } from "@t3tools/shared/path";++export type FirstRunDecision = "pending" | "app" | "wizard";++export interface FirstRunGateState {+ readonly decision: FirstRunDecision;+ readonly stalled: boolean;+}++type FirstRunGateEvent =+ | { readonly type: "evidence"; readonly decision: FirstRunDecision }+ | { readonly type: "timeout" };++interface FirstRunWorkspaceInput {+ readonly primaryEnvironmentId: string | null;+ readonly serverCwd: string | null;+ readonly bootstrapProjectId?: string | undefined;+ readonly bootstrapThreadId?: string | undefined;+ readonly bootstrapProjectCreated?: boolean | undefined;+ readonly bootstrapThreadCreated?: boolean | undefined;+ readonly projects: ReadonlyArray<{+ readonly id: string;+ readonly environmentId: string;+ readonly workspaceRoot: string;+ }>;+ readonly threads: ReadonlyArray<{+ readonly id: string;+ readonly projectId: string;+ readonly environmentId: string;+ readonly latestTurn: unknown;+ readonly latestUserMessageAt: string | null;+ readonly session: unknown;+ }>;+}++interface FirstRunDecisionInput {+ readonly enabled: boolean;+ readonly hydrated: boolean;+ readonly completed: boolean;+ readonly bootstrapped: boolean;+ readonly authoritative: boolean;+ readonly workspaceAuthoritative: boolean;+ readonly workspaceProvenanceAuthoritative: boolean;+ readonly catalogReady: boolean;+ readonly serverConfigAvailable: boolean;+ readonly workspaceFresh: boolean;+ readonly projectCount: number;+ readonly threadCount: number;+}++interface HostedFirstRunDecisionInput {+ readonly hydrated: boolean;+ readonly completed: boolean;+ readonly catalogReady: boolean;+ readonly environmentCount: number;+}++export function isFirstRunWorkspaceProvenanceAuthoritative(input: {+ readonly welcomeReceived: boolean;+ readonly bootstrapStatus: "pending" | "complete" | null;+}): boolean {+ // An empty catalog is not final while cwd auto-bootstrap is pending. Older+ // servers omit bootstrapStatus, so a received welcome with null stays valid.+ return input.welcomeReceived && input.bootstrapStatus !== "pending";+}++/** Keeps the authenticated app unmounted until workspace evidence settles. */+export function transitionFirstRunGateState(+ state: FirstRunGateState,+ event: FirstRunGateEvent,+): FirstRunGateState {+ if (event.type === "timeout") {+ return state.decision === "pending" && !state.stalled ? { ...state, stalled: true } : state;+ }++ if (+ state.decision === "wizard" ||+ event.decision === "pending" ||+ (state.decision === "app" && event.decision !== "wizard")+ ) {+ return state;+ }++ return { decision: event.decision, stalled: false };+}++/** Only a project and thread created by this startup count as a fresh nonempty workspace. */+export function isFreshFirstRunWorkspace(input: FirstRunWorkspaceInput): boolean {+ if (input.projects.length > 1 || input.threads.length > 1) {+ return false;+ }++ const bootstrapProject = input.projects[0];+ if (bootstrapProject !== undefined) {+ if (+ input.bootstrapProjectCreated !== true ||+ input.bootstrapProjectId !== bootstrapProject.id ||+ input.serverCwd === null ||+ bootstrapProject.environmentId !== input.primaryEnvironmentId ||+ normalizeProjectPathForComparison(bootstrapProject.workspaceRoot) !==+ normalizeProjectPathForComparison(input.serverCwd)+ ) {+ return false;+ }+ }++ const bootstrapThread = input.threads[0];+ if (bootstrapThread === undefined) {+ return true;+ }++ return (+ bootstrapProject !== undefined &&+ input.bootstrapThreadCreated === true &&+ input.bootstrapThreadId === bootstrapThread.id &&+ bootstrapThread.environmentId === input.primaryEnvironmentId &&+ bootstrapThread.projectId === bootstrapProject.id &&+ bootstrapThread.latestTurn === null &&+ bootstrapThread.latestUserMessageAt === null &&+ bootstrapThread.session === null+ );+}++/** Cached projects may open the app, but only live workspace data may complete onboarding. */+export function resolveFirstRunDecision(input: FirstRunDecisionInput): {+ readonly decision: FirstRunDecision;+ readonly persistCompletion: boolean;+} {+ if (!input.enabled || (input.hydrated && input.completed)) {+ return { decision: "app", persistCompletion: false };+ }++ if (!input.hydrated) {+ return { decision: "pending", persistCompletion: false };+ }++ if (input.projectCount > 1 || input.threadCount > 1) {+ return {+ decision: "app",+ persistCompletion:+ input.bootstrapped &&+ input.authoritative &&+ input.workspaceAuthoritative &&+ input.catalogReady &&+ input.serverConfigAvailable,+ };+ }++ if (+ !input.bootstrapped ||+ !input.authoritative ||+ !input.workspaceProvenanceAuthoritative ||+ !input.catalogReady ||+ !input.serverConfigAvailable+ ) {+ return { decision: "pending", persistCompletion: false };+ }++ return input.workspaceFresh+ ? { decision: "wizard", persistCompletion: false }+ : { decision: "app", persistCompletion: input.workspaceAuthoritative };+}++/** Hosted onboarding depends on saved environments because there is no primary server. */+export function resolveHostedFirstRunDecision(input: HostedFirstRunDecisionInput): {+ readonly decision: FirstRunDecision;+ readonly persistCompletion: boolean;+} {+ if (!input.hydrated) {+ return { decision: "pending", persistCompletion: false };+ }++ if (input.completed) {+ return { decision: "app", persistCompletion: false };+ }++ if (!input.catalogReady) {+ return { decision: "pending", persistCompletion: false };+ }++ return input.environmentCount === 0+ ? { decision: "wizard", persistCompletion: false }+ : { decision: "app", persistCompletion: true };+}diff --git a/apps/web/src/onboarding/firstRun.ts b/apps/web/src/onboarding/firstRun.tsnew file mode 100644index 00000000000..4daafc7cc92--- /dev/null+++ b/apps/web/src/onboarding/firstRun.ts@@ -0,0 +1,16 @@+import { useCallback } from "react";++import { ensureClientSettingsHydrated, persistClientSettingsUpdate } from "../hooks/useSettings";++/**+ * Marks first-run onboarding finished (or skipped) so FirstRunGate never+ * routes to the welcome wizard again. The gate itself lives in+ * `components/onboarding/FirstRunGate.tsx`.+ */+export function useCompleteOnboarding(): () => Promise<void> {+ return useCallback(async () => {+ await ensureClientSettingsHydrated();+ const onboardingCompletedAt = new Date().toISOString();+ await persistClientSettingsUpdate((current) => ({ ...current, onboardingCompletedAt }));+ }, []);+}diff --git a/apps/web/src/onboarding/projectImport.logic.test.ts b/apps/web/src/onboarding/projectImport.logic.test.tsnew file mode 100644index 00000000000..07028abd28b--- /dev/null+++ b/apps/web/src/onboarding/projectImport.logic.test.ts@@ -0,0 +1,245 @@+import { EnvironmentId, ProjectId, type AgentSessionProjectCandidate } from "@t3tools/contracts";+import { describe, expect, it } from "vite-plus/test";++import {+ partitionOnboardingProjects,+ resolveOnboardingLandingProject,+ resolveOnboardingProjectId,+} from "./projectImport.logic";++const now = Date.parse("2026-08-22T12:00:00.000Z");++function candidate(+ path: string,+ overrides: Partial<AgentSessionProjectCandidate> = {},+): AgentSessionProjectCandidate {+ return {+ title: path.split("/").at(-1) ?? path,+ path,+ sources: ["codex"],+ threadCount: 1,+ lastActiveAt: "2026-08-20T12:00:00.000Z",+ alreadyImported: false,+ ...overrides,+ };+}++describe("partitionOnboardingProjects", () => {+ it("keeps existing projects available for thread history import", () => {+ const imported = candidate("/projects/current", { alreadyImported: true });+ const available = candidate("/projects/other");++ expect(partitionOnboardingProjects([imported, available], now)).toEqual({+ available: [imported, available],+ recent: [imported, available],+ });+ });++ it("keeps projects older than 30 days out of the default selection", () => {+ const recent = candidate("/projects/recent");+ const older = candidate("/projects/older", {+ lastActiveAt: "2026-07-01T12:00:00.000Z",+ });++ expect(partitionOnboardingProjects([recent, older], now)).toEqual({+ available: [recent, older],+ recent: [recent],+ });+ });++ it("keeps future activity out of the default selection", () => {+ const recent = candidate("/projects/recent");+ const future = candidate("/projects/future", {+ lastActiveAt: "2026-08-23T12:00:00.000Z",+ });++ expect(partitionOnboardingProjects([recent, future], now)).toEqual({+ available: [recent, future],+ recent: [recent],+ });+ });+});++describe("resolveOnboardingProjectId", () => {+ const localEnvironmentId = EnvironmentId.make("local");+ const remoteEnvironmentId = EnvironmentId.make("remote");+ const localProjectId = ProjectId.make("local-project");++ it("uses the scanned project ID before the project reaches the client", () => {+ expect(+ resolveOnboardingProjectId(+ [],+ localEnvironmentId,+ candidate("/projects/repo", { projectId: localProjectId }),+ ),+ ).toBe(localProjectId);+ });++ it("uses the scanned project ID when the client still has an older project at that root", () => {+ expect(+ resolveOnboardingProjectId(+ [+ {+ id: ProjectId.make("stale-project"),+ environmentId: localEnvironmentId,+ workspaceRoot: "/projects/repo",+ },+ ],+ localEnvironmentId,+ candidate("/projects/repo", { projectId: localProjectId }),+ ),+ ).toBe(localProjectId);+ });++ it("returns null to create a project when neither the scan nor the client has a project ID", () => {+ expect(+ resolveOnboardingProjectId([], localEnvironmentId, candidate("/projects/new")),+ ).toBeNull();+ });++ it("finds an existing project by normalized root in the target environment", () => {+ expect(+ resolveOnboardingProjectId(+ [+ {+ id: ProjectId.make("remote-project"),+ environmentId: remoteEnvironmentId,+ workspaceRoot: "C:\\Work\\Repo",+ },+ {+ id: localProjectId,+ environmentId: localEnvironmentId,+ workspaceRoot: "C:\\Work\\Repo\\",+ },+ ],+ localEnvironmentId,+ candidate("c:/work/repo"),+ ),+ ).toBe(localProjectId);+ });++ it("does not reuse a project from another environment", () => {+ expect(+ resolveOnboardingProjectId(+ [+ {+ id: ProjectId.make("remote-project"),+ environmentId: remoteEnvironmentId,+ workspaceRoot: "/projects/repo",+ },+ ],+ localEnvironmentId,+ candidate("/projects/repo"),+ ),+ ).toBeNull();+ });++ it("finds an alias after the scanner returns its persisted project root", () => {+ expect(+ resolveOnboardingProjectId(+ [+ {+ id: localProjectId,+ environmentId: localEnvironmentId,+ workspaceRoot: "/real/projects/repo",+ },+ ],+ localEnvironmentId,+ candidate("/real/projects/repo"),+ ),+ ).toBe(localProjectId);+ });++ it("finds the current root owner when the scan has no project ID", () => {+ const recreatedProjectId = ProjectId.make("recreated-project");+ expect(+ resolveOnboardingProjectId(+ [+ {+ id: localProjectId,+ environmentId: localEnvironmentId,+ workspaceRoot: "/projects/other",+ },+ {+ id: recreatedProjectId,+ environmentId: localEnvironmentId,+ workspaceRoot: "/projects/repo",+ },+ ],+ localEnvironmentId,+ candidate("/projects/repo"),+ ),+ ).toBe(recreatedProjectId);+ });++ it("does not reuse a moved project when the scan has no project ID", () => {+ expect(+ resolveOnboardingProjectId(+ [+ {+ id: localProjectId,+ environmentId: localEnvironmentId,+ workspaceRoot: "/projects/moved",+ },+ ],+ localEnvironmentId,+ candidate("/projects/repo"),+ ),+ ).toBeNull();+ });+});++describe("resolveOnboardingLandingProject", () => {+ it("skips a failed first project for a later project with imported history", () => {+ expect(+ resolveOnboardingLandingProject(+ ["/projects/failed", "/projects/imported"],+ new Map([["/projects/imported", "imported"]]),+ new Map([["/projects/imported", "imported"]]),+ ),+ ).toBe("imported");+ });++ it("prefers a partial first import that added history", () => {+ expect(+ resolveOnboardingLandingProject(+ ["/projects/partial", "/projects/complete"],+ new Map([["/projects/partial", "partial"]]),+ new Map([["/projects/complete", "complete"]]),+ ),+ ).toBe("partial");+ });++ it("uses a completed zero-history project when no import added history", () => {+ expect(+ resolveOnboardingLandingProject(+ ["/projects/empty", "/projects/failed"],+ new Map(),+ new Map([["/projects/empty", "empty"]]),+ ),+ ).toBe("empty");+ });++ it("keeps an earlier successful import available on retry", () => {+ expect(+ resolveOnboardingLandingProject(+ ["/projects/imported", "/projects/retry"],+ new Map([["/projects/imported", "imported"]]),+ new Map([["/projects/imported", "imported"]]),+ ),+ ).toBe("imported");+ });++ it("ignores cached successes outside the current retry selection", () => {+ expect(+ resolveOnboardingLandingProject(+ ["/projects/current"],+ new Map([["/projects/previous", "previous"]]),+ new Map([+ ["/projects/previous", "previous"],+ ["/projects/current", "current"],+ ]),+ ),+ ).toBe("current");+ });+});diff --git a/apps/web/src/onboarding/projectImport.logic.ts b/apps/web/src/onboarding/projectImport.logic.tsnew file mode 100644index 00000000000..d723b911b66--- /dev/null+++ b/apps/web/src/onboarding/projectImport.logic.ts@@ -0,0 +1,55 @@+import { findProjectByPath } from "@t3tools/client-runtime/state/projects";+import type { AgentSessionProjectCandidate, EnvironmentId, ProjectId } from "@t3tools/contracts";++const RECENT_PROJECT_WINDOW_MS = 30 * 24 * 60 * 60 * 1000;++/** Existing projects still need their agent history imported, so every scan candidate is offered. */+export function partitionOnboardingProjects(+ candidates: ReadonlyArray<AgentSessionProjectCandidate>,+ now = Date.now(),+) {+ const cutoff = now - RECENT_PROJECT_WINDOW_MS;++ return {+ available: candidates,+ recent: candidates.filter((candidate) => {+ if (candidate.lastActiveAt === null) return false;+ const lastActiveAt = Date.parse(candidate.lastActiveAt);+ return lastActiveAt >= cutoff && lastActiveAt <= now;+ }),+ };+}++/** Use the server's project match before the client snapshot, which can lag behind the scan. */+export function resolveOnboardingProjectId(+ projects: ReadonlyArray<{+ readonly id: ProjectId;+ readonly environmentId: EnvironmentId;+ readonly workspaceRoot: string;+ }>,+ environmentId: EnvironmentId,+ candidate: Pick<AgentSessionProjectCandidate, "path" | "projectId">,+): ProjectId | null {+ if (candidate.projectId !== undefined) return candidate.projectId;+ const environmentProjects = projects.filter((project) => project.environmentId === environmentId);+ const currentRootMatch = findProjectByPath(environmentProjects, candidate.path);+ if (currentRootMatch !== undefined) return currentRootMatch.id;+ return null;+}++/** Prefer a selected project with imported history, then a completed empty import. */+export function resolveOnboardingLandingProject<T>(+ selection: ReadonlyArray<string>,+ projectsWithImportedHistory: ReadonlyMap<string, T>,+ completedProjects: ReadonlyMap<string, T>,+): T | undefined {+ for (const path of selection) {+ const project = projectsWithImportedHistory.get(path);+ if (project !== undefined) return project;+ }+ for (const path of selection) {+ const project = completedProjects.get(path);+ if (project !== undefined) return project;+ }+ return undefined;+}diff --git a/apps/web/src/onboarding/providerReadiness.logic.test.ts b/apps/web/src/onboarding/providerReadiness.logic.test.tsnew file mode 100644index 00000000000..ab742ac51b4--- /dev/null+++ b/apps/web/src/onboarding/providerReadiness.logic.test.ts@@ -0,0 +1,317 @@+import {+ DEFAULT_SERVER_SETTINGS,+ ProviderDriverKind,+ ProviderInstanceId,+ type ServerProvider,+} from "@t3tools/contracts";+import { describe, expect, it } from "vite-plus/test";++import {+ getOnboardingProviderState,+ resolveOnboardingProviderLoginCommand,+ selectOnboardingProvidersByDriver,+} from "./providerReadiness.logic";++const readyCodex: ServerProvider = {+ instanceId: ProviderInstanceId.make("codex"),+ driver: ProviderDriverKind.make("codex"),+ enabled: true,+ installed: true,+ version: "1.0.0",+ status: "ready",+ auth: { status: "unknown" },+ checkedAt: "2026-08-23T00:00:00.000Z",+ models: [],+ slashCommands: [],+ skills: [],+};++describe("getOnboardingProviderState", () => {+ it("treats an enabled Codex provider with ready status and unknown authentication as ready", () => {+ expect(getOnboardingProviderState(readyCodex)).toBe("ready");+ });++ it("treats authenticated providers as ready only when their provider status is ready", () => {+ expect(getOnboardingProviderState({ ...readyCodex, auth: { status: "authenticated" } })).toBe(+ "ready",+ );+ expect(+ getOnboardingProviderState({+ ...readyCodex,+ auth: { status: "authenticated" },+ status: "error",+ }),+ ).toBe("attention");+ expect(+ getOnboardingProviderState({+ ...readyCodex,+ auth: { status: "authenticated" },+ status: "warning",+ }),+ ).toBe("attention");+ });++ it("offers sign-in only when the server reports an authentication failure", () => {+ expect(+ getOnboardingProviderState({+ ...readyCodex,+ status: "error",+ auth: { status: "unauthenticated" },+ }),+ ).toBe("signIn");+ expect(getOnboardingProviderState({ ...readyCodex, status: "error" })).toBe("attention");+ expect(getOnboardingProviderState({ ...readyCodex, status: "warning" })).toBe("attention");+ });++ it("does not offer installation or sign-in for disabled providers", () => {+ expect(getOnboardingProviderState({ ...readyCodex, enabled: false, installed: false })).toBe(+ "disabled",+ );+ expect(getOnboardingProviderState({ ...readyCodex, status: "disabled" })).toBe("disabled");+ });++ it("offers installation only when an enabled provider is missing", () => {+ expect(getOnboardingProviderState({ ...readyCodex, installed: false, status: "error" })).toBe(+ "install",+ );+ });++ it("waits for a provider snapshot before offering an action", () => {+ expect(getOnboardingProviderState(undefined)).toBe("checking");+ });+});++describe("selectOnboardingProvidersByDriver", () => {+ it("prefers a ready instance with unknown authentication to an unauthenticated instance", () => {+ const signedOutCodex: ServerProvider = {+ ...readyCodex,+ instanceId: ProviderInstanceId.make("codex_work"),+ status: "error",+ auth: { status: "unauthenticated" },+ };++ expect(selectOnboardingProvidersByDriver([signedOutCodex, readyCodex]).get("codex")).toBe(+ readyCodex,+ );+ });++ it("prefers a provider with an actionable sign-in over a failed provider", () => {+ const failedCodex: ServerProvider = { ...readyCodex, status: "error" };+ const signedOutCodex: ServerProvider = {+ ...readyCodex,+ instanceId: ProviderInstanceId.make("codex_work"),+ status: "error",+ auth: { status: "unauthenticated" },+ };++ expect(selectOnboardingProvidersByDriver([failedCodex, signedOutCodex]).get("codex")).toBe(+ signedOutCodex,+ );+ });++ it("prefers installed providers over missing or disabled instances", () => {+ const disabledCodex: ServerProvider = { ...readyCodex, enabled: false };+ const missingCodex: ServerProvider = {+ ...readyCodex,+ instanceId: ProviderInstanceId.make("codex_work"),+ installed: false,+ status: "error",+ };++ expect(+ selectOnboardingProvidersByDriver([disabledCodex, missingCodex, readyCodex]).get("codex"),+ ).toBe(readyCodex);+ });++ it("handles provider snapshots that have not arrived", () => {+ expect(selectOnboardingProvidersByDriver(undefined).size).toBe(0);+ });++ it("keeps a ready custom account when the default account is signed out", () => {+ const signedOutDefault: ServerProvider = {+ ...readyCodex,+ status: "error",+ auth: { status: "unauthenticated" },+ };+ const readyCustom: ServerProvider = {+ ...readyCodex,+ instanceId: ProviderInstanceId.make("codex_work"),+ };++ expect(selectOnboardingProvidersByDriver([signedOutDefault, readyCustom]).get("codex")).toBe(+ readyCustom,+ );+ });+});++describe("resolveOnboardingProviderLoginCommand", () => {+ it("uses the selected Codex account binary", () => {+ const provider = { ...readyCodex, instanceId: ProviderInstanceId.make("codex_work") };++ expect(+ resolveOnboardingProviderLoginCommand(+ provider,+ {+ ...DEFAULT_SERVER_SETTINGS,+ providerInstances: {+ [provider.instanceId]: {+ driver: provider.driver,+ config: { binaryPath: "/opt/codex-work/bin/codex" },+ },+ },+ },+ "linux",+ ),+ ).toBe("/opt/codex-work/bin/codex login");+ });++ it("uses the selected Claude account binary", () => {+ const provider: ServerProvider = {+ ...readyCodex,+ driver: ProviderDriverKind.make("claudeAgent"),+ instanceId: ProviderInstanceId.make("claude_work"),+ };++ expect(+ resolveOnboardingProviderLoginCommand(+ provider,+ {+ ...DEFAULT_SERVER_SETTINGS,+ providerInstances: {+ [provider.instanceId]: {+ driver: provider.driver,+ config: { binaryPath: "/opt/claude-work/bin/claude" },+ },+ },+ },+ "linux",+ ),+ ).toBe("/opt/claude-work/bin/claude auth login");+ });++ it("quotes a Codex path with spaces for PowerShell", () => {+ expect(+ resolveOnboardingProviderLoginCommand(+ readyCodex,+ {+ ...DEFAULT_SERVER_SETTINGS,+ providers: {+ ...DEFAULT_SERVER_SETTINGS.providers,+ codex: {+ ...DEFAULT_SERVER_SETTINGS.providers.codex,+ binaryPath: "C:\\Program Files\\Codex & Tools\\codex.exe",+ },+ },+ },+ "windows",+ ),+ ).toBe("& 'C:\\Program Files\\Codex & Tools\\codex.exe' login");+ });++ it("quotes a Claude path with shell metacharacters on POSIX", () => {+ const provider: ServerProvider = {+ ...readyCodex,+ driver: ProviderDriverKind.make("claudeAgent"),+ instanceId: ProviderInstanceId.make("claude"),+ };++ expect(+ resolveOnboardingProviderLoginCommand(+ provider,+ {+ ...DEFAULT_SERVER_SETTINGS,+ providers: {+ ...DEFAULT_SERVER_SETTINGS.providers,+ claudeAgent: {+ ...DEFAULT_SERVER_SETTINGS.providers.claudeAgent,+ binaryPath: "/opt/Claude Tools/$current/claude",+ },+ },+ },+ "linux",+ ),+ ).toBe("'/opt/Claude Tools/$current/claude' auth login");+ });++ it.each([+ ["~/my tools/codex", "~/'my tools/codex' login"],+ ["~\\my tools/codex", "~/'my tools/codex' login"],+ ["~/tools/codex's build", `~/'tools/codex'"'"'s build' login`],+ ["~\\tools\\codex's build", `~/'tools\\codex'"'"'s build' login`],+ ["~/tools/codex; echo unsafe", "~/'tools/codex; echo unsafe' login"],+ ])("keeps the home prefix expandable while quoting %s", (binaryPath, expectedCommand) => {+ expect(+ resolveOnboardingProviderLoginCommand(+ readyCodex,+ {+ ...DEFAULT_SERVER_SETTINGS,+ providers: {+ ...DEFAULT_SERVER_SETTINGS.providers,+ codex: {+ ...DEFAULT_SERVER_SETTINGS.providers.codex,+ binaryPath,+ },+ },+ },+ "linux",+ ),+ ).toBe(expectedCommand);+ });++ it.each(["darwin", "linux"] as const)("quotes backslashes in a Codex path on %s", (platform) => {+ expect(+ resolveOnboardingProviderLoginCommand(+ readyCodex,+ {+ ...DEFAULT_SERVER_SETTINGS,+ providers: {+ ...DEFAULT_SERVER_SETTINGS.providers,+ codex: {+ ...DEFAULT_SERVER_SETTINGS.providers.codex,+ binaryPath: "/opt/codex\\work/codex",+ },+ },+ },+ platform,+ ),+ ).toBe("'/opt/codex\\work/codex' login");+ });++ it("keeps a plain Windows path unquoted", () => {+ expect(+ resolveOnboardingProviderLoginCommand(+ readyCodex,+ {+ ...DEFAULT_SERVER_SETTINGS,+ providers: {+ ...DEFAULT_SERVER_SETTINGS.providers,+ codex: {+ ...DEFAULT_SERVER_SETTINGS.providers.codex,+ binaryPath: "C:\\Tools\\codex.exe",+ },+ },+ },+ "windows",+ ),+ ).toBe("C:\\Tools\\codex.exe login");+ });++ it("uses the default command when an old server reports an unknown shell", () => {+ expect(+ resolveOnboardingProviderLoginCommand(+ readyCodex,+ {+ ...DEFAULT_SERVER_SETTINGS,+ providers: {+ ...DEFAULT_SERVER_SETTINGS.providers,+ codex: {+ ...DEFAULT_SERVER_SETTINGS.providers.codex,+ binaryPath: "/opt/Codex Tools/codex",+ },+ },+ },+ "unknown",+ ),+ ).toBe("codex login");+ });+});diff --git a/apps/web/src/onboarding/providerReadiness.logic.ts b/apps/web/src/onboarding/providerReadiness.logic.tsnew file mode 100644index 00000000000..939b4c64cd6--- /dev/null+++ b/apps/web/src/onboarding/providerReadiness.logic.ts@@ -0,0 +1,99 @@+import {+ ClaudeSettings,+ CodexSettings,+ type ExecutionEnvironmentPlatformOs,+ type ServerProvider,+ type ServerSettings,+} from "@t3tools/contracts";+import * as Option from "effect/Option";+import * as Schema from "effect/Schema";++const decodeClaudeSettings = Schema.decodeUnknownOption(ClaudeSettings);+const decodeCodexSettings = Schema.decodeUnknownOption(CodexSettings);+const SAFE_SHELL_BINARY_PATTERN = /^[A-Za-z0-9_./:\\-]+$/;++function quoteProviderBinary(+ binaryPath: string,+ fallback: string,+ platform: ExecutionEnvironmentPlatformOs,+): string {+ if (+ SAFE_SHELL_BINARY_PATTERN.test(binaryPath) &&+ (platform === "windows" || !binaryPath.includes("\\"))+ ) {+ return binaryPath;+ }+ if (platform === "windows") return `& '${binaryPath.replaceAll("'", "''")}'`;+ if (platform === "darwin" || platform === "linux") {+ if (binaryPath.startsWith("~/") || binaryPath.startsWith("~\\")) {+ return `~/'${binaryPath.slice(2).replaceAll("'", `'"'"'`)}'`;+ }+ return `'${binaryPath.replaceAll("'", `'"'"'`)}'`;+ }+ return fallback;+}++export function getOnboardingProviderState(provider: ServerProvider | undefined) {+ if (provider === undefined) return "checking";+ if (!provider.enabled || provider.status === "disabled") return "disabled";+ if (!provider.installed) return "install";+ if (provider.auth.status === "unauthenticated") return "signIn";+ if (provider.status === "ready") return "ready";+ return "attention";+}++const PROVIDER_STATE_PRIORITY = {+ checking: 0,+ disabled: 1,+ install: 2,+ attention: 3,+ signIn: 4,+ ready: 5,+} as const;++/** Select the most usable configured instance for each provider driver. */+export function selectOnboardingProvidersByDriver(+ providers: ReadonlyArray<ServerProvider> | null | undefined,+) {+ const providersByDriver = new Map<string, ServerProvider>();++ for (const provider of providers ?? []) {+ const existing = providersByDriver.get(provider.driver);+ if (+ existing === undefined ||+ PROVIDER_STATE_PRIORITY[getOnboardingProviderState(provider)] >+ PROVIDER_STATE_PRIORITY[getOnboardingProviderState(existing)]+ ) {+ providersByDriver.set(provider.driver, provider);+ }+ }++ return providersByDriver;+}++/** Use the selected provider instance's binary when the setup terminal opens its login flow. */+export function resolveOnboardingProviderLoginCommand(+ provider: ServerProvider,+ settings: ServerSettings,+ platform: ExecutionEnvironmentPlatformOs,+): string {+ const instance = settings.providerInstances[provider.instanceId];++ if (provider.driver === "claudeAgent") {+ const config = decodeClaudeSettings(+ instance ? (instance.config ?? {}) : settings.providers.claudeAgent,+ );+ const binaryPath = Option.isSome(config) ? config.value.binaryPath : "claude";+ return `${quoteProviderBinary(binaryPath, "claude", platform)} auth login`;+ }++ if (provider.driver === "codex") {+ const config = decodeCodexSettings(+ instance ? (instance.config ?? {}) : settings.providers.codex,+ );+ const binaryPath = Option.isSome(config) ? config.value.binaryPath : "codex";+ return `${quoteProviderBinary(binaryPath, "codex", platform)} login`;+ }++ return provider.driver;+}diff --git a/apps/web/src/onboarding/targetEnvironment.logic.test.ts b/apps/web/src/onboarding/targetEnvironment.logic.test.tsnew file mode 100644index 00000000000..9928f83b27d--- /dev/null+++ b/apps/web/src/onboarding/targetEnvironment.logic.test.ts@@ -0,0 +1,211 @@+import {+ BearerConnectionTarget,+ PrimaryConnectionTarget,+ RelayConnectionTarget,+ SshConnectionTarget,+} from "@t3tools/client-runtime/connection";+import { EnvironmentId } from "@t3tools/contracts";+import { describe, expect, it } from "vite-plus/test";++import {+ isOnboardingRelayEnvironment,+ resolveOnboardingTargetEnvironment,+} from "./targetEnvironment.logic";++const primaryEnvironment = {+ environmentId: EnvironmentId.make("primary"),+ connection: { phase: "connected" },+ entry: {+ target: new PrimaryConnectionTarget({+ environmentId: EnvironmentId.make("primary"),+ label: "This computer",+ httpBaseUrl: "http://127.0.0.1:3773",+ wsBaseUrl: "ws://127.0.0.1:3773",+ }),+ },+ label: "This computer",+} as const;++const olderRelay = {+ environmentId: EnvironmentId.make("older-remote"),+ connection: { phase: "connected" },+ entry: {+ target: new RelayConnectionTarget({+ environmentId: EnvironmentId.make("older-remote"),+ label: "Older computer",+ }),+ },+ label: "Older computer",+} as const;++const newerRelay = {+ environmentId: EnvironmentId.make("newer-relay"),+ connection: { phase: "connected" },+ entry: {+ target: new RelayConnectionTarget({+ environmentId: EnvironmentId.make("newer-relay"),+ label: "New computer",+ }),+ },+ label: "New computer",+} as const;++const pairedRemote = {+ environmentId: EnvironmentId.make("paired-remote"),+ connection: { phase: "connected" },+ entry: {+ target: new BearerConnectionTarget({+ environmentId: EnvironmentId.make("paired-remote"),+ label: "Direct computer",+ connectionId: "paired-remote",+ }),+ },+ label: "Direct computer",+} as const;++const sshEnvironment = {+ environmentId: EnvironmentId.make("ssh-remote"),+ connection: { phase: "connected" },+ entry: {+ target: new SshConnectionTarget({+ environmentId: EnvironmentId.make("ssh-remote"),+ label: "SSH computer",+ connectionId: "ssh-remote",+ }),+ },+ label: "SSH computer",+} as const;++const desktopLocalEnvironment = {+ environmentId: EnvironmentId.make("desktop-local-wsl"),+ connection: { phase: "connected" },+ entry: {+ target: new BearerConnectionTarget({+ environmentId: EnvironmentId.make("desktop-local-wsl"),+ label: "WSL",+ connectionId: "local:wsl:Ubuntu",+ }),+ },+ label: "WSL",+} as const;++describe("resolveOnboardingTargetEnvironment", () => {+ it("waits for the exact paired machine instead of using an older connected machine", () => {+ const pendingPairedRemote = { ...pairedRemote, connection: { phase: "connecting" } };++ expect(+ resolveOnboardingTargetEnvironment({+ mode: "direct",+ environments: [primaryEnvironment, olderRelay, pendingPairedRemote],+ primaryEnvironment,+ pairedEnvironmentId: pairedRemote.environmentId,+ }),+ ).toBeNull();+ });++ it("uses the exact paired machine once it connects", () => {+ expect(+ resolveOnboardingTargetEnvironment({+ mode: "direct",+ environments: [primaryEnvironment, olderRelay, pairedRemote],+ primaryEnvironment,+ pairedEnvironmentId: pairedRemote.environmentId,+ }),+ ).toBe(pairedRemote);+ });++ it("waits for a newly paired machine that has not appeared in the catalog", () => {+ expect(+ resolveOnboardingTargetEnvironment({+ mode: "direct",+ environments: [primaryEnvironment, olderRelay],+ primaryEnvironment,+ pairedEnvironmentId: pairedRemote.environmentId,+ }),+ ).toBeNull();+ });++ it("uses the primary machine for local onboarding", () => {+ expect(+ resolveOnboardingTargetEnvironment({+ mode: "local",+ environments: [primaryEnvironment, olderRelay],+ primaryEnvironment,+ pairedEnvironmentId: null,+ }),+ ).toBe(primaryEnvironment);+ });++ it("does not substitute a remote machine when the local primary is offline", () => {+ const offlinePrimary = { ...primaryEnvironment, connection: { phase: "disconnected" } };++ expect(+ resolveOnboardingTargetEnvironment({+ mode: "local",+ environments: [offlinePrimary, olderRelay],+ primaryEnvironment: offlinePrimary,+ pairedEnvironmentId: null,+ }),+ ).toBeNull();+ });++ it("uses the newest connected remote when no exact machine was selected", () => {+ expect(+ resolveOnboardingTargetEnvironment({+ mode: "connect",+ environments: [primaryEnvironment, olderRelay, newerRelay],+ primaryEnvironment,+ pairedEnvironmentId: null,+ }),+ ).toBe(newerRelay);+ });++ it("ignores direct, SSH, and desktop-managed connections in Connect mode", () => {+ expect(+ resolveOnboardingTargetEnvironment({+ mode: "connect",+ environments: [+ primaryEnvironment,+ olderRelay,+ pairedRemote,+ sshEnvironment,+ desktopLocalEnvironment,+ ],+ primaryEnvironment,+ pairedEnvironmentId: null,+ }),+ ).toBe(olderRelay);+ });++ it("uses the primary computer when no relay connection exists", () => {+ expect(+ resolveOnboardingTargetEnvironment({+ mode: "connect",+ environments: [primaryEnvironment, pairedRemote, sshEnvironment, desktopLocalEnvironment],+ primaryEnvironment,+ pairedEnvironmentId: null,+ }),+ ).toBe(primaryEnvironment);+ });++ it("falls back to the connected primary when no remote is available", () => {+ expect(+ resolveOnboardingTargetEnvironment({+ mode: "connect",+ environments: [primaryEnvironment],+ primaryEnvironment,+ pairedEnvironmentId: null,+ }),+ ).toBe(primaryEnvironment);+ });+});++describe("isOnboardingRelayEnvironment", () => {+ it("includes only T3 Connect relay targets", () => {+ expect(+ [olderRelay, pairedRemote, sshEnvironment, desktopLocalEnvironment].filter(+ isOnboardingRelayEnvironment,+ ),+ ).toEqual([olderRelay]);+ });+});diff --git a/apps/web/src/onboarding/targetEnvironment.logic.ts b/apps/web/src/onboarding/targetEnvironment.logic.tsnew file mode 100644index 00000000000..6045b8c441e--- /dev/null+++ b/apps/web/src/onboarding/targetEnvironment.logic.ts@@ -0,0 +1,49 @@+import type { ConnectionTarget } from "@t3tools/client-runtime/connection";+import type { EnvironmentId } from "@t3tools/contracts";++interface OnboardingEnvironment {+ readonly environmentId: EnvironmentId;+ readonly connection: { readonly phase: string };+ readonly entry: { readonly target: ConnectionTarget };+}++export function isOnboardingRelayEnvironment(+ environment: Pick<OnboardingEnvironment, "entry">,+): boolean {+ return environment.entry.target._tag === "RelayConnectionTarget";+}++/** Keep a directly paired machine pinned while its initial connection completes. */+export function resolveOnboardingTargetEnvironment<TEnvironment extends OnboardingEnvironment>({+ mode,+ environments,+ primaryEnvironment,+ pairedEnvironmentId,+}: {+ readonly mode: "local" | "connect" | "direct";+ readonly environments: ReadonlyArray<TEnvironment>;+ readonly primaryEnvironment: TEnvironment | null;+ readonly pairedEnvironmentId: EnvironmentId | null;+}): TEnvironment | null {+ if (mode === "direct" && pairedEnvironmentId !== null) {+ const pairedEnvironment = environments.find(+ (environment) => environment.environmentId === pairedEnvironmentId,+ );+ return pairedEnvironment?.connection.phase === "connected" ? pairedEnvironment : null;+ }++ const connectedRelayEnvironments = environments.filter(+ (environment) =>+ environment.connection.phase === "connected" && isOnboardingRelayEnvironment(environment),+ );++ if (mode === "connect" && connectedRelayEnvironments.length > 0) {+ return connectedRelayEnvironments[connectedRelayEnvironments.length - 1] ?? null;+ }++ if (primaryEnvironment?.connection.phase === "connected") {+ return primaryEnvironment;+ }++ return mode === "local" ? null : (connectedRelayEnvironments[0] ?? null);+}diff --git a/apps/web/src/routeTree.gen.ts b/apps/web/src/routeTree.gen.tsindex f7c47ace684..5c796f3ab6c 100644--- a/apps/web/src/routeTree.gen.ts+++ b/apps/web/src/routeTree.gen.ts@@ -9,6 +9,7 @@// Additionally, you should also exclude this file from your linter and/or formatter to prevent it from being checked or modified.import { Route as rootRouteImport } from './routes/__root'+import { Route as WelcomeRouteImport } from './routes/welcome'import { Route as UsageRouteImport } from './routes/usage'import { Route as SettingsRouteImport } from './routes/settings'import { Route as PairRouteImport } from './routes/pair'@@ -30,6 +31,11 @@ import { Route as ChatPullRequestsRouteImport } from './routes/_chat.pull-requesimport { Route as ChatDraftDraftIdRouteImport } from './routes/_chat.draft.$draftId'import { Route as ChatEnvironmentIdThreadIdRouteImport } from './routes/_chat.$environmentId.$threadId'+const WelcomeRoute = WelcomeRouteImport.update({+ id: '/welcome',+ path: '/welcome',+ getParentRoute: () => rootRouteImport,+} as any)const UsageRoute = UsageRouteImport.update({id: '/usage',path: '/usage',@@ -137,6 +143,7 @@ export interface FileRoutesByFullPath {'/pair': typeof PairRoute'/settings': typeof SettingsRouteWithChildren'/usage': typeof UsageRoute+ '/welcome': typeof WelcomeRoute'/pull-requests': typeof ChatPullRequestsRoute'/connect/callback': typeof ConnectCallbackRoute'/projects/$projectKey': typeof ProjectsProjectKeyRoute@@ -157,6 +164,7 @@ export interface FileRoutesByTo {'/pair': typeof PairRoute'/settings': typeof SettingsRouteWithChildren'/usage': typeof UsageRoute+ '/welcome': typeof WelcomeRoute'/pull-requests': typeof ChatPullRequestsRoute'/connect/callback': typeof ConnectCallbackRoute'/projects/$projectKey': typeof ProjectsProjectKeyRoute@@ -180,6 +188,7 @@ export interface FileRoutesById {'/pair': typeof PairRoute'/settings': typeof SettingsRouteWithChildren'/usage': typeof UsageRoute+ '/welcome': typeof WelcomeRoute'/_chat/pull-requests': typeof ChatPullRequestsRoute'/connect_/callback': typeof ConnectCallbackRoute'/projects/$projectKey': typeof ProjectsProjectKeyRoute@@ -204,6 +213,7 @@ export interface FileRouteTypes {| '/pair'| '/settings'| '/usage'+ | '/welcome'| '/pull-requests'| '/connect/callback'| '/projects/$projectKey'@@ -224,6 +234,7 @@ export interface FileRouteTypes {| '/pair'| '/settings'| '/usage'+ | '/welcome'| '/pull-requests'| '/connect/callback'| '/projects/$projectKey'@@ -246,6 +257,7 @@ export interface FileRouteTypes {| '/pair'| '/settings'| '/usage'+ | '/welcome'| '/_chat/pull-requests'| '/connect_/callback'| '/projects/$projectKey'@@ -269,12 +281,20 @@ export interface RootRouteChildren {PairRoute: typeof PairRouteSettingsRoute: typeof SettingsRouteWithChildrenUsageRoute: typeof UsageRoute+ WelcomeRoute: typeof WelcomeRouteConnectCallbackRoute: typeof ConnectCallbackRouteProjectsProjectKeyRoute: typeof ProjectsProjectKeyRoute}declare module '@tanstack/react-router' {interface FileRoutesByPath {+ '/welcome': {+ id: '/welcome'+ path: '/welcome'+ fullPath: '/welcome'+ preLoaderRoute: typeof WelcomeRouteImport+ parentRoute: typeof rootRouteImport+ }'/usage': {id: '/usage'path: '/usage'@@ -468,6 +488,7 @@ const rootRouteChildren: RootRouteChildren = {PairRoute: PairRoute,SettingsRoute: SettingsRouteWithChildren,UsageRoute: UsageRoute,+ WelcomeRoute: WelcomeRoute,ConnectCallbackRoute: ConnectCallbackRoute,ProjectsProjectKeyRoute: ProjectsProjectKeyRoute,}diff --git a/apps/web/src/routes/__root.tsx b/apps/web/src/routes/__root.tsxindex 64e858d186e..12cdd946f6c 100644--- a/apps/web/src/routes/__root.tsx+++ b/apps/web/src/routes/__root.tsx@@ -16,6 +16,7 @@ import { resolveServerBackedAppDisplayName } from "../branding.logic";import { AppSidebarLayout } from "../components/AppSidebarLayout";import { CommandPalette } from "../components/CommandPalette";import { ConfirmDialogHost } from "../components/ConfirmDialogHost";+import { FirstRunGate } from "../components/onboarding/FirstRunGate";import { ConnectOnboardingDialog } from "../components/cloud/ConnectOnboardingDialog";import { RelayClientInstallDialog } from "../components/cloud/RelayClientInstallDialog";import { SshPasswordPromptDialog } from "../components/desktop/SshPasswordPromptDialog";@@ -97,6 +98,13 @@ function RootRouteView() {const pathname = useLocation({ select: (location) => location.pathname });const { authGateState } = Route.useRouteContext();const primaryEnvironmentAuthenticated = authGateState.status === "authenticated";+ const returningFromWelcomeRef = useRef(pathname === "/welcome");++ useEffect(() => {+ if (pathname === "/welcome") {+ returningFromWelcomeRef.current = true;+ }+ }, [pathname]);useEffect(() => {const frame = window.requestAnimationFrame(() => {@@ -116,6 +124,19 @@ function RootRouteView() {);}+ // The welcome wizard is full-screen like /pair, but keeps toasts so its+ // connect/import actions can report failures.+ if (pathname === "/welcome") {+ return (+ <ToastProvider>+ <DocumentTitleSync />+ <ContrastAppearanceSync />+ <FontAppearanceSync />+ <Outlet />+ </ToastProvider>+ );+ }+if (authGateState.status !== "authenticated" && authGateState.status !== "hosted-static") {return (<>@@ -133,6 +154,10 @@ function RootRouteView() {</CommandPalette>);+ // FirstRunGate holds back everything below it — including EventRouter,+ // whose welcome payload navigates into a thread — until the first-run+ // decision is known, so a fresh install renders nothing (not the shell,+ // not a flash of threads) before landing on the welcome wizard.return (<ToastProvider><AnchoredToastProvider>@@ -141,21 +166,28 @@ function RootRouteView() {<EnvironmentThemeSync /><GlassAppearanceSync /><FontAppearanceSync />- {primaryEnvironmentAuthenticated ? <AuthenticatedTracingBootstrap /> : null}- {primaryEnvironmentAuthenticated ? <DesktopAppActivationCoordinator /> : null}- <RelayClientInstallDialog />- <ConnectOnboardingDialog />- <SshPasswordPromptDialog />- <ConfirmDialogHost />- <SlowRpcRequestToastCoordinator />- <HostedStaticEnvironmentBootstrap />- {primaryEnvironmentAuthenticated ? <EventRouter /> : null}- {primaryEnvironmentAuthenticated ? <PlanAgentSelectionHeal /> : null}- {primaryEnvironmentAuthenticated ? <ProviderUpdateLaunchNotification /> : null}- {appShell}- {/* Above the router: a theme draft is judged by walking the app, so the- editor has to survive navigation away from settings. */}- <ThemeEditorHost />+ <FirstRunGate+ enabled={primaryEnvironmentAuthenticated}+ hostedStatic={authGateState.status === "hosted-static"}+ >+ {primaryEnvironmentAuthenticated ? <AuthenticatedTracingBootstrap /> : null}+ {primaryEnvironmentAuthenticated ? <DesktopAppActivationCoordinator /> : null}+ <RelayClientInstallDialog />+ <ConnectOnboardingDialog />+ <SshPasswordPromptDialog />+ <ConfirmDialogHost />+ <SlowRpcRequestToastCoordinator />+ <HostedStaticEnvironmentBootstrap />+ {primaryEnvironmentAuthenticated ? (+ <EventRouter skipInitialBootstrapNavigation={returningFromWelcomeRef.current} />+ ) : null}+ {primaryEnvironmentAuthenticated ? <PlanAgentSelectionHeal /> : null}+ {primaryEnvironmentAuthenticated ? <ProviderUpdateLaunchNotification /> : null}+ {appShell}+ {/* Above the router: a theme draft is judged by walking the app, so the+ editor has to survive navigation away from settings. */}+ <ThemeEditorHost />+ </FirstRunGate></AnchoredToastProvider></ToastProvider>);@@ -381,7 +413,11 @@ function AuthenticatedTracingBootstrap() {return null;}-function EventRouter() {+function EventRouter({+ skipInitialBootstrapNavigation,+}: {+ readonly skipInitialBootstrapNavigation: boolean;+}) {const navigate = useNavigate();const pathname = useLocation({ select: (loc) => loc.pathname });const projectGroupingSettings = useClientSettings(selectProjectGroupingSettings);@@ -394,6 +430,7 @@ function EventRouter() {const serverWelcome = useAtomValue(primaryServerWelcomeAtom);const readPathname = useEffectEvent(() => pathname);const handledBootstrapThreadIdRef = useRef<string | null>(null);+ const skipInitialBootstrapNavigationRef = useRef(skipInitialBootstrapNavigation);const handledConfigEventRef = useRef(serverConfigEvent);const [keybindingsToastController] = useState<KeybindingsUpdateToastController>(() =>createKeybindingsUpdateToastController({}),@@ -425,6 +462,11 @@ function EventRouter() {if (readPathname() !== "/") {return;}+ if (skipInitialBootstrapNavigationRef.current) {+ skipInitialBootstrapNavigationRef.current = false;+ handledBootstrapThreadIdRef.current = payload.bootstrapThreadId;+ return;+ }if (handledBootstrapThreadIdRef.current === payload.bootstrapThreadId) {return;}diff --git a/apps/web/src/routes/_chat.index.tsx b/apps/web/src/routes/_chat.index.tsxindex ba96e8986a9..6a53ee02454 100644--- a/apps/web/src/routes/_chat.index.tsx+++ b/apps/web/src/routes/_chat.index.tsx@@ -21,10 +21,11 @@ import { hasCloudPublicConfig } from "~/cloud/publicConfig";function ChatIndexRouteView() {const { authGateState } = Route.useRouteContext();- const { environments } = useEnvironments();+ const { environments, isReady } = useEnvironments();- if (authGateState.status === "hosted-static" && environments.length === 0) {- return <HostedStaticOnboardingState />;+ if (authGateState.status === "hosted-static") {+ if (!isReady) return null;+ if (environments.length === 0) return <HostedStaticOnboardingState />;}return <IndexDraftLanding />;@@ -79,6 +80,8 @@ function IndexDraftLanding() {/>) : null;}+ // First-run routing to the welcome wizard happens in FirstRunGate at the+ // root, before this route ever renders.return <NoProjectsHero />;}diff --git a/apps/web/src/routes/welcome.tsx b/apps/web/src/routes/welcome.tsxnew file mode 100644index 00000000000..10caa4dd46f--- /dev/null+++ b/apps/web/src/routes/welcome.tsx@@ -0,0 +1,45 @@+import { createFileRoute, redirect, useNavigate } from "@tanstack/react-router";++import { WelcomeWizard } from "../components/onboarding/WelcomeWizard";+import { useNewThreadHandler } from "../hooks/useHandleNewThread";++/**+ * First-run welcome wizard. Full-screen, outside the sidebar shell (the root+ * route mounts this path bare, like /pair). Reached only via the first-run+ * gate on the index route; visiting it directly after onboarding is harmless —+ * finishing again just refreshes the completion flag.+ */+export const Route = createFileRoute("/welcome")({+ beforeLoad: ({ context }) => {+ const { authGateState } = context;+ if (authGateState.status !== "authenticated" && authGateState.status !== "hosted-static") {+ throw redirect({ to: "/pair", replace: true });+ }+ },+ component: WelcomeRouteView,+});++function WelcomeRouteView() {+ const { authGateState } = Route.useRouteContext();+ const navigate = useNavigate();+ const openNewThread = useNewThreadHandler();+ // An authenticated gate means a primary server is serving this app —+ // desktop, `npx t3`, or a dev server — and that server is "this machine"+ // no matter what hostname the browser used. Only hosted-static has no+ // local server to offer.+ const localAvailable = authGateState.status === "authenticated";+ return (+ <WelcomeWizard+ localAvailable={localAvailable}+ onDone={(projectRef) => {+ if (projectRef !== undefined) {+ void openNewThread(projectRef, { replace: true }).catch(() => {+ void navigate({ to: "/", replace: true });+ });+ return;+ }+ void navigate({ to: "/", replace: true });+ }}+ />+ );+}diff --git a/apps/web/src/state/agentSessions.ts b/apps/web/src/state/agentSessions.tsnew file mode 100644index 00000000000..996ddb0ea73--- /dev/null+++ b/apps/web/src/state/agentSessions.ts@@ -0,0 +1,25 @@+import { WS_METHODS } from "@t3tools/contracts";+import {+ createEnvironmentRpcCommand,+ createEnvironmentRpcQueryAtomFamily,+} from "@t3tools/client-runtime/state/runtime";++import { connectionAtomRuntime } from "../connection/runtime";++/**+ * Scan of Claude Code / Codex home directories on an environment, surfacing+ * project candidates for the welcome wizard's import step. The scan walks the+ * filesystem server-side, so results are cached briefly and refreshed when the+ * import step remounts.+ */+export const agentSessionScan = createEnvironmentRpcQueryAtomFamily(connectionAtomRuntime, {+ label: "environment-data:agent-sessions:scan",+ tag: WS_METHODS.agentSessionsScan,+ staleTimeMs: 30_000,+ idleTtlMs: 5 * 60_000,+});++export const agentSessionImport = createEnvironmentRpcCommand(connectionAtomRuntime, {+ label: "environment-data:agent-sessions:import",+ tag: WS_METHODS.agentSessionsImport,+});diff --git a/docs/user/welcome-wizard.md b/docs/user/welcome-wizard.mdnew file mode 100644index 00000000000..9a2aa116670--- /dev/null+++ b/docs/user/welcome-wizard.md@@ -0,0 +1,62 @@+# Welcome wizard++T3 Code shows a setup flow when you open a new installation or connect to the+hosted app for the first time. Existing workspaces skip this flow.++## Choose a connection++- **This computer** runs agents on the computer that hosts T3 Code. It does not+ require an account.+- **T3 Connect** connects computers that are signed in to your account. Run+ `npx t3 connect` on each computer you want to add, then start T3 Code or run+ `npx t3 serve` so the computer stays available.+- **Pair a server** connects directly to a server on your network or tailnet.+ Start the server with `npx t3 serve`, then run `npx t3 pair --tailscale` and+ paste the pairing link. You can also run `npx t3 serve --host <address>` and+ use `npx t3 pair` when the server is already reachable on your network.++If T3 Code cannot confirm the workspace during startup, the setup flow shows+**Still connecting** instead of opening the app. Select **Reload** to try again.++If T3 Code cannot read your saved settings, it shows **Could not read settings**.+Select **Retry** after storage becomes available. Setup does not replace+unreadable settings with defaults.++## Check your agents++T3 Code checks the selected computer for Claude Code and Codex. If an agent is+not installed or signed in, select its action to open a terminal with the+correct command ready to run. Other providers can be enabled in Settings.++The setup terminal uses the home directory and environment configured for the+selected provider instance. Sensitive values remain redacted in Settings and+terminal metadata while the terminal process can use them.++## Import your projects++T3 Code finds directories that Claude Code or Codex has used. The default+selection includes projects active within the last 30 days. Select **Choose**+to include older projects or change the selection.++A large or malformed history can reach the scan limit. T3 Code keeps the+projects it found and warns when projects or conversations may be missing.++Imported projects include Codex and Claude conversations active within the last+30 days. You can continue those conversations in T3 Code.++Conversation import is best effort. T3 Code keeps the first user prompt and the+newest remaining visible user and assistant messages, with 200 messages total.+It omits tool activity and attachments. For Codex, it omits generated setup+context only when a canonical user event and a valid shared turn ID identify the+same user turn. Ambiguous legacy or response-only context stays in the imported+conversation so T3 Code does not remove user text. It reads one conversation at+a time and skips files larger than 16 MiB. It ignores malformed records and skips+unreadable or unparseable conversations.++Each import attempt reads up to 100 conversation files and 64 MiB per project,+with up to 100,000 input records. Run import again to continue a large batch.+Completed conversations are not imported again. You can continue without the+remaining history.++You can skip agent setup and project import. Select **Back** to return to a+previous step.diff --git a/packages/client-runtime/src/rpc/client.test.ts b/packages/client-runtime/src/rpc/client.test.tsindex 4e6baba8bef..f2141add930 100644--- a/packages/client-runtime/src/rpc/client.test.ts+++ b/packages/client-runtime/src/rpc/client.test.ts@@ -3,10 +3,12 @@ import {EnvironmentId,type RelayClientInstallProgressEvent,type ServerConfigStreamEvent,+ type ServerLifecycleStreamEvent,WS_METHODS,} from "@t3tools/contracts";import { describe, expect, it } from "@effect/vitest";import * as Cause from "effect/Cause";+import * as Deferred from "effect/Deferred";import * as Effect from "effect/Effect";import * as Exit from "effect/Exit";import * as Fiber from "effect/Fiber";@@ -27,7 +29,13 @@ import {import * as EnvironmentSupervisor from "../connection/supervisor.ts";import * as RpcSession from "../rpc/session.ts";import type { WsRpcProtocolClient } from "../rpc/protocol.ts";-import { EnvironmentRpcRequestObserver, request, runStream, subscribe } from "./client.ts";+import {+ EnvironmentRpcRequestObserver,+ request,+ runStream,+ subscribe,+ subscribeDynamicWithSession,+} from "./client.ts";const TARGET = new PrimaryConnectionTarget({environmentId: EnvironmentId.make("environment-1"),@@ -221,6 +229,72 @@ describe("environment RPC", () => {}),);+ it.effect("keeps the producer session on an old value buffered across a session switch", () =>+ Effect.gen(function* () {+ const firstSubscribed = yield* Deferred.make<void>();+ const secondSubscribed = yield* Deferred.make<void>();+ const firstValueBlocked = yield* Deferred.make<void>();+ const releaseFirstValue = yield* Deferred.make<void>();+ const firstValue = { source: "first", index: 1 } as unknown as ServerLifecycleStreamEvent;+ const bufferedFirstValue = {+ source: "first",+ index: 2,+ } as unknown as ServerLifecycleStreamEvent;+ const secondValue = { source: "second", index: 1 } as unknown as ServerLifecycleStreamEvent;+ const firstClient = {+ [WS_METHODS.subscribeServerLifecycle]: () =>+ Stream.fromEffect(Deferred.succeed(firstSubscribed, undefined)).pipe(+ Stream.drain,+ Stream.concat(Stream.fromIterable([firstValue, bufferedFirstValue])),+ Stream.concat(Stream.never),+ ),+ } as unknown as WsRpcProtocolClient;+ const secondClient = {+ [WS_METHODS.subscribeServerLifecycle]: () =>+ Stream.fromEffect(Deferred.succeed(secondSubscribed, undefined)).pipe(+ Stream.drain,+ Stream.concat(Stream.make(secondValue)),+ Stream.concat(Stream.never),+ ),+ } as unknown as WsRpcProtocolClient;+ const firstSession = session(firstClient);+ const secondSession = session(secondClient);+ const { activeSession, supervisor } = yield* makeHarness();++ const resultFiber = yield* subscribeDynamicWithSession(+ WS_METHODS.subscribeServerLifecycle,+ () => Effect.succeed({}),+ ).pipe(+ Stream.mapEffect(([producerSession, value]) =>+ value === firstValue+ ? Deferred.succeed(firstValueBlocked, undefined).pipe(+ Effect.andThen(Deferred.await(releaseFirstValue)),+ Effect.as([producerSession, value] as const),+ )+ : Effect.succeed([producerSession, value] as const),+ ),+ Stream.take(3),+ Stream.runCollect,+ Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),+ Effect.forkChild,+ );++ yield* SubscriptionRef.set(activeSession, Option.some(firstSession));+ yield* Deferred.await(firstSubscribed);+ yield* Deferred.await(firstValueBlocked);+ yield* SubscriptionRef.set(activeSession, Option.some(secondSession));+ yield* Deferred.await(secondSubscribed);+ yield* Deferred.succeed(releaseFirstValue, undefined);++ const result = yield* Fiber.join(resultFiber);+ expect(result).toEqual([+ [firstSession, firstValue],+ [firstSession, bufferedFirstValue],+ [secondSession, secondValue],+ ]);+ }),+ );+it.effect("keeps durable subscriptions alive across a transport failure and new session", () =>Effect.gen(function* () {const subscriptions: string[] = [];diff --git a/packages/client-runtime/src/rpc/client.ts b/packages/client-runtime/src/rpc/client.tsindex 0d68d2b2d53..175d633e242 100644--- a/packages/client-runtime/src/rpc/client.ts+++ b/packages/client-runtime/src/rpc/client.ts@@ -178,15 +178,15 @@ interface SubscriptionOptions<TTag extends EnvironmentSubscriptionRpcTag> {readonly resubscribe?: Stream.Stream<unknown, never, never>;}-export function subscribeDynamic<TTag extends EnvironmentSubscriptionRpcTag>(+function subscribeDynamicMapped<TTag extends EnvironmentSubscriptionRpcTag, A>(tag: TTag,makeInput: (session: RpcSession) => Effect.Effect<EnvironmentRpcInput<TTag>>,+ mapStream: (+ session: RpcSession,+ stream: Stream.Stream<EnvironmentRpcStreamValue<TTag>, EnvironmentRpcStreamFailure<TTag>>,+ ) => Stream.Stream<A, EnvironmentRpcStreamFailure<TTag>>,options?: SubscriptionOptions<TTag>,-): Stream.Stream<- EnvironmentRpcStreamValue<TTag>,- EnvironmentRpcStreamFailure<TTag>,- EnvironmentSupervisor-> {+): Stream.Stream<A, EnvironmentRpcStreamFailure<TTag>, EnvironmentSupervisor> {return Stream.unwrap(Effect.gen(function* () {const supervisor = yield* EnvironmentSupervisor;@@ -216,10 +216,7 @@ export function subscribeDynamic<TTag extends EnvironmentSubscriptionRpcTag>(EnvironmentRpcStreamValue<TTag>,EnvironmentRpcStreamFailure<TTag>>;- const subscribeToSession = (): Stream.Stream<- EnvironmentRpcStreamValue<TTag>,- EnvironmentRpcStreamFailure<TTag>- > =>+ const subscribeToSession = (): Stream.Stream<A, EnvironmentRpcStreamFailure<TTag>> =>Stream.suspend(() =>Stream.unwrap(Effect.gen(function* () {@@ -229,7 +226,7 @@ export function subscribeDynamic<TTag extends EnvironmentSubscriptionRpcTag>(method: tag,input,});- return method(input).pipe(+ return mapStream(session, method(input)).pipe(Stream.ensuring(completeObservation),Stream.catchCause((cause) => {const hasOnlyExpectedFailures =@@ -287,6 +284,36 @@ export function subscribeDynamic<TTag extends EnvironmentSubscriptionRpcTag>();}+export function subscribeDynamic<TTag extends EnvironmentSubscriptionRpcTag>(+ tag: TTag,+ makeInput: (session: RpcSession) => Effect.Effect<EnvironmentRpcInput<TTag>>,+ options?: SubscriptionOptions<TTag>,+): Stream.Stream<+ EnvironmentRpcStreamValue<TTag>,+ EnvironmentRpcStreamFailure<TTag>,+ EnvironmentSupervisor+> {+ return subscribeDynamicMapped(tag, makeInput, (_session, stream) => stream, options);+}++/** Tags each value before `switchMap` can buffer it across a session change. */+export function subscribeDynamicWithSession<TTag extends EnvironmentSubscriptionRpcTag>(+ tag: TTag,+ makeInput: (session: RpcSession) => Effect.Effect<EnvironmentRpcInput<TTag>>,+ options?: SubscriptionOptions<TTag>,+): Stream.Stream<+ readonly [session: RpcSession, value: EnvironmentRpcStreamValue<TTag>],+ EnvironmentRpcStreamFailure<TTag>,+ EnvironmentSupervisor+> {+ return subscribeDynamicMapped(+ tag,+ makeInput,+ (session, stream) => stream.pipe(Stream.map((value) => [session, value] as const)),+ options,+ );+}+export function subscribe<TTag extends EnvironmentSubscriptionRpcTag>(tag: TTag,input: EnvironmentRpcInput<TTag>,diff --git a/packages/client-runtime/src/state/server.test.ts b/packages/client-runtime/src/state/server.test.tsindex 6567726a480..878f8c902f9 100644--- a/packages/client-runtime/src/state/server.test.ts+++ b/packages/client-runtime/src/state/server.test.ts@@ -7,8 +7,8 @@ import {} from "@t3tools/contracts";import { describe, expect, it } from "@effect/vitest";import * as Cause from "effect/Cause";-import * as Duration from "effect/Duration";import * as Deferred from "effect/Deferred";+import * as Duration from "effect/Duration";import * as Effect from "effect/Effect";import * as Exit from "effect/Exit";import * as Fiber from "effect/Fiber";@@ -31,13 +31,15 @@ import * as Persistence from "../platform/persistence.ts";import type { WsRpcProtocolClient } from "../rpc/protocol.ts";import type { RpcSession } from "../rpc/session.ts";import {+ applyServerWelcomeEvent,+ makeEnvironmentServerWelcomeState,makeEnvironmentServerConfigState,isLegacyUpdateHandoffLoss,matchesServerUpdateReadyEvent,matchesServerUpdateResumeEvent,nudgeReconnectDuringUpdateRestart,- projectServerWelcome,resolveServerConfigValue,+ resolveServerWelcomeState,resolveServerUpdateProgressResult,serverUpdateStateForProgressEvent,serverUpdateStateForServerVersion,@@ -494,25 +496,227 @@ describe("server state projection", () => {expect(Option.getOrThrow(downgraded).config.environmentThemes).toBeUndefined();});- it("retains welcome when a ready event follows in the same stream chunk", () => {+ it("keeps a current welcome on ready and rejects a buffered welcome from the old session", () => {+ const firstSession = session({} as WsRpcProtocolClient);+ const secondSession = session({} as WsRpcProtocolClient);const welcome = {environment: {} as ServerLifecycleWelcomePayload["environment"],cwd: "/repo",projectName: "repo",} as ServerLifecycleWelcomePayload;- const [afterWelcome] = projectServerWelcome(Option.none(), {+ const initial = {+ currentSession: firstSession,+ welcomeSession: firstSession,+ welcome: null,+ };+ const afterWelcome = applyServerWelcomeEvent(initial, firstSession, {type: "welcome",payload: welcome,});- const [afterReady, emitted] = projectServerWelcome(afterWelcome, {+ const afterReady = applyServerWelcomeEvent(afterWelcome, firstSession, {type: "ready",payload: {},});+ const afterSwitch = { ...afterReady, currentSession: secondSession };+ const afterBufferedOldWelcome = applyServerWelcomeEvent(afterSwitch, firstSession, {+ type: "welcome",+ payload: { ...welcome, cwd: "/stale" },+ });- expect(Option.getOrThrow(afterReady)).toBe(welcome);- expect(emitted).toEqual([]);+ expect(afterReady).toBe(afterWelcome);+ expect(resolveServerWelcomeState(afterReady)).toBe(welcome);+ expect(afterBufferedOldWelcome).toBe(afterSwitch);+ expect(resolveServerWelcomeState(afterBufferedOldWelcome)).toBeNull();});+ it.effect("checks the authoritative session before accepting a buffered welcome", () =>+ Effect.gen(function* () {+ const firstEvents = yield* Queue.unbounded<{+ readonly type: "welcome" | "ready";+ readonly payload: unknown;+ }>();+ const firstSubscribed = yield* Deferred.make<void>();+ const firstClient = {+ [WS_METHODS.subscribeServerLifecycle]: () =>+ Stream.fromEffect(Deferred.succeed(firstSubscribed, undefined)).pipe(+ Stream.drain,+ Stream.concat(Stream.fromQueue(firstEvents)),+ ),+ } as unknown as WsRpcProtocolClient;+ const firstSession = session(firstClient);+ const secondSession = session({} as WsRpcProtocolClient);+ const supervisorSession = yield* SubscriptionRef.make(Option.some(firstSession));+ const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({+ target: TARGET,+ state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),+ session: supervisorSession,+ prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),+ connect: Effect.void,+ disconnect: Effect.void,+ retryNow: Effect.void,+ } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);+ const staleWelcome = {+ environment: {} as ServerLifecycleWelcomePayload["environment"],+ cwd: "/stale",+ projectName: "stale",+ } as ServerLifecycleWelcomePayload;++ yield* Effect.scoped(+ Effect.gen(function* () {+ const state = yield* makeEnvironmentServerWelcomeState().pipe(+ Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),+ );+ yield* Deferred.await(firstSubscribed);++ // Model the point after the ref changed but before either subscriber+ // processed its publication.+ supervisorSession.value = Option.some(secondSession);+ const handled = yield* SubscriptionRef.changes(state).pipe(+ Stream.filter(+ (value) => value.currentSession === secondSession || value.welcome === staleWelcome,+ ),+ Stream.runHead,+ Effect.map(Option.getOrThrow),+ Effect.forkChild,+ );+ yield* Queue.offer(firstEvents, { type: "welcome", payload: staleWelcome });++ const next = yield* Fiber.join(handled);+ expect(next.currentSession).toBe(secondSession);+ expect(resolveServerWelcomeState(next)).toBeNull();+ }),+ );+ }),+ );++ it.effect("reads the authoritative session after waiting for the welcome state lock", () =>+ Effect.gen(function* () {+ const firstSubscribed = yield* Deferred.make<void>();+ const firstClient = {+ [WS_METHODS.subscribeServerLifecycle]: () =>+ Stream.fromEffect(Deferred.succeed(firstSubscribed, undefined)).pipe(Stream.drain),+ } as unknown as WsRpcProtocolClient;+ const secondClient = {+ [WS_METHODS.subscribeServerLifecycle]: () => Stream.never,+ } as unknown as WsRpcProtocolClient;+ const firstSession = session(firstClient);+ const secondSession = session(secondClient);+ const thirdSession = session({} as WsRpcProtocolClient);+ const supervisorSession = yield* SubscriptionRef.make(Option.some(firstSession));+ const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({+ target: TARGET,+ state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),+ session: supervisorSession,+ prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),+ connect: Effect.void,+ disconnect: Effect.void,+ retryNow: Effect.void,+ } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);++ yield* Effect.scoped(+ Effect.gen(function* () {+ const state = yield* makeEnvironmentServerWelcomeState().pipe(+ Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),+ );+ yield* Deferred.await(firstSubscribed);+ const changed = yield* SubscriptionRef.changes(state).pipe(+ Stream.filter((value) => value.currentSession !== firstSession),+ Stream.runHead,+ Effect.map(Option.getOrThrow),+ Effect.forkChild,+ );++ yield* state.semaphore.withPermit(+ Effect.gen(function* () {+ yield* SubscriptionRef.set(supervisorSession, Option.some(secondSession));+ yield* Effect.yieldNow;+ yield* Effect.yieldNow;+ yield* Effect.yieldNow;+ supervisorSession.value = Option.some(thirdSession);+ }),+ );++ expect((yield* Fiber.join(changed)).currentSession).toBe(thirdSession);+ }),+ );+ }),+ );++ it.effect("clears a welcome until the reconnected session sends its own", () =>+ Effect.gen(function* () {+ const firstEvents = yield* Queue.unbounded<{+ readonly type: "welcome" | "ready";+ readonly payload: unknown;+ }>();+ const secondEvents = yield* Queue.unbounded<{+ readonly type: "welcome" | "ready";+ readonly payload: unknown;+ }>();+ const firstClient = {+ [WS_METHODS.subscribeServerLifecycle]: () => Stream.fromQueue(firstEvents),+ } as unknown as WsRpcProtocolClient;+ const secondClient = {+ [WS_METHODS.subscribeServerLifecycle]: () => Stream.fromQueue(secondEvents),+ } as unknown as WsRpcProtocolClient;+ const firstSession = session(firstClient);+ const secondSession = session(secondClient);+ const supervisorSession = yield* SubscriptionRef.make(Option.some(firstSession));+ const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({+ target: TARGET,+ state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),+ session: supervisorSession,+ prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),+ connect: Effect.void,+ disconnect: Effect.void,+ retryNow: Effect.void,+ } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);+ const firstWelcome = {+ environment: {} as ServerLifecycleWelcomePayload["environment"],+ cwd: "/first",+ projectName: "first",+ } as ServerLifecycleWelcomePayload;+ const secondWelcome = {+ environment: {} as ServerLifecycleWelcomePayload["environment"],+ cwd: "/second",+ projectName: "second",+ } as ServerLifecycleWelcomePayload;++ yield* Effect.scoped(+ Effect.gen(function* () {+ const state = yield* makeEnvironmentServerWelcomeState().pipe(+ Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),+ );+ const nextResolved = (+ predicate: (value: ServerLifecycleWelcomePayload | null) => boolean,+ ) =>+ SubscriptionRef.changes(state).pipe(+ Stream.map(resolveServerWelcomeState),+ Stream.filter(predicate),+ Stream.runHead,+ Effect.map(Option.getOrThrow),+ );++ const first = yield* nextResolved((value) => value === firstWelcome).pipe(+ Effect.forkChild,+ );+ yield* Queue.offer(firstEvents, { type: "welcome", payload: firstWelcome });+ expect(yield* Fiber.join(first)).toBe(firstWelcome);++ const cleared = yield* nextResolved((value) => value === null).pipe(Effect.forkChild);+ yield* SubscriptionRef.set(supervisorSession, Option.some(secondSession));+ expect(yield* Fiber.join(cleared)).toBeNull();+ expect(resolveServerWelcomeState(yield* SubscriptionRef.get(state))).toBeNull();++ const second = yield* nextResolved((value) => value === secondWelcome).pipe(+ Effect.forkChild,+ );+ yield* Queue.offer(secondEvents, { type: "welcome", payload: secondWelcome });+ expect(yield* Fiber.join(second)).toBe(secondWelcome);+ }),+ );+ }),+ );+it("prefers an active session config over cache until a live event arrives", () => {const config = (source: string, serverVersion: string) =>({diff --git a/packages/client-runtime/src/state/server.ts b/packages/client-runtime/src/state/server.tsindex 7ba62a68148..9f94663388e 100644--- a/packages/client-runtime/src/state/server.ts+++ b/packages/client-runtime/src/state/server.ts@@ -40,8 +40,10 @@ import {request,runStream,subscribe,+ subscribeDynamicWithSession,type EnvironmentRpcInput,} from "../rpc/client.ts";+import type { RpcSession } from "../rpc/session.ts";import { followStreamInEnvironment } from "./runtime.ts";import {applyServerConfigProjection,@@ -476,21 +478,119 @@ export function serverConfigStateChanges();}-export function projectServerWelcome(- current: Option.Option<ServerLifecycleWelcomePayload>,+export function applyServerWelcomeEvent(+ current: EnvironmentServerWelcomeState,+ session: RpcSession,event: {readonly type: "welcome" | "ready";readonly payload: unknown;},-): readonly [- Option.Option<ServerLifecycleWelcomePayload>,- ReadonlyArray<ServerLifecycleWelcomePayload>,-] {- if (event.type !== "welcome") {- return [current, []];- }- const welcome = event.payload as ServerLifecycleWelcomePayload;- return [Option.some(welcome), [welcome]];+): EnvironmentServerWelcomeState {+ return event.type === "welcome" && current.currentSession === session+ ? {+ ...current,+ welcomeSession: session,+ welcome: event.payload as ServerLifecycleWelcomePayload,+ }+ : current;+}++export interface EnvironmentServerWelcomeState {+ readonly currentSession: RpcSession | null;+ readonly welcomeSession: RpcSession | null;+ readonly welcome: ServerLifecycleWelcomePayload | null;+}++export function resolveServerWelcomeState(+ state: EnvironmentServerWelcomeState,+): ServerLifecycleWelcomePayload | null {+ return state.currentSession === state.welcomeSession ? state.welcome : null;+}++export const makeEnvironmentServerWelcomeState = Effect.fn("EnvironmentServerWelcomeState.make")(+ function* () {+ const supervisor = yield* EnvironmentSupervisor;+ const initialSession = Option.getOrNull(yield* SubscriptionRef.get(supervisor.session));+ const state = yield* SubscriptionRef.make<EnvironmentServerWelcomeState>({+ currentSession: initialSession,+ welcomeSession: null,+ welcome: null,+ });++ const updateWithCurrentSession = Effect.fn(+ "EnvironmentServerWelcomeState.updateWithCurrentSession",+ )(function* (+ update: (+ current: EnvironmentServerWelcomeState,+ currentSession: RpcSession | null,+ ) => EnvironmentServerWelcomeState,+ ) {+ return yield* SubscriptionRef.modifyEffect(state, (current) =>+ SubscriptionRef.get(supervisor.session).pipe(+ Effect.map(+ (latestSession) =>+ [undefined, update(current, Option.getOrNull(latestSession))] as const,+ ),+ ),+ );+ });++ yield* SubscriptionRef.changes(supervisor.session).pipe(+ Stream.runForEach(() =>+ updateWithCurrentSession((current, currentSession) => ({+ ...current,+ currentSession,+ })),+ ),+ Effect.forkScoped,+ );++ yield* subscribeDynamicWithSession(+ WS_METHODS.subscribeServerLifecycle,+ Effect.fn("EnvironmentServerWelcomeState.makeSubscribeInput")(function* (session) {+ yield* updateWithCurrentSession((current, currentSession) =>+ currentSession === session+ ? {+ ...current,+ currentSession,+ welcomeSession: session,+ welcome: null,+ }+ : { ...current, currentSession },+ );+ return {};+ }),+ ).pipe(+ Stream.runForEach(([session, event]) =>+ updateWithCurrentSession((current, currentSession) =>+ applyServerWelcomeEvent(+ {+ ...current,+ currentSession,+ },+ session,+ event,+ ),+ ),+ ),+ Effect.forkScoped,+ );++ return state;+ },+);++export function serverWelcomeStateChanges(environmentId: EnvironmentId) {+ return followStreamInEnvironment(+ environmentId,+ Stream.unwrap(+ makeEnvironmentServerWelcomeState().pipe(+ Effect.map((state) =>+ SubscriptionRef.changes(state).pipe(Stream.map(resolveServerWelcomeState)),+ ),+ ),+ ),+ );}export function resolveServerConfigValue(@@ -833,6 +933,27 @@ export function createServerEnvironmentAtoms<R, E>(Atom.withLabel(`environment-data:server:providers:${environmentId}`),),);+ const welcomeStateFamily = Atom.family((environmentId: EnvironmentId) =>+ runtime+ .atom(serverWelcomeStateChanges(environmentId), { initialValue: null })+ .pipe(+ Atom.setIdleTTL(5 * 60_000),+ Atom.withLabel(`environment-data:server:welcome-state:${environmentId}`),+ ),+ );+ const welcomeFamily = Atom.family((environmentId: EnvironmentId) =>+ Atom.make((get) => {+ const result = get(welcomeStateFamily(environmentId));+ if (result._tag !== "Success") return result;+ return result.value === null+ ? AsyncResult.initial<ServerLifecycleWelcomePayload, never>(result.waiting)+ : AsyncResult.success(result.value, result);+ }).pipe(Atom.withLabel(`environment-data:server:welcome:${environmentId}`)),+ );+ const welcome = (target: {+ readonly environmentId: EnvironmentId;+ readonly input: EnvironmentRpcInput<typeof WS_METHODS.subscribeServerLifecycle>;+ }) => welcomeFamily(target.environmentId);return {configValueAtom,@@ -916,14 +1037,7 @@ export function createServerEnvironmentAtoms<R, E>(refreshTrigger: ({ environmentId }) => usagePricesAtom(environmentId),}),configProjection,- welcome: createEnvironmentRpcSubscriptionAtomFamily(runtime, {- label: "environment-data:server:welcome",- tag: WS_METHODS.subscribeServerLifecycle,- transform: (stream) =>- stream.pipe(- Stream.mapAccum(Option.none<ServerLifecycleWelcomePayload>, projectServerWelcome),- ),- }),+ welcome,consumeResetCredit: createEnvironmentRpcCommand(runtime, {label: "environment-data:server:consume-reset-credit",tag: WS_METHODS.providerConsumeResetCredit,diff --git a/packages/client-runtime/src/state/threadReducer.test.ts b/packages/client-runtime/src/state/threadReducer.test.tsindex 9a9be9b4da0..1f52eccc94c 100644--- a/packages/client-runtime/src/state/threadReducer.test.ts+++ b/packages/client-runtime/src/state/threadReducer.test.ts@@ -380,6 +380,40 @@ describe("applyThreadDetailEvent", () => {}});+ it("keeps imported replies turnless when delivered again", () => {+ const event = {+ ...baseEventFields,+ sequence: 6,+ occurredAt: "2026-04-01T06:00:00.000Z",+ aggregateKind: "thread",+ aggregateId: baseThread.id,+ type: "thread.message-sent",+ payload: {+ threadId: baseThread.id,+ messageId: MessageId.make("import:codex:session-1:000001"),+ role: "assistant",+ text: "Imported reply",+ turnId: null,+ streaming: false,+ createdAt: "2026-03-01T06:00:00.000Z",+ updatedAt: "2026-03-01T06:00:00.000Z",+ },+ } as const;++ const imported = applyThreadDetailEvent(baseThread, event);+ expect(imported.kind).toBe("updated");+ if (imported.kind !== "updated") return;+ expect(imported.thread.latestTurn).toBeNull();+ expect(imported.thread.checkpoints).toBe(baseThread.checkpoints);++ const repeated = applyThreadDetailEvent(imported.thread, { ...event, sequence: 7 });+ expect(repeated.kind).toBe("updated");+ if (repeated.kind !== "updated") return;+ expect(repeated.thread.messages).toEqual(imported.thread.messages);+ expect(repeated.thread.latestTurn).toBeNull();+ expect(repeated.thread.checkpoints).toBe(baseThread.checkpoints);+ });+it("appends text for streaming messages", () => {const threadWithMessage: OrchestrationThread = {...baseThread,@@ -1178,6 +1212,100 @@ describe("applyThreadDetailEvent", () => {});describe("thread.reverted", () => {+ it("keeps imported history and removes the first live prompt at checkpoint zero", () => {+ const threadWithImportedHistory: OrchestrationThread = {+ ...baseThread,+ messages: [+ {+ id: MessageId.make("import:codex:session-1:000000"),+ role: "user",+ text: "Imported prompt",+ turnId: null,+ streaming: false,+ createdAt: "2026-03-01T00:00:00.000Z",+ updatedAt: "2026-03-01T00:00:00.000Z",+ },+ {+ id: MessageId.make("import:codex:session-1:000001"),+ role: "assistant",+ text: "Imported answer",+ turnId: null,+ streaming: false,+ createdAt: "2026-03-01T00:01:00.000Z",+ updatedAt: "2026-03-01T00:01:00.000Z",+ },+ {+ id: MessageId.make("live-user-message"),+ role: "user",+ text: "New work",+ turnId: null,+ streaming: false,+ createdAt: "2026-04-01T01:00:00.000Z",+ updatedAt: "2026-04-01T01:00:00.000Z",+ },+ ],+ };++ const result = applyThreadDetailEvent(threadWithImportedHistory, {+ ...baseEventFields,+ sequence: 14,+ occurredAt: "2026-04-01T02:00:00.000Z",+ aggregateKind: "thread",+ aggregateId: ThreadId.make("thread-1"),+ type: "thread.reverted",+ payload: { threadId: ThreadId.make("thread-1"), turnCount: 0 },+ });++ expect(result.kind).toBe("updated");+ if (result.kind === "updated") {+ expect(result.thread.messages.map((message) => message.text)).toEqual([+ "Imported prompt",+ "Imported answer",+ ]);+ }+ });++ it("fallback-retains the earliest absolute timestamp across offsets", () => {+ const threadWithOffsetMessages: OrchestrationThread = {+ ...baseThread,+ messages: [+ {+ id: MessageId.make("earlier-by-offset"),+ role: "user",+ text: "Earlier",+ turnId: null,+ streaming: false,+ createdAt: "2026-04-01T10:30:00.000+02:00",+ updatedAt: "2026-04-01T10:30:00.000+02:00",+ },+ {+ id: MessageId.make("later-in-utc"),+ role: "user",+ text: "Later",+ turnId: null,+ streaming: false,+ createdAt: "2026-04-01T09:00:00.000Z",+ updatedAt: "2026-04-01T09:00:00.000Z",+ },+ ],+ };++ const result = applyThreadDetailEvent(threadWithOffsetMessages, {+ ...baseEventFields,+ sequence: 14,+ occurredAt: "2026-04-01T10:00:00.000Z",+ aggregateKind: "thread",+ aggregateId: ThreadId.make("thread-1"),+ type: "thread.reverted",+ payload: { threadId: ThreadId.make("thread-1"), turnCount: 1 },+ });++ expect(result.kind).toBe("updated");+ if (result.kind === "updated") {+ expect(result.thread.messages.map((message) => message.id)).toEqual(["earlier-by-offset"]);+ }+ });+it("filters entities to retained turns", () => {const threadWithData: OrchestrationThread = {...baseThread,diff --git a/packages/client-runtime/src/state/threadReducer.ts b/packages/client-runtime/src/state/threadReducer.tsindex 1de0b654c06..c237856f90b 100644--- a/packages/client-runtime/src/state/threadReducer.ts+++ b/packages/client-runtime/src/state/threadReducer.ts@@ -12,6 +12,8 @@ import type {OrchestrationThreadActivity,TurnId,} from "@t3tools/contracts";+import { isImportedAgentSessionMessageId } from "@t3tools/contracts";+import { compareDateTimeStrings } from "@t3tools/shared/dateTime";export type ThreadDetailReducerResult =| { readonly kind: "updated"; readonly thread: OrchestrationThread }@@ -551,7 +553,11 @@ export function applyThreadDetailEvent();const retainedTurnIds = new Set(Arr.map(checkpoints, (entry) => entry.turnId));- const messages = retainMessagesAfterRevert(thread.messages, retainedTurnIds);+ const messages = retainMessagesAfterRevert(+ thread.messages,+ retainedTurnIds,+ event.payload.turnCount,+ );const proposedPlans = pipe(thread.proposedPlans,Arr.filter((plan) => plan.turnId === null || retainedTurnIds.has(plan.turnId)),@@ -744,16 +750,42 @@ function rebindCheckpointAssistantMessage(function retainMessagesAfterRevert(messages: ReadonlyArray<OrchestrationMessage>,retainedTurnIds: ReadonlySet<string>,+ turnCount: number,): OrchestrationMessage[] {- // Keep messages that belong to a retained turn, plus system messages and- // messages without a turn binding (pre-turn-0 user messages).- return Arr.filter(messages, (message) => {- if (message.role === "system") {- return true;+ const retainedMessageIds = new Set<string>();+ for (const message of messages) {+ if (message.role === "system" || isImportedAgentSessionMessageId(message.id)) {+ retainedMessageIds.add(message.id);+ } else if (message.turnId !== null && retainedTurnIds.has(message.turnId)) {+ retainedMessageIds.add(message.id);}- if (message.turnId === null) {- return true;+ }++ for (const role of ["user", "assistant"] as const) {+ const retainedCount = messages.filter(+ (message) =>+ message.role === role &&+ !isImportedAgentSessionMessageId(message.id) &&+ retainedMessageIds.has(message.id),+ ).length;+ const missingCount = Math.max(0, turnCount - retainedCount);+ const fallbackMessages = messages+ .filter(+ (message) =>+ message.role === role &&+ !retainedMessageIds.has(message.id) &&+ (message.turnId === null || retainedTurnIds.has(message.turnId)),+ )+ .toSorted(+ (left, right) =>+ compareDateTimeStrings(left.createdAt, right.createdAt) ||+ left.id.localeCompare(right.id),+ )+ .slice(0, missingCount);+ for (const message of fallbackMessages) {+ retainedMessageIds.add(message.id);}- return retainedTurnIds.has(message.turnId);- });+ }++ return Arr.filter(messages, (message) => retainedMessageIds.has(message.id));}diff --git a/packages/contracts/src/agentSessions.ts b/packages/contracts/src/agentSessions.tsnew file mode 100644index 00000000000..ffd90dd79db--- /dev/null+++ b/packages/contracts/src/agentSessions.ts@@ -0,0 +1,98 @@+import * as Schema from "effect/Schema";+import { IsoDateTime, NonNegativeInt, ProjectId, TrimmedNonEmptyString } from "./baseSchemas.ts";+import { ProviderInstanceId } from "./providerInstance.ts";++/** Coding agent home directories the scanner knows how to read. */+export const AgentSessionSource = Schema.Literals(["claudeAgent", "codex"]);+export type AgentSessionSource = typeof AgentSessionSource.Type;++/** File identity saved with an imported session so bounded retries can skip unchanged history. */+export const AgentSessionImportSource = Schema.Struct({+ provider: AgentSessionSource,+ providerInstanceId: ProviderInstanceId,+ providerSessionId: TrimmedNonEmptyString,+ filePath: TrimmedNonEmptyString,+ size: NonNegativeInt,+ mtimeMs: Schema.NullOr(Schema.Number),+ device: Schema.Number,+ inode: Schema.NullOr(Schema.Number),+ birthtimeMs: Schema.NullOr(Schema.Number),+});+export type AgentSessionImportSource = typeof AgentSessionImportSource.Type;++/** Imported message ids retain their origin after event metadata is projected into SQLite. */+export function isImportedAgentSessionMessageId(messageId: string): boolean {+ return messageId.startsWith("import:");+}++/**+ * Empty for now. Kept as a struct so future scan options (source filters,+ * explicit roots) can be added without a new method.+ */+export const AgentSessionScanInput = Schema.Struct({});+export type AgentSessionScanInput = typeof AgentSessionScanInput.Type;++/**+ * A directory that at least one agent CLI has run in, suitable for import as a+ * T3 Code project. `alreadyImported` marks candidates that already have an+ * active project rooted at the same path.+ */+export const AgentSessionProjectCandidate = Schema.Struct({+ path: TrimmedNonEmptyString,+ title: TrimmedNonEmptyString,+ projectId: Schema.optional(ProjectId),+ sources: Schema.Array(AgentSessionSource),+ threadCount: NonNegativeInt,+ lastActiveAt: Schema.NullOr(IsoDateTime),+ alreadyImported: Schema.Boolean,+});+export type AgentSessionProjectCandidate = typeof AgentSessionProjectCandidate.Type;++export const AgentSessionScanResult = Schema.Struct({+ candidates: Schema.Array(AgentSessionProjectCandidate),+ scannedAt: IsoDateTime,+ truncated: Schema.optional(Schema.Boolean),+});+export type AgentSessionScanResult = typeof AgentSessionScanResult.Type;++export const AgentSessionImportInput = Schema.Struct({+ projectId: ProjectId,+ expectedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),+});+export type AgentSessionImportInput = typeof AgentSessionImportInput.Type;++export class AgentSessionImportProjectNotFoundError extends Schema.TaggedErrorClass<AgentSessionImportProjectNotFoundError>()(+ "AgentSessionImportProjectNotFoundError",+ { projectId: ProjectId },+) {+ override get message(): string {+ return `Project '${this.projectId}' does not exist.`;+ }+}++export class AgentSessionImportProjectChangedError extends Schema.TaggedErrorClass<AgentSessionImportProjectChangedError>()(+ "AgentSessionImportProjectChangedError",+ { projectId: ProjectId },+) {+ override get message(): string {+ return `Project '${this.projectId}' changed directories. Scan for projects again before importing history.`;+ }+}++export const AgentSessionImportResult = Schema.Struct({+ importedCount: NonNegativeInt,+ skippedCount: NonNegativeInt,+});+export type AgentSessionImportResult = typeof AgentSessionImportResult.Type;++export class AgentSessionScanError extends Schema.TaggedErrorClass<AgentSessionScanError>()(+ "AgentSessionScanError",+ {+ operation: Schema.Literals(["read-settings", "read-projects"]),+ cause: Schema.Defect(),+ },+) {+ override get message(): string {+ return `Failed to scan agent sessions during ${this.operation}.`;+ }+}diff --git a/packages/contracts/src/index.ts b/packages/contracts/src/index.tsindex 9d7cfd30d62..74a1b4939f1 100644--- a/packages/contracts/src/index.ts+++ b/packages/contracts/src/index.ts@@ -29,6 +29,7 @@ export * from "./t3ProjectFile.ts";export * from "./editor.ts";export * from "./project.ts";export * from "./filesystem.ts";+export * from "./agentSessions.ts";export * from "./assets.ts";export * from "./review.ts";export * from "./browserImport.ts";diff --git a/packages/contracts/src/orchestration.test.ts b/packages/contracts/src/orchestration.test.tsindex d7e33ebb713..11c21deecdd 100644--- a/packages/contracts/src/orchestration.test.ts+++ b/packages/contracts/src/orchestration.test.ts@@ -1135,6 +1135,21 @@ it.effect("project icon overrides accept Lucide icons, colors, and emoji", () =>}),);+it.effect("rejects thread history imports without messages", () =>+ Effect.gen(function* () {+ const result = yield* Effect.exit(+ decodeOrchestrationCommand({+ type: "thread.history.import",+ commandId: "command-empty-history",+ threadId: "thread-1",+ messages: [],+ }),+ );++ assert.strictEqual(result._tag, "Failure");+ }),+);+it("isProviderSendTurnSupportedImageMimeType accepts raster formats and rejects svg", () => {assert.strictEqual(isProviderSendTurnSupportedImageMimeType("image/png"), true);assert.strictEqual(isProviderSendTurnSupportedImageMimeType("IMAGE/JPEG"), true);diff --git a/packages/contracts/src/orchestration.ts b/packages/contracts/src/orchestration.tsindex 17cadc6d1d7..92e9fe01dd4 100644--- a/packages/contracts/src/orchestration.ts+++ b/packages/contracts/src/orchestration.ts@@ -800,6 +800,7 @@ const ThreadCreateCommand = Schema.Struct({branch: Schema.NullOr(TrimmedNonEmptyString),worktreePath: Schema.NullOr(TrimmedNonEmptyString),createdAt: IsoDateTime,+ historyImport: Schema.optional(Schema.Literal(true)),});const ThreadDeleteCommand = Schema.Struct({@@ -1122,6 +1123,20 @@ const ThreadMessageAssistantCompleteCommand = Schema.Struct({createdAt: IsoDateTime,});+const ThreadHistoryImportCommand = Schema.Struct({+ type: Schema.Literal("thread.history.import"),+ commandId: CommandId,+ threadId: ThreadId,+ messages: Schema.Array(+ Schema.Struct({+ messageId: MessageId,+ role: Schema.Literals(["user", "assistant"]),+ text: Schema.String,+ createdAt: IsoDateTime,+ }),+ ).check(Schema.isNonEmpty()),+});+const ThreadProposedPlanUpsertCommand = Schema.Struct({type: Schema.Literal("thread.proposed-plan.upsert"),commandId: CommandId,@@ -1173,6 +1188,7 @@ const InternalOrchestrationCommand = Schema.Union([ThreadSessionSetCommand,ThreadMessageAssistantDeltaCommand,ThreadMessageAssistantCompleteCommand,+ ThreadHistoryImportCommand,ThreadProposedPlanUpsertCommand,ThreadTurnDiffCompleteCommand,ThreadActivityAppendCommand,@@ -1473,6 +1489,7 @@ export const OrchestrationEventMetadata = Schema.Struct({adapterKey: Schema.optional(TrimmedNonEmptyString),requestId: Schema.optional(ApprovalRequestId),ingestedAt: Schema.optional(IsoDateTime),+ historyImport: Schema.optional(Schema.Boolean),origin: Schema.optional(OrchestrationClientOrigin),});export type OrchestrationEventMetadata = typeof OrchestrationEventMetadata.Type;diff --git a/packages/contracts/src/rpc.ts b/packages/contracts/src/rpc.tsindex f7f2c2b6faa..4e8ae2e5413 100644--- a/packages/contracts/src/rpc.ts+++ b/packages/contracts/src/rpc.ts@@ -28,6 +28,15 @@ import {FilesystemBrowseResult,FilesystemBrowseError,} from "./filesystem.ts";+import {+ AgentSessionImportInput,+ AgentSessionImportProjectChangedError,+ AgentSessionImportProjectNotFoundError,+ AgentSessionImportResult,+ AgentSessionScanInput,+ AgentSessionScanResult,+ AgentSessionScanError,+} from "./agentSessions.ts";import {AssetAccessError,AssetCreateUrlInput,@@ -240,6 +249,8 @@ export const WS_METHODS = {// Filesystem methodsfilesystemBrowse: "filesystem.browse",+ agentSessionsScan: "agentSessions.scan",+ agentSessionsImport: "agentSessions.import",assetsCreateUrl: "assets.createUrl",attachmentsCreateUploadUrl: "attachments.createUploadUrl",attachmentsDelete: "attachments.delete",@@ -823,6 +834,23 @@ export const WsFilesystemBrowseRpc = Rpc.make(WS_METHODS.filesystemBrowse, {error: Schema.Union([FilesystemBrowseError, EnvironmentAuthorizationError]),});+export const WsAgentSessionsScanRpc = Rpc.make(WS_METHODS.agentSessionsScan, {+ payload: AgentSessionScanInput,+ success: AgentSessionScanResult,+ error: Schema.Union([AgentSessionScanError, EnvironmentAuthorizationError]),+});++export const WsAgentSessionsImportRpc = Rpc.make(WS_METHODS.agentSessionsImport, {+ payload: AgentSessionImportInput,+ success: AgentSessionImportResult,+ error: Schema.Union([+ AgentSessionImportProjectChangedError,+ AgentSessionImportProjectNotFoundError,+ AgentSessionScanError,+ EnvironmentAuthorizationError,+ ]),+});+export const WsAssetsCreateUrlRpc = Rpc.make(WS_METHODS.assetsCreateUrl, {payload: AssetCreateUrlInput,success: AssetCreateUrlResult,@@ -1241,6 +1269,8 @@ export const WsRpcGroup = RpcGroup.make(WsProjectsWriteFileRpc,WsShellOpenInEditorRpc,WsFilesystemBrowseRpc,+ WsAgentSessionsScanRpc,+ WsAgentSessionsImportRpc,WsAssetsCreateUrlRpc,WsAttachmentsCreateUploadUrlRpc,WsAttachmentsDeleteRpc,diff --git a/packages/contracts/src/server.ts b/packages/contracts/src/server.tsindex ba0d6679cfc..3ea7bed8f1c 100644--- a/packages/contracts/src/server.ts+++ b/packages/contracts/src/server.ts@@ -748,8 +748,11 @@ export const ServerLifecycleWelcomePayload = Schema.Struct({environment: ExecutionEnvironmentDescriptor,cwd: TrimmedNonEmptyString,projectName: TrimmedNonEmptyString,+ bootstrapStatus: Schema.optional(Schema.Literals(["pending", "complete"])),bootstrapProjectId: Schema.optional(ProjectId),bootstrapThreadId: Schema.optional(ThreadId),+ bootstrapProjectCreated: Schema.optional(Schema.Boolean),+ bootstrapThreadCreated: Schema.optional(Schema.Boolean),});export type ServerLifecycleWelcomePayload = typeof ServerLifecycleWelcomePayload.Type;diff --git a/packages/contracts/src/settings.ts b/packages/contracts/src/settings.tsindex 50923423352..623780c1fb8 100644--- a/packages/contracts/src/settings.ts+++ b/packages/contracts/src/settings.ts@@ -286,6 +286,13 @@ export const ClientSettingsSchema = Schema.Struct({// Grayscale `-webkit-font-smoothing: antialiased` (thinner strokes);// disabling restores the platform's heavier default. No effect off macOS.fontSmoothing: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))),+ // When the first-run welcome wizard finished (or was skipped), as an ISO+ // timestamp. `null` alone does not mean "show the wizard" — every install+ // that predates this field decodes to `null` — so the gate also requires an+ // empty workspace before it treats the client as a fresh install.+ onboardingCompletedAt: Schema.NullOr(Schema.String).pipe(+ Schema.withDecodingDefault(Effect.succeed(null)),+ ),// Model favorites. Historically keyed by provider kind, now// widened to `ProviderInstanceId` so users can favorite a specific model// on a custom provider instance (e.g. "Codex Personal · gpt-5") without@@ -1188,6 +1195,7 @@ export const ClientSettingsPatch = Schema.Struct({diffLayout: Schema.optionalKey(DiffLayout),environmentIdentificationMode: Schema.optionalKey(EnvironmentIdentificationMode),glassOpacity: Schema.optionalKey(GlassOpacity),+ onboardingCompletedAt: Schema.optionalKey(Schema.NullOr(Schema.String)),fontSizeInterface: Schema.optionalKey(InterfaceFontSize),fontSizePrompt: Schema.optionalKey(PromptFontSize),fontSizeCode: Schema.optionalKey(CodeFontSize),diff --git a/packages/contracts/src/terminal.test.ts b/packages/contracts/src/terminal.test.tsindex a08ed492388..066253602a4 100644--- a/packages/contracts/src/terminal.test.ts+++ b/packages/contracts/src/terminal.test.ts@@ -7,12 +7,18 @@ import {TerminalClearInput,TerminalCloseInput,TerminalEvent,+ TerminalError,TerminalOpenInput,+ TerminalProviderEnvironmentError,TerminalResizeInput,TerminalSessionSnapshot,TerminalThreadInput,TerminalWriteInput,} from "./terminal.ts";+import { ProviderInstanceId } from "./providerInstance.ts";++const encodeTerminalError = Schema.encodeUnknownSync(TerminalError);+const decodeTerminalError = Schema.decodeUnknownSync(TerminalError);function decodeSync<S extends Schema.Top>(schema: S, input: unknown): Schema.Schema.Type<S> {return Schema.decodeUnknownSync(schema as never)(input) as Schema.Schema.Type<S>;@@ -27,6 +33,28 @@ function decodes<S extends Schema.Top>(schema: S, input: unknown): boolean {}}+describe("TerminalProviderEnvironmentError", () => {+ it("round-trips its required cause without exposing it in the message", () => {+ const cause = { operation: "read-secret", detail: "secret backend unavailable" };+ const error = new TerminalProviderEnvironmentError({+ providerInstanceId: ProviderInstanceId.make("codex_work"),+ cause,+ });+ const encoded = encodeTerminalError(error);+ const decoded = decodeTerminalError(encoded);++ expect(decoded).toMatchObject({+ _tag: "TerminalProviderEnvironmentError",+ providerInstanceId: "codex_work",+ cause,+ });+ expect(decoded.message).toBe(+ "Could not prepare the terminal environment for provider instance: codex_work",+ );+ expect(decoded.message).not.toContain("secret backend unavailable");+ });+});+describe("TerminalOpenInput", () => {it("accepts valid open input", () => {expect(@@ -87,12 +115,14 @@ describe("TerminalOpenInput", () => {T3CODE_PROJECT_ROOT: "/tmp/project",CUSTOM_FLAG: "1",},+ providerInstanceId: "codex_work",});expect(parsed.env).toMatchObject({T3CODE_PROJECT_ROOT: "/tmp/project",CUSTOM_FLAG: "1",});expect(parsed.worktreePath).toBe("/tmp/project/.t3/worktrees/feature-a");+ expect(parsed.providerInstanceId).toBe("codex_work");});it("rejects invalid env keys", () => {@@ -108,6 +138,19 @@ describe("TerminalOpenInput", () => {}),).toBe(false);});++ it("rejects invalid provider instance ids", () => {+ for (const providerInstanceId of ["", "1invalid", "invalid id"]) {+ expect(+ decodes(TerminalOpenInput, {+ threadId: "thread-1",+ terminalId: DEFAULT_TERMINAL_ID,+ cwd: "/tmp/project",+ providerInstanceId,+ }),+ ).toBe(false);+ }+ });});describe("TerminalAttachInput", () => {diff --git a/packages/contracts/src/terminal.ts b/packages/contracts/src/terminal.tsindex fa5f1821169..36e3d339f52 100644--- a/packages/contracts/src/terminal.ts+++ b/packages/contracts/src/terminal.ts@@ -1,5 +1,6 @@import * as Schema from "effect/Schema";import { TrimmedNonEmptyString } from "./baseSchemas.ts";+import { ProviderInstanceId } from "./providerInstance.ts";/*** Client-side id for the first shell opened on a thread. Ids are uniformly@@ -43,8 +44,9 @@ export const TerminalOpenInput = Schema.Struct({cols: Schema.optional(TerminalColsSchema),rows: Schema.optional(TerminalRowsSchema),env: Schema.optional(TerminalEnvSchema),+ providerInstanceId: Schema.optional(ProviderInstanceId),});-export type TerminalOpenInput = Schema.Codec.Encoded<typeof TerminalOpenInput>;+export type TerminalOpenInput = typeof TerminalOpenInput.Type;export const TerminalAttachInput = Schema.Struct({...TerminalSessionInput.fields,@@ -53,9 +55,10 @@ export const TerminalAttachInput = Schema.Struct({cols: Schema.optional(TerminalColsSchema),rows: Schema.optional(TerminalRowsSchema),env: Schema.optional(TerminalEnvSchema),+ providerInstanceId: Schema.optional(ProviderInstanceId),restartIfNotRunning: Schema.optional(Schema.Boolean),});-export type TerminalAttachInput = Schema.Codec.Encoded<typeof TerminalAttachInput>;+export type TerminalAttachInput = typeof TerminalAttachInput.Type;export const TerminalWriteInput = Schema.Struct({...TerminalSessionInput.fields,@@ -80,8 +83,9 @@ export const TerminalRestartInput = Schema.Struct({cols: TerminalColsSchema,rows: TerminalRowsSchema,env: Schema.optional(TerminalEnvSchema),+ providerInstanceId: Schema.optional(ProviderInstanceId),});-export type TerminalRestartInput = Schema.Codec.Encoded<typeof TerminalRestartInput>;+export type TerminalRestartInput = typeof TerminalRestartInput.Type;export const TerminalCloseInput = Schema.Struct({...TerminalThreadInput.fields,@@ -299,6 +303,29 @@ export class TerminalSessionLookupError extends Schema.TaggedErrorClass<Terminal}}+export class TerminalProviderInstanceNotFoundError extends Schema.TaggedErrorClass<TerminalProviderInstanceNotFoundError>()(+ "TerminalProviderInstanceNotFoundError",+ {+ providerInstanceId: ProviderInstanceId,+ },+) {+ override get message() {+ return `Provider instance is not available: ${this.providerInstanceId}`;+ }+}++export class TerminalProviderEnvironmentError extends Schema.TaggedErrorClass<TerminalProviderEnvironmentError>()(+ "TerminalProviderEnvironmentError",+ {+ providerInstanceId: ProviderInstanceId,+ cause: Schema.Defect(),+ },+) {+ override get message() {+ return `Could not prepare the terminal environment for provider instance: ${this.providerInstanceId}`;+ }+}+export class TerminalNotRunningError extends Schema.TaggedErrorClass<TerminalNotRunningError>()("TerminalNotRunningError",{@@ -345,6 +372,8 @@ export const TerminalError = Schema.Union([TerminalCwdError,TerminalHistoryError,TerminalSessionLookupError,+ TerminalProviderInstanceNotFoundError,+ TerminalProviderEnvironmentError,TerminalNotRunningError,TerminalWriteError,TerminalResizeError,diff --git a/packages/shared/package.json b/packages/shared/package.jsonindex fd932b8b146..d6915120b80 100644--- a/packages/shared/package.json+++ b/packages/shared/package.json@@ -270,6 +270,10 @@"./hostClassification": {"types": "./src/hostClassification.ts","import": "./src/hostClassification.ts"+ },+ "./dateTime": {+ "types": "./src/dateTime.ts",+ "import": "./src/dateTime.ts"}},"scripts": {diff --git a/packages/shared/src/dateTime.test.ts b/packages/shared/src/dateTime.test.tsnew file mode 100644index 00000000000..562507de3ac--- /dev/null+++ b/packages/shared/src/dateTime.test.ts@@ -0,0 +1,92 @@+import { describe, expect, it } from "@effect/vitest";++import { compareDateTimeStrings } from "./dateTime.ts";++describe("compareDateTimeStrings", () => {+ it("compares valid date-time strings by absolute time", () => {+ expect(+ compareDateTimeStrings("2026-09-01T12:00:00.000Z", "2026-09-01T05:00:00.000-07:00"),+ ).toBe(0);+ expect(+ compareDateTimeStrings("2026-09-01T12:00:01.000Z", "2026-09-01T12:00:00.000Z"),+ ).toBeGreaterThan(0);+ });++ it.each([+ ["2024-02-29T12:00:00Z", "2024-02-29T17:30:00+05:30"],+ ["2000-02-29T00:00:00.100Z", "2000-02-28T20:30:00.1-03:30"],+ ["0000-01-01T00:00:00.000Z", "+000000-01-01T00:00:00.000+00:00"],+ ["+010000-01-01T00:00:00.000Z", "9999-12-31T23:00:00.000-01:00"],+ ["2026-09-01T24:00:00Z", "2026-09-02T00:00:00Z"],+ ["2026-09-01T24:00:00.0000Z", "2026-09-02T00:00:00.000Z"],+ ["2024-02-29T24:00:00+05:30", "2024-03-01T00:00:00+05:30"],+ ["2026-12-31T24:00:00-07:00", "2027-01-01T07:00:00Z"],+ ["2026-09-01T12:00Z", "2026-09-01T12:00:00.000Z"],+ ["2026-09-01T05:00-07:00", "2026-09-01T12:00:00Z"],+ ["2026-09-01T24:00Z", "2026-09-02T00:00:00Z"],+ ["2026-09-01T24:00+05:30", "2026-09-02T00:00:00+05:30"],+ ])("preserves equal ISO instants %s and %s", (left, right) => {+ expect(compareDateTimeStrings(left, right)).toBe(0);+ });++ it("sorts malformed values before valid values", () => {+ expect(compareDateTimeStrings("invalid", "2026-09-01T12:00:00.000Z")).toBeLessThan(0);+ expect(compareDateTimeStrings("2026-09-01T12:00:00.000Z", "invalid")).toBeGreaterThan(0);+ });++ it.each([+ "2014-02-30",+ "2014-03-02",+ "2014-03-02T00:00:00",+ "2014-03-02T00:00:00.000",+ "03/02/2014",+ "March 2, 2014",+ "Sun, 02 Mar 2014 00:00:00 GMT",+ "2014-03-02T00:00:00.000Z\n",+ "2014-02-30T00:00:00.000Z",+ "1900-02-29T00:00:00.000-07:00",+ "2024-04-31T00:00:00.000+05:30",+ "2024-03-02T12:00:00.000+24:00",+ "2026-09-01T24:01:00Z",+ "2026-09-01T24:00:01Z",+ "2026-09-01T24:00:00.0001Z",+ "2026-09-01T24:01Z",+ "2026-09-01T25:00Z",+ ])("treats %s as malformed without native date guessing", (malformed) => {+ const valid = "1970-01-01T00:00:00.000Z";+ expect(compareDateTimeStrings(malformed, valid)).toBeLessThan(0);+ expect(compareDateTimeStrings(valid, malformed)).toBeGreaterThan(0);+ expect(compareDateTimeStrings(malformed, "invalid")).toBeLessThan(0);+ expect(compareDateTimeStrings("invalid", malformed)).toBeGreaterThan(0);+ expect(compareDateTimeStrings(malformed, malformed)).toBe(0);+ });++ it("uses code-unit order for malformed date-time strings", () => {+ expect(compareDateTimeStrings("invalid-a", "invalid-B")).toBeGreaterThan(0);+ expect(compareDateTimeStrings("invalid-B", "invalid-a")).toBeLessThan(0);+ });++ it("returns zero for equal malformed date-time strings", () => {+ expect(compareDateTimeStrings("invalid", "invalid")).toBe(0);+ });++ it("gives every permutation of mixed values the same order", () => {+ const early = "2026-09-01T12:00:00.000+14:00";+ const late = "2026-09-01T00:00:00.000-12:00";+ const malformed = "2026-09-01T06:invalid";+ const expected = [malformed, early, late];++ const permutations = [+ [early, late, malformed],+ [early, malformed, late],+ [late, early, malformed],+ [late, malformed, early],+ [malformed, early, late],+ [malformed, late, early],+ ];++ for (const values of permutations) {+ expect(values.toSorted(compareDateTimeStrings)).toEqual(expected);+ }+ });+});diff --git a/packages/shared/src/dateTime.ts b/packages/shared/src/dateTime.tsnew file mode 100644index 00000000000..544dd2d0d73--- /dev/null+++ b/packages/shared/src/dateTime.ts@@ -0,0 +1,38 @@+import * as DateTime from "effect/DateTime";+import * as Option from "effect/Option";+import * as Schema from "effect/Schema";++const isZonedIsoDateTime = Schema.is(+ Schema.String.check(+ Schema.isPattern(+ /^(?:\d{4}|[+-]\d{6})-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])T(?:(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d(?:\.\d+)?)?|24:00(?::00(?:\.0+)?)?)(?:Z|[+-](?:[01]\d|2[0-3]):[0-5]\d)$/,+ ),+ Schema.isTrimmed(),+ ),+);++function parseTimestamp(value: string): number {+ if (!isZonedIsoDateTime(value)) return Number.NaN;++ // Engines can normalize invalid calendar dates instead of rejecting them.+ const datePart = value.slice(0, value.indexOf("T"));+ const date = DateTime.make(`${datePart}T00:00:00.000Z`);+ if (Option.isNone(date)) return Number.NaN;+ const parts = DateTime.toPartsUtc(date.value);+ if (parts.month !== Number(datePart.slice(-5, -3)) || parts.day !== Number(datePart.slice(-2))) {+ return Number.NaN;+ }+ return Date.parse(value);+}++/** Compare date-time strings by absolute time, with stable handling for malformed stored values. */+export function compareDateTimeStrings(left: string, right: string): number {+ const leftTimestamp = parseTimestamp(left);+ const rightTimestamp = parseTimestamp(right);+ const leftIsValid = !Number.isNaN(leftTimestamp);+ const rightIsValid = !Number.isNaN(rightTimestamp);++ if (leftIsValid !== rightIsValid) return leftIsValid ? 1 : -1;+ if (leftIsValid) return leftTimestamp - rightTimestamp;+ return left < right ? -1 : left > right ? 1 : 0;+}