harness-everything 1.0.0 → 1.0.1
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/CHANGELOG.md +7 -0
- package/ci/mechanism-394-ledger-match.test.js +105 -3
- package/ci/mechanism-516-agy-post-tool-exit.test.js +472 -0
- package/ci/mechanism-516-agy-stop-revision.test.js +215 -0
- package/hooks/scripts/agy-post-tool.js +79 -16
- package/hooks/scripts/agy-stop.js +63 -10
- package/hooks/scripts/lib/ledger-match.js +132 -20
- package/hooks/scripts/state-persist.js +7 -1
- package/opencode-plugin/plugin.json +1 -1
- package/package.json +1 -1
- package/plugins/harness-everything/.antigravity-plugin/plugin.json +1 -1
- package/plugins/harness-everything/.codex-plugin/plugin.json +1 -1
- package/plugins/harness-everything/hooks/scripts/agy-post-tool.js +79 -16
- package/plugins/harness-everything/hooks/scripts/agy-stop.js +63 -10
- package/plugins/harness-everything/hooks/scripts/lib/ledger-match.js +132 -20
- package/plugins/harness-everything/hooks/scripts/state-persist.js +7 -1
- package/plugins/harness-everything/plugin.json +1 -1
- package/sbom.cdx.json +7 -7
|
@@ -0,0 +1,215 @@
|
|
|
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 PACKAGED_SCRIPTS = path.join(ROOT, 'plugins', 'harness-everything', 'hooks', 'scripts');
|
|
11
|
+
const TEMP_ROOT = fs.mkdtempSync(path.join(os.tmpdir(), 'harness-516-agy-stop-'));
|
|
12
|
+
const STATE_HOME = path.join(TEMP_ROOT, 'state-home');
|
|
13
|
+
const NO_GIT_PATH = path.join(TEMP_ROOT, 'no-git');
|
|
14
|
+
const originalStateHome = process.env.HARNESS_STATE_HOME;
|
|
15
|
+
fs.mkdirSync(STATE_HOME, { recursive: true });
|
|
16
|
+
fs.mkdirSync(NO_GIT_PATH, { recursive: true });
|
|
17
|
+
process.env.HARNESS_STATE_HOME = STATE_HOME;
|
|
18
|
+
|
|
19
|
+
const { getSessionDir } = require(path.join(PACKAGED_SCRIPTS, 'lib', 'harness-state'));
|
|
20
|
+
const realpath = fs.realpathSync.native || fs.realpathSync;
|
|
21
|
+
|
|
22
|
+
function run(script, payload, args = [], extraEnv = {}) {
|
|
23
|
+
const childEnv = { ...process.env, HARNESS_STATE_HOME: STATE_HOME, ...extraEnv };
|
|
24
|
+
delete childEnv.HARNESS_REQUIRE_VERIFICATION;
|
|
25
|
+
delete childEnv.HARNESS_DOC_RECONCILIATION_PILOT;
|
|
26
|
+
delete childEnv.HARNESS_WORKSPACE_ROOT;
|
|
27
|
+
const result = spawnSync(process.execPath, [path.join(PACKAGED_SCRIPTS, script), ...args], {
|
|
28
|
+
input: JSON.stringify(payload),
|
|
29
|
+
encoding: 'utf8',
|
|
30
|
+
env: childEnv,
|
|
31
|
+
timeout: 20000
|
|
32
|
+
});
|
|
33
|
+
assert.ifError(result.error);
|
|
34
|
+
assert.strictEqual(result.status, 0, `${script} failed: ${result.stderr}`);
|
|
35
|
+
return result.stdout.trim() ? JSON.parse(result.stdout.trim()) : null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function git(root, args) {
|
|
39
|
+
const result = spawnSync('git', args, { cwd: root, encoding: 'utf8', timeout: 20000 });
|
|
40
|
+
assert.ifError(result.error);
|
|
41
|
+
assert.strictEqual(result.status, 0, `git ${args.join(' ')} failed: ${result.stderr}`);
|
|
42
|
+
return result.stdout.trim();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function makeWorkspace(name, withGit = true) {
|
|
46
|
+
const root = path.join(TEMP_ROOT, name);
|
|
47
|
+
fs.mkdirSync(root, { recursive: true });
|
|
48
|
+
if (withGit) {
|
|
49
|
+
git(root, ['init']);
|
|
50
|
+
git(root, ['config', 'user.name', 'Harness test']);
|
|
51
|
+
git(root, ['config', 'user.email', 'harness@example.test']);
|
|
52
|
+
fs.writeFileSync(path.join(root, 'tracked.txt'), 'baseline\n');
|
|
53
|
+
git(root, ['add', 'tracked.txt']);
|
|
54
|
+
git(root, ['commit', '-m', 'test baseline']);
|
|
55
|
+
}
|
|
56
|
+
return realpath(root);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function postTool(root, sessionId, toolCall, fields = {}) {
|
|
60
|
+
return run('agy-post-tool.js', {
|
|
61
|
+
conversationId: sessionId,
|
|
62
|
+
workspacePaths: [root],
|
|
63
|
+
toolCall,
|
|
64
|
+
...fields
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function writeFile(root, sessionId, target, content) {
|
|
69
|
+
fs.writeFileSync(target, content, 'utf8');
|
|
70
|
+
postTool(root, sessionId, { name: 'write_file', args: { Path: target, Content: content } });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function handoffPath(root, sessionId) {
|
|
74
|
+
return path.join(getSessionDir(root, sessionId), 'handoff-state.json');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function readHandoff(root, sessionId) {
|
|
78
|
+
return JSON.parse(fs.readFileSync(handoffPath(root, sessionId), 'utf8'));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function runUnknownCommand(root, sessionId, command = 'harness_unrecognized_command') {
|
|
82
|
+
const before = readHandoff(root, sessionId).lastEditAt || 0;
|
|
83
|
+
while (Date.now() <= before) {}
|
|
84
|
+
postTool(root, sessionId, {
|
|
85
|
+
name: 'run_command',
|
|
86
|
+
args: { CommandLine: command, Cwd: root }
|
|
87
|
+
});
|
|
88
|
+
const after = readHandoff(root, sessionId).lastEditAt || 0;
|
|
89
|
+
assert.ok(after > before, 'unrecognized run_command must preserve the existing AGY shell edit signal');
|
|
90
|
+
return after;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function stop(root, sessionId, extraEnv = {}) {
|
|
94
|
+
return run('agy-stop.js', {
|
|
95
|
+
conversationId: sessionId,
|
|
96
|
+
workspacePaths: [root],
|
|
97
|
+
terminationReason: 'NO_TOOL_CALL'
|
|
98
|
+
}, [], extraEnv);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function expectAllow(root, sessionId, message, extraEnv = {}) {
|
|
102
|
+
assert.deepStrictEqual(stop(root, sessionId, extraEnv), { decision: 'allow' }, message);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
try {
|
|
106
|
+
const repo = makeWorkspace('git-workspace');
|
|
107
|
+
const session = 'p516-main';
|
|
108
|
+
const tracked = path.join(repo, 'tracked.txt');
|
|
109
|
+
assert.strictEqual(git(repo, ['status', '--porcelain']), '', 'the Git fixture starts clean');
|
|
110
|
+
|
|
111
|
+
// A direct in-workspace edit earns one continuation; shell timestamp churn
|
|
112
|
+
// during the same unresolved revision must not earn another.
|
|
113
|
+
writeFile(repo, session, tracked, 'direct edit\n');
|
|
114
|
+
assert.notStrictEqual(git(repo, ['status', '--porcelain']), '', 'the direct edit makes the Git fixture dirty');
|
|
115
|
+
assert.strictEqual(stop(repo, session).decision, 'continue', 'the first unresolved edit gets one continuation');
|
|
116
|
+
for (let i = 0; i < 3; i++) {
|
|
117
|
+
runUnknownCommand(repo, session);
|
|
118
|
+
expectAllow(repo, session, `shell timestamp churn ${i + 1} must not spend a second continuation`);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// A reason change from missing verification to a tool failure stays within
|
|
122
|
+
// the same revision and uses the already-spent continuation.
|
|
123
|
+
postTool(repo, session, {
|
|
124
|
+
name: 'run_command',
|
|
125
|
+
args: { CommandLine: 'harness_unrecognized_failure', Cwd: repo }
|
|
126
|
+
}, { error: 'command failed' });
|
|
127
|
+
expectAllow(repo, session, 'a failure reason transition in the same revision must not rearm the allowance');
|
|
128
|
+
|
|
129
|
+
// A proven verification resolves the episode. A later failure in the same
|
|
130
|
+
// content revision starts a fresh unresolved episode and gets one allowance.
|
|
131
|
+
postTool(repo, session, {
|
|
132
|
+
name: 'run_command',
|
|
133
|
+
args: { CommandLine: 'npm test', Cwd: repo }
|
|
134
|
+
}, { toolResponse: { exitCode: 0 } });
|
|
135
|
+
expectAllow(repo, session, 'proven verification resolves the prior episode');
|
|
136
|
+
postTool(repo, session, {
|
|
137
|
+
name: 'run_command',
|
|
138
|
+
args: { CommandLine: 'npm test', Cwd: repo }
|
|
139
|
+
}, { error: 'new failure after resolution' });
|
|
140
|
+
assert.strictEqual(stop(repo, session).decision, 'continue', 'a new unresolved episode after verification gets one continuation');
|
|
141
|
+
expectAllow(repo, session, 'the new episode is still bounded to one continuation');
|
|
142
|
+
|
|
143
|
+
// A real tracked-content edit made by a shell reopens one continuation even
|
|
144
|
+
// though state-persist also changes lastEditAt for the unrecognized command.
|
|
145
|
+
fs.writeFileSync(tracked, 'shell changed tracked bytes\n');
|
|
146
|
+
runUnknownCommand(repo, session);
|
|
147
|
+
assert.strictEqual(stop(repo, session).decision, 'continue', 'new tracked bytes rearm one continuation');
|
|
148
|
+
expectAllow(repo, session, 'the tracked-content revision is bounded to one continuation');
|
|
149
|
+
|
|
150
|
+
// Non-ignored untracked bytes are part of the same content identity. Staging
|
|
151
|
+
// and committing those exact bytes must not rearm the allowance.
|
|
152
|
+
const untracked = path.join(repo, 'shell-generated.txt');
|
|
153
|
+
fs.writeFileSync(untracked, 'untracked shell bytes\n');
|
|
154
|
+
runUnknownCommand(repo, session);
|
|
155
|
+
assert.strictEqual(stop(repo, session).decision, 'continue', 'new non-ignored untracked bytes rearm one continuation');
|
|
156
|
+
git(repo, ['add', 'tracked.txt', 'shell-generated.txt']);
|
|
157
|
+
git(repo, ['commit', '-m', 'record shell output']);
|
|
158
|
+
assert.strictEqual(git(repo, ['status', '--porcelain']), '', 'the Git fixture returns to clean after commit');
|
|
159
|
+
runUnknownCommand(repo, session, 'git add tracked.txt && git commit -m record');
|
|
160
|
+
expectAllow(repo, session, 'staging or committing the same bytes must not rearm the allowance');
|
|
161
|
+
|
|
162
|
+
// A missed content probe during a reason transition must preserve the last
|
|
163
|
+
// known digest so a real edit without a direct-write event still rearms once.
|
|
164
|
+
const probeMiss = makeWorkspace('probe-miss-workspace');
|
|
165
|
+
const probeMissSession = 'p516-probe-miss';
|
|
166
|
+
const probeMissTracked = path.join(probeMiss, 'tracked.txt');
|
|
167
|
+
writeFile(probeMiss, probeMissSession, probeMissTracked, 'direct edit\n');
|
|
168
|
+
assert.strictEqual(stop(probeMiss, probeMissSession).decision, 'continue', 'the probe-miss fixture spends its first allowance with a valid digest');
|
|
169
|
+
postTool(probeMiss, probeMissSession, {
|
|
170
|
+
name: 'run_command',
|
|
171
|
+
args: { CommandLine: 'harness_unrecognized_failure', Cwd: probeMiss }
|
|
172
|
+
}, { error: 'reason transition during probe miss' });
|
|
173
|
+
expectAllow(probeMiss, probeMissSession, 'a reason transition during a missed Git probe stays within the spent allowance', { PATH: NO_GIT_PATH });
|
|
174
|
+
fs.writeFileSync(probeMissTracked, 'new bytes without direct-write event\n');
|
|
175
|
+
assert.strictEqual(stop(probeMiss, probeMissSession).decision, 'continue', 'recovered Git detects changed bytes after the probe miss and rearms exactly once');
|
|
176
|
+
expectAllow(probeMiss, probeMissSession, 'the recovered content revision is still bounded to one continuation');
|
|
177
|
+
|
|
178
|
+
// The same reason transition on unchanged bytes remains allowed after Git returns.
|
|
179
|
+
const unchangedProbe = makeWorkspace('unchanged-probe-workspace');
|
|
180
|
+
const unchangedSession = 'p516-unchanged-probe';
|
|
181
|
+
writeFile(unchangedProbe, unchangedSession, path.join(unchangedProbe, 'tracked.txt'), 'direct edit\n');
|
|
182
|
+
assert.strictEqual(stop(unchangedProbe, unchangedSession).decision, 'continue', 'the unchanged-byte control spends one allowance with a valid digest');
|
|
183
|
+
postTool(unchangedProbe, unchangedSession, {
|
|
184
|
+
name: 'run_command',
|
|
185
|
+
args: { CommandLine: 'harness_unrecognized_failure', Cwd: unchangedProbe }
|
|
186
|
+
}, { error: 'same reason transition during probe miss' });
|
|
187
|
+
expectAllow(unchangedProbe, unchangedSession, 'the unchanged-byte reason transition is allowed while Git is unavailable', { PATH: NO_GIT_PATH });
|
|
188
|
+
expectAllow(unchangedProbe, unchangedSession, 'unchanged bytes remain allowed after Git returns');
|
|
189
|
+
expectAllow(unchangedProbe, unchangedSession, 'unchanged bytes do not gain verification credit or a new allowance');
|
|
190
|
+
|
|
191
|
+
// Non-Git workspaces fall back to the stable direct revision/session budget.
|
|
192
|
+
const plain = makeWorkspace('non-git-workspace', false);
|
|
193
|
+
const plainSession = 'p516-non-git';
|
|
194
|
+
writeFile(plain, plainSession, path.join(plain, 'edited.txt'), 'edit\n');
|
|
195
|
+
assert.strictEqual(stop(plain, plainSession).decision, 'continue', 'non-Git pending edit gets one continuation');
|
|
196
|
+
for (let i = 0; i < 2; i++) {
|
|
197
|
+
runUnknownCommand(plain, plainSession);
|
|
198
|
+
expectAllow(plain, plainSession, 'a failed workspace fingerprint probe must still be bounded');
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// If the marker cannot be written safely, Stop must fail open rather than
|
|
202
|
+
// return continue without a durable cap.
|
|
203
|
+
const markerRoot = makeWorkspace('marker-failure-workspace', false);
|
|
204
|
+
const markerSession = 'p516-marker-failure';
|
|
205
|
+
writeFile(markerRoot, markerSession, path.join(markerRoot, 'edited.txt'), 'edit\n');
|
|
206
|
+
const stopStatePath = path.join(getSessionDir(markerRoot, markerSession), 'agy-stop-state.json');
|
|
207
|
+
fs.mkdirSync(stopStatePath);
|
|
208
|
+
expectAllow(markerRoot, markerSession, 'a marker write failure must allow stop');
|
|
209
|
+
|
|
210
|
+
console.log('AGY Stop revision allowance verified through packaged hooks.');
|
|
211
|
+
} finally {
|
|
212
|
+
if (originalStateHome === undefined) delete process.env.HARNESS_STATE_HOME;
|
|
213
|
+
else process.env.HARNESS_STATE_HOME = originalStateHome;
|
|
214
|
+
fs.rmSync(TEMP_ROOT, { recursive: true, force: true });
|
|
215
|
+
}
|
|
@@ -25,6 +25,66 @@ const FILE_MUTATION_TOOLS = new Set([
|
|
|
25
25
|
'multi_replace_file_content'
|
|
26
26
|
]);
|
|
27
27
|
|
|
28
|
+
const FAILED_STATUSES = new Set([
|
|
29
|
+
'failed',
|
|
30
|
+
'fail',
|
|
31
|
+
'error',
|
|
32
|
+
'errored',
|
|
33
|
+
'timed_out',
|
|
34
|
+
'timeout',
|
|
35
|
+
'cancelled',
|
|
36
|
+
'canceled',
|
|
37
|
+
'aborted'
|
|
38
|
+
]);
|
|
39
|
+
|
|
40
|
+
function normalizeErrorText(rawError) {
|
|
41
|
+
if (typeof rawError === 'string') return rawError.trim();
|
|
42
|
+
if (rawError === null || rawError === undefined || rawError === false) return '';
|
|
43
|
+
if (typeof rawError === 'object') {
|
|
44
|
+
if (Array.isArray(rawError) && rawError.length === 0) return '';
|
|
45
|
+
if (!Array.isArray(rawError) && Object.keys(rawError).length === 0) return '';
|
|
46
|
+
}
|
|
47
|
+
const serialized = JSON.stringify(rawError);
|
|
48
|
+
return typeof serialized === 'string' ? serialized.trim() : '';
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function hasExplicitFailureFlag(obj) {
|
|
52
|
+
return obj.isError === true
|
|
53
|
+
|| obj.is_error === true
|
|
54
|
+
|| obj.success === false
|
|
55
|
+
|| obj.ok === false
|
|
56
|
+
|| obj.interrupted === true;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function classifyStructuredOutcome(safePayload, responseObj, error) {
|
|
60
|
+
const rawExitCodes = [
|
|
61
|
+
safePayload.exitCode,
|
|
62
|
+
safePayload.exit_code,
|
|
63
|
+
responseObj.exitCode,
|
|
64
|
+
responseObj.exit_code
|
|
65
|
+
].filter(Number.isInteger);
|
|
66
|
+
const nonzeroCode = rawExitCodes.find(code => code !== 0);
|
|
67
|
+
const exitCode = nonzeroCode ?? rawExitCodes[0];
|
|
68
|
+
|
|
69
|
+
const statuses = [safePayload.status, responseObj.status]
|
|
70
|
+
.filter(value => typeof value === 'string' && value.trim())
|
|
71
|
+
.map(value => value.trim().toLowerCase());
|
|
72
|
+
|
|
73
|
+
// Structured host errors, failure flags, and nonzero exit codes win over any
|
|
74
|
+
// zero or generic metadata fields. Text-only stdout/output markers are never parsed here
|
|
75
|
+
// because retained AGY captures do not distinguish host metadata from stdout.
|
|
76
|
+
const isFailed = Boolean(error && error.trim())
|
|
77
|
+
|| (exitCode !== undefined && exitCode !== 0)
|
|
78
|
+
|| hasExplicitFailureFlag(safePayload)
|
|
79
|
+
|| hasExplicitFailureFlag(responseObj)
|
|
80
|
+
|| statuses.some(status => FAILED_STATUSES.has(status));
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
isFailed,
|
|
84
|
+
exitCode: isFailed ? (exitCode !== undefined && exitCode !== 0 ? exitCode : undefined) : exitCode
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
28
88
|
function mapAgyPayloadToStatePersist(payload) {
|
|
29
89
|
const safePayload = payload && typeof payload === 'object' ? payload : {};
|
|
30
90
|
const sessionId = getAgySessionId(safePayload);
|
|
@@ -56,21 +116,22 @@ function mapAgyPayloadToStatePersist(payload) {
|
|
|
56
116
|
toolInput = { ...args, file_path: filePath };
|
|
57
117
|
}
|
|
58
118
|
|
|
59
|
-
const
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
safePayload.
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
119
|
+
const rawResponse = safePayload.toolResponse || safePayload.tool_response || safePayload.result;
|
|
120
|
+
const responseObj = (rawResponse && typeof rawResponse === 'object' && !Array.isArray(rawResponse))
|
|
121
|
+
? rawResponse
|
|
122
|
+
: {};
|
|
123
|
+
const error = normalizeErrorText(safePayload.error) || normalizeErrorText(responseObj.error);
|
|
124
|
+
const stdout = (typeof responseObj.stdout === 'string' && responseObj.stdout)
|
|
125
|
+
|| (typeof safePayload.stdout === 'string' && safePayload.stdout)
|
|
126
|
+
|| (typeof safePayload.output === 'string' && safePayload.output)
|
|
127
|
+
|| (typeof rawResponse === 'string' && rawResponse)
|
|
128
|
+
|| '';
|
|
129
|
+
const stderr = (typeof responseObj.stderr === 'string' && responseObj.stderr)
|
|
130
|
+
|| (typeof safePayload.stderr === 'string' && safePayload.stderr)
|
|
131
|
+
|| error
|
|
132
|
+
|| '';
|
|
72
133
|
|
|
73
|
-
const isFailed =
|
|
134
|
+
const { isFailed, exitCode } = classifyStructuredOutcome(safePayload, responseObj, error);
|
|
74
135
|
|
|
75
136
|
return {
|
|
76
137
|
hook_event_name: isFailed ? 'PostToolUseFailure' : 'PostToolUse',
|
|
@@ -89,7 +150,9 @@ function mapAgyPayloadToStatePersist(payload) {
|
|
|
89
150
|
stderr,
|
|
90
151
|
...(exitCode !== undefined ? { exitCode } : {})
|
|
91
152
|
},
|
|
92
|
-
error: isFailed
|
|
153
|
+
error: isFailed
|
|
154
|
+
? (error || stderr || (exitCode !== undefined ? `Command exited with code ${exitCode}` : 'Tool execution failed'))
|
|
155
|
+
: '',
|
|
93
156
|
...(exitCode !== undefined ? { exitCode } : {})
|
|
94
157
|
};
|
|
95
158
|
}
|
|
@@ -121,4 +184,4 @@ if (require.main === module) {
|
|
|
121
184
|
});
|
|
122
185
|
}
|
|
123
186
|
|
|
124
|
-
module.exports = { main, mapAgyPayloadToStatePersist };
|
|
187
|
+
module.exports = { main, mapAgyPayloadToStatePersist, classifyStructuredOutcome };
|
|
@@ -26,6 +26,7 @@ const {
|
|
|
26
26
|
normalizeAgyStatePayload
|
|
27
27
|
} = require('./lib/agy-adapter-core');
|
|
28
28
|
const { getWorkspaceRoot, getSessionDir } = require('./lib/harness-state');
|
|
29
|
+
const { workspaceFingerprint } = require('./lib/workflow-isolation');
|
|
29
30
|
|
|
30
31
|
const STOP_STATE_FILE = 'agy-stop-state.json';
|
|
31
32
|
|
|
@@ -40,16 +41,58 @@ function readStopState(filePath) {
|
|
|
40
41
|
}
|
|
41
42
|
|
|
42
43
|
function writeStopState(filePath, state) {
|
|
44
|
+
let temp;
|
|
43
45
|
try {
|
|
44
46
|
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
45
|
-
|
|
47
|
+
temp = `${filePath}.${process.pid}.${Date.now()}.tmp`;
|
|
46
48
|
fs.writeFileSync(temp, JSON.stringify(state, null, 2) + '\n', 'utf8');
|
|
47
49
|
fs.renameSync(temp, filePath);
|
|
50
|
+
return true;
|
|
48
51
|
} catch (_) {
|
|
49
|
-
|
|
52
|
+
if (temp) { try { fs.unlinkSync(temp); } catch (_) {} }
|
|
53
|
+
return false;
|
|
50
54
|
}
|
|
51
55
|
}
|
|
52
56
|
|
|
57
|
+
function clearStopState(filePath) {
|
|
58
|
+
try { fs.unlinkSync(filePath); } catch (_) {}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function pendingRevision(handoff, root, sessionId) {
|
|
62
|
+
const directRevision = Number.isSafeInteger(handoff && handoff.agyDirectMutationRevision)
|
|
63
|
+
? handoff.agyDirectMutationRevision
|
|
64
|
+
: null;
|
|
65
|
+
let contentFingerprint = null;
|
|
66
|
+
try { contentFingerprint = workspaceFingerprint(root, { maxPaths: 5000 }); } catch (_) {}
|
|
67
|
+
return {
|
|
68
|
+
revision: directRevision === null ? `session:${sessionId || 'default'}` : `direct:${directRevision}`,
|
|
69
|
+
directRevision,
|
|
70
|
+
contentFingerprint
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function samePendingRevision(saved, current) {
|
|
75
|
+
if (!saved) return false;
|
|
76
|
+
if (!saved.lastContinuedRevision) {
|
|
77
|
+
// Older markers have no stable revision. Keep their session allowance
|
|
78
|
+
// when handoff state is legacy too; the prior content baseline is unknown.
|
|
79
|
+
return current.directRevision === null && Boolean(saved.lastContinuedFingerprint);
|
|
80
|
+
}
|
|
81
|
+
if (saved.lastContinuedRevision !== current.revision) return false;
|
|
82
|
+
return !current.contentFingerprint || !saved.lastContinuedContentFingerprint
|
|
83
|
+
|| saved.lastContinuedContentFingerprint === current.contentFingerprint;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function stopMarker(revision, reason) {
|
|
87
|
+
return {
|
|
88
|
+
lastContinuedRevision: revision.revision,
|
|
89
|
+
lastContinuedContentFingerprint: revision.contentFingerprint,
|
|
90
|
+
lastContinuedFingerprint: `${revision.revision}${revision.contentFingerprint ? `|workspace:${revision.contentFingerprint}` : ''}`,
|
|
91
|
+
lastContinuedReason: reason,
|
|
92
|
+
updatedAt: new Date().toISOString()
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
53
96
|
const ABNORMAL_TERMINATION_REASONS = new Set([
|
|
54
97
|
'USER_CANCELED',
|
|
55
98
|
'CANCELED',
|
|
@@ -103,17 +146,14 @@ function evaluateStop(payload) {
|
|
|
103
146
|
let pending = null;
|
|
104
147
|
if (isFailed) {
|
|
105
148
|
pending = {
|
|
106
|
-
fingerprint: `failed:${handoff.timestamp || ''}:${handoff.tool || ''}:${handoff.exitCode ?? ''}`,
|
|
107
149
|
reason: 'Harness Verify-Before-Claim: pending tool failure must be resolved before claiming completion.'
|
|
108
150
|
};
|
|
109
151
|
} else if (lastEditAt > 0 && lastVerifyAt < lastEditAt) {
|
|
110
152
|
pending = {
|
|
111
|
-
fingerprint: `edit:${lastEditAt}`,
|
|
112
153
|
reason: 'Harness Verify-Before-Claim: unverified edits exist in session. Run relevant test/build/lint verification before claiming completion.'
|
|
113
154
|
};
|
|
114
155
|
} else if (process.env.HARNESS_REQUIRE_VERIFICATION === '1' && (!lastVerifyAt || lastVerifyAt < lastEditAt)) {
|
|
115
156
|
pending = {
|
|
116
|
-
fingerprint: `strict:${lastVerifyAt}`,
|
|
117
157
|
reason: 'Harness Verify-Before-Claim: objective test verification is required before claiming completion.'
|
|
118
158
|
};
|
|
119
159
|
}
|
|
@@ -121,19 +161,32 @@ function evaluateStop(payload) {
|
|
|
121
161
|
if (pending) {
|
|
122
162
|
const stopStateFile = path.join(sessionDir, STOP_STATE_FILE);
|
|
123
163
|
const saved = readStopState(stopStateFile);
|
|
124
|
-
|
|
164
|
+
const revision = pendingRevision(handoff, root, sessionId);
|
|
165
|
+
if (samePendingRevision(saved, revision)) {
|
|
166
|
+
if (saved.lastContinuedReason !== pending.reason
|
|
167
|
+
|| saved.lastContinuedRevision !== revision.revision
|
|
168
|
+
|| (!saved.lastContinuedContentFingerprint && revision.contentFingerprint)) {
|
|
169
|
+
const marker = stopMarker(revision, pending.reason);
|
|
170
|
+
// A missed probe can refresh diagnostics, but cannot erase the last content baseline.
|
|
171
|
+
if (!revision.contentFingerprint && saved.lastContinuedContentFingerprint) {
|
|
172
|
+
marker.lastContinuedContentFingerprint = saved.lastContinuedContentFingerprint;
|
|
173
|
+
marker.lastContinuedFingerprint = saved.lastContinuedFingerprint;
|
|
174
|
+
}
|
|
175
|
+
writeStopState(stopStateFile, {
|
|
176
|
+
...saved,
|
|
177
|
+
...marker
|
|
178
|
+
});
|
|
179
|
+
}
|
|
125
180
|
return { decision: 'allow' };
|
|
126
181
|
}
|
|
127
|
-
writeStopState(stopStateFile, {
|
|
128
|
-
lastContinuedFingerprint: pending.fingerprint,
|
|
129
|
-
updatedAt: new Date().toISOString()
|
|
130
|
-
});
|
|
182
|
+
if (!writeStopState(stopStateFile, stopMarker(revision, pending.reason))) return { decision: 'allow' };
|
|
131
183
|
return {
|
|
132
184
|
decision: 'continue',
|
|
133
185
|
reason: pending.reason
|
|
134
186
|
};
|
|
135
187
|
}
|
|
136
188
|
|
|
189
|
+
clearStopState(path.join(sessionDir, STOP_STATE_FILE));
|
|
137
190
|
return { decision: 'allow' };
|
|
138
191
|
}
|
|
139
192
|
|
|
@@ -190,40 +190,152 @@ function words(command) {
|
|
|
190
190
|
return tokens;
|
|
191
191
|
}
|
|
192
192
|
|
|
193
|
-
|
|
194
|
-
|
|
193
|
+
function literalRedirectTarget(command, start) {
|
|
194
|
+
let index = start;
|
|
195
|
+
while (index < command.length && /\s/.test(command[index])) index += 1;
|
|
196
|
+
const targetStart = index;
|
|
197
|
+
let quote = null;
|
|
198
|
+
let length = 0;
|
|
199
|
+
for (; index < command.length; index += 1) {
|
|
200
|
+
const char = command[index];
|
|
201
|
+
if (quote === "'") {
|
|
202
|
+
if (char === "'") quote = null;
|
|
203
|
+
else length += 1;
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
if (quote === '"') {
|
|
207
|
+
if (char === '"') quote = null;
|
|
208
|
+
else if (char === '\\' && index + 1 < command.length) { index += 1; length += 1; }
|
|
209
|
+
else if (char === '$' || char === '`') return null;
|
|
210
|
+
else length += 1;
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
if (char === '"' || char === "'") { quote = char; continue; }
|
|
214
|
+
if (char === '\\' && index + 1 < command.length) { index += 1; length += 1; continue; }
|
|
215
|
+
if (/\s/.test(char) || ';|&<>'.includes(char)) break;
|
|
216
|
+
if (char === '#' && index === targetStart) return null;
|
|
217
|
+
if ('()$`'.includes(char) || /[*?\[\]{}]/.test(char) || (char === '~' && length === 0)) return null;
|
|
218
|
+
length += 1;
|
|
219
|
+
}
|
|
220
|
+
return quote || length === 0 ? null : index;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function readRedirect(command, index, fileDescriptor) {
|
|
224
|
+
const char = command[index];
|
|
225
|
+
let operatorEnd = index + 1;
|
|
226
|
+
if (char === '<') {
|
|
227
|
+
if (command[index + 1] === '<' || command[index + 1] === '>') return null;
|
|
228
|
+
} else if (char === '>') {
|
|
229
|
+
if (command[index + 1] === '|') return null;
|
|
230
|
+
if (command[index + 1] === '>') operatorEnd += 1;
|
|
231
|
+
else if (command[index + 1] === '&') {
|
|
232
|
+
if (!fileDescriptor) return null;
|
|
233
|
+
let targetEnd = index + 2;
|
|
234
|
+
const digitsStart = targetEnd;
|
|
235
|
+
while (/[0-9]/.test(command[targetEnd] || '')) targetEnd += 1;
|
|
236
|
+
if (targetEnd === digitsStart ||
|
|
237
|
+
(targetEnd < command.length && !/\s/.test(command[targetEnd]) && !';|&<>'.includes(command[targetEnd]))) return null;
|
|
238
|
+
return targetEnd;
|
|
239
|
+
}
|
|
240
|
+
} else if (char === '&') {
|
|
241
|
+
if (command[index + 1] !== '>' || command[index + 2] === '>') return null;
|
|
242
|
+
operatorEnd += 1;
|
|
243
|
+
} else return null;
|
|
244
|
+
return literalRedirectTarget(command, operatorEnd);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Recognize a bounded top-level command list; redirects are removed before
|
|
248
|
+
// candidate matching so their filenames never become command words.
|
|
195
249
|
function splitSimpleCommandSegments(command) {
|
|
196
250
|
if (typeof command !== 'string' || command.length > LIMITS.command || /[\r\n\0]/.test(command)) return null;
|
|
197
251
|
const segments = [];
|
|
252
|
+
let segment = '';
|
|
198
253
|
let quote = null;
|
|
199
|
-
let
|
|
254
|
+
let tokenStart = true;
|
|
255
|
+
let afterSeparator = false;
|
|
200
256
|
for (let index = 0; index < command.length; index += 1) {
|
|
201
257
|
const char = command[index];
|
|
202
|
-
if (quote) {
|
|
203
|
-
|
|
204
|
-
|
|
258
|
+
if (quote === "'") {
|
|
259
|
+
segment += char;
|
|
260
|
+
if (char === "'") quote = null;
|
|
261
|
+
tokenStart = false;
|
|
262
|
+
afterSeparator = false;
|
|
205
263
|
continue;
|
|
206
264
|
}
|
|
207
|
-
if (
|
|
208
|
-
|
|
209
|
-
|
|
265
|
+
if (quote === '"') {
|
|
266
|
+
if (char === '"') { segment += char; quote = null; tokenStart = false; afterSeparator = false; continue; }
|
|
267
|
+
if (char === '\\' && index + 1 < command.length) { segment += char + command[++index]; afterSeparator = false; continue; }
|
|
268
|
+
if (char === '`' || (char === '$' && command[index + 1] === '(')) return null;
|
|
269
|
+
segment += char;
|
|
270
|
+
afterSeparator = false;
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
if (char === '"' || char === "'") { segment += char; quote = char; tokenStart = false; afterSeparator = false; continue; }
|
|
274
|
+
if (char === '\\' && index + 1 < command.length) { segment += char + command[++index]; tokenStart = false; afterSeparator = false; continue; }
|
|
275
|
+
if (char === '`' || (char === '$' && command[index + 1] === '(') || '()'.includes(char)) return null;
|
|
276
|
+
if (char === '#' && tokenStart) {
|
|
277
|
+
if (afterSeparator) return null;
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
if (/\s/.test(char)) { segment += char; tokenStart = true; continue; }
|
|
281
|
+
|
|
282
|
+
let descriptorEnd = index;
|
|
283
|
+
if (tokenStart && /[0-9]/.test(char)) {
|
|
284
|
+
while (/[0-9]/.test(command[descriptorEnd] || '')) descriptorEnd += 1;
|
|
285
|
+
if (command[descriptorEnd] === '>' || command[descriptorEnd] === '<') {
|
|
286
|
+
const end = readRedirect(command, descriptorEnd, true);
|
|
287
|
+
if (end === null) return null;
|
|
288
|
+
index = end - 1;
|
|
289
|
+
tokenStart = true;
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
if (char === '<' || char === '>') {
|
|
294
|
+
const end = readRedirect(command, index, false);
|
|
295
|
+
if (end === null) return null;
|
|
296
|
+
index = end - 1;
|
|
297
|
+
tokenStart = true;
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
if (char === '&' && command[index + 1] === '>') {
|
|
301
|
+
const end = readRedirect(command, index, false);
|
|
302
|
+
if (end === null) return null;
|
|
303
|
+
index = end - 1;
|
|
304
|
+
tokenStart = true;
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
|
|
210
308
|
let separatorLength = 0;
|
|
211
309
|
if (char === ';') separatorLength = 1;
|
|
212
|
-
else if (char === '&'
|
|
213
|
-
if (command[index + 1] !==
|
|
310
|
+
else if (char === '&') {
|
|
311
|
+
if (command[index + 1] !== '&' || command[index + 2] === '&') return null;
|
|
214
312
|
separatorLength = 2;
|
|
313
|
+
} else if (char === '|') {
|
|
314
|
+
if (command[index + 1] === '&' || command[index + 2] === '|') return null;
|
|
315
|
+
separatorLength = command[index + 1] === '|' ? 2 : 1;
|
|
215
316
|
}
|
|
216
|
-
if (
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
317
|
+
if (separatorLength) {
|
|
318
|
+
const current = segment.trim();
|
|
319
|
+
if (!current || segments.length >= LIMITS.commandSegments - 1) return null;
|
|
320
|
+
segments.push(current);
|
|
321
|
+
segment = '';
|
|
322
|
+
index += separatorLength - 1;
|
|
323
|
+
tokenStart = true;
|
|
324
|
+
afterSeparator = true;
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
if (char === '&' || char === '|') return null;
|
|
328
|
+
segment += char;
|
|
329
|
+
tokenStart = false;
|
|
330
|
+
afterSeparator = false;
|
|
222
331
|
}
|
|
223
332
|
if (quote) return null;
|
|
224
|
-
const finalSegment =
|
|
225
|
-
if (
|
|
226
|
-
|
|
333
|
+
const finalSegment = segment.trim();
|
|
334
|
+
if (afterSeparator) return null;
|
|
335
|
+
if (finalSegment) {
|
|
336
|
+
if (segments.length >= LIMITS.commandSegments) return null;
|
|
337
|
+
segments.push(finalSegment);
|
|
338
|
+
}
|
|
227
339
|
return segments;
|
|
228
340
|
}
|
|
229
341
|
|
|
@@ -139,7 +139,13 @@ function processState(payload) {
|
|
|
139
139
|
const isDirectTool = toolName === 'Edit' || toolName === 'Write' || toolName === 'apply_patch';
|
|
140
140
|
if (isDirectTool && (agy
|
|
141
141
|
? agyDirectMutationInWorkspace(payload, cwdOf(payload, root), root, toolName)
|
|
142
|
-
: directMutationInWorkspace(payload, cwdOf(payload, root), root)))
|
|
142
|
+
: directMutationInWorkspace(payload, cwdOf(payload, root), root))) {
|
|
143
|
+
state.lastEditAt = Date.now();
|
|
144
|
+
if (agy) {
|
|
145
|
+
const revision = Number.isSafeInteger(state.agyDirectMutationRevision) ? state.agyDirectMutationRevision : 0;
|
|
146
|
+
if (revision < Number.MAX_SAFE_INTEGER) state.agyDirectMutationRevision = revision + 1;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
143
149
|
const isShell = toolName === 'Bash' || toolName === 'PowerShell' || toolName === 'exec_command';
|
|
144
150
|
const command = (payload.tool_input && payload.tool_input.command) || '';
|
|
145
151
|
if (isShell) {
|