Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

fix(hooks): make every hook context read-only all the way down

Jack Rudenkob7577fd0bac7
diff --git a/packages/cli/src/hooks/freeze.ts b/packages/cli/src/hooks/freeze.ts
new file mode 100644
index 00000000..ca285742
--- /dev/null
+++ b/packages/cli/src/hooks/freeze.ts
@@ -0,0 +1,31 @@
+/**
+ * Read-only values for hook contexts.
+ *
+ * A context is frozen at its top level by the engine, but a frozen object's FIELDS can
+ * still be objects a hook writes into: `ctx.prior[i].action.append`, `ctx.harness`,
+ * `ctx.request.tools[i].input_schema`, `ctx.response.blocks[k].input`. Each of those is
+ * also something claudish reads after the hook — the next step's view, a stage-B replay,
+ * the payload sent to the provider or the client — so a write would change what claudish
+ * does with no journal line, under the wrong hook id, or from a warn-severity hook. Every
+ * value a hook can reach is therefore either a deep-frozen copy or deep-frozen in place
+ * when claudish owns it and never writes it again.
+ *
+ * Plain data only (actions, facts, views): no functions, no class instances.
+ */
+
+/** Freeze `value` and everything reachable from it, in place. Returns it. */
+export function deepFreeze<T>(value: T): T {
+ if (value === null || typeof value !== "object" || Object.isFrozen(value)) return value;
+ Object.freeze(value);
+ for (const key of Reflect.ownKeys(value as object)) {
+ const desc = Object.getOwnPropertyDescriptor(value as object, key);
+ // An accessor is left alone: calling it here would defeat a lazy view.
+ if (desc && "value" in desc) deepFreeze(desc.value);
+ }
+ return value;
+}
+
+/** A deep-frozen copy of plain data, for a value a hook sees while claudish keeps the original. */
+export function frozenClone<T>(value: T): T {
+ return deepFreeze(structuredClone(value));
+}
diff --git a/packages/cli/src/hooks/harness.ts b/packages/cli/src/hooks/harness.ts
index 5908dad4..8a713af4 100644
--- a/packages/cli/src/hooks/harness.ts
+++ b/packages/cli/src/hooks/harness.ts
@@ -157,8 +157,6 @@ function matchPlanPath(text: string): string | undefined {
* when the conversation is the ~180K tokens where this bug actually bites.
*/
export function detectHarnessFacts(claudeRequest: any): HarnessFacts {
- const facts: HarnessFacts = { planModeActive: false };
-
let planPath = matchPlanPath(textOf(claudeRequest?.system));
if (!planPath && Array.isArray(claudeRequest?.messages)) {
@@ -169,12 +167,9 @@ export function detectHarnessFacts(claudeRequest: any): HarnessFacts {
}
}
- if (planPath) {
- facts.planModeActive = true;
- facts.planFilePath = planPath;
- const slash = planPath.lastIndexOf("/");
- if (slash > 0) facts.planDir = planPath.slice(0, slash);
- }
-
- return facts;
+ if (!planPath) return { planModeActive: false };
+ const slash = planPath.lastIndexOf("/");
+ return slash > 0
+ ? { planModeActive: true, planFilePath: planPath, planDir: planPath.slice(0, slash) }
+ : { planModeActive: true, planFilePath: planPath };
}