Skip to content

Commit 36d4044

Browse files
committed
Backup original files in temp directory for sandbox diff generation
- in CLI > Edit, we shouldn't compare to live workspace, but to the context files the agent got in prompt (current solution breaks when there are multiple concurrent agentic runs) └── apps └── editor └── src ├── commands │ ├── apply-from-terminal-command.ts │ ├── code-at-cursor-command │ │ └── utils │ │ └── perform-code-at-cursor.ts │ └── generate-commit-message-commands │ └── actions │ └── run-generate-action.ts ├── context │ └── providers │ └── workspace │ └── workspace-provider.ts ├── features │ ├── agentic-search │ │ └── agentic-search.ts │ └── search-files │ └── search-modes │ └── perform-intelligent-search-mode.ts ├── utils │ ├── agentic-cli-invocation │ │ ├── agents │ │ │ ├── antigravity.ts │ │ │ ├── claude.ts │ │ │ ├── codex.ts │ │ │ ├── copilot.ts │ │ │ ├── cursor.ts │ │ │ ├── devin.ts │ │ │ ├── grok.ts │ │ │ ├── index.ts │ │ │ └── opencode.ts │ │ ├── index.ts │ │ ├── invoke-agentic-cli.ts │ │ ├── types.ts │ │ └── utils │ │ ├── check-command-exists.ts │ │ ├── format-duration.ts │ │ ├── generate-diff-markdown.ts │ │ └── get-progress-dots.ts │ └── prompt-builder.ts └── views └── prompt └── backend └── message-handlers ├── handle-invoke-agentic-cli.ts ├── handle-patch-repair.ts ├── handle-voice-input.ts └── utils └── build-cli-prompt.ts
1 parent 4dc3513 commit 36d4044

2 files changed

Lines changed: 34 additions & 39 deletions

File tree

‎apps/editor/src/commands/apply-from-terminal-command.ts‎

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import * as path from 'path'
55
import { get_response_preview_promise_resolve } from '@/features/apply-response/utils/preview'
66
import { apply_response } from '@/features/apply-response/apply-response'
77
import { t } from '@/i18n'
8-
import {
9-
generate_diff_markdown,
10-
get_all_files
11-
} from '../utils/agentic-cli-invocation/utils/generate-diff-markdown'
8+
import { generate_diff_markdown } from '../utils/agentic-cli-invocation/utils/generate-diff-markdown'
129
import { Logger } from '@shared/utils/logger'
1310

1411
const build_diff_from_sandbox = async (
@@ -43,50 +40,37 @@ const build_diff_from_sandbox = async (
4340
const metadata = JSON.parse(metadata_content)
4441
if (metadata && metadata.files && Array.isArray(metadata.files)) {
4542
for (const rel_path of metadata.files) {
46-
const original = resolve_original_path(rel_path)
47-
if (original) {
43+
const original_backup_path = path.join(
44+
sandbox_dir,
45+
'.cwc',
46+
'originals',
47+
rel_path
48+
)
49+
const original_exists = await fs.promises
50+
.access(original_backup_path)
51+
.then(() => true)
52+
.catch(() => false)
53+
54+
if (original_exists) {
4855
file_mappings.set(path.join(sandbox_dir, rel_path), {
49-
original,
56+
original: original_backup_path,
5057
dest_rel: rel_path
5158
})
59+
} else {
60+
const original = resolve_original_path(rel_path)
61+
if (original) {
62+
file_mappings.set(path.join(sandbox_dir, rel_path), {
63+
original,
64+
dest_rel: rel_path
65+
})
66+
}
5267
}
5368
}
5469
}
5570
} catch {
5671
// Ignore
5772
}
5873

59-
const temp_files = await get_all_files(sandbox_dir)
60-
61-
for (const temp_file of temp_files) {
62-
const temp_file_resolved = path.resolve(temp_file)
63-
let has_mapping = false
64-
for (const key of file_mappings.keys()) {
65-
if (path.resolve(key) === temp_file_resolved) {
66-
has_mapping = true
67-
break
68-
}
69-
}
70-
if (has_mapping) continue
71-
72-
const rel_path = path.relative(sandbox_dir, temp_file)
73-
const unix_rel_path = rel_path.replace(/\\/g, '/')
74-
75-
const original_path = resolve_original_path(unix_rel_path)
76-
77-
if (original_path) {
78-
try {
79-
await fs.promises.access(original_path)
80-
file_mappings.set(temp_file, {
81-
original: original_path,
82-
dest_rel: unix_rel_path
83-
})
84-
} catch {
85-
// Ignore
86-
}
87-
}
88-
}
89-
9074
return await generate_diff_markdown(sandbox_dir, file_mappings)
9175
}
9276

‎apps/editor/src/utils/agentic-cli-invocation/invoke-agentic-cli.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,10 +412,21 @@ export const invoke_agentic_cli = async (params: {
412412
})
413413
await fs.promises.copyFile(file_path, dest_path)
414414

415+
const original_backup_path = path.join(
416+
temp_dir_path,
417+
'.cwc',
418+
'originals',
419+
dest_rel
420+
)
421+
await fs.promises.mkdir(path.dirname(original_backup_path), {
422+
recursive: true
423+
})
424+
await fs.promises.copyFile(file_path, original_backup_path)
425+
415426
const unix_dest_rel = dest_rel.replace(/\\/g, '/')
416427

417428
file_mappings.set(dest_path, {
418-
original: file_path,
429+
original: original_backup_path,
419430
dest_rel: unix_dest_rel
420431
})
421432

0 commit comments

Comments
 (0)