oh-my-codex 0.22.7 → 0.22.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oh-my-codex",
3
- "version": "0.22.7",
3
+ "version": "0.22.8",
4
4
  "description": "Multi-agent orchestration layer for OpenAI Codex CLI",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oh-my-codex",
3
- "version": "0.22.7",
3
+ "version": "0.22.8",
4
4
  "description": "Multi-agent orchestration layer for OpenAI Codex CLI",
5
5
  "author": {
6
6
  "name": "Yeachan Heo",
@@ -0,0 +1,203 @@
1
+ import assert from "node:assert/strict";
2
+ import { mkdir, mkdtemp, readFile, rm, utimes, writeFile } from "node:fs/promises";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { afterEach, describe, it } from "node:test";
6
+ import { dispatchCodexNativeHook } from "../codex-native-hook.js";
7
+ import { initTeamState, writeWorkerIdentity } from "../../team/state.js";
8
+
9
+ const roots: string[] = [];
10
+ afterEach(async () => {
11
+ await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true })));
12
+ });
13
+
14
+ async function workspace(): Promise<string> {
15
+ const cwd = await mkdtemp(join(tmpdir(), "omx-issue-4086-stop-hook-lock-"));
16
+ roots.push(cwd);
17
+ return cwd;
18
+ }
19
+
20
+ async function staleLock(lockDir: string, token?: string): Promise<void> {
21
+ await mkdir(lockDir, { recursive: true });
22
+ if (token !== undefined) await writeFile(join(lockDir, 'owner'), token);
23
+ await writeFile(join(lockDir, 'lease'), 'abandoned');
24
+ const old = new Date(Date.now() - 10 * 60_000);
25
+ await utimes(join(lockDir, 'lease'), old, old);
26
+ await utimes(lockDir, old, old);
27
+ }
28
+
29
+ describe('issue #4086: stop hook with stale team lock', () => {
30
+ it('stop hook with stale team lock should return continue:false (terminal)', async () => {
31
+ const cwd = await workspace();
32
+ const team = 'audit';
33
+ const workerName = 'executor-1';
34
+
35
+ // Initialize team state
36
+ await initTeamState(team, 'stop hook lock test', 'executor', 1, cwd);
37
+
38
+ // Write worker identity to make this appear as a team worker
39
+ await writeWorkerIdentity(team, workerName, {
40
+ name: workerName,
41
+ index: 1,
42
+ role: 'executor',
43
+ assigned_tasks: [],
44
+ }, cwd);
45
+
46
+ // Create a stale team membership lock
47
+ const lockDir = join(cwd, '.omx/state/.team-locks', `${team}.membership`);
48
+ const staleLockToken = '99999999.0.dead';
49
+ await staleLock(lockDir, staleLockToken);
50
+
51
+ // Set up environment to identify as a team worker
52
+ const originalEnv = process.env;
53
+ process.env.OMX_TEAM_WORKER_NAME = workerName;
54
+ process.env.OMX_TEAM_WORKER_TEAM_NAME = team;
55
+ process.env.OMX_TEAM_WORKER_TEAM_LEADER = 'leader-1';
56
+ process.env.OMX_TEAM_WORKER_INTERNAL_CANONICAL = 'true';
57
+
58
+ try {
59
+ // Invoke Stop hook while team has a stale lock
60
+ const result = await dispatchCodexNativeHook(
61
+ {
62
+ hook_event_name: "Stop",
63
+ cwd,
64
+ session_id: "test-stop-hook",
65
+ stop_hook_active: false,
66
+ },
67
+ { cwd },
68
+ );
69
+
70
+ // Exact assertions for terminal output: continue:false, no decision, no reason
71
+ assert.ok(result.outputJson, 'outputJson must be present');
72
+ const outputJson = result.outputJson as Record<string, unknown>;
73
+
74
+ assert.strictEqual(
75
+ outputJson.continue,
76
+ false,
77
+ 'continue must be false to make Stop terminal'
78
+ );
79
+
80
+ assert.strictEqual(
81
+ outputJson.decision,
82
+ undefined,
83
+ 'decision must be undefined for terminal output (no decision property)'
84
+ );
85
+
86
+ assert.strictEqual(
87
+ outputJson.reason,
88
+ undefined,
89
+ 'reason must be undefined so model never receives recovery instructions'
90
+ );
91
+
92
+ assert.strictEqual(
93
+ outputJson.stopReason,
94
+ 'team_lock_recovery_required',
95
+ 'stopReason must identify the lock recovery issue'
96
+ );
97
+
98
+ // Recovery steps must be only in systemMessage, never exposed to model as decision/reason
99
+ const systemMessage = String(outputJson.systemMessage || '');
100
+ assert.ok(
101
+ systemMessage.length > 0,
102
+ 'systemMessage must contain recovery instructions'
103
+ );
104
+ assert.ok(
105
+ systemMessage.includes('stale lock') || systemMessage.includes(lockDir),
106
+ 'systemMessage must describe the stale lock location'
107
+ );
108
+ } finally {
109
+ process.env = originalEnv;
110
+ }
111
+ });
112
+
113
+ it('two consecutive Stop invocations are both terminal with lock preserved', async () => {
114
+ const cwd = await workspace();
115
+ const team = 'audit';
116
+ const workerName = 'executor-1';
117
+
118
+ // Initialize team state
119
+ await initTeamState(team, 'lock preservation test', 'executor', 1, cwd);
120
+
121
+ const lockDir = join(cwd, '.omx/state/.team-locks', `${team}.membership`);
122
+ const staleLockToken = '99999999.0.dead';
123
+ await staleLock(lockDir, staleLockToken);
124
+
125
+ // Write worker identity
126
+ await writeWorkerIdentity(team, workerName, {
127
+ name: workerName,
128
+ index: 1,
129
+ role: 'executor',
130
+ assigned_tasks: [],
131
+ }, cwd);
132
+
133
+ const originalEnv = process.env;
134
+ process.env.OMX_TEAM_WORKER_NAME = workerName;
135
+ process.env.OMX_TEAM_WORKER_TEAM_NAME = team;
136
+ process.env.OMX_TEAM_WORKER_TEAM_LEADER = 'leader-1';
137
+ process.env.OMX_TEAM_WORKER_INTERNAL_CANONICAL = 'true';
138
+
139
+ try {
140
+ // First Stop invocation
141
+ const result1 = await dispatchCodexNativeHook(
142
+ {
143
+ hook_event_name: "Stop",
144
+ cwd,
145
+ session_id: "test-stop-hook-1",
146
+ stop_hook_active: false,
147
+ },
148
+ { cwd },
149
+ );
150
+
151
+ assert.ok(result1.outputJson, 'First Stop result must have outputJson');
152
+ const output1 = result1.outputJson as Record<string, unknown>;
153
+ assert.strictEqual(
154
+ output1.continue,
155
+ false,
156
+ 'First Stop must have continue:false (terminal)'
157
+ );
158
+ assert.strictEqual(
159
+ output1.stopReason,
160
+ 'team_lock_recovery_required',
161
+ 'First Stop must identify lock recovery reason'
162
+ );
163
+
164
+ // Verify lock state is preserved after first Stop
165
+ let owner = await readFile(join(lockDir, 'owner'), 'utf8');
166
+ let lease = await readFile(join(lockDir, 'lease'), 'utf8');
167
+ assert.strictEqual(owner, staleLockToken, 'Lock owner must be preserved after first Stop');
168
+ assert.strictEqual(lease, 'abandoned', 'Lock lease must be preserved after first Stop');
169
+
170
+ // Second Stop invocation (simulating operator retrying after fixing)
171
+ const result2 = await dispatchCodexNativeHook(
172
+ {
173
+ hook_event_name: "Stop",
174
+ cwd,
175
+ session_id: "test-stop-hook-2",
176
+ stop_hook_active: false,
177
+ },
178
+ { cwd },
179
+ );
180
+
181
+ assert.ok(result2.outputJson, 'Second Stop result must have outputJson');
182
+ const output2 = result2.outputJson as Record<string, unknown>;
183
+ assert.strictEqual(
184
+ output2.continue,
185
+ false,
186
+ 'Second Stop must have continue:false (terminal)'
187
+ );
188
+ assert.strictEqual(
189
+ output2.stopReason,
190
+ 'team_lock_recovery_required',
191
+ 'Second Stop must identify lock recovery reason'
192
+ );
193
+
194
+ // Verify lock state is still preserved
195
+ owner = await readFile(join(lockDir, 'owner'), 'utf8');
196
+ lease = await readFile(join(lockDir, 'lease'), 'utf8');
197
+ assert.strictEqual(owner, staleLockToken, 'Lock owner must be preserved after second Stop');
198
+ assert.strictEqual(lease, 'abandoned', 'Lock lease must be preserved after second Stop');
199
+ } finally {
200
+ process.env = originalEnv;
201
+ }
202
+ });
203
+ });
@@ -2933,9 +2933,18 @@ async function resolveTeamStateDirForWorkerContext(
2933
2933
  cwd: string,
2934
2934
  workerContext: { teamName: string; workerName: string },
2935
2935
  ): Promise<string | null> {
2936
- const resolved = await resolveWorkerTeamStateRootPath(cwd, workerContext, process.env).catch(() => null);
2937
- if (resolved) return resolved;
2938
- return null;
2936
+ try {
2937
+ const resolved = await resolveWorkerTeamStateRootPath(cwd, workerContext, process.env);
2938
+ if (resolved) return resolved;
2939
+ return null;
2940
+ } catch (error) {
2941
+ // Re-throw team lock recovery errors so they're handled by the Stop hook error handler
2942
+ if (error instanceof Error && error.message.includes('team_lock_recovery_required')) {
2943
+ throw error;
2944
+ }
2945
+ // Suppress other errors (state root resolution failures)
2946
+ return null;
2947
+ }
2939
2948
  }
