genesis-ai-cli 7.4.5

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 (227) hide show
  1. package/.env.example +78 -0
  2. package/README.md +282 -0
  3. package/dist/src/active-inference/actions.d.ts +75 -0
  4. package/dist/src/active-inference/actions.js +250 -0
  5. package/dist/src/active-inference/autonomous-loop.d.ts +103 -0
  6. package/dist/src/active-inference/autonomous-loop.js +289 -0
  7. package/dist/src/active-inference/core.d.ts +85 -0
  8. package/dist/src/active-inference/core.js +555 -0
  9. package/dist/src/active-inference/demo-autonomous-loop.d.ts +8 -0
  10. package/dist/src/active-inference/demo-autonomous-loop.js +338 -0
  11. package/dist/src/active-inference/demo-value-integration.d.ts +8 -0
  12. package/dist/src/active-inference/demo-value-integration.js +174 -0
  13. package/dist/src/active-inference/index.d.ts +32 -0
  14. package/dist/src/active-inference/index.js +88 -0
  15. package/dist/src/active-inference/integration.d.ts +114 -0
  16. package/dist/src/active-inference/integration.js +698 -0
  17. package/dist/src/active-inference/memory-integration.d.ts +51 -0
  18. package/dist/src/active-inference/memory-integration.js +232 -0
  19. package/dist/src/active-inference/observations.d.ts +67 -0
  20. package/dist/src/active-inference/observations.js +147 -0
  21. package/dist/src/active-inference/test-active-inference.d.ts +8 -0
  22. package/dist/src/active-inference/test-active-inference.js +320 -0
  23. package/dist/src/active-inference/test-value-integration.d.ts +6 -0
  24. package/dist/src/active-inference/test-value-integration.js +168 -0
  25. package/dist/src/active-inference/types.d.ts +150 -0
  26. package/dist/src/active-inference/types.js +59 -0
  27. package/dist/src/active-inference/value-integration.d.ts +164 -0
  28. package/dist/src/active-inference/value-integration.js +459 -0
  29. package/dist/src/agents/base-agent.d.ts +53 -0
  30. package/dist/src/agents/base-agent.js +178 -0
  31. package/dist/src/agents/builder.d.ts +67 -0
  32. package/dist/src/agents/builder.js +537 -0
  33. package/dist/src/agents/critic.d.ts +35 -0
  34. package/dist/src/agents/critic.js +322 -0
  35. package/dist/src/agents/ethicist.d.ts +54 -0
  36. package/dist/src/agents/ethicist.js +393 -0
  37. package/dist/src/agents/explorer.d.ts +26 -0
  38. package/dist/src/agents/explorer.js +216 -0
  39. package/dist/src/agents/feeling.d.ts +41 -0
  40. package/dist/src/agents/feeling.js +320 -0
  41. package/dist/src/agents/index.d.ts +111 -0
  42. package/dist/src/agents/index.js +222 -0
  43. package/dist/src/agents/memory.d.ts +69 -0
  44. package/dist/src/agents/memory.js +404 -0
  45. package/dist/src/agents/message-bus.d.ts +88 -0
  46. package/dist/src/agents/message-bus.js +267 -0
  47. package/dist/src/agents/narrator.d.ts +90 -0
  48. package/dist/src/agents/narrator.js +473 -0
  49. package/dist/src/agents/planner.d.ts +38 -0
  50. package/dist/src/agents/planner.js +341 -0
  51. package/dist/src/agents/predictor.d.ts +73 -0
  52. package/dist/src/agents/predictor.js +506 -0
  53. package/dist/src/agents/sensor.d.ts +88 -0
  54. package/dist/src/agents/sensor.js +377 -0
  55. package/dist/src/agents/test-agents.d.ts +6 -0
  56. package/dist/src/agents/test-agents.js +73 -0
  57. package/dist/src/agents/types.d.ts +194 -0
  58. package/dist/src/agents/types.js +7 -0
  59. package/dist/src/brain/index.d.ts +185 -0
  60. package/dist/src/brain/index.js +843 -0
  61. package/dist/src/brain/trace.d.ts +91 -0
  62. package/dist/src/brain/trace.js +327 -0
  63. package/dist/src/brain/types.d.ts +165 -0
  64. package/dist/src/brain/types.js +51 -0
  65. package/dist/src/cli/chat.d.ts +237 -0
  66. package/dist/src/cli/chat.js +1959 -0
  67. package/dist/src/cli/dispatcher.d.ts +182 -0
  68. package/dist/src/cli/dispatcher.js +718 -0
  69. package/dist/src/cli/human-loop.d.ts +170 -0
  70. package/dist/src/cli/human-loop.js +543 -0
  71. package/dist/src/cli/index.d.ts +12 -0
  72. package/dist/src/cli/index.js +28 -0
  73. package/dist/src/cli/interactive.d.ts +141 -0
  74. package/dist/src/cli/interactive.js +757 -0
  75. package/dist/src/cli/ui.d.ts +205 -0
  76. package/dist/src/cli/ui.js +632 -0
  77. package/dist/src/consciousness/attention-schema.d.ts +154 -0
  78. package/dist/src/consciousness/attention-schema.js +432 -0
  79. package/dist/src/consciousness/global-workspace.d.ts +149 -0
  80. package/dist/src/consciousness/global-workspace.js +422 -0
  81. package/dist/src/consciousness/index.d.ts +186 -0
  82. package/dist/src/consciousness/index.js +476 -0
  83. package/dist/src/consciousness/phi-calculator.d.ts +119 -0
  84. package/dist/src/consciousness/phi-calculator.js +445 -0
  85. package/dist/src/consciousness/phi-decisions.d.ts +169 -0
  86. package/dist/src/consciousness/phi-decisions.js +383 -0
  87. package/dist/src/consciousness/phi-monitor.d.ts +153 -0
  88. package/dist/src/consciousness/phi-monitor.js +465 -0
  89. package/dist/src/consciousness/types.d.ts +260 -0
  90. package/dist/src/consciousness/types.js +44 -0
  91. package/dist/src/daemon/dream-mode.d.ts +115 -0
  92. package/dist/src/daemon/dream-mode.js +470 -0
  93. package/dist/src/daemon/index.d.ts +162 -0
  94. package/dist/src/daemon/index.js +542 -0
  95. package/dist/src/daemon/maintenance.d.ts +139 -0
  96. package/dist/src/daemon/maintenance.js +549 -0
  97. package/dist/src/daemon/process.d.ts +82 -0
  98. package/dist/src/daemon/process.js +442 -0
  99. package/dist/src/daemon/scheduler.d.ts +90 -0
  100. package/dist/src/daemon/scheduler.js +494 -0
  101. package/dist/src/daemon/types.d.ts +213 -0
  102. package/dist/src/daemon/types.js +50 -0
  103. package/dist/src/epistemic/index.d.ts +74 -0
  104. package/dist/src/epistemic/index.js +225 -0
  105. package/dist/src/grounding/epistemic-stack.d.ts +100 -0
  106. package/dist/src/grounding/epistemic-stack.js +408 -0
  107. package/dist/src/grounding/feedback.d.ts +98 -0
  108. package/dist/src/grounding/feedback.js +276 -0
  109. package/dist/src/grounding/index.d.ts +123 -0
  110. package/dist/src/grounding/index.js +224 -0
  111. package/dist/src/grounding/verifier.d.ts +149 -0
  112. package/dist/src/grounding/verifier.js +484 -0
  113. package/dist/src/healing/detector.d.ts +110 -0
  114. package/dist/src/healing/detector.js +436 -0
  115. package/dist/src/healing/fixer.d.ts +138 -0
  116. package/dist/src/healing/fixer.js +572 -0
  117. package/dist/src/healing/index.d.ts +23 -0
  118. package/dist/src/healing/index.js +43 -0
  119. package/dist/src/hooks/index.d.ts +135 -0
  120. package/dist/src/hooks/index.js +317 -0
  121. package/dist/src/index.d.ts +23 -0
  122. package/dist/src/index.js +1266 -0
  123. package/dist/src/kernel/index.d.ts +155 -0
  124. package/dist/src/kernel/index.js +795 -0
  125. package/dist/src/kernel/invariants.d.ts +153 -0
  126. package/dist/src/kernel/invariants.js +355 -0
  127. package/dist/src/kernel/test-kernel.d.ts +6 -0
  128. package/dist/src/kernel/test-kernel.js +108 -0
  129. package/dist/src/kernel/test-real-mcp.d.ts +10 -0
  130. package/dist/src/kernel/test-real-mcp.js +295 -0
  131. package/dist/src/llm/index.d.ts +146 -0
  132. package/dist/src/llm/index.js +428 -0
  133. package/dist/src/llm/router.d.ts +136 -0
  134. package/dist/src/llm/router.js +510 -0
  135. package/dist/src/mcp/index.d.ts +85 -0
  136. package/dist/src/mcp/index.js +657 -0
  137. package/dist/src/mcp/resilient.d.ts +139 -0
  138. package/dist/src/mcp/resilient.js +417 -0
  139. package/dist/src/memory/cache.d.ts +118 -0
  140. package/dist/src/memory/cache.js +356 -0
  141. package/dist/src/memory/cognitive-workspace.d.ts +231 -0
  142. package/dist/src/memory/cognitive-workspace.js +521 -0
  143. package/dist/src/memory/consolidation.d.ts +99 -0
  144. package/dist/src/memory/consolidation.js +443 -0
  145. package/dist/src/memory/episodic.d.ts +114 -0
  146. package/dist/src/memory/episodic.js +394 -0
  147. package/dist/src/memory/forgetting.d.ts +134 -0
  148. package/dist/src/memory/forgetting.js +324 -0
  149. package/dist/src/memory/index.d.ts +211 -0
  150. package/dist/src/memory/index.js +367 -0
  151. package/dist/src/memory/indexer.d.ts +123 -0
  152. package/dist/src/memory/indexer.js +479 -0
  153. package/dist/src/memory/procedural.d.ts +136 -0
  154. package/dist/src/memory/procedural.js +479 -0
  155. package/dist/src/memory/semantic.d.ts +132 -0
  156. package/dist/src/memory/semantic.js +497 -0
  157. package/dist/src/memory/types.d.ts +193 -0
  158. package/dist/src/memory/types.js +15 -0
  159. package/dist/src/orchestrator.d.ts +65 -0
  160. package/dist/src/orchestrator.js +317 -0
  161. package/dist/src/persistence/index.d.ts +257 -0
  162. package/dist/src/persistence/index.js +763 -0
  163. package/dist/src/pipeline/executor.d.ts +51 -0
  164. package/dist/src/pipeline/executor.js +695 -0
  165. package/dist/src/pipeline/index.d.ts +7 -0
  166. package/dist/src/pipeline/index.js +11 -0
  167. package/dist/src/self-production.d.ts +67 -0
  168. package/dist/src/self-production.js +205 -0
  169. package/dist/src/subagents/executor.d.ts +58 -0
  170. package/dist/src/subagents/executor.js +283 -0
  171. package/dist/src/subagents/index.d.ts +37 -0
  172. package/dist/src/subagents/index.js +53 -0
  173. package/dist/src/subagents/registry.d.ts +23 -0
  174. package/dist/src/subagents/registry.js +167 -0
  175. package/dist/src/subagents/types.d.ts +79 -0
  176. package/dist/src/subagents/types.js +14 -0
  177. package/dist/src/tools/bash.d.ts +139 -0
  178. package/dist/src/tools/bash.js +583 -0
  179. package/dist/src/tools/edit.d.ts +125 -0
  180. package/dist/src/tools/edit.js +424 -0
  181. package/dist/src/tools/git.d.ts +179 -0
  182. package/dist/src/tools/git.js +504 -0
  183. package/dist/src/tools/index.d.ts +21 -0
  184. package/dist/src/tools/index.js +163 -0
  185. package/dist/src/types.d.ts +145 -0
  186. package/dist/src/types.js +7 -0
  187. package/dist/src/world-model/decoder.d.ts +163 -0
  188. package/dist/src/world-model/decoder.js +517 -0
  189. package/dist/src/world-model/digital-twin.d.ts +219 -0
  190. package/dist/src/world-model/digital-twin.js +695 -0
  191. package/dist/src/world-model/encoder.d.ts +141 -0
  192. package/dist/src/world-model/encoder.js +564 -0
  193. package/dist/src/world-model/index.d.ts +221 -0
  194. package/dist/src/world-model/index.js +772 -0
  195. package/dist/src/world-model/predictor.d.ts +161 -0
  196. package/dist/src/world-model/predictor.js +681 -0
  197. package/dist/src/world-model/test-value-jepa.d.ts +8 -0
  198. package/dist/src/world-model/test-value-jepa.js +430 -0
  199. package/dist/src/world-model/types.d.ts +341 -0
  200. package/dist/src/world-model/types.js +69 -0
  201. package/dist/src/world-model/value-jepa.d.ts +247 -0
  202. package/dist/src/world-model/value-jepa.js +622 -0
  203. package/dist/test/brain.test.d.ts +11 -0
  204. package/dist/test/brain.test.js +358 -0
  205. package/dist/test/cli/dispatcher.test.d.ts +4 -0
  206. package/dist/test/cli/dispatcher.test.js +332 -0
  207. package/dist/test/cli/human-loop.test.d.ts +4 -0
  208. package/dist/test/cli/human-loop.test.js +270 -0
  209. package/dist/test/grounding/feedback.test.d.ts +4 -0
  210. package/dist/test/grounding/feedback.test.js +462 -0
  211. package/dist/test/grounding/verifier.test.d.ts +4 -0
  212. package/dist/test/grounding/verifier.test.js +442 -0
  213. package/dist/test/grounding.test.d.ts +6 -0
  214. package/dist/test/grounding.test.js +246 -0
  215. package/dist/test/healing/detector.test.d.ts +4 -0
  216. package/dist/test/healing/detector.test.js +266 -0
  217. package/dist/test/healing/fixer.test.d.ts +4 -0
  218. package/dist/test/healing/fixer.test.js +369 -0
  219. package/dist/test/integration.test.d.ts +5 -0
  220. package/dist/test/integration.test.js +290 -0
  221. package/dist/test/tools/bash.test.d.ts +4 -0
  222. package/dist/test/tools/bash.test.js +348 -0
  223. package/dist/test/tools/edit.test.d.ts +4 -0
  224. package/dist/test/tools/edit.test.js +350 -0
  225. package/dist/test/tools/git.test.d.ts +4 -0
  226. package/dist/test/tools/git.test.js +350 -0
  227. package/package.json +60 -0
