owenloop 0.4.1 → 0.5.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.
- package/CHANGELOG.md +26 -0
- package/README.md +58 -31
- package/bin/owenloop.mjs +2 -2
- package/dist/packages/work/src/agent/brief.d.ts +172 -0
- package/dist/packages/work/src/agent/brief.js +193 -0
- package/dist/packages/work/src/agent/loop.d.ts +135 -0
- package/dist/packages/work/src/agent/loop.js +542 -0
- package/dist/packages/work/src/agent/workdir.d.ts +210 -0
- package/dist/packages/work/src/agent/workdir.js +319 -0
- package/dist/packages/work/src/bundle/cache.d.ts +90 -0
- package/dist/packages/work/src/bundle/cache.js +324 -0
- package/dist/packages/work/src/bundle/fetch.d.ts +85 -0
- package/dist/packages/work/src/bundle/fetch.js +325 -0
- package/dist/packages/work/src/bundle/types.d.ts +160 -0
- package/dist/packages/work/src/bundle/types.js +1 -0
- package/dist/packages/work/src/credentials/owenloop.d.ts +11 -0
- package/dist/packages/work/src/credentials/owenloop.js +28 -0
- package/dist/packages/work/src/credentials/reader.d.ts +36 -0
- package/dist/packages/work/src/credentials/reader.js +32 -0
- package/dist/packages/work/src/credentials/resolve.d.ts +19 -0
- package/dist/packages/work/src/credentials/resolve.js +56 -0
- package/dist/packages/work/src/exec/loop.d.ts +29 -0
- package/dist/packages/work/src/exec/loop.js +215 -0
- package/dist/packages/work/src/exec/receipt.d.ts +38 -0
- package/dist/packages/work/src/exec/receipt.js +21 -0
- package/dist/packages/work/src/exec/runner.d.ts +65 -0
- package/dist/packages/work/src/exec/runner.js +185 -0
- package/dist/packages/work/src/harness/child-env.d.ts +100 -0
- package/dist/packages/work/src/harness/child-env.js +127 -0
- package/dist/packages/work/src/harness/claude.d.ts +128 -0
- package/dist/packages/work/src/harness/claude.js +635 -0
- package/dist/packages/work/src/harness/codex.d.ts +110 -0
- package/dist/packages/work/src/harness/codex.js +896 -0
- package/dist/packages/work/src/harness/contract.d.ts +283 -0
- package/dist/packages/work/src/harness/contract.js +32 -0
- package/dist/packages/work/src/harness/fake.d.ts +59 -0
- package/dist/packages/work/src/harness/fake.js +90 -0
- package/dist/packages/work/src/harness/jsonrpc-stdio.d.ts +108 -0
- package/dist/packages/work/src/harness/jsonrpc-stdio.js +344 -0
- package/dist/packages/work/src/harness/permissions.d.ts +62 -0
- package/dist/packages/work/src/harness/permissions.js +93 -0
- package/dist/packages/work/src/harness/registry.d.ts +47 -0
- package/dist/packages/work/src/harness/registry.js +45 -0
- package/dist/packages/work/src/harness/session-store.d.ts +163 -0
- package/dist/packages/work/src/harness/session-store.js +319 -0
- package/dist/packages/work/src/harness/types.d.ts +29 -0
- package/dist/packages/work/src/harness/types.js +14 -0
- package/dist/packages/work/src/harnesses.d.ts +38 -0
- package/dist/packages/work/src/harnesses.js +38 -0
- package/dist/packages/work/src/hold/loop.d.ts +36 -0
- package/dist/packages/work/src/hold/loop.js +23 -0
- package/dist/packages/work/src/hold/mcp.d.ts +55 -0
- package/dist/packages/work/src/hold/mcp.js +149 -0
- package/dist/packages/work/src/hub/client.d.ts +22 -0
- package/dist/packages/work/src/hub/client.js +78 -0
- package/dist/packages/work/src/hub/types.d.ts +290 -0
- package/dist/packages/work/src/hub/types.js +28 -0
- package/dist/packages/work/src/lease/loop.d.ts +94 -0
- package/dist/packages/work/src/lease/loop.js +277 -0
- package/dist/packages/work/src/main.d.ts +1 -0
- package/dist/packages/work/src/main.js +48 -0
- package/dist/packages/work/src/mcp/server.d.ts +125 -0
- package/dist/packages/work/src/mcp/server.js +320 -0
- package/dist/packages/work/src/roles/agent-run.d.ts +118 -0
- package/dist/packages/work/src/roles/agent-run.js +447 -0
- package/dist/packages/work/src/roles/exec.d.ts +40 -0
- package/dist/packages/work/src/roles/exec.js +203 -0
- package/dist/packages/work/src/roles/hold.d.ts +68 -0
- package/dist/packages/work/src/roles/hold.js +324 -0
- package/dist/packages/work/src/roles/join.d.ts +18 -0
- package/dist/packages/work/src/roles/join.js +204 -0
- package/dist/packages/work/src/roles/lint.d.ts +59 -0
- package/dist/packages/work/src/roles/lint.js +200 -0
- package/dist/packages/work/src/roles/prepare.d.ts +1 -0
- package/dist/packages/work/src/roles/prepare.js +249 -0
- package/dist/packages/work/src/roles/release.d.ts +48 -0
- package/dist/packages/work/src/roles/release.js +137 -0
- package/dist/packages/work/src/roles/sessions.d.ts +61 -0
- package/dist/packages/work/src/roles/sessions.js +180 -0
- package/dist/packages/work/src/roles/settings.d.ts +6 -0
- package/dist/packages/work/src/roles/settings.js +66 -0
- package/dist/packages/work/src/roles/shift.d.ts +12 -0
- package/dist/packages/work/src/roles/shift.js +228 -0
- package/dist/packages/work/src/roles/signals.d.ts +55 -0
- package/dist/packages/work/src/roles/signals.js +62 -0
- package/dist/packages/work/src/settings/provision.d.ts +21 -0
- package/dist/packages/work/src/settings/provision.js +47 -0
- package/dist/packages/work/src/settings/settings.d.ts +120 -0
- package/dist/packages/work/src/settings/settings.js +116 -0
- package/dist/packages/work/src/shift/client.d.ts +24 -0
- package/dist/packages/work/src/shift/client.js +124 -0
- package/dist/packages/work/src/shift/loop.d.ts +120 -0
- package/dist/packages/work/src/shift/loop.js +468 -0
- package/dist/packages/work/src/shift/protocol.d.ts +91 -0
- package/dist/packages/work/src/shift/protocol.js +24 -0
- package/dist/packages/work/src/shift/routing.d.ts +35 -0
- package/dist/packages/work/src/shift/routing.js +50 -0
- package/dist/packages/work/src/shift/runtime.d.ts +43 -0
- package/dist/packages/work/src/shift/runtime.js +274 -0
- package/dist/packages/work/src/shift/server.d.ts +23 -0
- package/dist/packages/work/src/shift/server.js +716 -0
- package/dist/packages/work/src/shift/spawn.d.ts +86 -0
- package/dist/packages/work/src/shift/spawn.js +100 -0
- package/dist/packages/work/src/shift/state.d.ts +77 -0
- package/dist/packages/work/src/shift/state.js +162 -0
- package/dist/packages/work/src/usage.d.ts +6 -0
- package/dist/packages/work/src/usage.js +175 -0
- package/dist/{cli.d.ts → src/cli.d.ts} +18 -3
- package/dist/{cli.js → src/cli.js} +770 -114
- package/dist/{credentials.d.ts → src/credentials.d.ts} +18 -12
- package/dist/{credentials.js → src/credentials.js} +14 -12
- package/dist/{defs.js → src/defs.js} +36 -36
- package/dist/{engine.d.ts → src/engine.d.ts} +8 -8
- package/dist/{engine.js → src/engine.js} +14 -14
- package/dist/{hub.d.ts → src/hub.d.ts} +205 -10
- package/dist/{hub.js → src/hub.js} +370 -12
- package/dist/{index.d.ts → src/index.d.ts} +1 -1
- package/dist/{index.js → src/index.js} +1 -1
- package/dist/{mcp → src/mcp}/serve.d.ts +5 -4
- package/dist/{mcp → src/mcp}/serve.js +168 -31
- package/dist/{mcp → src/mcp}/server.d.ts +6 -9
- package/dist/{mcp → src/mcp}/server.js +6 -9
- package/dist/{model.js → src/model.js} +1 -1
- package/dist/{types.d.ts → src/types.d.ts} +18 -18
- package/dist/{util.d.ts → src/util.d.ts} +3 -2
- package/dist/{util.js → src/util.js} +3 -2
- package/dist/{owenwork.d.ts → src/work-settings.d.ts} +22 -22
- package/dist/{owenwork.js → src/work-settings.js} +27 -27
- package/docs/authoring.md +93 -53
- package/docs/cli.md +597 -76
- package/docs/design.md +33 -33
- package/docs/embedding.md +10 -9
- package/examples/workflows/{command-worker.yaml → command-executor.yaml} +15 -15
- package/examples/workflows/compile-dev-playbook.yaml +338 -0
- package/examples/workflows/judges/gate-sharpness.md +39 -0
- package/examples/workflows/judges/security.md +34 -0
- package/package.json +17 -8
- /package/dist/{add.d.ts → src/add.d.ts} +0 -0
- /package/dist/{add.js → src/add.js} +0 -0
- /package/dist/{defs.d.ts → src/defs.d.ts} +0 -0
- /package/dist/{factory.d.ts → src/factory.d.ts} +0 -0
- /package/dist/{factory.js → src/factory.js} +0 -0
- /package/dist/{lock.d.ts → src/lock.d.ts} +0 -0
- /package/dist/{lock.js → src/lock.js} +0 -0
- /package/dist/{model.d.ts → src/model.d.ts} +0 -0
- /package/dist/{paths.d.ts → src/paths.d.ts} +0 -0
- /package/dist/{paths.js → src/paths.js} +0 -0
- /package/dist/{schema.d.ts → src/schema.d.ts} +0 -0
- /package/dist/{schema.js → src/schema.js} +0 -0
- /package/dist/{store.d.ts → src/store.d.ts} +0 -0
- /package/dist/{store.js → src/store.js} +0 -0
- /package/dist/{types.js → src/types.js} +0 -0
- /package/dist/{untar.d.ts → src/untar.d.ts} +0 -0
- /package/dist/{untar.js → src/untar.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-05',
|
|
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,118 @@
|
|
|
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 spawn argv (D6) carries only `<workflow>/<run>`, `--origin`, `--shift`
|
|
34
|
+
* and `--harness` — no def name and no def hash, both of which the bundle cache
|
|
35
|
+
* needs. So the worker resolves the def name ITSELF: one non-claiming
|
|
36
|
+
* `whats_next({workflow})` returns `def` in per-workflow mode, which feeds
|
|
37
|
+
* `readDispatchBundle(cacheDir, def)` → `bundle.def.hash` → `readStepSpec(...)`.
|
|
38
|
+
* That keeps the argv exactly as D6 specifies and adds no new settings surface.
|
|
39
|
+
*
|
|
40
|
+
* ── CREDENTIALS ──
|
|
41
|
+
*
|
|
42
|
+
* Origin/credential resolution mirrors `exec`: origin `--origin` → settings; the
|
|
43
|
+
* bearer comes from owenloop's store via `resolveBearer`, reading the
|
|
44
|
+
* `agent:<account>` slot for the account the shift set in this child's
|
|
45
|
+
* `OWENLOOP_ACCOUNT` spawn env (default `default`). `agent-run` has NO `--as`
|
|
46
|
+
* flag — the spawn-env channel is the contract.
|
|
47
|
+
*
|
|
48
|
+
* D10, CLOSED IN PHASE 6. The MCP mount this worker builds carries no
|
|
49
|
+
* credential — the mounted work-holder resolves its own from the same store —
|
|
50
|
+
* and as of Phase 6 the dev-only `OWENLOOP_TOKEN` override is no longer
|
|
51
|
+
* inherited by the harness child either. Both adapters filter it out through
|
|
52
|
+
* `filterOwenloopEnv` (`src/harness/child-env.ts`), an allowlist scoped to the
|
|
53
|
+
* `OWENLOOP_*` namespace ONLY, so nothing a harness needs in order to start —
|
|
54
|
+
* `PATH`, `HOME`, `NODE_OPTIONS`, a shift setting, a vendor's own credential
|
|
55
|
+
* variable — can be stranded by it.
|
|
56
|
+
*
|
|
57
|
+
* The consequence lands in this file: because the child cannot see the override,
|
|
58
|
+
* THIS ROLE IGNORES IT TOO, so worker and child authenticate as the same
|
|
59
|
+
* principal. See the block around the `resolveBearer` call. `resolveBearer`
|
|
60
|
+
* itself is unchanged and every other role still honours the override.
|
|
61
|
+
*
|
|
62
|
+
* Phase 4's narrower guarantee still holds and is still asserted: the rejection
|
|
63
|
+
* delta and the replay brief carry no credential material
|
|
64
|
+
* (`test/agent-rejection.test.ts`).
|
|
65
|
+
*
|
|
66
|
+
* ── WHERE THE STEP AGENT WORKS (Phase 4) ──
|
|
67
|
+
*
|
|
68
|
+
* `OrderPacket.workdir` (the hub's choice) > `<workRoot>/<workflow>/<run>/` >
|
|
69
|
+
* this process's cwd. The middle rung is new: see `src/agent/workdir.ts` for the
|
|
70
|
+
* per-RUN layout and the reaper that removes it.
|
|
71
|
+
*/
|
|
72
|
+
import '../harnesses.ts';
|
|
73
|
+
import { type AgentRunOutcome } from '../agent/loop.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
|
+
/** cwd for an order that carries no `workdir` (default `process.cwd()`). */
|
|
109
|
+
cwd?: string;
|
|
110
|
+
/**
|
|
111
|
+
* Holder id override (default `<hostname>:<pid>`) — pinned in tests. Injecting
|
|
112
|
+
* `hub` also skips credential resolution entirely, so a test needs no
|
|
113
|
+
* owenloop credential store on disk.
|
|
114
|
+
*/
|
|
115
|
+
holderId?: string;
|
|
116
|
+
}
|
|
117
|
+
export declare function run(args: string[], deps?: RunDeps): Promise<number>;
|
|
118
|
+
export {};
|