blun-king-cli 9.1.301 → 9.1.302

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.
@@ -5,6 +5,7 @@ const { recordIdentityJournalCandidate } = require('./identity-journal-policy.cj
5
5
  const { personalityContextEnabled } = require('./personality-mode.cjs');
6
6
  const { ensurePersonalityWorkspace } = require('./personality-setup-policy.cjs');
7
7
  const { prepareRelationshipTurn } = require('./relationship-continuity-policy.cjs');
8
+ const { recordExplicitRelationshipCandidate } = require('./relationship-learning-policy.cjs');
8
9
 
9
10
  const MAX_FILE_BYTES = 64 * 1024;
10
11
  const DEFAULT_MAX_CHARS = 1500;
@@ -323,6 +324,15 @@ function recordChannelIdentity(envelope, env = process.env) {
323
324
  occurredAt: firstSeenAt,
324
325
  });
325
326
  if (firstSeenAt) recordInteractionActivity(root, agentId, actorId, firstSeenAt);
327
+ const learning = recordExplicitRelationshipCandidate({
328
+ root,
329
+ tenantId,
330
+ agentId,
331
+ actorId,
332
+ groupId,
333
+ text: envelope.text,
334
+ occurredAt: firstSeenAt,
335
+ });
326
336
  const journal = created.includes('relationship') && firstSeenAt
327
337
  ? recordIdentityJournalCandidate({
328
338
  kind: 'first_interaction',
@@ -341,6 +351,7 @@ function recordChannelIdentity(envelope, env = process.env) {
341
351
  actor_id: actorId,
342
352
  ...(groupId ? { group_id: groupId } : {}),
343
353
  created,
354
+ ...(learning ? { learning } : {}),
344
355
  ...(journal ? { journal } : {}),
345
356
  model_context: renderIdentityContext({
346
357
  root,
@@ -0,0 +1,111 @@
1
+ 'use strict';
2
+
3
+ const crypto = require('node:crypto');
4
+ const fs = require('node:fs');
5
+ const path = require('node:path');
6
+
7
+ const SAFE_ID_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/u;
8
+ const MAX_FILE_BYTES = 64 * 1024;
9
+ const MAX_VALUE_CHARS = 280;
10
+ const EXPLICIT_PATTERNS = [
11
+ /^(?:bitte\s+)?merk(?:e)?\s+dir\s*(?::|,|\s+dass\s+)?\s*(.+)$/iu,
12
+ /^(?:please\s+)?remember(?:\s+that)?\s*(?::|,)?\s*(.+)$/iu,
13
+ ];
14
+ const FORBIDDEN_RE = /\b(?:acl|admin|api[_ -]?key|capability|deploy|freigabe|passwor[dt]|permission|secret|token|write access|zugriffs?recht|du darfst|you may)\b/iu;
15
+
16
+ function safeId(value) {
17
+ const normalized = String(value ?? '').trim();
18
+ return SAFE_ID_RE.test(normalized) ? normalized : '';
19
+ }
20
+
21
+ function isInside(root, target) {
22
+ const relative = path.relative(root, target);
23
+ return relative === '' || (!relative.startsWith(`..${path.sep}`) && relative !== '..' && !path.isAbsolute(relative));
24
+ }
25
+
26
+ function readManifest(root) {
27
+ const target = path.join(root, 'manifest.json');
28
+ try {
29
+ const stat = fs.lstatSync(target);
30
+ if (!stat.isFile() || stat.isSymbolicLink() || stat.size > MAX_FILE_BYTES) return undefined;
31
+ const value = JSON.parse(fs.readFileSync(target, 'utf8'));
32
+ return value && typeof value === 'object' && !Array.isArray(value) ? value : undefined;
33
+ } catch {
34
+ return undefined;
35
+ }
36
+ }
37
+
38
+ function explicitValue(text) {
39
+ const normalized = String(text ?? '').replace(/[\u0000-\u001f\u007f]+/gu, ' ').replace(/\s+/gu, ' ').trim();
40
+ for (const pattern of EXPLICIT_PATTERNS) {
41
+ const match = normalized.match(pattern);
42
+ const value = String(match?.[1] ?? '').trim().slice(0, MAX_VALUE_CHARS);
43
+ if (value.length >= 3 && !FORBIDDEN_RE.test(value)) return value;
44
+ }
45
+ return '';
46
+ }
47
+
48
+ function createJsonOnce(root, target, value) {
49
+ let handle;
50
+ try {
51
+ const parent = path.dirname(target);
52
+ fs.mkdirSync(parent, { recursive: true });
53
+ if (!isInside(root, fs.realpathSync(parent))) return false;
54
+ handle = fs.openSync(target, 'wx', 0o600);
55
+ fs.writeFileSync(handle, `${JSON.stringify(value, null, 2)}\n`, 'utf8');
56
+ fs.fsyncSync(handle);
57
+ return true;
58
+ } catch (error) {
59
+ if (error?.code === 'EEXIST') return false;
60
+ return false;
61
+ } finally {
62
+ if (handle !== undefined) fs.closeSync(handle);
63
+ }
64
+ }
65
+
66
+ function recordExplicitRelationshipCandidate(input = {}) {
67
+ if (input.groupId) return undefined;
68
+ const root = path.resolve(String(input.root ?? ''));
69
+ const tenantId = safeId(input.tenantId);
70
+ const agentId = safeId(input.agentId);
71
+ const actorId = safeId(input.actorId);
72
+ const occurredAt = String(input.occurredAt ?? '').trim();
73
+ const value = explicitValue(input.text);
74
+ if (!tenantId || !agentId || !actorId || !value || Number.isNaN(Date.parse(occurredAt))) return undefined;
75
+ try {
76
+ if (!fs.lstatSync(root).isDirectory() || fs.lstatSync(root).isSymbolicLink()) return undefined;
77
+ const realRoot = fs.realpathSync(root);
78
+ const manifest = readManifest(realRoot);
79
+ if (manifest?.version !== 1 || manifest.tenant_id !== tenantId || manifest.active_agent_id !== agentId) return undefined;
80
+ // The timestamp keeps later confirmations or contradictions as separate history,
81
+ // while an identical channel event remains idempotent on retry.
82
+ const identity = [tenantId, agentId, actorId, occurredAt, value].join('\0');
83
+ const candidateId = crypto.createHash('sha256').update(identity).digest('hex').slice(0, 32);
84
+ const target = path.resolve(realRoot, 'agents', agentId, 'relationship-candidates', `${candidateId}.json`);
85
+ if (!isInside(realRoot, target)) return undefined;
86
+ const candidate = {
87
+ version: 1,
88
+ candidate_id: candidateId,
89
+ status: 'pending',
90
+ kind: 'explicit_relationship_note',
91
+ tenant_id: tenantId,
92
+ agent_id: agentId,
93
+ actor_id: actorId,
94
+ value,
95
+ source: 'explicit_statement',
96
+ context: {
97
+ channel: 'telegram',
98
+ conversation: 'direct',
99
+ },
100
+ confidence: 1,
101
+ scope: 'agent_relationship',
102
+ history_policy: 'append_only',
103
+ occurred_at: occurredAt,
104
+ };
105
+ return { candidate_id: candidateId, created: createJsonOnce(realRoot, target, candidate) };
106
+ } catch {
107
+ return undefined;
108
+ }
109
+ }
110
+
111
+ module.exports = { recordExplicitRelationshipCandidate };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blun-king-cli",
3
- "version": "9.1.301",
3
+ "version": "9.1.302",
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": {