Skip to content

Commit e1b8ae3

Browse files
zknprclaude
andcommitted
fix(codex-rescue-hook): only claim success with positive evidence
Addresses the PR #346 Codex review (P1): buildCompletionContext fell back to the "COMPLETED ... final result" line for any synchronous return without a sentinel — including failed/errored/empty returns — falsely signaling success and suppressing retry/escalation. Now success is asserted only when the [[codex-task status=complete]] sentinel is present. A failure status (failed/error/timeout/...) or an empty return (codex-rescue returns nothing when Codex can't be invoked) gets a failure-aware "exited without a success signal — may have failed, verify/retry" line; any other tokenless return gets a neutral "exited, verify on disk" line. Every branch still says "do not wait" so a failed run doesn't reintroduce the original waiting bug. Tests: 94/94 (added failed-status and empty-return cases; tokenless default is now neutral). Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent d651c50 commit e1b8ae3

2 files changed

Lines changed: 86 additions & 7 deletions

File tree

‎plugins/codex/scripts/codex-rescue-completion-hook.mjs‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ import { parseTaskStatusToken } from "./lib/task-status-token.mjs";
77

88
const COMPLETE_CONTEXT =
99
"codex-rescue has COMPLETED and exited. The text above is the final result. Do NOT wait for a notification or poll status. If it ran with --write, verify changed files with git status.";
10+
const NEUTRAL_EXIT_CONTEXT =
11+
"codex-rescue has exited (synchronous return — it is not running). Treat the text above as its result and verify on disk (`git status` if it ran with --write); do not wait for a notification or poll status.";
12+
const FAILURE_CONTEXT =
13+
"codex-rescue exited WITHOUT a success signal — it is not running, so do not wait for a notification, but the run may have failed or produced no result. Review the output above and `git status`, then re-run or escalate instead of treating it as done.";
14+
// Known non-success statuses. Without the complete sentinel, one of these (or
15+
// an empty body) means there is no success signal, so we must not claim the
16+
// run succeeded (PR #346 review P1).
17+
const FAILURE_STATUSES = new Set(["failed", "fail", "error", "errored", "cancelled", "canceled", "timed_out", "timeout", "aborted"]);
1018
const BASH_AUTO_BACKGROUND_MARKER = "Command running in background with ID:";
1119
const BASH_AUTO_BACKGROUND_OUTPUT_PREFIX = "Output is being written to:";
1220

@@ -76,6 +84,16 @@ function isSynchronousAgentReturn(toolResponse) {
7684
return status !== "async_launched" && status !== "running";
7785
}
7886

87+
function isFailedOrEmptyReturn(toolResponse, responseText) {
88+
// codex-rescue returns nothing when Codex can't be invoked; an empty body or
89+
// a known failure status means there is no success signal to report.
90+
if (String(responseText ?? "").trim() === "") {
91+
return true;
92+
}
93+
const status = String(toolResponse?.status ?? "").toLowerCase();
94+
return FAILURE_STATUSES.has(status);
95+
}
96+
7997
function collectText(value) {
8098
if (value == null) {
8199
return "";
@@ -119,20 +137,28 @@ function buildCompletionContext(input) {
119137
if (token?.status === "dispatched" && token.id) {
120138
return buildDispatchedContext(token.id);
121139
}
140+
// The complete sentinel is the only positive proof of a successful run; the
141+
// companion stamps it solely on real completion, so it is the lone path that
142+
// asserts success (PR #346 review P1).
122143
if (token?.status === "complete") {
123144
return COMPLETE_CONTEXT;
124145
}
125146

126-
// Bash auto-backgrounds commands that exceed the 600s foreground cap. That
147+
// Bash auto-backgrounds commands that exceed the ~600s foreground cap. That
127148
// synchronous Agent return means the Bash wrapper detached, not that Codex
128-
// completed, so report the still-running state before tokenless completion.
149+
// completed, so report the still-running state before any completion claim.
129150
if (responseText.includes(BASH_AUTO_BACKGROUND_MARKER)) {
130151
return buildBashAutoBackgroundContext(extractBashAutoBackgroundOutputPath(responseText));
131152
}
132153

133-
// Token absence still means completion here because PostToolUse fires only
134-
// after a synchronous Agent tool call has returned to the parent thread.
135-
return COMPLETE_CONTEXT;
154+
// No success evidence: never claim success here. A failure status or an empty
155+
// return gets a failure-aware line; everything else gets a neutral "exited,
156+
// verify on disk" line. Every branch still says "do not wait" because a
157+
// synchronous Agent return means the agent has exited.
158+
if (isFailedOrEmptyReturn(toolResponse, responseText)) {
159+
return FAILURE_CONTEXT;
160+
}
161+
return NEUTRAL_EXIT_CONTEXT;
136162
}
137163

138164
function main() {

‎tests/codex-rescue-hook.test.mjs‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ test("codex-rescue hook injects a complete line when the completion token is pre
5050
});
5151
});
5252

