harnery 0.31.6 → 0.33.0

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.
Files changed (264) hide show
  1. package/README.md +2 -0
  2. package/dist/commander.d.ts.map +1 -1
  3. package/dist/commander.js +5 -1
  4. package/dist/commands/agents.d.ts +0 -9
  5. package/dist/commands/agents.d.ts.map +1 -1
  6. package/dist/commands/agents.js +280 -94
  7. package/dist/commands/browse-session.d.ts +21 -0
  8. package/dist/commands/browse-session.d.ts.map +1 -0
  9. package/dist/commands/browse-session.js +157 -0
  10. package/dist/commands/browse.d.ts.map +1 -1
  11. package/dist/commands/browse.js +205 -37
  12. package/dist/commands/checkpoint.js +1 -1
  13. package/dist/commands/claude-desktop.d.ts +2 -2
  14. package/dist/commands/claude-desktop.d.ts.map +1 -1
  15. package/dist/commands/claude-desktop.js +74 -3
  16. package/dist/commands/deinit.d.ts.map +1 -1
  17. package/dist/commands/deinit.js +4 -0
  18. package/dist/commands/docs.d.ts.map +1 -1
  19. package/dist/commands/docs.js +40 -0
  20. package/dist/commands/doctor.d.ts +4 -2
  21. package/dist/commands/doctor.d.ts.map +1 -1
  22. package/dist/commands/doctor.js +141 -19
  23. package/dist/commands/init.d.ts +2 -2
  24. package/dist/commands/init.d.ts.map +1 -1
  25. package/dist/commands/init.js +26 -5
  26. package/dist/commands/instructions.d.ts +4 -0
  27. package/dist/commands/instructions.d.ts.map +1 -0
  28. package/dist/commands/instructions.js +100 -0
  29. package/dist/commands/presence.d.ts.map +1 -1
  30. package/dist/commands/presence.js +4 -1
  31. package/dist/commands/tunnel.d.ts.map +1 -1
  32. package/dist/commands/tunnel.js +2 -3
  33. package/dist/core/agents/canonical-emit.d.ts +2 -3
  34. package/dist/core/agents/canonical-emit.d.ts.map +1 -1
  35. package/dist/core/agents/canonical-emit.js +6 -5
  36. package/dist/core/agents/cli.js +128 -89
  37. package/dist/core/agents/coord-client.d.ts +18 -3
  38. package/dist/core/agents/coord-client.d.ts.map +1 -1
  39. package/dist/core/agents/coord-client.js +5 -4
  40. package/dist/core/agents/finalization.d.ts +68 -0
  41. package/dist/core/agents/finalization.d.ts.map +1 -0
  42. package/dist/core/agents/finalization.js +443 -0
  43. package/dist/core/agents/git-hook.d.ts +51 -0
  44. package/dist/core/agents/git-hook.d.ts.map +1 -0
  45. package/dist/core/agents/git-hook.js +118 -0
  46. package/dist/core/agents/index.d.ts +1 -0
  47. package/dist/core/agents/index.d.ts.map +1 -1
  48. package/dist/core/agents/index.js +1 -0
  49. package/dist/core/agents/render/prompt-context.d.ts +12 -5
  50. package/dist/core/agents/render/prompt-context.d.ts.map +1 -1
  51. package/dist/core/agents/render/prompt-context.js +70 -16
  52. package/dist/core/agents/render/session-context.d.ts.map +1 -1
  53. package/dist/core/agents/render/session-context.js +23 -4
  54. package/dist/core/agents/rules/claim-conflict.js +3 -3
  55. package/dist/core/agents/rules/commit-conflict.d.ts +15 -6
  56. package/dist/core/agents/rules/commit-conflict.d.ts.map +1 -1
  57. package/dist/core/agents/rules/commit-conflict.js +21 -5
  58. package/dist/core/agents/rules/stop-hook.d.ts +3 -0
  59. package/dist/core/agents/rules/stop-hook.d.ts.map +1 -1
  60. package/dist/core/agents/rules/stop-hook.js +59 -23
  61. package/dist/core/agents/session-events.d.ts +8 -16
  62. package/dist/core/agents/session-events.d.ts.map +1 -1
  63. package/dist/core/agents/session-events.js +12 -26
  64. package/dist/core/agents/state/heartbeat-projector.d.ts +14 -0
  65. package/dist/core/agents/state/heartbeat-projector.d.ts.map +1 -1
  66. package/dist/core/agents/state/heartbeat-projector.js +98 -1
  67. package/dist/core/agents/state/heartbeat-writer.d.ts +47 -5
  68. package/dist/core/agents/state/heartbeat-writer.d.ts.map +1 -1
  69. package/dist/core/agents/state/heartbeat-writer.js +86 -17
  70. package/dist/core/agents/state/names.d.ts +22 -1
  71. package/dist/core/agents/state/names.d.ts.map +1 -1
  72. package/dist/core/agents/state/names.js +40 -2
  73. package/dist/core/agents/state/session-state.d.ts +44 -0
  74. package/dist/core/agents/state/session-state.d.ts.map +1 -0
  75. package/dist/core/agents/state/session-state.js +81 -0
  76. package/dist/core/config.d.ts +30 -2
  77. package/dist/core/config.d.ts.map +1 -1
  78. package/dist/core/config.js +74 -11
  79. package/dist/core/governor/planning.d.ts.map +1 -1
  80. package/dist/core/governor/planning.js +12 -13
  81. package/dist/core/hooks/adapter/detect.d.ts +2 -4
  82. package/dist/core/hooks/adapter/detect.d.ts.map +1 -1
  83. package/dist/core/hooks/adapter/detect.js +4 -14
  84. package/dist/core/hooks/adapter/events.d.ts.map +1 -1
  85. package/dist/core/hooks/adapter/events.js +1 -0
  86. package/dist/core/hooks/adapter/output.d.ts +1 -1
  87. package/dist/core/hooks/adapter/output.d.ts.map +1 -1
  88. package/dist/core/hooks/adapter/output.js +5 -4
  89. package/dist/core/hooks/adapter/parse.d.ts +1 -1
  90. package/dist/core/hooks/adapter/parse.d.ts.map +1 -1
  91. package/dist/core/hooks/adapter/parse.js +2 -0
  92. package/dist/core/hooks/adapter/wiring.d.ts +23 -0
  93. package/dist/core/hooks/adapter/wiring.d.ts.map +1 -1
  94. package/dist/core/hooks/adapter/wiring.js +32 -0
  95. package/dist/core/hooks/cli.d.ts +1 -2
  96. package/dist/core/hooks/cli.d.ts.map +1 -1
  97. package/dist/core/hooks/cli.js +258 -32
  98. package/dist/core/hooks/codex-authorization.d.ts +30 -0
  99. package/dist/core/hooks/codex-authorization.d.ts.map +1 -0
  100. package/dist/core/hooks/codex-authorization.js +199 -0
  101. package/dist/core/hooks/codex-wsl-bridge.d.ts +46 -0
  102. package/dist/core/hooks/codex-wsl-bridge.d.ts.map +1 -0
  103. package/dist/core/hooks/codex-wsl-bridge.js +137 -0
  104. package/dist/core/hooks/effects/index.d.ts +1 -1
  105. package/dist/core/hooks/effects/index.js +3 -3
  106. package/dist/core/hooks/events/schema.d.ts +57 -1
  107. package/dist/core/hooks/events/schema.d.ts.map +1 -1
  108. package/dist/core/hooks/resolve/owner.d.ts +7 -1
  109. package/dist/core/hooks/resolve/owner.d.ts.map +1 -1
  110. package/dist/core/hooks/resolve/owner.js +15 -0
  111. package/dist/core/hooks/resolve/transcript.d.ts +44 -0
  112. package/dist/core/hooks/resolve/transcript.d.ts.map +1 -1
  113. package/dist/core/hooks/resolve/transcript.js +176 -1
  114. package/dist/core/hooks/session-name-presence.d.ts +30 -0
  115. package/dist/core/hooks/session-name-presence.d.ts.map +1 -0
  116. package/dist/core/hooks/session-name-presence.js +43 -0
  117. package/dist/core/hooks/unsafe-cross-shell.d.ts +13 -0
  118. package/dist/core/hooks/unsafe-cross-shell.d.ts.map +1 -0
  119. package/dist/core/hooks/unsafe-cross-shell.js +151 -0
  120. package/dist/core/presence/blob.d.ts +4 -0
  121. package/dist/core/presence/blob.d.ts.map +1 -1
  122. package/dist/core/presence/blob.js +12 -0
  123. package/dist/core/presence/index.js +15 -1
  124. package/dist/core/work/state.d.ts +4 -5
  125. package/dist/core/work/state.d.ts.map +1 -1
  126. package/dist/core/work/state.js +5 -8
  127. package/dist/core/workflow/index.d.ts +1 -1
  128. package/dist/core/workflow/index.d.ts.map +1 -1
  129. package/dist/core/workflow/proof.d.ts +2 -2
  130. package/dist/core/workflow/proof.d.ts.map +1 -1
  131. package/dist/core/workflow/proof.js +2 -2
  132. package/dist/core/workflow/run-state.d.ts +2 -2
  133. package/dist/core/workflow/run-state.d.ts.map +1 -1
  134. package/dist/core/workflow/run-state.js +2 -2
  135. package/dist/core/workflow/types.d.ts +3 -3
  136. package/dist/core/workflow/types.d.ts.map +1 -1
  137. package/dist/core/workflow/workspaces/execution.d.ts +2 -2
  138. package/dist/core/workflow/workspaces/execution.d.ts.map +1 -1
  139. package/dist/core/workflow/workspaces/execution.js +5 -0
  140. package/dist/core/workflow/workspaces/index.d.ts +1 -1
  141. package/dist/core/workflow/workspaces/index.d.ts.map +1 -1
  142. package/dist/core/workflow/workspaces/local-git.d.ts.map +1 -1
  143. package/dist/core/workflow/workspaces/local-git.js +1 -2
  144. package/dist/core/workflow/workspaces/paths.d.ts +2 -2
  145. package/dist/core/workflow/workspaces/types.d.ts +0 -6
  146. package/dist/core/workflow/workspaces/types.d.ts.map +1 -1
  147. package/dist/core/workflow/workspaces/validate.d.ts +0 -2
  148. package/dist/core/workflow/workspaces/validate.d.ts.map +1 -1
  149. package/dist/core/workflow/workspaces/validate.js +0 -2
  150. package/dist/lib/browser/client.d.ts +118 -1
  151. package/dist/lib/browser/client.d.ts.map +1 -1
  152. package/dist/lib/browser/client.js +435 -6
  153. package/dist/lib/browser/geometry.d.ts.map +1 -1
  154. package/dist/lib/browser/geometry.js +180 -37
  155. package/dist/lib/browser/index.d.ts +5 -1
  156. package/dist/lib/browser/index.d.ts.map +1 -1
  157. package/dist/lib/browser/index.js +5 -1
  158. package/dist/lib/browser/netscape-cookies.d.ts +6 -0
  159. package/dist/lib/browser/netscape-cookies.d.ts.map +1 -0
  160. package/dist/lib/browser/netscape-cookies.js +31 -0
  161. package/dist/lib/browser/proxy.d.ts +24 -0
  162. package/dist/lib/browser/proxy.d.ts.map +1 -0
  163. package/dist/lib/browser/proxy.js +84 -0
  164. package/dist/lib/browser/runts.d.ts +6 -0
  165. package/dist/lib/browser/runts.d.ts.map +1 -1
  166. package/dist/lib/browser/runts.js +21 -4
  167. package/dist/lib/browser/session-control.d.ts +112 -0
  168. package/dist/lib/browser/session-control.d.ts.map +1 -0
  169. package/dist/lib/browser/session-control.js +670 -0
  170. package/dist/lib/claude-desktop.d.ts +47 -0
  171. package/dist/lib/claude-desktop.d.ts.map +1 -1
  172. package/dist/lib/claude-desktop.js +169 -1
  173. package/dist/lib/docs-links.d.ts +108 -0
  174. package/dist/lib/docs-links.d.ts.map +1 -0
  175. package/dist/lib/docs-links.js +555 -0
  176. package/dist/lib/exec.d.ts +1 -1
  177. package/dist/lib/exec.js +1 -1
  178. package/dist/lib/identities/assume.d.ts +4 -2
  179. package/dist/lib/identities/assume.d.ts.map +1 -1
  180. package/dist/lib/identities/assume.js +15 -2
  181. package/dist/lib/instructions/bundle.d.ts +33 -0
  182. package/dist/lib/instructions/bundle.d.ts.map +1 -0
  183. package/dist/lib/instructions/bundle.js +149 -0
  184. package/dist/lib/instructions/git-hooks.d.ts +75 -0
  185. package/dist/lib/instructions/git-hooks.d.ts.map +1 -0
  186. package/dist/lib/instructions/git-hooks.js +238 -0
  187. package/dist/lib/instructions/splice.d.ts +10 -4
  188. package/dist/lib/instructions/splice.d.ts.map +1 -1
  189. package/dist/lib/instructions/splice.js +19 -11
  190. package/package.json +2 -2
  191. package/schemas/config.schema.json +32 -0
  192. package/src/commander.ts +5 -1
  193. package/src/commands/agents.ts +341 -113
  194. package/src/commands/browse-session.ts +245 -0
  195. package/src/commands/browse.ts +287 -44
  196. package/src/commands/checkpoint.ts +1 -1
  197. package/src/commands/claude-desktop.ts +106 -5
  198. package/src/commands/deinit.ts +7 -1
  199. package/src/commands/docs.ts +56 -0
  200. package/src/commands/doctor.ts +155 -21
  201. package/src/commands/init.ts +28 -9
  202. package/src/commands/instructions.ts +129 -0
  203. package/src/commands/presence.ts +7 -1
  204. package/src/commands/tunnel.ts +2 -5
  205. package/src/core/agents/canonical-emit.ts +7 -6
  206. package/src/core/agents/cli.ts +143 -89
  207. package/src/core/agents/coord-client.ts +21 -5
  208. package/src/core/agents/finalization.ts +595 -0
  209. package/src/core/agents/git-hook.ts +126 -0
  210. package/src/core/agents/index.ts +1 -0
  211. package/src/core/agents/render/prompt-context.ts +96 -16
  212. package/src/core/agents/render/session-context.ts +29 -4
  213. package/src/core/agents/rules/claim-conflict.ts +3 -3
  214. package/src/core/agents/rules/commit-conflict.ts +35 -8
  215. package/src/core/agents/rules/stop-hook.ts +71 -23
  216. package/src/core/agents/session-events.ts +14 -41
  217. package/src/core/agents/state/heartbeat-projector.ts +108 -1
  218. package/src/core/agents/state/heartbeat-writer.ts +129 -16
  219. package/src/core/agents/state/names.ts +54 -2
  220. package/src/core/agents/state/session-state.ts +131 -0
  221. package/src/core/config.ts +92 -12
  222. package/src/core/governor/planning.ts +17 -12
  223. package/src/core/hooks/adapter/detect.ts +4 -13
  224. package/src/core/hooks/adapter/events.ts +1 -0
  225. package/src/core/hooks/adapter/output.ts +9 -4
  226. package/src/core/hooks/adapter/parse.ts +3 -0
  227. package/src/core/hooks/adapter/wiring.ts +43 -0
  228. package/src/core/hooks/cli.ts +296 -27
  229. package/src/core/hooks/codex-authorization.ts +253 -0
  230. package/src/core/hooks/codex-wsl-bridge.ts +188 -0
  231. package/src/core/hooks/effects/index.ts +5 -5
  232. package/src/core/hooks/events/schema.ts +71 -0
  233. package/src/core/hooks/resolve/owner.ts +22 -1
  234. package/src/core/hooks/resolve/transcript.ts +164 -1
  235. package/src/core/hooks/session-name-presence.ts +52 -0
  236. package/src/core/hooks/unsafe-cross-shell.ts +160 -0
  237. package/src/core/presence/blob.ts +18 -0
  238. package/src/core/presence/index.ts +18 -1
  239. package/src/core/work/state.ts +8 -12
  240. package/src/core/workflow/engine.ts +2 -2
  241. package/src/core/workflow/index.ts +0 -1
  242. package/src/core/workflow/proof.ts +4 -4
  243. package/src/core/workflow/run-state.ts +5 -5
  244. package/src/core/workflow/types.ts +3 -3
  245. package/src/core/workflow/workspaces/execution.ts +11 -4
  246. package/src/core/workflow/workspaces/index.ts +0 -3
  247. package/src/core/workflow/workspaces/local-git.ts +1 -2
  248. package/src/core/workflow/workspaces/paths.ts +2 -2
  249. package/src/core/workflow/workspaces/types.ts +0 -9
  250. package/src/core/workflow/workspaces/validate.ts +1 -5
  251. package/src/lib/browser/client.ts +528 -6
  252. package/src/lib/browser/geometry.ts +197 -37
  253. package/src/lib/browser/index.ts +39 -0
  254. package/src/lib/browser/netscape-cookies.ts +39 -0
  255. package/src/lib/browser/proxy.ts +105 -0
  256. package/src/lib/browser/runts.ts +27 -3
  257. package/src/lib/browser/session-control.ts +892 -0
  258. package/src/lib/claude-desktop.ts +232 -1
  259. package/src/lib/docs-links.ts +674 -0
  260. package/src/lib/exec.ts +1 -1
  261. package/src/lib/identities/assume.ts +22 -1
  262. package/src/lib/instructions/bundle.ts +199 -0
  263. package/src/lib/instructions/git-hooks.ts +259 -0
  264. package/src/lib/instructions/splice.ts +41 -11
