fix(hooks): drop classifier notices from LLM summary input

The safety-classifier notice filter only covered the mechanical summary.
When generateSessionSummary runs (low context or message interval),
extractConversationText read the raw transcript and passed the notice to
the LLM as a user turn, so it could still reach saved context.

Move the check into one shared isClassifierNotice() in
scripts/lib/llm-summary.js and use it in both userMessageText() and
extractConversationText(). Add LLM-path regression tests, and split the
session-end classifier-notice tests into small functions (<50 lines).

Refs #3867

Assisted-by: Claude <noreply@anthropic.com>
This commit is contained in:
Rahul Selvaraj committed 2026-11-05 18:06:51 +08:00
1 parent a08d5da8c9
commit 846a561a7b
4 files changed
+85 -72

No files matched your search

+2 -4
View File
@@ -12,7 +12,7 @@
const path = require('path');
const fs = require('fs');
const { getSessionsDir, getDateString, getTimeString, getSessionIdShort, sanitizeSessionId, getProjectName, getRepoIdentity, ensureDir, readFile, writeFile, runCommand, stripAnsi, log } = require('../lib/utils');
const { generateSessionSummary, getContextRemainingPct, getContextThreshold } = require('../lib/llm-summary');
const { generateSessionSummary, getContextRemainingPct, getContextThreshold, isClassifierNotice } = require('../lib/llm-summary');
const SUMMARY_START_MARKER = '<!-- ECC:SUMMARY:START -->';
const SUMMARY_END_MARKER = '<!-- ECC:SUMMARY:END -->';
@@ -20,8 +20,6 @@ const SESSION_SEPARATOR = '\n---\n';
// Harness noise: local command echoes, caveats, system reminders.
const HARNESS_NOISE = /^<(local-command-caveat|local-command-stdout|command-name|command-message|command-args|system-reminder|task-notification)/i;
// Harness notice shown after a safety-classifier stop; it is not a user ask (#3867).
const CLASSIFIER_NOTICE = /^Your response above was stopped by a safety classifier/i;
const TRANSCRIPT_UUID = /([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.jsonl$/i;
const NEXT_SESSION_FOOTER = '\n\n### Notes for Next Session\n-\n\n### Context to Load\n```\n[relevant files]\n```';
const EMPTY_SESSION_BODY = '## Current State\n\n[Session context goes here]\n\n### Completed\n- [ ]\n\n### In Progress\n- [ ]\n\n### Notes for Next Session\n-\n\n### Context to Load\n```\n[relevant files]\n```';
@@ -40,7 +38,7 @@ function userMessageText(entry) {
// `isMeta` is also used for genuine channel- and plugin-originated
// human prompts. Exclude known structured noise instead of
// discarding every metadata-marked user turn.
return HARNESS_NOISE.test(cleaned) || CLASSIFIER_NOTICE.test(cleaned) ? '' : cleaned;
return HARNESS_NOISE.test(cleaned) || isClassifierNotice(cleaned) ? '' : cleaned;
}
/**
+13 -2
View File
@@ -20,6 +20,8 @@ const MAX_TURNS = 25;
const LLM_TIMEOUT_MS = 90000;
// Leave the Stop hook time to persist its mechanical fallback after a timeout.
const HOOK_FINISH_RESERVE_MS = 1000;
// Harness notice shown after a safety-classifier stop; it is not a user ask (#3867).
const CLASSIFIER_NOTICE = /^Your response above was stopped by a safety classifier/i;
function getLLMModel() {
return process.env.ECC_LLM_SUMMARY_MODEL || 'haiku';
@@ -30,6 +32,15 @@ function getContextThreshold() {
return Number.isFinite(raw) && raw > 0 && raw <= 100 ? raw : 20;
}
/**
* True when a user-turn text is the harness notice left after a
* safety-classifier stop. Anchored to the start so user messages that only
* quote the notice are kept.
*/
function isClassifierNotice(text) {
return CLASSIFIER_NOTICE.test(text);
}
/**
* Extract the last MAX_TURNS user+assistant turns from a JSONL transcript.
* Returns null when the transcript is missing or has no parseable turns.
@@ -63,7 +74,7 @@ function extractConversationText(transcriptPath) {
.join(' ')
: '';
const cleaned = text.replace(/\n+/g, ' ').trim();
if (cleaned) {
if (cleaned && !isClassifierNotice(cleaned)) {
turns.push({ role: 'User', text: cleaned.slice(0, 400) });
}
}
@@ -201,4 +212,4 @@ function generateSessionSummary(transcriptPath, { onSkip = () => {} } = {}) {
}
}
module.exports = { generateSessionSummary, extractConversationText, getContextRemainingPct, getContextThreshold, getLLMModel };
module.exports = { generateSessionSummary, extractConversationText, isClassifierNotice, getContextRemainingPct, getContextThreshold, getLLMModel };