harness-reflex 0.1.0 → 0.3.0
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/CHANGELOG.md +20 -0
- package/README.md +103 -23
- package/USER_GUIDE.md +121 -43
- package/dist/adapters/agy.js +1 -1
- package/dist/adapters/agy.js.map +1 -1
- package/dist/adapters/codex.d.ts +133 -4
- package/dist/adapters/codex.js +325 -2
- package/dist/adapters/codex.js.map +1 -1
- package/dist/backend/defaults.d.ts +31 -0
- package/dist/backend/defaults.js +32 -0
- package/dist/backend/defaults.js.map +1 -0
- package/dist/backend/jev.js +6 -5
- package/dist/backend/jev.js.map +1 -1
- package/dist/backend/mock.d.ts +12 -1
- package/dist/backend/mock.js +34 -0
- package/dist/backend/mock.js.map +1 -1
- package/dist/backend/simple-jev.js +5 -4
- package/dist/backend/simple-jev.js.map +1 -1
- package/dist/backend/types.d.ts +34 -0
- package/dist/backend/types.js.map +1 -1
- package/dist/cli/index.js +5 -3
- package/dist/cli/index.js.map +1 -1
- package/dist/core/closing.d.ts +90 -0
- package/dist/core/closing.js +111 -0
- package/dist/core/closing.js.map +1 -0
- package/dist/core/evidence.d.ts +119 -0
- package/dist/core/evidence.js +209 -0
- package/dist/core/evidence.js.map +1 -0
- package/dist/core/judge.d.ts +221 -0
- package/dist/core/judge.js +251 -0
- package/dist/core/judge.js.map +1 -0
- package/dist/core/orchestrate-closing.d.ts +237 -0
- package/dist/core/orchestrate-closing.js +250 -0
- package/dist/core/orchestrate-closing.js.map +1 -0
- package/dist/core/orchestrate-correction.d.ts +209 -0
- package/dist/core/orchestrate-correction.js +141 -0
- package/dist/core/orchestrate-correction.js.map +1 -0
- package/dist/core/orchestrate-host.d.ts +225 -0
- package/dist/core/orchestrate-host.js +80 -0
- package/dist/core/orchestrate-host.js.map +1 -0
- package/dist/core/orchestrate-stagnation.d.ts +259 -0
- package/dist/core/orchestrate-stagnation.js +270 -0
- package/dist/core/orchestrate-stagnation.js.map +1 -0
- package/dist/core/orchestrate.d.ts +357 -0
- package/dist/core/orchestrate.js +418 -0
- package/dist/core/orchestrate.js.map +1 -0
- package/dist/core/policy.d.ts +122 -0
- package/dist/core/policy.js +102 -0
- package/dist/core/policy.js.map +1 -0
- package/dist/core/prune-plan.d.ts +141 -0
- package/dist/core/prune-plan.js +390 -0
- package/dist/core/prune-plan.js.map +1 -0
- package/dist/core/score.d.ts +2 -2
- package/dist/core/score.js.map +1 -1
- package/dist/core/text.d.ts +36 -0
- package/dist/core/text.js +66 -0
- package/dist/core/text.js.map +1 -0
- package/dist/hook/cli.d.ts +9 -0
- package/dist/hook/cli.js +52 -0
- package/dist/hook/cli.js.map +1 -0
- package/dist/hook/contract.d.ts +125 -0
- package/dist/hook/contract.js +159 -0
- package/dist/hook/contract.js.map +1 -0
- package/dist/hook/decision.d.ts +97 -0
- package/dist/hook/decision.js +955 -0
- package/dist/hook/decision.js.map +1 -0
- package/dist/hook/memory-store.d.ts +81 -0
- package/dist/hook/memory-store.js +157 -0
- package/dist/hook/memory-store.js.map +1 -0
- package/dist/hook/payload.d.ts +55 -0
- package/dist/hook/payload.js +129 -0
- package/dist/hook/payload.js.map +1 -0
- package/dist/hook/run.d.ts +48 -0
- package/dist/hook/run.js +218 -0
- package/dist/hook/run.js.map +1 -0
- package/dist/hook/store-port.d.ts +72 -0
- package/dist/hook/store-port.js +2 -0
- package/dist/hook/store-port.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/ports/audit.d.ts +18 -0
- package/dist/ports/audit.js +11 -0
- package/dist/ports/audit.js.map +1 -0
- package/dist/ports/clock.d.ts +14 -0
- package/dist/ports/clock.js +11 -0
- package/dist/ports/clock.js.map +1 -0
- package/dist/ports/decision.d.ts +75 -0
- package/dist/ports/decision.js +2 -0
- package/dist/ports/decision.js.map +1 -0
- package/dist/ports/index.d.ts +14 -0
- package/dist/ports/index.js +2 -0
- package/dist/ports/index.js.map +1 -0
- package/dist/ports/log.d.ts +22 -0
- package/dist/ports/log.js +12 -0
- package/dist/ports/log.js.map +1 -0
- package/dist/ports/state.d.ts +46 -0
- package/dist/ports/state.js +13 -0
- package/dist/ports/state.js.map +1 -0
- package/dist/state/clock.d.ts +40 -0
- package/dist/state/clock.js +43 -0
- package/dist/state/clock.js.map +1 -0
- package/dist/state/file-store.d.ts +340 -0
- package/dist/state/file-store.js +849 -0
- package/dist/state/file-store.js.map +1 -0
- package/dist/state/paths.d.ts +41 -0
- package/dist/state/paths.js +73 -0
- package/dist/state/paths.js.map +1 -0
- package/docs/USER_GUIDE.en.md +79 -36
- package/docs/claude-code.md +281 -0
- package/docs/design/v0.2.0-harness-plugins.md +381 -0
- package/docs/design/v0.3.0-orchestration-unification.md +580 -0
- package/docs/dsh-plugin.md +54 -54
- package/docs/release-checklist-v0.1.0.md +32 -21
- package/docs/release-handbook-porting-notes.md +11 -11
- package/docs/release-handbook.md +55 -30
- package/docs/release-notes-v0.1.0.md +27 -14
- package/docs/release-notes-v0.3.0.md +152 -0
- package/docs/research/claude-code-harness.md +607 -0
- package/docs/research/pi-harness.md +621 -0
- package/package.json +16 -7
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prune-plan.js","sourceRoot":"","sources":["../../src/core/prune-plan.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,iFAAiF;AACjF,MAAM,CAAC,MAAM,gBAAgB,GAAG,GAAG,CAAC;AACpC,iFAAiF;AACjF,MAAM,CAAC,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAuEpC;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,cAAc,CAC5B,aAAqB,EACrB,cAAsB,EACtB,OAA0B;IAE1B,IAAI,cAAc,KAAK,CAAC,IAAI,cAAc,IAAI,aAAa,EAAE,CAAC;QAC5D,OAAO,aAAa,CAAC,aAAa,CAAC,CAAC;IACtC,CAAC;IACD,MAAM,MAAM,GAAG,IAA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I;QACP,WAAW,EAAE,QAAQ,CAAC,IAAI,CAAC,MAAM;QACjC,YAAY,EAAE,QAAQ,CAAC,MAAM,GAAG,QAAQ,CAAC,IAAI,CAAC,MAAM;QACpD,SAAS,EAAE,QAAQ,CAAC,SAAS;QAC7B,SAAS,EAAE,QAAQ,CAAC,SAAS;QAC7B,WAAW,EAAE,QAAQ,CAAC,WAAW;QACjC,IAAI,EAAE,QAAQ,CAAC,IAAI;KACpB,CAAC;AACJ,CAAC","sourcesContent":["/**\n * The deterministic floor of a judged reduction.\n *\n * A prune judgment proposes; this module disposes. Given the original payload,\n * the judged candidate, and the floor options, it decides whether the head /\n * extract / tail form replaces the candidate, composes that form out of whole\n * lines, and reports the size the installed text really has. Everything here is\n * pure arithmetic over strings: no host type, no retention sink, no audit.\n *\n * @module core/prune-plan\n */\n\n/** Default share of a floored reduction's context budget taken from the head. */\nexport const FLOOR_HEAD_CHARS = 400;\n/** Default share of a floored reduction's context budget taken from the tail. */\nexport const FLOOR_TAIL_CHARS = 400;\n\n/**\n * The floor decision of one reduction, computed from lengths alone.\n *\n * {@link pruneFloorPlan} returns the *decision*: whether the floor applies and\n * the size that decision was modelled on. {@link applyPruneFloor} returns the\n * same decision next to the size the composed text *really* has, because line\n * alignment and duplicate removal move that size. Callers account for the text\n * a mode really installs, so the composed numbers are the authoritative ones.\n */\nexport interface PruneFloorPlan {\n /** Whether the floor form replaces the judged candidate. */\n floored: boolean;\n /**\n * Whether the installed reduction fell short of the floor's promised share.\n *\n * True exactly when the composition could not build a form that both carries\n * `floorRatio` of the original and stays smaller than it, so the judged\n * candidate itself was installed. It is what separates \"nothing was reduced\"\n * from \"the reduction was real, but it did not reach the floor's promise\"; a\n * plan that never needed the floor reports `false`.\n */\n floorMissed: boolean;\n /** Characters the installed text carries: modelled by the plan, real once composed. */\n resultChars: number;\n /** Characters the installed form does not carry. */\n omittedChars: number;\n /** Context characters taken from the head of the original. */\n headChars: number;\n /** Context characters taken from the tail of the original. */\n tailChars: number;\n /**\n * Marker characters the installed form carries, summed over its sides.\n *\n * Zero when nothing is floored, one marker when only the mandatory locator side\n * fits, two when the share's budget can pay for a matching marker after the\n * candidate. Publishing it is what lets {@link applyPruneFloor} rebuild exactly\n * the form whose size the plan promised.\n */\n markerChars: number;\n /**\n * Whether a floor-missed install wanted the locator marker but could not carry it.\n *\n * The marker is what tells a reader that the reduction happened and where the\n * full payload lives, so a candidate that fits only *without* it is still\n * installed — the reduction is real — but the omission is recorded instead of\n * being silently dropped. Always `false` when `floorMissed` is false.\n */\n markerOmitted: boolean;\n}\n\n/** The floor options shared by the plan and the composition. */\nexport interface PruneFloorOptions {\n /** Lowest share of the original a reduction may keep. */\n floorRatio: number;\n /** Default split of the context budget, in characters. */\n headChars?: number;\n /** Default split of the context budget, in characters. */\n tailChars?: number;\n /**\n * Length of the marker the installed form carries per side.\n *\n * A caller that composes a locator-carrying form passes the real marker\n * length here; a caller that only accounts for it passes the same number. That\n * is what keeps the predicted size and the installed size identical in both\n * modes.\n */\n markerChars?: number;\n}\n\n/**\n * Decide the deterministic floor of one reduction from lengths alone.\n *\n * Lengths are enough because the floor form is fixed: context, marker, candidate,\n * marker, context. Deciding it this way is what lets `observe` account for the\n * *same* candidate `active` would install without building it.\n *\n * The floor is a *share*, not a suggestion. `pruneFloorRatio` says how much of\n * the original must survive, so the context budget is derived from it rather than\n * capped by the head/tail hints: a candidate that keeps 8% of a long payload is\n * not \"floored\" to 8% plus two small slices, it is widened until the installed\n * form really carries the promised share. The head/tail hints only decide how\n * that budget is split. A plan that cannot reach the share — because the marker\n * alone would eat the whole allowance — is refused, so a reduction never grows\n * the context.\n *\n * @param originalChars - length of the raw payload.\n * @param candidateChars - length of the judged candidate.\n * @param options - floor ratio, split hints, and marker length.\n * @returns the floor decision.\n */\nexport function pruneFloorPlan(\n originalChars: number,\n candidateChars: number,\n options: PruneFloorOptions\n): PruneFloorPlan {\n if (candidateChars === 0 || candidateChars >= originalChars) {\n return idleFloorPlan(originalChars);\n }\n const marker = Math.max(0, options.markerChars ?? 0);\n const head = Math.max(0, options.headChars ?? FLOOR_HEAD_CHARS);\n const tail = Math.max(0, options.tailChars ?? FLOOR_TAIL_CHARS);\n const share = Math.min(1, Math.max(0, options.floorRatio));\n const target = Math.ceil(originalChars * share);\n // The judged candidate is already the installed text when the floor does not\n // apply, and it carries no marker; only a deeper cut pays for the marker.\n if (candidateChars >= target) {\n return {\n floored: false,\n floorMissed: false,\n resultChars: candidateChars,\n omittedChars: originalChars - candidateChars,\n headChars: 0,\n tailChars: 0,\n markerChars: 0,\n markerOmitted: false\n };\n }\n // One marker side is mandatory: it carries the locator of the original. The\n // second is bought only when the share's budget leaves room for it. A candidate\n // that cannot even pay for the mandatory marker is still a real reduction, so\n // it is installed *without* the marker and the omission is recorded: dropping\n // it would trade a smaller answer for the whole payload over a missing line.\n if (candidateChars + marker >= originalChars) {\n return {\n floored: false,\n floorMissed: true,\n resultChars: candidateChars,\n omittedChars: originalChars - candidateChars,\n headChars: 0,\n tailChars: 0,\n markerChars: 0,\n markerOmitted: true\n };\n }\n let budget = Math.max(candidateChars + marker, Math.min(originalChars, target));\n let markerChars = marker;\n let context = budget - candidateChars - markerChars;\n if (context >= marker) {\n markerChars += marker;\n context -= marker;\n }\n const split = splitContext(context, head, tail);\n const resultChars = candidateChars + markerChars + context;\n if (resultChars >= originalChars) return idleFloorPlan(originalChars);\n return {\n floored: true,\n floorMissed: false,\n resultChars,\n omittedChars: originalChars - resultChars,\n headChars: split.head,\n tailChars: split.tail,\n markerChars,\n markerOmitted: false\n };\n}\n\n/**\n * Split one context budget between the head and the tail.\n *\n * The hints describe the *shape* of the context, not its ceiling, so a large\n * budget keeps their proportion instead of stopping at their sum. A zero-shaped\n * split degrades to an even one rather than dropping the context.\n * @param context - characters available for context.\n * @param head - head hint.\n * @param tail - tail hint.\n * @returns the two slice lengths, summing to the context.\n */\nfunction splitContext(context: number, head: number, tail: number): { head: number; tail: number } {\n const total = head + tail;\n const headChars = total <= 0 ? Math.ceil(context / 2) : Math.ceil((context * head) / total);\n return { head: Math.min(context, headChars), tail: context - Math.min(context, headChars) };\n}\n\n/** The plan of a reduction that is not happening. */\nfunction idleFloorPlan(originalChars: number): PruneFloorPlan {\n return {\n floored: false,\n floorMissed: false,\n resultChars: originalChars,\n omittedChars: 0,\n headChars: 0,\n tailChars: 0,\n markerChars: 0,\n markerOmitted: false\n };\n}\n\n/** The marker one floored reduction carries on each side of its candidate. */\nexport function pruneMarker(locator: string): string {\n return `\\n… [reflex prune: full original retrievable as ${locator}] …\\n`;\n}\n\n/**\n * The marker a floor-missed install appends after its candidate.\n *\n * The floored form opens with the marker because the head has just ended with a\n * newline. A candidate that already ends with one needs no blank line before the\n * marker, so only the leading newline is dropped in that case; the trailing\n * newline is the same one the floored form carries.\n * @param candidate - the judged candidate about to be installed.\n * @param marker - the locator marker.\n * @returns the suffix to append.\n */\nfunction pruneMarkerSuffix(candidate: string, marker: string): string {\n return candidate.endsWith('\\n') ? marker.slice(1) : marker;\n}\n\n/** Split one payload into whole lines, the terminator kept with its line. */\nfunction floorLines(text: string): string[] {\n return text.match(/[^\\n]*\\n|[^\\n]+$/g) ?? [];\n}\n\n/** The identity of one line for the duplicate test: its text without its terminator. */\nfunction floorLineIdentity(line: string): string {\n return line.replace(/\\r?\\n$/, '').replace(/\\r$/, '');\n}\n\n/**\n * How many whole lines of the head a budget can pay for.\n *\n * The head never crosses its budget and never stops inside a line: a cut that\n * cannot pay for the next line stops before it, so the kept prefix is always a\n * run of complete lines.\n * @param lines - the payload, split into whole lines.\n * @param budget - characters the head may spend.\n * @returns the number of lines to keep from the top.\n */\nfunction alignedHeadLines(lines: readonly string[], budget: number): number {\n let used = 0;\n let count = 0;\n for (const line of lines) {\n if (used + line.length > budget) break;\n used += line.length;\n count += 1;\n }\n return count;\n}\n\n/**\n * How many whole lines of the tail a budget needs.\n *\n * The tail is the *shortest* run of complete lines from the end that still\n * carries the budget. A payload whose lines are longer than the budget cannot\n * honour it with a line-aligned suffix; the caller refuses instead of cutting\n * one, because half a line is not a shorter answer but a broken one.\n * @param lines - the payload, split into whole lines.\n * @param budget - characters the tail must carry.\n * @returns the number of lines to keep from the bottom.\n */\nfunction alignedTailLines(lines: readonly string[], budget: number): number {\n if (budget <= 0) return 0;\n let used = 0;\n let count = 0;\n for (let index = lines.length - 1; index >= 0; index -= 1) {\n used += lines[index]!.length;\n count += 1;\n if (used >= budget) break;\n }\n return count;\n}\n\n/**\n * Drop the candidate lines the kept context already shows verbatim.\n *\n * The judgment reads the whole payload, so its extract repeats the lines a\n * reader can already see around it — `BEGIN`, `RESULT`, `END` in the trace that\n * prompted this. Repeating them is pure cost: the same line twice in one\n * message. Removal is per line and verbatim, so a diagnostic the context does\n * *not* carry always survives.\n * @param candidate - the judged extract.\n * @param head - the kept head.\n * @param tail - the kept tail.\n * @returns the candidate without the lines the context already shows.\n */\nfunction dedupeCandidateLines(candidate: string, head: string, tail: string): string {\n const seen = new Set([...floorLines(head), ...floorLines(tail)].map(floorLineIdentity));\n const lines = floorLines(candidate);\n if (seen.size === 0 || lines.length === 0) return candidate;\n return lines.filter((line) => !seen.has(floorLineIdentity(line))).join('');\n}\n\n/** One composed floor form and the sizes it really kept. */\ninterface ComposedFloor {\n text: string;\n headChars: number;\n tailChars: number;\n markerChars: number;\n}\n\n/**\n * Compose one floored form out of whole lines of the original.\n * @param lines - the original, split into whole lines.\n * @param headCount - lines kept from the top.\n * @param tailCount - lines kept from the bottom.\n * @param candidate - the judged extract.\n * @param marker - the locator marker.\n * @param secondMarker - the closing marker, or `''` when the share cannot pay for it.\n * @returns the installed text and the sizes it really carries.\n */\nfunction composeFloorLines(\n lines: readonly string[],\n headCount: number,\n tailCount: number,\n candidate: string,\n marker: string,\n secondMarker: string\n): ComposedFloor {\n const head = lines.slice(0, headCount).join('');\n const tail = tailCount === 0 ? '' : lines.slice(lines.length - tailCount).join('');\n const middle = dedupeCandidateLines(candidate, head, tail);\n // The marker opens with a newline so it sits on its own line after the head.\n // With no head there is nothing to separate it from, and a leading blank line\n // would be the first thing a reader sees.\n const lead = head.length === 0 ? marker.replace(/^\\n/, '') : marker;\n const closing = middle.length === 0 ? '' : secondMarker;\n return {\n text: `${head}${lead}${middle}${closing}${tail}`,\n headChars: head.length,\n tailChars: tail.length,\n markerChars: lead.length + closing.length\n };\n}\n\n/**\n * The plan of a floor the composition refused.\n *\n * A refusal is *not* a reason to throw the candidate away. The candidate is the\n * reduction the judgment already produced, and a floor that cannot widen it\n * without either breaking the share or growing past the original has simply\n * missed the floor, not invalidated the reduction: installing the candidate is\n * strictly better than carrying the whole payload. So the candidate is installed\n * with `floored: false` (no head/tail form was built) and `floorMissed: true`\n * (the promised share is not carried), and the sizes are booked against the text\n * that is really installed.\n *\n * The locator marker is appended when the candidate can still pay for it, so the\n * reader sees that a prune happened and where the full payload lives. When the\n * marker would push the install to the original's size or past it, the reduction\n * wins and the marker is omitted — recorded as `markerOmitted: true`, never\n * silently dropped.\n *\n * Only a candidate that is not actually smaller leaves the original untouched —\n * that is a real \"no reduction\", and the caller's own degradation path owns it.\n *\n * @param original - the raw payload.\n * @param candidate - the judged reduction the composition could not widen.\n * @param marker - the locator marker a floor-missed install wants to carry.\n * @returns the text to install and the floor decision that says why.\n */\nfunction refusedFloor(original: string, candidate: string, marker: string): PruneFloorPlan & { text: string } {\n if (candidate.length >= original.length) {\n return { ...idleFloorPlan(original.length), text: original };\n }\n const suffix = pruneMarkerSuffix(candidate, marker);\n if (candidate.length + suffix.length < original.length) {\n return {\n floored: false,\n floorMissed: true,\n resultChars: candidate.length + suffix.length,\n omittedChars: original.length - candidate.length - suffix.length,\n headChars: 0,\n tailChars: 0,\n markerChars: suffix.length,\n markerOmitted: false,\n text: `${candidate}${suffix}`\n };\n }\n return {\n floored: false,\n floorMissed: true,\n resultChars: candidate.length,\n omittedChars: original.length - candidate.length,\n headChars: 0,\n tailChars: 0,\n markerChars: 0,\n markerOmitted: true,\n text: candidate\n };\n}\n\n/**\n * Enforce the deterministic floor of a judged reduction.\n *\n * The judgment proposes; this floor disposes. A candidate that cut deeper than\n * {@link ResolvedInterventionsConfig.pruneFloorRatio} of the original is replaced\n * by a form that keeps the head and the tail — the two places a compiler error, a\n * stack-trace summary, and a final result usually are — around the judgment's own\n * extract, with explicit markers that name where the full original was retained.\n * A candidate that is not actually smaller is refused outright, so a \"reduction\"\n * can never grow the context.\n *\n * The context is composed out of *whole lines* of the original, never out of\n * character slices: a cut that lands mid-line leaves the reader a fragment like\n * `=] progress 092`, which is not a shorter answer but a broken one. The head is\n * the longest run of complete lines its budget can pay for, the tail the shortest\n * run of complete lines that still carries its own, and any candidate line the\n * kept context already shows verbatim is dropped as a duplicate. Alignment and\n * removal move the real size, so the decision's modelled size is replaced here by\n * the size the text really has: a form that would fall short of the promised\n * share, or that would not be smaller than the original at all, is refused. A\n * refusal installs the judgment's own candidate with `floorMissed: true` rather\n * than the original — the candidate is a real reduction, and dropping it would\n * trade a smaller answer for the whole payload just because the floor form could\n * not be built.\n *\n * @param original - the raw payload.\n * @param candidate - the judged reduction.\n * @param options - floor options plus the locator the replacement publishes.\n * @returns the text to install, the floor decision, and the sizes involved.\n */\nexport function applyPruneFloor(\n original: string,\n candidate: string,\n options: PruneFloorOptions & { locator: string }\n): PruneFloorPlan & { text: string } {\n const marker = pruneMarker(options.locator);\n const plan = pruneFloorPlan(original.length, candidate.length, { ...options, markerChars: marker.length });\n if (!plan.floored) {\n return { ...plan, text: plan.resultChars === candidate.length ? candidate : original };\n }\n const lines = floorLines(original);\n const share = Math.min(1, Math.max(0, options.floorRatio));\n const target = Math.ceil(original.length * share);\n const secondMarker = plan.markerChars > marker.length ? marker : '';\n let headCount = alignedHeadLines(lines, plan.headChars);\n if (headCount === 0 && plan.headChars > 0 && lines.length > 0) {\n // A first line longer than the head budget: taking it whole keeps a real line\n // where a cut could only produce a fragment. Whether the enlarged form is\n // installable at all is decided by the size checks below.\n headCount = 1;\n }\n let tailCount = alignedTailLines(lines, plan.tailChars);\n if (headCount + tailCount > lines.length) return refusedFloor(original, candidate, marker);\n let composed = composeFloorLines(lines, headCount, tailCount, candidate, marker, secondMarker);\n // The share is a promise, and rounding to a boundary or dropping duplicates can\n // fall short of it: widen by whole lines until the form really carries the\n // share. A payload with no room left is refused by the checks below, never\n // installed short.\n while (composed.text.length < target) {\n // Grow the smaller side first, so the two keep their configured proportion.\n const order = composed.headChars <= composed.tailChars ? [0, 1] : [1, 0];\n let grew = false;\n for (const side of order) {\n const nextHead = side === 0 ? headCount + 1 : headCount;\n const nextTail = side === 1 ? tailCount + 1 : tailCount;\n if (nextHead + nextTail > lines.length) continue;\n const next = composeFloorLines(lines, nextHead, nextTail, candidate, marker, secondMarker);\n if (next.text.length <= composed.text.length) continue;\n headCount = nextHead;\n tailCount = nextTail;\n composed = next;\n grew = true;\n break;\n }\n if (!grew) break;\n }\n if (composed.text.length >= original.length || composed.text.length < target) {\n return refusedFloor(original, candidate, marker);\n }\n return {\n ...plan,\n resultChars: composed.text.length,\n omittedChars: original.length - composed.text.length,\n headChars: composed.headChars,\n tailChars: composed.tailChars,\n markerChars: composed.markerChars,\n text: composed.text\n };\n}\n"]}
|
package/dist/core/score.d.ts
CHANGED
|
@@ -2,8 +2,8 @@ import { DecisionBackend } from '../backend/types.js';
|
|
|
2
2
|
/**
|
|
3
3
|
* Continuous 0.0 – 1.0 scoring projected from a System One multi-class choice.
|
|
4
4
|
*
|
|
5
|
-
* Shared by every surface (stdio MCP server,
|
|
6
|
-
* model sees never depends on which adapter called it.
|
|
5
|
+
* Shared by every surface (stdio MCP server, in-process host tools) so the score
|
|
6
|
+
* a model sees never depends on which adapter called it.
|
|
7
7
|
*/
|
|
8
8
|
export interface ScoreRequest {
|
|
9
9
|
statement: string;
|
package/dist/core/score.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"score.js","sourceRoot":"","sources":["../../src/core/score.ts"],"names":[],"mappings":"AA4BA,MAAM,CAAC,MAAM,sBAAsB,GAAG,gC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,YAAY,CAAC,MAAM,GAAG,aAAa,CAAC,CAAC,CAAC;QACtC,YAAY,CAAC,GAAG,GAAG,aAAa,CAAC,CAAC,CAAC,CACpC,CAAC,OAAO,CAAC,CAAC,CAAC,CACb,CAAC;IAEF,OAAO;QACL,SAAS;QACT,KAAK;QACL,YAAY;QACZ,SAAS,EAAE,OAAO,CAAC,SAAS;KAC7B,CAAC;AACJ,CAAC","sourcesContent":["import { DecisionBackend } from '../backend/types.js';\n\n/**\n * Continuous 0.0 – 1.0 scoring projected from a System One multi-class choice.\n *\n * Shared by every surface (stdio MCP server,
|
|
1
|
+
{"version":3,"file":"score.js","sourceRoot":"","sources":["../../src/core/score.ts"],"names":[],"mappings":"AA4BA,MAAM,CAAC,MAAM,sBAAsB,GAAG,gCAAgC,CAAC;AAEvE,iFAAiF;AACjF,MAAM,CAAC,MAAM,aAAa,GAAsC;IAC9D,6BAA6B;IAC7B,iCAAiC;IACjC,6BAA6B;CAC9B,CAAC;AAEF,+EAA+E;AAC/E,MAAM,aAAa,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAU,CAAC;AAE/C;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAAC,QAAgB;IACjD,OAAO,2DAA2D,QAAQ,+CAA+C,CAAC;AAC5H,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,OAAwB,EAAE,OAAqB;IAClF,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,EAAE,CAAC;IAC1C,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,sBAAsB,CAAC;IAC5D,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,MAAM,CAAC;QACnC,QAAQ,EAAE,kBAAkB,CAAC,QAAQ,CAAC;QACtC,OAAO,EAAE,CAAC,GAAG,aAAa,CAAC;QAC3B,OAAO,EAAE,cAAc,SAAS,cAAc,OAAO,CAAC,OAAO,IAAI,MAAM,EAAE;KAC1E,CAAC,CAAC;IAEH,MAAM,aAAa,GAAG,CAAC,KAAa,EAAU,EAAE,CAC9C,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,WAAW,IAAI,CAAC,CAAC;IAEnF,MAAM,YAAY,GAAsB;QACtC,IAAI,EAAE,aAAa,CAAC,MAAM,CAAC;QAC3B,MAAM,EAAE,aAAa,CAAC,UAAU,CAAC;QACjC,GAAG,EAAE,aAAa,CAAC,MAAM,CAAC;KAC3B,CAAC;IAEF,MAAM,KAAK,GAAG,MAAM,CAClB,CACE,YAAY,CAAC,IAAI,GAAG,aAAa,CAAC,CAAC,CAAC;QACpC,YAAY,CAAC,MAAM,GAAG,aAAa,CAAC,CAAC,CAAC;QACtC,YAAY,CAAC,GAAG,GAAG,aAAa,CAAC,CAAC,CAAC,CACpC,CAAC,OAAO,CAAC,CAAC,CAAC,CACb,CAAC;IAEF,OAAO;QACL,SAAS;QACT,KAAK;QACL,YAAY;QACZ,SAAS,EAAE,OAAO,CAAC,SAAS;KAC7B,CAAC;AACJ,CAAC","sourcesContent":["import { DecisionBackend } from '../backend/types.js';\n\n/**\n * Continuous 0.0 – 1.0 scoring projected from a System One multi-class choice.\n *\n * Shared by every surface (stdio MCP server, in-process host tools) so the score\n * a model sees never depends on which adapter called it.\n */\n\nexport interface ScoreRequest {\n statement: string;\n criteria?: string;\n context?: string;\n}\n\nexport interface ScoreDistribution {\n high: number;\n medium: number;\n low: number;\n}\n\nexport interface ScoreResponse {\n statement: string;\n score: number;\n distribution: ScoreDistribution;\n latencyMs: number;\n}\n\nexport const DEFAULT_SCORE_CRITERIA = 'Rate alignment from 0.0 to 1.0';\n\n/** The three scored buckets; the labels double as the probability lookup key. */\nexport const SCORE_OPTIONS: readonly [string, string, string] = [\n 'High alignment (score ~0.9)',\n 'Moderate alignment (score ~0.5)',\n 'Poor alignment (score ~0.1)'\n];\n\n/** Representative score of each bucket, indexed like {@link SCORE_OPTIONS}. */\nconst BUCKET_SCORES = [0.9, 0.5, 0.1] as const;\n\n/**\n * Build the System One question that makes the backend classify one statement\n * into a scored bucket.\n * @param criteria - the rubric the statement is graded against.\n * @returns the classification prompt sent to the backend.\n */\nexport function buildScoreQuestion(criteria: string): string {\n return `Evaluate the following statement against the criteria: \"${criteria}\". Is the quality/truth high, medium, or low?`;\n}\n\n/**\n * Score one statement against a rubric with a single System One choice call.\n * @param backend - decision backend performing the classification.\n * @param request - statement, rubric, and optional supporting context.\n * @returns the expected-value score plus the raw bucket distribution.\n */\nexport async function scoreStatement(backend: DecisionBackend, request: ScoreRequest): Promise<ScoreResponse> {\n const statement = request.statement || '';\n const criteria = request.criteria || DEFAULT_SCORE_CRITERIA;\n const evalRes = await backend.choice({\n question: buildScoreQuestion(criteria),\n options: [...SCORE_OPTIONS],\n context: `Statement: ${statement}\\nContext: ${request.context || 'None'}`\n });\n\n const probabilityOf = (label: string): number =>\n evalRes.options.find((option) => option.label.includes(label))?.probability || 0;\n\n const distribution: ScoreDistribution = {\n high: probabilityOf('High'),\n medium: probabilityOf('Moderate'),\n low: probabilityOf('Poor')\n };\n\n const score = Number(\n (\n distribution.high * BUCKET_SCORES[0] +\n distribution.medium * BUCKET_SCORES[1] +\n distribution.low * BUCKET_SCORES[2]\n ).toFixed(3)\n );\n\n return {\n statement,\n score,\n distribution,\n latencyMs: evalRes.latencyMs\n };\n}\n"]}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** One content block as the decision surface sees it: a tag and optional text. */
|
|
2
|
+
export interface TextBlockLike {
|
|
3
|
+
readonly type?: string;
|
|
4
|
+
readonly text?: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Bound one text to a character budget, marking the cut.
|
|
8
|
+
* @param text - any text.
|
|
9
|
+
* @param max - maximum characters to keep.
|
|
10
|
+
* @returns the bounded text.
|
|
11
|
+
*/
|
|
12
|
+
export declare function clip(text: string, max: number): string;
|
|
13
|
+
/**
|
|
14
|
+
* Tokens one text costs, at the ledger's shared four-characters-per-token rate.
|
|
15
|
+
* @param text - any text.
|
|
16
|
+
* @returns the estimated token count.
|
|
17
|
+
*/
|
|
18
|
+
export declare function estimateTokens(text: string): number;
|
|
19
|
+
/**
|
|
20
|
+
* Stable short digest of one value, used as a payload or call identity.
|
|
21
|
+
* @param value - the text to digest.
|
|
22
|
+
* @returns sixteen hexadecimal characters.
|
|
23
|
+
*/
|
|
24
|
+
export declare function digestOf(value: string): string;
|
|
25
|
+
/**
|
|
26
|
+
* Concatenate the text blocks of one content list.
|
|
27
|
+
* @param content - message or result content.
|
|
28
|
+
* @returns the text, in block order.
|
|
29
|
+
*/
|
|
30
|
+
export declare function textOfContent(content: readonly TextBlockLike[] | undefined): string;
|
|
31
|
+
/**
|
|
32
|
+
* Whether one content list carries at least one text block.
|
|
33
|
+
* @param content - message or result content.
|
|
34
|
+
* @returns whether there is text to rewrite.
|
|
35
|
+
*/
|
|
36
|
+
export declare function hasTextBlock(content: readonly TextBlockLike[] | undefined): boolean;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Text helpers shared by the intervention decision core.
|
|
3
|
+
*
|
|
4
|
+
* Everything here is host-agnostic: the unit of work is a string, an estimated
|
|
5
|
+
* token count, or a structural content block. No host message type, no session,
|
|
6
|
+
* no tool-execution type is imported or assumed.
|
|
7
|
+
*
|
|
8
|
+
* @module core/text
|
|
9
|
+
*/
|
|
10
|
+
import { createHash } from 'node:crypto';
|
|
11
|
+
/**
|
|
12
|
+
* Bound one text to a character budget, marking the cut.
|
|
13
|
+
* @param text - any text.
|
|
14
|
+
* @param max - maximum characters to keep.
|
|
15
|
+
* @returns the bounded text.
|
|
16
|
+
*/
|
|
17
|
+
export function clip(text, max) {
|
|
18
|
+
return text.length <= max ? text : `${text.slice(0, max)}…`;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Tokens one text costs, at the ledger's shared four-characters-per-token rate.
|
|
22
|
+
* @param text - any text.
|
|
23
|
+
* @returns the estimated token count.
|
|
24
|
+
*/
|
|
25
|
+
export function estimateTokens(text) {
|
|
26
|
+
return text.length === 0 ? 0 : Math.ceil(text.length / 4);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Stable short digest of one value, used as a payload or call identity.
|
|
30
|
+
* @param value - the text to digest.
|
|
31
|
+
* @returns sixteen hexadecimal characters.
|
|
32
|
+
*/
|
|
33
|
+
export function digestOf(value) {
|
|
34
|
+
return createHash('sha256').update(value).digest('hex').slice(0, 16);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Concatenate the text blocks of one content list.
|
|
38
|
+
* @param content - message or result content.
|
|
39
|
+
* @returns the text, in block order.
|
|
40
|
+
*/
|
|
41
|
+
export function textOfContent(content) {
|
|
42
|
+
if (!content)
|
|
43
|
+
return '';
|
|
44
|
+
const parts = [];
|
|
45
|
+
for (const block of content) {
|
|
46
|
+
if (block && typeof block === 'object' && block.type === 'text') {
|
|
47
|
+
parts.push(block.text ?? '');
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return parts.join('\n');
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Whether one content list carries at least one text block.
|
|
54
|
+
* @param content - message or result content.
|
|
55
|
+
* @returns whether there is text to rewrite.
|
|
56
|
+
*/
|
|
57
|
+
export function hasTextBlock(content) {
|
|
58
|
+
if (!content)
|
|
59
|
+
return false;
|
|
60
|
+
for (const block of content) {
|
|
61
|
+
if (block && typeof block === 'object' && block.type === 'text')
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=text.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text.js","sourceRoot":"","sources":["../../src/core/text.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,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AC3B,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;QAC5B,IAAI,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,KAAK,MAAM;YAAE,OAAO,IAAI,CAAC;IAC/E,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["/**\n * Text helpers shared by the intervention decision core.\n *\n * Everything here is host-agnostic: the unit of work is a string, an estimated\n * token count, or a structural content block. No host message type, no session,\n * no tool-execution type is imported or assumed.\n *\n * @module core/text\n */\nimport { createHash } from 'node:crypto';\n\n/** One content block as the decision surface sees it: a tag and optional text. */\nexport interface TextBlockLike {\n readonly type?: string;\n readonly text?: string;\n}\n\n/**\n * Bound one text to a character budget, marking the cut.\n * @param text - any text.\n * @param max - maximum characters to keep.\n * @returns the bounded text.\n */\nexport function clip(text: string, max: number): string {\n return text.length <= max ? text : `${text.slice(0, max)}…`;\n}\n\n/**\n * Tokens one text costs, at the ledger's shared four-characters-per-token rate.\n * @param text - any text.\n * @returns the estimated token count.\n */\nexport function estimateTokens(text: string): number {\n return text.length === 0 ? 0 : Math.ceil(text.length / 4);\n}\n\n/**\n * Stable short digest of one value, used as a payload or call identity.\n * @param value - the text to digest.\n * @returns sixteen hexadecimal characters.\n */\nexport function digestOf(value: string): string {\n return createHash('sha256').update(value).digest('hex').slice(0, 16);\n}\n\n/**\n * Concatenate the text blocks of one content list.\n * @param content - message or result content.\n * @returns the text, in block order.\n */\nexport function textOfContent(content: readonly TextBlockLike[] | undefined): string {\n if (!content) return '';\n const parts: string[] = [];\n for (const block of content) {\n if (block && typeof block === 'object' && block.type === 'text') {\n parts.push(block.text ?? '');\n }\n }\n return parts.join('\\n');\n}\n\n/**\n * Whether one content list carries at least one text block.\n * @param content - message or result content.\n * @returns whether there is text to rewrite.\n */\nexport function hasTextBlock(content: readonly TextBlockLike[] | undefined): boolean {\n if (!content) return false;\n for (const block of content) {\n if (block && typeof block === 'object' && block.type === 'text') return true;\n }\n return false;\n}\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run the `hook` subcommand and exit.
|
|
3
|
+
*
|
|
4
|
+
* `stdout` receives exactly one JSON object (or nothing when the platform is
|
|
5
|
+
* refused). A termination signal writes `{}` and exits 0 so an interrupted hook
|
|
6
|
+
* is indistinguishable from an allow, never from a veto.
|
|
7
|
+
* @param argv - arguments after `hook`; the first is the event name.
|
|
8
|
+
*/
|
|
9
|
+
export declare function runHookCli(argv: string[]): Promise<void>;
|
package/dist/hook/cli.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The process-shaped wrapper around {@link runHook}.
|
|
3
|
+
*
|
|
4
|
+
* Everything here exists because the command is spawned per event: install the
|
|
5
|
+
* signal path that fails open, write exactly one JSON object to stdout, and
|
|
6
|
+
* flush it before exiting. The decision itself lives in `run.ts` and stays
|
|
7
|
+
* unit-testable without a process.
|
|
8
|
+
*
|
|
9
|
+
* @module hook/cli
|
|
10
|
+
*/
|
|
11
|
+
import { runHook } from './run.js';
|
|
12
|
+
/**
|
|
13
|
+
* Run the `hook` subcommand and exit.
|
|
14
|
+
*
|
|
15
|
+
* `stdout` receives exactly one JSON object (or nothing when the platform is
|
|
16
|
+
* refused). A termination signal writes `{}` and exits 0 so an interrupted hook
|
|
17
|
+
* is indistinguishable from an allow, never from a veto.
|
|
18
|
+
* @param argv - arguments after `hook`; the first is the event name.
|
|
19
|
+
*/
|
|
20
|
+
export async function runHookCli(argv) {
|
|
21
|
+
let finished = false;
|
|
22
|
+
const finish = (payload, code) => {
|
|
23
|
+
if (finished)
|
|
24
|
+
return;
|
|
25
|
+
finished = true;
|
|
26
|
+
if (payload.length === 0) {
|
|
27
|
+
process.exit(code);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
process.stdout.write(payload, () => process.exit(code));
|
|
31
|
+
};
|
|
32
|
+
const onSignal = () => finish('{}\n', 0);
|
|
33
|
+
process.once('SIGTERM', onSignal);
|
|
34
|
+
process.once('SIGINT', onSignal);
|
|
35
|
+
try {
|
|
36
|
+
const result = await runHook(argv[0]);
|
|
37
|
+
if (result.output === null)
|
|
38
|
+
finish('', result.code);
|
|
39
|
+
else
|
|
40
|
+
finish(`${JSON.stringify(result.output)}\n`, result.code);
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
// runHook itself never throws, but a broken stdout must still fail open.
|
|
44
|
+
process.stderr.write(`harness-reflex hook: ${error.message}\n`);
|
|
45
|
+
finish('{}\n', 0);
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
process.removeListener('SIGTERM', onSignal);
|
|
49
|
+
process.removeListener('SIGINT', onSignal);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=cli.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../../src/hook/cli.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAEnC;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,IAAc;IAC7C,IAAI,QAAQ,GAAG,KAAK,CAAC;IACrB,MAAM,MAAM,GAAG,CAAC,OAAe,EAAE,IAAY,EAAQ,EAAE;QACrD,IAAI,QAAQ;YAAE,OAAO;QACrB,QAAQ,GAAG,IAAI,CAAC;QAChB,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACzB,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACnB,OAAO;QACT,CAAC;QACD,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IAC1D,CAAC,CAAC;IACF,MAAM,QAAQ,GAAG,GAAS,EAAE,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IAC/C,OAAO,CAAC,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IAClC,OAAO,CAAC,IAAI,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IACjC,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QACtC,IAAI,MAAM,CAAC,MAAM,KAAK,IAAI;YAAE,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;;YAC/C,MAAM,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;IACjE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,yEAAyE;QACzE,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,wBAAyB,KAAe,CAAC,OAAO,IAAI,CAAC,CAAC;QAC3E,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IACpB,CAAC;YAAS,CAAC;QACT,OAAO,CAAC,cAAc,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;QAC5C,OAAO,CAAC,cAAc,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IAC7C,CAAC;AACH,CAAC","sourcesContent":["/**\n * The process-shaped wrapper around {@link runHook}.\n *\n * Everything here exists because the command is spawned per event: install the\n * signal path that fails open, write exactly one JSON object to stdout, and\n * flush it before exiting. The decision itself lives in `run.ts` and stays\n * unit-testable without a process.\n *\n * @module hook/cli\n */\nimport { runHook } from './run.js';\n\n/**\n * Run the `hook` subcommand and exit.\n *\n * `stdout` receives exactly one JSON object (or nothing when the platform is\n * refused). A termination signal writes `{}` and exits 0 so an interrupted hook\n * is indistinguishable from an allow, never from a veto.\n * @param argv - arguments after `hook`; the first is the event name.\n */\nexport async function runHookCli(argv: string[]): Promise<void> {\n let finished = false;\n const finish = (payload: string, code: number): void => {\n if (finished) return;\n finished = true;\n if (payload.length === 0) {\n process.exit(code);\n return;\n }\n process.stdout.write(payload, () => process.exit(code));\n };\n const onSignal = (): void => finish('{}\\n', 0);\n process.once('SIGTERM', onSignal);\n process.once('SIGINT', onSignal);\n try {\n const result = await runHook(argv[0]);\n if (result.output === null) finish('', result.code);\n else finish(`${JSON.stringify(result.output)}\\n`, result.code);\n } catch (error) {\n // runHook itself never throws, but a broken stdout must still fail open.\n process.stderr.write(`harness-reflex hook: ${(error as Error).message}\\n`);\n finish('{}\\n', 0);\n } finally {\n process.removeListener('SIGTERM', onSignal);\n process.removeListener('SIGINT', onSignal);\n }\n}\n"]}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `harness-reflex hook <event>` wire contract.
|
|
3
|
+
*
|
|
4
|
+
* One table, one source of truth: the event aliases a harness may invoke, the
|
|
5
|
+
* stdin shape this implementation reads, the stdout shape it may emit, and the
|
|
6
|
+
* per-event decision budget. The command only ever writes the JSON shape named
|
|
7
|
+
* here, because the host splices stdout into model-visible text and treats any
|
|
8
|
+
* non-JSON byte as a reason.
|
|
9
|
+
*
|
|
10
|
+
* @module hook/contract
|
|
11
|
+
*/
|
|
12
|
+
/** The events the hook accepts (design §2.5). */
|
|
13
|
+
export declare const HOOK_EVENTS: readonly ["pretooluse", "posttooluse", "postfailure", "stop", "substop", "sessionstart", "sessionend"];
|
|
14
|
+
/** One accepted hook event. */
|
|
15
|
+
export type HookEvent = (typeof HOOK_EVENTS)[number];
|
|
16
|
+
/**
|
|
17
|
+
* Events a host emits that this runtime has no capability for.
|
|
18
|
+
*
|
|
19
|
+
* Codex 0.163.0 fires twelve hook events (`hooks/src/engine/command_runner.rs`).
|
|
20
|
+
* Five of them describe something the decision runtime deliberately does not
|
|
21
|
+
* decide — the prompt is already submitted, the permission is already being
|
|
22
|
+
* asked, the compaction is already happening. They are still *recognized* so a
|
|
23
|
+
* wiring mistake is a silent `{}` instead of a stderr warning on every turn, but
|
|
24
|
+
* they can never produce a decision: `HookDecision.handle` never sees them.
|
|
25
|
+
*
|
|
26
|
+
* This is a separate type from {@link HookEvent} on purpose. Widening `HookEvent`
|
|
27
|
+
* would force every existing `switch` over it to grow a branch, which is exactly
|
|
28
|
+
* the kind of behavior change this adapter is not allowed to make.
|
|
29
|
+
*/
|
|
30
|
+
export declare const CODEX_ONLY_HOOK_EVENTS: readonly ["userpromptsubmit", "permissionrequest", "precompact", "postcompact", "subagentstart", "interrupt"];
|
|
31
|
+
/** One host event that is recognized but carries no reflex capability. */
|
|
32
|
+
export type CodexOnlyHookEvent = (typeof CODEX_ONLY_HOOK_EVENTS)[number];
|
|
33
|
+
/** What one raw event argument resolved to. */
|
|
34
|
+
export type ParsedHookEventName = {
|
|
35
|
+
kind: 'canonical';
|
|
36
|
+
event: HookEvent;
|
|
37
|
+
} | {
|
|
38
|
+
kind: 'codex-only';
|
|
39
|
+
event: CodexOnlyHookEvent;
|
|
40
|
+
} | {
|
|
41
|
+
kind: 'unknown';
|
|
42
|
+
};
|
|
43
|
+
/** The three-state mode every capability shares. */
|
|
44
|
+
export type HookMode = 'off' | 'observe' | 'active';
|
|
45
|
+
/** Per-event decision budget and the host-side timeout it must stay inside. */
|
|
46
|
+
export interface EventBudget {
|
|
47
|
+
/** Internal decision budget; on expiry the hook fails open. */
|
|
48
|
+
budgetMs: number;
|
|
49
|
+
/** The host's own timeout, kept for documentation and the hard deadline. */
|
|
50
|
+
hookTimeoutMs: number;
|
|
51
|
+
}
|
|
52
|
+
/** Budgets from design §2.5, in the same order as {@link HOOK_EVENTS}. */
|
|
53
|
+
export declare const EVENT_BUDGETS: Record<HookEvent, EventBudget>;
|
|
54
|
+
/**
|
|
55
|
+
* Resolve an event argument to its canonical name.
|
|
56
|
+
*
|
|
57
|
+
* The documented subcommand names are lowercase; the host's event names are
|
|
58
|
+
* PascalCase, kebab-case, or snake_case depending on the harness. All spellings
|
|
59
|
+
* are accepted so a wiring mistake is a no-op rather than a crash, and an
|
|
60
|
+
* unknown name is reported to stderr while stdout stays `{}`.
|
|
61
|
+
*
|
|
62
|
+
* Kebab-case is handled structurally: `pre-tool-use` loses its separators and
|
|
63
|
+
* becomes `pretooluse`, so the same table serves Claude Code's PascalCase and
|
|
64
|
+
* Codex's `pre-tool-use` without a second list.
|
|
65
|
+
*
|
|
66
|
+
* Events that are recognized but have no capability resolve to `undefined` here;
|
|
67
|
+
* use {@link parseHookEventName} when the difference matters.
|
|
68
|
+
* @param raw - the first argument after `hook`.
|
|
69
|
+
* @returns the canonical event, or `undefined` when unknown or capability-less.
|
|
70
|
+
*/
|
|
71
|
+
export declare function parseHookEvent(raw: string | undefined): HookEvent | undefined;
|
|
72
|
+
/**
|
|
73
|
+
* Resolve an event argument, distinguishing "no capability" from "unknown".
|
|
74
|
+
*
|
|
75
|
+
* The hook process uses this so that a Codex event the runtime has nothing to
|
|
76
|
+
* say about exits 0 with `{}` and no warning, while a genuine typo still gets one.
|
|
77
|
+
* @param raw - the first argument after `hook`.
|
|
78
|
+
* @returns the discriminated parse result.
|
|
79
|
+
*/
|
|
80
|
+
export declare function parseHookEventName(raw: string | undefined): ParsedHookEventName;
|
|
81
|
+
/** A JSON object, as every stdout shape and stdin payload is. */
|
|
82
|
+
export type JsonObject = Record<string, unknown>;
|
|
83
|
+
/** The `hookSpecificOutput` envelope of the Claude Code hook contract. */
|
|
84
|
+
export interface HookSpecificOutput {
|
|
85
|
+
hookEventName: string;
|
|
86
|
+
permissionDecision?: 'deny' | 'allow' | 'ask';
|
|
87
|
+
permissionDecisionReason?: string;
|
|
88
|
+
updatedToolOutput?: string;
|
|
89
|
+
/**
|
|
90
|
+
* Codex's name for a replaced tool result. It is a *different field* from
|
|
91
|
+
* `updatedToolOutput`, not an alias, and Codex only honors it for MCP tool
|
|
92
|
+
* output — a shell tool result cannot be rewritten at all. Both names are
|
|
93
|
+
* modelled so a host mismatch is a typed mistake instead of a silently
|
|
94
|
+
* ignored field.
|
|
95
|
+
*/
|
|
96
|
+
updatedMCPToolOutput?: string;
|
|
97
|
+
additionalContext?: string;
|
|
98
|
+
}
|
|
99
|
+
/** The stdout object of a hook invocation. `{}` means "no instruction". */
|
|
100
|
+
export interface HookOutput extends JsonObject {
|
|
101
|
+
hookSpecificOutput?: HookSpecificOutput;
|
|
102
|
+
decision?: 'block';
|
|
103
|
+
reason?: string;
|
|
104
|
+
}
|
|
105
|
+
/** The empty output that always means "allow, no instruction". */
|
|
106
|
+
export declare function emptyHookOutput(): HookOutput;
|
|
107
|
+
/** The output of one PreToolUse denial. */
|
|
108
|
+
export declare function preToolUseDeny(reason: string): HookOutput;
|
|
109
|
+
/** The output of one PostToolUse replacement. */
|
|
110
|
+
export declare function postToolUseReplace(text: string): HookOutput;
|
|
111
|
+
/**
|
|
112
|
+
* The output of one Codex PostToolUse replacement.
|
|
113
|
+
*
|
|
114
|
+
* Codex reads `updatedMCPToolOutput` and only for MCP tools; emitting Claude's
|
|
115
|
+
* `updatedToolOutput` there is silently ignored. Kept as a separate function so
|
|
116
|
+
* the adapter cannot pick the wrong name by accident.
|
|
117
|
+
* @param text - the replacement text for the MCP tool result.
|
|
118
|
+
*/
|
|
119
|
+
export declare function postToolUseMcpReplace(text: string): HookOutput;
|
|
120
|
+
/** The output of one PostToolUseFailure correction. */
|
|
121
|
+
export declare function postToolUseFailureContext(text: string): HookOutput;
|
|
122
|
+
/** The output of one Stop objection. */
|
|
123
|
+
export declare function stopBlock(reason: string): HookOutput;
|
|
124
|
+
/** The output of one SessionStart notice. */
|
|
125
|
+
export declare function sessionStartContext(text: string): HookOutput;
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `harness-reflex hook <event>` wire contract.
|
|
3
|
+
*
|
|
4
|
+
* One table, one source of truth: the event aliases a harness may invoke, the
|
|
5
|
+
* stdin shape this implementation reads, the stdout shape it may emit, and the
|
|
6
|
+
* per-event decision budget. The command only ever writes the JSON shape named
|
|
7
|
+
* here, because the host splices stdout into model-visible text and treats any
|
|
8
|
+
* non-JSON byte as a reason.
|
|
9
|
+
*
|
|
10
|
+
* @module hook/contract
|
|
11
|
+
*/
|
|
12
|
+
/** The events the hook accepts (design §2.5). */
|
|
13
|
+
export const HOOK_EVENTS = [
|
|
14
|
+
'pretooluse',
|
|
15
|
+
'posttooluse',
|
|
16
|
+
'postfailure',
|
|
17
|
+
'stop',
|
|
18
|
+
'substop',
|
|
19
|
+
'sessionstart',
|
|
20
|
+
'sessionend'
|
|
21
|
+
];
|
|
22
|
+
/**
|
|
23
|
+
* Events a host emits that this runtime has no capability for.
|
|
24
|
+
*
|
|
25
|
+
* Codex 0.163.0 fires twelve hook events (`hooks/src/engine/command_runner.rs`).
|
|
26
|
+
* Five of them describe something the decision runtime deliberately does not
|
|
27
|
+
* decide — the prompt is already submitted, the permission is already being
|
|
28
|
+
* asked, the compaction is already happening. They are still *recognized* so a
|
|
29
|
+
* wiring mistake is a silent `{}` instead of a stderr warning on every turn, but
|
|
30
|
+
* they can never produce a decision: `HookDecision.handle` never sees them.
|
|
31
|
+
*
|
|
32
|
+
* This is a separate type from {@link HookEvent} on purpose. Widening `HookEvent`
|
|
33
|
+
* would force every existing `switch` over it to grow a branch, which is exactly
|
|
34
|
+
* the kind of behavior change this adapter is not allowed to make.
|
|
35
|
+
*/
|
|
36
|
+
export const CODEX_ONLY_HOOK_EVENTS = [
|
|
37
|
+
'userpromptsubmit',
|
|
38
|
+
'permissionrequest',
|
|
39
|
+
'precompact',
|
|
40
|
+
'postcompact',
|
|
41
|
+
'subagentstart',
|
|
42
|
+
'interrupt'
|
|
43
|
+
];
|
|
44
|
+
/** Strip the dashes, underscores and case a host may spell an event with. */
|
|
45
|
+
function normalizeEventName(raw) {
|
|
46
|
+
return raw.trim().toLowerCase().replace(/[-_\s]/g, '');
|
|
47
|
+
}
|
|
48
|
+
/** Budgets from design §2.5, in the same order as {@link HOOK_EVENTS}. */
|
|
49
|
+
export const EVENT_BUDGETS = {
|
|
50
|
+
pretooluse: { budgetMs: 300, hookTimeoutMs: 1000 },
|
|
51
|
+
posttooluse: { budgetMs: 800, hookTimeoutMs: 2000 },
|
|
52
|
+
postfailure: { budgetMs: 300, hookTimeoutMs: 1000 },
|
|
53
|
+
stop: { budgetMs: 1000, hookTimeoutMs: 2000 },
|
|
54
|
+
substop: { budgetMs: 1000, hookTimeoutMs: 2000 },
|
|
55
|
+
sessionstart: { budgetMs: 1200, hookTimeoutMs: 2000 },
|
|
56
|
+
sessionend: { budgetMs: 1200, hookTimeoutMs: 1500 }
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Resolve an event argument to its canonical name.
|
|
60
|
+
*
|
|
61
|
+
* The documented subcommand names are lowercase; the host's event names are
|
|
62
|
+
* PascalCase, kebab-case, or snake_case depending on the harness. All spellings
|
|
63
|
+
* are accepted so a wiring mistake is a no-op rather than a crash, and an
|
|
64
|
+
* unknown name is reported to stderr while stdout stays `{}`.
|
|
65
|
+
*
|
|
66
|
+
* Kebab-case is handled structurally: `pre-tool-use` loses its separators and
|
|
67
|
+
* becomes `pretooluse`, so the same table serves Claude Code's PascalCase and
|
|
68
|
+
* Codex's `pre-tool-use` without a second list.
|
|
69
|
+
*
|
|
70
|
+
* Events that are recognized but have no capability resolve to `undefined` here;
|
|
71
|
+
* use {@link parseHookEventName} when the difference matters.
|
|
72
|
+
* @param raw - the first argument after `hook`.
|
|
73
|
+
* @returns the canonical event, or `undefined` when unknown or capability-less.
|
|
74
|
+
*/
|
|
75
|
+
export function parseHookEvent(raw) {
|
|
76
|
+
if (!raw)
|
|
77
|
+
return undefined;
|
|
78
|
+
const name = normalizeEventName(raw);
|
|
79
|
+
// `post-tool` / `pre-tool` are what a few bundlers spell the Claude Code
|
|
80
|
+
// events as, so the alias table normalizes them instead of silently no-oping.
|
|
81
|
+
if (name === 'pretooluse' || name === 'pretool')
|
|
82
|
+
return 'pretooluse';
|
|
83
|
+
if (name === 'posttooluse' || name === 'posttool')
|
|
84
|
+
return 'posttooluse';
|
|
85
|
+
if (name === 'posttoolusefailure' || name === 'postfailure')
|
|
86
|
+
return 'postfailure';
|
|
87
|
+
if (name === 'stop')
|
|
88
|
+
return 'stop';
|
|
89
|
+
if (name === 'subagentstop' || name === 'substop')
|
|
90
|
+
return 'substop';
|
|
91
|
+
if (name === 'sessionstart')
|
|
92
|
+
return 'sessionstart';
|
|
93
|
+
if (name === 'sessionend')
|
|
94
|
+
return 'sessionend';
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Resolve an event argument, distinguishing "no capability" from "unknown".
|
|
99
|
+
*
|
|
100
|
+
* The hook process uses this so that a Codex event the runtime has nothing to
|
|
101
|
+
* say about exits 0 with `{}` and no warning, while a genuine typo still gets one.
|
|
102
|
+
* @param raw - the first argument after `hook`.
|
|
103
|
+
* @returns the discriminated parse result.
|
|
104
|
+
*/
|
|
105
|
+
export function parseHookEventName(raw) {
|
|
106
|
+
const canonical = parseHookEvent(raw);
|
|
107
|
+
if (canonical)
|
|
108
|
+
return { kind: 'canonical', event: canonical };
|
|
109
|
+
if (!raw)
|
|
110
|
+
return { kind: 'unknown' };
|
|
111
|
+
const name = normalizeEventName(raw);
|
|
112
|
+
for (const event of CODEX_ONLY_HOOK_EVENTS) {
|
|
113
|
+
if (name === event)
|
|
114
|
+
return { kind: 'codex-only', event };
|
|
115
|
+
}
|
|
116
|
+
return { kind: 'unknown' };
|
|
117
|
+
}
|
|
118
|
+
/** The empty output that always means "allow, no instruction". */
|
|
119
|
+
export function emptyHookOutput() {
|
|
120
|
+
return {};
|
|
121
|
+
}
|
|
122
|
+
/** The output of one PreToolUse denial. */
|
|
123
|
+
export function preToolUseDeny(reason) {
|
|
124
|
+
return {
|
|
125
|
+
hookSpecificOutput: {
|
|
126
|
+
hookEventName: 'PreToolUse',
|
|
127
|
+
permissionDecision: 'deny',
|
|
128
|
+
permissionDecisionReason: reason
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/** The output of one PostToolUse replacement. */
|
|
133
|
+
export function postToolUseReplace(text) {
|
|
134
|
+
return { hookSpecificOutput: { hookEventName: 'PostToolUse', updatedToolOutput: text } };
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* The output of one Codex PostToolUse replacement.
|
|
138
|
+
*
|
|
139
|
+
* Codex reads `updatedMCPToolOutput` and only for MCP tools; emitting Claude's
|
|
140
|
+
* `updatedToolOutput` there is silently ignored. Kept as a separate function so
|
|
141
|
+
* the adapter cannot pick the wrong name by accident.
|
|
142
|
+
* @param text - the replacement text for the MCP tool result.
|
|
143
|
+
*/
|
|
144
|
+
export function postToolUseMcpReplace(text) {
|
|
145
|
+
return { hookSpecificOutput: { hookEventName: 'PostToolUse', updatedMCPToolOutput: text } };
|
|
146
|
+
}
|
|
147
|
+
/** The output of one PostToolUseFailure correction. */
|
|
148
|
+
export function postToolUseFailureContext(text) {
|
|
149
|
+
return { hookSpecificOutput: { hookEventName: 'PostToolUseFailure', additionalContext: text } };
|
|
150
|
+
}
|
|
151
|
+
/** The output of one Stop objection. */
|
|
152
|
+
export function stopBlock(reason) {
|
|
153
|
+
return { decision: 'block', reason };
|
|
154
|
+
}
|
|
155
|
+
/** The output of one SessionStart notice. */
|
|
156
|
+
export function sessionStartContext(text) {
|
|
157
|
+
return { hookSpecificOutput: { hookEventName: 'SessionStart', additionalContext: text } };
|
|
158
|
+
}
|
|
159
|
+
//# sourceMappingURL=contract.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contract.js","sourceRoot":"","sources":["../../src/hook/contract.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,iDAAiD;AACjD,MAAM,CAAC,MAAM,WAAW,GAAG;IACzB,YAAY;IACZ,aAAa;IACb,aAAa;IACb,MAAM;IACN,SAAS;IACT,cAAc;IACd,YAAY;CACJ,CAAC;AAKX;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,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AE,aAAa,EAAE,aAAa,EAAE,iBAAiB,EAAE,IAAI,EAAE,EAAE,CAAC;AAC3F,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,qBAAqB,CAAC,IAAY;IAChD,OAAO,EAAE,kBAAkB,EAAE,EAAE,aAAa,EAAE,aAAa,EAAE,oBAAoB,EAAE,IAAI,EAAE,EAAE,CAAC;AAC9F,CAAC;AAED,uDAAuD;AACvD,MAAM,UAAU,yBAAyB,CAAC,IAAY;IACpD,OAAO,EAAE,kBAAkB,EAAE,EAAE,aAAa,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,IAAI,EAAE,EAAE,CAAC;AAClG,CAAC;AAED,wCAAwC;AACxC,MAAM,UAAU,SAAS,CAAC,MAAc;IACtC,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;AACvC,CAAC;AAED,6CAA6C;AAC7C,MAAM,UAAU,mBAAmB,CAAC,IAAY;IAC9C,OAAO,EAAE,kBAAkB,EAAE,EAAE,aAAa,EAAE,cAAc,EAAE,iBAAiB,EAAE,IAAI,EAAE,EAAE,CAAC;AAC5F,CAAC","sourcesContent":["/**\n * The `harness-reflex hook <event>` wire contract.\n *\n * One table, one source of truth: the event aliases a harness may invoke, the\n * stdin shape this implementation reads, the stdout shape it may emit, and the\n * per-event decision budget. The command only ever writes the JSON shape named\n * here, because the host splices stdout into model-visible text and treats any\n * non-JSON byte as a reason.\n *\n * @module hook/contract\n */\n\n/** The events the hook accepts (design §2.5). */\nexport const HOOK_EVENTS = [\n 'pretooluse',\n 'posttooluse',\n 'postfailure',\n 'stop',\n 'substop',\n 'sessionstart',\n 'sessionend'\n] as const;\n\n/** One accepted hook event. */\nexport type HookEvent = (typeof HOOK_EVENTS)[number];\n\n/**\n * Events a host emits that this runtime has no capability for.\n *\n * Codex 0.163.0 fires twelve hook events (`hooks/src/engine/command_runner.rs`).\n * Five of them describe something the decision runtime deliberately does not\n * decide — the prompt is already submitted, the permission is already being\n * asked, the compaction is already happening. They are still *recognized* so a\n * wiring mistake is a silent `{}` instead of a stderr warning on every turn, but\n * they can never produce a decision: `HookDecision.handle` never sees them.\n *\n * This is a separate type from {@link HookEvent} on purpose. Widening `HookEvent`\n * would force every existing `switch` over it to grow a branch, which is exactly\n * the kind of behavior change this adapter is not allowed to make.\n */\nexport const CODEX_ONLY_HOOK_EVENTS = [\n 'userpromptsubmit',\n 'permissionrequest',\n 'precompact',\n 'postcompact',\n 'subagentstart',\n 'interrupt'\n] as const;\n\n/** One host event that is recognized but carries no reflex capability. */\nexport type CodexOnlyHookEvent = (typeof CODEX_ONLY_HOOK_EVENTS)[number];\n\n/** What one raw event argument resolved to. */\nexport type ParsedHookEventName =\n | { kind: 'canonical'; event: HookEvent }\n | { kind: 'codex-only'; event: CodexOnlyHookEvent }\n | { kind: 'unknown' };\n\n/** Strip the dashes, underscores and case a host may spell an event with. */\nfunction normalizeEventName(raw: string): string {\n return raw.trim().toLowerCase().replace(/[-_\\s]/g, '');\n}\n\n/** The three-state mode every capability shares. */\nexport type HookMode = 'off' | 'observe' | 'active';\n\n/** Per-event decision budget and the host-side timeout it must stay inside. */\nexport interface EventBudget {\n /** Internal decision budget; on expiry the hook fails open. */\n budgetMs: number;\n /** The host's own timeout, kept for documentation and the hard deadline. */\n hookTimeoutMs: number;\n}\n\n/** Budgets from design §2.5, in the same order as {@link HOOK_EVENTS}. */\nexport const EVENT_BUDGETS: Record<HookEvent, EventBudget> = {\n pretooluse: { budgetMs: 300, hookTimeoutMs: 1000 },\n posttooluse: { budgetMs: 800, hookTimeoutMs: 2000 },\n postfailure: { budgetMs: 300, hookTimeoutMs: 1000 },\n stop: { budgetMs: 1000, hookTimeoutMs: 2000 },\n substop: { budgetMs: 1000, hookTimeoutMs: 2000 },\n sessionstart: { budgetMs: 1200, hookTimeoutMs: 2000 },\n sessionend: { budgetMs: 1200, hookTimeoutMs: 1500 }\n};\n\n/**\n * Resolve an event argument to its canonical name.\n *\n * The documented subcommand names are lowercase; the host's event names are\n * PascalCase, kebab-case, or snake_case depending on the harness. All spellings\n * are accepted so a wiring mistake is a no-op rather than a crash, and an\n * unknown name is reported to stderr while stdout stays `{}`.\n *\n * Kebab-case is handled structurally: `pre-tool-use` loses its separators and\n * becomes `pretooluse`, so the same table serves Claude Code's PascalCase and\n * Codex's `pre-tool-use` without a second list.\n *\n * Events that are recognized but have no capability resolve to `undefined` here;\n * use {@link parseHookEventName} when the difference matters.\n * @param raw - the first argument after `hook`.\n * @returns the canonical event, or `undefined` when unknown or capability-less.\n */\nexport function parseHookEvent(raw: string | undefined): HookEvent | undefined {\n if (!raw) return undefined;\n const name = normalizeEventName(raw);\n // `post-tool` / `pre-tool` are what a few bundlers spell the Claude Code\n // events as, so the alias table normalizes them instead of silently no-oping.\n if (name === 'pretooluse' || name === 'pretool') return 'pretooluse';\n if (name === 'posttooluse' || name === 'posttool') return 'posttooluse';\n if (name === 'posttoolusefailure' || name === 'postfailure') return 'postfailure';\n if (name === 'stop') return 'stop';\n if (name === 'subagentstop' || name === 'substop') return 'substop';\n if (name === 'sessionstart') return 'sessionstart';\n if (name === 'sessionend') return 'sessionend';\n return undefined;\n}\n\n/**\n * Resolve an event argument, distinguishing \"no capability\" from \"unknown\".\n *\n * The hook process uses this so that a Codex event the runtime has nothing to\n * say about exits 0 with `{}` and no warning, while a genuine typo still gets one.\n * @param raw - the first argument after `hook`.\n * @returns the discriminated parse result.\n */\nexport function parseHookEventName(raw: string | undefined): ParsedHookEventName {\n const canonical = parseHookEvent(raw);\n if (canonical) return { kind: 'canonical', event: canonical };\n if (!raw) return { kind: 'unknown' };\n const name = normalizeEventName(raw);\n for (const event of CODEX_ONLY_HOOK_EVENTS) {\n if (name === event) return { kind: 'codex-only', event };\n }\n return { kind: 'unknown' };\n}\n\n/** A JSON object, as every stdout shape and stdin payload is. */\nexport type JsonObject = Record<string, unknown>;\n\n/** The `hookSpecificOutput` envelope of the Claude Code hook contract. */\nexport interface HookSpecificOutput {\n hookEventName: string;\n permissionDecision?: 'deny' | 'allow' | 'ask';\n permissionDecisionReason?: string;\n updatedToolOutput?: string;\n /**\n * Codex's name for a replaced tool result. It is a *different field* from\n * `updatedToolOutput`, not an alias, and Codex only honors it for MCP tool\n * output — a shell tool result cannot be rewritten at all. Both names are\n * modelled so a host mismatch is a typed mistake instead of a silently\n * ignored field.\n */\n updatedMCPToolOutput?: string;\n additionalContext?: string;\n}\n\n/** The stdout object of a hook invocation. `{}` means \"no instruction\". */\nexport interface HookOutput extends JsonObject {\n hookSpecificOutput?: HookSpecificOutput;\n decision?: 'block';\n reason?: string;\n}\n\n/** The empty output that always means \"allow, no instruction\". */\nexport function emptyHookOutput(): HookOutput {\n return {};\n}\n\n/** The output of one PreToolUse denial. */\nexport function preToolUseDeny(reason: string): HookOutput {\n return {\n hookSpecificOutput: {\n hookEventName: 'PreToolUse',\n permissionDecision: 'deny',\n permissionDecisionReason: reason\n }\n };\n}\n\n/** The output of one PostToolUse replacement. */\nexport function postToolUseReplace(text: string): HookOutput {\n return { hookSpecificOutput: { hookEventName: 'PostToolUse', updatedToolOutput: text } };\n}\n\n/**\n * The output of one Codex PostToolUse replacement.\n *\n * Codex reads `updatedMCPToolOutput` and only for MCP tools; emitting Claude's\n * `updatedToolOutput` there is silently ignored. Kept as a separate function so\n * the adapter cannot pick the wrong name by accident.\n * @param text - the replacement text for the MCP tool result.\n */\nexport function postToolUseMcpReplace(text: string): HookOutput {\n return { hookSpecificOutput: { hookEventName: 'PostToolUse', updatedMCPToolOutput: text } };\n}\n\n/** The output of one PostToolUseFailure correction. */\nexport function postToolUseFailureContext(text: string): HookOutput {\n return { hookSpecificOutput: { hookEventName: 'PostToolUseFailure', additionalContext: text } };\n}\n\n/** The output of one Stop objection. */\nexport function stopBlock(reason: string): HookOutput {\n return { decision: 'block', reason };\n}\n\n/** The output of one SessionStart notice. */\nexport function sessionStartContext(text: string): HookOutput {\n return { hookSpecificOutput: { hookEventName: 'SessionStart', additionalContext: text } };\n}\n"]}
|