diff --git a/apps/server/src/environment/ServerEnvironment.ts b/apps/server/src/environment/ServerEnvironment.ts index 9e29b0fdedc95..2fe81298ac8f0 100644 --- a/apps/server/src/environment/ServerEnvironment.ts +++ b/apps/server/src/environment/ServerEnvironment.ts @@ -234,6 +234,7 @@ export const make = Effect.gen(function* () { threadAutoSettlement: true, storageCleanup: true, storageCleanupRun: true, + agentSessionImportSettings: true, databaseStorage: true, otlpEndpointCheck: true, projectWorktreeCleanup: true, diff --git a/apps/server/src/project/AgentSessionScanner.test.ts b/apps/server/src/project/AgentSessionScanner.test.ts index f672768e7ac58..27646f3c06542 100644 --- a/apps/server/src/project/AgentSessionScanner.test.ts +++ b/apps/server/src/project/AgentSessionScanner.test.ts @@ -59,6 +59,7 @@ interface ScannerTestInput { readonly importedWorkspaceRoots?: ReadonlyArray; readonly configBaseDir?: string; readonly providerInstances?: ContractServerSettings["providerInstances"]; + readonly agentSessionImport?: Partial; readonly processRunner?: Layer.Layer; } @@ -69,9 +70,10 @@ const layerScannerTest = (input: ScannerTestInput) => Layer.provide(input.processRunner), ) ).pipe( - Layer.provide( + Layer.provideMerge( Layer.mergeAll( ServerSettings.layerTest({ + agentSessionImport: input.agentSessionImport ?? {}, providerInstances: { [ProviderInstanceId.make("claudeAgent")]: { driver: ProviderDriverKind.make("claudeAgent"), @@ -182,6 +184,80 @@ function makeRecordLimitTranscript(cwd: string, overflow: boolean): string { : records; } +const writeCodexRollout = Effect.fn("AgentSessionScanner.test.writeCodexRollout")( + function* (input: { + readonly codexHomePath: string; + readonly name: string; + readonly workspace: string; + readonly mtimeMs: number; + readonly replies?: ReadonlyArray; + readonly blankRecords?: number; + }) { + const path = yield* Path.Path; + yield* writeTranscript({ + filePath: path.join( + input.codexHomePath, + "sessions", + "2026", + "08", + "24", + `rollout-${input.name}.jsonl`, + ), + contents: [ + encodeTranscriptRecord({ + type: "session_meta", + payload: { id: input.name, cwd: input.workspace }, + }), + encodeTranscriptRecord({ + type: "event_msg", + payload: { type: "user_message", message: "First prompt" }, + }), + ...(input.replies ?? []).map((text) => + encodeTranscriptRecord({ + type: "response_item", + payload: { + type: "message", + role: "assistant", + content: [{ type: "output_text", text }], + }, + }), + ), + ...Array.from({ length: input.blankRecords ?? 0 }, () => "{}"), + ].join("\n"), + mtimeMs: input.mtimeMs, + }); + }, +); + +// Each cwd sits behind ~300 KiB of padding, so a 1 MiB metadata budget reads three of the four. +const writePaddedClaudeProjects = Effect.fn("AgentSessionScanner.test.writePaddedClaudeProjects")( + function* (claudeHomePath: string, nowMs: number) { + const path = yield* Path.Path; + const workspaces: Array = []; + for (const index of [0, 1, 2, 3]) { + const workspace = yield* makeTempDir(`t3code-padded-workspace-${index}-`); + workspaces.push(workspace); + yield* writeTranscript({ + filePath: path.join(claudeHomePath, "projects", `-padded-${index}`, "session.jsonl"), + contents: [ + encodeTranscriptRecord({ + type: "file-history-snapshot", + padding: "x".repeat(300 * 1024), + }), + encodeTranscriptRecord({ + type: "user", + cwd: workspace, + sessionId: `padded-${index}`, + message: { role: "user", content: "Fix the project" }, + }), + ].join("\n"), + mtimeMs: nowMs - index * 60_000, + }); + } + return workspaces; + }, +); + const makeFileIdentityFixture = Effect.fn("AgentSessionScanner.test.makeFileIdentityFixture")( function* (originalInode = 10_000_000_000_000_001n, replacementInode = 10_000_000_000_000_000n) { const path = yield* Path.Path; @@ -1120,6 +1196,65 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { ]); }), ); + + it.effect("stops reading transcript metadata at the configured scan budget", () => + Effect.gen(function* () { + 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 workspaces = yield* writePaddedClaudeProjects(claudeHomePath, nowMs); + + const limited = yield* runScan({ + claudeHomePath, + codexHomePath, + agentSessionImport: { scanBudgetMiB: 1 }, + }); + const unlimited = yield* runScan({ claudeHomePath, codexHomePath }); + + expect(limited.candidates.map((candidate) => candidate.path)).toEqual( + workspaces.slice(0, 3), + ); + expect(limited.truncated).toBe(true); + expect(unlimited.candidates.map((candidate) => candidate.path)).toEqual(workspaces); + expect(unlimited.truncated).toBeUndefined(); + }), + ); + + it.effect("raises the metadata record budget in step with the configured scan budget", () => + 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-records-claude-"); + const codexHomePath = yield* makeTempDir("t3code-records-codex-"); + const workspaces: Array = []; + // 101 transcripts reading 999 records each overrun the default 100,000-record budget. + for (let index = 0; index <= 100; index++) { + const workspace = yield* makeTempDir(`t3code-records-workspace-${index}-`); + workspaces.push(workspace); + yield* writeTranscript({ + filePath: path.join(claudeHomePath, "projects", `-records-${index}`, "session.jsonl"), + contents: `${"{}\n".repeat(998)}${claudeSessionLine(workspace)}`, + mtimeMs: nowMs - index * 60_000, + }); + } + + const defaults = yield* runScan({ claudeHomePath, codexHomePath }); + const raised = yield* runScan({ + claudeHomePath, + codexHomePath, + agentSessionImport: { scanBudgetMiB: 256 }, + }); + + expect(defaults.candidates.map((candidate) => candidate.path)).toEqual( + workspaces.slice(0, 100), + ); + expect(defaults.truncated).toBe(true); + expect(raised.candidates.map((candidate) => candidate.path)).toEqual(workspaces); + expect(raised.truncated).toBeUndefined(); + }), + ); }); describe("recentThreads", () => { @@ -1151,13 +1286,23 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { yield* writeTranscript({ filePath: path.join(claudeHomePath, "projects", "-selected", "claude-recent.jsonl"), contents: claudeTranscript(workspace, "claude-recent"), - mtimeMs: nowMs - 24 * 60 * 60 * 1000, + mtimeMs: nowMs - 36 * 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", "-selected", "claude-ancient.jsonl"), + contents: claudeTranscript(workspace, "claude-ancient"), + mtimeMs: nowMs - 400 * 24 * 60 * 60 * 1000, + }); + yield* writeTranscript({ + filePath: path.join(claudeHomePath, "projects", "-selected", "claude-future.jsonl"), + contents: claudeTranscript(workspace, "claude-future"), + mtimeMs: nowMs + 24 * 60 * 60 * 1000, + }); yield* writeTranscript({ filePath: path.join(claudeHomePath, "projects", "-other", "claude-other.jsonl"), contents: claudeTranscript(otherWorkspace, "claude-other"), @@ -1284,6 +1429,28 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { ["Legacy prompt", "Legacy answer"], ]); expect(threads[0]).toMatchObject({ source: "pi", title: "Pi refactor" }); + + // A future-dated transcript is never imported, even with unlimited history. + for (const [agentSessionImport, expected] of [ + [ + { windowDays: 45 }, + ["pi-recent", "codex-recent", "claude-recent", "pi-v1", "claude-old"], + ], + [{ windowDays: 1 }, ["pi-recent", "codex-recent"]], + [ + { windowDays: null }, + ["pi-recent", "codex-recent", "claude-recent", "pi-v1", "claude-old", "claude-ancient"], + ], + ] as const) { + const configured = yield* runRecentThreads({ + claudeHomePath, + codexHomePath, + piAgentDir, + workspaceRoot: workspace, + agentSessionImport, + }); + expect(configured.map((thread) => thread.providerSessionId)).toEqual(expected); + } }), ); @@ -1541,6 +1708,7 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { }), prompt("Keep the first request"), reply("First reply"), + ...Array.from({ length: 1_000 }, () => "{}"), ] .map((record) => `${record}\n`) .join(""); @@ -1575,25 +1743,35 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { mtimeMs: nowMs, }); - const threads = yield* runRecentThreads({ - claudeHomePath, - codexHomePath, - workspaceRoot: workspace, - }); + // The inherited prefix has 1,003 records, so a 1,000-record limit imports the rollout alone. + for (const [agentSessionImport, expected] of [ + [ + {}, + { + title: "Keep the first request", + messages: ["Keep the first request", "First reply", "Ask again", "Final reply"], + }, + ], + [ + { recordsPerConversation: 1_000 }, + { title: "Ask again", messages: ["Ask again", "Final reply"] }, + ], + ] as const) { + const threads = yield* runRecentThreads({ + claudeHomePath, + codexHomePath, + workspaceRoot: workspace, + agentSessionImport, + }); - expect( - threads.map((thread) => ({ - id: thread.providerSessionId, - title: thread.title, - messages: thread.messages.map((message) => message.text), - })), - ).toEqual([ - { - id: threadId, - title: "Keep the first request", - messages: ["Keep the first request", "First reply", "Ask again", "Final reply"], - }, - ]); + expect( + threads.map((thread) => ({ + id: thread.providerSessionId, + title: thread.title, + messages: thread.messages.map((message) => message.text), + })), + ).toEqual([{ id: threadId, ...expected }]); + } }), ); @@ -1729,15 +1907,103 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { mtimeMs, }); } - const outcomes = yield* runRecentThreadOutcomes({ - claudeHomePath, + for (const [agentSessionImport, expected] of [ + [{}, ["Skipped", "Importable"]], + [{ recordsPerConversation: 100_001 }, ["Importable", "Importable"]], + ] as const) { + const outcomes = yield* runRecentThreadOutcomes({ + claudeHomePath, + codexHomePath, + workspaceRoot: workspace, + agentSessionImport, + }); + expect(outcomes.map((outcome) => outcome._tag)).toEqual(expected); + expect(outcomes[1]).toMatchObject({ + thread: { providerSessionId: "record-limit-session" }, + }); + } + }), + ); + + it.effect("stops importing at the configured conversation and record limits", () => + Effect.gen(function* () { + const nowMs = Date.parse("2026-08-24T12:00:00.000Z"); + yield* TestClock.setTime(nowMs); + const claudeHomePath = yield* makeTempDir("t3code-limits-claude-"); + const codexHomePath = yield* makeTempDir("t3code-limits-codex-"); + const workspace = yield* makeTempDir("t3code-limits-workspace-"); + const rollouts = [ + ["newest", 500], + ["middle", 1_500], + ["oldest", 500], + ] as const; + for (const [index, [name, blankRecords]] of rollouts.entries()) { + yield* writeCodexRollout({ + codexHomePath, + name, + workspace, + mtimeMs: nowMs - (index + 1) * 60_000, + blankRecords, + }); + } + + for (const [agentSessionImport, expected] of [ + [{}, ["newest", "middle", "oldest"]], + [{ conversationsPerRun: 2 }, ["newest", "middle", "Skipped"]], + // The cap applies to each transcript, so the oldest still fits after a skipped one. + [{ recordsPerConversation: 1_000 }, ["newest", "Skipped", "oldest"]], + ] as const) { + const outcomes = yield* runRecentThreadOutcomes({ + claudeHomePath, + codexHomePath, + workspaceRoot: workspace, + agentSessionImport, + }); + expect( + outcomes.map((outcome) => + outcome._tag === "Importable" ? outcome.thread.providerSessionId : outcome._tag, + ), + ).toEqual(expected); + } + }), + ); + + it.effect("keeps the first prompt and the latest messages up to the configured limit", () => + Effect.gen(function* () { + const nowMs = Date.parse("2026-08-24T12:00:00.000Z"); + yield* TestClock.setTime(nowMs); + const claudeHomePath = yield* makeTempDir("t3code-messages-claude-"); + const codexHomePath = yield* makeTempDir("t3code-messages-codex-"); + const workspace = yield* makeTempDir("t3code-messages-workspace-"); + const replies = Array.from({ length: 249 }, (_, index) => `Reply ${index + 1}`); + const all = ["First prompt", ...replies]; + yield* writeCodexRollout({ codexHomePath, - workspaceRoot: workspace, - }); - expect(outcomes.map((outcome) => outcome._tag)).toEqual(["Skipped", "Importable"]); - expect(outcomes[1]).toMatchObject({ - thread: { providerSessionId: "record-limit-session" }, + name: "long", + workspace, + mtimeMs: nowMs, + replies, }); + + for (const [agentSessionImport, expected, omitted] of [ + [{}, [all[0], ...all.slice(-199)], 50], + [{ messagesPerThread: 4 }, [all[0], ...all.slice(-3)], 246], + [{ messagesPerThread: 2 }, [all[0], all.at(-1)], 248], + [{ messagesPerThread: null }, all, 0], + ] as const) { + const threads = yield* runRecentThreads({ + claudeHomePath, + codexHomePath, + workspaceRoot: workspace, + agentSessionImport, + }); + expect( + threads.map((thread) => [ + thread.messages.map((message) => message.text), + thread.omittedMessageCount, + ]), + ).toEqual([[expected, omitted]]); + } }), ); @@ -2034,6 +2300,77 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { }), ); + it.effect("charges retained Pi tool-result links against the selected-history budget", () => + 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-pi-links-claude-"); + const codexHomePath = yield* makeTempDir("t3code-pi-links-codex-"); + const piAgentDir = yield* makeTempDir("t3code-pi-links-agent-"); + const workspace = yield* makeTempDir("t3code-pi-links-workspace-"); + const linkId = (index: number) => String(index).padStart(1_000, "0"); + // Tool results keep only their links, but those still count against the 32 MiB budget. + const makeTranscript = (sessionId: string, toolResultsPerKind: number) => { + const toolResult = (index: number, content: unknown) => + encodeTranscriptRecord({ + type: "message", + id: linkId(index), + parentId: index === 1 ? "prompt" : linkId(index - 1), + message: { role: "toolResult", content }, + }); + return [ + encodeTranscriptRecord({ type: "session", version: 3, id: sessionId, cwd: workspace }), + encodeTranscriptRecord({ + type: "message", + id: "prompt", + parentId: null, + message: { role: "user", content: "Run the tools" }, + }), + // Null content fails the record schema, so those links take the rescue path. + ...Array.from({ length: toolResultsPerKind }, (_, index) => + toolResult(index + 1, [{ type: "text", text: "output" }]), + ), + ...Array.from({ length: toolResultsPerKind }, (_, index) => + toolResult(toolResultsPerKind + index + 1, null), + ), + encodeTranscriptRecord({ + type: "message", + id: "answer", + parentId: linkId(2 * toolResultsPerKind), + message: { role: "assistant", content: [{ type: "text", text: "Done" }] }, + }), + ].join("\n"); + }; + // Each kind alone stays within the budget; together they exceed it. + for (const [sessionId, toolResultsPerKind, mtimeMs] of [ + ["pi-links-over", 5_500, nowMs], + ["pi-links-under", 500, nowMs - 1_000], + ] as const) { + yield* writeTranscript({ + filePath: path.join(piAgentDir, "sessions", "--links--", `${sessionId}.jsonl`), + contents: makeTranscript(sessionId, toolResultsPerKind), + mtimeMs, + }); + } + + const outcomes = yield* runRecentThreadOutcomes({ + claudeHomePath, + codexHomePath, + piAgentDir, + workspaceRoot: workspace, + }); + + expect(outcomes.map((outcome) => outcome._tag)).toEqual(["Skipped", "Importable"]); + expect(outcomes[1]).toMatchObject({ + thread: { + providerSessionId: "pi-links-under", + messages: [{ text: "Run the tools" }, { text: "Done" }], + }, + }); + }), + ); + it.effect("skips growth during reading without exceeding the reserved bytes", () => Effect.gen(function* () { const path = yield* Path.Path; @@ -2149,6 +2486,41 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { expect(threads.map((thread) => thread.providerInstanceId)).toEqual(["codex-work"]); }), ); + + it.effect("recollects candidates when the scan budget is raised after a scan", () => + Effect.gen(function* () { + const nowMs = Date.parse("2026-08-24T12:00:00.000Z"); + yield* TestClock.setTime(nowMs); + const claudeHomePath = yield* makeTempDir("t3code-recollect-claude-"); + const codexHomePath = yield* makeTempDir("t3code-recollect-codex-"); + const workspaces = yield* writePaddedClaudeProjects(claudeHomePath, nowMs); + const missedWorkspace = workspaces[3]!; + + yield* Effect.gen(function* () { + const scanner = yield* AgentSessionScanner.AgentSessionScanner; + const settings = yield* ServerSettings.ServerSettingsService; + const scan = yield* scanner.scan; + const outcomeTags = scanner.recentThreads(missedWorkspace).pipe( + Stream.runCollect, + Effect.map((outcomes) => outcomes.map((outcome) => outcome._tag)), + ); + + expect(scan.candidates).toHaveLength(3); + expect(yield* outcomeTags).toEqual([]); + + yield* settings.updateSettings({ agentSessionImport: { scanBudgetMiB: 64 } }); + expect(yield* outcomeTags).toEqual(["Importable"]); + }).pipe( + Effect.provide( + layerScannerTest({ + claudeHomePath, + codexHomePath, + agentSessionImport: { scanBudgetMiB: 1 }, + }), + ), + ); + }), + ); }); }); diff --git a/apps/server/src/project/AgentSessionScanner.ts b/apps/server/src/project/AgentSessionScanner.ts index 1b32de22a8878..48d961b984292 100644 --- a/apps/server/src/project/AgentSessionScanner.ts +++ b/apps/server/src/project/AgentSessionScanner.ts @@ -5,6 +5,7 @@ import { AgentSessionScanError, ClaudeSettings, CodexSettings, + DEFAULT_SERVER_SETTINGS, ProviderDriverKind, ProviderInstanceId, resolveProviderInstanceEnabled, @@ -13,6 +14,7 @@ import { type AgentSessionProjectGit, type AgentSessionScanResult, type ProviderInstanceConfig, + type ServerSettings as ContractServerSettings, } from "@t3tools/contracts"; import * as Clock from "effect/Clock"; import * as Context from "effect/Context"; @@ -73,18 +75,15 @@ const MAX_CODEX_PROJECT_ROOT_ROWS = 100_000; const MAX_TRANSCRIPTS_PER_SOURCE = 5000; 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 DAY_MS = 24 * 60 * 60 * 1000; +const DEFAULT_IMPORT_SETTINGS = DEFAULT_SERVER_SETTINGS.agentSessionImport; // Screenshot-heavy transcripts can reach GiB; select fields while streaming to avoid allocating payloads. const MAX_IMPORTED_TRANSCRIPT_BYTES = 4 * 1024 * 1024 * 1024; -const MAX_IMPORTED_MESSAGES = 200; const MAX_IMPORT_HISTORY_BYTES = 32 * 1024 * 1024; const MAX_IMPORT_BYTES = 4 * 1024 * 1024 * 1024; -const MAX_IMPORT_TRANSCRIPTS = 100; -const MAX_IMPORT_RECORDS = 100_000; /** * open() of a file under macOS privacy protection waits on a consent prompt @@ -156,13 +155,15 @@ const decodeClaudeSettings = Schema.decodeUnknownOption(ClaudeSettings); const decodeCodexSettings = Schema.decodeUnknownOption(CodexSettings); const decodeTranscriptRecord = Schema.decodeUnknownOption(Schema.fromJsonString(TranscriptRecord)); const decodeTranscriptValue = Schema.decodeUnknownOption(TranscriptRecord); -const decodePiEntryLinks = Schema.decodeUnknownOption( - Schema.Struct({ - type: TranscriptRecord.fields.type, - id: TranscriptRecord.fields.id, - parentId: TranscriptRecord.fields.parentId, - }), -); +const PiEntryLinks = Schema.Struct({ + type: TranscriptRecord.fields.type, + id: TranscriptRecord.fields.id, + parentId: TranscriptRecord.fields.parentId, +}); +const decodePiEntryLinks = Schema.decodeUnknownOption(PiEntryLinks); +// Link-only records skip the reader's reservations; charge a token each for the record and its links. +const piLinkBytes = ({ type, id, parentId }: typeof PiEntryLinks.Type) => + 4 * 64 + 2 * ((type?.length ?? 0) + (id?.length ?? 0) + (parentId?.length ?? 0)); const selectTranscriptPath = createTranscriptJsonSelector(TranscriptRecord); const decodeCodexTurnMetadata = Schema.decodeUnknownOption(CodexTurnMetadata); const decodeCodexHistoryBase = Schema.decodeUnknownOption( @@ -423,21 +424,30 @@ export function parseAgentSessionTranscript( input: AgentSessionTranscriptMetadata & { readonly contents: string; }, - lines = splitTranscriptRecords(input.contents, MAX_IMPORT_RECORDS + 1), + lines = splitTranscriptRecords( + input.contents, + DEFAULT_IMPORT_SETTINGS.recordsPerConversation + 1, + ), ): AgentSessionThread | null { - if (lines.length > MAX_IMPORT_RECORDS) return null; + if (lines.length > DEFAULT_IMPORT_SETTINGS.recordsPerConversation) return null; const records = lines.flatMap((line) => Option.toArray(decodeTranscriptRecord(line))); const claudeFields = input.source === "claudeAgent" ? records.reduce(foldClaudeFields, emptyClaudeFields) : emptyClaudeFields; - return parseAgentSessionRecords(input, records, claudeFields); + return parseAgentSessionRecords( + input, + records, + claudeFields, + DEFAULT_IMPORT_SETTINGS.messagesPerThread ?? Infinity, + ); } function parseAgentSessionRecords( input: AgentSessionTranscriptMetadata, records: ReadonlyArray, claudeFields: ClaudeFields, + maxMessages: number, ): AgentSessionThread | null { const fallbackTimestamp = DateTime.formatIso(DateTime.makeUnsafe(input.lastActiveAtMs)); // Claude filenames are session IDs. Codex rollout filenames include extra @@ -525,7 +535,7 @@ function parseAgentSessionRecords( } conversationMessageCount += 1; messages.push(message); - if (messages.length > MAX_IMPORTED_MESSAGES) messages.shift(); + if (messages.length > maxMessages) messages.shift(); }; const hasMatchingCodexEventInTurn = (text: string) => { @@ -639,7 +649,7 @@ function parseAgentSessionRecords( const { codexResponseUser: _codexResponseUser, ...visibleFirstUserMessage } = firstUserMessage; const retainedMessages = firstUserMessageRetained ? visibleMessages - : [visibleFirstUserMessage, ...visibleMessages.slice(-(MAX_IMPORTED_MESSAGES - 1))]; + : [visibleFirstUserMessage, ...visibleMessages.slice(-(maxMessages - 1))]; return { source: input.source, @@ -1043,6 +1053,7 @@ export const make = Effect.gen(function* () { filePath: string, expected: ReturnType, source: AgentSessionSource, + maxRecords: number, prefix?: { readonly endByteOffset: number; readonly historyBytes: number }, ) { const readBytes = prefix?.endByteOffset ?? expected.size; @@ -1062,27 +1073,39 @@ export const make = Effect.gen(function* () { let recordBytes = 0; let recordCount = 0; let bytesRead = 0; - const reserve = (bytes: number) => { - recordBytes += bytes; - if (historyBytes + recordBytes > MAX_IMPORT_HISTORY_BYTES) { + const assertHistoryFits = (bytes: number) => { + if (historyBytes + bytes > MAX_IMPORT_HISTORY_BYTES) { throw new TranscriptJsonLimitError({ message: "Transcript selected history exceeds the 32 MiB memory budget", }); } }; + const reserve = (bytes: number) => { + recordBytes += bytes; + assertHistoryFits(recordBytes); + }; let reader = createTranscriptJsonReader(reserve, selectTranscriptPath); let decoder = new TextDecoder(); let recordStarted = false; + const retainPiLinks = (value: unknown) => { + const links = decodePiEntryLinks(value); + if (Option.isNone(links)) return; + const bytes = piLinkBytes(links.value); + assertHistoryFits(bytes); + historyBytes += bytes; + records.push(links.value); + }; + const finishRecord = () => { reader.write(decoder.decode()); recordCount += 1; - if (recordCount > MAX_IMPORT_RECORDS) return false; + if (recordCount > maxRecords) return false; const value = reader.finish(); const decoded = decodeTranscriptValue(value); if (Option.isNone(decoded) && source === "pi") { // Pi accepts messages this schema rejects, such as null content; keep their tree links. - records.push(...Option.toArray(decodePiEntryLinks(value))); + retainPiLinks(value); } else if (Option.isSome(decoded)) { if (Option.isNone(cwd)) cwd = Option.fromNullOr(extractDecodedCwd(decoded.value)); if (source === "claudeAgent") { @@ -1095,8 +1118,7 @@ export const make = Effect.gen(function* () { !isPiConversationRecord(decoded.value) ) { // Tool output can dwarf the conversation; keep only the tree links. - const { message: _message, ...links } = decoded.value; - records.push(links); + retainPiLinks(value); } else { records.push(decoded.value); historyBytes += recordBytes; @@ -1464,10 +1486,16 @@ export const make = Effect.gen(function* () { })); }); - const collectCandidates = Effect.fn("AgentSessionScanner.collectCandidates")(function* () { - const settings = yield* serverSettings.getSettings.pipe( - Effect.mapError((cause) => new AgentSessionScanError({ operation: "read-settings", cause })), - ); + const readSettings = serverSettings.getSettings.pipe( + Effect.mapError((cause) => new AgentSessionScanError({ operation: "read-settings", cause })), + ); + + const collectCandidates = Effect.fn("AgentSessionScanner.collectCandidates")(function* ( + settings: ContractServerSettings, + ) { + const { scanBudgetMiB } = settings.agentSessionImport; + // A raised byte budget would otherwise be capped by the operation and record limits. + const budgetScale = scanBudgetMiB / DEFAULT_IMPORT_SETTINGS.scanBudgetMiB; const raw: Array = []; let truncated = false; @@ -1576,9 +1604,9 @@ export const make = Effect.gen(function* () { } 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, + bytesRemaining: scanBudgetMiB * 1024 * 1024, + operationsRemaining: Math.ceil(MAX_METADATA_OPERATIONS_PER_SOURCE * budgetScale), + recordsRemaining: Math.ceil(MAX_METADATA_RECORDS_PER_SOURCE * budgetScale), truncated: false, }; raw.push(...(yield* groupTranscriptsByCwd(source, selectedTranscripts, metadataBudget))); @@ -1588,11 +1616,18 @@ export const make = Effect.gen(function* () { return { candidates: raw, truncated }; }); - let cachedCandidates: ReadonlyArray | null = null; + let cachedCandidates: { + readonly scanBudgetMiB: number; + readonly candidates: ReadonlyArray; + } | null = null; const scan: AgentSessionScanner["Service"]["scan"] = Effect.gen(function* () { - const { candidates: raw, truncated } = yield* collectCandidates(); - cachedCandidates = raw; + const settings = yield* readSettings; + const { candidates: raw, truncated } = yield* collectCandidates(settings); + cachedCandidates = { + scanBudgetMiB: settings.agentSessionImport.scanBudgetMiB, + candidates: raw, + }; const merged = new Map< string, @@ -1730,10 +1765,16 @@ export const make = Effect.gen(function* () { 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 settings = yield* readSettings; + const importSettings = settings.agentSessionImport; + const cutoffMs = + importSettings.windowDays === null ? -Infinity : nowMs - importSettings.windowDays * DAY_MS; - const candidates = cachedCandidates ?? (yield* collectCandidates()).candidates; - cachedCandidates = candidates; + const candidates = + cachedCandidates?.scanBudgetMiB === importSettings.scanBudgetMiB + ? cachedCandidates.candidates + : (yield* collectCandidates(settings)).candidates; + cachedCandidates = { scanBudgetMiB: importSettings.scanBudgetMiB, candidates }; const eligibleTranscripts: Array<{ readonly candidate: RawCandidate; @@ -1774,7 +1815,7 @@ export const make = Effect.gen(function* () { const importedSessions = new Set(); const codexThreadNamesByHome = new Map>(); let bytesRemaining = MAX_IMPORT_BYTES; - let transcriptsRemaining = MAX_IMPORT_TRANSCRIPTS; + let transcriptsRemaining = importSettings.conversationsPerRun; const codexRolloutPaths = new Map>(); const codexRolloutPath = (providerInstanceId: ProviderInstanceId, rolloutId: string) => { let paths = codexRolloutPaths.get(providerInstanceId); @@ -1816,6 +1857,7 @@ export const make = Effect.gen(function* () { filePath, transcriptIdentity(filePath, stats.value), "codex", + importSettings.recordsPerConversation, { endByteOffset: base.end_byte_offset, historyBytes }, ); if (inherited === null) return null; @@ -1862,7 +1904,12 @@ export const make = Effect.gen(function* () { } transcriptsRemaining -= 1; bytesRemaining -= identity.size; - const snapshot = yield* readTranscript(transcript.filePath, identity, candidate.source); + const snapshot = yield* readTranscript( + transcript.filePath, + identity, + candidate.source, + importSettings.recordsPerConversation, + ); if (snapshot === null) { return Option.some({ _tag: "Skipped" }); } @@ -1900,6 +1947,7 @@ export const make = Effect.gen(function* () { ? snapshot.records : [...inheritedRecords, ...snapshot.records], snapshot.claudeFields, + importSettings.messagesPerThread ?? Infinity, ); if (parsedThread === null) { return Option.some({ _tag: "Skipped" }); diff --git a/apps/web/src/components/onboarding/WelcomePage.tsx b/apps/web/src/components/onboarding/WelcomePage.tsx index 48171ccf94457..1aab634775f37 100644 --- a/apps/web/src/components/onboarding/WelcomePage.tsx +++ b/apps/web/src/components/onboarding/WelcomePage.tsx @@ -1378,6 +1378,7 @@ function useProjectImport({ importedThreadCount?: number, ) => Promise; }) { + const { environments } = useEnvironments(); const writableEnvironments = useEnvironmentsWithScope(scans, AuthOrchestrationOperateScope); const createProject = useAtomCommand(projectEnvironment.create, { reportFailure: false }); const importThreads = useAtomCommand(agentSessionImport, { reportFailure: false }); @@ -1406,6 +1407,9 @@ function useProjectImport({ ...candidate, environmentId: scan.environmentId, key: onboardingProjectKey(scan.environmentId, candidate.path), + importWindowDays: environments.find( + (environment) => environment.environmentId === scan.environmentId, + )?.serverConfig?.settings.agentSessionImport.windowDays, })), ), ); diff --git a/apps/web/src/components/settings/AgentSessionImportSettings.tsx b/apps/web/src/components/settings/AgentSessionImportSettings.tsx new file mode 100644 index 0000000000000..48785a0a80dbe --- /dev/null +++ b/apps/web/src/components/settings/AgentSessionImportSettings.tsx @@ -0,0 +1,279 @@ +import { + AGENT_SESSION_IMPORT_BOUNDS, + DEFAULT_SERVER_SETTINGS, + type AgentSessionImportSettings as AgentSessionImportLimits, +} from "@t3tools/contracts"; +import { TriangleAlertIcon } from "lucide-react"; +import { useState } from "react"; + +import { Alert, AlertDescription } from "../ui/alert"; +import { + NumberField, + NumberFieldDecrement, + NumberFieldGroup, + NumberFieldIncrement, + NumberFieldInput, +} from "../ui/number-field"; +import { Switch } from "../ui/switch"; +import { FoldedSettingsSection } from "./FoldedSettingsSection"; +import { SettingResetButton, SettingsRow } from "./settingsLayout"; +import { useSettingsScope } from "./SettingsScopeContext"; +import { SettingsScopeNotice } from "./SettingsScopeNotice"; +import { searchableSetting } from "./settingsSearch"; +import { useScopedSettings, useUpdateScopedSettings } from "./useScopedSettings"; + +type LimitKey = keyof AgentSessionImportLimits; + +const DEFAULTS = DEFAULT_SERVER_SETTINGS.agentSessionImport; + +const LIMITS: ReadonlyArray<{ + readonly key: LimitKey; + readonly id: Parameters[0]; + readonly title: string; + readonly description: string; + readonly unit: string; + readonly step?: number; + readonly offLabel?: string; +}> = [ + { + key: "windowDays", + id: "agent-session-import-window", + title: "Time window", + description: "Only conversations active within this many days are imported.", + unit: "days", + offLabel: "All history", + }, + { + key: "conversationsPerRun", + id: "agent-session-import-conversations", + title: "Conversations per run", + description: "Most conversations imported per project each time you import.", + unit: "conversations", + }, + { + key: "recordsPerConversation", + id: "agent-session-import-records", + title: "Records per conversation", + description: + "Transcript lines read from one conversation, including tool activity; longer ones are skipped.", + unit: "records", + step: 1000, + }, + { + key: "messagesPerThread", + id: "agent-session-import-messages", + title: "Messages per thread", + description: + "Keeps the first prompt plus the latest messages; threads already imported are not changed.", + unit: "messages", + offLabel: "Full history", + }, + { + key: "scanBudgetMiB", + id: "agent-session-import-scan-budget", + title: "Scan budget", + description: + "History data read from each agent to find where conversations ran; raise it when projects are missing.", + unit: "MiB", + }, +]; + +function isRaised(settings: AgentSessionImportLimits) { + return LIMITS.some(({ key }) => { + const value = settings[key]; + return value === null || value > DEFAULTS[key]!; + }); +} + +function isCustom(settings: AgentSessionImportLimits) { + return LIMITS.some(({ key }) => settings[key] !== DEFAULTS[key]); +} + +function LimitControl({ + label, + unit, + value, + fallback, + bounds, + step = 1, + offLabel, + mixed, + onChange, +}: { + label: string; + unit: string; + value: number | null; + fallback: number; + bounds: { readonly minimum: number; readonly maximum: number }; + step?: number | undefined; + offLabel?: string | undefined; + mixed: boolean; + onChange: (value: number | null, onFailed: () => void) => void; +}) { + const [draft, setDraft] = useState(value); + // `undefined` forces the next render to adopt the stored value after a failed save. + const [savedValue, setSavedValue] = useState(value); + if (savedValue !== value) { + setSavedValue(value); + setDraft(value); + } + + return ( +
+ {value !== null ? ( + { + if (next === null) setDraft(value); + else { + const rounded = Math.min(bounds.maximum, Math.max(bounds.minimum, Math.round(next))); + setDraft(rounded); + if (mixed || rounded !== value) onChange(rounded, () => setSavedValue(undefined)); + } + }} + > + + + { + if (event.key !== "Enter") return; + const input = event.currentTarget; + input.blur(); + input.focus(); + }} + /> + + + + + ) : ( + {offLabel} + )} + {offLabel !== undefined && ( + onChange(enabled ? fallback : null, () => {})} + /> + )} +
+ ); +} + +export function AgentSessionImportSettings() { + const { connectedEnvironments, targets, scope } = useSettingsScope(); + const settings = useScopedSettings((current) => current.agentSessionImport); + const updateSettings = useUpdateScopedSettings(); + + if (scope.kind === "project" || scope.kind === "checkout" || connectedEnvironments.length === 0) + return null; + + const supportedEnvironments = connectedEnvironments.filter( + (environment) => + environment.serverConfig?.environment.capabilities.agentSessionImportSettings === true, + ); + const supported = supportedEnvironments.length === connectedEnvironments.length; + const { id, title } = searchableSetting("agent-session-import"); + + return ( + isCustom(target.settings.agentSessionImport)) + ? "Custom limits" + : "Default limits" + : null + } + > + {supported ? ( + <> +

+ Limits for importing Claude Code, Codex, and Pi conversations. New limits apply to + conversations that are not imported yet. +

+ {LIMITS.map((limit) => { + const { key } = limit; + const status = targets.some( + (target) => target.settings.agentSessionImport[key] !== settings[key], + ) + ? "Mixed across selected machines" + : undefined; + return ( + + updateSettings({ agentSessionImport: { [key]: DEFAULTS[key] } }) + } + /> + ) : null + } + control={ + + updateSettings({ agentSessionImport: { [key]: value } }, { onFailed }) + } + /> + } + /> + ); + })} + {targets.some((target) => isRaised(target.settings.agentSessionImport)) && ( +
+ + + + Higher limits make scans and imports take longer and use more memory on the + computer running them. + + +
+ )} + + ) : ( + environment.environmentId, + )} + > + Update the selected environments to change import limits, or choose a machine that + supports them. + + )} +
+ ); +} diff --git a/apps/web/src/components/settings/SettingsPanels.tsx b/apps/web/src/components/settings/SettingsPanels.tsx index dcaca88e6c7f7..d42cdb7bc56f0 100644 --- a/apps/web/src/components/settings/SettingsPanels.tsx +++ b/apps/web/src/components/settings/SettingsPanels.tsx @@ -32,6 +32,7 @@ import * as Equal from "effect/Equal"; import * as Schema from "effect/Schema"; import { APP_VERSION, HOSTED_APP_CHANNEL, HOSTED_APP_CHANNEL_LABEL } from "../../branding"; import { AboutVersionTitle } from "./AboutVersionTitle"; +import { AgentSessionImportSettings } from "./AgentSessionImportSettings"; import { CliCommandSettingsRow } from "./CliCommandSettingsRow"; import { MobileAppSettingsRow } from "./MobileAppSettingsRow"; import { @@ -1707,6 +1708,8 @@ export function ThreadsSettingsPanel() { /> + + {!isProjectScope ? ( diff --git a/apps/web/src/components/settings/settingsSearch.ts b/apps/web/src/components/settings/settingsSearch.ts index 4f61ba41c0d86..0e8dca66e7c4f 100644 --- a/apps/web/src/components/settings/settingsSearch.ts +++ b/apps/web/src/components/settings/settingsSearch.ts @@ -801,6 +801,65 @@ export const SETTINGS_SEARCH_ITEMS = [ scope: "environment-defaults", searchTerms: ["ghq clone destination host owner repository root layout"], }, + { + id: "agent-session-import", + title: "Session import", + to: "/settings/threads", + scope: "environment-defaults", + environmentOnly: true, + searchTerms: [ + "import projects conversations sessions history limits advanced onboarding welcome claude codex pi", + ], + }, + { + id: "agent-session-import-window", + title: "Import time window", + to: "/settings/threads", + targetId: "agent-session-import", + scope: "environment-defaults", + environmentOnly: true, + searchTerms: ["import window days all history age old conversations claude codex pi sessions"], + }, + { + id: "agent-session-import-conversations", + title: "Import conversations per run", + to: "/settings/threads", + targetId: "agent-session-import", + scope: "environment-defaults", + environmentOnly: true, + searchTerms: ["import conversations per run batch per project limit claude codex pi sessions"], + }, + { + id: "agent-session-import-records", + title: "Import records per conversation", + to: "/settings/threads", + targetId: "agent-session-import", + scope: "environment-defaults", + environmentOnly: true, + searchTerms: [ + "import records per conversation transcript lines long sessions skipped very long claude codex pi", + ], + }, + { + id: "agent-session-import-messages", + title: "Import messages per thread", + to: "/settings/threads", + targetId: "agent-session-import", + scope: "environment-defaults", + environmentOnly: true, + searchTerms: ["import messages per thread full history truncate thread length claude codex pi"], + }, + { + id: "agent-session-import-scan-budget", + title: "Import scan budget", + to: "/settings/threads", + targetId: "agent-session-import", + scope: "environment-defaults", + environmentOnly: true, + searchTerms: [ + "import scan budget metadata MiB missing projects folders older projects claude codex pi", + ], + }, { id: "unpin-confirmation", title: "Unpin confirmation", diff --git a/apps/web/src/onboarding/projectImport.logic.test.ts b/apps/web/src/onboarding/projectImport.logic.test.ts index 10cb0d731ee75..5c41af151cf9e 100644 --- a/apps/web/src/onboarding/projectImport.logic.test.ts +++ b/apps/web/src/onboarding/projectImport.logic.test.ts @@ -56,6 +56,23 @@ describe("partitionOnboardingProjects", () => { }); }); + it("narrows the default selection to a shorter import window", () => { + const lastWeek = candidate("/projects/last-week", { lastActiveAt: "2026-08-17T12:00:00.000Z" }); + const lastMonth = candidate("/projects/last-month", { + lastActiveAt: "2026-08-02T12:00:00.000Z", + }); + const recentOf = (importWindowDays: number | null | undefined) => + partitionOnboardingProjects( + [lastWeek, lastMonth].map((project) => ({ ...project, importWindowDays })), + now, + ).recent.map((project) => project.path); + + expect(recentOf(7)).toEqual(["/projects/last-week"]); + expect(recentOf(90)).toEqual(["/projects/last-week", "/projects/last-month"]); + expect(recentOf(null)).toEqual(["/projects/last-week", "/projects/last-month"]); + expect(recentOf(undefined)).toEqual(["/projects/last-week", "/projects/last-month"]); + }); + it("keeps non-git folders and thin histories out of the default selection", () => { const repo = candidate("/projects/repo"); const folder = candidate("/projects/folder", { git: null }); diff --git a/apps/web/src/onboarding/projectImport.logic.ts b/apps/web/src/onboarding/projectImport.logic.ts index 1be9c7b824b2a..0eb5a67d02b7a 100644 --- a/apps/web/src/onboarding/projectImport.logic.ts +++ b/apps/web/src/onboarding/projectImport.logic.ts @@ -4,16 +4,19 @@ import { } from "@t3tools/client-runtime/state/projects"; import type { AgentSessionProjectCandidate, EnvironmentId, ProjectId } from "@t3tools/contracts"; -const RECENT_PROJECT_WINDOW_MS = 30 * 24 * 60 * 60 * 1000; +const DAY_MS = 24 * 60 * 60 * 1000; +const RECENT_PROJECT_WINDOW_MS = 30 * DAY_MS; const DEFAULT_SELECTION_MIN_THREADS = 3; -/** Treat candidates missing git as repositories for servers predating git scans. */ -export function partitionOnboardingProjects( - candidates: ReadonlyArray, - now = Date.now(), -) { - const cutoff = now - RECENT_PROJECT_WINDOW_MS; - +/** + * Treat candidates missing git as repositories for servers predating git scans. A shorter + * import window narrows `recent`, so it only holds projects with conversations to import. + */ +export function partitionOnboardingProjects< + T extends AgentSessionProjectCandidate & { + readonly importWindowDays?: number | null | undefined; + }, +>(candidates: ReadonlyArray, now = Date.now()) { return { available: candidates, recent: candidates.filter((candidate) => { @@ -21,7 +24,11 @@ export function partitionOnboardingProjects= cutoff && lastActiveAt <= now; + const windowMs = Math.min( + RECENT_PROJECT_WINDOW_MS, + (candidate.importWindowDays ?? Infinity) * DAY_MS, + ); + return lastActiveAt >= now - windowMs && lastActiveAt <= now; }), }; } diff --git a/docs/user/welcome-page.md b/docs/user/welcome-page.md index f280ace079147..c5ea525ce2dda 100644 --- a/docs/user/welcome-page.md +++ b/docs/user/welcome-page.md @@ -69,9 +69,10 @@ are listed first, newest activity on top. When the remote is on GitHub, the group shows the repository as `owner/name`. Clones with the same remote share one group. Directories that are not git repositories sit under "Other folders". -The page offers git repositories active within the last 30 days with at least -three conversations, already checked. Projects already in T3 Code are not checked -again. Uncheck the box to start without them, or +The page offers git repositories active within the last 30 days, or within the +import time window if it is shorter, with at least three conversations, already +checked. Projects already in T3 Code are not checked again. Uncheck the box to +start without them, or select **Review all** to pick from everything found, with "Select all" and "Select none". Linked worktrees of bare repositories can be offered as separate projects. Other linked git worktrees, Codex scratch @@ -80,9 +81,11 @@ offered. 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. +Raising the scan budget in **Settings → Threads → Session import** finds more. Imported projects include Claude, Codex and Pi conversations active within the last -30 days. You can continue those conversations in T3 Code. Pi is off by default: +30 days by default. You can continue those conversations in T3 Code. +Pi is off by default: open **Add more agents** and select **Turn on** next to Pi, and the page looks for its sessions too. Pi sessions are read from each enabled Pi provider's session directory, which follows @@ -90,22 +93,29 @@ are read from each enabled Pi provider's session directory, which follows Pi conversation imports its active branch, the one Pi reopens. 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. +newest remaining visible user and assistant messages, up to 200 by default. 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 4 GiB. It also skips a conversation when the message text and metadata it keeps need more than 32 MiB of memory. Tool output -does not count toward that limit. It skips a conversation with more than 100,000 -records every time, so opening the page again does not import it. It ignores +does not count toward that limit. By default it skips a conversation with more +than 100,000 records, and opening the page again does not import it until that +limit is raised. It ignores malformed records and skips unreadable or unparseable conversations. -Each import attempt reads up to 100 conversation files and 4 GiB per project. To -continue a large batch, open the Welcome page again and pick the project in +By default, each import attempt reads up to 100 conversation files per project, +never more than 4 GiB. To continue a large batch, open the Welcome page again +and pick the project in **Review all**. Completed conversations are not imported again. You can continue without the remaining history. +To change these limits, open **Settings → Threads → Session import**. Higher +limits take longer and use more memory on the computer running the import. New +limits apply to conversations that are not imported yet, and the 32 MiB memory +limit for each conversation still applies. + You can start without importing. While an import runs, T3 Code stays on the Welcome page. If you quit, open the Welcome page again from **Settings → About**; the projects you started stay checked. diff --git a/packages/contracts/src/environment.ts b/packages/contracts/src/environment.ts index 5e9cca70752b6..2ad2b3507225f 100644 --- a/packages/contracts/src/environment.ts +++ b/packages/contracts/src/environment.ts @@ -96,6 +96,8 @@ export const ExecutionEnvironmentCapabilities = Schema.Struct({ threadAutoSettlement: Schema.optionalKey(Schema.Boolean), storageCleanup: Schema.optionalKey(Schema.Boolean), storageCleanupRun: Schema.optionalKey(Schema.Boolean), + /** Hide import limits when absent; older servers silently drop the patch. */ + agentSessionImportSettings: Schema.optionalKey(Schema.Boolean), databaseStorage: Schema.optionalKey(Schema.Boolean), otlpEndpointCheck: Schema.optionalKey(Schema.Boolean), projectWorktreeCleanup: Schema.optionalKey(Schema.Boolean), diff --git a/packages/contracts/src/settings.test.ts b/packages/contracts/src/settings.test.ts index 4c194f473c7b0..494b2a2ee83bb 100644 --- a/packages/contracts/src/settings.test.ts +++ b/packages/contracts/src/settings.test.ts @@ -115,6 +115,69 @@ describe("storage cleanup settings", () => { }); }); +describe("agent session import settings", () => { + it("keeps the shipped import limits for existing installations", () => { + expect(decodeServerSettings({}).agentSessionImport).toEqual({ + windowDays: 30, + conversationsPerRun: 100, + recordsPerConversation: 100_000, + messagesPerThread: 200, + scanBudgetMiB: 64, + }); + }); + + it("keeps sibling limits when a stored group is partial", () => { + expect( + decodeServerSettings({ agentSessionImport: { windowDays: null, messagesPerThread: null } }) + .agentSessionImport, + ).toEqual({ + windowDays: null, + conversationsPerRun: 100, + recordsPerConversation: 100_000, + messagesPerThread: null, + scanBudgetMiB: 64, + }); + expect(decodeServerSettingsPatch({ agentSessionImport: { scanBudgetMiB: 256 } })).toEqual({ + agentSessionImport: { scanBudgetMiB: 256 }, + }); + }); + + it.each([ + ["windowDays", 0], + ["windowDays", 3651], + ["windowDays", 1.5], + ["conversationsPerRun", 0], + ["conversationsPerRun", 5001], + ["conversationsPerRun", null], + ["recordsPerConversation", 999], + ["recordsPerConversation", 1_000_001], + ["messagesPerThread", 1], + ["messagesPerThread", 10_001], + ["scanBudgetMiB", 0], + ["scanBudgetMiB", 1025], + ["scanBudgetMiB", null], + ])("rejects %s = %s in patches and stores the default instead", (key, value) => { + expect(() => decodeServerSettingsPatch({ agentSessionImport: { [key]: value } })).toThrow(); + expect( + decodeServerSettings({ agentSessionImport: { [key]: value } }).agentSessionImport, + ).toEqual(DEFAULT_SERVER_SETTINGS.agentSessionImport); + }); + + it("keeps the rest of a stored file when one import limit is out of range", () => { + const settings = decodeServerSettings({ + addProjectBaseDirectory: "~/code", + agentSessionImport: { windowDays: null, conversationsPerRun: 250, scanBudgetMiB: 4096 }, + }); + + expect(settings.addProjectBaseDirectory).toBe("~/code"); + expect(settings.agentSessionImport).toEqual({ + ...DEFAULT_SERVER_SETTINGS.agentSessionImport, + windowDays: null, + conversationsPerRun: 250, + }); + }); +}); + describe("ServerSettings usage price overrides", () => { const prices = { inputCostPerMillionTokens: 2, outputCostPerMillionTokens: 8 }; diff --git a/packages/contracts/src/settings.ts b/packages/contracts/src/settings.ts index 346213992e4b2..39dd93d837696 100644 --- a/packages/contracts/src/settings.ts +++ b/packages/contracts/src/settings.ts @@ -1337,6 +1337,57 @@ export const CloudSpeechCredentials = Schema.Struct({ openrouter: CloudSpeechCredential, }); +// Upper bounds keep one import run from tying up the server. The fixed 32 MiB history budget per +// conversation still applies, so raising the record or message limits cannot exhaust memory. +export const AGENT_SESSION_IMPORT_BOUNDS = { + windowDays: { minimum: 1, maximum: 3650 }, + conversationsPerRun: { minimum: 1, maximum: 5000 }, + recordsPerConversation: { minimum: 1000, maximum: 1_000_000 }, + messagesPerThread: { minimum: 2, maximum: 10_000 }, + scanBudgetMiB: { minimum: 1, maximum: 1024 }, +} as const; +const AgentSessionImportWindowDays = Schema.NullOr( + Schema.Int.check(Schema.isBetween(AGENT_SESSION_IMPORT_BOUNDS.windowDays)), +); +const AgentSessionImportConversationsPerRun = Schema.Int.check( + Schema.isBetween(AGENT_SESSION_IMPORT_BOUNDS.conversationsPerRun), +); +const AgentSessionImportRecordsPerConversation = Schema.Int.check( + Schema.isBetween(AGENT_SESSION_IMPORT_BOUNDS.recordsPerConversation), +); +const AgentSessionImportMessagesPerThread = Schema.NullOr( + Schema.Int.check(Schema.isBetween(AGENT_SESSION_IMPORT_BOUNDS.messagesPerThread)), +); +const AgentSessionImportScanBudgetMiB = Schema.Int.check( + Schema.isBetween(AGENT_SESSION_IMPORT_BOUNDS.scanBudgetMiB), +); + +// A stored limit outside its range falls back alone, so a hand edit cannot reset every setting. +// Patches keep the strict schemas. +const storedImportLimit = (value: Value, fallback: Value["Encoded"]) => { + const decodeValue = Schema.decodeUnknownOption(value as never); + const lenient = Schema.Unknown.pipe( + Schema.decodeTo( + value, + SchemaTransformation.transform({ + decode: (raw) => (Option.isSome(decodeValue(raw)) ? (raw as Value["Encoded"]) : fallback), + encode: (raw) => raw, + }), + ), + ); + return lenient.pipe(Schema.withDecodingDefault(Effect.succeed(fallback))); +}; + +/** Null `windowDays` imports all history; null `messagesPerThread` keeps every message. */ +export const AgentSessionImportSettings = Schema.Struct({ + windowDays: storedImportLimit(AgentSessionImportWindowDays, 30), + conversationsPerRun: storedImportLimit(AgentSessionImportConversationsPerRun, 100), + recordsPerConversation: storedImportLimit(AgentSessionImportRecordsPerConversation, 100_000), + messagesPerThread: storedImportLimit(AgentSessionImportMessagesPerThread, 200), + scanBudgetMiB: storedImportLimit(AgentSessionImportScanBudgetMiB, 64), +}); +export type AgentSessionImportSettings = typeof AgentSessionImportSettings.Type; + export const ServerSettings = Schema.Struct({ // Null means unmigrated; empty is authoritative and must not resurrect cleared preferences. favorites: Schema.NullOr(ModelFavorites).pipe(Schema.withDecodingDefault(Effect.succeed(null))), @@ -1347,6 +1398,9 @@ export const ServerSettings = Schema.Struct({ storageCleanup: StorageCleanupSettings.pipe( Schema.withDecodingDefault(Effect.succeed(Schema.decodeSync(StorageCleanupSettings)({}))), ), + agentSessionImport: AgentSessionImportSettings.pipe( + Schema.withDecodingDefault(Effect.succeed(Schema.decodeSync(AgentSessionImportSettings)({}))), + ), worktreesDirectory: TrimmedString.pipe(Schema.withDecodingDefault(Effect.succeed(""))), /** macOS only, Labs beta: clone new worktrees from a cached checkout instead of writing every file. */ @@ -1804,6 +1858,15 @@ export const ServerSettingsPatch = Schema.Struct({ logsAfterDays: Schema.optionalKey(StorageRetentionDays), }), ), + agentSessionImport: Schema.optionalKey( + Schema.Struct({ + windowDays: Schema.optionalKey(AgentSessionImportWindowDays), + conversationsPerRun: Schema.optionalKey(AgentSessionImportConversationsPerRun), + recordsPerConversation: Schema.optionalKey(AgentSessionImportRecordsPerConversation), + messagesPerThread: Schema.optionalKey(AgentSessionImportMessagesPerThread), + scanBudgetMiB: Schema.optionalKey(AgentSessionImportScanBudgetMiB), + }), + ), worktreesDirectory: Schema.optionalKey(TrimmedString), fastWorktreeCheckout: Schema.optionalKey(Schema.Boolean), responseStreamingMode: Schema.optionalKey(ResponseStreamingMode), diff --git a/packages/shared/src/serverSettings.test.ts b/packages/shared/src/serverSettings.test.ts index 4a28655ab4292..89152f50ab050 100644 --- a/packages/shared/src/serverSettings.test.ts +++ b/packages/shared/src/serverSettings.test.ts @@ -113,6 +113,20 @@ describe("serverSettings helpers", () => { browserArtifactsAfterDays: null, logsAfterDays: 30, }); + + const limited = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { + agentSessionImport: { conversationsPerRun: 250 }, + }); + expect( + applyServerSettingsPatch(limited, { + agentSessionImport: { windowDays: null, scanBudgetMiB: 256 }, + }).agentSessionImport, + ).toEqual({ + ...DEFAULT_SERVER_SETTINGS.agentSessionImport, + windowDays: null, + conversationsPerRun: 250, + scanBudgetMiB: 256, + }); }); it("replaces a source control host's patched field so a cleared account pin does not survive", () => { const pinned = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {