@yolo-labs/yolo-cli 0.42.0 → 0.43.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/dist/cli.js +4 -743
- package/dist/deploy-cli.js +119 -9
- package/dist/deploy-config.js +80 -1
- package/dist/work-client.js +17 -20
- package/package.json +1 -1
- package/dist/plan-diff.js +0 -162
- package/dist/plan-export.js +0 -261
- package/dist/plan-get.js +0 -244
- package/dist/plan-import.js +0 -420
- package/dist/plan-list.js +0 -153
- package/dist/plan-open.js +0 -100
- package/dist/plan-state.js +0 -228
- package/dist/run-get.js +0 -130
- package/dist/run-lifecycle.js +0 -133
- package/dist/run-start.js +0 -110
- package/dist/run-transfer.js +0 -128
package/dist/plan-state.js
DELETED
|
@@ -1,228 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `yolo plan activate <planId>` / `yolo plan archive <planId>`.
|
|
3
|
-
*
|
|
4
|
-
* Out-of-band Plan-state transitions for plans already in the DB.
|
|
5
|
-
* The file-driven flow can author a plan as `active` from the start
|
|
6
|
-
* (Phase 8d.2 — substrate now accepts `state` on `work.create_plan`),
|
|
7
|
-
* but operators still need a way to:
|
|
8
|
-
* - flip an existing draft to active without rewriting the file
|
|
9
|
-
* - archive a plan that's done its job
|
|
10
|
-
* neither of which has a clean file-level expression.
|
|
11
|
-
*
|
|
12
|
-
* Flow:
|
|
13
|
-
* 1. Validate planId regex.
|
|
14
|
-
* 2. Resolve env trio.
|
|
15
|
-
* 3. Mint a session-bound delegated token
|
|
16
|
-
* (`work.get_plan` + `work.update_plan`, both already in
|
|
17
|
-
* `SUBSTRATE_CLI_PLAN_SCOPES`).
|
|
18
|
-
* 4. Optional `--workspace` sanity-check against the minted
|
|
19
|
-
* workspaceId.
|
|
20
|
-
* 5. GET the current plan to fetch (a) its current state — for the
|
|
21
|
-
* "already in target state" no-op — and (b) its current
|
|
22
|
-
* `version` for the `update_plan(baseVersion, …)` CAS.
|
|
23
|
-
* 6. PATCH with `{ baseVersion, mutations: [{ op: 'set-state',
|
|
24
|
-
* state: <target> }] }`. The substrate's `applyMutations`
|
|
25
|
-
* enforces the transition matrix (draft→active|archived,
|
|
26
|
-
* active→archived; archived is terminal; same-state is allowed
|
|
27
|
-
* and bumps version).
|
|
28
|
-
* 7. Refresh the lockfile entry's `lastImportedVersion` to the
|
|
29
|
-
* new DB version so a subsequent `yolo plan import` is a
|
|
30
|
-
* NO_CHANGE rather than a false-positive DIVERGED. Revision
|
|
31
|
-
* is unchanged because the file didn't change — same
|
|
32
|
-
* revision pointer, new version.
|
|
33
|
-
*
|
|
34
|
-
* Field rename note: the Plan-level state field was `authoringState`
|
|
35
|
-
* before 2026-05-09 and `state` after (item 17 of
|
|
36
|
-
* `docs/SUBSTRATE_IMPROVEMENTS.md`). The substrate accepts only `state`.
|
|
37
|
-
*
|
|
38
|
-
* Exit codes (used by the CLI wrapper):
|
|
39
|
-
* - 0 = success (transition applied OR no-op when already in target)
|
|
40
|
-
* - 1 = http (plan not found, version conflict, transition rejected)
|
|
41
|
-
* - 64 = usage (bad planId, missing env, --workspace mismatch)
|
|
42
|
-
*/
|
|
43
|
-
import path from 'node:path';
|
|
44
|
-
import { LockfileError, getEntry, readLockfile, resolveLockfilePath, setEntry, writeLockfile, } from './lockfile.js';
|
|
45
|
-
import { SUBSTRATE_CLI_PLAN_SCOPES, WorkClientError, authenticatedRequest, mintSubstrateToken, } from './work-client.js';
|
|
46
|
-
import { resolveSubstrateContext } from './auth-context.js';
|
|
47
|
-
const PLAN_ID_REGEX = /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,127}$/;
|
|
48
|
-
// ─── Public entry ─────────────────────────────────────────────────────────
|
|
49
|
-
export async function runPlanStateTransition(options) {
|
|
50
|
-
const env = options.env ?? process.env;
|
|
51
|
-
const now = options.now ?? (() => new Date().toISOString());
|
|
52
|
-
// 1) planId regex
|
|
53
|
-
if (!PLAN_ID_REGEX.test(options.planId)) {
|
|
54
|
-
return fail('usage', `invalid planId '${options.planId}': must match /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,127}$/`);
|
|
55
|
-
}
|
|
56
|
-
// 2) env trio
|
|
57
|
-
const auth = resolveSubstrateContext(env);
|
|
58
|
-
if (!auth.ok)
|
|
59
|
-
return fail('auth', auth.message);
|
|
60
|
-
const { sessionId, commonApiUrl, userToken } = auth.context;
|
|
61
|
-
// 3) Mint token
|
|
62
|
-
let mint;
|
|
63
|
-
try {
|
|
64
|
-
mint = await mintSubstrateToken({
|
|
65
|
-
commonApiUrl,
|
|
66
|
-
userToken,
|
|
67
|
-
sessionId,
|
|
68
|
-
scopes: SUBSTRATE_CLI_PLAN_SCOPES,
|
|
69
|
-
fetchImpl: options.fetchImpl,
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
catch (err) {
|
|
73
|
-
if (err instanceof WorkClientError) {
|
|
74
|
-
return fail('auth', `failed to mint substrate token: ${err.message}`, { status: err.status, code: err.code });
|
|
75
|
-
}
|
|
76
|
-
return fail('auth', `failed to mint substrate token: ${describeError(err)}`);
|
|
77
|
-
}
|
|
78
|
-
// 4) --workspace assertion
|
|
79
|
-
if (options.workspaceFlag && options.workspaceFlag !== mint.workspaceId) {
|
|
80
|
-
return fail('workspace_mismatch', `--workspace ${options.workspaceFlag} does not match the workspace bound to this session (${mint.workspaceId}).`);
|
|
81
|
-
}
|
|
82
|
-
const ctx = {
|
|
83
|
-
commonApiUrl,
|
|
84
|
-
delegatedToken: mint.token,
|
|
85
|
-
fetchImpl: options.fetchImpl,
|
|
86
|
-
};
|
|
87
|
-
// 5) GET current plan (need version for CAS, state for no-op detection)
|
|
88
|
-
const getResponse = await authenticatedRequest(ctx, `/workspaces/${mint.workspaceId}/plans/${options.planId}`, { method: 'GET' });
|
|
89
|
-
if (!getResponse.ok) {
|
|
90
|
-
const text = await safeReadText(getResponse);
|
|
91
|
-
return fail('http', `work.get_plan failed: HTTP ${getResponse.status} — ${text}`, { status: getResponse.status });
|
|
92
|
-
}
|
|
93
|
-
const getJson = (await getResponse.json());
|
|
94
|
-
if (!getJson.plan) {
|
|
95
|
-
return fail('http', 'work.get_plan response missing `plan` field');
|
|
96
|
-
}
|
|
97
|
-
const currentState = getJson.plan.state;
|
|
98
|
-
const baseVersion = getJson.plan.version;
|
|
99
|
-
// 6) No-op short-circuit OR PATCH. Either way, we end up with
|
|
100
|
-
// a definitive (state, version) pair that the lockfile must
|
|
101
|
-
// reflect — the substrate's view of the plan is the source
|
|
102
|
-
// of truth for `lastImportedVersion`. Skipping the lockfile
|
|
103
|
-
// refresh on the no-op path was the Codex R1 finding:
|
|
104
|
-
// if the lockfile pointed at a stale version (e.g., the
|
|
105
|
-
// plan was activated outside this CLI between two
|
|
106
|
-
// `yolo plan activate` invocations), retry would return
|
|
107
|
-
// success but leave the divergence to fire on the next
|
|
108
|
-
// `yolo plan import`.
|
|
109
|
-
let finalVersion;
|
|
110
|
-
if (currentState === options.targetState) {
|
|
111
|
-
// No-op: substrate would accept same-state and bump version,
|
|
112
|
-
// but we'd rather not churn the DB on idempotent calls. The
|
|
113
|
-
// GET-response version is what the lockfile must align to.
|
|
114
|
-
finalVersion = baseVersion;
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
const patchResponse = await authenticatedRequest(ctx, `/workspaces/${mint.workspaceId}/plans/${options.planId}`, {
|
|
118
|
-
method: 'PATCH',
|
|
119
|
-
jsonBody: {
|
|
120
|
-
baseVersion,
|
|
121
|
-
mutations: [{ op: 'set-state', state: options.targetState }],
|
|
122
|
-
},
|
|
123
|
-
});
|
|
124
|
-
if (!patchResponse.ok) {
|
|
125
|
-
const text = await safeReadText(patchResponse);
|
|
126
|
-
return fail('http', `work.update_plan failed: HTTP ${patchResponse.status} — ${text}`, { status: patchResponse.status });
|
|
127
|
-
}
|
|
128
|
-
const patchJson = (await patchResponse.json());
|
|
129
|
-
if (typeof patchJson.version !== 'number') {
|
|
130
|
-
return fail('http', 'work.update_plan response missing version field');
|
|
131
|
-
}
|
|
132
|
-
finalVersion = patchJson.version;
|
|
133
|
-
}
|
|
134
|
-
// 7) Refresh the lockfile entry (if one exists for this
|
|
135
|
-
// workspace/plan). Same on both paths: revision pointer
|
|
136
|
-
// stays put (the file didn't change), version + timestamp
|
|
137
|
-
// realign to the substrate's current view. Best-effort skip
|
|
138
|
-
// when no entry exists (e.g., plan was created via curl,
|
|
139
|
-
// not import).
|
|
140
|
-
const plansDir = options.plansDir ?? path.resolve('.yolo', 'plans');
|
|
141
|
-
const lockfileResult = refreshLockfileEntry({
|
|
142
|
-
plansDir,
|
|
143
|
-
lockfileFlag: options.lockfileFlag,
|
|
144
|
-
workspaceId: mint.workspaceId,
|
|
145
|
-
planId: options.planId,
|
|
146
|
-
newVersion: finalVersion,
|
|
147
|
-
now,
|
|
148
|
-
});
|
|
149
|
-
if (!lockfileResult.ok)
|
|
150
|
-
return lockfileResult.error;
|
|
151
|
-
return {
|
|
152
|
-
ok: true,
|
|
153
|
-
planId: options.planId,
|
|
154
|
-
workspaceId: mint.workspaceId,
|
|
155
|
-
fromState: currentState,
|
|
156
|
-
toState: options.targetState,
|
|
157
|
-
version: finalVersion,
|
|
158
|
-
noop: currentState === options.targetState,
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
function refreshLockfileEntry(opts) {
|
|
162
|
-
let lockfilePath;
|
|
163
|
-
let lockfile;
|
|
164
|
-
try {
|
|
165
|
-
lockfilePath = resolveLockfilePath(opts.plansDir, opts.lockfileFlag);
|
|
166
|
-
lockfile = readLockfile(lockfilePath);
|
|
167
|
-
}
|
|
168
|
-
catch (err) {
|
|
169
|
-
if (err instanceof LockfileError) {
|
|
170
|
-
return { ok: false, error: fail('lockfile', err.message, { code: err.code }) };
|
|
171
|
-
}
|
|
172
|
-
return { ok: false, error: fail('lockfile', describeError(err)) };
|
|
173
|
-
}
|
|
174
|
-
const existing = getEntry(lockfile, opts.workspaceId, opts.planId);
|
|
175
|
-
if (!existing) {
|
|
176
|
-
// Best-effort: no entry, no write. The plan was likely
|
|
177
|
-
// created via curl or a different operator's machine.
|
|
178
|
-
return { ok: true };
|
|
179
|
-
}
|
|
180
|
-
const refreshed = {
|
|
181
|
-
lastImportedRevision: existing.lastImportedRevision,
|
|
182
|
-
lastImportedVersion: opts.newVersion,
|
|
183
|
-
lastImportedAt: opts.now(),
|
|
184
|
-
};
|
|
185
|
-
setEntry(lockfile, opts.workspaceId, opts.planId, refreshed);
|
|
186
|
-
try {
|
|
187
|
-
writeLockfile(lockfilePath, lockfile);
|
|
188
|
-
}
|
|
189
|
-
catch (err) {
|
|
190
|
-
if (err instanceof LockfileError) {
|
|
191
|
-
return { ok: false, error: fail('lockfile', err.message, { code: err.code }) };
|
|
192
|
-
}
|
|
193
|
-
return { ok: false, error: fail('lockfile', describeError(err)) };
|
|
194
|
-
}
|
|
195
|
-
return { ok: true };
|
|
196
|
-
}
|
|
197
|
-
// ─── Helpers ──────────────────────────────────────────────────────────────
|
|
198
|
-
function fail(kind, message, detail) {
|
|
199
|
-
return { ok: false, kind, message, detail };
|
|
200
|
-
}
|
|
201
|
-
function describeError(err) {
|
|
202
|
-
return err instanceof Error ? err.message : String(err);
|
|
203
|
-
}
|
|
204
|
-
async function safeReadText(response) {
|
|
205
|
-
try {
|
|
206
|
-
return await response.text();
|
|
207
|
-
}
|
|
208
|
-
catch {
|
|
209
|
-
return '<no body>';
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
export function exitCodeForFailure(kind) {
|
|
213
|
-
switch (kind) {
|
|
214
|
-
case 'usage':
|
|
215
|
-
case 'auth':
|
|
216
|
-
case 'workspace_mismatch':
|
|
217
|
-
return 64;
|
|
218
|
-
case 'http':
|
|
219
|
-
case 'lockfile':
|
|
220
|
-
return 1;
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
export function formatSuccess(result) {
|
|
224
|
-
if (result.noop) {
|
|
225
|
-
return `OK: plan '${result.planId}' is already ${result.toState} (workspace ${result.workspaceId}, version ${result.version})`;
|
|
226
|
-
}
|
|
227
|
-
return `OK: plan '${result.planId}' transitioned ${result.fromState} → ${result.toState} (workspace ${result.workspaceId}, version ${result.version})`;
|
|
228
|
-
}
|
package/dist/run-get.js
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `yolo run get <runId>` — read a Plan Run from the substrate.
|
|
3
|
-
*
|
|
4
|
-
* Mints a session-bound delegated token (work.get_run scope) and
|
|
5
|
-
* GETs `/internal/work/workspaces/<wsId>/runs/<planRunId>`. Prints
|
|
6
|
-
* a human-readable summary by default; `--json` for the raw response
|
|
7
|
-
* (useful for jq pipelines).
|
|
8
|
-
*
|
|
9
|
-
* No file I/O, no lockfile mutation. Pure read.
|
|
10
|
-
*
|
|
11
|
-
* Errors map to exit codes via the CLI wrapper:
|
|
12
|
-
* - 0 = success
|
|
13
|
-
* - 1 = http (run not found, etc.)
|
|
14
|
-
* - 64 = usage (bad runId, missing env trio, --workspace mismatch)
|
|
15
|
-
*/
|
|
16
|
-
import { SUBSTRATE_CLI_RUN_SCOPES, WorkClientError, authenticatedRequest, mintSubstrateToken, } from './work-client.js';
|
|
17
|
-
import { resolveSubstrateContext } from './auth-context.js';
|
|
18
|
-
// ─── Public entry ─────────────────────────────────────────────────────────
|
|
19
|
-
const PLAN_RUN_ID_REGEX = /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,191}$/;
|
|
20
|
-
export async function runRunGet(options) {
|
|
21
|
-
const env = options.env ?? process.env;
|
|
22
|
-
const format = options.outputFormat ?? 'summary';
|
|
23
|
-
if (!PLAN_RUN_ID_REGEX.test(options.planRunId)) {
|
|
24
|
-
return fail('usage', `invalid planRunId '${options.planRunId}': must match /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,191}$/`);
|
|
25
|
-
}
|
|
26
|
-
const auth = resolveSubstrateContext(env);
|
|
27
|
-
if (!auth.ok)
|
|
28
|
-
return fail('auth', auth.message);
|
|
29
|
-
const { sessionId, commonApiUrl, userToken } = auth.context;
|
|
30
|
-
let mint;
|
|
31
|
-
try {
|
|
32
|
-
mint = await mintSubstrateToken({
|
|
33
|
-
commonApiUrl,
|
|
34
|
-
userToken,
|
|
35
|
-
sessionId,
|
|
36
|
-
scopes: SUBSTRATE_CLI_RUN_SCOPES,
|
|
37
|
-
fetchImpl: options.fetchImpl,
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
catch (err) {
|
|
41
|
-
if (err instanceof WorkClientError) {
|
|
42
|
-
return fail('auth', `failed to mint substrate token: ${err.message}`, { status: err.status, code: err.code });
|
|
43
|
-
}
|
|
44
|
-
return fail('auth', `failed to mint substrate token: ${describeError(err)}`);
|
|
45
|
-
}
|
|
46
|
-
if (options.workspaceFlag && options.workspaceFlag !== mint.workspaceId) {
|
|
47
|
-
return fail('workspace_mismatch', `--workspace ${options.workspaceFlag} does not match the workspace bound to this session (${mint.workspaceId}).`);
|
|
48
|
-
}
|
|
49
|
-
const ctx = {
|
|
50
|
-
commonApiUrl,
|
|
51
|
-
delegatedToken: mint.token,
|
|
52
|
-
fetchImpl: options.fetchImpl,
|
|
53
|
-
};
|
|
54
|
-
const response = await authenticatedRequest(ctx, `/workspaces/${mint.workspaceId}/runs/${options.planRunId}`, { method: 'GET' });
|
|
55
|
-
if (!response.ok) {
|
|
56
|
-
const text = await safeReadText(response);
|
|
57
|
-
return fail('http', `work.get_run failed: HTTP ${response.status} — ${text}`, { status: response.status });
|
|
58
|
-
}
|
|
59
|
-
const json = (await response.json());
|
|
60
|
-
if (!json.run) {
|
|
61
|
-
return fail('http', 'work.get_run response missing `run` field');
|
|
62
|
-
}
|
|
63
|
-
return {
|
|
64
|
-
ok: true,
|
|
65
|
-
output: format === 'json' ? formatJson(json.run) : formatSummary(json.run, mint.workspaceId),
|
|
66
|
-
run: json.run,
|
|
67
|
-
workspaceId: mint.workspaceId,
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
// ─── Output formatting ────────────────────────────────────────────────────
|
|
71
|
-
/**
|
|
72
|
-
* Header + per-step table. Table columns auto-size to the longest
|
|
73
|
-
* stepId / state / stepRunId / tileId so output stays aligned even
|
|
74
|
-
* for plans with mixed-width identifiers.
|
|
75
|
-
*/
|
|
76
|
-
export function formatSummary(run, workspaceId) {
|
|
77
|
-
const lines = [];
|
|
78
|
-
lines.push(`Plan Run ${run.planRunId} (workspace ${workspaceId}, run #${run.runNumber}, plan ${run.planId} v${run.planVersion}, executionState=${run.executionState})`);
|
|
79
|
-
const operatorTag = run.operatorAgentId
|
|
80
|
-
? `${run.operatorAgentId}${run.operatorResponsive === false ? ' (unresponsive)' : run.operatorResponsive === true ? ' (responsive)' : ''}`
|
|
81
|
-
: '(unbound)';
|
|
82
|
-
lines.push(` operator: ${operatorTag}`);
|
|
83
|
-
if (run.startedAt)
|
|
84
|
-
lines.push(` startedAt: ${run.startedAt}`);
|
|
85
|
-
if (run.endedAt)
|
|
86
|
-
lines.push(` endedAt: ${run.endedAt}`);
|
|
87
|
-
const inputCount = run.inputs ? Object.keys(run.inputs).length : 0;
|
|
88
|
-
if (inputCount > 0)
|
|
89
|
-
lines.push(` inputs: ${inputCount}`);
|
|
90
|
-
lines.push(` steps: ${run.steps.length}`);
|
|
91
|
-
if (run.steps.length === 0)
|
|
92
|
-
return lines.join('\n');
|
|
93
|
-
const stepIdW = Math.max(6, ...run.steps.map((s) => s.stepId.length));
|
|
94
|
-
const stateW = Math.max(5, ...run.steps.map((s) => s.state.length));
|
|
95
|
-
const stepRunW = Math.max(8, ...run.steps.map((s) => (s.latestStepRunId ?? '—').length));
|
|
96
|
-
for (const step of run.steps) {
|
|
97
|
-
const stepRun = step.latestStepRunId ?? '—';
|
|
98
|
-
const tile = step.tileId ?? '—';
|
|
99
|
-
lines.push(` - ${step.stepId.padEnd(stepIdW)} ${step.state.padEnd(stateW)} ${stepRun.padEnd(stepRunW)} ${tile}`);
|
|
100
|
-
}
|
|
101
|
-
return lines.join('\n');
|
|
102
|
-
}
|
|
103
|
-
function formatJson(run) {
|
|
104
|
-
return JSON.stringify(run, null, 2);
|
|
105
|
-
}
|
|
106
|
-
// ─── Helpers ──────────────────────────────────────────────────────────────
|
|
107
|
-
function fail(kind, message, detail) {
|
|
108
|
-
return { ok: false, kind, message, detail };
|
|
109
|
-
}
|
|
110
|
-
function describeError(err) {
|
|
111
|
-
return err instanceof Error ? err.message : String(err);
|
|
112
|
-
}
|
|
113
|
-
async function safeReadText(response) {
|
|
114
|
-
try {
|
|
115
|
-
return await response.text();
|
|
116
|
-
}
|
|
117
|
-
catch {
|
|
118
|
-
return '<no body>';
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
export function exitCodeForFailure(kind) {
|
|
122
|
-
switch (kind) {
|
|
123
|
-
case 'usage':
|
|
124
|
-
case 'auth':
|
|
125
|
-
case 'workspace_mismatch':
|
|
126
|
-
return 64;
|
|
127
|
-
case 'http':
|
|
128
|
-
return 1;
|
|
129
|
-
}
|
|
130
|
-
}
|
package/dist/run-lifecycle.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `yolo run pause / resume / cancel <runId>` — lifecycle transitions.
|
|
3
|
-
*
|
|
4
|
-
* The three verbs share a single runner because the routes share a
|
|
5
|
-
* single transition handler on the server (common-api/src/routes/
|
|
6
|
-
* workspaces.ts:transitionPlanRunForUser). They differ only in
|
|
7
|
-
* target state, valid source states, and whether they accept a
|
|
8
|
-
* `--reason`.
|
|
9
|
-
*
|
|
10
|
-
* Auth model: hits the user-facing route at
|
|
11
|
-
* `/v1/workspaces/:id/runs/:planRunId/{verb}` via X-Internal-Auth +
|
|
12
|
-
* X-User-Id (the substrate CLI is by definition invoked by the
|
|
13
|
-
* workspace owner — operating as them is the right default). The
|
|
14
|
-
* user route checks workspace ownership only and skips R4 operator
|
|
15
|
-
* binding, so this works regardless of which agent (if any) is
|
|
16
|
-
* bound to the run.
|
|
17
|
-
*
|
|
18
|
-
* Why not the MCP path: that path's R4 binding makes sense for
|
|
19
|
-
* agent-to-agent calls (claude pausing its own run) but produces
|
|
20
|
-
* 403 NOT_AUTHORIZED whenever the user wants to cancel a run owned
|
|
21
|
-
* by a different agent or by the webapp launcher. Forcing the user
|
|
22
|
-
* to know which side of that fence they're on is bad UX. The CLI
|
|
23
|
-
* decides automatically.
|
|
24
|
-
*
|
|
25
|
-
* Errors map to exit codes via the CLI wrapper:
|
|
26
|
-
* - 0 = success
|
|
27
|
-
* - 1 = http (404, 409 INVALID_STATE, 5xx)
|
|
28
|
-
* - 64 = usage (bad runId, missing env trio, --workspace mismatch,
|
|
29
|
-
* reason too long pre-network, bad target verb)
|
|
30
|
-
*/
|
|
31
|
-
import { SUBSTRATE_CLI_RUN_SCOPES, WorkClientError, mintSubstrateToken, userRouteRequest, } from './work-client.js';
|
|
32
|
-
import { resolveSubstrateContext } from './auth-context.js';
|
|
33
|
-
// ─── Public entry ─────────────────────────────────────────────────────────
|
|
34
|
-
const PLAN_RUN_ID_REGEX = /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,191}$/;
|
|
35
|
-
const REASON_MAX_LEN = 1024;
|
|
36
|
-
export async function runRunLifecycle(options) {
|
|
37
|
-
const env = options.env ?? process.env;
|
|
38
|
-
const format = options.outputFormat ?? 'summary';
|
|
39
|
-
if (options.verb !== 'pause' && options.verb !== 'resume' && options.verb !== 'cancel') {
|
|
40
|
-
return fail('usage', `unknown verb '${options.verb}': must be pause, resume, or cancel`);
|
|
41
|
-
}
|
|
42
|
-
if (!PLAN_RUN_ID_REGEX.test(options.planRunId)) {
|
|
43
|
-
return fail('usage', `invalid planRunId '${options.planRunId}': must match /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,191}$/`);
|
|
44
|
-
}
|
|
45
|
-
if (options.reason !== undefined) {
|
|
46
|
-
if (typeof options.reason !== 'string' || options.reason.length > REASON_MAX_LEN) {
|
|
47
|
-
return fail('usage', `--reason must be a string of at most ${REASON_MAX_LEN} chars`);
|
|
48
|
-
}
|
|
49
|
-
if (options.verb === 'resume') {
|
|
50
|
-
return fail('usage', '--reason is not accepted by `run resume`');
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
const auth = resolveSubstrateContext(env);
|
|
54
|
-
if (!auth.ok)
|
|
55
|
-
return fail('auth', auth.message);
|
|
56
|
-
const { sessionId, commonApiUrl, userToken } = auth.context;
|
|
57
|
-
let mint;
|
|
58
|
-
try {
|
|
59
|
-
mint = await mintSubstrateToken({
|
|
60
|
-
commonApiUrl,
|
|
61
|
-
userToken,
|
|
62
|
-
sessionId,
|
|
63
|
-
scopes: SUBSTRATE_CLI_RUN_SCOPES,
|
|
64
|
-
fetchImpl: options.fetchImpl,
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
catch (err) {
|
|
68
|
-
if (err instanceof WorkClientError) {
|
|
69
|
-
return fail('auth', `failed to mint substrate token: ${err.message}`, { status: err.status, code: err.code });
|
|
70
|
-
}
|
|
71
|
-
return fail('auth', `failed to mint substrate token: ${describeError(err)}`);
|
|
72
|
-
}
|
|
73
|
-
if (options.workspaceFlag && options.workspaceFlag !== mint.workspaceId) {
|
|
74
|
-
return fail('workspace_mismatch', `--workspace ${options.workspaceFlag} does not match the workspace bound to this session (${mint.workspaceId}).`);
|
|
75
|
-
}
|
|
76
|
-
const body = {};
|
|
77
|
-
if (options.reason !== undefined && options.verb !== 'resume') {
|
|
78
|
-
body.reason = options.reason;
|
|
79
|
-
}
|
|
80
|
-
const path = `/workspaces/${mint.workspaceId}/runs/${options.planRunId}/${options.verb}`;
|
|
81
|
-
const response = await userRouteRequest({
|
|
82
|
-
commonApiUrl,
|
|
83
|
-
userToken,
|
|
84
|
-
fetchImpl: options.fetchImpl,
|
|
85
|
-
}, path, { method: 'POST', jsonBody: body });
|
|
86
|
-
if (!response.ok) {
|
|
87
|
-
const text = await safeReadText(response);
|
|
88
|
-
return fail('http', `work.${options.verb}_run failed: HTTP ${response.status} — ${text}`, { status: response.status });
|
|
89
|
-
}
|
|
90
|
-
const json = (await response.json());
|
|
91
|
-
if (typeof json.planRunId !== 'string' || typeof json.executionState !== 'string') {
|
|
92
|
-
return fail('http', `work.${options.verb}_run response missing planRunId or executionState`);
|
|
93
|
-
}
|
|
94
|
-
return {
|
|
95
|
-
ok: true,
|
|
96
|
-
output: format === 'json' ? formatJson(json) : formatSummary(options.verb, json),
|
|
97
|
-
response: json,
|
|
98
|
-
workspaceId: mint.workspaceId,
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
// ─── Output formatting ────────────────────────────────────────────────────
|
|
102
|
-
export function formatSummary(verb, response) {
|
|
103
|
-
const past = verb === 'pause' ? 'paused' : verb === 'resume' ? 'resumed' : 'cancelled';
|
|
104
|
-
return `OK: ${past} run ${response.planRunId} (executionState=${response.executionState})`;
|
|
105
|
-
}
|
|
106
|
-
function formatJson(response) {
|
|
107
|
-
return JSON.stringify(response, null, 2);
|
|
108
|
-
}
|
|
109
|
-
// ─── Helpers ──────────────────────────────────────────────────────────────
|
|
110
|
-
function fail(kind, message, detail) {
|
|
111
|
-
return { ok: false, kind, message, detail };
|
|
112
|
-
}
|
|
113
|
-
function describeError(err) {
|
|
114
|
-
return err instanceof Error ? err.message : String(err);
|
|
115
|
-
}
|
|
116
|
-
async function safeReadText(response) {
|
|
117
|
-
try {
|
|
118
|
-
return await response.text();
|
|
119
|
-
}
|
|
120
|
-
catch {
|
|
121
|
-
return '<no body>';
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
export function exitCodeForFailure(kind) {
|
|
125
|
-
switch (kind) {
|
|
126
|
-
case 'usage':
|
|
127
|
-
case 'auth':
|
|
128
|
-
case 'workspace_mismatch':
|
|
129
|
-
return 64;
|
|
130
|
-
case 'http':
|
|
131
|
-
return 1;
|
|
132
|
-
}
|
|
133
|
-
}
|
package/dist/run-start.js
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `yolo run start <planId>` — bootstrap a Plan Run via work.start_run.
|
|
3
|
-
*
|
|
4
|
-
* Mints a session-bound delegated token (work.start_run scope) and
|
|
5
|
-
* POSTs to `/internal/work/workspaces/<wsId>/runs` with the planId
|
|
6
|
-
* and (optional) inputs. The route handler (common-api/src/routes/
|
|
7
|
-
* work.ts) binds `operatorAgentId: ctx.agentId` — so a Run started
|
|
8
|
-
* via this verb has `operatorAgentId === 'substrate-cli'` and only
|
|
9
|
-
* the substrate CLI can pause/resume/cancel it via MCP.
|
|
10
|
-
*
|
|
11
|
-
* Inputs: passed as a JSON object literal via `--inputs '<json>'`.
|
|
12
|
-
* If absent, defaults to `{}` (the route accepts that and lets Plan
|
|
13
|
-
* input validation reject if any inputs were declared as required).
|
|
14
|
-
*
|
|
15
|
-
* Errors map to exit codes via the CLI wrapper:
|
|
16
|
-
* - 0 = success
|
|
17
|
-
* - 1 = http (plan not found, plan not active, validation failure)
|
|
18
|
-
* - 64 = usage (bad planId, malformed --inputs JSON, missing env
|
|
19
|
-
* trio, --workspace mismatch)
|
|
20
|
-
*/
|
|
21
|
-
import { SUBSTRATE_CLI_RUN_SCOPES, WorkClientError, authenticatedRequest, mintSubstrateToken, } from './work-client.js';
|
|
22
|
-
import { resolveSubstrateContext } from './auth-context.js';
|
|
23
|
-
// ─── Public entry ─────────────────────────────────────────────────────────
|
|
24
|
-
const PLAN_ID_REGEX = /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,127}$/;
|
|
25
|
-
export async function runRunStart(options) {
|
|
26
|
-
const env = options.env ?? process.env;
|
|
27
|
-
const format = options.outputFormat ?? 'summary';
|
|
28
|
-
if (!PLAN_ID_REGEX.test(options.planId)) {
|
|
29
|
-
return fail('usage', `invalid planId '${options.planId}': must match /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,127}$/`);
|
|
30
|
-
}
|
|
31
|
-
const auth = resolveSubstrateContext(env);
|
|
32
|
-
if (!auth.ok)
|
|
33
|
-
return fail('auth', auth.message);
|
|
34
|
-
const { sessionId, commonApiUrl, userToken } = auth.context;
|
|
35
|
-
let mint;
|
|
36
|
-
try {
|
|
37
|
-
mint = await mintSubstrateToken({
|
|
38
|
-
commonApiUrl,
|
|
39
|
-
userToken,
|
|
40
|
-
sessionId,
|
|
41
|
-
scopes: SUBSTRATE_CLI_RUN_SCOPES,
|
|
42
|
-
fetchImpl: options.fetchImpl,
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
catch (err) {
|
|
46
|
-
if (err instanceof WorkClientError) {
|
|
47
|
-
return fail('auth', `failed to mint substrate token: ${err.message}`, { status: err.status, code: err.code });
|
|
48
|
-
}
|
|
49
|
-
return fail('auth', `failed to mint substrate token: ${describeError(err)}`);
|
|
50
|
-
}
|
|
51
|
-
if (options.workspaceFlag && options.workspaceFlag !== mint.workspaceId) {
|
|
52
|
-
return fail('workspace_mismatch', `--workspace ${options.workspaceFlag} does not match the workspace bound to this session (${mint.workspaceId}).`);
|
|
53
|
-
}
|
|
54
|
-
const ctx = {
|
|
55
|
-
commonApiUrl,
|
|
56
|
-
delegatedToken: mint.token,
|
|
57
|
-
fetchImpl: options.fetchImpl,
|
|
58
|
-
};
|
|
59
|
-
const body = { planId: options.planId };
|
|
60
|
-
if (options.inputs !== undefined)
|
|
61
|
-
body.inputs = options.inputs;
|
|
62
|
-
const response = await authenticatedRequest(ctx, `/workspaces/${mint.workspaceId}/runs`, { method: 'POST', jsonBody: body });
|
|
63
|
-
if (!response.ok) {
|
|
64
|
-
const text = await safeReadText(response);
|
|
65
|
-
return fail('http', `work.start_run failed: HTTP ${response.status} — ${text}`, { status: response.status });
|
|
66
|
-
}
|
|
67
|
-
const json = (await response.json());
|
|
68
|
-
if (typeof json.planRunId !== 'string' || !json.planRunId) {
|
|
69
|
-
return fail('http', 'work.start_run response missing planRunId');
|
|
70
|
-
}
|
|
71
|
-
return {
|
|
72
|
-
ok: true,
|
|
73
|
-
output: format === 'json' ? formatJson(json) : formatSummary(json, options.planId),
|
|
74
|
-
response: json,
|
|
75
|
-
workspaceId: mint.workspaceId,
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
// ─── Output formatting ────────────────────────────────────────────────────
|
|
79
|
-
export function formatSummary(response, planId) {
|
|
80
|
-
const operatorTag = response.operatorAgentId ? `operator=${response.operatorAgentId}` : 'operator=(unbound)';
|
|
81
|
-
return `OK: started run ${response.planRunId} for plan ${planId} (run #${response.runNumber}, executionState=${response.executionState}, ${operatorTag})`;
|
|
82
|
-
}
|
|
83
|
-
function formatJson(response) {
|
|
84
|
-
return JSON.stringify(response, null, 2);
|
|
85
|
-
}
|
|
86
|
-
// ─── Helpers ──────────────────────────────────────────────────────────────
|
|
87
|
-
function fail(kind, message, detail) {
|
|
88
|
-
return { ok: false, kind, message, detail };
|
|
89
|
-
}
|
|
90
|
-
function describeError(err) {
|
|
91
|
-
return err instanceof Error ? err.message : String(err);
|
|
92
|
-
}
|
|
93
|
-
async function safeReadText(response) {
|
|
94
|
-
try {
|
|
95
|
-
return await response.text();
|
|
96
|
-
}
|
|
97
|
-
catch {
|
|
98
|
-
return '<no body>';
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
export function exitCodeForFailure(kind) {
|
|
102
|
-
switch (kind) {
|
|
103
|
-
case 'usage':
|
|
104
|
-
case 'auth':
|
|
105
|
-
case 'workspace_mismatch':
|
|
106
|
-
return 64;
|
|
107
|
-
case 'http':
|
|
108
|
-
return 1;
|
|
109
|
-
}
|
|
110
|
-
}
|