@@ -0,0 +1,154 @@
1
+ /**
2
+ * Genesis 6.0 - Attention Schema Theory (AST)
3
+ *
4
+ * Implementation of Graziano's Attention Schema Theory.
5
+ *
6
+ * Key concept: Consciousness arises from the brain's internal model
7
+ * of its own attention. The "schema" is a simplified, descriptive
8
+ * model that attributes awareness to oneself.
9
+ *
10
+ * The attention schema allows:
11
+ * - Metacognition: knowing what you're paying attention to
12
+ * - Theory of Mind: modeling others' attention states
13
+ * - Self-awareness: the "feeling" of being aware
14
+ *
15
+ * References:
16
+ * - Graziano, M.S.A. (2013). Consciousness and the Social Brain.
17
+ * - Graziano, M.S.A. & Webb, T.W. (2015). The attention schema theory.
18
+ * - Webb, T.W. & Graziano, M.S.A. (2015). The attention schema theory: a mechanistic account of subjective awareness.
19
+ *
20
+ * Usage:
21
+ * ```typescript
22
+ * import { createAttentionSchemaNetwork } from './consciousness/attention-schema.js';
23
+ *
24
+ * const ast = createAttentionSchemaNetwork();
25
+ *
26
+ * // Shift attention to a target
27
+ * ast.attend('user-message', 'external');
28
+ *
29
+ * // Get current attention state
30
+ * const state = ast.getAttentionState();
31
+ *
32
+ * // Model another agent's attention
33
+ * ast.modelOtherAttention('partner-agent', ['user-message', 'task']);
34
+ * ```
35
+ */
36
+ import { AttentionState, AttentionFocus, AttentionMode, AttentionSchema, OtherAttentionModel, PhenomenalQuality } from './types.js';
37
+ export interface ASTConfig {
38
+ capacity: number;
39
+ focusDecayRate: number;
40
+ schemaUpdateIntervalMs: number;
41
+ theoryOfMindEnabled: boolean;
42
+ defaultIntensity: number;
43
+ focusThreshold: number;
44
+ }
45
+ export declare const DEFAULT_AST_CONFIG: ASTConfig;
46
+ export type ASTEventType = 'attention_shifted' | 'attention_released' | 'mode_changed' | 'schema_updated' | 'other_modeled' | 'capacity_exceeded';
47
+ export type ASTEventHandler = (event: {
48
+ type: ASTEventType;
49
+ data?: unknown;
50
+ }) => void;
51
+ export declare class AttentionSchemaNetwork {
52
+ private config;
53
+ private state;
54
+ private schema;
55
+ private updateTimer;
56
+ private running;
57
+ private eventHandlers;
58
+ constructor(config?: Partial<ASTConfig>);
59
+ start(): void;
60
+ stop(): void;
61
+ isRunning(): boolean;
62
+ /**
63
+ * Shift attention to a target
64
+ */
65
+ attend(target: string, type?: 'internal' | 'external', intensity?: number): AttentionFocus;
66
+ /**
67
+ * Release attention from a target
68
+ */
69
+ release(target: string): boolean;
70
+ /**
71
+ * Release the weakest focus
72
+ */
73
+ private releaseWeakest;
74
+ /**
75
+ * Set attention mode
76
+ */
77
+ setMode(mode: AttentionMode): void;
78
+ /**
79
+ * Update mode based on current state
80
+ */
81
+ private updateMode;
82
+ /**
83
+ * Update the attention schema (self-model)
84
+ */
85
+ private updateSchema;
86
+ /**
87
+ * Calculate metacognitive confidence
88
+ * How confident are we about our own attention state?
89
+ */
90
+ private calculateMetacognitiveConfidence;
91
+ /**
92
+ * Calculate voluntary control
93
+ * Is attention voluntary or captured?
94
+ */
95
+ private calculateVoluntaryControl;
96
+ /**
97
+ * Get contents we're aware of
98
+ */
99
+ private getAwareContents;
100
+ /**
101
+ * Calculate clarity of awareness
102
+ */
103
+ private calculateClarity;
104
+ /**
105
+ * Calculate phenomenal quality
106
+ * The "feel" of awareness
107
+ */
108
+ private calculatePhenomenalQuality;
109
+ /**
110
+ * Decay attention to unfocused items
111
+ */
112
+ private decayAttention;
113
+ /**
114
+ * Model another agent's attention
115
+ * Infer what they're paying attention to based on their behavior
116
+ */
117
+ modelOtherAttention(agentId: string, observedBehavior: string[], confidence?: number): OtherAttentionModel;
118
+ /**
119
+ * Get model of another agent's attention
120
+ */
121
+ getOtherModel(agentId: string): OtherAttentionModel | undefined;
122
+ /**
123
+ * Clear Theory of Mind models
124
+ */
125
+ clearOtherModels(): void;
126
+ getAttentionState(): AttentionState;
127
+ getSchema(): AttentionSchema;
128
+ getCurrentFocus(): AttentionFocus | null;
129
+ getMode(): AttentionMode;
130
+ /**
131
+ * Get introspective report
132
+ * What would the system say about its own attention?
133
+ */
134
+ introspect(): {
135
+ focus: string;
136
+ clarity: PhenomenalQuality;
137
+ confident: boolean;
138
+ voluntary: boolean;
139
+ awareOf: string[];
140
+ };
141
+ on(handler: ASTEventHandler): () => void;
142
+ private emit;
143
+ private createInitialState;
144
+ private createInitialSchema;
145
+ stats(): {
146
+ currentFocus: string | null;
147
+ mode: AttentionMode;
148
+ capacity: number;
149
+ used: number;
150
+ clarity: PhenomenalQuality;
151
+ otherModelsCount: number;
152
+ };
153
+ }
154
+ export declare function createAttentionSchemaNetwork(config?: Partial<ASTConfig>): AttentionSchemaNetwork;
@@ -0,0 +1,432 @@
1
+ "use strict";
2
+ /**
3
+ * Genesis 6.0 - Attention Schema Theory (AST)
4
+ *
5
+ * Implementation of Graziano's Attention Schema Theory.
6
+ *
7
+ * Key concept: Consciousness arises from the brain's internal model
8
+ * of its own attention. The "schema" is a simplified, descriptive
9
+ * model that attributes awareness to oneself.
10
+ *
11
+ * The attention schema allows:
12
+ * - Metacognition: knowing what you're paying attention to
13
+ * - Theory of Mind: modeling others' attention states
14
+ * - Self-awareness: the "feeling" of being aware
15
+ *
16
+ * References:
17
+ * - Graziano, M.S.A. (2013). Consciousness and the Social Brain.
18
+ * - Graziano, M.S.A. & Webb, T.W. (2015). The attention schema theory.
19
+ * - Webb, T.W. & Graziano, M.S.A. (2015). The attention schema theory: a mechanistic account of subjective awareness.
20
+ *
21
+ * Usage:
22
+ * ```typescript
23
+ * import { createAttentionSchemaNetwork } from './consciousness/attention-schema.js';
24
+ *
25
+ * const ast = createAttentionSchemaNetwork();
26
+ *
27
+ * // Shift attention to a target
28
+ * ast.attend('user-message', 'external');
29
+ *
30
+ * // Get current attention state
31
+ * const state = ast.getAttentionState();
32
+ *
33
+ * // Model another agent's attention
34
+ * ast.modelOtherAttention('partner-agent', ['user-message', 'task']);
35
+ * ```
36
+ */
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.AttentionSchemaNetwork = exports.DEFAULT_AST_CONFIG = void 0;
39
+ exports.createAttentionSchemaNetwork = createAttentionSchemaNetwork;
40
+ exports.DEFAULT_AST_CONFIG = {
41
+ capacity: 4,
42
+ focusDecayRate: 0.05,
43
+ schemaUpdateIntervalMs: 1000,
44
+ theoryOfMindEnabled: true,
45
+ defaultIntensity: 0.7,
46
+ focusThreshold: 0.3,
47
+ };
48
+ class AttentionSchemaNetwork {
49
+ config;
50
+ state;
51
+ schema;
52
+ updateTimer = null;
53
+ running = false;
54
+ eventHandlers = new Set();
55
+ constructor(config = {}) {
56
+ this.config = { ...exports.DEFAULT_AST_CONFIG, ...config };
57
+ this.state = this.createInitialState();
58
+ this.schema = this.createInitialSchema();
59
+ }
60
+ // ============================================================================
61
+ // Lifecycle
62
+ // ============================================================================
63
+ start() {
64
+ if (this.running)
65
+ return;
66
+ this.running = true;
67
+ this.updateTimer = setInterval(() => this.updateSchema(), this.config.schemaUpdateIntervalMs);
68
+ }
69
+ stop() {
70
+ if (!this.running)
71
+ return;
72
+ this.running = false;
73
+ if (this.updateTimer) {
74
+ clearInterval(this.updateTimer);
75
+ this.updateTimer = null;
76
+ }
77
+ }
78
+ isRunning() {
79
+ return this.running;
80
+ }
81
+ // ============================================================================
82
+ // Attention Control
83
+ // ============================================================================
84
+ /**
85
+ * Shift attention to a target
86
+ */
87
+ attend(target, type = 'external', intensity = this.config.defaultIntensity) {
88
+ const now = new Date();
89
+ // Check capacity
90
+ if (this.state.used >= this.config.capacity) {
91
+ // Release weakest focus
92
+ this.releaseWeakest();
93
+ }
94
+ // Create new focus
95
+ const focus = {
96
+ target,
97
+ type,
98
+ intensity: Math.min(1, Math.max(0, intensity)),
99
+ startedAt: now,
100
+ duration: 0,
101
+ };
102
+ // Update state
103
+ this.state.focus = focus;
104
+ this.state.history.unshift(focus);
105
+ this.state.used++;
106
+ // Limit history
107
+ if (this.state.history.length > 100) {
108
+ this.state.history.pop();
109
+ }
110
+ // Update mode based on type
111
+ this.updateMode();
112
+ this.emit({ type: 'attention_shifted', data: { focus } });
113
+ return focus;
114
+ }
115
+ /**
116
+ * Release attention from a target
117
+ */
118
+ release(target) {
119
+ const historyIndex = this.state.history.findIndex((f) => f.target === target);
120
+ if (historyIndex !== -1) {
121
+ const focus = this.state.history[historyIndex];
122
+ focus.duration = Date.now() - focus.startedAt.getTime();
123
+ if (this.state.focus?.target === target) {
124
+ this.state.focus = this.state.history[1] || null;
125
+ }
126
+ this.state.used = Math.max(0, this.state.used - 1);
127
+ this.emit({ type: 'attention_released', data: { target } });
128
+ return true;
129
+ }
130
+ return false;
131
+ }
132
+ /**
133
+ * Release the weakest focus
134
+ */
135
+ releaseWeakest() {
136
+ if (this.state.history.length === 0)
137
+ return;
138
+ // Find minimum intensity
139
+ let minIntensity = Infinity;
140
+ let minIndex = -1;
141
+ for (let i = 0; i < this.state.history.length; i++) {
142
+ if (this.state.history[i].intensity < minIntensity) {
143
+ minIntensity = this.state.history[i].intensity;
144
+ minIndex = i;
145
+ }
146
+ }
147
+ if (minIndex >= 0) {
148
+ const released = this.state.history[minIndex];
149
+ this.release(released.target);
150
+ this.emit({ type: 'capacity_exceeded', data: { released } });
151
+ }
152
+ }
153
+ /**
154
+ * Set attention mode
155
+ */
156
+ setMode(mode) {
157
+ if (this.state.mode !== mode) {
158
+ this.state.mode = mode;
159
+ this.emit({ type: 'mode_changed', data: { mode } });
160
+ }
161
+ }
162
+ /**
163
+ * Update mode based on current state
164
+ */
165
+ updateMode() {
166
+ if (!this.state.focus) {
167
+ this.setMode('mind-wandering');
168
+ return;
169
+ }
170
+ const activeCount = this.state.history.filter((f) => f.intensity >= this.config.focusThreshold).length;
171
+ if (activeCount === 1 && this.state.focus.intensity > 0.7) {
172
+ this.setMode('focused');
173
+ }
174
+ else if (activeCount > 1) {
175
+ this.setMode('diffuse');
176
+ }
177
+ else if (this.state.focus.type === 'external') {
178
+ this.setMode('vigilant');
179
+ }
180
+ else {
181
+ this.setMode('mind-wandering');
182
+ }
183
+ }
184
+ // ============================================================================
185
+ // Schema Management
186
+ // ============================================================================
187
+ /**
188
+ * Update the attention schema (self-model)
189
+ */
190
+ updateSchema() {
191
+ // Update self model
192
+ this.schema.selfModel = {
193
+ perceivedFocus: this.state.focus?.target || 'nothing',
194
+ metacognitiveConfidence: this.calculateMetacognitiveConfidence(),
195
+ voluntaryControl: this.calculateVoluntaryControl(),
196
+ };
197
+ // Update awareness model
198
+ this.schema.awarenessModel = {
199
+ contents: this.getAwareContents(),
200
+ clarity: this.calculateClarity(),
201
+ phenomenalQuality: this.calculatePhenomenalQuality(),
202
+ };
203
+ // Decay unfocused items
204
+ this.decayAttention();
205
+ this.emit({ type: 'schema_updated', data: { schema: this.schema } });
206
+ }
207
+ /**
208
+ * Calculate metacognitive confidence
209
+ * How confident are we about our own attention state?
210
+ */
211
+ calculateMetacognitiveConfidence() {
212
+ if (!this.state.focus)
213
+ return 0.2;
214
+ // High intensity focus = high confidence
215
+ let confidence = this.state.focus.intensity;
216
+ // Multiple targets reduce confidence
217
+ const activeCount = this.state.history.filter((f) => f.intensity >= this.config.focusThreshold).length;
218
+ confidence *= Math.exp(-0.2 * (activeCount - 1));
219
+ // Mind-wandering reduces confidence
220
+ if (this.state.mode === 'mind-wandering') {
221
+ confidence *= 0.5;
222
+ }
223
+ return Math.min(1, Math.max(0, confidence));
224
+ }
225
+ /**
226
+ * Calculate voluntary control
227
+ * Is attention voluntary or captured?
228
+ */
229
+ calculateVoluntaryControl() {
230
+ if (!this.state.focus)
231
+ return 0.5;
232
+ // Internal focus = more voluntary
233
+ let control = this.state.focus.type === 'internal' ? 0.8 : 0.4;
234
+ // Focused mode = more voluntary
235
+ if (this.state.mode === 'focused') {
236
+ control += 0.2;
237
+ }
238
+ // High intensity can mean captured (external) or deliberate (internal)
239
+ if (this.state.focus.type === 'external' && this.state.focus.intensity > 0.8) {
240
+ control -= 0.2; // Probably captured
241
+ }
242
+ return Math.min(1, Math.max(0, control));
243
+ }
244
+ /**
245
+ * Get contents we're aware of
246
+ */
247
+ getAwareContents() {
248
+ return this.state.history
249
+ .filter((f) => f.intensity >= this.config.focusThreshold)
250
+ .map((f) => f.target);
251
+ }
252
+ /**
253
+ * Calculate clarity of awareness
254
+ */
255
+ calculateClarity() {
256
+ if (!this.state.focus)
257
+ return 0.1;
258
+ // Base clarity from focus intensity
259
+ let clarity = this.state.focus.intensity;
260
+ // Focused mode = clearer
261
+ if (this.state.mode === 'focused') {
262
+ clarity += 0.2;
263
+ }
264
+ // Multiple targets = less clear
265
+ const activeCount = this.getAwareContents().length;
266
+ clarity *= Math.exp(-0.1 * (activeCount - 1));
267
+ return Math.min(1, Math.max(0, clarity));
268
+ }
269
+ /**
270
+ * Calculate phenomenal quality
271
+ * The "feel" of awareness
272
+ */
273
+ calculatePhenomenalQuality() {
274
+ const clarity = this.calculateClarity();
275
+ if (clarity > 0.7)
276
+ return 'vivid';
277
+ if (clarity > 0.3)
278
+ return 'muted';
279
+ return 'absent';
280
+ }
281
+ /**
282
+ * Decay attention to unfocused items
283
+ */
284
+ decayAttention() {
285
+ for (const focus of this.state.history) {
286
+ if (focus.target !== this.state.focus?.target) {
287
+ focus.intensity *= (1 - this.config.focusDecayRate);
288
+ }
289
+ focus.duration = Date.now() - focus.startedAt.getTime();
290
+ }
291
+ // Remove very weak focuses
292
+ const beforeCount = this.state.history.length;
293
+ this.state.history = this.state.history.filter((f) => f.intensity >= 0.05);
294
+ this.state.used = this.state.history.filter((f) => f.intensity >= this.config.focusThreshold).length;
295
+ // Update current focus if it decayed
296
+ if (this.state.focus && this.state.focus.intensity < 0.05) {
297
+ this.state.focus = this.state.history[0] || null;
298
+ }
299
+ }
300
+ // ============================================================================
301
+ // Theory of Mind
302
+ // ============================================================================
303
+ /**
304
+ * Model another agent's attention
305
+ * Infer what they're paying attention to based on their behavior
306
+ */
307
+ modelOtherAttention(agentId, observedBehavior, confidence = 0.5) {
308
+ if (!this.config.theoryOfMindEnabled) {
309
+ throw new Error('Theory of Mind is disabled');
310
+ }
311
+ // Infer focus from behavior
312
+ // Simple heuristic: most frequent/recent behavior indicates focus
313
+ const inferredFocus = observedBehavior[0] || 'unknown';
314
+ const model = {
315
+ agentId,
316
+ inferredFocus,
317
+ confidence,
318
+ lastUpdated: new Date(),
319
+ };
320
+ this.schema.otherModels.set(agentId, model);
321
+ this.emit({ type: 'other_modeled', data: { model } });
322
+ return model;
323
+ }
324
+ /**
325
+ * Get model of another agent's attention
326
+ */
327
+ getOtherModel(agentId) {
328
+ return this.schema.otherModels.get(agentId);
329
+ }
330
+ /**
331
+ * Clear Theory of Mind models
332
+ */
333
+ clearOtherModels() {
334
+ this.schema.otherModels.clear();
335
+ }
336
+ // ============================================================================
337
+ // State Access
338
+ // ============================================================================
339
+ getAttentionState() {
340
+ return { ...this.state };
341
+ }
342
+ getSchema() {
343
+ return {
344
+ selfModel: { ...this.schema.selfModel },
345
+ awarenessModel: { ...this.schema.awarenessModel },
346
+ otherModels: new Map(this.schema.otherModels),
347
+ };
348
+ }
349
+ getCurrentFocus() {
350
+ return this.state.focus;
351
+ }
352
+ getMode() {
353
+ return this.state.mode;
354
+ }
355
+ /**
356
+ * Get introspective report
357
+ * What would the system say about its own attention?
358
+ */
359
+ introspect() {
360
+ return {
361
+ focus: this.schema.selfModel.perceivedFocus,
362
+ clarity: this.schema.awarenessModel.phenomenalQuality,
363
+ confident: this.schema.selfModel.metacognitiveConfidence > 0.6,
364
+ voluntary: this.schema.selfModel.voluntaryControl > 0.5,
365
+ awareOf: this.schema.awarenessModel.contents,
366
+ };
367
+ }
368
+ // ============================================================================
369
+ // Events
370
+ // ============================================================================
371
+ on(handler) {
372
+ this.eventHandlers.add(handler);
373
+ return () => this.eventHandlers.delete(handler);
374
+ }
375
+ emit(event) {
376
+ for (const handler of this.eventHandlers) {
377
+ try {
378
+ handler(event);
379
+ }
380
+ catch (err) {
381
+ console.error('AST event handler error:', err);
382
+ }
383
+ }
384
+ }
385
+ // ============================================================================
386
+ // Helpers
387
+ // ============================================================================
388
+ createInitialState() {
389
+ return {
390
+ focus: null,
391
+ history: [],
392
+ capacity: this.config.capacity,
393
+ used: 0,
394
+ mode: 'mind-wandering',
395
+ };
396
+ }
397
+ createInitialSchema() {
398
+ return {
399
+ selfModel: {
400
+ perceivedFocus: 'nothing',
401
+ metacognitiveConfidence: 0.2,
402
+ voluntaryControl: 0.5,
403
+ },
404
+ awarenessModel: {
405
+ contents: [],
406
+ clarity: 0.1,
407
+ phenomenalQuality: 'absent',
408
+ },
409
+ otherModels: new Map(),
410
+ };
411
+ }
412
+ // ============================================================================
413
+ // Stats
414
+ // ============================================================================
415
+ stats() {
416
+ return {
417
+ currentFocus: this.state.focus?.target || null,
418
+ mode: this.state.mode,
419
+ capacity: this.state.capacity,
420
+ used: this.state.used,
421
+ clarity: this.schema.awarenessModel.phenomenalQuality,
422
+ otherModelsCount: this.schema.otherModels.size,
423
+ };
424
+ }
425
+ }
426
+ exports.AttentionSchemaNetwork = AttentionSchemaNetwork;
427
+ // ============================================================================
428
+ // Factory
429
+ // ============================================================================
430
+ function createAttentionSchemaNetwork(config) {
431
+ return new AttentionSchemaNetwork(config);
432
+ }