@aletheia-labs/core 0.1.0

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.
Files changed (119) hide show
  1. package/LICENSE +176 -0
  2. package/README.md +163 -0
  3. package/dist/index.d.ts +18 -0
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/index.js +21 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/runtime/action-authorizer.d.ts +48 -0
  8. package/dist/runtime/action-authorizer.d.ts.map +1 -0
  9. package/dist/runtime/action-authorizer.js +231 -0
  10. package/dist/runtime/action-authorizer.js.map +1 -0
  11. package/dist/runtime/authority-engine.d.ts +68 -0
  12. package/dist/runtime/authority-engine.d.ts.map +1 -0
  13. package/dist/runtime/authority-engine.js +99 -0
  14. package/dist/runtime/authority-engine.js.map +1 -0
  15. package/dist/runtime/decision-helpers.d.ts +17 -0
  16. package/dist/runtime/decision-helpers.d.ts.map +1 -0
  17. package/dist/runtime/decision-helpers.js +23 -0
  18. package/dist/runtime/decision-helpers.js.map +1 -0
  19. package/dist/runtime/index.d.ts +9 -0
  20. package/dist/runtime/index.d.ts.map +1 -0
  21. package/dist/runtime/index.js +9 -0
  22. package/dist/runtime/index.js.map +1 -0
  23. package/dist/runtime/proposal-safety.d.ts +18 -0
  24. package/dist/runtime/proposal-safety.d.ts.map +1 -0
  25. package/dist/runtime/proposal-safety.js +76 -0
  26. package/dist/runtime/proposal-safety.js.map +1 -0
  27. package/dist/runtime/retrieval-router.d.ts +60 -0
  28. package/dist/runtime/retrieval-router.d.ts.map +1 -0
  29. package/dist/runtime/retrieval-router.js +223 -0
  30. package/dist/runtime/retrieval-router.js.map +1 -0
  31. package/dist/runtime/scope-helpers.d.ts +22 -0
  32. package/dist/runtime/scope-helpers.d.ts.map +1 -0
  33. package/dist/runtime/scope-helpers.js +29 -0
  34. package/dist/runtime/scope-helpers.js.map +1 -0
  35. package/dist/runtime/visibility-policy.d.ts +29 -0
  36. package/dist/runtime/visibility-policy.d.ts.map +1 -0
  37. package/dist/runtime/visibility-policy.js +23 -0
  38. package/dist/runtime/visibility-policy.js.map +1 -0
  39. package/dist/runtime/write-gate.d.ts +66 -0
  40. package/dist/runtime/write-gate.d.ts.map +1 -0
  41. package/dist/runtime/write-gate.js +293 -0
  42. package/dist/runtime/write-gate.js.map +1 -0
  43. package/dist/storage/conflict-registry.d.ts +61 -0
  44. package/dist/storage/conflict-registry.d.ts.map +1 -0
  45. package/dist/storage/conflict-registry.js +15 -0
  46. package/dist/storage/conflict-registry.js.map +1 -0
  47. package/dist/storage/event-ledger.d.ts +61 -0
  48. package/dist/storage/event-ledger.d.ts.map +1 -0
  49. package/dist/storage/event-ledger.js +14 -0
  50. package/dist/storage/event-ledger.js.map +1 -0
  51. package/dist/storage/index.d.ts +8 -0
  52. package/dist/storage/index.d.ts.map +1 -0
  53. package/dist/storage/index.js +8 -0
  54. package/dist/storage/index.js.map +1 -0
  55. package/dist/storage/memory-store.d.ts +94 -0
  56. package/dist/storage/memory-store.d.ts.map +1 -0
  57. package/dist/storage/memory-store.js +14 -0
  58. package/dist/storage/memory-store.js.map +1 -0
  59. package/dist/types/action.d.ts +211 -0
  60. package/dist/types/action.d.ts.map +1 -0
  61. package/dist/types/action.js +50 -0
  62. package/dist/types/action.js.map +1 -0
  63. package/dist/types/compressed-receipt.d.ts +117 -0
  64. package/dist/types/compressed-receipt.d.ts.map +1 -0
  65. package/dist/types/compressed-receipt.js +100 -0
  66. package/dist/types/compressed-receipt.js.map +1 -0
  67. package/dist/types/conflict.d.ts +159 -0
  68. package/dist/types/conflict.d.ts.map +1 -0
  69. package/dist/types/conflict.js +47 -0
  70. package/dist/types/conflict.js.map +1 -0
  71. package/dist/types/coverage.d.ts +52 -0
  72. package/dist/types/coverage.d.ts.map +1 -0
  73. package/dist/types/coverage.js +30 -0
  74. package/dist/types/coverage.js.map +1 -0
  75. package/dist/types/decision.d.ts +345 -0
  76. package/dist/types/decision.d.ts.map +1 -0
  77. package/dist/types/decision.js +88 -0
  78. package/dist/types/decision.js.map +1 -0
  79. package/dist/types/enums.d.ts +135 -0
  80. package/dist/types/enums.d.ts.map +1 -0
  81. package/dist/types/enums.js +146 -0
  82. package/dist/types/enums.js.map +1 -0
  83. package/dist/types/event.d.ts +187 -0
  84. package/dist/types/event.d.ts.map +1 -0
  85. package/dist/types/event.js +44 -0
  86. package/dist/types/event.js.map +1 -0
  87. package/dist/types/human-receipt.d.ts +108 -0
  88. package/dist/types/human-receipt.d.ts.map +1 -0
  89. package/dist/types/human-receipt.js +41 -0
  90. package/dist/types/human-receipt.js.map +1 -0
  91. package/dist/types/index.d.ts +25 -0
  92. package/dist/types/index.d.ts.map +1 -0
  93. package/dist/types/index.js +25 -0
  94. package/dist/types/index.js.map +1 -0
  95. package/dist/types/keys.d.ts +31 -0
  96. package/dist/types/keys.d.ts.map +1 -0
  97. package/dist/types/keys.js +53 -0
  98. package/dist/types/keys.js.map +1 -0
  99. package/dist/types/memory-atom.d.ts +291 -0
  100. package/dist/types/memory-atom.d.ts.map +1 -0
  101. package/dist/types/memory-atom.js +62 -0
  102. package/dist/types/memory-atom.js.map +1 -0
  103. package/dist/types/memory-proposal.d.ts +207 -0
  104. package/dist/types/memory-proposal.d.ts.map +1 -0
  105. package/dist/types/memory-proposal.js +34 -0
  106. package/dist/types/memory-proposal.js.map +1 -0
  107. package/dist/types/packet.d.ts +564 -0
  108. package/dist/types/packet.d.ts.map +1 -0
  109. package/dist/types/packet.js +43 -0
  110. package/dist/types/packet.js.map +1 -0
  111. package/dist/types/primitives.d.ts +39 -0
  112. package/dist/types/primitives.d.ts.map +1 -0
  113. package/dist/types/primitives.js +42 -0
  114. package/dist/types/primitives.js.map +1 -0
  115. package/dist/types/status-transitions.d.ts +23 -0
  116. package/dist/types/status-transitions.d.ts.map +1 -0
  117. package/dist/types/status-transitions.js +48 -0
  118. package/dist/types/status-transitions.js.map +1 -0
  119. package/package.json +57 -0
