@abelxiaoxing/cadence 2.0.36 → 2.0.37
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.de.md +2 -2
- package/README.es.md +2 -2
- package/README.fr.md +2 -2
- package/README.ja.md +2 -2
- package/README.ko.md +2 -2
- package/README.md +2 -2
- package/README.pt-BR.md +2 -2
- package/README.ru.md +2 -2
- package/README.zh-CN.md +2 -2
- package/THIRD_PARTY_NOTICES.md +9 -1
- package/docs/conversation-mode.md +3 -2
- package/docs/reuse-and-distribution.md +1 -1
- package/docs/service-management.md +35 -9
- package/package.json +13 -8
- package/src/agent-browser/labeled-rule.ts +74 -0
- package/src/agent-browser/session-navigator.ts +187 -0
- package/src/agent-browser/source.ts +170 -0
- package/src/agent-browser/transcript-content.ts +442 -0
- package/src/agent-browser/transcript-pane.ts +324 -0
- package/src/agent-panel-model.ts +4 -248
- package/src/agent-panel.ts +138 -432
- package/src/coordinator-tools.mjs +42 -14
- package/src/image-content.mjs +7 -28
- package/src/native-build.ts +1 -1
- package/src/research-scan.mjs +393 -0
- package/src/research-tools.mjs +406 -342
- package/src/sdk-worker.mjs +40 -55
- package/src/service/contracts/core.ts +0 -1
- package/src/service/contracts/workbench.ts +10 -16
- package/src/service-cli.mjs +328 -106
- package/src/service-host.mjs +545 -404
- package/src/worker-public.mjs +1 -2
- package/src/worker-toolkit.mjs +110 -0
- package/src/worker-workspace.d.mts +13 -0
- package/src/worker-workspace.mjs +48 -0
- package/web/dist/assets/cadence.css +1 -1
- package/web/dist/assets/cadence.js +17 -17
- package/web/dist/manifest.json +7 -7
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/** Serialized private child/owner bridge. Never a browser API or shell executor. */
|
|
2
2
|
import { Type } from "typebox";
|
|
3
3
|
|
|
4
4
|
const CHECK = Type.Object({
|
|
@@ -31,18 +31,22 @@ const PLAN = Type.Object({
|
|
|
31
31
|
Type.Literal("working-copy"),
|
|
32
32
|
]),
|
|
33
33
|
});
|
|
34
|
-
export function
|
|
34
|
+
export function createCoordinatorTools() {
|
|
35
35
|
if (!process.send) throw new Error("Coordinator bridge unavailable");
|
|
36
36
|
const pending = new Map();
|
|
37
37
|
let counter = 0;
|
|
38
38
|
let chain = Promise.resolve();
|
|
39
|
+
let disconnected = false;
|
|
40
|
+
let retiring = false;
|
|
41
|
+
let retirement;
|
|
39
42
|
const closed = () => {
|
|
43
|
+
disconnected = true;
|
|
40
44
|
for (const p of pending.values())
|
|
41
45
|
p.reject(new Error("Coordinator disconnected"));
|
|
42
46
|
pending.clear();
|
|
43
47
|
};
|
|
44
48
|
process.on("disconnect", closed);
|
|
45
|
-
|
|
49
|
+
const receive = (reply) => {
|
|
46
50
|
if (
|
|
47
51
|
!reply ||
|
|
48
52
|
typeof reply !== "object" ||
|
|
@@ -58,8 +62,9 @@ export function coordinatorTools() {
|
|
|
58
62
|
details: undefined,
|
|
59
63
|
});
|
|
60
64
|
else p.reject(new Error("Coordinator refused this request"));
|
|
61
|
-
}
|
|
62
|
-
|
|
65
|
+
};
|
|
66
|
+
process.on("message", receive);
|
|
67
|
+
const tools = [
|
|
63
68
|
{
|
|
64
69
|
name: "cadence_research",
|
|
65
70
|
description:
|
|
@@ -94,25 +99,48 @@ export function coordinatorTools() {
|
|
|
94
99
|
...tool,
|
|
95
100
|
label: tool.name,
|
|
96
101
|
execute: async (_callId, args, signal) => {
|
|
97
|
-
if (
|
|
98
|
-
|
|
99
|
-
const request = { id: counter, name: tool.name, args };
|
|
102
|
+
if (retiring) throw new Error("Coordinator closed");
|
|
103
|
+
if (signal?.aborted) throw new Error("Coordinator canceled");
|
|
104
|
+
const request = { id: ++counter, name: tool.name, args };
|
|
100
105
|
if (Buffer.byteLength(JSON.stringify(request)) > 65536)
|
|
101
106
|
throw new Error("Coordinator request exceeds bound");
|
|
102
107
|
const original = chain.then(
|
|
103
108
|
() =>
|
|
104
109
|
new Promise((resolve, reject) => {
|
|
110
|
+
if (signal?.aborted) throw new Error("Coordinator canceled");
|
|
111
|
+
if (disconnected) throw new Error("Coordinator disconnected");
|
|
112
|
+
if (retiring) throw new Error("Coordinator closed");
|
|
105
113
|
pending.set(request.id, { resolve, reject });
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
114
|
+
const unavailable = () => {
|
|
115
|
+
pending.delete(request.id);
|
|
116
|
+
reject(new Error("Coordinator request unavailable"));
|
|
117
|
+
};
|
|
118
|
+
try {
|
|
119
|
+
process.send(request, (error) => {
|
|
120
|
+
if (error) unavailable();
|
|
121
|
+
});
|
|
122
|
+
} catch {
|
|
123
|
+
// Synchronous transport failures own the same cleanup as the
|
|
124
|
+
// callback path; never retain pending entries or expose raw errors.
|
|
125
|
+
unavailable();
|
|
126
|
+
}
|
|
112
127
|
}),
|
|
113
128
|
);
|
|
114
129
|
chain = original.catch(() => {});
|
|
115
130
|
return original;
|
|
116
131
|
},
|
|
117
132
|
}));
|
|
133
|
+
return {
|
|
134
|
+
tools,
|
|
135
|
+
close() {
|
|
136
|
+
retiring = true;
|
|
137
|
+
// Already-sent calls still own their original reply/disconnect lifetime.
|
|
138
|
+
// Queued calls refuse before sending; listeners stay until that joins.
|
|
139
|
+
retirement ??= chain.finally(() => {
|
|
140
|
+
process.removeListener("disconnect", closed);
|
|
141
|
+
process.removeListener("message", receive);
|
|
142
|
+
});
|
|
143
|
+
return retirement;
|
|
144
|
+
},
|
|
145
|
+
};
|
|
118
146
|
}
|
package/src/image-content.mjs
CHANGED
|
@@ -1,17 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
1
|
+
/** Upload chunks and history parts fit existing control replies. There are no
|
|
2
|
+
* image-specific count, byte or dimension quotas; runtime/provider limits apply.
|
|
3
|
+
* Image bytes never enlarge the public worker request or become prompt text. */
|
|
4
4
|
export const IMAGE_LIMITS = Object.freeze({
|
|
5
|
-
count: IMAGE_COUNT,
|
|
6
|
-
bytes: 4 * 1024 * 1024,
|
|
7
|
-
totalBytes: 8 * 1024 * 1024,
|
|
8
5
|
chunkChars: 256 * 1024,
|
|
9
|
-
stagedBytes: 64 * 1024 * 1024,
|
|
10
|
-
stagedCount: 64,
|
|
11
6
|
idleMs: 10 * 60 * 1000,
|
|
12
|
-
pixels: 32 * 1024 * 1024,
|
|
13
|
-
dimension: 16384,
|
|
14
|
-
wireBytes: 12 * 1024 * 1024,
|
|
15
7
|
});
|
|
16
8
|
export const IMAGE_TYPES = Object.freeze([
|
|
17
9
|
"image/png",
|
|
@@ -19,17 +11,15 @@ export const IMAGE_TYPES = Object.freeze([
|
|
|
19
11
|
"image/webp",
|
|
20
12
|
]);
|
|
21
13
|
|
|
22
|
-
export function decodeImageBase64(data
|
|
14
|
+
export function decodeImageBase64(data) {
|
|
23
15
|
if (
|
|
24
16
|
typeof data !== "string" ||
|
|
25
17
|
!data.length ||
|
|
26
18
|
data.length % 4 ||
|
|
27
|
-
data.length > 4 * Math.ceil(maxBytes / 3) ||
|
|
28
19
|
!/^[A-Za-z0-9+/]*={0,2}$/.test(data)
|
|
29
20
|
)
|
|
30
21
|
throw new Error("Invalid image encoding");
|
|
31
22
|
const raw = atob(data);
|
|
32
|
-
if (raw.length > maxBytes) throw new Error("Image is too large");
|
|
33
23
|
const bytes = new Uint8Array(raw.length);
|
|
34
24
|
for (let i = 0; i < raw.length; i++) bytes[i] = raw.charCodeAt(i);
|
|
35
25
|
return bytes;
|
|
@@ -37,12 +27,8 @@ export function decodeImageBase64(data, maxBytes = IMAGE_LIMITS.bytes) {
|
|
|
37
27
|
|
|
38
28
|
/** Bounded header validation, not an image transcoder. No SVG/HTML or remote URLs. */
|
|
39
29
|
export function validateImageBytes(bytes, mimeType) {
|
|
40
|
-
if (
|
|
41
|
-
|
|
42
|
-
!bytes.length ||
|
|
43
|
-
bytes.length > IMAGE_LIMITS.bytes
|
|
44
|
-
)
|
|
45
|
-
throw new Error("Unsupported image or size");
|
|
30
|
+
if (!IMAGE_TYPES.includes(mimeType) || !bytes.length)
|
|
31
|
+
throw new Error("Unsupported or empty image");
|
|
46
32
|
const v = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
47
33
|
const ascii = (at, value) =>
|
|
48
34
|
[...value].every((c, i) => bytes[at + i] === c.charCodeAt(0));
|
|
@@ -108,13 +94,6 @@ export function validateImageBytes(bytes, mimeType) {
|
|
|
108
94
|
height = 1 + ((bits >>> 14) & 16383);
|
|
109
95
|
}
|
|
110
96
|
}
|
|
111
|
-
if (
|
|
112
|
-
!width ||
|
|
113
|
-
!height ||
|
|
114
|
-
width > IMAGE_LIMITS.dimension ||
|
|
115
|
-
height > IMAGE_LIMITS.dimension ||
|
|
116
|
-
width * height > IMAGE_LIMITS.pixels
|
|
117
|
-
)
|
|
118
|
-
throw new Error("Invalid image dimensions or header");
|
|
97
|
+
if (!width || !height) throw new Error("Invalid image dimensions or header");
|
|
119
98
|
return { width, height };
|
|
120
99
|
}
|
package/src/native-build.ts
CHANGED
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
/** One live filesystem observation. The suspended iterator owns real positions,
|
|
2
|
+
* not a page number or a replay of an earlier scan. No shell or spill files. */
|
|
3
|
+
import { constants } from "node:fs";
|
|
4
|
+
import { lstat, open, opendir, realpath } from "node:fs/promises";
|
|
5
|
+
import { basename, join, matchesGlob, relative, resolve, sep } from "node:path";
|
|
6
|
+
import { withinRoot } from "./worker-workspace.mjs";
|
|
7
|
+
|
|
8
|
+
export const RESEARCH_LIMITS = Object.freeze({
|
|
9
|
+
toolMs: 30_000,
|
|
10
|
+
entries: 2000,
|
|
11
|
+
depth: 8,
|
|
12
|
+
results: 100,
|
|
13
|
+
fileBytes: 1024 * 1024, // Per file per page, NOT a permanently invisible suffix.
|
|
14
|
+
scanBytes: 8 * 1024 * 1024,
|
|
15
|
+
outputBytes: 32 * 1024, // Complete JSON-encoded tool result, including metadata.
|
|
16
|
+
chunkBytes: 64 * 1024,
|
|
17
|
+
lineBytes: 8 * 1024,
|
|
18
|
+
scans: 4, // Includes opening, active, suspended and closing originals.
|
|
19
|
+
idleMs: 120_000,
|
|
20
|
+
files: 8,
|
|
21
|
+
ranges: 32,
|
|
22
|
+
patterns: 16,
|
|
23
|
+
});
|
|
24
|
+
export class SourceChanged extends Error {
|
|
25
|
+
constructor() {
|
|
26
|
+
super("Investigation source changed; start a fresh query explicitly.");
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const identity = (s) =>
|
|
30
|
+
`${s.dev}:${s.ino}:${s.mode}:${s.size}:${s.mtimeNs}:${s.ctimeNs}`;
|
|
31
|
+
const unavailable = (e) =>
|
|
32
|
+
["ENOENT", "ENOTDIR", "EACCES", "EPERM", "ELOOP"].includes(e?.code);
|
|
33
|
+
const counters = () => ({
|
|
34
|
+
readBytes: 0,
|
|
35
|
+
inspectedEntries: 0,
|
|
36
|
+
inspectedLines: 0,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
export class ResearchScan {
|
|
40
|
+
constructor(workspace, query) {
|
|
41
|
+
this.workspace = workspace;
|
|
42
|
+
this.query = query;
|
|
43
|
+
this.omissions = new Map();
|
|
44
|
+
this.total = counters();
|
|
45
|
+
this.page = counters();
|
|
46
|
+
this.fileBytes = new Map();
|
|
47
|
+
this.sources = new Set();
|
|
48
|
+
this.stopping = false;
|
|
49
|
+
this.iterator = this.records();
|
|
50
|
+
}
|
|
51
|
+
begin(signal) {
|
|
52
|
+
this.signal = signal;
|
|
53
|
+
this.page = counters();
|
|
54
|
+
this.fileBytes.clear();
|
|
55
|
+
}
|
|
56
|
+
check() {
|
|
57
|
+
if (this.stopping || this.signal?.aborted)
|
|
58
|
+
throw new Error("Investigation canceled");
|
|
59
|
+
}
|
|
60
|
+
count(key, n = 1) {
|
|
61
|
+
this.page[key] += n;
|
|
62
|
+
this.total[key] += n;
|
|
63
|
+
}
|
|
64
|
+
omit(reason) {
|
|
65
|
+
this.omissions.set(reason, (this.omissions.get(reason) ?? 0) + 1);
|
|
66
|
+
}
|
|
67
|
+
async source(path) {
|
|
68
|
+
this.check();
|
|
69
|
+
if ((await realpath(path)) !== path) throw new SourceChanged();
|
|
70
|
+
this.check();
|
|
71
|
+
const info = await lstat(path, { bigint: true });
|
|
72
|
+
this.check();
|
|
73
|
+
return { path, identity: identity(info), info };
|
|
74
|
+
}
|
|
75
|
+
async validateSource(source) {
|
|
76
|
+
let current;
|
|
77
|
+
try {
|
|
78
|
+
current = await this.source(source.path);
|
|
79
|
+
} catch (e) {
|
|
80
|
+
if (unavailable(e)) throw new SourceChanged();
|
|
81
|
+
throw e;
|
|
82
|
+
}
|
|
83
|
+
if (current.identity !== source.identity) throw new SourceChanged();
|
|
84
|
+
if (
|
|
85
|
+
source.file &&
|
|
86
|
+
identity(await source.file.stat({ bigint: true })) !== source.identity
|
|
87
|
+
)
|
|
88
|
+
throw new SourceChanged();
|
|
89
|
+
this.check();
|
|
90
|
+
}
|
|
91
|
+
async validate() {
|
|
92
|
+
for (const source of this.sources) await this.validateSource(source);
|
|
93
|
+
}
|
|
94
|
+
async closeHandle(handle) {
|
|
95
|
+
try {
|
|
96
|
+
await handle.close();
|
|
97
|
+
} catch {
|
|
98
|
+
this.closeFailed = true;
|
|
99
|
+
// Never classify a failed close as an ordinary unreadable-file omission.
|
|
100
|
+
throw new Error("Investigation handle closure failed");
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
async target(path) {
|
|
104
|
+
this.check();
|
|
105
|
+
const selected = await realpath(resolve(this.roots[0], path ?? "."));
|
|
106
|
+
this.check();
|
|
107
|
+
const root = this.roots.findIndex((p) => withinRoot(p, selected));
|
|
108
|
+
if (root < 0)
|
|
109
|
+
throw new Error(
|
|
110
|
+
"Path outside investigation roots; select the intended root explicitly.",
|
|
111
|
+
);
|
|
112
|
+
return { path: selected, root };
|
|
113
|
+
}
|
|
114
|
+
location(target) {
|
|
115
|
+
return {
|
|
116
|
+
root: target.root,
|
|
117
|
+
path:
|
|
118
|
+
relative(this.roots[target.root], target.path).split(sep).join("/") ||
|
|
119
|
+
basename(target.path),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
async *records() {
|
|
123
|
+
this.roots = [];
|
|
124
|
+
// No parallel root resolution: an invalid root never abandons sibling I/O.
|
|
125
|
+
for (const root of this.workspace.roots) {
|
|
126
|
+
this.check();
|
|
127
|
+
const path = await realpath(root);
|
|
128
|
+
const source = await this.source(path);
|
|
129
|
+
this.roots.push(path);
|
|
130
|
+
this.sources.add(source);
|
|
131
|
+
}
|
|
132
|
+
if (this.query.kind === "read") {
|
|
133
|
+
const files = new Map();
|
|
134
|
+
for (const request of this.query.files) {
|
|
135
|
+
const target = await this.target(request.path);
|
|
136
|
+
const existing = files.get(target.path);
|
|
137
|
+
if (existing) existing.ranges.push(...request.ranges);
|
|
138
|
+
else files.set(target.path, { ...target, ranges: [...request.ranges] });
|
|
139
|
+
}
|
|
140
|
+
for (const target of files.values()) {
|
|
141
|
+
target.ranges.sort((a, b) => a.start - b.start);
|
|
142
|
+
const ranges = [];
|
|
143
|
+
for (const range of target.ranges) {
|
|
144
|
+
const last = ranges.at(-1);
|
|
145
|
+
if (last && range.start <= last.end + 1)
|
|
146
|
+
last.end = Math.max(last.end, range.end);
|
|
147
|
+
else ranges.push({ ...range });
|
|
148
|
+
}
|
|
149
|
+
yield* this.lines(target, ranges);
|
|
150
|
+
}
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const target = await this.target(this.query.path);
|
|
154
|
+
const source = await this.source(target.path);
|
|
155
|
+
if (this.query.kind === "ls" && !source.info.isDirectory())
|
|
156
|
+
throw new Error("Select a directory");
|
|
157
|
+
if (!source.info.isDirectory() && !source.info.isFile()) {
|
|
158
|
+
this.omit("special file omitted");
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const files = source.info.isDirectory()
|
|
162
|
+
? this.walk(target, this.query.depth)
|
|
163
|
+
: (async function* () {
|
|
164
|
+
yield target;
|
|
165
|
+
})();
|
|
166
|
+
for await (const file of files) {
|
|
167
|
+
if (file === null || file.pause) {
|
|
168
|
+
yield file;
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
if (this.query.kind === "ls") {
|
|
172
|
+
yield { item: { ...this.location(file), kind: file.kind } };
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
const glob = this.query.glob;
|
|
176
|
+
const name =
|
|
177
|
+
relative(target.path, file.path).split(sep).join("/") ||
|
|
178
|
+
basename(file.path);
|
|
179
|
+
if (
|
|
180
|
+
glob &&
|
|
181
|
+
!matchesGlob(glob.includes("/") ? name : basename(file.path), glob)
|
|
182
|
+
) {
|
|
183
|
+
yield null;
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
if (this.query.kind === "find")
|
|
187
|
+
yield { item: { ...this.location(file), kind: "file" } };
|
|
188
|
+
else {
|
|
189
|
+
try {
|
|
190
|
+
yield* this.lines(file);
|
|
191
|
+
} catch (e) {
|
|
192
|
+
if (!unavailable(e)) throw e;
|
|
193
|
+
this.omit("unreadable file omitted");
|
|
194
|
+
yield null;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
async *walk(target, remaining) {
|
|
200
|
+
const source = await this.source(target.path);
|
|
201
|
+
this.check();
|
|
202
|
+
const dir = await opendir(target.path);
|
|
203
|
+
this.sources.add(source);
|
|
204
|
+
try {
|
|
205
|
+
await this.validateSource(source);
|
|
206
|
+
for (;;) {
|
|
207
|
+
this.check();
|
|
208
|
+
if (this.page.inspectedEntries >= RESEARCH_LIMITS.entries) {
|
|
209
|
+
yield { pause: "entry quantum" };
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
const entry = await dir.read();
|
|
213
|
+
this.check();
|
|
214
|
+
if (!entry) return;
|
|
215
|
+
this.count("inspectedEntries");
|
|
216
|
+
const file = { path: join(target.path, entry.name), root: target.root };
|
|
217
|
+
if (this.query.kind === "ls") {
|
|
218
|
+
yield {
|
|
219
|
+
...file,
|
|
220
|
+
kind: entry.isSymbolicLink()
|
|
221
|
+
? "symlink"
|
|
222
|
+
: entry.isDirectory()
|
|
223
|
+
? "directory"
|
|
224
|
+
: entry.isFile()
|
|
225
|
+
? "file"
|
|
226
|
+
: "special",
|
|
227
|
+
};
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
if (entry.isSymbolicLink()) this.omit("symlink omitted");
|
|
231
|
+
else if (entry.isDirectory()) {
|
|
232
|
+
if ([".git", "node_modules"].includes(entry.name))
|
|
233
|
+
this.omit(".git/node_modules omitted");
|
|
234
|
+
else if (!remaining) this.omit("depth limit");
|
|
235
|
+
else {
|
|
236
|
+
// Yield before descent too, so unmatched directories consume a quantum.
|
|
237
|
+
yield null;
|
|
238
|
+
try {
|
|
239
|
+
yield* this.walk(file, remaining - 1);
|
|
240
|
+
} catch (e) {
|
|
241
|
+
if (!unavailable(e)) throw e;
|
|
242
|
+
this.omit("unreadable directory omitted");
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
} else if (entry.isFile()) {
|
|
246
|
+
yield file;
|
|
247
|
+
continue;
|
|
248
|
+
} else this.omit("special file omitted");
|
|
249
|
+
yield null;
|
|
250
|
+
}
|
|
251
|
+
} finally {
|
|
252
|
+
try {
|
|
253
|
+
if (!this.stopping && !this.signal?.aborted)
|
|
254
|
+
await this.validateSource(source);
|
|
255
|
+
} finally {
|
|
256
|
+
await this.closeHandle(dir);
|
|
257
|
+
this.sources.delete(source);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
async *lines(target, ranges) {
|
|
262
|
+
const source = await this.source(target.path);
|
|
263
|
+
if (!source.info.isFile())
|
|
264
|
+
throw new Error("Only regular text files can be read");
|
|
265
|
+
this.check();
|
|
266
|
+
const file = await open(
|
|
267
|
+
target.path,
|
|
268
|
+
constants.O_RDONLY | constants.O_NONBLOCK | (constants.O_NOFOLLOW ?? 0),
|
|
269
|
+
);
|
|
270
|
+
source.file = file;
|
|
271
|
+
this.sources.add(source);
|
|
272
|
+
try {
|
|
273
|
+
this.check();
|
|
274
|
+
if (!(await file.stat()).isFile())
|
|
275
|
+
throw new Error("Only regular text files can be read");
|
|
276
|
+
await this.validateSource(source);
|
|
277
|
+
const buffer = Buffer.alloc(RESEARCH_LIMITS.chunkBytes);
|
|
278
|
+
const line = Buffer.alloc(RESEARCH_LIMITS.lineBytes);
|
|
279
|
+
// decode() starts a fresh stream for each line. Preserve U+FEFF rather
|
|
280
|
+
// than treating every line's leading bytes as a new file BOM.
|
|
281
|
+
const decoder = new TextDecoder("utf-8", {
|
|
282
|
+
fatal: true,
|
|
283
|
+
ignoreBOM: true,
|
|
284
|
+
});
|
|
285
|
+
let position = 0,
|
|
286
|
+
used = 0,
|
|
287
|
+
number = 1,
|
|
288
|
+
long = false,
|
|
289
|
+
rangeIndex = 0;
|
|
290
|
+
const emit = () => {
|
|
291
|
+
this.count("inspectedLines");
|
|
292
|
+
while (
|
|
293
|
+
ranges &&
|
|
294
|
+
rangeIndex < ranges.length &&
|
|
295
|
+
number > ranges[rangeIndex].end
|
|
296
|
+
)
|
|
297
|
+
rangeIndex++;
|
|
298
|
+
const wanted =
|
|
299
|
+
!ranges ||
|
|
300
|
+
(rangeIndex < ranges.length && number >= ranges[rangeIndex].start);
|
|
301
|
+
if (!wanted) return null;
|
|
302
|
+
if (long) {
|
|
303
|
+
this.omit("long line omitted");
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
306
|
+
let text;
|
|
307
|
+
try {
|
|
308
|
+
text = decoder.decode(
|
|
309
|
+
line.subarray(0, used && line[used - 1] === 13 ? used - 1 : used),
|
|
310
|
+
);
|
|
311
|
+
} catch {
|
|
312
|
+
this.omit("non-UTF-8 line omitted");
|
|
313
|
+
return null;
|
|
314
|
+
}
|
|
315
|
+
const item = { ...this.location(target), line: number, text };
|
|
316
|
+
if (!ranges) {
|
|
317
|
+
const haystack = this.query.ignoreCase ? text.toLowerCase() : text;
|
|
318
|
+
const matched = this.query.patterns.flatMap((pattern, index) =>
|
|
319
|
+
haystack.includes(pattern) ? [index] : [],
|
|
320
|
+
);
|
|
321
|
+
if (!matched.length) return null;
|
|
322
|
+
item.matchedPatterns = matched;
|
|
323
|
+
}
|
|
324
|
+
return { item };
|
|
325
|
+
};
|
|
326
|
+
for (;;) {
|
|
327
|
+
this.check();
|
|
328
|
+
const allowance = Math.min(
|
|
329
|
+
RESEARCH_LIMITS.chunkBytes,
|
|
330
|
+
RESEARCH_LIMITS.fileBytes - (this.fileBytes.get(target.path) ?? 0),
|
|
331
|
+
RESEARCH_LIMITS.scanBytes - this.page.readBytes,
|
|
332
|
+
);
|
|
333
|
+
if (!allowance) {
|
|
334
|
+
yield { pause: "byte quantum" };
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
const { bytesRead } = await file.read(buffer, 0, allowance, position);
|
|
338
|
+
this.check();
|
|
339
|
+
this.count("readBytes", bytesRead);
|
|
340
|
+
this.fileBytes.set(
|
|
341
|
+
target.path,
|
|
342
|
+
(this.fileBytes.get(target.path) ?? 0) + bytesRead,
|
|
343
|
+
);
|
|
344
|
+
position += bytesRead;
|
|
345
|
+
if (!bytesRead) {
|
|
346
|
+
if (used || long) yield emit();
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
if (buffer.subarray(0, bytesRead).includes(0)) {
|
|
350
|
+
this.omit("binary file omitted");
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
for (let at = 0; at < bytesRead; ) {
|
|
354
|
+
this.check();
|
|
355
|
+
const newline = buffer.indexOf(10, at);
|
|
356
|
+
const end = newline < 0 || newline >= bytesRead ? bytesRead : newline;
|
|
357
|
+
if (!long) {
|
|
358
|
+
if (used + end - at > line.length) long = true;
|
|
359
|
+
else {
|
|
360
|
+
buffer.copy(line, used, at, end);
|
|
361
|
+
used += end - at;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
at = end;
|
|
365
|
+
if (end < bytesRead) {
|
|
366
|
+
yield emit();
|
|
367
|
+
if (ranges && number >= ranges.at(-1).end) return;
|
|
368
|
+
number++;
|
|
369
|
+
used = 0;
|
|
370
|
+
long = false;
|
|
371
|
+
at++;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
// Even a single huge line makes bounded, observable byte progress.
|
|
375
|
+
yield null;
|
|
376
|
+
}
|
|
377
|
+
} finally {
|
|
378
|
+
try {
|
|
379
|
+
if (!this.stopping && !this.signal?.aborted)
|
|
380
|
+
await this.validateSource(source);
|
|
381
|
+
} finally {
|
|
382
|
+
await this.closeHandle(file);
|
|
383
|
+
this.sources.delete(source);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
async close() {
|
|
388
|
+
this.stopping = true;
|
|
389
|
+
await this.iterator.return();
|
|
390
|
+
if (this.closeFailed)
|
|
391
|
+
throw new Error("Investigation handle closure failed");
|
|
392
|
+
}
|
|
393
|
+
}
|