pi-braid 0.40.5 → 0.41.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/AGENTS.md +17 -18
- package/README.md +128 -130
- package/TOOLS.md +35 -35
- package/build-info.json +4 -4
- package/dist/extensions/core/agent-environment/index.js +4 -3
- package/dist/extensions/core/prompt-builder/index.js +12 -7
- package/dist/extensions/core/prompt-builder/instance-composition.d.ts +51 -10
- package/dist/extensions/core/prompt-builder/instance-composition.js +87 -14
- package/dist/extensions/core/prompt-builder/system-info-tool.d.ts +3 -3
- package/dist/extensions/core/prompt-builder/system-info-tool.js +3 -3
- package/dist/extensions/core/prompt-builder/tool-guidelines.js +1 -1
- package/dist/extensions/core/skills-tool/auto-inject.d.ts +89 -10
- package/dist/extensions/core/skills-tool/auto-inject.js +150 -21
- package/dist/extensions/core/skills-tool/index.d.ts +70 -2
- package/dist/extensions/core/skills-tool/index.js +149 -15
- package/dist/extensions/core/skills-tool/reach.d.ts +119 -0
- package/dist/extensions/core/skills-tool/reach.js +213 -0
- package/dist/extensions/core/skills-tool/skill-triggers.d.ts +47 -10
- package/dist/extensions/core/skills-tool/skill-triggers.js +66 -23
- package/dist/extensions/enforcement/handlers/register-session-compact.js +12 -1
- package/dist/extensions/enforcement/handlers/register-session-shutdown-resumption.js +15 -34
- package/dist/extensions/enforcement/handlers/register-session-start-discoverability.d.ts +2 -1
- package/dist/extensions/enforcement/handlers/register-session-start-discoverability.js +55 -28
- package/dist/extensions/enforcement/handlers/register-turn-end-hygiene.d.ts +14 -10
- package/dist/extensions/enforcement/handlers/register-turn-end-hygiene.js +88 -82
- package/dist/extensions/enforcement/resume-state.d.ts +76 -0
- package/dist/extensions/enforcement/resume-state.js +90 -0
- package/dist/extensions/enforcement/substate/context-hygiene-state.d.ts +9 -0
- package/dist/extensions/enforcement/substate/context-hygiene-state.js +8 -0
- package/dist/extensions/enforcement/whisper.d.ts +12 -0
- package/dist/extensions/enforcement/whisper.js +16 -1
- package/dist/extensions/heartbeat/heartbeat-scheduler/index.js +14 -6
- package/dist/extensions/heartbeat/heartbeat-scheduler/policies.d.ts +7 -0
- package/dist/extensions/heartbeat/heartbeat-scheduler/policies.js +372 -0
- package/dist/extensions/memory/context-manager/handlers/tools/context-compress.d.ts +8 -2
- package/dist/extensions/memory/context-manager/handlers/tools/context-compress.js +54 -21
- package/dist/lib/config/braid-paths.d.ts +14 -0
- package/dist/lib/config/braid-paths.js +18 -0
- package/dist/lib/config/braid-settings.d.ts +21 -0
- package/dist/lib/config/braid-settings.js +16 -0
- package/dist/lib/detection/context-tiers.d.ts +47 -3
- package/dist/lib/detection/context-tiers.js +122 -13
- package/dist/lib/events/turn-boundary.d.ts +78 -0
- package/dist/lib/events/turn-boundary.js +91 -0
- package/dist/lib/orchestrator/workers.d.ts +3 -2
- package/dist/lib/orchestrator/workers.js +22 -4
- package/dist/lib/state/global-state.d.ts +1 -1
- package/docs/CONTEXT-MAP.md +1 -1
- package/docs/CONTEXT-POLICY.md +8 -0
- package/docs/FLEET.md +99 -0
- package/docs/LOCAL_DEV.md +0 -1
- package/docs/ROADMAP.md +5 -5
- package/docs/SETUP.md +19 -18
- package/package.json +5 -1
- package/scripts/fleet/cli.mjs +499 -0
- package/scripts/fleet/driver-rpc.mjs +376 -0
- package/scripts/fleet/driver-scripted.mjs +239 -0
- package/scripts/fleet/journal.mjs +994 -0
- package/scripts/fleet/procs.mjs +76 -0
- package/scripts/fleet/supervisor.mjs +1494 -0
- package/scripts/fleet/tool-guard.mjs +467 -0
- package/skills/herdr-topology/SKILL.md +6 -1
- package/skills/release-pipeline/SKILL.md +20 -4
- package/skills/research/SKILL.md +32 -0
- package/skills/subagent-orchestration/SKILL.md +2 -2
- package/src/extensions/core/agent-environment/index.ts +4 -3
- package/src/extensions/core/prompt-builder/AGENTS.md +8 -4
- package/src/extensions/core/prompt-builder/index.ts +12 -7
- package/src/extensions/core/prompt-builder/instance-composition.ts +98 -14
- package/src/extensions/core/prompt-builder/system-info-tool.ts +3 -3
- package/src/extensions/core/prompt-builder/tool-guidelines.ts +1 -1
- package/src/extensions/core/skills-tool/auto-inject.ts +196 -24
- package/src/extensions/core/skills-tool/index.ts +217 -15
- package/src/extensions/core/skills-tool/reach.ts +256 -0
- package/src/extensions/core/skills-tool/skill-triggers.ts +90 -20
- package/src/extensions/enforcement/handlers/register-session-compact.ts +12 -1
- package/src/extensions/enforcement/handlers/register-session-shutdown-resumption.ts +23 -36
- package/src/extensions/enforcement/handlers/register-session-start-discoverability.ts +59 -29
- package/src/extensions/enforcement/handlers/register-turn-end-hygiene.ts +101 -81
- package/src/extensions/enforcement/resume-state.ts +127 -0
- package/src/extensions/enforcement/substate/context-hygiene-state.ts +17 -0
- package/src/extensions/enforcement/whisper.ts +21 -1
- package/src/extensions/heartbeat/heartbeat-scheduler/index.ts +20 -5
- package/src/extensions/heartbeat/heartbeat-scheduler/policies.ts +370 -0
- package/src/extensions/memory/AGENTS.md +25 -1
- package/src/extensions/memory/context-manager/handlers/tools/context-compress.ts +80 -27
- package/src/lib/config/braid-paths.ts +20 -0
- package/src/lib/config/braid-settings.ts +46 -0
- package/src/lib/detection/context-tiers.ts +186 -13
- package/src/lib/events/AGENTS.md +1 -0
- package/src/lib/events/turn-boundary.ts +115 -0
- package/src/lib/orchestrator/workers.ts +33 -12
- package/src/lib/state/global-state.ts +1 -0
- package/src/lib/utils/AGENTS.md +1 -1
- package/src/types/extension-context.d.ts +22 -79
|
@@ -41,6 +41,7 @@ export function resetScratchpadState(s) {
|
|
|
41
41
|
export function createContextHygieneState() {
|
|
42
42
|
return {
|
|
43
43
|
contextNudgeSent: false,
|
|
44
|
+
contextNudgeRung: null,
|
|
44
45
|
lastContextCheckTurn: 0,
|
|
45
46
|
lastCompactionTurn: 0,
|
|
46
47
|
overflowRecently: 0,
|
|
@@ -56,6 +57,7 @@ export function createContextHygieneState() {
|
|
|
56
57
|
}
|
|
57
58
|
export function resetContextHygieneState(s) {
|
|
58
59
|
s.contextNudgeSent = false;
|
|
60
|
+
s.contextNudgeRung = null;
|
|
59
61
|
s.lastContextCheckTurn = 0;
|
|
60
62
|
s.lastCompactionTurn = 0;
|
|
61
63
|
s.overflowRecently = 0;
|
|
@@ -91,22 +93,28 @@ export function recordLiveUsageSample(s, tokens) {
|
|
|
91
93
|
/** Mark context nudge as sent (system sends nudge to agent). */
|
|
92
94
|
export function markNudgeSent(s, turn) {
|
|
93
95
|
s.contextNudgeSent = true;
|
|
96
|
+
s.contextNudgeRung = null;
|
|
94
97
|
s.lastContextCheckTurn = turn;
|
|
95
98
|
}
|
|
96
99
|
/** Reset context nudge (e.g., after compaction freed space). */
|
|
97
100
|
export function resetNudge(s) {
|
|
98
101
|
s.contextNudgeSent = false;
|
|
102
|
+
s.contextNudgeRung = null;
|
|
99
103
|
}
|
|
100
104
|
/** Record that a context management tool was used. */
|
|
101
105
|
export function recordContextTool(s, turn) {
|
|
102
106
|
s.lastContextCheckTurn = turn;
|
|
103
107
|
s.contextNudgeSent = false;
|
|
108
|
+
s.contextNudgeRung = null;
|
|
104
109
|
}
|
|
105
110
|
/** Record that compaction happened. Also resets scratchpad nudge timer. */
|
|
106
111
|
export function recordCompaction(s, turn) {
|
|
107
112
|
s.lastCompactionTurn = turn;
|
|
108
113
|
s.lastContextCheckTurn = turn;
|
|
109
114
|
s.contextNudgeSent = false;
|
|
115
|
+
// A compaction is a fresh start: usage dropped by construction, so the rung
|
|
116
|
+
// that armed the old latch says nothing about where the next nudge belongs.
|
|
117
|
+
s.contextNudgeRung = null;
|
|
110
118
|
s.overflowRecently = turn;
|
|
111
119
|
if (typeof s.lastScratchpadNudgeTurn === "number") {
|
|
112
120
|
s.lastScratchpadNudgeTurn = turn;
|
|
@@ -35,6 +35,17 @@ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
|
35
35
|
* options.deliverAs - Delivery mode. Defaults to `"steer"` for a
|
|
36
36
|
* non-waking whisper; left undefined when triggerTurn:true so
|
|
37
37
|
* pi-core's wake branch is not shadowed by the nextTurn queue.
|
|
38
|
+
* options.bypassFloodCap - Skip the per-turn/per-source whisper flood cap
|
|
39
|
+
* in the funnel (see `sendTrackedMessage`'s JSDoc). Opt-in
|
|
40
|
+
* per call site, default false, and reserved for a wake
|
|
41
|
+
* whose loss is a correctness bug rather than a lost nudge:
|
|
42
|
+
* the post-compaction resume, which is the only thing that
|
|
43
|
+
* hands the aborted loop back. Every enforcement whisper
|
|
44
|
+
* shares the `enforcement/whisper` bucket, so three ordinary
|
|
45
|
+
* advisories in one turn are enough to spend it - a blanket
|
|
46
|
+
* exemption for `triggerTurn` would instead turn every
|
|
47
|
+
* waking caller into a flood risk, which is why the cap is
|
|
48
|
+
* bypassed here per call rather than in the funnel.
|
|
38
49
|
*
|
|
39
50
|
* Nudges are suppressed when braid.suppressNudges=true in settings.json.
|
|
40
51
|
*/
|
|
@@ -42,5 +53,6 @@ export declare function sendEnforcementWhisper(pi: ExtensionAPI, ctx: ExtensionC
|
|
|
42
53
|
triggerTurn?: boolean;
|
|
43
54
|
deliverAs?: "steer" | "nextTurn";
|
|
44
55
|
delivery?: "live" | "park";
|
|
56
|
+
bypassFloodCap?: boolean;
|
|
45
57
|
}): void;
|
|
46
58
|
//# sourceMappingURL=whisper.d.ts.map
|
|
@@ -39,6 +39,17 @@ const log = createLogger("enforcement");
|
|
|
39
39
|
* options.deliverAs - Delivery mode. Defaults to `"steer"` for a
|
|
40
40
|
* non-waking whisper; left undefined when triggerTurn:true so
|
|
41
41
|
* pi-core's wake branch is not shadowed by the nextTurn queue.
|
|
42
|
+
* options.bypassFloodCap - Skip the per-turn/per-source whisper flood cap
|
|
43
|
+
* in the funnel (see `sendTrackedMessage`'s JSDoc). Opt-in
|
|
44
|
+
* per call site, default false, and reserved for a wake
|
|
45
|
+
* whose loss is a correctness bug rather than a lost nudge:
|
|
46
|
+
* the post-compaction resume, which is the only thing that
|
|
47
|
+
* hands the aborted loop back. Every enforcement whisper
|
|
48
|
+
* shares the `enforcement/whisper` bucket, so three ordinary
|
|
49
|
+
* advisories in one turn are enough to spend it - a blanket
|
|
50
|
+
* exemption for `triggerTurn` would instead turn every
|
|
51
|
+
* waking caller into a flood risk, which is why the cap is
|
|
52
|
+
* bypassed here per call rather than in the funnel.
|
|
42
53
|
*
|
|
43
54
|
* Nudges are suppressed when braid.suppressNudges=true in settings.json.
|
|
44
55
|
*/
|
|
@@ -99,6 +110,10 @@ export function sendEnforcementWhisper(pi, ctx, message, critical = false, optio
|
|
|
99
110
|
customType: "enforcement",
|
|
100
111
|
content: [{ type: "text", text: message }],
|
|
101
112
|
display: true,
|
|
102
|
-
}, { deliverAs, triggerTurn, delivery }, { type: "enforcement", source: "enforcement/whisper" }
|
|
113
|
+
}, { deliverAs, triggerTurn, delivery }, { type: "enforcement", source: "enforcement/whisper" },
|
|
114
|
+
// Forwarded per call, never defaulted on: the funnel's flood cap stays
|
|
115
|
+
// the gate for every enforcement whisper that does not ask for the
|
|
116
|
+
// exemption explicitly.
|
|
117
|
+
{ bypassFloodCap: options?.bypassFloodCap === true });
|
|
103
118
|
}
|
|
104
119
|
//# sourceMappingURL=whisper.js.map
|
|
@@ -75,7 +75,7 @@ function newestUserText(ctx) {
|
|
|
75
75
|
}
|
|
76
76
|
import { DEFAULT_CONFIG, getState, getMemoryState } from "./state.js";
|
|
77
77
|
import { updateMetaLearning, getAdaptiveInterval } from "./meta-learning.js";
|
|
78
|
-
import { createDefaultPolicies,
|
|
78
|
+
import { createDefaultPolicies, isVerificationToolCall, WEB_TOOLS, CODE_TOOLS, } from "./policies.js";
|
|
79
79
|
import { formatPendingCompactionsLine, maybeArmAutoCompaction, } from "../../memory/context-manager/handlers/tools/context-compress.js";
|
|
80
80
|
import { pushBounded } from "../../../lib/utils/bounded.js";
|
|
81
81
|
import { RSS_RING_CAP } from "../../../lib/telemetry/rss-telemetry.js";
|
|
@@ -222,7 +222,7 @@ import { flushWhisperBuffer } from "./whisper-batcher.js";
|
|
|
222
222
|
import { buildSelfStateLine } from "./whisper-batcher.js";
|
|
223
223
|
import { getSelfStateSample } from "../../../lib/telemetry/context-heartbeat.js";
|
|
224
224
|
// Tick Engine
|
|
225
|
-
function tick(pi, state, config, ctx) {
|
|
225
|
+
function tick(pi, state, config, ctx, turnEvent) {
|
|
226
226
|
state.stats.totalTicks++;
|
|
227
227
|
// Real telemetry: collect live context/RSS/session metrics, append the
|
|
228
228
|
// JSONL heartbeat line, and feed recordTurnMetrics real values (not
|
|
@@ -296,7 +296,7 @@ function tick(pi, state, config, ctx) {
|
|
|
296
296
|
log.warn(`[heartbeat] pending-compaction advisory failed: ${err}`);
|
|
297
297
|
}
|
|
298
298
|
try {
|
|
299
|
-
maybeArmAutoCompaction(pi, ctx);
|
|
299
|
+
maybeArmAutoCompaction(pi, ctx, turnEvent);
|
|
300
300
|
}
|
|
301
301
|
catch (err) {
|
|
302
302
|
log.warn(`[heartbeat] context auto-arm check failed: ${err}`);
|
|
@@ -483,7 +483,7 @@ export default function heartbeatExtension(pi) {
|
|
|
483
483
|
behavioralDriftModule,
|
|
484
484
|
];
|
|
485
485
|
// Event: turn_end
|
|
486
|
-
pi.on(TURN_END, async (
|
|
486
|
+
pi.on(TURN_END, async (event, ctx) => {
|
|
487
487
|
const state = getState(ctx);
|
|
488
488
|
if (!state.enabled || !config.enabled)
|
|
489
489
|
return;
|
|
@@ -496,7 +496,12 @@ export default function heartbeatExtension(pi) {
|
|
|
496
496
|
const interval = getAdaptiveInterval(state);
|
|
497
497
|
if (state.turnCount % interval !== 0)
|
|
498
498
|
return;
|
|
499
|
-
tick
|
|
499
|
+
// The boundary travels with the tick: the usage-driven auto-arm arms inside
|
|
500
|
+
// this dispatch, and a drain there can only be judged from the event that
|
|
501
|
+
// named the boundary. Dropping this argument is invisible to every other
|
|
502
|
+
// fixture, so one drives the real handler end to end:
|
|
503
|
+
// test/runtime/heartbeat/test-turn-boundary-arm-wiring.mjs.
|
|
504
|
+
tick(pi, state, config, ctx, event);
|
|
500
505
|
});
|
|
501
506
|
// Event: before_agent_start - flush delayed whispers
|
|
502
507
|
// Deliver buffered heartbeat whispers as part of the turn context,
|
|
@@ -539,7 +544,10 @@ export default function heartbeatExtension(pi) {
|
|
|
539
544
|
const toolName = event?.toolName;
|
|
540
545
|
if (!toolName)
|
|
541
546
|
return;
|
|
542
|
-
|
|
547
|
+
// Verification activity is a name set plus bash by its command shape:
|
|
548
|
+
// a search or diff run through bash is the same activity the hint is
|
|
549
|
+
// about, and counting every bash call (edits, builds) would silence it.
|
|
550
|
+
if (isVerificationToolCall(toolName, event?.input)) {
|
|
543
551
|
state.metaLearning.toolTracking.verificationCount++;
|
|
544
552
|
state.metaLearning.toolTracking.lastVerificationTurn = state.policyTurnCount;
|
|
545
553
|
}
|
|
@@ -10,5 +10,12 @@ import type { ToolTracking } from "./types.js";
|
|
|
10
10
|
export declare const VERIFICATION_TOOLS: Set<string>;
|
|
11
11
|
export declare const WEB_TOOLS: Set<string>;
|
|
12
12
|
export declare const CODE_TOOLS: Set<string>;
|
|
13
|
+
/** True when the command is inspection-shaped (see the rule above). */
|
|
14
|
+
export declare function bashCommandIsVerification(command: string): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Does this tool call count as verification activity? The name set first, then
|
|
17
|
+
* bash by the command's shape. Every other tool is not verification.
|
|
18
|
+
*/
|
|
19
|
+
export declare function isVerificationToolCall(toolName: string, input: unknown): boolean;
|
|
13
20
|
export declare function createDefaultPolicies(getToolTracking: () => ToolTracking, onSignal: (type: string, data: Record<string, unknown>) => void, sendWhisper: (msg: string) => void, sendSteerMessage: (msg: string) => void): Policy[];
|
|
14
21
|
//# sourceMappingURL=policies.d.ts.map
|
|
@@ -25,6 +25,378 @@ export const VERIFICATION_TOOLS = new Set([
|
|
|
25
25
|
]);
|
|
26
26
|
export const WEB_TOOLS = new Set(["fetch_content", "browser_navigate", "browser_content"]);
|
|
27
27
|
export const CODE_TOOLS = new Set(["bash", "write", "edit"]);
|
|
28
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
29
|
+
// VERIFICATION-SHAPE RECOGNITION FOR BASH
|
|
30
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
31
|
+
// VERIFICATION_TOOLS is a tool-NAME set, and the same work routinely runs
|
|
32
|
+
// through bash: `grep -rn "needle" src`, `git diff main...HEAD`, `node -e
|
|
33
|
+
// 'console.log(m.x)'`. The set cannot see it, so the verification counter
|
|
34
|
+
// stayed at zero while the agent verified and the hint below told the agent it
|
|
35
|
+
// had not verified in ten turns. Counting every bash call would swing the
|
|
36
|
+
// defect the other way — an edit, a build or a publish script is not
|
|
37
|
+
// verification, and a counter that accepts them silences the hint it exists to
|
|
38
|
+
// raise.
|
|
39
|
+
//
|
|
40
|
+
// The rule, in one sentence: a command counts when it puts an inspecting
|
|
41
|
+
// program at a program position and no mutating program anywhere.
|
|
42
|
+
// - program positions are the first word of each `;` `&&` `||` `|` `&`
|
|
43
|
+
// segment, tokenized quote-aware (`grep -n "a|b" f` is one segment);
|
|
44
|
+
// - inspecting: search/locate, read, compare/extract programs, git read
|
|
45
|
+
// subcommands, and an inline interpreter probe (`node -e`, `python -c`)
|
|
46
|
+
// whose script shows no write primitive;
|
|
47
|
+
// - mutating: rm/mv/cp/mkdir/tee/dd/chmod…, `sed -i`, `perl -i`, git write
|
|
48
|
+
// subcommands, and any unquoted redirect to a file other than /dev/null.
|
|
49
|
+
// Both directions matter: the two named holes are an interpreter script that
|
|
50
|
+
// writes through a primitive outside INLINE_WRITE_RE, and a mutating program
|
|
51
|
+
// behind `eval` or a heredoc body (both are read as inspection when the same
|
|
52
|
+
// command also inspects — they are listed here rather than guessed at).
|
|
53
|
+
/** Programs that inspect existing state: search, locate, read, compare, extract. */
|
|
54
|
+
const INSPECT_PROGRAMS = new Set([
|
|
55
|
+
"grep",
|
|
56
|
+
"egrep",
|
|
57
|
+
"fgrep",
|
|
58
|
+
"rg",
|
|
59
|
+
"ag",
|
|
60
|
+
"ack",
|
|
61
|
+
"ack-grep",
|
|
62
|
+
"ugrep",
|
|
63
|
+
"pcregrep",
|
|
64
|
+
"find",
|
|
65
|
+
"fd",
|
|
66
|
+
"fdfind",
|
|
67
|
+
"locate",
|
|
68
|
+
"cat",
|
|
69
|
+
"tac",
|
|
70
|
+
"head",
|
|
71
|
+
"tail",
|
|
72
|
+
"bat",
|
|
73
|
+
"less",
|
|
74
|
+
"more",
|
|
75
|
+
"wc",
|
|
76
|
+
"nl",
|
|
77
|
+
"od",
|
|
78
|
+
"xxd",
|
|
79
|
+
"strings",
|
|
80
|
+
"file",
|
|
81
|
+
"stat",
|
|
82
|
+
"ls",
|
|
83
|
+
"tree",
|
|
84
|
+
"realpath",
|
|
85
|
+
"readlink",
|
|
86
|
+
"jq",
|
|
87
|
+
"yq",
|
|
88
|
+
"diff",
|
|
89
|
+
"diff3",
|
|
90
|
+
"cmp",
|
|
91
|
+
"comm",
|
|
92
|
+
"cut",
|
|
93
|
+
"uniq",
|
|
94
|
+
"tr",
|
|
95
|
+
"column",
|
|
96
|
+
"awk",
|
|
97
|
+
"gawk",
|
|
98
|
+
"sed",
|
|
99
|
+
]);
|
|
100
|
+
/** Programs that mutate the tree. Any one of them vetoes the call. */
|
|
101
|
+
const MUTATE_PROGRAMS = new Set([
|
|
102
|
+
"rm",
|
|
103
|
+
"rmdir",
|
|
104
|
+
"mv",
|
|
105
|
+
"cp",
|
|
106
|
+
"ln",
|
|
107
|
+
"touch",
|
|
108
|
+
"mkdir",
|
|
109
|
+
"install",
|
|
110
|
+
"truncate",
|
|
111
|
+
"dd",
|
|
112
|
+
"shred",
|
|
113
|
+
"tee",
|
|
114
|
+
"patch",
|
|
115
|
+
"chmod",
|
|
116
|
+
"chown",
|
|
117
|
+
"chgrp",
|
|
118
|
+
]);
|
|
119
|
+
/**
|
|
120
|
+
* `git` subcommands that only read (the inspection side of the verbs the
|
|
121
|
+
* destructive gate covers separately).
|
|
122
|
+
*/
|
|
123
|
+
const GIT_READ_SUBS = new Set([
|
|
124
|
+
"diff",
|
|
125
|
+
"diff-tree",
|
|
126
|
+
"diff-index",
|
|
127
|
+
"diff-files",
|
|
128
|
+
"show",
|
|
129
|
+
"log",
|
|
130
|
+
"status",
|
|
131
|
+
"grep",
|
|
132
|
+
"blame",
|
|
133
|
+
"shortlog",
|
|
134
|
+
"whatchanged",
|
|
135
|
+
"ls-files",
|
|
136
|
+
"ls-tree",
|
|
137
|
+
"cat-file",
|
|
138
|
+
"rev-parse",
|
|
139
|
+
"rev-list",
|
|
140
|
+
"show-ref",
|
|
141
|
+
"for-each-ref",
|
|
142
|
+
"describe",
|
|
143
|
+
"merge-base",
|
|
144
|
+
"name-rev",
|
|
145
|
+
"count-objects",
|
|
146
|
+
"fsck",
|
|
147
|
+
]);
|
|
148
|
+
/** `git` subcommands that write. Conservative: a remote/config/branch/tag
|
|
149
|
+
* verb may mutate, so it vetoes rather than counts. */
|
|
150
|
+
const GIT_WRITE_SUBS = new Set([
|
|
151
|
+
"add",
|
|
152
|
+
"commit",
|
|
153
|
+
"push",
|
|
154
|
+
"pull",
|
|
155
|
+
"fetch",
|
|
156
|
+
"checkout",
|
|
157
|
+
"switch",
|
|
158
|
+
"restore",
|
|
159
|
+
"reset",
|
|
160
|
+
"revert",
|
|
161
|
+
"merge",
|
|
162
|
+
"rebase",
|
|
163
|
+
"cherry-pick",
|
|
164
|
+
"stash",
|
|
165
|
+
"apply",
|
|
166
|
+
"am",
|
|
167
|
+
"clean",
|
|
168
|
+
"rm",
|
|
169
|
+
"mv",
|
|
170
|
+
"tag",
|
|
171
|
+
"branch",
|
|
172
|
+
"init",
|
|
173
|
+
"clone",
|
|
174
|
+
"config",
|
|
175
|
+
"remote",
|
|
176
|
+
"submodule",
|
|
177
|
+
"worktree",
|
|
178
|
+
"update-index",
|
|
179
|
+
"update-ref",
|
|
180
|
+
"symbolic-ref",
|
|
181
|
+
"notes",
|
|
182
|
+
"gc",
|
|
183
|
+
"prune",
|
|
184
|
+
"reflog",
|
|
185
|
+
]);
|
|
186
|
+
/** Interpreters whose inline-eval form can be a probe. */
|
|
187
|
+
const INLINE_INTERPRETERS = new Set(["node", "deno", "bun", "python", "python3", "ruby"]);
|
|
188
|
+
/** Write primitives inside an inline script (both JS/TS and Python spellings). */
|
|
189
|
+
const INLINE_WRITE_RE = /writeFileSync|writeFile\(|appendFileSync|createWriteStream|rmSync|unlinkSync|renameSync|mkdirSync|copyFileSync|truncateSync|chmodSync|openSync\([^)]*["'`][wa]|os\.remove|os\.rename|shutil\.|open\([^)]*["'`][wa]/;
|
|
190
|
+
/** Wrappers whose presence does not decide the kind of the segment. */
|
|
191
|
+
const WRAPPER_PROGRAMS = new Set(["sudo", "env", "time", "nice", "command", "nohup"]);
|
|
192
|
+
/** Tokens that end one command segment. */
|
|
193
|
+
const SEGMENT_SEPARATORS = new Set([";", "&&", "||", "|", "&", "\n"]);
|
|
194
|
+
/** The program name without its path (`/usr/bin/grep` → `grep`). */
|
|
195
|
+
function baseName(token) {
|
|
196
|
+
const parts = token.split("/");
|
|
197
|
+
return parts[parts.length - 1] ?? token;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Quote-aware tokenizer that keeps shell operators as their own tokens
|
|
201
|
+
* (`;` `&&` `||` `|` `&` newline `>` `>>`, and `&1`/`&2` as one token) and
|
|
202
|
+
* strips quote characters. An escaped character stays literal, so `find …
|
|
203
|
+
* -exec grep … \;` yields the word `;` rather than a separator.
|
|
204
|
+
*/
|
|
205
|
+
function tokenizeCommand(command) {
|
|
206
|
+
const tokens = [];
|
|
207
|
+
let cur = "";
|
|
208
|
+
let quote = null;
|
|
209
|
+
const flush = () => {
|
|
210
|
+
if (cur) {
|
|
211
|
+
tokens.push(cur);
|
|
212
|
+
cur = "";
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
for (let i = 0; i < command.length; i++) {
|
|
216
|
+
const ch = command[i] ?? "";
|
|
217
|
+
if (quote) {
|
|
218
|
+
if (ch === "\\" && quote === '"') {
|
|
219
|
+
cur += command[i + 1] ?? "";
|
|
220
|
+
i++;
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
if (ch === quote) {
|
|
224
|
+
quote = null;
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
cur += ch;
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
if (ch === "\\") {
|
|
231
|
+
cur += command[i + 1] ?? "";
|
|
232
|
+
i++;
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
if (ch === '"' || ch === "'") {
|
|
236
|
+
quote = ch;
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
if (/\s/.test(ch)) {
|
|
240
|
+
flush();
|
|
241
|
+
if (ch === "\n")
|
|
242
|
+
tokens.push("\n");
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
if (ch === ";" || ch === "&") {
|
|
246
|
+
flush();
|
|
247
|
+
const next = command[i + 1] ?? "";
|
|
248
|
+
if (ch === "&" && /\d/.test(next)) {
|
|
249
|
+
tokens.push(`&${next}`);
|
|
250
|
+
i++;
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
if (ch === "&" && next === "&") {
|
|
254
|
+
tokens.push("&&");
|
|
255
|
+
i++;
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
tokens.push(ch);
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
if (ch === "|") {
|
|
262
|
+
flush();
|
|
263
|
+
if ((command[i + 1] ?? "") === "|") {
|
|
264
|
+
tokens.push("||");
|
|
265
|
+
i++;
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
tokens.push("|");
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
if (ch === ">") {
|
|
272
|
+
flush();
|
|
273
|
+
if ((command[i + 1] ?? "") === ">") {
|
|
274
|
+
tokens.push(">>");
|
|
275
|
+
i++;
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
tokens.push(">");
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
cur += ch;
|
|
282
|
+
}
|
|
283
|
+
flush();
|
|
284
|
+
return tokens;
|
|
285
|
+
}
|
|
286
|
+
/** Split tokenized commands into segments at the separators. */
|
|
287
|
+
function splitSegments(tokens) {
|
|
288
|
+
const out = [];
|
|
289
|
+
let cur = [];
|
|
290
|
+
for (const token of tokens) {
|
|
291
|
+
if (SEGMENT_SEPARATORS.has(token)) {
|
|
292
|
+
if (cur.length > 0)
|
|
293
|
+
out.push(cur);
|
|
294
|
+
cur = [];
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
cur.push(token);
|
|
298
|
+
}
|
|
299
|
+
if (cur.length > 0)
|
|
300
|
+
out.push(cur);
|
|
301
|
+
return out;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* A redirect that writes a real file. `/dev/null` and fd duplications
|
|
305
|
+
* (`2>&1`) are the ordinary shapes of a silent inspection and stay benign — a
|
|
306
|
+
* redirect into a path is a write, whatever program it follows.
|
|
307
|
+
*/
|
|
308
|
+
function writesFile(tokens) {
|
|
309
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
310
|
+
const token = tokens[i];
|
|
311
|
+
if (token !== ">" && token !== ">>")
|
|
312
|
+
continue;
|
|
313
|
+
const target = tokens[i + 1];
|
|
314
|
+
if (!target || SEGMENT_SEPARATORS.has(target))
|
|
315
|
+
continue;
|
|
316
|
+
if (/^\/dev\/(?:null|stdout|stderr)$/.test(target))
|
|
317
|
+
continue;
|
|
318
|
+
if (/^&\d$/.test(target))
|
|
319
|
+
continue;
|
|
320
|
+
return true;
|
|
321
|
+
}
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
/** The first argument that is not a flag (`xargs -I{} rm` → `rm`). */
|
|
325
|
+
function firstArgument(args) {
|
|
326
|
+
for (const arg of args)
|
|
327
|
+
if (!arg.startsWith("-"))
|
|
328
|
+
return arg;
|
|
329
|
+
return null;
|
|
330
|
+
}
|
|
331
|
+
/** Classify one segment by the program in its program position. */
|
|
332
|
+
function classifySegment(tokens) {
|
|
333
|
+
let i = 0;
|
|
334
|
+
while (i < tokens.length && /^[A-Za-z_][A-Za-z0-9_]*=/.test(tokens[i] ?? ""))
|
|
335
|
+
i++;
|
|
336
|
+
while (i < tokens.length && WRAPPER_PROGRAMS.has(baseName(tokens[i] ?? "")))
|
|
337
|
+
i++;
|
|
338
|
+
const program = tokens[i] ? baseName(tokens[i]) : "";
|
|
339
|
+
if (!program)
|
|
340
|
+
return "neutral";
|
|
341
|
+
const args = tokens.slice(i + 1);
|
|
342
|
+
if (MUTATE_PROGRAMS.has(program))
|
|
343
|
+
return "mutate";
|
|
344
|
+
// xargs runs its first non-flag argument: classify the thing it runs.
|
|
345
|
+
if (program === "xargs") {
|
|
346
|
+
const target = firstArgument(args);
|
|
347
|
+
return target ? classifySegment([target]) : "neutral";
|
|
348
|
+
}
|
|
349
|
+
if (program === "git") {
|
|
350
|
+
const sub = args.find((a) => GIT_READ_SUBS.has(a) || GIT_WRITE_SUBS.has(a));
|
|
351
|
+
if (!sub)
|
|
352
|
+
return "neutral";
|
|
353
|
+
return GIT_WRITE_SUBS.has(sub) ? "mutate" : "inspect";
|
|
354
|
+
}
|
|
355
|
+
const inPlace = args.some((a) => a === "--in-place" || /^-[A-Za-z]*i/.test(a) || /^--in-place=/.test(a));
|
|
356
|
+
if (program === "sed" || program === "perl") {
|
|
357
|
+
if (inPlace)
|
|
358
|
+
return "mutate";
|
|
359
|
+
return "inspect";
|
|
360
|
+
}
|
|
361
|
+
if (INLINE_INTERPRETERS.has(program)) {
|
|
362
|
+
const inline = args.some((a) => a === "-e" || a === "-p" || a === "--eval" || a === "-c");
|
|
363
|
+
if (!inline)
|
|
364
|
+
return "neutral";
|
|
365
|
+
return INLINE_WRITE_RE.test(args.join(" ")) ? "mutate" : "inspect";
|
|
366
|
+
}
|
|
367
|
+
if (INSPECT_PROGRAMS.has(program))
|
|
368
|
+
return "inspect";
|
|
369
|
+
return "neutral";
|
|
370
|
+
}
|
|
371
|
+
/** True when the command is inspection-shaped (see the rule above). */
|
|
372
|
+
export function bashCommandIsVerification(command) {
|
|
373
|
+
if (!command.trim())
|
|
374
|
+
return false;
|
|
375
|
+
const tokens = tokenizeCommand(command);
|
|
376
|
+
if (writesFile(tokens))
|
|
377
|
+
return false;
|
|
378
|
+
let inspected = false;
|
|
379
|
+
for (const segment of splitSegments(tokens)) {
|
|
380
|
+
const kind = classifySegment(segment);
|
|
381
|
+
if (kind === "mutate")
|
|
382
|
+
return false;
|
|
383
|
+
if (kind === "inspect")
|
|
384
|
+
inspected = true;
|
|
385
|
+
}
|
|
386
|
+
return inspected;
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Does this tool call count as verification activity? The name set first, then
|
|
390
|
+
* bash by the command's shape. Every other tool is not verification.
|
|
391
|
+
*/
|
|
392
|
+
export function isVerificationToolCall(toolName, input) {
|
|
393
|
+
if (VERIFICATION_TOOLS.has(toolName))
|
|
394
|
+
return true;
|
|
395
|
+
if (toolName !== "bash")
|
|
396
|
+
return false;
|
|
397
|
+
const command = input?.command;
|
|
398
|
+
return typeof command === "string" && bashCommandIsVerification(command);
|
|
399
|
+
}
|
|
28
400
|
export function createDefaultPolicies(getToolTracking, onSignal, sendWhisper, sendSteerMessage) {
|
|
29
401
|
return [
|
|
30
402
|
{
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
*/
|
|
14
14
|
import { Type } from "typebox";
|
|
15
15
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
16
|
+
import { type TurnBoundary } from "../../../../../lib/events/turn-boundary.js";
|
|
16
17
|
/**
|
|
17
18
|
* Read the pending queue without creating state (inspect surfaces it).
|
|
18
19
|
* Exported so context_inspect and the heartbeat advisory can report
|
|
@@ -66,9 +67,14 @@ export declare function resolveAutoSlideKeep(branchLength: number): number;
|
|
|
66
67
|
* settle, never mid-run.
|
|
67
68
|
*
|
|
68
69
|
* Called from the heartbeat tick — the only cadence that runs while a session
|
|
69
|
-
* works
|
|
70
|
+
* works, and the only arm site that runs inside a turn boundary dispatch. It
|
|
71
|
+
* hands that boundary in, so the arm is judged by the same rule the drain
|
|
72
|
+
* applies: a "toolUse" boundary below the 0.90W backstop leaves the entry
|
|
73
|
+
* queued for the next loop-done boundary instead of aborting the request the
|
|
74
|
+
* loop is about to dispatch. Returns what happened and why, so the caller can
|
|
75
|
+
* log it.
|
|
70
76
|
*/
|
|
71
|
-
export declare function maybeArmAutoCompaction(pi: ExtensionAPI, ctx: any): {
|
|
77
|
+
export declare function maybeArmAutoCompaction(pi: ExtensionAPI, ctx: any, boundary?: TurnBoundary): {
|
|
72
78
|
armed: boolean;
|
|
73
79
|
reason: string;
|
|
74
80
|
};
|