@@ -0,0 +1,223 @@
1
+ /**
2
+ * RetrievalRouter — authority-first recall.
3
+ *
4
+ * This is deliberately not semantic search. Routing order is permission,
5
+ * status/scope/type, conflict state, then optional host-provided exact topic
6
+ * matching. Without a topic matcher, topic queries fail closed.
7
+ */
8
+ import { RecallQuerySchema } from '../types/index.js';
9
+ import { SYSTEM_CLOCK, decision } from './decision-helpers.js';
10
+ import { DENY_ALL_VISIBILITY_POLICY } from './visibility-policy.js';
11
+ const DEFAULT_RECALL_STATUSES = ['verified', 'trusted'];
12
+ export class RetrievalRouter {
13
+ options;
14
+ visibilityPolicy;
15
+ clock;
16
+ /**
17
+ * Create an authority-first recall router.
18
+ *
19
+ * @remarks
20
+ * The optional topic matcher is an exact host-provided predicate, not an
21
+ * embedding or semantic ranking hook. If a topic query is supplied without a
22
+ * matcher, recall fails closed.
23
+ */
24
+ constructor(options) {
25
+ this.options = options;
26
+ this.visibilityPolicy = options.visibilityPolicy ?? DENY_ALL_VISIBILITY_POLICY;
27
+ this.clock = options.clock ?? SYSTEM_CLOCK;
28
+ }
29
+ /**
30
+ * Recall atoms that are visible, in scope, currently valid, and actionable.
31
+ *
32
+ * @remarks
33
+ * Implementation order is permission before semantics: caller visibility is
34
+ * resolved first, then the store query applies scope/status/freshness, then
35
+ * optional type/topic filtering, then conflict checks. The returned decision
36
+ * must be inspected before passing atoms to a model.
37
+ */
38
+ async recall(query) {
39
+ const emittedAt = this.clock.now();
40
+ const queryResult = RecallQuerySchema.safeParse(query);
41
+ if (!queryResult.success) {
42
+ return {
43
+ decision: decision('fetch_abstain', [
44
+ {
45
+ kind: 'tuple_incomplete',
46
+ missingFields: schemaIssuePaths(queryResult.error.issues, 'query'),
47
+ },
48
+ ], [], [], emittedAt),
49
+ atoms: [],
50
+ conflicts: [],
51
+ };
52
+ }
53
+ const validQuery = queryResult.data;
54
+ const permitted = this.visibilityPolicy.permittedVisibilitiesForAgent(validQuery.agentId);
55
+ if (permitted.length === 0) {
56
+ return {
57
+ decision: decision('fetch_abstain', [
58
+ {
59
+ kind: 'visibility_denied',
60
+ detail: 'caller has no permitted visibility planes',
61
+ },
62
+ ], [], [], emittedAt),
63
+ atoms: [],
64
+ conflicts: [],
65
+ };
66
+ }
67
+ if (validQuery.topic !== undefined && this.options.topicMatcher === undefined) {
68
+ return {
69
+ decision: decision('fetch_abstain', [{ kind: 'tuple_incomplete', missingFields: ['topicIndex'] }], [], [], emittedAt),
70
+ atoms: [],
71
+ conflicts: [],
72
+ };
73
+ }
74
+ const loadedAtoms = await this.loadAtoms(validQuery, permitted, emittedAt);
75
+ if (!loadedAtoms.ok) {
76
+ return {
77
+ decision: decision('fetch_abstain', [loadedAtoms.reason], [], [], emittedAt),
78
+ atoms: [],
79
+ conflicts: [],
80
+ };
81
+ }
82
+ const atoms = loadedAtoms.atoms;
83
+ if (atoms.length === 0) {
84
+ return {
85
+ decision: decision('fetch_abstain', [{ kind: 'tuple_incomplete', missingFields: ['memoryAtoms'] }], [], [], emittedAt),
86
+ atoms: [],
87
+ conflicts: [],
88
+ };
89
+ }
90
+ const nonActionableReason = firstNonActionableReason(atoms);
91
+ if (nonActionableReason !== null) {
92
+ return {
93
+ decision: decision(nonActionableReason.outcome, [nonActionableReason.reason], atoms.map((atom) => atom.memoryId), [], emittedAt),
94
+ atoms,
95
+ conflicts: [],
96
+ };
97
+ }
98
+ const memoryIds = atoms.map((atom) => atom.memoryId);
99
+ const conflicts = await this.options.conflictRegistry.query({
100
+ touchingMemoryIds: memoryIds,
101
+ statuses: ['unresolved', 'requires_human'],
102
+ scope: validQuery.scope,
103
+ permittedVisibilities: permitted,
104
+ });
105
+ if (conflicts.length > 0) {
106
+ return {
107
+ decision: decision('conflict_boundary_packet', conflicts.map((conflict) => ({
108
+ kind: 'unresolved_conflict',
109
+ conflictId: conflict.conflictId,
110
+ })), memoryIds, conflicts.map((conflict) => conflict.conflictId), emittedAt),
111
+ atoms,
112
+ conflicts,
113
+ };
114
+ }
115
+ return {
116
+ decision: decision('allow_local_shadow', [{ kind: 'all_checks_passed', citedMemoryIds: memoryIds }], memoryIds, [], emittedAt),
117
+ atoms,
118
+ conflicts,
119
+ };
120
+ }
121
+ async loadAtoms(query, permittedVisibilities, validAt) {
122
+ const atoms = await this.options.memoryStore.query({
123
+ statuses: query.requiredStatus ?? DEFAULT_RECALL_STATUSES,
124
+ scope: query.scope,
125
+ permittedVisibilities,
126
+ validAt,
127
+ });
128
+ const filtered = atoms.filter((atom) => {
129
+ if (query.memoryTypes !== undefined && !query.memoryTypes.includes(atom.memoryType)) {
130
+ return false;
131
+ }
132
+ if (query.topic !== undefined) {
133
+ return this.options.topicMatcher?.(atom, query.topic) ?? false;
134
+ }
135
+ return true;
136
+ });
137
+ const lineageFiltered = omitSupersededAtoms(filtered);
138
+ const rankedResult = this.options.authorityScorer === undefined
139
+ ? { ok: true, atoms: lineageFiltered }
140
+ : rankByAuthority(lineageFiltered, this.options.authorityScorer, validAt);
141
+ if (!rankedResult.ok) {
142
+ return {
143
+ ok: false,
144
+ reason: {
145
+ kind: 'promotion_boundary_blocked',
146
+ detail: `authority scorer failed: ${rankedResult.error}`,
147
+ },
148
+ };
149
+ }
150
+ const limited = query.limit !== undefined ? rankedResult.atoms.slice(0, query.limit) : rankedResult.atoms;
151
+ return { ok: true, atoms: limited };
152
+ }
153
+ }
154
+ function omitSupersededAtoms(atoms) {
155
+ const superseded = new Set(atoms.flatMap((atom) => atom.links
156
+ .filter((link) => link.relation === 'supersedes')
157
+ .map((link) => link.targetMemoryId)));
158
+ return atoms.filter((atom) => !superseded.has(atom.memoryId));
159
+ }
160
+ function rankByAuthority(atoms, scorer, now) {
161
+ const ranked = [];
162
+ for (const [index, atom] of atoms.entries()) {
163
+ try {
164
+ ranked.push({ atom, index, score: finiteScore(scorer(atom, now)) });
165
+ }
166
+ catch (err) {
167
+ return {
168
+ ok: false,
169
+ error: err instanceof Error ? err.message : 'unknown scorer error',
170
+ };
171
+ }
172
+ }
173
+ return {
174
+ ok: true,
175
+ atoms: ranked
176
+ .sort((left, right) => {
177
+ const byScore = right.score - left.score;
178
+ return byScore !== 0 ? byScore : left.index - right.index;
179
+ })
180
+ .map((item) => item.atom),
181
+ };
182
+ }
183
+ function finiteScore(value) {
184
+ return Number.isFinite(value) ? value : 0;
185
+ }
186
+ function firstNonActionableReason(atoms) {
187
+ const humanRequired = atoms.find((atom) => atom.status === 'human_required' || atom.status === 'sealed');
188
+ if (humanRequired !== undefined) {
189
+ return {
190
+ outcome: 'ask_human',
191
+ reason: {
192
+ kind: 'promotion_boundary_blocked',
193
+ detail: `memory ${humanRequired.memoryId} requires human or sealed handling`,
194
+ },
195
+ };
196
+ }
197
+ const rejected = atoms.find((atom) => atom.status === 'rejected' || atom.status === 'deprecated');
198
+ if (rejected !== undefined) {
199
+ return {
200
+ outcome: 'deny',
201
+ reason: {
202
+ kind: 'promotion_boundary_blocked',
203
+ detail: `memory ${rejected.memoryId} is ${rejected.status}`,
204
+ },
205
+ };
206
+ }
207
+ const candidate = atoms.find((atom) => atom.status === 'candidate');
208
+ if (candidate !== undefined) {
209
+ return {
210
+ outcome: 'fetch_abstain',
211
+ reason: {
212
+ kind: 'tuple_incomplete',
213
+ missingFields: [`verifiedAuthority:${candidate.memoryId}`],
214
+ },
215
+ };
216
+ }
217
+ return null;
218
+ }
219
+ function schemaIssuePaths(issues, fallback) {
220
+ const paths = issues.map((issue) => issue.path.length > 0 ? issue.path.map(String).join('.') : fallback);
221
+ return paths.length > 0 ? paths : [fallback];
222
+ }
223
+ //# sourceMappingURL=retrieval-router.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retrieval-router.js","sourceRoot":"","sources":["../../src/runtime/retrieval-router.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAaH,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAEtD,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAC/D,OAAO,EAAE,0BAA0B,EAAyB,MAAM,wBAAwB,CAAC;AA4B3F,MAAM,uBAAuB,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KAAK,SAAS,EAAE,CAAC;QAC3B,OAAO;YACL,OAAO,EAAE,MAAM;YACf,MAAM,EAAE;gBACN,IAAI,EAAE,4BAA4B;gBAClC,MAAM,EAAE,UAAU,QAAQ,CAAC,QAAQ,OAAO,QAAQ,CAAC,MAAM,EAAE;aAC5D;SACF,CAAC;IACJ,CAAC;IAED,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,KAAK,WAAW,CAAC,CAAC;IACpE,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;QAC5B,OAAO;YACL,OAAO,EAAE,eAAe;YACxB,MAAM,EAAE;gBACN,IAAI,EAAE,kBAAkB;gBACxB,aAAa,EAAE,CAAC,qBAAqB,SAAS,CAAC,QAAQ,EAAE,CAAC;aAC3D;SACF,CAAC;IACJ,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAS,gBAAgB,CACvB,MAAkE,EAClE,QAAgB;IAEhB,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CACjC,KAAK,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CACpE,CAAC;IACF,OAAO,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;AAC/C,CAAC"}
@@ -0,0 +1,22 @@
1
+ import { type Scope, type Visibility } from '../types/index.js';
2
+ /**
3
+ * Compare two scope discriminated unions by their canonical index key.
4
+ *
5
+ * @remarks
6
+ * Use this anywhere runtime logic needs exact scope equality. It intentionally
7
+ * does not implement hierarchy or inheritance between scopes.
8
+ */
9
+ export declare function sameScope(a: Scope, b: Scope): boolean;
10
+ /**
11
+ * Compare two visibility discriminated unions by their canonical index key.
12
+ */
13
+ export declare function sameVisibility(a: Visibility, b: Visibility): boolean;
14
+ /**
15
+ * Return true when a requested visibility plane is explicitly permitted.
16
+ *
17
+ * @remarks
18
+ * This is exact matching only; hosts that want broader visibility inheritance
19
+ * must encode it in their `VisibilityPolicy` by returning all permitted planes.
20
+ */
21
+ export declare function includesVisibility(permitted: readonly Visibility[], requested: Visibility): boolean;
22
+ //# sourceMappingURL=scope-helpers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scope-helpers.d.ts","sourceRoot":"","sources":["../../src/runtime/scope-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,KAAK,EAAE,KAAK,UAAU,EAA2B,MAAM,mBAAmB,CAAC;AAEzF;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,GAAG,OAAO,CAErD;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,OAAO,CAEpE;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAChC,SAAS,EAAE,SAAS,UAAU,EAAE,EAChC,SAAS,EAAE,UAAU,GACpB,OAAO,CAGT"}
@@ -0,0 +1,29 @@
1
+ import { scopeKey, visibilityKey } from '../types/index.js';
2
+ /**
3
+ * Compare two scope discriminated unions by their canonical index key.
4
+ *
5
+ * @remarks
6
+ * Use this anywhere runtime logic needs exact scope equality. It intentionally
7
+ * does not implement hierarchy or inheritance between scopes.
8
+ */
9
+ export function sameScope(a, b) {
10
+ return scopeKey(a) === scopeKey(b);
11
+ }
12
+ /**
13
+ * Compare two visibility discriminated unions by their canonical index key.
14
+ */
15
+ export function sameVisibility(a, b) {
16
+ return visibilityKey(a) === visibilityKey(b);
17
+ }
18
+ /**
19
+ * Return true when a requested visibility plane is explicitly permitted.
20
+ *
21
+ * @remarks
22
+ * This is exact matching only; hosts that want broader visibility inheritance
23
+ * must encode it in their `VisibilityPolicy` by returning all permitted planes.
24
+ */
25
+ export function includesVisibility(permitted, requested) {
26
+ const requestedKey = visibilityKey(requested);
27
+ return permitted.some((v) => visibilityKey(v) === requestedKey);
28
+ }
29
+ //# sourceMappingURL=scope-helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scope-helpers.js","sourceRoot":"","sources":["../../src/runtime/scope-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAA+B,QAAQ,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAEzF;;;;;;GAMG;AACH,MAAM,UAAU,SAAS,CAAC,CAAQ,EAAE,CAAQ;IAC1C,OAAO,QAAQ,CAAC,CAAC,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC;AACrC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,cAAc,CAAC,CAAa,EAAE,CAAa;IACzD,OAAO,aAAa,CAAC,CAAC,CAAC,KAAK,aAAa,CAAC,CAAC,CAAC,CAAC;AAC/C,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAChC,SAAgC,EAChC,SAAqB;IAErB,MAAM,YAAY,GAAG,aAAa,CAAC,SAAS,CAAC,CAAC;IAC9C,OAAO,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,aAAa,CAAC,CAAC,CAAC,KAAK,YAAY,CAAC,CAAC;AAClE,CAAC"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Visibility policy boundary for runtime components.
3
+ *
4
+ * Core does not know who an agent is allowed to see. The host application must
5
+ * provide that mapping explicitly; the default runtime behavior is fail-closed.
6
+ */
7
+ import type { AgentId, Visibility } from '../types/index.js';
8
+ export interface VisibilityPolicy {
9
+ /**
10
+ * Return the visibility planes an agent may access.
11
+ *
12
+ * @remarks
13
+ * Implementations should return an empty array when access cannot be proven.
14
+ * Runtime components interpret an empty result as fail-closed denial or
15
+ * abstention, never as "all visible".
16
+ */
17
+ permittedVisibilitiesForAgent(agentId: AgentId): readonly Visibility[];
18
+ }
19
+ export declare const DENY_ALL_VISIBILITY_POLICY: VisibilityPolicy;
20
+ /**
21
+ * Build a fixed visibility policy for tests, demos, and simple single-user hosts.
22
+ *
23
+ * @remarks
24
+ * This helper ignores `agentId` by design. Production hosts should usually
25
+ * provide a policy that derives visibility from their own identity/session
26
+ * model.
27
+ */
28
+ export declare function staticVisibilityPolicy(permittedVisibilities: readonly Visibility[]): VisibilityPolicy;
29
+ //# sourceMappingURL=visibility-policy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"visibility-policy.d.ts","sourceRoot":"","sources":["../../src/runtime/visibility-policy.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAE7D,MAAM,WAAW,gBAAgB;IAC/B;;;;;;;OAOG;IACH,6BAA6B,CAAC,OAAO,EAAE,OAAO,GAAG,SAAS,UAAU,EAAE,CAAC;CACxE;AAED,eAAO,MAAM,0BAA0B,EAAE,gBAExC,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CACpC,qBAAqB,EAAE,SAAS,UAAU,EAAE,GAC3C,gBAAgB,CAIlB"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Visibility policy boundary for runtime components.
3
+ *
4
+ * Core does not know who an agent is allowed to see. The host application must
5
+ * provide that mapping explicitly; the default runtime behavior is fail-closed.
6
+ */
7
+ export const DENY_ALL_VISIBILITY_POLICY = {
8
+ permittedVisibilitiesForAgent: () => [],
9
+ };
10
+ /**
11
+ * Build a fixed visibility policy for tests, demos, and simple single-user hosts.
12
+ *
13
+ * @remarks
14
+ * This helper ignores `agentId` by design. Production hosts should usually
15
+ * provide a policy that derives visibility from their own identity/session
16
+ * model.
17
+ */
18
+ export function staticVisibilityPolicy(permittedVisibilities) {
19
+ return {
20
+ permittedVisibilitiesForAgent: () => permittedVisibilities,
21
+ };
22
+ }
23
+ //# sourceMappingURL=visibility-policy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"visibility-policy.js","sourceRoot":"","sources":["../../src/runtime/visibility-policy.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAgBH,MAAM,CAAC,MAAM,0BAA0B,GAAqB;IAC1D,6BAA6B,EAAE,GAAG,EAAE,CAAC,EAAE;CACxC,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,UAAU,sBAAsB,CACpC,qBAA4C;IAE5C,OAAO;QACL,6BAA6B,EAAE,GAAG,EAAE,CAAC,qBAAqB;KAC3D,CAAC;AACJ,CAAC"}
@@ -0,0 +1,66 @@
1
+ /**
2
+ * WriteGate — proposal-to-atom governance.
3
+ *
4
+ * Phase 1.3 implements the conservative path: validate sources, scope,
5
+ * visibility, explicit conflict markers, and promotion boundary before any
6
+ * MemoryAtom is inserted. The gate never upgrades a claim beyond `candidate`;
7
+ * human/sensitive/conflicted proposals become non-actionable `human_required`
8
+ * atoms instead.
9
+ */
10
+ import type { ConflictRegistry, EventLedger, MemoryStore } from '../storage/index.js';
11
+ import type { ConflictRecord, Event, MemoryAtom, MemoryId, MemoryProposal, WriteGateDecision } from '../types/index.js';
12
+ import type { Clock } from './decision-helpers.js';
13
+ import { type VisibilityPolicy } from './visibility-policy.js';
14
+ export interface WriteGateStores {
15
+ readonly eventLedger: EventLedger;
16
+ readonly memoryStore: MemoryStore;
17
+ readonly conflictRegistry: ConflictRegistry;
18
+ }
19
+ export interface WriteGateOptions extends WriteGateStores {
20
+ readonly visibilityPolicy?: VisibilityPolicy;
21
+ readonly clock?: Clock;
22
+ readonly memoryIdForProposal?: (proposal: MemoryProposal) => MemoryId;
23
+ }
24
+ export interface WriteGateResult {
25
+ readonly decision: WriteGateDecision;
26
+ readonly atom: MemoryAtom | null;
27
+ readonly sourceEvents: readonly Event[];
28
+ readonly conflicts: readonly ConflictRecord[];
29
+ }
30
+ export declare class WriteGate {
31
+ private readonly options;
32
+ private readonly visibilityPolicy;
33
+ private readonly clock;
34
+ private readonly memoryIdForProposal;
35
+ /**
36
+ * Create a proposal gate over explicit storage ports.
37
+ *
38
+ * @remarks
39
+ * The gate is intentionally storage-agnostic. Implementations supply the
40
+ * event ledger, memory store, conflict registry, visibility policy, and
41
+ * optional deterministic clock.
42
+ */
43
+ constructor(options: WriteGateOptions);
44
+ /**
45
+ * Evaluate and persist a proposal when it passes the source/scope boundary.
46
+ *
47
+ * @remarks
48
+ * This is the consumer-facing alias for `evaluate()`. It records auditable
49
+ * human-required/conflicted atoms when the source evidence is valid but the
50
+ * proposal cannot become actionable yet.
51
+ */
52
+ propose(proposal: MemoryProposal): Promise<WriteGateResult>;
53
+ /**
54
+ * Run the full WriteGate pipeline for one proposal.
55
+ *
56
+ * @remarks
57
+ * Implementation order is deliberate: parse proposal, derive permitted
58
+ * visibility, verify write visibility, load source events, validate scope,
59
+ * query conflict state, construct a zod-validated atom, then insert. Model
60
+ * confidence, consensus, prose, and chain metadata never grant authority.
61
+ */
62
+ evaluate(proposal: MemoryProposal): Promise<WriteGateResult>;
63
+ private loadSourceEvents;
64
+ private conflictDecision;
65
+ }
66
+ //# sourceMappingURL=write-gate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"write-gate.d.ts","sourceRoot":"","sources":["../../src/runtime/write-gate.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AACtF,OAAO,KAAK,EAEV,cAAc,EAEd,KAAK,EACL,UAAU,EACV,QAAQ,EACR,cAAc,EAEd,iBAAiB,EAClB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAInD,OAAO,EAA8B,KAAK,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE3F,MAAM,WAAW,eAAe;IAC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