feat(web): import individual Claude sessions from a project #10631
Closedmimen(GitHub profile, opens in a new tab)wants to merge 2 commits into
main fromrefs/pull/10631/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
- apps/server/src/project/AgentSessionImporter.ts line 147OutdatedResolvedby mimen(GitHub profile, opens in a new tab)@@ -98,0 +127,21 @@127+128+const importedThreadId = (selection: AgentSessionSelection) =>129+ ThreadId.make(`import:${selection.providerInstanceId}:${selection.providerSessionId}`);130+const decodeClaudeCursor = Schema.decodeUnknownOption(Schema.Struct({ resume: Schema.String }));131+const existingSessionThreads = Effect.fn("AgentSessionImporter.existingSessionThreads")(132+ function* () {133+ const directory = yield* ProviderSessionDirectory.ProviderSessionDirectory;134+ const snapshots = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;135+ const scanner = yield* AgentSessionScanner.AgentSessionScanner;136+ const homes = yield* scanner.claudeSessionHomes;137+ const key = (instanceId: AgentSessionSelection["providerInstanceId"], sessionId: string) =>138+ `${homes.has(instanceId) ? `home:${homes.get(instanceId)}` : `instance:${instanceId}`}\0${sessionId}`;139+ const bindings = yield* directory.listBindings();140+ const existing = new Map<string, ThreadId>();141+ for (const binding of bindings) {142+ if (binding.provider !== "claudeAgent" || !binding.providerInstanceId) continue;143+ const cursor = decodeClaudeCursor(binding.resumeCursor);144+ if (Option.isNone(cursor)) continue;145+ const thread = yield* snapshots.getThreadShellById(binding.threadId);146+ if (Option.isSome(thread))147+ existing.set(key(binding.providerInstanceId, cursor.value.resume), binding.threadId);apps/server/src/project/AgentSessionImporter.ts line 128Resolvedby mimen(GitHub profile, opens in a new tab)@@ -98,0 +108,21 @@108+ input: AgentSessionImportInput,109+) {110+ const snapshots = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;111+ const project = yield* snapshots112+ .getProjectShellById(input.projectId)113+ .pipe(114+ Effect.mapError((cause) => new AgentSessionScanError({ operation: "read-projects", cause })),115+ );116+ if (Option.isNone(project))117+ return yield* new AgentSessionImportProjectNotFoundError({ projectId: input.projectId });118+ if (119+ input.expectedWorkspaceRoot !== undefined &&120+ normalizeProjectPathForComparison(project.value.workspaceRoot) !==121+ normalizeProjectPathForComparison(input.expectedWorkspaceRoot)122+ ) {123+ return yield* new AgentSessionImportProjectChangedError({ projectId: input.projectId });124+ }125+ return project.value;126+});127+128+const importedThreadId = (selection: AgentSessionSelection) =>
- apps/server/src/project/AgentSessionImporter.ts lines 141 to 148OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -98,0 +128,21 @@128+const importedThreadId = (selection: AgentSessionSelection) =>129+ ThreadId.make(`import:${selection.providerInstanceId}:${selection.providerSessionId}`);130+const decodeClaudeCursor = Schema.decodeUnknownOption(Schema.Struct({ resume: Schema.String }));131+const existingSessionThreads = Effect.fn("AgentSessionImporter.existingSessionThreads")(132+ function* () {133+ const directory = yield* ProviderSessionDirectory.ProviderSessionDirectory;134+ const snapshots = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;135+ const scanner = yield* AgentSessionScanner.AgentSessionScanner;136+ const homes = yield* scanner.claudeSessionHomes;137+ const key = (instanceId: AgentSessionSelection["providerInstanceId"], sessionId: string) =>138+ `${homes.has(instanceId) ? `home:${homes.get(instanceId)}` : `instance:${instanceId}`}\0${sessionId}`;139+ const bindings = yield* directory.listBindings();140+ const existing = new Map<string, ThreadId>();141+ for (const binding of bindings) {142+ if (binding.provider !== "claudeAgent" || !binding.providerInstanceId) continue;143+ const cursor = decodeClaudeCursor(binding.resumeCursor);144+ if (Option.isNone(cursor)) continue;145+ const thread = yield* snapshots.getThreadShellById(binding.threadId);146+ if (Option.isSome(thread))147+ existing.set(key(binding.providerInstanceId, cursor.value.resume), binding.threadId);148+ }apps/server/src/project/AgentSessionScanner.test.ts line 448OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -179,0 +428,21 @@428+ ),429+ );430+ }),431+ );432+433+ it.effect("keeps valid sessions when a child directory or transcript is unreadable", () =>434+ Effect.gen(function* () {435+ const fs = yield* FileSystem.FileSystem;436+ const root = yield* makeTempDir("session-unreadable-");437+ const cwd = `${root}/workspace`;438+ const home = `${root}/claude`;439+ const blocked = `${home}/projects/blocked`;440+ yield* fs.makeDirectory(cwd);441+ yield* fs.makeDirectory(blocked, { recursive: true });442+ yield* writeTranscript({443+ filePath: `${home}/projects/project/${sessionId(202)}.jsonl`,444+ contents: sessionRecord(cwd, sessionId(202), "Readable session"),445+ mtimeMs: 1_735_689_600_000,446+ });447+ yield* fs.symlink(`${root}/missing`, `${home}/projects/project/${sessionId(203)}.jsonl`);448+ yield* fs.chmod(blocked, 0);apps/server/src/project/AgentSessionScanner.ts lines 1811 to 1891Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -1491,0 +1782,21 @@1782+ },1783+ };1784+ });1785+1786+ const list: AgentSessionScanner["Service"]["list"] = Effect.fn("AgentSessionScanner.list")(1787+ function* (workspaceRoot, cursor = 0) {1788+ const { transcripts, truncated } = yield* scopedTranscripts(workspaceRoot);1789+ const sessions: AgentSessionListResult["sessions"][number][] = [];1790+ let incomplete = truncated;1791+ for (const transcript of transcripts.slice(cursor, cursor + 40)) {1792+ const result = yield* inspect(workspaceRoot, transcript).pipe(Effect.result);1793+ if (result._tag === "Success") sessions.push(result.success.summary);1794+ else incomplete = true;1795+ }1796+ return {1797+ sessions,1798+ nextCursor: cursor + 40 < transcripts.length ? cursor + 40 : null,1799+ truncated: incomplete,1800+ };1801+ },1802+ );apps/web/src/components/AgentSessionImportDialog.tsx lines 119 to 120OutdatedResolvedby mimen(GitHub profile, opens in a new tab)@@ -0,0 +100,21 @@100+ const sessions = result.sessions.filter((session) =>101+ `${session.title}\n${session.firstRequest}`.toLowerCase().includes(query),102+ );103+ return (104+ <div className="grid min-h-0 flex-1 grid-cols-1 grid-rows-[minmax(100px,30%)_minmax(0,1fr)] sm:grid-cols-[minmax(200px,32%)_minmax(0,1fr)] sm:grid-rows-1">105+ <div className="flex min-h-0 flex-col border-b sm:border-r sm:border-b-0">106+ <div className="p-3">107+ <Input108+ aria-label="Search listed sessions"109+ placeholder="Search listed sessions…"110+ value={search}111+ onChange={(event) => setSearch(event.target.value)}112+ />113+ </div>114+ <div className="min-h-0 flex-1 overflow-y-auto p-2" aria-label="Claude Code sessions">115+ {sessions.map((session) => (116+ <button117+ type="button"118+ key={`${session.providerInstanceId}:${session.providerSessionId}`}119+ aria-pressed={selected === session}120+ className={`mb-1 w-full rounded-md p-3 text-left hover:bg-accent ${selected === session ? "bg-accent" : ""}`}
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed