owenloop 0.4.1 → 0.5.1

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 (282) hide show
  1. package/CHANGELOG.md +71 -0
  2. package/README.md +182 -53
  3. package/bin/owenloop.mjs +50 -2
  4. package/dist/packages/work/src/agent/brief.d.ts +176 -0
  5. package/dist/packages/work/src/agent/brief.js +197 -0
  6. package/dist/packages/work/src/agent/loop.d.ts +141 -0
  7. package/dist/packages/work/src/agent/loop.js +669 -0
  8. package/dist/packages/work/src/agent/model-policy.d.ts +49 -0
  9. package/dist/packages/work/src/agent/model-policy.js +138 -0
  10. package/dist/packages/work/src/agent/workdir.d.ts +210 -0
  11. package/dist/packages/work/src/agent/workdir.js +319 -0
  12. package/dist/packages/work/src/bundle/cache.d.ts +92 -0
  13. package/dist/packages/work/src/bundle/cache.js +338 -0
  14. package/dist/packages/work/src/bundle/fetch.d.ts +87 -0
  15. package/dist/packages/work/src/bundle/fetch.js +345 -0
  16. package/dist/packages/work/src/bundle/types.d.ts +164 -0
  17. package/dist/packages/work/src/bundle/types.js +1 -0
  18. package/dist/packages/work/src/consumed-verifier.d.ts +45 -0
  19. package/dist/packages/work/src/consumed-verifier.js +205 -0
  20. package/dist/packages/work/src/credentials/owenloop.d.ts +11 -0
  21. package/dist/packages/work/src/credentials/owenloop.js +28 -0
  22. package/dist/packages/work/src/credentials/reader.d.ts +36 -0
  23. package/dist/packages/work/src/credentials/reader.js +32 -0
  24. package/dist/packages/work/src/credentials/resolve.d.ts +19 -0
  25. package/dist/packages/work/src/credentials/resolve.js +56 -0
  26. package/dist/packages/work/src/exec/instructions.d.ts +99 -0
  27. package/dist/packages/work/src/exec/instructions.js +323 -0
  28. package/dist/packages/work/src/exec/loop.d.ts +40 -0
  29. package/dist/packages/work/src/exec/loop.js +332 -0
  30. package/dist/packages/work/src/exec/payload.d.ts +29 -0
  31. package/dist/packages/work/src/exec/payload.js +54 -0
  32. package/dist/packages/work/src/exec/receipt.d.ts +41 -0
  33. package/dist/packages/work/src/exec/receipt.js +32 -0
  34. package/dist/packages/work/src/exec/runner.d.ts +71 -0
  35. package/dist/packages/work/src/exec/runner.js +256 -0
  36. package/dist/packages/work/src/harness/child-env.d.ts +100 -0
  37. package/dist/packages/work/src/harness/child-env.js +134 -0
  38. package/dist/packages/work/src/harness/claude.d.ts +140 -0
  39. package/dist/packages/work/src/harness/claude.js +837 -0
  40. package/dist/packages/work/src/harness/codex.d.ts +111 -0
  41. package/dist/packages/work/src/harness/codex.js +1024 -0
  42. package/dist/packages/work/src/harness/contract.d.ts +308 -0
  43. package/dist/packages/work/src/harness/contract.js +32 -0
  44. package/dist/packages/work/src/harness/fake.d.ts +59 -0
  45. package/dist/packages/work/src/harness/fake.js +91 -0
  46. package/dist/packages/work/src/harness/jsonrpc-stdio.d.ts +108 -0
  47. package/dist/packages/work/src/harness/jsonrpc-stdio.js +351 -0
  48. package/dist/packages/work/src/harness/permissions.d.ts +75 -0
  49. package/dist/packages/work/src/harness/permissions.js +182 -0
  50. package/dist/packages/work/src/harness/registry.d.ts +47 -0
  51. package/dist/packages/work/src/harness/registry.js +45 -0
  52. package/dist/packages/work/src/harness/session-store.d.ts +181 -0
  53. package/dist/packages/work/src/harness/session-store.js +523 -0
  54. package/dist/packages/work/src/harness/types.d.ts +29 -0
  55. package/dist/packages/work/src/harness/types.js +14 -0
  56. package/dist/packages/work/src/harnesses.d.ts +38 -0
  57. package/dist/packages/work/src/harnesses.js +38 -0
  58. package/dist/packages/work/src/hold/loop.d.ts +36 -0
  59. package/dist/packages/work/src/hold/loop.js +23 -0
  60. package/dist/packages/work/src/hold/mcp.d.ts +75 -0
  61. package/dist/packages/work/src/hold/mcp.js +264 -0
  62. package/dist/packages/work/src/hub/client.d.ts +23 -0
  63. package/dist/packages/work/src/hub/client.js +91 -0
  64. package/dist/packages/work/src/hub/types.d.ts +326 -0
  65. package/dist/packages/work/src/hub/types.js +26 -0
  66. package/dist/packages/work/src/lease/loop.d.ts +94 -0
  67. package/dist/packages/work/src/lease/loop.js +277 -0
  68. package/dist/packages/work/src/main.d.ts +1 -0
  69. package/dist/packages/work/src/main.js +48 -0
  70. package/dist/packages/work/src/mcp/server.d.ts +125 -0
  71. package/dist/packages/work/src/mcp/server.js +320 -0
  72. package/dist/packages/work/src/owenloop-bin.d.ts +1 -0
  73. package/dist/packages/work/src/owenloop-bin.js +18 -0
  74. package/dist/packages/work/src/roles/agent-run.d.ts +124 -0
  75. package/dist/packages/work/src/roles/agent-run.js +529 -0
  76. package/dist/packages/work/src/roles/exec.d.ts +48 -0
  77. package/dist/packages/work/src/roles/exec.js +228 -0
  78. package/dist/packages/work/src/roles/hold.d.ts +72 -0
  79. package/dist/packages/work/src/roles/hold.js +363 -0
  80. package/dist/packages/work/src/roles/join.d.ts +18 -0
  81. package/dist/packages/work/src/roles/join.js +204 -0
  82. package/dist/packages/work/src/roles/lint.d.ts +58 -0
  83. package/dist/packages/work/src/roles/lint.js +234 -0
  84. package/dist/packages/work/src/roles/prepare.d.ts +1 -0
  85. package/dist/packages/work/src/roles/prepare.js +249 -0
  86. package/dist/packages/work/src/roles/release.d.ts +48 -0
  87. package/dist/packages/work/src/roles/release.js +137 -0
  88. package/dist/packages/work/src/roles/sessions.d.ts +61 -0
  89. package/dist/packages/work/src/roles/sessions.js +180 -0
  90. package/dist/packages/work/src/roles/settings.d.ts +6 -0
  91. package/dist/packages/work/src/roles/settings.js +71 -0
  92. package/dist/packages/work/src/roles/shift.d.ts +13 -0
  93. package/dist/packages/work/src/roles/shift.js +250 -0
  94. package/dist/packages/work/src/roles/signals.d.ts +55 -0
  95. package/dist/packages/work/src/roles/signals.js +62 -0
  96. package/dist/packages/work/src/settings/provision.d.ts +21 -0
  97. package/dist/packages/work/src/settings/provision.js +47 -0
  98. package/dist/packages/work/src/settings/settings.d.ts +136 -0
  99. package/dist/packages/work/src/settings/settings.js +155 -0
  100. package/dist/packages/work/src/shift/client.d.ts +24 -0
  101. package/dist/packages/work/src/shift/client.js +124 -0
  102. package/dist/packages/work/src/shift/loop.d.ts +131 -0
  103. package/dist/packages/work/src/shift/loop.js +751 -0
  104. package/dist/packages/work/src/shift/protocol.d.ts +95 -0
  105. package/dist/packages/work/src/shift/protocol.js +24 -0
  106. package/dist/packages/work/src/shift/routing.d.ts +35 -0
  107. package/dist/packages/work/src/shift/routing.js +50 -0
  108. package/dist/packages/work/src/shift/runtime.d.ts +45 -0
  109. package/dist/packages/work/src/shift/runtime.js +337 -0
  110. package/dist/packages/work/src/shift/server.d.ts +23 -0
  111. package/dist/packages/work/src/shift/server.js +716 -0
  112. package/dist/packages/work/src/shift/spawn.d.ts +109 -0
  113. package/dist/packages/work/src/shift/spawn.js +158 -0
  114. package/dist/packages/work/src/shift/state.d.ts +79 -0
  115. package/dist/packages/work/src/shift/state.js +385 -0
  116. package/dist/packages/work/src/submit-proof.d.ts +36 -0
  117. package/dist/packages/work/src/submit-proof.js +93 -0
  118. package/dist/packages/work/src/usage.d.ts +6 -0
  119. package/dist/packages/work/src/usage.js +175 -0
  120. package/dist/src/add.d.ts +189 -0
  121. package/dist/src/add.js +300 -0
  122. package/dist/src/archive.d.ts +97 -0
  123. package/dist/src/archive.js +591 -0
  124. package/dist/src/bundle/index.d.ts +64 -0
  125. package/dist/src/bundle/index.js +508 -0
  126. package/dist/src/bundle/manifest.d.ts +50 -0
  127. package/dist/src/bundle/manifest.js +435 -0
  128. package/dist/src/bundle/runtime.d.ts +26 -0
  129. package/dist/src/bundle/runtime.js +64 -0
  130. package/dist/src/bundle/tar.d.ts +62 -0
  131. package/dist/src/bundle/tar.js +238 -0
  132. package/dist/src/bundle/types.d.ts +105 -0
  133. package/dist/src/bundle/types.js +21 -0
  134. package/dist/{cli.d.ts → src/cli.d.ts} +92 -7
  135. package/dist/src/cli.js +5968 -0
  136. package/dist/{credentials.d.ts → src/credentials.d.ts} +22 -15
  137. package/dist/{credentials.js → src/credentials.js} +35 -31
  138. package/dist/src/crypto/allowed-signers.d.ts +68 -0
  139. package/dist/src/crypto/allowed-signers.js +307 -0
  140. package/dist/src/crypto/canonical.d.ts +8 -0
  141. package/dist/src/crypto/canonical.js +92 -0
  142. package/dist/src/crypto/chain.d.ts +65 -0
  143. package/dist/src/crypto/chain.js +377 -0
  144. package/dist/src/crypto/dsse.d.ts +209 -0
  145. package/dist/src/crypto/dsse.js +282 -0
  146. package/dist/src/crypto/enrollment.d.ts +78 -0
  147. package/dist/src/crypto/enrollment.js +186 -0
  148. package/dist/src/crypto/index.d.ts +48 -0
  149. package/dist/src/crypto/index.js +44 -0
  150. package/dist/src/crypto/keys.d.ts +258 -0
  151. package/dist/src/crypto/keys.js +908 -0
  152. package/dist/src/crypto/org-root.d.ts +33 -0
  153. package/dist/src/crypto/org-root.js +78 -0
  154. package/dist/src/crypto/origin-rules.d.ts +56 -0
  155. package/dist/src/crypto/origin-rules.js +143 -0
  156. package/dist/src/crypto/policy-floor.d.ts +98 -0
  157. package/dist/src/crypto/policy-floor.js +353 -0
  158. package/dist/src/crypto/records.d.ts +316 -0
  159. package/dist/src/crypto/records.js +157 -0
  160. package/dist/src/crypto/scope.d.ts +49 -0
  161. package/dist/src/crypto/scope.js +83 -0
  162. package/dist/src/crypto/ssh.d.ts +171 -0
  163. package/dist/src/crypto/ssh.js +396 -0
  164. package/dist/src/crypto/submission.d.ts +33 -0
  165. package/dist/src/crypto/submission.js +47 -0
  166. package/dist/src/crypto/trust-roots.d.ts +29 -0
  167. package/dist/src/crypto/trust-roots.js +39 -0
  168. package/dist/src/crypto/verify-consumed.d.ts +67 -0
  169. package/dist/src/crypto/verify-consumed.js +259 -0
  170. package/dist/src/crypto/verify-origin.d.ts +50 -0
  171. package/dist/src/crypto/verify-origin.js +151 -0
  172. package/dist/src/crypto/verify-publication.d.ts +60 -0
  173. package/dist/src/crypto/verify-publication.js +150 -0
  174. package/dist/{defs.d.ts → src/defs.d.ts} +24 -1
  175. package/dist/{defs.js → src/defs.js} +181 -61
  176. package/dist/{engine.d.ts → src/engine.d.ts} +105 -9
  177. package/dist/{engine.js → src/engine.js} +251 -47
  178. package/dist/{factory.d.ts → src/factory.d.ts} +16 -0
  179. package/dist/{factory.js → src/factory.js} +16 -4
  180. package/dist/src/global-config.d.ts +59 -0
  181. package/dist/src/global-config.js +84 -0
  182. package/dist/{hub.d.ts → src/hub.d.ts} +280 -31
  183. package/dist/{hub.js → src/hub.js} +493 -52
  184. package/dist/src/index.d.ts +47 -0
  185. package/dist/src/index.js +41 -0
  186. package/dist/src/install.d.ts +538 -0
  187. package/dist/src/install.js +1981 -0
  188. package/dist/src/lock.d.ts +38 -0
  189. package/dist/src/lock.js +325 -0
  190. package/dist/src/mcp/serve.d.ts +109 -0
  191. package/dist/{mcp → src/mcp}/serve.js +335 -75
  192. package/dist/{mcp → src/mcp}/server.d.ts +6 -9
  193. package/dist/{mcp → src/mcp}/server.js +18 -10
  194. package/dist/{model.d.ts → src/model.d.ts} +15 -0
  195. package/dist/{model.js → src/model.js} +112 -2
  196. package/dist/src/order-resolver.d.ts +184 -0
  197. package/dist/src/order-resolver.js +252 -0
  198. package/dist/src/package-version.d.ts +10 -0
  199. package/dist/src/package-version.js +42 -0
  200. package/dist/{paths.d.ts → src/paths.d.ts} +14 -1
  201. package/dist/{paths.js → src/paths.js} +18 -0
  202. package/dist/src/schemas/enrollment-grant.v1.schema.json +90 -0
  203. package/dist/src/schemas/index.d.ts +1416 -0
  204. package/dist/src/schemas/index.js +31 -0
  205. package/dist/src/schemas/order.v1.schema.json +106 -0
  206. package/dist/src/schemas/origin.v1.schema.json +68 -0
  207. package/dist/src/schemas/policy-floor.v1.schema.json +29 -0
  208. package/dist/src/schemas/publication.v1.schema.json +31 -0
  209. package/dist/src/schemas/revocation.v1.schema.json +39 -0
  210. package/dist/src/schemas/submission.v1.schema.json +61 -0
  211. package/dist/src/store/def-source.d.ts +52 -0
  212. package/dist/src/store/def-source.js +335 -0
  213. package/dist/src/store/index-file.d.ts +69 -0
  214. package/dist/src/store/index-file.js +151 -0
  215. package/dist/src/store/index.d.ts +27 -0
  216. package/dist/src/store/index.js +22 -0
  217. package/dist/src/store/ingestor.d.ts +43 -0
  218. package/dist/src/store/ingestor.js +211 -0
  219. package/dist/src/store/install.d.ts +228 -0
  220. package/dist/src/store/install.js +555 -0
  221. package/dist/src/store/instruction-source.d.ts +49 -0
  222. package/dist/src/store/instruction-source.js +298 -0
  223. package/dist/src/store/origin-evidence.d.ts +21 -0
  224. package/dist/src/store/origin-evidence.js +66 -0
  225. package/dist/src/store/pre-commit-verifier.d.ts +68 -0
  226. package/dist/src/store/pre-commit-verifier.js +418 -0
  227. package/dist/src/store/resolve.d.ts +138 -0
  228. package/dist/src/store/resolve.js +363 -0
  229. package/dist/src/store/types.d.ts +288 -0
  230. package/dist/src/store/types.js +356 -0
  231. package/dist/{types.d.ts → src/types.d.ts} +58 -24
  232. package/dist/{untar.d.ts → src/untar.d.ts} +8 -23
  233. package/dist/src/untar.js +38 -0
  234. package/dist/{util.d.ts → src/util.d.ts} +3 -2
  235. package/dist/{util.js → src/util.js} +3 -2
  236. package/dist/{owenwork.d.ts → src/work-settings.d.ts} +22 -22
  237. package/dist/{owenwork.js → src/work-settings.js} +27 -27
  238. package/docs/agent-runner.md +86 -0
  239. package/docs/authoring.md +150 -59
  240. package/docs/bundles.md +250 -0
  241. package/docs/cli.md +1758 -163
  242. package/docs/crypto.md +816 -0
  243. package/docs/design.md +374 -52
  244. package/docs/embedding.md +135 -27
  245. package/docs/wire-contracts.md +380 -0
  246. package/examples/workflows/{command-worker.yaml → command-executor.yaml} +15 -15
  247. package/examples/workflows/compile-dev-playbook.yaml +338 -0
  248. package/examples/workflows/judges/gate-sharpness.md +39 -0
  249. package/examples/workflows/judges/security.md +34 -0
  250. package/package.json +22 -8
  251. package/plugins/claude-code/.claude-plugin/marketplace.json +14 -0
  252. package/plugins/claude-code/plugin/.claude-plugin/plugin.json +8 -0
  253. package/plugins/claude-code/plugin/.mcp.json +14 -0
  254. package/plugins/claude-code/plugin/hooks/hooks.json +26 -0
  255. package/plugins/claude-code/plugin/hooks/session-end.sh +40 -0
  256. package/plugins/claude-code/plugin/hooks/session-start.sh +52 -0
  257. package/plugins/claude-code/plugin/skills/author/SKILL.md +181 -0
  258. package/plugins/claude-code/plugin/skills/conduct/SKILL.md +106 -0
  259. package/plugins/claude-code/plugin/skills/shift/SKILL.md +105 -0
  260. package/plugins/codex/.agents/plugins/marketplace.json +21 -0
  261. package/plugins/codex/plugins/owenloop/.codex-plugin/plugin.json +7 -0
  262. package/plugins/codex/plugins/owenloop/.mcp.json +20 -0
  263. package/plugins/codex/plugins/owenloop/hooks/hooks.json +26 -0
  264. package/plugins/codex/plugins/owenloop/hooks/session-end.sh +40 -0
  265. package/plugins/codex/plugins/owenloop/hooks/session-start.sh +52 -0
  266. package/plugins/codex/plugins/owenloop/skills/author/SKILL.md +181 -0
  267. package/plugins/codex/plugins/owenloop/skills/conduct/SKILL.md +106 -0
  268. package/plugins/codex/plugins/owenloop/skills/shift/SKILL.md +105 -0
  269. package/dist/add.d.ts +0 -384
  270. package/dist/add.js +0 -834
  271. package/dist/cli.js +0 -3270
  272. package/dist/index.d.ts +0 -32
  273. package/dist/index.js +0 -23
  274. package/dist/lock.d.ts +0 -80
  275. package/dist/lock.js +0 -217
  276. package/dist/mcp/serve.d.ts +0 -84
  277. package/dist/untar.js +0 -164
  278. /package/dist/{schema.d.ts → src/schema.d.ts} +0 -0
  279. /package/dist/{schema.js → src/schema.js} +0 -0
  280. /package/dist/{store.d.ts → src/store.d.ts} +0 -0
  281. /package/dist/{store.js → src/store.js} +0 -0
  282. /package/dist/{types.js → src/types.js} +0 -0