@@ -0,0 +1,253 @@
1
+ import { spawn } from "node:child_process";
2
+ import { createInterface } from "node:readline";
3
+
4
+ export type CodexHookTrustStatus = "managed" | "trusted" | "untrusted" | "modified";
5
+
6
+ export type CodexHookAuthorizationStatus =
7
+ | "runnable"
8
+ | "review_required"
9
+ | "disabled"
10
+ | "not_wired"
11
+ | "error";
12
+
13
+ export interface CodexHookAuthorizationResult {
14
+ status: CodexHookAuthorizationStatus;
15
+ detail: string;
16
+ hookCount: number;
17
+ disabledCount: number;
18
+ trustCounts: Partial<Record<CodexHookTrustStatus, number>>;
19
+ }
20
+
21
+ export interface CodexHookAuthorizationProbeOptions {
22
+ cwd: string;
23
+ codexBin?: string;
24
+ appServerArgs?: string[];
25
+ env?: NodeJS.ProcessEnv;
26
+ timeoutMs?: number;
27
+ }
28
+
29
+ interface JsonObject {
30
+ [key: string]: unknown;
31
+ }
32
+
33
+ function isObject(value: unknown): value is JsonObject {
34
+ return typeof value === "object" && value !== null && !Array.isArray(value);
35
+ }
36
+
37
+ function stringArray(value: unknown): string[] {
38
+ return Array.isArray(value)
39
+ ? value.filter((item): item is string => typeof item === "string")
40
+ : [];
41
+ }
42
+
43
+ function trustStatus(value: unknown): CodexHookTrustStatus | null {
44
+ return value === "managed" || value === "trusted" || value === "untrusted" || value === "modified"
45
+ ? value
46
+ : null;
47
+ }
48
+
49
+ function normalizedPath(value: string): string {
50
+ return value.replaceAll("\\", "/").replace(/\/$/, "").toLowerCase();
51
+ }
52
+
53
+ /**
54
+ * Convert Codex app-server's `hooks/list` response into the one authorization
55
+ * state doctor needs. This is intentionally read-only: hashes are observed but
56
+ * never persisted, and only commands containing Harnery's `agent-hook` binary
57
+ * are included in the result.
58
+ */
59
+ export function assessCodexHookList(response: unknown, cwd: string): CodexHookAuthorizationResult {
60
+ if (!isObject(response) || !isObject(response.result) || !Array.isArray(response.result.data)) {
61
+ return {
62
+ status: "error",
63
+ detail: "Codex hooks/list returned an invalid response",
64
+ hookCount: 0,
65
+ disabledCount: 0,
66
+ trustCounts: {},
67
+ };
68
+ }
69
+
70
+ const entries = response.result.data.filter(isObject);
71
+ const expected = normalizedPath(cwd);
72
+ const entry =
73
+ entries.find((candidate) => {
74
+ const candidateCwd = candidate.cwd;
75
+ return typeof candidateCwd === "string" && normalizedPath(candidateCwd) === expected;
76
+ }) ?? (entries.length === 1 ? entries[0] : undefined);
77
+ if (!entry) {
78
+ return {
79
+ status: "error",
80
+ detail: "Codex hooks/list omitted the requested workspace",
81
+ hookCount: 0,
82
+ disabledCount: 0,
83
+ trustCounts: {},
84
+ };
85
+ }
86
+
87
+ const errors = stringArray(entry.errors);
88
+ if (errors.length > 0) {
89
+ return {
90
+ status: "error",
91
+ detail: `Codex could not load hooks: ${errors.join("; ")}`,
92
+ hookCount: 0,
93
+ disabledCount: 0,
94
+ trustCounts: {},
95
+ };
96
+ }
97
+
98
+ const hooks = Array.isArray(entry.hooks) ? entry.hooks.filter(isObject) : [];
99
+ const harneryHooks = hooks.filter(
100
+ (hook) =>
101
+ typeof hook.command === "string" && /(?:^|[\\/\s])agent-hook(?:\s|$)/.test(hook.command),
102
+ );
103
+ if (harneryHooks.length === 0) {
104
+ return {
105
+ status: "not_wired",
106
+ detail: "Codex discovered no Harnery hooks",
107
+ hookCount: 0,
108
+ disabledCount: 0,
109
+ trustCounts: {},
110
+ };
111
+ }
112
+
113
+ const counts: Partial<Record<CodexHookTrustStatus, number>> = {};
114
+ let disabledCount = 0;
115
+ for (const hook of harneryHooks) {
116
+ const status = trustStatus(hook.trustStatus);
117
+ if (!status) {
118
+ return {
119
+ status: "error",
120
+ detail: "Codex returned an unknown hook trust state",
121
+ hookCount: harneryHooks.length,
122
+ disabledCount,
123
+ trustCounts: counts,
124
+ };
125
+ }
126
+ counts[status] = (counts[status] ?? 0) + 1;
127
+ if (hook.enabled !== true) disabledCount += 1;
128
+ }
129
+
130
+ const reviewCount = (counts.untrusted ?? 0) + (counts.modified ?? 0);
131
+ const warnings = stringArray(entry.warnings);
132
+ const warningSuffix = warnings.length > 0 ? `; ${warnings.length} Codex warning(s)` : "";
133
+ if (reviewCount > 0) {
134
+ return {
135
+ status: "review_required",
136
+ detail:
137
+ `${reviewCount} of ${harneryHooks.length} Harnery hooks require review` +
138
+ ` (${counts.untrusted ?? 0} new, ${counts.modified ?? 0} modified)${warningSuffix}`,
139
+ hookCount: harneryHooks.length,
140
+ disabledCount,
141
+ trustCounts: counts,
142
+ };
143
+ }
144
+ if (disabledCount > 0) {
145
+ return {
146
+ status: "disabled",
147
+ detail: `${disabledCount} of ${harneryHooks.length} trusted Harnery hooks are disabled${warningSuffix}`,
148
+ hookCount: harneryHooks.length,
149
+ disabledCount,
150
+ trustCounts: counts,
151
+ };
152
+ }
153
+ return {
154
+ status: "runnable",
155
+ detail: `${harneryHooks.length} Harnery hooks trusted and enabled${warningSuffix}`,
156
+ hookCount: harneryHooks.length,
157
+ disabledCount: 0,
158
+ trustCounts: counts,
159
+ };
160
+ }
161
+
162
+ /**
163
+ * Ask the active Codex CLI for hook authorization through its app-server
164
+ * protocol. The child is closed as soon as `hooks/list` responds. Harnery never
165
+ * calls the trust-write method and never reads Codex's private config format.
166
+ */
167
+ export function probeCodexHookAuthorization(
168
+ options: CodexHookAuthorizationProbeOptions,
169
+ ): Promise<CodexHookAuthorizationResult> {
170
+ const codexBin = options.codexBin ?? "codex";
171
+ const args = options.appServerArgs ?? ["app-server"];
172
+ const timeoutMs = options.timeoutMs ?? 5_000;
173
+
174
+ return new Promise((resolve) => {
175
+ const child = spawn(codexBin, args, {
176
+ cwd: options.cwd,
177
+ env: options.env ?? process.env,
178
+ stdio: ["pipe", "pipe", "pipe"],
179
+ windowsHide: true,
180
+ });
181
+ const lines = createInterface({ input: child.stdout });
182
+ let settled = false;
183
+ let stderr = "";
184
+
185
+ const finish = (result: CodexHookAuthorizationResult): void => {
186
+ if (settled) return;
187
+ settled = true;
188
+ clearTimeout(timer);
189
+ lines.close();
190
+ child.stdin.end();
191
+ child.kill();
192
+ resolve(result);
193
+ };
194
+ const fail = (detail: string): void =>
195
+ finish({ status: "error", detail, hookCount: 0, disabledCount: 0, trustCounts: {} });
196
+
197
+ const timer = setTimeout(() => {
198
+ fail(`Codex hooks/list timed out after ${timeoutMs}ms`);
199
+ }, timeoutMs);
200
+
201
+ child.stderr.on("data", (chunk: Buffer | string) => {
202
+ if (stderr.length < 4_096) stderr += chunk.toString();
203
+ });
204
+ child.stdin.on("error", (error) => fail(`Codex app-server input failed: ${error.message}`));
205
+ child.on("error", (error) => fail(`could not start Codex app-server: ${error.message}`));
206
+ child.on("close", (code) => {
207
+ if (!settled) {
208
+ const suffix = stderr.trim() ? `: ${stderr.trim().slice(0, 500)}` : "";
209
+ fail(
210
+ `Codex app-server exited before hooks/list completed (exit ${code ?? "unknown"})${suffix}`,
211
+ );
212
+ }
213
+ });
214
+ lines.on("line", (line) => {
215
+ let message: JsonObject;
216
+ try {
217
+ const parsed = JSON.parse(line) as unknown;
218
+ if (!isObject(parsed)) return;
219
+ message = parsed;
220
+ } catch {
221
+ return;
222
+ }
223
+ if (message.id === 0) {
224
+ if (message.error) {
225
+ fail("Codex app-server rejected initialization");
226
+ return;
227
+ }
228
+ child.stdin.write(`${JSON.stringify({ method: "initialized" })}\n`);
229
+ child.stdin.write(
230
+ `${JSON.stringify({ method: "hooks/list", id: 1, params: { cwds: [options.cwd] } })}\n`,
231
+ );
232
+ return;
233
+ }
234
+ if (message.id === 1) {
235
+ if (message.error) {
236
+ fail("Codex app-server rejected hooks/list");
237
+ return;
238
+ }
239
+ finish(assessCodexHookList(message, options.cwd));
240
+ }
241
+ });
242
+
243
+ child.stdin.write(
244
+ `${JSON.stringify({
245
+ method: "initialize",
246
+ id: 0,
247
+ params: {
248
+ clientInfo: { name: "harnery_doctor", title: "Harnery Doctor", version: "0.0.0" },
249
+ },
250
+ })}\n`,
251
+ );
252
+ });
253
+ }
@@ -0,0 +1,188 @@
1
+ export interface CodexWslBridgeStatus {
2
+ ok: boolean;
3
+ detail: string;
4
+ threadIdPresent: boolean;
5
+ wslenvForwardsThreadId: boolean;
6
+ }
7
+
8
+ export interface CodexWslBridgeOptions {
9
+ /** The caller already knows this is a Codex adapter process. */
10
+ expected?: boolean;
11
+ }
12
+
13
+ export interface CodexWslWorkspaceLinkMapping {
14
+ /** Absolute path seen by Linux-side tools. */
15
+ linuxRoot: string;
16
+ /** Forward-slash UNC path that Codex Desktop can use as a Markdown destination. */
17
+ hostRoot: string;
18
+ distro: string;
19
+ }
20
+
21
+ export interface CodexWslFileLinkTelemetry {
22
+ /** Markdown destinations that still use the Linux root in a Windows-hosted WSL task. */
23
+ wsl_linux_file_link_count: number;
24
+ /** Bounded examples for local diagnosis; omitted when the count is zero. */
25
+ wsl_linux_file_link_examples?: string[];
26
+ }
27
+
28
+ /** Whether an adapter-reported workspace is a Linux path exposed through WSL UNC. */
29
+ export function isWslUncPath(value: unknown): boolean {
30
+ if (typeof value !== "string") return false;
31
+ return (
32
+ /^\\\\(?:wsl\.localhost|wsl\$)\\/i.test(value) ||
33
+ /^\\\\\?\\unc\\(?:wsl\.localhost|wsl\$)\\/i.test(value)
34
+ );
35
+ }
36
+
37
+ interface ParsedWslUncPath {
38
+ host: string;
39
+ distro: string;
40
+ linuxPath: string;
41
+ }
42
+
43
+ function parseWslUncPath(value: unknown): ParsedWslUncPath | null {
44
+ if (typeof value !== "string") return null;
45
+ const ordinary = value.replace(/^\\\\\?\\UNC\\/i, "\\\\");
46
+ const match = ordinary.match(/^\\\\(wsl\.localhost|wsl\$)\\([^\\]+)(?:\\(.*))?$/i);
47
+ if (!match) return null;
48
+ const host = match[1]!;
49
+ const distro = match[2]!;
50
+ const tail = (match[3] ?? "").split("\\").filter(Boolean).join("/");
51
+ return { host, distro, linuxPath: tail ? `/${tail}` : "/" };
52
+ }
53
+
54
+ function trimTrailingSlash(path: string): string {
55
+ return path === "/" ? path : path.replace(/\/+$/, "");
56
+ }
57
+
58
+ function isWithinOrEqual(path: string, root: string): boolean {
59
+ return path === root || path.startsWith(`${root}/`);
60
+ }
61
+
62
+ /**
63
+ * Derive the Linux-to-host path pair from the actual adapter workspace. The
64
+ * roots must overlap so an unrelated WSL UNC path cannot authorize a mapping
65
+ * for some other checkout.
66
+ */
67
+ export function codexWslWorkspaceLinkMapping(
68
+ coordRootRaw: string,
69
+ workspaceCwd: unknown,
70
+ ): CodexWslWorkspaceLinkMapping | null {
71
+ const parsed = parseWslUncPath(workspaceCwd);
72
+ const linuxRoot = trimTrailingSlash(coordRootRaw.replace(/\\/g, "/"));
73
+ if (!parsed || !linuxRoot.startsWith("/")) return null;
74
+ const linuxCwd = trimTrailingSlash(parsed.linuxPath);
75
+ if (!isWithinOrEqual(linuxCwd, linuxRoot) && !isWithinOrEqual(linuxRoot, linuxCwd)) {
76
+ return null;
77
+ }
78
+ return {
79
+ linuxRoot,
80
+ hostRoot: `//${parsed.host}/${parsed.distro}${linuxRoot}`,
81
+ distro: parsed.distro,
82
+ };
83
+ }
84
+
85
+ /** Fresh adapter context for the model. Shell paths stay Linux-native; only
86
+ * Markdown destinations use the host-visible root. */
87
+ export function renderCodexWslFileLinkContext(coordRoot: string, workspaceCwd: unknown): string {
88
+ const mapping = codexWslWorkspaceLinkMapping(coordRoot, workspaceCwd);
89
+ if (!mapping) return "";
90
+ return (
91
+ "Codex WSL file links: this task is hosted by Windows while tools run inside WSL. " +
92
+ `For clickable local-file Markdown links, replace the Linux workspace root \`${mapping.linuxRoot}\` with the host-visible root \`${mapping.hostRoot}\`. ` +
93
+ `Example: \`[label](<${mapping.hostRoot}/path/to/file>)\`. ` +
94
+ "Keep Linux paths unchanged in shell commands and tool inputs."
95
+ );
96
+ }
97
+
98
+ function withoutMarkdownCode(text: string): string {
99
+ const output: string[] = [];
100
+ let fence: { marker: string; length: number } | null = null;
101
+ for (const line of text.split(/\r?\n/)) {
102
+ const candidate = line.match(/^\s{0,3}(`{3,}|~{3,})/);
103
+ if (fence) {
104
+ if (candidate && candidate[1]![0] === fence.marker && candidate[1]!.length >= fence.length) {
105
+ fence = null;
106
+ }
107
+ output.push("");
108
+ continue;
109
+ }
110
+ if (candidate) {
111
+ fence = { marker: candidate[1]![0]!, length: candidate[1]!.length };
112
+ output.push("");
113
+ continue;
114
+ }
115
+ output.push(line.replace(/(`+)([^`\n]*?)\1/g, ""));
116
+ }
117
+ return output.join("\n");
118
+ }
119
+
120
+ function markdownDestinations(text: string): string[] {
121
+ const out: string[] = [];
122
+ const visible = withoutMarkdownCode(text);
123
+ const re = /!?\[[^\]\n]*\]\(\s*(?:<([^>\n]+)>|([^\s)\n]+))/g;
124
+ for (const match of visible.matchAll(re)) {
125
+ const target = match[1] ?? match[2];
126
+ if (target) out.push(target);
127
+ }
128
+ return out;
129
+ }
130
+
131
+ /**
132
+ * Count Markdown links that point at the Linux workspace root even though the
133
+ * adapter workspace is Windows-visible UNC. This is telemetry only: the Stop
134
+ * hook must not replace or retry an otherwise complete Codex reply.
135
+ */
136
+ export function codexWslFileLinkTelemetry(
137
+ coordRoot: string,
138
+ workspaceCwd: unknown,
139
+ assistantMessage: string,
140
+ ): CodexWslFileLinkTelemetry | null {
141
+ const mapping = codexWslWorkspaceLinkMapping(coordRoot, workspaceCwd);
142
+ if (!mapping) return null;
143
+ const mismatches = markdownDestinations(assistantMessage).filter((target) =>
144
+ isWithinOrEqual(target, mapping.linuxRoot),
145
+ );
146
+ return {
147
+ wsl_linux_file_link_count: mismatches.length,
148
+ ...(mismatches.length > 0
149
+ ? { wsl_linux_file_link_examples: mismatches.slice(0, 3).map((path) => path.slice(0, 240)) }
150
+ : {}),
151
+ };
152
+ }
153
+
154
+ function wslenvForwards(wslenv: string | undefined, variable: string): boolean {
155
+ if (!wslenv) return false;
156
+ return wslenv.split(":").some((entry) => entry.split("/", 1)[0] === variable);
157
+ }
158
+
159
+ /**
160
+ * Inspect the environment contract used when Windows-native Codex launches a
161
+ * Harnery hook or shell inside WSL. Returns null outside that hybrid mode.
162
+ */
163
+ export function inspectCodexWslBridge(
164
+ env: NodeJS.ProcessEnv = process.env,
165
+ options: CodexWslBridgeOptions = {},
166
+ ): CodexWslBridgeStatus | null {
167
+ const inWsl = Boolean(env.WSL_DISTRO_NAME?.trim() || env.WSL_INTEROP?.trim());
168
+ if (!inWsl) return null;
169
+
170
+ const threadIdPresent = Boolean(env.CODEX_THREAD_ID?.trim());
171
+ const wslenvForwardsThreadId = wslenvForwards(env.WSLENV, "CODEX_THREAD_ID");
172
+ if (!options.expected && !threadIdPresent && !wslenvForwardsThreadId) return null;
173
+
174
+ const problems: string[] = [];
175
+ if (!threadIdPresent) problems.push("CODEX_THREAD_ID did not reach WSL");
176
+ if (!wslenvForwardsThreadId) problems.push("WSLENV does not forward CODEX_THREAD_ID");
177
+
178
+ const distro = env.WSL_DISTRO_NAME?.trim();
179
+ return {
180
+ ok: problems.length === 0,
181
+ detail:
182
+ problems.length === 0
183
+ ? `thread identity forwarded through WSLENV${distro ? ` (${distro})` : ""}`
184
+ : problems.join("; "),
185
+ threadIdPresent,
186
+ wslenvForwardsThreadId,
187
+ };
188
+ }
@@ -120,7 +120,7 @@ export function journalArchive(repoRoot: string, owner: string): void {
120
120
  * Fire the optional host session-sync extension on turn stop / session end.
121
121
  * harnery core has no session-telemetry sink of its own; a host that wants one
122
122
  * drops an executable at
123
- * `scripts/hooks/adapter/claude_code/extensions/session-sync.sh` under the coord
123
+ * `scripts/hooks/harness/claude-code/extensions/session-sync.sh` under the coord
124
124
  * root, and core runs it detached + unref'd so a slow sink never blocks the
125
125
  * hook. `force` arrives as argv $1 ("1" on session end, "0" on turn stop) so the
126
126
  * host can rate-limit the stop path and force-flush on end. No-op when the
@@ -134,8 +134,8 @@ export function runSessionSyncExtension(repoRoot: string, force: boolean): void
134
134
  repoRoot,
135
135
  "scripts",
136
136
  "hooks",
137
- "adapter",
138
- "claude_code",
137
+ "harness",
138
+ "claude-code",
139
139
  "extensions",
140
140
  "session-sync.sh",
141
141
  );
@@ -169,8 +169,8 @@ export function runTurnSummary(
169
169
  repoRoot,
170
170
  "scripts",
171
171
  "hooks",
172
- "adapter",
173
- "claude_code",
172
+ "harness",
173
+ "claude-code",
174
174
  "extensions",
175
175
  "turn-summary.sh",
176
176
  );
@@ -51,6 +51,14 @@ export type SessionStart = EventEnvelope<
51
51
  cwd: string;
52
52
  model?: string;
53
53
  pid: number;
54
+ /** Content hash of every repo-authored instruction/config file this
55
+ * adapter can load for the session's working-path profile. */
56
+ instruction_bundle_id?: string;
57
+ /** Parallel hash of canonical AGENTS.md + .agents sources. This diagnoses
58
+ * sync drift without splitting behavior cohorts when rendered bytes match. */
59
+ instruction_source_id?: string;
60
+ instruction_profile_root?: string;
61
+ instruction_component_count?: number;
54
62
  /** Present iff this session is a `workflow run` child: the run id whose
55
63
  * transcript owns it (child env HARNERY_WORKFLOW_RUN_ID). Optional-field
56
64
  * addition per the schema evolution rules (minor bump). */
@@ -93,12 +101,55 @@ export type UserPromptSubmit = EventEnvelope<
93
101
  }
94
102
  >;
95
103
 
104
+ /** Direct adapter evidence that the session is waiting for operator input. */
105
+ export type InteractionInputRequested = EventEnvelope<
106
+ "interaction.input_requested",
107
+ {
108
+ request_kind: "permission";
109
+ tool_name?: string;
110
+ reason?: string;
111
+ }
112
+ >;
113
+
96
114
  export type TurnStop = EventEnvelope<
97
115
  "turn.stop",
98
116
  {
99
117
  tool_call_count: number;
100
118
  text_length: number;
101
119
  status_box_present: boolean; // adapter sets via transcript scan for `┌─ agent-` prefix
120
+ /** Strict variant of status_box_present: assistant text blocks only, so a
121
+ * tool_result carrying the box (the status command's own output) does not
122
+ * count. Shadow telemetry while the rule-2/3 detector decision is open;
123
+ * the Stop verdict does not read it yet. */
124
+ status_box_present_strict?: boolean;
125
+ /** Whether the session's suggested name is satisfied as of this turn:
126
+ * either this reply showed it, or an earlier reply did. Present whenever a
127
+ * suggested name exists, since the Stop verdict reads it per turn. */
128
+ session_name_present?: boolean;
129
+ /** Which suggested name `session_name_present` refers to. A projector
130
+ * rebuild attributes the sighting to this name instead of to whichever
131
+ * name is current during the replay. */
132
+ session_name_present_for?: string;
133
+ /** Shadow telemetry for Windows-hosted Codex workspaces: Markdown link
134
+ * destinations that still use the Linux coordination root. */
135
+ wsl_linux_file_link_count?: number;
136
+ /** Up to three bounded examples for diagnosing the mismatches locally. */
137
+ wsl_linux_file_link_examples?: string[];
138
+ }
139
+ >;
140
+
141
+ /** The evaluated end-turn policy outcome. One row per Stop evaluation makes
142
+ * the future bounce denominator explicit and keeps repeated remediation nags
143
+ * from being mistaken for additional human turns. */
144
+ export type StopVerdict = EventEnvelope<
145
+ "stop.verdict",
146
+ {
147
+ allow: boolean;
148
+ rule: string;
149
+ reason?: string;
150
+ enforcement_mode: "enforced" | "observe_only" | "exempt" | "fail_open";
151
+ eligible: boolean;
152
+ nag_delivered: boolean;
102
153
  }
103
154
  >;
104
155
 
@@ -258,6 +309,10 @@ export type ClaimAcquire = EventEnvelope<
258
309
  {
259
310
  path: string;
260
311
  mode: "read" | "write";
312
+ finalization?: {
313
+ disposition: "git" | "output";
314
+ root: string;
315
+ };
261
316
  }
262
317
  >;
263
318
 
@@ -284,6 +339,19 @@ export type StateTaskSet = EventEnvelope<
284
339
  {
285
340
  task: string;
286
341
  cleared: boolean;
342
+ /** True exactly when this call produced the session name (first non-empty
343
+ * declaration of a human-facing session); never true with a null name. */
344
+ first_of_session?: boolean;
345
+ /** The name produced by this call, for the Stop-hook naming rule. */
346
+ suggested_session_name?: string | null;
347
+ }
348
+ >;
349
+
350
+ export type StateTaskState = EventEnvelope<
351
+ "state.task_state",
352
+ {
353
+ state: "active" | "blocked" | "done";
354
+ reason?: string;
287
355
  }
288
356
  >;
289
357
 
@@ -459,7 +527,9 @@ export type Event =
459
527
  | SubagentStart
460
528
  | SubagentStop
461
529
  | UserPromptSubmit
530
+ | InteractionInputRequested
462
531
  | TurnStop
532
+ | StopVerdict
463
533
  | CommandStart
464
534
  | CommandOutput
465
535
  | CommandEnd
@@ -478,6 +548,7 @@ export type Event =
478
548
  | ClaimRelease
479
549
  | ClaimConflict
480
550
  | StateTaskSet
551
+ | StateTaskState
481
552
  | StateStatusChecked
482
553
  | StateJournalAppend
483
554
  | StatePresenceChange
@@ -17,6 +17,12 @@ import { checkPidToken } from "../../agents/state/proc-start.ts";
17
17
  * events; session_id is the parent-shape default.
18
18
  * 3. PID-map lookup at `.harnery/pid-map/<pid>` for our own pid, then ppid
19
19
  * chain (up to 20 hops).
20
+ * 4. `CODEX_THREAD_ID` env var, accepted only when a live heartbeat by that
21
+ * id exists under `.harnery/active/`. Codex sessions use their thread id
22
+ * as the heartbeat instance_id, and a Windows→WSL bridge forwards the
23
+ * var into process trees that descend from no registered process — the
24
+ * one place the pid walk can never land. Validated so a garbage value
25
+ * cannot fabricate an identity.
20
26
  *
21
27
  * Returns null when nothing resolves. Callers must treat null as "no owner"
22
28
  * and skip the event (Phase 2 fail-safe; Phase 3 will mint a temporary owner
@@ -25,7 +31,10 @@ import { checkPidToken } from "../../agents/state/proc-start.ts";
25
31
  export function resolveOwner(opts: {
26
32
  payload: Record<string, unknown> | null;
27
33
  coordRoot: string;
28
- }): { instance_id: string; source: "env" | "payload" | "pidmap-self" | "pidmap-ancestor" } | null {
34
+ }): {
35
+ instance_id: string;
36
+ source: "env" | "payload" | "pidmap-self" | "pidmap-ancestor" | "codex-env";
37
+ } | null {
29
38
  const env = coordEnv("AGENT_COORD_OWNER");
30
39
  if (env && env.length > 0) {
31
40
  return { instance_id: env, source: "env" };
@@ -76,6 +85,18 @@ export function resolveOwner(opts: {
76
85
  }
77
86
  }
78
87
 
88
+ // Tier 4: Codex thread id, heartbeat-validated. Last because the pid map is
89
+ // more specific when it answers — an inherited env var can outlive the
90
+ // process tree it described, a live pid-map row cannot.
91
+ const codexThreadId = process.env.CODEX_THREAD_ID?.trim();
92
+ if (
93
+ codexThreadId &&
94
+ /^[A-Za-z0-9-]+$/.test(codexThreadId) &&
95
+ existsSync(join(opts.coordRoot, ".harnery", "active", `${codexThreadId}.json`))
96
+ ) {
97
+ return { instance_id: codexThreadId, source: "codex-env" };
98
+ }
99
+
79
100
  return null;
80
101
  }
81
102