2940
2949
 
2941
2950
  async function isConfirmedTeamWorkerPromptSubmitPane(cwd: string): Promise<boolean> {
@@ -22891,41 +22900,51 @@ export async function dispatchCodexNativeHook(
22891
22900
  }
22892
22901
  outputJson ??= buildNativePostToolUseOutput(payload);
22893
22902
  } else if (hookEventName === "Stop") {
22894
- if (declaredTeamWorkerStopOnly) {
22895
- outputJson = await buildStopHookOutput(payload, cwd, stateDir, { teamWorkerOnly: true });
22896
- } else if (allowImplicitSessionSideEffects) {
22897
- const stopOutput = await buildStopHookOutput(payload, cwd, stateDir, {
22898
- canonicalSessionId: canonicalSessionId || undefined,
22899
- skipRalphStopBlock: isSubagentStop,
22900
- skipAutoNudge: isSubagentStop,
22901
- sessionScopedOnly: !allowGlobalSideEffects,
22902
- });
22903
- // Record bounded completion evidence for native subagent Stop events
22904
- if (isSubagentStop) {
22905
- const threadId = readPayloadThreadId(payload);
22906
- if (canonicalSessionId?.trim() && threadId?.trim()) {
22907
- await recordSubagentTurnForSession(cwd, {
22908
- sessionId: canonicalSessionId.trim(),
22909
- threadId: threadId.trim(),
22910
- turnId: readPayloadTurnId(payload) || undefined,
22911
- completed: true,
22912
- completionSource: 'native_stop_event',
22913
- timestamp: new Date().toISOString(),
22914
- }).catch(() => {});
22903
+ try {
22904
+ if (declaredTeamWorkerStopOnly) {
22905
+ outputJson = await buildStopHookOutput(payload, cwd, stateDir, { teamWorkerOnly: true });
22906
+ } else if (allowImplicitSessionSideEffects) {
22907
+ const stopOutput = await buildStopHookOutput(payload, cwd, stateDir, {
22908
+ canonicalSessionId: canonicalSessionId || undefined,
22909
+ skipRalphStopBlock: isSubagentStop,
22910
+ skipAutoNudge: isSubagentStop,
22911
+ sessionScopedOnly: !allowGlobalSideEffects,
22912
+ });
22913
+ // Record bounded completion evidence for native subagent Stop events
22914
+ if (isSubagentStop) {
22915
+ const threadId = readPayloadThreadId(payload);
22916
+ if (canonicalSessionId?.trim() && threadId?.trim()) {
22917
+ await recordSubagentTurnForSession(cwd, {
22918
+ sessionId: canonicalSessionId.trim(),
22919
+ threadId: threadId.trim(),
22920
+ turnId: readPayloadTurnId(payload) || undefined,
22921
+ completed: true,
22922
+ completionSource: 'native_stop_event',
22923
+ timestamp: new Date().toISOString(),
22924
+ }).catch(() => {});
22925
+ }
22915
22926
  }
22927
+ outputJson = stopOutput ?? (
22928
+ allowGlobalSideEffects
22929
+ ? await buildCompletedGoalCleanupStopOutput(payload, cwd)
22930
+ : null
22931
+ );
22932
+ } else {
22933
+ // Authorization failure never grants continuation authority. It also must not
22934
+ // inject a repair request: the same provenance failure can deny every tool the
22935
+ // model would need to repair it, turning a Stop denial into an unbounded loop.
22936
+ // Returning no hook output permits termination while preserving the denial of
22937
+ // all session-scoped and global side effects above.
22938
+ outputJson = null;
22939
+ }
22940
+ } catch (err) {
22941
+ // Handle errors from Stop hook processing, particularly manual recovery errors
22942
+ // like team_lock_recovery_required that should not result in continuation
22943
+ if (isTeamLockRecoveryRequiredError(err)) {
22944
+ outputJson = buildTeamLockRecoveryStopOutput(err);
22945
+ } else {
22946
+ throw err;
22916
22947
  }
22917
- outputJson = stopOutput ?? (
22918
- allowGlobalSideEffects
22919
- ? await buildCompletedGoalCleanupStopOutput(payload, cwd)
22920
- : null
22921
- );
22922
- } else {
22923
- // Authorization failure never grants continuation authority. It also must not
22924
- // inject a repair request: the same provenance failure can deny every tool the
22925
- // model would need to repair it, turning a Stop denial into an unbounded loop.
22926
- // Returning no hook output permits termination while preserving the denial of
22927
- // all session-scoped and global side effects above.
22928
- outputJson = null;
22929
22948
  }
22930
22949
  }
22931
22950
 
@@ -23194,7 +23213,36 @@ function isPostCancelCommitTestTrigger(payload: CodexHookPayload, output: Record
23194
23213
  }
23195
23214
 
23196
23215
 
23216
+ function isTeamLockRecoveryRequiredError(error: unknown): boolean {
23217
+ if (!(error instanceof Error)) return false;
23218
+ return error.message.includes('team_lock_recovery_required');
23219
+ }
23220
+
23221
+ function extractTeamLockDirFromError(error: unknown): string | null {
23222
+ if (!(error instanceof Error)) return null;
23223
+ const match = error.message.match(/team_lock_recovery_required:([^:]+):/);
23224
+ return match ? match[1] : null;
23225
+ }
23226
+
23227
+ function buildTeamLockRecoveryStopOutput(error: unknown): Record<string, unknown> {
23228
+ const lockDir = extractTeamLockDirFromError(error);
23229
+ const systemMessage = lockDir
23230
+ ? `OMX Stop terminal: stale team lock at ${lockDir} requires manual recovery. ` +
23231
+ `Confirm no other operations are accessing the lock, then remove: rm -rf "${lockDir}"`
23232
+ : `OMX Stop terminal: stale team lock requires manual recovery.`;
23233
+ return {
23234
+ continue: false,
23235
+ stopReason: "team_lock_recovery_required",
23236
+ systemMessage,
23237
+ };
23238
+ }
23239
+
23197
23240
  function buildStopDispatchFailureOutput(error: unknown): Record<string, unknown> {
23241
+ // Check if this is a manual recovery error that should be treated as terminal
23242
+ if (isTeamLockRecoveryRequiredError(error)) {
23243
+ return buildTeamLockRecoveryStopOutput(error);
23244
+ }
23245
+
23198
23246
  const detail = error instanceof Error ? error.message : String(error);
23199
23247
  const reason =
23200
23248
  "OMX native Stop hook failed before normal continuation handling. Continue once more, preserve runtime state, inspect the hook logs, and retry with a valid Stop JSON response.";