@floydous/pi-agent-kernel 0.4.2 → 0.4.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 +12 -0
- package/package.json +1 -1
- package/src/editing/syntax-verify.ts +109 -11
- package/src/safety/epistemic_guard.ts +17 -1
- package/src/safety/output_clamper.ts +45 -29
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.4.3] - 2026-10-24
|
|
4
|
+
|
|
5
|
+
### Fixed
|
|
6
|
+
- **Syntax Verifier Host Executable Resolution**: Added `resolveNodeExecutable` in `src/editing/syntax-verify.ts` to probe candidate Node runtimes (including Electron environments with `ELECTRON_RUN_AS_NODE=1` and system `PATH` discovery) before running `--check` or `tsc`. Resolves false-alarm `[SYNTAX WARNING]` notices when operating inside packaged host environments where `process.execPath` is not a standard Node CLI.
|
|
7
|
+
- **Differentiated Syntax Diagnostics from Operational Crashes**: Syntax verification only classifies results as `failed` when genuine grammar diagnostics (`SyntaxError`, TypeScript core grammar codes `TS1000`–`TS1999`, or JSX codes `TS17000`–`TS17999`) are emitted. Runtime/checker operational faults or missing dependencies fail open gracefully with `status: "unavailable"`.
|
|
8
|
+
- **Host Truncation Deduplication**: Extended `HOST_TRUNCATION_REGEX` in `src/safety/output_clamper.ts` to match modern Pi host truncation footers (`[Truncated by host harness. Full: ...]`) with strict line termination, eliminating duplicate stacked notices.
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
- **Spillover Recovery Affordances & Omitted Line Ranges**: Middle truncation markers in `output_clamper.ts` now explicitly state the 1-based omitted line span (`[... N lines omitted (lines A–B) ...]`), and spillover footers provide an actionable tool-native `read` recipe (`read({ path, offset, limit })`) when byte budgets permit, allowing agents and developers to retrieve omitted failure regions in a single targeted turn without guessing line coordinates.
|
|
12
|
+
- **Actionable Stale-File Drift Diagnostics**: Enhanced `epistemic_guard.ts` stale-file drift rejections to calculate pinpoint read coordinates (`offset` and `limit`) around target edit ranges, enabling agents to re-synchronize in a single narrow read rather than prompting whole-file reads.
|
|
13
|
+
- **Ergonomics & Safety Boundary Adversarial Test Suite**: Added `tests/security-hardening/ergonomics_adversarial_test.ts` verifying syntax verification boundaries, search-to-edit boundaries, stale-drift pinpoint recovery, and forged truncation footer defense.
|
|
14
|
+
|
|
3
15
|
## [0.4.2] - 2026-10-24
|
|
4
16
|
|
|
5
17
|
### Added
|
package/package.json
CHANGED
|
@@ -202,6 +202,49 @@ function checkTsStructure(content: string): string | null {
|
|
|
202
202
|
return null;
|
|
203
203
|
}
|
|
204
204
|
|
|
205
|
+
let cachedNodeExec: { bin: string; env?: Record<string, string> } | null | undefined;
|
|
206
|
+
|
|
207
|
+
export function resolveNodeExecutable(): { bin: string; env?: Record<string, string> } | null {
|
|
208
|
+
if (cachedNodeExec !== undefined) return cachedNodeExec;
|
|
209
|
+
|
|
210
|
+
const candidates: { bin: string; env?: Record<string, string> }[] = [];
|
|
211
|
+
|
|
212
|
+
if (process.versions?.electron) {
|
|
213
|
+
candidates.push({ bin: process.execPath, env: { ELECTRON_RUN_AS_NODE: "1" } });
|
|
214
|
+
}
|
|
215
|
+
candidates.push({ bin: process.execPath });
|
|
216
|
+
candidates.push({ bin: "node" });
|
|
217
|
+
|
|
218
|
+
for (const cand of candidates) {
|
|
219
|
+
try {
|
|
220
|
+
const env = cand.env ? { ...process.env, ...cand.env } : process.env;
|
|
221
|
+
const res = execFileSync(cand.bin, ["-e", "process.stdout.write(process.versions.node || '')"], {
|
|
222
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
223
|
+
timeout: 2000,
|
|
224
|
+
env,
|
|
225
|
+
encoding: "utf8",
|
|
226
|
+
});
|
|
227
|
+
if (res && res.trim()) {
|
|
228
|
+
cachedNodeExec = cand;
|
|
229
|
+
return cand;
|
|
230
|
+
}
|
|
231
|
+
} catch {}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
cachedNodeExec = null;
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export function resetNodeExecutableCache(): void {
|
|
239
|
+
cachedNodeExec = undefined;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export function setNodeExecutableForTesting(
|
|
243
|
+
nodeExec: { bin: string; env?: Record<string, string> } | null | undefined,
|
|
244
|
+
): void {
|
|
245
|
+
cachedNodeExec = nodeExec;
|
|
246
|
+
}
|
|
247
|
+
|
|
205
248
|
export function resolveTypeScriptCompiler(): string {
|
|
206
249
|
try {
|
|
207
250
|
return path.join(path.dirname(require.resolve("typescript/package.json")), "bin", "tsc");
|
|
@@ -248,10 +291,41 @@ function validateSyntaxAtPath(resolvedPath: string): VerificationResult {
|
|
|
248
291
|
const content = fs.readFileSync(resolvedPath, "utf8");
|
|
249
292
|
JSON.parse(content);
|
|
250
293
|
} else if (ext === ".js" || ext === ".mjs" || ext === ".cjs") {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
294
|
+
const nodeExec = resolveNodeExecutable();
|
|
295
|
+
if (!nodeExec) {
|
|
296
|
+
return {
|
|
297
|
+
valid: true,
|
|
298
|
+
status: "unavailable",
|
|
299
|
+
error: "Node runtime unavailable for syntax verification",
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
try {
|
|
303
|
+
execFileSync(nodeExec.bin, ["--check", resolvedPath], {
|
|
304
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
305
|
+
timeout: 5000,
|
|
306
|
+
env: nodeExec.env ? { ...process.env, ...nodeExec.env } : process.env,
|
|
307
|
+
});
|
|
308
|
+
} catch (error: any) {
|
|
309
|
+
const stderr = error?.stderr?.toString?.() || "";
|
|
310
|
+
const stdout = error?.stdout?.toString?.() || "";
|
|
311
|
+
const output = (stderr || stdout || error?.message || "").trim();
|
|
312
|
+
if (
|
|
313
|
+
output.includes("SyntaxError") ||
|
|
314
|
+
output.includes("Unexpected token") ||
|
|
315
|
+
output.includes("Parsing error")
|
|
316
|
+
) {
|
|
317
|
+
return {
|
|
318
|
+
valid: false,
|
|
319
|
+
status: "failed",
|
|
320
|
+
error: `Syntax validation failed on ${path.basename(resolvedPath)}:\n${output}`,
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
return {
|
|
324
|
+
valid: true,
|
|
325
|
+
status: "unavailable",
|
|
326
|
+
error: output || "Node syntax check unavailable",
|
|
327
|
+
};
|
|
328
|
+
}
|
|
255
329
|
} else if (
|
|
256
330
|
ext === ".ts" ||
|
|
257
331
|
ext === ".tsx" ||
|
|
@@ -266,10 +340,25 @@ function validateSyntaxAtPath(resolvedPath: string): VerificationResult {
|
|
|
266
340
|
if (!isJsx) {
|
|
267
341
|
const tsContent = fs.readFileSync(resolvedPath, "utf8");
|
|
268
342
|
const structuralError = checkTsStructure(tsContent);
|
|
269
|
-
if (structuralError)
|
|
343
|
+
if (structuralError) {
|
|
344
|
+
return {
|
|
345
|
+
valid: false,
|
|
346
|
+
status: "failed",
|
|
347
|
+
error: `Syntax validation failed on ${path.basename(resolvedPath)}:\n${structuralError}`,
|
|
348
|
+
};
|
|
349
|
+
}
|
|
270
350
|
}
|
|
271
351
|
|
|
352
|
+
const nodeExec = resolveNodeExecutable();
|
|
272
353
|
const compiler = resolveTypeScriptCompiler();
|
|
354
|
+
if (!nodeExec || !fs.existsSync(compiler)) {
|
|
355
|
+
return {
|
|
356
|
+
valid: true,
|
|
357
|
+
status: "unavailable",
|
|
358
|
+
error: "TypeScript compiler unavailable",
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
|
|
273
362
|
const compilerArgs = [
|
|
274
363
|
"--noEmit",
|
|
275
364
|
"--noCheck",
|
|
@@ -288,19 +377,28 @@ function validateSyntaxAtPath(resolvedPath: string): VerificationResult {
|
|
|
288
377
|
resolvedPath,
|
|
289
378
|
];
|
|
290
379
|
try {
|
|
291
|
-
execFileSync(
|
|
292
|
-
stdio: "pipe",
|
|
380
|
+
execFileSync(nodeExec.bin, [compiler, ...compilerArgs], {
|
|
381
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
293
382
|
timeout: 5000,
|
|
383
|
+
env: nodeExec.env ? { ...process.env, ...nodeExec.env } : process.env,
|
|
294
384
|
});
|
|
295
385
|
} catch (error: any) {
|
|
296
|
-
|
|
386
|
+
const stderr = error?.stderr?.toString?.() || "";
|
|
387
|
+
const stdout = error?.stdout?.toString?.() || "";
|
|
388
|
+
const output = (stderr || stdout || error?.message || "").trim();
|
|
389
|
+
// TS syntax errors are TS1000-TS1999 (grammar) and TS17000-TS17999 (JSX syntax)
|
|
390
|
+
if (/error TS1(?:\d{3}|7\d{3}):/.test(output) || output.includes("SyntaxError")) {
|
|
297
391
|
return {
|
|
298
392
|
valid: false,
|
|
299
|
-
status: "
|
|
300
|
-
error:
|
|
393
|
+
status: "failed",
|
|
394
|
+
error: `Syntax validation failed on ${path.basename(resolvedPath)}:\n${output}`,
|
|
301
395
|
};
|
|
302
396
|
}
|
|
303
|
-
|
|
397
|
+
return {
|
|
398
|
+
valid: true,
|
|
399
|
+
status: "unavailable",
|
|
400
|
+
error: output || "TypeScript compiler unavailable",
|
|
401
|
+
};
|
|
304
402
|
}
|
|
305
403
|
} else {
|
|
306
404
|
return {
|
|
@@ -1113,9 +1113,25 @@ export class EpistemicGuard {
|
|
|
1113
1113
|
// 2. Stale file drift check
|
|
1114
1114
|
const currentFingerprint = this.fingerprint(resolvedPath, workspace);
|
|
1115
1115
|
if (!currentFingerprint || currentFingerprint !== evidence.fingerprint) {
|
|
1116
|
+
let remedy = `read({ path: "${relPath}" })`;
|
|
1117
|
+
if (
|
|
1118
|
+
targetRanges &&
|
|
1119
|
+
targetRanges.length > 0 &&
|
|
1120
|
+
targetRanges[0] &&
|
|
1121
|
+
Number.isFinite(targetRanges[0].startLine) &&
|
|
1122
|
+
Number.isFinite(targetRanges[0].endLine) &&
|
|
1123
|
+
targetRanges[0].startLine >= 1 &&
|
|
1124
|
+
targetRanges[0].endLine >= targetRanges[0].startLine
|
|
1125
|
+
) {
|
|
1126
|
+
const start = Math.max(1, targetRanges[0].startLine - 2);
|
|
1127
|
+
const span = targetRanges[0].endLine - start + 3;
|
|
1128
|
+
const limit = Math.min(100, Math.max(15, span));
|
|
1129
|
+
const blockSuffix = targetRanges.length > 1 ? ` (block 1/${targetRanges.length})` : "";
|
|
1130
|
+
remedy = `read({ path: "${relPath}", offset: ${start}, limit: ${limit} })${blockSuffix}`;
|
|
1131
|
+
}
|
|
1116
1132
|
return {
|
|
1117
1133
|
allowed: false,
|
|
1118
|
-
reason: `[BLOCKED: Stale File Drift] File '${relPath}' changed on disk since last observed
|
|
1134
|
+
reason: `[BLOCKED: Stale File Drift] File '${relPath}' changed on disk since last observed. Re-synchronize target lines -> ${remedy}`,
|
|
1119
1135
|
};
|
|
1120
1136
|
}
|
|
1121
1137
|
|
|
@@ -146,10 +146,10 @@ export function isExecutionOrTestCommand(command: string): boolean {
|
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
const HOST_TRUNCATION_REGEX =
|
|
149
|
-
/(?:\r?\n){2}\[Showing (?:lines \d+-\d+ of \d+(?: \([^)]+\))?|last [^)]+\))
|
|
149
|
+
/(?:\r?\n){1,2}\[(?:Showing (?:lines \d+-\d+ of \d+(?: \([^)]+\))?|last [^)]+\))\.[ \t]*Full output:|Truncated by host harness\.[ \t]*Full:)[ \t]*([^\]\r\n]+?)(?:[ \t]+\(use offset\/limit with read if needed\))?\][ \t\r\n]*$/;
|
|
150
150
|
|
|
151
151
|
const KERNEL_TRUNCATION_REGEX =
|
|
152
|
-
/(?:\r?\n){2}\[Truncated[^\r\n\]]*?(
|
|
152
|
+
/(?:\r?\n){2}\[Truncated[^\r\n\]]*?(?:\.[ \t]*(?:Full|Spillover):[ \t]*([^\]\r\n]+?)(?:[ \t]+\(use offset\/limit[^)]*\)|[ \t]+\(received fragment only\))?)?\][ \t\r\n]*$/;
|
|
153
153
|
|
|
154
154
|
function validateHostSpilloverPath(candidatePath: string): string | undefined {
|
|
155
155
|
try {
|
|
@@ -219,30 +219,30 @@ export function clampCommandOutput(
|
|
|
219
219
|
let hostSpilloverPath: string | undefined;
|
|
220
220
|
let priorSpilloverPath: string | undefined;
|
|
221
221
|
|
|
222
|
-
const
|
|
223
|
-
if (
|
|
224
|
-
|
|
225
|
-
textToProcess = rawText.slice(0,
|
|
226
|
-
const candidate =
|
|
227
|
-
|
|
228
|
-
const cleanCandidate = candidate.replace(/\s*\(use offset\/limit.*\)$/i, "").trim();
|
|
229
|
-
hostSpilloverPath = validateHostSpilloverPath(cleanCandidate);
|
|
230
|
-
if (!hostSpilloverPath && fs.existsSync(cleanCandidate)) {
|
|
231
|
-
try {
|
|
232
|
-
const lstat = fs.lstatSync(cleanCandidate);
|
|
233
|
-
if (!lstat.isSymbolicLink() && lstat.isFile()) {
|
|
234
|
-
priorSpilloverPath = path.resolve(cleanCandidate);
|
|
235
|
-
}
|
|
236
|
-
} catch {}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
222
|
+
const hostMatch = rawText.match(HOST_TRUNCATION_REGEX);
|
|
223
|
+
if (hostMatch && hostMatch.index !== undefined) {
|
|
224
|
+
hostTruncated = true;
|
|
225
|
+
textToProcess = rawText.slice(0, hostMatch.index);
|
|
226
|
+
const candidate = hostMatch[1].trim();
|
|
227
|
+
hostSpilloverPath = validateHostSpilloverPath(candidate);
|
|
239
228
|
} else {
|
|
240
|
-
const
|
|
241
|
-
if (
|
|
242
|
-
|
|
243
|
-
textToProcess = rawText.slice(0,
|
|
244
|
-
const candidate =
|
|
245
|
-
|
|
229
|
+
const kernelMatch = rawText.match(KERNEL_TRUNCATION_REGEX);
|
|
230
|
+
if (kernelMatch && kernelMatch.index !== undefined) {
|
|
231
|
+
priorKernelTruncated = true;
|
|
232
|
+
textToProcess = rawText.slice(0, kernelMatch.index);
|
|
233
|
+
const candidate = kernelMatch[1]?.trim();
|
|
234
|
+
if (candidate) {
|
|
235
|
+
const cleanCandidate = candidate.replace(/\s*\(use offset\/limit.*\)$/i, "").trim();
|
|
236
|
+
hostSpilloverPath = validateHostSpilloverPath(cleanCandidate);
|
|
237
|
+
if (!hostSpilloverPath && fs.existsSync(cleanCandidate)) {
|
|
238
|
+
try {
|
|
239
|
+
const lstat = fs.lstatSync(cleanCandidate);
|
|
240
|
+
if (!lstat.isSymbolicLink() && lstat.isFile()) {
|
|
241
|
+
priorSpilloverPath = path.resolve(cleanCandidate);
|
|
242
|
+
}
|
|
243
|
+
} catch {}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
246
|
}
|
|
247
247
|
}
|
|
248
248
|
|
|
@@ -344,6 +344,8 @@ export function clampCommandOutput(
|
|
|
344
344
|
/-->\s+\S+:\d+:\d+/,
|
|
345
345
|
];
|
|
346
346
|
|
|
347
|
+
let primaryOmittedRange: { start: number; end: number; limit: number } | undefined;
|
|
348
|
+
|
|
347
349
|
if (totalLines > maxLines) {
|
|
348
350
|
const effectiveMaxLines = Math.max(2, maxLines);
|
|
349
351
|
const isDiscovery = isDiscoveryCommand(command);
|
|
@@ -392,14 +394,22 @@ export function clampCommandOutput(
|
|
|
392
394
|
clampedLines.push(formatLine(rawLines[i], i));
|
|
393
395
|
}
|
|
394
396
|
if (windowStart > headCount) {
|
|
395
|
-
|
|
397
|
+
const count = windowStart - headCount;
|
|
398
|
+
clampedLines.push(`\n[... ${count.toLocaleString()} lines omitted (lines ${headCount + 1}–${windowStart}) ...]\n`);
|
|
399
|
+
if (!primaryOmittedRange || count > primaryOmittedRange.limit) {
|
|
400
|
+
primaryOmittedRange = { start: headCount + 1, end: windowStart, limit: count };
|
|
401
|
+
}
|
|
396
402
|
}
|
|
397
403
|
// Diagnostic window
|
|
398
404
|
for (let i = windowStart; i < windowEnd; i++) {
|
|
399
405
|
clampedLines.push(formatLine(rawLines[i], i));
|
|
400
406
|
}
|
|
401
407
|
if (totalLines - tailCount > windowEnd) {
|
|
402
|
-
|
|
408
|
+
const count = totalLines - tailCount - windowEnd;
|
|
409
|
+
clampedLines.push(`\n[... ${count.toLocaleString()} lines omitted (lines ${windowEnd + 1}–${totalLines - tailCount}) ...]\n`);
|
|
410
|
+
if (!primaryOmittedRange || count > primaryOmittedRange.limit) {
|
|
411
|
+
primaryOmittedRange = { start: windowEnd + 1, end: totalLines - tailCount, limit: count };
|
|
412
|
+
}
|
|
403
413
|
}
|
|
404
414
|
// Tail lines
|
|
405
415
|
for (let i = totalLines - tailCount; i < totalLines; i++) {
|
|
@@ -418,13 +428,15 @@ export function clampCommandOutput(
|
|
|
418
428
|
clampedLines.push(formatLine(rawLines[i], i));
|
|
419
429
|
}
|
|
420
430
|
|
|
431
|
+
const tailStart = totalLines - tailCount;
|
|
432
|
+
|
|
421
433
|
// Middle omission marker
|
|
422
434
|
clampedLines.push(
|
|
423
|
-
`\n[... ${omittedLines.toLocaleString()} lines omitted ...]\n`,
|
|
435
|
+
`\n[... ${omittedLines.toLocaleString()} lines omitted (lines ${headCount + 1}–${tailStart}) ...]\n`,
|
|
424
436
|
);
|
|
437
|
+
primaryOmittedRange = { start: headCount + 1, end: tailStart, limit: omittedLines };
|
|
425
438
|
|
|
426
439
|
// Tail lines
|
|
427
|
-
const tailStart = totalLines - tailCount;
|
|
428
440
|
for (let i = tailStart; i < totalLines; i++) {
|
|
429
441
|
clampedLines.push(formatLine(rawLines[i], i));
|
|
430
442
|
}
|
|
@@ -453,6 +465,10 @@ export function clampCommandOutput(
|
|
|
453
465
|
let f = `\n\n[Truncated: ${linesShown}/${totalLines} lines.`;
|
|
454
466
|
if (spilloverPath) {
|
|
455
467
|
f += ` Full: ${spilloverPath} (use offset/limit with read if needed)]`;
|
|
468
|
+
if (primaryOmittedRange && maxTotalBytes >= 1024) {
|
|
469
|
+
const boundedLimit = Math.min(250, primaryOmittedRange.limit);
|
|
470
|
+
f += `\nTo inspect omitted lines ${primaryOmittedRange.start}–${primaryOmittedRange.end}: read({ path: "${spilloverPath}", offset: ${primaryOmittedRange.start}, limit: ${boundedLimit} })`;
|
|
471
|
+
}
|
|
456
472
|
} else {
|
|
457
473
|
f += `]`;
|
|
458
474
|
}
|