blun-king-cli 9.1.567 → 9.1.569
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/agent-spine-plugin/.claude-plugin/marketplace.json +1 -1
- package/agent-spine-plugin/.claude-plugin/plugin.json +1 -1
- package/agent-spine-plugin/.codex-plugin/plugin.json +2 -1
- package/agent-spine-plugin/CHANGELOG.md +1581 -0
- package/agent-spine-plugin/README.md +30 -4
- package/agent-spine-plugin/blun.plugin.json +3 -3
- package/agent-spine-plugin/docs/acceptance.md +61 -0
- package/agent-spine-plugin/docs/assignment-continuation.md +48 -0
- package/agent-spine-plugin/docs/host-integration.md +178 -0
- package/agent-spine-plugin/docs/preflight-recall.md +69 -0
- package/agent-spine-plugin/docs/preservation-contract.md +53 -0
- package/agent-spine-plugin/docs/quality-gates.md +50 -0
- package/agent-spine-plugin/docs/releasing.md +85 -0
- package/agent-spine-plugin/docs/session-timeline.md +251 -0
- package/agent-spine-plugin/docs/source-roots.md +113 -0
- package/agent-spine-plugin/docs/structured-completion.md +67 -0
- package/agent-spine-plugin/docs/world-model.md +94 -0
- package/agent-spine-plugin/hooks/codex.json +2 -2
- package/agent-spine-plugin/hooks/hooks.json +1 -1
- package/agent-spine-plugin/hooks/version.json +1 -1
- package/agent-spine-plugin/package.json +13 -3
- package/agent-spine-plugin/scripts/check-codex-install.js +226 -0
- package/agent-spine-plugin/scripts/check-hosts.js +14 -5
- package/agent-spine-plugin/scripts/check-install-hook.js +226 -0
- package/agent-spine-plugin/scripts/check-install-selfstarter.js +154 -0
- package/agent-spine-plugin/scripts/check-install.js +478 -0
- package/agent-spine-plugin/scripts/check-line-budget.js +58 -0
- package/agent-spine-plugin/scripts/check-syntax.js +29 -0
- package/agent-spine-plugin/scripts/github-actions.js +11 -0
- package/agent-spine-plugin/scripts/hermetic-process.js +183 -0
- package/agent-spine-plugin/scripts/release-check.js +145 -0
- package/agent-spine-plugin/scripts/run-acceptance.js +19 -0
- package/agent-spine-plugin/scripts/run-checks.js +47 -0
- package/agent-spine-plugin/scripts/run-tests-hermetic.js +89 -0
- package/agent-spine-plugin/skills/agent-spine/SKILL.md +16 -2
- package/agent-spine-plugin/spine-example/1-identity.md +12 -0
- package/agent-spine-plugin/spine-example/2-voice.md +6 -0
- package/agent-spine-plugin/spine-example/3-conduct.md +8 -0
- package/agent-spine-plugin/spine-example/4-history.md +4 -0
- package/agent-spine-plugin/src/cli-agent.js +296 -0
- package/agent-spine-plugin/src/cli-attention.js +95 -0
- package/agent-spine-plugin/src/cli-autonomy.js +36 -0
- package/agent-spine-plugin/src/cli-common.js +71 -0
- package/agent-spine-plugin/src/cli-continuity.js +116 -0
- package/agent-spine-plugin/src/cli-core.js +128 -0
- package/agent-spine-plugin/src/cli-diagnostics.js +291 -0
- package/agent-spine-plugin/src/cli-host.js +21 -0
- package/agent-spine-plugin/src/cli-learning.js +305 -0
- package/agent-spine-plugin/src/cli-premortem.js +16 -0
- package/agent-spine-plugin/src/cli-sharing.js +230 -0
- package/agent-spine-plugin/src/cli.js +40 -1350
- package/agent-spine-plugin/src/codex-reader-launcher.js +182 -0
- package/agent-spine-plugin/src/hook.js +185 -567
- package/agent-spine-plugin/src/index.js +16 -2
- package/agent-spine-plugin/src/lib/acceptance.js +113 -11
- package/agent-spine-plugin/src/lib/action-lesson-recall.js +53 -0
- package/agent-spine-plugin/src/lib/attention-context.js +167 -0
- package/agent-spine-plugin/src/lib/attention-events.js +113 -0
- package/agent-spine-plugin/src/lib/attention-privacy.js +72 -0
- package/agent-spine-plugin/src/lib/attention-schema.js +164 -0
- package/agent-spine-plugin/src/lib/attention-storage.js +93 -0
- package/agent-spine-plugin/src/lib/attention.js +9 -600
- package/agent-spine-plugin/src/lib/audit-premortem.js +223 -0
- package/agent-spine-plugin/src/lib/audit.js +56 -6
- package/agent-spine-plugin/src/lib/autonomy-policy.js +110 -0
- package/agent-spine-plugin/src/lib/autonomy-store.js +202 -0
- package/agent-spine-plugin/src/lib/autonomy.js +8 -0
- package/agent-spine-plugin/src/lib/briefing.js +67 -4
- package/agent-spine-plugin/src/lib/catalog-document-read.js +51 -0
- package/agent-spine-plugin/src/lib/catalog.js +1 -1
- package/agent-spine-plugin/src/lib/codex-installation.js +231 -0
- package/agent-spine-plugin/src/lib/codex-skill-installation.js +211 -0
- package/agent-spine-plugin/src/lib/context.js +3 -1
- package/agent-spine-plugin/src/lib/delivery-agent-usage.js +224 -0
- package/agent-spine-plugin/src/lib/delivery-assignment.js +220 -0
- package/agent-spine-plugin/src/lib/delivery-command-actions.js +453 -0
- package/agent-spine-plugin/src/lib/delivery-knowledge.js +78 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-binding.js +107 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-closure.js +171 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-codec.js +45 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-correction.js +101 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-file.js +21 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-index.js +493 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-inspection.js +37 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-recovery.js +120 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-rejection.js +65 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-results.js +28 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-session-guard.js +46 -0
- package/agent-spine-plugin/src/lib/delivery-premortem-write-ledger.js +285 -0
- package/agent-spine-plugin/src/lib/delivery-premortem.js +499 -0
- package/agent-spine-plugin/src/lib/delivery-shell-heredoc.js +115 -0
- package/agent-spine-plugin/src/lib/delivery-shell-substitutions.js +111 -0
- package/agent-spine-plugin/src/lib/delivery-shell-wrapper.js +64 -0
- package/agent-spine-plugin/src/lib/delivery-target.js +73 -0
- package/agent-spine-plugin/src/lib/delivery-verification.js +445 -0
- package/agent-spine-plugin/src/lib/documents.js +27 -5
- package/agent-spine-plugin/src/lib/filesystem-retry.js +2 -0
- package/agent-spine-plugin/src/lib/gateway-common.js +68 -0
- package/agent-spine-plugin/src/lib/gateway-control.js +302 -0
- package/agent-spine-plugin/src/lib/gateway-delivery.js +103 -0
- package/agent-spine-plugin/src/lib/gateway-execution.js +350 -0
- package/agent-spine-plugin/src/lib/gateway-host-lifecycle.js +185 -0
- package/agent-spine-plugin/src/lib/gateway-inspection.js +85 -0
- package/agent-spine-plugin/src/lib/gateway-knowledge.js +129 -0
- package/agent-spine-plugin/src/lib/gateway-policy-provenance.js +197 -0
- package/agent-spine-plugin/src/lib/gateway-premortem-disposition.js +82 -0
- package/agent-spine-plugin/src/lib/gateway-premortem.js +363 -0
- package/agent-spine-plugin/src/lib/gateway-runs.js +300 -0
- package/agent-spine-plugin/src/lib/gateway-runtime-identity.js +31 -0
- package/agent-spine-plugin/src/lib/gateway-runtime-records.js +21 -0
- package/agent-spine-plugin/src/lib/gateway-runtime.js +18 -1623
- package/agent-spine-plugin/src/lib/gateway-state-transaction.js +356 -0
- package/agent-spine-plugin/src/lib/gateway-state.js +342 -0
- package/agent-spine-plugin/src/lib/hook-artifact-guards.js +356 -0
- package/agent-spine-plugin/src/lib/hook-audit.js +16 -2
- package/agent-spine-plugin/src/lib/hook-briefing-use.js +68 -0
- package/agent-spine-plugin/src/lib/hook-context.js +424 -0
- package/agent-spine-plugin/src/lib/hook-final-message.js +26 -0
- package/agent-spine-plugin/src/lib/hook-input.js +27 -0
- package/agent-spine-plugin/src/lib/hook-output.js +140 -0
- package/agent-spine-plugin/src/lib/hook-premortem.js +257 -0
- package/agent-spine-plugin/src/lib/hook-process-advisory.js +37 -0
- package/agent-spine-plugin/src/lib/hook-protection.js +95 -0
- package/agent-spine-plugin/src/lib/hook-stop-verification.js +84 -0
- package/agent-spine-plugin/src/lib/hook-timeline.js +91 -0
- package/agent-spine-plugin/src/lib/identifier-analysis.js +446 -0
- package/agent-spine-plugin/src/lib/indexed-memory.js +23 -6
- package/agent-spine-plugin/src/lib/knowledge-evidence.js +431 -0
- package/agent-spine-plugin/src/lib/learning-applications.js +441 -0
- package/agent-spine-plugin/src/lib/learning-candidates.js +274 -0
- package/agent-spine-plugin/src/lib/learning-context.js +130 -0
- package/agent-spine-plugin/src/lib/learning-delivery-contracts.js +310 -0
- package/agent-spine-plugin/src/lib/learning-evaluation-contracts.js +350 -0
- package/agent-spine-plugin/src/lib/learning-evaluation-registration.js +372 -0
- package/agent-spine-plugin/src/lib/learning-evaluation-revocation.js +292 -0
- package/agent-spine-plugin/src/lib/learning-evidence-contracts.js +321 -0
- package/agent-spine-plugin/src/lib/learning-findings.js +458 -0
- package/agent-spine-plugin/src/lib/learning-measurement-contracts.js +285 -0
- package/agent-spine-plugin/src/lib/learning-measurements.js +265 -0
- package/agent-spine-plugin/src/lib/learning-outcome-contracts.js +165 -0
- package/agent-spine-plugin/src/lib/learning-outcomes.js +343 -0
- package/agent-spine-plugin/src/lib/learning-reconciliation.js +290 -0
- package/agent-spine-plugin/src/lib/learning-retry-contracts.js +188 -0
- package/agent-spine-plugin/src/lib/learning-schema.js +229 -0
- package/agent-spine-plugin/src/lib/learning-scope-targets.js +424 -0
- package/agent-spine-plugin/src/lib/learning-state-upgrade.js +477 -0
- package/agent-spine-plugin/src/lib/learning-status-configuration.js +476 -0
- package/agent-spine-plugin/src/lib/learning-storage.js +203 -0
- package/agent-spine-plugin/src/lib/learning-trial-recovery.js +218 -0
- package/agent-spine-plugin/src/lib/learning-validation-contracts.js +375 -0
- package/agent-spine-plugin/src/lib/learning-validation-renewal.js +298 -0
- package/agent-spine-plugin/src/lib/learning-validation-runtime.js +234 -0
- package/agent-spine-plugin/src/lib/learning.js +36 -6923
- package/agent-spine-plugin/src/lib/lesson-recall-session.js +172 -0
- package/agent-spine-plugin/src/lib/mcp-autonomy-tools.js +37 -0
- package/agent-spine-plugin/src/lib/mcp-delivery-completion.js +124 -0
- package/agent-spine-plugin/src/lib/mcp-delivery-tools.js +45 -0
- package/agent-spine-plugin/src/lib/mcp-premortem.js +68 -0
- package/agent-spine-plugin/src/lib/mcp-runtime.js +269 -0
- package/agent-spine-plugin/src/lib/mcp-source-context.js +51 -0
- package/agent-spine-plugin/src/lib/mcp-timeline-tools.js +164 -0
- package/agent-spine-plugin/src/lib/mcp-world-tools.js +52 -0
- package/agent-spine-plugin/src/lib/owned-file-lock.js +30 -10
- package/agent-spine-plugin/src/lib/project-portfolio.js +176 -0
- package/agent-spine-plugin/src/lib/selfstarter-core.js +382 -0
- package/agent-spine-plugin/src/lib/selfstarter-jobs.js +149 -0
- package/agent-spine-plugin/src/lib/selfstarter-lease.js +204 -0
- package/agent-spine-plugin/src/lib/selfstarter-policy.js +90 -0
- package/agent-spine-plugin/src/lib/selfstarter-workspace.js +111 -0
- package/agent-spine-plugin/src/lib/selfstarter.js +11 -911
- package/agent-spine-plugin/src/lib/session-timeline-auth.js +316 -0
- package/agent-spine-plugin/src/lib/session-timeline-codex.js +58 -0
- package/agent-spine-plugin/src/lib/session-timeline-contract.js +48 -0
- package/agent-spine-plugin/src/lib/session-timeline-enrollment-source.js +41 -0
- package/agent-spine-plugin/src/lib/session-timeline-enrollment-storage.js +132 -0
- package/agent-spine-plugin/src/lib/session-timeline-enrollment-transport.js +17 -0
- package/agent-spine-plugin/src/lib/session-timeline-enrollment.js +500 -0
- package/agent-spine-plugin/src/lib/session-timeline-event-extract.js +157 -0
- package/agent-spine-plugin/src/lib/session-timeline-host-origin.js +74 -0
- package/agent-spine-plugin/src/lib/session-timeline-host-receipt.js +117 -0
- package/agent-spine-plugin/src/lib/session-timeline-invocation.js +201 -0
- package/agent-spine-plugin/src/lib/session-timeline-king.js +79 -0
- package/agent-spine-plugin/src/lib/session-timeline-prior.js +59 -0
- package/agent-spine-plugin/src/lib/session-timeline-provider.js +34 -0
- package/agent-spine-plugin/src/lib/session-timeline-query.js +55 -0
- package/agent-spine-plugin/src/lib/session-timeline-results.js +31 -0
- package/agent-spine-plugin/src/lib/session-timeline-root.js +11 -0
- package/agent-spine-plugin/src/lib/session-timeline-search.js +82 -0
- package/agent-spine-plugin/src/lib/session-timeline-sid-acl.js +217 -0
- package/agent-spine-plugin/src/lib/session-timeline-source.js +83 -0
- package/agent-spine-plugin/src/lib/session-timeline-state.js +45 -0
- package/agent-spine-plugin/src/lib/session-timeline-transport.js +50 -0
- package/agent-spine-plugin/src/lib/session-timeline-windows-acl.js +148 -0
- package/agent-spine-plugin/src/lib/session-timeline.js +453 -0
- package/agent-spine-plugin/src/lib/source-roots.js +69 -136
- package/agent-spine-plugin/src/lib/source-tree-scan.js +178 -0
- package/agent-spine-plugin/src/lib/task-knowledge-context.js +78 -0
- package/agent-spine-plugin/src/lib/timeline-tool-guard.js +202 -0
- package/agent-spine-plugin/src/lib/world-knowledge.js +249 -0
- package/agent-spine-plugin/src/lib/world-model.js +278 -0
- package/agent-spine-plugin/src/mcp.js +20 -161
- package/agent-spine-plugin/src/version.js +1 -1
- package/agent-spine-plugin/src/worker.js +22 -5
- package/bin/agent-resume-snapshot.cjs +2 -2
- package/bin/agentspine-king-goal-inbox.mjs +111 -0
- package/bin/agentspine-king-goal-intake.mjs +106 -0
- package/bin/baseline-skill-performance-policy.cjs +1 -16
- package/bin/core-bootstrap.js +2 -0
- package/bin/curiosity-scout-policy.cjs +5 -1
- package/bin/input-draft-persistence.cjs +2 -2
- package/bin/king-tui-function-contract.json +33 -0
- package/bin/launcher-restart-policy.cjs +150 -0
- package/bin/launcher-runtime.js +56 -44
- package/bin/managed-context-startup-policy.cjs +27 -0
- package/bin/managed-plugin-selection.cjs +116 -0
- package/bin/mistake-relevance-policy.cjs +1 -1
- package/bin/observer-hooks.cjs +14 -0
- package/bin/oversized-context-offload-policy.cjs +86 -0
- package/bin/plugin-bootstrap.js +7 -40
- package/bin/proactive-compaction-policy.cjs +1 -1
- package/bin/provider-model-refresh-deadline.cjs +53 -0
- package/bin/provider-model-refresh-policy.cjs +107 -0
- package/bin/release-artifact-freeze-policy.cjs +30 -0
- package/bin/repeated-user-message-projection.cjs +3 -126
- package/bin/research-page-result.cjs +74 -0
- package/bin/runtime-exit-ledger.cjs +1 -0
- package/bin/session-compaction-policy.cjs +84 -0
- package/bin/skill-listing-performance-policy.cjs +2 -2
- package/bin/standard-tools-bootstrap.js +0 -37
- package/bin/subagent-skill-policy.cjs +3 -1
- package/bin/telegram-approval-relay.cjs +12 -7
- package/bin/telegram-private-conversation-policy.cjs +3 -2
- package/bin/telegram-queue-handoff-policy.cjs +24 -0
- package/bin/thinking-activity-status-policy.cjs +1 -1
- package/bin/thinking-only-guard.cjs +16 -12
- package/bin/tool-call-loop-policy.cjs +0 -2
- package/bin/tool-result-offload-policy.cjs +11 -2
- package/bin/tui-functional-contract.cjs +55 -0
- package/bin/update-notice.js +18 -14
- package/bin/user-message-offload-policy.cjs +1 -1
- package/bin/user-prompt-hook-origin-policy.cjs +34 -0
- package/bin/windows-node-crash-dump.cjs +110 -0
- package/blun.mjs +2517 -1048
- package/codebase-index/codebase_index.py +4 -3
- package/package.json +3 -17
- package/standard-skills/research-evidence/SKILL.md +39 -0
- package/standard-skills/research-evidence/references/evidence-format.md +82 -0
- package/standard-skills/research-evidence/scripts/evidence-collection.cjs +254 -0
- package/standard-skills/research-evidence/scripts/score-report.cjs +112 -0
- package/standard-skills/web-lesen/SKILL.md +37 -22
- package/standard-skills/web-lesen/scripts/crawl_public.py +376 -0
- package/telegram-plugin/DELIVERY.md +36 -0
- package/telegram-plugin/bin/telegram-approval-relay.cjs +13 -7
- package/telegram-plugin/bin/telegram-launcher-status-queue.cjs +122 -0
- package/telegram-plugin/bin/telegram-private-conversation-policy.cjs +3 -2
- package/telegram-plugin/bin/telegram-reply-parts.cjs +149 -0
- package/telegram-plugin/dist/bridge.mjs +7 -56
- package/telegram-plugin/dist/mcp-server.mjs +33 -4
- package/agent-spine-plugin/skill/SKILL.md +0 -76
- package/bin/mnemo-connect-heartbeat.cjs +0 -204
- package/bin/mnemo-tool-agent-policy.cjs +0 -22
- package/telegram-plugin/bin/telegram-mnemo-capture.cjs +0 -297
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import { loadChannelPolicy, loadChannelRuntime } from "./channel-runtime.js";
|
|
2
|
+
import {
|
|
3
|
+
assertGatewayCompletionMode, exactGatewayLeaseGeneration, failGatewayLane,
|
|
4
|
+
markHostStarted, newGatewayLease, normalizeGatewayExecutionMode,
|
|
5
|
+
requireExactGatewayLease, requireExactHostLease
|
|
6
|
+
} from "./gateway-host-lifecycle.js";
|
|
7
|
+
import { handlePlanKnowledge } from "./gateway-knowledge.js";
|
|
8
|
+
import { assertActivePersona, currentLane, exactReplyBinding } from "./gateway-runtime-identity.js";
|
|
9
|
+
import { appendReceipt, preserve } from "./gateway-runtime-records.js";
|
|
10
|
+
import * as goalPremortem from "./gateway-premortem.js";
|
|
11
|
+
import { writeGatewayStatePair } from "./gateway-state-transaction.js";
|
|
12
|
+
import { loadPersonaRuntime } from "./persona-runtime.js";
|
|
13
|
+
import { evaluateVoiceOutput } from "./voice-runtime.js";
|
|
14
|
+
import {
|
|
15
|
+
PRIORITY, emptyPolicy, emptyRuntime, exactId, safeCheckpoint, safeText,
|
|
16
|
+
sha256, timestamp
|
|
17
|
+
} from "./gateway-common.js";
|
|
18
|
+
import { reviewExecutionResult } from "./gateway-execution.js";
|
|
19
|
+
import {
|
|
20
|
+
activateNextPlanStep, conflictingResources, currentPlanStep, effectiveQueuePriority,
|
|
21
|
+
newGoalQueue, normalizePolicy, normalizeRuntime, pathsFor, planQueueKey,
|
|
22
|
+
planStepAgentId, readJson, withLock, writeJson
|
|
23
|
+
} from "./gateway-state.js";
|
|
24
|
+
|
|
25
|
+
export async function claimGatewayWork({ root = process.cwd(), workerId, leaseSeconds = 120,
|
|
26
|
+
executionMode = "host-effect", now = new Date() }) {
|
|
27
|
+
const paths = await pathsFor(root);
|
|
28
|
+
return withLock(paths, async () => {
|
|
29
|
+
const [policy, runtime, personas] = await Promise.all([
|
|
30
|
+
readJson(paths.gatewayPolicyPath, paths.catalog.root, normalizePolicy, emptyPolicy),
|
|
31
|
+
readJson(paths.gatewayRuntimePath, paths.catalog.root, normalizeRuntime, emptyRuntime),
|
|
32
|
+
loadPersonaRuntime(paths.catalog.root, paths.catalog)
|
|
33
|
+
]);
|
|
34
|
+
if (!policy.enabled || policy.killSwitch) return { item: null, reason: "disabled" };
|
|
35
|
+
const current = timestamp(now); workerId = exactId(workerId, "workerId");
|
|
36
|
+
executionMode = normalizeGatewayExecutionMode(executionMode);
|
|
37
|
+
const seconds = Number(leaseSeconds);
|
|
38
|
+
if (!Number.isInteger(seconds) || seconds < 15 || seconds > 900) throw new Error("leaseSeconds must be 15-900");
|
|
39
|
+
const items = runtime.queue.filter((item) => item.status === "pending" && new Date(item.availableAt) <= new Date(current)
|
|
40
|
+
&& !currentLane(runtime, item.agentId));
|
|
41
|
+
items.sort((a, b) => effectiveQueuePriority(policy, b) - effectiveQueuePriority(policy, a)
|
|
42
|
+
|| a.createdAt.localeCompare(b.createdAt) || a.queueId.localeCompare(b.queueId));
|
|
43
|
+
let item = null; let revoked = false;
|
|
44
|
+
for (const candidate of items) {
|
|
45
|
+
try {
|
|
46
|
+
assertActivePersona(personas.policy, personas.runtime, candidate.agentId, candidate.projectId, candidate.groupId);
|
|
47
|
+
} catch {
|
|
48
|
+
preserve(runtime, "queue", candidate, "identity-revoked", current);
|
|
49
|
+
candidate.status = "cancelled"; candidate.completedAt = current; candidate.updatedAt = current;
|
|
50
|
+
appendReceipt(runtime, "identity-revoked", candidate.queueId, current, {});
|
|
51
|
+
runtime.revision += 1; revoked = true;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (candidate.goalStepId) {
|
|
55
|
+
const goal = policy.goals.find((entry) => entry.goalId === candidate.goalId);
|
|
56
|
+
const step = goal && currentPlanStep(goal);
|
|
57
|
+
if (!goal?.plan || goal.status !== "active" || step?.stepId !== candidate.goalStepId || step.status !== "active"
|
|
58
|
+
|| candidate.agentId !== planStepAgentId(goal, step)) {
|
|
59
|
+
preserve(runtime, "queue", candidate, "plan-step-stale", current);
|
|
60
|
+
candidate.status = "cancelled"; candidate.completedAt = current; candidate.updatedAt = current;
|
|
61
|
+
appendReceipt(runtime, "plan-step-stale", candidate.queueId, current, { goalStepId: candidate.goalStepId });
|
|
62
|
+
runtime.revision += 1; revoked = true;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const resourceConflict = runtime.queue.some((leased) => leased.status === "leased"
|
|
67
|
+
&& leased.queueId !== candidate.queueId && conflictingResources(policy, candidate, leased).length > 0);
|
|
68
|
+
if (resourceConflict) continue;
|
|
69
|
+
item = candidate;
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
if (!item && revoked) await writeJson(paths.gatewayRuntimePath, runtime);
|
|
73
|
+
if (!item) return { item: null, reason: runtime.queue.some((entry) => entry.status === "pending") ? "waiting" : "idle/needs-goal" };
|
|
74
|
+
preserve(runtime, "queue", item, "leased", current);
|
|
75
|
+
item.status = "leased"; item.attempts += 1; item.updatedAt = current;
|
|
76
|
+
item.lease = newGatewayLease(workerId, current, seconds, executionMode);
|
|
77
|
+
runtime.lanes = runtime.lanes.filter((lane) => lane.agentId !== item.agentId || lane.status !== "leased");
|
|
78
|
+
runtime.lanes.push({ agentId: item.agentId, queueId: item.queueId, workerId, status: "leased", claimedAt: current,
|
|
79
|
+
expiresAt: item.lease.expiresAt, updatedAt: current, authority: "execution-state-only" });
|
|
80
|
+
runtime.health.worker = "healthy"; runtime.health.lastTickAt = current; runtime.revision += 1;
|
|
81
|
+
const receipt = appendReceipt(runtime, "leased", item.queueId, current,
|
|
82
|
+
{ workerId, attempt: item.attempts, executionMode });
|
|
83
|
+
await writeJson(paths.gatewayRuntimePath, runtime);
|
|
84
|
+
return { item: structuredClone(item), receipt };
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export async function markGatewayHostStarted({ root = process.cwd(), queueId, workerId, claimedAt, attempt,
|
|
89
|
+
now = new Date() }) {
|
|
90
|
+
const paths = await pathsFor(root);
|
|
91
|
+
return withLock(paths, async () => {
|
|
92
|
+
const [policy, runtime] = await Promise.all([
|
|
93
|
+
readJson(paths.gatewayPolicyPath, paths.catalog.root, normalizePolicy, emptyPolicy),
|
|
94
|
+
readJson(paths.gatewayRuntimePath, paths.catalog.root, normalizeRuntime, emptyRuntime)
|
|
95
|
+
]);
|
|
96
|
+
if (!policy.enabled || policy.killSwitch) throw new Error("gateway was disabled before host execution");
|
|
97
|
+
const item = runtime.queue.find((entry) => entry.queueId === exactId(queueId, "queueId"));
|
|
98
|
+
workerId = exactId(workerId, "workerId"); claimedAt = exactGatewayLeaseGeneration(claimedAt, attempt, "host start");
|
|
99
|
+
const current = timestamp(now);
|
|
100
|
+
const lane = requireExactHostLease(runtime, item, { workerId, claimedAt, attempt, current });
|
|
101
|
+
const receipt = markHostStarted(runtime, item, lane, workerId, current, { preserve, appendReceipt });
|
|
102
|
+
await writeJson(paths.gatewayRuntimePath, runtime);
|
|
103
|
+
return { item: structuredClone(item), receipt };
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export async function completeGatewayRun({ root = process.cwd(), queueId, workerId, claimedAt, attempt,
|
|
108
|
+
result, now = new Date() }) {
|
|
109
|
+
const paths = await pathsFor(root);
|
|
110
|
+
return withLock(paths, async () => {
|
|
111
|
+
const [policy, runtime, channelPolicy, channelRuntime, personas] = await Promise.all([
|
|
112
|
+
readJson(paths.gatewayPolicyPath, paths.catalog.root, normalizePolicy, emptyPolicy),
|
|
113
|
+
readJson(paths.gatewayRuntimePath, paths.catalog.root, normalizeRuntime, emptyRuntime),
|
|
114
|
+
loadChannelPolicy(paths.catalog.root, paths.catalog), loadChannelRuntime(paths.catalog.root, paths.catalog),
|
|
115
|
+
loadPersonaRuntime(paths.catalog.root, paths.catalog)
|
|
116
|
+
]);
|
|
117
|
+
if (!policy.enabled || policy.killSwitch) throw new Error("gateway was disabled before run completion");
|
|
118
|
+
const item = runtime.queue.find((entry) => entry.queueId === exactId(queueId, "queueId"));
|
|
119
|
+
workerId = exactId(workerId, "workerId");
|
|
120
|
+
claimedAt = exactGatewayLeaseGeneration(claimedAt, attempt, "run completion"); const current = timestamp(now);
|
|
121
|
+
const lane = requireExactGatewayLease(runtime, item,
|
|
122
|
+
{ workerId, claimedAt, attempt, current, action: "run completion" });
|
|
123
|
+
const markedHostRun = assertGatewayCompletionMode(item.lease, result);
|
|
124
|
+
const runIdentity = assertActivePersona(personas.policy, personas.runtime, item.agentId, item.projectId, item.groupId);
|
|
125
|
+
const boundGoal = item.goalId ? policy.goals.find((entry) => entry.goalId === item.goalId) : null;
|
|
126
|
+
const boundStep = item.goalStepId ? boundGoal && currentPlanStep(boundGoal) : null;
|
|
127
|
+
if (item.goalStepId) {
|
|
128
|
+
if (!boundGoal?.plan || boundGoal.status !== "active" || boundStep?.stepId !== item.goalStepId
|
|
129
|
+
|| boundStep.status !== "active" || item.agentId !== planStepAgentId(boundGoal, boundStep)) {
|
|
130
|
+
throw new Error("run completion is not bound to the current active goal step");
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const knowledgeGapRequest = result?.knowledgeGap === undefined || result?.knowledgeGap === null
|
|
134
|
+
? null : result.knowledgeGap;
|
|
135
|
+
const selfHelpRequest = result?.selfHelp === undefined || result?.selfHelp === null ? null : result.selfHelp;
|
|
136
|
+
if (knowledgeGapRequest && (item.channelEventId || !item.goalStepId || !boundGoal?.plan)) {
|
|
137
|
+
throw new Error("knowledge gaps require an exact goal-plan step without a channel obligation");
|
|
138
|
+
}
|
|
139
|
+
if (selfHelpRequest && (item.channelEventId || !item.goalStepId || !boundGoal?.plan)) {
|
|
140
|
+
throw new Error("self-help research requires an exact goal-plan step without a channel obligation");
|
|
141
|
+
}
|
|
142
|
+
if (knowledgeGapRequest && (result?.blocked || result?.completed)) {
|
|
143
|
+
throw new Error("knowledge gap result cannot also complete or generically block a step");
|
|
144
|
+
}
|
|
145
|
+
if (selfHelpRequest && (knowledgeGapRequest || result?.blocked || result?.completed)) {
|
|
146
|
+
throw new Error("self-help research cannot also complete, block, or open a knowledge gap");
|
|
147
|
+
}
|
|
148
|
+
const resultCheckpoint = boundGoal && result?.checkpoint !== undefined ? safeCheckpoint(result.checkpoint) : boundGoal?.checkpoint ?? null;
|
|
149
|
+
const premortemReview = boundStep?.premortemContractVersion === 1 && result?.completed
|
|
150
|
+
? await goalPremortem.reviewGoalPremortem({ root: paths.catalog.root, goal: boundGoal, step: boundStep,
|
|
151
|
+
item, checkpoint: resultCheckpoint, completedAt: current, host: runIdentity.binding.host, readOnly: result?.readOnly === true }) : null;
|
|
152
|
+
let executionReview = boundStep?.execution && result?.completed
|
|
153
|
+
? reviewExecutionResult(boundStep.execution, boundStep.executionOutcomes || [],
|
|
154
|
+
item.queueId, result.execution, current) : null;
|
|
155
|
+
if (executionReview && (boundStep.executionOutcomes || []).length >= 8) {
|
|
156
|
+
executionReview = { passed: false, outcome: null, reason: "Execution outcome history is full." };
|
|
157
|
+
}
|
|
158
|
+
const text = result?.text ? safeText(result.text, "result.text", 16000) : null;
|
|
159
|
+
let clarification = null; let exploration = null; let selfHelp = null; let selfHelpRequired = null;
|
|
160
|
+
preserve(runtime, "queue", item, "run-completed", current);
|
|
161
|
+
if (item.channelEventId) {
|
|
162
|
+
if (!text) throw new Error("a channel obligation requires a non-empty response");
|
|
163
|
+
const voice = evaluateVoiceOutput(text);
|
|
164
|
+
if (!voice.ok) throw new Error("channel response contains a prohibited attachment or consciousness claim");
|
|
165
|
+
const event = channelRuntime.runtime.events.find((entry) => entry.eventId === item.channelEventId);
|
|
166
|
+
if (!event) throw new Error("channel event disappeared before delivery preparation");
|
|
167
|
+
const binding = exactReplyBinding(channelPolicy.policy, event);
|
|
168
|
+
const idempotencyKey = "delivery:" + sha256([event.eventId, binding.id, event.chatId, event.threadId || "", event.replyTo || ""].join("\0")).slice(0, 32);
|
|
169
|
+
let outbox = runtime.outbox.find((entry) => entry.idempotencyKey === idempotencyKey);
|
|
170
|
+
if (!outbox) {
|
|
171
|
+
outbox = { outboxId: "gateway-outbox:" + sha256(idempotencyKey).slice(0, 32), queueId: item.queueId,
|
|
172
|
+
idempotencyKey, bindingId: binding.id, eventId: event.eventId, provider: event.provider, tenantId: event.tenantId,
|
|
173
|
+
accountId: event.accountId, chatId: event.chatId, threadId: event.threadId, replyTo: event.replyTo,
|
|
174
|
+
text, status: "prepared", attempts: 0, nextAttemptAt: current, createdAt: current, updatedAt: current,
|
|
175
|
+
deliveredAt: null, adapterReceipt: null, lastError: null, authority: "delivery-state-only" };
|
|
176
|
+
runtime.outbox.push(outbox);
|
|
177
|
+
}
|
|
178
|
+
item.status = "awaiting-delivery";
|
|
179
|
+
} else {
|
|
180
|
+
item.status = result?.blocked || knowledgeGapRequest || premortemReview?.blocked || (executionReview && !executionReview.passed)
|
|
181
|
+
? "blocked" : "completed"; item.completedAt = current;
|
|
182
|
+
const goal = boundGoal;
|
|
183
|
+
if (goal) {
|
|
184
|
+
policy.history.push({ kind: "goal", at: current, value: structuredClone(goal), authority: "authenticated-goal-policy" });
|
|
185
|
+
const checkpoint = resultCheckpoint;
|
|
186
|
+
goal.checkpoint = checkpoint; goal.heartbeatAt = current;
|
|
187
|
+
goal.blocker = result?.blocked ? safeText(result.blocker || "Run blocked.", "blocker", 500)
|
|
188
|
+
: premortemReview?.blocked ? premortemReview.reason : executionReview && !executionReview.passed ? executionReview.reason : null;
|
|
189
|
+
if (goal.plan && item.goalStepId) {
|
|
190
|
+
const step = currentPlanStep(goal);
|
|
191
|
+
step.checkpoint = checkpoint; step.updatedAt = current;
|
|
192
|
+
const knowledge = handlePlanKnowledge({ goal, step, item, runtime, current,
|
|
193
|
+
selfHelpRequest, knowledgeGapRequest, appendReceipt, newGoalQueue, planQueueKey });
|
|
194
|
+
if (knowledge) ({ clarification = null, selfHelp = null, selfHelpRequired = null } = knowledge);
|
|
195
|
+
else if (result?.blocked || premortemReview?.blocked) {
|
|
196
|
+
step.status = "blocked"; step.blocker = goal.blocker; goal.status = "blocked";
|
|
197
|
+
} else if (executionReview && !executionReview.passed) {
|
|
198
|
+
if (executionReview.outcome) step.executionOutcomes.push(executionReview.outcome);
|
|
199
|
+
if (executionReview.outcome && executionReview.nextAttempt) {
|
|
200
|
+
item.status = "completed"; step.status = "active"; step.blocker = null;
|
|
201
|
+
goal.status = "active"; goal.blocker = null; goal.nextSafeStep = step.title;
|
|
202
|
+
goal.plan.revision += 1;
|
|
203
|
+
const key = planQueueKey(goal.goalId, step.stepId, "explore",
|
|
204
|
+
executionReview.outcome.digest.slice(0, 20));
|
|
205
|
+
let queued = runtime.queue.find((entry) => entry.dedupeKey === key);
|
|
206
|
+
if (!queued) {
|
|
207
|
+
queued = newGoalQueue(goal, step, "follow-up", key, current);
|
|
208
|
+
runtime.queue.push(queued);
|
|
209
|
+
}
|
|
210
|
+
exploration = { queueId: queued.queueId, ...executionReview.nextAttempt };
|
|
211
|
+
appendReceipt(runtime, "execution-exploration-continued", queued.queueId, current, {
|
|
212
|
+
goalStepId: step.stepId, failedOutcomeId: executionReview.outcome.outcomeId,
|
|
213
|
+
attempt: executionReview.nextAttempt.attempt,
|
|
214
|
+
strategyId: executionReview.nextAttempt.strategyId
|
|
215
|
+
});
|
|
216
|
+
} else {
|
|
217
|
+
step.status = "blocked"; step.blocker = goal.blocker; goal.status = "blocked";
|
|
218
|
+
appendReceipt(runtime, executionReview.outcome ? "execution-gate-failed" : "execution-proof-invalid",
|
|
219
|
+
item.queueId, current, { goalStepId: step.stepId,
|
|
220
|
+
outcomeId: executionReview.outcome?.outcomeId || null });
|
|
221
|
+
}
|
|
222
|
+
} else if (result?.completed) {
|
|
223
|
+
if (premortemReview?.attachments) Object.assign(step, premortemReview.attachments);
|
|
224
|
+
if (executionReview?.outcome) {
|
|
225
|
+
step.executionOutcomes.push(executionReview.outcome);
|
|
226
|
+
appendReceipt(runtime, "execution-gate-passed", item.queueId, current, {
|
|
227
|
+
goalStepId: step.stepId, outcomeId: executionReview.outcome.outcomeId
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
step.status = "completed"; step.completedAt = current; step.completedByQueueId = item.queueId; step.blocker = null;
|
|
231
|
+
goal.plan.currentStepId = null; goal.plan.revision += 1;
|
|
232
|
+
const next = activateNextPlanStep(goal.plan, current);
|
|
233
|
+
if (next) {
|
|
234
|
+
goal.status = "active"; goal.nextSafeStep = next.title; goal.blocker = null;
|
|
235
|
+
const key = planQueueKey(goal.goalId, next.stepId, "ready", String(goal.plan.revision));
|
|
236
|
+
if (!runtime.queue.some((entry) => entry.dedupeKey === key)) {
|
|
237
|
+
const queued = newGoalQueue(goal, next, "follow-up", key, current);
|
|
238
|
+
runtime.queue.push(queued);
|
|
239
|
+
appendReceipt(runtime, "goal-step-ready", queued.queueId, current, { goalStepId: next.stepId });
|
|
240
|
+
}
|
|
241
|
+
} else {
|
|
242
|
+
goal.status = "completed";
|
|
243
|
+
}
|
|
244
|
+
} else {
|
|
245
|
+
goal.status = "active"; step.blocker = null;
|
|
246
|
+
}
|
|
247
|
+
} else if (!goal.plan) {
|
|
248
|
+
goal.status = result?.completed ? "completed" : result?.blocked ? "blocked" : "active";
|
|
249
|
+
}
|
|
250
|
+
goal.updatedAt = current;
|
|
251
|
+
policy.revision += 1;
|
|
252
|
+
if (goal.status === "active" && (!goal.plan || !result?.completed) && !selfHelp && !selfHelpRequired) {
|
|
253
|
+
const checkpointDigest = sha256(JSON.stringify(goal.checkpoint || { heartbeatAt: current })).slice(0, 20);
|
|
254
|
+
const step = currentPlanStep(goal);
|
|
255
|
+
const key = step ? planQueueKey(goal.goalId, step.stepId, "follow-up", checkpointDigest)
|
|
256
|
+
: "goal:" + goal.goalId + ":follow-up:" + checkpointDigest;
|
|
257
|
+
if (!runtime.queue.some((entry) => entry.dedupeKey === key)) runtime.queue.push({
|
|
258
|
+
queueId: "gateway-queue:" + sha256(key).slice(0, 32), dedupeKey: key, kind: "follow-up",
|
|
259
|
+
agentId: planStepAgentId(goal, step), projectId: goal.projectId, groupId: goal.groupId, goalId: goal.goalId,
|
|
260
|
+
goalStepId: step?.stepId || null, channelEventId: null, priority: goal.priority, status: "pending", attempts: 0, lease: null,
|
|
261
|
+
availableAt: new Date(new Date(current).getTime() + 60000).toISOString(), createdAt: current, updatedAt: current,
|
|
262
|
+
completedAt: null, lastError: null, authority: "execution-state-only"
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
item.lease = null; item.updatedAt = current; lane.status = "completed"; lane.updatedAt = current;
|
|
268
|
+
if (markedHostRun) runtime.health.host = "healthy";
|
|
269
|
+
runtime.health.worker = "healthy"; runtime.health.lastTickAt = current;
|
|
270
|
+
appendReceipt(runtime, "run-terminal", item.queueId, current, { status: item.status, goalStepId: item.goalStepId || null }); runtime.revision += 1;
|
|
271
|
+
await writeGatewayStatePair(policy, runtime);
|
|
272
|
+
return { item, outbox: runtime.outbox.find((entry) => entry.queueId === item.queueId) || null,
|
|
273
|
+
clarification, exploration, selfHelp, selfHelpRequired, executionReview, premortemReview };
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export async function failGatewayRun({ root = process.cwd(), queueId, workerId, claimedAt, attempt, error, retryAfterMs = 5000,
|
|
278
|
+
now = new Date() }) {
|
|
279
|
+
const paths = await pathsFor(root);
|
|
280
|
+
return withLock(paths, async () => {
|
|
281
|
+
const [policy, runtime] = await Promise.all([
|
|
282
|
+
readJson(paths.gatewayPolicyPath, paths.catalog.root, normalizePolicy, emptyPolicy),
|
|
283
|
+
readJson(paths.gatewayRuntimePath, paths.catalog.root, normalizeRuntime, emptyRuntime)
|
|
284
|
+
]);
|
|
285
|
+
const item = runtime.queue.find((entry) => entry.queueId === exactId(queueId, "queueId"));
|
|
286
|
+
workerId = exactId(workerId, "workerId"); claimedAt = exactGatewayLeaseGeneration(claimedAt, attempt, "run failure");
|
|
287
|
+
const current = timestamp(now);
|
|
288
|
+
const lane = requireExactGatewayLease(runtime, item,
|
|
289
|
+
{ workerId, claimedAt, attempt, current, action: "run failure" });
|
|
290
|
+
const message = safeText(String(error || "host runtime unavailable"), "runError", 500);
|
|
291
|
+
const delay = Number(retryAfterMs);
|
|
292
|
+
if (!Number.isFinite(delay) || delay < 250 || delay > 300000) throw new Error("retryAfterMs must be 250-300000");
|
|
293
|
+
const failure = failGatewayLane(policy, runtime, item, lane, current,
|
|
294
|
+
{ error: message, retryAfterMs: delay, preserve, appendReceipt });
|
|
295
|
+
if (failure.ambiguous) await writeGatewayStatePair(policy, runtime);
|
|
296
|
+
else await writeJson(paths.gatewayRuntimePath, runtime);
|
|
297
|
+
return { item, receipt: failure.receipt, policyEnabled: policy.enabled && !policy.killSwitch };
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { personaRuntimeFindings } from "./persona-runtime.js";
|
|
2
|
+
|
|
3
|
+
export function currentLane(runtime, agentId) {
|
|
4
|
+
return runtime.lanes.find((item) => item.agentId === agentId && item.status === "leased") || null;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function assertActivePersona(personaPolicy, personaRuntime, agentId, projectId, groupId) {
|
|
8
|
+
const findings = personaRuntimeFindings(personaPolicy, personaRuntime);
|
|
9
|
+
if (findings.length) throw new Error("persona runtime is unhealthy: " + findings.join(", "));
|
|
10
|
+
const persona = personaRuntime.personas.find((item) => item.personaId === agentId && item.status === "active");
|
|
11
|
+
if (!persona || !["agent", "bot"].includes(persona.kind)) {
|
|
12
|
+
throw new Error("gateway work requires an active authenticated agent or bot");
|
|
13
|
+
}
|
|
14
|
+
if (groupId !== null && persona.groupId !== groupId) {
|
|
15
|
+
throw new Error("gateway work group does not match authenticated persona membership");
|
|
16
|
+
}
|
|
17
|
+
const binding = personaPolicy.bindings.find((item) => item.id === persona.bindingId && item.active);
|
|
18
|
+
if (!binding || !binding.tenantId || !binding.profileId || !projectId) {
|
|
19
|
+
throw new Error("gateway work lacks an active authenticated identity binding");
|
|
20
|
+
}
|
|
21
|
+
return { persona, binding };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function exactReplyBinding(channelPolicy, event) {
|
|
25
|
+
const binding = channelPolicy.bindings.find((item) => item.id === event.bindingId && item.status === "active"
|
|
26
|
+
&& item.agentId === event.agentId && item.projectId === event.projectId && item.groupId === event.groupId
|
|
27
|
+
&& item.provider === event.provider && item.tenantId === event.tenantId && item.accountId === event.accountId
|
|
28
|
+
&& item.chatId === event.chatId && item.threadId === event.threadId && item.capabilities.includes("reply"));
|
|
29
|
+
if (!binding) throw new Error("current exact channel reply capability is unavailable");
|
|
30
|
+
return binding;
|
|
31
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
function sha256(value) {
|
|
4
|
+
return createHash("sha256").update(value).digest("hex");
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function appendReceipt(runtime, kind, objectId, now, details = {}) {
|
|
8
|
+
const material = { kind, objectId, at: now, details, authority: "execution-state-only" };
|
|
9
|
+
const digest = sha256(JSON.stringify(material));
|
|
10
|
+
const id = "gateway-receipt:" + digest.slice(0, 24);
|
|
11
|
+
const previous = runtime.receipts.find((item) => item.id === id);
|
|
12
|
+
if (previous) return previous;
|
|
13
|
+
const receipt = { id, ...material, digest };
|
|
14
|
+
runtime.receipts.push(receipt);
|
|
15
|
+
return receipt;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function preserve(runtime, kind, value, transition, now) {
|
|
19
|
+
runtime.history.push({ kind, objectId: kind === "outbox" ? value.outboxId : value.queueId, transition, at: now,
|
|
20
|
+
value: structuredClone(value), authority: "execution-state-only" });
|
|
21
|
+
}
|