harness-everything 0.57.4 → 1.0.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/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/.releaserc.json +2 -0
- package/AGENTS.md +8 -2
- package/CHANGELOG.md +23 -0
- package/README.md +4 -4
- package/benchmarks/results/live-host/agy-2026-11-22/README.md +49 -0
- package/benchmarks/results/live-host/agy-2026-11-23/README.md +48 -0
- package/benchmarks/results/live-host/opencode-2026-11-24-v2-lifecycle/README.md +40 -0
- package/benchmarks/results/live-host/opencode-2026-11-24-v2-lifecycle/edit-state-final.json +11 -0
- package/benchmarks/results/live-host/opencode-2026-11-24-v2-lifecycle/events-2026-11-24.jsonl +103 -0
- package/benchmarks/results/live-host/opencode-2026-11-24-v2-lifecycle/host-version.txt +3 -0
- package/benchmarks/results/live-host/opencode-2026-11-24-v2-lifecycle/live-event-trace-probe.js +11 -0
- package/benchmarks/results/live-host/opencode-2026-11-24-v2-lifecycle/plugin-load-lines.txt +2 -0
- package/benchmarks/results/live-host/opencode-2026-11-24-v2-lifecycle/reminder-excerpt.txt +1 -0
- package/ci/doc-capability-consistency.test.js +15 -3
- package/ci/lib/opencode-v2-driver.js +169 -0
- package/ci/mechanism-2ao-plugin-sync.test.js +235 -2
- package/ci/mechanism-2n-opencode-plugin.test.js +387 -223
- package/ci/mechanism-2p-opencode-v2-entrypoint.test.js +24 -15
- package/ci/mechanism-2u-issue-42-regressions.test.js +41 -2
- package/ci/mechanism-2w-platform-compatibility.test.js +29 -5
- package/ci/mechanism-2x-release-automation.test.js +3 -1
- package/ci/mechanism-30-opencode-plugin-loadability.test.js +78 -45
- package/ci/mechanism-338-opencode-async.test.js +367 -194
- package/ci/mechanism-341-opencode-lane.test.js +143 -183
- package/ci/mechanism-39-opencode-verify-outcomes.test.js +118 -86
- package/ci/mechanism-394-opencode-ledger-lesson.test.js +46 -97
- package/ci/mechanism-397-opencode-observations.test.js +58 -47
- package/ci/mechanism-453-opencode-v2-driver.test.js +51 -0
- package/ci/mechanism-453-opencode-v2-only-events.test.js +241 -0
- package/ci/mechanism-agy-hooks.test.js +1096 -0
- package/ci/mechanism-agy-plugin-package.test.js +76 -0
- package/ci/telemetry.test.js +31 -24
- package/docs/architecture.md +4 -2
- package/docs/platform-capabilities.md +62 -26
- package/docs/platform-compatibility.json +41 -6
- package/docs/system-one-observations.md +1 -1
- package/docs/telemetry.md +1 -1
- package/docs/troubleshooting.md +2 -2
- package/hooks/scripts/agy-post-tool.js +124 -0
- package/hooks/scripts/agy-pre-invocation.js +185 -0
- package/hooks/scripts/agy-pre-tool.js +158 -0
- package/hooks/scripts/agy-session-start.js +54 -0
- package/hooks/scripts/agy-stop.js +152 -0
- package/hooks/scripts/lib/agy-adapter-core.js +168 -0
- package/hooks/scripts/state-persist.js +50 -10
- package/opencode-plugin/README.md +60 -66
- package/opencode-plugin/index.mjs +11 -13
- package/opencode-plugin/plugin.json +1 -1
- package/package.json +1 -1
- package/plugins/harness-everything/.antigravity-plugin/plugin.json +18 -0
- package/plugins/harness-everything/.codex-plugin/plugin.json +1 -1
- package/plugins/harness-everything/hooks/scripts/agy-post-tool.js +124 -0
- package/plugins/harness-everything/hooks/scripts/agy-pre-invocation.js +185 -0
- package/plugins/harness-everything/hooks/scripts/agy-pre-tool.js +158 -0
- package/plugins/harness-everything/hooks/scripts/agy-session-start.js +54 -0
- package/plugins/harness-everything/hooks/scripts/agy-stop.js +152 -0
- package/plugins/harness-everything/hooks/scripts/lib/agy-adapter-core.js +168 -0
- package/plugins/harness-everything/hooks/scripts/state-persist.js +50 -10
- package/plugins/harness-everything/hooks.json +44 -0
- package/plugins/harness-everything/plugin.json +1 -1
- package/plugins/harness-everything/rules/AGENTS.md +117 -0
- package/sbom.cdx.json +12 -12
- package/scripts/plugin-sync.js +105 -14
- package/scripts/sync-openai-plugin.js +16 -1
- package/scripts/sync-release-version.js +1 -0
|
@@ -0,0 +1,1096 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const assert = require('assert');
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const os = require('os');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const { spawnSync } = require('child_process');
|
|
8
|
+
|
|
9
|
+
const ROOT = path.resolve(__dirname, '..');
|
|
10
|
+
const SCRIPTS_DIR = path.join(ROOT, 'hooks', 'scripts');
|
|
11
|
+
const HOOKS_JSON = path.join(ROOT, 'plugins', 'harness-everything', 'hooks.json');
|
|
12
|
+
|
|
13
|
+
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'harness-agy-hooks-test-'));
|
|
14
|
+
const tempStateHome = path.join(tempRoot, 'state-home');
|
|
15
|
+
fs.mkdirSync(tempStateHome, { recursive: true });
|
|
16
|
+
|
|
17
|
+
const originalStateHome = process.env.HARNESS_STATE_HOME;
|
|
18
|
+
process.env.HARNESS_STATE_HOME = tempStateHome;
|
|
19
|
+
|
|
20
|
+
const { getSessionDir, getWorkspaceRoot } = require(path.join(SCRIPTS_DIR, 'lib', 'harness-state'));
|
|
21
|
+
|
|
22
|
+
function runHook(scriptName, inputPayload, env = {}) {
|
|
23
|
+
const scriptPath = path.join(SCRIPTS_DIR, scriptName);
|
|
24
|
+
assert.ok(fs.existsSync(scriptPath), `Hook script ${scriptName} must exist`);
|
|
25
|
+
|
|
26
|
+
const childEnv = { ...process.env, HARNESS_STATE_HOME: tempStateHome };
|
|
27
|
+
delete childEnv.HARNESS_REQUIRE_VERIFICATION;
|
|
28
|
+
Object.assign(childEnv, env);
|
|
29
|
+
|
|
30
|
+
const run = spawnSync(process.execPath, [scriptPath], {
|
|
31
|
+
input: JSON.stringify(inputPayload),
|
|
32
|
+
encoding: 'utf8',
|
|
33
|
+
env: childEnv,
|
|
34
|
+
timeout: 20000
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
assert.strictEqual(run.status, 0, `Script ${scriptName} exited non-zero: ${run.stderr}`);
|
|
38
|
+
let parsed;
|
|
39
|
+
try {
|
|
40
|
+
parsed = JSON.parse(run.stdout.trim());
|
|
41
|
+
} catch (err) {
|
|
42
|
+
assert.fail(`Script ${scriptName} stdout is not valid JSON: "${run.stdout}"`);
|
|
43
|
+
}
|
|
44
|
+
return parsed;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
// 1. Verify plugins/harness-everything/hooks.json structure & matcher regexes
|
|
49
|
+
assert.ok(fs.existsSync(HOOKS_JSON), 'plugins/harness-everything/hooks.json must exist at plugin root');
|
|
50
|
+
const hooksConfig = JSON.parse(fs.readFileSync(HOOKS_JSON, 'utf8'));
|
|
51
|
+
assert.ok(hooksConfig['harness-everything'], 'Top-level hook name must be harness-everything');
|
|
52
|
+
|
|
53
|
+
const agyHooks = hooksConfig['harness-everything'];
|
|
54
|
+
assert.ok(Array.isArray(agyHooks.SessionStart), 'SessionStart hook must be defined');
|
|
55
|
+
assert.ok(Array.isArray(agyHooks.PreInvocation), 'PreInvocation hook must be defined');
|
|
56
|
+
assert.ok(Array.isArray(agyHooks.PreToolUse), 'PreToolUse hook must be defined');
|
|
57
|
+
assert.ok(Array.isArray(agyHooks.PostToolUse), 'PostToolUse hook must be defined');
|
|
58
|
+
assert.ok(Array.isArray(agyHooks.Stop), 'Stop hook must be defined');
|
|
59
|
+
|
|
60
|
+
const expectedTools = [
|
|
61
|
+
'run_command',
|
|
62
|
+
'write_file',
|
|
63
|
+
'edit_file',
|
|
64
|
+
'write_to_file',
|
|
65
|
+
'replace_file_content',
|
|
66
|
+
'multi_replace_file_content'
|
|
67
|
+
];
|
|
68
|
+
const expectedMatcher = expectedTools.join('|');
|
|
69
|
+
for (const eventName of ['PreToolUse', 'PostToolUse']) {
|
|
70
|
+
const matcher = agyHooks[eventName][0].matcher;
|
|
71
|
+
assert.strictEqual(
|
|
72
|
+
matcher,
|
|
73
|
+
expectedMatcher,
|
|
74
|
+
`${eventName} matcher must cover all documented AGY command and file-modification tools`
|
|
75
|
+
);
|
|
76
|
+
const regex = new RegExp(`^(?:${matcher})$`);
|
|
77
|
+
for (const toolName of expectedTools) {
|
|
78
|
+
assert.ok(regex.test(toolName), `${eventName} matcher regex must match ${toolName}`);
|
|
79
|
+
}
|
|
80
|
+
for (const nonMatched of ['view_file', 'grep_search', 'read_file']) {
|
|
81
|
+
assert.ok(!regex.test(nonMatched), `${eventName} matcher regex must not match read-only tool ${nonMatched}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// 2. SessionStart Hook: bootstrap message + workspace binding on workspacePaths-only payload
|
|
86
|
+
{
|
|
87
|
+
const sessionWs = path.join(tempRoot, 'session-start-ws');
|
|
88
|
+
fs.mkdirSync(sessionWs, { recursive: true });
|
|
89
|
+
const convId = 'test-session-start-001';
|
|
90
|
+
const start = runHook('agy-session-start.js', {
|
|
91
|
+
conversationId: convId,
|
|
92
|
+
workspacePaths: [sessionWs]
|
|
93
|
+
});
|
|
94
|
+
assert.ok(Array.isArray(start.injectSteps), 'SessionStart must emit injectSteps');
|
|
95
|
+
assert.ok(
|
|
96
|
+
start.injectSteps.some(s => s.systemMessage && s.systemMessage.systemMessage.includes('Harness Skills OS')),
|
|
97
|
+
'Must include Harness bootstrap message'
|
|
98
|
+
);
|
|
99
|
+
const boundRoot = getWorkspaceRoot({ session_id: convId });
|
|
100
|
+
assert.strictEqual(
|
|
101
|
+
fs.realpathSync(boundRoot),
|
|
102
|
+
fs.realpathSync(sessionWs),
|
|
103
|
+
'SessionStart must bind workspace identity from workspacePaths'
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// 3. PreInvocation Hook: router context, workflow persistence, and per-turn deduplication
|
|
108
|
+
{
|
|
109
|
+
const preWs = path.join(tempRoot, 'pre-inv-ws');
|
|
110
|
+
fs.mkdirSync(preWs, { recursive: true });
|
|
111
|
+
|
|
112
|
+
// 3a. 0-based host (AGY 2.34.0 protojson omits invocationNum=0 on first call)
|
|
113
|
+
const convZeroBased = 'test-conv-dedup-0based-' + Date.now();
|
|
114
|
+
const turn1Call0 = runHook('agy-pre-invocation.js', {
|
|
115
|
+
hookEventName: 'PreInvocation',
|
|
116
|
+
conversationId: convZeroBased,
|
|
117
|
+
initialNumSteps: 1,
|
|
118
|
+
lastUserInput: 'Please implement new auth feature and write tests',
|
|
119
|
+
workspacePaths: [preWs]
|
|
120
|
+
});
|
|
121
|
+
assert.ok(Array.isArray(turn1Call0.injectSteps) && turn1Call0.injectSteps.length === 1, 'First call of turn 1 must inject routing');
|
|
122
|
+
|
|
123
|
+
const sessionDir = getSessionDir(preWs, convZeroBased);
|
|
124
|
+
const workflowFile = path.join(sessionDir, 'workflow-run.json');
|
|
125
|
+
assert.ok(fs.existsSync(workflowFile), 'Workflow must be persisted in session dir for conversationId');
|
|
126
|
+
const workflow = JSON.parse(fs.readFileSync(workflowFile, 'utf8'));
|
|
127
|
+
assert.strictEqual(workflow.sessionId, convZeroBased, 'Persisted workflow must match conversationId');
|
|
128
|
+
|
|
129
|
+
// Second model call within the same turn -> must skip injection
|
|
130
|
+
const turn1Call1 = runHook('agy-pre-invocation.js', {
|
|
131
|
+
hookEventName: 'PreInvocation',
|
|
132
|
+
conversationId: convZeroBased,
|
|
133
|
+
initialNumSteps: 1,
|
|
134
|
+
invocationNum: 1,
|
|
135
|
+
lastUserInput: 'Please implement new auth feature and write tests',
|
|
136
|
+
workspacePaths: [preWs]
|
|
137
|
+
});
|
|
138
|
+
assert.deepStrictEqual(turn1Call1.injectSteps, [], 'Second call in same turn must not double-inject');
|
|
139
|
+
|
|
140
|
+
// Third model call within the same turn -> must skip injection
|
|
141
|
+
const turn1Call2 = runHook('agy-pre-invocation.js', {
|
|
142
|
+
hookEventName: 'PreInvocation',
|
|
143
|
+
conversationId: convZeroBased,
|
|
144
|
+
initialNumSteps: 1,
|
|
145
|
+
invocationNum: 2,
|
|
146
|
+
lastUserInput: 'Please implement new auth feature and write tests',
|
|
147
|
+
workspacePaths: [preWs]
|
|
148
|
+
});
|
|
149
|
+
assert.deepStrictEqual(turn1Call2.injectSteps, [], 'Third call in same turn must not double-inject');
|
|
150
|
+
|
|
151
|
+
// New user turn (initialNumSteps advanced) -> must inject fresh routing
|
|
152
|
+
const turn2Call0 = runHook('agy-pre-invocation.js', {
|
|
153
|
+
hookEventName: 'PreInvocation',
|
|
154
|
+
conversationId: convZeroBased,
|
|
155
|
+
initialNumSteps: 9,
|
|
156
|
+
lastUserInput: 'Now review the security edge cases',
|
|
157
|
+
workspacePaths: [preWs]
|
|
158
|
+
});
|
|
159
|
+
assert.strictEqual(turn2Call0.injectSteps.length, 1, 'New user turn must inject fresh routing');
|
|
160
|
+
|
|
161
|
+
// New user turn with identical prompt text but higher initialNumSteps -> must inject fresh routing
|
|
162
|
+
const turn3Call0 = runHook('agy-pre-invocation.js', {
|
|
163
|
+
hookEventName: 'PreInvocation',
|
|
164
|
+
conversationId: convZeroBased,
|
|
165
|
+
initialNumSteps: 15,
|
|
166
|
+
lastUserInput: 'Now review the security edge cases',
|
|
167
|
+
workspacePaths: [preWs]
|
|
168
|
+
});
|
|
169
|
+
assert.strictEqual(turn3Call0.injectSteps.length, 1, 'Repeated prompt on a new turn (new initialNumSteps) must inject fresh routing');
|
|
170
|
+
|
|
171
|
+
// 3b. 1-based invocationNum host -> injects on call 1, skips call 2, injects on new turn call 1
|
|
172
|
+
const convOneBased = 'test-conv-dedup-1based-' + Date.now();
|
|
173
|
+
const oneBasedCall1 = runHook('agy-pre-invocation.js', {
|
|
174
|
+
hookEventName: 'PreInvocation',
|
|
175
|
+
conversationId: convOneBased,
|
|
176
|
+
initialNumSteps: 1,
|
|
177
|
+
invocationNum: 1,
|
|
178
|
+
lastUserInput: 'Add pagination to list API',
|
|
179
|
+
workspacePaths: [preWs]
|
|
180
|
+
});
|
|
181
|
+
assert.strictEqual(oneBasedCall1.injectSteps.length, 1, '1-based host first call must inject');
|
|
182
|
+
|
|
183
|
+
const oneBasedCall2 = runHook('agy-pre-invocation.js', {
|
|
184
|
+
hookEventName: 'PreInvocation',
|
|
185
|
+
conversationId: convOneBased,
|
|
186
|
+
initialNumSteps: 1,
|
|
187
|
+
invocationNum: 2,
|
|
188
|
+
lastUserInput: 'Add pagination to list API',
|
|
189
|
+
workspacePaths: [preWs]
|
|
190
|
+
});
|
|
191
|
+
assert.deepStrictEqual(oneBasedCall2.injectSteps, [], '1-based host second call in same turn must skip');
|
|
192
|
+
|
|
193
|
+
const oneBasedTurn2 = runHook('agy-pre-invocation.js', {
|
|
194
|
+
hookEventName: 'PreInvocation',
|
|
195
|
+
conversationId: convOneBased,
|
|
196
|
+
initialNumSteps: 7,
|
|
197
|
+
invocationNum: 1,
|
|
198
|
+
lastUserInput: 'Add tests for pagination',
|
|
199
|
+
workspacePaths: [preWs]
|
|
200
|
+
});
|
|
201
|
+
assert.strictEqual(oneBasedTurn2.injectSteps.length, 1, '1-based host new turn first call must inject');
|
|
202
|
+
|
|
203
|
+
// 3c. Stateless invocationNum > 0 guard without initialNumSteps
|
|
204
|
+
const statelessMidTurn = runHook('agy-pre-invocation.js', {
|
|
205
|
+
hookEventName: 'PreInvocation',
|
|
206
|
+
conversationId: 'test-conv-stateless-' + Date.now(),
|
|
207
|
+
invocationNum: 1,
|
|
208
|
+
lastUserInput: 'Fix failing test',
|
|
209
|
+
workspacePaths: [preWs]
|
|
210
|
+
});
|
|
211
|
+
assert.deepStrictEqual(statelessMidTurn.injectSteps, [], 'Mid-turn invocationNum > 0 without initialNumSteps must skip');
|
|
212
|
+
|
|
213
|
+
// 3d. Without initialNumSteps: single-call turn followed by explicit invocationNum: 0 with identical prompt must inject fresh
|
|
214
|
+
const convExplicitZero = 'test-conv-explicit-zero-' + Date.now();
|
|
215
|
+
const zeroTurn1 = runHook('agy-pre-invocation.js', {
|
|
216
|
+
hookEventName: 'PreInvocation',
|
|
217
|
+
conversationId: convExplicitZero,
|
|
218
|
+
invocationNum: 0,
|
|
219
|
+
lastUserInput: 'continue',
|
|
220
|
+
workspacePaths: [preWs]
|
|
221
|
+
});
|
|
222
|
+
assert.strictEqual(zeroTurn1.injectSteps.length, 1, 'First turn with explicit invocationNum: 0 must inject');
|
|
223
|
+
|
|
224
|
+
const zeroTurn2 = runHook('agy-pre-invocation.js', {
|
|
225
|
+
hookEventName: 'PreInvocation',
|
|
226
|
+
conversationId: convExplicitZero,
|
|
227
|
+
invocationNum: 0,
|
|
228
|
+
lastUserInput: 'continue',
|
|
229
|
+
workspacePaths: [preWs]
|
|
230
|
+
});
|
|
231
|
+
assert.strictEqual(zeroTurn2.injectSteps.length, 1, 'New turn with explicit invocationNum: 0 and identical prompt must inject fresh');
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// 4. PreToolUse Hook (Canonical Action Gate, Cwd parity, Filesystem Boundary, Artifacts, Symlinks)
|
|
235
|
+
{
|
|
236
|
+
const wsA = path.join(tempRoot, 'ws-a');
|
|
237
|
+
const wsB = path.join(tempRoot, 'ws-b');
|
|
238
|
+
const artifactDir = path.join(tempRoot, 'brain', 'conv-123');
|
|
239
|
+
const outsideDir = path.join(tempRoot, 'outside-dir');
|
|
240
|
+
fs.mkdirSync(wsA, { recursive: true });
|
|
241
|
+
fs.mkdirSync(wsB, { recursive: true });
|
|
242
|
+
fs.mkdirSync(artifactDir, { recursive: true });
|
|
243
|
+
fs.mkdirSync(outsideDir, { recursive: true });
|
|
244
|
+
|
|
245
|
+
// Happy path command
|
|
246
|
+
const allowCmd = runHook('agy-pre-tool.js', {
|
|
247
|
+
toolCall: { id: 'toolu_safe_1', name: 'run_command', args: { CommandLine: 'npm test', Cwd: wsA } },
|
|
248
|
+
workspacePaths: [wsA]
|
|
249
|
+
});
|
|
250
|
+
assert.strictEqual(allowCmd.decision, 'allow', 'npm test should be allowed');
|
|
251
|
+
|
|
252
|
+
// Canonical Action Gate: Destructive rm -rf outside scratch
|
|
253
|
+
const denyRm = runHook('agy-pre-tool.js', {
|
|
254
|
+
toolCall: { id: 'toolu_rm_1', name: 'run_command', args: { CommandLine: 'rm -rf /' } },
|
|
255
|
+
cwd: wsA
|
|
256
|
+
});
|
|
257
|
+
assert.strictEqual(denyRm.decision, 'deny', 'rm -rf / must be denied');
|
|
258
|
+
assert.ok(denyRm.reason.includes('destructive') || denyRm.reason.includes('actionGate:recursive-delete'), 'Must provide actionable reason');
|
|
259
|
+
|
|
260
|
+
// Canonical Action Gate: args.Cwd parity for outside-scratch relative delete
|
|
261
|
+
const scratchSubdir = path.join(tempRoot, 'scratch-run');
|
|
262
|
+
fs.mkdirSync(scratchSubdir, { recursive: true });
|
|
263
|
+
const allowScratchRm = runHook('agy-pre-tool.js', {
|
|
264
|
+
conversationId: 'conv-cwd-parity',
|
|
265
|
+
toolCall: { id: 'toolu_scratch_rm', name: 'run_command', args: { CommandLine: 'rm -rf build-cache', Cwd: scratchSubdir } },
|
|
266
|
+
cwd: ROOT
|
|
267
|
+
});
|
|
268
|
+
assert.strictEqual(allowScratchRm.decision, 'allow', 'rm -rf relative inside args.Cwd scratch dir must be allowed');
|
|
269
|
+
|
|
270
|
+
const denyNonScratchRm = runHook('agy-pre-tool.js', {
|
|
271
|
+
conversationId: 'conv-cwd-parity',
|
|
272
|
+
toolCall: { id: 'toolu_repo_rm', name: 'run_command', args: { CommandLine: 'rm -rf build-cache', Cwd: ROOT } },
|
|
273
|
+
cwd: scratchSubdir
|
|
274
|
+
});
|
|
275
|
+
assert.strictEqual(denyNonScratchRm.decision, 'deny', 'rm -rf relative inside args.Cwd repo root must be denied even if top-level cwd is tmp');
|
|
276
|
+
assert.ok(denyNonScratchRm.reason.includes('actionGate:recursive-delete'), 'Must report recursive-delete rule');
|
|
277
|
+
|
|
278
|
+
// Audit record should be keyed by toolCall.id
|
|
279
|
+
const paritySessionDir = getSessionDir(ROOT, 'conv-cwd-parity');
|
|
280
|
+
assert.ok(
|
|
281
|
+
fs.existsSync(path.join(paritySessionDir, 'action-gate', 'toolu_repo_rm.json')),
|
|
282
|
+
'Action Gate audit record must use forwarded toolCall.id'
|
|
283
|
+
);
|
|
284
|
+
|
|
285
|
+
// Canonical Action Gate: Force-push variants
|
|
286
|
+
for (const pushCmd of [
|
|
287
|
+
'git push --force origin main',
|
|
288
|
+
'git push -f origin main',
|
|
289
|
+
'git -C . push -f origin main'
|
|
290
|
+
]) {
|
|
291
|
+
const denyPush = runHook('agy-pre-tool.js', {
|
|
292
|
+
toolCall: { name: 'run_command', args: { CommandLine: pushCmd, Cwd: wsA } },
|
|
293
|
+
workspacePaths: [wsA]
|
|
294
|
+
});
|
|
295
|
+
assert.strictEqual(denyPush.decision, 'deny', `${pushCmd} must be denied by canonical action gate`);
|
|
296
|
+
assert.ok(denyPush.reason.includes('Force-pushing') || denyPush.reason.includes('actionGate:git-force-push'), 'Action gate reason required');
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Canonical Action Gate: git reset --hard
|
|
300
|
+
const denyReset = runHook('agy-pre-tool.js', {
|
|
301
|
+
toolCall: { name: 'run_command', args: { CommandLine: 'git reset --hard HEAD~1' } },
|
|
302
|
+
cwd: wsA
|
|
303
|
+
});
|
|
304
|
+
assert.strictEqual(denyReset.decision, 'deny', 'git reset --hard must be denied');
|
|
305
|
+
assert.ok(denyReset.reason.includes('reset') || denyReset.reason.includes('actionGate:git-reset-hard'), 'Action gate reason required');
|
|
306
|
+
|
|
307
|
+
// Canonical Action Gate: npm publish
|
|
308
|
+
const denyPublish = runHook('agy-pre-tool.js', {
|
|
309
|
+
toolCall: { name: 'run_command', args: { CommandLine: 'npm publish' } },
|
|
310
|
+
cwd: wsA
|
|
311
|
+
});
|
|
312
|
+
assert.strictEqual(denyPublish.decision, 'deny', 'npm publish must be denied');
|
|
313
|
+
|
|
314
|
+
// Fallback payload shape: tool_name + tool_input
|
|
315
|
+
const denyFallbackCmd = runHook('agy-pre-tool.js', {
|
|
316
|
+
tool_name: 'run_command',
|
|
317
|
+
tool_input: { command: 'git push --force origin main' },
|
|
318
|
+
cwd: wsA
|
|
319
|
+
});
|
|
320
|
+
assert.strictEqual(denyFallbackCmd.decision, 'deny', 'tool_name/tool_input fallback must enforce Action Gate');
|
|
321
|
+
|
|
322
|
+
// Filesystem boundary checks across all documented AGY file mutation tools
|
|
323
|
+
const fileTools = [
|
|
324
|
+
{ name: 'write_file', argKey: 'Path' },
|
|
325
|
+
{ name: 'edit_file', argKey: 'path' },
|
|
326
|
+
{ name: 'write_to_file', argKey: 'TargetFile' },
|
|
327
|
+
{ name: 'replace_file_content', argKey: 'TargetFile' },
|
|
328
|
+
{ name: 'multi_replace_file_content', argKey: 'TargetFile' }
|
|
329
|
+
];
|
|
330
|
+
|
|
331
|
+
for (const { name, argKey } of fileTools) {
|
|
332
|
+
// Missing/empty target file path -> must deny (no silent allow)
|
|
333
|
+
const denyMissing = runHook('agy-pre-tool.js', {
|
|
334
|
+
toolCall: { name, args: { Content: 'bad' } },
|
|
335
|
+
workspacePaths: [wsA],
|
|
336
|
+
artifactDirectoryPath: artifactDir
|
|
337
|
+
});
|
|
338
|
+
assert.strictEqual(denyMissing.decision, 'deny', `${name} with missing path must be denied`);
|
|
339
|
+
assert.ok(denyMissing.reason.includes('missing target file path'), 'Missing path reason required');
|
|
340
|
+
|
|
341
|
+
const denyEmpty = runHook('agy-pre-tool.js', {
|
|
342
|
+
toolCall: { name, args: { [argKey]: ' ' } },
|
|
343
|
+
workspacePaths: [wsA],
|
|
344
|
+
artifactDirectoryPath: artifactDir
|
|
345
|
+
});
|
|
346
|
+
assert.strictEqual(denyEmpty.decision, 'deny', `${name} with empty path must be denied`);
|
|
347
|
+
|
|
348
|
+
// Outside all allowed roots -> must deny
|
|
349
|
+
for (const badPath of ['/etc/shadow', path.join(outsideDir, 'outside.txt')]) {
|
|
350
|
+
const denyOob = runHook('agy-pre-tool.js', {
|
|
351
|
+
toolCall: { name, args: { [argKey]: badPath, Content: 'bad' } },
|
|
352
|
+
workspacePaths: [wsA, wsB],
|
|
353
|
+
artifactDirectoryPath: artifactDir
|
|
354
|
+
});
|
|
355
|
+
assert.strictEqual(denyOob.decision, 'deny', `${name} to ${badPath} must be denied`);
|
|
356
|
+
assert.ok(denyOob.reason.includes('outside workspace root'), 'Boundary violation reason required');
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// Inside primary workspacePaths[0] -> must allow
|
|
360
|
+
const allowWsA = runHook('agy-pre-tool.js', {
|
|
361
|
+
toolCall: { name, args: { [argKey]: path.join(wsA, 'src', 'file.js'), Content: '{}' } },
|
|
362
|
+
workspacePaths: [wsA, wsB],
|
|
363
|
+
artifactDirectoryPath: artifactDir
|
|
364
|
+
});
|
|
365
|
+
assert.strictEqual(allowWsA.decision, 'allow', `${name} inside workspacePaths[0] must be allowed`);
|
|
366
|
+
|
|
367
|
+
// Inside secondary multi-root workspacePaths[1] -> must allow
|
|
368
|
+
const allowWsB = runHook('agy-pre-tool.js', {
|
|
369
|
+
toolCall: { name, args: { [argKey]: path.join(wsB, 'nested', 'new.js'), Content: '{}' } },
|
|
370
|
+
workspacePaths: [wsA, wsB],
|
|
371
|
+
artifactDirectoryPath: artifactDir
|
|
372
|
+
});
|
|
373
|
+
assert.strictEqual(allowWsB.decision, 'allow', `${name} inside workspacePaths[1] must be allowed`);
|
|
374
|
+
|
|
375
|
+
// Inside artifactDirectoryPath (outside workspace) -> must allow
|
|
376
|
+
const allowArtifact = runHook('agy-pre-tool.js', {
|
|
377
|
+
toolCall: { name, args: { [argKey]: path.join(artifactDir, 'implementation_plan.md'), Content: '# Plan' } },
|
|
378
|
+
workspacePaths: [wsA],
|
|
379
|
+
artifactDirectoryPath: artifactDir
|
|
380
|
+
});
|
|
381
|
+
assert.strictEqual(allowArtifact.decision, 'allow', `${name} inside artifactDirectoryPath must be allowed`);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// Fallback payload shape for write tool
|
|
385
|
+
const denyFallbackWrite = runHook('agy-pre-tool.js', {
|
|
386
|
+
tool_name: 'write_file',
|
|
387
|
+
tool_input: { Path: '/etc/shadow' },
|
|
388
|
+
cwd: wsA
|
|
389
|
+
});
|
|
390
|
+
assert.strictEqual(denyFallbackWrite.decision, 'deny', 'tool_name/tool_input fallback must enforce write boundary');
|
|
391
|
+
|
|
392
|
+
// Symlinked workspace & symlink escape checks (including dangling file/dir symlinks and symlink loops)
|
|
393
|
+
const symlinkWs = path.join(tempRoot, 'ws-a-link');
|
|
394
|
+
const escapeLink = path.join(wsA, 'escape-link');
|
|
395
|
+
const danglingFileLink = path.join(wsA, 'dangling-file.txt');
|
|
396
|
+
const danglingDirLink = path.join(wsA, 'dangling-dir');
|
|
397
|
+
const loopA = path.join(wsA, 'loop-a');
|
|
398
|
+
const loopB = path.join(wsA, 'loop-b');
|
|
399
|
+
let symlinksSupported = true;
|
|
400
|
+
try {
|
|
401
|
+
fs.symlinkSync(wsA, symlinkWs, 'dir');
|
|
402
|
+
fs.symlinkSync(outsideDir, escapeLink, 'dir');
|
|
403
|
+
fs.symlinkSync(path.join(outsideDir, 'nonexistent-secret.txt'), danglingFileLink);
|
|
404
|
+
fs.symlinkSync(path.join(outsideDir, 'nonexistent-dir'), danglingDirLink, 'dir');
|
|
405
|
+
fs.symlinkSync(loopB, loopA);
|
|
406
|
+
fs.symlinkSync(loopA, loopB);
|
|
407
|
+
} catch (_) {
|
|
408
|
+
symlinksSupported = false;
|
|
409
|
+
}
|
|
410
|
+
if (symlinksSupported) {
|
|
411
|
+
const allowSymlinkNewFile = runHook('agy-pre-tool.js', {
|
|
412
|
+
toolCall: { name: 'write_file', args: { Path: path.join(symlinkWs, 'new-subdir', 'hello.js') } },
|
|
413
|
+
cwd: fs.realpathSync(wsA)
|
|
414
|
+
});
|
|
415
|
+
assert.strictEqual(allowSymlinkNewFile.decision, 'allow', 'Write via symlinked alias of workspace root must be allowed');
|
|
416
|
+
|
|
417
|
+
const denySymlinkEscape = runHook('agy-pre-tool.js', {
|
|
418
|
+
toolCall: { name: 'write_file', args: { Path: path.join(escapeLink, 'pwn.txt') } },
|
|
419
|
+
cwd: wsA
|
|
420
|
+
});
|
|
421
|
+
assert.strictEqual(denySymlinkEscape.decision, 'deny', 'Write through symlink escaping outside workspace must be denied');
|
|
422
|
+
|
|
423
|
+
const denyDanglingFile = runHook('agy-pre-tool.js', {
|
|
424
|
+
toolCall: { name: 'write_file', args: { Path: danglingFileLink } },
|
|
425
|
+
cwd: wsA
|
|
426
|
+
});
|
|
427
|
+
assert.strictEqual(denyDanglingFile.decision, 'deny', 'Write through dangling file symlink pointing outside workspace must be denied');
|
|
428
|
+
|
|
429
|
+
const denyDanglingDir = runHook('agy-pre-tool.js', {
|
|
430
|
+
toolCall: { name: 'write_file', args: { Path: path.join(danglingDirLink, 'pwn.txt') } },
|
|
431
|
+
cwd: wsA
|
|
432
|
+
});
|
|
433
|
+
assert.strictEqual(denyDanglingDir.decision, 'deny', 'Write through dangling directory symlink pointing outside workspace must be denied');
|
|
434
|
+
|
|
435
|
+
const denySymlinkLoop = runHook('agy-pre-tool.js', {
|
|
436
|
+
toolCall: { name: 'write_file', args: { Path: loopA } },
|
|
437
|
+
cwd: wsA
|
|
438
|
+
});
|
|
439
|
+
assert.strictEqual(denySymlinkLoop.decision, 'deny', 'Write to cyclic symlink loop must fail closed (deny)');
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// 5. End-to-end PostToolUse -> Stop with workspacePaths-only (NO cwd) & session isolation
|
|
444
|
+
{
|
|
445
|
+
const e2eWs = path.join(tempRoot, 'e2e-workspace');
|
|
446
|
+
fs.mkdirSync(e2eWs, { recursive: true });
|
|
447
|
+
const convA = 'test-conv-e2e-a-' + Date.now();
|
|
448
|
+
const convB = 'test-conv-e2e-b-' + Date.now();
|
|
449
|
+
|
|
450
|
+
// Step 1: PostToolUse records a write_to_file inside workspacePaths[0] (NO cwd field!)
|
|
451
|
+
const editRes = runHook('agy-post-tool.js', {
|
|
452
|
+
conversationId: convA,
|
|
453
|
+
workspacePaths: [e2eWs],
|
|
454
|
+
toolCall: { name: 'write_to_file', args: { TargetFile: path.join(e2eWs, 'feature.js') } },
|
|
455
|
+
stepIdx: 2
|
|
456
|
+
});
|
|
457
|
+
assert.deepStrictEqual(editRes, {}, 'PostToolUse must output empty object');
|
|
458
|
+
|
|
459
|
+
// Step 2: Stop on unverified edit with workspacePaths-only (NO cwd!) -> MUST continue across normal reasons
|
|
460
|
+
for (const [idx, normalCase] of [
|
|
461
|
+
{ terminationReason: 'NO_TOOL_CALL', fullyIdle: true, executionNum: 0 },
|
|
462
|
+
{ terminationReason: 'model_stop', fullyIdle: false, executionNum: 1 },
|
|
463
|
+
{ terminationReason: 'MODEL_STOP', fullyIdle: true, executionNum: 2 },
|
|
464
|
+
{ terminationReason: '' },
|
|
465
|
+
{},
|
|
466
|
+
{ terminationReason: 'normal_completion' }
|
|
467
|
+
].entries()) {
|
|
468
|
+
const caseConv = `${convA}-normal-${idx}`;
|
|
469
|
+
runHook('agy-post-tool.js', {
|
|
470
|
+
conversationId: caseConv,
|
|
471
|
+
workspacePaths: [e2eWs],
|
|
472
|
+
toolCall: { name: 'write_to_file', args: { TargetFile: path.join(e2eWs, 'feature.js') } },
|
|
473
|
+
stepIdx: 2
|
|
474
|
+
});
|
|
475
|
+
const stopUnverified = runHook('agy-stop.js', {
|
|
476
|
+
conversationId: caseConv,
|
|
477
|
+
workspacePaths: [e2eWs],
|
|
478
|
+
continuationActive: false,
|
|
479
|
+
...normalCase
|
|
480
|
+
});
|
|
481
|
+
assert.strictEqual(
|
|
482
|
+
stopUnverified.decision,
|
|
483
|
+
'continue',
|
|
484
|
+
`Stop must continue when unverified edits exist (${JSON.stringify(normalCase)})`
|
|
485
|
+
);
|
|
486
|
+
assert.ok(stopUnverified.reason.includes('Verify-Before-Claim'), 'Reason must mention Verify-Before-Claim');
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
// Session isolation: convB on the same workspace has no edits -> Stop allows ({"decision": "allow"})
|
|
490
|
+
const stopConvB = runHook('agy-stop.js', {
|
|
491
|
+
conversationId: convB,
|
|
492
|
+
workspacePaths: [e2eWs],
|
|
493
|
+
continuationActive: false,
|
|
494
|
+
fullyIdle: true,
|
|
495
|
+
executionNum: 0,
|
|
496
|
+
terminationReason: 'NO_TOOL_CALL'
|
|
497
|
+
});
|
|
498
|
+
assert.deepStrictEqual(stopConvB, { decision: 'allow' }, 'Independent conversationId must not inherit convA unverified state');
|
|
499
|
+
|
|
500
|
+
// Step 3: Explicit abnormal terminationReason values allow stop even with unverified edits (case-insensitive / trimmed)
|
|
501
|
+
for (const reason of [
|
|
502
|
+
'USER_CANCELED',
|
|
503
|
+
'CANCELED',
|
|
504
|
+
'CANCELLED',
|
|
505
|
+
'ERROR',
|
|
506
|
+
'MAX_INVOCATIONS',
|
|
507
|
+
'MAX_STEPS_EXCEEDED',
|
|
508
|
+
'MAX_TOKEN_BUDGET_EXCEEDED',
|
|
509
|
+
'TERMINAL_CUSTOM_HOOK',
|
|
510
|
+
'INTERRUPTED',
|
|
511
|
+
'ABORTED',
|
|
512
|
+
' error ',
|
|
513
|
+
'max_steps_exceeded'
|
|
514
|
+
]) {
|
|
515
|
+
const stopTerminated = runHook('agy-stop.js', {
|
|
516
|
+
conversationId: convA,
|
|
517
|
+
workspacePaths: [e2eWs],
|
|
518
|
+
continuationActive: false,
|
|
519
|
+
terminationReason: reason
|
|
520
|
+
});
|
|
521
|
+
assert.deepStrictEqual(stopTerminated, { decision: 'allow' }, `Stop must allow stop on abnormal terminationReason=${reason}`);
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
// Step 4: Compatibility recursion guards (continuationActive / stop_hook_active) allow stop to prevent loops
|
|
525
|
+
assert.deepStrictEqual(
|
|
526
|
+
runHook('agy-stop.js', { conversationId: convA, workspacePaths: [e2eWs], continuationActive: true }),
|
|
527
|
+
{ decision: 'allow' },
|
|
528
|
+
'Must allow stop when continuationActive is true'
|
|
529
|
+
);
|
|
530
|
+
assert.deepStrictEqual(
|
|
531
|
+
runHook('agy-stop.js', { conversationId: convA, workspacePaths: [e2eWs], stop_hook_active: true }),
|
|
532
|
+
{ decision: 'allow' },
|
|
533
|
+
'Must allow stop when stop_hook_active is true'
|
|
534
|
+
);
|
|
535
|
+
|
|
536
|
+
// Step 5: P1-B Unknown outcome (missing error and exitCode) after unverified edit grants NO verification credit
|
|
537
|
+
const unknownVerifyRes = runHook('agy-post-tool.js', {
|
|
538
|
+
conversationId: convA,
|
|
539
|
+
workspacePaths: [e2eWs],
|
|
540
|
+
toolCall: { name: 'run_command', args: { CommandLine: 'npm test', Cwd: e2eWs } },
|
|
541
|
+
error: '',
|
|
542
|
+
stepIdx: 3
|
|
543
|
+
});
|
|
544
|
+
assert.deepStrictEqual(unknownVerifyRes, {}, 'PostToolUse must output empty object on unknown shell outcome');
|
|
545
|
+
|
|
546
|
+
const handoffFile = path.join(getSessionDir(e2eWs, convA), 'handoff-state.json');
|
|
547
|
+
assert.ok(fs.existsSync(handoffFile), 'handoff-state.json must be written in e2eWs session dir');
|
|
548
|
+
const unknownHandoff = JSON.parse(fs.readFileSync(handoffFile, 'utf8'));
|
|
549
|
+
assert.ok(unknownHandoff.lastEditAt > 0, 'lastEditAt must remain recorded');
|
|
550
|
+
assert.ok(!unknownHandoff.lastVerifyAt, 'Unknown shell outcome (missing exitCode/error) must NOT grant lastVerifyAt');
|
|
551
|
+
|
|
552
|
+
const stopAfterUnknownVerify = runHook('agy-stop.js', {
|
|
553
|
+
conversationId: convA,
|
|
554
|
+
workspacePaths: [e2eWs],
|
|
555
|
+
continuationActive: false,
|
|
556
|
+
fullyIdle: true,
|
|
557
|
+
executionNum: 0,
|
|
558
|
+
terminationReason: 'NO_TOOL_CALL'
|
|
559
|
+
});
|
|
560
|
+
assert.strictEqual(stopAfterUnknownVerify.decision, 'continue', 'Stop attempt 1 must continue after unverified edit + unknown shell outcome');
|
|
561
|
+
|
|
562
|
+
const stopAfterUnknownVerifyRetry = runHook('agy-stop.js', {
|
|
563
|
+
conversationId: convA,
|
|
564
|
+
workspacePaths: [e2eWs],
|
|
565
|
+
continuationActive: false,
|
|
566
|
+
fullyIdle: true,
|
|
567
|
+
executionNum: 1,
|
|
568
|
+
terminationReason: 'NO_TOOL_CALL'
|
|
569
|
+
});
|
|
570
|
+
assert.deepStrictEqual(stopAfterUnknownVerifyRetry, { decision: 'allow' }, 'Stop attempt 2 on same unverified edit must allow (bounded retry, no infinite loop)');
|
|
571
|
+
const handoffAfterBoundedStop = JSON.parse(fs.readFileSync(handoffFile, 'utf8'));
|
|
572
|
+
assert.ok(!handoffAfterBoundedStop.lastVerifyAt, 'Bounded Stop allow must leave P1-B lastVerifyAt unset on unknown shell outcome');
|
|
573
|
+
|
|
574
|
+
// Step 6: PostToolUse observes failing run_command via AGY protojson `error` string (no exitCode) and explicit exitCode
|
|
575
|
+
const failRes = runHook('agy-post-tool.js', {
|
|
576
|
+
conversationId: convA,
|
|
577
|
+
workspacePaths: [e2eWs],
|
|
578
|
+
toolCall: { name: 'run_command', args: { CommandLine: 'npm test', Cwd: e2eWs } },
|
|
579
|
+
error: 'exit status 1',
|
|
580
|
+
stepIdx: 4
|
|
581
|
+
});
|
|
582
|
+
assert.deepStrictEqual(failRes, {}, 'PostToolUse must output empty object on failure');
|
|
583
|
+
|
|
584
|
+
const failHandoff = JSON.parse(fs.readFileSync(handoffFile, 'utf8'));
|
|
585
|
+
assert.strictEqual(failHandoff.status, 'failed', 'Status must be failed when AGY error string is present');
|
|
586
|
+
assert.ok(failHandoff.errorSummary.includes('exit status 1'), 'Error summary must capture AGY error string');
|
|
587
|
+
|
|
588
|
+
// File write with omitted outcome metadata preserves edit observation AND does NOT clear failed state
|
|
589
|
+
const prevEditAt = failHandoff.lastEditAt || 0;
|
|
590
|
+
const writeWhileFailed = runHook('agy-post-tool.js', {
|
|
591
|
+
conversationId: convA,
|
|
592
|
+
workspacePaths: [e2eWs],
|
|
593
|
+
toolCall: { name: 'write_file', args: { Path: path.join(e2eWs, 'fix.js'), Content: 'module.exports = 1;' } },
|
|
594
|
+
stepIdx: 5
|
|
595
|
+
});
|
|
596
|
+
assert.deepStrictEqual(writeWhileFailed, {}, 'PostToolUse must output empty object on write_file');
|
|
597
|
+
const handoffAfterWrite = JSON.parse(fs.readFileSync(handoffFile, 'utf8'));
|
|
598
|
+
assert.strictEqual(handoffAfterWrite.status, 'failed', 'File write without exitCode: 0 must preserve existing failed state');
|
|
599
|
+
assert.ok(handoffAfterWrite.lastEditAt >= prevEditAt, 'File write must preserve/advance lastEditAt even when outcome metadata is omitted');
|
|
600
|
+
|
|
601
|
+
// Unknown-outcome shell command (missing error/exitCode) MUST preserve existing failed state
|
|
602
|
+
const unknownWhileFailed = runHook('agy-post-tool.js', {
|
|
603
|
+
conversationId: convA,
|
|
604
|
+
workspacePaths: [e2eWs],
|
|
605
|
+
toolCall: { name: 'run_command', args: { CommandLine: 'npm test', Cwd: e2eWs } },
|
|
606
|
+
error: '',
|
|
607
|
+
stepIdx: 6
|
|
608
|
+
});
|
|
609
|
+
assert.deepStrictEqual(unknownWhileFailed, {}, 'PostToolUse must output empty object on unknown outcome while failed');
|
|
610
|
+
const handoffStillFailed = JSON.parse(fs.readFileSync(handoffFile, 'utf8'));
|
|
611
|
+
assert.strictEqual(handoffStillFailed.status, 'failed', 'Unknown shell outcome must preserve existing failed state');
|
|
612
|
+
assert.ok(!handoffStillFailed.lastVerifyAt, 'Unknown shell outcome while failed must not set lastVerifyAt');
|
|
613
|
+
|
|
614
|
+
const stopFailed = runHook('agy-stop.js', {
|
|
615
|
+
conversationId: convA,
|
|
616
|
+
workspacePaths: [e2eWs],
|
|
617
|
+
continuationActive: false,
|
|
618
|
+
terminationReason: 'NO_TOOL_CALL'
|
|
619
|
+
});
|
|
620
|
+
assert.strictEqual(stopFailed.decision, 'continue', 'Stop must continue when tool failure is unresolved');
|
|
621
|
+
assert.ok(stopFailed.reason.includes('pending tool failure'), 'Reason must mention pending tool failure');
|
|
622
|
+
|
|
623
|
+
// Explicit non-zero exitCode (without error string) also records failed
|
|
624
|
+
const exitCodeFailRes = runHook('agy-post-tool.js', {
|
|
625
|
+
conversationId: convA,
|
|
626
|
+
workspacePaths: [e2eWs],
|
|
627
|
+
toolCall: { name: 'run_command', args: { CommandLine: 'npm test', Cwd: e2eWs } },
|
|
628
|
+
toolResponse: { exit_code: 2 },
|
|
629
|
+
stepIdx: 7
|
|
630
|
+
});
|
|
631
|
+
assert.deepStrictEqual(exitCodeFailRes, {}, 'PostToolUse must output empty object on explicit exit_code failure');
|
|
632
|
+
const exitCodeFailHandoff = JSON.parse(fs.readFileSync(handoffFile, 'utf8'));
|
|
633
|
+
assert.strictEqual(exitCodeFailHandoff.status, 'failed', 'Explicit non-zero exit_code must record failed');
|
|
634
|
+
assert.strictEqual(exitCodeFailHandoff.exitCode, 2, 'Explicit exit_code must be persisted');
|
|
635
|
+
|
|
636
|
+
// Step 7: PostToolUse observes proven verification success (explicit exitCode: 0) -> Stop now allows ({"decision": "allow"})
|
|
637
|
+
const passRes = runHook('agy-post-tool.js', {
|
|
638
|
+
conversationId: convA,
|
|
639
|
+
workspacePaths: [e2eWs],
|
|
640
|
+
toolCall: { name: 'run_command', args: { CommandLine: 'npm test', Cwd: e2eWs } },
|
|
641
|
+
exitCode: 0,
|
|
642
|
+
stepIdx: 8
|
|
643
|
+
});
|
|
644
|
+
assert.deepStrictEqual(passRes, {}, 'PostToolUse must output empty object on success');
|
|
645
|
+
|
|
646
|
+
const resolvedHandoff = JSON.parse(fs.readFileSync(handoffFile, 'utf8'));
|
|
647
|
+
assert.strictEqual(resolvedHandoff.status, 'idle', 'Status must recover to idle after explicit exitCode: 0 verification');
|
|
648
|
+
assert.ok(resolvedHandoff.lastResolved, 'lastResolved record must be retained');
|
|
649
|
+
assert.ok(resolvedHandoff.lastVerifyAt >= resolvedHandoff.lastEditAt, 'lastVerifyAt must be >= lastEditAt');
|
|
650
|
+
assert.strictEqual(resolvedHandoff.lastVerifyExitCode, 0, 'lastVerifyExitCode must be 0 on proven verification');
|
|
651
|
+
|
|
652
|
+
for (const verifiedStopPayload of [
|
|
653
|
+
{ terminationReason: 'NO_TOOL_CALL', fullyIdle: true, executionNum: 1 },
|
|
654
|
+
{ terminationReason: 'model_stop', fullyIdle: false, executionNum: 1 }
|
|
655
|
+
]) {
|
|
656
|
+
const stopVerified = runHook('agy-stop.js', {
|
|
657
|
+
conversationId: convA,
|
|
658
|
+
workspacePaths: [e2eWs],
|
|
659
|
+
continuationActive: false,
|
|
660
|
+
...verifiedStopPayload
|
|
661
|
+
});
|
|
662
|
+
assert.deepStrictEqual(stopVerified, { decision: 'allow' }, 'Stop must allow completion after proven exitCode: 0 verification');
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
const stopStrictVerified = runHook('agy-stop.js', {
|
|
666
|
+
conversationId: convA,
|
|
667
|
+
workspacePaths: [e2eWs],
|
|
668
|
+
continuationActive: false,
|
|
669
|
+
terminationReason: 'NO_TOOL_CALL'
|
|
670
|
+
}, { HARNESS_REQUIRE_VERIFICATION: '1' });
|
|
671
|
+
assert.deepStrictEqual(stopStrictVerified, { decision: 'allow' }, 'Strict verification must allow verified work');
|
|
672
|
+
|
|
673
|
+
const convStrictBounded = `${convA}-strict-bounded`;
|
|
674
|
+
const strictAttempt1 = runHook('agy-stop.js', {
|
|
675
|
+
conversationId: convStrictBounded,
|
|
676
|
+
workspacePaths: [e2eWs],
|
|
677
|
+
continuationActive: false,
|
|
678
|
+
terminationReason: 'NO_TOOL_CALL'
|
|
679
|
+
}, { HARNESS_REQUIRE_VERIFICATION: '1' });
|
|
680
|
+
assert.strictEqual(strictAttempt1.decision, 'continue', 'Strict verification attempt 1 without verify must continue');
|
|
681
|
+
const strictAttempt2 = runHook('agy-stop.js', {
|
|
682
|
+
conversationId: convStrictBounded,
|
|
683
|
+
workspacePaths: [e2eWs],
|
|
684
|
+
continuationActive: false,
|
|
685
|
+
terminationReason: 'NO_TOOL_CALL'
|
|
686
|
+
}, { HARNESS_REQUIRE_VERIFICATION: '1' });
|
|
687
|
+
assert.deepStrictEqual(strictAttempt2, { decision: 'allow' }, 'Strict verification attempt 2 on same state must allow (bounded retry)');
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
// 6. Multi-root workspacePaths end-to-end (PreToolUse -> PostToolUse -> Stop)
|
|
691
|
+
{
|
|
692
|
+
const multiA = path.join(tempRoot, 'multi-ws-a');
|
|
693
|
+
const multiB = path.join(tempRoot, 'multi-ws-b');
|
|
694
|
+
const multiArtifact = path.join(tempRoot, 'multi-artifacts', 'conv');
|
|
695
|
+
const multiOutside = path.join(tempRoot, 'multi-outside');
|
|
696
|
+
fs.mkdirSync(multiA, { recursive: true });
|
|
697
|
+
fs.mkdirSync(multiB, { recursive: true });
|
|
698
|
+
fs.mkdirSync(multiArtifact, { recursive: true });
|
|
699
|
+
fs.mkdirSync(multiOutside, { recursive: true });
|
|
700
|
+
|
|
701
|
+
const convMulti = 'test-conv-multiroot-' + Date.now();
|
|
702
|
+
const convMultiNoToolCall = 'test-conv-multiroot-ntc-' + Date.now();
|
|
703
|
+
const convOther = 'test-conv-multiroot-other-' + Date.now();
|
|
704
|
+
const targetInB = path.join(multiB, 'file.js');
|
|
705
|
+
const sessionDirA = getSessionDir(multiA, convMulti);
|
|
706
|
+
const handoffMultiA = path.join(sessionDirA, 'handoff-state.json');
|
|
707
|
+
|
|
708
|
+
// 6.1-6.2: PreToolUse allows B/file.js; PostToolUse advances lastEditAt in A-bound session
|
|
709
|
+
const preWriteB = runHook('agy-pre-tool.js', {
|
|
710
|
+
conversationId: convMulti,
|
|
711
|
+
workspacePaths: [multiA, multiB],
|
|
712
|
+
cwd: multiA,
|
|
713
|
+
toolCall: { id: 'toolu_multi_b_1', name: 'write_file', args: { Path: targetInB, Content: 'export const x = 1;' } }
|
|
714
|
+
});
|
|
715
|
+
assert.strictEqual(preWriteB.decision, 'allow', 'PreToolUse must allow write inside secondary workspacePaths[1]');
|
|
716
|
+
|
|
717
|
+
const postWriteB = runHook('agy-post-tool.js', {
|
|
718
|
+
conversationId: convMulti,
|
|
719
|
+
workspacePaths: [multiA, multiB],
|
|
720
|
+
cwd: multiA,
|
|
721
|
+
toolCall: { id: 'toolu_multi_b_1', name: 'write_file', args: { Path: targetInB, Content: 'export const x = 1;' } }
|
|
722
|
+
});
|
|
723
|
+
assert.deepStrictEqual(postWriteB, {}, 'PostToolUse must output empty object on secondary root write');
|
|
724
|
+
|
|
725
|
+
assert.ok(fs.existsSync(handoffMultiA), 'handoff-state.json must be written to primary A-bound session');
|
|
726
|
+
const stateAfterB = JSON.parse(fs.readFileSync(handoffMultiA, 'utf8'));
|
|
727
|
+
assert.ok(stateAfterB.lastEditAt > 0, 'PostToolUse write to B/file.js must advance lastEditAt in A-bound session');
|
|
728
|
+
|
|
729
|
+
const sessionDirB = getSessionDir(multiB, convMulti);
|
|
730
|
+
assert.ok(
|
|
731
|
+
!fs.existsSync(path.join(sessionDirB, 'handoff-state.json')),
|
|
732
|
+
'Multi-root edit must stay bound to primary A session rather than forking state into B'
|
|
733
|
+
);
|
|
734
|
+
|
|
735
|
+
// 6.3: Unverified edit in B blocks Stop on NO_TOOL_CALL (separate session) and model_stop (after unknown shell outcome)
|
|
736
|
+
runHook('agy-post-tool.js', {
|
|
737
|
+
conversationId: convMultiNoToolCall,
|
|
738
|
+
workspacePaths: [multiA, multiB],
|
|
739
|
+
cwd: multiA,
|
|
740
|
+
toolCall: { id: 'toolu_multi_b_ntc', name: 'write_file', args: { Path: targetInB, Content: 'export const ntc = 1;' } }
|
|
741
|
+
});
|
|
742
|
+
const stopNoToolCall = runHook('agy-stop.js', {
|
|
743
|
+
conversationId: convMultiNoToolCall,
|
|
744
|
+
workspacePaths: [multiA, multiB],
|
|
745
|
+
cwd: multiA,
|
|
746
|
+
continuationActive: false,
|
|
747
|
+
terminationReason: 'NO_TOOL_CALL'
|
|
748
|
+
});
|
|
749
|
+
assert.strictEqual(stopNoToolCall.decision, 'continue', 'Stop (NO_TOOL_CALL) must continue when unverified edit exists in secondary root B');
|
|
750
|
+
|
|
751
|
+
// Separate conversationId must not inherit convMulti's edit
|
|
752
|
+
const stopOtherConv = runHook('agy-stop.js', {
|
|
753
|
+
conversationId: convOther,
|
|
754
|
+
workspacePaths: [multiA, multiB],
|
|
755
|
+
cwd: multiA,
|
|
756
|
+
continuationActive: false,
|
|
757
|
+
terminationReason: 'NO_TOOL_CALL'
|
|
758
|
+
});
|
|
759
|
+
assert.deepStrictEqual(stopOtherConv, { decision: 'allow' }, 'Separate conversationId must not inherit multi-root edit');
|
|
760
|
+
|
|
761
|
+
// 6.4: Unknown shell outcome (omitted error/exitCode) grants NO verification credit; Stop is bounded to 1 continuation
|
|
762
|
+
const postUnknownVerify = runHook('agy-post-tool.js', {
|
|
763
|
+
conversationId: convMulti,
|
|
764
|
+
workspacePaths: [multiA, multiB],
|
|
765
|
+
cwd: multiA,
|
|
766
|
+
toolCall: { name: 'run_command', args: { CommandLine: 'npm test', Cwd: multiB } },
|
|
767
|
+
error: ''
|
|
768
|
+
});
|
|
769
|
+
assert.deepStrictEqual(postUnknownVerify, {}, 'PostToolUse must output empty object on unknown shell outcome');
|
|
770
|
+
const stateAfterUnknown = JSON.parse(fs.readFileSync(handoffMultiA, 'utf8'));
|
|
771
|
+
assert.ok(!stateAfterUnknown.lastVerifyAt, 'Unknown shell outcome must not grant lastVerifyAt after multi-root edit');
|
|
772
|
+
|
|
773
|
+
const stopAttempt1 = runHook('agy-stop.js', {
|
|
774
|
+
conversationId: convMulti,
|
|
775
|
+
workspacePaths: [multiA, multiB],
|
|
776
|
+
cwd: multiA,
|
|
777
|
+
continuationActive: false,
|
|
778
|
+
executionNum: 0,
|
|
779
|
+
terminationReason: 'model_stop'
|
|
780
|
+
});
|
|
781
|
+
assert.strictEqual(stopAttempt1.decision, 'continue', 'Stop attempt 1 (model_stop) must continue when shell outcome after B edit was unknown');
|
|
782
|
+
|
|
783
|
+
const stopAttempt2 = runHook('agy-stop.js', {
|
|
784
|
+
conversationId: convMulti,
|
|
785
|
+
workspacePaths: [multiA, multiB],
|
|
786
|
+
cwd: multiA,
|
|
787
|
+
continuationActive: false,
|
|
788
|
+
executionNum: 1,
|
|
789
|
+
terminationReason: 'model_stop'
|
|
790
|
+
});
|
|
791
|
+
assert.deepStrictEqual(stopAttempt2, { decision: 'allow' }, 'Stop attempt 2 on same unverified edit must allow (bounded retry, no infinite loop)');
|
|
792
|
+
const stateAfterBoundedAllow = JSON.parse(fs.readFileSync(handoffMultiA, 'utf8'));
|
|
793
|
+
assert.ok(!stateAfterBoundedAllow.lastVerifyAt, 'Bounded Stop allow must leave P1-B lastVerifyAt unset when shell outcome was unknown');
|
|
794
|
+
|
|
795
|
+
// Subsequent new edit in same session advances lastEditAt -> Stop reminds once again, then retry allows
|
|
796
|
+
const postSecondEditB = runHook('agy-post-tool.js', {
|
|
797
|
+
conversationId: convMulti,
|
|
798
|
+
workspacePaths: [multiA, multiB],
|
|
799
|
+
cwd: multiA,
|
|
800
|
+
toolCall: { id: 'toolu_multi_b_2', name: 'write_file', args: { Path: path.join(multiB, 'file2.js'), Content: 'export const y = 2;' } }
|
|
801
|
+
});
|
|
802
|
+
assert.deepStrictEqual(postSecondEditB, {}, 'PostToolUse must output empty object on subsequent edit');
|
|
803
|
+
const stateAfterSecondEdit = JSON.parse(fs.readFileSync(handoffMultiA, 'utf8'));
|
|
804
|
+
assert.ok(stateAfterSecondEdit.lastEditAt > stateAfterBoundedAllow.lastEditAt, 'Subsequent edit must advance lastEditAt');
|
|
805
|
+
|
|
806
|
+
const stopSecondEditAttempt1 = runHook('agy-stop.js', {
|
|
807
|
+
conversationId: convMulti,
|
|
808
|
+
workspacePaths: [multiA, multiB],
|
|
809
|
+
cwd: multiA,
|
|
810
|
+
continuationActive: false,
|
|
811
|
+
executionNum: 2,
|
|
812
|
+
terminationReason: 'NO_TOOL_CALL'
|
|
813
|
+
});
|
|
814
|
+
assert.strictEqual(stopSecondEditAttempt1.decision, 'continue', 'Stop must continue once again after a new unverified edit');
|
|
815
|
+
|
|
816
|
+
const stopSecondEditAttempt2 = runHook('agy-stop.js', {
|
|
817
|
+
conversationId: convMulti,
|
|
818
|
+
workspacePaths: [multiA, multiB],
|
|
819
|
+
cwd: multiA,
|
|
820
|
+
continuationActive: false,
|
|
821
|
+
executionNum: 3,
|
|
822
|
+
terminationReason: 'NO_TOOL_CALL'
|
|
823
|
+
});
|
|
824
|
+
assert.deepStrictEqual(stopSecondEditAttempt2, { decision: 'allow' }, 'Stop retry after second edit must allow (bounded to 1 continuation per edit)');
|
|
825
|
+
|
|
826
|
+
// Trusted verification (run_command with exitCode: 0) -> Stop allows
|
|
827
|
+
const postTrustedVerify = runHook('agy-post-tool.js', {
|
|
828
|
+
conversationId: convMulti,
|
|
829
|
+
workspacePaths: [multiA, multiB],
|
|
830
|
+
cwd: multiA,
|
|
831
|
+
toolCall: { name: 'run_command', args: { CommandLine: 'npm test', Cwd: multiB } },
|
|
832
|
+
exitCode: 0
|
|
833
|
+
});
|
|
834
|
+
assert.deepStrictEqual(postTrustedVerify, {}, 'PostToolUse must output empty object on trusted verification');
|
|
835
|
+
|
|
836
|
+
const stateAfterVerify = JSON.parse(fs.readFileSync(handoffMultiA, 'utf8'));
|
|
837
|
+
assert.ok(stateAfterVerify.lastVerifyAt >= stateAfterVerify.lastEditAt, 'Trusted verification must advance lastVerifyAt in A-bound session');
|
|
838
|
+
assert.strictEqual(stateAfterVerify.lastVerifyExitCode, 0, 'Trusted verification must record exitCode 0');
|
|
839
|
+
|
|
840
|
+
for (const reason of ['model_stop', 'NO_TOOL_CALL']) {
|
|
841
|
+
const stopAllowed = runHook('agy-stop.js', {
|
|
842
|
+
conversationId: convMulti,
|
|
843
|
+
workspacePaths: [multiA, multiB],
|
|
844
|
+
cwd: multiA,
|
|
845
|
+
continuationActive: false,
|
|
846
|
+
terminationReason: reason
|
|
847
|
+
});
|
|
848
|
+
assert.deepStrictEqual(stopAllowed, { decision: 'allow' }, `Stop (${reason}) must allow after trusted verification`);
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
// 6.5 & edge cases: outside roots, artifactDirectoryPath, relative paths, symlinks, empty path
|
|
852
|
+
const convIsolated = 'test-conv-multiroot-oob-' + Date.now();
|
|
853
|
+
const handoffIsolated = path.join(getSessionDir(multiA, convIsolated), 'handoff-state.json');
|
|
854
|
+
|
|
855
|
+
// Outside all allowed roots -> PreToolUse denies, PostToolUse does NOT advance lastEditAt
|
|
856
|
+
const outsideTarget = path.join(multiOutside, 'x.js');
|
|
857
|
+
const preOob = runHook('agy-pre-tool.js', {
|
|
858
|
+
conversationId: convIsolated,
|
|
859
|
+
workspacePaths: [multiA, multiB],
|
|
860
|
+
cwd: multiA,
|
|
861
|
+
artifactDirectoryPath: multiArtifact,
|
|
862
|
+
toolCall: { name: 'write_file', args: { Path: outsideTarget } }
|
|
863
|
+
});
|
|
864
|
+
assert.strictEqual(preOob.decision, 'deny', 'PreToolUse must deny write outside all allowed roots');
|
|
865
|
+
|
|
866
|
+
runHook('agy-post-tool.js', {
|
|
867
|
+
conversationId: convIsolated,
|
|
868
|
+
workspacePaths: [multiA, multiB],
|
|
869
|
+
cwd: multiA,
|
|
870
|
+
artifactDirectoryPath: multiArtifact,
|
|
871
|
+
toolCall: { name: 'write_file', args: { Path: outsideTarget } }
|
|
872
|
+
});
|
|
873
|
+
const stateAfterOob = JSON.parse(fs.readFileSync(handoffIsolated, 'utf8'));
|
|
874
|
+
assert.ok(!stateAfterOob.lastEditAt, 'PostToolUse outside all allowed roots must NOT advance lastEditAt');
|
|
875
|
+
|
|
876
|
+
// artifactDirectoryPath outside workspacePaths -> PreToolUse allows, PostToolUse does NOT advance lastEditAt
|
|
877
|
+
const artifactTarget = path.join(multiArtifact, 'notes.md');
|
|
878
|
+
const preArtifact = runHook('agy-pre-tool.js', {
|
|
879
|
+
conversationId: convIsolated,
|
|
880
|
+
workspacePaths: [multiA, multiB],
|
|
881
|
+
cwd: multiA,
|
|
882
|
+
artifactDirectoryPath: multiArtifact,
|
|
883
|
+
toolCall: { name: 'write_file', args: { Path: artifactTarget } }
|
|
884
|
+
});
|
|
885
|
+
assert.strictEqual(preArtifact.decision, 'allow', 'PreToolUse must still allow writes to artifactDirectoryPath');
|
|
886
|
+
|
|
887
|
+
runHook('agy-post-tool.js', {
|
|
888
|
+
conversationId: convIsolated,
|
|
889
|
+
workspacePaths: [multiA, multiB],
|
|
890
|
+
cwd: multiA,
|
|
891
|
+
artifactDirectoryPath: multiArtifact,
|
|
892
|
+
toolCall: { name: 'write_file', args: { Path: artifactTarget } }
|
|
893
|
+
});
|
|
894
|
+
const stateAfterArtifact = JSON.parse(fs.readFileSync(handoffIsolated, 'utf8'));
|
|
895
|
+
assert.ok(!stateAfterArtifact.lastEditAt, 'PostToolUse to artifactDirectoryPath outside workspacePaths must NOT advance lastEditAt');
|
|
896
|
+
|
|
897
|
+
// Symlink inside B pointing outside + symlink loop inside B -> PreToolUse denies, PostToolUse does NOT advance lastEditAt
|
|
898
|
+
const escapeInB = path.join(multiB, 'escape-to-outside');
|
|
899
|
+
const loopInB1 = path.join(multiB, 'loop-1');
|
|
900
|
+
const loopInB2 = path.join(multiB, 'loop-2');
|
|
901
|
+
let multiSymlinksSupported = true;
|
|
902
|
+
try {
|
|
903
|
+
fs.symlinkSync(multiOutside, escapeInB, 'dir');
|
|
904
|
+
fs.symlinkSync(loopInB2, loopInB1);
|
|
905
|
+
fs.symlinkSync(loopInB1, loopInB2);
|
|
906
|
+
} catch (_) {
|
|
907
|
+
multiSymlinksSupported = false;
|
|
908
|
+
}
|
|
909
|
+
if (multiSymlinksSupported) {
|
|
910
|
+
const symTarget = path.join(escapeInB, 'leak.js');
|
|
911
|
+
const preSym = runHook('agy-pre-tool.js', {
|
|
912
|
+
conversationId: convIsolated,
|
|
913
|
+
workspacePaths: [multiA, multiB],
|
|
914
|
+
cwd: multiA,
|
|
915
|
+
toolCall: { name: 'write_file', args: { Path: symTarget } }
|
|
916
|
+
});
|
|
917
|
+
assert.strictEqual(preSym.decision, 'deny', 'PreToolUse must deny symlink in B pointing outside allowed roots');
|
|
918
|
+
|
|
919
|
+
runHook('agy-post-tool.js', {
|
|
920
|
+
conversationId: convIsolated,
|
|
921
|
+
workspacePaths: [multiA, multiB],
|
|
922
|
+
cwd: multiA,
|
|
923
|
+
toolCall: { name: 'write_file', args: { Path: symTarget } }
|
|
924
|
+
});
|
|
925
|
+
const stateAfterSym = JSON.parse(fs.readFileSync(handoffIsolated, 'utf8'));
|
|
926
|
+
assert.ok(!stateAfterSym.lastEditAt, 'PostToolUse through symlink in B escaping outside must NOT advance lastEditAt');
|
|
927
|
+
|
|
928
|
+
const preLoop = runHook('agy-pre-tool.js', {
|
|
929
|
+
conversationId: convIsolated,
|
|
930
|
+
workspacePaths: [multiA, multiB],
|
|
931
|
+
cwd: multiA,
|
|
932
|
+
toolCall: { name: 'write_file', args: { Path: loopInB1 } }
|
|
933
|
+
});
|
|
934
|
+
assert.strictEqual(preLoop.decision, 'deny', 'PreToolUse must deny symlink loop in B');
|
|
935
|
+
|
|
936
|
+
runHook('agy-post-tool.js', {
|
|
937
|
+
conversationId: convIsolated,
|
|
938
|
+
workspacePaths: [multiA, multiB],
|
|
939
|
+
cwd: multiA,
|
|
940
|
+
toolCall: { name: 'write_file', args: { Path: loopInB1 } }
|
|
941
|
+
});
|
|
942
|
+
const stateAfterLoop = JSON.parse(fs.readFileSync(handoffIsolated, 'utf8'));
|
|
943
|
+
assert.ok(!stateAfterLoop.lastEditAt, 'PostToolUse on symlink loop in B must fail closed and NOT advance lastEditAt');
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
// Empty path in state-persist.js --agy must fail closed and NOT advance lastEditAt
|
|
947
|
+
const emptyStateEnv = { ...process.env, HARNESS_STATE_HOME: tempStateHome };
|
|
948
|
+
delete emptyStateEnv.HARNESS_REQUIRE_VERIFICATION;
|
|
949
|
+
const emptyStateRun = spawnSync(process.execPath, [path.join(SCRIPTS_DIR, 'state-persist.js'), '--agy'], {
|
|
950
|
+
input: JSON.stringify({
|
|
951
|
+
hook_event_name: 'PostToolUse',
|
|
952
|
+
session_id: convIsolated,
|
|
953
|
+
workspace_root: multiA,
|
|
954
|
+
cwd: multiA,
|
|
955
|
+
workspacePaths: [multiA, multiB],
|
|
956
|
+
tool_name: 'Write',
|
|
957
|
+
tool_input: { file_path: ' ' }
|
|
958
|
+
}),
|
|
959
|
+
encoding: 'utf8',
|
|
960
|
+
env: emptyStateEnv,
|
|
961
|
+
timeout: 20000
|
|
962
|
+
});
|
|
963
|
+
assert.strictEqual(emptyStateRun.status, 0, 'state-persist.js --agy must exit 0 on empty path');
|
|
964
|
+
const stateAfterEmpty = JSON.parse(fs.readFileSync(handoffIsolated, 'utf8'));
|
|
965
|
+
assert.ok(!stateAfterEmpty.lastEditAt, 'state-persist.js --agy must fail closed on empty path and NOT advance lastEditAt');
|
|
966
|
+
|
|
967
|
+
// Relative path resolved against cwd (A -> ../multi-ws-b/rel.js) -> PreToolUse allows & PostToolUse advances lastEditAt
|
|
968
|
+
const relTarget = path.join('..', 'multi-ws-b', 'rel.js');
|
|
969
|
+
const preRelB = runHook('agy-pre-tool.js', {
|
|
970
|
+
conversationId: convIsolated,
|
|
971
|
+
workspacePaths: [multiA, multiB],
|
|
972
|
+
cwd: multiA,
|
|
973
|
+
toolCall: { name: 'write_file', args: { Path: relTarget } }
|
|
974
|
+
});
|
|
975
|
+
assert.strictEqual(preRelB.decision, 'allow', 'PreToolUse must allow relative path from cwd A into workspace B');
|
|
976
|
+
|
|
977
|
+
runHook('agy-post-tool.js', {
|
|
978
|
+
conversationId: convIsolated,
|
|
979
|
+
workspacePaths: [multiA, multiB],
|
|
980
|
+
cwd: multiA,
|
|
981
|
+
toolCall: { name: 'write_file', args: { Path: relTarget } }
|
|
982
|
+
});
|
|
983
|
+
const stateAfterRelB = JSON.parse(fs.readFileSync(handoffIsolated, 'utf8'));
|
|
984
|
+
assert.ok(stateAfterRelB.lastEditAt > 0, 'PostToolUse relative path into B must advance lastEditAt');
|
|
985
|
+
|
|
986
|
+
const stopAfterRelB = runHook('agy-stop.js', {
|
|
987
|
+
conversationId: convIsolated,
|
|
988
|
+
workspacePaths: [multiA, multiB],
|
|
989
|
+
cwd: multiA,
|
|
990
|
+
continuationActive: false,
|
|
991
|
+
terminationReason: 'model_stop'
|
|
992
|
+
});
|
|
993
|
+
assert.strictEqual(stopAfterRelB.decision, 'continue', 'Stop must continue after unverified relative-path edit in B');
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
// 7. Unknown-outcome verification shell retries must not advance lastEditAt or defeat bounded Stop
|
|
997
|
+
{
|
|
998
|
+
const retryWs = path.join(tempRoot, 'verify-retry-ws');
|
|
999
|
+
fs.mkdirSync(retryWs, { recursive: true });
|
|
1000
|
+
const convRetry = 'test-conv-verify-retry-' + Date.now();
|
|
1001
|
+
const retryHandoffFile = path.join(getSessionDir(retryWs, convRetry), 'handoff-state.json');
|
|
1002
|
+
|
|
1003
|
+
// 1. File edit -> lastEditAt = T1
|
|
1004
|
+
runHook('agy-post-tool.js', {
|
|
1005
|
+
conversationId: convRetry,
|
|
1006
|
+
workspacePaths: [retryWs],
|
|
1007
|
+
cwd: retryWs,
|
|
1008
|
+
toolCall: { id: 'toolu_retry_edit_1', name: 'write_file', args: { Path: path.join(retryWs, 'app.js'), Content: 'const a = 1;' } }
|
|
1009
|
+
});
|
|
1010
|
+
const state1 = JSON.parse(fs.readFileSync(retryHandoffFile, 'utf8'));
|
|
1011
|
+
const t1 = state1.lastEditAt;
|
|
1012
|
+
assert.ok(t1 > 0, 'Initial file edit must set lastEditAt = T1');
|
|
1013
|
+
|
|
1014
|
+
// 2. Stop attempt 0 -> continue
|
|
1015
|
+
const stop0 = runHook('agy-stop.js', {
|
|
1016
|
+
conversationId: convRetry,
|
|
1017
|
+
workspacePaths: [retryWs],
|
|
1018
|
+
cwd: retryWs,
|
|
1019
|
+
continuationActive: false,
|
|
1020
|
+
executionNum: 0,
|
|
1021
|
+
terminationReason: 'model_stop'
|
|
1022
|
+
});
|
|
1023
|
+
assert.strictEqual(stop0.decision, 'continue', 'Stop attempt 0 must continue on unverified edit');
|
|
1024
|
+
|
|
1025
|
+
// 3. Agent runs npm test with unknown outcome (error: "", no exitCode) -> lastEditAt === T1, lastVerifyAt unset
|
|
1026
|
+
runHook('agy-post-tool.js', {
|
|
1027
|
+
conversationId: convRetry,
|
|
1028
|
+
workspacePaths: [retryWs],
|
|
1029
|
+
cwd: retryWs,
|
|
1030
|
+
toolCall: { id: 'toolu_retry_test_1', name: 'run_command', args: { CommandLine: 'npm test', Cwd: retryWs } },
|
|
1031
|
+
error: ''
|
|
1032
|
+
});
|
|
1033
|
+
const state2 = JSON.parse(fs.readFileSync(retryHandoffFile, 'utf8'));
|
|
1034
|
+
assert.strictEqual(state2.lastEditAt, t1, 'Unknown-outcome npm test must NOT advance lastEditAt');
|
|
1035
|
+
assert.ok(!state2.lastVerifyAt, 'Unknown-outcome npm test must leave lastVerifyAt unset (P1-B)');
|
|
1036
|
+
|
|
1037
|
+
// 4. Stop attempt 1 -> allow (bounded retry)
|
|
1038
|
+
const stop1 = runHook('agy-stop.js', {
|
|
1039
|
+
conversationId: convRetry,
|
|
1040
|
+
workspacePaths: [retryWs],
|
|
1041
|
+
cwd: retryWs,
|
|
1042
|
+
continuationActive: false,
|
|
1043
|
+
executionNum: 1,
|
|
1044
|
+
terminationReason: 'model_stop'
|
|
1045
|
+
});
|
|
1046
|
+
assert.deepStrictEqual(stop1, { decision: 'allow' }, 'Stop attempt 1 after unknown-outcome npm test must allow');
|
|
1047
|
+
|
|
1048
|
+
// 5. Agent retries npm test again (unknown outcome) -> lastEditAt === T1 -> Stop attempt 2 -> allow
|
|
1049
|
+
runHook('agy-post-tool.js', {
|
|
1050
|
+
conversationId: convRetry,
|
|
1051
|
+
workspacePaths: [retryWs],
|
|
1052
|
+
cwd: retryWs,
|
|
1053
|
+
toolCall: { id: 'toolu_retry_test_2', name: 'run_command', args: { CommandLine: 'npm test', Cwd: retryWs } },
|
|
1054
|
+
error: ''
|
|
1055
|
+
});
|
|
1056
|
+
const state3 = JSON.parse(fs.readFileSync(retryHandoffFile, 'utf8'));
|
|
1057
|
+
assert.strictEqual(state3.lastEditAt, t1, 'Repeated unknown-outcome npm test must still NOT advance lastEditAt');
|
|
1058
|
+
assert.ok(!state3.lastVerifyAt, 'Repeated unknown-outcome npm test must still leave lastVerifyAt unset');
|
|
1059
|
+
|
|
1060
|
+
const stop2 = runHook('agy-stop.js', {
|
|
1061
|
+
conversationId: convRetry,
|
|
1062
|
+
workspacePaths: [retryWs],
|
|
1063
|
+
cwd: retryWs,
|
|
1064
|
+
continuationActive: false,
|
|
1065
|
+
executionNum: 2,
|
|
1066
|
+
terminationReason: 'NO_TOOL_CALL'
|
|
1067
|
+
});
|
|
1068
|
+
assert.deepStrictEqual(stop2, { decision: 'allow' }, 'Stop attempt 2 after repeated unknown-outcome npm test must allow');
|
|
1069
|
+
|
|
1070
|
+
// 6. Real new file edit -> lastEditAt > T1 -> Stop returns continue once again
|
|
1071
|
+
runHook('agy-post-tool.js', {
|
|
1072
|
+
conversationId: convRetry,
|
|
1073
|
+
workspacePaths: [retryWs],
|
|
1074
|
+
cwd: retryWs,
|
|
1075
|
+
toolCall: { id: 'toolu_retry_edit_2', name: 'write_file', args: { Path: path.join(retryWs, 'app.js'), Content: 'const a = 2;' } }
|
|
1076
|
+
});
|
|
1077
|
+
const state4 = JSON.parse(fs.readFileSync(retryHandoffFile, 'utf8'));
|
|
1078
|
+
assert.ok(state4.lastEditAt > t1, 'Real new file edit must advance lastEditAt > T1');
|
|
1079
|
+
|
|
1080
|
+
const stopAfterNewEdit = runHook('agy-stop.js', {
|
|
1081
|
+
conversationId: convRetry,
|
|
1082
|
+
workspacePaths: [retryWs],
|
|
1083
|
+
cwd: retryWs,
|
|
1084
|
+
continuationActive: false,
|
|
1085
|
+
executionNum: 3,
|
|
1086
|
+
terminationReason: 'model_stop'
|
|
1087
|
+
});
|
|
1088
|
+
assert.strictEqual(stopAfterNewEdit.decision, 'continue', 'Stop must continue once again after real new file edit');
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
console.log('Antigravity (AGY) lifecycle hook adapters verified: SessionStart, PreInvocation, PreToolUse, PostToolUse, and Stop all conform to protocol.');
|
|
1092
|
+
} finally {
|
|
1093
|
+
if (originalStateHome === undefined) delete process.env.HARNESS_STATE_HOME;
|
|
1094
|
+
else process.env.HARNESS_STATE_HOME = originalStateHome;
|
|
1095
|
+
fs.rmSync(tempRoot, { recursive: true, force: true });
|
|
1096
|
+
}
|