fix: support Codex CLI 0.142 hook schema

This commit is contained in:
thanhnv
2026-07-24 16:49:44 +07:00
parent 6804fd14f4
commit 353d87b6ae
6 changed files with 50 additions and 8 deletions
+20 -3
View File
@@ -89,6 +89,16 @@ CLIENT_SURFACES = {
"contract": "casan_owned_explicit_route",
},
}
CASAN_CODEX_DESCRIPTIONS = {
(
"CASAN Plan-20 lifecycle hooks. Review with /hooks; the project "
"bootstrap resolves and verifies the pinned global harness."
),
(
"CASAN lifecycle hooks. Review with /hooks; the project bootstrap "
"resolves and verifies the pinned Core runtime."
),
}
RUNTIME_MODES = ("managed", "vendored")
@@ -642,10 +652,12 @@ def _remove_marker_hooks(doc, marker):
return changed
def merge_json_hooks(target_file, template_file, marker, backups):
def merge_json_hooks(
target_file, template_file, marker, backups, owned_scalars=None):
"""MERGE the template's hook groups into an existing hooks JSON without
clobbering the user's own hooks/agents/skills config. Existing CASAN-owned
handlers are replaced so upgrades cannot leave a stale command behind.
handlers and explicitly listed CASAN-owned scalar metadata are replaced so
upgrades cannot leave a stale or client-incompatible value behind.
Returns created|merged|unchanged|None."""
tmpl = _load_json_or(template_file, None)
if not isinstance(tmpl, dict):
@@ -655,6 +667,9 @@ def merge_json_hooks(target_file, template_file, marker, backups):
if not isinstance(doc, dict):
doc = {}
before = json.dumps(doc, ensure_ascii=False, sort_keys=True)
for key, owned_values in (owned_scalars or {}).items():
if doc.get(key) in owned_values:
doc.pop(key, None)
hooks = doc.get("hooks")
if not isinstance(hooks, dict):
hooks = {}
@@ -1271,7 +1286,9 @@ def cmd_init(args):
mark_owned_if_absent(dsth)
remove_json_hooks(dsth, "codex_hook.py", backups)
r = merge_json_hooks(dsth, os.path.join(ad, "codex", "hooks.template.json"),
"casan-hook.py", backups)
"casan-hook.py", backups, {
"description": CASAN_CODEX_DESCRIPTIONS,
})
if r and r != "unchanged":
created_add(dsth)
merges[".codex/hooks.json"] = r