fix(node): retry rejected prev_chain_hash within a bounded submit budget; release 2.0.1

Claude-Session: https://claude.ai/code/session_01QoUjdc7HGKTsgj5GhG8HvF
This commit is contained in:
雷电芽衣 committed 2026-09-02 03:39:10 -04:00
1 parent f371d8fc89
commit 85c2d2d491
4 files changed
+186 -54

No files matched your search

+113
View File
@@ -1,5 +1,6 @@
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import http from 'node:http';
import path from 'node:path';
import test from 'node:test';
import {
@@ -143,3 +144,115 @@ test('Claude runtime failures stay observable while guard and audit delivery rem
await fixture.close();
}
});
function mismatchServer(respond) {
const submissions = [];
const server = http.createServer((request, response) => {
let raw = '';
request.on('data', (chunk) => { raw += chunk; });
request.on('end', () => {
if (request.url !== '/v1/operations') {
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify({ agent: { status: 'active' } }));
return;
}
submissions.push(JSON.parse(raw));
const reply = respond(submissions) ?? mismatch(String(submissions.length).padStart(43, 'A'), 'x');
const send = () => {
response.writeHead(reply.status, { 'Content-Type': 'application/json' });
response.end(JSON.stringify(reply.body));
};
if (api.slow) setTimeout(send, 2600); else send();
});
});
const api = { submissions, slow: false };
return new Promise((resolve) => server.listen(0, '127.0.0.1', () => resolve(Object.assign(api, {
baseUrl: 'http://127.0.0.1:' + server.address().port,
close: () => new Promise((done) => { server.closeAllConnections?.(); server.close(done); }),
}))));
}
function mismatch(expected, actual) {
return {
status: 400,
body: { error: { code: 'PREV_HASH_MISMATCH', message: 'Expected prev_chain_hash "' + expected + '", got "' + actual + '".' } },
};
}
test('Claude audit retries a rejected prev_chain_hash with the server value', async () => {
const expected = 'Rxlf4j36C3KvIQ3hWuOkX698BR5iDypUFuB70JjEuvM';
const api = await mismatchServer((submissions) => (submissions.length === 1
? mismatch(expected, submissions[0].prev_chain_hash)
: { status: 202, body: { receipt: { seq_no: 2 } } }));
const fixture = await createFixture({ baseUrl: api.baseUrl });
try {
assert.equal((await fixture.install()).code, 0);
const { settings } = await readSettings(fixture.settingsPath);
const audit = managedHandler(settings, 'PostToolUse');
const result = await runClaudeHook(
audit,
JSON.stringify(officialPayload('PostToolUse', { tool_response: { stdout: 'ok' } })),
fixture,
);
assert.equal(result.code, 0, result.stderr);
assert.equal(api.submissions.length, 2);
assert.equal(api.submissions[1].prev_chain_hash, expected);
assert.notEqual(api.submissions[1].operation_id, api.submissions[0].operation_id);
assert.notEqual(api.submissions[1].nonce, api.submissions[0].nonce);
const state = JSON.parse(await readFile(path.join(fixture.agentDir, 'chain-state.json'), 'utf-8'));
assert.equal(state.prev_chain_hash, api.submissions[1].chain_hash);
assert.match(await readFile(path.join(fixture.agentDir, 'error.log'), 'utf-8'), /resynced to server: Rxlf/);
} finally {
await fixture.close();
await api.close();
}
});
test('Claude audit stops after five rejected prev_chain_hash attempts', async () => {
const api = await mismatchServer((submissions) => (
mismatch(String(submissions.length).padStart(43, 'A'), submissions.at(-1).prev_chain_hash)
));
const fixture = await createFixture({ baseUrl: api.baseUrl });
try {
assert.equal((await fixture.install()).code, 0);
const { settings } = await readSettings(fixture.settingsPath);
const audit = managedHandler(settings, 'PostToolUse');
const result = await runClaudeHook(
audit,
JSON.stringify(officialPayload('PostToolUse', { tool_response: { stdout: 'ok' } })),
fixture,
);
assert.equal(result.code, 0, result.stderr);
assert.equal(api.submissions.length, 5);
assert.equal(api.submissions[4].prev_chain_hash, '4'.padStart(43, 'A'));
assert.match(await readFile(path.join(fixture.agentDir, 'error.log'), 'utf-8'), /rejected prev_chain_hash 5 times/);
} finally {
await fixture.close();
await api.close();
}
});
test('Claude audit stops retrying when the submit budget is spent', async () => {
const api = await mismatchServer(() => null);
api.slow = true;
const fixture = await createFixture({ baseUrl: api.baseUrl });
try {
assert.equal((await fixture.install()).code, 0);
const { settings } = await readSettings(fixture.settingsPath);
const audit = managedHandler(settings, 'PostToolUse');
const started = Date.now();
const result = await runClaudeHook(
audit,
JSON.stringify(officialPayload('PostToolUse', { tool_response: { stdout: 'ok' } })),
fixture,
);
const elapsed = Date.now() - started;
assert.equal(result.code, 0, result.stderr);
assert.ok(elapsed < 7000, `hook ran ${elapsed}ms`);
assert.ok(api.submissions.length >= 2 && api.submissions.length <= 3, `${api.submissions.length} submissions`);
assert.match(await readFile(path.join(fixture.agentDir, 'error.log'), 'utf-8'), /aborted|retry budget exhausted/i);
} finally {
await fixture.close();
await api.close();
}
});