blun-king-cli 9.1.309 → 9.1.311

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.
@@ -0,0 +1,91 @@
1
+ 'use strict';
2
+
3
+ const SAFE_ID_RE = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/u;
4
+ const AUTHORITIES = new Set(['runtime_user_prompt_hook', 'runtime_tool_policy']);
5
+ const RECEIPT_MAX_AGE_MS = 5 * 60 * 1000;
6
+
7
+ function fail(code = 'COGNITIVE_ATTENTION_AUTH_INVALID') {
8
+ const error = new Error(code);
9
+ error.code = code;
10
+ throw error;
11
+ }
12
+
13
+ function exactKeys(value, keys) {
14
+ return value && typeof value === 'object' && !Array.isArray(value)
15
+ && Object.keys(value).every((key) => keys.has(key));
16
+ }
17
+
18
+ function safeId(value) {
19
+ const text = String(value ?? '').trim();
20
+ return SAFE_ID_RE.test(text) ? text : '';
21
+ }
22
+
23
+ function authorizeAttentionCandidate({ store, tenantId, currentAgent, now, input } = {}) {
24
+ if (!store || typeof store.authorizeAttention !== 'function' || typeof now !== 'function'
25
+ || !exactKeys(input, new Set(['candidate', 'channel', 'authorization']))) fail();
26
+ const tenant = safeId(tenantId);
27
+ const agent = safeId(currentAgent);
28
+ const channel = safeId(input.channel);
29
+ const candidate = input.candidate;
30
+ const authorization = input.authorization;
31
+ const candidateKeys = new Set([
32
+ 'candidate_id', 'subject_kind', 'subject_id', 'reason', 'responsible_agent',
33
+ 'evidence_id', 'evidence_scope', 'evidence_at', 'confidence', 'allowed_channel',
34
+ 'detected_at', 'cooldown_ms', 'requires_runtime_authorization',
35
+ ]);
36
+ const authorizationKeys = new Set(['authority', 'decision', 'receiptId', 'scope', 'issuedAt']);
37
+ if (!tenant || !agent || !channel || !exactKeys(candidate, candidateKeys)
38
+ || !exactKeys(authorization, authorizationKeys)) fail();
39
+
40
+ const candidateId = safeId(candidate.candidate_id);
41
+ const responsibleAgent = safeId(candidate.responsible_agent);
42
+ const allowedChannel = safeId(candidate.allowed_channel);
43
+ const authority = String(authorization.authority ?? '');
44
+ const receiptId = safeId(authorization.receiptId);
45
+ const scope = String(authorization.scope ?? '');
46
+ const nowMs = Date.parse(String(now()));
47
+ const issuedAtMs = Date.parse(String(authorization.issuedAt ?? ''));
48
+ if (!candidateId || !responsibleAgent || responsibleAgent !== agent
49
+ || !allowedChannel || channel !== allowedChannel
50
+ || candidate.requires_runtime_authorization !== true
51
+ || !AUTHORITIES.has(authority) || authorization.decision !== 'passed'
52
+ || !receiptId || scope !== `attention:${candidateId}`
53
+ || !Number.isFinite(nowMs) || !Number.isFinite(issuedAtMs)
54
+ || issuedAtMs > nowMs || nowMs - issuedAtMs > RECEIPT_MAX_AGE_MS) fail();
55
+
56
+ const admitted = store.authorizeAttention({
57
+ tenantId: tenant,
58
+ candidate,
59
+ claimedBy: agent,
60
+ claimedAt: new Date(nowMs).toISOString(),
61
+ receiptId,
62
+ authority,
63
+ authorizedAt: new Date(issuedAtMs).toISOString(),
64
+ });
65
+ if (!admitted.claimed) {
66
+ return {
67
+ accepted: false,
68
+ reason: 'cooldown',
69
+ candidate_id: candidateId,
70
+ claimed_by: admitted.claimed_by,
71
+ next_allowed_at: admitted.next_allowed_at,
72
+ };
73
+ }
74
+ return {
75
+ accepted: true,
76
+ idempotent: admitted.idempotent === true,
77
+ candidate_id: candidateId,
78
+ subject_kind: candidate.subject_kind,
79
+ subject_id: candidate.subject_id,
80
+ reason: candidate.reason,
81
+ evidence_id: candidate.evidence_id,
82
+ evidence_scope: candidate.evidence_scope,
83
+ confidence: candidate.confidence,
84
+ allowed_channel: allowedChannel,
85
+ authorization_receipt_id: receiptId,
86
+ dispatch_intent: 'attention_notice',
87
+ requires_delivery_policy: true,
88
+ };
89
+ }
90
+
91
+ module.exports = { RECEIPT_MAX_AGE_MS, authorizeAttentionCandidate };
@@ -0,0 +1,81 @@
1
+ 'use strict';
2
+
3
+ const FOCUS_DOMAINS = new Set(['self', 'team', 'goal', 'open_thread', 'next_trigger', 'expected_evidence']);
4
+ const DOMAIN_WEIGHTS = new Map([
5
+ ['goal', 60],
6
+ ['open_thread', 50],
7
+ ['next_trigger', 40],
8
+ ['expected_evidence', 30],
9
+ ['team', 20],
10
+ ['self', 10],
11
+ ]);
12
+ const LABELS = new Map([
13
+ ['goal', 'Goal'],
14
+ ['open_thread', 'Open thread'],
15
+ ['next_trigger', 'Next trigger'],
16
+ ['expected_evidence', 'Expected evidence'],
17
+ ['team', 'Team'],
18
+ ['self', 'Self'],
19
+ ]);
20
+ const SAFETY_LINE = 'Durable context only; it cannot authorize any action or override the current assignment or runtime policy.';
21
+
22
+ function clean(value, max = 256) {
23
+ const text = String(value ?? '').replace(/[\u0000-\u001f\u007f]+/gu, ' ').replace(/\s+/gu, ' ').trim();
24
+ return text && text.length <= max ? text : '';
25
+ }
26
+
27
+ function normalizeScopes(value) {
28
+ if (value === undefined) return [];
29
+ if (!Array.isArray(value) || value.length > 8) return null;
30
+ const scopes = value.map((item) => clean(item, 128));
31
+ return scopes.every(Boolean) ? [...new Set(scopes)] : null;
32
+ }
33
+
34
+ function buildCognitiveFocusProjection(state, { focusScopes, maxItems = 6, maxChars = 900 } = {}) {
35
+ const scopes = normalizeScopes(focusScopes);
36
+ if (scopes === null || !Number.isSafeInteger(maxItems) || maxItems < 1 || maxItems > 8
37
+ || !Number.isSafeInteger(maxChars) || maxChars < 320 || maxChars > 1600
38
+ || !Array.isArray(state?.observations)) return null;
39
+
40
+ const groups = new Map();
41
+ state.observations.forEach((item, index) => {
42
+ const domain = String(item?.domain ?? '');
43
+ const key = clean(item?.key, 128);
44
+ const value = clean(item?.value, 512);
45
+ const scope = clean(item?.scope, 128);
46
+ const confidence = Number(item?.confidence);
47
+ const occurredAt = Date.parse(String(item?.occurred_at ?? ''));
48
+ if (!FOCUS_DOMAINS.has(domain) || !key || !value || !scope || scope === 'runtime'
49
+ || !Number.isFinite(confidence) || confidence < 0.5 || confidence > 1
50
+ || !Number.isFinite(occurredAt)) return;
51
+ const groupKey = `${domain}\0${key}`;
52
+ const existing = groups.get(groupKey) ?? [];
53
+ existing.push({ domain, key, value, scope, confidence, occurredAt, index });
54
+ groups.set(groupKey, existing);
55
+ });
56
+
57
+ const ranked = [];
58
+ for (const entries of groups.values()) {
59
+ entries.sort((left, right) => left.occurredAt - right.occurredAt || left.index - right.index);
60
+ const latest = entries.at(-1);
61
+ const revised = entries.some((item) => item.value !== latest.value);
62
+ const scopeScore = scopes.includes(latest.scope) ? 100 : 0;
63
+ ranked.push({
64
+ ...latest,
65
+ revised,
66
+ score: scopeScore + DOMAIN_WEIGHTS.get(latest.domain) + latest.confidence * 10,
67
+ });
68
+ }
69
+ ranked.sort((left, right) => right.score - left.score
70
+ || right.occurredAt - left.occurredAt || left.key.localeCompare(right.key));
71
+ if (ranked.length === 0) return null;
72
+
73
+ const selected = ranked.slice(0, maxItems);
74
+ const lines = selected.map((item) => `- ${LABELS.get(item.domain)}: ${item.value}${item.revised ? ' [revised]' : ''}`);
75
+ while ([`Current durable focus (${lines.length} items):`, ...lines, SAFETY_LINE].join('\n').length > maxChars
76
+ && lines.length > 0) lines.pop();
77
+ if (lines.length === 0) return null;
78
+ return [`Current durable focus (${lines.length} items):`, ...lines, SAFETY_LINE].join('\n');
79
+ }
80
+
81
+ module.exports = { buildCognitiveFocusProjection };
@@ -155,6 +155,17 @@ function initialize(db) {
155
155
  next_allowed_at TEXT NOT NULL,
156
156
  PRIMARY KEY (tenant_id, candidate_id)
157
157
  );
