Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion plugins/disk-hygiene/hooks/disk_hygiene.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
)
from cleanup_registry import Protected, registry
from cleanup_switch import cleanup_enabled
from hook_output import protocol_output


def parse_args():
Expand Down Expand Up @@ -111,7 +112,7 @@ def main():
"explicit session identity is required; no ownership inferred from cwd"
)
result = run_command(args, payload)
print(json.dumps(result))
print(json.dumps(protocol_output(args, result)))


def run_command(args, payload):
Expand Down
19 changes: 19 additions & 0 deletions plugins/disk-hygiene/hooks/hook_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Codex hook objects. Source: https://learn.chatgpt.com/docs/hooks"""

import json


def protocol_output(args, result):
if args.host != "codex" or args.command != "hook" or not isinstance(result, list):
return result
if not result:
return {}
report = json.dumps(result)
if args.event == "PostToolUse":
return {
"hookSpecificOutput": {
"hookEventName": args.event,
"additionalContext": report,
}
}
return {"systemMessage": report}
63 changes: 63 additions & 0 deletions tests/test_hook_output_protocol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""Codex output regression. Source: https://learn.chatgpt.com/docs/hooks"""

import importlib
import io
import json
from pathlib import Path
from types import SimpleNamespace

import pytest


@pytest.mark.parametrize(
"host,command,event,result,expected",
[
("codex", "hook", "PostToolUse", [], {}),
("codex", "hook", "Stop", [], {}),
(
"codex",
"hook",
"PostToolUse",
[{"protected": "active process"}],
{
"hookSpecificOutput": {
"hookEventName": "PostToolUse",
"additionalContext": '[{"protected": "active process"}]',
}
},
),
(
"codex",
"hook",
"Stop",
[{"removed": "owned path"}],
{"systemMessage": '[{"removed": "owned path"}]'},
),
(
"codex",
"hook",
"Stop",
{"decision": "block", "reason": "pending"},
{"decision": "block", "reason": "pending"},
),
("claude", "hook", "PostToolUse", [], []),
("codex", "status", None, [], []),
],
)
def test_main_emits_host_protocol(
monkeypatch, capsys, host, command, event, result, expected
):
monkeypatch.syspath_prepend(
str(Path(__file__).resolve().parents[1] / "plugins/disk-hygiene/hooks")
)
module = importlib.import_module("disk_hygiene")
args = SimpleNamespace(
host=host, command=command, event=event, session="test-session"
)
monkeypatch.setattr(module, "parse_args", lambda: args)
monkeypatch.setattr(module, "cleanup_enabled", lambda: True)
monkeypatch.setattr(module.transcript_policy, "delete_enabled", lambda: False)
monkeypatch.setattr(module, "run_command", lambda *args: result)
monkeypatch.setattr(module.sys, "stdin", io.StringIO("{}"))
module.main()
assert json.loads(capsys.readouterr().out) == expected
Loading