@wenathlan/extension 1.1.58 → 1.1.59
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/README.md +5 -4
- package/dist/coordination.d.ts +139 -0
- package/dist/coordination.d.ts.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +522 -1
- package/dist/index.js.map +4 -4
- package/dist/memory.d.ts +65 -1
- package/dist/memory.d.ts.map +1 -1
- package/dist/multiagent.d.ts +1 -1
- package/dist/multiagent.d.ts.map +1 -1
- package/dist/orchestration.d.ts +174 -0
- package/dist/orchestration.d.ts.map +1 -0
- package/dist/policy.d.ts +45 -1
- package/dist/policy.d.ts.map +1 -1
- package/dist/protocol.d.ts +36 -1
- package/dist/protocol.d.ts.map +1 -1
- package/dist/types.d.ts +277 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/version.d.ts +1 -1
- package/extension/dist/background.js +919 -55
- package/extension/dist/background.js.map +4 -4
- package/extension/dist/manifest.json +1 -1
- package/extension/dist/pagebridge.js.map +1 -1
- package/extension/dist/popup.html +1 -1
- package/extension/dist/popup.js +1 -1
- package/extension/dist/popup.js.map +2 -2
- package/extension/dist/sidepanel.js +555 -3
- package/extension/dist/sidepanel.js.map +3 -3
- package/extension/manifest.json +1 -1
- package/package.json +1 -1
|
@@ -1824,6 +1824,8 @@ function watchdogpass(input) {
|
|
|
1824
1824
|
function roledefaults(role) {
|
|
1825
1825
|
if (role === "planner") return { toolnamespaces: ["workflow", "memory", "system"], description: "Planners compose reviewed plans and read memory; they never act on the page themselves." };
|
|
1826
1826
|
if (role === "observer") return { toolnamespaces: ["memory", "system"], description: "Observers read the shared memory and the system reports only." };
|
|
1827
|
+
if (role === "critic") return { toolnamespaces: ["workflow", "memory", "system"], description: "Critics review the outputs of the other agents read only; they never act on the page themselves." };
|
|
1828
|
+
if (role === "verifier") return { toolnamespaces: ["browser", "memory", "system"], description: "Verifiers re-read the page to check the claims of the other agents; their checks stay read side." };
|
|
1827
1829
|
return { toolnamespaces: ["browser", "workflow", "memory", "system"], description: role === "worker" ? "Workers execute the reviewed steps of approved plans." : `The custom role ${role} carries the worker defaults until the user narrows its scope.` };
|
|
1828
1830
|
}
|
|
1829
1831
|
function registeragent(input) {
|
|
@@ -4133,6 +4135,129 @@ var sessionmemory = class {
|
|
|
4133
4135
|
const mailboxes = await this.getmailboxes();
|
|
4134
4136
|
return swarmoverview({ agents, queue: queue ?? { lanes: [], priorities: [], completionpolicy: "all", items: [], claims: [] }, mailboxes });
|
|
4135
4137
|
}
|
|
4138
|
+
/** Returns the leader worker topology of the 1.1.59 swarm with its leader, worker, critic and verifier lanes and its worker assignments. */
|
|
4139
|
+
async gettopology() {
|
|
4140
|
+
return this.adapter.get("swarmtopology");
|
|
4141
|
+
}
|
|
4142
|
+
/** Replaces the stored leader worker topology after one election, assignment, collection or scaling change. */
|
|
4143
|
+
async settopology(topology) {
|
|
4144
|
+
return this.adapter.set("swarmtopology", topology);
|
|
4145
|
+
}
|
|
4146
|
+
/** Returns the stored planner executor splits of the 1.1.59 swarm with their step reports. */
|
|
4147
|
+
async getplannersplits() {
|
|
4148
|
+
return await this.adapter.get("swarmsplits") ?? [];
|
|
4149
|
+
}
|
|
4150
|
+
/** Replaces the stored planner executor splits after one split or one executor step report. */
|
|
4151
|
+
async setplannersplits(splits) {
|
|
4152
|
+
return this.adapter.set("swarmsplits", splits);
|
|
4153
|
+
}
|
|
4154
|
+
/** Records one critic review of an agent output, newest first. */
|
|
4155
|
+
async addcriticreview(review) {
|
|
4156
|
+
await this.adapter.set("swarmreviews", [review, ...await this.adapter.get("swarmreviews") ?? []]);
|
|
4157
|
+
}
|
|
4158
|
+
/** Returns the recorded critic reviews, newest first. */
|
|
4159
|
+
async getcriticreviews() {
|
|
4160
|
+
return await this.adapter.get("swarmreviews") ?? [];
|
|
4161
|
+
}
|
|
4162
|
+
/** Records one verifier check of a result claim, newest first. */
|
|
4163
|
+
async addverifiercheck(check) {
|
|
4164
|
+
await this.adapter.set("swarmverifierchecks", [check, ...await this.adapter.get("swarmverifierchecks") ?? []]);
|
|
4165
|
+
}
|
|
4166
|
+
/** Returns the recorded verifier checks with their pass and fail outcomes, newest first. */
|
|
4167
|
+
async getverifierchecks() {
|
|
4168
|
+
return await this.adapter.get("swarmverifierchecks") ?? [];
|
|
4169
|
+
}
|
|
4170
|
+
/** Replaces the stored review requests routed between agents after one request, ack, answer or timeout. */
|
|
4171
|
+
async setreviewrequests(requests) {
|
|
4172
|
+
return this.adapter.set("swarmreviewrequests", requests);
|
|
4173
|
+
}
|
|
4174
|
+
/** Returns the stored review requests routed between agents. */
|
|
4175
|
+
async getreviewrequests() {
|
|
4176
|
+
return await this.adapter.get("swarmreviewrequests") ?? [];
|
|
4177
|
+
}
|
|
4178
|
+
/** Records one tab handoff with its packaged task state and its resumed state. */
|
|
4179
|
+
async addhandoff(record2) {
|
|
4180
|
+
await this.adapter.set("swarmhandoffs", [record2, ...await this.adapter.get("swarmhandoffs") ?? []].filter((entry, index, all) => all.findIndex((candidate) => candidate.id === entry.id) === index));
|
|
4181
|
+
}
|
|
4182
|
+
/** Replaces one stored handoff record after its transfer or resume. */
|
|
4183
|
+
async updatehandoff(record2) {
|
|
4184
|
+
await this.adapter.set("swarmhandoffs", (await this.adapter.get("swarmhandoffs") ?? []).map((entry) => entry.id === record2.id ? record2 : entry));
|
|
4185
|
+
}
|
|
4186
|
+
/** Returns the handoff log of tab transfers between agents, newest first. */
|
|
4187
|
+
async gethandoffs() {
|
|
4188
|
+
return await this.adapter.get("swarmhandoffs") ?? [];
|
|
4189
|
+
}
|
|
4190
|
+
/** Replaces the stored resource locks after one acquire, release or expiry sweep. */
|
|
4191
|
+
async setlocks(locks) {
|
|
4192
|
+
return this.adapter.set("swarmlocks", locks);
|
|
4193
|
+
}
|
|
4194
|
+
/** Returns the held resource locks with their holders and expiries. */
|
|
4195
|
+
async getlocks() {
|
|
4196
|
+
return await this.adapter.get("swarmlocks") ?? [];
|
|
4197
|
+
}
|
|
4198
|
+
/** Records one conflict scan report of overlapping writes, newest first. */
|
|
4199
|
+
async addconflictscan(scan) {
|
|
4200
|
+
await this.adapter.set("swarmconflicts", [scan, ...await this.adapter.get("swarmconflicts") ?? []]);
|
|
4201
|
+
}
|
|
4202
|
+
/** Returns the recorded conflict scan reports, newest first. */
|
|
4203
|
+
async getconflictscans() {
|
|
4204
|
+
return await this.adapter.get("swarmconflicts") ?? [];
|
|
4205
|
+
}
|
|
4206
|
+
/** Stores the merged result report with its mergeentry provenance. */
|
|
4207
|
+
async setreport(report) {
|
|
4208
|
+
return this.adapter.set("swarmreport", report);
|
|
4209
|
+
}
|
|
4210
|
+
/** Returns the stored merged result report across agents. */
|
|
4211
|
+
async getreport() {
|
|
4212
|
+
return this.adapter.get("swarmreport");
|
|
4213
|
+
}
|
|
4214
|
+
/** Records one progressboard snapshot under the user configured retention window; an absent window keeps every snapshot. */
|
|
4215
|
+
async addboardsnapshot(board) {
|
|
4216
|
+
const retention = (await this.getsettings())?.boardretention;
|
|
4217
|
+
await this.adapter.set("swarmboards", [board, ...await this.adapter.get("swarmboards") ?? []].slice(0, retention ?? 100));
|
|
4218
|
+
}
|
|
4219
|
+
/** Returns the stored progressboard snapshots, newest first. */
|
|
4220
|
+
async getboardsnapshots() {
|
|
4221
|
+
return await this.adapter.get("swarmboards") ?? [];
|
|
4222
|
+
}
|
|
4223
|
+
/** Records one escalation lifted to the user, newest first. */
|
|
4224
|
+
async addescalation(escalation) {
|
|
4225
|
+
await this.adapter.set("swarmescalations", [escalation, ...await this.adapter.get("swarmescalations") ?? []]);
|
|
4226
|
+
}
|
|
4227
|
+
/** Replaces one stored escalation after its user decision. */
|
|
4228
|
+
async updateescalation(escalation) {
|
|
4229
|
+
await this.adapter.set("swarmescalations", (await this.adapter.get("swarmescalations") ?? []).map((entry) => entry.id === escalation.id ? escalation : entry));
|
|
4230
|
+
}
|
|
4231
|
+
/** Returns the escalations awaiting the user and the decided ones, newest first. */
|
|
4232
|
+
async getescalations() {
|
|
4233
|
+
return await this.adapter.get("swarmescalations") ?? [];
|
|
4234
|
+
}
|
|
4235
|
+
/** Records one consensus round or replaces the stored one after a vote. */
|
|
4236
|
+
async setconsensusround(round) {
|
|
4237
|
+
const rounds = await this.adapter.get("swarmconsensus") ?? [];
|
|
4238
|
+
await this.adapter.set("swarmconsensus", rounds.some((entry) => entry.id === round.id) ? rounds.map((entry) => entry.id === round.id ? round : entry) : [round, ...rounds]);
|
|
4239
|
+
}
|
|
4240
|
+
/** Returns the consensus rounds with their votes and quorum states, newest first. */
|
|
4241
|
+
async getconsensusrounds() {
|
|
4242
|
+
return await this.adapter.get("swarmconsensus") ?? [];
|
|
4243
|
+
}
|
|
4244
|
+
/** Appends one action to the interleaved timeline of swarm actions, oldest first under a window of 500. */
|
|
4245
|
+
async addswarmaction(action) {
|
|
4246
|
+
await this.adapter.set("swarmtimeline", [...await this.adapter.get("swarmtimeline") ?? [], action].slice(-500));
|
|
4247
|
+
}
|
|
4248
|
+
/** Returns the interleaved timeline of swarm actions with the optional agent and kind filters, oldest first. */
|
|
4249
|
+
async getswarmtimeline(filters) {
|
|
4250
|
+
const actions = await this.adapter.get("swarmtimeline") ?? [];
|
|
4251
|
+
return actions.filter((action) => filters?.agentid === void 0 || action.agentid === filters.agentid).filter((action) => filters?.kind === void 0 || action.kind === filters.kind).filter((action) => filters?.since === void 0 || action.at >= filters.since);
|
|
4252
|
+
}
|
|
4253
|
+
/** Stores one shared cost accounting snapshot of the swarm, newest first. */
|
|
4254
|
+
async addswarmcost(cost) {
|
|
4255
|
+
await this.adapter.set("swarmcosts", [cost, ...await this.adapter.get("swarmcosts") ?? []].slice(0, 100));
|
|
4256
|
+
}
|
|
4257
|
+
/** Returns the stored shared cost accounting snapshots of the swarm, newest first. */
|
|
4258
|
+
async getswarmcosts() {
|
|
4259
|
+
return await this.adapter.get("swarmcosts") ?? [];
|
|
4260
|
+
}
|
|
4136
4261
|
};
|
|
4137
4262
|
function mediakindof(record2) {
|
|
4138
4263
|
if ("pages" in record2) return "pdf";
|
|
@@ -8940,6 +9065,68 @@ function blackboardconsentgrade(entry) {
|
|
|
8940
9065
|
if (entry.consentclass === "sensitive") return { allowed: true, reason: `The blackboard entry ${entry.key} inherits the sensitive class of its source extraction; every agent reads the class beside the value.` };
|
|
8941
9066
|
return { allowed: true, reason: `The blackboard entry ${entry.key} inherits the ${entry.consentclass} class of its source extraction; every agent reads the class beside the value.` };
|
|
8942
9067
|
}
|
|
9068
|
+
function leaderelectionvalid(input) {
|
|
9069
|
+
if (input.rule.kind !== "first" && input.rule.kind !== "named") return { allowed: false, reason: "The leader election rule stays first or named as the user configured it." };
|
|
9070
|
+
if (input.rule.kind === "named") {
|
|
9071
|
+
if (input.rule.agentid === void 0 || input.rule.agentid.trim() === "") return { allowed: false, reason: "The named leader election rule needs the agent id the user named." };
|
|
9072
|
+
if (!input.agents.some((agent) => agent.id === input.rule.agentid && agent.state !== "stopped")) return { allowed: false, reason: `The named leader election rule names the agent ${input.rule.agentid} which is not a live agent of the swarm.` };
|
|
9073
|
+
}
|
|
9074
|
+
return { allowed: true, reason: input.rule.kind === "first" ? "The first registration rule elects the leader exactly as the user configured." : `The named rule elects the agent ${input.rule.agentid} exactly as the user configured.` };
|
|
9075
|
+
}
|
|
9076
|
+
function criticreviewgrade(review) {
|
|
9077
|
+
if (review.reviewerid.trim() === "") return { allowed: false, reason: "The critic review needs its reviewing agent." };
|
|
9078
|
+
if (review.subjectagentid.trim() === "") return { allowed: false, reason: "The critic review needs the subject agent whose output it reviews." };
|
|
9079
|
+
if (review.verdict !== "approve" && review.verdict !== "changes" && review.verdict !== "reject") return { allowed: false, reason: "The critic review carries one of the three verdicts approve, changes or reject." };
|
|
9080
|
+
if (review.verdict === "changes" && review.requiredchanges.length === 0) return { allowed: false, reason: "A changes verdict needs its required changes in plain language." };
|
|
9081
|
+
if (review.verdict === "reject" && review.issues.length === 0) return { allowed: false, reason: "A reject verdict needs the issues the critic found." };
|
|
9082
|
+
return { allowed: true, reason: `The critic review of the output of ${review.subjectagentid} stays read only: the critic ${review.reviewerid} returns its ${review.verdict} verdict and never acts on the page; the rework still passes the same human review.` };
|
|
9083
|
+
}
|
|
9084
|
+
function verifiermethodgrade(input) {
|
|
9085
|
+
if (input.method.trim() === "") return { allowed: false, reason: "The verifier check needs the method it used." };
|
|
9086
|
+
if (input.allowed.length > 0 && !input.allowed.includes(input.method)) return { allowed: false, reason: `The verifier method ${input.method} is not one of the methods the user allowed: ${input.allowed.join(", ")}.` };
|
|
9087
|
+
return { allowed: true, reason: input.allowed.length === 0 ? `The verifier method ${input.method} runs under the documented open method list the user chose not to narrow.` : `The verifier method ${input.method} sits inside the methods the user allowed.` };
|
|
9088
|
+
}
|
|
9089
|
+
function handoffgrantgate(input) {
|
|
9090
|
+
if (input.record.toagentid.trim() === "" || input.record.fromagentid.trim() === "") return { allowed: false, reason: "The handoff names its transferring and receiving agents." };
|
|
9091
|
+
if (input.toscope === void 0) return { allowed: true, reason: `The receiving agent ${input.record.toagentid} carries no narrowed scope, so the handoff stays unbounded inside the original session grants.` };
|
|
9092
|
+
const outside = input.toscope.origins.filter((origin) => input.sessiongrants.length > 0 && !input.sessiongrants.includes(origin));
|
|
9093
|
+
if (outside.length > 0) return { allowed: false, reason: `The handoff to ${input.record.toagentid} would need the origins ${outside.join(", ")} which the session grant list does not carry; a tab transfer never widens the session grants.` };
|
|
9094
|
+
return { allowed: true, reason: `The handoff from ${input.record.fromagentid} to ${input.record.toagentid} preserves the original session grants; the receiving scope stays inside them.` };
|
|
9095
|
+
}
|
|
9096
|
+
function lockscopevalid(lock) {
|
|
9097
|
+
if (lock.key.trim() === "") return { allowed: false, reason: "The resource lock needs its key." };
|
|
9098
|
+
if (lock.origin.trim() === "" || lock.selector.trim() === "") return { allowed: false, reason: "The resource lock names exactly one origin and one selector; a lock never spans unrelated origins." };
|
|
9099
|
+
if (lock.key !== `${lock.origin}|${lock.selector}`) return { allowed: false, reason: "The lock key must compose of its one origin and its one selector so the scope never spans unrelated origins." };
|
|
9100
|
+
if (lock.kind !== "exclusive" && lock.kind !== "shared") return { allowed: false, reason: "The lock kind stays exclusive or shared." };
|
|
9101
|
+
return { allowed: true, reason: `The lock ${lock.key} spans exactly one target of one origin for the holder ${lock.holder}.` };
|
|
9102
|
+
}
|
|
9103
|
+
function conflictresolutiongrade(rule) {
|
|
9104
|
+
if (rule !== "first" && rule !== "last" && rule !== "preferagent" && rule !== "fail") return { allowed: false, reason: "The conflict resolution rule stays first, last, preferagent or fail as the user configured it." };
|
|
9105
|
+
if (rule === "last" || rule === "preferagent") return { allowed: true, reason: `The ${rule} conflict resolution rule overwrites one parallel value with another, so it grades sensitive and the merged report still passes the human review.` };
|
|
9106
|
+
return { allowed: true, reason: `The ${rule} conflict resolution rule keeps or refuses the parallel values without overwriting, so it grades read side.` };
|
|
9107
|
+
}
|
|
9108
|
+
function escalationgate(escalation) {
|
|
9109
|
+
if (escalation.agentid.trim() === "") return { allowed: false, reason: "The escalation names the agent whose decision it lifts." };
|
|
9110
|
+
if (escalation.subject.trim() === "") return { allowed: false, reason: "The escalation needs its subject." };
|
|
9111
|
+
if (escalation.context.trim() === "") return { allowed: false, reason: "The escalation needs its full context in plain language; the user decides on what the agent saw." };
|
|
9112
|
+
if (escalation.state === "decided" && (escalation.decision === void 0 || escalation.decision.trim() === "")) return { allowed: false, reason: "A decided escalation carries the decision the user wrote." };
|
|
9113
|
+
return { allowed: true, reason: `The escalation of ${escalation.agentid} stays human decided: the agent lifts the stalled decision with its full context and the user alone writes the outcome.` };
|
|
9114
|
+
}
|
|
9115
|
+
function consensusquorumvalid(input) {
|
|
9116
|
+
if (!Number.isInteger(input.quorum) || input.quorum < 1) return { allowed: false, reason: "The consensus quorum stays a positive whole number the user configured." };
|
|
9117
|
+
if (input.quorum > input.voters) return { allowed: false, reason: `The consensus quorum ${input.quorum} exceeds the ${input.voters} voting agents the user counted; an unreachable quorum never carries.` };
|
|
9118
|
+
return { allowed: true, reason: `The consensus quorum ${input.quorum} of ${input.voters} voting agents stays the user configured value with no engine default.` };
|
|
9119
|
+
}
|
|
9120
|
+
function workerscalevalid(bound) {
|
|
9121
|
+
if (bound === void 0) return { allowed: true, reason: "No worker bound is configured, so the worker scale stays the user choice alone with no engine cap." };
|
|
9122
|
+
if (!Number.isFinite(bound) || bound < 1) return { allowed: false, reason: "The worker scale bound stays a positive user value; no engine cap exists." };
|
|
9123
|
+
return { allowed: true, reason: `The worker scale bound ${bound} stays the user configured value; the scaling never passes it and no engine cap exists.` };
|
|
9124
|
+
}
|
|
9125
|
+
function mergeegressgrade(input) {
|
|
9126
|
+
if (input.report.title.trim() === "") return { allowed: false, reason: "The merged report needs its title before any export." };
|
|
9127
|
+
if (input.carriespagecontent) return { allowed: true, reason: `The export of the report ${input.report.title} carries page content from the sources ${input.report.sources.join(", ")} and grades as a data egress event in the audit trail.` };
|
|
9128
|
+
return { allowed: true, reason: `The export of the report ${input.report.title} carries no page content and stays a plain report export.` };
|
|
9129
|
+
}
|
|
8943
9130
|
|
|
8944
9131
|
// progress.ts
|
|
8945
9132
|
function emptyprogress(planid, now) {
|
|
@@ -9119,7 +9306,7 @@ function recordtoolcall(progress, planid, stepid, entry, now) {
|
|
|
9119
9306
|
}
|
|
9120
9307
|
|
|
9121
9308
|
// version.ts
|
|
9122
|
-
var packageversion = "1.1.
|
|
9309
|
+
var packageversion = "1.1.59";
|
|
9123
9310
|
|
|
9124
9311
|
// types.ts
|
|
9125
9312
|
var protocolversion = packageversion;
|
|
@@ -11517,6 +11704,376 @@ function budgetcheck(input) {
|
|
|
11517
11704
|
return { allowed: true, halted: false, asksuser: false };
|
|
11518
11705
|
}
|
|
11519
11706
|
|
|
11707
|
+
// orchestration.ts
|
|
11708
|
+
function electleader(input) {
|
|
11709
|
+
const live = input.agents.filter((agent) => agent.state !== "stopped");
|
|
11710
|
+
if (live.length === 0) throw new Error("The swarm holds no live agent; the leader election waits for the user to register one.");
|
|
11711
|
+
const rule = input.rule ?? { kind: "first" };
|
|
11712
|
+
let leader;
|
|
11713
|
+
if (rule.kind === "named") {
|
|
11714
|
+
if (rule.agentid === void 0 || rule.agentid.trim() === "") throw new Error("The named election rule needs the agent id the user named.");
|
|
11715
|
+
leader = live.find((agent) => agent.id === rule.agentid);
|
|
11716
|
+
if (!leader) throw new Error(`The named election rule names the agent ${rule.agentid} which is not a live agent of the swarm.`);
|
|
11717
|
+
} else {
|
|
11718
|
+
leader = live[live.length - 1];
|
|
11719
|
+
}
|
|
11720
|
+
if (!leader) throw new Error("The leader election found no live agent.");
|
|
11721
|
+
const leaderid = leader.id;
|
|
11722
|
+
const workers = live.filter((agent) => agent.id !== leaderid && agent.role === "worker");
|
|
11723
|
+
const critics = live.filter((agent) => agent.id !== leaderid && agent.role === "critic");
|
|
11724
|
+
const verifiers = live.filter((agent) => agent.id !== leaderid && agent.role === "verifier");
|
|
11725
|
+
return {
|
|
11726
|
+
id: input.id,
|
|
11727
|
+
leaderid: leader.id,
|
|
11728
|
+
workerids: workers.map((agent) => agent.id),
|
|
11729
|
+
criticids: critics.map((agent) => agent.id),
|
|
11730
|
+
verifierids: verifiers.map((agent) => agent.id),
|
|
11731
|
+
assignments: [],
|
|
11732
|
+
rule: { kind: rule.kind, ...rule.agentid !== void 0 ? { agentid: rule.agentid } : {} },
|
|
11733
|
+
electedat: input.now
|
|
11734
|
+
};
|
|
11735
|
+
}
|
|
11736
|
+
function assignwork(input) {
|
|
11737
|
+
if (input.topology.workerids.length === 0) throw new Error("The topology holds no worker; the user adds workers before the assignment.");
|
|
11738
|
+
const assignments = [];
|
|
11739
|
+
const tasks = input.tasks.filter((task) => task.state === "queued" || task.state === "claimed");
|
|
11740
|
+
tasks.forEach((task, index) => {
|
|
11741
|
+
const workerid = input.topology.workerids[index % input.topology.workerids.length];
|
|
11742
|
+
assignments.push({ workerid, taskid: task.id, slice: `${task.payload} (slice ${Math.floor(index / input.topology.workerids.length) + 1} of lane ${task.lane})`, assignedat: input.now });
|
|
11743
|
+
});
|
|
11744
|
+
return { ...input.topology, assignments };
|
|
11745
|
+
}
|
|
11746
|
+
function collectresults(input) {
|
|
11747
|
+
const gathered = input.topology.assignments.map((assignment) => {
|
|
11748
|
+
const output = input.outputs.find((entry) => entry.taskid === assignment.taskid && entry.workerid === assignment.workerid);
|
|
11749
|
+
return output ?? { workerid: assignment.workerid, taskid: assignment.taskid, state: "pending", summary: `The worker ${assignment.workerid} has not returned its slice of the task ${assignment.taskid} yet.` };
|
|
11750
|
+
});
|
|
11751
|
+
return { gathered, missing: gathered.filter((entry) => entry.state === "pending").map((entry) => `${entry.workerid}:${entry.taskid}`) };
|
|
11752
|
+
}
|
|
11753
|
+
function scaleworkers(input) {
|
|
11754
|
+
const live = input.agents.filter((agent) => agent.state === "active" && agent.role === "worker" && agent.id !== input.topology.leaderid);
|
|
11755
|
+
const current = input.topology.workerids.filter((workerid) => live.some((agent) => agent.id === workerid));
|
|
11756
|
+
const ceiling = input.bound;
|
|
11757
|
+
if (input.pending > current.length) {
|
|
11758
|
+
const available = live.filter((agent) => !current.includes(agent.id)).map((agent) => agent.id);
|
|
11759
|
+
const wanted = input.pending - current.length;
|
|
11760
|
+
const addable = ceiling === void 0 ? available.slice(0, wanted) : available.slice(0, Math.min(wanted, Math.max(ceiling - current.length, 0)));
|
|
11761
|
+
if (addable.length === 0) return { topology: input.topology, added: [], retired: [], reason: ceiling === void 0 ? `The load of ${input.pending} pending slices exceeds the ${current.length} workers but the swarm holds no further live worker role agent to add.` : `The load of ${input.pending} pending slices exceeds the ${current.length} workers but the user configured bound of ${ceiling} workers holds.` };
|
|
11762
|
+
return { topology: { ...input.topology, workerids: [...current, ...addable], electedat: input.topology.electedat, assignments: input.topology.assignments }, added: addable, retired: [], reason: `The load of ${input.pending} pending slices added the workers ${addable.join(", ")}; ${ceiling === void 0 ? "no bound is configured so the user scale stands alone" : `the user configured bound of ${ceiling} workers holds`}.` };
|
|
11763
|
+
}
|
|
11764
|
+
const keep = Math.max(input.pending, 0);
|
|
11765
|
+
if (current.length > keep) {
|
|
11766
|
+
const retired = current.slice(keep);
|
|
11767
|
+
return { topology: { ...input.topology, workerids: current.slice(0, keep), electedat: input.topology.electedat, assignments: input.topology.assignments.filter((assignment) => !retired.includes(assignment.workerid)) }, added: [], retired, reason: `The load of ${input.pending} pending slices retired the idle workers ${retired.join(", ")}.` };
|
|
11768
|
+
}
|
|
11769
|
+
return { topology: input.topology, added: [], retired: [], reason: `The load of ${input.pending} pending slices matches the ${current.length} workers; the scale stays unchanged.` };
|
|
11770
|
+
}
|
|
11771
|
+
function plannersplit(input) {
|
|
11772
|
+
if (input.planownerid.trim() === "" || input.runownerid.trim() === "") throw new Error("The planner executor split needs its plan owner and run owner agent ids.");
|
|
11773
|
+
if (input.planownerid === input.runownerid) throw new Error("The planner executor split keeps plan drafting and execution in different agents; one agent holds both sides never.");
|
|
11774
|
+
return { id: input.id, planownerid: input.planownerid, runownerid: input.runownerid, ...input.taskid !== void 0 && input.taskid.trim() !== "" ? { taskid: input.taskid } : {}, stepreports: [], splitat: input.now };
|
|
11775
|
+
}
|
|
11776
|
+
function reportstep(input) {
|
|
11777
|
+
if (input.stepid.trim() === "") throw new Error("The executor report needs its step id.");
|
|
11778
|
+
if (input.detail.trim() === "") throw new Error("The executor report needs its detail in plain language.");
|
|
11779
|
+
const report = { stepid: input.stepid.trim(), outcome: input.outcome, detail: input.detail, reportedat: input.now };
|
|
11780
|
+
return { ...input.split, stepreports: [...input.split.stepreports.filter((entry) => entry.stepid !== report.stepid), report] };
|
|
11781
|
+
}
|
|
11782
|
+
function requestreview(input) {
|
|
11783
|
+
if (input.subject.trim() === "") throw new Error("The review request needs its subject.");
|
|
11784
|
+
if (input.payload.trim() === "") throw new Error("The review request needs its payload.");
|
|
11785
|
+
if (input.toagentid.trim() === "" || input.toagentid === input.fromagentid) throw new Error("The review request names another reviewing agent, never its own requester.");
|
|
11786
|
+
const request = { id: input.id, fromagentid: input.fromagentid, toagentid: input.toagentid, subject: input.subject, payload: input.payload, state: "open", requestedat: input.now, ...input.timeoutms !== void 0 ? { timeoutat: input.now + input.timeoutms } : {} };
|
|
11787
|
+
return [request, ...input.requests];
|
|
11788
|
+
}
|
|
11789
|
+
function ackreview(input) {
|
|
11790
|
+
const request = input.requests.find((entry) => entry.id === input.id);
|
|
11791
|
+
if (!request) throw new Error(`The review request ${input.id} does not exist.`);
|
|
11792
|
+
if (request.state !== "open") throw new Error(`The review request ${input.id} is ${request.state}; only an open request receives its ack.`);
|
|
11793
|
+
return input.requests.map((entry) => entry.id === input.id ? { ...entry, state: "acked", ackedat: input.now } : entry);
|
|
11794
|
+
}
|
|
11795
|
+
function applyreview(input) {
|
|
11796
|
+
const request = input.requests.find((entry) => entry.id === input.id);
|
|
11797
|
+
if (!request) throw new Error(`The review request ${input.id} does not exist.`);
|
|
11798
|
+
if (request.state === "answered" || request.state === "timeout") throw new Error(`The review request ${input.id} is ${request.state}; an answered or timed out request never reviews again.`);
|
|
11799
|
+
if (request.toagentid !== input.reviewerid) throw new Error(`The review request ${input.id} routes to the agent ${request.toagentid}; the agent ${input.reviewerid} never answers in its place.`);
|
|
11800
|
+
if (input.verdict === "changes" && input.requiredchanges.length === 0) throw new Error("A changes verdict needs its required changes in plain language.");
|
|
11801
|
+
const review = { id: request.id, reviewerid: input.reviewerid, subjectagentid: request.fromagentid, ...input.taskid !== void 0 && input.taskid.trim() !== "" ? { taskid: input.taskid } : {}, verdict: input.verdict, issues: input.issues, requiredchanges: input.requiredchanges, reviewedat: input.now };
|
|
11802
|
+
return { review, requests: input.requests.map((entry) => entry.id === input.id ? { ...entry, state: "answered", answeredat: input.now } : entry) };
|
|
11803
|
+
}
|
|
11804
|
+
function sweepreviews(input) {
|
|
11805
|
+
const expired = input.requests.filter((request) => request.state === "open" || request.state === "acked").filter((request) => request.timeoutat !== void 0 && input.now > request.timeoutat);
|
|
11806
|
+
if (expired.length === 0) return { requests: input.requests, timedout: [] };
|
|
11807
|
+
const ids = new Set(expired.map((request) => request.id));
|
|
11808
|
+
return { requests: input.requests.map((request) => ids.has(request.id) ? { ...request, state: "timeout" } : request), timedout: [...ids] };
|
|
11809
|
+
}
|
|
11810
|
+
function checkclaim(input) {
|
|
11811
|
+
if (input.claim.trim() === "") throw new Error("The verifier check needs its claim in plain language.");
|
|
11812
|
+
if (input.method.trim() === "") throw new Error("The verifier check needs the method the user configured.");
|
|
11813
|
+
if (input.claimagentid.trim() === "") throw new Error("The verifier check names the agent whose claim it checks.");
|
|
11814
|
+
return { id: input.id, verifierid: input.verifierid, claimagentid: input.claimagentid, ...input.taskid !== void 0 && input.taskid.trim() !== "" ? { taskid: input.taskid } : {}, claim: input.claim, method: input.method, outcome: input.outcome, ...input.evidence !== void 0 && input.evidence.trim() !== "" ? { evidence: input.evidence } : {}, checkedat: input.now };
|
|
11815
|
+
}
|
|
11816
|
+
function boardstate(input) {
|
|
11817
|
+
const lanes = input.agents.filter((agent) => agent.state !== "stopped").map((agent) => {
|
|
11818
|
+
const assignment = input.topology?.assignments.find((entry) => entry.workerid === agent.id);
|
|
11819
|
+
const claim2 = input.queue.claims.find((record2) => record2.agentid === agent.id && input.queue.items.some((item) => item.id === record2.taskid && item.state === "claimed"));
|
|
11820
|
+
const task = claim2 !== void 0 ? input.queue.items.find((item) => item.id === claim2.taskid) : void 0;
|
|
11821
|
+
const lane = task?.lane ?? (agent.role === "critic" || agent.role === "verifier" ? agent.role : agent.role === "planner" ? "planning" : "idle");
|
|
11822
|
+
return { agentid: agent.id, name: agent.name, role: agent.role, state: agent.state, lane, ...task !== void 0 ? { currenttask: task.payload } : assignment !== void 0 ? { currenttask: assignment.slice } : {}, milestones: input.milestones?.[agent.id] ?? [] };
|
|
11823
|
+
});
|
|
11824
|
+
return { id: `board:${input.now}`, lanes, builtat: input.now };
|
|
11825
|
+
}
|
|
11826
|
+
function escalate(input) {
|
|
11827
|
+
if (input.subject.trim() === "") throw new Error("The escalation needs its subject.");
|
|
11828
|
+
if (input.context.trim() === "") throw new Error("The escalation needs its full context in plain language; the user decides on what the agent saw.");
|
|
11829
|
+
if (input.agentid.trim() === "") throw new Error("The escalation names the agent whose decision it lifts.");
|
|
11830
|
+
return { id: input.id, agentid: input.agentid, subject: input.subject, context: input.context, state: "open", raisedat: input.now };
|
|
11831
|
+
}
|
|
11832
|
+
function resolveescalation(input) {
|
|
11833
|
+
if (input.escalation.state === "decided") throw new Error("The escalation already carries its user decision.");
|
|
11834
|
+
if (input.decision.trim() === "") throw new Error("The escalation decision needs the words the user wrote.");
|
|
11835
|
+
return { ...input.escalation, state: "decided", decision: input.decision, decidedat: input.now };
|
|
11836
|
+
}
|
|
11837
|
+
function arbitrate(input) {
|
|
11838
|
+
if (input.claims.length === 0) return [];
|
|
11839
|
+
if (input.rule.strategy === "priority") {
|
|
11840
|
+
const order = input.rule.priorityorder;
|
|
11841
|
+
return [...input.claims].sort((one, two) => {
|
|
11842
|
+
const oneindex = order.indexOf(one.agentid);
|
|
11843
|
+
const twoindex = order.indexOf(two.agentid);
|
|
11844
|
+
return (oneindex === -1 ? order.length : oneindex) - (twoindex === -1 ? order.length : twoindex) || one.claimedat - two.claimedat;
|
|
11845
|
+
}).map((claim2) => claim2.agentid);
|
|
11846
|
+
}
|
|
11847
|
+
if (input.rule.strategy === "age") return [...input.claims].sort((one, two) => one.claimedat - two.claimedat || (one.agentid < two.agentid ? -1 : 1)).map((claim2) => claim2.agentid);
|
|
11848
|
+
if (input.leaderid === void 0) throw new Error("The leader arbitration strategy needs the elected leader of the topology.");
|
|
11849
|
+
return [...input.claims].sort((one, two) => (one.agentid === input.leaderid ? -1 : 1) - (two.agentid === input.leaderid ? -1 : 1) || one.claimedat - two.claimedat).map((claim2) => claim2.agentid);
|
|
11850
|
+
}
|
|
11851
|
+
function openconsensus(input) {
|
|
11852
|
+
if (input.subject.trim() === "") throw new Error("The consensus round needs its subject in plain language.");
|
|
11853
|
+
if (!Number.isInteger(input.quorum) || input.quorum < 1) throw new Error("The consensus round needs its quorum as a positive whole number the user configured.");
|
|
11854
|
+
return { id: input.id, subject: input.subject, votes: [], quorum: input.quorum, state: "open", openedat: input.now };
|
|
11855
|
+
}
|
|
11856
|
+
function castvote(input) {
|
|
11857
|
+
if (input.round.state !== "open") throw new Error(`The consensus round ${input.round.id} is ${input.round.state}; a closed round collects no vote.`);
|
|
11858
|
+
if (input.round.votes.some((entry) => entry.agentid === input.agentid)) throw new Error(`The agent ${input.agentid} already voted in the round ${input.round.id}.`);
|
|
11859
|
+
const votes = [...input.round.votes, { agentid: input.agentid, vote: input.vote, votedat: input.now }];
|
|
11860
|
+
const yes = votes.filter((entry) => entry.vote === "yes").length;
|
|
11861
|
+
const no = votes.filter((entry) => entry.vote === "no").length;
|
|
11862
|
+
if (yes >= input.round.quorum) return { ...input.round, votes, state: "carried", closedat: input.now };
|
|
11863
|
+
if (no >= input.round.quorum) return { ...input.round, votes, state: "failed", closedat: input.now };
|
|
11864
|
+
return { ...input.round, votes };
|
|
11865
|
+
}
|
|
11866
|
+
function consensusstate(round) {
|
|
11867
|
+
return {
|
|
11868
|
+
yes: round.votes.filter((entry) => entry.vote === "yes").length,
|
|
11869
|
+
no: round.votes.filter((entry) => entry.vote === "no").length,
|
|
11870
|
+
abstain: round.votes.filter((entry) => entry.vote === "abstain").length,
|
|
11871
|
+
quorum: round.quorum,
|
|
11872
|
+
state: round.state
|
|
11873
|
+
};
|
|
11874
|
+
}
|
|
11875
|
+
|
|
11876
|
+
// blackboard.ts
|
|
11877
|
+
var blackboardsections = ["goals", "facts", "findings", "scratch"];
|
|
11878
|
+
function emptyboard(sections) {
|
|
11879
|
+
return { sections: sections ?? blackboardsections, entries: [] };
|
|
11880
|
+
}
|
|
11881
|
+
function postentry(input) {
|
|
11882
|
+
if (input.key.trim() === "") throw new Error("The blackboard entry needs its key.");
|
|
11883
|
+
if (input.value.trim() === "") throw new Error("The blackboard entry needs its value.");
|
|
11884
|
+
if (input.author.trim() === "") throw new Error("The blackboard entry needs its author.");
|
|
11885
|
+
if (!blackboardsections.includes(input.section)) throw new Error(`The section ${input.section} is not one of the shared blackboard sections.`);
|
|
11886
|
+
if (input.board.entries.some((entry2) => entry2.id === input.id)) throw new Error(`The blackboard entry id ${input.id} already exists.`);
|
|
11887
|
+
if (input.valuekind === "json") {
|
|
11888
|
+
try {
|
|
11889
|
+
JSON.parse(input.value);
|
|
11890
|
+
} catch {
|
|
11891
|
+
throw new Error("The json blackboard entry needs a well-formed json value.");
|
|
11892
|
+
}
|
|
11893
|
+
}
|
|
11894
|
+
const entry = { id: input.id, key: input.key.trim(), valuekind: input.valuekind ?? "text", value: input.value, author: input.author, section: input.section, consentclass: input.consentclass ?? "read", postedat: input.now };
|
|
11895
|
+
return { ...input.board, sections: input.board.sections.includes(input.section) ? input.board.sections : [...input.board.sections, input.section], entries: [entry, ...input.board.entries] };
|
|
11896
|
+
}
|
|
11897
|
+
function entryfresh(entry, now, window2) {
|
|
11898
|
+
if (entry.retiredat !== void 0) return false;
|
|
11899
|
+
if (window2 === void 0) return true;
|
|
11900
|
+
return now - entry.postedat <= window2;
|
|
11901
|
+
}
|
|
11902
|
+
function readentries(input) {
|
|
11903
|
+
return input.board.entries.filter((entry) => entry.retiredat === void 0).filter((entry) => input.section === void 0 || entry.section === input.section).filter((entry) => entryfresh(entry, input.now, input.freshness)).sort((one, two) => two.postedat - one.postedat);
|
|
11904
|
+
}
|
|
11905
|
+
function retireentries(input) {
|
|
11906
|
+
if (input.board.retirementwindow === void 0) return { board: input.board, retired: [] };
|
|
11907
|
+
const stale = input.board.entries.filter((entry) => entry.retiredat === void 0 && input.now - entry.postedat > input.board.retirementwindow);
|
|
11908
|
+
if (stale.length === 0) return { board: input.board, retired: [] };
|
|
11909
|
+
const staleids = new Set(stale.map((entry) => entry.id));
|
|
11910
|
+
return {
|
|
11911
|
+
board: { ...input.board, entries: input.board.entries.map((entry) => staleids.has(entry.id) ? { ...entry, retiredat: input.now } : entry) },
|
|
11912
|
+
retired: [...staleids]
|
|
11913
|
+
};
|
|
11914
|
+
}
|
|
11915
|
+
function retireentry(input) {
|
|
11916
|
+
const entry = input.board.entries.find((candidate) => candidate.id === input.entryid);
|
|
11917
|
+
if (!entry) throw new Error(`The blackboard entry ${input.entryid} does not exist.`);
|
|
11918
|
+
if (entry.retiredat !== void 0) throw new Error(`The blackboard entry ${entry.key} is already retired.`);
|
|
11919
|
+
return { ...input.board, entries: input.board.entries.map((candidate) => candidate.id === input.entryid ? { ...candidate, retiredat: input.now } : candidate) };
|
|
11920
|
+
}
|
|
11921
|
+
function boardsummary(board, now) {
|
|
11922
|
+
return board.sections.map((section) => {
|
|
11923
|
+
const live = board.entries.filter((entry) => entry.section === section && entry.retiredat === void 0);
|
|
11924
|
+
return { section, entries: live.length, authors: [...new Set(live.map((entry) => entry.author))], ...live.length > 0 ? { freshestat: Math.max(...live.map((entry) => entry.postedat)) } : {} };
|
|
11925
|
+
});
|
|
11926
|
+
}
|
|
11927
|
+
|
|
11928
|
+
// coordination.ts
|
|
11929
|
+
function lockkey(origin, selector) {
|
|
11930
|
+
return `${origin}|${selector}`;
|
|
11931
|
+
}
|
|
11932
|
+
function preparehandoff(input) {
|
|
11933
|
+
if (!input.agents.some((agent) => agent.id === input.fromagentid)) throw new Error(`The handoff names the transferring agent ${input.fromagentid} which is not registered.`);
|
|
11934
|
+
if (!input.agents.some((agent) => agent.id === input.toagentid)) throw new Error(`The handoff names the receiving agent ${input.toagentid} which is not registered.`);
|
|
11935
|
+
if (input.fromagentid === input.toagentid) throw new Error("A handoff moves a task between two different agents; an agent never hands off to itself.");
|
|
11936
|
+
if (input.taskstate.trim() === "") throw new Error("The handoff needs its packaged task state in plain language; the resume continues exactly from it.");
|
|
11937
|
+
const from = input.agents.find((agent) => agent.id === input.fromagentid);
|
|
11938
|
+
const tabid2 = input.tabid ?? from.tabid;
|
|
11939
|
+
if (tabid2 === void 0) throw new Error("The handoff needs its tab id; the transferring agent holds no tab to hand off.");
|
|
11940
|
+
return { id: input.id, fromagentid: input.fromagentid, toagentid: input.toagentid, tabid: tabid2, taskstate: input.taskstate, state: "prepared", ...input.reason !== void 0 && input.reason.trim() !== "" ? { reason: input.reason } : {}, createdat: input.now };
|
|
11941
|
+
}
|
|
11942
|
+
function transferhandoff(input) {
|
|
11943
|
+
const record2 = input.handoffs.find((entry) => entry.id === input.id);
|
|
11944
|
+
if (!record2) throw new Error(`The handoff ${input.id} does not exist.`);
|
|
11945
|
+
if (record2.state !== "prepared") throw new Error(`The handoff ${record2.id} is ${record2.state}; only a prepared handoff transfers.`);
|
|
11946
|
+
const receiver = input.agents.find((agent) => agent.id === record2.toagentid);
|
|
11947
|
+
if (!receiver) throw new Error(`The receiving agent ${record2.toagentid} is not registered.`);
|
|
11948
|
+
if (receiver.state === "stopped") throw new Error(`The receiving agent ${receiver.name} is stopped; the handoff waits for its resume or another receiver.`);
|
|
11949
|
+
const holder = input.agents.find((agent) => agent.tabid === record2.tabid && agent.id !== record2.fromagentid && agent.state !== "stopped");
|
|
11950
|
+
if (holder) throw new Error(`Tab ${record2.tabid} already holds the agent ${holder.name}; one tab binds one agent.`);
|
|
11951
|
+
const agents = input.agents.map((agent) => {
|
|
11952
|
+
if (agent.id === record2.fromagentid) {
|
|
11953
|
+
const { tabid: tabid2, ...rest } = agent;
|
|
11954
|
+
void tabid2;
|
|
11955
|
+
return rest;
|
|
11956
|
+
}
|
|
11957
|
+
if (agent.id === record2.toagentid && record2.tabid !== void 0) return { ...agent, tabid: record2.tabid };
|
|
11958
|
+
return agent;
|
|
11959
|
+
});
|
|
11960
|
+
return { agents, handoffs: input.handoffs.map((entry) => entry.id === input.id ? { ...entry, state: "transferred", transferredat: input.now } : entry) };
|
|
11961
|
+
}
|
|
11962
|
+
function resumehandoff(input) {
|
|
11963
|
+
const record2 = input.handoffs.find((entry) => entry.id === input.id);
|
|
11964
|
+
if (!record2) throw new Error(`The handoff ${input.id} does not exist.`);
|
|
11965
|
+
if (record2.state !== "transferred") throw new Error(`The handoff ${record2.id} is ${record2.state}; only a transferred handoff resumes.`);
|
|
11966
|
+
return { ...record2, state: "resumed", resumedat: input.now };
|
|
11967
|
+
}
|
|
11968
|
+
function acquirelock(input) {
|
|
11969
|
+
if (input.holder.trim() === "") throw new Error("The lock needs its holder agent id.");
|
|
11970
|
+
if (input.origin.trim() === "") throw new Error("The lock needs its origin; a lock never spans unrelated origins.");
|
|
11971
|
+
if (input.selector.trim() === "") throw new Error("The lock needs its selector of the origin.");
|
|
11972
|
+
const kind = input.kind ?? "exclusive";
|
|
11973
|
+
const key = lockkey(input.origin.trim(), input.selector.trim());
|
|
11974
|
+
const held = input.locks.filter((lock2) => lock2.key === key);
|
|
11975
|
+
if (held.some((lock2) => lock2.holder === input.holder)) return { locks: input.locks, acquired: false, reason: `The agent ${input.holder} already holds the lock ${key}.` };
|
|
11976
|
+
if (held.length > 0) {
|
|
11977
|
+
if (held.some((lock2) => lock2.kind === "exclusive") || kind === "exclusive") return { locks: input.locks, acquired: false, reason: `The lock ${key} is held ${held.some((lock2) => lock2.kind === "exclusive") ? "exclusively" : "shared"}; the ${kind} request of ${input.holder} refuses.` };
|
|
11978
|
+
}
|
|
11979
|
+
const lock = { key, holder: input.holder, kind, origin: input.origin.trim(), selector: input.selector.trim(), acquiredat: input.now, ...input.expiresat !== void 0 ? { expiresat: input.expiresat } : {} };
|
|
11980
|
+
return { locks: [...input.locks, lock], acquired: true, reason: `The ${kind} lock ${key} went to the agent ${input.holder}.` };
|
|
11981
|
+
}
|
|
11982
|
+
function releaselock(input) {
|
|
11983
|
+
const lock = input.locks.find((entry) => entry.key === input.key && entry.holder === input.holder);
|
|
11984
|
+
if (!lock) return { locks: input.locks, released: false };
|
|
11985
|
+
return { locks: input.locks.filter((entry) => entry.key !== input.key || entry.holder !== input.holder), released: true };
|
|
11986
|
+
}
|
|
11987
|
+
function expirelocks(input) {
|
|
11988
|
+
const stale = input.locks.filter((lock) => lock.expiresat !== void 0 && input.now > lock.expiresat);
|
|
11989
|
+
if (stale.length === 0) return { locks: input.locks, expired: [] };
|
|
11990
|
+
const keys = new Set(stale.map((lock) => `${lock.key}:${lock.holder}`));
|
|
11991
|
+
return { locks: input.locks.filter((lock) => !keys.has(`${lock.key}:${lock.holder}`)), expired: [...keys] };
|
|
11992
|
+
}
|
|
11993
|
+
function scanconflicts(input) {
|
|
11994
|
+
const targets = /* @__PURE__ */ new Map();
|
|
11995
|
+
for (const writer of input.writers) {
|
|
11996
|
+
const key = lockkey(writer.origin, writer.selector);
|
|
11997
|
+
targets.set(key, [...targets.get(key) ?? [], writer]);
|
|
11998
|
+
}
|
|
11999
|
+
const overlaps = [...targets.entries()].filter(([, writers]) => writers.length > 1).map(([key, writers]) => ({ origin: writers[0].origin, selector: writers[0].selector, writers: writers.map((writer) => writer.agentid) }));
|
|
12000
|
+
const overlappingagents = new Set(overlaps.flatMap((entry) => entry.writers));
|
|
12001
|
+
return {
|
|
12002
|
+
id: input.id,
|
|
12003
|
+
writers: input.writers,
|
|
12004
|
+
overlaps,
|
|
12005
|
+
suggestedorder: input.writers.filter((writer) => overlappingagents.has(writer.agentid)).map((writer) => writer.agentid).filter((agentid, index, all) => all.indexOf(agentid) === index).sort((one, two) => one < two ? -1 : 1),
|
|
12006
|
+
clean: overlaps.length === 0,
|
|
12007
|
+
scannedat: input.now
|
|
12008
|
+
};
|
|
12009
|
+
}
|
|
12010
|
+
function mergeresults(input) {
|
|
12011
|
+
const keys = /* @__PURE__ */ new Map();
|
|
12012
|
+
for (const entry of input.entries) {
|
|
12013
|
+
keys.set(entry.key, [...keys.get(entry.key) ?? [], entry]);
|
|
12014
|
+
}
|
|
12015
|
+
const conflicts = [];
|
|
12016
|
+
const merged = [];
|
|
12017
|
+
for (const [key, entries] of keys) {
|
|
12018
|
+
const ordered = [...entries].sort((one, two) => one.mergedat - two.mergedat);
|
|
12019
|
+
if (ordered.length === 1) {
|
|
12020
|
+
merged.push(ordered[0]);
|
|
12021
|
+
continue;
|
|
12022
|
+
}
|
|
12023
|
+
if (input.rule === "fail") {
|
|
12024
|
+
conflicts.push(`The key ${key} carries ${ordered.length} parallel values from ${ordered.map((entry) => entry.agentid).join(", ")}; the fail rule refuses the fold.`);
|
|
12025
|
+
continue;
|
|
12026
|
+
}
|
|
12027
|
+
const winner = input.rule === "first" ? ordered[0] : input.rule === "last" ? ordered[ordered.length - 1] : ordered.find((entry) => entry.agentid === input.preferagent) ?? ordered[ordered.length - 1];
|
|
12028
|
+
const note = input.rule === "preferagent" && input.preferagent !== void 0 && !ordered.some((entry) => entry.agentid === input.preferagent) ? `The preferagent rule names the agent ${input.preferagent} which wrote no value; the latest value of ${winner.agentid} stayed.` : `The ${input.rule} rule kept the value of ${winner.agentid} from ${ordered.map((entry) => entry.agentid).join(", ")}.`;
|
|
12029
|
+
conflicts.push(`The key ${key}: ${note}`);
|
|
12030
|
+
merged.push({ ...winner, id: `${winner.id}:merged`, conflict: note });
|
|
12031
|
+
}
|
|
12032
|
+
return { entries: merged, conflicts, refused: input.rule === "fail" && conflicts.length > 0 };
|
|
12033
|
+
}
|
|
12034
|
+
function swarmreport(input) {
|
|
12035
|
+
if (input.title.trim() === "") throw new Error("The report needs its title.");
|
|
12036
|
+
const fold = mergeresults({ entries: input.outputs, rule: input.rule, ...input.preferagent !== void 0 ? { preferagent: input.preferagent } : {}, now: input.now });
|
|
12037
|
+
const groups = /* @__PURE__ */ new Map();
|
|
12038
|
+
for (const entry of fold.entries) {
|
|
12039
|
+
const group = entry.taskid ?? "general";
|
|
12040
|
+
groups.set(group, [...groups.get(group) ?? [], entry]);
|
|
12041
|
+
}
|
|
12042
|
+
const sections = [...groups.entries()].map(([taskid, entries]) => ({ title: `Task ${taskid}`, entries, sources: [...new Set(input.outputs.filter((output) => (output.taskid ?? "general") === taskid).map((output) => output.agentid))] }));
|
|
12043
|
+
return {
|
|
12044
|
+
report: { id: input.id, title: input.title.trim(), sections, sources: [...new Set(input.outputs.map((output) => output.agentid))], ...input.confidence !== void 0 && input.confidence.trim() !== "" ? { confidence: input.confidence } : {}, createdat: input.now },
|
|
12045
|
+
conflicts: fold.conflicts,
|
|
12046
|
+
refused: fold.refused
|
|
12047
|
+
};
|
|
12048
|
+
}
|
|
12049
|
+
function compareoutputs(input) {
|
|
12050
|
+
if (input.subject.trim() === "") throw new Error("The comparison needs its subject.");
|
|
12051
|
+
if (input.outputs.length < 2) throw new Error("The comparison contrasts at least two competing outputs.");
|
|
12052
|
+
const differences = input.outputs.filter((output) => output.value !== input.outputs[0]?.value).map((output) => `The agent ${output.agentid} answers ${output.value} while the agent ${input.outputs[0].agentid} answers ${input.outputs[0].value}.`);
|
|
12053
|
+
return { id: input.id, subject: input.subject, outputs: input.outputs, differences, comparedat: input.now };
|
|
12054
|
+
}
|
|
12055
|
+
function interleavetimeline(actions) {
|
|
12056
|
+
return [...actions].sort((one, two) => one.at - two.at || (one.id < two.id ? -1 : 1));
|
|
12057
|
+
}
|
|
12058
|
+
function sharelesson(input) {
|
|
12059
|
+
if (input.statement.trim() === "") throw new Error("The lesson needs its statement in plain language.");
|
|
12060
|
+
if (input.verifiedby.trim() === "") throw new Error("The lesson needs its verifier; only a verified lesson lands on the board.");
|
|
12061
|
+
return postentry({ board: input.board, id: input.id, key: `lesson:${input.statement.trim().slice(0, 40)}`, value: `${input.statement.trim()} (verified by ${input.verifiedby.trim()})`, section: input.section ?? "findings", author: input.agentid.trim() === "" ? "user" : input.agentid, consentclass: input.consentclass ?? "read", now: input.now });
|
|
12062
|
+
}
|
|
12063
|
+
function swarmcosts(input) {
|
|
12064
|
+
return {
|
|
12065
|
+
agents: input.usage.length,
|
|
12066
|
+
tokens: input.usage.reduce((total, usage) => total + usage.tokens, 0),
|
|
12067
|
+
cost: input.usage.reduce((total, usage) => total + usage.cost, 0),
|
|
12068
|
+
steps: input.usage.reduce((total, usage) => total + usage.steps, 0),
|
|
12069
|
+
...input.currency !== void 0 && input.currency.trim() !== "" ? { currency: input.currency } : {},
|
|
12070
|
+
computedat: input.now
|
|
12071
|
+
};
|
|
12072
|
+
}
|
|
12073
|
+
function replayagentrun(input) {
|
|
12074
|
+
return interleavetimeline(input.events.filter((event) => event.agentid === input.agentid).map((event) => ({ id: event.id, kind: event.kind, summary: event.summary, at: event.at, ...event.agentid !== void 0 ? { agentid: event.agentid } : {} })));
|
|
12075
|
+
}
|
|
12076
|
+
|
|
11520
12077
|
// taskqueue.ts
|
|
11521
12078
|
function emptyqueue(input = {}) {
|
|
11522
12079
|
return { lanes: input.lanes ?? [], priorities: input.priorities ?? [], completionpolicy: input.completionpolicy ?? "all", items: [], claims: [] };
|
|
@@ -11612,58 +12169,6 @@ function taskcounts(queue) {
|
|
|
11612
12169
|
};
|
|
11613
12170
|
}
|
|
11614
12171
|
|
|
11615
|
-
// blackboard.ts
|
|
11616
|
-
var blackboardsections = ["goals", "facts", "findings", "scratch"];
|
|
11617
|
-
function emptyboard(sections) {
|
|
11618
|
-
return { sections: sections ?? blackboardsections, entries: [] };
|
|
11619
|
-
}
|
|
11620
|
-
function postentry(input) {
|
|
11621
|
-
if (input.key.trim() === "") throw new Error("The blackboard entry needs its key.");
|
|
11622
|
-
if (input.value.trim() === "") throw new Error("The blackboard entry needs its value.");
|
|
11623
|
-
if (input.author.trim() === "") throw new Error("The blackboard entry needs its author.");
|
|
11624
|
-
if (!blackboardsections.includes(input.section)) throw new Error(`The section ${input.section} is not one of the shared blackboard sections.`);
|
|
11625
|
-
if (input.board.entries.some((entry2) => entry2.id === input.id)) throw new Error(`The blackboard entry id ${input.id} already exists.`);
|
|
11626
|
-
if (input.valuekind === "json") {
|
|
11627
|
-
try {
|
|
11628
|
-
JSON.parse(input.value);
|
|
11629
|
-
} catch {
|
|
11630
|
-
throw new Error("The json blackboard entry needs a well-formed json value.");
|
|
11631
|
-
}
|
|
11632
|
-
}
|
|
11633
|
-
const entry = { id: input.id, key: input.key.trim(), valuekind: input.valuekind ?? "text", value: input.value, author: input.author, section: input.section, consentclass: input.consentclass ?? "read", postedat: input.now };
|
|
11634
|
-
return { ...input.board, sections: input.board.sections.includes(input.section) ? input.board.sections : [...input.board.sections, input.section], entries: [entry, ...input.board.entries] };
|
|
11635
|
-
}
|
|
11636
|
-
function entryfresh(entry, now, window2) {
|
|
11637
|
-
if (entry.retiredat !== void 0) return false;
|
|
11638
|
-
if (window2 === void 0) return true;
|
|
11639
|
-
return now - entry.postedat <= window2;
|
|
11640
|
-
}
|
|
11641
|
-
function readentries(input) {
|
|
11642
|
-
return input.board.entries.filter((entry) => entry.retiredat === void 0).filter((entry) => input.section === void 0 || entry.section === input.section).filter((entry) => entryfresh(entry, input.now, input.freshness)).sort((one, two) => two.postedat - one.postedat);
|
|
11643
|
-
}
|
|
11644
|
-
function retireentries(input) {
|
|
11645
|
-
if (input.board.retirementwindow === void 0) return { board: input.board, retired: [] };
|
|
11646
|
-
const stale = input.board.entries.filter((entry) => entry.retiredat === void 0 && input.now - entry.postedat > input.board.retirementwindow);
|
|
11647
|
-
if (stale.length === 0) return { board: input.board, retired: [] };
|
|
11648
|
-
const staleids = new Set(stale.map((entry) => entry.id));
|
|
11649
|
-
return {
|
|
11650
|
-
board: { ...input.board, entries: input.board.entries.map((entry) => staleids.has(entry.id) ? { ...entry, retiredat: input.now } : entry) },
|
|
11651
|
-
retired: [...staleids]
|
|
11652
|
-
};
|
|
11653
|
-
}
|
|
11654
|
-
function retireentry(input) {
|
|
11655
|
-
const entry = input.board.entries.find((candidate) => candidate.id === input.entryid);
|
|
11656
|
-
if (!entry) throw new Error(`The blackboard entry ${input.entryid} does not exist.`);
|
|
11657
|
-
if (entry.retiredat !== void 0) throw new Error(`The blackboard entry ${entry.key} is already retired.`);
|
|
11658
|
-
return { ...input.board, entries: input.board.entries.map((candidate) => candidate.id === input.entryid ? { ...candidate, retiredat: input.now } : candidate) };
|
|
11659
|
-
}
|
|
11660
|
-
function boardsummary(board, now) {
|
|
11661
|
-
return board.sections.map((section) => {
|
|
11662
|
-
const live = board.entries.filter((entry) => entry.section === section && entry.retiredat === void 0);
|
|
11663
|
-
return { section, entries: live.length, authors: [...new Set(live.map((entry) => entry.author))], ...live.length > 0 ? { freshestat: Math.max(...live.map((entry) => entry.postedat)) } : {} };
|
|
11664
|
-
});
|
|
11665
|
-
}
|
|
11666
|
-
|
|
11667
12172
|
// agentmailbox.ts
|
|
11668
12173
|
function mailboxof(mailboxes, agentid) {
|
|
11669
12174
|
return mailboxes.find((mailbox) => mailbox.agentid === agentid) ?? { agentid, inbox: [], outbox: [], unread: 0 };
|
|
@@ -20820,6 +21325,7 @@ async function handlerequest(message, sender) {
|
|
|
20820
21325
|
const context = agentruncontext({ agent, task: claimed.task, now });
|
|
20821
21326
|
await memory.addagentevent(agenteventof({ id: randomid(), kind: "claimed", agentid, taskid: claimed.task.id, summary: `The agent ${agent.name} claimed the task ${claimed.task.payload} from the lane ${claimed.task.lane}; the run context ${context.run.id} reuses the workflow engine.`, now }));
|
|
20822
21327
|
await audit("swarm", `The agent ${agent.name} claimed the highest priority task of the lane ${claimed.task.lane}; the per agent run context ${context.run.id} reuses the workflow engine and the proposal still passes the human review.`, {});
|
|
21328
|
+
await memory.addswarmaction({ id: randomid(), kind: "claim", agentid, summary: `The agent ${agent.name} claimed the task ${claimed.task.payload} from the lane ${claimed.task.lane}.`, at: now });
|
|
20823
21329
|
}
|
|
20824
21330
|
return swarmstateof();
|
|
20825
21331
|
}
|
|
@@ -20835,6 +21341,7 @@ async function handlerequest(message, sender) {
|
|
|
20835
21341
|
if (stolen.task !== void 0) {
|
|
20836
21342
|
await memory.addagentevent(agenteventof({ id: randomid(), kind: "stole", agentid, taskid: stolen.task.id, summary: `The ${input2.role} agent ${agentid} stole the task ${stolen.task.payload} from the lane ${stolen.task.lane}; ${grade.reason ?? ""}`, now }));
|
|
20837
21343
|
await audit("swarm", `The ${input2.role} agent ${agentid} stole a task from the lane ${stolen.task.lane} inside the approved swarm; the lane ownership rules the user configured held.`, {});
|
|
21344
|
+
await memory.addswarmaction({ id: randomid(), kind: "steal", agentid, summary: `The ${input2.role} agent ${agentid} stole the task ${stolen.task.payload} from the lane ${stolen.task.lane}.`, at: now });
|
|
20838
21345
|
}
|
|
20839
21346
|
return swarmstateof();
|
|
20840
21347
|
}
|
|
@@ -20849,6 +21356,7 @@ async function handlerequest(message, sender) {
|
|
|
20849
21356
|
await memory.setagentusage(nextusage);
|
|
20850
21357
|
await memory.addagentevent(agenteventof({ id: randomid(), kind: "completed", taskid, ...holder !== void 0 ? { agentid: holder.agentid } : {}, summary: `The task ${completedtask?.payload ?? taskid} completed and its claim released.`, now }));
|
|
20851
21358
|
await audit("swarm", `The task ${taskid} completed${holder !== void 0 ? ` under the agent ${holder.agentid}` : ""} and released its claim; the per agent usage counters moved with it.`, {});
|
|
21359
|
+
await memory.addswarmaction({ id: randomid(), kind: "complete", ...holder !== void 0 ? { agentid: holder.agentid } : {}, summary: `The task ${completedtask?.payload ?? taskid} completed and its claim released.`, at: now });
|
|
20852
21360
|
return swarmstateof();
|
|
20853
21361
|
}
|
|
20854
21362
|
if (input2.cancel === true && input2.taskid !== void 0) {
|
|
@@ -20930,6 +21438,340 @@ async function handlerequest(message, sender) {
|
|
|
20930
21438
|
}
|
|
20931
21439
|
throw new Error("The swarm blackboard request carries no post, retire or sweep action.");
|
|
20932
21440
|
}
|
|
21441
|
+
case "swarmleader": {
|
|
21442
|
+
const input2 = message;
|
|
21443
|
+
const now = Date.now();
|
|
21444
|
+
if (input2.elect !== void 0) {
|
|
21445
|
+
const agents = await memory.getagents();
|
|
21446
|
+
const rule = input2.elect.rule === "named" ? { kind: "named", agentid: input2.elect.agentid ?? "" } : { kind: "first" };
|
|
21447
|
+
const verdict = leaderelectionvalid({ rule, agents });
|
|
21448
|
+
if (!verdict.allowed) throw new Error(verdict.reason);
|
|
21449
|
+
const topology2 = electleader({ agents, id: randomid(), rule, now });
|
|
21450
|
+
await memory.settopology(topology2);
|
|
21451
|
+
await memory.addswarmaction({ id: randomid(), kind: "elect", agentid: topology2.leaderid, summary: `The ${rule.kind} election rule the user configured elected the agent ${topology2.leaderid} leader of the swarm with ${topology2.workerids.length} workers, ${topology2.criticids.length} critics and ${topology2.verifierids.length} verifiers.`, at: now });
|
|
21452
|
+
await memory.addagentevent(agenteventof({ id: randomid(), kind: "assign", agentid: topology2.leaderid, summary: `The leader election picked ${topology2.leaderid} by the ${rule.kind} rule; the topology records the lanes.`, now }));
|
|
21453
|
+
await audit("swarm", `The user elected the agent ${topology2.leaderid} leader by the ${rule.kind} rule; the topology holds ${topology2.workerids.length} workers, ${topology2.criticids.length} critics and ${topology2.verifierids.length} verifiers, and the leader only organizes work that still passes the same review.`, {});
|
|
21454
|
+
return swarmstateof();
|
|
21455
|
+
}
|
|
21456
|
+
const topology = await memory.gettopology();
|
|
21457
|
+
if (input2.assign !== void 0) {
|
|
21458
|
+
if (!topology) throw new Error("The swarm holds no topology; the user elects a leader before the assignment.");
|
|
21459
|
+
const queue = await queueof();
|
|
21460
|
+
const wanted = input2.assign.taskids ?? [];
|
|
21461
|
+
const tasks = queue.items.filter((task) => wanted.length === 0 || wanted.includes(task.id));
|
|
21462
|
+
const next = assignwork({ topology, tasks, now });
|
|
21463
|
+
await memory.settopology(next);
|
|
21464
|
+
await memory.addswarmaction({ id: randomid(), kind: "assign", agentid: next.leaderid, summary: `The leader sliced ${next.assignments.length} task${next.assignments.length === 1 ? "" : "s"} across the ${next.workerids.length} workers of the topology.`, at: now });
|
|
21465
|
+
await audit("swarm", `The leader ${next.leaderid} assigned ${next.assignments.length} task slice${next.assignments.length === 1 ? "" : "s"} across the ${next.workerids.length} workers; every slice still passes the same plan review before anything executes.`, {});
|
|
21466
|
+
return swarmstateof();
|
|
21467
|
+
}
|
|
21468
|
+
if (input2.collect !== void 0) {
|
|
21469
|
+
if (!topology) throw new Error("The swarm holds no topology; the user elects a leader before the collection.");
|
|
21470
|
+
const gathered = collectresults({ topology, outputs: input2.collect.outputs ?? [] });
|
|
21471
|
+
await memory.addswarmaction({ id: randomid(), kind: "collect", agentid: topology.leaderid, summary: `The leader gathered ${gathered.gathered.length - gathered.missing.length} worker output${gathered.gathered.length - gathered.missing.length === 1 ? "" : "s"} with ${gathered.missing.length} still pending.`, at: now });
|
|
21472
|
+
await audit("swarm", `The leader ${topology.leaderid} collected ${gathered.gathered.length} worker outputs with ${gathered.missing.length} pending; the missing slices stay visible to the user.`, {});
|
|
21473
|
+
return { ...await swarmstateof(), collected: gathered.gathered, missing: gathered.missing };
|
|
21474
|
+
}
|
|
21475
|
+
if (input2.scale !== void 0) {
|
|
21476
|
+
if (!topology) throw new Error("The swarm holds no topology; the user elects a leader before the scaling.");
|
|
21477
|
+
const settings = await memory.getsettings();
|
|
21478
|
+
const bound = input2.scale.bound ?? settings?.swarmworkers;
|
|
21479
|
+
const verdict = workerscalevalid(bound);
|
|
21480
|
+
if (!verdict.allowed) throw new Error(verdict.reason);
|
|
21481
|
+
const agents = await memory.getagents();
|
|
21482
|
+
const outcome = scaleworkers({ topology, agents, pending: input2.scale.pending ?? 0, ...bound !== void 0 ? { bound } : {}, now });
|
|
21483
|
+
await memory.settopology(outcome.topology);
|
|
21484
|
+
await memory.addswarmaction({ id: randomid(), kind: "scale", agentid: topology.leaderid, summary: outcome.reason, at: now });
|
|
21485
|
+
await audit("swarm", `The worker lane scaled: ${outcome.reason}`, {});
|
|
21486
|
+
return { ...await swarmstateof(), scaleadded: outcome.added, scaleretired: outcome.retired };
|
|
21487
|
+
}
|
|
21488
|
+
if (input2.split !== void 0) {
|
|
21489
|
+
const split = plannersplit({ id: randomid(), planownerid: input2.split.planownerid?.trim() ?? "", runownerid: input2.split.runownerid?.trim() ?? "", ...input2.split.taskid !== void 0 && input2.split.taskid.trim() !== "" ? { taskid: input2.split.taskid.trim() } : {}, now });
|
|
21490
|
+
await memory.setplannersplits([...(await memory.getplannersplits()).filter((entry) => entry.id !== split.id), split]);
|
|
21491
|
+
await memory.addswarmaction({ id: randomid(), kind: "split", summary: `The task ${split.taskid ?? "of the swarm"} split between the planner ${split.planownerid} and the executor ${split.runownerid}; the executor reports every step outcome back.`, at: now });
|
|
21492
|
+
await audit("swarm", `The user split the task ${split.taskid ?? ""} between the planner agent ${split.planownerid} and the executor agent ${split.runownerid}; the executor reports every step outcome back to the planner.`, {});
|
|
21493
|
+
return swarmstateof();
|
|
21494
|
+
}
|
|
21495
|
+
if (input2.stepreport !== void 0) {
|
|
21496
|
+
const splitid = input2.stepreport.splitid?.trim() ?? "";
|
|
21497
|
+
const split = (await memory.getplannersplits()).find((entry) => entry.id === splitid);
|
|
21498
|
+
if (!split) throw new Error(`The planner executor split ${splitid} does not exist.`);
|
|
21499
|
+
const outcome = input2.stepreport.outcome === "failed" ? "failed" : "done";
|
|
21500
|
+
const next = reportstep({ split, stepid: input2.stepreport.stepid?.trim() ?? "", outcome, detail: input2.stepreport.detail?.trim() ?? "", now });
|
|
21501
|
+
await memory.setplannersplits((await memory.getplannersplits()).map((entry) => entry.id === split.id ? next : entry));
|
|
21502
|
+
await memory.addswarmaction({ id: randomid(), kind: "stepreport", agentid: split.runownerid, summary: `The executor ${split.runownerid} reported the step ${next.stepreports[next.stepreports.length - 1]?.stepid ?? ""} ${outcome} back to the planner ${split.planownerid}.`, at: now });
|
|
21503
|
+
await audit("swarm", `The executor ${split.runownerid} reported the step ${input2.stepreport.stepid ?? ""} ${outcome} back to the planner ${split.planownerid}; the split record keeps every step outcome.`, {});
|
|
21504
|
+
return swarmstateof();
|
|
21505
|
+
}
|
|
21506
|
+
if (input2.milestone !== void 0) {
|
|
21507
|
+
const agentid = input2.milestone.agentid?.trim() ?? "";
|
|
21508
|
+
if (agentid === "" || input2.milestone.label === void 0 || input2.milestone.label.trim() === "") throw new Error("The milestone needs its agent and its label.");
|
|
21509
|
+
const board = boardstate({ agents: await memory.getagents(), queue: await queueof(), ...topology !== void 0 ? { topology } : {}, now });
|
|
21510
|
+
const milestone = { label: input2.milestone.label.trim(), done: input2.milestone.done !== false, ...input2.milestone.done !== false ? { at: now } : {} };
|
|
21511
|
+
await memory.addboardsnapshot({ ...board, lanes: board.lanes.map((lane) => lane.agentid === agentid ? { ...lane, milestones: [...lane.milestones.filter((entry) => entry.label !== milestone.label), milestone] } : lane) });
|
|
21512
|
+
await memory.addswarmaction({ id: randomid(), kind: "milestone", agentid, summary: `The agent ${agentid} reached the milestone ${milestone.label}.`, at: now });
|
|
21513
|
+
await audit("swarm", `The agent ${agentid} reported the milestone ${milestone.label}; the progressboard snapshot stores it under the user configured retention.`, {});
|
|
21514
|
+
return swarmstateof();
|
|
21515
|
+
}
|
|
21516
|
+
throw new Error("The swarm leader request carries no elect, assign, collect, scale, split, stepreport or milestone action.");
|
|
21517
|
+
}
|
|
21518
|
+
case "swarmreview": {
|
|
21519
|
+
const input2 = message;
|
|
21520
|
+
const now = Date.now();
|
|
21521
|
+
if (input2.request !== void 0) {
|
|
21522
|
+
const requests = requestreview({ requests: await memory.getreviewrequests(), id: randomid(), fromagentid: input2.request.fromagentid?.trim() ?? "", toagentid: input2.request.toagentid?.trim() ?? "", subject: input2.request.subject ?? "", payload: input2.request.payload ?? "", ...input2.request.timeoutms !== void 0 ? { timeoutms: input2.request.timeoutms } : {}, now });
|
|
21523
|
+
await memory.setreviewrequests(requests);
|
|
21524
|
+
const requesterid = input2.request.fromagentid?.trim() ?? "";
|
|
21525
|
+
await memory.addswarmaction({ id: randomid(), kind: "review", ...requesterid !== "" ? { agentid: requesterid } : {}, summary: `The agent ${input2.request.fromagentid?.trim() ?? ""} routed the review of ${input2.request.subject ?? ""} to the agent ${input2.request.toagentid?.trim() ?? ""}.`, at: now });
|
|
21526
|
+
await audit("swarm", `The review request of ${input2.request.subject ?? ""} was routed from ${input2.request.fromagentid ?? ""} to ${input2.request.toagentid ?? ""}${input2.request.timeoutms !== void 0 ? ` with the user configured answer window ${input2.request.timeoutms}ms` : ""}; the review stays read only over the agent output.`, {});
|
|
21527
|
+
return swarmstateof();
|
|
21528
|
+
}
|
|
21529
|
+
if (input2.ack !== void 0) {
|
|
21530
|
+
const requests = ackreview({ requests: await memory.getreviewrequests(), id: input2.ack.trim(), now });
|
|
21531
|
+
await memory.setreviewrequests(requests);
|
|
21532
|
+
await audit("swarm", `The review request ${input2.ack} was acked; the answer still waits.`, {});
|
|
21533
|
+
return swarmstateof();
|
|
21534
|
+
}
|
|
21535
|
+
if (input2.apply !== void 0) {
|
|
21536
|
+
const verdict = input2.apply.verdict === "approve" || input2.apply.verdict === "reject" ? input2.apply.verdict : "changes";
|
|
21537
|
+
const outcome = applyreview({ requests: await memory.getreviewrequests(), id: input2.apply.id?.trim() ?? "", reviewerid: input2.apply.reviewerid?.trim() ?? "", verdict, issues: input2.apply.issues ?? [], requiredchanges: input2.apply.requiredchanges ?? [], ...input2.apply.taskid !== void 0 && input2.apply.taskid.trim() !== "" ? { taskid: input2.apply.taskid.trim() } : {}, now });
|
|
21538
|
+
const grade = criticreviewgrade(outcome.review);
|
|
21539
|
+
if (!grade.allowed) throw new Error(grade.reason);
|
|
21540
|
+
await memory.setreviewrequests(outcome.requests);
|
|
21541
|
+
await memory.addcriticreview(outcome.review);
|
|
21542
|
+
await memory.addswarmaction({ id: randomid(), kind: "review", agentid: outcome.review.reviewerid, summary: `The critic ${outcome.review.reviewerid} returned the ${verdict} verdict over the output of ${outcome.review.subjectagentid}.`, at: now });
|
|
21543
|
+
await audit("swarm", `The critic ${outcome.review.reviewerid} reviewed the output of ${outcome.review.subjectagentid} with the ${verdict} verdict${outcome.review.requiredchanges.length > 0 ? ` and ${outcome.review.requiredchanges.length} required change${outcome.review.requiredchanges.length === 1 ? "" : "s"}` : ""}; the critic reads only and the rework still passes the same human review.`, {});
|
|
21544
|
+
return swarmstateof();
|
|
21545
|
+
}
|
|
21546
|
+
if (input2.sweep === true) {
|
|
21547
|
+
const swept = sweepreviews({ requests: await memory.getreviewrequests(), now });
|
|
21548
|
+
await memory.setreviewrequests(swept.requests);
|
|
21549
|
+
if (swept.timedout.length > 0) await audit("swarm", `The review sweep timed out ${swept.timedout.length} unanswered request${swept.timedout.length === 1 ? "" : "s"} past the user configured window.`, {});
|
|
21550
|
+
return swarmstateof();
|
|
21551
|
+
}
|
|
21552
|
+
if (input2.verify !== void 0) {
|
|
21553
|
+
const settings = await memory.getsettings();
|
|
21554
|
+
const check = checkclaim({ id: randomid(), verifierid: input2.verify.verifierid?.trim() ?? "", claimagentid: input2.verify.claimagentid?.trim() ?? "", claim: input2.verify.claim ?? "", method: input2.verify.method?.trim() ?? "", outcome: input2.verify.outcome === "fail" ? "fail" : "pass", ...input2.verify.evidence !== void 0 && input2.verify.evidence.trim() !== "" ? { evidence: input2.verify.evidence } : {}, ...input2.verify.taskid !== void 0 && input2.verify.taskid.trim() !== "" ? { taskid: input2.verify.taskid.trim() } : {}, now });
|
|
21555
|
+
const grade = verifiermethodgrade({ method: check.method, allowed: settings?.verifiermethods ?? [] });
|
|
21556
|
+
if (!grade.allowed) throw new Error(grade.reason);
|
|
21557
|
+
await memory.addverifiercheck(check);
|
|
21558
|
+
await memory.addswarmaction({ id: randomid(), kind: "verify", agentid: check.verifierid, summary: `The verifier ${check.verifierid} marked the claim of ${check.claimagentid} ${check.outcome} by the ${check.method} method.`, at: now });
|
|
21559
|
+
await audit("swarm", `The verifier ${check.verifierid} checked the claim of ${check.claimagentid} by the ${check.method} method and marked it ${check.outcome}${check.evidence !== void 0 ? ` with the evidence ${check.evidence}` : ""}; the verifier check reads the page and never writes.`, {});
|
|
21560
|
+
return swarmstateof();
|
|
21561
|
+
}
|
|
21562
|
+
if (input2.escalate !== void 0) {
|
|
21563
|
+
const record2 = escalate({ id: randomid(), agentid: input2.escalate.agentid?.trim() ?? "", subject: input2.escalate.subject ?? "", context: input2.escalate.context ?? "", now });
|
|
21564
|
+
const gate = escalationgate(record2);
|
|
21565
|
+
if (!gate.allowed) throw new Error(gate.reason);
|
|
21566
|
+
await memory.addescalation(record2);
|
|
21567
|
+
await memory.addswarmaction({ id: randomid(), kind: "escalate", agentid: record2.agentid, summary: `The agent ${record2.agentid} lifted the stalled decision ${record2.subject} to the user with its full context.`, at: now });
|
|
21568
|
+
await audit("swarm", `The agent ${record2.agentid} escalated the decision ${record2.subject} to the user; the escalation stays human decided and the agent waits.`, {});
|
|
21569
|
+
return swarmstateof();
|
|
21570
|
+
}
|
|
21571
|
+
if (input2.decide !== void 0) {
|
|
21572
|
+
const record2 = (await memory.getescalations()).find((entry) => entry.id === input2.decide?.id?.trim());
|
|
21573
|
+
if (!record2) throw new Error(`The escalation ${input2.decide?.id ?? ""} does not exist.`);
|
|
21574
|
+
const resolved = resolveescalation({ escalation: record2, decision: input2.decide.decision ?? "", now });
|
|
21575
|
+
await memory.updateescalation(resolved);
|
|
21576
|
+
await memory.addswarmaction({ id: randomid(), kind: "escalate", agentid: record2.agentid, summary: `The user decided the escalation ${record2.subject}: ${resolved.decision}.`, at: now });
|
|
21577
|
+
await audit("swarm", `The user decided the escalation ${record2.subject} of the agent ${record2.agentid}; the decision is recorded and the agent continues from it.`, {});
|
|
21578
|
+
return swarmstateof();
|
|
21579
|
+
}
|
|
21580
|
+
if (input2.consensus !== void 0) {
|
|
21581
|
+
const settings = await memory.getsettings();
|
|
21582
|
+
const agents = await memory.getagents();
|
|
21583
|
+
const quorum = input2.consensus.quorum ?? settings?.swarmquorum;
|
|
21584
|
+
if (quorum === void 0) throw new Error("The consensus round needs its quorum; the user configures it or sets the swarmquorum run setting.");
|
|
21585
|
+
const voters = agents.filter((agent) => agent.state !== "stopped").length;
|
|
21586
|
+
const verdict = consensusquorumvalid({ quorum, voters });
|
|
21587
|
+
if (!verdict.allowed) throw new Error(verdict.reason);
|
|
21588
|
+
const round = openconsensus({ id: randomid(), subject: input2.consensus.subject ?? "", quorum, now });
|
|
21589
|
+
await memory.setconsensusround(round);
|
|
21590
|
+
await memory.addswarmaction({ id: randomid(), kind: "consensus", summary: `The consensus round on ${round.subject} opened with the user configured quorum ${quorum}.`, at: now });
|
|
21591
|
+
await audit("swarm", `The consensus round on ${round.subject} opened with the user configured quorum ${quorum} of ${voters} voting agents; the round carries when the yes votes reach it.`, {});
|
|
21592
|
+
return swarmstateof();
|
|
21593
|
+
}
|
|
21594
|
+
if (input2.vote !== void 0) {
|
|
21595
|
+
const round = (await memory.getconsensusrounds()).find((entry) => entry.id === input2.vote?.id?.trim());
|
|
21596
|
+
if (!round) throw new Error(`The consensus round ${input2.vote?.id ?? ""} does not exist.`);
|
|
21597
|
+
const vote = input2.vote.vote === "no" ? "no" : input2.vote.vote === "abstain" ? "abstain" : "yes";
|
|
21598
|
+
const next = castvote({ round, agentid: input2.vote.agentid?.trim() ?? "", vote, now });
|
|
21599
|
+
await memory.setconsensusround(next);
|
|
21600
|
+
const voterid = input2.vote.agentid?.trim() ?? "";
|
|
21601
|
+
await memory.addswarmaction({ id: randomid(), kind: "consensus", ...voterid !== "" ? { agentid: voterid } : {}, summary: `The agent ${input2.vote.agentid?.trim() ?? ""} voted ${vote} on ${round.subject}; the round is ${next.state}.`, at: now });
|
|
21602
|
+
await audit("swarm", `The agent ${input2.vote.agentid ?? ""} voted ${vote} on ${round.subject}; the round reads ${consensusstate(next).yes} yes, ${consensusstate(next).no} no and ${consensusstate(next).abstain} abstain against the quorum ${round.quorum}.`, {});
|
|
21603
|
+
return swarmstateof();
|
|
21604
|
+
}
|
|
21605
|
+
throw new Error("The swarm review request carries no request, ack, apply, sweep, verify, escalate, decide, consensus or vote action.");
|
|
21606
|
+
}
|
|
21607
|
+
case "swarmhandoff": {
|
|
21608
|
+
const input2 = message;
|
|
21609
|
+
const now = Date.now();
|
|
21610
|
+
if (input2.prepare !== void 0) {
|
|
21611
|
+
const agents = await memory.getagents();
|
|
21612
|
+
const record2 = preparehandoff({ agents, id: randomid(), fromagentid: input2.prepare.fromagentid?.trim() ?? "", toagentid: input2.prepare.toagentid?.trim() ?? "", taskstate: input2.prepare.taskstate ?? "", ...input2.prepare.tabid !== void 0 ? { tabid: input2.prepare.tabid } : {}, ...input2.prepare.reason !== void 0 && input2.prepare.reason.trim() !== "" ? { reason: input2.prepare.reason } : {}, now });
|
|
21613
|
+
await memory.addhandoff(record2);
|
|
21614
|
+
await memory.addswarmaction({ id: randomid(), kind: "handoff", agentid: record2.fromagentid, summary: `The tab ${record2.tabid} handoff from ${record2.fromagentid} to ${record2.toagentid} was prepared with the packaged task state.`, at: now });
|
|
21615
|
+
await audit("swarm", `The handoff of tab ${record2.tabid} from ${record2.fromagentid} to ${record2.toagentid} was prepared with the packaged task state; the transfer preserves the original session grants.`, {});
|
|
21616
|
+
return swarmstateof();
|
|
21617
|
+
}
|
|
21618
|
+
if (input2.transfer !== void 0) {
|
|
21619
|
+
const record2 = (await memory.gethandoffs()).find((entry) => entry.id === input2.transfer?.trim());
|
|
21620
|
+
if (!record2) throw new Error(`The handoff ${input2.transfer ?? ""} does not exist.`);
|
|
21621
|
+
const agents = await memory.getagents();
|
|
21622
|
+
const receiver = agents.find((agent) => agent.id === record2.toagentid);
|
|
21623
|
+
const session = await memory.getsession();
|
|
21624
|
+
const gate = handoffgrantgate({ record: record2, toscope: receiver?.scope, sessiongrants: session?.grants ?? [] });
|
|
21625
|
+
if (!gate.allowed) throw new Error(gate.reason);
|
|
21626
|
+
const outcome = transferhandoff({ agents, handoffs: await memory.gethandoffs(), id: record2.id, now });
|
|
21627
|
+
await memory.setagents(outcome.agents);
|
|
21628
|
+
await memory.updatehandoff(outcome.handoffs.find((entry) => entry.id === record2.id));
|
|
21629
|
+
await memory.addswarmaction({ id: randomid(), kind: "handoff", agentid: record2.toagentid, summary: `The tab ${record2.tabid} moved from ${record2.fromagentid} to ${record2.toagentid}; the task state stays packaged until the resume.`, at: now });
|
|
21630
|
+
await audit("swarm", `The tab ${record2.tabid} moved from ${record2.fromagentid} to ${record2.toagentid} under the one agent per tab rule; ${gate.reason ?? ""}`, {});
|
|
21631
|
+
return swarmstateof();
|
|
21632
|
+
}
|
|
21633
|
+
if (input2.resume !== void 0) {
|
|
21634
|
+
const record2 = (await memory.gethandoffs()).find((entry) => entry.id === input2.resume?.trim());
|
|
21635
|
+
if (!record2) throw new Error(`The handoff ${input2.resume ?? ""} does not exist.`);
|
|
21636
|
+
const resumed = resumehandoff({ handoffs: await memory.gethandoffs(), id: record2.id, now });
|
|
21637
|
+
await memory.updatehandoff(resumed);
|
|
21638
|
+
await memory.addswarmaction({ id: randomid(), kind: "handoff", agentid: resumed.toagentid, summary: `The agent ${resumed.toagentid} resumed the task from the packaged state of the handoff ${resumed.id}.`, at: now });
|
|
21639
|
+
await audit("swarm", `The agent ${resumed.toagentid} resumed the handed off task from its packaged state; the run continues behind the same session, plan and origin gates.`, {});
|
|
21640
|
+
return swarmstateof();
|
|
21641
|
+
}
|
|
21642
|
+
throw new Error("The swarm handoff request carries no prepare, transfer or resume action.");
|
|
21643
|
+
}
|
|
21644
|
+
case "swarmlocks": {
|
|
21645
|
+
const input2 = message;
|
|
21646
|
+
const now = Date.now();
|
|
21647
|
+
if (input2.acquire !== void 0) {
|
|
21648
|
+
const holder = input2.acquire.holder?.trim() ?? "";
|
|
21649
|
+
const origin = input2.acquire.origin?.trim() ?? "";
|
|
21650
|
+
const selector = input2.acquire.selector?.trim() ?? "";
|
|
21651
|
+
if (holder === "" || origin === "" || selector === "") throw new Error("The lock acquisition needs its holder, origin and selector.");
|
|
21652
|
+
const kind = input2.acquire.kind === "shared" ? "shared" : "exclusive";
|
|
21653
|
+
const locks = await memory.getlocks();
|
|
21654
|
+
const grade = lockscopevalid({ key: lockkey(origin, selector), holder, kind, origin, selector, acquiredat: now, ...input2.acquire.expiresat !== void 0 ? { expiresat: input2.acquire.expiresat } : {} });
|
|
21655
|
+
if (!grade.allowed) throw new Error(grade.reason);
|
|
21656
|
+
const outcome = acquirelock({ locks, holder, origin, selector, kind, ...input2.acquire.expiresat !== void 0 ? { expiresat: input2.acquire.expiresat } : {}, now });
|
|
21657
|
+
if (!outcome.acquired) throw new Error(outcome.reason);
|
|
21658
|
+
await memory.setlocks(outcome.locks);
|
|
21659
|
+
await memory.addswarmaction({ id: randomid(), kind: "lock", agentid: holder, summary: outcome.reason, at: now });
|
|
21660
|
+
await audit("swarm", `The ${kind} lock ${origin}|${selector} went to the agent ${holder}${input2.acquire.expiresat !== void 0 ? ` with the user configured expiry ${new Date(input2.acquire.expiresat).toISOString()}` : " with no expiry"}; the lock serializes work the same review already approved.`, {});
|
|
21661
|
+
return swarmstateof();
|
|
21662
|
+
}
|
|
21663
|
+
if (input2.release !== void 0) {
|
|
21664
|
+
const locks = await memory.getlocks();
|
|
21665
|
+
const outcome = releaselock({ locks, key: input2.release.key?.trim() ?? "", holder: input2.release.holder?.trim() ?? "", now });
|
|
21666
|
+
if (!outcome.released) throw new Error(`The agent ${input2.release.holder ?? ""} holds no lock ${input2.release.key ?? ""}.`);
|
|
21667
|
+
await memory.setlocks(outcome.locks);
|
|
21668
|
+
const releaserid = input2.release.holder?.trim() ?? "";
|
|
21669
|
+
await memory.addswarmaction({ id: randomid(), kind: "lock", ...releaserid !== "" ? { agentid: releaserid } : {}, summary: `The agent ${input2.release.holder?.trim() ?? ""} released the lock ${input2.release.key ?? ""}; the resource returned to the pool.`, at: now });
|
|
21670
|
+
await audit("swarm", `The agent ${input2.release.holder ?? ""} released the lock ${input2.release.key ?? ""}; the resource returned to the pool for the next agent.`, {});
|
|
21671
|
+
return swarmstateof();
|
|
21672
|
+
}
|
|
21673
|
+
if (input2.sweep === true) {
|
|
21674
|
+
const outcome = expirelocks({ locks: await memory.getlocks(), now });
|
|
21675
|
+
await memory.setlocks(outcome.locks);
|
|
21676
|
+
if (outcome.expired.length > 0) {
|
|
21677
|
+
await memory.addswarmaction({ id: randomid(), kind: "lock", summary: `The expiry sweep returned ${outcome.expired.length} abandoned lock${outcome.expired.length === 1 ? "" : "s"} to the pool.`, at: now });
|
|
21678
|
+
await audit("swarm", `The lock sweep expired ${outcome.expired.length} abandoned lock${outcome.expired.length === 1 ? "" : "s"} past their user configured expiry; the keys returned to the pool.`, {});
|
|
21679
|
+
}
|
|
21680
|
+
return swarmstateof();
|
|
21681
|
+
}
|
|
21682
|
+
if (input2.scan !== void 0) {
|
|
21683
|
+
const writers = (input2.scan.writers ?? []).map((writer) => ({ agentid: writer.agentid?.trim() ?? "", origin: writer.origin?.trim() ?? "", selector: writer.selector?.trim() ?? "", ...writer.taskid !== void 0 && writer.taskid.trim() !== "" ? { taskid: writer.taskid.trim() } : {} })).filter((writer) => writer.agentid !== "" && writer.origin !== "" && writer.selector !== "");
|
|
21684
|
+
const scan = scanconflicts({ id: randomid(), writers, now });
|
|
21685
|
+
await memory.addconflictscan(scan);
|
|
21686
|
+
await memory.addswarmaction({ id: randomid(), kind: "conflict", summary: scan.clean ? `The conflict scan found no overlapping write among ${writers.length} writers.` : `The conflict scan found ${scan.overlaps.length} overlapping target${scan.overlaps.length === 1 ? "" : "s"} among ${writers.length} writers with the suggested order ${scan.suggestedorder.join(" \u2192 ")}.`, at: now });
|
|
21687
|
+
await audit("swarm", scan.clean ? `The conflict scan of ${writers.length} parallel writers found no overlapping write; the runs stay safe side by side.` : `The conflict scan found ${scan.overlaps.length} overlapping target${scan.overlaps.length === 1 ? "" : "s"} (${scan.overlaps.map((overlap) => `${overlap.origin}|${overlap.selector} by ${overlap.writers.join(", ")}`).join("; ")}) and suggested the order ${scan.suggestedorder.join(" \u2192 ")}.`, {});
|
|
21688
|
+
return { ...await swarmstateof(), scan };
|
|
21689
|
+
}
|
|
21690
|
+
if (input2.arbitrate !== void 0) {
|
|
21691
|
+
const strategy = input2.arbitrate.strategy === "age" ? "age" : input2.arbitrate.strategy === "leader" ? "leader" : "priority";
|
|
21692
|
+
const rule = { id: randomid(), strategy, priorityorder: input2.arbitrate.priorityorder ?? [], configuredat: now };
|
|
21693
|
+
const topology = await memory.gettopology();
|
|
21694
|
+
const claims = (input2.arbitrate.claims ?? []).map((claim2) => ({ agentid: claim2.agentid?.trim() ?? "", claimedat: claim2.claimedat ?? now })).filter((claim2) => claim2.agentid !== "");
|
|
21695
|
+
const order = arbitrate({ rule, ...topology !== void 0 ? { leaderid: topology.leaderid } : {}, claims });
|
|
21696
|
+
await memory.addswarmaction({ id: randomid(), kind: "arbitrate", summary: `The ${strategy} arbitration rule ordered the competing claims ${order.join(" \u2192 ")}.`, at: now });
|
|
21697
|
+
await audit("swarm", `The user configured ${strategy} arbitration ordered the competing resource claims ${order.join(" \u2192 ")}; the ordering stays a user rule.`, {});
|
|
21698
|
+
return { ...await swarmstateof(), arbitration: order };
|
|
21699
|
+
}
|
|
21700
|
+
throw new Error("The swarm locks request carries no acquire, release, sweep, scan or arbitrate action.");
|
|
21701
|
+
}
|
|
21702
|
+
case "swarmmerge": {
|
|
21703
|
+
const input2 = message;
|
|
21704
|
+
const now = Date.now();
|
|
21705
|
+
const toentries = () => (input2.entries ?? []).map((entry, index) => ({ id: `${now}:${index}`, agentid: entry.agentid?.trim() ?? "", ...entry.taskid !== void 0 && entry.taskid.trim() !== "" ? { taskid: entry.taskid.trim() } : {}, key: entry.key?.trim() ?? "", value: entry.value ?? "", mergedat: now })).filter((entry) => entry.key !== "" && entry.agentid !== "");
|
|
21706
|
+
const ruleof = (value) => value === "last" ? "last" : value === "preferagent" ? "preferagent" : value === "fail" ? "fail" : "first";
|
|
21707
|
+
if (input2.merge !== void 0) {
|
|
21708
|
+
const fold = mergeresults({ entries: toentries(), rule: ruleof(input2.merge.rule), ...input2.merge.preferagent !== void 0 && input2.merge.preferagent.trim() !== "" ? { preferagent: input2.merge.preferagent.trim() } : {}, now });
|
|
21709
|
+
const grade = conflictresolutiongrade(ruleof(input2.merge.rule));
|
|
21710
|
+
if (!grade.allowed) throw new Error(grade.reason);
|
|
21711
|
+
await audit("swarm", `The merge folded ${input2.entries?.length ?? 0} parallel results under the ${ruleof(input2.merge.rule)} rule with ${fold.conflicts.length} conflict${fold.conflicts.length === 1 ? "" : "s"} resolved and every merged value keeping its provenance; ${grade.reason ?? ""}`, {});
|
|
21712
|
+
return { ...await swarmstateof(), merged: fold.entries, mergeconflicts: fold.conflicts, mergerefused: fold.refused };
|
|
21713
|
+
}
|
|
21714
|
+
if (input2.report !== void 0) {
|
|
21715
|
+
const rule = ruleof(input2.report.rule);
|
|
21716
|
+
const grade = conflictresolutiongrade(rule);
|
|
21717
|
+
if (!grade.allowed) throw new Error(grade.reason);
|
|
21718
|
+
const built = swarmreport({ id: randomid(), title: input2.report.title ?? "", outputs: toentries(), rule, ...input2.report.preferagent !== void 0 && input2.report.preferagent.trim() !== "" ? { preferagent: input2.report.preferagent.trim() } : {}, ...input2.report.confidence !== void 0 && input2.report.confidence.trim() !== "" ? { confidence: input2.report.confidence } : {}, now });
|
|
21719
|
+
if (built.refused) throw new Error(`The report refused the fold: ${built.conflicts.join("; ")}`);
|
|
21720
|
+
await memory.setreport(built.report);
|
|
21721
|
+
await memory.addswarmaction({ id: randomid(), kind: "merge", summary: `The report ${built.report.title} merged the outputs of ${built.report.sources.length} agent${built.report.sources.length === 1 ? "" : "s"} into ${built.report.sections.length} section${built.report.sections.length === 1 ? "" : "s"}.`, at: now });
|
|
21722
|
+
await audit("swarm", `The aggregate report ${built.report.title} folded the parallel outputs of ${built.report.sources.join(", ")} into ${built.report.sections.length} section${built.report.sections.length === 1 ? "" : "s"} under the ${rule} rule; every merged value keeps its provenance.`, {});
|
|
21723
|
+
return swarmstateof();
|
|
21724
|
+
}
|
|
21725
|
+
if (input2.export !== void 0) {
|
|
21726
|
+
const report = await memory.getreport();
|
|
21727
|
+
if (!report) throw new Error("No merged report is stored; the user builds the report before any export.");
|
|
21728
|
+
const grade = mergeegressgrade({ report, carriespagecontent: input2.export.carriespagecontent === true });
|
|
21729
|
+
if (!grade.allowed) throw new Error(grade.reason);
|
|
21730
|
+
await audit("swarm", `The merged report ${report.title} was exported; ${grade.reason ?? ""}`, {});
|
|
21731
|
+
return swarmstateof();
|
|
21732
|
+
}
|
|
21733
|
+
if (input2.compare !== void 0) {
|
|
21734
|
+
const outputs = (input2.compare.outputs ?? []).map((output) => ({ agentid: output.agentid?.trim() ?? "", value: output.value ?? "" })).filter((output) => output.agentid !== "");
|
|
21735
|
+
const comparison = compareoutputs({ id: randomid(), subject: input2.compare.subject ?? "", outputs, now });
|
|
21736
|
+
await audit("swarm", `The user compared ${outputs.length} competing agent outputs on ${comparison.subject}; the differences name where the agents disagree.`, {});
|
|
21737
|
+
return { ...await swarmstateof(), comparison };
|
|
21738
|
+
}
|
|
21739
|
+
if (input2.lesson !== void 0) {
|
|
21740
|
+
const board = await memory.getblackboard() ?? emptyboard();
|
|
21741
|
+
const next = sharelesson({ board, id: randomid(), agentid: input2.lesson.agentid?.trim() ?? "", statement: input2.lesson.statement ?? "", verifiedby: input2.lesson.verifiedby?.trim() ?? "", ...input2.lesson.section !== void 0 ? { section: ["goals", "facts", "findings", "scratch"].includes(input2.lesson.section) ? input2.lesson.section : "findings" } : {}, ...input2.lesson.consentclass !== void 0 ? { consentclass: input2.lesson.consentclass === "interaction" || input2.lesson.consentclass === "sensitive" ? input2.lesson.consentclass : "read" } : {}, now });
|
|
21742
|
+
await memory.setblackboard(next);
|
|
21743
|
+
const lessonauthor = input2.lesson.agentid?.trim() ?? "";
|
|
21744
|
+
await memory.addswarmaction({ id: randomid(), kind: "lesson", ...lessonauthor !== "" ? { agentid: lessonauthor } : {}, summary: `The verified lesson of ${input2.lesson.agentid?.trim() ?? "user"} landed on the findings section for every agent to read.`, at: now });
|
|
21745
|
+
await audit("swarm", `The verified lesson of the agent ${input2.lesson.agentid ?? ""} was shared to the blackboard findings section; only a verified lesson lands on the board.`, {});
|
|
21746
|
+
return swarmstateof();
|
|
21747
|
+
}
|
|
21748
|
+
if (input2.costs === true) {
|
|
21749
|
+
const usage = await memory.getagentusage();
|
|
21750
|
+
const cost = swarmcosts({ usage, now });
|
|
21751
|
+
await memory.addswarmcost(cost);
|
|
21752
|
+
await audit("swarm", `The shared cost accounting summed the usage of ${cost.agents} agent${cost.agents === 1 ? "" : "s"} into the swarm totals of ${cost.tokens} tokens, ${cost.cost} cost and ${cost.steps} steps.`, {});
|
|
21753
|
+
return swarmstateof();
|
|
21754
|
+
}
|
|
21755
|
+
if (input2.timeline !== void 0) {
|
|
21756
|
+
return { ...await swarmstateof(), timelinefiltered: await memory.getswarmtimeline({ ...input2.timeline.agentid !== void 0 && input2.timeline.agentid.trim() !== "" ? { agentid: input2.timeline.agentid.trim() } : {}, ...input2.timeline.kind !== void 0 && input2.timeline.kind.trim() !== "" ? { kind: input2.timeline.kind.trim() } : {} }) };
|
|
21757
|
+
}
|
|
21758
|
+
if (input2.replay !== void 0) {
|
|
21759
|
+
const agentid = input2.replay.trim();
|
|
21760
|
+
if (agentid === "") throw new Error("The replay needs its agent id.");
|
|
21761
|
+
const events = await memory.getagentevents();
|
|
21762
|
+
const actions = await memory.getswarmtimeline();
|
|
21763
|
+
const replay = replayagentrun({ events: [...events.map((event) => ({ id: event.id, kind: event.kind, summary: event.summary, at: event.at, ...event.agentid !== void 0 ? { agentid: event.agentid } : {} })), ...actions.map((action) => ({ id: action.id, kind: action.kind, summary: action.summary, at: action.at, ...action.agentid !== void 0 ? { agentid: action.agentid } : {} }))], agentid });
|
|
21764
|
+
await audit("swarm", `The replay rebuilt the run of the agent ${agentid} from the audit trail with ${replay.length} recorded action${replay.length === 1 ? "" : "s"}.`, {});
|
|
21765
|
+
return { ...await swarmstateof(), replay };
|
|
21766
|
+
}
|
|
21767
|
+
if (input2.snapshot === true) {
|
|
21768
|
+
const state = await swarmstateof();
|
|
21769
|
+
await memory.addboardsnapshot(state.board);
|
|
21770
|
+
await audit("swarm", `The progressboard snapshot was stored with ${state.board.lanes.length} lane${state.board.lanes.length === 1 ? "" : "s"} under the user configured retention.`, {});
|
|
21771
|
+
return swarmstateof();
|
|
21772
|
+
}
|
|
21773
|
+
throw new Error("The swarm merge request carries no merge, report, export, compare, lesson, costs, timeline, replay or snapshot action.");
|
|
21774
|
+
}
|
|
20933
21775
|
default:
|
|
20934
21776
|
throw new Error("Unknown Devthink request.");
|
|
20935
21777
|
}
|
|
@@ -21111,20 +21953,42 @@ async function swarmstateof() {
|
|
|
21111
21953
|
const killswitch = await memory.getkillswitch() ?? { engaged: false };
|
|
21112
21954
|
const usage = await memory.getagentusage();
|
|
21113
21955
|
const board = await memory.getblackboard() ?? emptyboard();
|
|
21956
|
+
const topology = await memory.gettopology();
|
|
21957
|
+
const reviewrequests = await memory.getreviewrequests();
|
|
21958
|
+
const reviews = await memory.getcriticreviews();
|
|
21959
|
+
const handoffs = await memory.gethandoffs();
|
|
21960
|
+
const report = await memory.getreport();
|
|
21114
21961
|
return {
|
|
21115
21962
|
overview: swarmoverview({ agents, queue, mailboxes }),
|
|
21116
21963
|
agents: agents.map((agent) => {
|
|
21117
21964
|
const claim2 = queue.claims.find((record2) => record2.agentid === agent.id);
|
|
21118
21965
|
const task = claim2 !== void 0 ? queue.items.find((item) => item.id === claim2.taskid) : void 0;
|
|
21119
21966
|
const agentusage = usage.find((entry) => entry.agentid === agent.id);
|
|
21120
|
-
|
|
21967
|
+
const openreview = reviewrequests.find((request) => request.fromagentid === agent.id && (request.state === "open" || request.state === "acked"));
|
|
21968
|
+
const receivedreview = reviews.find((review) => review.subjectagentid === agent.id);
|
|
21969
|
+
const arrows = handoffs.filter((record2) => record2.fromagentid === agent.id || record2.toagentid === agent.id).map((record2) => `${record2.fromagentid}\u2192${record2.toagentid}`);
|
|
21970
|
+
return { id: agent.id, name: agent.name, role: agent.role, state: agent.state, depth: agent.depth, ...agent.tabid !== void 0 ? { tabid: agent.tabid } : {}, ...agent.sessionid !== void 0 ? { sessionid: agent.sessionid } : {}, ...agent.parentid !== void 0 ? { parentid: agent.parentid } : {}, registeredat: agent.registeredat, ...agent.heartbeatat !== void 0 ? { heartbeatat: agent.heartbeatat } : {}, ...agent.budget !== void 0 ? { budget: agent.budget } : {}, ...agent.scope !== void 0 ? { scope: agent.scope } : {}, ...agentusage !== void 0 ? { usage: agentusage } : {}, ...task !== void 0 ? { currenttask: task.payload } : {}, unread: unreadcount(mailboxes, agent.id), ...openreview !== void 0 ? { reviewstatus: `awaiting the review of ${openreview.toagentid}` } : receivedreview !== void 0 ? { reviewstatus: `last critic verdict ${receivedreview.verdict}` } : {}, handoffarrows: [...new Set(arrows)] };
|
|
21121
21971
|
}),
|
|
21122
21972
|
queue: { lanes: queue.lanes, priorities: queue.priorities, completionpolicy: queue.completionpolicy, items: queue.items, claims: queue.claims, lanesreport: lanereport(queue), counts: taskcounts(queue), complete: queuecomplete(queue) },
|
|
21123
21973
|
mailboxes: mailboxes.map((mailbox) => ({ agentid: mailbox.agentid, unread: mailbox.unread, inbox: mailbox.inbox, outbox: mailbox.outbox })),
|
|
21124
21974
|
blackboard: { sections: boardsummary(board, now), entries: readentries({ board, now }) },
|
|
21125
21975
|
spawns: await memory.getspawnrecords(),
|
|
21126
21976
|
events: await memory.getagentevents(),
|
|
21127
|
-
killswitch
|
|
21977
|
+
killswitch,
|
|
21978
|
+
...topology !== void 0 ? { topology } : {},
|
|
21979
|
+
splits: await memory.getplannersplits(),
|
|
21980
|
+
reviews,
|
|
21981
|
+
verifications: await memory.getverifierchecks(),
|
|
21982
|
+
reviewrequests,
|
|
21983
|
+
handoffs,
|
|
21984
|
+
locks: await memory.getlocks(),
|
|
21985
|
+
conflicts: await memory.getconflictscans(),
|
|
21986
|
+
...report !== void 0 ? { report } : {},
|
|
21987
|
+
board: boardstate({ agents, queue, ...topology !== void 0 ? { topology } : {}, now }),
|
|
21988
|
+
escalations: await memory.getescalations(),
|
|
21989
|
+
consensus: await memory.getconsensusrounds(),
|
|
21990
|
+
timeline: interleavetimeline(await memory.getswarmtimeline()),
|
|
21991
|
+
costs: await memory.getswarmcosts()
|
|
21128
21992
|
};
|
|
21129
21993
|
}
|
|
21130
21994
|
async function llmstateof() {
|