-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Expand file tree
/
Copy pathvela-child-evidence.ts
More file actions
849 lines (811 loc) · 30.1 KB
/
Copy pathvela-child-evidence.ts
File metadata and controls
849 lines (811 loc) · 30.1 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
import {
NORMALIZED_AGENT_OBSERVATION_V1_SCHEMA,
NormalizedAgentObservationV1Schema,
type ChildEvidenceCoverageV1,
type NormalizedAgentObservationV1,
type RuntimeObservationEvidenceLevelV1,
type StrategyInputStageV2,
} from '@open-design/contracts';
import {
buildSafeChildPromptTelemetry,
type SafeChildPromptInput,
} from '../prompt-telemetry.js';
export const VELA_CHILD_EVIDENCE_EXTENSION =
'vela.opencode.child_agent_lifecycle' as const;
export const VELA_CHILD_EVIDENCE_SCHEMA_VERSION = 1 as const;
export const VELA_CHILD_EVIDENCE_ADAPTER_VERSION =
'od-vela-opencode-child-evidence/v1' as const;
/**
* Coverage `source` discriminator for this runtime, alongside the sibling
* runtimes' `opencode_json_event_stream` and `claude_stream_json`. Downstream
* aggregation reads it to attribute a coverage gap to the producing runtime.
*/
export const VELA_CHILD_EVIDENCE_COVERAGE_SOURCE = 'vela_opencode_acp' as const;
/**
* Review pin for the candidate wire fixture, carried into every observation as
* `candidateCommit` so evidence stays attributable to the producer that emitted
* it.
*
* `published` stays false: the producer lives on an unmerged Vela branch, so a
* build carrying it is not something a user can install. `verifiedRuntimeSupport`
* is now true — a local Vela built from that branch negotiated the extension and
* drove a complex OD Next task to `availability: complete` with three observed
* Children, which is the claim this flag makes. Flip `published` and re-pin the
* commit once the producer lands on Vela's main.
*/
export const VELA_CHILD_EVIDENCE_CANDIDATE = Object.freeze({
repository: 'PowerformerAI/vela',
commit: 'c833b74e82e31c89414b7eaf01edabab1e2d0b06',
fixture: 'apps/cli/internal/agent/testdata/opencode_child_evidence_wire_v1.golden.json',
published: false,
bestEffortEvidenceVerified: true,
verifiedOpenCodeVersion: '1.18.18',
verifiedRuntimeSupport: true,
});
type RecordValue = Record<string, unknown>;
export type VelaChildLifecycleStatus =
| 'running'
| 'completed'
| 'failed'
| 'cancelled'
| 'timed_out';
export type VelaChildEvidenceRejectionReason =
| 'capability_not_negotiated'
| 'unsupported_schema_version'
| 'acp_session_mismatch'
| 'invalid_wire_shape'
| 'root_session_conflict'
| 'parent_cycle'
| 'parent_conflict'
| 'tool_call_rebound'
| 'evidence_id_conflict'
| 'status_regression'
| 'terminal_conflict';
export interface VelaChildEvidenceNegotiation {
advertised: boolean;
supported: boolean;
schemaVersion?: number;
producerName?: string;
producerVersion?: string;
reason: 'extension_missing' | 'supported_candidate' | 'unsupported_schema_version';
candidatePublished: false;
candidateCommit: typeof VELA_CHILD_EVIDENCE_CANDIDATE.commit;
}
export interface VelaChildRuntimeFact {
adapterVersion: typeof VELA_CHILD_EVIDENCE_ADAPTER_VERSION;
schemaVersion: typeof VELA_CHILD_EVIDENCE_SCHEMA_VERSION;
producerVersion?: string;
evidenceId: string;
state: VelaChildLifecycleStatus;
phase: 'start' | 'end';
rootSessionId: string;
childSessionId: string;
toolCallId: string;
observedAtMs: number;
startedAtMs: number;
endedAtMs?: number;
timingEvidence: 'source_timestamp' | 'bridge_observed';
lifecycleCompleteness: 'complete' | 'partial';
sourceEvidence: VelaChildSourceEvidence[];
role?: string;
provider?: string;
model?: string;
prompt?: {
sha256: string;
bytes: number;
safePayload?: SafeChildPromptInput;
};
usage?: {
completeness: 'complete' | 'partial';
source:
| 'child_step_finish'
| 'opencode_export_step_finish'
| 'opencode_export_message_snapshot';
inputTokens?: number;
outputTokens?: number;
totalTokens?: number;
thoughtTokens?: number;
cacheReadTokens?: number;
cacheWriteTokens?: number;
};
evidenceLevel: RuntimeObservationEvidenceLevelV1;
l3Eligible: false;
limitations: string[];
}
export interface VelaChildEvidenceObserveResult {
handled: boolean;
accepted: boolean;
fact?: VelaChildRuntimeFact;
reason?: VelaChildEvidenceRejectionReason;
}
export interface VelaChildEvidenceCoverageInput {
/**
* `true` only when the ACP prompt turn resolved without a fatal protocol or
* transport error and without an abort. A turn that ended any other way may
* have dropped child terminals that were still in flight, so it can never
* claim complete coverage.
*/
sessionComplete: boolean;
}
export interface VelaChildEvidenceConsumer {
negotiate(initializeResult: unknown): VelaChildEvidenceNegotiation;
observe(input: {
expectedAcpSessionId: string | null;
envelopeAcpSessionId: unknown;
update: unknown;
}): VelaChildEvidenceObserveResult;
getNegotiation(): VelaChildEvidenceNegotiation;
/**
* Provider-neutral child-evidence coverage for this ACP run. Without it the
* daemon has nothing to publish as `child_evidence_coverage_v1`, and task
* aggregation falls back to `child_lifecycle_unavailable_not_zero` for every
* AMR task — which cannot distinguish a run that provably had no Child
* agents from a run nobody was observing.
*/
childEvidenceCoverage(input: VelaChildEvidenceCoverageInput): ChildEvidenceCoverageV1;
}
function isRecord(value: unknown): value is RecordValue {
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
}
function safeString(value: unknown, maxLength = 256): string | undefined {
if (typeof value !== 'string') return undefined;
const trimmed = value.trim();
if (!trimmed || trimmed.length > maxLength || /[\u0000-\u001f\u007f]/u.test(trimmed)) {
return undefined;
}
return trimmed;
}
function nonNegativeNumber(value: unknown): number | undefined {
return typeof value === 'number' && Number.isFinite(value) && value >= 0
? value
: undefined;
}
function nonNegativeInteger(value: unknown): number | undefined {
const parsed = nonNegativeNumber(value);
return parsed !== undefined && Number.isInteger(parsed) ? parsed : undefined;
}
function safeSha256(value: unknown): string | undefined {
return typeof value === 'string' && /^[a-f0-9]{64}$/iu.test(value)
? value.toLowerCase()
: undefined;
}
function safeProducerIdentity(value: unknown): string | undefined {
return safeString(value, 128);
}
function observableProducerVersion(value: unknown): string | undefined {
const version = safeProducerIdentity(value);
// Vela versions before the candidate fix advertised this literal ACP
// placeholder even though the root CLI already exposed its real build via
// `--version`. Treat the placeholder as absent so telemetry can fall back to
// the independently probed executable instead of publishing false identity.
return version === '0.0.0' ? undefined : version;
}
export function negotiateVelaChildEvidence(
initializeResult: unknown,
): VelaChildEvidenceNegotiation {
const result = isRecord(initializeResult) ? initializeResult : undefined;
const agentInfo = isRecord(result?.agentInfo) ? result.agentInfo : undefined;
const capabilities = isRecord(result?.agentCapabilities)
? result.agentCapabilities
: undefined;
const extensions = isRecord(capabilities?.extensions)
? capabilities.extensions
: undefined;
const extension = isRecord(extensions?.[VELA_CHILD_EVIDENCE_EXTENSION])
? extensions[VELA_CHILD_EVIDENCE_EXTENSION]
: undefined;
const schemaVersion = nonNegativeInteger(extension?.schemaVersion);
const producerName = safeProducerIdentity(agentInfo?.name);
const producerVersion = safeProducerIdentity(agentInfo?.version);
const identity = {
...(producerName ? { producerName } : {}),
...(producerVersion ? { producerVersion } : {}),
candidatePublished: false as const,
candidateCommit: VELA_CHILD_EVIDENCE_CANDIDATE.commit,
};
if (!extension) {
return {
...identity,
advertised: false,
supported: false,
reason: 'extension_missing',
};
}
if (schemaVersion !== VELA_CHILD_EVIDENCE_SCHEMA_VERSION) {
return {
...identity,
advertised: true,
supported: false,
...(schemaVersion === undefined ? {} : { schemaVersion }),
reason: 'unsupported_schema_version',
};
}
return {
...identity,
advertised: true,
supported: true,
schemaVersion,
reason: 'supported_candidate',
};
}
const SOURCE_EVIDENCE = new Set([
'root_task_metadata',
'session.created',
'child_session_status',
'child_session_error',
'root_task_tool',
'parent_prompt_cancelled',
'parent_prompt_timeout',
'opencode_export_step_finish',
'opencode_export_message_snapshot',
'opencode_export_unavailable',
] as const);
type VelaChildSourceEvidence =
| 'root_task_metadata'
| 'session.created'
| 'child_session_status'
| 'child_session_error'
| 'root_task_tool'
| 'parent_prompt_cancelled'
| 'parent_prompt_timeout'
| 'opencode_export_step_finish'
| 'opencode_export_message_snapshot'
| 'opencode_export_unavailable';
const TERMINAL_SOURCE_EVIDENCE = new Set<VelaChildSourceEvidence>([
'child_session_status',
'child_session_error',
'root_task_tool',
'parent_prompt_cancelled',
'parent_prompt_timeout',
]);
function terminalSourceIsCoherent(
status: VelaChildLifecycleStatus,
completeness: VelaChildRuntimeFact['lifecycleCompleteness'],
sources: readonly VelaChildSourceEvidence[],
): boolean {
const terminalSources = sources.filter((source) => TERMINAL_SOURCE_EVIDENCE.has(source));
if (terminalSources.length !== 1) return false;
const source = terminalSources[0];
if (source === 'parent_prompt_timeout') {
return status === 'timed_out' && completeness === 'partial';
}
if (source === 'parent_prompt_cancelled') {
return status === 'cancelled' && completeness === 'partial';
}
if (completeness !== 'complete') return false;
if (source === 'child_session_error') {
return status === 'failed' || status === 'timed_out';
}
if (source === 'child_session_status') {
return status === 'completed' || status === 'cancelled';
}
return source === 'root_task_tool' && status !== 'running';
}
function sourceEvidence(value: unknown): VelaChildRuntimeFact['sourceEvidence'] {
if (!Array.isArray(value)) return [];
return [...new Set(value.flatMap((candidate) => (
typeof candidate === 'string' && SOURCE_EVIDENCE.has(
candidate as VelaChildRuntimeFact['sourceEvidence'][number],
)
? [candidate as VelaChildRuntimeFact['sourceEvidence'][number]]
: []
)))];
}
function parsePrompt(value: unknown): VelaChildRuntimeFact['prompt'] | undefined {
if (!isRecord(value) || value.availability !== 'hash_only') return undefined;
const sha256 = safeSha256(value.sha256);
const bytes = nonNegativeInteger(value.bytes);
if (!sha256 || bytes === undefined) return undefined;
const safe = typeof value.text === 'string'
? buildSafeChildPromptTelemetry([value.text]).safePayload
: undefined;
return { sha256, bytes, ...(safe ? { safePayload: safe } : {}) };
}
const USAGE_SOURCES = new Set([
'child_step_finish',
'opencode_export_step_finish',
'opencode_export_message_snapshot',
] as const);
function usageWireShapeIsCoherent(value: unknown): boolean {
if (!isRecord(value)) return false;
if (value.availability === 'unavailable') {
return value.completeness === 'unavailable' &&
value.source === undefined &&
[
value.inputTokens,
value.outputTokens,
value.totalTokens,
value.reasoningTokens,
value.cacheReadTokens,
value.cacheWriteTokens,
].every((candidate) => candidate === undefined);
}
if (value.availability !== 'available' || typeof value.source !== 'string') {
return false;
}
if (value.source === 'opencode_export_message_snapshot') {
return value.completeness === 'partial';
}
if (value.source === 'child_step_finish' || value.source === 'opencode_export_step_finish') {
return value.completeness === 'complete';
}
return false;
}
function parseUsage(value: unknown): VelaChildRuntimeFact['usage'] | undefined {
if (!isRecord(value) || value.availability !== 'available') return undefined;
if (value.completeness !== 'complete' && value.completeness !== 'partial') return undefined;
if (
typeof value.source !== 'string' ||
!USAGE_SOURCES.has(value.source as NonNullable<VelaChildRuntimeFact['usage']>['source'])
) {
return undefined;
}
const values = {
inputTokens: nonNegativeNumber(value.inputTokens),
outputTokens: nonNegativeNumber(value.outputTokens),
totalTokens: nonNegativeNumber(value.totalTokens),
thoughtTokens: nonNegativeNumber(value.reasoningTokens),
cacheReadTokens: nonNegativeNumber(value.cacheReadTokens),
cacheWriteTokens: nonNegativeNumber(value.cacheWriteTokens),
};
if (!Object.values(values).some((candidate) => candidate !== undefined)) return undefined;
return {
completeness: value.completeness,
source: value.source as NonNullable<VelaChildRuntimeFact['usage']>['source'],
...Object.fromEntries(
Object.entries(values).filter((entry): entry is [string, number] => entry[1] !== undefined),
),
};
}
function parseWireFact(update: RecordValue, now: () => number): VelaChildRuntimeFact | undefined {
if (
update.extension !== VELA_CHILD_EVIDENCE_EXTENSION ||
update.schemaVersion !== VELA_CHILD_EVIDENCE_SCHEMA_VERSION
) {
return undefined;
}
const evidenceId = safeString(update.evidenceId);
const childSessionId = safeString(update.childSessionId);
const rootSessionId = safeString(update.parentSessionId);
const toolCallId = safeString(update.toolCallId);
const startedAtMs = nonNegativeNumber(update.startedAtMs);
const endedAtMs = nonNegativeNumber(update.endedAtMs);
const phase = update.phase === 'start' || update.phase === 'end'
? update.phase
: undefined;
const status = (
update.status === 'running' ||
update.status === 'completed' ||
update.status === 'failed' ||
update.status === 'cancelled' ||
update.status === 'timed_out'
) ? update.status : undefined;
const timingEvidence = update.timingEvidence === 'source_timestamp' ||
update.timingEvidence === 'bridge_observed'
? update.timingEvidence
: undefined;
const lifecycleCompleteness = update.lifecycleCompleteness === 'complete' ||
update.lifecycleCompleteness === 'partial'
? update.lifecycleCompleteness
: undefined;
if (
!evidenceId || !childSessionId || !rootSessionId || !toolCallId ||
startedAtMs === undefined || !phase || !status || !timingEvidence ||
!lifecycleCompleteness ||
(phase === 'start' && status !== 'running') ||
(phase === 'start' && endedAtMs !== undefined) ||
(phase === 'end' && status === 'running') ||
(phase === 'end' && (endedAtMs === undefined || endedAtMs < startedAtMs))
) {
return undefined;
}
const prompt = parsePrompt(update.prompt);
if (!usageWireShapeIsCoherent(update.usage)) return undefined;
const usage = parseUsage(update.usage);
const sources = sourceEvidence(update.sourceEvidence);
const hostIncompleteTerminal = sources.includes('parent_prompt_cancelled') ||
sources.includes('parent_prompt_timeout');
const exportUsageSource = usage?.source === 'opencode_export_step_finish' ||
usage?.source === 'opencode_export_message_snapshot'
? usage.source
: undefined;
if (
!sources.includes('root_task_metadata') ||
!sources.includes('session.created') ||
(phase === 'start' && sources.some((source) => TERMINAL_SOURCE_EVIDENCE.has(source))) ||
(phase === 'end' && !terminalSourceIsCoherent(status, lifecycleCompleteness, sources)) ||
(hostIncompleteTerminal && usage !== undefined) ||
(exportUsageSource !== undefined && !sources.includes(exportUsageSource))
) {
return undefined;
}
const role = safeString(update.role, 128);
const provider = safeString(update.provider, 128);
const model = safeString(update.model, 256);
const limitations = [
'Consumed from the versioned ACP extension without Vela private I/O.',
'Producer candidate is not published and is not production capability proof.',
...(prompt
? ['Only Prompt hash and byte length are retained.']
: ['Child Prompt identity was unavailable or invalid.']),
...(usage
? []
: ['Independent child usage was unavailable or invalid.']),
...(sources.length === (Array.isArray(update.sourceEvidence) ? update.sourceEvidence.length : 0)
? []
: ['Unknown source-evidence labels were discarded.']),
'L3 remains unavailable until one closed model-turn accounting group proves ownership and inherited-copy exclusion.',
];
return {
adapterVersion: VELA_CHILD_EVIDENCE_ADAPTER_VERSION,
schemaVersion: VELA_CHILD_EVIDENCE_SCHEMA_VERSION,
evidenceId,
state: status,
phase,
rootSessionId,
childSessionId,
toolCallId,
observedAtMs: now(),
startedAtMs,
...(endedAtMs === undefined ? {} : { endedAtMs }),
timingEvidence,
lifecycleCompleteness,
sourceEvidence: sources,
...(role ? { role } : {}),
...(provider ? { provider } : {}),
...(model ? { model } : {}),
...(prompt ? { prompt } : {}),
...(usage ? { usage } : {}),
evidenceLevel: phase === 'end' && lifecycleCompleteness === 'complete' ? 'L2' : 'L1',
l3Eligible: false,
limitations,
};
}
function factSignature(fact: VelaChildRuntimeFact): string {
return JSON.stringify({
evidenceId: fact.evidenceId,
phase: fact.phase,
state: fact.state,
rootSessionId: fact.rootSessionId,
childSessionId: fact.childSessionId,
toolCallId: fact.toolCallId,
startedAtMs: fact.startedAtMs,
endedAtMs: fact.endedAtMs,
});
}
/**
* Stateful, per-ACP-run consumer. It never lists sessions or reads Vela
* storage. The first accepted root id is bound to this ACP run and every
* later fact must remain in that flat root/child graph.
*/
export function createVelaChildEvidenceConsumer(input: {
onFact?: (fact: VelaChildRuntimeFact) => void;
now?: () => number;
} = {}): VelaChildEvidenceConsumer {
const now = input.now ?? Date.now;
let negotiation = negotiateVelaChildEvidence(undefined);
let rootSessionId: string | undefined;
const childBindings = new Map<string, { rootSessionId: string; toolCallId: string; terminal?: string }>();
const toolBindings = new Map<string, string>();
const evidenceSignatures = new Map<string, string>();
const rejectionCounts = new Map<VelaChildEvidenceRejectionReason, number>();
/**
* Every rejected frame is a hole in the child graph, so the reason has to
* survive past the per-run diagnostic emit cap in the ACP session. Coverage
* reads these counts; without them a run that dropped a malformed or
* out-of-order child frame would be indistinguishable from a run that
* genuinely had no Child agents.
*/
function reject(
reason: VelaChildEvidenceRejectionReason,
): VelaChildEvidenceObserveResult {
rejectionCounts.set(reason, (rejectionCounts.get(reason) ?? 0) + 1);
return { handled: true, accepted: false, reason };
}
/**
* One limitation is recorded for every reason this ACP run could have missed
* a Child agent. `complete` is therefore reserved for a negotiated producer,
* a cleanly closed turn, no still-open child, and no rejected frame — the
* only state in which `knownChildCount === 0` is a real observation rather
* than a blind spot.
*/
function childEvidenceCoverage(
{ sessionComplete }: VelaChildEvidenceCoverageInput,
): ChildEvidenceCoverageV1 {
const limitations: string[] = [];
const diagnostics = new Map<string, number>();
const note = (code: string, count: number): void => {
limitations.push(`vela_${code}`);
diagnostics.set(code, (diagnostics.get(code) ?? 0) + count);
};
if (!negotiation.supported) {
// The common AMR case: the installed Vela never advertised the
// extension, so no child frame could ever be consumed. Claiming
// `complete` here would assert "this run provably had no Child agents"
// when the daemon simply had no producer to observe.
note(
negotiation.advertised
? 'child_evidence_schema_unsupported'
: 'child_evidence_capability_not_negotiated',
1,
);
}
const openChildCount = [...childBindings.values()]
.filter((binding) => !binding.terminal).length;
if (openChildCount > 0) note('child_terminal_unobserved', openChildCount);
if (!sessionComplete) note('child_stream_incomplete', 1);
for (const [reason, count] of rejectionCounts) {
note(`child_evidence_rejected_${reason}`, count);
}
const knownChildCount = childBindings.size;
const complete = limitations.length === 0;
return {
availability: complete
? 'complete'
: knownChildCount > 0
? 'partial'
: 'unavailable',
source: VELA_CHILD_EVIDENCE_COVERAGE_SOURCE,
knownChildCount,
explicitZero: complete && knownChildCount === 0,
limitations,
diagnosticCounts: [...diagnostics.entries()]
.sort(([left], [right]) => left.localeCompare(right))
.map(([code, count]) => ({ code, count })),
};
}
return {
negotiate(initializeResult) {
negotiation = negotiateVelaChildEvidence(initializeResult);
return negotiation;
},
getNegotiation() {
return negotiation;
},
childEvidenceCoverage,
observe({ expectedAcpSessionId, envelopeAcpSessionId, update }) {
if (!isRecord(update) || update.sessionUpdate !== 'child_agent_lifecycle') {
return { handled: false, accepted: false };
}
if (!negotiation.supported) {
return reject(
negotiation.advertised
? 'unsupported_schema_version'
: 'capability_not_negotiated',
);
}
if (
!expectedAcpSessionId ||
envelopeAcpSessionId !== expectedAcpSessionId
) {
return reject('acp_session_mismatch');
}
const parsedFact = parseWireFact(update, now);
if (!parsedFact) return reject('invalid_wire_shape');
const producerVersion = observableProducerVersion(negotiation.producerVersion);
const fact: VelaChildRuntimeFact = {
...parsedFact,
...(producerVersion ? { producerVersion } : {}),
};
if (fact.childSessionId === fact.rootSessionId) {
return reject('parent_cycle');
}
const evidenceSignature = factSignature(fact);
const knownEvidence = evidenceSignatures.get(fact.evidenceId);
if (knownEvidence) {
return knownEvidence === evidenceSignature
? { handled: true, accepted: false }
: reject('evidence_id_conflict');
}
const childBinding = childBindings.get(fact.childSessionId);
if (fact.phase === 'end' && !childBinding) {
return reject('status_regression');
}
if (childBinding?.rootSessionId !== undefined && childBinding.rootSessionId !== fact.rootSessionId) {
return reject('parent_conflict');
}
if (rootSessionId && fact.rootSessionId !== rootSessionId) {
return reject('root_session_conflict');
}
if (childBinding?.toolCallId !== undefined && childBinding.toolCallId !== fact.toolCallId) {
return reject('tool_call_rebound');
}
const toolChild = toolBindings.get(fact.toolCallId);
if (toolChild && toolChild !== fact.childSessionId) {
return reject('tool_call_rebound');
}
if (fact.phase === 'start' && childBinding) {
return reject('status_regression');
}
if (childBinding?.terminal) {
return reject(
childBinding.terminal === fact.state ? 'status_regression' : 'terminal_conflict',
);
}
rootSessionId ??= fact.rootSessionId;
childBindings.set(fact.childSessionId, {
rootSessionId: fact.rootSessionId,
toolCallId: fact.toolCallId,
...(fact.phase === 'end' ? { terminal: fact.state } : {}),
});
toolBindings.set(fact.toolCallId, fact.childSessionId);
evidenceSignatures.set(fact.evidenceId, evidenceSignature);
try {
input.onFact?.(fact);
} catch {
// Evidence is a best-effort side channel and cannot fail the parent Run.
}
return { handled: true, accepted: true, fact };
},
};
}
export interface AdaptVelaChildFactInput {
fact: VelaChildRuntimeFact;
agentCliVersion?: string;
runtimeCompanionVersion?: string;
taskExecutionId: string;
runId: string;
taskRunIndex: number;
taskRunObservationId: string;
stage: StrategyInputStageV2;
}
function normalizedUsage(
usage: VelaChildRuntimeFact['usage'],
): NormalizedAgentObservationV1['usage'] {
if (!usage) {
return {
availability: 'unavailable',
source: 'unknown',
accountingMode: 'unknown',
limitations: ['Vela child lifecycle did not report valid independent usage.'],
};
}
const values = {
...(usage.inputTokens === undefined ? {} : { inputTokens: usage.inputTokens }),
...(usage.outputTokens === undefined ? {} : { outputTokens: usage.outputTokens }),
...(usage.totalTokens === undefined ? {} : { totalTokens: usage.totalTokens }),
...(usage.thoughtTokens === undefined ? {} : { thoughtTokens: usage.thoughtTokens }),
...(usage.cacheReadTokens === undefined ? {} : { cacheReadTokens: usage.cacheReadTokens }),
...(usage.cacheWriteTokens === undefined ? {} : { cacheWriteTokens: usage.cacheWriteTokens }),
};
const valueSources = Object.fromEntries(
Object.keys(values).map((key) => [key, 'acp' as const]),
);
const complete = usage.completeness === 'complete' &&
usage.inputTokens !== undefined && usage.outputTokens !== undefined;
return {
availability: complete ? 'complete' : 'partial',
source: 'acp',
accountingMode: 'additive',
values,
valueSources,
limitations: complete
? []
: ['Vela reported only a partial independent child-usage snapshot.'],
};
}
/** Map one accepted, allowlisted wire fact without consulting Vela state. */
export function adaptVelaChildRuntimeFactV1(
input: AdaptVelaChildFactInput,
): NormalizedAgentObservationV1 {
const fact = input.fact;
if (
fact.adapterVersion !== VELA_CHILD_EVIDENCE_ADAPTER_VERSION ||
fact.schemaVersion !== VELA_CHILD_EVIDENCE_SCHEMA_VERSION
) {
throw new TypeError('Unsupported Vela child evidence adapter/schema version.');
}
const probedAgentCliVersion = safeProducerIdentity(input.agentCliVersion);
const producerVersion = observableProducerVersion(fact.producerVersion);
if (
probedAgentCliVersion &&
producerVersion &&
probedAgentCliVersion !== producerVersion
) {
throw new TypeError(
`Vela CLI version mismatch: probe=${probedAgentCliVersion} ACP=${producerVersion}`,
);
}
const agentCliVersion = producerVersion ?? probedAgentCliVersion;
const runtimeCompanionVersion = safeProducerIdentity(
input.runtimeCompanionVersion,
);
const prompt = fact.prompt
? {
availability: 'partial' as const,
source: 'acp' as const,
hash: fact.prompt.sha256,
bytes: fact.prompt.bytes,
...(fact.prompt.safePayload ? { safePayload: fact.prompt.safePayload } : {}),
limitations: fact.prompt.safePayload
? ['Vela Child Prompt text was re-redacted and bounded by Open Design.']
: ['Only Prompt hash and byte length are retained from Vela.'],
}
: {
availability: 'unavailable' as const,
source: 'unknown' as const,
limitations: ['Vela child Prompt identity was unavailable.'],
};
const terminal = fact.phase === 'end';
const timingEvidence = {
source: 'runtime' as const,
clockDomain: fact.timingEvidence === 'source_timestamp'
? 'opencode_unix_epoch_ms'
: 'vela_bridge_unix_epoch_ms',
startedAtMs: fact.startedAtMs,
...(terminal && fact.endedAtMs !== undefined
? {
endedAtMs: fact.endedAtMs,
durationMs: fact.endedAtMs - fact.startedAtMs,
}
: {}),
};
const status = fact.state === 'running'
? 'running'
: fact.state === 'cancelled'
? 'canceled'
: fact.state === 'timed_out'
? 'failed'
: fact.state;
const observationId = `vela-child:${input.runId}:${fact.childSessionId}`;
return NormalizedAgentObservationV1Schema.parse({
schema: NORMALIZED_AGENT_OBSERVATION_V1_SCHEMA,
identity: {
observationId,
taskExecutionId: input.taskExecutionId,
runId: input.runId,
taskRunIndex: input.taskRunIndex,
parentObservationId: input.taskRunObservationId,
runtimeSessionId: fact.childSessionId,
},
kind: 'child_agent',
stage: input.stage,
status,
prompt: {
hostComposed: {
availability: 'unobservable',
limitations: ['The daemon does not compose Vela native child Prompts.'],
},
childInjected: prompt,
agentEffectiveContext: {
availability: 'unobservable',
limitations: ['Vela does not expose effective child context in this extension.'],
},
},
usage: normalizedUsage(terminal ? fact.usage : undefined),
timing: {
availability: terminal ? 'complete' : 'partial',
evidence: [timingEvidence],
limitations: terminal
? []
: ['The child is running, so terminal timing is not available yet.'],
},
limitations: [
...fact.limitations,
...(fact.state === 'timed_out'
? ['Producer timed_out is normalized to failed; timeout identity remains in attributes.']
: []),
],
attributes: {
runtimeAdapterVersion: fact.adapterVersion,
...(agentCliVersion ? { agentCliVersion } : {}),
...(runtimeCompanionVersion ? { runtimeCompanionVersion } : {}),
wireSchemaVersion: fact.schemaVersion,
producerCandidateCommit: VELA_CHILD_EVIDENCE_CANDIDATE.commit,
producerCandidatePublished: false,
nativeEvidenceId: fact.evidenceId,
nativeRootSessionId: fact.rootSessionId,
nativeToolCallId: fact.toolCallId,
nativeStatus: fact.state,
timingEvidence: fact.timingEvidence,
lifecycleCompleteness: fact.lifecycleCompleteness,
sourceEvidence: fact.sourceEvidence,
evidenceLevel: fact.evidenceLevel,
l3Eligible: fact.l3Eligible,
...(fact.role ? { role: fact.role } : {}),
...(fact.provider ? { provider: fact.provider } : {}),
...(fact.model ? { model: fact.model } : {}),
...(fact.usage ? { producerUsageSource: fact.usage.source } : {}),
},
});
}