53-
test("codex-rescue hook defaults synchronous tokenless returns to complete", () => {
53+
test("codex-rescue hook reports synchronous tokenless returns neutrally", () => {
5454
const result = runHook({
5555
hook_event_name: "PostToolUse",
5656
tool_name: "Agent",
@@ -72,7 +72,60 @@ test("codex-rescue hook defaults synchronous tokenless returns to complete", ()
7272
const payload = parseHookOutput(result);
7373
assert.equal(
7474
payload.hookSpecificOutput.additionalContext,
75-
"codex-rescue has COMPLETED and exited. The text above is the final result. Do NOT wait for a notification or poll status. If it ran with --write, verify changed files with git status."
75+
"codex-rescue has exited (synchronous return — it is not running). Treat the text above as its result and verify on disk (`git status` if it ran with --write); do not wait for a notification or poll status."
76+
);
77+
assert.doesNotMatch(payload.hookSpecificOutput.additionalContext, /final result/);
78+
});
79+
80+
test("codex-rescue hook reports failed tokenless returns without a success signal", () => {
81+
const result = runHook({
82+
hook_event_name: "PostToolUse",
83+
tool_name: "Agent",
84+
tool_input: {
85+
subagent_type: "codex:codex-rescue"
86+
},
87+
tool_response: {
88+
status: "failed",
89+
agentId: "agent-failed",
90+
content: [
91+
{
92+
type: "text",
93+
text: "Codex could not be invoked.\n"
94+
}
95+
]
96+
}
97+
});
98+
99+
const payload = parseHookOutput(result);
100+
assert.equal(
101+
payload.hookSpecificOutput.additionalContext,
102+
"codex-rescue exited WITHOUT a success signal — it is not running, so do not wait for a notification, but the run may have failed or produced no result. Review the output above and `git status`, then re-run or escalate instead of treating it as done."
103+
);
104+
});
105+
106+
test("codex-rescue hook reports empty tokenless returns without a success signal", () => {
107+
const result = runHook({
108+
hook_event_name: "PostToolUse",
109+
tool_name: "Agent",
110+
tool_input: {
111+
subagent_type: "codex:codex-rescue"
112+
},
113+
tool_response: {
114+
status: "completed",
115+
agentId: "agent-empty",
116+
content: [
117+
{
118+
type: "text",
119+
text: ""
120+
}
121+
]
122+
}
123+
});
124+
125+
const payload = parseHookOutput(result);
126+
assert.equal(
127+
payload.hookSpecificOutput.additionalContext,
128+
"codex-rescue exited WITHOUT a success signal — it is not running, so do not wait for a notification, but the run may have failed or produced no result. Review the output above and `git status`, then re-run or escalate instead of treating it as done."
76129
);
77130
});
78131

0 commit comments

Comments
 (0)