Repository navigation
Expand file tree
/
Copy pathexecute.ts
More file actions
883 lines (835 loc) · 29.2 KB
/
Copy pathexecute.ts
File metadata and controls
883 lines (835 loc) · 29.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
/**
* MCP JSON-RPC client — POST /mcp tools/call agentstack.execute.
* SoT wire format: genetic-ai-starter recipes/03-mcp-execute (+ busy retry from docs_site).
* Genetic tag: repo.tooling.user_cli.gen1
*/
import { canonicalMcpAction } from './mcpActionAliases.generated';
import { stepNeedsSemanticAnchor } from './batchParamAnchorEval';
import { parseMcpExecuteUiOutcome } from './parseMcpExecuteUiOutcome';
import { MCP_WORK_GRAPH_HOIST_KEYS, normalizeMcpWorkGraphPayload } from './workGraphHoist';
export interface McpStep {
action: string;
params?: Record<string, unknown>;
id?: string;
}
export interface McpExecuteOptions {
token: string;
/** Omitted for ecosystem-scoped discovery reads (context.project_id unset). */
projectId?: number;
mcpUrl: string;
/**
* When JWT project_id ≠ tenant workspace, call AgentAuth.switchProject (REST
* POST /api/auth/switch-project) and pass the returned access_token here before mutate batches.
*/
/** Passed to agentstack.execute options when set. */
idempotencyKey?: string;
/** Expand recipe steps server-side (`options.recipe_id`). */
recipeId?: string;
/** Values for recipe placeholders (`options.recipe_params`). */
recipeParams?: Record<string, unknown>;
/** Queue batch; poll discovery.job_status (`options.async`). */
async?: boolean;
/** Playbook text. The server returns instruction_packet for the current step. */
goal?: string;
/** Work graph node id from agents.work_next / plan_claim. */
focusNodeId?: string;
/** Bind batch to plan_execute / graph execution lane. */
planExecuteMode?: boolean;
/** V5 batch anchor: direct CRUD bootstrap or validate-only without options.goal. */
executionIntent?: 'direct' | 'workflow' | 'validate';
/** Structured semantic anchor (intent_id, goal_text, step_actions). */
routedGoal?: Record<string, unknown>;
/** Playbook progress. completed_steps holds step ids. */
observed?: Record<string, unknown>;
/** Server session id from the previous envelope. */
workflowRunId?: string;
/** Wording only. The allowed action set stays the same. */
density?: 'strict' | 'guided' | 'autonomous';
/** One-request copy locale. Not saved on the profile. */
locale?: 'en-US' | 'ru-RU' | 'pt-BR' | string;
stopOnError?: boolean;
timeoutMs?: number;
maxAttempts?: number;
/** JSON-RPC tools/call name; read tool sets server-side read-only guard. */
mcpTool?: 'agentstack.execute' | 'agentstack.read';
}
/** Ensure mutating batches carry options.goal when the caller omitted it. */
export function withSemanticAnchor<T extends { goal?: string }>(
opts: T,
goal: string,
): T {
const trimmed = String(goal ?? '').trim();
const existing = String(opts.goal ?? '').trim();
if (!trimmed || existing) {
return opts;
}
return { ...opts, goal: trimmed };
}
/** Authorized CRUD / validate-only batches without workflow goal text. */
export function withExecutionIntent<T extends { executionIntent?: string }>(
opts: T,
intent: 'direct' | 'workflow' | 'validate',
): T {
if (opts.executionIntent) {
return opts;
}
return { ...opts, executionIntent: intent };
}
/** Shorthand for authorized CRUD batches (semantic anchor). */
export function withDirectExecutionIntent<T extends { executionIntent?: string }>(
opts: T,
): T {
return withExecutionIntent(opts, 'direct');
}
function paramTruthy(val: unknown): boolean {
return val === true || val === 'true' || val === 1 || val === '1';
}
function paramFalsey(val: unknown): boolean {
return val === false || val === 'false' || val === 0 || val === '0';
}
function batchHasSemanticAnchor(opts: McpExecuteOptions): boolean {
return Boolean(
opts.executionIntent ||
String(opts.goal ?? '').trim() ||
String(opts.focusNodeId ?? '').trim() ||
opts.planExecuteMode ||
(opts.routedGoal && Object.keys(opts.routedGoal).length > 0),
);
}
function ensureDirectExecutionIntentForBatch(
steps: McpStep[],
opts: McpExecuteOptions,
): McpExecuteOptions {
const hasAnchor = batchHasSemanticAnchor(opts);
if (hasAnchor) {
return opts;
}
for (const step of steps) {
const action = canonicalActionForBatchAnchor(String(step?.action ?? '').trim());
if (action && stepNeedsSemanticAnchor(action, step.params ?? {})) {
return withDirectExecutionIntent(opts);
}
}
return opts;
}
function canonicalActionForBatchAnchor(action: string): string {
return canonicalMcpAction(action);
}
/** DRY batch anchor for single-step param-sensitive mutates (mirrors server audit_batch_policy). */
export function ensureBatchSemanticAnchor(
steps: McpStep[],
opts: McpExecuteOptions,
): McpExecuteOptions {
if (steps.length !== 1) {
return opts;
}
const step = steps[0];
const action = canonicalActionForBatchAnchor(String(step?.action ?? '').trim());
if (!action) {
return opts;
}
const hasAnchor = batchHasSemanticAnchor(opts);
if (hasAnchor) {
return opts;
}
const params = step.params ?? {};
if (action === 'agents.work_next' && paramTruthy(params.claim)) {
return withExecutionIntent(opts, 'direct');
}
if (action === 'agents.ensure_agent' && paramTruthy(params.create)) {
return withExecutionIntent(opts, 'direct');
}
if (action === 'hosting.bucket.files.bulk_delete') {
return withExecutionIntent(opts, 'direct');
}
if (action === 'script.run') {
return withExecutionIntent(opts, 'direct');
}
if (
action === 'rooms.quick_match' ||
action === 'rooms.leave' ||
action === 'rooms.poll_events' ||
action === 'rooms.match_status' ||
action === 'stream.relay.ws_ticket'
) {
return withExecutionIntent(opts, 'direct');
}
return opts;
}
export interface McpStepResult {
id?: string;
ok?: boolean;
result?: unknown;
error?: string;
[key: string]: unknown;
}
export type WorkflowDiagnostic = {
code?: string;
severity?: 'warning' | 'error' | string;
reason?: string;
action?: string;
};
import { goalVerificationFromEnvelope, type GoalVerificationWire } from './goalVerificationWire';
export type { GoalVerificationWire } from './goalVerificationWire';
export {
goalVerificationFromEnvelope,
normalizeGoalVerificationWire,
} from './goalVerificationWire';
/** Mirrors Python ``StepCompletionResult`` (three-lane workflow envelope). */
export type StepCompletionResult = {
step_id?: string;
completed?: boolean;
reason?: string;
evidence_receipt_ids?: string[];
revision_before?: number;
revision_after?: number;
};
export type WorkflowCompletion = {
advanced?: boolean;
workflow_revision?: number;
receipt_recorded?: boolean;
step_completion?: boolean;
execution_advanced?: boolean;
step_completion_result?: StepCompletionResult;
workflow_run_id?: string;
workflow_diagnostics?: WorkflowDiagnostic[];
goal_verification?: GoalVerificationWire;
};
/** Pointer stamped on agentstack.execute batch root (result.v1). */
export type AgentOsCard = {
session: string;
batch: string;
result: string;
catalog: string;
follow?: 'next_step';
};
export interface McpInstructionSliceWire {
when_to_use?: string;
when_blocked?: Record<string, string>;
verify?: string[];
rollback?: string[];
effect?: Record<string, unknown>;
}
export interface McpExecuteResult {
ok: boolean;
results: McpStepResult[];
raw: unknown;
agent_os?: AgentOsCard;
error?: string;
status?: number;
error_code?: string;
workflow_status?: string;
partial_success?: boolean;
succeeded_count?: number;
failed_step_ids?: string[];
/** Batch / step trace ids from MCP envelope (FlowReceipt handoff). */
trace_ids?: string[];
workflow_diagnostics?: WorkflowDiagnostic[];
workflow_completion?: WorkflowCompletion;
goal_verification?: GoalVerificationWire;
instruction_slice?: McpInstructionSliceWire;
}
/** MCP step result_kind values that mean work is not executable despite transport ok. */
export const WORK_GRAPH_BLOCK_RESULT_KINDS = new Set([
'work_graph_blocked',
'work_graph_waiting',
]);
export function isWorkGraphBlockResultKind(kind: unknown): boolean {
return typeof kind === 'string' && WORK_GRAPH_BLOCK_RESULT_KINDS.has(kind);
}
/** Scan agentstack.execute envelope steps for work_graph_* result_kind. */
function stepDataRecord(step: Record<string, unknown>): Record<string, unknown> | null {
const result = step.result;
if (result && typeof result === 'object') {
const data = (result as Record<string, unknown>).data;
if (data && typeof data === 'object') return data as Record<string, unknown>;
return result as Record<string, unknown>;
}
const data = step.data;
return data && typeof data === 'object' ? (data as Record<string, unknown>) : null;
}
export function batchEnvelopeHasWorkGraphBlock(root: unknown): boolean {
if (!root || typeof root !== 'object') return false;
const rec = root as Record<string, unknown>;
const steps = Array.isArray(rec.steps) ? rec.steps : [];
for (const rawStep of steps) {
if (!rawStep || typeof rawStep !== 'object') continue;
const step = rawStep as Record<string, unknown>;
if (isWorkGraphBlockResultKind(step.result_kind)) return true;
const data = stepDataRecord(step);
if (data) {
if (isWorkGraphBlockResultKind(data.result_kind)) return true;
const readiness = data.action_readiness;
if (readiness && typeof readiness === 'object') {
const reason = String((readiness as Record<string, unknown>).blocked_reason || '');
if (reason === 'internal_execution_error') return true;
if ((readiness as Record<string, unknown>).can_claim === false) return true;
}
}
}
return false;
}
/** Map server partial_success / legacy aliases to UI workflow_status. */
export function normalizeWorkflowStatus(
status: string,
partialSuccess?: boolean,
): string {
const s = String(status || '').trim();
if (partialSuccess || s === 'partial_success') return 'partial';
return s;
}
/** Merge W6 MCP batch envelope fields onto work_next packet shapes (SDK SoT). */
export function mergeExecuteEnvelopeIntoPacket<T extends Record<string, unknown>>(
packet: T,
batch?: Record<string, unknown> | null,
): T {
if (!batch || typeof batch !== 'object') {
return packet;
}
const out = { ...packet } as T;
const outRec = out as Record<string, unknown>;
if (Array.isArray(batch.workflow_diagnostics)) {
const existing = outRec.workflow_diagnostics;
outRec.workflow_diagnostics = Array.isArray(existing)
? [...existing, ...batch.workflow_diagnostics]
: batch.workflow_diagnostics;
}
if (
batch.workflow_completion &&
typeof batch.workflow_completion === 'object' &&
!outRec.workflow_completion
) {
outRec.workflow_completion = batch.workflow_completion;
}
const gv = goalVerificationFromEnvelope({ ...outRec, ...batch });
if (gv) {
const prior = outRec.goal_verification;
outRec.goal_verification =
prior && typeof prior === 'object' && !Array.isArray(prior)
? { ...(prior as Record<string, unknown>), ...gv }
: gv;
}
const runId = String(batch.workflow_run_id ?? '').trim();
if (runId && !String(outRec.workflow_run_id ?? '').trim()) {
outRec.workflow_run_id = runId;
}
if (batch.partial_success === true) {
outRec.partial_success = true;
}
const slice = batch.instruction_slice;
if (
slice &&
typeof slice === 'object' &&
!Array.isArray(slice) &&
!outRec.instruction_slice
) {
outRec.instruction_slice = slice;
}
if (batch.error_code && !outRec.error_code) {
outRec.error_code = batch.error_code;
}
for (const key of MCP_WORK_GRAPH_HOIST_KEYS) {
if (outRec[key] === undefined && batch[key] !== undefined) {
outRec[key] = batch[key];
}
}
const agentOs = batch.agent_os;
if (agentOs && typeof agentOs === 'object' && !Array.isArray(agentOs) && !outRec.agent_os) {
outRec.agent_os = agentOs;
}
return normalizeMcpWorkGraphPayload(outRec) as T;
}
function isServerBusy(payload: unknown, status?: number): boolean {
if (status === 503) return true;
const text = JSON.stringify(payload ?? {}).toLowerCase();
return text.includes('server_busy');
}
async function sleep(ms: number): Promise<void> {
await new Promise((r) => setTimeout(r, ms));
}
/**
* Execute one or more MCP actions via JSON-RPC `tools/call` → `agentstack.execute`.
*/
export async function mcpExecute(
steps: McpStep[],
opts: McpExecuteOptions,
): Promise<McpExecuteResult> {
const effectiveOpts = ensureDirectExecutionIntentForBatch(
steps,
ensureBatchSemanticAnchor(steps, opts),
);
const context: Record<string, unknown> = {};
const pid = effectiveOpts.projectId;
if (pid != null && Number(pid) > 0) {
context.project_id = Number(pid);
}
const locale = String(effectiveOpts.locale ?? '').trim();
if (locale) context.locale = locale;
const mcpTool =
effectiveOpts.mcpTool === 'agentstack.read' ? 'agentstack.read' : 'agentstack.execute';
const readGuard =
mcpTool === 'agentstack.read' ? ({ _mcp_read_tool: true as const } as const) : {};
const argumentsBody = {
steps: steps.map((s, i) => ({
id: s.id ?? `step_${i}`,
action: s.action,
params: s.params ?? {},
})),
context,
options: {
stopOnError: effectiveOpts.stopOnError !== false,
...readGuard,
...(effectiveOpts.idempotencyKey ? { idempotency_key: effectiveOpts.idempotencyKey } : {}),
...(effectiveOpts.recipeId ? { recipe_id: effectiveOpts.recipeId } : {}),
...(effectiveOpts.recipeParams ? { recipe_params: effectiveOpts.recipeParams } : {}),
...(effectiveOpts.async ? { async: true } : {}),
...(effectiveOpts.goal ? { goal: effectiveOpts.goal } : {}),
...(effectiveOpts.focusNodeId ? { focus_node_id: effectiveOpts.focusNodeId } : {}),
...(effectiveOpts.planExecuteMode ? { plan_execute_mode: effectiveOpts.planExecuteMode } : {}),
...(effectiveOpts.executionIntent ? { execution_intent: effectiveOpts.executionIntent } : {}),
...(effectiveOpts.routedGoal ? { routed_goal: effectiveOpts.routedGoal } : {}),
...(effectiveOpts.observed ? { observed: effectiveOpts.observed } : {}),
...(effectiveOpts.workflowRunId ? { workflow_run_id: effectiveOpts.workflowRunId } : {}),
...(effectiveOpts.density ? { density: effectiveOpts.density } : {}),
},
};
const body = {
jsonrpc: '2.0',
id: 1,
method: 'tools/call',
params: {
name: mcpTool,
arguments: argumentsBody,
},
};
const maxAttempts = Math.max(1, opts.maxAttempts ?? 4);
let delayMs = 1000;
let lastRaw: unknown = null;
let lastStatus: number | undefined;
for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
const controller = typeof AbortController !== 'undefined' ? new AbortController() : null;
const timer =
controller && opts.timeoutMs
? setTimeout(() => controller.abort(), opts.timeoutMs)
: null;
try {
const headers: Record<string, string> = {
Authorization: `Bearer ${effectiveOpts.token}`,
'Content-Type': 'application/json',
Accept: 'application/json',
};
const headerPid = effectiveOpts.projectId;
if (headerPid != null && Number(headerPid) > 0) {
headers['X-Project-ID'] = String(Number(headerPid));
}
if (locale) {
headers['Accept-Language'] = locale;
}
const res = await fetch(opts.mcpUrl, {
method: 'POST',
headers,
body: JSON.stringify(body),
signal: controller?.signal,
});
lastStatus = res.status;
lastRaw = await res.json().catch(() => ({}));
if (isServerBusy(lastRaw, res.status) && attempt < maxAttempts - 1) {
await sleep(delayMs);
delayMs = Math.min(delayMs * 2, 30_000);
continue;
}
const results = extractResults(lastRaw);
const batchMeta = extractBatchMeta(lastRaw);
const domainBlocked = batchEnvelopeHasWorkGraphBlock(lastRaw);
if (domainBlocked && batchMeta.partial_success !== true) {
batchMeta.partial_success = true;
}
const stepsOk =
results.length === 0 || results.every((r) => r.ok !== false && !r.error);
const ok =
res.ok &&
(batchMeta.partial_success === true || domainBlocked || stepsOk);
return {
ok,
results,
raw: lastRaw,
status: res.status,
error: ok ? undefined : summarizeError(lastRaw, results),
...batchMeta,
};
} catch (err) {
lastRaw = { error: err instanceof Error ? err.message : String(err) };
if (attempt >= maxAttempts - 1) {
return {
ok: false,
results: [],
raw: lastRaw,
error: err instanceof Error ? err.message : String(err),
};
}
await sleep(delayMs);
delayMs = Math.min(delayMs * 2, 30_000);
} finally {
if (timer) clearTimeout(timer);
}
}
return {
ok: false,
results: [],
raw: lastRaw,
status: lastStatus,
error: 'mcp_execute_exhausted',
};
}
export function extractBatchMeta(payload: unknown): Pick<
McpExecuteResult,
| 'partial_success'
| 'succeeded_count'
| 'failed_step_ids'
| 'trace_ids'
| 'workflow_diagnostics'
| 'workflow_completion'
| 'goal_verification'
| 'agent_os'
| 'workflow_status'
| 'instruction_slice'
| 'error_code'
> {
if (!payload || typeof payload !== 'object') return {};
const p = payload as Record<string, unknown>;
const inner = (p.result as Record<string, unknown> | undefined) ?? p;
const out: Pick<
McpExecuteResult,
| 'partial_success'
| 'succeeded_count'
| 'failed_step_ids'
| 'trace_ids'
| 'workflow_diagnostics'
| 'workflow_completion'
| 'goal_verification'
| 'agent_os'
| 'workflow_status'
| 'instruction_slice'
| 'error_code'
> = {};
if (typeof inner.partial_success === 'boolean') out.partial_success = inner.partial_success;
if (typeof inner.workflow_status === 'string') {
out.workflow_status = normalizeWorkflowStatus(
inner.workflow_status,
inner.partial_success === true,
);
}
if (typeof inner.error_code === 'string') out.error_code = inner.error_code;
if (inner.instruction_slice && typeof inner.instruction_slice === 'object') {
out.instruction_slice = inner.instruction_slice as McpInstructionSliceWire;
}
if (typeof inner.succeeded_count === 'number') out.succeeded_count = inner.succeeded_count;
if (Array.isArray(inner.failed_step_ids)) {
out.failed_step_ids = inner.failed_step_ids.map((id) => String(id));
}
const traceIds: string[] = [];
if (Array.isArray(inner.trace_ids)) {
for (const tid of inner.trace_ids) {
if (typeof tid === 'string' && tid.trim()) traceIds.push(tid.trim());
}
}
if (typeof inner.trace_id === 'string' && inner.trace_id.trim()) {
traceIds.push(inner.trace_id.trim());
}
if (traceIds.length) {
out.trace_ids = [...new Set(traceIds)];
}
if (Array.isArray(inner.workflow_diagnostics)) {
out.workflow_diagnostics = inner.workflow_diagnostics as WorkflowDiagnostic[];
}
const topGv = inner.goal_verification;
if (topGv && typeof topGv === 'object') {
out.goal_verification = topGv as GoalVerificationWire;
}
const wc = inner.workflow_completion;
if (wc && typeof wc === 'object') {
out.workflow_completion = wc as WorkflowCompletion;
const nested = (wc as WorkflowCompletion).workflow_diagnostics;
if (!out.workflow_diagnostics && Array.isArray(nested)) {
out.workflow_diagnostics = nested;
}
const wcGv = (wc as WorkflowCompletion).goal_verification;
if (!out.goal_verification && wcGv && typeof wcGv === 'object') {
out.goal_verification = wcGv;
}
}
const aos = inner.agent_os;
if (aos && typeof aos === 'object' && !Array.isArray(aos)) {
out.agent_os = aos as AgentOsCard;
}
return out;
}
function asMcpRecord(value: unknown): Record<string, unknown> | null {
if (!value || typeof value !== 'object' || Array.isArray(value)) return null;
return value as Record<string, unknown>;
}
/** One step from tools/call structuredContent or `{ data, job_id }` text. */
function stepFromToolsCallRecord(rec: Record<string, unknown> | null): McpStepResult | null {
if (!rec) return null;
const data = asMcpRecord(rec.data);
const payload = data ?? (rec.job_id != null ? rec : null);
if (!payload) return null;
return {
ok: rec.ok !== false,
data: payload,
result: { data: payload },
error: rec.error != null ? asErrorText(rec.error) || undefined : undefined,
};
}
export function extractResults(payload: unknown): McpStepResult[] {
if (!payload || typeof payload !== 'object') return [];
const p = payload as Record<string, unknown>;
if (Array.isArray(p.results)) return p.results as McpStepResult[];
const result = p.result as Record<string, unknown> | undefined;
if (result && Array.isArray(result.results)) return result.results as McpStepResult[];
// Some gateways wrap content as text JSON
const content = result?.content;
let fromText: McpStepResult | null = null;
if (Array.isArray(content)) {
for (const block of content) {
const b = block as { text?: string; type?: string };
if (b?.text) {
try {
const parsed = JSON.parse(b.text) as {
results?: McpStepResult[];
data?: unknown;
job_id?: unknown;
};
if (Array.isArray(parsed.results)) return parsed.results;
fromText = fromText ?? stepFromToolsCallRecord(parsed);
} catch {
/* ignore */
}
}
}
}
if (Array.isArray(p.steps)) {
return (p.steps as Array<Record<string, unknown>>).map((s) => {
const status = typeof s.status === 'string' ? s.status : undefined;
const isError = status === 'error' || Boolean(s.error);
const isSkipped = status === 'skipped';
const ok =
status === 'success' ||
(!isError && !isSkipped && s.success !== false && !s.error);
return {
...s,
id: String(s.id ?? ''),
ok,
status,
result: s.result,
error: s.error != null ? String(s.error) : undefined,
error_code: typeof s.error_code === 'string' ? s.error_code : undefined,
instruction_slice: s.instruction_slice,
capability_result: s.capability_result,
};
});
}
const structured = stepFromToolsCallRecord(asMcpRecord(result?.structuredContent));
if (structured) {
if (result?.isError === true) structured.ok = false;
return [structured];
}
if (fromText) return [fromText];
return [];
}
export interface InversePacket {
step_id: string;
allowed_actions: string[];
text: string;
writes_allowed: boolean;
}
export interface InverseEnvelope {
packet: InversePacket | null;
observed: Record<string, unknown> | null;
workflowRunId: string;
workflowStatus: string;
goalVerification: GoalVerificationWire | null;
}
function asRecord(value: unknown): Record<string, unknown> | null {
if (!value || typeof value !== 'object' || Array.isArray(value)) return null;
return value as Record<string, unknown>;
}
function findInverseBody(payload: unknown): Record<string, unknown> | null {
const root = asRecord(payload);
if (!root) return null;
const result = asRecord(root.result);
const layers = [root, result, asRecord(result?.result)];
for (const row of layers) {
if (!row) continue;
if ('instruction_packet' in row || 'next_step' in row || 'completion' in row) return row;
}
return null;
}
/** Read the playbook cursor from an execute JSON-RPC payload. Null packet means the graph is finished. */
export function readInverseEnvelope(raw: unknown): InverseEnvelope | null {
const body = findInverseBody(raw);
if (!body) return null;
const packet = asRecord(body.instruction_packet);
const observed = asRecord(body.observed);
const completion = asRecord(body.completion);
const wc = asRecord(body.workflow_completion);
return {
packet: packet
? {
step_id: String(packet.step_id ?? ''),
allowed_actions: Array.isArray(packet.allowed_actions)
? packet.allowed_actions.map((name) => String(name))
: [],
text: String(packet.text ?? ''),
writes_allowed: packet.writes_allowed !== false,
}
: null,
observed,
workflowRunId: String(
body.workflow_run_id ?? wc?.workflow_run_id ?? completion?.workflow_run_id ?? '',
),
workflowStatus: normalizeWorkflowStatus(
String(completion?.workflow_status ?? body.workflow_status ?? ''),
body.partial_success === true,
),
goalVerification: goalVerificationFromEnvelope(body),
};
}
/** True when batch completed with mixed step outcomes (server partial_success). */
export function isMcpPartialSuccess(
result: Pick<McpExecuteResult, 'partial_success' | 'ok'>,
): boolean {
return Boolean(result.partial_success);
}
/** Compact UI/agent summary for execute batch outcomes. */
export function summarizeMcpBatchOutcome(result: McpExecuteResult): {
state: 'success' | 'partial_success' | 'error';
message: string;
succeededCount?: number;
failedStepIds?: string[];
} {
const root =
result.raw && typeof result.raw === 'object'
? {
...(result.raw as Record<string, unknown>),
ok: result.ok,
partial_success: result.partial_success,
succeeded_count: result.succeeded_count,
failed_step_ids: result.failed_step_ids,
error_code: result.error_code,
}
: {
ok: result.ok,
partial_success: result.partial_success,
succeeded_count: result.succeeded_count,
failed_step_ids: result.failed_step_ids,
error: result.error,
error_code: result.error_code,
};
const ui = parseMcpExecuteUiOutcome(root);
const failed = ui.failedStepIds ?? result.failed_step_ids;
const n = ui.succeededCount ?? result.succeeded_count;
if (ui.state === 'partial_success') {
const failedLen = failed?.length ?? 0;
return {
state: 'partial_success',
message: `Partial success: ${n ?? 0} step(s) ok, ${failedLen} failed`,
succeededCount: n,
failedStepIds: failed,
};
}
if (ui.state === 'success') {
return { state: 'success', message: 'All steps succeeded', succeededCount: n, failedStepIds: failed };
}
return {
state: 'error',
message: result.error ?? 'MCP execute failed',
failedStepIds: failed,
};
}
/**
* Run a multi-step inverse workflow loop with stable workflowRunId + idempotency key.
*
* - Pass `workflowRunId` on every batch; server may mint on first goal batch when omitted.
* - On `run_id_rebound` / `workflow_run_identity_drift`, adopt `completion.workflow_run_id`
* from the response for subsequent calls (return value may include `workflowRunId`).
* - Pair with Compass / PTC recovery tasks for `plan_ops_parent_missing` after plan_patch errors.
*/
export async function runInverseWorkflowLoop(
steps: McpStep[],
opts: McpExecuteOptions & { workflowRunId: string },
): Promise<McpExecuteResult> {
const runId = String(opts.workflowRunId || '').trim();
const key =
opts.idempotencyKey ||
`inverse-${runId}-${steps.map((s) => s.action).join('-')}`.slice(0, 120);
let observed = opts.observed ? { ...opts.observed } : undefined;
const result = await mcpExecute(steps, {
...opts,
workflowRunId: runId,
idempotencyKey: key,
observed,
});
const env = readInverseEnvelope(result.raw);
if (env?.observed && typeof env.observed === 'object') {
observed = env.observed as Record<string, unknown>;
}
let workflowRunId = runId;
const body = findInverseBody(result.raw);
const completion = body ? asRecord(body.completion) : null;
const reboundId = String(
completion?.workflow_run_id ?? body?.workflow_run_id ?? env?.workflowRunId ?? '',
).trim();
const warnings = Array.isArray(completion?.warnings)
? (completion!.warnings as unknown[])
: [];
const reboundWarning = warnings.some(
(w) =>
w === 'run_id_rebound' ||
(typeof w === 'object' &&
w !== null &&
(w as { code?: string }).code === 'workflow_run_identity_drift'),
);
if (reboundId && (reboundWarning || !workflowRunId)) {
workflowRunId = reboundId;
}
return {
...result,
...(observed ? { raw: { ...(result.raw as object), observed } } : {}),
...(workflowRunId !== runId ? { workflowRunId } : {}),
};
}
function asErrorText(value: unknown): string {
if (typeof value === 'string') {
return value === '[object Object]' ? '' : value;
}
if (value && typeof value === 'object') {
const rec = value as { message?: unknown; error?: unknown; field?: unknown };
if (typeof rec.message === 'string' && rec.message && rec.message !== '[object Object]') {
return rec.message;
}
if (typeof rec.error === 'string' && rec.error && rec.error !== '[object Object]') {
return rec.error;
}
if (typeof rec.field === 'string' && rec.field) {
return `Validation failed (${rec.field})`;
}
try {
return JSON.stringify(value);
} catch {
return '';
}
}
const text = String(value ?? '');
return text === '[object Object]' ? '' : text;
}
function summarizeError(raw: unknown, results: McpStepResult[]): string {
const failed = results.find((r) => r.ok === false || r.error);
const fromStep = asErrorText(failed?.error);
if (fromStep) return fromStep;
if (raw && typeof raw === 'object' && 'error' in raw) {
const fromRaw = asErrorText((raw as { error?: unknown }).error);
if (fromRaw) return fromRaw;
}
return 'mcp_execute_failed';
}