sim 2.2.18 → 2.2.19-preview.249.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.
- package/dist/commands/protocol/workflow-run-wait.d.ts +33 -0
- package/dist/generated/v2-api.d.ts +7 -2
- package/dist/index.js +185 -130
- package/dist/runtime.js +185 -130
- package/package.json +1 -1
|
@@ -1,3 +1,36 @@
|
|
|
1
1
|
import { type Command } from 'commander';
|
|
2
|
+
import { type Connection } from '../../runtime/called-operations';
|
|
3
|
+
/**
|
|
4
|
+
* Spelled `--wait-timeout` rather than `--timeout`, because `SIM_TIMEOUT_SECONDS`
|
|
5
|
+
* already bounds a single HTTP request while this bounds the whole wait across
|
|
6
|
+
* many of them. Two knobs both called "timeout" would be read as one, and the
|
|
7
|
+
* failure that follows is silent — raising the wrong one changes nothing.
|
|
8
|
+
*/
|
|
9
|
+
export declare const WAIT_TIMEOUT_FLAG = "--wait-timeout <seconds>";
|
|
10
|
+
export interface RunSnapshot {
|
|
11
|
+
status: string;
|
|
12
|
+
/** `time` resumes itself; `human` and null wait for a person. */
|
|
13
|
+
pauseKind: string | null;
|
|
14
|
+
resumeAt: string | null;
|
|
15
|
+
contextId: string | null;
|
|
16
|
+
}
|
|
17
|
+
export declare function parseWaitTimeout(raw: string): number;
|
|
18
|
+
interface RunWait {
|
|
19
|
+
runId: string;
|
|
20
|
+
workflowId: string;
|
|
21
|
+
/** When the wait began, for the progress line. */
|
|
22
|
+
startedAt: number;
|
|
23
|
+
/** When the wait gives up, in epoch milliseconds; `Infinity` waits indefinitely. */
|
|
24
|
+
deadline: number;
|
|
25
|
+
/** What to say when the bound elapses with the run still moving. */
|
|
26
|
+
timedOut: (snapshot: RunSnapshot) => string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Polls one run until it settles or the deadline passes, prints the run, and sets the
|
|
30
|
+
* exit status {@link WAIT_EXIT_CODES} names. A deadline already past still reads the run
|
|
31
|
+
* once, so a run that settled meanwhile reports its outcome rather than a timeout.
|
|
32
|
+
*/
|
|
33
|
+
export declare function waitForRun({ client, profile }: Connection<'getWorkflowRun'>, wait: RunWait): Promise<void>;
|
|
2
34
|
/** Adds `workflows runs wait` — poll one run until it stops moving. */
|
|
3
35
|
export declare function attachWorkflowRunWait(runs: Command): void;
|
|
36
|
+
export {};
|
|
@@ -3935,6 +3935,7 @@ export type ExecuteWorkflowBody = {
|
|
|
3935
3935
|
};
|
|
3936
3936
|
async?: boolean;
|
|
3937
3937
|
executionTimeoutSeconds?: number;
|
|
3938
|
+
waitTimeoutSeconds?: number;
|
|
3938
3939
|
stream?: boolean;
|
|
3939
3940
|
selectedOutputs?: Array<string>;
|
|
3940
3941
|
includeThinking?: boolean;
|
|
@@ -14569,7 +14570,7 @@ export declare const V2_OPERATIONS: {
|
|
|
14569
14570
|
};
|
|
14570
14571
|
readonly run: {
|
|
14571
14572
|
readonly kind: 'unknown';
|
|
14572
|
-
readonly describe: 'Workflow state and entry point to execute. Omit for the active deployment. Manual execution requires OAuth or personal-key write access and supports synchronous or streamed runs only.';
|
|
14573
|
+
readonly describe: 'Workflow state and entry point to execute. Omit for the active deployment. Manual execution requires OAuth or personal-key write access and supports synchronous or streamed runs only; `waitTimeoutSeconds` lets a synchronous one continue in the background.';
|
|
14573
14574
|
};
|
|
14574
14575
|
readonly async: {
|
|
14575
14576
|
readonly kind: 'boolean';
|
|
@@ -14578,7 +14579,11 @@ export declare const V2_OPERATIONS: {
|
|
|
14578
14579
|
};
|
|
14579
14580
|
readonly executionTimeoutSeconds: {
|
|
14580
14581
|
readonly kind: 'integer';
|
|
14581
|
-
readonly describe: "Maximum duration of an asynchronous run, in seconds, capped by the plan's execution timeout. Requires `async: true`; otherwise returns `400`.";
|
|
14582
|
+
readonly describe: "Maximum duration of an asynchronous run, in seconds, capped by the plan's execution timeout. Requires `async: true` or `waitTimeoutSeconds`; otherwise returns `400`.";
|
|
14583
|
+
};
|
|
14584
|
+
readonly waitTimeoutSeconds: {
|
|
14585
|
+
readonly kind: 'integer';
|
|
14586
|
+
readonly describe: 'Wait at most this many seconds for a synchronous run; `0` waits until it settles. A run still going then continues in the background and the response carries its run receipt instead of its result: `202` on a JSON response, or a closing `detached` frame on an `application/x-ndjson` result stream, which keeps its `200`. The receipt is sent only once the run can be read on the runs resource, where it can also be cancelled. The run is admitted under asynchronous limits, and a dropped connection no longer cancels it. Requires an API key or OAuth access token. Cannot be combined with `async` or `stream`.';
|
|
14582
14587
|
};
|
|
14583
14588
|
readonly stream: {
|
|
14584
14589
|
readonly kind: 'boolean';
|
package/dist/index.js
CHANGED
|
@@ -11953,7 +11953,7 @@ var V2_OPERATIONS = {
|
|
|
11953
11953
|
},
|
|
11954
11954
|
run: {
|
|
11955
11955
|
kind: "unknown",
|
|
11956
|
-
describe: "Workflow state and entry point to execute. Omit for the active deployment. Manual execution requires OAuth or personal-key write access and supports synchronous or streamed runs only."
|
|
11956
|
+
describe: "Workflow state and entry point to execute. Omit for the active deployment. Manual execution requires OAuth or personal-key write access and supports synchronous or streamed runs only; `waitTimeoutSeconds` lets a synchronous one continue in the background."
|
|
11957
11957
|
},
|
|
11958
11958
|
async: {
|
|
11959
11959
|
kind: "boolean",
|
|
@@ -11962,7 +11962,11 @@ var V2_OPERATIONS = {
|
|
|
11962
11962
|
},
|
|
11963
11963
|
executionTimeoutSeconds: {
|
|
11964
11964
|
kind: "integer",
|
|
11965
|
-
describe: "Maximum duration of an asynchronous run, in seconds, capped by the plan's execution timeout. Requires `async: true`; otherwise returns `400`."
|
|
11965
|
+
describe: "Maximum duration of an asynchronous run, in seconds, capped by the plan's execution timeout. Requires `async: true` or `waitTimeoutSeconds`; otherwise returns `400`."
|
|
11966
|
+
},
|
|
11967
|
+
waitTimeoutSeconds: {
|
|
11968
|
+
kind: "integer",
|
|
11969
|
+
describe: "Wait at most this many seconds for a synchronous run; `0` waits until it settles. A run still going then continues in the background and the response carries its run receipt instead of its result: `202` on a JSON response, or a closing `detached` frame on an `application/x-ndjson` result stream, which keeps its `200`. The receipt is sent only once the run can be read on the runs resource, where it can also be cancelled. The run is admitted under asynchronous limits, and a dropped connection no longer cancels it. Requires an API key or OAuth access token. Cannot be combined with `async` or `stream`."
|
|
11966
11970
|
},
|
|
11967
11971
|
stream: {
|
|
11968
11972
|
kind: "boolean",
|
|
@@ -20661,6 +20665,9 @@ var CLI_CONTRACT = {
|
|
|
20661
20665
|
describe: "Run a deployed workflow or execute saved state manually",
|
|
20662
20666
|
flags: {
|
|
20663
20667
|
async: { boolean: true, describe: "Queue the run and return immediately" },
|
|
20668
|
+
executionTimeoutSeconds: {
|
|
20669
|
+
describe: "Maximum duration of the run in seconds, capped by the plan's execution timeout. Requires --async or --wait-timeout"
|
|
20670
|
+
},
|
|
20664
20671
|
input: { json: true, describe: "Trigger input as JSON" },
|
|
20665
20672
|
run: {
|
|
20666
20673
|
hidden: true,
|
|
@@ -20674,6 +20681,7 @@ var CLI_CONTRACT = {
|
|
|
20674
20681
|
stream: { omit: true },
|
|
20675
20682
|
includeThinking: { omit: true },
|
|
20676
20683
|
includeToolCalls: { omit: true },
|
|
20684
|
+
waitTimeoutSeconds: { omit: true },
|
|
20677
20685
|
includeFileBase64: {
|
|
20678
20686
|
describe: "Inline eligible output files as base64 content. Defaults to true for the installed CLI; Chat defaults to file references and admits requested bytes only for synchronous runs. Rejected when async is true."
|
|
20679
20687
|
},
|
|
@@ -24032,6 +24040,135 @@ function attachTableImport(tables) {
|
|
|
24032
24040
|
});
|
|
24033
24041
|
}
|
|
24034
24042
|
|
|
24043
|
+
// src/commands/protocol/workflow-run-wait.ts
|
|
24044
|
+
var TERMINAL_STATUSES = new Set(["completed", "failed", "cancelled"]);
|
|
24045
|
+
var WAIT_EXIT_CODES = {
|
|
24046
|
+
completed: 0,
|
|
24047
|
+
failed: 1,
|
|
24048
|
+
cancelled: 2,
|
|
24049
|
+
paused: 3,
|
|
24050
|
+
timeout: 4
|
|
24051
|
+
};
|
|
24052
|
+
var FIRST_POLL_DELAY_MS = 2000;
|
|
24053
|
+
var MAX_POLL_DELAY_MS = 15000;
|
|
24054
|
+
var POLL_BACKOFF_FACTOR = 2;
|
|
24055
|
+
var DEFAULT_WAIT_TIMEOUT_SECONDS = 3600;
|
|
24056
|
+
var WAIT_TIMEOUT_FLAG = "--wait-timeout <seconds>";
|
|
24057
|
+
function optionalString(value) {
|
|
24058
|
+
return typeof value === "string" && value !== "" ? value : null;
|
|
24059
|
+
}
|
|
24060
|
+
function runData(raw) {
|
|
24061
|
+
return isRecordLike(raw) && isRecordLike(raw.data) ? raw.data : raw;
|
|
24062
|
+
}
|
|
24063
|
+
function readRun(raw) {
|
|
24064
|
+
const run = isRecordLike(raw) && isRecordLike(raw.data) ? raw.data : raw;
|
|
24065
|
+
if (!isRecordLike(run) || typeof run.status !== "string") {
|
|
24066
|
+
throw new SimApiError("Run status response carried no status.", 0);
|
|
24067
|
+
}
|
|
24068
|
+
const paused = toRecordOrNull(run.paused);
|
|
24069
|
+
return {
|
|
24070
|
+
status: run.status,
|
|
24071
|
+
pauseKind: paused ? optionalString(paused.pauseKind) : null,
|
|
24072
|
+
resumeAt: paused ? optionalString(paused.resumeAt) : null,
|
|
24073
|
+
contextId: paused ? optionalString(paused.contextId) : null
|
|
24074
|
+
};
|
|
24075
|
+
}
|
|
24076
|
+
function classify(snapshot) {
|
|
24077
|
+
if (snapshot.status === "paused")
|
|
24078
|
+
return snapshot.pauseKind === "time" ? null : "paused";
|
|
24079
|
+
if (!TERMINAL_STATUSES.has(snapshot.status))
|
|
24080
|
+
return null;
|
|
24081
|
+
return snapshot.status === "completed" ? "completed" : snapshot.status === "cancelled" ? "cancelled" : "failed";
|
|
24082
|
+
}
|
|
24083
|
+
function waitProgress() {
|
|
24084
|
+
let reported = false;
|
|
24085
|
+
return {
|
|
24086
|
+
advance: (status, elapsedMs) => {
|
|
24087
|
+
if (!hasProgressTerminal())
|
|
24088
|
+
return;
|
|
24089
|
+
reported = true;
|
|
24090
|
+
writeStderr(`\r${styles3().dim(`${status} — waiting ${Math.round(elapsedMs / 1000)}s…`)}\x1B[K`);
|
|
24091
|
+
},
|
|
24092
|
+
finish: () => {
|
|
24093
|
+
if (!reported)
|
|
24094
|
+
return;
|
|
24095
|
+
reported = false;
|
|
24096
|
+
writeStderr("\r\x1B[K");
|
|
24097
|
+
}
|
|
24098
|
+
};
|
|
24099
|
+
}
|
|
24100
|
+
function parseWaitTimeout(raw) {
|
|
24101
|
+
const seconds = Number(raw);
|
|
24102
|
+
if (!Number.isFinite(seconds) || seconds < 0) {
|
|
24103
|
+
throw new SimApiError(`Invalid ${WAIT_TIMEOUT_FLAG} "${raw}". Use a non-negative number of seconds, or 0 to wait indefinitely.`, 0);
|
|
24104
|
+
}
|
|
24105
|
+
return seconds;
|
|
24106
|
+
}
|
|
24107
|
+
function explain(outcome, runId, workflowId, snapshot) {
|
|
24108
|
+
if (outcome === "completed")
|
|
24109
|
+
return null;
|
|
24110
|
+
if (outcome === "failed")
|
|
24111
|
+
return `Run ${runId} failed.`;
|
|
24112
|
+
if (outcome === "cancelled")
|
|
24113
|
+
return `Run ${runId} was cancelled.`;
|
|
24114
|
+
const context = snapshot.contextId ? ` --context ${snapshot.contextId}` : "";
|
|
24115
|
+
return `Run ${runId} is paused waiting for input. Resume it: sim workflows runs resume ${runId} --workflow ${workflowId}${context}`;
|
|
24116
|
+
}
|
|
24117
|
+
function runSpec() {
|
|
24118
|
+
return CLI_CONTRACT.getWorkflowRun ?? {};
|
|
24119
|
+
}
|
|
24120
|
+
function waitTimedOut(wait) {
|
|
24121
|
+
return (snapshot) => `Timed out after ${wait.timeoutSeconds}s waiting for run ${wait.runId} (status: ${snapshot.status}${snapshot.resumeAt ? `, resuming at ${snapshot.resumeAt}` : ""}). Raise ${WAIT_TIMEOUT_FLAG}, or set it to 0 to wait indefinitely.`;
|
|
24122
|
+
}
|
|
24123
|
+
async function waitForRun({ client, profile }, wait) {
|
|
24124
|
+
const params = { workflowId: wait.workflowId, runId: wait.runId };
|
|
24125
|
+
const progress = waitProgress();
|
|
24126
|
+
let delayMs = FIRST_POLL_DELAY_MS;
|
|
24127
|
+
try {
|
|
24128
|
+
while (true) {
|
|
24129
|
+
const raw = await client.request("getWorkflowRun", { params });
|
|
24130
|
+
const snapshot = readRun(raw);
|
|
24131
|
+
const outcome = classify(snapshot);
|
|
24132
|
+
if (outcome) {
|
|
24133
|
+
progress.finish();
|
|
24134
|
+
renderResult("getWorkflowRun", profile.output, runData(raw), runSpec());
|
|
24135
|
+
const message = explain(outcome, wait.runId, wait.workflowId, snapshot);
|
|
24136
|
+
if (message)
|
|
24137
|
+
printError(styles3().red(message));
|
|
24138
|
+
setSoftExitCode(WAIT_EXIT_CODES[outcome]);
|
|
24139
|
+
return;
|
|
24140
|
+
}
|
|
24141
|
+
const remainingMs = wait.deadline - Date.now();
|
|
24142
|
+
if (remainingMs <= 0) {
|
|
24143
|
+
progress.finish();
|
|
24144
|
+
renderResult("getWorkflowRun", profile.output, runData(raw), runSpec());
|
|
24145
|
+
printError(styles3().red(wait.timedOut(snapshot)));
|
|
24146
|
+
setSoftExitCode(WAIT_EXIT_CODES.timeout);
|
|
24147
|
+
return;
|
|
24148
|
+
}
|
|
24149
|
+
progress.advance(snapshot.status, Date.now() - wait.startedAt);
|
|
24150
|
+
await sleep(Math.min(delayMs, remainingMs));
|
|
24151
|
+
delayMs = Math.min(delayMs * POLL_BACKOFF_FACTOR, MAX_POLL_DELAY_MS);
|
|
24152
|
+
}
|
|
24153
|
+
} finally {
|
|
24154
|
+
progress.finish();
|
|
24155
|
+
}
|
|
24156
|
+
}
|
|
24157
|
+
function attachWorkflowRunWait(runs) {
|
|
24158
|
+
const [wait, connectWait] = apiCommand(runs, "wait", ["getWorkflowRun"]);
|
|
24159
|
+
wait.argument("<runId>", V2_OPERATIONS.getWorkflowRun.pathParamDocs?.runId).allowExcessArguments(false).description("Wait until a run finishes or needs input").addOption(new Option("--workflow <workflowId>", "Workflow ID (required)").makeOptionMandatory()).addOption(new Option(WAIT_TIMEOUT_FLAG, `Give up after this many seconds, or 0 to wait indefinitely (default: ${DEFAULT_WAIT_TIMEOUT_SECONDS}). Bounds the whole wait; SIM_TIMEOUT_SECONDS bounds one request`)).addHelpText("after", "Exit codes: 0 completed, 1 failed, 2 cancelled, 3 needs input, 4 timed out. Time-based pauses continue waiting; a human pause or an unspecified pause kind ends the wait with code 3. Use `sim workflows runs resume` with the pause context to continue.").action(async (runId, options) => {
|
|
24160
|
+
const timeoutSeconds = options.waitTimeout === undefined ? DEFAULT_WAIT_TIMEOUT_SECONDS : parseWaitTimeout(options.waitTimeout);
|
|
24161
|
+
const startedAt = Date.now();
|
|
24162
|
+
await waitForRun(connectWait(), {
|
|
24163
|
+
runId,
|
|
24164
|
+
workflowId: options.workflow,
|
|
24165
|
+
startedAt,
|
|
24166
|
+
deadline: timeoutSeconds === 0 ? Number.POSITIVE_INFINITY : startedAt + timeoutSeconds * 1000,
|
|
24167
|
+
timedOut: waitTimedOut({ runId, timeoutSeconds })
|
|
24168
|
+
});
|
|
24169
|
+
});
|
|
24170
|
+
}
|
|
24171
|
+
|
|
24035
24172
|
// src/commands/protocol/workflow-run-follow.ts
|
|
24036
24173
|
var AGENT_STREAM_PROTOCOL_HEADER = "x-sim-stream-protocol";
|
|
24037
24174
|
var AGENT_STREAM_PROTOCOL_V1 = "agent-events-v1";
|
|
@@ -24054,7 +24191,7 @@ function resolveWorkflowRunSelection(flags) {
|
|
|
24054
24191
|
throw new SimApiError("--source-run requires --from-block <blockId>", 0);
|
|
24055
24192
|
}
|
|
24056
24193
|
if ((manual || fromBlock) && flags.async === true) {
|
|
24057
|
-
throw new SimApiError(
|
|
24194
|
+
throw new SimApiError(`Manual execution does not support --async; pass ${WAIT_TIMEOUT_FLAG} to let a long run continue in the background`, 0);
|
|
24058
24195
|
}
|
|
24059
24196
|
if (useMockPayload && flags.input !== undefined) {
|
|
24060
24197
|
throw new SimApiError("--mock-payload cannot be combined with --input", 0);
|
|
@@ -24088,6 +24225,12 @@ function stringField(frame, key) {
|
|
|
24088
24225
|
const value = frame[key];
|
|
24089
24226
|
return typeof value === "string" ? value : null;
|
|
24090
24227
|
}
|
|
24228
|
+
function detachedRunId(receipt, status) {
|
|
24229
|
+
if (!isRecordLike(receipt) || typeof receipt.runId !== "string" || receipt.runId === "") {
|
|
24230
|
+
throw new SimApiError("Workflow run continued in the background without naming its run", status);
|
|
24231
|
+
}
|
|
24232
|
+
return receipt.runId;
|
|
24233
|
+
}
|
|
24091
24234
|
async function readWorkflowResult(response) {
|
|
24092
24235
|
const contentType = (response.headers.get("content-type") ?? "").toLowerCase();
|
|
24093
24236
|
if (!contentType.includes(WORKFLOW_RESULT_STREAM_CONTENT_TYPE)) {
|
|
@@ -24100,7 +24243,10 @@ async function readWorkflowResult(response) {
|
|
|
24100
24243
|
if (!isRecordLike(envelope)) {
|
|
24101
24244
|
throw new SimApiError("Workflow run returned an invalid result envelope", response.status);
|
|
24102
24245
|
}
|
|
24103
|
-
|
|
24246
|
+
const data = isRecordLike(envelope.data) ? envelope.data : envelope;
|
|
24247
|
+
if (response.status === 202)
|
|
24248
|
+
return { detachedRunId: detachedRunId(data, response.status) };
|
|
24249
|
+
return { settled: data };
|
|
24104
24250
|
}
|
|
24105
24251
|
for await (const value of readNdjson(response.body, "Workflow result stream")) {
|
|
24106
24252
|
if (!isRecordLike(value) || typeof value.type !== "string") {
|
|
@@ -24112,7 +24258,10 @@ async function readWorkflowResult(response) {
|
|
|
24112
24258
|
throw new SimApiError(safeOneLine(typeof value.error === "string" ? value.error : "Workflow run failed"), typeof value.status === "number" ? value.status : 0, typeof value.code === "string" ? value.code : null);
|
|
24113
24259
|
}
|
|
24114
24260
|
if (value.type === "final" && isRecordLike(value.data))
|
|
24115
|
-
return value.data;
|
|
24261
|
+
return { settled: value.data };
|
|
24262
|
+
if (value.type === "detached") {
|
|
24263
|
+
return { detachedRunId: detachedRunId(value.data, response.status) };
|
|
24264
|
+
}
|
|
24116
24265
|
throw new SimApiError("Workflow result stream returned an unknown event", response.status);
|
|
24117
24266
|
}
|
|
24118
24267
|
throw new SimApiError("Workflow result stream ended without a final result", response.status);
|
|
@@ -24122,20 +24271,37 @@ function withEmbeddedFileReferences(body) {
|
|
|
24122
24271
|
return body;
|
|
24123
24272
|
return { ...body, includeFileBase64: false };
|
|
24124
24273
|
}
|
|
24125
|
-
|
|
24274
|
+
function stillRunning(runId, workflowId, waitedSeconds) {
|
|
24275
|
+
return (snapshot) => `Run ${runId} is still ${snapshot.status} after ${waitedSeconds}s and continues in the background. Keep waiting: sim workflows runs wait ${runId} --workflow ${workflowId} --wait-timeout ${waitedSeconds}. Stop it: sim workflows runs cancel ${runId} --workflow ${workflowId}`;
|
|
24276
|
+
}
|
|
24277
|
+
async function runWithResultStream(workflowId, command, waitTimeoutSeconds) {
|
|
24126
24278
|
const flags = command.optsWithGlobals();
|
|
24127
|
-
const
|
|
24279
|
+
const connection = connect(command, ["executeWorkflow", "getWorkflowRun"]);
|
|
24280
|
+
const { client, profile } = connection;
|
|
24128
24281
|
const operation = V2_OPERATIONS.executeWorkflow;
|
|
24129
24282
|
const commandSpec = CLI_CONTRACT.executeWorkflow ?? {};
|
|
24130
24283
|
try {
|
|
24131
24284
|
const request = await buildRequest("executeWorkflow", [workflowId], flags, profile.workspaceId);
|
|
24285
|
+
const body = withEmbeddedFileReferences(request.body);
|
|
24132
24286
|
const response = await client.requestRaw("executeWorkflow", {
|
|
24133
24287
|
params: request.params,
|
|
24134
24288
|
query: request.query,
|
|
24135
|
-
body:
|
|
24289
|
+
body: waitTimeoutSeconds === undefined ? body : { ...body, waitTimeoutSeconds },
|
|
24136
24290
|
headers: { ...request.headers, accept: WORKFLOW_RESULT_STREAM_CONTENT_TYPE }
|
|
24137
24291
|
});
|
|
24138
|
-
const
|
|
24292
|
+
const result = await readWorkflowResult(response);
|
|
24293
|
+
if ("detachedRunId" in result) {
|
|
24294
|
+
const now = Date.now();
|
|
24295
|
+
await waitForRun(connection, {
|
|
24296
|
+
runId: result.detachedRunId,
|
|
24297
|
+
workflowId,
|
|
24298
|
+
startedAt: now,
|
|
24299
|
+
deadline: now,
|
|
24300
|
+
timedOut: stillRunning(result.detachedRunId, workflowId, waitTimeoutSeconds ?? 0)
|
|
24301
|
+
});
|
|
24302
|
+
return;
|
|
24303
|
+
}
|
|
24304
|
+
const payload = result.settled;
|
|
24139
24305
|
renderResult("executeWorkflow", profile.output, payload, commandSpec, {
|
|
24140
24306
|
expandedTrace: flags.trace === true
|
|
24141
24307
|
});
|
|
@@ -24306,6 +24472,13 @@ async function followRun(workflowId, command) {
|
|
|
24306
24472
|
function followOrDelegate(previous) {
|
|
24307
24473
|
return async (workflowId, _options, command) => {
|
|
24308
24474
|
const initialFlags = command.optsWithGlobals();
|
|
24475
|
+
const waitTimeoutSeconds = typeof initialFlags.waitTimeout === "string" ? Math.ceil(parseWaitTimeout(initialFlags.waitTimeout)) : undefined;
|
|
24476
|
+
if (waitTimeoutSeconds !== undefined && initialFlags.async === true) {
|
|
24477
|
+
throw new SimApiError("--wait-timeout bounds how long a synchronous run is waited for, and --async never waits; pass one, not both", 0);
|
|
24478
|
+
}
|
|
24479
|
+
if (waitTimeoutSeconds !== undefined && initialFlags.follow === true) {
|
|
24480
|
+
throw new SimApiError("--wait-timeout cannot be combined with --follow, which streams the run until it ends", 0);
|
|
24481
|
+
}
|
|
24309
24482
|
const selection = resolveWorkflowRunSelection(initialFlags);
|
|
24310
24483
|
if (selection)
|
|
24311
24484
|
command.setOptionValue("run", selection);
|
|
@@ -24318,7 +24491,7 @@ function followOrDelegate(previous) {
|
|
|
24318
24491
|
throw new SimApiError("--include-thinking and --include-tool-calls describe a stream; add --follow", 0);
|
|
24319
24492
|
}
|
|
24320
24493
|
if (flags.async !== true) {
|
|
24321
|
-
await runWithResultStream(workflowId, command);
|
|
24494
|
+
await runWithResultStream(workflowId, command, waitTimeoutSeconds);
|
|
24322
24495
|
return;
|
|
24323
24496
|
}
|
|
24324
24497
|
if (previous) {
|
|
@@ -24338,7 +24511,8 @@ function attachWorkflowRunFollow(workflows) {
|
|
|
24338
24511
|
}
|
|
24339
24512
|
const held = run._actionHandler;
|
|
24340
24513
|
const previous = typeof held === "function" ? held : null;
|
|
24341
|
-
|
|
24514
|
+
callsOperations(run, ["executeWorkflow", "getWorkflowRun"], { runs: "executeWorkflow" });
|
|
24515
|
+
run.option("--manual", "Run the current saved workflow state instead of the active deployment").option("--trigger <blockId>", "Enter the run through this runnable trigger; runs the current saved workflow state (implies --manual)").option("--mock-payload", "Use the selected trigger's server-derived mock payload; runs the current saved workflow state (implies --manual)").option("--from-block <blockId>", "Run manually from this saved workflow block").option("--source-run <runId>", "Prior run whose persisted state supplies upstream outputs (requires --from-block)").option("--stop-after <blockId>", "Stop the run after this saved block, failing it if the run takes a path that skips the block; with --from-block on the same block, re-runs only that block (implies --manual)").option(WAIT_TIMEOUT_FLAG, "Wait at most this many seconds for a synchronous run (0 waits until it settles). A run still going then continues in the background under async limits: the command prints its run record and exits 4, and `sim workflows runs wait` keeps waiting. Not with --async or --follow").option("--follow", "Stream the run as it happens; progress on stderr, result on stdout. The stream reports only success and output, so the result omits the run id and timings a non-streaming run returns").option("--include-thinking", "Show model reasoning while following (requires --follow)").option("--include-tool-calls", "Show tool calls while following (requires --follow)").action(followOrDelegate(previous));
|
|
24342
24516
|
}
|
|
24343
24517
|
|
|
24344
24518
|
// src/commands/protocol/workflow-run-get.ts
|
|
@@ -24447,125 +24621,6 @@ function attachWorkflowRunGet(runs) {
|
|
|
24447
24621
|
});
|
|
24448
24622
|
}
|
|
24449
24623
|
|
|
24450
|
-
// src/commands/protocol/workflow-run-wait.ts
|
|
24451
|
-
var TERMINAL_STATUSES = new Set(["completed", "failed", "cancelled"]);
|
|
24452
|
-
var WAIT_EXIT_CODES = {
|
|
24453
|
-
completed: 0,
|
|
24454
|
-
failed: 1,
|
|
24455
|
-
cancelled: 2,
|
|
24456
|
-
paused: 3,
|
|
24457
|
-
timeout: 4
|
|
24458
|
-
};
|
|
24459
|
-
var FIRST_POLL_DELAY_MS = 2000;
|
|
24460
|
-
var MAX_POLL_DELAY_MS = 15000;
|
|
24461
|
-
var POLL_BACKOFF_FACTOR = 2;
|
|
24462
|
-
var DEFAULT_WAIT_TIMEOUT_SECONDS = 3600;
|
|
24463
|
-
var WAIT_TIMEOUT_FLAG = "--wait-timeout <seconds>";
|
|
24464
|
-
function optionalString(value) {
|
|
24465
|
-
return typeof value === "string" && value !== "" ? value : null;
|
|
24466
|
-
}
|
|
24467
|
-
function runData(raw) {
|
|
24468
|
-
return isRecordLike(raw) && isRecordLike(raw.data) ? raw.data : raw;
|
|
24469
|
-
}
|
|
24470
|
-
function readRun(raw) {
|
|
24471
|
-
const run = isRecordLike(raw) && isRecordLike(raw.data) ? raw.data : raw;
|
|
24472
|
-
if (!isRecordLike(run) || typeof run.status !== "string") {
|
|
24473
|
-
throw new SimApiError("Run status response carried no status.", 0);
|
|
24474
|
-
}
|
|
24475
|
-
const paused = toRecordOrNull(run.paused);
|
|
24476
|
-
return {
|
|
24477
|
-
status: run.status,
|
|
24478
|
-
pauseKind: paused ? optionalString(paused.pauseKind) : null,
|
|
24479
|
-
resumeAt: paused ? optionalString(paused.resumeAt) : null,
|
|
24480
|
-
contextId: paused ? optionalString(paused.contextId) : null
|
|
24481
|
-
};
|
|
24482
|
-
}
|
|
24483
|
-
function classify(snapshot) {
|
|
24484
|
-
if (snapshot.status === "paused")
|
|
24485
|
-
return snapshot.pauseKind === "time" ? null : "paused";
|
|
24486
|
-
if (!TERMINAL_STATUSES.has(snapshot.status))
|
|
24487
|
-
return null;
|
|
24488
|
-
return snapshot.status === "completed" ? "completed" : snapshot.status === "cancelled" ? "cancelled" : "failed";
|
|
24489
|
-
}
|
|
24490
|
-
function waitProgress() {
|
|
24491
|
-
let reported = false;
|
|
24492
|
-
return {
|
|
24493
|
-
advance: (status, elapsedMs) => {
|
|
24494
|
-
if (!hasProgressTerminal())
|
|
24495
|
-
return;
|
|
24496
|
-
reported = true;
|
|
24497
|
-
writeStderr(`\r${styles3().dim(`${status} — waiting ${Math.round(elapsedMs / 1000)}s…`)}\x1B[K`);
|
|
24498
|
-
},
|
|
24499
|
-
finish: () => {
|
|
24500
|
-
if (!reported)
|
|
24501
|
-
return;
|
|
24502
|
-
reported = false;
|
|
24503
|
-
writeStderr("\r\x1B[K");
|
|
24504
|
-
}
|
|
24505
|
-
};
|
|
24506
|
-
}
|
|
24507
|
-
function parseWaitTimeout(raw) {
|
|
24508
|
-
const seconds = Number(raw);
|
|
24509
|
-
if (!Number.isFinite(seconds) || seconds < 0) {
|
|
24510
|
-
throw new SimApiError(`Invalid ${WAIT_TIMEOUT_FLAG} "${raw}". Use a non-negative number of seconds, or 0 to wait indefinitely.`, 0);
|
|
24511
|
-
}
|
|
24512
|
-
return seconds;
|
|
24513
|
-
}
|
|
24514
|
-
function explain(outcome, runId, workflowId, snapshot) {
|
|
24515
|
-
if (outcome === "completed")
|
|
24516
|
-
return null;
|
|
24517
|
-
if (outcome === "failed")
|
|
24518
|
-
return `Run ${runId} failed.`;
|
|
24519
|
-
if (outcome === "cancelled")
|
|
24520
|
-
return `Run ${runId} was cancelled.`;
|
|
24521
|
-
const context = snapshot.contextId ? ` --context ${snapshot.contextId}` : "";
|
|
24522
|
-
return `Run ${runId} is paused waiting for input. Resume it: sim workflows runs resume ${runId} --workflow ${workflowId}${context}`;
|
|
24523
|
-
}
|
|
24524
|
-
function runSpec() {
|
|
24525
|
-
return CLI_CONTRACT.getWorkflowRun ?? {};
|
|
24526
|
-
}
|
|
24527
|
-
function attachWorkflowRunWait(runs) {
|
|
24528
|
-
const [wait, connectWait] = apiCommand(runs, "wait", ["getWorkflowRun"]);
|
|
24529
|
-
wait.argument("<runId>", V2_OPERATIONS.getWorkflowRun.pathParamDocs?.runId).allowExcessArguments(false).description("Wait until a run finishes or needs input").addOption(new Option("--workflow <workflowId>", "Workflow ID (required)").makeOptionMandatory()).addOption(new Option(WAIT_TIMEOUT_FLAG, `Give up after this many seconds, or 0 to wait indefinitely (default: ${DEFAULT_WAIT_TIMEOUT_SECONDS}). Bounds the whole wait; SIM_TIMEOUT_SECONDS bounds one request`)).addHelpText("after", "Exit codes: 0 completed, 1 failed, 2 cancelled, 3 needs input, 4 timed out. Time-based pauses continue waiting; a human pause or an unspecified pause kind ends the wait with code 3. Use `sim workflows runs resume` with the pause context to continue.").action(async (runId, options) => {
|
|
24530
|
-
const timeoutSeconds = options.waitTimeout === undefined ? DEFAULT_WAIT_TIMEOUT_SECONDS : parseWaitTimeout(options.waitTimeout);
|
|
24531
|
-
const { client, profile } = connectWait();
|
|
24532
|
-
const params = { workflowId: options.workflow, runId };
|
|
24533
|
-
const startedAt = Date.now();
|
|
24534
|
-
const deadline = timeoutSeconds === 0 ? Number.POSITIVE_INFINITY : startedAt + timeoutSeconds * 1000;
|
|
24535
|
-
const progress = waitProgress();
|
|
24536
|
-
let delayMs = FIRST_POLL_DELAY_MS;
|
|
24537
|
-
try {
|
|
24538
|
-
while (true) {
|
|
24539
|
-
const raw = await client.request("getWorkflowRun", { params });
|
|
24540
|
-
const snapshot = readRun(raw);
|
|
24541
|
-
const outcome = classify(snapshot);
|
|
24542
|
-
if (outcome) {
|
|
24543
|
-
progress.finish();
|
|
24544
|
-
renderResult("getWorkflowRun", profile.output, runData(raw), runSpec());
|
|
24545
|
-
const message = explain(outcome, runId, options.workflow, snapshot);
|
|
24546
|
-
if (message)
|
|
24547
|
-
printError(styles3().red(message));
|
|
24548
|
-
setSoftExitCode(WAIT_EXIT_CODES[outcome]);
|
|
24549
|
-
return;
|
|
24550
|
-
}
|
|
24551
|
-
const remainingMs = deadline - Date.now();
|
|
24552
|
-
if (remainingMs <= 0) {
|
|
24553
|
-
progress.finish();
|
|
24554
|
-
renderResult("getWorkflowRun", profile.output, runData(raw), runSpec());
|
|
24555
|
-
printError(styles3().red(`Timed out after ${timeoutSeconds}s waiting for run ${runId} (status: ${snapshot.status}${snapshot.resumeAt ? `, resuming at ${snapshot.resumeAt}` : ""}). Raise ${WAIT_TIMEOUT_FLAG}, or set it to 0 to wait indefinitely.`));
|
|
24556
|
-
setSoftExitCode(WAIT_EXIT_CODES.timeout);
|
|
24557
|
-
return;
|
|
24558
|
-
}
|
|
24559
|
-
progress.advance(snapshot.status, Date.now() - startedAt);
|
|
24560
|
-
await sleep(Math.min(delayMs, remainingMs));
|
|
24561
|
-
delayMs = Math.min(delayMs * POLL_BACKOFF_FACTOR, MAX_POLL_DELAY_MS);
|
|
24562
|
-
}
|
|
24563
|
-
} finally {
|
|
24564
|
-
progress.finish();
|
|
24565
|
-
}
|
|
24566
|
-
});
|
|
24567
|
-
}
|
|
24568
|
-
|
|
24569
24624
|
// src/commands/protocol/index.ts
|
|
24570
24625
|
function group(program, name) {
|
|
24571
24626
|
const existing = program.commands.find((command) => command.name() === name);
|
package/dist/runtime.js
CHANGED
|
@@ -12002,7 +12002,7 @@ var V2_OPERATIONS = {
|
|
|
12002
12002
|
},
|
|
12003
12003
|
run: {
|
|
12004
12004
|
kind: "unknown",
|
|
12005
|
-
describe: "Workflow state and entry point to execute. Omit for the active deployment. Manual execution requires OAuth or personal-key write access and supports synchronous or streamed runs only."
|
|
12005
|
+
describe: "Workflow state and entry point to execute. Omit for the active deployment. Manual execution requires OAuth or personal-key write access and supports synchronous or streamed runs only; `waitTimeoutSeconds` lets a synchronous one continue in the background."
|
|
12006
12006
|
},
|
|
12007
12007
|
async: {
|
|
12008
12008
|
kind: "boolean",
|
|
@@ -12011,7 +12011,11 @@ var V2_OPERATIONS = {
|
|
|
12011
12011
|
},
|
|
12012
12012
|
executionTimeoutSeconds: {
|
|
12013
12013
|
kind: "integer",
|
|
12014
|
-
describe: "Maximum duration of an asynchronous run, in seconds, capped by the plan's execution timeout. Requires `async: true`; otherwise returns `400`."
|
|
12014
|
+
describe: "Maximum duration of an asynchronous run, in seconds, capped by the plan's execution timeout. Requires `async: true` or `waitTimeoutSeconds`; otherwise returns `400`."
|
|
12015
|
+
},
|
|
12016
|
+
waitTimeoutSeconds: {
|
|
12017
|
+
kind: "integer",
|
|
12018
|
+
describe: "Wait at most this many seconds for a synchronous run; `0` waits until it settles. A run still going then continues in the background and the response carries its run receipt instead of its result: `202` on a JSON response, or a closing `detached` frame on an `application/x-ndjson` result stream, which keeps its `200`. The receipt is sent only once the run can be read on the runs resource, where it can also be cancelled. The run is admitted under asynchronous limits, and a dropped connection no longer cancels it. Requires an API key or OAuth access token. Cannot be combined with `async` or `stream`."
|
|
12015
12019
|
},
|
|
12016
12020
|
stream: {
|
|
12017
12021
|
kind: "boolean",
|
|
@@ -20710,6 +20714,9 @@ var CLI_CONTRACT = {
|
|
|
20710
20714
|
describe: "Run a deployed workflow or execute saved state manually",
|
|
20711
20715
|
flags: {
|
|
20712
20716
|
async: { boolean: true, describe: "Queue the run and return immediately" },
|
|
20717
|
+
executionTimeoutSeconds: {
|
|
20718
|
+
describe: "Maximum duration of the run in seconds, capped by the plan's execution timeout. Requires --async or --wait-timeout"
|
|
20719
|
+
},
|
|
20713
20720
|
input: { json: true, describe: "Trigger input as JSON" },
|
|
20714
20721
|
run: {
|
|
20715
20722
|
hidden: true,
|
|
@@ -20723,6 +20730,7 @@ var CLI_CONTRACT = {
|
|
|
20723
20730
|
stream: { omit: true },
|
|
20724
20731
|
includeThinking: { omit: true },
|
|
20725
20732
|
includeToolCalls: { omit: true },
|
|
20733
|
+
waitTimeoutSeconds: { omit: true },
|
|
20726
20734
|
includeFileBase64: {
|
|
20727
20735
|
describe: "Inline eligible output files as base64 content. Defaults to true for the installed CLI; Chat defaults to file references and admits requested bytes only for synchronous runs. Rejected when async is true."
|
|
20728
20736
|
},
|
|
@@ -24081,6 +24089,135 @@ function attachTableImport(tables) {
|
|
|
24081
24089
|
});
|
|
24082
24090
|
}
|
|
24083
24091
|
|
|
24092
|
+
// src/commands/protocol/workflow-run-wait.ts
|
|
24093
|
+
var TERMINAL_STATUSES = new Set(["completed", "failed", "cancelled"]);
|
|
24094
|
+
var WAIT_EXIT_CODES = {
|
|
24095
|
+
completed: 0,
|
|
24096
|
+
failed: 1,
|
|
24097
|
+
cancelled: 2,
|
|
24098
|
+
paused: 3,
|
|
24099
|
+
timeout: 4
|
|
24100
|
+
};
|
|
24101
|
+
var FIRST_POLL_DELAY_MS = 2000;
|
|
24102
|
+
var MAX_POLL_DELAY_MS = 15000;
|
|
24103
|
+
var POLL_BACKOFF_FACTOR = 2;
|
|
24104
|
+
var DEFAULT_WAIT_TIMEOUT_SECONDS = 3600;
|
|
24105
|
+
var WAIT_TIMEOUT_FLAG = "--wait-timeout <seconds>";
|
|
24106
|
+
function optionalString(value) {
|
|
24107
|
+
return typeof value === "string" && value !== "" ? value : null;
|
|
24108
|
+
}
|
|
24109
|
+
function runData(raw) {
|
|
24110
|
+
return isRecordLike(raw) && isRecordLike(raw.data) ? raw.data : raw;
|
|
24111
|
+
}
|
|
24112
|
+
function readRun(raw) {
|
|
24113
|
+
const run = isRecordLike(raw) && isRecordLike(raw.data) ? raw.data : raw;
|
|
24114
|
+
if (!isRecordLike(run) || typeof run.status !== "string") {
|
|
24115
|
+
throw new SimApiError("Run status response carried no status.", 0);
|
|
24116
|
+
}
|
|
24117
|
+
const paused = toRecordOrNull(run.paused);
|
|
24118
|
+
return {
|
|
24119
|
+
status: run.status,
|
|
24120
|
+
pauseKind: paused ? optionalString(paused.pauseKind) : null,
|
|
24121
|
+
resumeAt: paused ? optionalString(paused.resumeAt) : null,
|
|
24122
|
+
contextId: paused ? optionalString(paused.contextId) : null
|
|
24123
|
+
};
|
|
24124
|
+
}
|
|
24125
|
+
function classify(snapshot) {
|
|
24126
|
+
if (snapshot.status === "paused")
|
|
24127
|
+
return snapshot.pauseKind === "time" ? null : "paused";
|
|
24128
|
+
if (!TERMINAL_STATUSES.has(snapshot.status))
|
|
24129
|
+
return null;
|
|
24130
|
+
return snapshot.status === "completed" ? "completed" : snapshot.status === "cancelled" ? "cancelled" : "failed";
|
|
24131
|
+
}
|
|
24132
|
+
function waitProgress() {
|
|
24133
|
+
let reported = false;
|
|
24134
|
+
return {
|
|
24135
|
+
advance: (status, elapsedMs) => {
|
|
24136
|
+
if (!hasProgressTerminal())
|
|
24137
|
+
return;
|
|
24138
|
+
reported = true;
|
|
24139
|
+
writeStderr(`\r${styles3().dim(`${status} — waiting ${Math.round(elapsedMs / 1000)}s…`)}\x1B[K`);
|
|
24140
|
+
},
|
|
24141
|
+
finish: () => {
|
|
24142
|
+
if (!reported)
|
|
24143
|
+
return;
|
|
24144
|
+
reported = false;
|
|
24145
|
+
writeStderr("\r\x1B[K");
|
|
24146
|
+
}
|
|
24147
|
+
};
|
|
24148
|
+
}
|
|
24149
|
+
function parseWaitTimeout(raw) {
|
|
24150
|
+
const seconds = Number(raw);
|
|
24151
|
+
if (!Number.isFinite(seconds) || seconds < 0) {
|
|
24152
|
+
throw new SimApiError(`Invalid ${WAIT_TIMEOUT_FLAG} "${raw}". Use a non-negative number of seconds, or 0 to wait indefinitely.`, 0);
|
|
24153
|
+
}
|
|
24154
|
+
return seconds;
|
|
24155
|
+
}
|
|
24156
|
+
function explain(outcome, runId, workflowId, snapshot) {
|
|
24157
|
+
if (outcome === "completed")
|
|
24158
|
+
return null;
|
|
24159
|
+
if (outcome === "failed")
|
|
24160
|
+
return `Run ${runId} failed.`;
|
|
24161
|
+
if (outcome === "cancelled")
|
|
24162
|
+
return `Run ${runId} was cancelled.`;
|
|
24163
|
+
const context = snapshot.contextId ? ` --context ${snapshot.contextId}` : "";
|
|
24164
|
+
return `Run ${runId} is paused waiting for input. Resume it: sim workflows runs resume ${runId} --workflow ${workflowId}${context}`;
|
|
24165
|
+
}
|
|
24166
|
+
function runSpec() {
|
|
24167
|
+
return CLI_CONTRACT.getWorkflowRun ?? {};
|
|
24168
|
+
}
|
|
24169
|
+
function waitTimedOut(wait) {
|
|
24170
|
+
return (snapshot) => `Timed out after ${wait.timeoutSeconds}s waiting for run ${wait.runId} (status: ${snapshot.status}${snapshot.resumeAt ? `, resuming at ${snapshot.resumeAt}` : ""}). Raise ${WAIT_TIMEOUT_FLAG}, or set it to 0 to wait indefinitely.`;
|
|
24171
|
+
}
|
|
24172
|
+
async function waitForRun({ client, profile }, wait) {
|
|
24173
|
+
const params = { workflowId: wait.workflowId, runId: wait.runId };
|
|
24174
|
+
const progress = waitProgress();
|
|
24175
|
+
let delayMs = FIRST_POLL_DELAY_MS;
|
|
24176
|
+
try {
|
|
24177
|
+
while (true) {
|
|
24178
|
+
const raw = await client.request("getWorkflowRun", { params });
|
|
24179
|
+
const snapshot = readRun(raw);
|
|
24180
|
+
const outcome = classify(snapshot);
|
|
24181
|
+
if (outcome) {
|
|
24182
|
+
progress.finish();
|
|
24183
|
+
renderResult("getWorkflowRun", profile.output, runData(raw), runSpec());
|
|
24184
|
+
const message = explain(outcome, wait.runId, wait.workflowId, snapshot);
|
|
24185
|
+
if (message)
|
|
24186
|
+
printError(styles3().red(message));
|
|
24187
|
+
setSoftExitCode(WAIT_EXIT_CODES[outcome]);
|
|
24188
|
+
return;
|
|
24189
|
+
}
|
|
24190
|
+
const remainingMs = wait.deadline - Date.now();
|
|
24191
|
+
if (remainingMs <= 0) {
|
|
24192
|
+
progress.finish();
|
|
24193
|
+
renderResult("getWorkflowRun", profile.output, runData(raw), runSpec());
|
|
24194
|
+
printError(styles3().red(wait.timedOut(snapshot)));
|
|
24195
|
+
setSoftExitCode(WAIT_EXIT_CODES.timeout);
|
|
24196
|
+
return;
|
|
24197
|
+
}
|
|
24198
|
+
progress.advance(snapshot.status, Date.now() - wait.startedAt);
|
|
24199
|
+
await sleep(Math.min(delayMs, remainingMs));
|
|
24200
|
+
delayMs = Math.min(delayMs * POLL_BACKOFF_FACTOR, MAX_POLL_DELAY_MS);
|
|
24201
|
+
}
|
|
24202
|
+
} finally {
|
|
24203
|
+
progress.finish();
|
|
24204
|
+
}
|
|
24205
|
+
}
|
|
24206
|
+
function attachWorkflowRunWait(runs) {
|
|
24207
|
+
const [wait, connectWait] = apiCommand(runs, "wait", ["getWorkflowRun"]);
|
|
24208
|
+
wait.argument("<runId>", V2_OPERATIONS.getWorkflowRun.pathParamDocs?.runId).allowExcessArguments(false).description("Wait until a run finishes or needs input").addOption(new Option("--workflow <workflowId>", "Workflow ID (required)").makeOptionMandatory()).addOption(new Option(WAIT_TIMEOUT_FLAG, `Give up after this many seconds, or 0 to wait indefinitely (default: ${DEFAULT_WAIT_TIMEOUT_SECONDS}). Bounds the whole wait; SIM_TIMEOUT_SECONDS bounds one request`)).addHelpText("after", "Exit codes: 0 completed, 1 failed, 2 cancelled, 3 needs input, 4 timed out. Time-based pauses continue waiting; a human pause or an unspecified pause kind ends the wait with code 3. Use `sim workflows runs resume` with the pause context to continue.").action(async (runId, options) => {
|
|
24209
|
+
const timeoutSeconds = options.waitTimeout === undefined ? DEFAULT_WAIT_TIMEOUT_SECONDS : parseWaitTimeout(options.waitTimeout);
|
|
24210
|
+
const startedAt = Date.now();
|
|
24211
|
+
await waitForRun(connectWait(), {
|
|
24212
|
+
runId,
|
|
24213
|
+
workflowId: options.workflow,
|
|
24214
|
+
startedAt,
|
|
24215
|
+
deadline: timeoutSeconds === 0 ? Number.POSITIVE_INFINITY : startedAt + timeoutSeconds * 1000,
|
|
24216
|
+
timedOut: waitTimedOut({ runId, timeoutSeconds })
|
|
24217
|
+
});
|
|
24218
|
+
});
|
|
24219
|
+
}
|
|
24220
|
+
|
|
24084
24221
|
// src/commands/protocol/workflow-run-follow.ts
|
|
24085
24222
|
var AGENT_STREAM_PROTOCOL_HEADER = "x-sim-stream-protocol";
|
|
24086
24223
|
var AGENT_STREAM_PROTOCOL_V1 = "agent-events-v1";
|
|
@@ -24103,7 +24240,7 @@ function resolveWorkflowRunSelection(flags) {
|
|
|
24103
24240
|
throw new SimApiError("--source-run requires --from-block <blockId>", 0);
|
|
24104
24241
|
}
|
|
24105
24242
|
if ((manual || fromBlock) && flags.async === true) {
|
|
24106
|
-
throw new SimApiError(
|
|
24243
|
+
throw new SimApiError(`Manual execution does not support --async; pass ${WAIT_TIMEOUT_FLAG} to let a long run continue in the background`, 0);
|
|
24107
24244
|
}
|
|
24108
24245
|
if (useMockPayload && flags.input !== undefined) {
|
|
24109
24246
|
throw new SimApiError("--mock-payload cannot be combined with --input", 0);
|
|
@@ -24137,6 +24274,12 @@ function stringField(frame, key) {
|
|
|
24137
24274
|
const value = frame[key];
|
|
24138
24275
|
return typeof value === "string" ? value : null;
|
|
24139
24276
|
}
|
|
24277
|
+
function detachedRunId(receipt, status) {
|
|
24278
|
+
if (!isRecordLike(receipt) || typeof receipt.runId !== "string" || receipt.runId === "") {
|
|
24279
|
+
throw new SimApiError("Workflow run continued in the background without naming its run", status);
|
|
24280
|
+
}
|
|
24281
|
+
return receipt.runId;
|
|
24282
|
+
}
|
|
24140
24283
|
async function readWorkflowResult(response) {
|
|
24141
24284
|
const contentType = (response.headers.get("content-type") ?? "").toLowerCase();
|
|
24142
24285
|
if (!contentType.includes(WORKFLOW_RESULT_STREAM_CONTENT_TYPE)) {
|
|
@@ -24149,7 +24292,10 @@ async function readWorkflowResult(response) {
|
|
|
24149
24292
|
if (!isRecordLike(envelope)) {
|
|
24150
24293
|
throw new SimApiError("Workflow run returned an invalid result envelope", response.status);
|
|
24151
24294
|
}
|
|
24152
|
-
|
|
24295
|
+
const data = isRecordLike(envelope.data) ? envelope.data : envelope;
|
|
24296
|
+
if (response.status === 202)
|
|
24297
|
+
return { detachedRunId: detachedRunId(data, response.status) };
|
|
24298
|
+
return { settled: data };
|
|
24153
24299
|
}
|
|
24154
24300
|
for await (const value of readNdjson(response.body, "Workflow result stream")) {
|
|
24155
24301
|
if (!isRecordLike(value) || typeof value.type !== "string") {
|
|
@@ -24161,7 +24307,10 @@ async function readWorkflowResult(response) {
|
|
|
24161
24307
|
throw new SimApiError(safeOneLine(typeof value.error === "string" ? value.error : "Workflow run failed"), typeof value.status === "number" ? value.status : 0, typeof value.code === "string" ? value.code : null);
|
|
24162
24308
|
}
|
|
24163
24309
|
if (value.type === "final" && isRecordLike(value.data))
|
|
24164
|
-
return value.data;
|
|
24310
|
+
return { settled: value.data };
|
|
24311
|
+
if (value.type === "detached") {
|
|
24312
|
+
return { detachedRunId: detachedRunId(value.data, response.status) };
|
|
24313
|
+
}
|
|
24165
24314
|
throw new SimApiError("Workflow result stream returned an unknown event", response.status);
|
|
24166
24315
|
}
|
|
24167
24316
|
throw new SimApiError("Workflow result stream ended without a final result", response.status);
|
|
@@ -24171,20 +24320,37 @@ function withEmbeddedFileReferences(body) {
|
|
|
24171
24320
|
return body;
|
|
24172
24321
|
return { ...body, includeFileBase64: false };
|
|
24173
24322
|
}
|
|
24174
|
-
|
|
24323
|
+
function stillRunning(runId, workflowId, waitedSeconds) {
|
|
24324
|
+
return (snapshot) => `Run ${runId} is still ${snapshot.status} after ${waitedSeconds}s and continues in the background. Keep waiting: sim workflows runs wait ${runId} --workflow ${workflowId} --wait-timeout ${waitedSeconds}. Stop it: sim workflows runs cancel ${runId} --workflow ${workflowId}`;
|
|
24325
|
+
}
|
|
24326
|
+
async function runWithResultStream(workflowId, command, waitTimeoutSeconds) {
|
|
24175
24327
|
const flags = command.optsWithGlobals();
|
|
24176
|
-
const
|
|
24328
|
+
const connection = connect(command, ["executeWorkflow", "getWorkflowRun"]);
|
|
24329
|
+
const { client, profile } = connection;
|
|
24177
24330
|
const operation = V2_OPERATIONS.executeWorkflow;
|
|
24178
24331
|
const commandSpec = CLI_CONTRACT.executeWorkflow ?? {};
|
|
24179
24332
|
try {
|
|
24180
24333
|
const request = await buildRequest("executeWorkflow", [workflowId], flags, profile.workspaceId);
|
|
24334
|
+
const body = withEmbeddedFileReferences(request.body);
|
|
24181
24335
|
const response = await client.requestRaw("executeWorkflow", {
|
|
24182
24336
|
params: request.params,
|
|
24183
24337
|
query: request.query,
|
|
24184
|
-
body:
|
|
24338
|
+
body: waitTimeoutSeconds === undefined ? body : { ...body, waitTimeoutSeconds },
|
|
24185
24339
|
headers: { ...request.headers, accept: WORKFLOW_RESULT_STREAM_CONTENT_TYPE }
|
|
24186
24340
|
});
|
|
24187
|
-
const
|
|
24341
|
+
const result = await readWorkflowResult(response);
|
|
24342
|
+
if ("detachedRunId" in result) {
|
|
24343
|
+
const now = Date.now();
|
|
24344
|
+
await waitForRun(connection, {
|
|
24345
|
+
runId: result.detachedRunId,
|
|
24346
|
+
workflowId,
|
|
24347
|
+
startedAt: now,
|
|
24348
|
+
deadline: now,
|
|
24349
|
+
timedOut: stillRunning(result.detachedRunId, workflowId, waitTimeoutSeconds ?? 0)
|
|
24350
|
+
});
|
|
24351
|
+
return;
|
|
24352
|
+
}
|
|
24353
|
+
const payload = result.settled;
|
|
24188
24354
|
renderResult("executeWorkflow", profile.output, payload, commandSpec, {
|
|
24189
24355
|
expandedTrace: flags.trace === true
|
|
24190
24356
|
});
|
|
@@ -24355,6 +24521,13 @@ async function followRun(workflowId, command) {
|
|
|
24355
24521
|
function followOrDelegate(previous) {
|
|
24356
24522
|
return async (workflowId, _options, command) => {
|
|
24357
24523
|
const initialFlags = command.optsWithGlobals();
|
|
24524
|
+
const waitTimeoutSeconds = typeof initialFlags.waitTimeout === "string" ? Math.ceil(parseWaitTimeout(initialFlags.waitTimeout)) : undefined;
|
|
24525
|
+
if (waitTimeoutSeconds !== undefined && initialFlags.async === true) {
|
|
24526
|
+
throw new SimApiError("--wait-timeout bounds how long a synchronous run is waited for, and --async never waits; pass one, not both", 0);
|
|
24527
|
+
}
|
|
24528
|
+
if (waitTimeoutSeconds !== undefined && initialFlags.follow === true) {
|
|
24529
|
+
throw new SimApiError("--wait-timeout cannot be combined with --follow, which streams the run until it ends", 0);
|
|
24530
|
+
}
|
|
24358
24531
|
const selection = resolveWorkflowRunSelection(initialFlags);
|
|
24359
24532
|
if (selection)
|
|
24360
24533
|
command.setOptionValue("run", selection);
|
|
@@ -24367,7 +24540,7 @@ function followOrDelegate(previous) {
|
|
|
24367
24540
|
throw new SimApiError("--include-thinking and --include-tool-calls describe a stream; add --follow", 0);
|
|
24368
24541
|
}
|
|
24369
24542
|
if (flags.async !== true) {
|
|
24370
|
-
await runWithResultStream(workflowId, command);
|
|
24543
|
+
await runWithResultStream(workflowId, command, waitTimeoutSeconds);
|
|
24371
24544
|
return;
|
|
24372
24545
|
}
|
|
24373
24546
|
if (previous) {
|
|
@@ -24387,7 +24560,8 @@ function attachWorkflowRunFollow(workflows) {
|
|
|
24387
24560
|
}
|
|
24388
24561
|
const held = run._actionHandler;
|
|
24389
24562
|
const previous = typeof held === "function" ? held : null;
|
|
24390
|
-
|
|
24563
|
+
callsOperations(run, ["executeWorkflow", "getWorkflowRun"], { runs: "executeWorkflow" });
|
|
24564
|
+
run.option("--manual", "Run the current saved workflow state instead of the active deployment").option("--trigger <blockId>", "Enter the run through this runnable trigger; runs the current saved workflow state (implies --manual)").option("--mock-payload", "Use the selected trigger's server-derived mock payload; runs the current saved workflow state (implies --manual)").option("--from-block <blockId>", "Run manually from this saved workflow block").option("--source-run <runId>", "Prior run whose persisted state supplies upstream outputs (requires --from-block)").option("--stop-after <blockId>", "Stop the run after this saved block, failing it if the run takes a path that skips the block; with --from-block on the same block, re-runs only that block (implies --manual)").option(WAIT_TIMEOUT_FLAG, "Wait at most this many seconds for a synchronous run (0 waits until it settles). A run still going then continues in the background under async limits: the command prints its run record and exits 4, and `sim workflows runs wait` keeps waiting. Not with --async or --follow").option("--follow", "Stream the run as it happens; progress on stderr, result on stdout. The stream reports only success and output, so the result omits the run id and timings a non-streaming run returns").option("--include-thinking", "Show model reasoning while following (requires --follow)").option("--include-tool-calls", "Show tool calls while following (requires --follow)").action(followOrDelegate(previous));
|
|
24391
24565
|
}
|
|
24392
24566
|
|
|
24393
24567
|
// src/commands/protocol/workflow-run-get.ts
|
|
@@ -24496,125 +24670,6 @@ function attachWorkflowRunGet(runs) {
|
|
|
24496
24670
|
});
|
|
24497
24671
|
}
|
|
24498
24672
|
|
|
24499
|
-
// src/commands/protocol/workflow-run-wait.ts
|
|
24500
|
-
var TERMINAL_STATUSES = new Set(["completed", "failed", "cancelled"]);
|
|
24501
|
-
var WAIT_EXIT_CODES = {
|
|
24502
|
-
completed: 0,
|
|
24503
|
-
failed: 1,
|
|
24504
|
-
cancelled: 2,
|
|
24505
|
-
paused: 3,
|
|
24506
|
-
timeout: 4
|
|
24507
|
-
};
|
|
24508
|
-
var FIRST_POLL_DELAY_MS = 2000;
|
|
24509
|
-
var MAX_POLL_DELAY_MS = 15000;
|
|
24510
|
-
var POLL_BACKOFF_FACTOR = 2;
|
|
24511
|
-
var DEFAULT_WAIT_TIMEOUT_SECONDS = 3600;
|
|
24512
|
-
var WAIT_TIMEOUT_FLAG = "--wait-timeout <seconds>";
|
|
24513
|
-
function optionalString(value) {
|
|
24514
|
-
return typeof value === "string" && value !== "" ? value : null;
|
|
24515
|
-
}
|
|
24516
|
-
function runData(raw) {
|
|
24517
|
-
return isRecordLike(raw) && isRecordLike(raw.data) ? raw.data : raw;
|
|
24518
|
-
}
|
|
24519
|
-
function readRun(raw) {
|
|
24520
|
-
const run = isRecordLike(raw) && isRecordLike(raw.data) ? raw.data : raw;
|
|
24521
|
-
if (!isRecordLike(run) || typeof run.status !== "string") {
|
|
24522
|
-
throw new SimApiError("Run status response carried no status.", 0);
|
|
24523
|
-
}
|
|
24524
|
-
const paused = toRecordOrNull(run.paused);
|
|
24525
|
-
return {
|
|
24526
|
-
status: run.status,
|
|
24527
|
-
pauseKind: paused ? optionalString(paused.pauseKind) : null,
|
|
24528
|
-
resumeAt: paused ? optionalString(paused.resumeAt) : null,
|
|
24529
|
-
contextId: paused ? optionalString(paused.contextId) : null
|
|
24530
|
-
};
|
|
24531
|
-
}
|
|
24532
|
-
function classify(snapshot) {
|
|
24533
|
-
if (snapshot.status === "paused")
|
|
24534
|
-
return snapshot.pauseKind === "time" ? null : "paused";
|
|
24535
|
-
if (!TERMINAL_STATUSES.has(snapshot.status))
|
|
24536
|
-
return null;
|
|
24537
|
-
return snapshot.status === "completed" ? "completed" : snapshot.status === "cancelled" ? "cancelled" : "failed";
|
|
24538
|
-
}
|
|
24539
|
-
function waitProgress() {
|
|
24540
|
-
let reported = false;
|
|
24541
|
-
return {
|
|
24542
|
-
advance: (status, elapsedMs) => {
|
|
24543
|
-
if (!hasProgressTerminal())
|
|
24544
|
-
return;
|
|
24545
|
-
reported = true;
|
|
24546
|
-
writeStderr(`\r${styles3().dim(`${status} — waiting ${Math.round(elapsedMs / 1000)}s…`)}\x1B[K`);
|
|
24547
|
-
},
|
|
24548
|
-
finish: () => {
|
|
24549
|
-
if (!reported)
|
|
24550
|
-
return;
|
|
24551
|
-
reported = false;
|
|
24552
|
-
writeStderr("\r\x1B[K");
|
|
24553
|
-
}
|
|
24554
|
-
};
|
|
24555
|
-
}
|
|
24556
|
-
function parseWaitTimeout(raw) {
|
|
24557
|
-
const seconds = Number(raw);
|
|
24558
|
-
if (!Number.isFinite(seconds) || seconds < 0) {
|
|
24559
|
-
throw new SimApiError(`Invalid ${WAIT_TIMEOUT_FLAG} "${raw}". Use a non-negative number of seconds, or 0 to wait indefinitely.`, 0);
|
|
24560
|
-
}
|
|
24561
|
-
return seconds;
|
|
24562
|
-
}
|
|
24563
|
-
function explain(outcome, runId, workflowId, snapshot) {
|
|
24564
|
-
if (outcome === "completed")
|
|
24565
|
-
return null;
|
|
24566
|
-
if (outcome === "failed")
|
|
24567
|
-
return `Run ${runId} failed.`;
|
|
24568
|
-
if (outcome === "cancelled")
|
|
24569
|
-
return `Run ${runId} was cancelled.`;
|
|
24570
|
-
const context = snapshot.contextId ? ` --context ${snapshot.contextId}` : "";
|
|
24571
|
-
return `Run ${runId} is paused waiting for input. Resume it: sim workflows runs resume ${runId} --workflow ${workflowId}${context}`;
|
|
24572
|
-
}
|
|
24573
|
-
function runSpec() {
|
|
24574
|
-
return CLI_CONTRACT.getWorkflowRun ?? {};
|
|
24575
|
-
}
|
|
24576
|
-
function attachWorkflowRunWait(runs) {
|
|
24577
|
-
const [wait, connectWait] = apiCommand(runs, "wait", ["getWorkflowRun"]);
|
|
24578
|
-
wait.argument("<runId>", V2_OPERATIONS.getWorkflowRun.pathParamDocs?.runId).allowExcessArguments(false).description("Wait until a run finishes or needs input").addOption(new Option("--workflow <workflowId>", "Workflow ID (required)").makeOptionMandatory()).addOption(new Option(WAIT_TIMEOUT_FLAG, `Give up after this many seconds, or 0 to wait indefinitely (default: ${DEFAULT_WAIT_TIMEOUT_SECONDS}). Bounds the whole wait; SIM_TIMEOUT_SECONDS bounds one request`)).addHelpText("after", "Exit codes: 0 completed, 1 failed, 2 cancelled, 3 needs input, 4 timed out. Time-based pauses continue waiting; a human pause or an unspecified pause kind ends the wait with code 3. Use `sim workflows runs resume` with the pause context to continue.").action(async (runId, options) => {
|
|
24579
|
-
const timeoutSeconds = options.waitTimeout === undefined ? DEFAULT_WAIT_TIMEOUT_SECONDS : parseWaitTimeout(options.waitTimeout);
|
|
24580
|
-
const { client, profile } = connectWait();
|
|
24581
|
-
const params = { workflowId: options.workflow, runId };
|
|
24582
|
-
const startedAt = Date.now();
|
|
24583
|
-
const deadline = timeoutSeconds === 0 ? Number.POSITIVE_INFINITY : startedAt + timeoutSeconds * 1000;
|
|
24584
|
-
const progress = waitProgress();
|
|
24585
|
-
let delayMs = FIRST_POLL_DELAY_MS;
|
|
24586
|
-
try {
|
|
24587
|
-
while (true) {
|
|
24588
|
-
const raw = await client.request("getWorkflowRun", { params });
|
|
24589
|
-
const snapshot = readRun(raw);
|
|
24590
|
-
const outcome = classify(snapshot);
|
|
24591
|
-
if (outcome) {
|
|
24592
|
-
progress.finish();
|
|
24593
|
-
renderResult("getWorkflowRun", profile.output, runData(raw), runSpec());
|
|
24594
|
-
const message = explain(outcome, runId, options.workflow, snapshot);
|
|
24595
|
-
if (message)
|
|
24596
|
-
printError(styles3().red(message));
|
|
24597
|
-
setSoftExitCode(WAIT_EXIT_CODES[outcome]);
|
|
24598
|
-
return;
|
|
24599
|
-
}
|
|
24600
|
-
const remainingMs = deadline - Date.now();
|
|
24601
|
-
if (remainingMs <= 0) {
|
|
24602
|
-
progress.finish();
|
|
24603
|
-
renderResult("getWorkflowRun", profile.output, runData(raw), runSpec());
|
|
24604
|
-
printError(styles3().red(`Timed out after ${timeoutSeconds}s waiting for run ${runId} (status: ${snapshot.status}${snapshot.resumeAt ? `, resuming at ${snapshot.resumeAt}` : ""}). Raise ${WAIT_TIMEOUT_FLAG}, or set it to 0 to wait indefinitely.`));
|
|
24605
|
-
setSoftExitCode(WAIT_EXIT_CODES.timeout);
|
|
24606
|
-
return;
|
|
24607
|
-
}
|
|
24608
|
-
progress.advance(snapshot.status, Date.now() - startedAt);
|
|
24609
|
-
await sleep(Math.min(delayMs, remainingMs));
|
|
24610
|
-
delayMs = Math.min(delayMs * POLL_BACKOFF_FACTOR, MAX_POLL_DELAY_MS);
|
|
24611
|
-
}
|
|
24612
|
-
} finally {
|
|
24613
|
-
progress.finish();
|
|
24614
|
-
}
|
|
24615
|
-
});
|
|
24616
|
-
}
|
|
24617
|
-
|
|
24618
24673
|
// src/commands/protocol/index.ts
|
|
24619
24674
|
function group(program, name) {
|
|
24620
24675
|
const existing = program.commands.find((command) => command.name() === name);
|