🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(hetero): find a resumed Claude Code transcript outside the spawn cwd (#21247)
Arvin Xudfe61e0bc4eb
diff --git a/apps/desktop/src/main/controllers/HeterogeneousAgentImpl.ts b/apps/desktop/src/main/controllers/HeterogeneousAgentImpl.tsindex 049c02983d0..4794175534d 100644--- a/apps/desktop/src/main/controllers/HeterogeneousAgentImpl.ts+++ b/apps/desktop/src/main/controllers/HeterogeneousAgentImpl.ts@@ -91,6 +91,7 @@ import {DroidAcpSession,DSH_COMMAND,ensureClaudeCodeResumeTranscript,+ findClaudeCodeTranscriptPath,getCodexAppServerUnsupportedArgs,getHeterogeneousAgentPresetEnv,GrokAcpSession,@@ -100,7 +101,6 @@ import {normalizeImage,readClaudeCodeSessionCost,readCodexSessionModel,- resolveClaudeCodeTranscriptPath,resolveCliSpawnPlan,resolveCodexInitialModel,spawnDshAcpSession,@@ -3388,7 +3388,7 @@ export default class HeterogeneousAgentCtr {notBefore?: string;sessionId: string;}): Promise<ReturnType<typeof buildClaudeCodeReplayTurn>> {- const filePath = await resolveClaudeCodeTranscriptPath(params);+ const filePath = await findClaudeCodeTranscriptPath(params);if (!filePath) throw new Error(`Transcript replay rejected session id ${params.sessionId}`);let content: string;diff --git a/packages/heterogeneous-agents/src/spawn/claudeCodeSessionCost.test.ts b/packages/heterogeneous-agents/src/spawn/claudeCodeSessionCost.test.tsindex d1fc75ed2d3..b849c2c15ec 100644--- a/packages/heterogeneous-agents/src/spawn/claudeCodeSessionCost.test.ts+++ b/packages/heterogeneous-agents/src/spawn/claudeCodeSessionCost.test.ts@@ -95,4 +95,28 @@ describe('readClaudeCodeSessionCost', () => {await readClaudeCodeSessionCost({ configDir, cwd, sessionId: '../../escape' }),).toBeUndefined();});++ it('reads a session created under another cwd', async () => {+ // A topic that moves into a worktree resumes the session from the new cwd,+ // but CC keeps appending to the transcript under the cwd the session was+ // created in. Missing it there reports the whole session's spend as this+ // run's cost.+ const repo = path.join(dir, 'repo');+ const worktree = path.join(dir, 'wt-feature');+ await mkdir(repo);+ await mkdir(worktree);++ const configDir = path.join(dir, 'profile');+ const file = (await resolveClaudeCodeTranscriptPath({+ configDir,+ cwd: repo,+ sessionId: SESSION_ID,+ }))!;+ await mkdir(path.dirname(file), { recursive: true });+ await writeFile(file, `${costState(58.87)}\n`);++ expect(+ await readClaudeCodeSessionCost({ configDir, cwd: worktree, sessionId: SESSION_ID }),+ ).toBe(58.87);+ });});diff --git a/packages/heterogeneous-agents/src/spawn/claudeCodeSessionCost.ts b/packages/heterogeneous-agents/src/spawn/claudeCodeSessionCost.tsindex ba97c42351f..33701b734c6 100644--- a/packages/heterogeneous-agents/src/spawn/claudeCodeSessionCost.ts+++ b/packages/heterogeneous-agents/src/spawn/claudeCodeSessionCost.ts@@ -1,6 +1,6 @@import { open } from 'node:fs/promises';-import { resolveClaudeCodeTranscriptPath } from './ensureResumeTranscript';+import { findClaudeCodeTranscriptPath } from './ensureResumeTranscript';/** Tail window read per step while scanning backwards for the last cost-state. */const CHUNK_BYTES = 64 * 1024;@@ -116,7 +116,7 @@ export const readClaudeCodeSessionCost = async (params: {sessionId: string;}): Promise<number | undefined> => {try {- const filePath = await resolveClaudeCodeTranscriptPath(params);+ const filePath = await findClaudeCodeTranscriptPath(params);return filePath ? await readTranscriptSessionCost(filePath) : undefined;} catch {return undefined;diff --git a/packages/heterogeneous-agents/src/spawn/ensureResumeTranscript.test.ts b/packages/heterogeneous-agents/src/spawn/ensureResumeTranscript.test.tsindex fdd7d0cf37f..9a68b7fa021 100644--- a/packages/heterogeneous-agents/src/spawn/ensureResumeTranscript.test.ts+++ b/packages/heterogeneous-agents/src/spawn/ensureResumeTranscript.test.ts@@ -7,6 +7,7 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest';import {ensureClaudeCodeResumeTranscript,+ findClaudeCodeTranscriptPath,resolveClaudeCodeTranscriptPath,} from './ensureResumeTranscript';@@ -92,6 +93,25 @@ describe('ensureClaudeCodeResumeTranscript', () => {expect(await readFile(p, 'utf8')).toContain('"original":true');});+ it('finds the live transcript created under another cwd instead of rebuilding', async () => {+ const worktree = path.join(home, 'wt-feature');+ await mkdir(worktree);+ const p = (await resolveClaudeCodeTranscriptPath({ cwd, home, sessionId: SESSION_ID }))!;+ await mkdir(path.dirname(p), { recursive: true });+ await writeFile(p, '{"type":"user","original":true}\n', 'utf8');++ const res = await ensureClaudeCodeResumeTranscript({+ cwd: worktree,+ home,+ messages,+ sessionId: SESSION_ID,+ });+ expect(res).toEqual({ path: p, reason: 'exists', written: false });+ expect(await findClaudeCodeTranscriptPath({ cwd: worktree, home, sessionId: SESSION_ID })).toBe(+ p,+ );+ });+it('no-ops (does not write) when there are no messages', async () => {const res = await ensureClaudeCodeResumeTranscript({cwd,diff --git a/packages/heterogeneous-agents/src/spawn/ensureResumeTranscript.ts b/packages/heterogeneous-agents/src/spawn/ensureResumeTranscript.tsindex 9c7ef4474b0..b022ea96aa0 100644--- a/packages/heterogeneous-agents/src/spawn/ensureResumeTranscript.ts+++ b/packages/heterogeneous-agents/src/spawn/ensureResumeTranscript.ts@@ -1,4 +1,4 @@-import { mkdir, realpath, stat, writeFile } from 'node:fs/promises';+import { mkdir, readdir, realpath, stat, writeFile } from 'node:fs/promises';import { homedir } from 'node:os';import path from 'node:path';@@ -78,6 +78,41 @@ const fileExists = async (p: string): Promise<boolean> => {}};+/**+ * Locate the transcript `--resume` will actually load, which is not always the+ * one {@link resolveClaudeCodeTranscriptPath} points at: CC keeps appending to+ * the file under the cwd the session was CREATED in, and resumes it by id from+ * any cwd. A topic that moves into a worktree therefore resumes a transcript+ * stored under another directory's project folder.+ *+ * Session ids are UUIDs, so the first `<id>.jsonl` under any project folder is+ * that session. Returns the cwd-derived path when none exists (so a caller can+ * create it there), and `null` for a session id that could not be real.+ */+export const findClaudeCodeTranscriptPath = async (params: {+ configDir?: string;+ cwd: string;+ home?: string;+ sessionId: string;+}): Promise<string | null> => {+ const filePath = await resolveClaudeCodeTranscriptPath(params);+ if (!filePath || (await fileExists(filePath))) return filePath;++ const projectsDir = path.dirname(path.dirname(filePath));+ let projectDirs: string[];+ try {+ projectDirs = await readdir(projectsDir);+ } catch {+ return filePath;+ }+ for (const projectDir of projectDirs) {+ const candidate = path.join(projectsDir, projectDir, path.basename(filePath));+ if (await fileExists(candidate)) return candidate;+ }++ return filePath;+};+export type EnsureResumeTranscriptReason ='exists' | 'invalid-session-id' | 'no-messages' | 'empty-transcript' | 'written';@@ -110,7 +145,7 @@ export const ensureClaudeCodeResumeTranscript = async (params: {transcriptOptions?: Partial<Omit<BuildClaudeCodeTranscriptOptions, 'cwd' | 'sessionId'>>;}): Promise<EnsureResumeTranscriptResult> => {const { cwd, messages, sessionId } = params;- const filePath = await resolveClaudeCodeTranscriptPath({+ const filePath = await findClaudeCodeTranscriptPath({configDir: params.configDir,cwd,home: params.home,diff --git a/packages/heterogeneous-agents/src/spawn/index.ts b/packages/heterogeneous-agents/src/spawn/index.tsindex 96750d8fe62..b8ecd89ae15 100644--- a/packages/heterogeneous-agents/src/spawn/index.ts+++ b/packages/heterogeneous-agents/src/spawn/index.ts@@ -180,6 +180,7 @@ export {ensureClaudeCodeResumeTranscript,type EnsureResumeTranscriptReason,type EnsureResumeTranscriptResult,+ findClaudeCodeTranscriptPath,resolveClaudeCodeTranscriptPath,} from './ensureResumeTranscript';export { getHeterogeneousAgentPresetEnv } from './presetEnv';