@caupulican/pi-adaptative 0.84.1 → 0.85.3
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/CHANGELOG.md +53 -0
- package/dist/bundled-resources/extensions/tmux-agent-manager/index.d.ts.map +1 -1
- package/dist/bundled-resources/extensions/tmux-agent-manager/index.js +14 -8
- package/dist/bundled-resources/extensions/tmux-agent-manager/index.js.map +1 -1
- package/dist/bundled-resources/extensions/tmux-agent-manager/index.ts +14 -8
- package/dist/bundled-resources/runtimes/pi-shell-engine/arithmetic.py +364 -0
- package/dist/bundled-resources/runtimes/pi-shell-engine/context.py +2 -1
- package/dist/bundled-resources/runtimes/pi-shell-engine/errors.py +16 -0
- package/dist/bundled-resources/runtimes/pi-shell-engine/exec.py +137 -14
- package/dist/bundled-resources/runtimes/pi-shell-engine/nodes.py +23 -2
- package/dist/bundled-resources/runtimes/pi-shell-engine/parser.py +134 -6
- package/dist/bundled-resources/runtimes/pi-shell-engine/tokens.py +25 -2
- package/dist/bundled-resources/skills/deduplicate-by-evidence/SKILL.md +7 -5
- package/dist/bundled-resources/skills/worker-profile-writer/SKILL.md +63 -0
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +8 -0
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/autonomy/lane-tool-surface.d.ts.map +1 -1
- package/dist/core/autonomy/lane-tool-surface.js +4 -2
- package/dist/core/autonomy/lane-tool-surface.js.map +1 -1
- package/dist/core/background-lane-controller.d.ts +3 -0
- package/dist/core/background-lane-controller.d.ts.map +1 -1
- package/dist/core/background-lane-controller.js +6 -0
- package/dist/core/background-lane-controller.js.map +1 -1
- package/dist/core/compaction-controller.d.ts +2 -0
- package/dist/core/compaction-controller.d.ts.map +1 -1
- package/dist/core/compaction-controller.js +35 -13
- package/dist/core/compaction-controller.js.map +1 -1
- package/dist/core/default-tool-surface.d.ts.map +1 -1
- package/dist/core/default-tool-surface.js +1 -0
- package/dist/core/default-tool-surface.js.map +1 -1
- package/dist/core/delegation/worker-delegation-controller.d.ts +7 -0
- package/dist/core/delegation/worker-delegation-controller.d.ts.map +1 -1
- package/dist/core/delegation/worker-delegation-controller.js +28 -0
- package/dist/core/delegation/worker-delegation-controller.js.map +1 -1
- package/dist/core/delegation/worker-profile-resolver.d.ts +2 -0
- package/dist/core/delegation/worker-profile-resolver.d.ts.map +1 -1
- package/dist/core/delegation/worker-profile-resolver.js +46 -11
- package/dist/core/delegation/worker-profile-resolver.js.map +1 -1
- package/dist/core/doctor.d.ts +1 -1
- package/dist/core/doctor.d.ts.map +1 -1
- package/dist/core/doctor.js +5 -4
- package/dist/core/doctor.js.map +1 -1
- package/dist/core/failure-corpus.d.ts +26 -1
- package/dist/core/failure-corpus.d.ts.map +1 -1
- package/dist/core/failure-corpus.js +17 -0
- package/dist/core/failure-corpus.js.map +1 -1
- package/dist/core/model-capability.d.ts.map +1 -1
- package/dist/core/model-capability.js +1 -0
- package/dist/core/model-capability.js.map +1 -1
- package/dist/core/model-tool-protocol.d.ts +3 -2
- package/dist/core/model-tool-protocol.d.ts.map +1 -1
- package/dist/core/model-tool-protocol.js +5 -1
- package/dist/core/model-tool-protocol.js.map +1 -1
- package/dist/core/models/runtime-process.d.ts +7 -1
- package/dist/core/models/runtime-process.d.ts.map +1 -1
- package/dist/core/models/runtime-process.js +4 -2
- package/dist/core/models/runtime-process.js.map +1 -1
- package/dist/core/orchestration/lane-tool-manifests.d.ts +1 -1
- package/dist/core/orchestration/lane-tool-manifests.d.ts.map +1 -1
- package/dist/core/orchestration/lane-tool-manifests.js +6 -1
- package/dist/core/orchestration/lane-tool-manifests.js.map +1 -1
- package/dist/core/orchestration/session-task-profile-store.d.ts +31 -0
- package/dist/core/orchestration/session-task-profile-store.d.ts.map +1 -0
- package/dist/core/orchestration/session-task-profile-store.js +138 -0
- package/dist/core/orchestration/session-task-profile-store.js.map +1 -0
- package/dist/core/orchestration/task-profile-writer.d.ts +58 -0
- package/dist/core/orchestration/task-profile-writer.d.ts.map +1 -0
- package/dist/core/orchestration/task-profile-writer.js +165 -0
- package/dist/core/orchestration/task-profile-writer.js.map +1 -0
- package/dist/core/process-matrix/runtime.js +43 -11
- package/dist/core/process-matrix/runtime.js.map +1 -1
- package/dist/core/resource-loader.d.ts +2 -0
- package/dist/core/resource-loader.d.ts.map +1 -1
- package/dist/core/resource-loader.js +18 -6
- package/dist/core/resource-loader.js.map +1 -1
- package/dist/core/runtime-builder.d.ts +3 -1
- package/dist/core/runtime-builder.d.ts.map +1 -1
- package/dist/core/runtime-builder.js +14 -0
- package/dist/core/runtime-builder.js.map +1 -1
- package/dist/core/security/untrusted-boundary.d.ts.map +1 -1
- package/dist/core/security/untrusted-boundary.js +4 -6
- package/dist/core/security/untrusted-boundary.js.map +1 -1
- package/dist/core/session-role.d.ts.map +1 -1
- package/dist/core/session-role.js +1 -0
- package/dist/core/session-role.js.map +1 -1
- package/dist/core/system-prompt.d.ts.map +1 -1
- package/dist/core/system-prompt.js +21 -23
- package/dist/core/system-prompt.js.map +1 -1
- package/dist/core/tool-capability-policy.js +1 -1
- package/dist/core/tool-capability-policy.js.map +1 -1
- package/dist/core/tool-recovery-log-records.d.ts +33 -1
- package/dist/core/tool-recovery-log-records.d.ts.map +1 -1
- package/dist/core/tool-recovery-log-records.js +59 -12
- package/dist/core/tool-recovery-log-records.js.map +1 -1
- package/dist/core/tool-recovery-logger.d.ts +12 -1
- package/dist/core/tool-recovery-logger.d.ts.map +1 -1
- package/dist/core/tool-recovery-logger.js +24 -1
- package/dist/core/tool-recovery-logger.js.map +1 -1
- package/dist/core/tool-repair-health.d.ts.map +1 -1
- package/dist/core/tool-repair-health.js +6 -0
- package/dist/core/tool-repair-health.js.map +1 -1
- package/dist/core/tools/bash.d.ts +3 -8
- package/dist/core/tools/bash.d.ts.map +1 -1
- package/dist/core/tools/bash.js +4 -28
- package/dist/core/tools/bash.js.map +1 -1
- package/dist/core/tools/delegate.js +4 -4
- package/dist/core/tools/delegate.js.map +1 -1
- package/dist/core/tools/edit-diff.d.ts +32 -5
- package/dist/core/tools/edit-diff.d.ts.map +1 -1
- package/dist/core/tools/edit-diff.js +137 -69
- package/dist/core/tools/edit-diff.js.map +1 -1
- package/dist/core/tools/edit.d.ts +22 -7
- package/dist/core/tools/edit.d.ts.map +1 -1
- package/dist/core/tools/edit.js +128 -55
- package/dist/core/tools/edit.js.map +1 -1
- package/dist/core/tools/file-mutation-intent.d.ts +70 -0
- package/dist/core/tools/file-mutation-intent.d.ts.map +1 -0
- package/dist/core/tools/file-mutation-intent.js +325 -0
- package/dist/core/tools/file-mutation-intent.js.map +1 -0
- package/dist/core/tools/find.d.ts +2 -1
- package/dist/core/tools/find.d.ts.map +1 -1
- package/dist/core/tools/find.js +2 -6
- package/dist/core/tools/find.js.map +1 -1
- package/dist/core/tools/goal.d.ts.map +1 -1
- package/dist/core/tools/goal.js +41 -18
- package/dist/core/tools/goal.js.map +1 -1
- package/dist/core/tools/grep.d.ts +2 -1
- package/dist/core/tools/grep.d.ts.map +1 -1
- package/dist/core/tools/grep.js +2 -6
- package/dist/core/tools/grep.js.map +1 -1
- package/dist/core/tools/index.d.ts +3 -1
- package/dist/core/tools/index.d.ts.map +1 -1
- package/dist/core/tools/index.js +31 -8
- package/dist/core/tools/index.js.map +1 -1
- package/dist/core/tools/managed-search-tool.d.ts +8 -0
- package/dist/core/tools/managed-search-tool.d.ts.map +1 -0
- package/dist/core/tools/managed-search-tool.js +10 -0
- package/dist/core/tools/managed-search-tool.js.map +1 -0
- package/dist/core/tools/profile-writer.d.ts +33 -0
- package/dist/core/tools/profile-writer.d.ts.map +1 -0
- package/dist/core/tools/profile-writer.js +67 -0
- package/dist/core/tools/profile-writer.js.map +1 -0
- package/dist/core/tools/shell-command-parser.d.ts +4 -3
- package/dist/core/tools/shell-command-parser.d.ts.map +1 -1
- package/dist/core/tools/shell-command-parser.js +32 -7
- package/dist/core/tools/shell-command-parser.js.map +1 -1
- package/dist/core/tools/shell-contract-router.d.ts.map +1 -1
- package/dist/core/tools/shell-contract-router.js +23 -1
- package/dist/core/tools/shell-contract-router.js.map +1 -1
- package/dist/core/tools/task-steps.d.ts.map +1 -1
- package/dist/core/tools/task-steps.js +6 -11
- package/dist/core/tools/task-steps.js.map +1 -1
- package/dist/core/tools/tmux-dispatch.d.ts +2 -0
- package/dist/core/tools/tmux-dispatch.d.ts.map +1 -1
- package/dist/core/tools/tmux-dispatch.js +16 -2
- package/dist/core/tools/tmux-dispatch.js.map +1 -1
- package/dist/core/tools/windows-shell-engine.js +1 -1
- package/dist/core/tools/windows-shell-engine.js.map +1 -1
- package/dist/core/tools/write.d.ts +24 -7
- package/dist/core/tools/write.d.ts.map +1 -1
- package/dist/core/tools/write.js +182 -107
- package/dist/core/tools/write.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/jscpd-cli.d.ts +3 -0
- package/dist/jscpd-cli.d.ts.map +1 -0
- package/dist/jscpd-cli.js +22 -0
- package/dist/jscpd-cli.js.map +1 -0
- package/dist/modes/interactive/components/profile-resource-editor.js +2 -2
- package/dist/modes/interactive/components/profile-resource-editor.js.map +1 -1
- package/dist/modes/interactive/components/tool-execution.d.ts +1 -0
- package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
- package/dist/modes/interactive/components/tool-execution.js +85 -3
- package/dist/modes/interactive/components/tool-execution.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +1 -1
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/utils/bundled-jscpd.d.ts +20 -0
- package/dist/utils/bundled-jscpd.d.ts.map +1 -0
- package/dist/utils/bundled-jscpd.js +154 -0
- package/dist/utils/bundled-jscpd.js.map +1 -0
- package/dist/utils/paths.js +1 -1
- package/dist/utils/paths.js.map +1 -1
- package/dist/utils/shell.d.ts.map +1 -1
- package/dist/utils/shell.js +11 -0
- package/dist/utils/shell.js.map +1 -1
- package/dist/utils/tools-manager.d.ts +19 -2
- package/dist/utils/tools-manager.d.ts.map +1 -1
- package/dist/utils/tools-manager.js +92 -40
- package/dist/utils/tools-manager.js.map +1 -1
- package/docs/index.md +3 -0
- package/docs/managed-data-tools.md +15 -0
- package/docs/models.md +1 -1
- package/docs/quickstart.md +2 -2
- package/docs/settings.md +1 -1
- package/docs/tool-boundary-performance-roadmap-2026-08-02.md +166 -0
- package/docs/tool-repair.md +4 -4
- package/docs/windows.md +7 -5
- package/docs/worker-profiles.md +16 -0
- package/examples/extensions/built-in-tool-renderer.ts +12 -5
- package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
- package/examples/extensions/custom-provider-anthropic/package.json +1 -1
- package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/examples/extensions/minimal-mode.ts +8 -7
- package/examples/extensions/prompt-customizer.ts +4 -2
- package/examples/extensions/sandbox/package-lock.json +2 -2
- package/examples/extensions/sandbox/package.json +1 -1
- package/examples/extensions/ssh.ts +113 -24
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/npm-shrinkwrap.json +112 -12
- package/package.json +7 -5
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { constants } from "node:fs";
|
|
3
|
+
import { access, copyFile, lstat, open, stat, unlink } from "node:fs/promises";
|
|
4
|
+
import { dirname, resolve } from "node:path";
|
|
5
|
+
const DEFAULT_INTENT_LIMIT = 64;
|
|
6
|
+
const DEFAULT_CONTENT_REFERENCE_LIMIT = 64;
|
|
7
|
+
const DEFAULT_INTENT_TTL_MS = 10 * 60 * 1000;
|
|
8
|
+
const DEFAULT_CONTENT_REFERENCE_TTL_MS = 60 * 60 * 1000;
|
|
9
|
+
const HASH_BUFFER_BYTES = 64 * 1024;
|
|
10
|
+
const CONTENT_REFERENCE_PREFIX = "file-content:";
|
|
11
|
+
function isMissingPathError(error) {
|
|
12
|
+
return (typeof error === "object" &&
|
|
13
|
+
error !== null &&
|
|
14
|
+
"code" in error &&
|
|
15
|
+
(error.code === "ENOENT" || error.code === "ENOTDIR"));
|
|
16
|
+
}
|
|
17
|
+
function errorCode(error) {
|
|
18
|
+
return typeof error === "object" && error !== null && "code" in error && typeof error.code === "string"
|
|
19
|
+
? error.code
|
|
20
|
+
: undefined;
|
|
21
|
+
}
|
|
22
|
+
function normalizedPath(path) {
|
|
23
|
+
const absolutePath = resolve(path);
|
|
24
|
+
return process.platform === "win32" ? absolutePath.toLocaleLowerCase("en-US") : absolutePath;
|
|
25
|
+
}
|
|
26
|
+
function positiveBound(value, fallback, label) {
|
|
27
|
+
const resolvedValue = value ?? fallback;
|
|
28
|
+
if (!Number.isSafeInteger(resolvedValue) || resolvedValue < 1) {
|
|
29
|
+
throw new TypeError(`${label} must be a positive safe integer.`);
|
|
30
|
+
}
|
|
31
|
+
return resolvedValue;
|
|
32
|
+
}
|
|
33
|
+
function normalizedIdentity(value) {
|
|
34
|
+
return {
|
|
35
|
+
dev: String(value.dev),
|
|
36
|
+
ino: String(value.ino),
|
|
37
|
+
mode: String(value.mode),
|
|
38
|
+
size: String(value.size),
|
|
39
|
+
mtimeMs: String(value.mtimeMs),
|
|
40
|
+
ctimeMs: String(value.ctimeMs),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function identitiesMatch(left, right) {
|
|
44
|
+
return (left !== undefined &&
|
|
45
|
+
right !== undefined &&
|
|
46
|
+
left.dev === right.dev &&
|
|
47
|
+
left.ino === right.ino &&
|
|
48
|
+
left.mode === right.mode &&
|
|
49
|
+
left.size === right.size &&
|
|
50
|
+
left.mtimeMs === right.mtimeMs &&
|
|
51
|
+
left.ctimeMs === right.ctimeMs);
|
|
52
|
+
}
|
|
53
|
+
async function inspectLocalPath(path, followSymlinks) {
|
|
54
|
+
try {
|
|
55
|
+
const result = followSymlinks ? await stat(path, { bigint: true }) : await lstat(path, { bigint: true });
|
|
56
|
+
return {
|
|
57
|
+
kind: result.isFile() ? "file" : result.isDirectory() ? "directory" : "other",
|
|
58
|
+
identity: normalizedIdentity(result),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
if (isMissingPathError(error))
|
|
63
|
+
return undefined;
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async function hashLocalFile(path, signal) {
|
|
68
|
+
const handle = await open(path, "r");
|
|
69
|
+
const hash = createHash("sha256");
|
|
70
|
+
const buffer = Buffer.allocUnsafe(HASH_BUFFER_BYTES);
|
|
71
|
+
try {
|
|
72
|
+
let position = 0;
|
|
73
|
+
while (true) {
|
|
74
|
+
if (signal?.aborted)
|
|
75
|
+
throw new Error("Operation aborted");
|
|
76
|
+
const { bytesRead } = await handle.read(buffer, 0, buffer.length, position);
|
|
77
|
+
if (bytesRead === 0)
|
|
78
|
+
break;
|
|
79
|
+
hash.update(buffer.subarray(0, bytesRead));
|
|
80
|
+
position += bytesRead;
|
|
81
|
+
}
|
|
82
|
+
return hash.digest("hex");
|
|
83
|
+
}
|
|
84
|
+
finally {
|
|
85
|
+
await handle.close();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
export const localFileMutationIntentOperations = {
|
|
89
|
+
inspect: inspectLocalPath,
|
|
90
|
+
access,
|
|
91
|
+
copyFileExclusive: (sourcePath, targetPath) => copyFile(sourcePath, targetPath, constants.COPYFILE_EXCL),
|
|
92
|
+
hashFile: hashLocalFile,
|
|
93
|
+
removeFile: unlink,
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* Session-owned authority for two-phase file mutation intents and bounded exact-content references.
|
|
97
|
+
* It retains only fixed-size metadata; file bytes remain owned by the filesystem.
|
|
98
|
+
*/
|
|
99
|
+
export class FileMutationIntentController {
|
|
100
|
+
constructor(options = {}) {
|
|
101
|
+
this.intents = new Map();
|
|
102
|
+
this.intentByPath = new Map();
|
|
103
|
+
this.contentReferences = new Map();
|
|
104
|
+
this.operations = options.operations ?? localFileMutationIntentOperations;
|
|
105
|
+
this.intentLimit = positiveBound(options.intentLimit, DEFAULT_INTENT_LIMIT, "Intent limit");
|
|
106
|
+
this.contentReferenceLimit = positiveBound(options.contentReferenceLimit, DEFAULT_CONTENT_REFERENCE_LIMIT, "Content reference limit");
|
|
107
|
+
this.intentTtlMs = positiveBound(options.intentTtlMs, DEFAULT_INTENT_TTL_MS, "Intent TTL");
|
|
108
|
+
this.contentReferenceTtlMs = positiveBound(options.contentReferenceTtlMs, DEFAULT_CONTENT_REFERENCE_TTL_MS, "Content reference TTL");
|
|
109
|
+
this.now = options.now ?? Date.now;
|
|
110
|
+
}
|
|
111
|
+
async prepare(kind, path, signal, displayPath = path) {
|
|
112
|
+
this.pruneExpired();
|
|
113
|
+
if (signal?.aborted)
|
|
114
|
+
throw new Error("Operation aborted");
|
|
115
|
+
const absolutePath = resolve(path);
|
|
116
|
+
let identity;
|
|
117
|
+
if (kind === "write") {
|
|
118
|
+
const existing = await this.operations.inspect(absolutePath, false);
|
|
119
|
+
if (existing)
|
|
120
|
+
throw new Error(`Write collision: ${displayPath} already exists.`);
|
|
121
|
+
await this.assertCreatableParent(absolutePath);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
const existing = await this.operations.inspect(absolutePath, true);
|
|
125
|
+
if (!existing)
|
|
126
|
+
throw new Error(`Could not edit file: ${displayPath}. Error code: ENOENT.`);
|
|
127
|
+
if (existing.kind !== "file")
|
|
128
|
+
throw new Error(`Could not edit file: ${displayPath}. Target is not a regular file.`);
|
|
129
|
+
try {
|
|
130
|
+
await this.operations.access(absolutePath, constants.R_OK | constants.W_OK);
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
const code = errorCode(error);
|
|
134
|
+
throw new Error(`Could not edit file: ${displayPath}. ${code ? `Error code: ${code}` : String(error)}.`);
|
|
135
|
+
}
|
|
136
|
+
identity = existing.identity;
|
|
137
|
+
}
|
|
138
|
+
if (signal?.aborted)
|
|
139
|
+
throw new Error("Operation aborted");
|
|
140
|
+
const key = `${kind}\0${normalizedPath(absolutePath)}`;
|
|
141
|
+
const priorIntentId = this.intentByPath.get(key);
|
|
142
|
+
if (priorIntentId)
|
|
143
|
+
this.deleteIntent(priorIntentId);
|
|
144
|
+
while (this.intents.size >= this.intentLimit) {
|
|
145
|
+
const oldestIntentId = this.intents.keys().next().value;
|
|
146
|
+
if (typeof oldestIntentId !== "string")
|
|
147
|
+
break;
|
|
148
|
+
this.deleteIntent(oldestIntentId);
|
|
149
|
+
}
|
|
150
|
+
const intentId = randomUUID();
|
|
151
|
+
const record = {
|
|
152
|
+
intentId,
|
|
153
|
+
kind,
|
|
154
|
+
path: absolutePath,
|
|
155
|
+
displayPath,
|
|
156
|
+
...(identity ? { identity } : {}),
|
|
157
|
+
expiresAt: this.now() + this.intentTtlMs,
|
|
158
|
+
};
|
|
159
|
+
this.intents.set(intentId, record);
|
|
160
|
+
this.intentByPath.set(key, intentId);
|
|
161
|
+
return { intentId, kind, path: absolutePath, displayPath };
|
|
162
|
+
}
|
|
163
|
+
consume(intentId, kind, path) {
|
|
164
|
+
this.pruneExpired();
|
|
165
|
+
const record = this.intents.get(intentId);
|
|
166
|
+
if (!record)
|
|
167
|
+
throw new Error("File mutation intent is invalid, expired, or belongs to another session.");
|
|
168
|
+
this.deleteIntent(intentId);
|
|
169
|
+
if (record.kind !== kind)
|
|
170
|
+
throw new Error(`File mutation intent is for ${record.kind}, not ${kind}.`);
|
|
171
|
+
if (normalizedPath(record.path) !== normalizedPath(path)) {
|
|
172
|
+
throw new Error("File mutation intent path does not match the commit path.");
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
intentId: record.intentId,
|
|
176
|
+
kind: record.kind,
|
|
177
|
+
path: record.path,
|
|
178
|
+
displayPath: record.displayPath,
|
|
179
|
+
...(record.identity ? { identity: record.identity } : {}),
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
async assertCurrent(lease, signal) {
|
|
183
|
+
if (signal?.aborted)
|
|
184
|
+
throw new Error("Operation aborted");
|
|
185
|
+
if (lease.kind === "write") {
|
|
186
|
+
if (await this.operations.inspect(lease.path, false)) {
|
|
187
|
+
throw new Error(`Write collision: ${lease.displayPath} already exists; the prepared intent is stale.`);
|
|
188
|
+
}
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
const current = await this.operations.inspect(lease.path, true);
|
|
192
|
+
if (!current || current.kind !== "file") {
|
|
193
|
+
throw new Error(`Could not edit file: ${lease.displayPath}. The prepared target no longer exists.`);
|
|
194
|
+
}
|
|
195
|
+
if (!identitiesMatch(lease.identity, current.identity)) {
|
|
196
|
+
throw new Error(`Could not edit file: ${lease.displayPath}. The file changed after preparation; prepare again.`);
|
|
197
|
+
}
|
|
198
|
+
try {
|
|
199
|
+
await this.operations.access(lease.path, constants.R_OK | constants.W_OK);
|
|
200
|
+
}
|
|
201
|
+
catch (error) {
|
|
202
|
+
const code = errorCode(error);
|
|
203
|
+
throw new Error(`Could not edit file: ${lease.displayPath}. ${code ? `Error code: ${code}` : String(error)}.`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
rememberContent(sourcePath, content) {
|
|
207
|
+
const digest = createHash("sha256").update(content, "utf8").digest("hex");
|
|
208
|
+
return this.rememberContentDigest(sourcePath, digest, Buffer.byteLength(content, "utf8"));
|
|
209
|
+
}
|
|
210
|
+
async copyReferencedContent(contentRef, targetPath, signal) {
|
|
211
|
+
this.pruneExpired();
|
|
212
|
+
const record = this.contentReferences.get(contentRef);
|
|
213
|
+
if (!record)
|
|
214
|
+
throw new Error("File content reference is invalid, expired, or belongs to another session.");
|
|
215
|
+
this.touchContentReference(record);
|
|
216
|
+
if (signal?.aborted)
|
|
217
|
+
throw new Error("Operation aborted");
|
|
218
|
+
let sourceDigest;
|
|
219
|
+
try {
|
|
220
|
+
sourceDigest = await this.operations.hashFile(record.sourcePath, signal);
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
throw new Error(`File content reference source is unavailable: ${String(error)}.`);
|
|
224
|
+
}
|
|
225
|
+
if (sourceDigest !== record.digest) {
|
|
226
|
+
this.contentReferences.delete(contentRef);
|
|
227
|
+
throw new Error("File content reference source changed after it was cached.");
|
|
228
|
+
}
|
|
229
|
+
await this.operations.copyFileExclusive(record.sourcePath, targetPath);
|
|
230
|
+
let targetDigest;
|
|
231
|
+
try {
|
|
232
|
+
targetDigest = await this.operations.hashFile(targetPath, signal);
|
|
233
|
+
}
|
|
234
|
+
catch (error) {
|
|
235
|
+
await this.removeFailedCopy(targetPath, error);
|
|
236
|
+
throw error;
|
|
237
|
+
}
|
|
238
|
+
if (targetDigest !== record.digest) {
|
|
239
|
+
await this.removeFailedCopy(targetPath, new Error("copied bytes did not match the cached digest"));
|
|
240
|
+
throw new Error("File content copy changed while it was being created; the target was removed.");
|
|
241
|
+
}
|
|
242
|
+
return this.rememberContentDigest(targetPath, record.digest, record.byteLength);
|
|
243
|
+
}
|
|
244
|
+
async assertCreatableParent(path) {
|
|
245
|
+
let candidate = dirname(path);
|
|
246
|
+
while (true) {
|
|
247
|
+
const inspection = await this.operations.inspect(candidate, true);
|
|
248
|
+
if (inspection) {
|
|
249
|
+
if (inspection.kind !== "directory") {
|
|
250
|
+
throw new Error(`Write parent path is not a directory: ${candidate}.`);
|
|
251
|
+
}
|
|
252
|
+
try {
|
|
253
|
+
await this.operations.access(candidate, constants.W_OK);
|
|
254
|
+
}
|
|
255
|
+
catch (error) {
|
|
256
|
+
const code = errorCode(error);
|
|
257
|
+
throw new Error(`Write parent is not writable: ${candidate}${code ? ` (${code})` : ""}.`);
|
|
258
|
+
}
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
const parent = dirname(candidate);
|
|
262
|
+
if (parent === candidate)
|
|
263
|
+
throw new Error(`No writable parent exists for ${path}.`);
|
|
264
|
+
candidate = parent;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
rememberContentDigest(sourcePath, digest, byteLength) {
|
|
268
|
+
this.pruneExpired();
|
|
269
|
+
for (const existing of this.contentReferences.values()) {
|
|
270
|
+
if (existing.sourcePath === sourcePath && existing.digest === digest) {
|
|
271
|
+
this.touchContentReference(existing);
|
|
272
|
+
return { contentRef: existing.contentRef, byteLength: existing.byteLength };
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
while (this.contentReferences.size >= this.contentReferenceLimit) {
|
|
276
|
+
const oldestContentRef = this.contentReferences.keys().next().value;
|
|
277
|
+
if (typeof oldestContentRef !== "string")
|
|
278
|
+
break;
|
|
279
|
+
this.contentReferences.delete(oldestContentRef);
|
|
280
|
+
}
|
|
281
|
+
const contentRef = `${CONTENT_REFERENCE_PREFIX}${randomUUID()}`;
|
|
282
|
+
this.contentReferences.set(contentRef, {
|
|
283
|
+
contentRef,
|
|
284
|
+
sourcePath,
|
|
285
|
+
digest,
|
|
286
|
+
byteLength,
|
|
287
|
+
expiresAt: this.now() + this.contentReferenceTtlMs,
|
|
288
|
+
});
|
|
289
|
+
return { contentRef, byteLength };
|
|
290
|
+
}
|
|
291
|
+
touchContentReference(record) {
|
|
292
|
+
record.expiresAt = this.now() + this.contentReferenceTtlMs;
|
|
293
|
+
this.contentReferences.delete(record.contentRef);
|
|
294
|
+
this.contentReferences.set(record.contentRef, record);
|
|
295
|
+
}
|
|
296
|
+
async removeFailedCopy(path, cause) {
|
|
297
|
+
try {
|
|
298
|
+
await this.operations.removeFile(path);
|
|
299
|
+
}
|
|
300
|
+
catch (cleanupError) {
|
|
301
|
+
throw new Error(`Copied content verification failed (${String(cause)}), and the newly created target could not be removed: ${String(cleanupError)}.`);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
deleteIntent(intentId) {
|
|
305
|
+
const record = this.intents.get(intentId);
|
|
306
|
+
if (!record)
|
|
307
|
+
return;
|
|
308
|
+
this.intents.delete(intentId);
|
|
309
|
+
const key = `${record.kind}\0${normalizedPath(record.path)}`;
|
|
310
|
+
if (this.intentByPath.get(key) === intentId)
|
|
311
|
+
this.intentByPath.delete(key);
|
|
312
|
+
}
|
|
313
|
+
pruneExpired() {
|
|
314
|
+
const now = this.now();
|
|
315
|
+
for (const [intentId, record] of this.intents) {
|
|
316
|
+
if (record.expiresAt <= now)
|
|
317
|
+
this.deleteIntent(intentId);
|
|
318
|
+
}
|
|
319
|
+
for (const [contentRef, record] of this.contentReferences) {
|
|
320
|
+
if (record.expiresAt <= now)
|
|
321
|
+
this.contentReferences.delete(contentRef);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
//# sourceMappingURL=file-mutation-intent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-mutation-intent.js","sourceRoot":"","sources":["../../../src/core/tools/file-mutation-intent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC/E,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAE7C,MAAM,oBAAoB,GAAG,EAAE,CAAC;AAChC,MAAM,+BAA+B,GAAG,EAAE,CAAC;AAC3C,MAAM,qBAAqB,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAC7C,MAAM,gCAAgC,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AACxD,MAAM,iBAAiB,GAAG,EAAE,GAAG,IAAI,CAAC;AACpC,MAAM,wBAAwB,GAAG,eAAe,CAAC;AA+DjD,SAAS,kBAAkB,CAAC,KAAc;IACzC,OAAO,CACN,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,MAAM,IAAI,KAAK;QACf,CAA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AAC,QAAQ,CAAC,CAAC;QAC9B,MAAM,GAAG,GAAG,GAAG,MAAM,CAAC,IAAI,KAAK,cAAc,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;QAC7D,IAAI,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,QAAQ;YAAE,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAC5E,CAAC;IAEO,YAAY;QACnB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACvB,KAAK,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YAC/C,IAAI,MAAM,CAAC,SAAS,IAAI,GAAG;gBAAE,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC;QAC1D,CAAC;QACD,KAAK,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,IAAI,IAAI,CAAC,iBAAiB,EAAE,CAAC;YAC3D,IAAI,MAAM,CAAC,SAAS,IAAI,GAAG;gBAAE,IAAI,CAAC,iBAAiB,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;QACxE,CAAC;IACF,CAAC;CACD","sourcesContent":["import { createHash, randomUUID } from \"node:crypto\";\nimport { constants } from \"node:fs\";\nimport { access, copyFile, lstat, open, stat, unlink } from \"node:fs/promises\";\nimport { dirname, resolve } from \"node:path\";\n\nconst DEFAULT_INTENT_LIMIT = 64;\nconst DEFAULT_CONTENT_REFERENCE_LIMIT = 64;\nconst DEFAULT_INTENT_TTL_MS = 10 * 60 * 1000;\nconst DEFAULT_CONTENT_REFERENCE_TTL_MS = 60 * 60 * 1000;\nconst HASH_BUFFER_BYTES = 64 * 1024;\nconst CONTENT_REFERENCE_PREFIX = \"file-content:\";\n\nexport type FileMutationKind = \"write\" | \"edit\";\n\nexport interface FilePathIdentity {\n\tdev: string;\n\tino: string;\n\tmode: string;\n\tsize: string;\n\tmtimeMs: string;\n\tctimeMs: string;\n}\n\nexport interface FilePathInspection {\n\tkind: \"file\" | \"directory\" | \"other\";\n\tidentity: FilePathIdentity;\n}\n\nexport interface FileMutationIntentOperations {\n\tinspect(path: string, followSymlinks: boolean): Promise<FilePathInspection | undefined>;\n\taccess(path: string, mode: number): Promise<void>;\n\tcopyFileExclusive(sourcePath: string, targetPath: string): Promise<void>;\n\thashFile(path: string, signal?: AbortSignal): Promise<string>;\n\tremoveFile(path: string): Promise<void>;\n}\n\nexport interface PreparedFileMutation {\n\tintentId: string;\n\tkind: FileMutationKind;\n\tpath: string;\n\tdisplayPath: string;\n}\n\nexport interface FileMutationLease extends PreparedFileMutation {\n\tidentity?: FilePathIdentity;\n}\n\nexport interface FileContentReference {\n\tcontentRef: string;\n\tbyteLength: number;\n}\n\ninterface MutationIntentRecord extends FileMutationLease {\n\texpiresAt: number;\n}\n\ninterface ContentReferenceRecord {\n\tcontentRef: string;\n\tsourcePath: string;\n\tdigest: string;\n\tbyteLength: number;\n\texpiresAt: number;\n}\n\nexport interface FileMutationIntentControllerOptions {\n\toperations?: FileMutationIntentOperations;\n\tintentLimit?: number;\n\tcontentReferenceLimit?: number;\n\tintentTtlMs?: number;\n\tcontentReferenceTtlMs?: number;\n\tnow?: () => number;\n}\n\nfunction isMissingPathError(error: unknown): boolean {\n\treturn (\n\t\ttypeof error === \"object\" &&\n\t\terror !== null &&\n\t\t\"code\" in error &&\n\t\t(error.code === \"ENOENT\" || error.code === \"ENOTDIR\")\n\t);\n}\n\nfunction errorCode(error: unknown): string | undefined {\n\treturn typeof error === \"object\" && error !== null && \"code\" in error && typeof error.code === \"string\"\n\t\t? error.code\n\t\t: undefined;\n}\n\nfunction normalizedPath(path: string): string {\n\tconst absolutePath = resolve(path);\n\treturn process.platform === \"win32\" ? absolutePath.toLocaleLowerCase(\"en-US\") : absolutePath;\n}\n\nfunction positiveBound(value: number | undefined, fallback: number, label: string): number {\n\tconst resolvedValue = value ?? fallback;\n\tif (!Number.isSafeInteger(resolvedValue) || resolvedValue < 1) {\n\t\tthrow new TypeError(`${label} must be a positive safe integer.`);\n\t}\n\treturn resolvedValue;\n}\n\nfunction normalizedIdentity(value: {\n\tdev: number | bigint;\n\tino: number | bigint;\n\tmode: number | bigint;\n\tsize: number | bigint;\n\tmtimeMs: number | bigint;\n\tctimeMs: number | bigint;\n}): FilePathIdentity {\n\treturn {\n\t\tdev: String(value.dev),\n\t\tino: String(value.ino),\n\t\tmode: String(value.mode),\n\t\tsize: String(value.size),\n\t\tmtimeMs: String(value.mtimeMs),\n\t\tctimeMs: String(value.ctimeMs),\n\t};\n}\n\nfunction identitiesMatch(left: FilePathIdentity | undefined, right: FilePathIdentity | undefined): boolean {\n\treturn (\n\t\tleft !== undefined &&\n\t\tright !== undefined &&\n\t\tleft.dev === right.dev &&\n\t\tleft.ino === right.ino &&\n\t\tleft.mode === right.mode &&\n\t\tleft.size === right.size &&\n\t\tleft.mtimeMs === right.mtimeMs &&\n\t\tleft.ctimeMs === right.ctimeMs\n\t);\n}\n\nasync function inspectLocalPath(path: string, followSymlinks: boolean): Promise<FilePathInspection | undefined> {\n\ttry {\n\t\tconst result = followSymlinks ? await stat(path, { bigint: true }) : await lstat(path, { bigint: true });\n\t\treturn {\n\t\t\tkind: result.isFile() ? \"file\" : result.isDirectory() ? \"directory\" : \"other\",\n\t\t\tidentity: normalizedIdentity(result),\n\t\t};\n\t} catch (error) {\n\t\tif (isMissingPathError(error)) return undefined;\n\t\tthrow error;\n\t}\n}\n\nasync function hashLocalFile(path: string, signal?: AbortSignal): Promise<string> {\n\tconst handle = await open(path, \"r\");\n\tconst hash = createHash(\"sha256\");\n\tconst buffer = Buffer.allocUnsafe(HASH_BUFFER_BYTES);\n\ttry {\n\t\tlet position = 0;\n\t\twhile (true) {\n\t\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\t\tconst { bytesRead } = await handle.read(buffer, 0, buffer.length, position);\n\t\t\tif (bytesRead === 0) break;\n\t\t\thash.update(buffer.subarray(0, bytesRead));\n\t\t\tposition += bytesRead;\n\t\t}\n\t\treturn hash.digest(\"hex\");\n\t} finally {\n\t\tawait handle.close();\n\t}\n}\n\nexport const localFileMutationIntentOperations: FileMutationIntentOperations = {\n\tinspect: inspectLocalPath,\n\taccess,\n\tcopyFileExclusive: (sourcePath, targetPath) => copyFile(sourcePath, targetPath, constants.COPYFILE_EXCL),\n\thashFile: hashLocalFile,\n\tremoveFile: unlink,\n};\n\n/**\n * Session-owned authority for two-phase file mutation intents and bounded exact-content references.\n * It retains only fixed-size metadata; file bytes remain owned by the filesystem.\n */\nexport class FileMutationIntentController {\n\tprivate readonly operations: FileMutationIntentOperations;\n\tprivate readonly intentLimit: number;\n\tprivate readonly contentReferenceLimit: number;\n\tprivate readonly intentTtlMs: number;\n\tprivate readonly contentReferenceTtlMs: number;\n\tprivate readonly now: () => number;\n\tprivate readonly intents = new Map<string, MutationIntentRecord>();\n\tprivate readonly intentByPath = new Map<string, string>();\n\tprivate readonly contentReferences = new Map<string, ContentReferenceRecord>();\n\n\tconstructor(options: FileMutationIntentControllerOptions = {}) {\n\t\tthis.operations = options.operations ?? localFileMutationIntentOperations;\n\t\tthis.intentLimit = positiveBound(options.intentLimit, DEFAULT_INTENT_LIMIT, \"Intent limit\");\n\t\tthis.contentReferenceLimit = positiveBound(\n\t\t\toptions.contentReferenceLimit,\n\t\t\tDEFAULT_CONTENT_REFERENCE_LIMIT,\n\t\t\t\"Content reference limit\",\n\t\t);\n\t\tthis.intentTtlMs = positiveBound(options.intentTtlMs, DEFAULT_INTENT_TTL_MS, \"Intent TTL\");\n\t\tthis.contentReferenceTtlMs = positiveBound(\n\t\t\toptions.contentReferenceTtlMs,\n\t\t\tDEFAULT_CONTENT_REFERENCE_TTL_MS,\n\t\t\t\"Content reference TTL\",\n\t\t);\n\t\tthis.now = options.now ?? Date.now;\n\t}\n\n\tasync prepare(\n\t\tkind: FileMutationKind,\n\t\tpath: string,\n\t\tsignal?: AbortSignal,\n\t\tdisplayPath: string = path,\n\t): Promise<PreparedFileMutation> {\n\t\tthis.pruneExpired();\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tconst absolutePath = resolve(path);\n\t\tlet identity: FilePathIdentity | undefined;\n\t\tif (kind === \"write\") {\n\t\t\tconst existing = await this.operations.inspect(absolutePath, false);\n\t\t\tif (existing) throw new Error(`Write collision: ${displayPath} already exists.`);\n\t\t\tawait this.assertCreatableParent(absolutePath);\n\t\t} else {\n\t\t\tconst existing = await this.operations.inspect(absolutePath, true);\n\t\t\tif (!existing) throw new Error(`Could not edit file: ${displayPath}. Error code: ENOENT.`);\n\t\t\tif (existing.kind !== \"file\")\n\t\t\t\tthrow new Error(`Could not edit file: ${displayPath}. Target is not a regular file.`);\n\t\t\ttry {\n\t\t\t\tawait this.operations.access(absolutePath, constants.R_OK | constants.W_OK);\n\t\t\t} catch (error) {\n\t\t\t\tconst code = errorCode(error);\n\t\t\t\tthrow new Error(`Could not edit file: ${displayPath}. ${code ? `Error code: ${code}` : String(error)}.`);\n\t\t\t}\n\t\t\tidentity = existing.identity;\n\t\t}\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\n\t\tconst key = `${kind}\\0${normalizedPath(absolutePath)}`;\n\t\tconst priorIntentId = this.intentByPath.get(key);\n\t\tif (priorIntentId) this.deleteIntent(priorIntentId);\n\t\twhile (this.intents.size >= this.intentLimit) {\n\t\t\tconst oldestIntentId = this.intents.keys().next().value;\n\t\t\tif (typeof oldestIntentId !== \"string\") break;\n\t\t\tthis.deleteIntent(oldestIntentId);\n\t\t}\n\t\tconst intentId = randomUUID();\n\t\tconst record: MutationIntentRecord = {\n\t\t\tintentId,\n\t\t\tkind,\n\t\t\tpath: absolutePath,\n\t\t\tdisplayPath,\n\t\t\t...(identity ? { identity } : {}),\n\t\t\texpiresAt: this.now() + this.intentTtlMs,\n\t\t};\n\t\tthis.intents.set(intentId, record);\n\t\tthis.intentByPath.set(key, intentId);\n\t\treturn { intentId, kind, path: absolutePath, displayPath };\n\t}\n\n\tconsume(intentId: string, kind: FileMutationKind, path: string): FileMutationLease {\n\t\tthis.pruneExpired();\n\t\tconst record = this.intents.get(intentId);\n\t\tif (!record) throw new Error(\"File mutation intent is invalid, expired, or belongs to another session.\");\n\t\tthis.deleteIntent(intentId);\n\t\tif (record.kind !== kind) throw new Error(`File mutation intent is for ${record.kind}, not ${kind}.`);\n\t\tif (normalizedPath(record.path) !== normalizedPath(path)) {\n\t\t\tthrow new Error(\"File mutation intent path does not match the commit path.\");\n\t\t}\n\t\treturn {\n\t\t\tintentId: record.intentId,\n\t\t\tkind: record.kind,\n\t\t\tpath: record.path,\n\t\t\tdisplayPath: record.displayPath,\n\t\t\t...(record.identity ? { identity: record.identity } : {}),\n\t\t};\n\t}\n\n\tasync assertCurrent(lease: FileMutationLease, signal?: AbortSignal): Promise<void> {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tif (lease.kind === \"write\") {\n\t\t\tif (await this.operations.inspect(lease.path, false)) {\n\t\t\t\tthrow new Error(`Write collision: ${lease.displayPath} already exists; the prepared intent is stale.`);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\n\t\tconst current = await this.operations.inspect(lease.path, true);\n\t\tif (!current || current.kind !== \"file\") {\n\t\t\tthrow new Error(`Could not edit file: ${lease.displayPath}. The prepared target no longer exists.`);\n\t\t}\n\t\tif (!identitiesMatch(lease.identity, current.identity)) {\n\t\t\tthrow new Error(\n\t\t\t\t`Could not edit file: ${lease.displayPath}. The file changed after preparation; prepare again.`,\n\t\t\t);\n\t\t}\n\t\ttry {\n\t\t\tawait this.operations.access(lease.path, constants.R_OK | constants.W_OK);\n\t\t} catch (error) {\n\t\t\tconst code = errorCode(error);\n\t\t\tthrow new Error(`Could not edit file: ${lease.displayPath}. ${code ? `Error code: ${code}` : String(error)}.`);\n\t\t}\n\t}\n\n\trememberContent(sourcePath: string, content: string): FileContentReference {\n\t\tconst digest = createHash(\"sha256\").update(content, \"utf8\").digest(\"hex\");\n\t\treturn this.rememberContentDigest(sourcePath, digest, Buffer.byteLength(content, \"utf8\"));\n\t}\n\n\tasync copyReferencedContent(\n\t\tcontentRef: string,\n\t\ttargetPath: string,\n\t\tsignal?: AbortSignal,\n\t): Promise<FileContentReference> {\n\t\tthis.pruneExpired();\n\t\tconst record = this.contentReferences.get(contentRef);\n\t\tif (!record) throw new Error(\"File content reference is invalid, expired, or belongs to another session.\");\n\t\tthis.touchContentReference(record);\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tlet sourceDigest: string;\n\t\ttry {\n\t\t\tsourceDigest = await this.operations.hashFile(record.sourcePath, signal);\n\t\t} catch (error) {\n\t\t\tthrow new Error(`File content reference source is unavailable: ${String(error)}.`);\n\t\t}\n\t\tif (sourceDigest !== record.digest) {\n\t\t\tthis.contentReferences.delete(contentRef);\n\t\t\tthrow new Error(\"File content reference source changed after it was cached.\");\n\t\t}\n\n\t\tawait this.operations.copyFileExclusive(record.sourcePath, targetPath);\n\t\tlet targetDigest: string;\n\t\ttry {\n\t\t\ttargetDigest = await this.operations.hashFile(targetPath, signal);\n\t\t} catch (error) {\n\t\t\tawait this.removeFailedCopy(targetPath, error);\n\t\t\tthrow error;\n\t\t}\n\t\tif (targetDigest !== record.digest) {\n\t\t\tawait this.removeFailedCopy(targetPath, new Error(\"copied bytes did not match the cached digest\"));\n\t\t\tthrow new Error(\"File content copy changed while it was being created; the target was removed.\");\n\t\t}\n\t\treturn this.rememberContentDigest(targetPath, record.digest, record.byteLength);\n\t}\n\n\tprivate async assertCreatableParent(path: string): Promise<void> {\n\t\tlet candidate = dirname(path);\n\t\twhile (true) {\n\t\t\tconst inspection = await this.operations.inspect(candidate, true);\n\t\t\tif (inspection) {\n\t\t\t\tif (inspection.kind !== \"directory\") {\n\t\t\t\t\tthrow new Error(`Write parent path is not a directory: ${candidate}.`);\n\t\t\t\t}\n\t\t\t\ttry {\n\t\t\t\t\tawait this.operations.access(candidate, constants.W_OK);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tconst code = errorCode(error);\n\t\t\t\t\tthrow new Error(`Write parent is not writable: ${candidate}${code ? ` (${code})` : \"\"}.`);\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst parent = dirname(candidate);\n\t\t\tif (parent === candidate) throw new Error(`No writable parent exists for ${path}.`);\n\t\t\tcandidate = parent;\n\t\t}\n\t}\n\n\tprivate rememberContentDigest(sourcePath: string, digest: string, byteLength: number): FileContentReference {\n\t\tthis.pruneExpired();\n\t\tfor (const existing of this.contentReferences.values()) {\n\t\t\tif (existing.sourcePath === sourcePath && existing.digest === digest) {\n\t\t\t\tthis.touchContentReference(existing);\n\t\t\t\treturn { contentRef: existing.contentRef, byteLength: existing.byteLength };\n\t\t\t}\n\t\t}\n\t\twhile (this.contentReferences.size >= this.contentReferenceLimit) {\n\t\t\tconst oldestContentRef = this.contentReferences.keys().next().value;\n\t\t\tif (typeof oldestContentRef !== \"string\") break;\n\t\t\tthis.contentReferences.delete(oldestContentRef);\n\t\t}\n\t\tconst contentRef = `${CONTENT_REFERENCE_PREFIX}${randomUUID()}`;\n\t\tthis.contentReferences.set(contentRef, {\n\t\t\tcontentRef,\n\t\t\tsourcePath,\n\t\t\tdigest,\n\t\t\tbyteLength,\n\t\t\texpiresAt: this.now() + this.contentReferenceTtlMs,\n\t\t});\n\t\treturn { contentRef, byteLength };\n\t}\n\n\tprivate touchContentReference(record: ContentReferenceRecord): void {\n\t\trecord.expiresAt = this.now() + this.contentReferenceTtlMs;\n\t\tthis.contentReferences.delete(record.contentRef);\n\t\tthis.contentReferences.set(record.contentRef, record);\n\t}\n\n\tprivate async removeFailedCopy(path: string, cause: unknown): Promise<void> {\n\t\ttry {\n\t\t\tawait this.operations.removeFile(path);\n\t\t} catch (cleanupError) {\n\t\t\tthrow new Error(\n\t\t\t\t`Copied content verification failed (${String(cause)}), and the newly created target could not be removed: ${String(cleanupError)}.`,\n\t\t\t);\n\t\t}\n\t}\n\n\tprivate deleteIntent(intentId: string): void {\n\t\tconst record = this.intents.get(intentId);\n\t\tif (!record) return;\n\t\tthis.intents.delete(intentId);\n\t\tconst key = `${record.kind}\\0${normalizedPath(record.path)}`;\n\t\tif (this.intentByPath.get(key) === intentId) this.intentByPath.delete(key);\n\t}\n\n\tprivate pruneExpired(): void {\n\t\tconst now = this.now();\n\t\tfor (const [intentId, record] of this.intents) {\n\t\t\tif (record.expiresAt <= now) this.deleteIntent(intentId);\n\t\t}\n\t\tfor (const [contentRef, record] of this.contentReferences) {\n\t\t\tif (record.expiresAt <= now) this.contentReferences.delete(contentRef);\n\t\t}\n\t}\n}\n"]}
|
|
@@ -5,6 +5,7 @@ import type { ArtifactStore } from "../context/context-artifacts.ts";
|
|
|
5
5
|
import { type BroadQueryTracker } from "../context/tool-output-packer.ts";
|
|
6
6
|
import type { ToolDefinition } from "../extensions/types.ts";
|
|
7
7
|
import { type FffSearchBackend } from "./fff-search-backend.ts";
|
|
8
|
+
import { type ManagedSearchToolOptions } from "./managed-search-tool.ts";
|
|
8
9
|
import type { SearchRouter } from "./search-router.ts";
|
|
9
10
|
declare const findSchema: Type.TObject<{
|
|
10
11
|
pattern: Type.TString;
|
|
@@ -34,7 +35,7 @@ export interface FindOperations {
|
|
|
34
35
|
limit: number;
|
|
35
36
|
}) => Promise<string[]> | string[];
|
|
36
37
|
}
|
|
37
|
-
export interface FindToolOptions {
|
|
38
|
+
export interface FindToolOptions extends ManagedSearchToolOptions {
|
|
38
39
|
/** Custom operations for find. Default: local filesystem plus routed FFF/fd search */
|
|
39
40
|
operations?: FindOperations;
|
|
40
41
|
/** FFF backend for resident indexed search. Set false to force fd fallback. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"find.d.ts","sourceRoot":"","sources":["../../../src/core/tools/find.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,EAAiC,KAAK,gBAAgB,EAAE,MA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E,eAAe,GAAG,SAAS,CAAC,OAAO,UAAU,CAAC,CAEnG"}
|
package/dist/core/tools/find.js
CHANGED
|
@@ -5,9 +5,9 @@ import { minimatch } from "minimatch";
|
|
|
5
5
|
import path from "path";
|
|
6
6
|
import { Type } from "typebox";
|
|
7
7
|
import { waitForChildProcessWithTermination } from "../../utils/child-process.js";
|
|
8
|
-
import { ensureTool } from "../../utils/tools-manager.js";
|
|
9
8
|
import { broadQueryInvalidationNote, formatArtifactNotice, normalizeBroadQueryKey, packToolOutput, } from "../context/tool-output-packer.js";
|
|
10
9
|
import { hasGitignoreInTree, resolveRoutedFffFinder, } from "./fff-search-backend.js";
|
|
10
|
+
import { resolveManagedSearchTool } from "./managed-search-tool.js";
|
|
11
11
|
import { pathExists, resolveToCwd } from "./path-utils.js";
|
|
12
12
|
import { formatCollapsibleToolResult, invalidArgText, renderTextComponent, shortenPath, str, toolTextResult, } from "./render-utils.js";
|
|
13
13
|
import { resolveSearchToolRuntime } from "./search-tool-runtime.js";
|
|
@@ -324,15 +324,11 @@ export function createFindToolDefinition(cwd, options) {
|
|
|
324
324
|
return;
|
|
325
325
|
}
|
|
326
326
|
// Default implementation uses fd.
|
|
327
|
-
const fdPath = await
|
|
327
|
+
const fdPath = await resolveManagedSearchTool("fd", options?.managedToolResolver);
|
|
328
328
|
if (signal?.aborted) {
|
|
329
329
|
settle(() => reject(new Error("Operation aborted")));
|
|
330
330
|
return;
|
|
331
331
|
}
|
|
332
|
-
if (!fdPath) {
|
|
333
|
-
settle(() => reject(new Error("fd is not available and could not be downloaded")));
|
|
334
|
-
return;
|
|
335
|
-
}
|
|
336
332
|
// Build fd arguments. --no-require-git makes fd apply hierarchical .gitignore
|
|
337
333
|
// semantics whether or not the search path is inside a git repository, without
|
|
338
334
|
// leaking sibling-directory rules the way --ignore-file (a global source) would.
|