@eyeauras/insight-cli 0.6.6 → 0.8.0
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/.cli-package-build.json +1 -1
- package/SKILL.md +1 -1
- package/dist/src/activity-audit.d.ts +130 -0
- package/dist/src/activity-audit.js +320 -0
- package/dist/src/analysis-storage.d.ts +5 -1
- package/dist/src/analysis-storage.js +9 -2
- package/dist/src/audit-commands.d.ts +10 -0
- package/dist/src/audit-commands.js +85 -0
- package/dist/src/audit-retention.d.ts +44 -0
- package/dist/src/audit-retention.js +167 -0
- package/dist/src/cli.js +28 -6
- package/dist/src/context-diagnostics.d.ts +1 -1
- package/dist/src/context-diagnostics.js +10 -2
- package/dist/src/contexts.d.ts +118 -10
- package/dist/src/contexts.js +851 -199
- package/dist/src/debugger.js +2 -1
- package/dist/src/diagnostic-error.d.ts +12 -0
- package/dist/src/diagnostic-error.js +82 -0
- package/dist/src/inline-script.js +3 -4
- package/dist/src/internal-timing.d.ts +8 -2
- package/dist/src/internal-timing.js +50 -7
- package/dist/src/lifecycle.d.ts +12 -1
- package/dist/src/lifecycle.js +34 -6
- package/dist/src/native-inputs.d.ts +6 -0
- package/dist/src/native-inputs.js +30 -6
- package/dist/src/native-queries.js +1 -6
- package/dist/src/native-service.d.ts +40 -0
- package/dist/src/native-service.js +406 -0
- package/dist/src/research.d.ts +30 -2
- package/dist/src/research.js +106 -27
- package/dist/src/server.d.ts +38 -1
- package/dist/src/server.js +160 -12
- package/dist/src/storage-gc.js +374 -341
- package/dist/src/sync-policy.js +2 -1
- package/dist/src/timed-backend.js +8 -0
- package/dist/src/timing-report.d.ts +32 -0
- package/dist/src/timing-report.js +74 -0
- package/dist/src/tool-runtime/identity.js +86 -1
- package/dist/src/tool-runtime/supervisor.js +6 -5
- package/dist/src/transport.d.ts +35 -0
- package/dist/src/transport.js +95 -16
- package/dist/src/unresolved-guidance.d.ts +46 -0
- package/dist/src/unresolved-guidance.js +55 -0
- package/dist/src/windbg.d.ts +1 -1
- package/dist/src/worker-lease.d.ts +17 -0
- package/dist/src/worker-lease.js +50 -0
- package/dist/src/workspace.d.ts +5 -9
- package/dist/src/workspace.js +34 -41
- package/package.json +1 -1
- package/references/native/InsightHeadlessMain.java +9 -2
- package/references/native/InsightNativeCheckout.java +113 -43
- package/references/native/InsightNativeFingerprint.java +28 -1
- package/references/native/InsightNativeRead.java +32 -9
- package/references/native/InsightNativeService.java +43 -0
- package/references/native/InsightNativeView.java +17 -6
- package/references/native/InsightOwnedScripts.java +170 -0
- package/references/native/InsightWorkerLease.java +132 -0
- package/references/recovery.md +68 -3
- package/references/research.md +71 -6
- package/references/setup.md +5 -1
- package/skill-bundle/insight-artifacts/SKILL.md +1 -1
- package/skill-bundle/insight-artifacts/references/recovery.md +68 -3
- package/skill-bundle/insight-binary/SKILL.md +1 -1
- package/skill-bundle/insight-capture/SKILL.md +1 -1
- package/skill-bundle/insight-cli/SKILL.md +1 -1
- package/skill-bundle/insight-cli/references/recovery.md +68 -3
- package/skill-bundle/insight-cli/references/setup.md +5 -1
- package/skill-bundle/insight-die/SKILL.md +1 -1
- package/skill-bundle/insight-dnspy/SKILL.md +1 -1
- package/skill-bundle/insight-dotnet-dump/SKILL.md +1 -1
- package/skill-bundle/insight-dotnet-stack/SKILL.md +1 -1
- package/skill-bundle/insight-dotnet-trace/SKILL.md +1 -1
- package/skill-bundle/insight-frida/SKILL.md +1 -1
- package/skill-bundle/insight-ghidra/SKILL.md +1 -1
- package/skill-bundle/insight-ghidra/references/recovery.md +68 -3
- package/skill-bundle/insight-ghidra/references/research.md +71 -6
- package/skill-bundle/insight-ghidra/references/setup.md +5 -1
- package/skill-bundle/insight-ilspy/SKILL.md +1 -1
- package/skill-bundle/insight-interop/SKILL.md +1 -1
- package/skill-bundle/insight-lldb/SKILL.md +1 -1
- package/skill-bundle/insight-llvm/SKILL.md +1 -1
- package/skill-bundle/insight-managed-heap/SKILL.md +1 -1
- package/skill-bundle/insight-managed-metadata/SKILL.md +1 -1
- package/skill-bundle/insight-mitmproxy/SKILL.md +1 -1
- package/skill-bundle/insight-overview/SKILL.md +1 -1
- package/skill-bundle/insight-procmon/SKILL.md +1 -1
- package/skill-bundle/insight-re/SKILL.md +7 -1
- package/skill-bundle/insight-re/references/recovery.md +68 -3
- package/skill-bundle/insight-sevenzip/SKILL.md +1 -1
- package/skill-bundle/insight-strace/SKILL.md +1 -1
- package/skill-bundle/insight-tshark/SKILL.md +1 -1
- package/skill-bundle/insight-windbg/SKILL.md +1 -1
- package/skill-bundle/manifest.json +1 -1
- package/skills/insight-artifacts/SKILL.md +1 -1
- package/skills/insight-artifacts/references/recovery.md +68 -3
- package/skills/insight-binary/SKILL.md +1 -1
- package/skills/insight-capture/SKILL.md +1 -1
- package/skills/insight-die/SKILL.md +1 -1
- package/skills/insight-dnspy/SKILL.md +1 -1
- package/skills/insight-dotnet-dump/SKILL.md +1 -1
- package/skills/insight-dotnet-stack/SKILL.md +1 -1
- package/skills/insight-dotnet-trace/SKILL.md +1 -1
- package/skills/insight-frida/SKILL.md +1 -1
- package/skills/insight-ghidra/SKILL.md +1 -1
- package/skills/insight-ghidra/references/recovery.md +68 -3
- package/skills/insight-ghidra/references/research.md +71 -6
- package/skills/insight-ghidra/references/setup.md +5 -1
- package/skills/insight-ilspy/SKILL.md +1 -1
- package/skills/insight-interop/SKILL.md +1 -1
- package/skills/insight-lldb/SKILL.md +1 -1
- package/skills/insight-llvm/SKILL.md +1 -1
- package/skills/insight-managed-heap/SKILL.md +1 -1
- package/skills/insight-managed-metadata/SKILL.md +1 -1
- package/skills/insight-mitmproxy/SKILL.md +1 -1
- package/skills/insight-overview/SKILL.md +1 -1
- package/skills/insight-procmon/SKILL.md +1 -1
- package/skills/insight-re/SKILL.md +7 -1
- package/skills/insight-re/references/recovery.md +68 -3
- package/skills/insight-sevenzip/SKILL.md +1 -1
- package/skills/insight-strace/SKILL.md +1 -1
- package/skills/insight-tshark/SKILL.md +1 -1
- package/skills/insight-windbg/SKILL.md +1 -1
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
import { mkdir, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { randomUUID } from 'node:crypto';
|
|
4
|
+
import { CliError } from '@eyeauras/cli-factory';
|
|
5
|
+
import { launchProcess } from './tool-runtime/supervisor.js';
|
|
6
|
+
import { withToolUse } from './tool-runtime/store.js';
|
|
7
|
+
import { timed } from './internal-timing.js';
|
|
8
|
+
import { parseDomainJson } from './json.js';
|
|
9
|
+
const maximumPerKey = 2, lineLimit = 1024 * 1024, stderrLimit = 65536, failureLimit = 3, disabledMs = 300000;
|
|
10
|
+
/** A request that was never written to a service; it may run elsewhere without any outcome ambiguity. */
|
|
11
|
+
class NotSent extends Error {
|
|
12
|
+
}
|
|
13
|
+
/** A start that failed; its process (possibly with an unproved exit) stays tracked until its exit is proved. */
|
|
14
|
+
class StartFailed extends Error {
|
|
15
|
+
service;
|
|
16
|
+
}
|
|
17
|
+
const failure = (message, code, result) => new CliError(message, { code, ...(result ? { result } : {}) });
|
|
18
|
+
/**
|
|
19
|
+
* One long-lived trusted native service JVM (InsightNativeService.java). Requests name an owned request file
|
|
20
|
+
* and are answered in order. A dispatched request that times out, is cancelled, breaks the protocol or loses
|
|
21
|
+
* its service terminates the whole service first (with escalation, like a one-shot helper); only then is it
|
|
22
|
+
* reported, and an unproved termination is reported as such. A request still queued is never sent elsewhere
|
|
23
|
+
* silently: it is reported as not sent, which callers may retry on another service or a one-shot helper.
|
|
24
|
+
*/
|
|
25
|
+
class Service {
|
|
26
|
+
role;
|
|
27
|
+
key;
|
|
28
|
+
child;
|
|
29
|
+
launch;
|
|
30
|
+
secrets;
|
|
31
|
+
busy = 0;
|
|
32
|
+
dead = false;
|
|
33
|
+
draining = false;
|
|
34
|
+
buffer = '';
|
|
35
|
+
stderr = [];
|
|
36
|
+
stderrTotal = 0;
|
|
37
|
+
stderrStart = 0;
|
|
38
|
+
inflight;
|
|
39
|
+
queue = Promise.resolve();
|
|
40
|
+
terminating;
|
|
41
|
+
constructor(role, key, child, launch, secrets) {
|
|
42
|
+
this.role = role;
|
|
43
|
+
this.key = key;
|
|
44
|
+
this.child = child;
|
|
45
|
+
this.launch = launch;
|
|
46
|
+
this.secrets = secrets;
|
|
47
|
+
this.exited = new Promise(resolve => child.once('exit', () => resolve()));
|
|
48
|
+
this.drained = new Promise(resolve => child.once('close', () => resolve()));
|
|
49
|
+
}
|
|
50
|
+
static async start(role, key, launch, readyMs) {
|
|
51
|
+
await mkdir(launch.root, { recursive: true, mode: 0o700 });
|
|
52
|
+
const child = await launchProcess(launch.directory, { tool: 'ghidra', role: 'native-' + role, installation: launch.installation }, launch.executable, launch.arguments, { cwd: launch.root, env: launch.environment, windowsHide: true, stdio: ['pipe', 'pipe', 'pipe'] });
|
|
53
|
+
const secrets = Object.entries(launch.environment).filter(([name, value]) => /TOKEN|PASSWORD|SECRET|KEY/i.test(name) && value && value.length >= 4).map(([, value]) => value);
|
|
54
|
+
const service = new Service(role, key, child, launch, secrets);
|
|
55
|
+
try {
|
|
56
|
+
await service.ready(readyMs);
|
|
57
|
+
return service;
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
await service.terminate();
|
|
61
|
+
throw Object.assign(new StartFailed(error instanceof Error ? error.message : 'Native service start failed.'), { service, cause: error });
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
redact(text) { for (const secret of this.secrets)
|
|
65
|
+
text = text.split(secret).join('[redacted]'); return text; }
|
|
66
|
+
ready(readyMs) {
|
|
67
|
+
const child = this.child;
|
|
68
|
+
return new Promise((resolve, reject) => {
|
|
69
|
+
let started = false;
|
|
70
|
+
const timer = setTimeout(() => reject(failure('Native service did not become ready.', 'tool.timeout')), readyMs);
|
|
71
|
+
timer.unref();
|
|
72
|
+
child.stderr.on('data', (chunk) => {
|
|
73
|
+
this.stderr.push(chunk);
|
|
74
|
+
this.stderrTotal += chunk.length;
|
|
75
|
+
while (this.stderr.length > 1 && this.stderrTotal - this.stderrStart - this.stderr[0].length > stderrLimit) {
|
|
76
|
+
this.stderrStart += this.stderr.shift().length;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
child.stdout.setEncoding('utf8');
|
|
80
|
+
child.stdout.on('data', (chunk) => {
|
|
81
|
+
this.buffer += chunk;
|
|
82
|
+
let index;
|
|
83
|
+
while ((index = this.buffer.indexOf('\n')) >= 0) {
|
|
84
|
+
const line = this.buffer.slice(0, index).trim();
|
|
85
|
+
this.buffer = this.buffer.slice(index + 1);
|
|
86
|
+
if (!line)
|
|
87
|
+
continue;
|
|
88
|
+
if (line.length > lineLimit) {
|
|
89
|
+
void this.abort('tool.output.limit');
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
// Same number handling as one-shot helper output: unsafe integers stay exact strings.
|
|
93
|
+
let message;
|
|
94
|
+
try {
|
|
95
|
+
message = parseDomainJson(line);
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
void this.abort('coordinator.malformed');
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (!started) {
|
|
102
|
+
if (message.state === 'ready') {
|
|
103
|
+
started = true;
|
|
104
|
+
clearTimeout(timer);
|
|
105
|
+
resolve();
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
clearTimeout(timer);
|
|
109
|
+
reject(failure('Native service could not initialize.', 'tool.exit.code'));
|
|
110
|
+
void this.terminate();
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const waiter = this.inflight;
|
|
114
|
+
if (!waiter || message.id !== waiter.id || !message.result || typeof message.result !== 'object') {
|
|
115
|
+
void this.abort('coordinator.malformed');
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
// A service that announces its exit takes no further request: queued ones are then never sent.
|
|
119
|
+
if (message.closing === true)
|
|
120
|
+
this.dead = true;
|
|
121
|
+
this.inflight = undefined;
|
|
122
|
+
clearTimeout(waiter.timer);
|
|
123
|
+
void this.retain(waiter);
|
|
124
|
+
waiter.resolve(message.result);
|
|
125
|
+
}
|
|
126
|
+
if (this.buffer.length > lineLimit)
|
|
127
|
+
void this.abort('tool.output.limit');
|
|
128
|
+
});
|
|
129
|
+
child.once('exit', () => { clearTimeout(timer); this.dead = true; if (!started)
|
|
130
|
+
reject(failure('Native service exited before it was ready.', 'tool.exit.code')); void this.abortDrained(); });
|
|
131
|
+
child.stdin.on('error', () => { void this.abortDrained(); });
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
exited;
|
|
135
|
+
drained;
|
|
136
|
+
/**
|
|
137
|
+
* A service whose process ended (or whose input broke) fails its request only after its output drained: exit,
|
|
138
|
+
* then closed stdio, each bounded, so the failure carries all of that request's output.
|
|
139
|
+
*/
|
|
140
|
+
async abortDrained() {
|
|
141
|
+
this.dead = true;
|
|
142
|
+
const bounded = (promise) => Promise.race([promise, new Promise(resolve => setTimeout(resolve, 1000).unref())]);
|
|
143
|
+
if (!this.exitedNow())
|
|
144
|
+
await bounded(this.exited);
|
|
145
|
+
await bounded(this.drained);
|
|
146
|
+
return this.abort('tool.exit.code');
|
|
147
|
+
}
|
|
148
|
+
/** Per-request stderr beside its request file, bounded and redacted, as one-shot helpers retained it. */
|
|
149
|
+
requestStderr(waiter) { return this.redact(Buffer.concat(this.stderr).subarray(Math.max(0, waiter.stderrFrom - this.stderrStart)).toString('utf8')).slice(-stderrLimit); }
|
|
150
|
+
async retain(waiter) {
|
|
151
|
+
try {
|
|
152
|
+
const text = this.requestStderr(waiter);
|
|
153
|
+
if (text)
|
|
154
|
+
await writeFile(join(dirname(waiter.requestFile), 'service-stderr.log'), text.slice(-stderrLimit), { mode: 0o600 });
|
|
155
|
+
}
|
|
156
|
+
catch { /* Diagnostics never change an outcome. */ }
|
|
157
|
+
}
|
|
158
|
+
/** The process ID while this service can still serve requests (not dead or draining). */
|
|
159
|
+
get servingPid() { return this.dead || this.draining ? undefined : this.child.pid; }
|
|
160
|
+
/** The process has exited by now (a termination once reported unverified may have completed since). */
|
|
161
|
+
exitedNow() { return this.child.exitCode !== null || this.child.signalCode !== null; }
|
|
162
|
+
/** Terminate with escalation; the result says whether the exit was proved. */
|
|
163
|
+
terminate() {
|
|
164
|
+
this.dead = true;
|
|
165
|
+
return this.terminating ??= (async () => {
|
|
166
|
+
const exited = () => this.child.exitCode !== null || this.child.signalCode !== null;
|
|
167
|
+
const closed = async (ms) => { if (exited())
|
|
168
|
+
return true; await Promise.race([this.child.toolClosed?.catch(() => { }), new Promise(resolve => setTimeout(resolve, ms).unref())]); return exited(); };
|
|
169
|
+
if (!exited()) {
|
|
170
|
+
try {
|
|
171
|
+
this.child.stdin.end();
|
|
172
|
+
}
|
|
173
|
+
catch { /* Closed. */ }
|
|
174
|
+
try {
|
|
175
|
+
this.child.kill('SIGTERM');
|
|
176
|
+
}
|
|
177
|
+
catch { /* Gone. */ }
|
|
178
|
+
}
|
|
179
|
+
if (!await closed(5000)) {
|
|
180
|
+
try {
|
|
181
|
+
this.child.kill('SIGKILL');
|
|
182
|
+
}
|
|
183
|
+
catch { /* Gone. */ }
|
|
184
|
+
}
|
|
185
|
+
const proved = await closed(5000);
|
|
186
|
+
try {
|
|
187
|
+
await writeFile(join(this.launch.root, 'stderr.log'), this.redact(Buffer.concat(this.stderr).toString('utf8')), { mode: 0o600 });
|
|
188
|
+
}
|
|
189
|
+
catch { /* Diagnostics only. */ }
|
|
190
|
+
return proved ? 'exited' : 'unverified';
|
|
191
|
+
})();
|
|
192
|
+
}
|
|
193
|
+
/** End the service; its dispatched request (if any) is reported only after the termination result. */
|
|
194
|
+
async abort(code) {
|
|
195
|
+
const waiter = this.inflight;
|
|
196
|
+
this.inflight = undefined;
|
|
197
|
+
const result = await this.terminate();
|
|
198
|
+
if (waiter) {
|
|
199
|
+
clearTimeout(waiter.timer);
|
|
200
|
+
void this.retain(waiter);
|
|
201
|
+
// Only this request's own output is attached, so a failure is never classified by an earlier request's output.
|
|
202
|
+
const stderr = this.requestStderr(waiter);
|
|
203
|
+
waiter.reject(result === 'unverified' ? failure('Tool termination could not be verified; its process record blocks removal.', 'tool.termination.unverified', { diagnostics: this.launch.root, stderr }) :
|
|
204
|
+
failure(code === 'coordinator.malformed' ? 'Native server operation returned malformed content.' : 'Tool execution did not complete successfully: ' + code.replace(/^tool\./, '').replaceAll('.', '_') + '.', code, { diagnostics: this.launch.root, stderr }));
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
call(requestFile, timeoutMs, signal, queueMs) {
|
|
208
|
+
this.busy++;
|
|
209
|
+
let dispatched = false, cancelled = false, queued, queuedAbort;
|
|
210
|
+
// Waiting in the queue is bounded and cancellable; such a request was never sent.
|
|
211
|
+
const notSent = new Promise((_ok, reject) => {
|
|
212
|
+
queued = setTimeout(() => { if (!dispatched) {
|
|
213
|
+
cancelled = true;
|
|
214
|
+
reject(new NotSent('queue'));
|
|
215
|
+
} }, queueMs);
|
|
216
|
+
queued.unref();
|
|
217
|
+
queuedAbort = () => { if (!dispatched) {
|
|
218
|
+
cancelled = true;
|
|
219
|
+
reject(new NotSent('cancelled'));
|
|
220
|
+
} };
|
|
221
|
+
signal.addEventListener('abort', queuedAbort, { once: true });
|
|
222
|
+
});
|
|
223
|
+
notSent.catch(() => { });
|
|
224
|
+
const run = this.queue.then(async () => {
|
|
225
|
+
clearTimeout(queued);
|
|
226
|
+
signal.removeEventListener('abort', queuedAbort);
|
|
227
|
+
if (cancelled)
|
|
228
|
+
throw new NotSent('abandoned');
|
|
229
|
+
if (this.dead)
|
|
230
|
+
throw new NotSent('dead');
|
|
231
|
+
const lease = await withToolUse(this.launch.directory, 'ghidra', async () => undefined);
|
|
232
|
+
try {
|
|
233
|
+
// Until its line is written the request is not sent: a cancellation or a dead service here is safe.
|
|
234
|
+
if (signal.aborted)
|
|
235
|
+
throw new NotSent('cancelled');
|
|
236
|
+
if (this.dead)
|
|
237
|
+
throw new NotSent('dead');
|
|
238
|
+
dispatched = true;
|
|
239
|
+
return await new Promise((resolve, reject) => {
|
|
240
|
+
const id = randomUUID();
|
|
241
|
+
const timer = setTimeout(() => { void this.abort('tool.timeout'); }, timeoutMs);
|
|
242
|
+
timer.unref();
|
|
243
|
+
const abort = () => { void this.abort('tool.cancelled'); };
|
|
244
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
245
|
+
const done = (settle) => (value) => { signal.removeEventListener('abort', abort); settle(value); };
|
|
246
|
+
this.inflight = { id, timer, requestFile, stderrFrom: this.stderrTotal, resolve: done(resolve), reject: done(reject) };
|
|
247
|
+
try {
|
|
248
|
+
this.child.stdin.write(JSON.stringify({ id, request: requestFile }) + '\n');
|
|
249
|
+
}
|
|
250
|
+
catch {
|
|
251
|
+
void this.abortDrained();
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
finally {
|
|
256
|
+
lease.release();
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
this.queue = run.catch(() => { });
|
|
260
|
+
return Promise.race([run, notSent]).finally(() => { this.busy--; if (this.draining && this.busy === 0)
|
|
261
|
+
void this.terminate(); });
|
|
262
|
+
}
|
|
263
|
+
/** Retire after the queued requests finish (credential key replaced). */
|
|
264
|
+
drain() { if (this.busy === 0)
|
|
265
|
+
void this.terminate();
|
|
266
|
+
else
|
|
267
|
+
this.draining = true; }
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Long-lived native services of one profile owner: per role and credential key at most two services, requests
|
|
271
|
+
* per service in order. Undefined from `call` means nothing was sent and a one-shot helper must run it.
|
|
272
|
+
*/
|
|
273
|
+
export class NativeServices {
|
|
274
|
+
options;
|
|
275
|
+
pools = new Map();
|
|
276
|
+
starting = new Map();
|
|
277
|
+
closed = false;
|
|
278
|
+
failures = 0;
|
|
279
|
+
disabledUntil = 0;
|
|
280
|
+
/** Services out of rotation (dead, draining) whose exit is not proved yet; still tracked until it is. */
|
|
281
|
+
retired = new Set();
|
|
282
|
+
constructor(options = {}) {
|
|
283
|
+
this.options = options;
|
|
284
|
+
}
|
|
285
|
+
async call(role, key, launch, requestFile, timeoutMs, signal) {
|
|
286
|
+
// Services of a previous credential key drain and stop; they never take requests of another key.
|
|
287
|
+
for (const [name, pool] of this.pools)
|
|
288
|
+
if (name.startsWith(role + ':') && name !== role + ':' + key)
|
|
289
|
+
for (const service of pool)
|
|
290
|
+
service.drain();
|
|
291
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
292
|
+
if (this.closed || Date.now() < this.disabledUntil)
|
|
293
|
+
return;
|
|
294
|
+
const service = await this.pick(role, key, launch);
|
|
295
|
+
if (!service)
|
|
296
|
+
return;
|
|
297
|
+
try {
|
|
298
|
+
return await timed('native.service', role, () => service.call(requestFile, timeoutMs, signal, this.options.queueMs ?? 5000));
|
|
299
|
+
}
|
|
300
|
+
catch (error) {
|
|
301
|
+
if (!(error instanceof NotSent))
|
|
302
|
+
throw error;
|
|
303
|
+
// Distinct from a cancelled dispatched request: this one provably never reached a service.
|
|
304
|
+
if (error.message === 'cancelled')
|
|
305
|
+
throw failure('Native operation was cancelled before it was sent.', 'tool.notSent');
|
|
306
|
+
// Both services stayed busy: run it in parallel as a one-shot helper, as before services existed.
|
|
307
|
+
if (error.message === 'queue')
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
async pick(role, key, launch) {
|
|
313
|
+
const name = role + ':' + key;
|
|
314
|
+
let pool = this.pools.get(name);
|
|
315
|
+
if (!pool) {
|
|
316
|
+
pool = [];
|
|
317
|
+
this.pools.set(name, pool);
|
|
318
|
+
}
|
|
319
|
+
for (let index = pool.length - 1; index >= 0; index--)
|
|
320
|
+
if (pool[index].dead || pool[index].draining) {
|
|
321
|
+
this.retired.add(pool[index]);
|
|
322
|
+
pool.splice(index, 1);
|
|
323
|
+
}
|
|
324
|
+
const idle = pool.find(service => service.busy === 0);
|
|
325
|
+
if (idle)
|
|
326
|
+
return idle;
|
|
327
|
+
const starting = this.starting.get(name) ?? [];
|
|
328
|
+
this.starting.set(name, starting);
|
|
329
|
+
if (pool.length + starting.length < maximumPerKey) {
|
|
330
|
+
// No idle service and room in the pool: this caller starts its own.
|
|
331
|
+
const start = timed('native.service.start', role, async () => Service.start(role, key, await launch(), this.options.readyMs ?? 180000));
|
|
332
|
+
starting.push(start);
|
|
333
|
+
const current = pool;
|
|
334
|
+
try {
|
|
335
|
+
const service = await start;
|
|
336
|
+
this.failures = 0;
|
|
337
|
+
if (this.closed) {
|
|
338
|
+
void service.terminate();
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
current.push(service);
|
|
342
|
+
return service;
|
|
343
|
+
}
|
|
344
|
+
catch (error) {
|
|
345
|
+
if (error instanceof StartFailed && error.service)
|
|
346
|
+
this.retired.add(error.service);
|
|
347
|
+
// Repeated start failures leave one-shot helpers in charge for a while, never for the owner's lifetime.
|
|
348
|
+
if (++this.failures >= failureLimit) {
|
|
349
|
+
this.disabledUntil = Date.now() + disabledMs;
|
|
350
|
+
this.failures = 0;
|
|
351
|
+
}
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
finally {
|
|
355
|
+
starting.splice(starting.indexOf(start), 1);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
const live = pool.filter(service => !service.dead && !service.draining);
|
|
359
|
+
if (live.length)
|
|
360
|
+
return live.reduce((best, service) => service.busy < best.busy ? service : best, live[0]);
|
|
361
|
+
// Both members are still starting: wait for one of them.
|
|
362
|
+
try {
|
|
363
|
+
const service = await Promise.any(starting);
|
|
364
|
+
return this.closed || service.dead ? undefined : service;
|
|
365
|
+
}
|
|
366
|
+
catch {
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Stop idle services now and await their proved exit; true only when nothing remains running or starting.
|
|
372
|
+
* Busy services are left untouched (no draining), so a refused maintenance attempt costs nothing later.
|
|
373
|
+
*/
|
|
374
|
+
/** Process IDs of this owner's services that are tracked and still serving; every other service must prove its exit. */
|
|
375
|
+
servingPids(role) { return new Set([...this.pools.entries()].filter(([name]) => name.startsWith(role + ':')).flatMap(([, pool]) => pool.map(service => service.servingPid)).filter((pid) => pid !== undefined)); }
|
|
376
|
+
async stopIdle() {
|
|
377
|
+
for (const pool of this.pools.values())
|
|
378
|
+
for (const service of [...pool])
|
|
379
|
+
if (service.busy === 0) {
|
|
380
|
+
// Concurrent picks may have moved it meanwhile; never remove by a missing index.
|
|
381
|
+
const index = pool.indexOf(service);
|
|
382
|
+
if (index >= 0)
|
|
383
|
+
pool.splice(index, 1);
|
|
384
|
+
this.retired.add(service);
|
|
385
|
+
}
|
|
386
|
+
for (const service of [...this.retired])
|
|
387
|
+
if (service.busy === 0 && (await service.terminate() === 'exited' || service.exitedNow()))
|
|
388
|
+
this.retired.delete(service);
|
|
389
|
+
// Recount after the awaits: anything busy, starting, or with an unproved exit keeps the profile busy.
|
|
390
|
+
return [...this.starting.values()].every(list => list.length === 0) && [...this.pools.values()].every(pool => pool.length === 0) && this.retired.size === 0;
|
|
391
|
+
}
|
|
392
|
+
/** Close for good: no further requests are accepted and every service terminates. */
|
|
393
|
+
async stop() {
|
|
394
|
+
this.closed = true;
|
|
395
|
+
const all = [...this.pools.values()].flat().concat([...this.retired]);
|
|
396
|
+
this.pools.clear();
|
|
397
|
+
this.retired.clear();
|
|
398
|
+
for (const start of [...this.starting.values()].flat())
|
|
399
|
+
try {
|
|
400
|
+
all.push(await start);
|
|
401
|
+
}
|
|
402
|
+
catch { /* Never started. */ }
|
|
403
|
+
this.starting.clear();
|
|
404
|
+
await Promise.all(all.map(service => service.terminate()));
|
|
405
|
+
}
|
|
406
|
+
}
|
package/dist/src/research.d.ts
CHANGED
|
@@ -47,11 +47,18 @@ export interface ResearchBackend {
|
|
|
47
47
|
state: 'published';
|
|
48
48
|
revision: number;
|
|
49
49
|
snapshot?: Artifact;
|
|
50
|
+
observation?: Record<string, unknown>;
|
|
50
51
|
} | {
|
|
51
52
|
state: 'conflict';
|
|
52
53
|
applied: 0;
|
|
53
54
|
conflict?: string;
|
|
54
55
|
}>;
|
|
56
|
+
/** Keep a verified publication's checkout as the context's working copy at the published version; false keeps disposal. */
|
|
57
|
+
continueNative?(scope: Scope, observation: Record<string, unknown>): Promise<boolean>;
|
|
58
|
+
/** Stop idle services and idle leased workers before exclusive maintenance. */
|
|
59
|
+
quiesce?(): Promise<void>;
|
|
60
|
+
/** Reopen (or stop) a worker parked by this command's native Update/check-in. */
|
|
61
|
+
resumeParked?(scope: Scope): Promise<void>;
|
|
55
62
|
dropNative?(scope: Scope, request?: string): Promise<void>;
|
|
56
63
|
finishEmptyNative?(scope: Scope, request?: string): Promise<void>;
|
|
57
64
|
recoverEmptyNative?(scope: Scope, request: string): Promise<{
|
|
@@ -64,6 +71,11 @@ export interface ResearchBackend {
|
|
|
64
71
|
baseRevision: number;
|
|
65
72
|
retainedKeepFile: string;
|
|
66
73
|
}>;
|
|
74
|
+
/** Ghidra restores and saves (keep) or deletes the unsaved changes an interrupted session left in the checkout. */
|
|
75
|
+
settleUnsaved?(scope: Scope, keep: boolean): Promise<{
|
|
76
|
+
state: 'recovered' | 'discarded' | 'absent';
|
|
77
|
+
modified: boolean;
|
|
78
|
+
}>;
|
|
67
79
|
recoverDiscardNative?(scope: Scope, request: string, original: NativeCheckout): Promise<{
|
|
68
80
|
state: 'verified';
|
|
69
81
|
baseRevision: number;
|
|
@@ -132,6 +144,16 @@ export declare function scalarText(value: unknown): string | null;
|
|
|
132
144
|
export declare function commentKind(value: unknown): "eol" | "pre" | "post" | "plate" | "repeatable";
|
|
133
145
|
export declare function number(value: unknown, maximum: number, minimum?: number): number;
|
|
134
146
|
export declare function scope(options: Record<string, unknown>): Scope;
|
|
147
|
+
/** A not-sent request keeps its memory diagnosis in its saved result, not only in the first response. */
|
|
148
|
+
export declare const memoryOf: (error: unknown) => {
|
|
149
|
+
memory?: string | undefined;
|
|
150
|
+
memoryCause?: unknown;
|
|
151
|
+
cause?: {
|
|
152
|
+
code: string;
|
|
153
|
+
message: string;
|
|
154
|
+
} | undefined;
|
|
155
|
+
phase?: string | undefined;
|
|
156
|
+
};
|
|
135
157
|
/** One ordered working context per tuple, shared by every transport through the Factory owner. */
|
|
136
158
|
export declare class Research {
|
|
137
159
|
readonly workspace: Workspace;
|
|
@@ -177,7 +199,7 @@ export declare class Research {
|
|
|
177
199
|
state: "active" | "verified" | "dropped";
|
|
178
200
|
} | undefined;
|
|
179
201
|
syncPolicy: import("./workspace.js").SyncPolicy;
|
|
180
|
-
state: "
|
|
202
|
+
state: "ready" | "opening" | "stopped" | "needs_recovery";
|
|
181
203
|
baseRevision: string | undefined;
|
|
182
204
|
engine: string;
|
|
183
205
|
};
|
|
@@ -322,10 +344,16 @@ export declare class Research {
|
|
|
322
344
|
edit(selected: Scope, at: string, field: ScalarChange['field'], value: string | null, callerId?: string, resolution?: {
|
|
323
345
|
expectedShared: string | null;
|
|
324
346
|
}): Promise<unknown>;
|
|
347
|
+
/** A worker parked by native Update/check-in is reopened or stopped before the command ends, whatever its outcome. */
|
|
348
|
+
private resuming;
|
|
325
349
|
private sharedOrder;
|
|
326
350
|
reconcileRequests(selected: Scope, backend: ResearchBackend): Promise<void>;
|
|
327
351
|
private reconcile;
|
|
328
|
-
|
|
352
|
+
/**
|
|
353
|
+
* `close` releases the working copy after publication; otherwise the context continues at the published version.
|
|
354
|
+
* It only chooses what follows a publication, so it is not part of the request identity.
|
|
355
|
+
*/
|
|
356
|
+
merge(selected: Scope, callerId?: string, close?: boolean): Promise<unknown>;
|
|
329
357
|
refresh(selected: Scope, callerId?: string): Promise<unknown>;
|
|
330
358
|
mutate(selected: Scope, operation: string, parameters: Record<string, unknown>, verify: (backend: ResearchBackend, result: Record<string, unknown>) => Promise<unknown>, callerId?: string, verifySaved?: (backend: ResearchBackend, snapshot: Artifact, result: Record<string, unknown>) => Promise<unknown>): Promise<unknown>;
|
|
331
359
|
}
|