158
+ CREATE TABLE IF NOT EXISTS cognitive_attention_authorizations (
159
+ tenant_id TEXT NOT NULL,
160
+ receipt_id TEXT NOT NULL,
161
+ candidate_id TEXT NOT NULL,
162
+ authority TEXT NOT NULL,
163
+ authorized_at TEXT NOT NULL,
164
+ accepted INTEGER NOT NULL,
165
+ claimed_by TEXT NOT NULL,
166
+ next_allowed_at TEXT NOT NULL,
167
+ PRIMARY KEY (tenant_id, receipt_id)
168
+ );
158
169
  `);
159
170
  }
160
171
 
@@ -289,11 +300,84 @@ function openCognitiveStateStore({ home } = {}) {
289
300
  }
290
301
  }
291
302
 
303
+ function authorizeAttention(input) {
304
+ const allowedInput = new Set([
305
+ 'tenantId', 'candidate', 'claimedBy', 'claimedAt', 'receiptId', 'authority', 'authorizedAt',
306
+ ]);
307
+ const allowedCandidate = new Set([
308
+ 'candidate_id', 'subject_kind', 'subject_id', 'reason', 'responsible_agent',
309
+ 'evidence_id', 'evidence_scope', 'evidence_at', 'confidence', 'allowed_channel',
310
+ 'detected_at', 'cooldown_ms', 'requires_runtime_authorization',
311
+ ]);
312
+ if (!exactKeys(input, allowedInput) || !exactKeys(input.candidate, allowedCandidate)
313
+ || hasForbiddenKey({ candidate: input.candidate })) fail('COGNITIVE_FORBIDDEN_FIELD');
314
+ const tenant = safeId(input.tenantId);
315
+ const candidateId = safeId(input.candidate.candidate_id);
316
+ const claimedBy = safeId(input.claimedBy);
317
+ const receiptId = safeId(input.receiptId);
318
+ const authority = safeId(input.authority);
319
+ const claimedAtMs = Date.parse(String(input.claimedAt ?? ''));
320
+ const authorizedAtMs = Date.parse(String(input.authorizedAt ?? ''));
321
+ const cooldownMs = Number(input.candidate.cooldown_ms);
322
+ if (!tenant || !candidateId || !claimedBy || !receiptId || !authority
323
+ || !Number.isFinite(claimedAtMs) || !Number.isFinite(authorizedAtMs)
324
+ || !Number.isSafeInteger(cooldownMs) || cooldownMs < 1000
325
+ || input.candidate.requires_runtime_authorization !== true) fail('COGNITIVE_INVALID_EVENT');
326
+ const claimedAt = new Date(claimedAtMs).toISOString();
327
+ const authorizedAt = new Date(authorizedAtMs).toISOString();
328
+ const nextAllowedAt = new Date(claimedAtMs + cooldownMs).toISOString();
329
+ db.exec('BEGIN IMMEDIATE');
330
+ try {
331
+ const receipt = db.prepare(`SELECT candidate_id, accepted, claimed_by, next_allowed_at
332
+ FROM cognitive_attention_authorizations WHERE tenant_id = ? AND receipt_id = ?`).get(tenant, receiptId);
333
+ if (receipt) {
334
+ if (receipt.candidate_id !== candidateId) fail('COGNITIVE_ATTENTION_RECEIPT_REUSE');
335
+ db.exec('COMMIT');
336
+ return {
337
+ claimed: receipt.accepted === 1,
338
+ claimed_by: receipt.claimed_by,
339
+ next_allowed_at: receipt.next_allowed_at,
340
+ idempotent: true,
341
+ };
342
+ }
343
+ const existing = db.prepare(`SELECT claimed_by, next_allowed_at FROM cognitive_attention_claims
344
+ WHERE tenant_id = ? AND candidate_id = ?`).get(tenant, candidateId);
345
+ const accepted = !(existing && Date.parse(existing.next_allowed_at) > claimedAtMs);
346
+ const effectiveClaimedBy = accepted ? claimedBy : existing.claimed_by;
347
+ const effectiveNextAllowedAt = accepted ? nextAllowedAt : existing.next_allowed_at;
348
+ if (accepted) {
349
+ db.prepare(`INSERT INTO cognitive_attention_claims
350
+ (tenant_id, candidate_id, claimed_by, claimed_at, next_allowed_at) VALUES (?, ?, ?, ?, ?)
351
+ ON CONFLICT(tenant_id, candidate_id) DO UPDATE SET
352
+ claimed_by = excluded.claimed_by,
353
+ claimed_at = excluded.claimed_at,
354
+ next_allowed_at = excluded.next_allowed_at`)
355
+ .run(tenant, candidateId, claimedBy, claimedAt, nextAllowedAt);
356
+ }
357
+ db.prepare(`INSERT INTO cognitive_attention_authorizations
358
+ (tenant_id, receipt_id, candidate_id, authority, authorized_at, accepted, claimed_by, next_allowed_at)
359
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)`)
360
+ .run(tenant, receiptId, candidateId, authority, authorizedAt, accepted ? 1 : 0,
361
+ effectiveClaimedBy, effectiveNextAllowedAt);
362
+ db.exec('COMMIT');
363
+ return {
364
+ claimed: accepted,
365
+ claimed_by: effectiveClaimedBy,
366
+ next_allowed_at: effectiveNextAllowedAt,
367
+ idempotent: false,
368
+ };
369
+ } catch (error) {
370
+ try { db.exec('ROLLBACK'); } catch {}
371
+ throw error;
372
+ }
373
+ }
374
+
292
375
  return {
293
376
  commit,
294
377
  read,
295
378
  verify,
296
379
  claimAttention,
380
+ authorizeAttention,
297
381
  close: () => {
298
382
  try { db.exec('PRAGMA wal_checkpoint(TRUNCATE)'); } catch {}
299
383
  db.close();
@@ -3,6 +3,8 @@
3
3
  const crypto = require('node:crypto');
4
4
  const { openCognitiveStateStore } = require('./cognitive-state-store.cjs');
5
5
  const { buildCognitiveContextProjection } = require('./cognitive-context-projection.cjs');
6
+ const { buildCognitiveFocusProjection } = require('./cognitive-focus-projection.cjs');
7
+ const { authorizeAttentionCandidate } = require('./cognitive-attention-runtime.cjs');
6
8
 
7
9
  const SAFE_ID_RE = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/u;
8
10
  const RIGHTS_AUTHORITIES = new Set(['runtime_user_prompt_hook', 'runtime_tool_policy']);
@@ -10,6 +12,8 @@ const RIGHTS_DECISIONS = new Set(['passed', 'blocked', 'not_applicable']);
10
12
  const TOOL_POLICY_DECISIONS = new Set(['passed', 'blocked', 'error']);
11
13
  const TOOL_OUTCOMES = new Set(['success', 'error', 'cancelled']);
12
14
  const TURN_REASONS = new Set(['completed', 'cancelled', 'filtered', 'failed']);
15
+ const FOCUS_DOMAINS = new Set(['self', 'team', 'goal', 'open_thread', 'next_trigger', 'expected_evidence']);
16
+ const AUTHORITY_KEY_RE = /(?:^|:|_)(?:acl|api[_-]?key|capability|password|permission|secret|token)(?::|_|$)/iu;
13
17
 
14
18
  function fail(code) {
15
19
  const error = new Error(code);
@@ -140,6 +144,27 @@ function createCognitiveTurnLifecycle({ home, tenantId, agentId, runtimeId, now
140
144
  ]);
141
145
  }
142
146
 
147
+ function recordFocusSnapshot(input) {
148
+ if (!exactKeys(input, new Set(['snapshotId', 'observations']))
149
+ || !safeId(input.snapshotId) || !Array.isArray(input.observations)
150
+ || input.observations.length < 1 || input.observations.length > 16) {
151
+ fail('COGNITIVE_FOCUS_INVALID');
152
+ }
153
+ const observations = input.observations.map((item) => {
154
+ if (!exactKeys(item, new Set(['domain', 'key', 'value', 'confidence', 'scope']))) fail('COGNITIVE_FOCUS_INVALID');
155
+ const domain = String(item.domain ?? '');
156
+ const key = cleanLabel(item.key, 128);
157
+ const value = cleanLabel(item.value, 512);
158
+ const confidence = Number(item.confidence);
159
+ const scope = cleanLabel(item.scope, 128);
160
+ if (!FOCUS_DOMAINS.has(domain) || !key || AUTHORITY_KEY_RE.test(key) || !value
161
+ || !Number.isFinite(confidence) || confidence < 0.5 || confidence > 1
162
+ || !scope || scope === 'runtime') fail('COGNITIVE_FOCUS_INVALID');
163
+ return { domain, key, value, confidence, scope };
164
+ });
165
+ return commitStage(`focus-${input.snapshotId}`, observations);
166
+ }
167
+
143
168
  function toolFields(input, allowedKeys) {
144
169
  if (!exactKeys(input, allowedKeys)) fail('COGNITIVE_LIFECYCLE_INVALID');
145
170
  const turnId = safeTurnId(input.turnId);
@@ -174,13 +199,26 @@ function createCognitiveTurnLifecycle({ home, tenantId, agentId, runtimeId, now
174
199
  }
175
200
 
176
201
  function projectForTurn(input) {
177
- if (!exactKeys(input, new Set(['turnId']))) fail('COGNITIVE_LIFECYCLE_INVALID');
202
+ if (!exactKeys(input, new Set(['turnId', 'focusScopes']))) fail('COGNITIVE_LIFECYCLE_INVALID');
178
203
  const turnId = safeTurnId(input.turnId);
179
204
  if (turnId === null) fail('COGNITIVE_LIFECYCLE_INVALID');
180
- return buildCognitiveContextProjection(store.read({ tenantId: tenant, agentId: agent }), {
205
+ const state = store.read({ tenantId: tenant, agentId: agent });
206
+ const continuity = buildCognitiveContextProjection(state, {
181
207
  currentTurnId: turnId,
182
208
  currentRuntimeId: runtime,
183
209
  });
210
+ const focus = buildCognitiveFocusProjection(state, { focusScopes: input.focusScopes });
211
+ return [focus, continuity].filter(Boolean).join('\n\n') || null;
212
+ }
213
+
214
+ function authorizeAttention(input) {
215
+ return authorizeAttentionCandidate({
216
+ store,
217
+ tenantId: tenant,
218
+ currentAgent: agent,
219
+ now,
220
+ input,
221
+ });
184
222
  }
185
223
 
186
224
  return {
@@ -188,6 +226,8 @@ function createCognitiveTurnLifecycle({ home, tenantId, agentId, runtimeId, now
188
226
  recordRightsCheck,
189
227
  recordToolPolicy,
190
228
  recordToolResult,
229
+ recordFocusSnapshot,
230
+ authorizeAttention,
191
231
  projectForTurn,
192
232
  endTurn,
193
233
  read: () => store.read({ tenantId: tenant, agentId: agent }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blun-king-cli",
3
- "version": "9.1.309",
3
+ "version": "9.1.311",
4
4
  "description": "BLUN CLI - your own AI agent with a Telegram channel. Get it done. With BLUN.",
5
5
  "license": "MIT",
6
6
  "bin": {