nomad / scripts /precompact.js
LucioLiu's picture
Initial release - mirrors github.com/LucioLiu/nomad
0110783 verified
Raw
History Blame Contribute Delete
2.22 kB
#!/usr/bin/env node
/**
* precompact.js — Nomad PreCompact hook (portable port of production
* `hooks-core/precompact-core.js`, parameterized per team-skeleton.md §3/§5).
*
* Best-effort reminder to flush progress before context gets compacted — the
* Stop hook (session-stop.js) is the primary flush mechanism, this is a
* secondary nudge, not a guarantee. Never blocks compaction.
*
* PreCompact does NOT support hookSpecificOutput.additionalContext on
* claude/codex (that schema is for SessionStart/UserPromptSubmit/PostToolUse/
* Stop) — only the top-level `systemMessage` field is honored, so this script
* always emits `{ systemMessage: ... }` on those platforms rather than routing
* through emit.buildSessionStartPayload.
*
* Usage: node precompact.js [--platform claude|codex|cursor]
*/
'use strict';
const emit = require('./lib/emit');
const cfg = require('./lib/config');
const messages = require('./lib/messages');
async function main() {
const platform = emit.getPlatform();
try {
const raw = await emit.readStdin(); // read in; content isn't otherwise used, only session_id for dedup
const sid = emit.extractSessionId(raw);
if (sid && emit.isDuplicateInvocation('PreCompact', sid)) {
process.exit(0);
return;
}
const cwd = emit.resolveCwd(raw, platform);
const teamRoot = cfg.resolveTeamRoot(cwd || process.cwd());
const config = cfg.loadTeamConfig(teamRoot);
const lang = cfg.resolveLanguage(config);
const msg = messages.get(lang);
emit.writeJson(emit.buildPreCompactPayload(msg.precompactMessage, platform));
} catch (e) {
// Never block compaction: emit nothing on stdout. But surface an unexpected
// exception on stderr rather than swallowing it silently — stderr is not
// part of the compaction flow, so this keeps the "never block compaction"
// contract while still leaving a trace when something genuinely broke.
process.stderr.write('[precompact] non-fatal error, no message emitted (compaction not blocked): ' + ((e && e.stack) || e) + '\n');
}
process.exit(0);
}
if (require.main === module) {
main();
}
module.exports = {};