@cydm/pie 4.14.0 → 4.15.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/README.md +5 -5
- package/dist/builtin/extensions/ask-user/index.js +4 -4
- package/dist/builtin/extensions/cron/index.js +70 -17
- package/dist/builtin/extensions/im-forward/index.js +889 -205
- package/dist/builtin/extensions/memory/index.js +6 -6
- package/dist/builtin/extensions/memory/package.json +1 -2
- package/dist/builtin/extensions/pie-proc/README.md +12 -3
- package/dist/builtin/extensions/pie-proc/index.js +88 -45
- package/dist/builtin/extensions/plan-mode/index.js +4 -4
- package/dist/builtin/extensions/subagent/index.js +5 -5
- package/dist/builtin/extensions/todo/index.js +4 -4
- package/dist/chunks/{chunk-FD3KBPDR.js → chunk-6AYPNTCO.js} +8 -8
- package/dist/chunks/{chunk-JJH77AUU.js → chunk-J6EZ335F.js} +8 -7
- package/dist/chunks/{chunk-EZZTMDWZ.js → chunk-JE53PC46.js} +1064 -161
- package/dist/chunks/{chunk-BOQIGMGQ.js → chunk-LIA52LJU.js} +3 -3
- package/dist/chunks/{chunk-26KRUYZH.js → chunk-MTH6QSYD.js} +1 -1
- package/dist/chunks/{chunk-UYDS75SI.js → chunk-NAMH667S.js} +11 -15
- package/dist/chunks/{chunk-IK2FR2YF.js → chunk-VWDCKHCC.js} +1 -1
- package/dist/chunks/{chunk-MCR4JYI6.js → chunk-YKV45EMY.js} +313 -149
- package/dist/chunks/{chunk-JKO42HZW.js → chunk-ZBIR5YS7.js} +1 -1
- package/dist/chunks/{chunk-KGWKRO7Z.js → chunk-ZH6EAKVM.js} +2 -2
- package/dist/chunks/{host-image-fallback-LPHB5CTL.js → host-image-fallback-GX2KXZXQ.js} +1 -1
- package/dist/chunks/{memory-git-ZJN6SPIU.js → memory-git-BJMYZHIE.js} +5 -5
- package/dist/chunks/{model-registry-GLBIUOJR.js → model-registry-T6EMM66I.js} +2 -2
- package/dist/chunks/{read-file-understanding-DFJPTTSB.js → read-file-understanding-ZRE5KDG3.js} +5 -5
- package/dist/chunks/{src-KZ7YID3I.js → src-DLAIQCKP.js} +3 -3
- package/dist/chunks/{src-ALHMJZ5R.js → src-FWL3LCBK.js} +2 -2
- package/dist/chunks/{src-LEXKDSFV.js → src-OP6LFLR6.js} +8 -4
- package/dist/chunks/{src-76Q33OY5.js → src-XJKZIY3S.js} +11 -1
- package/dist/chunks/{test-stream-OVGDTXOY.js → test-stream-P3CUEGEE.js} +1 -1
- package/dist/cli.js +408 -52
- package/models.schema.json +4 -0
- package/package.json +2 -2
|
@@ -18,22 +18,23 @@ import {
|
|
|
18
18
|
parseEntryIdSeq,
|
|
19
19
|
readAncestryPage,
|
|
20
20
|
requestInteraction
|
|
21
|
-
} from "./chunk-
|
|
21
|
+
} from "./chunk-J6EZ335F.js";
|
|
22
22
|
import {
|
|
23
23
|
Agent,
|
|
24
24
|
getToolCallerIdentity
|
|
25
|
-
} from "./chunk-
|
|
25
|
+
} from "./chunk-MTH6QSYD.js";
|
|
26
26
|
import {
|
|
27
27
|
ALL_THINKING_LEVELS,
|
|
28
28
|
AssistantMessageEventStream,
|
|
29
29
|
completeSimple,
|
|
30
30
|
detectPlatform,
|
|
31
|
+
extractResponsesOutputText,
|
|
31
32
|
getFileSystem,
|
|
32
33
|
getSupportedThinkingLevels,
|
|
33
34
|
hashText,
|
|
34
35
|
resolveThinkingSelection,
|
|
35
36
|
streamSimple
|
|
36
|
-
} from "./chunk-
|
|
37
|
+
} from "./chunk-YKV45EMY.js";
|
|
37
38
|
import {
|
|
38
39
|
Type
|
|
39
40
|
} from "./chunk-XXGYUYXI.js";
|
|
@@ -1925,6 +1926,7 @@ function isRetryableSubagentFailure(message) {
|
|
|
1925
1926
|
}
|
|
1926
1927
|
function isRetryableSubagentFailureFrom(facts) {
|
|
1927
1928
|
if (facts.stopReason === "aborted" || facts.retryable === false) return false;
|
|
1929
|
+
if (facts.remoteFailure?.quotaExhausted === true) return false;
|
|
1928
1930
|
if (facts.replayClass === "stream_lost" || facts.replayClass === "stream_lost_partial") return true;
|
|
1929
1931
|
const message = facts.errorMessage ?? "";
|
|
1930
1932
|
if (isDefaultQuotaExhaustedError(message)) return false;
|
|
@@ -1998,6 +2000,7 @@ function failureDetails(facts, classification, recovery) {
|
|
|
1998
2000
|
...facts.remoteFailure !== void 0 ? {
|
|
1999
2001
|
remoteFailure: {
|
|
2000
2002
|
category: facts.remoteFailure.category,
|
|
2003
|
+
...facts.remoteFailure.quotaExhausted === true ? { quotaExhausted: true } : {},
|
|
2001
2004
|
...typeof facts.remoteFailure.retryAfterMs === "number" && Number.isFinite(facts.remoteFailure.retryAfterMs) && facts.remoteFailure.retryAfterMs >= 0 ? { retryAfterMs: facts.remoteFailure.retryAfterMs } : {}
|
|
2002
2005
|
}
|
|
2003
2006
|
} : {},
|
|
@@ -2388,24 +2391,782 @@ function transplantMessages(target, messages) {
|
|
|
2388
2391
|
}
|
|
2389
2392
|
}
|
|
2390
2393
|
|
|
2394
|
+
// ../../packages/shared-headless-capabilities/src/bash-policy-writes.ts
|
|
2395
|
+
function executableName(word) {
|
|
2396
|
+
const normalized = word.replace(/\\/g, "/").toLowerCase();
|
|
2397
|
+
const base = normalized.slice(normalized.lastIndexOf("/") + 1);
|
|
2398
|
+
return base.endsWith(".exe") ? base.slice(0, -4) : base;
|
|
2399
|
+
}
|
|
2400
|
+
var LEGACY_FILESYSTEM_WRITE_RE = /(^|[;&|\n])\s*(cp|mv|mkdir|touch|tee|sed|perl)\b/;
|
|
2401
|
+
var FILESYSTEM_WRITE_COMMANDS = /* @__PURE__ */ new Set(["cp", "mv", "mkdir", "touch", "tee", "sed", "perl"]);
|
|
2402
|
+
var SHELL_CONTROL_PREFIXES = /* @__PURE__ */ new Set(["!", "if", "then", "elif", "else", "do", "while", "until", "coproc", "{"]);
|
|
2403
|
+
var WRITE_WRAPPER_NAMES = /* @__PURE__ */ new Set(["env", "command", "exec", "nohup", "setsid", "time", "nice", "timeout", "xargs", "builtin"]);
|
|
2404
|
+
var WRITE_WRAPPER_VALUE_FLAGS = {
|
|
2405
|
+
env: /* @__PURE__ */ new Set(["-u", "--unset", "-C", "--chdir"]),
|
|
2406
|
+
exec: /* @__PURE__ */ new Set(["-a"]),
|
|
2407
|
+
time: /* @__PURE__ */ new Set(["-o", "-f", "--output", "--format"]),
|
|
2408
|
+
nice: /* @__PURE__ */ new Set(["-n", "--adjustment"]),
|
|
2409
|
+
timeout: /* @__PURE__ */ new Set(["-k", "--kill-after", "-s", "--signal"]),
|
|
2410
|
+
xargs: /* @__PURE__ */ new Set(["-a", "-d", "-E", "-I", "-L", "-n", "-P", "-s", "--arg-file", "--delimiter", "--eof", "--replace", "--max-lines", "--max-args", "--max-procs", "--max-chars"])
|
|
2411
|
+
};
|
|
2412
|
+
var STANDALONE_REDIR_RE = /^(?:\d+)?(?:>>?|<<?|<>|>&|<&|&>>?)$/;
|
|
2413
|
+
var INLINE_REDIR_RE = /^(?:\d+)?(?:>>?|<<?|<>|>&|<&|&>>?).+$/;
|
|
2414
|
+
function tokenizeCommandSegments(command, shell) {
|
|
2415
|
+
const escapeChar = shell === "powershell" ? "`" : "\\";
|
|
2416
|
+
const segments = [];
|
|
2417
|
+
const backtickBodies = [];
|
|
2418
|
+
const stack = [
|
|
2419
|
+
{ words: [], current: "", kind: "root", caseDepth: 0 }
|
|
2420
|
+
];
|
|
2421
|
+
const flushWord = () => {
|
|
2422
|
+
const top = stack[stack.length - 1];
|
|
2423
|
+
if (top.current) {
|
|
2424
|
+
if (top.words.length === 0) {
|
|
2425
|
+
const lower = top.current.toLowerCase();
|
|
2426
|
+
if (lower === "case") top.caseDepth += 1;
|
|
2427
|
+
else if (lower === "esac" && top.caseDepth > 0) top.caseDepth -= 1;
|
|
2428
|
+
}
|
|
2429
|
+
top.words.push(top.current);
|
|
2430
|
+
top.current = "";
|
|
2431
|
+
}
|
|
2432
|
+
};
|
|
2433
|
+
const flushSegment = () => {
|
|
2434
|
+
flushWord();
|
|
2435
|
+
const top = stack[stack.length - 1];
|
|
2436
|
+
if (top.words.length > 0) {
|
|
2437
|
+
segments.push(top.words);
|
|
2438
|
+
top.words = [];
|
|
2439
|
+
}
|
|
2440
|
+
};
|
|
2441
|
+
const readBacktickBody = (startIndex) => {
|
|
2442
|
+
let body = "";
|
|
2443
|
+
let i = startIndex + 1;
|
|
2444
|
+
for (; i < command.length; i += 1) {
|
|
2445
|
+
const c = command[i];
|
|
2446
|
+
if (c === "\\" && i + 1 < command.length) {
|
|
2447
|
+
const next = command[i + 1];
|
|
2448
|
+
if (next === "\n") {
|
|
2449
|
+
i += 1;
|
|
2450
|
+
continue;
|
|
2451
|
+
}
|
|
2452
|
+
if (next === "`" || next === "$" || next === "\\") {
|
|
2453
|
+
body += next;
|
|
2454
|
+
i += 1;
|
|
2455
|
+
continue;
|
|
2456
|
+
}
|
|
2457
|
+
body += c + next;
|
|
2458
|
+
i += 1;
|
|
2459
|
+
continue;
|
|
2460
|
+
}
|
|
2461
|
+
if (c === "`") break;
|
|
2462
|
+
body += c;
|
|
2463
|
+
}
|
|
2464
|
+
if (body) backtickBodies.push(body);
|
|
2465
|
+
return i;
|
|
2466
|
+
};
|
|
2467
|
+
for (let i = 0; i < command.length; i += 1) {
|
|
2468
|
+
const top = stack[stack.length - 1];
|
|
2469
|
+
const ch = command[i];
|
|
2470
|
+
if (top.quote === "'") {
|
|
2471
|
+
if (ch === "'") top.quote = void 0;
|
|
2472
|
+
else top.current += ch;
|
|
2473
|
+
continue;
|
|
2474
|
+
}
|
|
2475
|
+
if (top.quote === '"') {
|
|
2476
|
+
if (ch === escapeChar && i + 1 < command.length) {
|
|
2477
|
+
if (shell === "bash" && command[i + 1] === "\n") {
|
|
2478
|
+
i += 1;
|
|
2479
|
+
continue;
|
|
2480
|
+
}
|
|
2481
|
+
top.current += command[i + 1];
|
|
2482
|
+
i += 1;
|
|
2483
|
+
continue;
|
|
2484
|
+
}
|
|
2485
|
+
if (ch === '"') {
|
|
2486
|
+
top.quote = void 0;
|
|
2487
|
+
continue;
|
|
2488
|
+
}
|
|
2489
|
+
if (shell === "bash" && ch === "$" && command[i + 1] === "(") {
|
|
2490
|
+
stack.push({ words: [], current: "", kind: "paren", caseDepth: 0 });
|
|
2491
|
+
i += 1;
|
|
2492
|
+
continue;
|
|
2493
|
+
}
|
|
2494
|
+
if (shell === "bash" && ch === "`") {
|
|
2495
|
+
i = readBacktickBody(i);
|
|
2496
|
+
continue;
|
|
2497
|
+
}
|
|
2498
|
+
top.current += ch;
|
|
2499
|
+
continue;
|
|
2500
|
+
}
|
|
2501
|
+
if (ch === escapeChar && i + 1 < command.length) {
|
|
2502
|
+
if (shell === "bash" && command[i + 1] === "\n") {
|
|
2503
|
+
i += 1;
|
|
2504
|
+
continue;
|
|
2505
|
+
}
|
|
2506
|
+
top.current += command[i + 1];
|
|
2507
|
+
i += 1;
|
|
2508
|
+
continue;
|
|
2509
|
+
}
|
|
2510
|
+
if (ch === "'" || ch === '"') {
|
|
2511
|
+
top.quote = ch;
|
|
2512
|
+
continue;
|
|
2513
|
+
}
|
|
2514
|
+
if (shell === "bash" && ch === "`") {
|
|
2515
|
+
flushWord();
|
|
2516
|
+
i = readBacktickBody(i);
|
|
2517
|
+
continue;
|
|
2518
|
+
}
|
|
2519
|
+
if (shell !== "cmd" && ((ch === "$" || ch === "<" || ch === ">") && command[i + 1] === "(")) {
|
|
2520
|
+
flushWord();
|
|
2521
|
+
stack.push({ words: [], current: "", kind: "paren", caseDepth: 0 });
|
|
2522
|
+
i += 1;
|
|
2523
|
+
continue;
|
|
2524
|
+
}
|
|
2525
|
+
if (shell !== "cmd" && ch === "(") {
|
|
2526
|
+
flushWord();
|
|
2527
|
+
stack.push({ words: [], current: "", kind: "paren", caseDepth: 0 });
|
|
2528
|
+
continue;
|
|
2529
|
+
}
|
|
2530
|
+
if (shell !== "cmd" && ch === ")") {
|
|
2531
|
+
flushSegment();
|
|
2532
|
+
if (top.caseDepth === 0 && top.kind === "paren") {
|
|
2533
|
+
stack.pop();
|
|
2534
|
+
if (stack.length > 0 && stack[stack.length - 1].caseDepth > 0) {
|
|
2535
|
+
flushSegment();
|
|
2536
|
+
}
|
|
2537
|
+
}
|
|
2538
|
+
continue;
|
|
2539
|
+
}
|
|
2540
|
+
if (ch === ";" || ch === "\n" || ch === "|") {
|
|
2541
|
+
if (ch === "|" && command[i + 1] === "|") i += 1;
|
|
2542
|
+
flushSegment();
|
|
2543
|
+
continue;
|
|
2544
|
+
}
|
|
2545
|
+
if (ch === "&") {
|
|
2546
|
+
const prev = top.current[top.current.length - 1];
|
|
2547
|
+
if (prev === ">" || prev === "<" || command[i + 1] === ">") {
|
|
2548
|
+
top.current += ch;
|
|
2549
|
+
continue;
|
|
2550
|
+
}
|
|
2551
|
+
if (command[i + 1] === "&") i += 1;
|
|
2552
|
+
flushSegment();
|
|
2553
|
+
continue;
|
|
2554
|
+
}
|
|
2555
|
+
if (/\s/.test(ch)) {
|
|
2556
|
+
flushWord();
|
|
2557
|
+
continue;
|
|
2558
|
+
}
|
|
2559
|
+
top.current += ch;
|
|
2560
|
+
}
|
|
2561
|
+
while (stack.length > 0) {
|
|
2562
|
+
flushSegment();
|
|
2563
|
+
stack.pop();
|
|
2564
|
+
}
|
|
2565
|
+
return { segments, backtickBodies };
|
|
2566
|
+
}
|
|
2567
|
+
function splitEnvSplitString(payload) {
|
|
2568
|
+
const normalized = payload.replace(/\\_/g, " ");
|
|
2569
|
+
const { segments } = tokenizeCommandSegments(normalized, "bash");
|
|
2570
|
+
return segments.flat();
|
|
2571
|
+
}
|
|
2572
|
+
function segmentHasFilesystemWrite(words, shell, depth = 0) {
|
|
2573
|
+
let index = 0;
|
|
2574
|
+
while (index < words.length) {
|
|
2575
|
+
const word = words[index];
|
|
2576
|
+
const lower = word.toLowerCase();
|
|
2577
|
+
if (SHELL_CONTROL_PREFIXES.has(lower) || shell === "powershell" && (word === "." || word === "&")) {
|
|
2578
|
+
index += 1;
|
|
2579
|
+
continue;
|
|
2580
|
+
}
|
|
2581
|
+
if (STANDALONE_REDIR_RE.test(word)) {
|
|
2582
|
+
index += 2;
|
|
2583
|
+
continue;
|
|
2584
|
+
}
|
|
2585
|
+
if (INLINE_REDIR_RE.test(word) || /^[A-Za-z_][A-Za-z0-9_]*=/.test(word)) {
|
|
2586
|
+
index += 1;
|
|
2587
|
+
continue;
|
|
2588
|
+
}
|
|
2589
|
+
const program = executableName(word);
|
|
2590
|
+
if (FILESYSTEM_WRITE_COMMANDS.has(program)) return true;
|
|
2591
|
+
if (program === "eval") {
|
|
2592
|
+
if (depth >= 2) return true;
|
|
2593
|
+
return hasFilesystemWriteCommand(words.slice(index + 1).join(" "), shell, depth + 1);
|
|
2594
|
+
}
|
|
2595
|
+
if (WRITE_WRAPPER_NAMES.has(program)) {
|
|
2596
|
+
const valueFlags = WRITE_WRAPPER_VALUE_FLAGS[program];
|
|
2597
|
+
index += 1;
|
|
2598
|
+
while (index < words.length) {
|
|
2599
|
+
const arg = words[index];
|
|
2600
|
+
if (arg === "--") {
|
|
2601
|
+
index += 1;
|
|
2602
|
+
break;
|
|
2603
|
+
}
|
|
2604
|
+
if (program === "env") {
|
|
2605
|
+
if (arg === "-S" || arg === "--split-string" || arg.startsWith("-S") || arg.startsWith("--split-string=")) {
|
|
2606
|
+
const separate = arg === "-S" || arg === "--split-string";
|
|
2607
|
+
const splitPayload = separate ? words[index + 1] : arg.startsWith("-S") ? arg.slice(2) : arg.slice("--split-string=".length);
|
|
2608
|
+
if (!splitPayload || depth >= 2) return true;
|
|
2609
|
+
const nextIndex = separate ? index + 2 : index + 1;
|
|
2610
|
+
const expanded = [...splitEnvSplitString(splitPayload), ...words.slice(nextIndex)];
|
|
2611
|
+
return segmentHasFilesystemWrite(expanded, shell, depth + 1) || hasFilesystemWriteCommand(splitPayload.replace(/\\_/g, " "), shell, depth + 1);
|
|
2612
|
+
}
|
|
2613
|
+
if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(arg)) {
|
|
2614
|
+
index += 1;
|
|
2615
|
+
continue;
|
|
2616
|
+
}
|
|
2617
|
+
}
|
|
2618
|
+
if (valueFlags?.has(arg)) {
|
|
2619
|
+
index += 2;
|
|
2620
|
+
continue;
|
|
2621
|
+
}
|
|
2622
|
+
if (arg.startsWith("-")) {
|
|
2623
|
+
index += 1;
|
|
2624
|
+
continue;
|
|
2625
|
+
}
|
|
2626
|
+
break;
|
|
2627
|
+
}
|
|
2628
|
+
if (program === "timeout") index += 1;
|
|
2629
|
+
continue;
|
|
2630
|
+
}
|
|
2631
|
+
return false;
|
|
2632
|
+
}
|
|
2633
|
+
return false;
|
|
2634
|
+
}
|
|
2635
|
+
function hasFilesystemWriteCommand(command, shell, depth = 0) {
|
|
2636
|
+
const { segments, backtickBodies } = tokenizeCommandSegments(command, shell);
|
|
2637
|
+
for (const words of segments) {
|
|
2638
|
+
if (segmentHasFilesystemWrite(words, shell, depth)) return true;
|
|
2639
|
+
}
|
|
2640
|
+
for (const body of backtickBodies) {
|
|
2641
|
+
if (depth >= 4) return true;
|
|
2642
|
+
if (hasFilesystemWriteCommand(body, shell, depth + 1)) return true;
|
|
2643
|
+
}
|
|
2644
|
+
return false;
|
|
2645
|
+
}
|
|
2646
|
+
|
|
2647
|
+
// ../../packages/shared-headless-capabilities/src/bash-policy-segments.ts
|
|
2648
|
+
var CMD_WRITE_HEADS = /* @__PURE__ */ new Set(["copy", "xcopy", "robocopy", "move", "mkdir", "md", "ren", "rename"]);
|
|
2649
|
+
var CMD_IF_COMPARE_OPS = /* @__PURE__ */ new Set(["equ", "neq", "lss", "leq", "gtr", "geq"]);
|
|
2650
|
+
var NON_SQL_TEXT_HEADS = /* @__PURE__ */ new Set(["rg", "grep", "git", "echo", "printf", "cat", "head", "tail", "wc", "sort", "uniq", "cut", "tr"]);
|
|
2651
|
+
var GIT_GLOBAL_VALUE_FLAGS = /* @__PURE__ */ new Set(["-C", "-c", "--git-dir", "--work-tree", "--namespace", "--super-prefix", "--config-env"]);
|
|
2652
|
+
var GIT_MSG_VALUE_FLAGS = /* @__PURE__ */ new Set(["-m", "--message", "-F", "--file", "--author"]);
|
|
2653
|
+
var GIT_DESTRUCTIVE_SUBCOMMANDS = /* @__PURE__ */ new Set(["reset", "push", "clean", "checkout", "restore", "stash", "branch", "commit", "merge"]);
|
|
2654
|
+
var DESTRUCTIVE_CONTROL_PREFIXES = /* @__PURE__ */ new Set(["!", "if", "then", "elif", "else", "do", "while", "until", "coproc", "{"]);
|
|
2655
|
+
var DESTRUCTIVE_WRAPPER_NAMES = /* @__PURE__ */ new Set(["env", "command", "exec", "nohup", "setsid", "time", "nice", "timeout", "xargs", "builtin"]);
|
|
2656
|
+
var DESTRUCTIVE_WRAPPER_VALUE_FLAGS = {
|
|
2657
|
+
env: /* @__PURE__ */ new Set(["-u", "--unset", "-C", "--chdir"]),
|
|
2658
|
+
exec: /* @__PURE__ */ new Set(["-a"]),
|
|
2659
|
+
time: /* @__PURE__ */ new Set(["-o", "-f", "--output", "--format"]),
|
|
2660
|
+
nice: /* @__PURE__ */ new Set(["-n", "--adjustment"]),
|
|
2661
|
+
timeout: /* @__PURE__ */ new Set(["-k", "--kill-after", "-s", "--signal"]),
|
|
2662
|
+
xargs: /* @__PURE__ */ new Set(["-a", "-d", "-E", "-I", "-L", "-n", "-P", "-s", "--arg-file", "--delimiter", "--eof", "--replace", "--max-lines", "--max-args", "--max-procs", "--max-chars"])
|
|
2663
|
+
};
|
|
2664
|
+
var STANDALONE_REDIR_RE2 = /^(?:\d+)?(?:>>?|<<?|<>|>&|<&|&>>?)$/;
|
|
2665
|
+
var INLINE_REDIR_RE2 = /^(?:\d+)?(?:>>?|<<?|<>|>&|<&|&>>?).+$/;
|
|
2666
|
+
var GIT_EXEC_CONFIG_KEY_RE = /^(?:core\.(?:pager|editor|sshcommand|fsmonitor|hookspath|askpass)|credential\.|diff\.external|filter\.|mergetool\.|difftool\.|sequence\.editor|protocol\.(?:ext\.)?allow)/i;
|
|
2667
|
+
var GIT_RISKY_ENV_RE = /^(?:GIT_ALLOW_PROTOCOL|GIT_SSH_COMMAND|GIT_SSH|GIT_EDITOR|GIT_SEQUENCE_EDITOR|GIT_PAGER|GIT_ASKPASS|GIT_EXTERNAL_DIFF|GIT_CONFIG_PARAMETERS|GIT_CONFIG_COUNT)=/i;
|
|
2668
|
+
var GIT_EXEC_CONFIG_WARNING = "may execute arbitrary command via git config";
|
|
2669
|
+
function splitShellSegments(command, shell = "bash") {
|
|
2670
|
+
const segments = [];
|
|
2671
|
+
let current = "";
|
|
2672
|
+
let quote;
|
|
2673
|
+
for (let index = 0; index < command.length; index += 1) {
|
|
2674
|
+
const char = command[index];
|
|
2675
|
+
if (quote) {
|
|
2676
|
+
if (shell !== "cmd" && quote === '"' && char === (shell === "powershell" ? "`" : "\\") && index + 1 < command.length) {
|
|
2677
|
+
current += char;
|
|
2678
|
+
current += command[index + 1];
|
|
2679
|
+
index += 1;
|
|
2680
|
+
continue;
|
|
2681
|
+
}
|
|
2682
|
+
current += char;
|
|
2683
|
+
if (char === quote) quote = void 0;
|
|
2684
|
+
continue;
|
|
2685
|
+
}
|
|
2686
|
+
if (char === '"' || shell !== "cmd" && char === "'") {
|
|
2687
|
+
quote = char;
|
|
2688
|
+
current += char;
|
|
2689
|
+
continue;
|
|
2690
|
+
}
|
|
2691
|
+
if (shell === "cmd" && char === "^" && index + 1 < command.length) {
|
|
2692
|
+
current += char;
|
|
2693
|
+
current += command[index + 1];
|
|
2694
|
+
index += 1;
|
|
2695
|
+
continue;
|
|
2696
|
+
}
|
|
2697
|
+
if (shell !== "cmd" && char === (shell === "powershell" ? "`" : "\\") && index + 1 < command.length) {
|
|
2698
|
+
current += char;
|
|
2699
|
+
current += command[index + 1];
|
|
2700
|
+
index += 1;
|
|
2701
|
+
continue;
|
|
2702
|
+
}
|
|
2703
|
+
if (char === "&") {
|
|
2704
|
+
const prev = current[current.length - 1];
|
|
2705
|
+
if (prev === ">" || prev === "<" || command[index + 1] === ">") {
|
|
2706
|
+
current += char;
|
|
2707
|
+
continue;
|
|
2708
|
+
}
|
|
2709
|
+
if (command[index + 1] === "&") index += 1;
|
|
2710
|
+
segments.push(current);
|
|
2711
|
+
current = "";
|
|
2712
|
+
continue;
|
|
2713
|
+
}
|
|
2714
|
+
if (char === "|" || char === "\n" || char === "(" || char === ")" || shell !== "cmd" && char === ";" || shell === "powershell" && (char === "{" || char === "}")) {
|
|
2715
|
+
if (char === "|" && command[index + 1] === "|") index += 1;
|
|
2716
|
+
segments.push(current);
|
|
2717
|
+
current = "";
|
|
2718
|
+
continue;
|
|
2719
|
+
}
|
|
2720
|
+
current += char;
|
|
2721
|
+
}
|
|
2722
|
+
segments.push(current);
|
|
2723
|
+
return segments.map((part) => part.trim()).filter(Boolean);
|
|
2724
|
+
}
|
|
2725
|
+
function splitShellWords(segment, shell = "bash") {
|
|
2726
|
+
const words = [];
|
|
2727
|
+
let current = "";
|
|
2728
|
+
let hasWord = false;
|
|
2729
|
+
let quote;
|
|
2730
|
+
for (let index = 0; index < segment.length; index += 1) {
|
|
2731
|
+
const char = segment[index];
|
|
2732
|
+
if (quote) {
|
|
2733
|
+
if (char === quote) {
|
|
2734
|
+
quote = void 0;
|
|
2735
|
+
} else if (shell !== "cmd" && quote === '"' && char === (shell === "powershell" ? "`" : "\\") && index + 1 < segment.length) {
|
|
2736
|
+
current += segment[index + 1];
|
|
2737
|
+
index += 1;
|
|
2738
|
+
} else {
|
|
2739
|
+
current += char;
|
|
2740
|
+
}
|
|
2741
|
+
continue;
|
|
2742
|
+
}
|
|
2743
|
+
if (char === '"' || shell !== "cmd" && char === "'") {
|
|
2744
|
+
quote = char;
|
|
2745
|
+
hasWord = true;
|
|
2746
|
+
continue;
|
|
2747
|
+
}
|
|
2748
|
+
if (shell === "cmd" && char === "^") {
|
|
2749
|
+
if (index + 1 >= segment.length) return void 0;
|
|
2750
|
+
current += segment[index + 1];
|
|
2751
|
+
hasWord = true;
|
|
2752
|
+
index += 1;
|
|
2753
|
+
continue;
|
|
2754
|
+
}
|
|
2755
|
+
if (shell !== "cmd" && char === (shell === "powershell" ? "`" : "\\")) {
|
|
2756
|
+
if (index + 1 >= segment.length) return void 0;
|
|
2757
|
+
current += segment[index + 1];
|
|
2758
|
+
hasWord = true;
|
|
2759
|
+
index += 1;
|
|
2760
|
+
continue;
|
|
2761
|
+
}
|
|
2762
|
+
if (/\s/.test(char)) {
|
|
2763
|
+
if (hasWord) {
|
|
2764
|
+
words.push(current);
|
|
2765
|
+
current = "";
|
|
2766
|
+
hasWord = false;
|
|
2767
|
+
}
|
|
2768
|
+
continue;
|
|
2769
|
+
}
|
|
2770
|
+
current += char;
|
|
2771
|
+
hasWord = true;
|
|
2772
|
+
}
|
|
2773
|
+
if (quote) return void 0;
|
|
2774
|
+
if (hasWord) words.push(current);
|
|
2775
|
+
return words;
|
|
2776
|
+
}
|
|
2777
|
+
function stripLeadingPowerShellPrefixes(words) {
|
|
2778
|
+
let i = 0;
|
|
2779
|
+
while (i < words.length) {
|
|
2780
|
+
const w = words[i];
|
|
2781
|
+
if (w === "." || w === "&" || w === "2>&1") {
|
|
2782
|
+
i += 1;
|
|
2783
|
+
continue;
|
|
2784
|
+
}
|
|
2785
|
+
break;
|
|
2786
|
+
}
|
|
2787
|
+
return words.slice(i);
|
|
2788
|
+
}
|
|
2789
|
+
function isSimplePowerShellCommand(command, segments) {
|
|
2790
|
+
if (command.includes("$(") || command.includes("`") || command.includes("<#")) return false;
|
|
2791
|
+
let quote;
|
|
2792
|
+
for (let i = 0; i < command.length; i += 1) {
|
|
2793
|
+
const ch = command[i];
|
|
2794
|
+
if (quote) {
|
|
2795
|
+
if (ch === quote) quote = void 0;
|
|
2796
|
+
continue;
|
|
2797
|
+
}
|
|
2798
|
+
if (ch === "'" || ch === '"') {
|
|
2799
|
+
quote = ch;
|
|
2800
|
+
continue;
|
|
2801
|
+
}
|
|
2802
|
+
if (ch === "#" || ch === "@" || ch === "[" || ch === "]" || ch === "<") return false;
|
|
2803
|
+
if (ch === ">") {
|
|
2804
|
+
const isStderrMerge = i > 0 && command[i - 1] === "2" && (i < 2 || /[\s;|&({]/.test(command[i - 2])) && command.slice(i, i + 3) === ">&1" && (i + 3 >= command.length || /[\s;|&)}]/.test(command[i + 3]));
|
|
2805
|
+
if (!isStderrMerge) return false;
|
|
2806
|
+
continue;
|
|
2807
|
+
}
|
|
2808
|
+
if (ch === "$") {
|
|
2809
|
+
if (command[i + 1] === "?" && (i + 2 >= command.length || !/[A-Za-z0-9_]/.test(command[i + 2]))) {
|
|
2810
|
+
i += 1;
|
|
2811
|
+
continue;
|
|
2812
|
+
}
|
|
2813
|
+
if (/^\$LASTEXITCODE(?![A-Za-z0-9_])/i.test(command.slice(i))) {
|
|
2814
|
+
i += "$LASTEXITCODE".length - 1;
|
|
2815
|
+
continue;
|
|
2816
|
+
}
|
|
2817
|
+
return false;
|
|
2818
|
+
}
|
|
2819
|
+
if (ch === "=") {
|
|
2820
|
+
if (i === 0 || !/[A-Za-z0-9_.-]/.test(command[i - 1])) return false;
|
|
2821
|
+
}
|
|
2822
|
+
}
|
|
2823
|
+
if (quote) return false;
|
|
2824
|
+
return segments.every((segment) => {
|
|
2825
|
+
const rawWords = splitShellWords(segment, "powershell");
|
|
2826
|
+
if (!rawWords?.length) return false;
|
|
2827
|
+
const words = stripLeadingPowerShellPrefixes(rawWords);
|
|
2828
|
+
if (!words.length) return false;
|
|
2829
|
+
if (words.length === 1 && /^(?:if|else)$/i.test(words[0])) return true;
|
|
2830
|
+
if (words.length === 1 && words[0] === "$?") return true;
|
|
2831
|
+
if (words.length === 3 && /^\$LASTEXITCODE$/i.test(words[0]) && /^-(?:eq|ne|gt|ge|lt|le)$/i.test(words[1]) && /^-?\d+$/.test(words[2])) {
|
|
2832
|
+
return true;
|
|
2833
|
+
}
|
|
2834
|
+
if (/^exit$/i.test(words[0]) && (words.length === 1 || words.length === 2 && /^(?:-?\d+|\$LASTEXITCODE)$/i.test(words[1]))) {
|
|
2835
|
+
return true;
|
|
2836
|
+
}
|
|
2837
|
+
const head = words[0];
|
|
2838
|
+
return !!head && /^[A-Za-z0-9_.\\/][A-Za-z0-9_.\\/:-]*$/.test(head) && !/^(?:return|throw|exit|if|elseif|else|switch|foreach|for|while|do|until|try|catch|finally|function|filter|class|begin|process|end|trap|data|using|workflow|configuration|break|continue|inlinescript|parallel|sequence)$/i.test(head);
|
|
2839
|
+
});
|
|
2840
|
+
}
|
|
2841
|
+
function decodeCmdCaretsInWord(word) {
|
|
2842
|
+
let out = "";
|
|
2843
|
+
for (let i = 0; i < word.length; i += 1) {
|
|
2844
|
+
if (word[i] === "^" && i + 1 < word.length) {
|
|
2845
|
+
out += word[i + 1];
|
|
2846
|
+
i += 1;
|
|
2847
|
+
continue;
|
|
2848
|
+
}
|
|
2849
|
+
out += word[i];
|
|
2850
|
+
}
|
|
2851
|
+
return out;
|
|
2852
|
+
}
|
|
2853
|
+
function unwrapCmdControlWords(rawWords) {
|
|
2854
|
+
let words = rawWords;
|
|
2855
|
+
let i = 0;
|
|
2856
|
+
while (i < words.length) {
|
|
2857
|
+
const stripped = words[i].startsWith("@") ? words[i].slice(1) : words[i];
|
|
2858
|
+
if (!stripped) {
|
|
2859
|
+
i += 1;
|
|
2860
|
+
continue;
|
|
2861
|
+
}
|
|
2862
|
+
const head = executableName(stripped);
|
|
2863
|
+
if (head === "call") {
|
|
2864
|
+
words = words.slice(i + 1).map(decodeCmdCaretsInWord);
|
|
2865
|
+
i = 0;
|
|
2866
|
+
continue;
|
|
2867
|
+
}
|
|
2868
|
+
if (head === "do" || head === "else") {
|
|
2869
|
+
i += 1;
|
|
2870
|
+
continue;
|
|
2871
|
+
}
|
|
2872
|
+
if (head === "if") {
|
|
2873
|
+
let j = i + 1;
|
|
2874
|
+
if (words[j]?.toLowerCase() === "/i") j += 1;
|
|
2875
|
+
if (words[j]?.toLowerCase() === "not") j += 1;
|
|
2876
|
+
const cond = words[j]?.toLowerCase();
|
|
2877
|
+
if (cond === "exist" || cond === "defined" || cond === "errorlevel") {
|
|
2878
|
+
i = j + 2;
|
|
2879
|
+
continue;
|
|
2880
|
+
}
|
|
2881
|
+
if (j + 2 < words.length && (words[j + 1] === "==" || CMD_IF_COMPARE_OPS.has(words[j + 1].toLowerCase()))) {
|
|
2882
|
+
i = j + 3;
|
|
2883
|
+
continue;
|
|
2884
|
+
}
|
|
2885
|
+
if (j < words.length && words[j].includes("==")) {
|
|
2886
|
+
i = j + 1;
|
|
2887
|
+
continue;
|
|
2888
|
+
}
|
|
2889
|
+
return { words: words.slice(i), opaque: true };
|
|
2890
|
+
}
|
|
2891
|
+
if (head === "for") {
|
|
2892
|
+
const doIdx = words.findIndex((w, idx) => idx > i && w.toLowerCase() === "do");
|
|
2893
|
+
if (doIdx >= 0 && doIdx + 1 < words.length) {
|
|
2894
|
+
i = doIdx + 1;
|
|
2895
|
+
continue;
|
|
2896
|
+
}
|
|
2897
|
+
return { words: words.slice(i), opaque: true };
|
|
2898
|
+
}
|
|
2899
|
+
if (head === "start") {
|
|
2900
|
+
return { words: words.slice(i), opaque: true };
|
|
2901
|
+
}
|
|
2902
|
+
return { words: [stripped, ...words.slice(i + 1)], opaque: false };
|
|
2903
|
+
}
|
|
2904
|
+
return { words: [], opaque: false };
|
|
2905
|
+
}
|
|
2906
|
+
function unwrapSegmentCommandWords(words, shell) {
|
|
2907
|
+
if (shell === "cmd") return unwrapCmdControlWords(words);
|
|
2908
|
+
let index = 0;
|
|
2909
|
+
while (index < words.length) {
|
|
2910
|
+
const word = words[index];
|
|
2911
|
+
const lower = word.toLowerCase();
|
|
2912
|
+
if (DESTRUCTIVE_CONTROL_PREFIXES.has(lower) || shell === "powershell" && (word === "." || word === "&")) {
|
|
2913
|
+
index += 1;
|
|
2914
|
+
continue;
|
|
2915
|
+
}
|
|
2916
|
+
if (STANDALONE_REDIR_RE2.test(word)) {
|
|
2917
|
+
index += 2;
|
|
2918
|
+
continue;
|
|
2919
|
+
}
|
|
2920
|
+
if (INLINE_REDIR_RE2.test(word) || /^[A-Za-z_][A-Za-z0-9_]*=/.test(word)) {
|
|
2921
|
+
index += 1;
|
|
2922
|
+
continue;
|
|
2923
|
+
}
|
|
2924
|
+
const program = executableName(word);
|
|
2925
|
+
if (program === "eval") return { words: words.slice(index), opaque: true };
|
|
2926
|
+
if (DESTRUCTIVE_WRAPPER_NAMES.has(program)) {
|
|
2927
|
+
const valueFlags = DESTRUCTIVE_WRAPPER_VALUE_FLAGS[program];
|
|
2928
|
+
index += 1;
|
|
2929
|
+
while (index < words.length) {
|
|
2930
|
+
const arg = words[index];
|
|
2931
|
+
if (arg === "--") {
|
|
2932
|
+
index += 1;
|
|
2933
|
+
break;
|
|
2934
|
+
}
|
|
2935
|
+
if (program === "env") {
|
|
2936
|
+
if (arg === "-S" || arg === "--split-string" || arg.startsWith("-S") || arg.startsWith("--split-string=")) {
|
|
2937
|
+
return { words: words.slice(index), opaque: true };
|
|
2938
|
+
}
|
|
2939
|
+
if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(arg)) {
|
|
2940
|
+
index += 1;
|
|
2941
|
+
continue;
|
|
2942
|
+
}
|
|
2943
|
+
}
|
|
2944
|
+
if (valueFlags?.has(arg)) {
|
|
2945
|
+
index += 2;
|
|
2946
|
+
continue;
|
|
2947
|
+
}
|
|
2948
|
+
if (arg.startsWith("-")) {
|
|
2949
|
+
index += 1;
|
|
2950
|
+
continue;
|
|
2951
|
+
}
|
|
2952
|
+
break;
|
|
2953
|
+
}
|
|
2954
|
+
if (program === "timeout") index += 1;
|
|
2955
|
+
continue;
|
|
2956
|
+
}
|
|
2957
|
+
return { words: words.slice(index), opaque: false };
|
|
2958
|
+
}
|
|
2959
|
+
return { words: [], opaque: false };
|
|
2960
|
+
}
|
|
2961
|
+
function isRiskyGitConfigEntry(entry) {
|
|
2962
|
+
if (!entry) return true;
|
|
2963
|
+
const eq = entry.indexOf("=");
|
|
2964
|
+
const key = eq >= 0 ? entry.slice(0, eq) : entry;
|
|
2965
|
+
const val = eq >= 0 ? entry.slice(eq + 1) : "";
|
|
2966
|
+
if (GIT_EXEC_CONFIG_KEY_RE.test(key)) return true;
|
|
2967
|
+
if (val.startsWith("!") || /\s/.test(val)) return true;
|
|
2968
|
+
if (/^alias\./i.test(key) && (!val || GIT_DESTRUCTIVE_SUBCOMMANDS.has(val.toLowerCase()))) return true;
|
|
2969
|
+
return false;
|
|
2970
|
+
}
|
|
2971
|
+
function stripRedirectionTokens(words) {
|
|
2972
|
+
const out = [];
|
|
2973
|
+
for (let i = 0; i < words.length; i += 1) {
|
|
2974
|
+
const w = words[i];
|
|
2975
|
+
if (STANDALONE_REDIR_RE2.test(w)) {
|
|
2976
|
+
i += 1;
|
|
2977
|
+
continue;
|
|
2978
|
+
}
|
|
2979
|
+
if (INLINE_REDIR_RE2.test(w)) continue;
|
|
2980
|
+
out.push(w);
|
|
2981
|
+
}
|
|
2982
|
+
return out;
|
|
2983
|
+
}
|
|
2984
|
+
function normalizeGitCommandForDestructiveCheck(rawCmdWords) {
|
|
2985
|
+
const cmdWords = stripRedirectionTokens(rawCmdWords);
|
|
2986
|
+
let i = 1;
|
|
2987
|
+
while (i < cmdWords.length) {
|
|
2988
|
+
const w = cmdWords[i];
|
|
2989
|
+
if (w === "--config-env" || w.startsWith("--config-env=")) return { riskyConfig: true };
|
|
2990
|
+
if (w === "-c") {
|
|
2991
|
+
const val = cmdWords[i + 1] ?? "";
|
|
2992
|
+
if (isRiskyGitConfigEntry(val)) return { riskyConfig: true };
|
|
2993
|
+
i += 2;
|
|
2994
|
+
continue;
|
|
2995
|
+
}
|
|
2996
|
+
if (w.startsWith("-c") && w.length > 2) {
|
|
2997
|
+
if (isRiskyGitConfigEntry(w.slice(2))) return { riskyConfig: true };
|
|
2998
|
+
i += 1;
|
|
2999
|
+
continue;
|
|
3000
|
+
}
|
|
3001
|
+
if (GIT_GLOBAL_VALUE_FLAGS.has(w)) {
|
|
3002
|
+
i += 2;
|
|
3003
|
+
continue;
|
|
3004
|
+
}
|
|
3005
|
+
if (w.startsWith("-")) {
|
|
3006
|
+
i += 1;
|
|
3007
|
+
continue;
|
|
3008
|
+
}
|
|
3009
|
+
break;
|
|
3010
|
+
}
|
|
3011
|
+
const sub = cmdWords[i]?.toLowerCase();
|
|
3012
|
+
if (!sub) return { riskyConfig: false };
|
|
3013
|
+
const out = ["git", sub];
|
|
3014
|
+
for (let j = i + 1; j < cmdWords.length; j += 1) {
|
|
3015
|
+
const arg = cmdWords[j];
|
|
3016
|
+
if (sub === "commit" || sub === "merge") {
|
|
3017
|
+
if (GIT_MSG_VALUE_FLAGS.has(arg) || /^-[a-zA-Z]*[mF]$/.test(arg)) {
|
|
3018
|
+
j += 1;
|
|
3019
|
+
continue;
|
|
3020
|
+
}
|
|
3021
|
+
if (/^(?:--(?:message|file|author)=|-m.+|-F.+)/.test(arg)) continue;
|
|
3022
|
+
}
|
|
3023
|
+
if (/^ext::/i.test(arg)) return { riskyConfig: true };
|
|
3024
|
+
out.push(arg);
|
|
3025
|
+
}
|
|
3026
|
+
if (!GIT_DESTRUCTIVE_SUBCOMMANDS.has(sub)) return { riskyConfig: false };
|
|
3027
|
+
return { normalized: out.join(" "), riskyConfig: false };
|
|
3028
|
+
}
|
|
3029
|
+
function hasDynamicOrBraceSyntax(command, shell) {
|
|
3030
|
+
if (command.includes("$(") || command.includes("`") || command.includes("${") || /(?:^|[^<])<<(?!<)/.test(command)) {
|
|
3031
|
+
return true;
|
|
3032
|
+
}
|
|
3033
|
+
if (shell === "powershell") return false;
|
|
3034
|
+
let quote;
|
|
3035
|
+
for (let i = 0; i < command.length; i += 1) {
|
|
3036
|
+
const ch = command[i];
|
|
3037
|
+
if (quote) {
|
|
3038
|
+
if (shell !== "cmd" && quote === '"' && ch === "\\" && i + 1 < command.length) {
|
|
3039
|
+
i += 1;
|
|
3040
|
+
continue;
|
|
3041
|
+
}
|
|
3042
|
+
if (ch === quote) quote = void 0;
|
|
3043
|
+
continue;
|
|
3044
|
+
}
|
|
3045
|
+
if (ch === '"' || shell !== "cmd" && ch === "'") {
|
|
3046
|
+
quote = ch;
|
|
3047
|
+
continue;
|
|
3048
|
+
}
|
|
3049
|
+
if (shell === "cmd" && ch === "^" && i + 1 < command.length) {
|
|
3050
|
+
i += 1;
|
|
3051
|
+
continue;
|
|
3052
|
+
}
|
|
3053
|
+
if (shell !== "cmd" && ch === "\\" && i + 1 < command.length) {
|
|
3054
|
+
i += 1;
|
|
3055
|
+
continue;
|
|
3056
|
+
}
|
|
3057
|
+
if (ch === "{" && i + 1 < command.length && !/\s/.test(command[i + 1])) return true;
|
|
3058
|
+
}
|
|
3059
|
+
return Boolean(quote);
|
|
3060
|
+
}
|
|
3061
|
+
function matchDestructivePatterns(inspectedCommand, shell, patterns, gitConfigClass) {
|
|
3062
|
+
const hits = [];
|
|
3063
|
+
const scanTextAgainstPatterns = (text) => {
|
|
3064
|
+
for (const entry of patterns) {
|
|
3065
|
+
if (entry.pattern.test(text)) {
|
|
3066
|
+
hits.push({ warning: entry.warning, commandClass: entry.commandClass });
|
|
3067
|
+
}
|
|
3068
|
+
}
|
|
3069
|
+
};
|
|
3070
|
+
if (hasDynamicOrBraceSyntax(inspectedCommand, shell)) {
|
|
3071
|
+
scanTextAgainstPatterns(inspectedCommand);
|
|
3072
|
+
return hits;
|
|
3073
|
+
}
|
|
3074
|
+
const segments = splitShellSegments(inspectedCommand, shell);
|
|
3075
|
+
const tokenized = [];
|
|
3076
|
+
for (const segment of segments) {
|
|
3077
|
+
const words = splitShellWords(segment, shell);
|
|
3078
|
+
if (!words) {
|
|
3079
|
+
scanTextAgainstPatterns(segment);
|
|
3080
|
+
continue;
|
|
3081
|
+
}
|
|
3082
|
+
const unwrapped = unwrapSegmentCommandWords(words, shell);
|
|
3083
|
+
if (unwrapped.opaque) {
|
|
3084
|
+
scanTextAgainstPatterns(segment);
|
|
3085
|
+
scanTextAgainstPatterns(unwrapped.words.join(" "));
|
|
3086
|
+
continue;
|
|
3087
|
+
}
|
|
3088
|
+
const cmdWords = unwrapped.words;
|
|
3089
|
+
const rawNormalized = words.join(" ");
|
|
3090
|
+
const head = cmdWords[0] ? executableName(cmdWords[0]) : "";
|
|
3091
|
+
if (head === "git") {
|
|
3092
|
+
const hasRiskyGitEnv = words.some((w) => GIT_RISKY_ENV_RE.test(w));
|
|
3093
|
+
const gitCheck = normalizeGitCommandForDestructiveCheck(cmdWords);
|
|
3094
|
+
if (hasRiskyGitEnv || gitCheck.riskyConfig) {
|
|
3095
|
+
hits.push({ warning: GIT_EXEC_CONFIG_WARNING, commandClass: gitConfigClass });
|
|
3096
|
+
scanTextAgainstPatterns(segment);
|
|
3097
|
+
continue;
|
|
3098
|
+
}
|
|
3099
|
+
tokenized.push({ segment, rawNormalized, head, normalizedLine: gitCheck.normalized });
|
|
3100
|
+
} else {
|
|
3101
|
+
tokenized.push({
|
|
3102
|
+
segment,
|
|
3103
|
+
rawNormalized,
|
|
3104
|
+
head,
|
|
3105
|
+
normalizedLine: head ? stripRedirectionTokens([head, ...cmdWords.slice(1)]).join(" ") : void 0
|
|
3106
|
+
});
|
|
3107
|
+
}
|
|
3108
|
+
}
|
|
3109
|
+
const allHeadsNonSql = tokenized.every((t) => !t.head || NON_SQL_TEXT_HEADS.has(t.head));
|
|
3110
|
+
for (const { segment, rawNormalized, head, normalizedLine } of tokenized) {
|
|
3111
|
+
for (const entry of patterns) {
|
|
3112
|
+
if (entry.heads) {
|
|
3113
|
+
if (!head || !entry.heads.has(head) || !normalizedLine) continue;
|
|
3114
|
+
if (entry.pattern.test(normalizedLine)) {
|
|
3115
|
+
hits.push({ warning: entry.warning, commandClass: entry.commandClass });
|
|
3116
|
+
}
|
|
3117
|
+
} else if (!allHeadsNonSql && (entry.pattern.test(segment) || entry.pattern.test(rawNormalized))) {
|
|
3118
|
+
hits.push({ warning: entry.warning, commandClass: entry.commandClass });
|
|
3119
|
+
}
|
|
3120
|
+
}
|
|
3121
|
+
}
|
|
3122
|
+
return hits;
|
|
3123
|
+
}
|
|
3124
|
+
function hasCmdFilesystemWriteHead(command) {
|
|
3125
|
+
for (const segment of splitShellSegments(command, "cmd")) {
|
|
3126
|
+
const words = splitShellWords(segment, "cmd");
|
|
3127
|
+
if (!words) {
|
|
3128
|
+
if (/(?<![\w.-])\b(copy|xcopy|robocopy|move|mkdir|md|ren|rename)\b/i.test(segment)) return true;
|
|
3129
|
+
continue;
|
|
3130
|
+
}
|
|
3131
|
+
const unwrapped = unwrapCmdControlWords(words);
|
|
3132
|
+
if (unwrapped.opaque) {
|
|
3133
|
+
if (unwrapped.words.some((w) => CMD_WRITE_HEADS.has(executableName(w.startsWith("@") ? w.slice(1) : w)))) {
|
|
3134
|
+
return true;
|
|
3135
|
+
}
|
|
3136
|
+
continue;
|
|
3137
|
+
}
|
|
3138
|
+
const head = unwrapped.words[0] ? executableName(unwrapped.words[0]) : "";
|
|
3139
|
+
if (CMD_WRITE_HEADS.has(head)) return true;
|
|
3140
|
+
}
|
|
3141
|
+
return false;
|
|
3142
|
+
}
|
|
3143
|
+
|
|
2391
3144
|
// ../../packages/shared-headless-capabilities/src/bash-policy.ts
|
|
3145
|
+
var GIT_HEAD = /* @__PURE__ */ new Set(["git"]);
|
|
3146
|
+
var PRIV_HEADS = /* @__PURE__ */ new Set(["sudo", "su", "chmod", "chown"]);
|
|
3147
|
+
var KUBECTL_HEAD = /* @__PURE__ */ new Set(["kubectl"]);
|
|
3148
|
+
var TERRAFORM_HEAD = /* @__PURE__ */ new Set(["terraform"]);
|
|
3149
|
+
var CMD_DEL_HEADS = /* @__PURE__ */ new Set(["del", "erase"]);
|
|
3150
|
+
var CMD_RMDIR_HEADS = /* @__PURE__ */ new Set(["rmdir", "rd"]);
|
|
3151
|
+
var CMD_FORMAT_HEAD = /* @__PURE__ */ new Set(["format"]);
|
|
3152
|
+
var CMD_ACL_HEADS = /* @__PURE__ */ new Set(["takeown", "icacls"]);
|
|
2392
3153
|
var DESTRUCTIVE_PATTERNS = [
|
|
2393
|
-
{ pattern: /\bgit\s+reset\
|
|
2394
|
-
{ pattern: /\bgit\s+push\b[^;&|\n]*[ \t](--force|--force-with-lease|-f)\b/, warning: "may overwrite remote history", commandClass: "git_destructive" },
|
|
2395
|
-
{ pattern: /\bgit\s+clean\b(?![^;&|\n]*(?:-[a-zA-Z]*n|--dry-run))[^;&|\n]
|
|
2396
|
-
{ pattern: /\bgit\s+(checkout|restore)\s+(--\s+)?\.[ \t]*($|[;&|\n])/, warning: "may discard all working tree changes", commandClass: "git_destructive" },
|
|
2397
|
-
{ pattern: /\bgit\s+stash[ \t]+(drop|clear)\b/, warning: "may permanently remove stashed changes", commandClass: "git_destructive" },
|
|
2398
|
-
{ pattern: /\bgit\s+branch\s+(-D[ \t]|--delete\s+--force|--force\s+--delete)\b/, warning: "may force-delete a branch", commandClass: "git_destructive" },
|
|
2399
|
-
{ pattern: /\bgit\s+(commit|push|merge)\b[^;&|\n]*--no-verify\b/, warning: "may skip safety hooks", commandClass: "git_destructive" },
|
|
3154
|
+
{ pattern: /\bgit\s+reset\b[^;&|\n]*[ \t]--hard\b/, warning: "may discard uncommitted changes", commandClass: "git_destructive", heads: GIT_HEAD },
|
|
3155
|
+
{ pattern: /\bgit\s+push\b[^;&|\n]*[ \t](--force|--force-with-lease|-f)\b/, warning: "may overwrite remote history", commandClass: "git_destructive", heads: GIT_HEAD },
|
|
3156
|
+
{ pattern: /\bgit\s+clean\b(?![^;&|\n]*[ \t](?:-[a-zA-Z]*n|--dry-run)(?:[ \t]|$))[^;&|\n]*[ \t]-[a-zA-Z]*f/, warning: "may permanently delete untracked files", commandClass: "git_destructive", heads: GIT_HEAD },
|
|
3157
|
+
{ pattern: /\bgit\s+(checkout|restore)\s+(--\s+)?\.[ \t]*($|[;&|\n])/, warning: "may discard all working tree changes", commandClass: "git_destructive", heads: GIT_HEAD },
|
|
3158
|
+
{ pattern: /\bgit\s+stash[ \t]+(drop|clear)\b/, warning: "may permanently remove stashed changes", commandClass: "git_destructive", heads: GIT_HEAD },
|
|
3159
|
+
{ pattern: /\bgit\s+branch\s+(-D[ \t]|--delete\s+--force|--force\s+--delete)\b/, warning: "may force-delete a branch", commandClass: "git_destructive", heads: GIT_HEAD },
|
|
3160
|
+
{ pattern: /\bgit\s+(commit|push|merge)\b[^;&|\n]*--no-verify\b/, warning: "may skip safety hooks", commandClass: "git_destructive", heads: GIT_HEAD },
|
|
2400
3161
|
// `rm -rf` is NOT in this regex list: raw-text matching both missed split
|
|
2401
3162
|
// flags (`rm -r -f`) and false-positived on quoted literals mentioning
|
|
2402
3163
|
// `rm -rf` (grep patterns, echo, docs). It is detected semantically by
|
|
2403
3164
|
// containsRecursiveForceRemove in classifyShellCommand instead.
|
|
2404
|
-
{ pattern: /\bsudo\b|\bsu\s+-|\bchmod\s+777\b|\bchown\s+-R\b/, warning: "may require elevated privileges or weaken permissions", commandClass: "privilege_escalation" },
|
|
3165
|
+
{ pattern: /\bsudo\b|\bsu\s+-|\bchmod\s+777\b|\bchown\s+-R\b/, warning: "may require elevated privileges or weaken permissions", commandClass: "privilege_escalation", heads: PRIV_HEADS },
|
|
2405
3166
|
{ pattern: /\b(DROP|TRUNCATE)\s+(TABLE|DATABASE|SCHEMA)\b/i, warning: "may drop or truncate database objects", commandClass: "infra_destructive" },
|
|
2406
3167
|
{ pattern: /\bDELETE\s+FROM\s+\w+[ \t]*(;|"|'|\n|$)/i, warning: "may delete all rows from a database table", commandClass: "infra_destructive" },
|
|
2407
|
-
{ pattern: /\bkubectl\s+delete\b/, warning: "may delete Kubernetes resources", commandClass: "infra_destructive" },
|
|
2408
|
-
{ pattern: /\bterraform\s+destroy\b/, warning: "may destroy Terraform infrastructure", commandClass: "infra_destructive" }
|
|
3168
|
+
{ pattern: /\bkubectl\s+delete\b/, warning: "may delete Kubernetes resources", commandClass: "infra_destructive", heads: KUBECTL_HEAD },
|
|
3169
|
+
{ pattern: /\bterraform\s+destroy\b/, warning: "may destroy Terraform infrastructure", commandClass: "infra_destructive", heads: TERRAFORM_HEAD }
|
|
2409
3170
|
];
|
|
2410
3171
|
var RM_FORCE_RECURSIVE_WARNING = "may recursively force-remove files";
|
|
2411
3172
|
var RM_RECURSIVE_WARNING = "may recursively remove files";
|
|
@@ -2443,28 +3204,70 @@ var POWERSHELL_DESTRUCTIVE_PATTERNS = [
|
|
|
2443
3204
|
// parameter matcher below accepts any unambiguous-style abbreviation,
|
|
2444
3205
|
// not just the full spellings.
|
|
2445
3206
|
{ pattern: buildPowerShellRemoveItemPattern(), warning: "may recursively or forcefully remove files", commandClass: "filesystem_write" },
|
|
2446
|
-
{ pattern:
|
|
3207
|
+
{ pattern: /(?<![\w.-])\b(?:Remove-Item|ri|rm|rmdir|del|erase|rd)\b[^;\n|]*(?:HKLM:|HKCU:|Registry::)/i, warning: "may remove registry entries", commandClass: "infra_destructive" },
|
|
2447
3208
|
// These write aliases are not all destructive in the recursive-delete sense,
|
|
2448
3209
|
// but they can overwrite or create arbitrary files. Keep them behind the
|
|
2449
3210
|
// same explicit shell confirmation boundary as other PowerShell writes.
|
|
2450
|
-
|
|
3211
|
+
// Note: `add` is NOT a PowerShell alias (`Add-Content` is `ac`) and `set`
|
|
3212
|
+
// is `Set-Variable` (`Set-Content` is `sc`); `(?<![\w.-])` prevents
|
|
3213
|
+
// matching file extensions like `README.md` or hyphenated flags.
|
|
3214
|
+
{ pattern: /(?<![\w.-])\b(?:Set-Content|sc|Add-Content|ac|Out-File|Clear-Content|clc|Tee-Object|tee|Export-Csv|Export-Clixml|New-Item|ni|md|mkdir|Copy-Item|cpi|cp|copy|Move-Item|mi|mv|move|Rename-Item|rni|ren)\b/i, warning: "may write or overwrite files", commandClass: "filesystem_write" },
|
|
2451
3215
|
{ pattern: /\b(Set-ExecutionPolicy|Start-Process\b[^;\n|]*-Verb\s+RunAs)\b/i, warning: "may change execution policy or request elevation", commandClass: "privilege_escalation" },
|
|
2452
3216
|
{ pattern: /\b(Stop-Computer|Restart-Computer)\b/i, warning: "may stop or restart the machine", commandClass: "infra_destructive" },
|
|
2453
3217
|
{ pattern: /(^|[;|&\n])\s*(?:Invoke-Expression|iex)\b/i, warning: "evaluates a dynamically constructed string as code", commandClass: "privilege_escalation" },
|
|
2454
|
-
{ pattern: /\bgit\s+reset\
|
|
3218
|
+
{ pattern: /\bgit\s+reset\b[^;&|\n]*[ \t]--hard\b/i, warning: "may discard uncommitted changes", commandClass: "git_destructive" },
|
|
2455
3219
|
{ pattern: /\bgit\s+push\b[^;&|\n]*[ \t](--force|--force-with-lease|-f)\b/i, warning: "may overwrite remote history", commandClass: "git_destructive" }
|
|
2456
3220
|
];
|
|
2457
3221
|
var CMD_DESTRUCTIVE_PATTERNS = [
|
|
2458
|
-
{ pattern: /\b(del|erase)\b[^&|\n]*(\/s|\/q)/i, warning: "may recursively or quietly delete files", commandClass: "filesystem_write" },
|
|
3222
|
+
{ pattern: /\b(del|erase)\b[^&|\n]*(\/s|\/q)/i, warning: "may recursively or quietly delete files", commandClass: "filesystem_write", heads: CMD_DEL_HEADS },
|
|
2459
3223
|
// `rd` is cmd's documented synonym for rmdir and accepts the same /s /q
|
|
2460
3224
|
// switches; classifying only `rmdir` let `rd /s /q C:\\anywhere` run as an
|
|
2461
3225
|
// undetected recursive deletion.
|
|
2462
|
-
{ pattern: /\b(?:rmdir|rd)\b[^&|\n]*(\/s|\/q)/i, warning: "may recursively remove directories", commandClass: "filesystem_write" },
|
|
2463
|
-
{ pattern: /\bformat\s+[a-z]:/i, warning: "may format a drive", commandClass: "infra_destructive" },
|
|
2464
|
-
{ pattern: /\b(takeown|icacls)\b[^&|\n]*(\/grant|\/reset|\/remove)/i, warning: "may alter ownership or permissions", commandClass: "privilege_escalation" },
|
|
2465
|
-
{ pattern: /\bgit\s+reset\
|
|
2466
|
-
{ pattern: /\bgit\s+push\b[^&|\n]*[ \t](--force|--force-with-lease|-f)\b/i, warning: "may overwrite remote history", commandClass: "git_destructive" }
|
|
3226
|
+
{ pattern: /\b(?:rmdir|rd)\b[^&|\n]*(\/s|\/q)/i, warning: "may recursively remove directories", commandClass: "filesystem_write", heads: CMD_RMDIR_HEADS },
|
|
3227
|
+
{ pattern: /\bformat\s+[a-z]:/i, warning: "may format a drive", commandClass: "infra_destructive", heads: CMD_FORMAT_HEAD },
|
|
3228
|
+
{ pattern: /\b(takeown|icacls)\b[^&|\n]*(\/grant|\/reset|\/remove)/i, warning: "may alter ownership or permissions", commandClass: "privilege_escalation", heads: CMD_ACL_HEADS },
|
|
3229
|
+
{ pattern: /\bgit\s+reset\b[^&|\n]*[ \t]--hard\b/i, warning: "may discard uncommitted changes", commandClass: "git_destructive", heads: GIT_HEAD },
|
|
3230
|
+
{ pattern: /\bgit\s+push\b[^&|\n]*[ \t](--force|--force-with-lease|-f)\b/i, warning: "may overwrite remote history", commandClass: "git_destructive", heads: GIT_HEAD }
|
|
2467
3231
|
];
|
|
3232
|
+
function hasPowerShellFileRedirection(command) {
|
|
3233
|
+
let quote;
|
|
3234
|
+
for (let i = 0; i < command.length; i += 1) {
|
|
3235
|
+
const ch = command[i];
|
|
3236
|
+
if (quote) {
|
|
3237
|
+
if (quote === '"' && ch === "`" && i + 1 < command.length) {
|
|
3238
|
+
i += 1;
|
|
3239
|
+
continue;
|
|
3240
|
+
}
|
|
3241
|
+
if (quote === "'" && ch === "'" && command[i + 1] === "'") {
|
|
3242
|
+
i += 1;
|
|
3243
|
+
continue;
|
|
3244
|
+
}
|
|
3245
|
+
if (quote === '"' && ch === '"' && command[i + 1] === '"') {
|
|
3246
|
+
i += 1;
|
|
3247
|
+
continue;
|
|
3248
|
+
}
|
|
3249
|
+
if (ch === quote) quote = void 0;
|
|
3250
|
+
continue;
|
|
3251
|
+
}
|
|
3252
|
+
if (ch === "'" || ch === '"') {
|
|
3253
|
+
quote = ch;
|
|
3254
|
+
continue;
|
|
3255
|
+
}
|
|
3256
|
+
if (ch === "`" && i + 1 < command.length) {
|
|
3257
|
+
i += 1;
|
|
3258
|
+
continue;
|
|
3259
|
+
}
|
|
3260
|
+
if (ch === ">") {
|
|
3261
|
+
const next = command[i + 1];
|
|
3262
|
+
if (next === "&" && /[0-6]/.test(command[i + 2] ?? "")) {
|
|
3263
|
+
i += 2;
|
|
3264
|
+
continue;
|
|
3265
|
+
}
|
|
3266
|
+
return true;
|
|
3267
|
+
}
|
|
3268
|
+
}
|
|
3269
|
+
return false;
|
|
3270
|
+
}
|
|
2468
3271
|
function splitCommand(command) {
|
|
2469
3272
|
return command.split(/&&|\|\||[;\n]/).map((part) => part.trim()).filter(Boolean);
|
|
2470
3273
|
}
|
|
@@ -2476,7 +3279,7 @@ function buildPowerShellRemoveItemPattern() {
|
|
|
2476
3279
|
const recurseg = "-(?:r(?:e(?:c(?:u(?:r(?:s(?:e?)?)?)?)?)?)?)";
|
|
2477
3280
|
const force = "-(?:f(?:o(?:r(?:c(?:e?)?)?)?)?)";
|
|
2478
3281
|
return new RegExp(
|
|
2479
|
-
|
|
3282
|
+
`(?<![\\w.-])\\b${removeVerb}\\b[^;\\n|]*(?:${recurseg}|${force})\\b`,
|
|
2480
3283
|
"i"
|
|
2481
3284
|
);
|
|
2482
3285
|
}
|
|
@@ -2487,87 +3290,6 @@ function lastPipelineCommand(command) {
|
|
|
2487
3290
|
function classifyBashCommand(command, options) {
|
|
2488
3291
|
return classifyShellCommand(command, "bash", options);
|
|
2489
3292
|
}
|
|
2490
|
-
function splitShellSegments(command, shell = "bash") {
|
|
2491
|
-
const segments = [];
|
|
2492
|
-
let current = "";
|
|
2493
|
-
let quote;
|
|
2494
|
-
for (let index = 0; index < command.length; index += 1) {
|
|
2495
|
-
const char = command[index];
|
|
2496
|
-
if (quote) {
|
|
2497
|
-
if (quote === '"' && char === (shell === "powershell" ? "`" : "\\") && index + 1 < command.length) {
|
|
2498
|
-
current += char;
|
|
2499
|
-
current += command[index + 1];
|
|
2500
|
-
index += 1;
|
|
2501
|
-
continue;
|
|
2502
|
-
}
|
|
2503
|
-
current += char;
|
|
2504
|
-
if (char === quote) quote = void 0;
|
|
2505
|
-
continue;
|
|
2506
|
-
}
|
|
2507
|
-
if (char === "'" || char === '"') {
|
|
2508
|
-
quote = char;
|
|
2509
|
-
current += char;
|
|
2510
|
-
continue;
|
|
2511
|
-
}
|
|
2512
|
-
if (char === (shell === "powershell" ? "`" : "\\") && index + 1 < command.length) {
|
|
2513
|
-
current += char;
|
|
2514
|
-
current += command[index + 1];
|
|
2515
|
-
index += 1;
|
|
2516
|
-
continue;
|
|
2517
|
-
}
|
|
2518
|
-
if (char === "&" || char === "|" || char === ";" || char === "\n" || shell === "powershell" && /[(){}]/.test(char)) {
|
|
2519
|
-
if ((char === "&" || char === "|") && command[index + 1] === char) {
|
|
2520
|
-
index += 1;
|
|
2521
|
-
}
|
|
2522
|
-
segments.push(current);
|
|
2523
|
-
current = "";
|
|
2524
|
-
continue;
|
|
2525
|
-
}
|
|
2526
|
-
current += char;
|
|
2527
|
-
}
|
|
2528
|
-
segments.push(current);
|
|
2529
|
-
return segments.map((part) => part.trim()).filter(Boolean);
|
|
2530
|
-
}
|
|
2531
|
-
function splitShellWords(segment, shell = "bash") {
|
|
2532
|
-
const words = [];
|
|
2533
|
-
let current = "";
|
|
2534
|
-
let quote;
|
|
2535
|
-
for (let index = 0; index < segment.length; index += 1) {
|
|
2536
|
-
const char = segment[index];
|
|
2537
|
-
if (quote) {
|
|
2538
|
-
if (char === quote) {
|
|
2539
|
-
quote = void 0;
|
|
2540
|
-
} else if (quote === '"' && char === (shell === "powershell" ? "`" : "\\") && index + 1 < segment.length) {
|
|
2541
|
-
current += segment[index + 1];
|
|
2542
|
-
index += 1;
|
|
2543
|
-
} else {
|
|
2544
|
-
current += char;
|
|
2545
|
-
}
|
|
2546
|
-
continue;
|
|
2547
|
-
}
|
|
2548
|
-
if (char === "'" || char === '"') {
|
|
2549
|
-
quote = char;
|
|
2550
|
-
continue;
|
|
2551
|
-
}
|
|
2552
|
-
if (char === (shell === "powershell" ? "`" : "\\")) {
|
|
2553
|
-
if (index + 1 >= segment.length) return void 0;
|
|
2554
|
-
current += segment[index + 1];
|
|
2555
|
-
index += 1;
|
|
2556
|
-
continue;
|
|
2557
|
-
}
|
|
2558
|
-
if (/\s/.test(char)) {
|
|
2559
|
-
if (current) {
|
|
2560
|
-
words.push(current);
|
|
2561
|
-
current = "";
|
|
2562
|
-
}
|
|
2563
|
-
continue;
|
|
2564
|
-
}
|
|
2565
|
-
current += char;
|
|
2566
|
-
}
|
|
2567
|
-
if (quote) return void 0;
|
|
2568
|
-
if (current) words.push(current);
|
|
2569
|
-
return words;
|
|
2570
|
-
}
|
|
2571
3293
|
function normalizePosixPath(input) {
|
|
2572
3294
|
const absolute = input.startsWith("/");
|
|
2573
3295
|
const parts = [];
|
|
@@ -2669,10 +3391,171 @@ function resolveStaticCwdTarget(target, cwd, root) {
|
|
|
2669
3391
|
return resolved;
|
|
2670
3392
|
}
|
|
2671
3393
|
var DYNAMIC_EXECUTION_WARNING = "may execute nested shell or interpreter code";
|
|
2672
|
-
function
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
3394
|
+
function stripBashComments(command) {
|
|
3395
|
+
let hasUnquotedHeredoc = false;
|
|
3396
|
+
{
|
|
3397
|
+
let q;
|
|
3398
|
+
for (let i = 0; i < command.length; i += 1) {
|
|
3399
|
+
const c = command[i];
|
|
3400
|
+
if (q) {
|
|
3401
|
+
if (q === '"' && c === "\\" && i + 1 < command.length) {
|
|
3402
|
+
i += 1;
|
|
3403
|
+
continue;
|
|
3404
|
+
}
|
|
3405
|
+
if (c === q) q = void 0;
|
|
3406
|
+
continue;
|
|
3407
|
+
}
|
|
3408
|
+
if (c === "'" || c === '"') {
|
|
3409
|
+
q = c;
|
|
3410
|
+
continue;
|
|
3411
|
+
}
|
|
3412
|
+
if (c === "\\" && i + 1 < command.length) {
|
|
3413
|
+
i += 1;
|
|
3414
|
+
continue;
|
|
3415
|
+
}
|
|
3416
|
+
if (c === "<" && command[i + 1] === "<") {
|
|
3417
|
+
if (command[i + 2] === "<") {
|
|
3418
|
+
i += 2;
|
|
3419
|
+
continue;
|
|
3420
|
+
}
|
|
3421
|
+
hasUnquotedHeredoc = true;
|
|
3422
|
+
break;
|
|
3423
|
+
}
|
|
3424
|
+
}
|
|
3425
|
+
}
|
|
3426
|
+
if (hasUnquotedHeredoc) return command;
|
|
3427
|
+
let result = "";
|
|
3428
|
+
let quote;
|
|
3429
|
+
let inWord = false;
|
|
3430
|
+
let inBacktick = false;
|
|
3431
|
+
let paramExpansionDepth = 0;
|
|
3432
|
+
let cmdSubDepth = 0;
|
|
3433
|
+
for (let index = 0; index < command.length; index += 1) {
|
|
3434
|
+
const char = command[index];
|
|
3435
|
+
if (quote) {
|
|
3436
|
+
result += char;
|
|
3437
|
+
if (quote === '"' && char === "\\" && index + 1 < command.length) {
|
|
3438
|
+
result += command[index + 1];
|
|
3439
|
+
index += 1;
|
|
3440
|
+
continue;
|
|
3441
|
+
}
|
|
3442
|
+
if (char === quote) quote = void 0;
|
|
3443
|
+
continue;
|
|
3444
|
+
}
|
|
3445
|
+
if (char === "'" || char === '"') {
|
|
3446
|
+
quote = char;
|
|
3447
|
+
inWord = true;
|
|
3448
|
+
result += char;
|
|
3449
|
+
continue;
|
|
3450
|
+
}
|
|
3451
|
+
if (char === "\\" && index + 1 < command.length) {
|
|
3452
|
+
const next = command[index + 1];
|
|
3453
|
+
if (next !== "\n") inWord = true;
|
|
3454
|
+
result += char;
|
|
3455
|
+
result += next;
|
|
3456
|
+
index += 1;
|
|
3457
|
+
continue;
|
|
3458
|
+
}
|
|
3459
|
+
if (char === "`") {
|
|
3460
|
+
inBacktick = !inBacktick;
|
|
3461
|
+
inWord = true;
|
|
3462
|
+
result += char;
|
|
3463
|
+
continue;
|
|
3464
|
+
}
|
|
3465
|
+
if (char === "$" && command[index + 1] === "{") {
|
|
3466
|
+
paramExpansionDepth += 1;
|
|
3467
|
+
inWord = true;
|
|
3468
|
+
result += "${";
|
|
3469
|
+
index += 1;
|
|
3470
|
+
continue;
|
|
3471
|
+
}
|
|
3472
|
+
if (char === "$" && command[index + 1] === "(") {
|
|
3473
|
+
cmdSubDepth += 1;
|
|
3474
|
+
inWord = true;
|
|
3475
|
+
result += "$(";
|
|
3476
|
+
index += 1;
|
|
3477
|
+
continue;
|
|
3478
|
+
}
|
|
3479
|
+
if (char === "{" && paramExpansionDepth > 0) {
|
|
3480
|
+
paramExpansionDepth += 1;
|
|
3481
|
+
inWord = true;
|
|
3482
|
+
result += char;
|
|
3483
|
+
continue;
|
|
3484
|
+
}
|
|
3485
|
+
if (char === "(" && cmdSubDepth > 0) {
|
|
3486
|
+
cmdSubDepth += 1;
|
|
3487
|
+
inWord = true;
|
|
3488
|
+
result += char;
|
|
3489
|
+
continue;
|
|
3490
|
+
}
|
|
3491
|
+
if (char === "}" && paramExpansionDepth > 0) {
|
|
3492
|
+
paramExpansionDepth -= 1;
|
|
3493
|
+
inWord = true;
|
|
3494
|
+
result += char;
|
|
3495
|
+
continue;
|
|
3496
|
+
}
|
|
3497
|
+
if (char === ")" && cmdSubDepth > 0) {
|
|
3498
|
+
cmdSubDepth -= 1;
|
|
3499
|
+
inWord = true;
|
|
3500
|
+
result += char;
|
|
3501
|
+
continue;
|
|
3502
|
+
}
|
|
3503
|
+
if (char === "#" && !inWord && !inBacktick && paramExpansionDepth === 0 && cmdSubDepth === 0) {
|
|
3504
|
+
while (index < command.length && command[index] !== "\n") {
|
|
3505
|
+
index += 1;
|
|
3506
|
+
}
|
|
3507
|
+
if (index < command.length && command[index] === "\n") {
|
|
3508
|
+
result += "\n";
|
|
3509
|
+
inWord = false;
|
|
3510
|
+
}
|
|
3511
|
+
continue;
|
|
3512
|
+
}
|
|
3513
|
+
if (/[\s;&|()]/.test(char)) {
|
|
3514
|
+
inWord = false;
|
|
3515
|
+
} else {
|
|
3516
|
+
inWord = true;
|
|
3517
|
+
}
|
|
3518
|
+
result += char;
|
|
3519
|
+
}
|
|
3520
|
+
if (quote || inBacktick || paramExpansionDepth > 0 || cmdSubDepth > 0) {
|
|
3521
|
+
return command;
|
|
3522
|
+
}
|
|
3523
|
+
return result;
|
|
3524
|
+
}
|
|
3525
|
+
var SAFE_PYTHON_TEST_MODULES = /* @__PURE__ */ new Set(["pytest", "unittest"]);
|
|
3526
|
+
var PYTHON_VALUE_OPTIONS = /* @__PURE__ */ new Set(["-W", "-X", "--check-hash-based-pycs"]);
|
|
3527
|
+
var PYTHON_SAFE_BOOL_FLAGS_RE = /^-[BEIOPqSsuv]+$/;
|
|
3528
|
+
function isPythonExecutable(program) {
|
|
3529
|
+
return /^python(?:\d+(?:\.\d+)?)?$/.test(program);
|
|
3530
|
+
}
|
|
3531
|
+
function hasDynamicPythonInvocation(args) {
|
|
3532
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
3533
|
+
const word = args[index];
|
|
3534
|
+
if (word === "--") return false;
|
|
3535
|
+
if (/^-[ce]/i.test(word)) return true;
|
|
3536
|
+
if (/^-m$/i.test(word)) {
|
|
3537
|
+
const mod = args[index + 1]?.toLowerCase();
|
|
3538
|
+
return !(mod && SAFE_PYTHON_TEST_MODULES.has(mod));
|
|
3539
|
+
}
|
|
3540
|
+
if (/^-m.+$/i.test(word)) {
|
|
3541
|
+
const mod = word.slice(2).toLowerCase();
|
|
3542
|
+
return !SAFE_PYTHON_TEST_MODULES.has(mod);
|
|
3543
|
+
}
|
|
3544
|
+
if (PYTHON_VALUE_OPTIONS.has(word)) {
|
|
3545
|
+
if (index + 1 >= args.length) return true;
|
|
3546
|
+
index += 1;
|
|
3547
|
+
continue;
|
|
3548
|
+
}
|
|
3549
|
+
if (/^(?:-W|-X|--check-hash-based-pycs=)/i.test(word)) {
|
|
3550
|
+
continue;
|
|
3551
|
+
}
|
|
3552
|
+
if (PYTHON_SAFE_BOOL_FLAGS_RE.test(word)) {
|
|
3553
|
+
continue;
|
|
3554
|
+
}
|
|
3555
|
+
if (word.startsWith("-")) return true;
|
|
3556
|
+
return false;
|
|
3557
|
+
}
|
|
3558
|
+
return false;
|
|
2676
3559
|
}
|
|
2677
3560
|
function hasDynamicExecutionInvocation(words) {
|
|
2678
3561
|
if (words.length === 0) return false;
|
|
@@ -2691,7 +3574,10 @@ function hasDynamicExecutionInvocation(words) {
|
|
|
2691
3574
|
if (program === "cmd") {
|
|
2692
3575
|
return words.slice(1).some((word) => /^\/(?:c|k)$/i.test(word));
|
|
2693
3576
|
}
|
|
2694
|
-
if (
|
|
3577
|
+
if (isPythonExecutable(program)) {
|
|
3578
|
+
return hasDynamicPythonInvocation(words.slice(1));
|
|
3579
|
+
}
|
|
3580
|
+
if (["perl", "ruby"].includes(program)) {
|
|
2695
3581
|
return words.slice(1).some((word) => /^(?:-c|-e|-m)$/i.test(word));
|
|
2696
3582
|
}
|
|
2697
3583
|
if (program === "node") {
|
|
@@ -2755,13 +3641,14 @@ function isStaticallyScopedRecursiveForceRemove(command, options) {
|
|
|
2755
3641
|
return sawRm;
|
|
2756
3642
|
}
|
|
2757
3643
|
function classifyShellCommand(command, shell = "bash", options) {
|
|
3644
|
+
const inspectedCommand = shell === "bash" ? stripBashComments(command) : command;
|
|
2758
3645
|
const warnings = [];
|
|
2759
3646
|
let commandClass = "unknown_shell";
|
|
2760
3647
|
let destructive = false;
|
|
2761
3648
|
const destructivePatterns = shell === "cmd" ? [...CMD_DESTRUCTIVE_PATTERNS, ...DESTRUCTIVE_PATTERNS] : DESTRUCTIVE_PATTERNS;
|
|
2762
|
-
const removeWarnings = recursiveRemoveWarnings(
|
|
3649
|
+
const removeWarnings = recursiveRemoveWarnings(inspectedCommand);
|
|
2763
3650
|
if (removeWarnings.length > 0) {
|
|
2764
|
-
if (shell === "bash" && isStaticallyScopedRecursiveForceRemove(
|
|
3651
|
+
if (shell === "bash" && isStaticallyScopedRecursiveForceRemove(inspectedCommand, options)) {
|
|
2765
3652
|
commandClass = "filesystem_write";
|
|
2766
3653
|
} else {
|
|
2767
3654
|
warnings.push(...removeWarnings);
|
|
@@ -2769,20 +3656,20 @@ function classifyShellCommand(command, shell = "bash", options) {
|
|
|
2769
3656
|
destructive = true;
|
|
2770
3657
|
}
|
|
2771
3658
|
}
|
|
2772
|
-
const dynamicWarnings = dynamicExecutionWarnings(
|
|
3659
|
+
const dynamicWarnings = dynamicExecutionWarnings(inspectedCommand);
|
|
2773
3660
|
if (dynamicWarnings.length > 0) {
|
|
2774
3661
|
warnings.push(...dynamicWarnings);
|
|
2775
3662
|
commandClass = "unknown_shell";
|
|
2776
3663
|
destructive = true;
|
|
2777
3664
|
}
|
|
2778
3665
|
if (shell === "powershell") {
|
|
3666
|
+
if (hasPowerShellFileRedirection(command)) {
|
|
3667
|
+
warnings.push("may write or overwrite files");
|
|
3668
|
+
commandClass = "filesystem_write";
|
|
3669
|
+
destructive = true;
|
|
3670
|
+
}
|
|
2779
3671
|
const segments = splitShellSegments(command, shell);
|
|
2780
|
-
const simple =
|
|
2781
|
-
const words2 = splitShellWords(segment, shell);
|
|
2782
|
-
if (!words2?.length) return false;
|
|
2783
|
-
const head = words2[0] === "." ? words2[1] : words2[0];
|
|
2784
|
-
return !!head && /^[A-Za-z0-9_.\\/][A-Za-z0-9_.\\/:-]*$/.test(head) && !/^(?:return|throw|exit|if|elseif|else|switch|foreach|for|while|do|until|try|catch|finally|function|filter|class|begin|process|end|trap|data|using|workflow|configuration|break|continue|inlinescript|parallel|sequence)$/i.test(head);
|
|
2785
|
-
});
|
|
3672
|
+
const simple = isSimplePowerShellCommand(command, segments);
|
|
2786
3673
|
if (!simple) {
|
|
2787
3674
|
for (const entry of POWERSHELL_DESTRUCTIVE_PATTERNS) {
|
|
2788
3675
|
if (!entry.pattern.test(command)) continue;
|
|
@@ -2792,8 +3679,7 @@ function classifyShellCommand(command, shell = "bash", options) {
|
|
|
2792
3679
|
}
|
|
2793
3680
|
}
|
|
2794
3681
|
for (const segment of segments) {
|
|
2795
|
-
const words2 = splitShellWords(segment, shell) ?? segment.split(/\s+/);
|
|
2796
|
-
if (words2[0] === ".") words2.shift();
|
|
3682
|
+
const words2 = stripLeadingPowerShellPrefixes(splitShellWords(segment, shell) ?? segment.split(/\s+/));
|
|
2797
3683
|
if (!words2[0]) continue;
|
|
2798
3684
|
words2[0] = executableName(words2[0]);
|
|
2799
3685
|
for (const entry of POWERSHELL_DESTRUCTIVE_PATTERNS) {
|
|
@@ -2806,33 +3692,31 @@ function classifyShellCommand(command, shell = "bash", options) {
|
|
|
2806
3692
|
}
|
|
2807
3693
|
}
|
|
2808
3694
|
}
|
|
2809
|
-
for (const
|
|
2810
|
-
|
|
2811
|
-
|
|
2812
|
-
|
|
2813
|
-
destructive = true;
|
|
2814
|
-
}
|
|
3695
|
+
for (const hit of matchDestructivePatterns(inspectedCommand, shell, destructivePatterns, "git_destructive")) {
|
|
3696
|
+
warnings.push(hit.warning);
|
|
3697
|
+
commandClass = hit.commandClass;
|
|
3698
|
+
destructive = true;
|
|
2815
3699
|
}
|
|
2816
3700
|
if (destructive) {
|
|
2817
3701
|
return { commandClass, riskWarnings: [...new Set(warnings)], destructive, requiresPermission: true };
|
|
2818
3702
|
}
|
|
2819
|
-
const normalized =
|
|
3703
|
+
const normalized = inspectedCommand.trim().toLowerCase();
|
|
2820
3704
|
const words = splitCommand(normalized).map(firstWord);
|
|
2821
3705
|
const primary = words[0] || firstWord(normalized);
|
|
2822
3706
|
if (shell === "powershell" && /\b(invoke-webrequest|iwr|invoke-restmethod|irm)\b/.test(normalized)) {
|
|
2823
3707
|
commandClass = "network";
|
|
2824
3708
|
} else if (shell === "cmd" && /\b(curl|bitsadmin|certutil)\b/.test(normalized)) {
|
|
2825
3709
|
commandClass = "network";
|
|
2826
|
-
} else if (shell === "cmd" &&
|
|
3710
|
+
} else if (shell === "cmd" && hasCmdFilesystemWriteHead(inspectedCommand)) {
|
|
2827
3711
|
commandClass = "filesystem_write";
|
|
2828
3712
|
} else if (/\b(npm|pnpm|yarn|bun)\s+(install|add|remove|update|ci)\b/.test(normalized)) {
|
|
2829
3713
|
commandClass = "package_manager";
|
|
2830
|
-
} else if (/\b(
|
|
2831
|
-
commandClass = "test_or_build";
|
|
2832
|
-
} else if (/\b(curl|wget|http|npx|pip|gem|brew)\b/.test(normalized)) {
|
|
3714
|
+
} else if (/\b(curl|wget|http|npx|pip3?|gem|brew)\b/.test(normalized)) {
|
|
2833
3715
|
commandClass = "network";
|
|
2834
|
-
} else if (
|
|
3716
|
+
} else if (commandClass === "filesystem_write" || LEGACY_FILESYSTEM_WRITE_RE.test(normalized) || hasFilesystemWriteCommand(inspectedCommand, shell)) {
|
|
2835
3717
|
commandClass = "filesystem_write";
|
|
3718
|
+
} else if (/\b(npm|pnpm|yarn|bun|cargo|go|mvn|gradle|pytest|python -m pytest)\s+(run\s+)?(test|build|check|lint|typecheck|verify)\b/.test(normalized) || /(?:^|[;&|\n])\s*(?:[^\s;|&]*\/)?python(?:\d+(?:\.\d+)?)?\s+-m\s*(?:pytest|unittest)\b/.test(normalized) || /(?:^|[;&|\n])\s*(?:[^\s;|&]*\/)?pytest\b/.test(normalized)) {
|
|
3719
|
+
commandClass = "test_or_build";
|
|
2836
3720
|
} else if (["ls", "pwd", "cat", "head", "tail", "grep", "rg", "find", "wc", "git", "diff", "test", "["].includes(primary)) {
|
|
2837
3721
|
commandClass = "diagnostic_read";
|
|
2838
3722
|
}
|
|
@@ -3064,7 +3948,7 @@ function safelyObserve(callback, event) {
|
|
|
3064
3948
|
} catch {
|
|
3065
3949
|
}
|
|
3066
3950
|
}
|
|
3067
|
-
function createAgentInstance(deps, systemPrompt, tools, model, apiKey, thinkingLevel) {
|
|
3951
|
+
function createAgentInstance(deps, systemPrompt, tools, model, apiKey, thinkingLevel, sessionId) {
|
|
3068
3952
|
const hooks = { finalizing: false };
|
|
3069
3953
|
tools = tools.map((tool) => ({ ...tool, execute: async (...args) => {
|
|
3070
3954
|
if (hooks.softDeadlineAt !== void 0 && Date.now() >= hooks.softDeadlineAt) hooks.finalizing = true;
|
|
@@ -3105,7 +3989,8 @@ function createAgentInstance(deps, systemPrompt, tools, model, apiKey, thinkingL
|
|
|
3105
3989
|
model,
|
|
3106
3990
|
tools,
|
|
3107
3991
|
apiKey,
|
|
3108
|
-
thinkingLevel
|
|
3992
|
+
thinkingLevel,
|
|
3993
|
+
...sessionId ? { sessionId } : {}
|
|
3109
3994
|
});
|
|
3110
3995
|
agentRunHooks.set(agent2, hooks);
|
|
3111
3996
|
agentRuntimeCredentials.set(agent2, { model, apiKey });
|
|
@@ -3119,6 +4004,7 @@ function createAgentInstance(deps, systemPrompt, tools, model, apiKey, thinkingL
|
|
|
3119
4004
|
thinkingLevel
|
|
3120
4005
|
},
|
|
3121
4006
|
apiKey,
|
|
4007
|
+
...sessionId ? { sessionId } : {},
|
|
3122
4008
|
// Subagent turns are single-purpose report generations: request an
|
|
3123
4009
|
// explicit output budget (see subagentOutputBudget) instead of leaving
|
|
3124
4010
|
// it to endpoint defaults that silently truncate long reports.
|
|
@@ -3278,6 +4164,7 @@ function isSubagentModelFallbackTrigger(errorMessage, failure, stopReason) {
|
|
|
3278
4164
|
if (stopReason === "aborted" || stopReason === "length") return false;
|
|
3279
4165
|
const kind = failure?.classification?.kind;
|
|
3280
4166
|
if (kind === "user_abort" || kind === "invalid_request" || kind === "context_overflow") return false;
|
|
4167
|
+
if (failure?.remoteFailure?.quotaExhausted === true) return true;
|
|
3281
4168
|
if (errorMessage && isDefaultQuotaExhaustedError(errorMessage)) return true;
|
|
3282
4169
|
if (kind === "auth_config") return true;
|
|
3283
4170
|
if (failure?.recovery?.outcome === "exhausted" && (kind === "provider_transient" || kind === "provider_confirmation" || kind === "transient_network")) {
|
|
@@ -3372,6 +4259,7 @@ async function tryCompactSubagentOverflow(options) {
|
|
|
3372
4259
|
summary = options.compactSummaryFn ? await options.compactSummaryFn(cleaned, options.model, options.apiKey, controller.signal) : await createCompactionSummary(cleaned, {
|
|
3373
4260
|
model: options.model,
|
|
3374
4261
|
apiKey: options.apiKey,
|
|
4262
|
+
...options.sessionId ? { sessionId: options.sessionId } : {},
|
|
3375
4263
|
signal: controller.signal
|
|
3376
4264
|
});
|
|
3377
4265
|
} catch (summaryError) {
|
|
@@ -3518,7 +4406,7 @@ async function runSubagentWithProgress(deps, task, reportProgress, signal, sessi
|
|
|
3518
4406
|
throw new Error("No model configured for subagent execution.");
|
|
3519
4407
|
}
|
|
3520
4408
|
const priorMessages = task.resumeMode === "report" ? [] : session.agent ? session.agent.state.messages : session.pendingMessages ?? [];
|
|
3521
|
-
const rebuilt = createAgentInstance(deps, buildSubagentSystemPrompt(nextMode, deps.skills), built.tools, built.model, built.apiKey, plan.thinkingLevel);
|
|
4409
|
+
const rebuilt = createAgentInstance(deps, buildSubagentSystemPrompt(nextMode, deps.skills), built.tools, built.model, built.apiKey, plan.thinkingLevel, session.id);
|
|
3522
4410
|
transplantMessages(rebuilt, priorMessages);
|
|
3523
4411
|
baseline = priorMessages.length;
|
|
3524
4412
|
subagent = rebuilt;
|
|
@@ -3563,12 +4451,13 @@ async function runSubagentWithProgress(deps, task, reportProgress, signal, sessi
|
|
|
3563
4451
|
}
|
|
3564
4452
|
deps.log(`[Subagent ${task.name}] Plan: ${plan.complexity}/${plan.modelClass}${plan.modelOverride ? `, model=${plan.modelOverrideApplied ? plan.modelOverride : "override failed"}` : ""}, ${tools.length} tools`);
|
|
3565
4453
|
debugLog(deps, "plan", "SubagentPlan", plan);
|
|
3566
|
-
|
|
4454
|
+
const childSessionId = `${task.id}-session-${(++taskIdCounter).toString(36)}`;
|
|
4455
|
+
subagent = createAgentInstance(deps, buildSubagentSystemPrompt(task.mode, deps.skills), tools, model, apiKey, plan.thinkingLevel, childSessionId);
|
|
3567
4456
|
activeTools = tools;
|
|
3568
4457
|
activeModel = model;
|
|
3569
4458
|
activeApiKey = apiKey;
|
|
3570
4459
|
session = {
|
|
3571
|
-
id:
|
|
4460
|
+
id: childSessionId,
|
|
3572
4461
|
name: task.name,
|
|
3573
4462
|
mode: task.mode,
|
|
3574
4463
|
priority: task.priority,
|
|
@@ -3731,7 +4620,7 @@ ${DELEGATION_REPORT_INSTRUCTION}`;
|
|
|
3731
4620
|
if (!compactedOnce && !signal?.aborted && Date.now() < hardDeadlineAt && isSubagentOverflowFailure(subagentFailure, terminalError, terminalStopReason)) {
|
|
3732
4621
|
compactedOnce = true;
|
|
3733
4622
|
report({ message: `[${task.name}] Context overflow; compacting history...` });
|
|
3734
|
-
const compacted = await tryCompactSubagentOverflow({ agent: subagent, model: activeModel, apiKey: activeApiKey, signal, hardDeadlineAt, compactSummaryFn: deps.compactSummaryFn });
|
|
4623
|
+
const compacted = await tryCompactSubagentOverflow({ agent: subagent, model: activeModel, apiKey: activeApiKey, sessionId: session?.id, signal, hardDeadlineAt, compactSummaryFn: deps.compactSummaryFn });
|
|
3735
4624
|
if (compacted.compacted) {
|
|
3736
4625
|
baseline = compacted.newBaseline;
|
|
3737
4626
|
runMode = "continue";
|
|
@@ -3748,7 +4637,7 @@ ${DELEGATION_REPORT_INSTRUCTION}`;
|
|
|
3748
4637
|
modelFallbacksUsed += 1;
|
|
3749
4638
|
const nextThinking = resolveThinkingSelection(next.model, plan.thinkingLevel).effective;
|
|
3750
4639
|
subagent.releaseTransport?.();
|
|
3751
|
-
subagent = createAgentInstance(deps, buildSubagentSystemPrompt(task.mode, deps.skills), activeTools, next.model, next.apiKey, nextThinking);
|
|
4640
|
+
subagent = createAgentInstance(deps, buildSubagentSystemPrompt(task.mode, deps.skills), activeTools, next.model, next.apiKey, nextThinking, session?.id);
|
|
3752
4641
|
activeModel = next.model;
|
|
3753
4642
|
activeApiKey = next.apiKey;
|
|
3754
4643
|
session.agent = subagent;
|
|
@@ -11546,13 +12435,9 @@ function extractOpenAIResponsesContent(response, originalQuery) {
|
|
|
11546
12435
|
const body = isRecord(response) ? response : {};
|
|
11547
12436
|
const sources = [];
|
|
11548
12437
|
collectUrlCitations(body.output, sources);
|
|
11549
|
-
|
|
11550
|
-
|
|
11551
|
-
|
|
11552
|
-
sources: uniqueSources(sources),
|
|
11553
|
-
results: [makeTextSegment(body.output_text.trim())],
|
|
11554
|
-
actualQueries: [originalQuery]
|
|
11555
|
-
};
|
|
12438
|
+
const sharedText = extractResponsesOutputText(body);
|
|
12439
|
+
if (sharedText) {
|
|
12440
|
+
return { text: sharedText, sources: uniqueSources(sources), results: [makeTextSegment(sharedText)], actualQueries: [originalQuery] };
|
|
11556
12441
|
}
|
|
11557
12442
|
const textChunks = [];
|
|
11558
12443
|
collectOutputText(body.output, textChunks);
|
|
@@ -17248,6 +18133,35 @@ function normalizeInboxItem(raw) {
|
|
|
17248
18133
|
function dedupeKey(item) {
|
|
17249
18134
|
return `${item.kind}\0${item.text}\0${item.projectKey ?? ""}\0${item.cwd ?? ""}`;
|
|
17250
18135
|
}
|
|
18136
|
+
function compareWikiInboxOrder(a, b) {
|
|
18137
|
+
if (a.createdAt !== b.createdAt) return a.createdAt - b.createdAt;
|
|
18138
|
+
return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
|
|
18139
|
+
}
|
|
18140
|
+
function selectWikiInboxPruneIds(existing, incoming, maxItems = MAX_WIKI_INBOX_ITEMS) {
|
|
18141
|
+
const winnersByKey = /* @__PURE__ */ new Map();
|
|
18142
|
+
winnersByKey.set(dedupeKey(incoming), incoming);
|
|
18143
|
+
for (const entry of existing) {
|
|
18144
|
+
const key = dedupeKey(entry);
|
|
18145
|
+
const current = winnersByKey.get(key);
|
|
18146
|
+
if (!current || compareWikiInboxOrder(entry, current) > 0) {
|
|
18147
|
+
winnersByKey.set(key, entry);
|
|
18148
|
+
}
|
|
18149
|
+
}
|
|
18150
|
+
const toRemoveSet = /* @__PURE__ */ new Set();
|
|
18151
|
+
for (const entry of existing) {
|
|
18152
|
+
const winner = winnersByKey.get(dedupeKey(entry));
|
|
18153
|
+
if (winner && entry.id !== winner.id) toRemoveSet.add(entry.id);
|
|
18154
|
+
}
|
|
18155
|
+
const remaining = existing.filter((entry) => !toRemoveSet.has(entry.id)).sort(compareWikiInboxOrder);
|
|
18156
|
+
const cap = Math.max(0, maxItems);
|
|
18157
|
+
if (remaining.length > cap) {
|
|
18158
|
+
const overflow = remaining.length - cap;
|
|
18159
|
+
for (const old of remaining.slice(0, overflow)) {
|
|
18160
|
+
toRemoveSet.add(old.id);
|
|
18161
|
+
}
|
|
18162
|
+
}
|
|
18163
|
+
return [...toRemoveSet];
|
|
18164
|
+
}
|
|
17251
18165
|
function readWikiInbox(memoryRoot) {
|
|
17252
18166
|
const dir = wikiInboxPath(memoryRoot);
|
|
17253
18167
|
assertSafeMemoryPath(memoryRoot, dir);
|
|
@@ -17272,7 +18186,7 @@ function readWikiInbox(memoryRoot) {
|
|
|
17272
18186
|
} catch {
|
|
17273
18187
|
}
|
|
17274
18188
|
}
|
|
17275
|
-
items.sort(
|
|
18189
|
+
items.sort(compareWikiInboxOrder);
|
|
17276
18190
|
return items;
|
|
17277
18191
|
}
|
|
17278
18192
|
function countWikiInbox(memoryRoot) {
|
|
@@ -17289,20 +18203,7 @@ function appendWikiInboxItem(memoryRoot, input) {
|
|
|
17289
18203
|
writeFileAtomic(memoryRoot, target, JSON.stringify(item) + "\n");
|
|
17290
18204
|
try {
|
|
17291
18205
|
const existing = readWikiInbox(memoryRoot);
|
|
17292
|
-
const
|
|
17293
|
-
const toRemove = [];
|
|
17294
|
-
for (const entry of existing) {
|
|
17295
|
-
if (entry.id !== item.id && dedupeKey(entry) === key) {
|
|
17296
|
-
toRemove.push(entry.id);
|
|
17297
|
-
}
|
|
17298
|
-
}
|
|
17299
|
-
const remaining = existing.filter((entry) => !toRemove.includes(entry.id));
|
|
17300
|
-
if (remaining.length > MAX_WIKI_INBOX_ITEMS) {
|
|
17301
|
-
const overflow = remaining.length - MAX_WIKI_INBOX_ITEMS;
|
|
17302
|
-
for (const old of remaining.slice(0, overflow)) {
|
|
17303
|
-
if (old.id !== item.id) toRemove.push(old.id);
|
|
17304
|
-
}
|
|
17305
|
-
}
|
|
18206
|
+
const toRemove = selectWikiInboxPruneIds(existing, item, MAX_WIKI_INBOX_ITEMS);
|
|
17306
18207
|
if (toRemove.length > 0) removeWikiInboxItems(memoryRoot, toRemove);
|
|
17307
18208
|
} catch {
|
|
17308
18209
|
}
|
|
@@ -18769,6 +19670,8 @@ export {
|
|
|
18769
19670
|
readMemoryFile,
|
|
18770
19671
|
MAX_WIKI_INBOX_ITEMS,
|
|
18771
19672
|
wikiInboxPath,
|
|
19673
|
+
compareWikiInboxOrder,
|
|
19674
|
+
selectWikiInboxPruneIds,
|
|
18772
19675
|
readWikiInbox,
|
|
18773
19676
|
countWikiInbox,
|
|
18774
19677
|
appendWikiInboxItem,
|