@@ -0,0 +1,320 @@
1
+ /**
2
+ * A minimal, dependency-light MCP server core — newline-delimited JSON-RPC 2.0
3
+ * over stdio, hand-rolled (plan decision 1).
4
+ *
5
+ * WHY hand-rolled, not `@modelcontextprotocol/sdk`: this repo's posture is one
6
+ * runtime dep (`yaml`). The SDK drags zod and a transport stack for a surface we
7
+ * need only a few hundred lines of. The hub side already proves interop against
8
+ * the official SDK client, and the `--mcp` e2e tests here drive a real
9
+ * client-side handshake, so drift is tested rather than assumed.
10
+ *
11
+ * The core is transport-agnostic and side-effect-injected: it exposes
12
+ * `handleLine(line)` (feed it one framed JSON-RPC line) and `close(reason)`
13
+ * (transport EOF), and it emits every outbound frame through the injected
14
+ * `write(msg)`. Unit tests feed lines and inspect writes with no child process;
15
+ * `pumpStdin` wires the real `process.stdin`/`process.stdout` in the roles.
16
+ *
17
+ * Supported methods (the subset the Shift + work-holder need):
18
+ * - `initialize` → capabilities `{tools:{}}`, serverInfo, and the
19
+ * agreed protocolVersion (echo the client's when
20
+ * recognized, else answer with ours).
21
+ * - `notifications/initialized` → no-op (no response; it is a notification).
22
+ * - `ping` → `{}`.
23
+ * - `tools/list` → the registered tool defs.
24
+ * - `tools/call` → dispatch to the tool handler; the result is
25
+ * `{content:[{type:'text',text:<JSON>}], isError?}`.
26
+ * - `notifications/cancelled` → abort a tracked in-flight `tools/call`; the
27
+ * aborted call sends NO response frame.
28
+ * - outbound `notifications/progress` when a call carried
29
+ * `params._meta.progressToken` (via `ctx.sendProgress`).
30
+ *
31
+ * Errors: unknown method → -32601; malformed JSON / bad envelope → -32700;
32
+ * invalid params → -32602. A notification (no `id`) never gets a response, even
33
+ * on error. The process must never crash on a bad line.
34
+ */
35
+ /** The MCP revisions this server will agree to echo back on `initialize`. */
36
+ const RECOGNIZED_PROTOCOL_VERSIONS = new Set([
37
+ '2024-11-05',
38
+ '2025-03-26',
39
+ '2025-06-18',
40
+ '2025-11-25',
41
+ ]);
42
+ /** The version we answer with when the client's is missing or unrecognized. */
43
+ const OUR_PROTOCOL_VERSION = '2025-06-18';
44
+ // ---- JSON-RPC error codes ---------------------------------------------------
45
+ export const PARSE_ERROR = -32700;
46
+ export const INVALID_REQUEST = -32600;
47
+ export const METHOD_NOT_FOUND = -32601;
48
+ export const INVALID_PARAMS = -32602;
49
+ export const INTERNAL_ERROR = -32603;
50
+ /** Convenience: wrap a JSON-able value as a single text content block. */
51
+ export function textResult(value, isError = false) {
52
+ return { content: [{ type: 'text', text: JSON.stringify(value) }], ...(isError ? { isError: true } : {}) };
53
+ }
54
+ export function createMcpServer(opts) {
55
+ const err = opts.err ?? (() => { });
56
+ const tools = new Map();
57
+ for (const t of opts.tools)
58
+ tools.set(t.name, t);
59
+ // Tracks in-flight tools/call requests by id so notifications/cancelled can
60
+ // abort them (and so we can suppress the response frame of a cancelled call).
61
+ const inFlight = new Map();
62
+ let closed = false;
63
+ // NOTE: deliberately NOT gated on `closed` — a reply whose handler settled a
64
+ // microtask after transport EOF must still reach the pipe (the cancel
65
+ // contract already suppresses replies of cancelled calls, and sendProgress
66
+ // gates on `closed` itself). A write to a gone pipe lands in the catch.
67
+ function send(msg) {
68
+ try {
69
+ opts.write(msg);
70
+ }
71
+ catch (e) {
72
+ err(`mcp: write failed: ${e instanceof Error ? e.message : String(e)}`);
73
+ }
74
+ }
75
+ function reply(id, result) {
76
+ send({ jsonrpc: '2.0', id, result });
77
+ }
78
+ function replyError(id, code, message) {
79
+ send({ jsonrpc: '2.0', id, error: { code, message } });
80
+ }
81
+ function keyOf(id) {
82
+ return typeof id === 'number' ? `n:${id}` : `s:${String(id)}`;
83
+ }
84
+ async function handleToolsCall(id, params) {
85
+ if (!isObject(params) || typeof params['name'] !== 'string') {
86
+ replyError(id, INVALID_PARAMS, 'tools/call requires a string "name"');
87
+ return;
88
+ }
89
+ const name = params['name'];
90
+ const reg = tools.get(name);
91
+ if (reg === undefined) {
92
+ replyError(id, METHOD_NOT_FOUND, `unknown tool '${name}'`);
93
+ return;
94
+ }
95
+ const rawArgs = params['arguments'];
96
+ const args = isObject(rawArgs) ? rawArgs : {};
97
+ const progressToken = readProgressToken(params);
98
+ const tracked = { cancelled: false, byClose: false, cancelCbs: [] };
99
+ // Only tracked when it has a concrete id (a notification cannot be cancelled).
100
+ if (id !== null && id !== undefined)
101
+ inFlight.set(keyOf(id), tracked);
102
+ const ctx = {
103
+ get cancelled() {
104
+ return tracked.cancelled;
105
+ },
106
+ onCancel(cb) {
107
+ if (tracked.cancelled)
108
+ cb();
109
+ else
110
+ tracked.cancelCbs.push(cb);
111
+ },
112
+ sendProgress(payload) {
113
+ if (progressToken === undefined || tracked.cancelled || closed)
114
+ return;
115
+ send({
116
+ jsonrpc: '2.0',
117
+ method: 'notifications/progress',
118
+ params: { progressToken, ...payload },
119
+ });
120
+ },
121
+ };
122
+ try {
123
+ const result = await reg.handler(args, ctx);
124
+ // A CLIENT-cancelled call sends NO response frame (the JSON-RPC/MCP
125
+ // cancel contract). A close-cancelled call still replies — see InFlight.
126
+ if (tracked.cancelled && !tracked.byClose)
127
+ return;
128
+ reply(id, result);
129
+ }
130
+ catch (e) {
131
+ if (tracked.cancelled && !tracked.byClose)
132
+ return;
133
+ err(`mcp: tool '${name}' threw: ${e instanceof Error ? e.message : String(e)}`);
134
+ reply(id, textResult({ error: e instanceof Error ? e.message : String(e) }, true));
135
+ }
136
+ finally {
137
+ if (id !== null && id !== undefined)
138
+ inFlight.delete(keyOf(id));
139
+ }
140
+ }
141
+ function handleCancelled(params) {
142
+ if (!isObject(params))
143
+ return;
144
+ const requestId = params['requestId'];
145
+ if (requestId === undefined || requestId === null)
146
+ return;
147
+ const tracked = inFlight.get(keyOf(requestId));
148
+ if (tracked === undefined)
149
+ return;
150
+ tracked.cancelled = true;
151
+ for (const cb of tracked.cancelCbs.splice(0)) {
152
+ try {
153
+ cb();
154
+ }
155
+ catch {
156
+ // a cancel callback must never take the server down
157
+ }
158
+ }
159
+ }
160
+ function handleInitialize(id, params) {
161
+ const requested = isObject(params) ? params['protocolVersion'] : undefined;
162
+ const agreed = typeof requested === 'string' && RECOGNIZED_PROTOCOL_VERSIONS.has(requested) ? requested : OUR_PROTOCOL_VERSION;
163
+ reply(id, {
164
+ protocolVersion: agreed,
165
+ capabilities: { tools: {} },
166
+ serverInfo: { name: opts.name, version: opts.version },
167
+ });
168
+ }
169
+ function handleToolsList(id) {
170
+ reply(id, {
171
+ tools: [...tools.values()].map((t) => ({ name: t.name, description: t.description, inputSchema: t.inputSchema })),
172
+ });
173
+ }
174
+ async function handleLine(line) {
175
+ const trimmed = line.trim();
176
+ if (trimmed === '')
177
+ return;
178
+ let msg;
179
+ try {
180
+ msg = JSON.parse(trimmed);
181
+ }
182
+ catch {
183
+ // No id is recoverable from an unparseable line — reply with a null-id
184
+ // parse error per JSON-RPC and keep serving.
185
+ replyError(null, PARSE_ERROR, 'parse error: line was not valid JSON');
186
+ return;
187
+ }
188
+ if (!isObject(msg) || typeof msg.method !== 'string') {
189
+ const id = isObject(msg) && 'id' in msg ? msg.id : null;
190
+ replyError(id ?? null, INVALID_REQUEST, 'invalid request: missing "method"');
191
+ return;
192
+ }
193
+ const isNotification = !('id' in msg) || msg.id === undefined;
194
+ const id = (msg.id ?? null);
195
+ switch (msg.method) {
196
+ case 'initialize':
197
+ if (!isNotification)
198
+ handleInitialize(id, msg.params);
199
+ return;
200
+ case 'notifications/initialized':
201
+ case 'initialized':
202
+ return; // no-op notification
203
+ case 'ping':
204
+ if (!isNotification)
205
+ reply(id, {});
206
+ return;
207
+ case 'tools/list':
208
+ if (!isNotification)
209
+ handleToolsList(id);
210
+ return;
211
+ case 'tools/call':
212
+ if (isNotification)
213
+ return; // a call with no id has nowhere to answer
214
+ await handleToolsCall(id, msg.params);
215
+ return;
216
+ case 'notifications/cancelled':
217
+ case 'cancelled':
218
+ handleCancelled(msg.params);
219
+ return;
220
+ default:
221
+ if (!isNotification)
222
+ replyError(id, METHOD_NOT_FOUND, `unknown method '${msg.method}'`);
223
+ return;
224
+ }
225
+ }
226
+ function close() {
227
+ if (closed)
228
+ return;
229
+ closed = true;
230
+ for (const tracked of inFlight.values()) {
231
+ tracked.cancelled = true;
232
+ tracked.byClose = true;
233
+ for (const cb of tracked.cancelCbs.splice(0)) {
234
+ try {
235
+ cb();
236
+ }
237
+ catch {
238
+ // best-effort
239
+ }
240
+ }
241
+ }
242
+ inFlight.clear();
243
+ }
244
+ return { handleLine, close };
245
+ }
246
+ // ---- helpers ----------------------------------------------------------------
247
+ function isObject(v) {
248
+ return typeof v === 'object' && v !== null && !Array.isArray(v);
249
+ }
250
+ /** Pull `params._meta.progressToken` (string|number) when present. */
251
+ function readProgressToken(params) {
252
+ const meta = params['_meta'];
253
+ if (!isObject(meta))
254
+ return undefined;
255
+ const tok = meta['progressToken'];
256
+ return typeof tok === 'string' || typeof tok === 'number' ? tok : undefined;
257
+ }
258
+ /**
259
+ * Wire a real byte stream (`process.stdin`) into a server: buffer bytes, split
260
+ * on newlines, feed each complete line to `server.handleLine`, and call
261
+ * `server.close` + `onEof` once on EOF. A trailing partial line at EOF is
262
+ * flushed if non-empty.
263
+ *
264
+ * Every line is DISPATCHED THE MOMENT IT ARRIVES — never queued behind an
265
+ * in-flight `tools/call`. JSON-RPC over MCP is a concurrent protocol: while a
266
+ * long `whats_next` park is outstanding, `notifications/cancelled` must reach
267
+ * the server to abort it (D12 cancel-mid-park) and `ping` must still answer.
268
+ * Serializing the pump behind the in-flight call chain would deadlock exactly
269
+ * those frames on the real transport. Per-frame ordering still holds for the
270
+ * non-parking methods because `handleLine` runs synchronously up to the first
271
+ * tool-handler await, and responses are matched by id, not by order.
272
+ *
273
+ * EOF flushes the tail line, then calls `server.close` RIGHT AWAY — `close()`
274
+ * cancels any parked in-flight call, which is what lets a process waiting on
275
+ * this pump actually exit (chaining the close behind a parked call would
276
+ * deadlock EOF against the very call only the close can unpark). A close-cancel
277
+ * does NOT suppress the call's reply the way a client cancel does — every call
278
+ * received before EOF still answers. `onEof` fires only after every dispatched
279
+ * line has settled, so those reply frames are on the pipe before the caller
280
+ * acts on EOF (e.g. exits the process).
281
+ */
282
+ export function pumpStdin(stream, server, onEof) {
283
+ stream.setEncoding?.('utf8');
284
+ let buffer = '';
285
+ let ended = false;
286
+ const pending = new Set();
287
+ const dispatch = (line) => {
288
+ const p = Promise.resolve(server.handleLine(line)).catch(() => { });
289
+ pending.add(p);
290
+ void p.finally(() => pending.delete(p));
291
+ };
292
+ stream.on('data', (chunk) => {
293
+ buffer += typeof chunk === 'string' ? chunk : chunk.toString('utf8');
294
+ let nl = buffer.indexOf('\n');
295
+ while (nl !== -1) {
296
+ const line = buffer.slice(0, nl);
297
+ buffer = buffer.slice(nl + 1);
298
+ dispatch(line);
299
+ nl = buffer.indexOf('\n');
300
+ }
301
+ });
302
+ const finish = () => {
303
+ if (ended)
304
+ return;
305
+ ended = true;
306
+ const tail = buffer;
307
+ buffer = '';
308
+ if (tail.trim() !== '')
309
+ dispatch(tail);
310
+ // Snapshot BEFORE close: close() cancels parked calls, which is what makes
311
+ // these settle. A handler that ignores cancellation would stall EOF — ours
312
+ // all resolve on cancel (whats_next) or are bounded hub calls (hold tools).
313
+ const flushed = Promise.allSettled([...pending]);
314
+ server.close('stdin-eof');
315
+ void flushed.then(() => onEof?.());
316
+ };
317
+ stream.on('end', finish);
318
+ stream.on('close', finish);
319
+ stream.resume?.();
320
+ }
@@ -0,0 +1 @@
1
+ export declare function resolveOwenloopBin(): string;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Resolve this installation's packaged Owenloop entrypoint.
3
+ *
4
+ * Source-driven tests import `packages/work/src/**`, while built/runtime code
5
+ * imports `dist/packages/work/src/**`; those layouts are one parent apart.
6
+ * Every child Owenloop process must use this resolver so a stale executable
7
+ * earlier on PATH cannot split a Shift, its worker, and the worker's born-bound
8
+ * MCP server across different installations.
9
+ */
10
+ import { existsSync } from 'node:fs';
11
+ import { fileURLToPath } from 'node:url';
12
+ export function resolveOwenloopBin() {
13
+ const candidates = [
14
+ new URL('../../../bin/owenloop.mjs', import.meta.url),
15
+ new URL('../../../../bin/owenloop.mjs', import.meta.url),
16
+ ].map((url) => fileURLToPath(url));
17
+ return candidates.find((candidate) => existsSync(candidate)) ?? candidates[0];
18
+ }
@@ -0,0 +1,124 @@
1
+ /**
2
+ * `owenloop work agent-run <order-id>` (Phase 3) — the detached, self-leasing worker
3
+ * that HOSTS one step agent, and the harness-registry COMPOSITION ROOT.
4
+ *
5
+ * The shift spawns one detached
6
+ * `owenloop work agent-run <workflow>/<run> --origin <url>` per AGENT order — the
7
+ * ONLY path an agent order takes, since Phase 5 deleted the legacy stamp path
8
+ * it used to share the lane with. This
9
+ * process owns that order end to end: it takes the lease on first contact,
10
+ * renders the step's brief, starts the step agent inside a harness adapter with
11
+ * a work-holder MCP mount born bound to this order, keeps the lease warm
12
+ * underneath the turn, and learns whether the agent submitted FROM THE HUB.
13
+ * Nothing is ever written to any agent-definition directory.
14
+ *
15
+ * The orchestration CORE lives in `src/agent/loop.ts` (lease + adapter + confirm
16
+ * phase, every side effect injected); this role only parses the arg contract,
17
+ * resolves origin/credential/cache, tags the holder identity, resolves WHICH
18
+ * adapter hosts the agent, loads the brief template, wires the signal seam, and
19
+ * maps `AgentRunOutcome` to an exit code.
20
+ *
21
+ * ── WHERE THE ADAPTERS COME FROM (Phase 6) ──
22
+ *
23
+ * `src/harness/registry.ts` starts empty and forbids a barrel, so SOMEBODY has
24
+ * to import the adapter modules for their `register(...)` side effect to fire.
25
+ * Through Phase 5 that somebody was this file AND `src/roles/lint.ts`, each
26
+ * carrying the same import pair in the same hand-maintained order. Phase 6
27
+ * consolidated both into `src/harnesses.ts`; this file imports THAT, and the
28
+ * single line is directly below the header. Adapter import order — which decides
29
+ * the default harness — now has exactly one owner.
30
+ *
31
+ * ── WHERE THE STEP SPEC COMES FROM ──
32
+ *
33
+ * The order packet carries a `defDigest` reference but no authoritative prompt,
34
+ * harness, or permission text. The worker resolves the digest and step name
35
+ * through the verified local workflow store. The prepare cache remains available
36
+ * for routing and session state, but never supplies execution instructions.
37
+ *
38
+ * ── CREDENTIALS ──
39
+ *
40
+ * Origin/credential resolution mirrors `exec`: origin `--origin` → settings; the
41
+ * bearer comes from owenloop's store via `resolveBearer`, reading the
42
+ * `agent:<account>` slot for the account the shift set in this child's
43
+ * `OWENLOOP_ACCOUNT` spawn env (default `default`). `agent-run` has NO `--as`
44
+ * flag — the spawn-env channel is the contract.
45
+ *
46
+ * D10, CLOSED IN PHASE 6. The MCP mount this worker builds carries no
47
+ * credential — the mounted work-holder resolves its own from the same store —
48
+ * and as of Phase 6 the dev-only `OWENLOOP_TOKEN` override is no longer
49
+ * inherited by the harness child either. Both adapters filter it out through
50
+ * `filterOwenloopEnv` (`src/harness/child-env.ts`), an allowlist scoped to the
51
+ * `OWENLOOP_*` namespace ONLY, so nothing a harness needs in order to start —
52
+ * `PATH`, `HOME`, `NODE_OPTIONS`, a shift setting, a vendor's own credential
53
+ * variable — can be stranded by it.
54
+ *
55
+ * The consequence lands in this file: because the child cannot see the override,
56
+ * THIS ROLE IGNORES IT TOO, so worker and child authenticate as the same
57
+ * principal. See the block around the `resolveBearer` call. `resolveBearer`
58
+ * itself is unchanged and every other role still honours the override.
59
+ *
60
+ * Phase 4's narrower guarantee still holds and is still asserted: the rejection
61
+ * delta and the replay brief carry no credential material
62
+ * (`test/agent-rejection.test.ts`).
63
+ *
64
+ * ── WHERE THE STEP AGENT WORKS (Phase 4) ──
65
+ *
66
+ * `OrderPacket.workdir` (the hub's choice) > `<workRoot>/<workflow>/<run>/` >
67
+ * this process's cwd. The middle rung is new: see `src/agent/workdir.ts` for the
68
+ * per-RUN layout and the reaper that removes it.
69
+ */
70
+ import '../harnesses.ts';
71
+ import { type AgentRunOutcome } from '../agent/loop.ts';
72
+ import { type InstructionResolver } from '../exec/instructions.ts';
73
+ import { type ConsumedVerifier } from '../consumed-verifier.ts';
74
+ import { type HubClient } from '../hub/client.ts';
75
+ import { type SignalHost } from './signals.ts';
76
+ interface ParsedArgs {
77
+ orderId?: string;
78
+ workflow?: string;
79
+ origin?: string;
80
+ shift?: string;
81
+ harness?: string;
82
+ heartbeatIntervalMs?: number;
83
+ jumpToleranceMs?: number;
84
+ submitGraceMs?: number;
85
+ confirmIntervalMs?: number;
86
+ error?: string;
87
+ }
88
+ /**
89
+ * Parse the positional `<order-id>` plus `--workflow`/`--origin`/`--shift`/
90
+ * `--harness`/`--heartbeat-interval`/`--jump-tolerance`/`--submit-grace`/
91
+ * `--confirm-interval`. Supports `--flag value` and `--flag=value`; a second
92
+ * positional or an unknown flag is an error. Mirrors `src/roles/exec.ts`.
93
+ */
94
+ export declare function parseArgs(args: string[]): ParsedArgs;
95
+ /** Map the loop's outcome onto the process exit code (see usage.ts). */
96
+ export declare function exitCodeFor(outcome: AgentRunOutcome): number;
97
+ /**
98
+ * Injectable process-boundary deps for `run` — defaulting to the real ones, so
99
+ * the role wiring (holder tag, adapter resolution, signal handlers, outcome
100
+ * mapping) is testable without spawning a harness, signaling the test process,
101
+ * or reaching a real hub.
102
+ */
103
+ export interface RunDeps {
104
+ signalHost?: SignalHost;
105
+ hub?: HubClient;
106
+ out?: (line: string) => void;
107
+ err?: (line: string) => void;
108
+ /** Store-backed instruction resolver; injected tests may provide a fake. */
109
+ instructions?: InstructionResolver;
110
+ /** Consume-side verifier; injected tests may provide a fake. */
111
+ consumedVerifier?: ConsumedVerifier;
112
+ /** Environment used to derive the global workflow-store root. */
113
+ env?: Record<string, string | undefined>;
114
+ /** cwd for an order that carries no `workdir` (default `process.cwd()`). */
115
+ cwd?: string;
116
+ /**
117
+ * Holder id override (default `<hostname>:<pid>`) — pinned in tests. Injecting
118
+ * `hub` also skips credential resolution entirely, so a test needs no
119
+ * owenloop credential store on disk.
120
+ */
121
+ holderId?: string;
122
+ }
123
+ export declare function run(args: string[], deps?: RunDeps): Promise<number>;
124
+ export {};