fix(unslop-hook): the Stop hook records the last prompt it checked per session in <log>.stop.json and waits while the transcript still ends with that turn, so a Stop with no last_assistant_message, or one whose reply has the last turn's text, no longer checks the last turn as this one; the 2 s wait stays (#1873, lane triage-p3s)

This commit is contained in:
jason.woltje committed 2026-11-18 10:52:53 -06:00
1 parent b39d5e2ff3
commit 80ce06851d
3 files changed
+113 -20

No files matched your search

+13 -6
View File
@@ -114,13 +114,20 @@ lists.
- The extension's per-reply dedupe is in memory. After a restart the same
reply can get a second notice.
- Claude Code can run the Stop hook before it writes the turn to the
transcript (seen on 2.1.319 to 2.1.325, on any turn). The hook reads the
transcript again until its last reply matches `last_assistant_message`,
for up to 2 s. If it never does, the hook checks `last_assistant_message`
transcript (seen on 2.1.319 to 2.1.325, on any turn). The Stop hook keeps,
per session id, the uuid of the last prompt whose turn it checked, in
`<log>.stop.json` next to its log. A transcript is stale while its last
prompt has that uuid, or while its last reply does not match
`last_assistant_message`. The hook reads it again until it is not, for up
to 2 s. If it never catches up, the hook checks `last_assistant_message`
alone, logs an empty `entryId` and `late: true`, and the turn's earlier
replies go unchecked. Two stale cases remain: a hook input with no
`last_assistant_message`, and a reply whose text equals the last turn's
last reply. Either can log the last turn's replies again.
replies go unchecked; with no `last_assistant_message` it checks nothing.
Two cases can still log a turn twice. After a late fallback the state
keeps the prompt before, so a later Stop with no `last_assistant_message`
can check the fallback turn's replies again. A second Stop in one turn,
after a Stop hook that blocks, waits 2 s and then checks
`last_assistant_message` alone, late. The state file sits in the run
directory with the log, so it is the session's own record too.
- The word/phrase lists are the mechanical subset of ms-unslop only, keyed to
pattern ids in lists.json. Style judgments (voice, rhythm, structure) stay in
the skill, not the gate.
+52 -14
View File
@@ -77,38 +77,75 @@ function check(text, at) {
}
}
// The assistant messages with text since the transcript's last prompt.
function turnReplies(path) {
// The transcript's last prompt (its uuid, or null) and the assistant messages with text since it.
function lastTurn(path) {
const list = entries(path);
let start = -1;
list.forEach((e, i) => { if (isPrompt(e)) start = i; });
return replies(list.slice(start + 1));
const uuid = start >= 0 && typeof list[start].uuid === "string" && list[start].uuid ? list[start].uuid : null;
return { prompt: uuid, replies: replies(list.slice(start + 1)) };
}
// The Stop hook's own state, next to its log: per session id, the uuid of the last prompt whose
// turn it checked. At most STATE_MAX sessions are kept, the latest last.
const STATE = LOG ? `${LOG}.stop.json` : null;
const STATE_MAX = 64;
function stopState() {
if (!STATE) return {};
try {
const value = JSON.parse(fs.readFileSync(STATE, "utf8"));
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
} catch { return {}; }
}
function recordStop(session, prompt) {
if (!STATE) return;
const state = stopState();
delete state[session];
state[session] = prompt;
const keys = Object.keys(state);
for (const k of keys.slice(0, Math.max(0, keys.length - STATE_MAX))) delete state[k];
const tmp = `${STATE}.${process.pid}`;
try {
fs.writeFileSync(tmp, JSON.stringify(state) + "\n");
fs.renameSync(tmp, STATE);
} catch {
try { fs.rmSync(tmp, { force: true }); } catch {}
}
}
// Claude Code can run the Stop hook before it writes the turn to the transcript, so the
// transcript may still end with the last turn. Read it again until its last reply is the one
// the hook input names, for up to STOP_WAIT_MS (the hook's own limit is 10 s). A transcript that
// never catches up gives last_assistant_message alone, logged with late: true so a release where
// it never matches shows in the log.
// Two stale cases remain: an input with no last_assistant_message is not waited for, and a reply
// whose text equals the last turn's last reply matches a stale transcript at once. Either can log
// the last turn's replies again.
// transcript may still end with the last turn. It is stale while its last prompt is the one the
// hook last checked for this session, or while its last reply is not the one the hook input
// names in last_assistant_message. Read it again until it is not, for up to STOP_WAIT_MS (the
// hook's own limit is 10 s). A transcript that never catches up gives last_assistant_message
// alone, logged with late: true so a release where it never matches shows in the log; with no
// last_assistant_message there is no reply to check, and nothing is logged.
// After a late fallback the turn's prompt uuid is not known, so the state keeps the one before:
// a later Stop with no last_assistant_message can then log that turn's replies once more. So can
// a second Stop in one turn (a Stop hook that blocks makes Claude go on with no new prompt): its
// transcript is stale by the uuid rule, so it waits and then logs only last_assistant_message.
const STOP_WAIT_MS = 2000;
const flat = (s) => s.replace(/\s+/g, "");
const pause = (ms) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
function stop(input) {
const told = typeof input.last_assistant_message === "string" && input.last_assistant_message.trim() ? input.last_assistant_message : null;
const session = typeof input.session_id === "string" ? input.session_id : "";
const state = stopState();
const seen = Object.hasOwn(state, session) && typeof state[session] === "string" ? state[session] : null;
const stale = (t) => (t.prompt !== null && t.prompt === seen) || (told !== null && !(t.replies.length && flat(t.replies.at(-1).text) === flat(told)));
const deadline = Date.now() + STOP_WAIT_MS;
let turn = turnReplies(input.transcript_path), late = false;
while (told !== null && !(turn.length && flat(turn.at(-1).text) === flat(told))) {
let t = lastTurn(input.transcript_path);
let turn = t.replies, late = false;
while (stale(t)) {
if (Date.now() >= deadline) {
turn = [{ id: "", text: told }];
turn = told === null ? [] : [{ id: "", text: told }];
late = true;
break;
}
pause(25);
turn = turnReplies(input.transcript_path);
t = lastTurn(input.transcript_path);
turn = t.replies;
}
for (const r of turn) {
const result = check(r.text, "stop");
@@ -117,6 +154,7 @@ function stop(input) {
if (result.clean) log({ ev: "checked", clean: true, entryId: r.id, charsChecked: result.charsChecked, ...extra });
else log({ ev: "flagged", clean: false, entryId: r.id, charsChecked: result.charsChecked, findings: result.findings, ...extra });
}
if (!late && t.prompt !== null) recordStop(session, t.prompt);
}
// The entry ids the log already holds a notice for.
+48
View File
@@ -340,6 +340,54 @@ t("claude-hook: Stop with a transcript that never catches up checks last_assista
assert.deepStrictEqual(events(now).map((e) => [e.ev, e.entryId, e.late]), [["checked", "a1", undefined]]);
});
// The Stop hook keeps, per session and next to its log, the last prompt whose turn it checked. A
// transcript that still ends with that prompt is stale, even with no last_assistant_message or
// with a reply whose text equals the last turn's.
const appendLater = (from, to) => require("node:child_process").spawn("sh", ["-c", 'sleep 0.3; cat "$1" >> "$2"', "sh", from, to], { stdio: "ignore" });
t("claude-hook: Stop with no last_assistant_message does not check a turn it checked again, and waits for the late tail", () => {
const transcript = writeTmp("transcript-untold.jsonl", jsonl(lastTurn));
const log = tmp("hook-untold.jsonl");
const env = { MOSAIC_UNSLOP_LOG: log };
assert.strictEqual(hook("stop", { session_id: "s1", transcript_path: transcript }, env).status, 0);
assert.deepStrictEqual(events(log).map((e) => [e.ev, e.entryId]), [["checked", "a1"]]);
// The next turn's Stop runs before its turn reaches the transcript.
const writer = appendLater(writeTmp("transcript-untold-tail.jsonl", jsonl([
{ type: "user", uuid: "p2", message: { role: "user", content: "and now?" } },
line("assistant", "m2", "a2", [{ type: "text", text: TELL_REPLY }]),
])), transcript);
const r = hook("stop", { session_id: "s1", transcript_path: transcript }, env);
writer.kill();
assert.strictEqual(r.status, 0, r.stderr);
assert.deepStrictEqual(events(log).map((e) => [e.ev, e.entryId]), [["checked", "a1"], ["flagged", "a2"]]);
// A transcript that never catches up gives nothing to check: nothing is logged.
assert.strictEqual(hook("stop", { session_id: "s1", transcript_path: transcript }, env).status, 0);
assert.strictEqual(events(log).length, 2);
// The state is per session: another session on the same log has checked nothing yet.
hook("stop", { session_id: "s2", transcript_path: transcript }, env);
assert.deepStrictEqual(events(log).slice(2).map((e) => [e.ev, e.entryId]), [["flagged", "a2"]]);
});
t("claude-hook: Stop whose reply has the last turn's text does not take the stale transcript for this turn", () => {
const transcript = writeTmp("transcript-same.jsonl", jsonl(lastTurn));
const log = tmp("hook-same.jsonl");
const env = { MOSAIC_UNSLOP_LOG: log };
const told = { session_id: "s1", transcript_path: transcript, last_assistant_message: "The build is green." };
assert.strictEqual(hook("stop", told, env).status, 0);
assert.deepStrictEqual(events(log).map((e) => [e.ev, e.entryId]), [["checked", "a1"]]);
const writer = appendLater(writeTmp("transcript-same-tail.jsonl", jsonl([
{ type: "user", uuid: "p2", message: { role: "user", content: "still?" } },
line("assistant", "m2", "a2", [{ type: "text", text: "The build is green." }]),
])), transcript);
const r = hook("stop", told, env);
writer.kill();
assert.strictEqual(r.status, 0, r.stderr);
assert.deepStrictEqual(events(log).map((e) => [e.ev, e.entryId]), [["checked", "a1"], ["checked", "a2"]]);
// A transcript that never catches up gives last_assistant_message alone, late.
hook("stop", told, env);
assert.deepStrictEqual(events(log).slice(2).map((e) => [e.ev, e.entryId, e.late]), [["checked", "", true]]);
});
let failed = 0;
for (const [name, ok] of results) { console.log(`${ok ? "PASS" : "FAIL"} ${name}`); if (!ok) failed++; }
console.log(`${results.length - failed}/${results.length} passed`);