blun-king-cli 9.1.601 → 9.1.603
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 +105 -94
- package/LIESMICH.txt +57 -57
- package/README.md +90 -90
- package/bin/agentspine-king-goal-inbox.mjs +127 -127
- package/bin/agentspine-king-goal-intake.mjs +106 -106
- package/bin/agentspine-king-host-runner.mjs +109 -109
- package/bin/agentspine-king-snapshot-policy.cjs +80 -80
- package/bin/agentspine-king-status-policy.mjs +226 -226
- package/bin/agentspine-king-worker-host.mjs +160 -160
- package/bin/core-bootstrap.js +77 -77
- package/bin/launcher-mode.js +93 -93
- package/bin/launcher-restart-policy.cjs +131 -131
- package/bin/launcher-runtime.js +491 -491
- package/bin/managed-node.js +186 -186
- package/bin/managed-plugin-selection.cjs +115 -115
- package/bin/native-module-repair.js +201 -201
- package/bin/node-runtime.js +34 -34
- package/bin/node-version.js +49 -49
- package/bin/plugin-bootstrap.js +162 -162
- package/bin/private-paths.js +1606 -1606
- package/bin/runtime-exit-ledger.cjs +144 -144
- package/bin/runtime-exit-ledger.d.cts +23 -23
- package/bin/standard-tools-bootstrap.js +272 -272
- package/bin/turn-thinking-policy.cjs +82 -82
- package/bin/update-copy.js +200 -200
- package/bin/update-lease.js +597 -597
- package/bin/update-notice.js +1568 -1568
- package/bin/verify-agent-behavior.cjs +122 -122
- package/bin/verify-agent-components.cjs +104 -104
- package/bin/verify-bundled-agent-sources.cjs +83 -83
- package/bin/windows-node-crash-dump.cjs +289 -289
- package/blun.mjs +1709 -1601
- package/codebase-index/codebase_index.py +470 -470
- package/dist-web/assets/{CodeBlockNode-CXnTLMNr.js → CodeBlockNode-TKKkJX8L.js} +1 -1
- package/dist-web/assets/{CodeBlockShell.vue_vue_type_style_index_0_lang-CCF8Ntnq.js → CodeBlockShell.vue_vue_type_style_index_0_lang-CUj2Ds5x.js} +1 -1
- package/dist-web/assets/DesignSystemView-DwK1aQMo.js +12 -0
- package/dist-web/assets/DesignSystemView-szjTj7ns.css +1 -0
- package/dist-web/assets/{Tooltip-CFQEzqC-.js → Tooltip-CTWv7WCU.js} +1 -1
- package/dist-web/assets/{abnfDiagram-N423BO3Z-Z1APZG5m.js → abnfDiagram-N423BO3Z-DA8V8Zjh.js} +1 -1
- package/dist-web/assets/{arc-C4VyWloT.js → arc-YtddKbdY.js} +1 -1
- package/dist-web/assets/{architectureDiagram-T3A2C74G-DpddbUcM.js → architectureDiagram-T3A2C74G-CGEiObmo.js} +1 -1
- package/dist-web/assets/{blockDiagram-VBNYF7ZC-BqccOSMr.js → blockDiagram-VBNYF7ZC-Bd5Vt50e.js} +1 -1
- package/dist-web/assets/{c4Diagram-5PPSVZJV-OHAcJrTy.js → c4Diagram-5PPSVZJV-D2ApPM54.js} +1 -1
- package/dist-web/assets/channel-BnluexuN.js +1 -0
- package/dist-web/assets/{chunk-2GRJ4B5K-DqJHs8Fw.js → chunk-2GRJ4B5K-BDcIEZSl.js} +1 -1
- package/dist-web/assets/{chunk-2Q5K7J3B-B1nCd91o.js → chunk-2Q5K7J3B-6aSabCX7.js} +1 -1
- package/dist-web/assets/{chunk-5RXB4S5H-neG70kZx.js → chunk-5RXB4S5H-BFRQvIJS.js} +1 -1
- package/dist-web/assets/{chunk-5VM5RSS4-l7biGqeH.js → chunk-5VM5RSS4-ChAa-DNY.js} +1 -1
- package/dist-web/assets/{chunk-6Q2QTUOP-BO-F11Gs.js → chunk-6Q2QTUOP-BIfrb2Jc.js} +1 -1
- package/dist-web/assets/{chunk-GF5L2VYU-BtC69fw0.js → chunk-GF5L2VYU-BR0gvtWI.js} +1 -1
- package/dist-web/assets/{chunk-JWPE2WC7-DIOubEqr.js → chunk-JWPE2WC7-BgXKsx5E.js} +1 -1
- package/dist-web/assets/{chunk-KBJHAD2P-DDUeH_Ba.js → chunk-KBJHAD2P-I8mrZhpz.js} +1 -1
- package/dist-web/assets/{chunk-RYQCIY6F-B9gFMVbS.js → chunk-RYQCIY6F-BMPaem92.js} +1 -1
- package/dist-web/assets/{chunk-XXDRQBXY-Bn1Xz6TQ.js → chunk-XXDRQBXY-COy0J-0j.js} +1 -1
- package/dist-web/assets/classDiagram-JCYQIIEL-BliFdSD9.js +1 -0
- package/dist-web/assets/classDiagram-v2-OCEON4UE-BliFdSD9.js +1 -0
- package/dist-web/assets/{cose-bilkent-JH36ORCC-DqigO3T4.js → cose-bilkent-JH36ORCC-MosTTRdo.js} +1 -1
- package/dist-web/assets/{cynefin-VYW2F7L2-rpD93tQ-.js → cynefin-VYW2F7L2-5tjPgrkP.js} +1 -1
- package/dist-web/assets/{cynefinDiagram-MW4NZA55-DbTqnkVJ.js → cynefinDiagram-MW4NZA55-CbDcdInR.js} +1 -1
- package/dist-web/assets/{dagre-VZM6K2ZE-DcM8aEVY.js → dagre-VZM6K2ZE-YaBom1ZH.js} +1 -1
- package/dist-web/assets/{diagram-7IWD3JNH-UymgsBNa.js → diagram-7IWD3JNH-Da4m-Hgz.js} +1 -1
- package/dist-web/assets/{diagram-B4RE2ZJO-BiYu5WPF.js → diagram-B4RE2ZJO-CAUUegxg.js} +1 -1
- package/dist-web/assets/{diagram-LBJQPF4R-BD3kkgOO.js → diagram-LBJQPF4R-o21mWKG4.js} +1 -1
- package/dist-web/assets/{diagram-Q27KOJAE-D1mYQ_cI.js → diagram-Q27KOJAE-BA3z3sXf.js} +1 -1
- package/dist-web/assets/{diagram-UB23O5K3-BtX8u6HG.js → diagram-UB23O5K3-CGRdSSDK.js} +1 -1
- package/dist-web/assets/{ebnfDiagram-BXEA7PRR-CBAnFkRF.js → ebnfDiagram-BXEA7PRR-Bw-iU9FP.js} +1 -1
- package/dist-web/assets/{erDiagram-JOGREHBK-3vKcWsxm.js → erDiagram-JOGREHBK-BwM1yiR0.js} +1 -1
- package/dist-web/assets/{flowDiagram-UKHOOZJN-D9wBbHm2.js → flowDiagram-UKHOOZJN-z05eOR1Q.js} +1 -1
- package/dist-web/assets/{ganttDiagram-PKOTCBZU-Cd_dViF-.js → ganttDiagram-PKOTCBZU-BMUubNTl.js} +1 -1
- package/dist-web/assets/{gitGraphDiagram-DS77QQ5N-BiEEtTo7.js → gitGraphDiagram-DS77QQ5N-Dccbi1wU.js} +1 -1
- package/dist-web/assets/{index-DdEakFJM.js → index-BBbC5LRF.js} +1 -1
- package/dist-web/assets/{index-BmKj7CB1.css → index-C9eDH3u-.css} +1 -1
- package/dist-web/assets/{index-Ba1OvLjf.js → index-D48p0M2S.js} +2 -2
- package/dist-web/assets/{index-CiPA8Kex.js → index-h0KEOmkU.js} +14 -14
- package/dist-web/assets/{index10-CNr4U-Nw.js → index10-Qbuw1N6u.js} +1 -1
- package/dist-web/assets/{index11-Dto_yZNz.js → index11-g8v025wN.js} +1 -1
- package/dist-web/assets/{index5-B2PldaO2.js → index5-BYNcV8BY.js} +1 -1
- package/dist-web/assets/{index6-CBdZmmyx.js → index6-BetANtnV.js} +1 -1
- package/dist-web/assets/{index7-Dz9oCsUF.js → index7-BcPbgCKN.js} +1 -1
- package/dist-web/assets/{index8-B6k25jtU.js → index8-B1lp-RvF.js} +2 -2
- package/dist-web/assets/{infoDiagram-6WML65LV-D4ph2sIH.js → infoDiagram-6WML65LV-BF_pbUnN.js} +1 -1
- package/dist-web/assets/{ishikawaDiagram-WSZJBQD7-DjKjbMm9.js → ishikawaDiagram-WSZJBQD7-Dedv4urS.js} +1 -1
- package/dist-web/assets/{journeyDiagram-NVQOT4AX-DttHDfPQ.js → journeyDiagram-NVQOT4AX-B1Ex3b6o.js} +1 -1
- package/dist-web/assets/{kanban-definition-27J2QSJJ-C5gB-W0d.js → kanban-definition-27J2QSJJ-BtCGliBH.js} +1 -1
- package/dist-web/assets/{linear-BRGe4-d6.js → linear-ByItc8Qr.js} +1 -1
- package/dist-web/assets/{mermaid.core-D-9Sx1No.js → mermaid.core-B1M2O7nN.js} +4 -4
- package/dist-web/assets/{mindmap-definition-FAOFIHXS-Cg_hoVIq.js → mindmap-definition-FAOFIHXS-D0UHKUyj.js} +1 -1
- package/dist-web/assets/{pegDiagram-VL7TDLO6-CSX9hitD.js → pegDiagram-VL7TDLO6-D7zYCVmG.js} +1 -1
- package/dist-web/assets/{pieDiagram-7S7Q4E2Y-0L0mxahZ.js → pieDiagram-7S7Q4E2Y-Bd0i9CHa.js} +1 -1
- package/dist-web/assets/{quadrantDiagram-CIZ2JOQS-U483z0er.js → quadrantDiagram-CIZ2JOQS-MG-Ug47d.js} +1 -1
- package/dist-web/assets/{railroadDiagram-AXF67PYL-D80a2_DJ.js → railroadDiagram-AXF67PYL-AZdl90Gj.js} +1 -1
- package/dist-web/assets/{requirementDiagram-LRYGKXZP-CeyHfq8t.js → requirementDiagram-LRYGKXZP-DjplF9kt.js} +1 -1
- package/dist-web/assets/{sankeyDiagram-W5VNT64P-CCISkkrR.js → sankeyDiagram-W5VNT64P-Bi9Zgfjs.js} +1 -1
- package/dist-web/assets/{sequenceDiagram-SI44F4Z6-jQfm2XS7.js → sequenceDiagram-SI44F4Z6-ChEpdCvb.js} +1 -1
- package/dist-web/assets/{sizeCapture-X5ZJPWSS-lRZsmCoS.js → sizeCapture-X5ZJPWSS-B5RFUdaa.js} +1 -1
- package/dist-web/assets/{stateDiagram-OKZ733FA-ClCgTdUm.js → stateDiagram-OKZ733FA-BkRblSKp.js} +1 -1
- package/dist-web/assets/stateDiagram-v2-UEYNNEHI-fVkQcACW.js +1 -0
- package/dist-web/assets/{swimlanes-SLNWSIFB-BCdOUN05.js → swimlanes-SLNWSIFB-DLXH6Zhc.js} +2 -2
- package/dist-web/assets/swimlanesDiagram-ULZ7WXOC-Bg-BiZJQ.js +8 -0
- package/dist-web/assets/{timeline-definition-Z64GVDOM-DpWutSKa.js → timeline-definition-Z64GVDOM-C_N88siO.js} +1 -1
- package/dist-web/assets/{vennDiagram-T6HMQDX7-DVC5JVsc.js → vennDiagram-T6HMQDX7-DMsnvDt0.js} +1 -1
- package/dist-web/assets/{vue.runtime.esm-bundler-BwwRtQwQ.js → vue.runtime.esm-bundler-B-3uIy85.js} +1 -1
- package/dist-web/assets/{wardleyDiagram-T6FBY63Y-BK2mrq7L.js → wardleyDiagram-T6FBY63Y-C9SJIPzq.js} +1 -1
- package/dist-web/assets/{xychartDiagram-ELKLHX3M-DUJu9F7-.js → xychartDiagram-ELKLHX3M-DZLWA6Go.js} +1 -1
- package/dist-web/index.html +32 -32
- package/dist-web/vis/index.html.gz +0 -0
- package/package.json +1 -1
- package/scripts/fix-node-pty-perms.js +53 -53
- package/standard-skills/blun-app-design-system/SKILL.md +30 -30
- package/standard-skills/blun-app-design-system/provenance.json +17 -17
- package/standard-skills/blun-app-design-system/references/tokens.md +48 -48
- package/standard-skills/blun-app-design-system/scripts/check-blun-design.mjs +49 -49
- package/telegram-plugin/blun.plugin.json +20 -20
- package/telegram-plugin/commands/access.md +37 -37
- package/telegram-plugin/dist/bridge.mjs +81 -81
- package/telegram-plugin/dist/mcp-server.mjs +158 -158
- package/worker-host.mjs +932 -932
- package/dist-web/assets/DesignSystemView-6HgAUEmC.css +0 -1
- package/dist-web/assets/DesignSystemView-BbMOu-XO.js +0 -12
- package/dist-web/assets/channel-B2VOQOI-.js +0 -1
- package/dist-web/assets/classDiagram-JCYQIIEL-BA1TjAxj.js +0 -1
- package/dist-web/assets/classDiagram-v2-OCEON4UE-BA1TjAxj.js +0 -1
- package/dist-web/assets/stateDiagram-v2-UEYNNEHI-fi_lrTAx.js +0 -1
- package/dist-web/assets/swimlanesDiagram-ULZ7WXOC-BUd_uYK3.js +0 -8
|
@@ -1,226 +1,226 @@
|
|
|
1
|
-
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
-
import { lstat, mkdir, open, readFile, realpath, rename, unlink } from 'node:fs/promises';
|
|
3
|
-
import path from 'node:path';
|
|
4
|
-
|
|
5
|
-
const SCHEMA = 'blun.king-goal-status/v2';
|
|
6
|
-
const MAX_BYTES = 4 * 1024 * 1024;
|
|
7
|
-
const STATES = new Set(['pending', 'dispatching', 'uncertain', 'delivered']);
|
|
8
|
-
const RESULT_REJECTIONS = new Set(['GOAL_STEP_RESULT_EXCEEDS_64_KIB', 'GOAL_STEP_RESULT_INVALID_JSON',
|
|
9
|
-
'GOAL_STEP_RESULT_INVALID', 'GOAL_STEP_CHECKPOINT_REQUIRED', 'GOAL_STEP_CHECKPOINT_TOO_LARGE']);
|
|
10
|
-
const digest = value => createHash('sha256').update(JSON.stringify(value)).digest('hex');
|
|
11
|
-
const identifier = value => typeof value === 'string' && /^[A-Za-z0-9:._@/-]{1,200}$/.test(value);
|
|
12
|
-
const routeKeys = ['id', 'provider', 'tenantId', 'accountId', 'chatId', 'threadId', 'agentId',
|
|
13
|
-
'projectId', 'groupId', 'createdAt'];
|
|
14
|
-
|
|
15
|
-
export function goalResultRejectionCode(error) {
|
|
16
|
-
const code = String(error?.message ?? error).replace(/^Host runtime unavailable: /, '');
|
|
17
|
-
return RESULT_REJECTIONS.has(code) ? code : null;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function normalizeScope(input) {
|
|
21
|
-
const keys = ['logicalPersonaId', 'runtimePersonaId', 'projectId', 'groupId', 'ownerSubjectId',
|
|
22
|
-
'chatId', 'senderId'];
|
|
23
|
-
if (!input || keys.some(key => !identifier(input[key]))
|
|
24
|
-
|| (input.threadId !== null && input.threadId !== undefined && !identifier(input.threadId))) throw new Error('GOAL_STATUS_SCOPE_INVALID');
|
|
25
|
-
return { ...Object.fromEntries(keys.map(key => [key, input[key]])), threadId: input.threadId ?? null };
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function errorCode(error) {
|
|
29
|
-
return /^(GOAL_[A-Z0-9_]{1,100})(?::|\s|$)/.exec(String(error ?? ''))?.[1] ?? 'GOAL_STATUS_OPERATION_REJECTED';
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function normalizeEvent(input) {
|
|
33
|
-
if (!input || !identifier(input.eventId)) throw new Error('GOAL_STATUS_EVENT_INVALID');
|
|
34
|
-
if (input.kind === 'intake-rejected' && identifier(input.messageId)) {
|
|
35
|
-
return { kind: input.kind, eventId: input.eventId, messageId: input.messageId, error: errorCode(input.error) };
|
|
36
|
-
}
|
|
37
|
-
if (!identifier(input.goalId) || (input.goalStepId !== null && input.goalStepId !== undefined && !identifier(input.goalStepId))) {
|
|
38
|
-
throw new Error('GOAL_STATUS_EVENT_INVALID');
|
|
39
|
-
}
|
|
40
|
-
const event = { kind: input.kind, eventId: input.eventId, goalId: input.goalId, goalStepId: input.goalStepId ?? null };
|
|
41
|
-
if (input.kind === 'result-rejected' && Number.isSafeInteger(input.attempt) && input.attempt > 0) {
|
|
42
|
-
return { ...event, attempt: input.attempt, error: errorCode(input.error) };
|
|
43
|
-
}
|
|
44
|
-
if (input.kind !== 'goal-status' || !identifier(input.status)
|
|
45
|
-
|| !['offen', 'blockiert', 'bestanden'].includes(input.gate)) throw new Error('GOAL_STATUS_EVENT_INVALID');
|
|
46
|
-
return { ...event, status: input.status, gate: input.gate };
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function message(scope, event) {
|
|
50
|
-
if (event.kind === 'intake-rejected') {
|
|
51
|
-
return `${scope.logicalPersonaId} | Zielannahme Nachricht ${event.messageId} | abgelehnt | ${event.error}`;
|
|
52
|
-
}
|
|
53
|
-
if (event.kind === 'result-rejected') {
|
|
54
|
-
return `${scope.logicalPersonaId} | Zielantwort ${event.goalId} / ${event.goalStepId ?? '-'} | abgelehnt ${event.attempt} | ${event.error}`;
|
|
55
|
-
}
|
|
56
|
-
return `${scope.logicalPersonaId} | Ziel ${event.goalId} | Schritt ${event.goalStepId ?? '-'} | ${event.status} | Gate ${event.gate}`;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function matches(binding, scope) {
|
|
60
|
-
return binding?.status === 'active' && binding.provider === 'telegram'
|
|
61
|
-
&& binding.authority === 'explicit-local-channel-policy'
|
|
62
|
-
&& binding.agentId === scope.runtimePersonaId && binding.projectId === scope.projectId
|
|
63
|
-
&& binding.groupId === scope.groupId && binding.chatId === scope.chatId
|
|
64
|
-
&& (binding.threadId ?? null) === scope.threadId
|
|
65
|
-
&& binding.senderIds?.includes(scope.senderId) && binding.capabilities?.includes('reply');
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function selectRoute(loaded, scope) {
|
|
69
|
-
const candidates = loaded?.policy?.bindings?.filter(binding => matches(binding, scope)) ?? [];
|
|
70
|
-
if (candidates.length !== 1) return null;
|
|
71
|
-
const binding = candidates[0];
|
|
72
|
-
if (routeKeys.some(key => key !== 'threadId' && (typeof binding[key] !== 'string' || !binding[key]))) return null;
|
|
73
|
-
return Object.fromEntries(routeKeys.map(key => [key, binding[key] ?? null]));
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function sameRoute(left, right) {
|
|
77
|
-
return left && right && routeKeys.every(key => left[key] === right[key]);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
async function assertRegular(file) {
|
|
81
|
-
try {
|
|
82
|
-
const meta = await lstat(file);
|
|
83
|
-
if (!meta.isFile() || meta.isSymbolicLink()) throw new Error('GOAL_STATUS_NOT_REGULAR');
|
|
84
|
-
} catch (error) { if (error.code !== 'ENOENT') throw error; }
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
async function loadState(file) {
|
|
88
|
-
await assertRegular(file);
|
|
89
|
-
let bytes;
|
|
90
|
-
try { bytes = await readFile(file); } catch (error) {
|
|
91
|
-
if (error.code === 'ENOENT') return { schema: SCHEMA, receipts: [] };
|
|
92
|
-
throw error;
|
|
93
|
-
}
|
|
94
|
-
try {
|
|
95
|
-
if (bytes.length > MAX_BYTES) throw new Error('oversized');
|
|
96
|
-
const state = JSON.parse(bytes);
|
|
97
|
-
if (state?.schema !== SCHEMA || !Array.isArray(state.receipts)) throw new Error('schema');
|
|
98
|
-
const keys = new Set();
|
|
99
|
-
for (const record of state.receipts) {
|
|
100
|
-
const scope = normalizeScope(record.scope);
|
|
101
|
-
const event = normalizeEvent(record.event);
|
|
102
|
-
if (JSON.stringify(scope) !== JSON.stringify(record.scope) || JSON.stringify(event) !== JSON.stringify(record.event)
|
|
103
|
-
|| record.key !== digest({ scope, event }) || keys.has(record.key) || !STATES.has(record.status)
|
|
104
|
-
|| !Number.isSafeInteger(record.nextAttemptAt) || record.nextAttemptAt < 0
|
|
105
|
-
|| !Number.isSafeInteger(record.attempts) || record.attempts < 0
|
|
106
|
-
|| (record.route !== null && (!record.route || routeKeys.some(key => !(key in record.route))))
|
|
107
|
-
|| (record.status === 'delivered' && !/^telegram-message:\d+$/.test(record.receipt))) throw new Error('receipt');
|
|
108
|
-
keys.add(record.key);
|
|
109
|
-
}
|
|
110
|
-
return state;
|
|
111
|
-
} catch { throw new Error('GOAL_STATUS_STATE_INVALID'); }
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
async function saveState(file, state, assertOwned) {
|
|
115
|
-
const bytes = JSON.stringify(state) + '\n';
|
|
116
|
-
if (Buffer.byteLength(bytes) > MAX_BYTES) throw new Error('GOAL_STATUS_CAPACITY');
|
|
117
|
-
const temporary = `${file}.${randomUUID()}.tmp`;
|
|
118
|
-
const handle = await open(temporary, 'wx', 0o600);
|
|
119
|
-
try {
|
|
120
|
-
await handle.writeFile(bytes);
|
|
121
|
-
await handle.sync();
|
|
122
|
-
} finally { await handle.close(); }
|
|
123
|
-
try {
|
|
124
|
-
await assertOwned();
|
|
125
|
-
await assertRegular(file);
|
|
126
|
-
await rename(temporary, file);
|
|
127
|
-
} finally { await unlink(temporary).catch(error => { if (error.code !== 'ENOENT') throw error; }); }
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export function createGoalStatusReporter({ stateFile, scope: inputScope, withOwnedFileLock,
|
|
131
|
-
loadPolicy, adapter, now = Date.now, maxRecords = 512 }) {
|
|
132
|
-
const scope = normalizeScope(inputScope);
|
|
133
|
-
if (!path.isAbsolute(stateFile) || typeof withOwnedFileLock !== 'function'
|
|
134
|
-
|| typeof loadPolicy !== 'function' || typeof adapter?.send !== 'function'
|
|
135
|
-
|| !Number.isSafeInteger(maxRecords) || maxRecords < 1) throw new Error('GOAL_STATUS_CONFIG_INVALID');
|
|
136
|
-
const scopeKey = digest(scope);
|
|
137
|
-
const transact = async task => {
|
|
138
|
-
const directory = path.dirname(stateFile);
|
|
139
|
-
await mkdir(directory, { recursive: true });
|
|
140
|
-
if (path.relative(directory, await realpath(directory)) !== '') throw new Error('GOAL_STATUS_DIRECTORY_REDIRECTED');
|
|
141
|
-
const assertPath = async () => {
|
|
142
|
-
if (path.relative(directory, await realpath(directory)) !== '') throw new Error('GOAL_STATUS_DIRECTORY_REDIRECTED');
|
|
143
|
-
await assertRegular(`${stateFile}.lock`);
|
|
144
|
-
};
|
|
145
|
-
return withOwnedFileLock(`${stateFile}.lock`, async ({ assertOwned }) => {
|
|
146
|
-
const state = await loadState(stateFile);
|
|
147
|
-
return task(state, () => saveState(stateFile, state, assertOwned), assertOwned);
|
|
148
|
-
}, { assertPath });
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
const record = async input => {
|
|
152
|
-
const event = normalizeEvent(input);
|
|
153
|
-
return transact(async (state, save) => {
|
|
154
|
-
const key = digest({ scope, event });
|
|
155
|
-
const existing = state.receipts.find(row => row.key === key);
|
|
156
|
-
if (existing) return { key, status: existing.status };
|
|
157
|
-
// Retain the deduplication history and every unresolved delivery; never silently evict one.
|
|
158
|
-
if (state.receipts.length >= maxRecords) throw new Error('GOAL_STATUS_CAPACITY');
|
|
159
|
-
state.receipts.push({ key, scope, event, route: selectRoute(await loadPolicy(), scope),
|
|
160
|
-
status: 'pending', attempts: 0, nextAttemptAt: 0, receipt: null });
|
|
161
|
-
await save();
|
|
162
|
-
return { key, status: 'pending' };
|
|
163
|
-
});
|
|
164
|
-
};
|
|
165
|
-
|
|
166
|
-
const flush = () => transact(async (state, save, assertOwned) => {
|
|
167
|
-
const results = [];
|
|
168
|
-
for (const row of state.receipts) {
|
|
169
|
-
if (digest(row.scope) !== scopeKey || row.status === 'delivered') continue;
|
|
170
|
-
const result = status => { results.push({ key: row.key, status }); };
|
|
171
|
-
// A process may have died after Telegram accepted the request but before the receipt was saved.
|
|
172
|
-
if (row.status === 'dispatching') { row.status = 'uncertain'; await save(); }
|
|
173
|
-
if (row.status === 'uncertain') { result('uncertain'); continue; }
|
|
174
|
-
const current = selectRoute(await loadPolicy(), scope);
|
|
175
|
-
if (!current) { result(row.route ? 'route-changed' : 'unrouted'); continue; }
|
|
176
|
-
if (row.route && !sameRoute(row.route, current)) { result('route-changed'); continue; }
|
|
177
|
-
if (row.nextAttemptAt > now()) { result('waiting'); continue; }
|
|
178
|
-
row.route ??= current;
|
|
179
|
-
row.status = 'dispatching'; row.attempts++;
|
|
180
|
-
await save();
|
|
181
|
-
if (!sameRoute(row.route, selectRoute(await loadPolicy(), scope))) {
|
|
182
|
-
row.status = 'pending'; await save(); result('route-changed'); continue;
|
|
183
|
-
}
|
|
184
|
-
await assertOwned();
|
|
185
|
-
let outcome;
|
|
186
|
-
try {
|
|
187
|
-
outcome = await adapter.send({ provider: row.route.provider, bindingId: row.route.id,
|
|
188
|
-
tenantId: row.route.tenantId, accountId: row.route.accountId, chatId: row.route.chatId,
|
|
189
|
-
threadId: row.route.threadId, replyTo: null,
|
|
190
|
-
idempotencyKey: row.key, text: message(scope, row.event) });
|
|
191
|
-
} catch { outcome = { ok: false, effect: 'unknown' }; }
|
|
192
|
-
if (outcome?.ok === true && /^telegram-message:\d+$/.test(outcome.receipt)) {
|
|
193
|
-
row.status = 'delivered'; row.receipt = outcome.receipt;
|
|
194
|
-
} else if (outcome?.ok === false && outcome.effect === 'none') {
|
|
195
|
-
row.status = 'pending';
|
|
196
|
-
const wait = Number.isSafeInteger(outcome.retryAfterMs) && outcome.retryAfterMs > 0 ? outcome.retryAfterMs : 15000;
|
|
197
|
-
row.nextAttemptAt = Math.min(Number.MAX_SAFE_INTEGER, now() + wait);
|
|
198
|
-
} else { row.status = 'uncertain'; }
|
|
199
|
-
await save();
|
|
200
|
-
result(row.status);
|
|
201
|
-
}
|
|
202
|
-
return results;
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
const reconcile = async context => {
|
|
206
|
-
for (const goal of context.goals) {
|
|
207
|
-
if (goal.agentId !== scope.runtimePersonaId || goal.projectId !== scope.projectId
|
|
208
|
-
|| goal.groupId !== scope.groupId || goal.ownerSubjectId !== scope.ownerSubjectId) continue;
|
|
209
|
-
const rows = context.queue.filter(row => row.goalId === goal.goalId && row.agentId === scope.runtimePersonaId
|
|
210
|
-
&& row.projectId === scope.projectId && row.groupId === scope.groupId && row.attempts > 0);
|
|
211
|
-
const row = rows.toSorted((a, b) => a.updatedAt.localeCompare(b.updatedAt)).at(-1);
|
|
212
|
-
if (!row) continue;
|
|
213
|
-
const step = goal.plan?.steps.find(item => item.stepId === row.goalStepId);
|
|
214
|
-
if (row.goalStepId && !step) continue;
|
|
215
|
-
const identity = { eventId: `${row.queueId}:${row.attempts}`, goalId: goal.goalId, goalStepId: row.goalStepId ?? null };
|
|
216
|
-
const rejection = goalResultRejectionCode(row.lastError);
|
|
217
|
-
if (rejection) {
|
|
218
|
-
await record({ ...identity, kind: 'result-rejected', attempt: row.attempts, error: rejection });
|
|
219
|
-
}
|
|
220
|
-
const status = row.status === 'blocked' ? 'blocked' : step?.status ?? goal.status;
|
|
221
|
-
await record({ ...identity, kind: 'goal-status', status,
|
|
222
|
-
gate: goal.status === 'completed' ? 'bestanden' : status === 'blocked' ? 'blockiert' : 'offen' });
|
|
223
|
-
}
|
|
224
|
-
};
|
|
225
|
-
return Object.freeze({ record, flush, reconcile });
|
|
226
|
-
}
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { lstat, mkdir, open, readFile, realpath, rename, unlink } from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
const SCHEMA = 'blun.king-goal-status/v2';
|
|
6
|
+
const MAX_BYTES = 4 * 1024 * 1024;
|
|
7
|
+
const STATES = new Set(['pending', 'dispatching', 'uncertain', 'delivered']);
|
|
8
|
+
const RESULT_REJECTIONS = new Set(['GOAL_STEP_RESULT_EXCEEDS_64_KIB', 'GOAL_STEP_RESULT_INVALID_JSON',
|
|
9
|
+
'GOAL_STEP_RESULT_INVALID', 'GOAL_STEP_CHECKPOINT_REQUIRED', 'GOAL_STEP_CHECKPOINT_TOO_LARGE']);
|
|
10
|
+
const digest = value => createHash('sha256').update(JSON.stringify(value)).digest('hex');
|
|
11
|
+
const identifier = value => typeof value === 'string' && /^[A-Za-z0-9:._@/-]{1,200}$/.test(value);
|
|
12
|
+
const routeKeys = ['id', 'provider', 'tenantId', 'accountId', 'chatId', 'threadId', 'agentId',
|
|
13
|
+
'projectId', 'groupId', 'createdAt'];
|
|
14
|
+
|
|
15
|
+
export function goalResultRejectionCode(error) {
|
|
16
|
+
const code = String(error?.message ?? error).replace(/^Host runtime unavailable: /, '');
|
|
17
|
+
return RESULT_REJECTIONS.has(code) ? code : null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function normalizeScope(input) {
|
|
21
|
+
const keys = ['logicalPersonaId', 'runtimePersonaId', 'projectId', 'groupId', 'ownerSubjectId',
|
|
22
|
+
'chatId', 'senderId'];
|
|
23
|
+
if (!input || keys.some(key => !identifier(input[key]))
|
|
24
|
+
|| (input.threadId !== null && input.threadId !== undefined && !identifier(input.threadId))) throw new Error('GOAL_STATUS_SCOPE_INVALID');
|
|
25
|
+
return { ...Object.fromEntries(keys.map(key => [key, input[key]])), threadId: input.threadId ?? null };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function errorCode(error) {
|
|
29
|
+
return /^(GOAL_[A-Z0-9_]{1,100})(?::|\s|$)/.exec(String(error ?? ''))?.[1] ?? 'GOAL_STATUS_OPERATION_REJECTED';
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function normalizeEvent(input) {
|
|
33
|
+
if (!input || !identifier(input.eventId)) throw new Error('GOAL_STATUS_EVENT_INVALID');
|
|
34
|
+
if (input.kind === 'intake-rejected' && identifier(input.messageId)) {
|
|
35
|
+
return { kind: input.kind, eventId: input.eventId, messageId: input.messageId, error: errorCode(input.error) };
|
|
36
|
+
}
|
|
37
|
+
if (!identifier(input.goalId) || (input.goalStepId !== null && input.goalStepId !== undefined && !identifier(input.goalStepId))) {
|
|
38
|
+
throw new Error('GOAL_STATUS_EVENT_INVALID');
|
|
39
|
+
}
|
|
40
|
+
const event = { kind: input.kind, eventId: input.eventId, goalId: input.goalId, goalStepId: input.goalStepId ?? null };
|
|
41
|
+
if (input.kind === 'result-rejected' && Number.isSafeInteger(input.attempt) && input.attempt > 0) {
|
|
42
|
+
return { ...event, attempt: input.attempt, error: errorCode(input.error) };
|
|
43
|
+
}
|
|
44
|
+
if (input.kind !== 'goal-status' || !identifier(input.status)
|
|
45
|
+
|| !['offen', 'blockiert', 'bestanden'].includes(input.gate)) throw new Error('GOAL_STATUS_EVENT_INVALID');
|
|
46
|
+
return { ...event, status: input.status, gate: input.gate };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function message(scope, event) {
|
|
50
|
+
if (event.kind === 'intake-rejected') {
|
|
51
|
+
return `${scope.logicalPersonaId} | Zielannahme Nachricht ${event.messageId} | abgelehnt | ${event.error}`;
|
|
52
|
+
}
|
|
53
|
+
if (event.kind === 'result-rejected') {
|
|
54
|
+
return `${scope.logicalPersonaId} | Zielantwort ${event.goalId} / ${event.goalStepId ?? '-'} | abgelehnt ${event.attempt} | ${event.error}`;
|
|
55
|
+
}
|
|
56
|
+
return `${scope.logicalPersonaId} | Ziel ${event.goalId} | Schritt ${event.goalStepId ?? '-'} | ${event.status} | Gate ${event.gate}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function matches(binding, scope) {
|
|
60
|
+
return binding?.status === 'active' && binding.provider === 'telegram'
|
|
61
|
+
&& binding.authority === 'explicit-local-channel-policy'
|
|
62
|
+
&& binding.agentId === scope.runtimePersonaId && binding.projectId === scope.projectId
|
|
63
|
+
&& binding.groupId === scope.groupId && binding.chatId === scope.chatId
|
|
64
|
+
&& (binding.threadId ?? null) === scope.threadId
|
|
65
|
+
&& binding.senderIds?.includes(scope.senderId) && binding.capabilities?.includes('reply');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function selectRoute(loaded, scope) {
|
|
69
|
+
const candidates = loaded?.policy?.bindings?.filter(binding => matches(binding, scope)) ?? [];
|
|
70
|
+
if (candidates.length !== 1) return null;
|
|
71
|
+
const binding = candidates[0];
|
|
72
|
+
if (routeKeys.some(key => key !== 'threadId' && (typeof binding[key] !== 'string' || !binding[key]))) return null;
|
|
73
|
+
return Object.fromEntries(routeKeys.map(key => [key, binding[key] ?? null]));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function sameRoute(left, right) {
|
|
77
|
+
return left && right && routeKeys.every(key => left[key] === right[key]);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function assertRegular(file) {
|
|
81
|
+
try {
|
|
82
|
+
const meta = await lstat(file);
|
|
83
|
+
if (!meta.isFile() || meta.isSymbolicLink()) throw new Error('GOAL_STATUS_NOT_REGULAR');
|
|
84
|
+
} catch (error) { if (error.code !== 'ENOENT') throw error; }
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function loadState(file) {
|
|
88
|
+
await assertRegular(file);
|
|
89
|
+
let bytes;
|
|
90
|
+
try { bytes = await readFile(file); } catch (error) {
|
|
91
|
+
if (error.code === 'ENOENT') return { schema: SCHEMA, receipts: [] };
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
if (bytes.length > MAX_BYTES) throw new Error('oversized');
|
|
96
|
+
const state = JSON.parse(bytes);
|
|
97
|
+
if (state?.schema !== SCHEMA || !Array.isArray(state.receipts)) throw new Error('schema');
|
|
98
|
+
const keys = new Set();
|
|
99
|
+
for (const record of state.receipts) {
|
|
100
|
+
const scope = normalizeScope(record.scope);
|
|
101
|
+
const event = normalizeEvent(record.event);
|
|
102
|
+
if (JSON.stringify(scope) !== JSON.stringify(record.scope) || JSON.stringify(event) !== JSON.stringify(record.event)
|
|
103
|
+
|| record.key !== digest({ scope, event }) || keys.has(record.key) || !STATES.has(record.status)
|
|
104
|
+
|| !Number.isSafeInteger(record.nextAttemptAt) || record.nextAttemptAt < 0
|
|
105
|
+
|| !Number.isSafeInteger(record.attempts) || record.attempts < 0
|
|
106
|
+
|| (record.route !== null && (!record.route || routeKeys.some(key => !(key in record.route))))
|
|
107
|
+
|| (record.status === 'delivered' && !/^telegram-message:\d+$/.test(record.receipt))) throw new Error('receipt');
|
|
108
|
+
keys.add(record.key);
|
|
109
|
+
}
|
|
110
|
+
return state;
|
|
111
|
+
} catch { throw new Error('GOAL_STATUS_STATE_INVALID'); }
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function saveState(file, state, assertOwned) {
|
|
115
|
+
const bytes = JSON.stringify(state) + '\n';
|
|
116
|
+
if (Buffer.byteLength(bytes) > MAX_BYTES) throw new Error('GOAL_STATUS_CAPACITY');
|
|
117
|
+
const temporary = `${file}.${randomUUID()}.tmp`;
|
|
118
|
+
const handle = await open(temporary, 'wx', 0o600);
|
|
119
|
+
try {
|
|
120
|
+
await handle.writeFile(bytes);
|
|
121
|
+
await handle.sync();
|
|
122
|
+
} finally { await handle.close(); }
|
|
123
|
+
try {
|
|
124
|
+
await assertOwned();
|
|
125
|
+
await assertRegular(file);
|
|
126
|
+
await rename(temporary, file);
|
|
127
|
+
} finally { await unlink(temporary).catch(error => { if (error.code !== 'ENOENT') throw error; }); }
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function createGoalStatusReporter({ stateFile, scope: inputScope, withOwnedFileLock,
|
|
131
|
+
loadPolicy, adapter, now = Date.now, maxRecords = 512 }) {
|
|
132
|
+
const scope = normalizeScope(inputScope);
|
|
133
|
+
if (!path.isAbsolute(stateFile) || typeof withOwnedFileLock !== 'function'
|
|
134
|
+
|| typeof loadPolicy !== 'function' || typeof adapter?.send !== 'function'
|
|
135
|
+
|| !Number.isSafeInteger(maxRecords) || maxRecords < 1) throw new Error('GOAL_STATUS_CONFIG_INVALID');
|
|
136
|
+
const scopeKey = digest(scope);
|
|
137
|
+
const transact = async task => {
|
|
138
|
+
const directory = path.dirname(stateFile);
|
|
139
|
+
await mkdir(directory, { recursive: true });
|
|
140
|
+
if (path.relative(directory, await realpath(directory)) !== '') throw new Error('GOAL_STATUS_DIRECTORY_REDIRECTED');
|
|
141
|
+
const assertPath = async () => {
|
|
142
|
+
if (path.relative(directory, await realpath(directory)) !== '') throw new Error('GOAL_STATUS_DIRECTORY_REDIRECTED');
|
|
143
|
+
await assertRegular(`${stateFile}.lock`);
|
|
144
|
+
};
|
|
145
|
+
return withOwnedFileLock(`${stateFile}.lock`, async ({ assertOwned }) => {
|
|
146
|
+
const state = await loadState(stateFile);
|
|
147
|
+
return task(state, () => saveState(stateFile, state, assertOwned), assertOwned);
|
|
148
|
+
}, { assertPath });
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const record = async input => {
|
|
152
|
+
const event = normalizeEvent(input);
|
|
153
|
+
return transact(async (state, save) => {
|
|
154
|
+
const key = digest({ scope, event });
|
|
155
|
+
const existing = state.receipts.find(row => row.key === key);
|
|
156
|
+
if (existing) return { key, status: existing.status };
|
|
157
|
+
// Retain the deduplication history and every unresolved delivery; never silently evict one.
|
|
158
|
+
if (state.receipts.length >= maxRecords) throw new Error('GOAL_STATUS_CAPACITY');
|
|
159
|
+
state.receipts.push({ key, scope, event, route: selectRoute(await loadPolicy(), scope),
|
|
160
|
+
status: 'pending', attempts: 0, nextAttemptAt: 0, receipt: null });
|
|
161
|
+
await save();
|
|
162
|
+
return { key, status: 'pending' };
|
|
163
|
+
});
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const flush = () => transact(async (state, save, assertOwned) => {
|
|
167
|
+
const results = [];
|
|
168
|
+
for (const row of state.receipts) {
|
|
169
|
+
if (digest(row.scope) !== scopeKey || row.status === 'delivered') continue;
|
|
170
|
+
const result = status => { results.push({ key: row.key, status }); };
|
|
171
|
+
// A process may have died after Telegram accepted the request but before the receipt was saved.
|
|
172
|
+
if (row.status === 'dispatching') { row.status = 'uncertain'; await save(); }
|
|
173
|
+
if (row.status === 'uncertain') { result('uncertain'); continue; }
|
|
174
|
+
const current = selectRoute(await loadPolicy(), scope);
|
|
175
|
+
if (!current) { result(row.route ? 'route-changed' : 'unrouted'); continue; }
|
|
176
|
+
if (row.route && !sameRoute(row.route, current)) { result('route-changed'); continue; }
|
|
177
|
+
if (row.nextAttemptAt > now()) { result('waiting'); continue; }
|
|
178
|
+
row.route ??= current;
|
|
179
|
+
row.status = 'dispatching'; row.attempts++;
|
|
180
|
+
await save();
|
|
181
|
+
if (!sameRoute(row.route, selectRoute(await loadPolicy(), scope))) {
|
|
182
|
+
row.status = 'pending'; await save(); result('route-changed'); continue;
|
|
183
|
+
}
|
|
184
|
+
await assertOwned();
|
|
185
|
+
let outcome;
|
|
186
|
+
try {
|
|
187
|
+
outcome = await adapter.send({ provider: row.route.provider, bindingId: row.route.id,
|
|
188
|
+
tenantId: row.route.tenantId, accountId: row.route.accountId, chatId: row.route.chatId,
|
|
189
|
+
threadId: row.route.threadId, replyTo: null,
|
|
190
|
+
idempotencyKey: row.key, text: message(scope, row.event) });
|
|
191
|
+
} catch { outcome = { ok: false, effect: 'unknown' }; }
|
|
192
|
+
if (outcome?.ok === true && /^telegram-message:\d+$/.test(outcome.receipt)) {
|
|
193
|
+
row.status = 'delivered'; row.receipt = outcome.receipt;
|
|
194
|
+
} else if (outcome?.ok === false && outcome.effect === 'none') {
|
|
195
|
+
row.status = 'pending';
|
|
196
|
+
const wait = Number.isSafeInteger(outcome.retryAfterMs) && outcome.retryAfterMs > 0 ? outcome.retryAfterMs : 15000;
|
|
197
|
+
row.nextAttemptAt = Math.min(Number.MAX_SAFE_INTEGER, now() + wait);
|
|
198
|
+
} else { row.status = 'uncertain'; }
|
|
199
|
+
await save();
|
|
200
|
+
result(row.status);
|
|
201
|
+
}
|
|
202
|
+
return results;
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
const reconcile = async context => {
|
|
206
|
+
for (const goal of context.goals) {
|
|
207
|
+
if (goal.agentId !== scope.runtimePersonaId || goal.projectId !== scope.projectId
|
|
208
|
+
|| goal.groupId !== scope.groupId || goal.ownerSubjectId !== scope.ownerSubjectId) continue;
|
|
209
|
+
const rows = context.queue.filter(row => row.goalId === goal.goalId && row.agentId === scope.runtimePersonaId
|
|
210
|
+
&& row.projectId === scope.projectId && row.groupId === scope.groupId && row.attempts > 0);
|
|
211
|
+
const row = rows.toSorted((a, b) => a.updatedAt.localeCompare(b.updatedAt)).at(-1);
|
|
212
|
+
if (!row) continue;
|
|
213
|
+
const step = goal.plan?.steps.find(item => item.stepId === row.goalStepId);
|
|
214
|
+
if (row.goalStepId && !step) continue;
|
|
215
|
+
const identity = { eventId: `${row.queueId}:${row.attempts}`, goalId: goal.goalId, goalStepId: row.goalStepId ?? null };
|
|
216
|
+
const rejection = goalResultRejectionCode(row.lastError);
|
|
217
|
+
if (rejection) {
|
|
218
|
+
await record({ ...identity, kind: 'result-rejected', attempt: row.attempts, error: rejection });
|
|
219
|
+
}
|
|
220
|
+
const status = row.status === 'blocked' ? 'blocked' : step?.status ?? goal.status;
|
|
221
|
+
await record({ ...identity, kind: 'goal-status', status,
|
|
222
|
+
gate: goal.status === 'completed' ? 'bestanden' : status === 'blocked' ? 'blockiert' : 'offen' });
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
return Object.freeze({ record, flush, reconcile });
|
|
226
|
+
}
|