cool-workflow 0.1.78
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/.claude-plugin/plugin.json +20 -0
- package/.codex-plugin/mcp.json +10 -0
- package/.codex-plugin/plugin.json +38 -0
- package/.mcp.json +10 -0
- package/LICENSE +24 -0
- package/README.md +638 -0
- package/apps/architecture-review/app.json +51 -0
- package/apps/architecture-review/workflow.js +116 -0
- package/apps/end-to-end-golden-path/app.json +30 -0
- package/apps/end-to-end-golden-path/workflow.js +33 -0
- package/apps/pr-review-fix-ci/app.json +59 -0
- package/apps/pr-review-fix-ci/workflow.js +90 -0
- package/apps/release-cut/app.json +54 -0
- package/apps/release-cut/workflow.js +82 -0
- package/apps/research-synthesis/app.json +50 -0
- package/apps/research-synthesis/workflow.js +76 -0
- package/apps/workflow-app-framework-demo/app.json +29 -0
- package/apps/workflow-app-framework-demo/workflow.js +44 -0
- package/dist/agent-config.js +223 -0
- package/dist/candidate-scoring.js +715 -0
- package/dist/capability-core.js +630 -0
- package/dist/capability-dispatcher.js +86 -0
- package/dist/capability-registry.js +523 -0
- package/dist/cli.js +1276 -0
- package/dist/collaboration.js +727 -0
- package/dist/commit.js +570 -0
- package/dist/contract-migration.js +234 -0
- package/dist/coordinator.js +1163 -0
- package/dist/daemon.js +44 -0
- package/dist/dispatch.js +201 -0
- package/dist/drive.js +503 -0
- package/dist/error-feedback.js +415 -0
- package/dist/evidence-grounding.js +179 -0
- package/dist/evidence-reasoning.js +733 -0
- package/dist/execution-backend.js +1279 -0
- package/dist/harness.js +61 -0
- package/dist/mcp-server.js +1615 -0
- package/dist/multi-agent-eval.js +857 -0
- package/dist/multi-agent-host.js +764 -0
- package/dist/multi-agent-operator-ux.js +537 -0
- package/dist/multi-agent-trust.js +366 -0
- package/dist/multi-agent.js +1173 -0
- package/dist/node-snapshot.js +270 -0
- package/dist/observability.js +922 -0
- package/dist/operator-ux.js +971 -0
- package/dist/orchestrator/audit-operations.js +182 -0
- package/dist/orchestrator/candidate-operations.js +117 -0
- package/dist/orchestrator/cli-options.js +288 -0
- package/dist/orchestrator/collaboration-operations.js +86 -0
- package/dist/orchestrator/feedback-operations.js +81 -0
- package/dist/orchestrator/host-operations.js +78 -0
- package/dist/orchestrator/lifecycle-operations.js +462 -0
- package/dist/orchestrator/migration-operations.js +44 -0
- package/dist/orchestrator/multi-agent-operations.js +362 -0
- package/dist/orchestrator/report.js +369 -0
- package/dist/orchestrator/topology-operations.js +84 -0
- package/dist/orchestrator.js +874 -0
- package/dist/pipeline-contract.js +92 -0
- package/dist/pipeline-runner.js +285 -0
- package/dist/reclamation.js +882 -0
- package/dist/result-normalize.js +194 -0
- package/dist/run-export.js +64 -0
- package/dist/run-registry.js +1347 -0
- package/dist/run-state-schema.js +67 -0
- package/dist/sandbox-profile.js +471 -0
- package/dist/scheduler.js +266 -0
- package/dist/scheduling.js +184 -0
- package/dist/schema-validate.js +98 -0
- package/dist/state-explosion.js +1213 -0
- package/dist/state-migrations.js +463 -0
- package/dist/state-node.js +301 -0
- package/dist/state.js +308 -0
- package/dist/telemetry-attestation.js +156 -0
- package/dist/telemetry-ledger.js +145 -0
- package/dist/topology.js +527 -0
- package/dist/triggers.js +159 -0
- package/dist/trust-audit.js +475 -0
- package/dist/types/blackboard.js +2 -0
- package/dist/types/boundary.js +29 -0
- package/dist/types/candidate.js +2 -0
- package/dist/types/collaboration.js +2 -0
- package/dist/types/core.js +2 -0
- package/dist/types/drive.js +10 -0
- package/dist/types/error-feedback.js +2 -0
- package/dist/types/evidence-reasoning.js +2 -0
- package/dist/types/execution-backend.js +2 -0
- package/dist/types/multi-agent.js +2 -0
- package/dist/types/observability.js +2 -0
- package/dist/types/pipeline.js +2 -0
- package/dist/types/reclamation.js +8 -0
- package/dist/types/result.js +2 -0
- package/dist/types/run-registry.js +2 -0
- package/dist/types/run.js +2 -0
- package/dist/types/sandbox.js +2 -0
- package/dist/types/schedule.js +2 -0
- package/dist/types/state-node.js +2 -0
- package/dist/types/topology.js +2 -0
- package/dist/types/trust.js +2 -0
- package/dist/types/workbench.js +2 -0
- package/dist/types/worker.js +2 -0
- package/dist/types/workflow-app.js +2 -0
- package/dist/types.js +43 -0
- package/dist/verifier-registry.js +46 -0
- package/dist/verifier.js +78 -0
- package/dist/version.js +8 -0
- package/dist/workbench-host.js +172 -0
- package/dist/workbench.js +190 -0
- package/dist/worker-isolation.js +1028 -0
- package/dist/workflow-api.js +98 -0
- package/dist/workflow-app-framework.js +626 -0
- package/docs/agent-delegation-drive.7.md +190 -0
- package/docs/agent-framework.md +176 -0
- package/docs/candidate-scoring.7.md +106 -0
- package/docs/canonical-workflow-apps.7.md +137 -0
- package/docs/capability-topology-registry.7.md +168 -0
- package/docs/cli-mcp-parity.7.md +373 -0
- package/docs/contract-migration-tooling.7.md +123 -0
- package/docs/control-plane-scheduling.7.md +110 -0
- package/docs/coordinator-blackboard.7.md +183 -0
- package/docs/dogfood/architecture-review-cool-workflow.md +16 -0
- package/docs/dogfood-one-real-repo.7.md +168 -0
- package/docs/durable-state-and-locking.7.md +107 -0
- package/docs/end-to-end-golden-path.7.md +117 -0
- package/docs/error-feedback.7.md +153 -0
- package/docs/evidence-adoption-reasoning-chain.7.md +270 -0
- package/docs/execution-backends.7.md +300 -0
- package/docs/getting-started.md +99 -0
- package/docs/index.md +41 -0
- package/docs/mcp-app-surface.7.md +235 -0
- package/docs/multi-agent-cli-mcp-surface.7.md +265 -0
- package/docs/multi-agent-eval-replay-harness.7.md +302 -0
- package/docs/multi-agent-operator-ux.7.md +314 -0
- package/docs/multi-agent-runtime-core.7.md +231 -0
- package/docs/multi-agent-topologies.7.md +103 -0
- package/docs/multi-agent-trust-policy-audit.7.md +154 -0
- package/docs/node-snapshot-diff-replay.7.md +135 -0
- package/docs/observability-cost-accounting.7.md +194 -0
- package/docs/operator-ux.7.md +180 -0
- package/docs/pipeline-runner.7.md +136 -0
- package/docs/project-index.md +261 -0
- package/docs/real-execution-backends.7.md +142 -0
- package/docs/release-and-migration.7.md +280 -0
- package/docs/release-tooling.7.md +159 -0
- package/docs/routines.md +48 -0
- package/docs/run-registry-control-plane.7.md +312 -0
- package/docs/run-retention-reclamation.7.md +191 -0
- package/docs/sandbox-profiles.7.md +137 -0
- package/docs/scheduled-tasks.md +80 -0
- package/docs/security-trust-hardening.7.md +117 -0
- package/docs/state-explosion-management.7.md +264 -0
- package/docs/state-node.7.md +96 -0
- package/docs/team-collaboration.7.md +207 -0
- package/docs/unix-principles.md +192 -0
- package/docs/verifier-gated-commit.7.md +140 -0
- package/docs/web-desktop-workbench.7.md +215 -0
- package/docs/worker-isolation.7.md +167 -0
- package/docs/workflow-app-framework.7.md +274 -0
- package/manifest/README.md +43 -0
- package/manifest/plugin.manifest.json +316 -0
- package/manifest/pricing.policy.json +14 -0
- package/package.json +79 -0
- package/scripts/agents/claude-p-agent.js +104 -0
- package/scripts/agents/claude-p-agent.sh +9 -0
- package/scripts/agents/cw-attest-keygen.js +55 -0
- package/scripts/agents/cw-attest-wrap.js +143 -0
- package/scripts/block-unapproved-tag.sh +39 -0
- package/scripts/bump-version.js +249 -0
- package/scripts/canonical-apps.js +171 -0
- package/scripts/cw.js +4 -0
- package/scripts/dist-drift-check.js +79 -0
- package/scripts/dogfood-architecture-review.js +237 -0
- package/scripts/dogfood-release.js +624 -0
- package/scripts/forward-ref-docs.js +73 -0
- package/scripts/gen-manifests.js +232 -0
- package/scripts/golden-path.js +300 -0
- package/scripts/mcp-server.js +4 -0
- package/scripts/new-feature.js +121 -0
- package/scripts/parity-check.js +213 -0
- package/scripts/release-check.js +118 -0
- package/scripts/release-flow.js +272 -0
- package/scripts/release-gate.sh +85 -0
- package/scripts/sync-project-index.js +387 -0
- package/scripts/validate-run-state-schema.js +126 -0
- package/scripts/verify-container-selfref.js +64 -0
- package/scripts/version-sync-check.js +237 -0
- package/skills/cool-workflow/SKILL.md +162 -0
- package/skills/cool-workflow/references/commands.md +282 -0
- package/tsconfig.json +16 -0
- package/ui/workbench/app.css +76 -0
- package/ui/workbench/app.js +159 -0
- package/ui/workbench/index.html +32 -0
- package/workflows/architecture-review.workflow.js +84 -0
- package/workflows/research-synthesis.workflow.js +47 -0
|
@@ -0,0 +1,1173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.MULTI_AGENT_SCHEMA_VERSION = void 0;
|
|
7
|
+
exports.ensureMultiAgentState = ensureMultiAgentState;
|
|
8
|
+
exports.persistMultiAgentState = persistMultiAgentState;
|
|
9
|
+
exports.createMultiAgentRun = createMultiAgentRun;
|
|
10
|
+
exports.transitionMultiAgentRun = transitionMultiAgentRun;
|
|
11
|
+
exports.createAgentRole = createAgentRole;
|
|
12
|
+
exports.createAgentGroup = createAgentGroup;
|
|
13
|
+
exports.assignAgentMembership = assignAgentMembership;
|
|
14
|
+
exports.createAgentFanout = createAgentFanout;
|
|
15
|
+
exports.attachDispatchToMultiAgent = attachDispatchToMultiAgent;
|
|
16
|
+
exports.collectAgentFanin = collectAgentFanin;
|
|
17
|
+
exports.recordMultiAgentWorkerOutput = recordMultiAgentWorkerOutput;
|
|
18
|
+
exports.summarizeMultiAgent = summarizeMultiAgent;
|
|
19
|
+
exports.buildMultiAgentGraph = buildMultiAgentGraph;
|
|
20
|
+
exports.getMultiAgentRun = getMultiAgentRun;
|
|
21
|
+
exports.getAgentRole = getAgentRole;
|
|
22
|
+
exports.getAgentGroup = getAgentGroup;
|
|
23
|
+
exports.getAgentMembership = getAgentMembership;
|
|
24
|
+
exports.getAgentFanout = getAgentFanout;
|
|
25
|
+
exports.getAgentFanin = getAgentFanin;
|
|
26
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
27
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
28
|
+
const state_1 = require("./state");
|
|
29
|
+
const pipeline_contract_1 = require("./pipeline-contract");
|
|
30
|
+
const state_node_1 = require("./state-node");
|
|
31
|
+
const trust_audit_1 = require("./trust-audit");
|
|
32
|
+
const multi_agent_trust_1 = require("./multi-agent-trust");
|
|
33
|
+
exports.MULTI_AGENT_SCHEMA_VERSION = 1;
|
|
34
|
+
function ensureMultiAgentState(run) {
|
|
35
|
+
run.paths.multiAgentDir = multiAgentRoot(run);
|
|
36
|
+
node_fs_1.default.mkdirSync(run.paths.multiAgentDir, { recursive: true });
|
|
37
|
+
for (const dir of ["runs", "roles", "groups", "memberships", "fanouts", "fanins"]) {
|
|
38
|
+
node_fs_1.default.mkdirSync(node_path_1.default.join(run.paths.multiAgentDir, dir), { recursive: true });
|
|
39
|
+
}
|
|
40
|
+
if (!run.multiAgent) {
|
|
41
|
+
run.multiAgent = {
|
|
42
|
+
schemaVersion: exports.MULTI_AGENT_SCHEMA_VERSION,
|
|
43
|
+
runs: [],
|
|
44
|
+
roles: [],
|
|
45
|
+
groups: [],
|
|
46
|
+
memberships: [],
|
|
47
|
+
fanouts: [],
|
|
48
|
+
fanins: []
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
run.multiAgent.schemaVersion = exports.MULTI_AGENT_SCHEMA_VERSION;
|
|
52
|
+
run.multiAgent.runs = run.multiAgent.runs || [];
|
|
53
|
+
run.multiAgent.roles = run.multiAgent.roles || [];
|
|
54
|
+
run.multiAgent.groups = run.multiAgent.groups || [];
|
|
55
|
+
run.multiAgent.memberships = run.multiAgent.memberships || [];
|
|
56
|
+
run.multiAgent.fanouts = run.multiAgent.fanouts || [];
|
|
57
|
+
run.multiAgent.fanins = run.multiAgent.fanins || [];
|
|
58
|
+
return run.multiAgent;
|
|
59
|
+
}
|
|
60
|
+
function persistMultiAgentState(run) {
|
|
61
|
+
const state = ensureMultiAgentState(run);
|
|
62
|
+
const root = multiAgentRoot(run);
|
|
63
|
+
assertNoRecordPathCollisions("MultiAgentRun", state.runs);
|
|
64
|
+
assertNoRecordPathCollisions("AgentRole", state.roles);
|
|
65
|
+
assertNoRecordPathCollisions("AgentGroup", state.groups);
|
|
66
|
+
assertNoRecordPathCollisions("AgentMembership", state.memberships);
|
|
67
|
+
assertNoRecordPathCollisions("AgentFanout", state.fanouts);
|
|
68
|
+
assertNoRecordPathCollisions("AgentFanin", state.fanins);
|
|
69
|
+
(0, state_1.writeJson)(node_path_1.default.join(root, "index.json"), {
|
|
70
|
+
schemaVersion: exports.MULTI_AGENT_SCHEMA_VERSION,
|
|
71
|
+
runId: run.id,
|
|
72
|
+
counts: {
|
|
73
|
+
runs: state.runs.length,
|
|
74
|
+
roles: state.roles.length,
|
|
75
|
+
groups: state.groups.length,
|
|
76
|
+
memberships: state.memberships.length,
|
|
77
|
+
fanouts: state.fanouts.length,
|
|
78
|
+
fanins: state.fanins.length
|
|
79
|
+
},
|
|
80
|
+
runs: state.runs.map(indexRow),
|
|
81
|
+
roles: state.roles.map(indexRow),
|
|
82
|
+
groups: state.groups.map(indexRow),
|
|
83
|
+
memberships: state.memberships.map(indexRow),
|
|
84
|
+
fanouts: state.fanouts.map(indexRow),
|
|
85
|
+
fanins: state.fanins.map(indexRow)
|
|
86
|
+
});
|
|
87
|
+
for (const record of state.runs)
|
|
88
|
+
writeRecord(run, "runs", record);
|
|
89
|
+
for (const record of state.roles)
|
|
90
|
+
writeRecord(run, "roles", record);
|
|
91
|
+
for (const record of state.groups)
|
|
92
|
+
writeRecord(run, "groups", record);
|
|
93
|
+
for (const record of state.memberships)
|
|
94
|
+
writeRecord(run, "memberships", record);
|
|
95
|
+
for (const record of state.fanouts)
|
|
96
|
+
writeRecord(run, "fanouts", record);
|
|
97
|
+
for (const record of state.fanins)
|
|
98
|
+
writeRecord(run, "fanins", record);
|
|
99
|
+
}
|
|
100
|
+
function createMultiAgentRun(run, input = {}) {
|
|
101
|
+
const state = ensureMultiAgentState(run);
|
|
102
|
+
const id = input.id || createId("mar");
|
|
103
|
+
if (state.runs.some((record) => record.id === id))
|
|
104
|
+
throw new Error(`Duplicate MultiAgentRun id: ${id}`);
|
|
105
|
+
const now = new Date().toISOString();
|
|
106
|
+
const status = input.status || "planned";
|
|
107
|
+
const record = {
|
|
108
|
+
schemaVersion: exports.MULTI_AGENT_SCHEMA_VERSION,
|
|
109
|
+
id,
|
|
110
|
+
runId: run.id,
|
|
111
|
+
createdAt: now,
|
|
112
|
+
updatedAt: now,
|
|
113
|
+
status,
|
|
114
|
+
title: input.title || id,
|
|
115
|
+
objective: input.objective,
|
|
116
|
+
parentMultiAgentRunId: input.parentMultiAgentRunId,
|
|
117
|
+
childMultiAgentRunIds: [],
|
|
118
|
+
roleIds: [],
|
|
119
|
+
groupIds: [],
|
|
120
|
+
fanoutIds: [],
|
|
121
|
+
faninIds: [],
|
|
122
|
+
blackboardId: input.blackboardId,
|
|
123
|
+
topicIds: unique(input.topicIds || []),
|
|
124
|
+
lifecycle: [lifecycleEvent(undefined, status, "created")],
|
|
125
|
+
links: {
|
|
126
|
+
workflowRunId: run.id,
|
|
127
|
+
phase: input.phase,
|
|
128
|
+
phaseId: input.phaseId,
|
|
129
|
+
blackboardId: input.blackboardId,
|
|
130
|
+
blackboardTopicIds: unique(input.topicIds || [])
|
|
131
|
+
},
|
|
132
|
+
policy: {
|
|
133
|
+
schemaVersion: 1,
|
|
134
|
+
id: `${id}-policy`,
|
|
135
|
+
policyRef: `multiAgent.runs.${id}.policy`,
|
|
136
|
+
subjectKind: "multi-agent-run",
|
|
137
|
+
subjectId: id,
|
|
138
|
+
allowedBlackboardTopicIds: unique(input.topicIds || ["*"]),
|
|
139
|
+
allowedWriteOperations: ["message", "context", "artifact", "snapshot", "topic", "coordinator-decision"],
|
|
140
|
+
allowedCandidateOperations: ["register", "score", "select"],
|
|
141
|
+
allowedJudgeOperations: ["verdict", "rationale", "panel-decision"],
|
|
142
|
+
sandboxProfileHints: [],
|
|
143
|
+
requiredEvidenceRefs: [],
|
|
144
|
+
deniedOperations: [],
|
|
145
|
+
metadata: { title: input.title }
|
|
146
|
+
},
|
|
147
|
+
metadata: compact(input.metadata)
|
|
148
|
+
};
|
|
149
|
+
if (record.parentMultiAgentRunId) {
|
|
150
|
+
const parent = requireMultiAgentRun(run, record.parentMultiAgentRunId);
|
|
151
|
+
parent.childMultiAgentRunIds = unique([...parent.childMultiAgentRunIds, record.id]);
|
|
152
|
+
touch(parent);
|
|
153
|
+
}
|
|
154
|
+
state.runs.push(record);
|
|
155
|
+
appendMultiAgentNode(run, "multi-agent-run", record.id, statusToNodeStatus(status), {
|
|
156
|
+
title: record.title,
|
|
157
|
+
objective: record.objective,
|
|
158
|
+
phase: record.links.phase
|
|
159
|
+
});
|
|
160
|
+
(0, trust_audit_1.recordTrustAuditEvent)(run, {
|
|
161
|
+
kind: "multi-agent.run",
|
|
162
|
+
decision: "recorded",
|
|
163
|
+
source: "runtime-derived",
|
|
164
|
+
multiAgentRunId: record.id,
|
|
165
|
+
metadata: { status: record.status, objective: record.objective }
|
|
166
|
+
});
|
|
167
|
+
persistMultiAgentState(run);
|
|
168
|
+
return record;
|
|
169
|
+
}
|
|
170
|
+
function transitionMultiAgentRun(run, multiAgentRunId, status, options = {}) {
|
|
171
|
+
ensureMultiAgentState(run);
|
|
172
|
+
const record = requireMultiAgentRun(run, multiAgentRunId);
|
|
173
|
+
assertLifecycleTransition(record.status, status);
|
|
174
|
+
if (status === "completed")
|
|
175
|
+
assertMultiAgentRunCompletionReady(run, record);
|
|
176
|
+
const before = record.status;
|
|
177
|
+
record.status = status;
|
|
178
|
+
record.updatedAt = new Date().toISOString();
|
|
179
|
+
record.lifecycle.push(lifecycleEvent(before, status, options.reason, options.actor, options.metadata));
|
|
180
|
+
if (status === "completed")
|
|
181
|
+
completeOwnedMultiAgentRecords(run, record, options.reason);
|
|
182
|
+
appendMultiAgentNode(run, "multi-agent-run", record.id, statusToNodeStatus(status), {
|
|
183
|
+
status,
|
|
184
|
+
reason: options.reason
|
|
185
|
+
});
|
|
186
|
+
(0, trust_audit_1.recordTrustAuditEvent)(run, {
|
|
187
|
+
kind: "multi-agent.lifecycle",
|
|
188
|
+
decision: status === "failed" ? "failed" : "validated",
|
|
189
|
+
source: "cw-validated",
|
|
190
|
+
multiAgentRunId: record.id,
|
|
191
|
+
metadata: { from: before, to: status, reason: options.reason }
|
|
192
|
+
});
|
|
193
|
+
persistMultiAgentState(run);
|
|
194
|
+
return record;
|
|
195
|
+
}
|
|
196
|
+
function assertMultiAgentRunCompletionReady(run, multiAgentRun) {
|
|
197
|
+
const state = ensureMultiAgentState(run);
|
|
198
|
+
const groups = state.groups.filter((record) => record.multiAgentRunId === multiAgentRun.id);
|
|
199
|
+
const fanins = state.fanins.filter((record) => record.multiAgentRunId === multiAgentRun.id);
|
|
200
|
+
const blocked = fanins.flatMap((fanin) => {
|
|
201
|
+
const reasons = [...fanin.blockedReasons];
|
|
202
|
+
if (fanin.status === "blocked" || fanin.status === "failed")
|
|
203
|
+
reasons.push(`fanin ${fanin.id} status is ${fanin.status}`);
|
|
204
|
+
if (!fanin.verifierReady)
|
|
205
|
+
reasons.push(`fanin ${fanin.id} is not verifier-ready`);
|
|
206
|
+
return reasons.map((reason) => `${fanin.id}: ${reason}`);
|
|
207
|
+
});
|
|
208
|
+
for (const group of groups) {
|
|
209
|
+
if ((group.membershipIds.length || group.fanoutIds.length) && !group.faninIds.length) {
|
|
210
|
+
blocked.push(`group ${group.id} has no fanin record`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (blocked.length) {
|
|
214
|
+
throw new Error(`Cannot complete MultiAgentRun ${multiAgentRun.id}: ${blocked.join("; ")}`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
function completeOwnedMultiAgentRecords(run, multiAgentRun, reason) {
|
|
218
|
+
const state = ensureMultiAgentState(run);
|
|
219
|
+
for (const role of state.roles.filter((record) => record.multiAgentRunId === multiAgentRun.id)) {
|
|
220
|
+
if (role.status === "completed" || role.status === "cancelled")
|
|
221
|
+
continue;
|
|
222
|
+
const before = role.status;
|
|
223
|
+
role.status = "completed";
|
|
224
|
+
role.updatedAt = multiAgentRun.updatedAt;
|
|
225
|
+
role.lifecycle.push(lifecycleEvent(before, "completed", reason || "multi-agent run completed"));
|
|
226
|
+
}
|
|
227
|
+
for (const group of state.groups.filter((record) => record.multiAgentRunId === multiAgentRun.id)) {
|
|
228
|
+
if (group.status === "completed" || group.status === "failed" || group.status === "cancelled")
|
|
229
|
+
continue;
|
|
230
|
+
const before = group.status;
|
|
231
|
+
group.status = "completed";
|
|
232
|
+
group.updatedAt = multiAgentRun.updatedAt;
|
|
233
|
+
group.lifecycle.push(lifecycleEvent(before, "completed", reason || "multi-agent run completed"));
|
|
234
|
+
}
|
|
235
|
+
for (const fanout of state.fanouts.filter((record) => record.multiAgentRunId === multiAgentRun.id)) {
|
|
236
|
+
if (fanout.status === "completed" || fanout.status === "failed" || fanout.status === "cancelled")
|
|
237
|
+
continue;
|
|
238
|
+
const before = fanout.status;
|
|
239
|
+
fanout.status = "completed";
|
|
240
|
+
fanout.updatedAt = multiAgentRun.updatedAt;
|
|
241
|
+
fanout.lifecycle.push(lifecycleEvent(before, "completed", reason || "multi-agent run completed"));
|
|
242
|
+
}
|
|
243
|
+
for (const fanin of state.fanins.filter((record) => record.multiAgentRunId === multiAgentRun.id)) {
|
|
244
|
+
if (fanin.status === "completed" || fanin.status === "failed")
|
|
245
|
+
continue;
|
|
246
|
+
const before = fanin.status;
|
|
247
|
+
fanin.status = "completed";
|
|
248
|
+
fanin.updatedAt = multiAgentRun.updatedAt;
|
|
249
|
+
fanin.lifecycle.push(lifecycleEvent(before, "completed", reason || "multi-agent run completed"));
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
function createAgentRole(run, input) {
|
|
253
|
+
const state = ensureMultiAgentState(run);
|
|
254
|
+
const multiAgentRun = requireMultiAgentRun(run, input.multiAgentRunId);
|
|
255
|
+
const id = input.id || createId("role");
|
|
256
|
+
if (state.roles.some((record) => record.id === id))
|
|
257
|
+
throw new Error(`Duplicate AgentRole id: ${id}`);
|
|
258
|
+
if (input.parentRoleId)
|
|
259
|
+
requireAgentRole(run, input.parentRoleId);
|
|
260
|
+
const now = new Date().toISOString();
|
|
261
|
+
const role = {
|
|
262
|
+
schemaVersion: exports.MULTI_AGENT_SCHEMA_VERSION,
|
|
263
|
+
id,
|
|
264
|
+
runId: run.id,
|
|
265
|
+
multiAgentRunId: multiAgentRun.id,
|
|
266
|
+
createdAt: now,
|
|
267
|
+
updatedAt: now,
|
|
268
|
+
status: "planned",
|
|
269
|
+
title: input.title || id,
|
|
270
|
+
responsibilities: input.responsibilities || [],
|
|
271
|
+
requiredEvidence: input.requiredEvidence || [],
|
|
272
|
+
sandboxProfileHints: input.sandboxProfileHints || [],
|
|
273
|
+
expectedArtifacts: input.expectedArtifacts || [],
|
|
274
|
+
faninObligations: input.faninObligations || [],
|
|
275
|
+
blackboardId: input.blackboardId || multiAgentRun.blackboardId,
|
|
276
|
+
topicIds: unique([...(multiAgentRun.topicIds || []), ...(input.topicIds || [])]),
|
|
277
|
+
lifecycle: [lifecycleEvent(undefined, "planned", "created")],
|
|
278
|
+
parentRoleId: input.parentRoleId,
|
|
279
|
+
childRoleIds: [],
|
|
280
|
+
policy: undefined,
|
|
281
|
+
metadata: compact(input.metadata)
|
|
282
|
+
};
|
|
283
|
+
role.policy = (0, multi_agent_trust_1.policyForRole)(role);
|
|
284
|
+
if (role.parentRoleId) {
|
|
285
|
+
const parent = requireAgentRole(run, role.parentRoleId);
|
|
286
|
+
parent.childRoleIds = unique([...parent.childRoleIds, role.id]);
|
|
287
|
+
touch(parent);
|
|
288
|
+
}
|
|
289
|
+
state.roles.push(role);
|
|
290
|
+
multiAgentRun.roleIds = unique([...multiAgentRun.roleIds, role.id]);
|
|
291
|
+
touch(multiAgentRun);
|
|
292
|
+
appendMultiAgentNode(run, "agent-role", role.id, "pending", {
|
|
293
|
+
multiAgentRunId: role.multiAgentRunId,
|
|
294
|
+
title: role.title,
|
|
295
|
+
responsibilities: role.responsibilities,
|
|
296
|
+
requiredEvidence: role.requiredEvidence
|
|
297
|
+
}, [`${run.id}:multi-agent:${role.multiAgentRunId}`]);
|
|
298
|
+
(0, trust_audit_1.recordTrustAuditEvent)(run, {
|
|
299
|
+
kind: "multi-agent.role",
|
|
300
|
+
decision: "recorded",
|
|
301
|
+
source: "runtime-derived",
|
|
302
|
+
multiAgentRunId: role.multiAgentRunId,
|
|
303
|
+
agentRoleId: role.id,
|
|
304
|
+
metadata: {
|
|
305
|
+
responsibilities: role.responsibilities,
|
|
306
|
+
requiredEvidence: role.requiredEvidence,
|
|
307
|
+
sandboxProfileHints: role.sandboxProfileHints,
|
|
308
|
+
faninObligations: role.faninObligations
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
(0, multi_agent_trust_1.recordRolePolicyAudit)(run, role);
|
|
312
|
+
persistMultiAgentState(run);
|
|
313
|
+
return role;
|
|
314
|
+
}
|
|
315
|
+
function createAgentGroup(run, input) {
|
|
316
|
+
const state = ensureMultiAgentState(run);
|
|
317
|
+
const multiAgentRun = requireMultiAgentRun(run, input.multiAgentRunId);
|
|
318
|
+
const id = input.id || createId("group");
|
|
319
|
+
if (state.groups.some((record) => record.id === id))
|
|
320
|
+
throw new Error(`Duplicate AgentGroup id: ${id}`);
|
|
321
|
+
if (input.parentGroupId)
|
|
322
|
+
requireAgentGroup(run, input.parentGroupId);
|
|
323
|
+
for (const taskId of input.taskIds || [])
|
|
324
|
+
requireRunTask(run, taskId);
|
|
325
|
+
const now = new Date().toISOString();
|
|
326
|
+
const group = {
|
|
327
|
+
schemaVersion: exports.MULTI_AGENT_SCHEMA_VERSION,
|
|
328
|
+
id,
|
|
329
|
+
runId: run.id,
|
|
330
|
+
multiAgentRunId: multiAgentRun.id,
|
|
331
|
+
createdAt: now,
|
|
332
|
+
updatedAt: now,
|
|
333
|
+
status: "forming",
|
|
334
|
+
title: input.title || id,
|
|
335
|
+
phase: input.phase,
|
|
336
|
+
phaseId: input.phaseId,
|
|
337
|
+
taskIds: unique(input.taskIds || []),
|
|
338
|
+
roleIds: [],
|
|
339
|
+
membershipIds: [],
|
|
340
|
+
workerIds: [],
|
|
341
|
+
fanoutIds: [],
|
|
342
|
+
faninIds: [],
|
|
343
|
+
blackboardId: input.blackboardId || multiAgentRun.blackboardId,
|
|
344
|
+
topicIds: unique([...(multiAgentRun.topicIds || []), ...(input.topicIds || [])]),
|
|
345
|
+
lifecycle: [lifecycleEvent(undefined, "forming", "created")],
|
|
346
|
+
parentGroupId: input.parentGroupId,
|
|
347
|
+
childGroupIds: [],
|
|
348
|
+
policy: undefined,
|
|
349
|
+
metadata: compact(input.metadata)
|
|
350
|
+
};
|
|
351
|
+
group.policy = (0, multi_agent_trust_1.policyForGroup)(group);
|
|
352
|
+
if (group.parentGroupId) {
|
|
353
|
+
const parent = requireAgentGroup(run, group.parentGroupId);
|
|
354
|
+
parent.childGroupIds = unique([...parent.childGroupIds, group.id]);
|
|
355
|
+
touch(parent);
|
|
356
|
+
}
|
|
357
|
+
state.groups.push(group);
|
|
358
|
+
multiAgentRun.groupIds = unique([...multiAgentRun.groupIds, group.id]);
|
|
359
|
+
touch(multiAgentRun);
|
|
360
|
+
appendMultiAgentNode(run, "agent-group", group.id, "running", {
|
|
361
|
+
multiAgentRunId: group.multiAgentRunId,
|
|
362
|
+
phase: group.phase,
|
|
363
|
+
taskIds: group.taskIds
|
|
364
|
+
}, [`${run.id}:multi-agent:${group.multiAgentRunId}`]);
|
|
365
|
+
(0, trust_audit_1.recordTrustAuditEvent)(run, {
|
|
366
|
+
kind: "multi-agent.group",
|
|
367
|
+
decision: "recorded",
|
|
368
|
+
source: "runtime-derived",
|
|
369
|
+
multiAgentRunId: group.multiAgentRunId,
|
|
370
|
+
agentGroupId: group.id,
|
|
371
|
+
metadata: { phase: group.phase, taskIds: group.taskIds }
|
|
372
|
+
});
|
|
373
|
+
persistMultiAgentState(run);
|
|
374
|
+
return group;
|
|
375
|
+
}
|
|
376
|
+
function assignAgentMembership(run, input) {
|
|
377
|
+
const state = ensureMultiAgentState(run);
|
|
378
|
+
const group = requireAgentGroup(run, input.groupId);
|
|
379
|
+
const role = requireAgentRole(run, input.roleId);
|
|
380
|
+
if (role.multiAgentRunId !== group.multiAgentRunId) {
|
|
381
|
+
throw new Error(`AgentRole ${role.id} belongs to ${role.multiAgentRunId}, not group run ${group.multiAgentRunId}`);
|
|
382
|
+
}
|
|
383
|
+
if (input.multiAgentRunId && input.multiAgentRunId !== group.multiAgentRunId) {
|
|
384
|
+
throw new Error(`Membership multiAgentRunId ${input.multiAgentRunId} does not match group ${group.id}`);
|
|
385
|
+
}
|
|
386
|
+
const task = requireRunTask(run, input.taskId);
|
|
387
|
+
if (input.workerId && !(run.workers || []).some((worker) => worker.id === input.workerId)) {
|
|
388
|
+
throw new Error(`Unknown worker id for membership: ${input.workerId}`);
|
|
389
|
+
}
|
|
390
|
+
const duplicate = state.memberships.find((membership) => membership.groupId === group.id &&
|
|
391
|
+
membership.roleId === role.id &&
|
|
392
|
+
membership.taskId === task.id &&
|
|
393
|
+
(input.workerId ? membership.workerId === input.workerId : !membership.workerId));
|
|
394
|
+
if (duplicate) {
|
|
395
|
+
throw new Error(`Duplicate AgentMembership for group=${group.id}, role=${role.id}, task=${task.id}, worker=${input.workerId || "none"}`);
|
|
396
|
+
}
|
|
397
|
+
const id = input.id || createId("membership");
|
|
398
|
+
if (state.memberships.some((record) => record.id === id))
|
|
399
|
+
throw new Error(`Duplicate AgentMembership id: ${id}`);
|
|
400
|
+
const now = new Date().toISOString();
|
|
401
|
+
const status = input.status || (input.workerId ? "running" : "assigned");
|
|
402
|
+
const membership = {
|
|
403
|
+
schemaVersion: exports.MULTI_AGENT_SCHEMA_VERSION,
|
|
404
|
+
id,
|
|
405
|
+
runId: run.id,
|
|
406
|
+
multiAgentRunId: group.multiAgentRunId,
|
|
407
|
+
groupId: group.id,
|
|
408
|
+
roleId: role.id,
|
|
409
|
+
taskId: task.id,
|
|
410
|
+
workerId: input.workerId,
|
|
411
|
+
dispatchId: input.dispatchId,
|
|
412
|
+
fanoutId: input.fanoutId,
|
|
413
|
+
createdAt: now,
|
|
414
|
+
updatedAt: now,
|
|
415
|
+
status,
|
|
416
|
+
lifecycle: [lifecycleEvent(undefined, status, "assigned")],
|
|
417
|
+
evidenceRefs: [],
|
|
418
|
+
artifactPaths: [],
|
|
419
|
+
blackboardId: input.blackboardId || group.blackboardId || role.blackboardId,
|
|
420
|
+
topicIds: unique([...(group.topicIds || []), ...(role.topicIds || []), ...(input.topicIds || [])]),
|
|
421
|
+
blackboardMessageIds: [],
|
|
422
|
+
blackboardArtifactRefIds: [],
|
|
423
|
+
policy: undefined,
|
|
424
|
+
metadata: compact(input.metadata)
|
|
425
|
+
};
|
|
426
|
+
membership.policy = (0, multi_agent_trust_1.policyForMembership)(membership, role);
|
|
427
|
+
state.memberships.push(membership);
|
|
428
|
+
group.membershipIds = unique([...group.membershipIds, membership.id]);
|
|
429
|
+
group.roleIds = unique([...group.roleIds, role.id]);
|
|
430
|
+
group.taskIds = unique([...group.taskIds, task.id]);
|
|
431
|
+
if (membership.workerId)
|
|
432
|
+
group.workerIds = unique([...group.workerIds, membership.workerId]);
|
|
433
|
+
touch(group);
|
|
434
|
+
const roleStatusBefore = role.status;
|
|
435
|
+
role.status = "active";
|
|
436
|
+
role.updatedAt = now;
|
|
437
|
+
role.lifecycle.push(lifecycleEvent(roleStatusBefore, "active", "membership assigned"));
|
|
438
|
+
if (membership.workerId)
|
|
439
|
+
attachWorkerMetadata(run, membership);
|
|
440
|
+
appendMultiAgentNode(run, "agent-membership", membership.id, statusToNodeStatus(membership.status), {
|
|
441
|
+
multiAgentRunId: membership.multiAgentRunId,
|
|
442
|
+
groupId: membership.groupId,
|
|
443
|
+
roleId: membership.roleId,
|
|
444
|
+
taskId: membership.taskId,
|
|
445
|
+
workerId: membership.workerId,
|
|
446
|
+
dispatchId: membership.dispatchId,
|
|
447
|
+
fanoutId: membership.fanoutId
|
|
448
|
+
}, [`${run.id}:multi-agent:group:${membership.groupId}`, `${run.id}:multi-agent:role:${membership.roleId}`]);
|
|
449
|
+
(0, trust_audit_1.recordTrustAuditEvent)(run, {
|
|
450
|
+
kind: "multi-agent.membership",
|
|
451
|
+
decision: "recorded",
|
|
452
|
+
source: "runtime-derived",
|
|
453
|
+
workerId: membership.workerId,
|
|
454
|
+
taskId: membership.taskId,
|
|
455
|
+
multiAgentRunId: membership.multiAgentRunId,
|
|
456
|
+
agentRoleId: membership.roleId,
|
|
457
|
+
agentGroupId: membership.groupId,
|
|
458
|
+
agentMembershipId: membership.id,
|
|
459
|
+
agentFanoutId: membership.fanoutId,
|
|
460
|
+
metadata: { status: membership.status, dispatchId: membership.dispatchId }
|
|
461
|
+
});
|
|
462
|
+
persistMultiAgentState(run);
|
|
463
|
+
return membership;
|
|
464
|
+
}
|
|
465
|
+
function createAgentFanout(run, input) {
|
|
466
|
+
const state = ensureMultiAgentState(run);
|
|
467
|
+
const group = requireAgentGroup(run, input.groupId);
|
|
468
|
+
const multiAgentRun = requireMultiAgentRun(run, input.multiAgentRunId || group.multiAgentRunId);
|
|
469
|
+
if (group.multiAgentRunId !== multiAgentRun.id)
|
|
470
|
+
throw new Error(`AgentGroup ${group.id} does not belong to ${multiAgentRun.id}`);
|
|
471
|
+
const id = input.id || createId("fanout");
|
|
472
|
+
if (state.fanouts.some((record) => record.id === id))
|
|
473
|
+
throw new Error(`Duplicate AgentFanout id: ${id}`);
|
|
474
|
+
for (const roleId of input.roleIds || [])
|
|
475
|
+
requireAgentRole(run, roleId);
|
|
476
|
+
for (const taskId of input.taskIds || [])
|
|
477
|
+
requireRunTask(run, taskId);
|
|
478
|
+
const now = new Date().toISOString();
|
|
479
|
+
const fanout = {
|
|
480
|
+
schemaVersion: exports.MULTI_AGENT_SCHEMA_VERSION,
|
|
481
|
+
id,
|
|
482
|
+
runId: run.id,
|
|
483
|
+
multiAgentRunId: multiAgentRun.id,
|
|
484
|
+
groupId: group.id,
|
|
485
|
+
createdAt: now,
|
|
486
|
+
updatedAt: now,
|
|
487
|
+
status: "planned",
|
|
488
|
+
reason: input.reason,
|
|
489
|
+
roleIds: unique(input.roleIds || group.roleIds),
|
|
490
|
+
taskIds: unique(input.taskIds || group.taskIds),
|
|
491
|
+
workerIds: unique(input.workerIds || []),
|
|
492
|
+
membershipIds: unique(input.membershipIds || []),
|
|
493
|
+
dispatchIds: unique(input.dispatchIds || []),
|
|
494
|
+
concurrencyLimit: input.concurrencyLimit,
|
|
495
|
+
sandboxProfileChoices: input.sandboxProfileChoices || {},
|
|
496
|
+
expectedReturnShape: input.expectedReturnShape || "Each member writes a Markdown result with a cw:result JSON fence containing summary, findings, and evidence.",
|
|
497
|
+
blackboardId: input.blackboardId || group.blackboardId || multiAgentRun.blackboardId,
|
|
498
|
+
topicIds: unique([...(group.topicIds || []), ...(multiAgentRun.topicIds || []), ...(input.topicIds || [])]),
|
|
499
|
+
lifecycle: [lifecycleEvent(undefined, "planned", "created")],
|
|
500
|
+
policy: {
|
|
501
|
+
schemaVersion: 1,
|
|
502
|
+
id: `${id}-policy`,
|
|
503
|
+
policyRef: `multiAgent.fanouts.${id}.policy`,
|
|
504
|
+
subjectKind: "fanout",
|
|
505
|
+
subjectId: id,
|
|
506
|
+
allowedBlackboardTopicIds: unique(fanoutTopicIds(group, multiAgentRun, input)),
|
|
507
|
+
allowedWriteOperations: ["message", "context", "artifact"],
|
|
508
|
+
allowedCandidateOperations: ["register"],
|
|
509
|
+
allowedJudgeOperations: [],
|
|
510
|
+
sandboxProfileHints: unique(Object.values(input.sandboxProfileChoices || {}).map(String)),
|
|
511
|
+
requiredEvidenceRefs: [],
|
|
512
|
+
deniedOperations: [],
|
|
513
|
+
metadata: { reason: input.reason }
|
|
514
|
+
},
|
|
515
|
+
metadata: compact(input.metadata)
|
|
516
|
+
};
|
|
517
|
+
state.fanouts.push(fanout);
|
|
518
|
+
group.fanoutIds = unique([...group.fanoutIds, fanout.id]);
|
|
519
|
+
group.roleIds = unique([...group.roleIds, ...fanout.roleIds]);
|
|
520
|
+
group.taskIds = unique([...group.taskIds, ...fanout.taskIds]);
|
|
521
|
+
touch(group);
|
|
522
|
+
multiAgentRun.fanoutIds = unique([...multiAgentRun.fanoutIds, fanout.id]);
|
|
523
|
+
touch(multiAgentRun);
|
|
524
|
+
appendMultiAgentNode(run, "agent-fanout", fanout.id, "pending", {
|
|
525
|
+
multiAgentRunId: fanout.multiAgentRunId,
|
|
526
|
+
groupId: fanout.groupId,
|
|
527
|
+
reason: fanout.reason,
|
|
528
|
+
roleIds: fanout.roleIds,
|
|
529
|
+
taskIds: fanout.taskIds,
|
|
530
|
+
concurrencyLimit: fanout.concurrencyLimit,
|
|
531
|
+
sandboxProfileChoices: fanout.sandboxProfileChoices
|
|
532
|
+
}, [`${run.id}:multi-agent:group:${fanout.groupId}`]);
|
|
533
|
+
(0, trust_audit_1.recordTrustAuditEvent)(run, {
|
|
534
|
+
kind: "multi-agent.fanout",
|
|
535
|
+
decision: "recorded",
|
|
536
|
+
source: "runtime-derived",
|
|
537
|
+
multiAgentRunId: fanout.multiAgentRunId,
|
|
538
|
+
agentGroupId: fanout.groupId,
|
|
539
|
+
agentFanoutId: fanout.id,
|
|
540
|
+
metadata: {
|
|
541
|
+
reason: fanout.reason,
|
|
542
|
+
roleIds: fanout.roleIds,
|
|
543
|
+
taskIds: fanout.taskIds,
|
|
544
|
+
concurrencyLimit: fanout.concurrencyLimit,
|
|
545
|
+
sandboxProfileChoices: fanout.sandboxProfileChoices
|
|
546
|
+
}
|
|
547
|
+
});
|
|
548
|
+
persistMultiAgentState(run);
|
|
549
|
+
return fanout;
|
|
550
|
+
}
|
|
551
|
+
function attachDispatchToMultiAgent(run, input) {
|
|
552
|
+
if (!input.multiAgentRunId && !input.groupId && !input.roleId && !input.fanoutId)
|
|
553
|
+
return { membershipIds: [] };
|
|
554
|
+
const state = ensureMultiAgentState(run);
|
|
555
|
+
let fanout = input.fanoutId ? requireAgentFanout(run, input.fanoutId) : undefined;
|
|
556
|
+
let group = input.groupId ? requireAgentGroup(run, input.groupId) : undefined;
|
|
557
|
+
if (!group && fanout)
|
|
558
|
+
group = requireAgentGroup(run, fanout.groupId);
|
|
559
|
+
const multiAgentRun = requireMultiAgentRun(run, input.multiAgentRunId || group?.multiAgentRunId || fanout?.multiAgentRunId || "");
|
|
560
|
+
if (!group)
|
|
561
|
+
throw new Error("Dispatch multi-agent attach requires --multi-agent-group or --multiAgentGroup");
|
|
562
|
+
if (group.multiAgentRunId !== multiAgentRun.id)
|
|
563
|
+
throw new Error(`Group ${group.id} does not belong to MultiAgentRun ${multiAgentRun.id}`);
|
|
564
|
+
const roleIds = input.roleId ? [input.roleId] : unique([...(fanout ? fanout.roleIds : [])]);
|
|
565
|
+
if (roleIds.length !== 1) {
|
|
566
|
+
throw new Error(`Dispatch multi-agent attach requires exactly one role for deterministic membership; found ${roleIds.length || 0}`);
|
|
567
|
+
}
|
|
568
|
+
const role = requireAgentRole(run, roleIds[0]);
|
|
569
|
+
if (role.multiAgentRunId !== multiAgentRun.id)
|
|
570
|
+
throw new Error(`Role ${role.id} does not belong to MultiAgentRun ${multiAgentRun.id}`);
|
|
571
|
+
if (!fanout) {
|
|
572
|
+
fanout = createAgentFanout(run, {
|
|
573
|
+
multiAgentRunId: multiAgentRun.id,
|
|
574
|
+
groupId: group.id,
|
|
575
|
+
reason: "dispatch attachment",
|
|
576
|
+
roleIds: [role.id],
|
|
577
|
+
taskIds: input.tasks.map((task) => task.id),
|
|
578
|
+
dispatchIds: [input.dispatchId],
|
|
579
|
+
concurrencyLimit: input.concurrencyLimit,
|
|
580
|
+
sandboxProfileChoices: input.sandboxProfileId ? { dispatch: input.sandboxProfileId } : {}
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
if (fanout.multiAgentRunId !== multiAgentRun.id || fanout.groupId !== group.id) {
|
|
584
|
+
throw new Error(`Fanout ${fanout.id} does not match MultiAgentRun ${multiAgentRun.id} and group ${group.id}`);
|
|
585
|
+
}
|
|
586
|
+
const membershipIds = [];
|
|
587
|
+
for (const task of input.tasks) {
|
|
588
|
+
if (!task.workerId)
|
|
589
|
+
throw new Error(`Task ${task.id} has no worker id for multi-agent membership`);
|
|
590
|
+
const membership = assignAgentMembership(run, {
|
|
591
|
+
multiAgentRunId: multiAgentRun.id,
|
|
592
|
+
groupId: group.id,
|
|
593
|
+
roleId: role.id,
|
|
594
|
+
taskId: task.id,
|
|
595
|
+
workerId: task.workerId,
|
|
596
|
+
dispatchId: input.dispatchId,
|
|
597
|
+
fanoutId: fanout.id,
|
|
598
|
+
status: "running"
|
|
599
|
+
});
|
|
600
|
+
task.multiAgent = {
|
|
601
|
+
runId: multiAgentRun.id,
|
|
602
|
+
groupId: group.id,
|
|
603
|
+
roleId: role.id,
|
|
604
|
+
membershipId: membership.id,
|
|
605
|
+
fanoutId: fanout.id
|
|
606
|
+
};
|
|
607
|
+
membershipIds.push(membership.id);
|
|
608
|
+
}
|
|
609
|
+
fanout.status = "dispatched";
|
|
610
|
+
fanout.updatedAt = new Date().toISOString();
|
|
611
|
+
fanout.lifecycle.push(lifecycleEvent("planned", "dispatched", "dispatch created"));
|
|
612
|
+
fanout.dispatchIds = unique([...fanout.dispatchIds, input.dispatchId]);
|
|
613
|
+
fanout.taskIds = unique([...fanout.taskIds, ...input.tasks.map((task) => task.id)]);
|
|
614
|
+
fanout.workerIds = unique([...fanout.workerIds, ...input.tasks.map((task) => task.workerId || "").filter(Boolean)]);
|
|
615
|
+
fanout.membershipIds = unique([...fanout.membershipIds, ...membershipIds]);
|
|
616
|
+
if (input.sandboxProfileId)
|
|
617
|
+
fanout.sandboxProfileChoices.dispatch = input.sandboxProfileId;
|
|
618
|
+
const groupStatusBefore = group.status;
|
|
619
|
+
group.status = "running";
|
|
620
|
+
group.updatedAt = fanout.updatedAt;
|
|
621
|
+
group.lifecycle.push(lifecycleEvent(groupStatusBefore, "running", "dispatch created"));
|
|
622
|
+
multiAgentRun.status = multiAgentRun.status === "planned" || multiAgentRun.status === "forming" ? "running" : multiAgentRun.status;
|
|
623
|
+
touch(multiAgentRun);
|
|
624
|
+
appendMultiAgentNode(run, "agent-fanout", fanout.id, "running", {
|
|
625
|
+
status: fanout.status,
|
|
626
|
+
dispatchIds: fanout.dispatchIds,
|
|
627
|
+
workerIds: fanout.workerIds,
|
|
628
|
+
membershipIds: fanout.membershipIds
|
|
629
|
+
}, [`${run.id}:dispatch:${input.dispatchId}`]);
|
|
630
|
+
(0, trust_audit_1.recordTrustAuditEvent)(run, {
|
|
631
|
+
kind: "multi-agent.fanout.dispatch",
|
|
632
|
+
decision: "validated",
|
|
633
|
+
source: "cw-validated",
|
|
634
|
+
multiAgentRunId: multiAgentRun.id,
|
|
635
|
+
agentRoleId: role.id,
|
|
636
|
+
agentGroupId: group.id,
|
|
637
|
+
agentFanoutId: fanout.id,
|
|
638
|
+
metadata: { dispatchId: input.dispatchId, membershipIds, workerIds: fanout.workerIds }
|
|
639
|
+
});
|
|
640
|
+
persistMultiAgentState(run);
|
|
641
|
+
return {
|
|
642
|
+
multiAgent: {
|
|
643
|
+
runId: multiAgentRun.id,
|
|
644
|
+
groupId: group.id,
|
|
645
|
+
roleId: role.id,
|
|
646
|
+
fanoutId: fanout.id
|
|
647
|
+
},
|
|
648
|
+
membershipIds
|
|
649
|
+
};
|
|
650
|
+
}
|
|
651
|
+
function collectAgentFanin(run, input) {
|
|
652
|
+
const state = ensureMultiAgentState(run);
|
|
653
|
+
const fanout = input.fanoutId ? requireAgentFanout(run, input.fanoutId) : undefined;
|
|
654
|
+
const group = requireAgentGroup(run, input.groupId || fanout?.groupId || "");
|
|
655
|
+
const multiAgentRun = requireMultiAgentRun(run, input.multiAgentRunId || group.multiAgentRunId);
|
|
656
|
+
if (group.multiAgentRunId !== multiAgentRun.id)
|
|
657
|
+
throw new Error(`Group ${group.id} does not belong to MultiAgentRun ${multiAgentRun.id}`);
|
|
658
|
+
if (fanout && fanout.groupId !== group.id)
|
|
659
|
+
throw new Error(`Fanout ${fanout.id} does not belong to group ${group.id}`);
|
|
660
|
+
const id = input.id || createId("fanin");
|
|
661
|
+
if (state.fanins.some((record) => record.id === id))
|
|
662
|
+
throw new Error(`Duplicate AgentFanin id: ${id}`);
|
|
663
|
+
const requiredRoleIds = unique(input.requiredRoleIds?.length ? input.requiredRoleIds : group.roleIds);
|
|
664
|
+
for (const roleId of requiredRoleIds)
|
|
665
|
+
requireAgentRole(run, roleId);
|
|
666
|
+
const scopedMemberships = state.memberships.filter((membership) => membership.groupId === group.id && (!fanout || membership.fanoutId === fanout.id));
|
|
667
|
+
const coverage = scopedMemberships.map((membership) => ({
|
|
668
|
+
membershipId: membership.id,
|
|
669
|
+
roleId: membership.roleId,
|
|
670
|
+
taskId: membership.taskId,
|
|
671
|
+
workerId: membership.workerId,
|
|
672
|
+
evidenceRefs: membership.evidenceRefs,
|
|
673
|
+
blackboardMessageIds: membership.blackboardMessageIds || [],
|
|
674
|
+
blackboardArtifactRefIds: membership.blackboardArtifactRefIds || [],
|
|
675
|
+
resultNodeId: membership.resultNodeId,
|
|
676
|
+
verifierNodeId: membership.verifierNodeId,
|
|
677
|
+
complete: isMembershipReported(membership)
|
|
678
|
+
}));
|
|
679
|
+
const missingRoleIds = requiredRoleIds.filter((roleId) => !scopedMemberships.some((membership) => membership.roleId === roleId));
|
|
680
|
+
const missingMembershipIds = scopedMemberships
|
|
681
|
+
.filter((membership) => requiredRoleIds.includes(membership.roleId) && !isMembershipReported(membership))
|
|
682
|
+
.map((membership) => membership.id);
|
|
683
|
+
const blockedReasons = [
|
|
684
|
+
...missingRoleIds.map((roleId) => `required role ${roleId} has no membership`),
|
|
685
|
+
...missingMembershipIds.map((membershipId) => `membership ${membershipId} has not reported required evidence`)
|
|
686
|
+
];
|
|
687
|
+
const requiredMemberships = scopedMemberships.filter((membership) => requiredRoleIds.includes(membership.roleId));
|
|
688
|
+
const blackboardId = input.blackboardId || group.blackboardId || multiAgentRun.blackboardId;
|
|
689
|
+
const requiresBlackboardEvidence = Boolean(blackboardId || requiredMemberships.some((membership) => membership.blackboardId));
|
|
690
|
+
if (requiresBlackboardEvidence) {
|
|
691
|
+
for (const membership of requiredMemberships) {
|
|
692
|
+
const indexedEvidence = [
|
|
693
|
+
...((membership.blackboardArtifactRefIds || [])),
|
|
694
|
+
...((membership.blackboardMessageIds || []))
|
|
695
|
+
];
|
|
696
|
+
if (!indexedEvidence.length)
|
|
697
|
+
blockedReasons.push(`membership ${membership.id} has no indexed blackboard evidence`);
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
const verifierReady = blockedReasons.length === 0;
|
|
701
|
+
const status = verifierReady ? "ready" : "blocked";
|
|
702
|
+
const now = new Date().toISOString();
|
|
703
|
+
const fanin = {
|
|
704
|
+
schemaVersion: exports.MULTI_AGENT_SCHEMA_VERSION,
|
|
705
|
+
id,
|
|
706
|
+
runId: run.id,
|
|
707
|
+
multiAgentRunId: multiAgentRun.id,
|
|
708
|
+
groupId: group.id,
|
|
709
|
+
fanoutId: fanout?.id,
|
|
710
|
+
createdAt: now,
|
|
711
|
+
updatedAt: now,
|
|
712
|
+
status,
|
|
713
|
+
strategy: input.strategy || "required-role-evidence",
|
|
714
|
+
requiredRoleIds,
|
|
715
|
+
reportedMembershipIds: coverage.filter((entry) => entry.complete).map((entry) => entry.membershipId),
|
|
716
|
+
missingMembershipIds,
|
|
717
|
+
missingRoleIds,
|
|
718
|
+
evidenceCoverage: coverage,
|
|
719
|
+
verifierReady,
|
|
720
|
+
blockedReasons,
|
|
721
|
+
blackboardId,
|
|
722
|
+
topicIds: unique([...(group.topicIds || []), ...(multiAgentRun.topicIds || []), ...(input.topicIds || [])]),
|
|
723
|
+
blackboardArtifactRefIds: unique(coverage.flatMap((entry) => entry.blackboardArtifactRefIds || [])),
|
|
724
|
+
blackboardMessageIds: unique(coverage.flatMap((entry) => entry.blackboardMessageIds || [])),
|
|
725
|
+
lifecycle: [lifecycleEvent(undefined, status, "collected")],
|
|
726
|
+
policy: {
|
|
727
|
+
schemaVersion: 1,
|
|
728
|
+
id: `${id}-policy`,
|
|
729
|
+
policyRef: `multiAgent.fanins.${id}.policy`,
|
|
730
|
+
subjectKind: "fanin",
|
|
731
|
+
subjectId: id,
|
|
732
|
+
allowedBlackboardTopicIds: unique([...(group.topicIds || []), ...(multiAgentRun.topicIds || []), ...(input.topicIds || [])]),
|
|
733
|
+
allowedWriteOperations: ["message", "context", "artifact", "snapshot", "coordinator-decision"],
|
|
734
|
+
allowedCandidateOperations: verifierReady ? ["register", "score", "select"] : [],
|
|
735
|
+
allowedJudgeOperations: verifierReady ? ["panel-decision", "rationale"] : [],
|
|
736
|
+
sandboxProfileHints: [],
|
|
737
|
+
requiredEvidenceRefs: unique(coverage.flatMap((entry) => entry.evidenceRefs)),
|
|
738
|
+
deniedOperations: verifierReady ? [] : blockedReasons.map((reason) => ({ operation: "candidate.select", reason })),
|
|
739
|
+
metadata: { verifierReady, strategy: input.strategy || "required-role-evidence" }
|
|
740
|
+
},
|
|
741
|
+
metadata: compact(input.metadata)
|
|
742
|
+
};
|
|
743
|
+
state.fanins.push(fanin);
|
|
744
|
+
group.faninIds = unique([...group.faninIds, fanin.id]);
|
|
745
|
+
group.status = verifierReady ? "verifying" : "collecting";
|
|
746
|
+
touch(group);
|
|
747
|
+
multiAgentRun.faninIds = unique([...multiAgentRun.faninIds, fanin.id]);
|
|
748
|
+
multiAgentRun.status = verifierReady ? "verifying" : "collecting";
|
|
749
|
+
touch(multiAgentRun);
|
|
750
|
+
appendMultiAgentNode(run, "agent-fanin", fanin.id, verifierReady ? "verified" : "blocked", {
|
|
751
|
+
multiAgentRunId: fanin.multiAgentRunId,
|
|
752
|
+
groupId: fanin.groupId,
|
|
753
|
+
fanoutId: fanin.fanoutId,
|
|
754
|
+
requiredRoleIds,
|
|
755
|
+
missingRoleIds,
|
|
756
|
+
missingMembershipIds,
|
|
757
|
+
verifierReady
|
|
758
|
+
}, [
|
|
759
|
+
`${run.id}:multi-agent:group:${fanin.groupId}`,
|
|
760
|
+
...(fanin.fanoutId ? [`${run.id}:multi-agent:fanout:${fanin.fanoutId}`] : []),
|
|
761
|
+
...fanin.evidenceCoverage.map((entry) => `${run.id}:multi-agent:membership:${entry.membershipId}`)
|
|
762
|
+
]);
|
|
763
|
+
(0, trust_audit_1.recordTrustAuditEvent)(run, {
|
|
764
|
+
kind: "multi-agent.fanin",
|
|
765
|
+
decision: verifierReady ? "validated" : "failed",
|
|
766
|
+
source: "cw-validated",
|
|
767
|
+
multiAgentRunId: fanin.multiAgentRunId,
|
|
768
|
+
agentGroupId: fanin.groupId,
|
|
769
|
+
agentFanoutId: fanin.fanoutId,
|
|
770
|
+
agentFaninId: fanin.id,
|
|
771
|
+
evidenceRefs: fanin.evidenceCoverage.flatMap((entry) => entry.evidenceRefs),
|
|
772
|
+
metadata: {
|
|
773
|
+
verifierReady,
|
|
774
|
+
requiredRoleIds,
|
|
775
|
+
missingRoleIds,
|
|
776
|
+
missingMembershipIds,
|
|
777
|
+
blockedReasons
|
|
778
|
+
}
|
|
779
|
+
});
|
|
780
|
+
persistMultiAgentState(run);
|
|
781
|
+
return fanin;
|
|
782
|
+
}
|
|
783
|
+
function recordMultiAgentWorkerOutput(run, input) {
|
|
784
|
+
const state = ensureMultiAgentState(run);
|
|
785
|
+
const memberships = state.memberships.filter((membership) => membership.workerId === input.workerId && membership.taskId === input.taskId);
|
|
786
|
+
if (!memberships.length)
|
|
787
|
+
return [];
|
|
788
|
+
const evidenceRefs = input.evidence.map((entry) => entry.locator || entry.path || entry.summary || entry.id).filter(Boolean);
|
|
789
|
+
for (const membership of memberships) {
|
|
790
|
+
const before = membership.status;
|
|
791
|
+
membership.status = "reported";
|
|
792
|
+
membership.updatedAt = new Date().toISOString();
|
|
793
|
+
membership.resultNodeId = input.resultNodeId || membership.resultNodeId;
|
|
794
|
+
membership.verifierNodeId = input.verifierNodeId || membership.verifierNodeId;
|
|
795
|
+
membership.evidenceRefs = unique([...membership.evidenceRefs, ...evidenceRefs]);
|
|
796
|
+
membership.artifactPaths = unique([...(membership.artifactPaths || []), ...(input.artifactPaths || [])]);
|
|
797
|
+
membership.blackboardMessageIds = unique([...(membership.blackboardMessageIds || []), ...(input.blackboardMessageIds || [])]);
|
|
798
|
+
membership.blackboardArtifactRefIds = unique([...(membership.blackboardArtifactRefIds || []), ...(input.blackboardArtifactRefIds || [])]);
|
|
799
|
+
membership.lifecycle.push(lifecycleEvent(before, "reported", "worker output accepted"));
|
|
800
|
+
appendMultiAgentNode(run, "agent-membership", membership.id, "completed", {
|
|
801
|
+
resultNodeId: membership.resultNodeId,
|
|
802
|
+
verifierNodeId: membership.verifierNodeId,
|
|
803
|
+
evidenceRefs: membership.evidenceRefs
|
|
804
|
+
}, [membership.resultNodeId, membership.verifierNodeId].filter(Boolean));
|
|
805
|
+
(0, trust_audit_1.recordTrustAuditEvent)(run, {
|
|
806
|
+
kind: "multi-agent.membership.output",
|
|
807
|
+
decision: "accepted",
|
|
808
|
+
source: "cw-validated",
|
|
809
|
+
workerId: input.workerId,
|
|
810
|
+
taskId: input.taskId,
|
|
811
|
+
nodeId: input.resultNodeId,
|
|
812
|
+
multiAgentRunId: membership.multiAgentRunId,
|
|
813
|
+
agentRoleId: membership.roleId,
|
|
814
|
+
agentGroupId: membership.groupId,
|
|
815
|
+
agentMembershipId: membership.id,
|
|
816
|
+
agentFanoutId: membership.fanoutId,
|
|
817
|
+
evidence: input.evidence,
|
|
818
|
+
metadata: { verifierNodeId: input.verifierNodeId }
|
|
819
|
+
});
|
|
820
|
+
}
|
|
821
|
+
persistMultiAgentState(run);
|
|
822
|
+
return memberships;
|
|
823
|
+
}
|
|
824
|
+
function summarizeMultiAgent(run) {
|
|
825
|
+
const state = ensureMultiAgentState(run);
|
|
826
|
+
const blockedReasons = [];
|
|
827
|
+
for (const fanin of state.fanins)
|
|
828
|
+
blockedReasons.push(...fanin.blockedReasons.map((reason) => `${fanin.id}: ${reason}`));
|
|
829
|
+
for (const membership of state.memberships) {
|
|
830
|
+
if (membership.status === "failed")
|
|
831
|
+
blockedReasons.push(`${membership.id}: failed membership`);
|
|
832
|
+
}
|
|
833
|
+
const groupsDetail = state.groups.map((group) => {
|
|
834
|
+
const roleIds = unique([...group.roleIds, ...state.memberships.filter((membership) => membership.groupId === group.id).map((membership) => membership.roleId)]);
|
|
835
|
+
return {
|
|
836
|
+
id: group.id,
|
|
837
|
+
multiAgentRunId: group.multiAgentRunId,
|
|
838
|
+
status: group.status,
|
|
839
|
+
phase: group.phase,
|
|
840
|
+
roles: roleIds.map((roleId) => {
|
|
841
|
+
const role = state.roles.find((entry) => entry.id === roleId);
|
|
842
|
+
const memberships = state.memberships.filter((membership) => membership.groupId === group.id && membership.roleId === roleId);
|
|
843
|
+
const reported = memberships.filter(isMembershipReported).length;
|
|
844
|
+
return {
|
|
845
|
+
roleId,
|
|
846
|
+
requiredEvidence: role?.requiredEvidence.length || 0,
|
|
847
|
+
memberships: memberships.length,
|
|
848
|
+
reported,
|
|
849
|
+
missing: Math.max(0, memberships.length - reported)
|
|
850
|
+
};
|
|
851
|
+
}),
|
|
852
|
+
fanouts: group.fanoutIds,
|
|
853
|
+
fanins: group.faninIds
|
|
854
|
+
};
|
|
855
|
+
});
|
|
856
|
+
return {
|
|
857
|
+
totalRuns: state.runs.length,
|
|
858
|
+
runsByStatus: countBy(state.runs, (record) => record.status),
|
|
859
|
+
roles: state.roles.length,
|
|
860
|
+
groups: state.groups.length,
|
|
861
|
+
memberships: state.memberships.length,
|
|
862
|
+
fanouts: state.fanouts.length,
|
|
863
|
+
fanins: state.fanins.length,
|
|
864
|
+
groupsByStatus: countBy(state.groups, (record) => record.status),
|
|
865
|
+
membershipsByStatus: countBy(state.memberships, (record) => record.status),
|
|
866
|
+
faninsByStatus: countBy(state.fanins, (record) => record.status),
|
|
867
|
+
blockedReasons,
|
|
868
|
+
groupsDetail,
|
|
869
|
+
nextAction: nextMultiAgentAction(run, blockedReasons)
|
|
870
|
+
};
|
|
871
|
+
}
|
|
872
|
+
function buildMultiAgentGraph(run) {
|
|
873
|
+
const state = ensureMultiAgentState(run);
|
|
874
|
+
const root = multiAgentRoot(run);
|
|
875
|
+
const nodes = [];
|
|
876
|
+
const edges = [];
|
|
877
|
+
for (const record of state.runs) {
|
|
878
|
+
nodes.push({ id: `${run.id}:multi-agent:${record.id}`, kind: "multi-agent-run", status: record.status, label: record.title || record.id, path: recordPath(run, "runs", record.id) });
|
|
879
|
+
edges.push({ from: `${run.id}:run`, to: `${run.id}:multi-agent:${record.id}` });
|
|
880
|
+
if (record.blackboardId)
|
|
881
|
+
edges.push({ from: `${run.id}:multi-agent:${record.id}`, to: `${run.id}:blackboard:${record.blackboardId}`, label: "blackboard" });
|
|
882
|
+
if (record.parentMultiAgentRunId)
|
|
883
|
+
edges.push({ from: `${run.id}:multi-agent:${record.parentMultiAgentRunId}`, to: `${run.id}:multi-agent:${record.id}`, label: "child" });
|
|
884
|
+
}
|
|
885
|
+
for (const record of state.roles) {
|
|
886
|
+
nodes.push({ id: `${run.id}:multi-agent:role:${record.id}`, kind: "agent-role", status: record.status, label: record.title, path: recordPath(run, "roles", record.id) });
|
|
887
|
+
edges.push({ from: `${run.id}:multi-agent:${record.multiAgentRunId}`, to: `${run.id}:multi-agent:role:${record.id}` });
|
|
888
|
+
if (record.blackboardId)
|
|
889
|
+
edges.push({ from: `${run.id}:multi-agent:role:${record.id}`, to: `${run.id}:blackboard:${record.blackboardId}`, label: "blackboard" });
|
|
890
|
+
}
|
|
891
|
+
for (const record of state.groups) {
|
|
892
|
+
nodes.push({ id: `${run.id}:multi-agent:group:${record.id}`, kind: "agent-group", status: record.status, label: record.title || record.id, path: recordPath(run, "groups", record.id) });
|
|
893
|
+
edges.push({ from: `${run.id}:multi-agent:${record.multiAgentRunId}`, to: `${run.id}:multi-agent:group:${record.id}` });
|
|
894
|
+
if (record.blackboardId)
|
|
895
|
+
edges.push({ from: `${run.id}:multi-agent:group:${record.id}`, to: `${run.id}:blackboard:${record.blackboardId}`, label: "blackboard" });
|
|
896
|
+
for (const taskId of record.taskIds)
|
|
897
|
+
edges.push({ from: `${run.id}:multi-agent:group:${record.id}`, to: `${run.id}:task:${taskId}`, label: "task" });
|
|
898
|
+
}
|
|
899
|
+
for (const record of state.fanouts) {
|
|
900
|
+
nodes.push({ id: `${run.id}:multi-agent:fanout:${record.id}`, kind: "agent-fanout", status: record.status, label: record.reason, path: recordPath(run, "fanouts", record.id) });
|
|
901
|
+
edges.push({ from: `${run.id}:multi-agent:group:${record.groupId}`, to: `${run.id}:multi-agent:fanout:${record.id}` });
|
|
902
|
+
for (const dispatchId of record.dispatchIds)
|
|
903
|
+
edges.push({ from: `${run.id}:multi-agent:fanout:${record.id}`, to: `${run.id}:dispatch:${dispatchId}`, label: "dispatch" });
|
|
904
|
+
}
|
|
905
|
+
for (const record of state.memberships) {
|
|
906
|
+
nodes.push({ id: `${run.id}:multi-agent:membership:${record.id}`, kind: "agent-membership", status: record.status, label: `${record.roleId}/${record.taskId}`, path: recordPath(run, "memberships", record.id) });
|
|
907
|
+
edges.push({ from: `${run.id}:multi-agent:group:${record.groupId}`, to: `${run.id}:multi-agent:membership:${record.id}` });
|
|
908
|
+
edges.push({ from: `${run.id}:multi-agent:role:${record.roleId}`, to: `${run.id}:multi-agent:membership:${record.id}` });
|
|
909
|
+
edges.push({ from: `${run.id}:multi-agent:membership:${record.id}`, to: `${run.id}:task:${record.taskId}`, label: "task" });
|
|
910
|
+
if (record.workerId)
|
|
911
|
+
edges.push({ from: `${run.id}:multi-agent:membership:${record.id}`, to: `${run.id}:worker:${record.workerId}`, label: "worker" });
|
|
912
|
+
if (record.resultNodeId)
|
|
913
|
+
edges.push({ from: `${run.id}:multi-agent:membership:${record.id}`, to: record.resultNodeId, label: "result" });
|
|
914
|
+
if (record.verifierNodeId)
|
|
915
|
+
edges.push({ from: `${run.id}:multi-agent:membership:${record.id}`, to: record.verifierNodeId, label: "verifier" });
|
|
916
|
+
if (record.blackboardId)
|
|
917
|
+
edges.push({ from: `${run.id}:multi-agent:membership:${record.id}`, to: `${run.id}:blackboard:${record.blackboardId}`, label: "blackboard" });
|
|
918
|
+
for (const artifactId of record.blackboardArtifactRefIds || [])
|
|
919
|
+
edges.push({ from: `${run.id}:multi-agent:membership:${record.id}`, to: `${run.id}:blackboard:artifact:${artifactId}`, label: "evidence" });
|
|
920
|
+
for (const messageId of record.blackboardMessageIds || [])
|
|
921
|
+
edges.push({ from: `${run.id}:multi-agent:membership:${record.id}`, to: `${run.id}:blackboard:message:${messageId}`, label: "message" });
|
|
922
|
+
}
|
|
923
|
+
for (const record of state.fanins) {
|
|
924
|
+
nodes.push({ id: `${run.id}:multi-agent:fanin:${record.id}`, kind: "agent-fanin", status: record.status, label: record.strategy, path: recordPath(run, "fanins", record.id) });
|
|
925
|
+
edges.push({ from: `${run.id}:multi-agent:group:${record.groupId}`, to: `${run.id}:multi-agent:fanin:${record.id}` });
|
|
926
|
+
if (record.fanoutId)
|
|
927
|
+
edges.push({ from: `${run.id}:multi-agent:fanout:${record.fanoutId}`, to: `${run.id}:multi-agent:fanin:${record.id}` });
|
|
928
|
+
for (const membershipId of record.reportedMembershipIds)
|
|
929
|
+
edges.push({ from: `${run.id}:multi-agent:membership:${membershipId}`, to: `${run.id}:multi-agent:fanin:${record.id}`, label: "reported" });
|
|
930
|
+
for (const membershipId of record.missingMembershipIds)
|
|
931
|
+
edges.push({ from: `${run.id}:multi-agent:membership:${membershipId}`, to: `${run.id}:multi-agent:fanin:${record.id}`, label: "missing" });
|
|
932
|
+
if (record.blackboardId)
|
|
933
|
+
edges.push({ from: `${run.id}:multi-agent:fanin:${record.id}`, to: `${run.id}:blackboard:${record.blackboardId}`, label: "blackboard" });
|
|
934
|
+
}
|
|
935
|
+
if (!node_fs_1.default.existsSync(root))
|
|
936
|
+
node_fs_1.default.mkdirSync(root, { recursive: true });
|
|
937
|
+
return { nodes, edges: uniqueEdges(edges) };
|
|
938
|
+
}
|
|
939
|
+
function getMultiAgentRun(run, id) {
|
|
940
|
+
return ensureMultiAgentState(run).runs.find((record) => record.id === id);
|
|
941
|
+
}
|
|
942
|
+
function getAgentRole(run, id) {
|
|
943
|
+
return ensureMultiAgentState(run).roles.find((record) => record.id === id);
|
|
944
|
+
}
|
|
945
|
+
function getAgentGroup(run, id) {
|
|
946
|
+
return ensureMultiAgentState(run).groups.find((record) => record.id === id);
|
|
947
|
+
}
|
|
948
|
+
function getAgentMembership(run, id) {
|
|
949
|
+
return ensureMultiAgentState(run).memberships.find((record) => record.id === id);
|
|
950
|
+
}
|
|
951
|
+
function getAgentFanout(run, id) {
|
|
952
|
+
return ensureMultiAgentState(run).fanouts.find((record) => record.id === id);
|
|
953
|
+
}
|
|
954
|
+
function getAgentFanin(run, id) {
|
|
955
|
+
return ensureMultiAgentState(run).fanins.find((record) => record.id === id);
|
|
956
|
+
}
|
|
957
|
+
function requireMultiAgentRun(run, id) {
|
|
958
|
+
const record = getMultiAgentRun(run, id);
|
|
959
|
+
if (!record)
|
|
960
|
+
throw new Error(`Unknown MultiAgentRun id: ${id}`);
|
|
961
|
+
return record;
|
|
962
|
+
}
|
|
963
|
+
function requireAgentRole(run, id) {
|
|
964
|
+
const record = getAgentRole(run, id);
|
|
965
|
+
if (!record)
|
|
966
|
+
throw new Error(`Unknown AgentRole id: ${id}`);
|
|
967
|
+
return record;
|
|
968
|
+
}
|
|
969
|
+
function requireAgentGroup(run, id) {
|
|
970
|
+
const record = getAgentGroup(run, id);
|
|
971
|
+
if (!record)
|
|
972
|
+
throw new Error(`Unknown AgentGroup id: ${id}`);
|
|
973
|
+
return record;
|
|
974
|
+
}
|
|
975
|
+
function requireAgentFanout(run, id) {
|
|
976
|
+
const record = getAgentFanout(run, id);
|
|
977
|
+
if (!record)
|
|
978
|
+
throw new Error(`Unknown AgentFanout id: ${id}`);
|
|
979
|
+
return record;
|
|
980
|
+
}
|
|
981
|
+
function requireRunTask(run, id) {
|
|
982
|
+
const task = run.tasks.find((record) => record.id === id);
|
|
983
|
+
if (!task)
|
|
984
|
+
throw new Error(`Unknown task id for multi-agent record: ${id}`);
|
|
985
|
+
return task;
|
|
986
|
+
}
|
|
987
|
+
function multiAgentRoot(run) {
|
|
988
|
+
return run.paths.multiAgentDir || node_path_1.default.join(run.paths.runDir, "multi-agent");
|
|
989
|
+
}
|
|
990
|
+
function recordPath(run, kind, id) {
|
|
991
|
+
return node_path_1.default.join(multiAgentRoot(run), kind, `${(0, state_1.safeFileName)(id)}.json`);
|
|
992
|
+
}
|
|
993
|
+
function fanoutTopicIds(group, multiAgentRun, input) {
|
|
994
|
+
return [...(group.topicIds || []), ...(multiAgentRun.topicIds || []), ...(input.topicIds || [])];
|
|
995
|
+
}
|
|
996
|
+
function writeRecord(run, kind, record) {
|
|
997
|
+
(0, state_1.writeJson)(recordPath(run, kind, record.id), record);
|
|
998
|
+
}
|
|
999
|
+
function assertNoRecordPathCollisions(label, records) {
|
|
1000
|
+
const seen = new Map();
|
|
1001
|
+
for (const record of records) {
|
|
1002
|
+
const safe = (0, state_1.safeFileName)(record.id);
|
|
1003
|
+
const existing = seen.get(safe);
|
|
1004
|
+
if (existing && existing !== record.id) {
|
|
1005
|
+
throw new Error(`${label} ids ${existing} and ${record.id} collide on safe file name ${safe}`);
|
|
1006
|
+
}
|
|
1007
|
+
seen.set(safe, record.id);
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
function indexRow(record) {
|
|
1011
|
+
return { id: record.id, status: record.status, updatedAt: record.updatedAt };
|
|
1012
|
+
}
|
|
1013
|
+
function appendMultiAgentNode(run, kind, id, status, metadata, parents = []) {
|
|
1014
|
+
const nodeId = kind === "multi-agent-run" ? `${run.id}:multi-agent:${id}` : `${run.id}:multi-agent:${kind.replace("agent-", "")}:${id}`;
|
|
1015
|
+
(0, state_node_1.appendRunNode)(run, (0, state_node_1.createStateNode)({
|
|
1016
|
+
id: nodeId,
|
|
1017
|
+
kind,
|
|
1018
|
+
status,
|
|
1019
|
+
loopStage: run.loopStage,
|
|
1020
|
+
outputs: metadata,
|
|
1021
|
+
artifacts: [{ id: kind, kind: "json", path: recordPath(run, pluralKind(kind), id) }],
|
|
1022
|
+
parents,
|
|
1023
|
+
contractId: pipeline_contract_1.DEFAULT_PIPELINE_CONTRACT_ID,
|
|
1024
|
+
metadata
|
|
1025
|
+
}));
|
|
1026
|
+
}
|
|
1027
|
+
function pluralKind(kind) {
|
|
1028
|
+
switch (kind) {
|
|
1029
|
+
case "multi-agent-run":
|
|
1030
|
+
return "runs";
|
|
1031
|
+
case "agent-role":
|
|
1032
|
+
return "roles";
|
|
1033
|
+
case "agent-group":
|
|
1034
|
+
return "groups";
|
|
1035
|
+
case "agent-membership":
|
|
1036
|
+
return "memberships";
|
|
1037
|
+
case "agent-fanout":
|
|
1038
|
+
return "fanouts";
|
|
1039
|
+
case "agent-fanin":
|
|
1040
|
+
return "fanins";
|
|
1041
|
+
default:
|
|
1042
|
+
return `${kind}s`;
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
function statusToNodeStatus(status) {
|
|
1046
|
+
switch (status) {
|
|
1047
|
+
case "completed":
|
|
1048
|
+
case "reported":
|
|
1049
|
+
case "ready":
|
|
1050
|
+
return "completed";
|
|
1051
|
+
case "running":
|
|
1052
|
+
case "forming":
|
|
1053
|
+
case "collecting":
|
|
1054
|
+
case "verifying":
|
|
1055
|
+
case "assigned":
|
|
1056
|
+
case "active":
|
|
1057
|
+
case "dispatched":
|
|
1058
|
+
return "running";
|
|
1059
|
+
case "blocked":
|
|
1060
|
+
return "blocked";
|
|
1061
|
+
case "failed":
|
|
1062
|
+
return "failed";
|
|
1063
|
+
case "cancelled":
|
|
1064
|
+
case "rejected":
|
|
1065
|
+
return "rejected";
|
|
1066
|
+
default:
|
|
1067
|
+
return "pending";
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
function assertLifecycleTransition(from, to) {
|
|
1071
|
+
const allowed = {
|
|
1072
|
+
planned: ["forming", "running", "failed", "cancelled"],
|
|
1073
|
+
forming: ["running", "failed", "cancelled"],
|
|
1074
|
+
running: ["collecting", "completed", "failed", "cancelled"],
|
|
1075
|
+
collecting: ["verifying", "completed", "failed", "cancelled"],
|
|
1076
|
+
verifying: ["completed", "failed", "cancelled"],
|
|
1077
|
+
completed: [],
|
|
1078
|
+
failed: [],
|
|
1079
|
+
cancelled: []
|
|
1080
|
+
};
|
|
1081
|
+
if (from === to)
|
|
1082
|
+
return;
|
|
1083
|
+
if (!allowed[from].includes(to))
|
|
1084
|
+
throw new Error(`Invalid MultiAgentRun lifecycle transition: ${from} -> ${to}`);
|
|
1085
|
+
}
|
|
1086
|
+
function lifecycleEvent(from, to, reason, actor = "cw", metadata) {
|
|
1087
|
+
return {
|
|
1088
|
+
at: new Date().toISOString(),
|
|
1089
|
+
from,
|
|
1090
|
+
to,
|
|
1091
|
+
actor,
|
|
1092
|
+
reason,
|
|
1093
|
+
metadata: compact(metadata)
|
|
1094
|
+
};
|
|
1095
|
+
}
|
|
1096
|
+
function attachWorkerMetadata(run, membership) {
|
|
1097
|
+
const workers = run.workers || [];
|
|
1098
|
+
const index = workers.findIndex((worker) => worker.id === membership.workerId);
|
|
1099
|
+
if (index < 0)
|
|
1100
|
+
return;
|
|
1101
|
+
const worker = workers[index];
|
|
1102
|
+
const multiAgent = {
|
|
1103
|
+
runId: membership.multiAgentRunId,
|
|
1104
|
+
groupId: membership.groupId,
|
|
1105
|
+
roleId: membership.roleId,
|
|
1106
|
+
membershipId: membership.id,
|
|
1107
|
+
fanoutId: membership.fanoutId
|
|
1108
|
+
};
|
|
1109
|
+
const updated = {
|
|
1110
|
+
...worker,
|
|
1111
|
+
updatedAt: new Date().toISOString(),
|
|
1112
|
+
multiAgent,
|
|
1113
|
+
metadata: {
|
|
1114
|
+
...(worker.metadata || {}),
|
|
1115
|
+
multiAgent
|
|
1116
|
+
}
|
|
1117
|
+
};
|
|
1118
|
+
run.workers = workers.map((candidate) => (candidate.id === worker.id ? updated : candidate));
|
|
1119
|
+
}
|
|
1120
|
+
function isMembershipReported(membership) {
|
|
1121
|
+
return (membership.status === "reported" || membership.status === "verified") && membership.evidenceRefs.length > 0;
|
|
1122
|
+
}
|
|
1123
|
+
function nextMultiAgentAction(run, blockedReasons) {
|
|
1124
|
+
const state = ensureMultiAgentState(run);
|
|
1125
|
+
if (!state.runs.length)
|
|
1126
|
+
return `node scripts/cw.js multi-agent run ${run.id} --id <multi-agent-run-id>`;
|
|
1127
|
+
if (blockedReasons.length)
|
|
1128
|
+
return `node scripts/cw.js multi-agent fanin ${run.id} --group <group-id> --fanout <fanout-id>`;
|
|
1129
|
+
const running = state.memberships.find((membership) => membership.status === "running");
|
|
1130
|
+
if (running?.workerId)
|
|
1131
|
+
return `node scripts/cw.js worker manifest ${run.id} ${running.workerId}`;
|
|
1132
|
+
const groupWithoutFanin = state.groups.find((group) => group.membershipIds.length && !group.faninIds.length);
|
|
1133
|
+
if (groupWithoutFanin)
|
|
1134
|
+
return `node scripts/cw.js multi-agent fanin ${run.id} --group ${groupWithoutFanin.id}`;
|
|
1135
|
+
return undefined;
|
|
1136
|
+
}
|
|
1137
|
+
function touch(record) {
|
|
1138
|
+
record.updatedAt = new Date().toISOString();
|
|
1139
|
+
return record;
|
|
1140
|
+
}
|
|
1141
|
+
function createId(prefix) {
|
|
1142
|
+
const stamp = new Date().toISOString().replace(/[-:]/g, "").replace(/\..+/, "Z");
|
|
1143
|
+
return `${prefix}-${stamp}-${Math.random().toString(36).slice(2, 8)}`;
|
|
1144
|
+
}
|
|
1145
|
+
function compact(value) {
|
|
1146
|
+
if (!value)
|
|
1147
|
+
return undefined;
|
|
1148
|
+
const entries = Object.entries(value).filter(([, entry]) => entry !== undefined);
|
|
1149
|
+
return entries.length ? Object.fromEntries(entries) : undefined;
|
|
1150
|
+
}
|
|
1151
|
+
function unique(values) {
|
|
1152
|
+
return Array.from(new Set(values.filter(Boolean))).sort();
|
|
1153
|
+
}
|
|
1154
|
+
function countBy(items, key) {
|
|
1155
|
+
const counts = {};
|
|
1156
|
+
for (const item of items) {
|
|
1157
|
+
const value = key(item);
|
|
1158
|
+
counts[value] = (counts[value] || 0) + 1;
|
|
1159
|
+
}
|
|
1160
|
+
return counts;
|
|
1161
|
+
}
|
|
1162
|
+
function uniqueEdges(edges) {
|
|
1163
|
+
const seen = new Set();
|
|
1164
|
+
const result = [];
|
|
1165
|
+
for (const edge of edges) {
|
|
1166
|
+
const key = `${edge.from}\0${edge.to}\0${edge.label || ""}`;
|
|
1167
|
+
if (seen.has(key))
|
|
1168
|
+
continue;
|
|
1169
|
+
seen.add(key);
|
|
1170
|
+
result.push(edge);
|
|
1171
|
+
}
|
|
1172
|
+
return result;
|
|
1173
|
+
}
|