@causyne/agent 1.20.121 → 1.20.122
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/package.json +1 -1
- package/src/acp/agentCatalog.js +81 -26
- package/src/acp/agentCatalog.ts +73 -29
- package/src/acp/client.js +210 -53
- package/src/acp/client.ts +202 -52
- package/src/acp/peerProfiles.js +55 -22
- package/src/acp/peerProfiles.ts +51 -22
- package/src/acp/protocol.js +37 -8
- package/src/acp/protocol.ts +30 -8
- package/src/acp/server.js +123 -25
- package/src/acp/server.ts +124 -24
- package/src/agent/errorClassifier.js +6 -2
- package/src/agent/errorClassifier.ts +6 -2
- package/src/agentLoop.js +11 -5
- package/src/agentLoop.ts +11 -5
- package/src/approvalGrants.js +19 -7
- package/src/approvalGrants.ts +20 -7
- package/src/approvalPolicy.js +48 -15
- package/src/approvalPolicy.ts +46 -15
- package/src/autoUpdate.js +5 -2
- package/src/autoUpdate.ts +5 -2
- package/src/browser/causyneBrowser.js +3 -2
- package/src/browser/causyneBrowser.ts +3 -2
- package/src/browser/searchProviders.js +1 -1
- package/src/browser/searchProviders.ts +1 -1
- package/src/config.js +287 -250
- package/src/config.ts +342 -0
- package/src/configVault.js +84 -17
- package/src/configVault.ts +77 -17
- package/src/continuum/compactor.js +2 -1
- package/src/continuum/compactor.ts +2 -1
- package/src/continuum/sanitizer.js +10 -3
- package/src/continuum/sanitizer.ts +9 -3
- package/src/continuum/ui/continuumPickerView.js +3 -2
- package/src/continuum/ui/continuumPickerView.ts +3 -2
- package/src/cortex/vectorDb.js +26 -15
- package/src/cortex/vectorDb.ts +26 -16
- package/src/creative/liveBridge.js +7 -5
- package/src/creative/liveBridge.ts +7 -5
- package/src/creative/studioScripts.js +16 -5
- package/src/creative/studioScripts.ts +14 -5
- package/src/decisionPolicy.js +16 -4
- package/src/decisionPolicy.ts +14 -4
- package/src/documentEngine/antiSlopAuditor.js +10 -5
- package/src/documentEngine/antiSlopAuditor.ts +11 -5
- package/src/documentEngine/documentForge.js +47 -15
- package/src/documentEngine/documentForge.ts +43 -14
- package/src/engines/design.js +2 -1
- package/src/engines/design.ts +2 -1
- package/src/engines/film.js +4 -4
- package/src/engines/film.ts +2 -2
- package/src/engines/light.js +2 -1
- package/src/engines/light.ts +2 -1
- package/src/engines/registry.js +1 -1
- package/src/engines/registry.ts +1 -1
- package/src/engines/reverseHarness.js +4 -3
- package/src/engines/reverseHarness.ts +4 -3
- package/src/headlessCli.js +2 -1
- package/src/headlessCli.ts +2 -1
- package/src/historyPruner.js +3 -2
- package/src/historyPruner.ts +3 -2
- package/src/hooks.js +3 -0
- package/src/hooks.ts +3 -0
- package/src/implementationPlan.js +21 -11
- package/src/implementationPlan.ts +21 -11
- package/src/infra/atomicWriter.js +4 -1
- package/src/infra/atomicWriter.ts +4 -1
- package/src/launchIntent.js +1 -1
- package/src/launchIntent.ts +1 -1
- package/src/memory/scopedFacts.js +1 -1
- package/src/memory/scopedFacts.ts +1 -1
- package/src/memory/symbolFolder.js +1 -1
- package/src/memory/symbolFolder.ts +1 -1
- package/src/modelRouter.js +1 -1
- package/src/modelRouter.ts +1 -1
- package/src/observer/evidence.js +10 -4
- package/src/observer/evidence.ts +10 -4
- package/src/osKeychain.js +62 -8
- package/src/osKeychain.ts +50 -8
- package/src/perception.js +3 -2
- package/src/perception.ts +3 -2
- package/src/planCache.js +2 -2
- package/src/planCache.ts +2 -2
- package/src/pluginsFactory/pluginsFactory.js +19 -11
- package/src/pluginsFactory/pluginsFactory.ts +18 -11
- package/src/promptMentions.js +2 -2
- package/src/promptMentions.ts +2 -2
- package/src/providers/acp.js +1 -1
- package/src/providers/acp.ts +1 -1
- package/src/providers/anthropic.js +327 -401
- package/src/providers/anthropic.ts +386 -0
- package/src/providers/antigravity.js +10 -4
- package/src/providers/antigravity.ts +10 -4
- package/src/providers/base.js +221 -319
- package/src/providers/base.ts +340 -0
- package/src/providers/byokHttp.js +58 -15
- package/src/providers/byokHttp.ts +59 -16
- package/src/providers/byokStream.js +142 -51
- package/src/providers/byokStream.ts +146 -42
- package/src/providers/dashscope-byok.js +97 -89
- package/src/providers/dashscope-byok.ts +114 -0
- package/src/providers/deepseek.js +73 -72
- package/src/providers/deepseek.ts +99 -0
- package/src/providers/factory.js +35 -28
- package/src/providers/factory.ts +40 -0
- package/src/providers/gemini.js +227 -278
- package/src/providers/gemini.ts +261 -0
- package/src/providers/loopGuard.js +9 -5
- package/src/providers/loopGuard.ts +10 -5
- package/src/providers/moonshot.js +70 -72
- package/src/providers/moonshot.ts +93 -0
- package/src/providers/muse.js +82 -152
- package/src/providers/muse.ts +122 -0
- package/src/providers/ollama.js +114 -106
- package/src/providers/ollama.ts +135 -0
- package/src/providers/openai-compatible.js +113 -96
- package/src/providers/openai-compatible.ts +139 -0
- package/src/providers/openai.js +22 -15
- package/src/providers/openai.ts +30 -0
- package/src/providers/openrouter.js +136 -74
- package/src/providers/openrouter.ts +133 -72
- package/src/providers/pi.js +6 -0
- package/src/providers/pi.ts +6 -0
- package/src/providers/qwen-byok.js +96 -87
- package/src/providers/qwen-byok.ts +113 -0
- package/src/providers/toolContracts.js +40 -14
- package/src/providers/toolContracts.ts +35 -14
- package/src/providers/wireNormalizer.js +78 -11
- package/src/providers/wireNormalizer.ts +68 -12
- package/src/providers/xai.js +73 -72
- package/src/providers/xai.ts +97 -0
- package/src/reliability/assetEvidence.js +4 -2
- package/src/reliability/assetEvidence.ts +4 -2
- package/src/sandbox.js +489 -554
- package/src/sandbox.ts +552 -0
- package/src/sandboxPolicy.js +95 -16
- package/src/sandboxPolicy.ts +97 -16
- package/src/sessionJournal.js +8 -3
- package/src/sessionJournal.ts +9 -3
- package/src/skills/skillResourceSafety.js +19 -8
- package/src/skills/skillResourceSafety.ts +16 -7
- package/src/skillsManager.js +3 -3
- package/src/skillsManager.ts +3 -3
- package/src/smartToolsUse.js +28 -4
- package/src/smartToolsUse.ts +29 -7
- package/src/stallGuard.js +45 -22
- package/src/stallGuard.ts +46 -22
- package/src/subagentJournal.js +6 -4
- package/src/subagentJournal.ts +6 -4
- package/src/textDiff.js +6 -1
- package/src/textDiff.ts +6 -1
- package/src/timeMachine.js +7 -1
- package/src/timeMachine.ts +7 -1
- package/src/tokenTracker.js +43 -28
- package/src/tokenTracker.ts +44 -30
- package/src/toolPacks.js +3 -1
- package/src/toolPacks.ts +3 -1
- package/src/tools/archive.js +245 -35
- package/src/tools/archive.ts +244 -35
- package/src/tools/computerUse.js +7 -3
- package/src/tools/computerUse.ts +7 -3
- package/src/tools/creativeApps.js +13 -8
- package/src/tools/creativeApps.ts +22 -9
- package/src/tools/deferredMcpSearch.js +26 -14
- package/src/tools/deferredMcpSearch.ts +26 -14
- package/src/tools/desktopOpen.js +69 -10
- package/src/tools/desktopOpen.ts +70 -11
- package/src/tools/documentForge.js +167 -82
- package/src/tools/documentForge.ts +169 -84
- package/src/tools/fsEdit.js +101 -30
- package/src/tools/fsEdit.ts +105 -32
- package/src/tools/fsFind.js +8 -5
- package/src/tools/fsFind.ts +7 -4
- package/src/tools/fsRead.js +119 -13
- package/src/tools/fsRead.ts +124 -13
- package/src/tools/fsWrite.js +17 -5
- package/src/tools/fsWrite.ts +17 -7
- package/src/tools/generateMediaViaApi.js +82 -19
- package/src/tools/generateMediaViaApi.ts +80 -19
- package/src/tools/memoryRecall.js +62 -11
- package/src/tools/memoryRecall.ts +70 -13
- package/src/tools/memoryWrite.js +27 -14
- package/src/tools/memoryWrite.ts +30 -14
- package/src/tools/multiEdit.js +100 -30
- package/src/tools/multiEdit.ts +104 -32
- package/src/tools/nativeEngine.js +33 -18
- package/src/tools/nativeEngine.ts +31 -18
- package/src/tools/openSourceToolProvisioner.js +49 -14
- package/src/tools/openSourceToolProvisioner.ts +54 -15
- package/src/tools/osSandbox.js +54 -14
- package/src/tools/osSandbox.ts +48 -14
- package/src/tools/payloadSanitizer.js +40 -8
- package/src/tools/payloadSanitizer.ts +39 -8
- package/src/tools/peerAgent.js +96 -34
- package/src/tools/peerAgent.ts +114 -37
- package/src/tools/pluginsFactory.js +38 -6
- package/src/tools/pluginsFactory.ts +42 -6
- package/src/tools/readNativeSkill.js +10 -8
- package/src/tools/readNativeSkill.ts +10 -8
- package/src/tools/requestDecision.js +10 -3
- package/src/tools/requestDecision.ts +10 -3
- package/src/tools/resolveAutomator.js +91 -17
- package/src/tools/resolveAutomator.ts +102 -17
- package/src/tools/reverseEngineering.js +168 -41
- package/src/tools/reverseEngineering.ts +161 -41
- package/src/tools/sysRun.js +69 -29
- package/src/tools/sysRun.ts +66 -24
- package/src/tools/toolDispatch.js +24 -1
- package/src/tools/toolDispatch.ts +21 -1
- package/src/tools/toolOutputStore.js +62 -23
- package/src/tools/toolOutputStore.ts +56 -23
- package/src/tools/toolScanner.js +13 -10
- package/src/tools/toolScanner.ts +13 -10
- package/src/tools/toolSelfRepair.js +94 -28
- package/src/tools/toolSelfRepair.ts +96 -28
- package/src/tools/uiForge.js +71 -6
- package/src/tools/uiForge.ts +74 -6
- package/src/tools/viewImage.js +62 -17
- package/src/tools/viewImage.ts +57 -12
- package/src/tools/visionInspector.js +15 -3
- package/src/tools/visionInspector.ts +15 -3
- package/src/tools/walkerSafety.js +2 -0
- package/src/tools/walkerSafety.ts +1 -0
- package/src/tools/webFetch.js +105 -10
- package/src/tools/webFetch.ts +105 -11
- package/src/tui/banner.js +3 -2
- package/src/tui/banner.ts +3 -2
- package/src/tui/consoleGuard.js +12 -4
- package/src/tui/consoleGuard.ts +9 -4
- package/src/tui/messageInteraction.js +2 -3
- package/src/tui/messageInteraction.ts +2 -3
- package/src/tui/modelPicker.js +1 -0
- package/src/tui/modelPicker.ts +1 -0
- package/src/tui/overlay.js +3 -1
- package/src/tui/overlay.ts +3 -1
- package/src/tui/sessionPicker.js +3 -2
- package/src/tui/sessionPicker.ts +3 -2
- package/src/tui/sessionPickerView.js +7 -1
- package/src/tui/sessionPickerView.ts +6 -1
- package/src/tui/toolOutputNormalizer.js +2 -3
- package/src/tui/toolOutputNormalizer.ts +4 -3
- package/src/tui/viewport.js +4 -4
- package/src/tui/viewport.ts +5 -5
- package/src/tui/workspaceIdentity.js +2 -1
- package/src/tui/workspaceIdentity.ts +2 -1
- package/src/uncertainty.js +8 -3
- package/src/uncertainty.ts +8 -3
- package/src/utils/paths.js +8 -5
- package/src/utils/paths.ts +9 -7
- package/src/vfeTracker.js +9 -3
- package/src/vfeTracker.ts +9 -3
- package/src/config.d.ts +0 -2
- package/src/providers/base.d.ts +0 -94
- package/src/providers/factory.d.ts +0 -1
- package/src/sandbox.d.ts +0 -1
|
@@ -12,19 +12,46 @@
|
|
|
12
12
|
* Authorship: Strict TypeScript (Causyne Agent).
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
|
+
import fs from 'node:fs';
|
|
15
16
|
import os from 'node:os';
|
|
16
17
|
import path from 'node:path';
|
|
17
18
|
import { episodicStore } from '../memory/episodicStore.js';
|
|
18
19
|
import { foldSourceFileOutline } from '../memory/symbolFolder.js';
|
|
19
20
|
import type { ObservationType } from '../memory/episodicTypes.js';
|
|
20
21
|
|
|
22
|
+
const MAX_OUTLINE_FILE_BYTES = 2 * 1024 * 1024;
|
|
23
|
+
|
|
21
24
|
interface MemoryRecallContext {
|
|
22
25
|
workspaceRoot?: string | undefined;
|
|
26
|
+
workspacePaths?: string[] | undefined;
|
|
27
|
+
grantAllAccess?: boolean | undefined;
|
|
28
|
+
isPathAllowed?: ((targetPath: string) => boolean) | undefined;
|
|
23
29
|
parentLoop?: {
|
|
24
30
|
workspaceRoot?: string | undefined;
|
|
31
|
+
sandbox?: {
|
|
32
|
+
isPathAllowed?: ((targetPath: string) => boolean) | undefined;
|
|
33
|
+
} | undefined;
|
|
25
34
|
} | undefined;
|
|
26
35
|
}
|
|
27
36
|
|
|
37
|
+
function safeOptionalInt(value: unknown): number | undefined {
|
|
38
|
+
return typeof value === 'number' && Number.isFinite(value) ? Math.trunc(value) : undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function expandOutlineHome(raw: string): string {
|
|
42
|
+
const trimmed = raw.trim();
|
|
43
|
+
if (trimmed === '~') return os.homedir();
|
|
44
|
+
if (trimmed.startsWith('~/') || trimmed.startsWith('~\\')) {
|
|
45
|
+
return path.join(os.homedir(), trimmed.slice(2));
|
|
46
|
+
}
|
|
47
|
+
return trimmed;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function isWithinOutlineRoot(root: string, candidate: string): boolean {
|
|
51
|
+
const rel = path.relative(root, candidate);
|
|
52
|
+
return rel === '' || (rel !== '..' && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel));
|
|
53
|
+
}
|
|
54
|
+
|
|
28
55
|
interface MemoryRecallArgs {
|
|
29
56
|
action?: 'search' | 'timeline' | 'get' | 'outline' | 'stats' | undefined;
|
|
30
57
|
query?: string | undefined;
|
|
@@ -106,24 +133,27 @@ export const memoryRecall = {
|
|
|
106
133
|
const args = (rawArgs && typeof rawArgs === 'object' && !Array.isArray(rawArgs)
|
|
107
134
|
? rawArgs
|
|
108
135
|
: {}) as MemoryRecallArgs;
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
).trim();
|
|
136
|
+
const rawCandidateValue = args.filePath ?? args.path ?? args.file_path ?? args.filepath ?? args.target;
|
|
137
|
+
const rawFileCandidate = typeof rawCandidateValue === 'string' ? rawCandidateValue.trim() : '';
|
|
112
138
|
const inferredAction = args.action
|
|
113
139
|
?? (rawFileCandidate ? 'outline' : undefined)
|
|
114
140
|
?? (args.anchorId !== undefined ? 'timeline' : undefined)
|
|
115
141
|
?? (Array.isArray(args.ids) && args.ids.length > 0 ? 'get' : undefined)
|
|
116
142
|
?? 'search';
|
|
117
143
|
const action = inferredAction;
|
|
118
|
-
const workspace = context
|
|
144
|
+
const workspace = context?.parentLoop?.workspaceRoot
|
|
145
|
+
?? context?.workspaceRoot
|
|
146
|
+
?? context?.workspacePaths?.find((p): p is string => typeof p === 'string' && p.trim().length > 0)
|
|
147
|
+
?? process.cwd();
|
|
148
|
+
const safeQuery = typeof args.query === 'string' ? args.query : undefined;
|
|
119
149
|
|
|
120
150
|
try {
|
|
121
151
|
if (action === 'search') {
|
|
122
152
|
const res = episodicStore.searchIndex({
|
|
123
|
-
query:
|
|
153
|
+
query: safeQuery,
|
|
124
154
|
workspace,
|
|
125
155
|
type: args.type,
|
|
126
|
-
limit: args.limit,
|
|
156
|
+
limit: safeOptionalInt(args.limit),
|
|
127
157
|
workspaceOnly: args.workspaceOnly
|
|
128
158
|
});
|
|
129
159
|
const formatted = res.results.length === 0
|
|
@@ -151,8 +181,8 @@ export const memoryRecall = {
|
|
|
151
181
|
const windowResult = episodicStore.getTimelineWindow({
|
|
152
182
|
anchorId: args.anchorId,
|
|
153
183
|
workspace,
|
|
154
|
-
depthBefore: args.depthBefore,
|
|
155
|
-
depthAfter: args.depthAfter
|
|
184
|
+
depthBefore: safeOptionalInt(args.depthBefore),
|
|
185
|
+
depthAfter: safeOptionalInt(args.depthAfter)
|
|
156
186
|
});
|
|
157
187
|
if (!windowResult.anchor) {
|
|
158
188
|
return {
|
|
@@ -212,13 +242,40 @@ export const memoryRecall = {
|
|
|
212
242
|
if (!rawFileCandidate) {
|
|
213
243
|
return { success: false, error: 'Missing required parameter "filePath" for action="outline".' };
|
|
214
244
|
}
|
|
215
|
-
const expanded = rawFileCandidate
|
|
216
|
-
? path.join(os.homedir(), rawFileCandidate.slice(2))
|
|
217
|
-
: rawFileCandidate;
|
|
245
|
+
const expanded = expandOutlineHome(rawFileCandidate);
|
|
218
246
|
const targetPath = path.isAbsolute(expanded)
|
|
219
|
-
? expanded
|
|
247
|
+
? path.resolve(expanded)
|
|
220
248
|
: path.resolve(workspace, expanded);
|
|
221
|
-
const
|
|
249
|
+
const pathPolicy = context?.parentLoop?.sandbox ?? context;
|
|
250
|
+
if (context?.grantAllAccess !== true) {
|
|
251
|
+
if (typeof pathPolicy?.isPathAllowed === 'function' && !pathPolicy.isPathAllowed(targetPath)) {
|
|
252
|
+
return { success: false, error: `Path outside allowed workspace: ${targetPath}` };
|
|
253
|
+
}
|
|
254
|
+
const configuredRoots = Array.isArray(context?.workspacePaths)
|
|
255
|
+
? context.workspacePaths.filter((p): p is string => typeof p === 'string' && p.trim().length > 0)
|
|
256
|
+
: [context?.parentLoop?.workspaceRoot, context?.workspaceRoot].filter((p): p is string => typeof p === 'string' && p.trim().length > 0);
|
|
257
|
+
if (configuredRoots.length > 0) {
|
|
258
|
+
const realTarget = fs.realpathSync(targetPath);
|
|
259
|
+
const allowed = configuredRoots.some((root) => {
|
|
260
|
+
try {
|
|
261
|
+
return isWithinOutlineRoot(fs.realpathSync(path.resolve(expandOutlineHome(root))), realTarget);
|
|
262
|
+
} catch {
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
if (!allowed) {
|
|
267
|
+
return { success: false, error: `Path outside allowed workspace: ${targetPath}` };
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
const stat = fs.statSync(targetPath);
|
|
272
|
+
if (!stat.isFile()) {
|
|
273
|
+
return { success: false, error: `Outline target is not a regular file: ${targetPath}` };
|
|
274
|
+
}
|
|
275
|
+
if (stat.size > MAX_OUTLINE_FILE_BYTES) {
|
|
276
|
+
return { success: false, error: `File exceeds ${MAX_OUTLINE_FILE_BYTES / (1024 * 1024)} MB outline limit: ${targetPath}` };
|
|
277
|
+
}
|
|
278
|
+
const outline = foldSourceFileOutline(targetPath, safeQuery);
|
|
222
279
|
return {
|
|
223
280
|
success: true,
|
|
224
281
|
action: 'outline',
|
package/src/tools/memoryWrite.js
CHANGED
|
@@ -28,20 +28,33 @@ export const memoryWrite = {
|
|
|
28
28
|
}
|
|
29
29
|
},
|
|
30
30
|
execute: async (args, dependencies) => {
|
|
31
|
-
if (!args ||
|
|
31
|
+
if (!args || typeof args !== 'object' || Array.isArray(args)) {
|
|
32
32
|
return { success: false, error: "Missing 'fact' or 'creativePattern' parameter." };
|
|
33
33
|
}
|
|
34
|
-
if (
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
34
|
+
if (args.fact !== undefined && typeof args.fact !== 'string') {
|
|
35
|
+
return { success: false, error: "'fact' parameter must be a string." };
|
|
36
|
+
}
|
|
37
|
+
if (args.creativePattern !== undefined &&
|
|
38
|
+
(args.creativePattern === null || typeof args.creativePattern !== 'object' || Array.isArray(args.creativePattern))) {
|
|
39
|
+
return { success: false, error: "'creativePattern' parameter must be an object." };
|
|
40
|
+
}
|
|
41
|
+
const rawFact = typeof args.fact === 'string' ? args.fact.trim() : '';
|
|
42
|
+
const cp = args.creativePattern;
|
|
43
|
+
let fact = rawFact;
|
|
44
|
+
if (!fact && cp) {
|
|
45
|
+
const project = typeof cp.project === 'string' && cp.project.trim() ? cp.project.trim() : 'project';
|
|
46
|
+
const approach = typeof cp.designApproach === 'string' && cp.designApproach.trim() ? ` ${cp.designApproach.trim()}` : '';
|
|
47
|
+
const palette = typeof cp.colorPalette === 'string' && cp.colorPalette.trim() ? ` with ${cp.colorPalette.trim()}` : '';
|
|
48
|
+
const fonts = typeof cp.fonts === 'string' && cp.fonts.trim() ? ` (${cp.fonts.trim()})` : '';
|
|
49
|
+
if (approach || palette || fonts || (typeof cp.project === 'string' && cp.project.trim())) {
|
|
50
|
+
fact = `Creative pattern for ${project}:${approach}${palette}${fonts}`.trim();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (!fact) {
|
|
54
|
+
return { success: false, error: "Missing non-empty 'fact' or 'creativePattern' parameter." };
|
|
41
55
|
}
|
|
42
56
|
try {
|
|
43
57
|
const memory = getMemory();
|
|
44
|
-
const fact = String(args.fact).trim();
|
|
45
58
|
const workspace = dependencies?.parentLoop?.workspaceRoot || dependencies?.workspaceRoot;
|
|
46
59
|
const { memory: updated, alreadyKnown } = recordScopedFact(memory, fact, {
|
|
47
60
|
workspace,
|
|
@@ -51,13 +64,13 @@ export const memoryWrite = {
|
|
|
51
64
|
saveMemory(updated);
|
|
52
65
|
}
|
|
53
66
|
let creativeSaved = false;
|
|
54
|
-
if (
|
|
67
|
+
if (cp && typeof cp.designApproach === 'string' && cp.designApproach.trim()) {
|
|
55
68
|
try {
|
|
56
69
|
await cortexDb.saveCreativePattern({
|
|
57
|
-
project:
|
|
58
|
-
designApproach:
|
|
59
|
-
colorPalette:
|
|
60
|
-
fonts:
|
|
70
|
+
project: typeof cp.project === 'string' && cp.project.trim() ? cp.project.trim() : 'untitled project',
|
|
71
|
+
designApproach: cp.designApproach.trim(),
|
|
72
|
+
colorPalette: typeof cp.colorPalette === 'string' && cp.colorPalette.trim() ? cp.colorPalette.trim() : 'unspecified',
|
|
73
|
+
fonts: typeof cp.fonts === 'string' && cp.fonts.trim() ? cp.fonts.trim() : 'unspecified'
|
|
61
74
|
});
|
|
62
75
|
creativeSaved = true;
|
|
63
76
|
}
|
package/src/tools/memoryWrite.ts
CHANGED
|
@@ -55,22 +55,38 @@ export const memoryWrite = {
|
|
|
55
55
|
args: MemoryWriteArgs | null | undefined,
|
|
56
56
|
dependencies?: MemoryWriteDependencies
|
|
57
57
|
): Promise<{ success: boolean; output?: string; error?: string }> => {
|
|
58
|
-
if (!args ||
|
|
58
|
+
if (!args || typeof args !== 'object' || Array.isArray(args)) {
|
|
59
59
|
return { success: false, error: "Missing 'fact' or 'creativePattern' parameter." };
|
|
60
60
|
}
|
|
61
|
+
if (args.fact !== undefined && typeof args.fact !== 'string') {
|
|
62
|
+
return { success: false, error: "'fact' parameter must be a string." };
|
|
63
|
+
}
|
|
64
|
+
if (
|
|
65
|
+
args.creativePattern !== undefined &&
|
|
66
|
+
(args.creativePattern === null || typeof args.creativePattern !== 'object' || Array.isArray(args.creativePattern))
|
|
67
|
+
) {
|
|
68
|
+
return { success: false, error: "'creativePattern' parameter must be an object." };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const rawFact = typeof args.fact === 'string' ? args.fact.trim() : '';
|
|
72
|
+
const cp = args.creativePattern;
|
|
73
|
+
let fact = rawFact;
|
|
74
|
+
if (!fact && cp) {
|
|
75
|
+
const project = typeof cp.project === 'string' && cp.project.trim() ? cp.project.trim() : 'project';
|
|
76
|
+
const approach = typeof cp.designApproach === 'string' && cp.designApproach.trim() ? ` ${cp.designApproach.trim()}` : '';
|
|
77
|
+
const palette = typeof cp.colorPalette === 'string' && cp.colorPalette.trim() ? ` with ${cp.colorPalette.trim()}` : '';
|
|
78
|
+
const fonts = typeof cp.fonts === 'string' && cp.fonts.trim() ? ` (${cp.fonts.trim()})` : '';
|
|
79
|
+
if (approach || palette || fonts || (typeof cp.project === 'string' && cp.project.trim())) {
|
|
80
|
+
fact = `Creative pattern for ${project}:${approach}${palette}${fonts}`.trim();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
61
83
|
|
|
62
|
-
if (!
|
|
63
|
-
|
|
64
|
-
const project = cp.project || 'project';
|
|
65
|
-
const approach = cp.designApproach ? ` ${cp.designApproach}` : '';
|
|
66
|
-
const palette = cp.colorPalette ? ` with ${cp.colorPalette}` : '';
|
|
67
|
-
const fonts = cp.fonts ? ` (${cp.fonts})` : '';
|
|
68
|
-
args.fact = `Creative pattern for ${project}:${approach}${palette}${fonts}`.trim();
|
|
84
|
+
if (!fact) {
|
|
85
|
+
return { success: false, error: "Missing non-empty 'fact' or 'creativePattern' parameter." };
|
|
69
86
|
}
|
|
70
87
|
|
|
71
88
|
try {
|
|
72
89
|
const memory = getMemory();
|
|
73
|
-
const fact = String(args.fact).trim();
|
|
74
90
|
const workspace = dependencies?.parentLoop?.workspaceRoot || dependencies?.workspaceRoot;
|
|
75
91
|
const { memory: updated, alreadyKnown } = recordScopedFact(memory, fact, {
|
|
76
92
|
workspace,
|
|
@@ -82,13 +98,13 @@ export const memoryWrite = {
|
|
|
82
98
|
}
|
|
83
99
|
|
|
84
100
|
let creativeSaved = false;
|
|
85
|
-
if (
|
|
101
|
+
if (cp && typeof cp.designApproach === 'string' && cp.designApproach.trim()) {
|
|
86
102
|
try {
|
|
87
103
|
await cortexDb.saveCreativePattern({
|
|
88
|
-
project:
|
|
89
|
-
designApproach:
|
|
90
|
-
colorPalette:
|
|
91
|
-
fonts:
|
|
104
|
+
project: typeof cp.project === 'string' && cp.project.trim() ? cp.project.trim() : 'untitled project',
|
|
105
|
+
designApproach: cp.designApproach.trim(),
|
|
106
|
+
colorPalette: typeof cp.colorPalette === 'string' && cp.colorPalette.trim() ? cp.colorPalette.trim() : 'unspecified',
|
|
107
|
+
fonts: typeof cp.fonts === 'string' && cp.fonts.trim() ? cp.fonts.trim() : 'unspecified'
|
|
92
108
|
});
|
|
93
109
|
creativeSaved = true;
|
|
94
110
|
} catch {
|
package/src/tools/multiEdit.js
CHANGED
|
@@ -2,30 +2,87 @@ import fs from 'node:fs/promises';
|
|
|
2
2
|
import os from 'node:os';
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import { atomicWriteFileSync } from '../infra/atomicWriter.js';
|
|
5
|
+
function expandHome(value) {
|
|
6
|
+
const trimmed = value.trim();
|
|
7
|
+
if (trimmed === '~')
|
|
8
|
+
return os.homedir();
|
|
9
|
+
if (trimmed.startsWith('~/') || trimmed.startsWith('~\\')) {
|
|
10
|
+
return path.join(os.homedir(), trimmed.slice(2));
|
|
11
|
+
}
|
|
12
|
+
return trimmed;
|
|
13
|
+
}
|
|
14
|
+
function insideRoot(root, target) {
|
|
15
|
+
const rel = path.relative(root, target);
|
|
16
|
+
return rel === '' || (rel !== '..' && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel));
|
|
17
|
+
}
|
|
18
|
+
async function assertWithinScope(target, context) {
|
|
19
|
+
if (!context || context.grantAllAccess === true)
|
|
20
|
+
return;
|
|
21
|
+
const configured = Array.isArray(context.workspacePaths)
|
|
22
|
+
? context.workspacePaths.filter((p) => typeof p === 'string' && p.trim().length > 0).map(p => p.trim())
|
|
23
|
+
: [];
|
|
24
|
+
const roots = configured.length > 0
|
|
25
|
+
? configured
|
|
26
|
+
: [context.parentLoop?.workspaceRoot, context.workspaceRoot]
|
|
27
|
+
.filter((p) => typeof p === 'string' && p.trim().length > 0)
|
|
28
|
+
.map(p => p.trim());
|
|
29
|
+
if (roots.length === 0)
|
|
30
|
+
return;
|
|
31
|
+
const realTarget = await fs.realpath(target);
|
|
32
|
+
for (const root of roots) {
|
|
33
|
+
try {
|
|
34
|
+
const realRoot = await fs.realpath(path.resolve(expandHome(root)));
|
|
35
|
+
if (insideRoot(realRoot, realTarget))
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
throw new Error(`Target resolves outside the selected workspace: ${target}`);
|
|
43
|
+
}
|
|
44
|
+
const MAX_FUZZY_CHARS = 8_192;
|
|
5
45
|
function getLevenshteinDistance(a, b) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
46
|
+
let s1 = a;
|
|
47
|
+
let s2 = b;
|
|
48
|
+
if (s1.length > s2.length) {
|
|
49
|
+
const tmp = s1;
|
|
50
|
+
s1 = s2;
|
|
51
|
+
s2 = tmp;
|
|
52
|
+
}
|
|
53
|
+
const m = s1.length;
|
|
54
|
+
const n = s2.length;
|
|
55
|
+
if (m === 0)
|
|
56
|
+
return n;
|
|
57
|
+
let prev = new Int32Array(m + 1);
|
|
58
|
+
let curr = new Int32Array(m + 1);
|
|
59
|
+
for (let j = 0; j <= m; j++)
|
|
60
|
+
prev[j] = j;
|
|
61
|
+
for (let i = 1; i <= n; i++) {
|
|
62
|
+
curr[0] = i;
|
|
63
|
+
const ch2 = s2.charCodeAt(i - 1);
|
|
64
|
+
for (let j = 1; j <= m; j++) {
|
|
65
|
+
const cost = s1.charCodeAt(j - 1) === ch2 ? 0 : 1;
|
|
66
|
+
const del = prev[j] + 1;
|
|
67
|
+
const ins = curr[j - 1] + 1;
|
|
68
|
+
const sub = prev[j - 1] + cost;
|
|
69
|
+
curr[j] = del < ins ? (del < sub ? del : sub) : (ins < sub ? ins : sub);
|
|
20
70
|
}
|
|
71
|
+
const swap = prev;
|
|
72
|
+
prev = curr;
|
|
73
|
+
curr = swap;
|
|
21
74
|
}
|
|
22
|
-
return
|
|
75
|
+
return prev[m];
|
|
23
76
|
}
|
|
24
77
|
function calculateConfidence(original, match) {
|
|
25
|
-
const dist = getLevenshteinDistance(original, match);
|
|
26
78
|
const maxLen = Math.max(original.length, match.length);
|
|
27
79
|
if (maxLen === 0)
|
|
28
80
|
return 100;
|
|
81
|
+
if (maxLen > MAX_FUZZY_CHARS)
|
|
82
|
+
return 0;
|
|
83
|
+
if (Math.abs(original.length - match.length) / maxLen > 0.1)
|
|
84
|
+
return 0;
|
|
85
|
+
const dist = getLevenshteinDistance(original, match);
|
|
29
86
|
return ((maxLen - dist) / maxLen) * 100;
|
|
30
87
|
}
|
|
31
88
|
export const multiEdit = {
|
|
@@ -52,7 +109,7 @@ export const multiEdit = {
|
|
|
52
109
|
required: ['file_path', 'edits']
|
|
53
110
|
}
|
|
54
111
|
},
|
|
55
|
-
async execute(args) {
|
|
112
|
+
async execute(args, context) {
|
|
56
113
|
try {
|
|
57
114
|
if (!args || typeof args !== 'object') {
|
|
58
115
|
return { success: false, error: 'MultiEdit requires an object with file_path and edits array.' };
|
|
@@ -63,42 +120,55 @@ export const multiEdit = {
|
|
|
63
120
|
if (!Array.isArray(args.edits) || args.edits.length === 0) {
|
|
64
121
|
return { success: false, error: 'MultiEdit requires a non-empty edits array.' };
|
|
65
122
|
}
|
|
66
|
-
const
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
123
|
+
const expandedPath = expandHome(args.file_path);
|
|
124
|
+
const baseRoot = context?.parentLoop?.workspaceRoot?.trim()
|
|
125
|
+
|| context?.workspaceRoot?.trim()
|
|
126
|
+
|| context?.workspacePaths?.find((p) => typeof p === 'string' && p.trim().length > 0)?.trim();
|
|
127
|
+
const target = path.isAbsolute(expandedPath)
|
|
128
|
+
? path.resolve(expandedPath)
|
|
129
|
+
: path.resolve(baseRoot ? path.resolve(expandHome(baseRoot)) : process.cwd(), expandedPath);
|
|
130
|
+
const stat = await fs.lstat(target);
|
|
131
|
+
if (stat.isSymbolicLink()) {
|
|
132
|
+
return { success: false, error: `Refusing to edit symbolic link ${target}; edit a regular file in the selected workspace.` };
|
|
133
|
+
}
|
|
134
|
+
if (!stat.isFile()) {
|
|
135
|
+
return { success: false, error: `Target is not a regular file: ${target}` };
|
|
136
|
+
}
|
|
137
|
+
await assertWithinScope(target, context);
|
|
138
|
+
const originalMode = stat.mode & 0o777;
|
|
70
139
|
const originalContent = await fs.readFile(target, 'utf8');
|
|
71
140
|
let workingContent = originalContent;
|
|
72
141
|
let replacements = 0;
|
|
73
142
|
for (let i = 0; i < args.edits.length; i++) {
|
|
74
143
|
const edit = args.edits[i];
|
|
75
|
-
if (!edit || typeof edit.old_string !== 'string' || typeof edit.new_string !== 'string') {
|
|
144
|
+
if (!edit || typeof edit.old_string !== 'string' || edit.old_string.length === 0 || typeof edit.new_string !== 'string') {
|
|
76
145
|
return {
|
|
77
146
|
success: false,
|
|
78
|
-
error: `ATOMIC ROLLBACK: Edit index ${i} is missing valid old_string or new_string. Rollback complete.`
|
|
147
|
+
error: `ATOMIC ROLLBACK: Edit index ${i} is missing valid non-empty old_string or new_string. Rollback complete.`
|
|
79
148
|
};
|
|
80
149
|
}
|
|
81
150
|
// Try exact match first
|
|
82
151
|
if (workingContent.includes(edit.old_string)) {
|
|
83
|
-
workingContent = workingContent.replace(edit.old_string, edit.new_string);
|
|
152
|
+
workingContent = workingContent.replace(edit.old_string, () => edit.new_string);
|
|
84
153
|
replacements++;
|
|
85
154
|
continue;
|
|
86
155
|
}
|
|
87
156
|
// Fuzzy Match Fallback
|
|
88
157
|
const lines = workingContent.split('\n');
|
|
89
158
|
const oldLines = edit.old_string.split('\n');
|
|
90
|
-
let
|
|
159
|
+
let bestIndex = -1;
|
|
91
160
|
let highestConfidence = 0;
|
|
92
161
|
for (let j = 0; j <= lines.length - oldLines.length; j++) {
|
|
93
162
|
const chunk = lines.slice(j, j + oldLines.length).join('\n');
|
|
94
163
|
const conf = calculateConfidence(edit.old_string, chunk);
|
|
95
164
|
if (conf > highestConfidence) {
|
|
96
165
|
highestConfidence = conf;
|
|
97
|
-
|
|
166
|
+
bestIndex = j;
|
|
98
167
|
}
|
|
99
168
|
}
|
|
100
|
-
if (highestConfidence >= 90 &&
|
|
101
|
-
|
|
169
|
+
if (highestConfidence >= 90 && bestIndex !== -1) {
|
|
170
|
+
lines.splice(bestIndex, oldLines.length, edit.new_string);
|
|
171
|
+
workingContent = lines.join('\n');
|
|
102
172
|
replacements++;
|
|
103
173
|
}
|
|
104
174
|
else {
|
|
@@ -108,8 +178,8 @@ export const multiEdit = {
|
|
|
108
178
|
};
|
|
109
179
|
}
|
|
110
180
|
}
|
|
111
|
-
// Commit the transaction atomically
|
|
112
|
-
atomicWriteFileSync(target, workingContent, { encoding: 'utf8' });
|
|
181
|
+
// Commit the transaction atomically, preserving original file permissions
|
|
182
|
+
atomicWriteFileSync(target, workingContent, { encoding: 'utf8', mode: originalMode });
|
|
113
183
|
return { success: true, output: `Transaction Success: ${replacements} atomic edits applied to ${args.file_path}` };
|
|
114
184
|
}
|
|
115
185
|
catch (e) {
|
package/src/tools/multiEdit.ts
CHANGED
|
@@ -19,31 +19,90 @@ export interface MultiEditResult {
|
|
|
19
19
|
error?: string;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
export interface MultiEditContext {
|
|
23
|
+
workspaceRoot?: string;
|
|
24
|
+
workspacePaths?: string[];
|
|
25
|
+
grantAllAccess?: boolean;
|
|
26
|
+
parentLoop?: { workspaceRoot?: string };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function expandHome(value: string): string {
|
|
30
|
+
const trimmed = value.trim();
|
|
31
|
+
if (trimmed === '~') return os.homedir();
|
|
32
|
+
if (trimmed.startsWith('~/') || trimmed.startsWith('~\\')) {
|
|
33
|
+
return path.join(os.homedir(), trimmed.slice(2));
|
|
34
|
+
}
|
|
35
|
+
return trimmed;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function insideRoot(root: string, target: string): boolean {
|
|
39
|
+
const rel = path.relative(root, target);
|
|
40
|
+
return rel === '' || (rel !== '..' && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function assertWithinScope(target: string, context?: MultiEditContext): Promise<void> {
|
|
44
|
+
if (!context || context.grantAllAccess === true) return;
|
|
45
|
+
const configured = Array.isArray(context.workspacePaths)
|
|
46
|
+
? context.workspacePaths.filter((p): p is string => typeof p === 'string' && p.trim().length > 0).map(p => p.trim())
|
|
47
|
+
: [];
|
|
48
|
+
const roots = configured.length > 0
|
|
49
|
+
? configured
|
|
50
|
+
: [context.parentLoop?.workspaceRoot, context.workspaceRoot]
|
|
51
|
+
.filter((p): p is string => typeof p === 'string' && p.trim().length > 0)
|
|
52
|
+
.map(p => p.trim());
|
|
53
|
+
if (roots.length === 0) return;
|
|
54
|
+
const realTarget = await fs.realpath(target);
|
|
55
|
+
for (const root of roots) {
|
|
56
|
+
try {
|
|
57
|
+
const realRoot = await fs.realpath(path.resolve(expandHome(root)));
|
|
58
|
+
if (insideRoot(realRoot, realTarget)) return;
|
|
59
|
+
} catch {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
throw new Error(`Target resolves outside the selected workspace: ${target}`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const MAX_FUZZY_CHARS = 8_192;
|
|
67
|
+
|
|
22
68
|
function getLevenshteinDistance(a: string, b: string): number {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
69
|
+
let s1 = a;
|
|
70
|
+
let s2 = b;
|
|
71
|
+
if (s1.length > s2.length) {
|
|
72
|
+
const tmp = s1;
|
|
73
|
+
s1 = s2;
|
|
74
|
+
s2 = tmp;
|
|
75
|
+
}
|
|
76
|
+
const m = s1.length;
|
|
77
|
+
const n = s2.length;
|
|
78
|
+
if (m === 0) return n;
|
|
79
|
+
let prev = new Int32Array(m + 1);
|
|
80
|
+
let curr = new Int32Array(m + 1);
|
|
81
|
+
for (let j = 0; j <= m; j++) prev[j] = j;
|
|
82
|
+
|
|
83
|
+
for (let i = 1; i <= n; i++) {
|
|
84
|
+
curr[0] = i;
|
|
85
|
+
const ch2 = s2.charCodeAt(i - 1);
|
|
86
|
+
for (let j = 1; j <= m; j++) {
|
|
87
|
+
const cost = s1.charCodeAt(j - 1) === ch2 ? 0 : 1;
|
|
88
|
+
const del = prev[j]! + 1;
|
|
89
|
+
const ins = curr[j - 1]! + 1;
|
|
90
|
+
const sub = prev[j - 1]! + cost;
|
|
91
|
+
curr[j] = del < ins ? (del < sub ? del : sub) : (ins < sub ? ins : sub);
|
|
38
92
|
}
|
|
93
|
+
const swap = prev;
|
|
94
|
+
prev = curr;
|
|
95
|
+
curr = swap;
|
|
39
96
|
}
|
|
40
|
-
return
|
|
97
|
+
return prev[m]!;
|
|
41
98
|
}
|
|
42
99
|
|
|
43
100
|
function calculateConfidence(original: string, match: string): number {
|
|
44
|
-
const dist = getLevenshteinDistance(original, match);
|
|
45
101
|
const maxLen = Math.max(original.length, match.length);
|
|
46
102
|
if (maxLen === 0) return 100;
|
|
103
|
+
if (maxLen > MAX_FUZZY_CHARS) return 0;
|
|
104
|
+
if (Math.abs(original.length - match.length) / maxLen > 0.1) return 0;
|
|
105
|
+
const dist = getLevenshteinDistance(original, match);
|
|
47
106
|
return ((maxLen - dist) / maxLen) * 100;
|
|
48
107
|
}
|
|
49
108
|
|
|
@@ -72,7 +131,7 @@ export const multiEdit = {
|
|
|
72
131
|
}
|
|
73
132
|
},
|
|
74
133
|
|
|
75
|
-
async execute(args: MultiEditArgs): Promise<MultiEditResult> {
|
|
134
|
+
async execute(args: MultiEditArgs, context?: MultiEditContext): Promise<MultiEditResult> {
|
|
76
135
|
try {
|
|
77
136
|
if (!args || typeof args !== 'object') {
|
|
78
137
|
return { success: false, error: 'MultiEdit requires an object with file_path and edits array.' };
|
|
@@ -84,27 +143,39 @@ export const multiEdit = {
|
|
|
84
143
|
return { success: false, error: 'MultiEdit requires a non-empty edits array.' };
|
|
85
144
|
}
|
|
86
145
|
|
|
87
|
-
const
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
:
|
|
91
|
-
|
|
146
|
+
const expandedPath = expandHome(args.file_path);
|
|
147
|
+
const baseRoot = context?.parentLoop?.workspaceRoot?.trim()
|
|
148
|
+
|| context?.workspaceRoot?.trim()
|
|
149
|
+
|| context?.workspacePaths?.find((p): p is string => typeof p === 'string' && p.trim().length > 0)?.trim();
|
|
150
|
+
const target = path.isAbsolute(expandedPath)
|
|
151
|
+
? path.resolve(expandedPath)
|
|
152
|
+
: path.resolve(baseRoot ? path.resolve(expandHome(baseRoot)) : process.cwd(), expandedPath);
|
|
153
|
+
|
|
154
|
+
const stat = await fs.lstat(target);
|
|
155
|
+
if (stat.isSymbolicLink()) {
|
|
156
|
+
return { success: false, error: `Refusing to edit symbolic link ${target}; edit a regular file in the selected workspace.` };
|
|
157
|
+
}
|
|
158
|
+
if (!stat.isFile()) {
|
|
159
|
+
return { success: false, error: `Target is not a regular file: ${target}` };
|
|
160
|
+
}
|
|
161
|
+
await assertWithinScope(target, context);
|
|
162
|
+
const originalMode = stat.mode & 0o777;
|
|
92
163
|
const originalContent = await fs.readFile(target, 'utf8');
|
|
93
164
|
let workingContent = originalContent;
|
|
94
165
|
let replacements = 0;
|
|
95
166
|
|
|
96
167
|
for (let i = 0; i < args.edits.length; i++) {
|
|
97
168
|
const edit = args.edits[i];
|
|
98
|
-
if (!edit || typeof edit.old_string !== 'string' || typeof edit.new_string !== 'string') {
|
|
169
|
+
if (!edit || typeof edit.old_string !== 'string' || edit.old_string.length === 0 || typeof edit.new_string !== 'string') {
|
|
99
170
|
return {
|
|
100
171
|
success: false,
|
|
101
|
-
error: `ATOMIC ROLLBACK: Edit index ${i} is missing valid old_string or new_string. Rollback complete.`
|
|
172
|
+
error: `ATOMIC ROLLBACK: Edit index ${i} is missing valid non-empty old_string or new_string. Rollback complete.`
|
|
102
173
|
};
|
|
103
174
|
}
|
|
104
175
|
|
|
105
176
|
// Try exact match first
|
|
106
177
|
if (workingContent.includes(edit.old_string)) {
|
|
107
|
-
workingContent = workingContent.replace(edit.old_string, edit.new_string);
|
|
178
|
+
workingContent = workingContent.replace(edit.old_string, () => edit.new_string);
|
|
108
179
|
replacements++;
|
|
109
180
|
continue;
|
|
110
181
|
}
|
|
@@ -112,7 +183,7 @@ export const multiEdit = {
|
|
|
112
183
|
// Fuzzy Match Fallback
|
|
113
184
|
const lines = workingContent.split('\n');
|
|
114
185
|
const oldLines = edit.old_string.split('\n');
|
|
115
|
-
let
|
|
186
|
+
let bestIndex = -1;
|
|
116
187
|
let highestConfidence = 0;
|
|
117
188
|
|
|
118
189
|
for (let j = 0; j <= lines.length - oldLines.length; j++) {
|
|
@@ -120,12 +191,13 @@ export const multiEdit = {
|
|
|
120
191
|
const conf = calculateConfidence(edit.old_string, chunk);
|
|
121
192
|
if (conf > highestConfidence) {
|
|
122
193
|
highestConfidence = conf;
|
|
123
|
-
|
|
194
|
+
bestIndex = j;
|
|
124
195
|
}
|
|
125
196
|
}
|
|
126
197
|
|
|
127
|
-
if (highestConfidence >= 90 &&
|
|
128
|
-
|
|
198
|
+
if (highestConfidence >= 90 && bestIndex !== -1) {
|
|
199
|
+
lines.splice(bestIndex, oldLines.length, edit.new_string);
|
|
200
|
+
workingContent = lines.join('\n');
|
|
129
201
|
replacements++;
|
|
130
202
|
} else {
|
|
131
203
|
return {
|
|
@@ -135,8 +207,8 @@ export const multiEdit = {
|
|
|
135
207
|
}
|
|
136
208
|
}
|
|
137
209
|
|
|
138
|
-
// Commit the transaction atomically
|
|
139
|
-
atomicWriteFileSync(target, workingContent, { encoding: 'utf8' });
|
|
210
|
+
// Commit the transaction atomically, preserving original file permissions
|
|
211
|
+
atomicWriteFileSync(target, workingContent, { encoding: 'utf8', mode: originalMode });
|
|
140
212
|
return { success: true, output: `Transaction Success: ${replacements} atomic edits applied to ${args.file_path}` };
|
|
141
213
|
} catch (e: unknown) {
|
|
142
214
|
return { success: false, error: e instanceof Error ? e.message : String(e) };
|