@wenathlan/devthink 2.0.13 → 2.0.15
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/CHANGELOG.md +27 -0
- package/accounts.ts +2087 -0
- package/agent.ts +2331 -0
- package/antigravity.ts +2139 -0
- package/apifreeze.ts +4146 -0
- package/auth.ts +642 -0
- package/background.ts +16837 -0
- package/bridge.ts +2080 -0
- package/build.ts +54 -0
- package/bun.ts +26 -0
- package/capture.ts +1614 -0
- package/checksecrets.ts +72 -0
- package/cjs.ts +9 -0
- package/cli.ts +3500 -0
- package/commands.ts +1101 -0
- package/companion.ts +450 -0
- package/compatibility.ts +37 -0
- package/config.ts +3137 -0
- package/constants.ts +943 -0
- package/core.ts +1473 -0
- package/crossbrowser.ts +1359 -0
- package/dashdone.ts +605 -0
- package/data.ts +943 -0
- package/database.ts +133 -0
- package/debug.ts +850 -0
- package/deno.ts +27 -0
- package/devthink.ts +707 -0
- package/dist/accounts.d.ts +409 -0
- package/dist/agent.d.ts +837 -0
- package/dist/antigravity.d.ts +380 -0
- package/dist/apifreeze.d.ts +113 -0
- package/dist/auth.d.ts +224 -0
- package/{bridge.d.ts → dist/bridge.d.ts} +1 -1
- package/dist/capture.d.ts +595 -0
- package/dist/cjs.d.ts +6 -0
- package/{cli.js → dist/cli.js} +2 -2
- package/dist/commands.d.ts +310 -0
- package/dist/compatibility.d.ts +17 -0
- package/dist/config.d.ts +1853 -0
- package/dist/constants.d.ts +637 -0
- package/dist/core.d.ts +361 -0
- package/dist/data.d.ts +330 -0
- package/dist/database.d.ts +24 -0
- package/dist/debug.d.ts +270 -0
- package/dist/engine.d.ts +74 -0
- package/dist/environments.d.ts +244 -0
- package/dist/evidence.d.ts +277 -0
- package/dist/export.d.ts +311 -0
- package/dist/fingerprint.d.ts +594 -0
- package/dist/flow.d.ts +300 -0
- package/dist/gates.d.ts +103 -0
- package/dist/identity.d.ts +51 -0
- package/dist/ids.d.ts +7 -0
- package/dist/llm.d.ts +652 -0
- package/dist/models.d.ts +1807 -0
- package/dist/net.d.ts +325 -0
- package/dist/neutral.d.ts +41 -0
- package/dist/oauth.d.ts +703 -0
- package/dist/page.d.ts +1166 -0
- package/dist/perf.d.ts +481 -0
- package/dist/plan.d.ts +134 -0
- package/dist/project.d.ts +395 -0
- package/dist/providers.d.ts +37 -0
- package/dist/quota.d.ts +453 -0
- package/dist/recovery.d.ts +621 -0
- package/dist/request.d.ts +1492 -0
- package/dist/run.d.ts +779 -0
- package/dist/runtime.d.ts +212 -0
- package/dist/search.d.ts +375 -0
- package/dist/security.d.ts +534 -0
- package/dist/serve.d.ts +136 -0
- package/dist/session.d.ts +296 -0
- package/dist/storage.d.ts +53 -0
- package/dist/streaming.d.ts +1119 -0
- package/dist/swarm.d.ts +328 -0
- package/dist/tools.d.ts +170 -0
- package/dist/types.d.ts +8357 -0
- package/dist/utils.d.ts +82 -0
- package/dist/version.d.ts +7 -0
- package/dist/versionregistry.d.ts +406 -0
- package/dist/views.d.ts +579 -0
- package/dist/workbenchsession.d.ts +69 -0
- package/dist/workflow.d.ts +649 -0
- package/docs/readme.md +92 -0
- package/docs/schemas/antigravity.json +192 -0
- package/engine.ts +1524 -0
- package/environments.ts +1005 -0
- package/evidence.ts +643 -0
- package/export.ts +889 -0
- package/fingerprint.ts +1458 -0
- package/flow.ts +1190 -0
- package/gates.ts +307 -0
- package/gateway.ts +34 -0
- package/hardening.ts +191 -0
- package/headless.ts +369 -0
- package/http.ts +2110 -0
- package/identity.ts +246 -0
- package/ids.ts +26 -0
- package/index.ts +369 -0
- package/llm.ts +2682 -0
- package/mcp.ts +1835 -0
- package/{memory.js → memory.ts} +3834 -2310
- package/models.ts +3805 -0
- package/modes.ts +43 -0
- package/net.ts +1214 -0
- package/neutral.ts +362 -0
- package/node.ts +26 -0
- package/oauth.ts +2216 -0
- package/offscreen.ts +170 -0
- package/pack.ts +956 -0
- package/package.json +175 -82
- package/page.ts +7824 -0
- package/pagebridge.ts +1116 -0
- package/perf.ts +957 -0
- package/plan.ts +672 -0
- package/plugin.ts +76 -0
- package/{policy.js → policy.ts} +5073 -4679
- package/prisma.config.ts +27 -0
- package/progress.ts +1308 -0
- package/project.ts +1291 -0
- package/protocol.ts +4187 -0
- package/providers.ts +246 -0
- package/quota.ts +1887 -0
- package/recovery.ts +2433 -0
- package/request.ts +4482 -0
- package/run.ts +2135 -0
- package/runtime.ts +526 -0
- package/search.ts +1760 -0
- package/security.ts +1673 -0
- package/serve.ts +419 -0
- package/server.ts +5538 -0
- package/session.ts +1162 -0
- package/storage.ts +182 -0
- package/streaming.ts +4101 -0
- package/swarm.ts +884 -0
- package/sync.ts +56 -0
- package/tests/agentcert.test.ts +128 -0
- package/tests/agentfleet.test.ts +214 -0
- package/tests/agentreview.test.ts +241 -0
- package/tests/agentstream.test.ts +335 -0
- package/tests/agentwork.test.ts +356 -0
- package/tests/apifreeze.test.ts +290 -0
- package/tests/apimap.test.ts +112 -0
- package/tests/approvalgate.test.ts +175 -0
- package/tests/artifactmanifest.test.ts +204 -0
- package/tests/attentionfeed.test.ts +163 -0
- package/tests/backgroundruns.test.ts +100 -0
- package/tests/batchscheduling.test.ts +131 -0
- package/tests/blackboard.test.ts +238 -0
- package/tests/bridge.test.ts +1088 -0
- package/tests/browsercoverage.test.ts +134 -0
- package/tests/browserpolyfills.test.ts +124 -0
- package/tests/build.mjs +1260 -0
- package/tests/clientauth.test.ts +223 -0
- package/tests/clitools.test.ts +578 -0
- package/tests/confirmgates.test.ts +190 -0
- package/tests/containerpack.test.ts +143 -0
- package/tests/controlflow.test.ts +2120 -0
- package/tests/coordination.test.ts +345 -0
- package/tests/copyscreen.test.ts +139 -0
- package/tests/core.test.ts +417 -0
- package/tests/costcert.test.ts +95 -0
- package/tests/crossbrowser.test.ts +115 -0
- package/tests/cspaudit.test.ts +79 -0
- package/tests/dashdone.test.ts +615 -0
- package/tests/datagrid.test.ts +137 -0
- package/tests/doccheck.test.ts +107 -0
- package/tests/dombatch.test.ts +109 -0
- package/tests/emulation.test.ts +282 -0
- package/tests/environments.test.ts +259 -0
- package/tests/evidenceviews.test.ts +88 -0
- package/tests/exporttools.test.ts +185 -0
- package/tests/firefoxprep.test.ts +134 -0
- package/tests/flowlibrary.test.ts +242 -0
- package/tests/flowrun.test.ts +303 -0
- package/tests/forensics.test.ts +439 -0
- package/tests/gateway.test.ts +939 -0
- package/tests/headlesslib.test.ts +171 -0
- package/tests/httpstream.test.ts +277 -0
- package/tests/immutablelog.test.ts +157 -0
- package/tests/inboundguard.test.ts +177 -0
- package/tests/lazyload.test.ts +72 -0
- package/tests/librarymodes.test.ts +623 -0
- package/tests/llm.test.ts +696 -0
- package/tests/manifest.test.ts +226 -0
- package/tests/maskinputs.test.ts +150 -0
- package/tests/matrixverify.test.ts +111 -0
- package/tests/mavenpack.test.ts +75 -0
- package/tests/mcpserver.test.ts +704 -0
- package/tests/memory.test.ts +4155 -0
- package/tests/memorycare.test.ts +230 -0
- package/tests/migrateplan.test.ts +349 -0
- package/tests/minimization.test.ts +414 -0
- package/tests/modelroute.test.ts +112 -0
- package/tests/multiagent.test.ts +440 -0
- package/tests/nativebridge.test.ts +807 -0
- package/tests/navigation.test.ts +284 -0
- package/tests/nugetpack.test.ts +83 -0
- package/tests/orchestration.test.ts +449 -0
- package/tests/originpolicy.test.ts +405 -0
- package/tests/outputcompare.test.ts +97 -0
- package/tests/package.test.ts +91 -0
- package/tests/pagebridge.test.ts +5909 -0
- package/tests/pentest.test.ts +75 -0
- package/tests/perfrecords.test.ts +257 -0
- package/tests/permdiff.test.ts +85 -0
- package/tests/phishguard.test.ts +128 -0
- package/tests/pickerviews.test.ts +155 -0
- package/tests/pipelines.test.ts +381 -0
- package/tests/planlint.test.ts +240 -0
- package/tests/planreview.test.ts +383 -0
- package/tests/platformtargets.test.ts +56 -0
- package/tests/policy.test.ts +8284 -0
- package/tests/portability.test.ts +96 -0
- package/tests/progress.test.ts +1036 -0
- package/tests/promptlibrary.test.ts +116 -0
- package/tests/protocol.test.ts +4793 -0
- package/tests/publishingpipeline.test.ts +314 -0
- package/tests/quickactions.test.ts +164 -0
- package/tests/readiness.test.ts +78 -0
- package/tests/recipes.test.ts +163 -0
- package/tests/redactshots.test.ts +216 -0
- package/tests/registernvidiakeys.ts +51 -0
- package/tests/relayserve.test.ts +304 -0
- package/tests/releasenotes.test.ts +276 -0
- package/tests/resilience.test.ts +467 -0
- package/tests/resourceaware.test.ts +187 -0
- package/tests/runbudget.test.ts +125 -0
- package/tests/runhistory.test.ts +299 -0
- package/tests/runreplay.test.ts +113 -0
- package/tests/runresume.test.ts +184 -0
- package/tests/runstate.test.ts +288 -0
- package/tests/runtimeadapters.test.ts +251 -0
- package/tests/safariskeleton.test.ts +115 -0
- package/tests/sandboxframe.test.ts +196 -0
- package/tests/sbom.test.ts +105 -0
- package/tests/secretvault.test.ts +188 -0
- package/tests/selcache.test.ts +111 -0
- package/tests/sessioninterface.test.ts +791 -0
- package/tests/sessions.test.ts +591 -0
- package/tests/siteprefs.test.ts +228 -0
- package/tests/snapshotdelta.test.ts +165 -0
- package/tests/statusviews.test.ts +237 -0
- package/tests/stepmeter.test.ts +200 -0
- package/tests/streamparse.test.ts +163 -0
- package/tests/stylepruning.test.ts +90 -0
- package/tests/surfaces.test.ts +282 -0
- package/tests/sweep.test.ts +78 -0
- package/tests/syncbridge.test.ts +164 -0
- package/tests/taskqueue.test.ts +206 -0
- package/tests/toolcalls.test.ts +336 -0
- package/tests/toolcatalog.test.ts +201 -0
- package/tests/torture-agent.test.ts +706 -0
- package/tests/torture-auth.test.ts +461 -0
- package/tests/torture-commands.test.ts +895 -0
- package/tests/torture-data.test.ts +923 -0
- package/tests/torture-environments.test.ts +285 -0
- package/tests/torture-export.test.ts +263 -0
- package/tests/torture-http.test.ts +1379 -0
- package/tests/torture-llm.test.ts +808 -0
- package/tests/torture-memory.test.ts +1084 -0
- package/tests/torture-plan.test.ts +829 -0
- package/tests/torture-run.test.ts +626 -0
- package/tests/torture-session.test.ts +928 -0
- package/tests/torture-swarm.test.ts +1245 -0
- package/tests/torture-tools.test.ts +964 -0
- package/tests/torture-workflow.test.ts +2354 -0
- package/tests/tourviews.test.ts +52 -0
- package/tests/transparency.test.ts +147 -0
- package/tests/trigger.test.ts +941 -0
- package/tests/v1sunset.test.ts +428 -0
- package/tests/virtlist.test.ts +94 -0
- package/tests/vision.test.ts +540 -0
- package/tests/vsixpack.test.ts +145 -0
- package/tests/webapi.test.ts +523 -0
- package/tests/workflow.test.ts +1785 -0
- package/tests/workfloweditor.test.ts +759 -0
- package/tests/xpipack.test.ts +128 -0
- package/tools.ts +734 -0
- package/types.ts +9908 -0
- package/umd.ts +9 -0
- package/utils.ts +249 -0
- package/version.ts +8 -0
- package/versionregistry.ts +1310 -0
- package/views.ts +2153 -0
- package/vitest.config.ts +12 -0
- package/web/App.tsx +78 -0
- package/web/README.md +47 -0
- package/web/capacitor.config.ts +46 -0
- package/web/const.ts +19 -0
- package/web/control.shell.tsx +66 -0
- package/web/dashboardpage.ts +554 -0
- package/web/db.ts +493 -0
- package/web/design.html +541 -0
- package/web/gatewayview/Gateway.tsx +345 -0
- package/web/gatewayview/config.ts +1042 -0
- package/web/gatewayview/definition.ts +174 -0
- package/web/icons.ts +15 -0
- package/{manifest.json → web/manifest.json} +1 -1
- package/web/optionspage.ts +691 -0
- package/web/popup.ts +1651 -0
- package/web/schema.prisma +176 -0
- package/web/sidepanel.ts +7941 -0
- package/web/sitemanifest.json +13 -0
- package/web/terminal-ui.tsx +622 -0
- package/web/transparencypage.ts +541 -0
- package/web/vite.config.ts +39 -0
- package/web/workspace.ts +23 -0
- package/workbenchmemory.ts +104 -0
- package/workbenchsession.ts +304 -0
- package/workflow.ts +4495 -0
- package/bridge.d.ts.map +0 -1
- package/bridge.js +0 -1877
- package/bun.d.ts.map +0 -1
- package/bun.js +0 -52329
- package/caps/background.json +0 -712
- package/caps/cli.json +0 -360
- package/caps/library.json +0 -3611
- package/caps/mcp.json +0 -126
- package/caps/pagebridge.json +0 -377
- package/caps/popup.json +0 -51
- package/caps/sidepanel.json +0 -522
- package/checksums.txt +0 -142
- package/cli.d.ts.map +0 -1
- package/companion.d.ts.map +0 -1
- package/companion.js +0 -389
- package/crossbrowser.d.ts.map +0 -1
- package/crossbrowser.js +0 -1191
- package/dashdone.d.ts.map +0 -1
- package/dashdone.js +0 -368
- package/deno.d.ts.map +0 -1
- package/deno.js +0 -52325
- package/devthink.umd.js +0 -52346
- package/fixtures/bun.ts +0 -7
- package/fixtures/cjs.js +0 -6
- package/fixtures/deno.ts +0 -6
- package/fixtures/example-org-pagestate.json +0 -22
- package/fixtures/importers/automa-workflow.json +0 -18
- package/fixtures/importers/selenium-side.json +0 -20
- package/fixtures/importers/tabular-plan.csv +0 -7
- package/fixtures/importers/uivision-macro.json +0 -10
- package/fixtures/importers/v1-plan.json +0 -13
- package/fixtures/mcp-client.mjs +0 -14
- package/fixtures/pages/checkout.html +0 -192
- package/fixtures/pages/dashboard.html +0 -110
- package/fixtures/pages/feed.html +0 -113
- package/fixtures/pages/product-grid.html +0 -150
- package/fixtures/plans/form-inventory-plan.json +0 -11
- package/fixtures/plans/release-notes-plan.json +0 -11
- package/fixtures/recipes/agentscompete.json +0 -11
- package/fixtures/recipes/agentsforms.json +0 -11
- package/fixtures/recipes/agentsmonitor.json +0 -11
- package/fixtures/recipes/agentsreview.json +0 -29
- package/fixtures/recipes/agentswarm.json +0 -11
- package/fixtures/recipes/fillcalendar.json +0 -12
- package/fixtures/recipes/fillcheckout.json +0 -13
- package/fixtures/recipes/filldependentdropdowns.json +0 -12
- package/fixtures/recipes/fillfromcsv.json +0 -13
- package/fixtures/recipes/fillloginform.json +0 -12
- package/fixtures/recipes/fillmultistepwizard.json +0 -13
- package/fixtures/recipes/fillregistration.json +0 -12
- package/fixtures/recipes/fillsearchfilters.json +0 -12
- package/fixtures/recipes/filltypeahead.json +0 -11
- package/fixtures/recipes/monitoralerts.json +0 -12
- package/fixtures/recipes/monitoravailability.json +0 -11
- package/fixtures/recipes/monitornetworkquiet.json +0 -12
- package/fixtures/recipes/monitorpagechanges.json +0 -11
- package/fixtures/recipes/monitorpricechange.json +0 -11
- package/fixtures/recipes/monitorschedule.json +0 -12
- package/fixtures/recipes/scrapedynamicprices.json +0 -13
- package/fixtures/recipes/scrapeimagegallery.json +0 -12
- package/fixtures/recipes/scrapeinfinitefeed.json +0 -13
- package/fixtures/recipes/scrapenestedlists.json +0 -12
- package/fixtures/recipes/scrapeproductgrid.json +0 -12
- package/fixtures/recipes/scrapereviews.json +0 -12
- package/fixtures/recipes/scrapesearchresults.json +0 -12
- package/fixtures/recipes/scrapetablepagination.json +0 -12
- package/fixtures/recipes/testaccessibility.json +0 -34
- package/fixtures/recipes/testconsoleerrors.json +0 -12
- package/fixtures/recipes/testdeeplinks.json +0 -12
- package/fixtures/recipes/testformvalidation.json +0 -13
- package/fixtures/recipes/testlayoutshift.json +0 -12
- package/fixtures/recipes/testlinkcheck.json +0 -12
- package/fixtures/recipes/testloadtiming.json +0 -12
- package/fixtures/recipes/testvisualdiff.json +0 -12
- package/fixtures/workflows/changelog-digest-workflow.json +0 -13
- package/fixtures/workflows/form-survey-workflow.json +0 -13
- package/gallery.json +0 -706
- package/hardening.d.ts.map +0 -1
- package/hardening.js +0 -188
- package/headless.d.ts.map +0 -1
- package/headless.js +0 -11522
- package/http.d.ts.map +0 -1
- package/http.js +0 -2165
- package/index.cjs +0 -54682
- package/index.d.ts.map +0 -1
- package/index.js +0 -52310
- package/index.neutral.js +0 -46343
- package/mcp.d.ts.map +0 -1
- package/mcp.js +0 -20515
- package/memory.d.ts.map +0 -1
- package/nativehost.template.json +0 -10
- package/node.cjs +0 -54686
- package/node.d.ts.map +0 -1
- package/pack.d.ts.map +0 -1
- package/pack.js +0 -805
- package/policy.d.ts.map +0 -1
- package/progress.d.ts.map +0 -1
- package/progress.js +0 -645
- package/protocol.d.ts.map +0 -1
- package/protocol.js +0 -8334
- package/schemas/audit.schema.json +0 -226
- package/schemas/capability.schema.json +0 -113
- package/schemas/envelope.schema.json +0 -162
- package/schemas/memory.schema.json +0 -51
- package/schemas/observation.schema.json +0 -113
- package/schemas/plan.schema.json +0 -208
- package/schemas/progress.schema.json +0 -126
- package/schemas/proposal.schema.json +0 -68
- package/schemas/session.schema.json +0 -82
- package/schemas/tool.schema.json +0 -94
- package/server.d.ts.map +0 -1
- package/server.js +0 -28877
- package/umd-example.html +0 -18
- package/umd.d.ts.map +0 -1
- /package/{bun.d.ts → dist/bun.d.ts} +0 -0
- /package/{cli.d.ts → dist/cli.d.ts} +0 -0
- /package/{companion.d.ts → dist/companion.d.ts} +0 -0
- /package/{crossbrowser.d.ts → dist/crossbrowser.d.ts} +0 -0
- /package/{dashdone.d.ts → dist/dashdone.d.ts} +0 -0
- /package/{deno.d.ts → dist/deno.d.ts} +0 -0
- /package/{hardening.d.ts → dist/hardening.d.ts} +0 -0
- /package/{headless.d.ts → dist/headless.d.ts} +0 -0
- /package/{http.d.ts → dist/http.d.ts} +0 -0
- /package/{index.d.ts → dist/index.d.ts} +0 -0
- /package/{mcp.d.ts → dist/mcp.d.ts} +0 -0
- /package/{memory.d.ts → dist/memory.d.ts} +0 -0
- /package/{node.d.ts → dist/node.d.ts} +0 -0
- /package/{pack.d.ts → dist/pack.d.ts} +0 -0
- /package/{policy.d.ts → dist/policy.d.ts} +0 -0
- /package/{progress.d.ts → dist/progress.d.ts} +0 -0
- /package/{protocol.d.ts → dist/protocol.d.ts} +0 -0
- /package/{server.d.ts → dist/server.d.ts} +0 -0
- /package/{umd.d.ts → dist/umd.d.ts} +0 -0
package/swarm.ts
ADDED
|
@@ -0,0 +1,884 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The swarm module of the 1.1.90 consolidation: every correlated variation of the swarm logic interned in this one file, so the module family carries one surface without duplicate variations.
|
|
3
|
+
* The correlation is the multi agent swarm organized over one shared queue and one shared board: orchestration holds the organizing topology (leader worker election by user rule, work slicing, result collection, worker scaling inside the user bound, the planner executor split with step reporting, critic reviews, verifier checks, the progressboard aggregation, human decided escalations, arbitration of competing claims and consensus rounds with quorum); taskqueue holds the shared work every agent of the swarm claims from (lanes, priorities, claims with heartbeats, work stealing with lane ownership, completion policy and the requeue of orphaned tasks); and blackboard is the shared memory every agent reads with no relay (sections, posting with authors, freshness filtered reads, retirement by the user window and the consent class inheritance of the source extraction).
|
|
4
|
+
* Orchestration never bypasses review: the leader only organizes work whose proposals still pass the same human review, the critic reads outputs without acting on the page, and every escalation lands on the user.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/* ── Merged from orchestration.ts ── */
|
|
8
|
+
|
|
9
|
+
import type {
|
|
10
|
+
agentidentity,
|
|
11
|
+
arbitrationrule,
|
|
12
|
+
boardlane,
|
|
13
|
+
boardmilestone,
|
|
14
|
+
consensusround,
|
|
15
|
+
consensusvote,
|
|
16
|
+
criticreview,
|
|
17
|
+
escalationrecord,
|
|
18
|
+
executorreport,
|
|
19
|
+
leaderworker,
|
|
20
|
+
plannerexecutor,
|
|
21
|
+
progressboard,
|
|
22
|
+
reviewrequest,
|
|
23
|
+
reviewverdict,
|
|
24
|
+
taskitem,
|
|
25
|
+
taskqueue,
|
|
26
|
+
verifiercheck,
|
|
27
|
+
workerassignment,
|
|
28
|
+
} from "./types.js";
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Multi agent orchestration logic of the 1.1.59 family.
|
|
32
|
+
* Every correlated rule for the leader worker topology (election by user rule, work slicing, result collection, worker scaling), the planner executor split with step reporting, critic reviews over agent outputs, verifier checks of result claims, the progressboard aggregation, escalations that stay human decided, arbitration of competing claims and consensus rounds with quorum lives in this file.
|
|
33
|
+
* Orchestration never bypasses review: the leader only organizes work whose proposals still pass the same human review, the critic reads outputs without acting on the page and every escalation lands on the user.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
/** Elects the leader of the swarm and builds the leader worker topology: the user rule names the leader (first registration or one named agent), the live workers, critics and verifiers join their lanes by role, and the topology records the election. */
|
|
37
|
+
export function electleader(input: {
|
|
38
|
+
agents: agentidentity[];
|
|
39
|
+
id: string;
|
|
40
|
+
rule?: { kind: "first" | "named"; agentid?: string };
|
|
41
|
+
now: number;
|
|
42
|
+
}): leaderworker {
|
|
43
|
+
const live = input.agents.filter((agent) => agent.state !== "stopped");
|
|
44
|
+
if (live.length === 0)
|
|
45
|
+
throw new Error("The swarm holds no live agent; the leader election waits for the user to register one.");
|
|
46
|
+
const rule = input.rule ?? { kind: "first" as const };
|
|
47
|
+
let leader: agentidentity | undefined;
|
|
48
|
+
if (rule.kind === "named") {
|
|
49
|
+
if (rule.agentid === undefined || rule.agentid.trim() === "")
|
|
50
|
+
throw new Error("The named election rule needs the agent id the user named.");
|
|
51
|
+
leader = live.find((agent) => agent.id === rule.agentid);
|
|
52
|
+
if (!leader)
|
|
53
|
+
throw new Error(
|
|
54
|
+
`The named election rule names the agent ${rule.agentid} which is not a live agent of the swarm.`,
|
|
55
|
+
);
|
|
56
|
+
} else {
|
|
57
|
+
leader = live[live.length - 1];
|
|
58
|
+
}
|
|
59
|
+
if (!leader) throw new Error("The leader election found no live agent.");
|
|
60
|
+
const leaderid = leader.id;
|
|
61
|
+
const workers = live.filter((agent) => agent.id !== leaderid && agent.role === "worker");
|
|
62
|
+
const critics = live.filter((agent) => agent.id !== leaderid && agent.role === "critic");
|
|
63
|
+
const verifiers = live.filter((agent) => agent.id !== leaderid && agent.role === "verifier");
|
|
64
|
+
return {
|
|
65
|
+
id: input.id,
|
|
66
|
+
leaderid: leader.id,
|
|
67
|
+
workerids: workers.map((agent) => agent.id),
|
|
68
|
+
criticids: critics.map((agent) => agent.id),
|
|
69
|
+
verifierids: verifiers.map((agent) => agent.id),
|
|
70
|
+
assignments: [],
|
|
71
|
+
rule: { kind: rule.kind, ...(rule.agentid !== undefined ? { agentid: rule.agentid } : {}) },
|
|
72
|
+
electedat: input.now,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Slices the tasks across the workers of the topology: the workers take turns over the queued and claimed tasks in queue order, every slice names the task payload it owns and the assignments record the worker, the task and the slice. */
|
|
77
|
+
export function assignwork(input: { topology: leaderworker; tasks: taskitem[]; now: number }): leaderworker {
|
|
78
|
+
if (input.topology.workerids.length === 0)
|
|
79
|
+
throw new Error("The topology holds no worker; the user adds workers before the assignment.");
|
|
80
|
+
const assignments: workerassignment[] = [];
|
|
81
|
+
const tasks = input.tasks.filter((task) => task.state === "queued" || task.state === "claimed");
|
|
82
|
+
tasks.forEach((task, index) => {
|
|
83
|
+
const workerid = input.topology.workerids[index % input.topology.workerids.length]!;
|
|
84
|
+
assignments.push({
|
|
85
|
+
workerid,
|
|
86
|
+
taskid: task.id,
|
|
87
|
+
slice: `${task.payload} (slice ${Math.floor(index / input.topology.workerids.length) + 1} of lane ${task.lane})`,
|
|
88
|
+
assignedat: input.now,
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
return { ...input.topology, assignments };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Gathers the worker outputs with status: every assignment reports its state while the outputs the workers returned land beside their tasks, and the missing list names the assignments still without an output. */
|
|
95
|
+
export function collectresults(input: {
|
|
96
|
+
topology: leaderworker;
|
|
97
|
+
outputs: Array<{ workerid: string; taskid: string; state: string; summary: string }>;
|
|
98
|
+
}): { gathered: Array<{ workerid: string; taskid: string; state: string; summary: string }>; missing: string[] } {
|
|
99
|
+
const gathered = input.topology.assignments.map((assignment) => {
|
|
100
|
+
const output = input.outputs.find(
|
|
101
|
+
(entry) => entry.taskid === assignment.taskid && entry.workerid === assignment.workerid,
|
|
102
|
+
);
|
|
103
|
+
return (
|
|
104
|
+
output ?? {
|
|
105
|
+
workerid: assignment.workerid,
|
|
106
|
+
taskid: assignment.taskid,
|
|
107
|
+
state: "pending",
|
|
108
|
+
summary: `The worker ${assignment.workerid} has not returned its slice of the task ${assignment.taskid} yet.`,
|
|
109
|
+
}
|
|
110
|
+
);
|
|
111
|
+
});
|
|
112
|
+
return {
|
|
113
|
+
gathered,
|
|
114
|
+
missing: gathered.filter((entry) => entry.state === "pending").map((entry) => `${entry.workerid}:${entry.taskid}`),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Scales the worker lane by load: queued work beyond the current workers adds the live worker role agents the swarm already holds (never past the user configured bound while one exists), idle workers retire down to the work left, and no engine cap ever exists. */
|
|
119
|
+
export function scaleworkers(input: {
|
|
120
|
+
topology: leaderworker;
|
|
121
|
+
agents: agentidentity[];
|
|
122
|
+
pending: number;
|
|
123
|
+
bound?: number;
|
|
124
|
+
now: number;
|
|
125
|
+
}): { topology: leaderworker; added: string[]; retired: string[]; reason: string } {
|
|
126
|
+
const live = input.agents.filter(
|
|
127
|
+
(agent) => agent.state === "active" && agent.role === "worker" && agent.id !== input.topology.leaderid,
|
|
128
|
+
);
|
|
129
|
+
const current = input.topology.workerids.filter((workerid) => live.some((agent) => agent.id === workerid));
|
|
130
|
+
const ceiling = input.bound;
|
|
131
|
+
if (input.pending > current.length) {
|
|
132
|
+
const available = live.filter((agent) => !current.includes(agent.id)).map((agent) => agent.id);
|
|
133
|
+
const wanted = input.pending - current.length;
|
|
134
|
+
const addable =
|
|
135
|
+
ceiling === undefined
|
|
136
|
+
? available.slice(0, wanted)
|
|
137
|
+
: available.slice(0, Math.min(wanted, Math.max(ceiling - current.length, 0)));
|
|
138
|
+
if (addable.length === 0)
|
|
139
|
+
return {
|
|
140
|
+
topology: input.topology,
|
|
141
|
+
added: [],
|
|
142
|
+
retired: [],
|
|
143
|
+
reason:
|
|
144
|
+
ceiling === undefined
|
|
145
|
+
? `The load of ${input.pending} pending slices exceeds the ${current.length} workers but the swarm holds no further live worker role agent to add.`
|
|
146
|
+
: `The load of ${input.pending} pending slices exceeds the ${current.length} workers but the user configured bound of ${ceiling} workers holds.`,
|
|
147
|
+
};
|
|
148
|
+
return {
|
|
149
|
+
topology: {
|
|
150
|
+
...input.topology,
|
|
151
|
+
workerids: [...current, ...addable],
|
|
152
|
+
electedat: input.topology.electedat,
|
|
153
|
+
assignments: input.topology.assignments,
|
|
154
|
+
},
|
|
155
|
+
added: addable,
|
|
156
|
+
retired: [],
|
|
157
|
+
reason: `The load of ${input.pending} pending slices added the workers ${addable.join(", ")}; ${ceiling === undefined ? "no bound is configured so the user scale stands alone" : `the user configured bound of ${ceiling} workers holds`}.`,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
const keep = Math.max(input.pending, 0);
|
|
161
|
+
if (current.length > keep) {
|
|
162
|
+
const retired = current.slice(keep);
|
|
163
|
+
return {
|
|
164
|
+
topology: {
|
|
165
|
+
...input.topology,
|
|
166
|
+
workerids: current.slice(0, keep),
|
|
167
|
+
electedat: input.topology.electedat,
|
|
168
|
+
assignments: input.topology.assignments.filter((assignment) => !retired.includes(assignment.workerid)),
|
|
169
|
+
},
|
|
170
|
+
added: [],
|
|
171
|
+
retired,
|
|
172
|
+
reason: `The load of ${input.pending} pending slices retired the idle workers ${retired.join(", ")}.`,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
topology: input.topology,
|
|
177
|
+
added: [],
|
|
178
|
+
retired: [],
|
|
179
|
+
reason: `The load of ${input.pending} pending slices matches the ${current.length} workers; the scale stays unchanged.`,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Splits one task between the planner and the executor: the plan owner agent drafts the reviewed plan while the run owner agent executes it, and the split record starts empty of step reports. */
|
|
184
|
+
export function plannersplit(input: {
|
|
185
|
+
id: string;
|
|
186
|
+
planownerid: string;
|
|
187
|
+
runownerid: string;
|
|
188
|
+
taskid?: string;
|
|
189
|
+
now: number;
|
|
190
|
+
}): plannerexecutor {
|
|
191
|
+
if (input.planownerid.trim() === "" || input.runownerid.trim() === "")
|
|
192
|
+
throw new Error("The planner executor split needs its plan owner and run owner agent ids.");
|
|
193
|
+
if (input.planownerid === input.runownerid)
|
|
194
|
+
throw new Error(
|
|
195
|
+
"The planner executor split keeps plan drafting and execution in different agents; one agent holds both sides never.",
|
|
196
|
+
);
|
|
197
|
+
return {
|
|
198
|
+
id: input.id,
|
|
199
|
+
planownerid: input.planownerid,
|
|
200
|
+
runownerid: input.runownerid,
|
|
201
|
+
...(input.taskid !== undefined && input.taskid.trim() !== "" ? { taskid: input.taskid } : {}),
|
|
202
|
+
stepreports: [],
|
|
203
|
+
splitat: input.now,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Reports one executor step outcome back to the planner agent: the executor appends its report to the split record so the planner reads every step result. */
|
|
208
|
+
export function reportstep(input: {
|
|
209
|
+
split: plannerexecutor;
|
|
210
|
+
stepid: string;
|
|
211
|
+
outcome: "done" | "failed";
|
|
212
|
+
detail: string;
|
|
213
|
+
now: number;
|
|
214
|
+
}): plannerexecutor {
|
|
215
|
+
if (input.stepid.trim() === "") throw new Error("The executor report needs its step id.");
|
|
216
|
+
if (input.detail.trim() === "") throw new Error("The executor report needs its detail in plain language.");
|
|
217
|
+
const report: executorreport = {
|
|
218
|
+
stepid: input.stepid.trim(),
|
|
219
|
+
outcome: input.outcome,
|
|
220
|
+
detail: input.detail,
|
|
221
|
+
reportedat: input.now,
|
|
222
|
+
};
|
|
223
|
+
return {
|
|
224
|
+
...input.split,
|
|
225
|
+
stepreports: [...input.split.stepreports.filter((entry) => entry.stepid !== report.stepid), report],
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** Routes one review request to a reviewing agent: the request names the subject and payload and stays open until its ack, its answer or its user configured timeout. */
|
|
230
|
+
export function requestreview(input: {
|
|
231
|
+
requests: reviewrequest[];
|
|
232
|
+
id: string;
|
|
233
|
+
fromagentid: string;
|
|
234
|
+
toagentid: string;
|
|
235
|
+
subject: string;
|
|
236
|
+
payload: string;
|
|
237
|
+
timeoutms?: number;
|
|
238
|
+
now: number;
|
|
239
|
+
}): reviewrequest[] {
|
|
240
|
+
if (input.subject.trim() === "") throw new Error("The review request needs its subject.");
|
|
241
|
+
if (input.payload.trim() === "") throw new Error("The review request needs its payload.");
|
|
242
|
+
if (input.toagentid.trim() === "" || input.toagentid === input.fromagentid)
|
|
243
|
+
throw new Error("The review request names another reviewing agent, never its own requester.");
|
|
244
|
+
const request: reviewrequest = {
|
|
245
|
+
id: input.id,
|
|
246
|
+
fromagentid: input.fromagentid,
|
|
247
|
+
toagentid: input.toagentid,
|
|
248
|
+
subject: input.subject,
|
|
249
|
+
payload: input.payload,
|
|
250
|
+
state: "open",
|
|
251
|
+
requestedat: input.now,
|
|
252
|
+
...(input.timeoutms !== undefined ? { timeoutat: input.now + input.timeoutms } : {}),
|
|
253
|
+
};
|
|
254
|
+
return [request, ...input.requests];
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** Acks one review request: the reviewing agent confirms receipt while the answer still waits. */
|
|
258
|
+
export function ackreview(input: { requests: reviewrequest[]; id: string; now: number }): reviewrequest[] {
|
|
259
|
+
const request = input.requests.find((entry) => entry.id === input.id);
|
|
260
|
+
if (!request) throw new Error(`The review request ${input.id} does not exist.`);
|
|
261
|
+
if (request.state !== "open")
|
|
262
|
+
throw new Error(`The review request ${input.id} is ${request.state}; only an open request receives its ack.`);
|
|
263
|
+
return input.requests.map((entry) =>
|
|
264
|
+
entry.id === input.id ? { ...entry, state: "acked" as const, ackedat: input.now } : entry,
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** Applies the critic verdict to one review request: the review carries the verdict, the issues and the required changes while the request closes as answered. */
|
|
269
|
+
export function applyreview(input: {
|
|
270
|
+
requests: reviewrequest[];
|
|
271
|
+
id: string;
|
|
272
|
+
reviewerid: string;
|
|
273
|
+
verdict: reviewverdict;
|
|
274
|
+
issues: string[];
|
|
275
|
+
requiredchanges: string[];
|
|
276
|
+
taskid?: string;
|
|
277
|
+
now: number;
|
|
278
|
+
}): { review: criticreview; requests: reviewrequest[] } {
|
|
279
|
+
const request = input.requests.find((entry) => entry.id === input.id);
|
|
280
|
+
if (!request) throw new Error(`The review request ${input.id} does not exist.`);
|
|
281
|
+
if (request.state === "answered" || request.state === "timeout")
|
|
282
|
+
throw new Error(
|
|
283
|
+
`The review request ${input.id} is ${request.state}; an answered or timed out request never reviews again.`,
|
|
284
|
+
);
|
|
285
|
+
if (request.toagentid !== input.reviewerid)
|
|
286
|
+
throw new Error(
|
|
287
|
+
`The review request ${input.id} routes to the agent ${request.toagentid}; the agent ${input.reviewerid} never answers in its place.`,
|
|
288
|
+
);
|
|
289
|
+
if (input.verdict === "changes" && input.requiredchanges.length === 0)
|
|
290
|
+
throw new Error("A changes verdict needs its required changes in plain language.");
|
|
291
|
+
const review: criticreview = {
|
|
292
|
+
id: request.id,
|
|
293
|
+
reviewerid: input.reviewerid,
|
|
294
|
+
subjectagentid: request.fromagentid,
|
|
295
|
+
...(input.taskid !== undefined && input.taskid.trim() !== "" ? { taskid: input.taskid } : {}),
|
|
296
|
+
verdict: input.verdict,
|
|
297
|
+
issues: input.issues,
|
|
298
|
+
requiredchanges: input.requiredchanges,
|
|
299
|
+
reviewedat: input.now,
|
|
300
|
+
};
|
|
301
|
+
return {
|
|
302
|
+
review,
|
|
303
|
+
requests: input.requests.map((entry) =>
|
|
304
|
+
entry.id === input.id ? { ...entry, state: "answered" as const, answeredat: input.now } : entry,
|
|
305
|
+
),
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** Sweeps the review requests past their user configured timeout: an unanswered request without an ack or an answer times out and the requester reads the expiry. */
|
|
310
|
+
export function sweepreviews(input: { requests: reviewrequest[]; now: number }): {
|
|
311
|
+
requests: reviewrequest[];
|
|
312
|
+
timedout: string[];
|
|
313
|
+
} {
|
|
314
|
+
const expired = input.requests
|
|
315
|
+
.filter((request) => request.state === "open" || request.state === "acked")
|
|
316
|
+
.filter((request) => request.timeoutat !== undefined && input.now > request.timeoutat);
|
|
317
|
+
if (expired.length === 0) return { requests: input.requests, timedout: [] };
|
|
318
|
+
const ids = new Set(expired.map((request) => request.id));
|
|
319
|
+
return {
|
|
320
|
+
requests: input.requests.map((request) =>
|
|
321
|
+
ids.has(request.id) ? { ...request, state: "timeout" as const } : request,
|
|
322
|
+
),
|
|
323
|
+
timedout: [...ids],
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** Records one verifier check of a result claim: the verifier names the claim, the method it used and the pass or fail outcome with its evidence; the check itself never writes to the page. */
|
|
328
|
+
export function checkclaim(input: {
|
|
329
|
+
id: string;
|
|
330
|
+
verifierid: string;
|
|
331
|
+
claimagentid: string;
|
|
332
|
+
claim: string;
|
|
333
|
+
method: string;
|
|
334
|
+
outcome: "pass" | "fail";
|
|
335
|
+
evidence?: string;
|
|
336
|
+
taskid?: string;
|
|
337
|
+
now: number;
|
|
338
|
+
}): verifiercheck {
|
|
339
|
+
if (input.claim.trim() === "") throw new Error("The verifier check needs its claim in plain language.");
|
|
340
|
+
if (input.method.trim() === "") throw new Error("The verifier check needs the method the user configured.");
|
|
341
|
+
if (input.claimagentid.trim() === "") throw new Error("The verifier check names the agent whose claim it checks.");
|
|
342
|
+
return {
|
|
343
|
+
id: input.id,
|
|
344
|
+
verifierid: input.verifierid,
|
|
345
|
+
claimagentid: input.claimagentid,
|
|
346
|
+
...(input.taskid !== undefined && input.taskid.trim() !== "" ? { taskid: input.taskid } : {}),
|
|
347
|
+
claim: input.claim,
|
|
348
|
+
method: input.method,
|
|
349
|
+
outcome: input.outcome,
|
|
350
|
+
...(input.evidence !== undefined && input.evidence.trim() !== "" ? { evidence: input.evidence } : {}),
|
|
351
|
+
checkedat: input.now,
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/** Builds the progressboard that aggregates the swarm: one lane per live agent with its role, state, lane of work, current task and milestones, so the user reads every agent at once. */
|
|
356
|
+
export function boardstate(input: {
|
|
357
|
+
agents: agentidentity[];
|
|
358
|
+
queue: taskqueue;
|
|
359
|
+
topology?: leaderworker;
|
|
360
|
+
milestones?: Record<string, boardmilestone[]>;
|
|
361
|
+
now: number;
|
|
362
|
+
}): progressboard {
|
|
363
|
+
const lanes: boardlane[] = input.agents
|
|
364
|
+
.filter((agent) => agent.state !== "stopped")
|
|
365
|
+
.map((agent) => {
|
|
366
|
+
const assignment = input.topology?.assignments.find((entry) => entry.workerid === agent.id);
|
|
367
|
+
const claim = input.queue.claims.find(
|
|
368
|
+
(record) =>
|
|
369
|
+
record.agentid === agent.id &&
|
|
370
|
+
input.queue.items.some((item) => item.id === record.taskid && item.state === "claimed"),
|
|
371
|
+
);
|
|
372
|
+
const task = claim !== undefined ? input.queue.items.find((item) => item.id === claim.taskid) : undefined;
|
|
373
|
+
const lane =
|
|
374
|
+
task?.lane ??
|
|
375
|
+
(agent.role === "critic" || agent.role === "verifier"
|
|
376
|
+
? agent.role
|
|
377
|
+
: agent.role === "planner"
|
|
378
|
+
? "planning"
|
|
379
|
+
: "idle");
|
|
380
|
+
return {
|
|
381
|
+
agentid: agent.id,
|
|
382
|
+
name: agent.name,
|
|
383
|
+
role: agent.role,
|
|
384
|
+
state: agent.state,
|
|
385
|
+
lane,
|
|
386
|
+
...(task !== undefined
|
|
387
|
+
? { currenttask: task.payload }
|
|
388
|
+
: assignment !== undefined
|
|
389
|
+
? { currenttask: assignment.slice }
|
|
390
|
+
: {}),
|
|
391
|
+
milestones: input.milestones?.[agent.id] ?? [],
|
|
392
|
+
};
|
|
393
|
+
});
|
|
394
|
+
return { id: `board:${input.now}`, lanes, builtat: input.now };
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** Lifts one stalled agent decision to the user with its full context: the escalation stays open until the user writes the decision because escalations are always human decided. */
|
|
398
|
+
export function escalate(input: {
|
|
399
|
+
id: string;
|
|
400
|
+
agentid: string;
|
|
401
|
+
subject: string;
|
|
402
|
+
context: string;
|
|
403
|
+
now: number;
|
|
404
|
+
}): escalationrecord {
|
|
405
|
+
if (input.subject.trim() === "") throw new Error("The escalation needs its subject.");
|
|
406
|
+
if (input.context.trim() === "")
|
|
407
|
+
throw new Error("The escalation needs its full context in plain language; the user decides on what the agent saw.");
|
|
408
|
+
if (input.agentid.trim() === "") throw new Error("The escalation names the agent whose decision it lifts.");
|
|
409
|
+
return {
|
|
410
|
+
id: input.id,
|
|
411
|
+
agentid: input.agentid,
|
|
412
|
+
subject: input.subject,
|
|
413
|
+
context: input.context,
|
|
414
|
+
state: "open",
|
|
415
|
+
raisedat: input.now,
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** Resolves one escalation with the decision the user wrote; the decision time records and the record closes. */
|
|
420
|
+
export function resolveescalation(input: {
|
|
421
|
+
escalation: escalationrecord;
|
|
422
|
+
decision: string;
|
|
423
|
+
now: number;
|
|
424
|
+
}): escalationrecord {
|
|
425
|
+
if (input.escalation.state === "decided") throw new Error("The escalation already carries its user decision.");
|
|
426
|
+
if (input.decision.trim() === "") throw new Error("The escalation decision needs the words the user wrote.");
|
|
427
|
+
return { ...input.escalation, state: "decided", decision: input.decision, decidedat: input.now };
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/** Orders competing resource claims by the user configured arbitration rule: the priority strategy follows the user ordered agent list, the age strategy gives the resource to the oldest claim and the leader strategy names the elected leader first. */
|
|
431
|
+
export function arbitrate(input: {
|
|
432
|
+
rule: arbitrationrule;
|
|
433
|
+
leaderid?: string;
|
|
434
|
+
claims: Array<{ agentid: string; claimedat: number }>;
|
|
435
|
+
}): string[] {
|
|
436
|
+
if (input.claims.length === 0) return [];
|
|
437
|
+
if (input.rule.strategy === "priority") {
|
|
438
|
+
const order = input.rule.priorityorder;
|
|
439
|
+
return [...input.claims]
|
|
440
|
+
.sort((one, two) => {
|
|
441
|
+
const oneindex = order.indexOf(one.agentid);
|
|
442
|
+
const twoindex = order.indexOf(two.agentid);
|
|
443
|
+
return (
|
|
444
|
+
(oneindex === -1 ? order.length : oneindex) - (twoindex === -1 ? order.length : twoindex) ||
|
|
445
|
+
one.claimedat - two.claimedat
|
|
446
|
+
);
|
|
447
|
+
})
|
|
448
|
+
.map((claim) => claim.agentid);
|
|
449
|
+
}
|
|
450
|
+
if (input.rule.strategy === "age")
|
|
451
|
+
return [...input.claims]
|
|
452
|
+
.sort((one, two) => one.claimedat - two.claimedat || (one.agentid < two.agentid ? -1 : 1))
|
|
453
|
+
.map((claim) => claim.agentid);
|
|
454
|
+
if (input.leaderid === undefined)
|
|
455
|
+
throw new Error("The leader arbitration strategy needs the elected leader of the topology.");
|
|
456
|
+
return [...input.claims]
|
|
457
|
+
.sort(
|
|
458
|
+
(one, two) =>
|
|
459
|
+
(one.agentid === input.leaderid ? -1 : 1) - (two.agentid === input.leaderid ? -1 : 1) ||
|
|
460
|
+
one.claimedat - two.claimedat,
|
|
461
|
+
)
|
|
462
|
+
.map((claim) => claim.agentid);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/** Opens one consensus round: the subject stays plain language and the quorum stays the user configured value the round needs to carry. */
|
|
466
|
+
export function openconsensus(input: { id: string; subject: string; quorum: number; now: number }): consensusround {
|
|
467
|
+
if (input.subject.trim() === "") throw new Error("The consensus round needs its subject in plain language.");
|
|
468
|
+
if (!Number.isInteger(input.quorum) || input.quorum < 1)
|
|
469
|
+
throw new Error("The consensus round needs its quorum as a positive whole number the user configured.");
|
|
470
|
+
return { id: input.id, subject: input.subject, votes: [], quorum: input.quorum, state: "open", openedat: input.now };
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/** Collects one verdict into a consensus round: an agent votes once, the round stays open until the votes carry or the round closes, and the quorum counts yes votes only. */
|
|
474
|
+
export function castvote(input: {
|
|
475
|
+
round: consensusround;
|
|
476
|
+
agentid: string;
|
|
477
|
+
vote: consensusvote;
|
|
478
|
+
now: number;
|
|
479
|
+
}): consensusround {
|
|
480
|
+
if (input.agentid.trim() === "") throw new Error("The consensus vote names its agent; a blank agent id never votes.");
|
|
481
|
+
if (input.round.state !== "open")
|
|
482
|
+
throw new Error(`The consensus round ${input.round.id} is ${input.round.state}; a closed round collects no vote.`);
|
|
483
|
+
if (input.round.votes.some((entry) => entry.agentid === input.agentid))
|
|
484
|
+
throw new Error(`The agent ${input.agentid} already voted in the round ${input.round.id}.`);
|
|
485
|
+
const votes = [...input.round.votes, { agentid: input.agentid, vote: input.vote, votedat: input.now }];
|
|
486
|
+
const yes = votes.filter((entry) => entry.vote === "yes").length;
|
|
487
|
+
const no = votes.filter((entry) => entry.vote === "no").length;
|
|
488
|
+
if (yes >= input.round.quorum) return { ...input.round, votes, state: "carried", closedat: input.now };
|
|
489
|
+
if (no >= input.round.quorum) return { ...input.round, votes, state: "failed", closedat: input.now };
|
|
490
|
+
return { ...input.round, votes };
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/** Reads the state of one consensus round: the yes, no and abstain counts beside the quorum so the panel shows the votes and the quorum state at once. */
|
|
494
|
+
export function consensusstate(round: consensusround): {
|
|
495
|
+
yes: number;
|
|
496
|
+
no: number;
|
|
497
|
+
abstain: number;
|
|
498
|
+
quorum: number;
|
|
499
|
+
state: string;
|
|
500
|
+
} {
|
|
501
|
+
return {
|
|
502
|
+
yes: round.votes.filter((entry) => entry.vote === "yes").length,
|
|
503
|
+
no: round.votes.filter((entry) => entry.vote === "no").length,
|
|
504
|
+
abstain: round.votes.filter((entry) => entry.vote === "abstain").length,
|
|
505
|
+
quorum: round.quorum,
|
|
506
|
+
state: round.state,
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/* ── Merged from taskqueue.ts ── */
|
|
511
|
+
|
|
512
|
+
import type { claimrecord } from "./types.js";
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* Shared task queue logic of the 1.1.58 multi agent family.
|
|
516
|
+
* Every correlated rule for lanes, priorities, claims, work stealing with lane ownership, completion, heartbeats and the requeue of orphaned tasks lives in this file.
|
|
517
|
+
* The queue never executes anything: a claimed task only names the work whose proposal still passes the same human review every single agent proposal passes.
|
|
518
|
+
*/
|
|
519
|
+
|
|
520
|
+
/** One lane ownership rule the user configures: the lane name and the roles allowed to steal from it; a lane without a rule stays open to every agent of the approved swarm. */
|
|
521
|
+
export interface laneownership {
|
|
522
|
+
lane: string;
|
|
523
|
+
roles: string[];
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/** Builds one empty queue with the user configured lanes, priority scale and completion policy; the empty set of lanes accepts any lane name the user enqueues. */
|
|
527
|
+
export function emptyqueue(
|
|
528
|
+
input: { lanes?: string[]; priorities?: number[]; completionpolicy?: "all" | "any" } = {},
|
|
529
|
+
): taskqueue {
|
|
530
|
+
return {
|
|
531
|
+
lanes: input.lanes ?? [],
|
|
532
|
+
priorities: input.priorities ?? [],
|
|
533
|
+
completionpolicy: input.completionpolicy ?? "all",
|
|
534
|
+
items: [],
|
|
535
|
+
claims: [],
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/** Enqueues one task item into a lane with its priority; a configured lane list refuses unknown lanes and the payload stays the plain language text the user typed. */
|
|
540
|
+
export function enqueue(input: {
|
|
541
|
+
queue: taskqueue;
|
|
542
|
+
id: string;
|
|
543
|
+
lane: string;
|
|
544
|
+
priority: number;
|
|
545
|
+
payload: string;
|
|
546
|
+
now: number;
|
|
547
|
+
}): taskqueue {
|
|
548
|
+
if (input.lane.trim() === "") throw new Error("The task needs its lane name.");
|
|
549
|
+
if (input.payload.trim() === "") throw new Error("The task needs its payload in plain language.");
|
|
550
|
+
if (input.queue.lanes.length > 0 && !input.queue.lanes.includes(input.lane))
|
|
551
|
+
throw new Error(`The lane ${input.lane} is not one of the configured lanes.`);
|
|
552
|
+
if (input.queue.items.some((item) => item.id === input.id))
|
|
553
|
+
throw new Error(`The task id ${input.id} already waits in the queue.`);
|
|
554
|
+
const item: taskitem = {
|
|
555
|
+
id: input.id,
|
|
556
|
+
lane: input.lane,
|
|
557
|
+
priority: input.priority,
|
|
558
|
+
payload: input.payload,
|
|
559
|
+
state: "queued",
|
|
560
|
+
enqueuedat: input.now,
|
|
561
|
+
};
|
|
562
|
+
return { ...input.queue, items: [...input.queue.items, item] };
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/** Sorts the queued items of one lane: the highest priority first and the oldest task wins a tie. */
|
|
566
|
+
function orderedcandidates(queue: taskqueue, lane?: string): taskitem[] {
|
|
567
|
+
return queue.items
|
|
568
|
+
.filter((item) => item.state === "queued" && (lane === undefined || item.lane === lane))
|
|
569
|
+
.sort((one, two) => two.priority - one.priority || one.enqueuedat - two.enqueuedat);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/** Lets one agent claim the highest priority queued task; the claim carries the heartbeat time that keeps it alive and an agent holds one task at a time. */
|
|
573
|
+
export function claim(input: { queue: taskqueue; agentid: string; now: number }): {
|
|
574
|
+
queue: taskqueue;
|
|
575
|
+
task?: taskitem;
|
|
576
|
+
} {
|
|
577
|
+
const held = input.queue.claims.find((record) => record.agentid === input.agentid);
|
|
578
|
+
if (held) {
|
|
579
|
+
const task = input.queue.items.find((item) => item.id === held.taskid);
|
|
580
|
+
if (task && task.state === "claimed") return { queue: input.queue, task };
|
|
581
|
+
}
|
|
582
|
+
const candidate = orderedcandidates(input.queue)[0];
|
|
583
|
+
if (!candidate) return { queue: input.queue };
|
|
584
|
+
const record: claimrecord = {
|
|
585
|
+
agentid: input.agentid,
|
|
586
|
+
taskid: candidate.id,
|
|
587
|
+
claimedat: input.now,
|
|
588
|
+
heartbeatat: input.now,
|
|
589
|
+
};
|
|
590
|
+
return {
|
|
591
|
+
queue: {
|
|
592
|
+
...input.queue,
|
|
593
|
+
items: input.queue.items.map((item) =>
|
|
594
|
+
item.id === candidate.id ? { ...item, state: "claimed" as const } : item,
|
|
595
|
+
),
|
|
596
|
+
claims: [
|
|
597
|
+
...input.queue.claims.filter(
|
|
598
|
+
(entry) =>
|
|
599
|
+
entry.agentid !== input.agentid ||
|
|
600
|
+
input.queue.items.find((item) => item.id === entry.taskid)?.state !== "claimed",
|
|
601
|
+
),
|
|
602
|
+
record,
|
|
603
|
+
],
|
|
604
|
+
},
|
|
605
|
+
task: { ...candidate, state: "claimed" },
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/** Lets one idle agent steal a queued task from a named lane; the lane ownership rules the user configures refuse agents whose role holds no grant for the lane. */
|
|
610
|
+
export function steal(input: {
|
|
611
|
+
queue: taskqueue;
|
|
612
|
+
agentid: string;
|
|
613
|
+
role: string;
|
|
614
|
+
fromlane: string;
|
|
615
|
+
ownership?: laneownership[];
|
|
616
|
+
now: number;
|
|
617
|
+
}): { queue: taskqueue; task?: taskitem } {
|
|
618
|
+
const rule = (input.ownership ?? []).find((entry) => entry.lane === input.fromlane);
|
|
619
|
+
if (rule && !rule.roles.includes(input.role))
|
|
620
|
+
throw new Error(
|
|
621
|
+
`The lane ${input.fromlane} only lets the roles ${rule.roles.join(", ")} steal its tasks; the ${input.role} agent stays out.`,
|
|
622
|
+
);
|
|
623
|
+
const candidate = orderedcandidates(input.queue, input.fromlane)[0];
|
|
624
|
+
if (!candidate) return { queue: input.queue };
|
|
625
|
+
const record: claimrecord = {
|
|
626
|
+
agentid: input.agentid,
|
|
627
|
+
taskid: candidate.id,
|
|
628
|
+
claimedat: input.now,
|
|
629
|
+
heartbeatat: input.now,
|
|
630
|
+
};
|
|
631
|
+
return {
|
|
632
|
+
queue: {
|
|
633
|
+
...input.queue,
|
|
634
|
+
items: input.queue.items.map((item) =>
|
|
635
|
+
item.id === candidate.id ? { ...item, state: "claimed" as const } : item,
|
|
636
|
+
),
|
|
637
|
+
claims: [...input.queue.claims.filter((entry) => entry.agentid !== input.agentid), record],
|
|
638
|
+
},
|
|
639
|
+
task: { ...candidate, state: "claimed" },
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
/** Marks one task done and releases the claim of the agent that held it. */
|
|
644
|
+
export function complete(input: { queue: taskqueue; taskid: string; now: number }): taskqueue {
|
|
645
|
+
const task = input.queue.items.find((item) => item.id === input.taskid);
|
|
646
|
+
if (!task) throw new Error(`The task ${input.taskid} is not in the queue.`);
|
|
647
|
+
if (task.state !== "claimed")
|
|
648
|
+
throw new Error(`The task ${input.taskid} is ${task.state}; only a claimed task completes.`);
|
|
649
|
+
return {
|
|
650
|
+
...input.queue,
|
|
651
|
+
items: input.queue.items.map((item) => (item.id === input.taskid ? { ...item, state: "done" as const } : item)),
|
|
652
|
+
claims: input.queue.claims.filter((record) => record.taskid !== input.taskid),
|
|
653
|
+
};
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/** Cancels one queued or claimed task; the cancelled task releases its claim and leaves the queue. */
|
|
657
|
+
export function canceltask(input: { queue: taskqueue; taskid: string; now: number }): taskqueue {
|
|
658
|
+
const task = input.queue.items.find((item) => item.id === input.taskid);
|
|
659
|
+
if (!task) throw new Error(`The task ${input.taskid} is not in the queue.`);
|
|
660
|
+
if (task.state === "done") throw new Error(`The task ${input.taskid} is done; a completed task never cancels.`);
|
|
661
|
+
return {
|
|
662
|
+
...input.queue,
|
|
663
|
+
items: input.queue.items.map((item) =>
|
|
664
|
+
item.id === input.taskid ? { ...item, state: "cancelled" as const } : item,
|
|
665
|
+
),
|
|
666
|
+
claims: input.queue.claims.filter((record) => record.taskid !== input.taskid),
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/** Refreshes the heartbeats of every live claim of one agent so a working agent keeps its tasks. */
|
|
671
|
+
export function claimheartbeat(input: { queue: taskqueue; agentid: string; now: number }): taskqueue {
|
|
672
|
+
return {
|
|
673
|
+
...input.queue,
|
|
674
|
+
claims: input.queue.claims.map((record) =>
|
|
675
|
+
record.agentid === input.agentid ? { ...record, heartbeatat: input.now } : record,
|
|
676
|
+
),
|
|
677
|
+
};
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
/** Returns the orphaned tasks to their lane: a claim whose heartbeat stayed silent past the user configured expiry window releases and the task waits as queued again; an absent window never expires a claim. */
|
|
681
|
+
export function requeue(input: { queue: taskqueue; now: number; window?: number }): {
|
|
682
|
+
queue: taskqueue;
|
|
683
|
+
requeued: string[];
|
|
684
|
+
} {
|
|
685
|
+
if (input.window === undefined) return { queue: input.queue, requeued: [] };
|
|
686
|
+
const window = input.window;
|
|
687
|
+
const expired = input.queue.claims.filter((record) => input.now - record.heartbeatat > window);
|
|
688
|
+
if (expired.length === 0) return { queue: input.queue, requeued: [] };
|
|
689
|
+
const expiredids = new Set(expired.map((record) => record.taskid));
|
|
690
|
+
return {
|
|
691
|
+
queue: {
|
|
692
|
+
...input.queue,
|
|
693
|
+
items: input.queue.items.map((item) =>
|
|
694
|
+
expiredids.has(item.id) && item.state === "claimed" ? { ...item, state: "queued" as const } : item,
|
|
695
|
+
),
|
|
696
|
+
claims: input.queue.claims.filter((record) => !expiredids.has(record.taskid)),
|
|
697
|
+
},
|
|
698
|
+
requeued: [...expiredids],
|
|
699
|
+
};
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
/** Reports every lane with its queued, claimed, done and cancelled task counts and the live claims, so the queue view reads the lanes at a glance. */
|
|
703
|
+
export function lanereport(
|
|
704
|
+
queue: taskqueue,
|
|
705
|
+
): Array<{
|
|
706
|
+
lane: string;
|
|
707
|
+
queued: number;
|
|
708
|
+
claimed: number;
|
|
709
|
+
done: number;
|
|
710
|
+
cancelled: number;
|
|
711
|
+
claims: Array<{ agentid: string; taskid: string; heartbeatat: number }>;
|
|
712
|
+
}> {
|
|
713
|
+
const lanes = [...queue.lanes, ...queue.items.map((item) => item.lane)].filter(
|
|
714
|
+
(lane, index, all) => all.indexOf(lane) === index,
|
|
715
|
+
);
|
|
716
|
+
return lanes.map((lane) => ({
|
|
717
|
+
lane,
|
|
718
|
+
queued: queue.items.filter((item) => item.lane === lane && item.state === "queued").length,
|
|
719
|
+
claimed: queue.items.filter((item) => item.lane === lane && item.state === "claimed").length,
|
|
720
|
+
done: queue.items.filter((item) => item.lane === lane && item.state === "done").length,
|
|
721
|
+
cancelled: queue.items.filter((item) => item.lane === lane && item.state === "cancelled").length,
|
|
722
|
+
claims: queue.claims
|
|
723
|
+
.filter((record) =>
|
|
724
|
+
queue.items.some((item) => item.id === record.taskid && item.lane === lane && item.state === "claimed"),
|
|
725
|
+
)
|
|
726
|
+
.map((record) => ({ agentid: record.agentid, taskid: record.taskid, heartbeatat: record.heartbeatat })),
|
|
727
|
+
}));
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
/** Evaluates the completion policy of the queue: all completes only when every task is done while any completes with the first done task. */
|
|
731
|
+
export function queuecomplete(queue: taskqueue): boolean {
|
|
732
|
+
const open = queue.items.filter((item) => item.state !== "cancelled");
|
|
733
|
+
if (open.length === 0) return false;
|
|
734
|
+
return queue.completionpolicy === "any"
|
|
735
|
+
? open.some((item) => item.state === "done")
|
|
736
|
+
: open.every((item) => item.state === "done");
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
/** Counts the tasks by claim state for the popup gauge: the claimed tasks are the active work of the swarm. */
|
|
740
|
+
export function taskcounts(queue: taskqueue): { queued: number; claimed: number; done: number; cancelled: number } {
|
|
741
|
+
return {
|
|
742
|
+
queued: queue.items.filter((item) => item.state === "queued").length,
|
|
743
|
+
claimed: queue.items.filter((item) => item.state === "claimed").length,
|
|
744
|
+
done: queue.items.filter((item) => item.state === "done").length,
|
|
745
|
+
cancelled: queue.items.filter((item) => item.state === "cancelled").length,
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/* ── Merged from blackboard.ts ── */
|
|
750
|
+
|
|
751
|
+
import type { actionrisk, blackboard, blackboardentry, blackboardsection } from "./types.js";
|
|
752
|
+
|
|
753
|
+
/**
|
|
754
|
+
* Blackboard shared memory logic of the 1.1.58 multi agent family.
|
|
755
|
+
* Every correlated rule for the shared sections, posting with authors, reading with freshness filters, retirement by the user configured window and the consent class inheritance of the source extraction lives in this file.
|
|
756
|
+
* Every agent of the swarm reads the same board: the writes of one agent are visible to every other agent with no relay.
|
|
757
|
+
*/
|
|
758
|
+
|
|
759
|
+
/** The blackboard sections the swarm shares by default; the user may narrow the list to the sections in use. */
|
|
760
|
+
export const blackboardsections: blackboardsection[] = ["goals", "facts", "findings", "scratch"];
|
|
761
|
+
|
|
762
|
+
/** Builds one empty blackboard over the four shared sections with no retirement window; an absent window keeps every entry. */
|
|
763
|
+
export function emptyboard(sections?: blackboardsection[]): blackboard {
|
|
764
|
+
return { sections: sections ?? blackboardsections, entries: [] };
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
/** Writes one entry to a section with its author: the key and value stay the plain text the author typed, the section joins the board when it is new and the entry carries the consent class of its source extraction. */
|
|
768
|
+
export function postentry(input: {
|
|
769
|
+
board: blackboard;
|
|
770
|
+
id: string;
|
|
771
|
+
key: string;
|
|
772
|
+
value: string;
|
|
773
|
+
section: blackboardsection;
|
|
774
|
+
author: string;
|
|
775
|
+
valuekind?: "text" | "json";
|
|
776
|
+
consentclass?: actionrisk;
|
|
777
|
+
now: number;
|
|
778
|
+
}): blackboard {
|
|
779
|
+
if (input.key.trim() === "") throw new Error("The blackboard entry needs its key.");
|
|
780
|
+
if (input.value.trim() === "") throw new Error("The blackboard entry needs its value.");
|
|
781
|
+
if (input.author.trim() === "") throw new Error("The blackboard entry needs its author.");
|
|
782
|
+
if (!blackboardsections.includes(input.section))
|
|
783
|
+
throw new Error(`The section ${input.section} is not one of the shared blackboard sections.`);
|
|
784
|
+
if (input.board.entries.some((entry) => entry.id === input.id))
|
|
785
|
+
throw new Error(`The blackboard entry id ${input.id} already exists.`);
|
|
786
|
+
if (input.valuekind === "json") {
|
|
787
|
+
try {
|
|
788
|
+
JSON.parse(input.value);
|
|
789
|
+
} catch {
|
|
790
|
+
throw new Error("The json blackboard entry needs a well-formed json value.");
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
const entry: blackboardentry = {
|
|
794
|
+
id: input.id,
|
|
795
|
+
key: input.key.trim(),
|
|
796
|
+
valuekind: input.valuekind ?? "text",
|
|
797
|
+
value: input.value,
|
|
798
|
+
author: input.author,
|
|
799
|
+
section: input.section,
|
|
800
|
+
consentclass: input.consentclass ?? "read",
|
|
801
|
+
postedat: input.now,
|
|
802
|
+
};
|
|
803
|
+
return {
|
|
804
|
+
...input.board,
|
|
805
|
+
sections: input.board.sections.includes(input.section)
|
|
806
|
+
? input.board.sections
|
|
807
|
+
: [...input.board.sections, input.section],
|
|
808
|
+
entries: [entry, ...input.board.entries],
|
|
809
|
+
};
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
/** True when one entry stayed fresh inside the window; an absent window keeps every entry fresh. */
|
|
813
|
+
export function entryfresh(entry: blackboardentry, now: number, window?: number): boolean {
|
|
814
|
+
if (entry.retiredat !== undefined) return false;
|
|
815
|
+
if (window === undefined) return true;
|
|
816
|
+
return now - entry.postedat <= window;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
/** Returns the entries of one section with freshness filters: retired entries stay out and an optional window keeps only the fresh ones; an absent section returns every live entry. */
|
|
820
|
+
export function readentries(input: {
|
|
821
|
+
board: blackboard;
|
|
822
|
+
section?: blackboardsection;
|
|
823
|
+
freshness?: number;
|
|
824
|
+
now: number;
|
|
825
|
+
}): blackboardentry[] {
|
|
826
|
+
return input.board.entries
|
|
827
|
+
.filter((entry) => entry.retiredat === undefined)
|
|
828
|
+
.filter((entry) => input.section === undefined || entry.section === input.section)
|
|
829
|
+
.filter((entry) => entryfresh(entry, input.now, input.freshness))
|
|
830
|
+
.sort((one, two) => two.postedat - one.postedat);
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
/** Retires the stale entries by the user configured policy: entries older than the retirement window of the board receive their retirement time while an absent window retires nothing. */
|
|
834
|
+
export function retireentries(input: { board: blackboard; now: number }): { board: blackboard; retired: string[] } {
|
|
835
|
+
if (input.board.retirementwindow === undefined) return { board: input.board, retired: [] };
|
|
836
|
+
const stale = input.board.entries.filter(
|
|
837
|
+
(entry) => entry.retiredat === undefined && input.now - entry.postedat > input.board.retirementwindow!,
|
|
838
|
+
);
|
|
839
|
+
if (stale.length === 0) return { board: input.board, retired: [] };
|
|
840
|
+
const staleids = new Set(stale.map((entry) => entry.id));
|
|
841
|
+
return {
|
|
842
|
+
board: {
|
|
843
|
+
...input.board,
|
|
844
|
+
entries: input.board.entries.map((entry) =>
|
|
845
|
+
staleids.has(entry.id) ? { ...entry, retiredat: input.now } : entry,
|
|
846
|
+
),
|
|
847
|
+
},
|
|
848
|
+
retired: [...staleids],
|
|
849
|
+
};
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
/** Retires one entry by its id; the retired entry stays stored for the audit trail while the live reads stop showing it. */
|
|
853
|
+
export function retireentry(input: { board: blackboard; entryid: string; now: number }): blackboard {
|
|
854
|
+
const entry = input.board.entries.find((candidate) => candidate.id === input.entryid);
|
|
855
|
+
if (!entry) throw new Error(`The blackboard entry ${input.entryid} does not exist.`);
|
|
856
|
+
if (entry.retiredat !== undefined) throw new Error(`The blackboard entry ${entry.key} is already retired.`);
|
|
857
|
+
return {
|
|
858
|
+
...input.board,
|
|
859
|
+
entries: input.board.entries.map((candidate) =>
|
|
860
|
+
candidate.id === input.entryid ? { ...candidate, retiredat: input.now } : candidate,
|
|
861
|
+
),
|
|
862
|
+
};
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
/** Inherits the consent class of the source extraction into one entry: the reader of the entry sees the class of the extraction it came from, so a sensitive extraction stays sensitive on the board. */
|
|
866
|
+
export function inheritconsent(entry: blackboardentry, sourcerisk: actionrisk): blackboardentry {
|
|
867
|
+
return { ...entry, consentclass: sourcerisk };
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
/** Summarizes the board for the panel: every section with its live entry count, its authors and the freshest posting time. */
|
|
871
|
+
export function boardsummary(
|
|
872
|
+
board: blackboard,
|
|
873
|
+
now: number,
|
|
874
|
+
): Array<{ section: blackboardsection; entries: number; authors: string[]; freshestat?: number }> {
|
|
875
|
+
return board.sections.map((section) => {
|
|
876
|
+
const live = board.entries.filter((entry) => entry.section === section && entry.retiredat === undefined);
|
|
877
|
+
return {
|
|
878
|
+
section,
|
|
879
|
+
entries: live.length,
|
|
880
|
+
authors: [...new Set(live.map((entry) => entry.author))],
|
|
881
|
+
...(live.length > 0 ? { freshestat: Math.max(...live.map((entry) => entry.postedat)) } : {}),
|
|
882
|
+
};
|
|
883
|
+
});
|
|
884
|
+
}
|