-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmemory.ts
More file actions
1668 lines (1531 loc) · 73.4 KB
/
Copy pathmemory.ts
File metadata and controls
1668 lines (1531 loc) · 73.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* MemGPT-inspired Hierarchical Memory for SecureContext
*
* Architecture (learned from MemGPT / Letta):
*
* ┌─────────────────────────────────────────────────────────┐
* │ WORKING MEMORY (hot, bounded, fast) │
* │ - Key-value facts with importance scores (1–5) │
* │ - Max 100 entries before auto-eviction (dynamic 100-250)│
* │ - Persisted in SQLite for cross-restart continuity │
* │ - Returned in full on zc_recall_context() │
* └─────────────────────────────────────────────────────────┘
* │ eviction (low importance / oldest)
* ▼
* ┌─────────────────────────────────────────────────────────┐
* │ ARCHIVAL MEMORY (cold, unbounded, searchable) │
* │ = the FTS5 knowledge base (knowledge.ts) │
* │ - Evicted WM facts land here with source "memory:key" │
* │ - Session summaries land here with source "[SUMMARY]" │
* │ - Searchable via BM25 + vector hybrid (zc_search) │
* └─────────────────────────────────────────────────────────┘
*
* AGENT NAMESPACING:
* When multiple agents run in parallel (e.g., SecureContext multi-agent pattern),
* keys are namespaced by agent_id to prevent last-write-wins collisions.
* Default agent_id is "default" — single-agent use is unchanged.
*
* SECURITY:
* - All values sanitized (strip \r\n\x00) before storage
* - Max 500 chars per value to prevent DB bloat attacks
* - Max 100 chars per key
* - Eviction is deterministic — no LLM call in the critical path
*/
import { DatabaseSync } from "node:sqlite";
import { createHash, createHmac, scryptSync, randomBytes, timingSafeEqual } from "node:crypto";
import { mkdirSync } from "node:fs";
import { join } from "node:path";
import { Config } from "./config.js";
import { runMigrations } from "./migrations.js";
import { indexContent } from "./knowledge.js";
import {
verifyToken,
canBroadcast,
ROLE_PERMISSIONS,
} from "./access-control.js";
import { computeRowHash, getLastHash, verifyChain } from "./chain.js";
import { computeSalience, salienceEnabled } from "./salience.js";
import { budgetFacts, effectiveImportance, type TemporalWindow } from "./recall_budget.js";
/** v0.31.0 epistemology layer — WHAT kind of claim a fact is. */
export type MemoryKind = "fact" | "decision" | "hypothesis" | "prediction";
/** Resolution state of a prediction/hypothesis (whether it came true). */
export type ResolutionStatus = "open" | "resolved_correct" | "resolved_incorrect" | "resolved_partial";
export interface MemoryFact {
key: string;
value: string;
importance: number;
created_at: string;
agent_id?: string;
// v0.31.0 epistemology layer (all optional — absent ⇒ plain observed fact, byte-identical):
provenance?: Provenance; // HOW we know it (orthogonal axis, declared below)
kind?: MemoryKind; // WHAT kind of claim
confidence?: number | null; // HOW sure (0–1; predictions/hypotheses)
resolution_status?: ResolutionStatus | null; // did it come true
resolved_at?: string | null;
// v0.32.0 recency-decay/salience (optional; absent ⇒ no salience contribution):
access_count?: number;
last_retrieved_at?: string | null;
// v0.38.0 per-claim citation (what created the fact):
origin?: string | null;
}
// SECURITY: Strip control chars and limit length to prevent log injection / DB bloat
function sanitize(s: string, maxLen: number): string {
return String(s).replace(/[\r\n\x00\x01-\x08\x0b\x0c\x0e-\x1f]/g, " ").trim().slice(0, maxLen);
}
// ─────────────────────────────────────────────────────────────────────────────
// SMART WORKING MEMORY SIZING
//
// Instead of a fixed 100-fact ceiling for every project, SecureContext measures
// three objective complexity signals and derives a project-specific limit:
//
// Signal 1 — KB depth (source_meta count):
// Each 15 indexed sources adds +1 to the limit, capped at +60.
// Rationale: a project with 300 KB entries (API docs, specs, code files)
// needs more memory to track what has been read vs what is still pending.
//
// Signal 2 — Coordination history (broadcasts count):
// Each 30 broadcast events adds +1, capped at +40.
// Rationale: projects with many ASSIGN/MERGE cycles have more decisions in
// flight that the agent must not forget mid-session.
//
// Signal 3 — Agent density (active agent_sessions count):
// Each active agent adds +15, capped at +50.
// Rationale: parallel agents produce parallel facts; each agent's state
// must be independently trackable without evicting the others.
//
// Formula: limit = clamp(100 + kb_bonus + bc_bonus + agent_bonus, 100, 250)
// evictTo = floor(limit × 0.80)
//
// Range examples:
// Solo scratch project (0 agents, <15 KB, <30 BC): limit=100, evictTo=80
// Single-dev project (1 agent, 30 KB, 60 BC): limit=119, evictTo=95
// Medium multi-agent (2 agents, 100 KB, 100 BC): limit=139, evictTo=111
// RevClear-scale (4 agents, 300 KB, 200 BC): limit=176, evictTo=140
// Full platform (5 agents, 600 KB, 600 BC): limit=210, evictTo=168
// Theoretical max (5 agents, 900 KB, 1200 BC):limit=250, evictTo=200
//
// The computed profile is cached in project_meta for 10 minutes. This means
// one fast KV lookup per rememberFact() call rather than multiple table scans.
// The cache auto-invalidates: zc_recall_context() always forces a recompute.
// ─────────────────────────────────────────────────────────────────────────────
export interface ComplexityProfile {
kbEntries: number; // source_meta row count
broadcastCount: number; // broadcasts row count
activeAgents: number; // non-revoked, non-expired agent_sessions count
computedLimit: number; // final working memory max
evictTo: number; // eviction target (80% of computedLimit)
computedAt: string; // ISO timestamp — used for cache staleness check
}
const WM_CACHE_TTL_MS = 10 * 60 * 1000; // 10 minutes
/**
* Measure project complexity and derive the working memory limit.
* Stores the result in project_meta for fast subsequent access.
* Always writes a fresh value — call when you want to force a recompute.
*/
export function computeProjectComplexity(db: DatabaseSync): ComplexityProfile {
// Signal 1: KB depth
let kbEntries = 0;
try {
kbEntries = (db.prepare("SELECT COUNT(*) as n FROM source_meta").get() as { n: number }).n;
} catch { /* table may not exist in very old DBs */ }
// Signal 2: Broadcast coordination history
let broadcastCount = 0;
try {
broadcastCount = (db.prepare("SELECT COUNT(*) as n FROM broadcasts").get() as { n: number }).n;
} catch { /* table may not exist */ }
// Signal 3: Active agent density (non-revoked, not-yet-expired sessions)
let activeAgents = 0;
try {
const now = new Date().toISOString();
activeAgents = (db.prepare(
"SELECT COUNT(*) as n FROM agent_sessions WHERE revoked = 0 AND expires_at > ?"
).get(now) as { n: number }).n;
} catch { /* table may not exist */ }
// Derive bonuses — clamped individually to prevent any single signal dominating
const kbBonus = Math.min(Math.floor(kbEntries / 15), 60);
const bcBonus = Math.min(Math.floor(broadcastCount / 30), 40);
const agentBonus = Math.min(activeAgents * 15, 50);
const computedLimit = Math.max(100, Math.min(250, 100 + kbBonus + bcBonus + agentBonus));
const evictTo = Math.floor(computedLimit * 0.80);
const computedAt = new Date().toISOString();
const profile: ComplexityProfile = {
kbEntries, broadcastCount, activeAgents,
computedLimit, evictTo, computedAt,
};
// Cache to project_meta — silently skip if table isn't ready yet
try {
db.prepare(
"INSERT OR REPLACE INTO project_meta(key, value) VALUES (?, ?)"
).run("zc_complexity_profile", JSON.stringify(profile));
} catch { /* migration not yet applied — fallback to Config defaults */ }
return profile;
}
/**
* Return the current working memory limits for this database.
* Uses the cached complexity profile if it exists and is < 10 minutes old.
* Falls back to Config defaults if the cache is missing (first use / fresh DB).
*
* @param forceRecompute Pass true to always recompute (used by zc_recall_context)
*/
export function getWorkingMemoryLimits(
db: DatabaseSync,
forceRecompute = false
): { max: number; evictTo: number; profile: ComplexityProfile | null } {
if (!forceRecompute) {
try {
const row = db.prepare(
"SELECT value FROM project_meta WHERE key = 'zc_complexity_profile'"
).get() as { value: string } | undefined;
if (row) {
const cached = JSON.parse(row.value) as ComplexityProfile;
const ageMs = Date.now() - new Date(cached.computedAt).getTime();
if (ageMs < WM_CACHE_TTL_MS) {
return { max: cached.computedLimit, evictTo: cached.evictTo, profile: cached };
}
}
} catch { /* fall through to recompute */ }
}
// Cache miss, stale, or forced — compute fresh
const profile = computeProjectComplexity(db);
return { max: profile.computedLimit, evictTo: profile.evictTo, profile };
}
function dbPath(projectPath: string): string {
const hash = createHash("sha256").update(projectPath).digest("hex").slice(0, 16);
return join(Config.DB_DIR, `${hash}.db`);
}
function openDb(projectPath: string): DatabaseSync {
mkdirSync(Config.DB_DIR, { recursive: true });
const db = new DatabaseSync(dbPath(projectPath));
db.exec("PRAGMA journal_mode = WAL");
db.exec("PRAGMA busy_timeout = 5000");
// Base working_memory table with agent_id support
db.exec(`
CREATE TABLE IF NOT EXISTS working_memory (
id INTEGER PRIMARY KEY AUTOINCREMENT,
key TEXT NOT NULL,
value TEXT NOT NULL,
importance INTEGER NOT NULL DEFAULT 3,
agent_id TEXT NOT NULL DEFAULT 'default',
created_at TEXT NOT NULL,
UNIQUE(key, agent_id)
);
CREATE INDEX IF NOT EXISTS idx_wm_evict
ON working_memory(agent_id, importance ASC, created_at ASC);
`);
runMigrations(db);
return db;
}
/**
* Ensure agent_id column exists on existing v0.5 databases.
* Safe to call repeatedly — silently ignored if already present.
*/
function ensureAgentIdColumn(db: DatabaseSync): void {
try {
db.exec(`ALTER TABLE working_memory ADD COLUMN agent_id TEXT NOT NULL DEFAULT 'default'`);
} catch {}
}
/**
* v0.31.0 — defensive idempotent ALTERs for the epistemology columns (mirrors
* ensureAgentIdColumn). Migration 31 normally adds these; this guarantees they exist
* even on a mid-session-healed DB, so the extended INSERT/SELECT never fails.
*/
function ensureEpistemologyColumns(db: DatabaseSync): void {
const add = (col: string, ddl: string) => {
try { db.exec(`ALTER TABLE working_memory ADD COLUMN ${col} ${ddl}`); } catch {}
};
add("kind", `TEXT NOT NULL DEFAULT 'fact'`);
add("confidence", `REAL`);
add("resolution_status", `TEXT`);
add("resolved_at", `TEXT`);
add("access_count", `INTEGER NOT NULL DEFAULT 0`);
add("last_retrieved_at", `TEXT`);
add("valid_to", `TEXT`);
add("superseded_by", `TEXT`);
add("retired_reason", `TEXT`);
add("origin", `TEXT`);
add("valid_at", `TEXT`); // M3 event-time
add("invalid_at", `TEXT`); // M3 event-time
add("expires_at", `TEXT`); // R1 TTL
}
/**
* MemGPT operation: WRITE to working memory.
* If the key already exists for this agent, it is updated in place.
* Triggers eviction to archival if working memory is full.
*
* @param agentId Optional agent namespace for parallel multi-agent use (default: "default")
*/
export type Provenance = "EXTRACTED" | "INFERRED" | "AMBIGUOUS" | "UNKNOWN";
/** v0.31.0 — optional epistemic metadata accepted by zc_remember / rememberFact. */
export interface EpistemicOpts {
kind?: MemoryKind;
confidence?: number | null;
resolution?: ResolutionStatus | null;
/** v0.38.0 — per-claim citation: WHAT created the fact ("zc_remember", "compact:<session>",
* "broadcast:REJECT:<task>", …). Surfaced by recall {cite:true} + the dashboard. */
origin?: string | null;
/** R1 (v0.42.0) — per-fact TTL: ISO timestamp after which the fact is excluded from
* recall and retired ('expired', revivable) by the enrichment sweep. Null = never. */
expiresAt?: string | null;
}
/**
* Zero-LLM auto-classifier (Tier-1 B adoption). Infers an epistemic `kind` from the
* fact text so claims get typed even when the agent passes nothing. CONSERVATIVE:
* only upgrades from 'fact' on a clear signal; an explicit `kind` always wins; never
* fabricates confidence. Precedence: prediction > decision > hypothesis > fact.
*/
export function classifyFactKind(value: string): MemoryKind {
const v = ` ${value.toLowerCase()} `;
if (/\b(will|won'?t|going to|gonna|expects?|expected|predicts?|predicted|prediction|by (next|tomorrow|monday|tuesday|wednesday|thursday|friday|q[1-4]|end of)|should (pass|fail|work|break|ship|land|succeed))\b/.test(v)) {
return "prediction";
}
if (/\b(decided|decision|chose|chosen|choosing|going with|opted for|opting for|settled on|we'?ll use|let'?s use|approach is to|plan is to)\b/.test(v)) {
return "decision";
}
if (/\b(might|maybe|perhaps|possibly|suspect|suspected|hypothesis|hypothesi[sz]e|i think|probably|seems? (to|like)|could be)\b/.test(v)) {
return "hypothesis";
}
return "fact";
}
export function rememberFact(
projectPath: string,
key: string,
value: string,
importance: number = 3,
agentId: string = "default",
provenance: Provenance = "EXTRACTED",
epi: EpistemicOpts = {}
): void {
const safeKey = sanitize(key, 100);
const safeValue = sanitize(value, 500);
const safeImp = Math.max(1, Math.min(5, Math.round(importance)));
const safeAgent = sanitize(agentId, 64);
// v0.14.0: provenance flag (Chin & Older 2011 Ch6+Ch7 — every claim
// carries its trust chain). Default EXTRACTED for direct user input
// (the agent typed it deliberately = high trust).
const safeProv: Provenance = (["EXTRACTED", "INFERRED", "AMBIGUOUS", "UNKNOWN"] as const).includes(provenance)
? provenance : "UNKNOWN";
// v0.31.0 epistemology layer. Explicit kind wins; otherwise auto-classify from the
// text (zero-LLM, conservative). confidence/resolution are set ONLY when the caller
// explicitly provides them — the auto-classifier never fabricates them, which keeps
// auto-typed facts OUT of the eviction-protection guard below (that needs explicit intent).
const KINDS = ["fact", "decision", "hypothesis", "prediction"] as const;
const RES = ["open", "resolved_correct", "resolved_incorrect", "resolved_partial"] as const;
const safeKind: MemoryKind = epi.kind && (KINDS as readonly string[]).includes(epi.kind)
? epi.kind : classifyFactKind(safeValue);
const safeConf: number | null = (typeof epi.confidence === "number" && isFinite(epi.confidence))
? Math.max(0, Math.min(1, epi.confidence)) : null;
const safeRes: ResolutionStatus | null = epi.resolution && (RES as readonly string[]).includes(epi.resolution)
? epi.resolution : null;
const resolvedAt: string | null = (safeRes && safeRes !== "open") ? new Date().toISOString() : null;
const now = new Date().toISOString();
const db = openDb(projectPath);
ensureAgentIdColumn(db);
ensureEpistemologyColumns(db);
// ON CONFLICT path: also update provenance + epistemic columns. Re-asserting a
// prediction's key with resolution='resolved_incorrect' RESOLVES it in place —
// this is the resolution mechanism (no separate tool needed).
// R1 — optional TTL: validate ISO, must be in the future; invalid values are dropped.
const safeExpires: string | null = (() => {
if (!epi.expiresAt) return null;
const t = Date.parse(String(epi.expiresAt));
return Number.isFinite(t) && t > Date.now() ? new Date(t).toISOString() : null;
})();
db.prepare(`
INSERT INTO working_memory(key, value, importance, agent_id, created_at, provenance, kind, confidence, resolution_status, resolved_at, origin, expires_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(key, agent_id) DO UPDATE SET
value = excluded.value,
importance = excluded.importance,
created_at = excluded.created_at,
provenance = excluded.provenance,
kind = excluded.kind,
confidence = excluded.confidence,
resolution_status = excluded.resolution_status,
resolved_at = excluded.resolved_at,
origin = excluded.origin,
expires_at = excluded.expires_at,
valid_to = NULL,
superseded_by = NULL,
retired_reason = NULL
`).run(safeKey, safeValue, safeImp, safeAgent, now, safeProv, safeKind, safeConf, safeRes, resolvedAt, epi.origin ? sanitize(epi.origin, 120) : "zc_remember", safeExpires);
// (valid_to reset: re-asserting a RETIRED key REVIVES it — the agent explicitly said it again.)
// v0.36.0 — memory facts are co-reference sources (memory-aware edge extraction), so a
// memory write refreshes the backlink graph. Debounced 5s + fire-and-forget; dynamic
// import keeps the memory↔knowledge↔backlinks module graph cycle-free at load time.
void import("./indexing/backlinks.js").then((m) => m.rebuildBacklinksAsync(projectPath)).catch(() => undefined);
// M1 (v0.41.0) — embed the LIVE fact at remember-time (fire-and-forget, content-hash
// deduped) so focused recall can rank facts by relevance to the agent's current task.
// Same `memory:<agent>:<key>` source the eviction archive uses, so the vector is
// reused if the fact is later archived. Dynamic import: cycle-proof.
void import("./knowledge.js")
.then((m) => m.storeEmbeddingAsync(projectPath, safeValue, `memory:${safeAgent}:${safeKey}`))
.catch(() => undefined);
// Evict if over limit — evict lowest importance + oldest first (MemGPT eviction policy)
// Limit is dynamically sized based on project complexity (see getWorkingMemoryLimits)
// v0.37.0 — retired facts (valid_to set) don't count against the bound and are never
// eviction candidates: they're already out of recall and purged by the enrichment cycle.
const count = (db.prepare(
"SELECT COUNT(*) as n FROM working_memory WHERE agent_id = ? AND valid_to IS NULL"
).get(safeAgent) as { n: number }).n;
const { max: wmMax, evictTo: wmEvictTo } = getWorkingMemoryLimits(db);
if (count > wmMax) {
type Row = { key: string; value: string };
const need = count - wmEvictTo;
// v0.31.0: protect explicitly-tracked OPEN predictions/hypotheses and high-confidence
// decisions from eviction (additive — plain facts match neither clause and evict exactly
// as before; auto-classified facts have no resolution/confidence so they evict normally too).
const PROTECT = `NOT (
(kind IN ('prediction','hypothesis') AND resolution_status = 'open')
OR (kind = 'decision' AND confidence IS NOT NULL AND confidence >= 0.8)
)`;
const toEvict = db.prepare(`
SELECT key, value FROM working_memory
WHERE agent_id = ? AND valid_to IS NULL AND ${PROTECT}
ORDER BY importance ASC, created_at ASC
LIMIT ?
`).all(safeAgent, need) as Row[];
// Safety valve: if protected facts leave us short of the eviction target, fall back to
// the original unfiltered eviction for the remainder so the hard `max` bound always holds.
if (toEvict.length < need) {
const have = new Set(toEvict.map((r) => r.key));
const extra = db.prepare(`
SELECT key, value FROM working_memory
WHERE agent_id = ? AND valid_to IS NULL
ORDER BY importance ASC, created_at ASC
LIMIT ?
`).all(safeAgent, need) as Row[];
for (const r of extra) {
if (toEvict.length >= need) break;
if (!have.has(r.key)) { toEvict.push(r); have.add(r.key); }
}
}
for (const row of toEvict) {
db.prepare("DELETE FROM working_memory WHERE key = ? AND agent_id = ?").run(row.key, safeAgent);
// Archive evicted fact to KB — still findable via zc_search
indexContent(projectPath, row.value, `memory:${safeAgent}:${row.key}`);
}
}
db.close();
}
/**
* MemGPT operation: RECALL working memory.
* Returns all facts for the given agent, ordered by importance (desc).
*
* @param agentId Defaults to "default" (standard single-agent use)
*/
export function recallWorkingMemory(
projectPath: string,
agentId: string = "default"
): MemoryFact[] {
const db = openDb(projectPath);
ensureAgentIdColumn(db);
ensureEpistemologyColumns(db);
const safeAgent = sanitize(agentId, 64);
// v0.22.2 — per-agent namespacing with shared pool. Mirrors the PG path
// in store-postgres.ts. See that file's recall() for full rationale.
let rows: MemoryFact[];
if (safeAgent === "default") {
rows = db.prepare(`
SELECT key, value, importance, agent_id, created_at, kind, confidence, resolution_status, resolved_at, access_count, last_retrieved_at, origin, valid_at
FROM working_memory
WHERE agent_id = 'default' AND valid_to IS NULL
AND (expires_at IS NULL OR expires_at > datetime('now'))
ORDER BY importance DESC, created_at DESC
`).all() as unknown as MemoryFact[];
} else {
rows = db.prepare(`
SELECT key, value, importance, agent_id, created_at, kind, confidence, resolution_status, resolved_at, access_count, last_retrieved_at, origin, valid_at
FROM working_memory
WHERE (agent_id = ? OR agent_id = 'default') AND valid_to IS NULL
AND (expires_at IS NULL OR expires_at > datetime('now'))
ORDER BY
CASE WHEN agent_id = ? THEN 0 ELSE 1 END,
importance DESC,
created_at DESC
`).all(safeAgent, safeAgent) as unknown as MemoryFact[];
}
// Tier-2 #4: fold recency/salience in as a SECONDARY key (importance stays primary),
// then best-effort bump access_count/last_retrieved_at. Fully inert when
// W_SALIENCE=0 — no re-sort, no writes (byte-identical recall + the kill-switch).
// R8 (v0.43.0): the sort key is now EFFECTIVE importance (staleness-demoted; see
// recall_budget.ts — inert when ZC_RECALL_STALE_DEMOTE=0), and the access bump
// covers only the facts that will RENDER under the recall budget. Bumping every
// returned row (the old behaviour) reset last_retrieved_at project-wide on every
// recall, which made "stale" undetectable — rehearsal must be selective to decay.
const demoteStale = Config.RECALL_STALE_DEMOTE > 0;
if ((salienceEnabled() || demoteStale) && rows.length > 0) {
const now = Date.now();
const k = (r: MemoryFact) => `${r.key}\u0000${r.agent_id ?? ""}`;
const sal = salienceEnabled()
? new Map(rows.map((r) => [k(r), computeSalience(r.access_count, r.last_retrieved_at, now)]))
: null;
const prio = (r: MemoryFact) => (safeAgent !== "default" && r.agent_id === safeAgent ? 0 : 1);
const eff = (r: MemoryFact) => (demoteStale ? effectiveImportance(r, now) : r.importance);
rows = [...rows].sort((a, b) =>
prio(a) - prio(b) ||
eff(b) - eff(a) ||
(sal ? (sal.get(k(b)) ?? 0) - (sal.get(k(a)) ?? 0) : 0) ||
(a.created_at < b.created_at ? 1 : a.created_at > b.created_at ? -1 : 0)
);
if (salienceEnabled()) {
try {
// R8 — bump ONLY the facts that will render under the recall budget.
// Bumping every returned row reset last_retrieved_at project-wide on each
// recall, which made staleness undetectable; selective rehearsal lets
// collapsed facts genuinely decay while surfaced facts stay fresh.
const toBump = budgetFacts(rows).rendered;
const nowIso = new Date().toISOString();
const bump = db.prepare(`UPDATE working_memory SET access_count = COALESCE(access_count,0) + 1, last_retrieved_at = ? WHERE key = ? AND agent_id = ?`);
db.exec("BEGIN");
for (const r of toBump) bump.run(nowIso, r.key, r.agent_id ?? safeAgent);
db.exec("COMMIT");
} catch { try { db.exec("ROLLBACK"); } catch { /* no-op */ } /* pre-migration DB — skip */ }
}
}
db.close();
return rows;
}
/**
* M1 (v0.41.0) — FOCUSED recall: re-rank live working-memory facts by blended
* relevance to the agent's CURRENT task instead of raw importance.
*
* Why: importance-ordered recall answers "what are this project's most
* important facts", not "which facts matter for what I'm doing right now" —
* the M0 benchmark showed task-relevant facts ranking 74-79th of 81 behind
* high-importance work-log noise. With a focus string, ordering becomes
* score = RECALL_W_REL·cosine(focus, fact) + RECALL_W_IMP·(importance/5)
* + RECALL_W_SAL·salience
* Facts without a stored vector fall back to rel=0 (they still rank via
* importance — graceful before the embedding backfill completes). If the
* focus embedding itself fails (Ollama down), the unfocused order is
* returned unchanged. Without focus, behaviour is byte-identical.
*/
export async function recallWorkingMemoryFocused(
projectPath: string,
agentId: string,
focus: string,
win: { from?: Date; to?: Date; asOf?: Date } = {}, // M3 — temporal window / as-of
): Promise<MemoryFact[]> {
let rows: MemoryFact[];
if (win.asOf) {
// M3 — AS-OF time travel over the transaction timeline: what was live THEN
// (includes facts retired since; excludes facts created after).
const db = openDb(projectPath);
try {
ensureAgentIdColumn(db);
ensureEpistemologyColumns(db);
const safeAgent = sanitize(agentId, 64);
const iso = win.asOf.toISOString();
rows = db.prepare(`
SELECT key, value, importance, agent_id, created_at, kind, confidence, resolution_status, resolved_at, access_count, last_retrieved_at, origin, valid_at
FROM working_memory
WHERE (agent_id = ? OR agent_id = 'default')
AND created_at <= ? AND (valid_to IS NULL OR valid_to > ?)
ORDER BY importance DESC, created_at DESC
`).all(safeAgent, iso, iso) as unknown as MemoryFact[];
} finally { db.close(); }
} else if (win.from || win.to) {
// S1 (v0.44.0) — historical WINDOW queries include RETIRED facts whose
// event-time falls inside the window (mirrors store-postgres.ts): once
// auto-supersession retires a stale fact, "what did we decide three weeks
// ago?" must still surface it — it was the truth THEN.
const db = openDb(projectPath);
try {
ensureAgentIdColumn(db);
ensureEpistemologyColumns(db);
const safeAgent = sanitize(agentId, 64);
const conds: string[] = [];
const winParams: string[] = [];
if (win.from) { conds.push(`COALESCE(valid_at, created_at) >= ?`); winParams.push(win.from.toISOString()); }
if (win.to) { conds.push(`COALESCE(valid_at, created_at) <= ?`); winParams.push(win.to.toISOString()); }
rows = db.prepare(`
SELECT key, value, importance, agent_id, created_at, kind, confidence, resolution_status, resolved_at, access_count, last_retrieved_at, origin, valid_at
FROM working_memory
WHERE (agent_id = ? OR agent_id = 'default')
AND ((valid_to IS NULL AND (expires_at IS NULL OR expires_at > datetime('now')))
OR (valid_to IS NOT NULL AND ${conds.join(" AND ")}))
ORDER BY importance DESC, created_at DESC
`).all(safeAgent, ...winParams) as unknown as MemoryFact[];
} finally { db.close(); }
} else {
rows = recallWorkingMemory(projectPath, agentId);
}
if (!focus.trim() || rows.length === 0) return rows;
const { getEmbedding, cosineSimilarity, deserializeVector, ACTIVE_MODEL } = await import("./embedder.js");
const qEmbed = await getEmbedding(focus.slice(0, 2000));
if (!qEmbed) return rows; // Ollama down — degrade to unfocused order
// One batched read of the live facts' vectors (source = memory:<agent>:<key>).
const vecMap = new Map<string, Float32Array>();
try {
const db = openDb(projectPath);
try {
const sources = rows.map((r) => `memory:${r.agent_id ?? agentId}:${r.key}`);
const ph = sources.map(() => "?").join(",");
const embRows = db.prepare(
`SELECT source, vector FROM embeddings WHERE model_name = ? AND source IN (${ph})`
).all(ACTIVE_MODEL, ...sources) as Array<{ source: string; vector: Buffer }>;
for (const e of embRows) vecMap.set(e.source, deserializeVector(e.vector));
} finally { db.close(); }
} catch { /* embeddings table absent — rel=0 for all, order falls back to importance */ }
const now = Date.now();
const scored = rows.map((r) => {
const v = vecMap.get(`memory:${r.agent_id ?? agentId}:${r.key}`);
const rel = v ? Math.max(0, cosineSimilarity(qEmbed.vector, v)) : 0;
const sal = computeSalience(r.access_count, r.last_retrieved_at, now);
let score = Config.RECALL_W_REL * rel + Config.RECALL_W_IMP * (r.importance / 5) + Config.RECALL_W_SAL * sal;
// M3 — temporal window bonus (event-time valid_at falls back to created_at)
if (win.from || win.to) {
const evRaw = (r as MemoryFact & { valid_at?: string | null }).valid_at ?? r.created_at;
const ev = Date.parse(String(evRaw));
const inWindow = Number.isFinite(ev) &&
(!win.from || ev >= win.from.getTime()) &&
(!win.to || ev <= win.to.getTime());
// R3 — flat bonus by default; gate only when explicitly configured
// (see store-postgres.ts for the measured rationale).
if (inWindow) {
score += Config.RECALL_TEMPORAL_REL_GATE > 0
? (rel >= Config.RECALL_TEMPORAL_REL_GATE ? Config.RECALL_W_TEMPORAL : 0)
: Config.RECALL_W_TEMPORAL;
}
}
return { r, score };
});
scored.sort((a, b) =>
b.score - a.score ||
b.r.importance - a.r.importance ||
(a.r.created_at < b.r.created_at ? 1 : a.r.created_at > b.r.created_at ? -1 : 0)
);
// S1 (v0.44.0) — prefer-latest: a near-identical conflicting pair among the top
// candidates demotes the OLDER fact below the newer (the un-retired stale update
// problem — bench KU decoy outranked its own update 100% of the time). Skipped
// for temporal/as-of queries: "what was it three weeks ago" wants the old fact.
if (Config.PREFER_LATEST && !win.from && !win.to && !win.asOf && scored.length > 1) {
const { preferLatestAdjust } = await import("./contradiction_heuristics.js");
const evOf = (r: MemoryFact): number => {
const raw = (r as MemoryFact & { valid_at?: string | null }).valid_at ?? r.created_at;
return Date.parse(String(raw));
};
// Fixpoint loop (mirrors store-postgres.ts): demoting a stale duplicate frees
// top-K slots that can expose NEW conflicting pairs — re-slice and re-run
// until a pass adjusts nothing (≤3 passes).
for (let pass = 0; pass < 3; pass++) {
const top = scored.slice(0, Config.PREFER_LATEST_TOPK).map((x) => ({
fact: x.r,
score: x.score,
vec: vecMap.get(`memory:${x.r.agent_id ?? agentId}:${x.r.key}`),
ev: evOf(x.r),
}));
const adjusted = preferLatestAdjust(top, cosineSimilarity, Config.PREFER_LATEST_MARGIN);
if (adjusted.size === 0) break;
for (const s of scored) {
const adj = adjusted.get(s.r.key);
if (adj !== undefined && adj < s.score) s.score = adj;
}
scored.sort((a, b) =>
b.score - a.score ||
b.r.importance - a.r.importance ||
(a.r.created_at < b.r.created_at ? 1 : a.r.created_at > b.r.created_at ? -1 : 0)
);
}
}
return scored.map((x) => x.r);
}
/**
* MemGPT operation: ARCHIVE SESSION SUMMARY.
* Stored with 'summary' retention tier — kept for 365 days.
*/
export function archiveSessionSummary(projectPath: string, summary: string): void {
const safeSummary = sanitize(summary, 2000);
const now = new Date().toISOString();
const source = `[SESSION_SUMMARY] ${now.slice(0, 10)}`;
// Write to archival KB with summary retention tier (365 days)
indexContent(projectPath, safeSummary, source, "internal", "summary");
// Also keep as high-importance working memory for the next session
rememberFact(projectPath, "last_session_summary", safeSummary, 5);
}
/**
* MemGPT operation: DELETE from working memory.
* Returns true if the key existed and was deleted.
*/
export function forgetFact(
projectPath: string,
key: string,
agentId: string = "default"
): boolean {
const safeKey = sanitize(key, 100);
const safeAgent = sanitize(agentId, 64);
const db = openDb(projectPath);
ensureAgentIdColumn(db);
// v0.38.0 — SOFT DELETE with a recovery window: forget RETIRES the fact (valid_to set,
// archived to the KB, out of recall immediately) instead of hard-deleting it. Recoverable
// via reviveFact / the dashboard for RETIRE_PURGE_DAYS, after which the enrichment cycle
// purges the tombstone (the KB archive remains searchable forever).
db.close();
return retireFact(projectPath, safeKey, safeAgent, null, "forgotten");
}
/**
* v0.37.0 — RETIRE a fact (temporal close-out, NOT deletion): sets valid_to so it drops
* out of recall/stats/eviction/scans, records what superseded it and why, and archives
* the value to the KB (still findable via zc_search, recoverable via reviveFact). Used by
* contradiction auto-resolution and the dashboard Keep-left/Keep-right actions. The row
* itself is purged to archival-only by the enrichment cycle after RETIRE_PURGE_DAYS.
*/
export function retireFact(
projectPath: string,
key: string,
agentId: string,
supersededBy: string | null,
reason: string,
): boolean {
const safeKey = sanitize(key, 100);
const safeAgent = sanitize(agentId, 64);
const db = openDb(projectPath);
ensureAgentIdColumn(db);
ensureEpistemologyColumns(db);
const row = db.prepare(
"SELECT value FROM working_memory WHERE key = ? AND agent_id = ? AND valid_to IS NULL"
).get(safeKey, safeAgent) as { value: string } | undefined;
if (!row) { db.close(); return false; }
db.prepare(
"UPDATE working_memory SET valid_to = ?, superseded_by = ?, retired_reason = ? WHERE key = ? AND agent_id = ?"
).run(new Date().toISOString(), supersededBy ? sanitize(supersededBy, 100) : null, sanitize(reason, 100), safeKey, safeAgent);
db.close();
// Archive to KB (same naming as eviction) + refresh graph edges.
try { indexContent(projectPath, row.value, `memory:${safeAgent}:${safeKey}`); } catch { /* best-effort */ }
void import("./indexing/backlinks.js").then((m) => m.rebuildBacklinksAsync(projectPath)).catch(() => undefined);
return true;
}
/** v0.37.0 — undo a retirement (dashboard Undo): clears valid_to so the fact is live again. */
export function reviveFact(projectPath: string, key: string, agentId: string): boolean {
const safeKey = sanitize(key, 100);
const safeAgent = sanitize(agentId, 64);
const db = openDb(projectPath);
ensureAgentIdColumn(db);
ensureEpistemologyColumns(db);
const r = db.prepare(
"UPDATE working_memory SET valid_to = NULL, superseded_by = NULL, retired_reason = NULL WHERE key = ? AND agent_id = ? AND valid_to IS NOT NULL"
).run(safeKey, safeAgent) as { changes: number };
db.close();
if (r.changes > 0) {
void import("./indexing/backlinks.js").then((m) => m.rebuildBacklinksAsync(projectPath)).catch(() => undefined);
}
return r.changes > 0;
}
/**
* Format working memory for context injection.
* Returns a structured, token-efficient representation with priority sections.
*
* @param max Dynamic working memory limit (from getWorkingMemoryLimits). Defaults to Config value if omitted.
*/
export function formatWorkingMemoryForContext(
facts: MemoryFact[],
agentId: string = "default",
max: number = Config.WORKING_MEMORY_MAX,
cite: boolean = false, // v0.38.0 — per-claim citation chips (opt-in; recall stays lean by default)
focused: boolean = false, // M1 (v0.41.0) — facts arrive RELEVANCE-ordered; render flat, don't regroup by ★
win?: TemporalWindow, // R8 (v0.43.0) — parsed time window from the focus: in-window facts get tier-1 priority under the budget
): string {
if (facts.length === 0) return "## Working Memory\nEmpty — no facts stored yet.";
// R8 (v0.43.0) — recall output budget. Measured on a mature project: 237 facts
// rendered ~47k tokens and agents started spawning subagents to "digest" the
// recall. Top-ranked facts render fully; the tail collapses into a grouped,
// retrievable index. Small projects fit entirely → byte-identical output.
const budget = budgetFacts(facts, { win });
const shown = budget.rendered;
const critical = shown.filter((f) => f.importance >= 4);
const normal = shown.filter((f) => f.importance === 3);
const ephemeral = shown.filter((f) => f.importance <= 2);
const headCount = budget.collapsed.length > 0
? `${facts.length}/${max} facts · top ${shown.length} rendered`
: `${facts.length}/${max} facts`;
const lines: string[] = [
`## Working Memory (${headCount}${agentId !== "default" ? ` · agent: ${agentId}` : ""}${focused ? " · ranked by task relevance" : ""})`,
];
// v0.31.0: plain facts render byte-identical; non-fact / resolved claims get an inline badge.
const citeChip = (f: MemoryFact): string => {
if (!cite) return "";
const d = f.created_at ? String(f.created_at).slice(0, 10) : "?";
return ` 〔${f.agent_id ?? agentId} · ${d}${f.origin ? ` · ${f.origin}` : ""}〕`;
};
const fmtFact = (f: MemoryFact): string => {
const base = ` [★${f.importance}] ${f.key}: ${f.value}`;
if ((!f.kind || f.kind === "fact") && !f.resolution_status) return base + citeChip(f);
const tags: string[] = [];
if (f.kind && f.kind !== "fact") tags.push(f.kind);
if (f.confidence != null) tags.push(`p=${f.confidence.toFixed(2)}`);
if (f.resolution_status === "open") tags.push("⏳ open");
else if (f.resolution_status === "resolved_correct") tags.push("✓ correct");
else if (f.resolution_status === "resolved_incorrect") tags.push("✗ incorrect");
else if (f.resolution_status === "resolved_partial") tags.push("~ partial");
return (tags.length ? `${base} ⟨${tags.join(" · ")}⟩` : base) + citeChip(f);
};
// M1 — focused recall is RELEVANCE-ordered top-to-bottom; regrouping into ★ tiers
// would destroy exactly the ordering the caller asked for. Render flat instead.
if (focused) {
for (const f of shown) lines.push(fmtFact(f));
if (budget.tailNotice) lines.push(budget.tailNotice);
return lines.join("\n");
}
if (critical.length > 0) {
lines.push("\n**Critical [★4-5]**");
for (const f of critical) lines.push(fmtFact(f));
}
if (normal.length > 0) {
lines.push("\n**Normal [★3]**");
for (const f of normal) lines.push(fmtFact(f));
}
if (ephemeral.length > 0) {
lines.push("\n**Ephemeral [★1-2]**");
for (const f of ephemeral) lines.push(fmtFact(f));
}
if (budget.tailNotice) lines.push(budget.tailNotice);
return lines.join("\n");
}
/**
* R8c (v0.43.0) — count live importance-5 facts in a namespace. Used by the
* zc_remember soft-quota nudge: beyond Config.IMP5_SOFT_CAP the tool response
* warns (never blocks) that "critical" is being diluted. Measured trigger: a
* mature project had 207/237 facts at ★5 — when everything is critical,
* eviction and ranking lose all discriminating power.
*/
export function countImportance5(projectPath: string, agentId: string = "default"): number {
const db = openDb(projectPath);
const safeAgent = sanitize(agentId, 64);
try {
return (db.prepare(
"SELECT COUNT(*) as n FROM working_memory WHERE agent_id = ? AND importance = 5 AND valid_to IS NULL"
).get(safeAgent) as { n: number }).n;
} catch {
return 0; // pre-migration DB — nudge silently disabled
} finally {
db.close();
}
}
/** Returns working memory stats for the zc_status tool, including dynamic limit and complexity profile */
export function getMemoryStats(
projectPath: string,
agentId: string = "default"
): { count: number; max: number; evictTo: number; criticalCount: number; complexity: ComplexityProfile | null } {
const db = openDb(projectPath);
const safeAgent = sanitize(agentId, 64);
const count = (db.prepare(
"SELECT COUNT(*) as n FROM working_memory WHERE agent_id = ? AND valid_to IS NULL"
).get(safeAgent) as { n: number }).n;
const criticalCount = (db.prepare(
"SELECT COUNT(*) as n FROM working_memory WHERE agent_id = ? AND importance >= 4 AND valid_to IS NULL"
).get(safeAgent) as { n: number }).n;
const { max, evictTo, profile } = getWorkingMemoryLimits(db);
db.close();
return { count, max, evictTo, criticalCount, complexity: profile };
}
// ─────────────────────────────────────────────────────────────────────────────
// A2A SHARED BROADCAST CHANNEL (Phase 2 — security-hardened in v0.7.1)
//
// Architecture (Chin & Older 2011 access control principles):
//
// BIBA INTEGRITY: No-write-up — worker agents cannot push to the shared
// channel without the channel key (a capability token).
// Orchestrators hold the key; workers do not.
//
// BELL-LA PADULA: No-read-up — private working_memory facts are invisible
// to other agents. Broadcasts are explicitly public.
//
// REFERENCE MONITOR: broadcastFact() is the single enforcement point. Every
// shared write goes through key verification here.
//
// LEAST PRIVILEGE: Default visibility = private (working_memory only).
// Shared broadcast requires explicit channel_key capability.
//
// NON-TRANSITIVE DELEGATION: Workers can READ broadcasts but cannot
// re-broadcast as orchestrator (key is never returned to
// caller; comparison is constant-time only against stored hash).
//
// CHANNEL KEY STORAGE: scrypt(key, randomSalt, N=65536, r=8, p=1) → stored as
// "scrypt:v1:{N}:{r}:{p}:{salt_hex}:{hash_hex}" in project_meta.
// Raw plaintext key NEVER persisted. Salt is 32 random bytes, unique per set_key call.
// Offline brute force: ~10¹² guesses/sec on GPU cluster → impractical for ≥16-char keys.
//
// VERIFICATION CACHE: scryptSync blocks ~100ms per call. A session-scoped HMAC cache
// ensures only the FIRST broadcastFact call per project pays the KDF cost.
// Subsequent calls verify in <1ms via HMAC comparison against a session secret.
// Cache is in-process memory only — never persisted or readable by sandboxed code.
//
// INJECTION DEFENSE: Worker-written summaries (STATUS, PROPOSED, DEPENDENCY) are
// labeled ⚠ [UNVERIFIED WORKER CONTENT] when injected into agent context.
// Orchestrator-issued types (ASSIGN, MERGE, REJECT, REVISE) are trusted by
// construction (require the capability key in key-protected mode).
// ─────────────────────────────────────────────────────────────────────────────
// Valid broadcast types — drives CHECK constraint in DB schema too
export type BroadcastType =
| "ASSIGN" // orchestrator assigns a task to an agent
| "STATUS" // agent reports current work state
| "PROPOSED" // agent proposes file changes pending review
| "DEPENDENCY" // agent declares it depends on another agent's output
| "MERGE" // orchestrator approves and merges proposed changes
| "REJECT" // orchestrator rejects proposed changes
| "REVISE" // orchestrator requests revision of proposed changes
| "LAUNCH_ROLE" // orchestrator requests dispatcher to spawn a new agent role
| "RETIRE_ROLE"; // orchestrator requests dispatcher to retire an agent role
// Worker-originated types whose summaries are labeled [UNVERIFIED WORKER CONTENT]
// in formatted context output. Orchestrator types are trusted by construction.
const WORKER_TYPES: ReadonlySet<BroadcastType> = new Set<BroadcastType>([
"STATUS", "PROPOSED", "DEPENDENCY",
]);
export interface BroadcastMessage {
id: number;
type: BroadcastType;
agent_id: string;
task: string;
files: string[]; // parsed JSON array of affected file paths
state: string;
summary: string;
depends_on: string[]; // parsed JSON array of agent_ids this depends on
reason: string;
importance: number;
created_at: string;
// v0.15.0/v0.16.0 §8.1 — structured ASSIGN fields (NULLABLE for non-ASSIGN broadcasts)
acceptance_criteria?: string[];
complexity_estimate?: number | null;
file_ownership_exclusive?: string[];
file_ownership_read_only?: string[];
task_dependencies?: number[];
required_skills?: string[];
estimated_tokens?: number | null;
}
export interface BroadcastResult {
id: number;
type: BroadcastType;
agent_id: string;
task: string;
files: string[];
state: string;
summary: string;
depends_on: string[];
reason: string;
importance: number;
created_at: string;
// v0.15.0 §8.1 — structured ASSIGN fields. All NULLABLE/empty for
// non-ASSIGN broadcasts and for legacy clients that don't supply them.
acceptance_criteria?: string[];
complexity_estimate?: number | null;
file_ownership_exclusive?: string[];
file_ownership_read_only?: string[];
task_dependencies?: number[];
required_skills?: string[];
estimated_tokens?: number | null;
}
// ── Scrypt KDF constants (from Config — repeated here for inline readability) ──
const SCRYPT_PREFIX = "scrypt:v1";
/**
* SECURITY: Hash a channel key using scrypt KDF with a random salt.
* Returns a versioned string: "scrypt:v1:{N}:{r}:{p}:{salt_hex}:{hash_hex}"