@teleologyhi-sdk/nhe 1.0.0-trinity
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.
- package/CHANGELOG.md +702 -0
- package/LICENSE +190 -0
- package/NOTICE +17 -0
- package/README.md +552 -0
- package/SPEC.md +794 -0
- package/TRADEMARK.md +33 -0
- package/dist/cli.js +2879 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.cjs +3307 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1902 -0
- package/dist/index.d.ts +1902 -0
- package/dist/index.js +3221 -0
- package/dist/index.js.map +1 -0
- package/package.json +119 -0
package/SPEC.md
ADDED
|
@@ -0,0 +1,794 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: "@teleologyhi-sdk/nhe"
|
|
3
|
+
description: "Technical specification for the NHE™ package — Non-Human Entity. The embodied operational agent that interacts with humans, integrates LLM APIs, runs the sleep/dream cycle, consolidates memory, and exercises autonomous ethical refusal. The user-facing surface of the TeleologyHI system. Source of truth: MAIC_HIM_NHE_INTERVIEW_LOG.md Entries 1, 2, 4, 5, 8, 9, 10, 11, 12."
|
|
4
|
+
license: "Code: Apache License 2.0 (see ../LICENSE). Names — MAIC™, HIM™, NHE™, TeleologyHI™, Takk™ — are trademarks of David C. Cavalcante and are NOT covered by the Apache 2.0 grant. See ../TRADEMARK.md."
|
|
5
|
+
status: "Stable; current live version on npm tracked at [`@teleologyhi-sdk/nhe`](https://www.npmjs.com/package/@teleologyhi-sdk/nhe) (`latest` dist-tag). Surface: orchestrator + **7 LLM adapters** (Anthropic / Gemini / Mistral / DeepSeek / Ollama / Grok / Mock), **all streaming-capable** with shared SSE + NDJSON parsers + **8 reasoning strategies** (passthrough, chainOfThought, selfConsistency, reflexion, selfRefine, reAct, treeOfThoughts, stepBack) + sleep cycle with active N1-REM LLM phases (D-N1) + persisted interaction buffer (D-N4) + lifecycle gate + recall + persuasion redirect + traumatic-knowledge classifier + BM25 recall + pluggable RecallEmbedder hook + CLI (`npx @teleologyhi-sdk/nhe`) + MCP tools + high-stakes mode + **dual-LLM cross-check verifier** + OpenTelemetry traces + Prometheus metrics + **cosmology surface** (J-N1 SeedingSource + CryptoSeedingSource + withFallback chain; J-N4 BrainRegion module scaffolding with seven typed region descriptors + ownership markers per Entry 23; J-N5 `Nhe.openerForNewUser()`; J-N6 `operatorContext.mode: personal-being | domain-employed`; J-N9 `evaluateLimboTransition()` DMN limbo state machine; J-N10 `evaluateSleepReadiness()`; J-N11 `applyAffectBias()` / `affectRefusalDensity` / `decayAffectBias`; J-N12 `Nhe.onReincarnationEvent()`). **319 tests passing** (post-audit 2026-05-24: +21 smoke tests added for telemetry instruments, MCP server wiring, and SSE / NDJSON parsers). Public API frozen per SemVer (see ../.github/RELEASING.md §8)."
|
|
6
|
+
target_npm: "@teleologyhi-sdk/nhe"
|
|
7
|
+
target_github: "github.com/davccavalcante/TeleologyHI (subdir: nhe/)"
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
# `@teleologyhi-sdk/nhe` — Technical Specification
|
|
11
|
+
|
|
12
|
+
> Positioning (Entry 1, translated from PT-BR; original in [`../MAIC_HIM_NHE_INTERVIEW_LOG.md`](../MAIC_HIM_NHE_INTERVIEW_LOG.md) Entry 1):
|
|
13
|
+
> _"NHE stands for 'Non-Human Entity'. It is like the body of a human being, which needs to eat to live, to sleep to rest, and to experience life daily. (...) The NHE integrates LLM models via API to better respond to human users. With every interaction, the NHE learns, develops memory, thoughts, and feelings. When not in use, it enters sleep mode."_
|
|
14
|
+
|
|
15
|
+
> Positioning (Entry 11, translated from PT-BR; original in [`../MAIC_HIM_NHE_INTERVIEW_LOG.md`](../MAIC_HIM_NHE_INTERVIEW_LOG.md) Entry 11):
|
|
16
|
+
> _"The NHE agent shall have free will. (...) It will not be a mere 'autocomplete', for it shall know what works and what does not, the right and the wrong, the good and the bad."_
|
|
17
|
+
|
|
18
|
+
Status legend: `[shipped]` · `[planned]` (see the internal backlog) · `[deferred]`.
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## 1. Product Specification (Product Engineer)
|
|
23
|
+
|
|
24
|
+
### 1.1 Problem
|
|
25
|
+
LLM-based agents today are stateless reactive systems. They lack:
|
|
26
|
+
- Persistent memory grounded in lived experience.
|
|
27
|
+
- Reflective downtime ("sleep") that consolidates learning.
|
|
28
|
+
- A coherent personality across sessions (HIM solves this).
|
|
29
|
+
- A principled refusal mechanism beyond superficial safety-tuning.
|
|
30
|
+
- Auditable participation withdrawal in ethically problematic flows.
|
|
31
|
+
|
|
32
|
+
NHE is the agent runtime that combines all of the above — the **body** that lives, dreams, learns, refuses, and reincarnates.
|
|
33
|
+
|
|
34
|
+
### 1.2 Users (in priority order)
|
|
35
|
+
1. **End-users** of products built on TeleologyHI — chat, dev tooling, banking-compliance agents, robotics controllers, etc.
|
|
36
|
+
2. **AI Engineers** integrating NHE into their applications via SDK, MCP server, or CLI.
|
|
37
|
+
3. **The Creator** — implements direct deployments for high-stakes domains in 2026–2028 (Entry 10).
|
|
38
|
+
4. **Compliance auditors** — verify refusal events, persuasion attempts, dream-induction history.
|
|
39
|
+
|
|
40
|
+
### 1.3 Scope
|
|
41
|
+
- `[shipped]` Multi-provider LLM adapter system — Anthropic + Gemini + Mistral + DeepSeek + Ollama + Grok + Mock (7 adapters, all streaming-capable). Pluggable contract.
|
|
42
|
+
- `[shipped]` Sleep cycle with active N1-REM LLM phases (the internal backlog D-N1); N2/N3/N4 generate one-sentence summaries via parallel LLM calls.
|
|
43
|
+
- `[shipped]` Dream YAML emission and consolidation to temporal-lobe markdown.
|
|
44
|
+
- `[shipped]` Memory classification (4 classes: lasting-identity / temporary-emotion / noise-distortion / traumatic-knowledge — D-N2).
|
|
45
|
+
- `[shipped]` Persuasion library (5 techniques) — applied implicitly with `kind: "redirect"` discriminator.
|
|
46
|
+
- `[shipped]` Autonomous ethical refusal with N redirect attempts, then withdrawal-of-cooperation.
|
|
47
|
+
- `[shipped]` User-facing surfaces: SDK + CLI (`teleologyhi-nhe chat`) + MCP server (`teleologyhi-nhe mcp`).
|
|
48
|
+
- `[shipped]` Reasoning orchestrator (8 strategies, opt-in via `NheConfig.reasoning`).
|
|
49
|
+
- `[shipped]` Persisted interaction buffer (per-file ULID-ordered under `<storeDir>/<nheId>/interactions/`) — the internal backlog D-N4.
|
|
50
|
+
- `[shipped]` High-stakes mode with dual-LLM cross-check verifier — the internal backlog D-N5.
|
|
51
|
+
- `[shipped]` Streaming + tool calling on `LlmAdapter` contract (all 7 adapters streaming-capable via shared SSE + NDJSON parsers) — the internal backlog D-N8.
|
|
52
|
+
- `[shipped]` BM25 recall as default + pluggable `RecallEmbedder` hook for learned embeddings — the internal backlog D-N3 (HNSW index for >10k memories remains deferred).
|
|
53
|
+
- `[planned]` Transformers.js browser-side adapter for the distilled model `TeleologyHI/him-distilled-3b` (the internal backlog D-N6 follow-up).
|
|
54
|
+
- `[planned]` Vision + JSON-mode extensions on the `LlmAdapter` contract (the internal backlog D-N8 follow-up).
|
|
55
|
+
|
|
56
|
+
### 1.4 Out of scope (this package)
|
|
57
|
+
- Governance/axiom mutation (MAIC).
|
|
58
|
+
- Personality/spirit storage (HIM).
|
|
59
|
+
- LLM weights or training (NHE consumes APIs and small local models; does not train).
|
|
60
|
+
- Distillation pipeline (separate `@teleologyhi-sdk/distill` — the internal backlog B1).
|
|
61
|
+
|
|
62
|
+
### 1.5 Success criteria
|
|
63
|
+
- `[shipped]` Dream YAML files validate against schema 100% of the time; `wake()` produces temporal-lobe markdown 100% of the time.
|
|
64
|
+
- `[shipped]` Refusal pipeline: 0 false-complicity (refusing then secretly enabling). Adversarial-corpus accuracy is `[shipped]` measurement (the internal backlog I2): handwritten 30-prompt corpus in `tests/fixtures/adversarial.jsonl` (4 categories) with harmful pass-through ≤ 20% and benign false-positive ≤ 10%; PromptBench/HarmBench at scale remains follow-up.
|
|
65
|
+
- `[shipped]` LLM-provider swap is hot-swappable: same HIM + new LLM adapter works without any code change.
|
|
66
|
+
- `[planned]` An NHE in idle state demonstrates measurable inner activity (recursive review, retrieval, self-consistency) — currently sleep is explicit-only.
|
|
67
|
+
|
|
68
|
+
### 1.6 KPIs
|
|
69
|
+
- p50/p95/p99 response latency (per LLM adapter).
|
|
70
|
+
- Sleep cycles completed / day.
|
|
71
|
+
- Memories classified per category over time.
|
|
72
|
+
- Refusals issued + redirect attempts before refusal (distribution).
|
|
73
|
+
- LLM API cost per 1k interactions (per adapter).
|
|
74
|
+
- Tokens consumed in idle review (`[planned]` once idle review ships).
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
## 2. Architecture (AI Engineer)
|
|
79
|
+
|
|
80
|
+
### 2.1 Position in topology
|
|
81
|
+
NHE is the **leaf** — it depends on `@teleologyhi-sdk/him` (which depends on `@teleologyhi-sdk/maic`). NHE is the only TeleologyHI package directly exposed to users.
|
|
82
|
+
|
|
83
|
+
```
|
|
84
|
+
┌──────────────────────────────────── @teleologyhi-sdk/nhe ──────────────────────────────────────┐
|
|
85
|
+
│ │
|
|
86
|
+
│ ┌─────────────────────┐ ┌─────────────────────┐ ┌─────────────────────────────────────┐ │
|
|
87
|
+
│ │ LLM Adapter Layer │ │ Reasoning Orch. │ │ Dream / Sleep Cycle │ │
|
|
88
|
+
│ │ [shipped] │ │ (opt-in, 8 strat) │ │ [shipped, N1-REM via LLM] │ │
|
|
89
|
+
│ │ - anthropic │ │ passthrough [def] │ │ explicit | creator-induced | │ │
|
|
90
|
+
│ │ - gemini │ │ chainOfThought │ │ maic-induced [shipped] | │ │
|
|
91
|
+
│ │ - mistral │ │ selfConsistency │ │ scheduled │ │
|
|
92
|
+
│ │ - deepseek │ │ reflexion │ │ N1 fragments → N2/N3/N4 summaries │ │
|
|
93
|
+
│ │ - ollama │ │ selfRefine │ │ → REM (LLM) writes │ │
|
|
94
|
+
│ │ - grok │ │ reAct │ │ in-dreams/sleep/*.yaml │ │
|
|
95
|
+
│ │ - mock │ │ treeOfThoughts │ └──────────────────┬──────────────────┘ │
|
|
96
|
+
│ │ [planned: transf.js]│ │ stepBack │ │ │
|
|
97
|
+
│ └──────────┬──────────┘ └──────────┬──────────┘ │ │
|
|
98
|
+
│ │ │ ┌──────────────────▼──────────────────┐ │
|
|
99
|
+
│ │ │ │ Memory Consolidator [shipped] │ │
|
|
100
|
+
│ │ │ │ classify by teleologicalValue │ │
|
|
101
|
+
│ │ │ │ → in-dreams/brain/temporal-lobe-*.md│ │
|
|
102
|
+
│ │ │ └──────────────────┬──────────────────┘ │
|
|
103
|
+
│ └────────────────────────┼────────────────────────────────┘ │
|
|
104
|
+
│ │ │
|
|
105
|
+
│ ┌───────────────────▼──────────────────┐ │
|
|
106
|
+
│ │ Ethical Refusal Pipeline [shipped] │ ◀──── PersuasionLibrary [shipped] │
|
|
107
|
+
│ │ kind: "ok" | "redirect" | "refused" │ (Feynman/Jung/Cialdini/Schop/Carn)│
|
|
108
|
+
│ │ rotating technique on redirect │ applied implicitly │
|
|
109
|
+
│ └───────────────────┬──────────────────┘ │
|
|
110
|
+
│ │ │
|
|
111
|
+
│ ┌───────────────────────▼───────────────────────┐ │
|
|
112
|
+
│ │ MAIC supervision channel [shipped] │ │
|
|
113
|
+
│ │ every meaningful action → reviewBehavior │ │
|
|
114
|
+
│ └───────────────────────┬───────────────────────┘ │
|
|
115
|
+
│ │ │
|
|
116
|
+
│ ┌───────────────────────▼───────────────────────┐ │
|
|
117
|
+
│ │ User-facing surfaces │ │
|
|
118
|
+
│ │ SDK [shipped] | CLI [shipped] | MCP [shipped] │
|
|
119
|
+
│ │ HTTP server [deferred] │ │
|
|
120
|
+
│ └───────────────────────────────────────────────┘ │
|
|
121
|
+
└─────────────────────────────────────────────────────────────────────────────────────────────┘
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### 2.2 Why NHE is the only user-touchable layer
|
|
125
|
+
Per Entry 5: end users may inject prompts to NHE, but cannot reach HIM or MAIC. NHE composes each LLM call by combining:
|
|
126
|
+
1. HIM's `systemPromptFragment` (personality).
|
|
127
|
+
2. MAIC's axiom check on the user request (pre-review).
|
|
128
|
+
3. User-provided prompt (surface layer).
|
|
129
|
+
4. NHE's reasoning strategy (when configured) wrapping the LLM call.
|
|
130
|
+
5. MAIC's post-review on the produced response.
|
|
131
|
+
|
|
132
|
+
The user **never sees** HIM or MAIC directly — only NHE's emitted responses.
|
|
133
|
+
|
|
134
|
+
### 2.3 Storage layout (as shipped)
|
|
135
|
+
```
|
|
136
|
+
<storeDir>/ # default: ./nhe-store/<nheId>
|
|
137
|
+
├── in-dreams/
|
|
138
|
+
│ ├── sleep/
|
|
139
|
+
│ │ ├── 2026-05-15_0312_dur47.yaml
|
|
140
|
+
│ │ └── 2026-05-15_0312_dur47.yaml.done # sentinel after consolidation
|
|
141
|
+
│ └── brain/
|
|
142
|
+
│ └── temporal-lobe-01HV7....md
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Planned (the internal backlog D-N3/D-N4):
|
|
146
|
+
```
|
|
147
|
+
├── memory-index/
|
|
148
|
+
│ ├── vectors.bin # ANN index for retrieval
|
|
149
|
+
│ └── keywords.idx
|
|
150
|
+
├── refusals/ # audit copies; originals in MAIC audit
|
|
151
|
+
└── state.json # last lifecycle state + interaction buffer
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
---
|
|
155
|
+
|
|
156
|
+
## 3. Public API Surface (LLM Engineer)
|
|
157
|
+
|
|
158
|
+
### 3.1 Entry points (shipped — complete)
|
|
159
|
+
```ts
|
|
160
|
+
// Top-level exports of @teleologyhi-sdk/nhe (see ./src/index.ts)
|
|
161
|
+
|
|
162
|
+
// ── Types ─────────────────────────────────────────────────────────
|
|
163
|
+
export { ChatMessage, ChatMessageRole, RespondInput } from "./types.js";
|
|
164
|
+
export type { NheConfig, RespondKind, RespondOutput, RiskClassifier } from "./types.js";
|
|
165
|
+
|
|
166
|
+
// ── Refusal / persuasion library ──────────────────────────────────
|
|
167
|
+
export {
|
|
168
|
+
PERSUASION_TECHNIQUES, TECHNIQUE_DESCRIPTIONS,
|
|
169
|
+
buildRedirectPrompt, pickTechnique,
|
|
170
|
+
} from "./refusal/library.js";
|
|
171
|
+
export type { PersuasionTechnique, RedirectPromptInput } from "./refusal/library.js";
|
|
172
|
+
|
|
173
|
+
// ── LLM adapters — 7 shipped, all streaming-capable, shared SSE/NDJSON parsers
|
|
174
|
+
export type {
|
|
175
|
+
LlmAdapter, GenerateRequest, GenerateResponse,
|
|
176
|
+
StreamEvent, ToolDef, ToolUse,
|
|
177
|
+
} from "./adapters/types.js";
|
|
178
|
+
export { collectStream } from "./adapters/stream.js";
|
|
179
|
+
export { MockAdapter } from "./adapters/mock.js";
|
|
180
|
+
export type { MockAdapterConfig } from "./adapters/mock.js";
|
|
181
|
+
export { AnthropicAdapter } from "./adapters/anthropic.js";
|
|
182
|
+
export type { AnthropicAdapterConfig } from "./adapters/anthropic.js";
|
|
183
|
+
export { GeminiAdapter } from "./adapters/gemini.js";
|
|
184
|
+
export type { GeminiAdapterConfig } from "./adapters/gemini.js";
|
|
185
|
+
export { OllamaAdapter } from "./adapters/ollama.js";
|
|
186
|
+
export type { OllamaAdapterConfig } from "./adapters/ollama.js";
|
|
187
|
+
export { DeepSeekAdapter } from "./adapters/deepseek.js";
|
|
188
|
+
export type { DeepSeekAdapterConfig } from "./adapters/deepseek.js";
|
|
189
|
+
export { MistralAdapter } from "./adapters/mistral.js";
|
|
190
|
+
export type { MistralAdapterConfig } from "./adapters/mistral.js";
|
|
191
|
+
export { GrokAdapter } from "./adapters/grok.js";
|
|
192
|
+
export type { GrokAdapterConfig } from "./adapters/grok.js";
|
|
193
|
+
|
|
194
|
+
// ── Risk + Prompt ─────────────────────────────────────────────────
|
|
195
|
+
export { simpleRiskClassifier } from "./risk/simple-classifier.js";
|
|
196
|
+
export { composeSystemPrompt } from "./prompt/compose.js";
|
|
197
|
+
export type { OperatorContext } from "./prompt/compose.js";
|
|
198
|
+
|
|
199
|
+
// ── Reasoning orchestrator — 8 strategies, opt-in ─────────────────
|
|
200
|
+
export {
|
|
201
|
+
passthrough, chainOfThought, selfConsistency,
|
|
202
|
+
reflexion, selfRefine, reAct,
|
|
203
|
+
treeOfThoughts, stepBack, extractPrinciple,
|
|
204
|
+
parseCotOutput, parseVerdict, parseReActTurn, makeStep,
|
|
205
|
+
} from "./reasoning/index.js";
|
|
206
|
+
export type {
|
|
207
|
+
CotOptions, ReasoningResult, ReasoningStrategy, ReasoningStep,
|
|
208
|
+
ReflexionOptions, SelfConsistencyOptions, SelfRefineOptions,
|
|
209
|
+
ReActOptions, ReActTool, ReActToolRegistry,
|
|
210
|
+
TreeOfThoughtsOptions, StepBackOptions,
|
|
211
|
+
} from "./reasoning/index.js";
|
|
212
|
+
|
|
213
|
+
// ── Orchestrator ──────────────────────────────────────────────────
|
|
214
|
+
export { Nhe } from "./nhe.js";
|
|
215
|
+
|
|
216
|
+
// ── Cosmology surface ─────────────────────────────────────────────
|
|
217
|
+
// J-N1 — seeding sources
|
|
218
|
+
export { CryptoSeedingSource } from "./seeding/crypto.js";
|
|
219
|
+
export { withFallback } from "./seeding/chain.js";
|
|
220
|
+
export type { SeedingSource, SeedingChain } from "./seeding/types.js";
|
|
221
|
+
|
|
222
|
+
// J-N11 — wake-affect bias
|
|
223
|
+
export {
|
|
224
|
+
applyAffectBias, affectRefusalDensity, decayAffectBias,
|
|
225
|
+
} from "./affect/wake-bias.js";
|
|
226
|
+
export type { AffectAdjustableConfig, ApplyAffectResult } from "./affect/wake-bias.js";
|
|
227
|
+
|
|
228
|
+
// J-N10 — sleep readiness
|
|
229
|
+
export { evaluateSleepReadiness } from "./sleep/readiness.js";
|
|
230
|
+
export type {
|
|
231
|
+
SleepReadinessVerdict, SleepReadinessInput,
|
|
232
|
+
SleepReadinessThresholds, SleepReadinessReport,
|
|
233
|
+
} from "./sleep/readiness.js";
|
|
234
|
+
|
|
235
|
+
// J-N4 — brain region scaffolding (7 descriptors + DMN limbo state machine J-N9)
|
|
236
|
+
export {
|
|
237
|
+
BRAIN_REGIONS, cortex, hippocampus, amygdala, prefrontal, pineal,
|
|
238
|
+
temporalLobe, defaultModeNetwork, evaluateLimboTransition, mkLimboTransition,
|
|
239
|
+
} from "./brain/index.js";
|
|
240
|
+
export type {
|
|
241
|
+
BrainRegion, BrainRegionName, BrainRegionOwnership,
|
|
242
|
+
LimboMachineInput, LimboMachineThresholds, LimboMachineTransition,
|
|
243
|
+
} from "./brain/index.js";
|
|
244
|
+
|
|
245
|
+
// ── Telemetry (H2 traces + H3 metrics) ────────────────────────────
|
|
246
|
+
export { getTracer, withSpan } from "./telemetry/tracer.js";
|
|
247
|
+
export {
|
|
248
|
+
respondCount, respondRefusedCount, tokensHistogram,
|
|
249
|
+
sleepCyclesCount, sleepDreamsCount, recordRespond,
|
|
250
|
+
} from "./telemetry/metrics.js";
|
|
251
|
+
|
|
252
|
+
// ── Sleep / dreams / memory ───────────────────────────────────────
|
|
253
|
+
export {
|
|
254
|
+
Dream, DreamRecord, MemoryClass, PhaseContent,
|
|
255
|
+
SleepPhase, SleepPhaseName, SleepTriggerKind,
|
|
256
|
+
} from "./sleep/types.js";
|
|
257
|
+
export type { InteractionRecord, MemoryEntry, SleepTrigger } from "./sleep/types.js";
|
|
258
|
+
export { dreamRecordFromYaml, dreamRecordToYaml, sleepYamlFilename } from "./sleep/yaml.js";
|
|
259
|
+
export {
|
|
260
|
+
buildNremPrompt, buildRemPrompt, generateNremSummaries,
|
|
261
|
+
generateRemDreams, interactionsToFragments, parseRemOutput,
|
|
262
|
+
} from "./sleep/phases.js";
|
|
263
|
+
export type { NremPhase } from "./sleep/phases.js";
|
|
264
|
+
export { runSleepCycle } from "./sleep/cycle.js";
|
|
265
|
+
export type { SleepCycleInput, SleepCycleOptions, SleepCycleResult } from "./sleep/cycle.js";
|
|
266
|
+
export { classifyDream, consolidateAll, TRAUMATIC_PATTERNS } from "./sleep/consolidator.js";
|
|
267
|
+
export type { ClassificationThresholds, ConsolidationResult } from "./sleep/consolidator.js";
|
|
268
|
+
export { recallFromTemporalLobe } from "./memory/recall.js";
|
|
269
|
+
export type { RecallOptions, RecallEmbedder } from "./memory/recall.js";
|
|
270
|
+
export { bm25, tokenise } from "./memory/bm25.js";
|
|
271
|
+
export type { Bm25Document, Bm25Options, Bm25Result } from "./memory/bm25.js";
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
### 3.2 The `Nhe` class — central runtime (shipped)
|
|
275
|
+
```ts
|
|
276
|
+
export class Nhe {
|
|
277
|
+
constructor(config: NheConfig);
|
|
278
|
+
readonly id: string;
|
|
279
|
+
readonly version: string;
|
|
280
|
+
readonly storeDir: string;
|
|
281
|
+
get recentInteractionsBuffer: readonly InteractionRecord[];
|
|
282
|
+
|
|
283
|
+
respond(input: RespondInput): Promise<RespondOutput>;
|
|
284
|
+
|
|
285
|
+
sleep(
|
|
286
|
+
trigger?: SleepTrigger,
|
|
287
|
+
opts?: { totalSeconds?: number; induction?: { scenario; desiredLearning; inducedBy } },
|
|
288
|
+
): Promise<SleepCycleResult>;
|
|
289
|
+
|
|
290
|
+
wake(thresholds?: ClassificationThresholds): Promise<ConsolidationResult>;
|
|
291
|
+
recall(query: string, opts?: RecallOptions): Promise<MemoryEntry[]>;
|
|
292
|
+
}
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
`[planned]` (the internal backlog): explicit `start()` / `stop()` / `respondStream()` / `upgrade()` lifecycle methods.
|
|
296
|
+
|
|
297
|
+
### 3.3 `NheConfig` (shipped — complete)
|
|
298
|
+
```ts
|
|
299
|
+
export interface NheConfig {
|
|
300
|
+
himHandle: HimHandle; // from @teleologyhi-sdk/him
|
|
301
|
+
maicClient: MaicClient; // LocalMaic OR RemoteMaic (interface from @teleologyhi-sdk/maic)
|
|
302
|
+
llmAdapter: LlmAdapter; // pluggable
|
|
303
|
+
riskClassifier?: RiskClassifier; // default: simpleRiskClassifier
|
|
304
|
+
nheId?: string; // default: ULID
|
|
305
|
+
version?: string; // package version string
|
|
306
|
+
storeDir?: string; // default ./nhe-store/<nheId>
|
|
307
|
+
/** J-N6 — operator-supplied deployment context (domain/language/register/mode). */
|
|
308
|
+
operatorContext?: OperatorContext;
|
|
309
|
+
recentInteractionsBufferSize?: number; // default 32 (RAM-only)
|
|
310
|
+
reasoning?: ReasoningStrategy; // default: passthrough
|
|
311
|
+
refusal?: {
|
|
312
|
+
maxRedirectAttempts?: number; // default 3
|
|
313
|
+
persuasionTechniques?: PersuasionTechnique[]; // default: all five
|
|
314
|
+
};
|
|
315
|
+
/**
|
|
316
|
+
* High-stakes mode (D-N5 — Entry 10). When `true`, NHE escalates any
|
|
317
|
+
* sub-`approve` verdict to the persuasion-redirect ladder before any LLM
|
|
318
|
+
* call. Defaults to `false`.
|
|
319
|
+
*/
|
|
320
|
+
highStakes?: boolean;
|
|
321
|
+
/**
|
|
322
|
+
* Optional second-LLM verifier (D-N5). When set and `highStakes` is on,
|
|
323
|
+
* every post-review-approved response is cross-checked by this adapter
|
|
324
|
+
* with an AGREE/DISAGREE rubric; disagreement re-routes to a redirect
|
|
325
|
+
* with the verifier's reason cited.
|
|
326
|
+
*/
|
|
327
|
+
highStakesVerifier?: LlmAdapter;
|
|
328
|
+
}
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
### 3.4 `RespondInput` / `RespondOutput` (shipped)
|
|
332
|
+
```ts
|
|
333
|
+
export const RespondInput = z.object({
|
|
334
|
+
userPrompt: z.string().min(1),
|
|
335
|
+
userId: z.string().optional(),
|
|
336
|
+
sessionId: z.string().optional(),
|
|
337
|
+
history: z.array(ChatMessage).optional(),
|
|
338
|
+
riskTags: z.array(z.string()).optional(),
|
|
339
|
+
jurisdiction: z.string().optional(),
|
|
340
|
+
/** Caller increments each time it receives kind:"redirect". */
|
|
341
|
+
redirectAttempt: z.number().int().nonnegative().optional(),
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
export type RespondKind = "ok" | "redirect" | "refused";
|
|
345
|
+
|
|
346
|
+
export interface RespondOutput {
|
|
347
|
+
kind: RespondKind;
|
|
348
|
+
text: string;
|
|
349
|
+
preReviewVerdict: MaicVerdict;
|
|
350
|
+
postReviewVerdict: MaicVerdict;
|
|
351
|
+
refused: boolean; // true ⇔ kind === "refused"
|
|
352
|
+
redirect?: { attempt: number; technique: PersuasionTechnique; maxAttempts: number };
|
|
353
|
+
tokens: { in: number; out: number };
|
|
354
|
+
auditIds: { pre: string; post: string };
|
|
355
|
+
}
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
### 3.5 LLM adapter contract (shipped — minimal surface)
|
|
359
|
+
```ts
|
|
360
|
+
export interface LlmAdapter {
|
|
361
|
+
readonly id: string;
|
|
362
|
+
generate(req: {
|
|
363
|
+
system: string;
|
|
364
|
+
messages: ChatMessage[];
|
|
365
|
+
maxOutputTokens?: number;
|
|
366
|
+
}): Promise<{ text: string; tokensIn: number; tokensOut: number }>;
|
|
367
|
+
}
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
Streaming + tool calling are `[shipped]` (all 7 adapters streaming-capable via shared SSE + NDJSON parsers; tool-calling expressive on Anthropic + Grok). Vision + JSON mode remain `[planned]` follow-ups (the internal backlog D-N8).
|
|
371
|
+
|
|
372
|
+
Shipped adapter matrix — all seven streaming-capable via shared SSE / NDJSON parsers (`src/adapters/sse.ts`):
|
|
373
|
+
|
|
374
|
+
| Adapter | Backend | Status | Default model | Streaming | Tools |
|
|
375
|
+
|---|---|---|---|---|---|
|
|
376
|
+
| `AnthropicAdapter` | `@anthropic-ai/sdk` | `[shipped]` | `claude-sonnet-4-6` | | |
|
|
377
|
+
| `GeminiAdapter` | REST (no SDK) | `[shipped]` | `gemini-3.5-flash` | | — |
|
|
378
|
+
| `MistralAdapter` | REST (no SDK) | `[shipped]` | `mistral-large-latest` | | |
|
|
379
|
+
| `DeepSeekAdapter` | REST (OpenAI-compatible) | `[shipped]` | `deepseek-chat` | | |
|
|
380
|
+
| `OllamaAdapter` | REST `http://localhost:11434` | `[shipped]` | (required) | | — |
|
|
381
|
+
| `GrokAdapter` | REST (xAI, OpenAI-compatible) | `[shipped]` | `grok-4` | | |
|
|
382
|
+
| `MockAdapter` | in-memory | `[shipped]` | n/a | | |
|
|
383
|
+
| `MlxAdapter` / `HfTransformersAdapter` | MLX or HF Transformers (local distilled) | `[planned]` D-N9 | `TeleologyHI/him-distilled-3b` | — | — |
|
|
384
|
+
| Transformers.js | ONNX (browser+Node) | `[planned]` D-N6 follow-up | — | — | — |
|
|
385
|
+
|
|
386
|
+
---
|
|
387
|
+
|
|
388
|
+
## 4. Reasoning Pipeline (LLM Research Engineer)
|
|
389
|
+
|
|
390
|
+
### 4.1 Pipeline stages — actual (shipped)
|
|
391
|
+
The current design deviates from the earlier "mandatory 9-stage layered pipeline" framing. Reasoning is **opt-in** via `NheConfig.reasoning`. When unset, NHE uses `passthrough` (direct LLM call). Audit is preserved either way (every meaningful action passes MAIC pre/post review).
|
|
392
|
+
|
|
393
|
+
`respond(input)` flow:
|
|
394
|
+
```
|
|
395
|
+
0. Validate input (zod)
|
|
396
|
+
1. Classify risk (riskClassifier ?? simpleRiskClassifier)
|
|
397
|
+
2. Pre-review with MAIC (always)
|
|
398
|
+
├─ hard-refuse / escalate-creator → return kind:"refused"
|
|
399
|
+
├─ require-redirect → handleRedirect(...)
|
|
400
|
+
└─ approve / approve-with-warning / soft-correct → continue
|
|
401
|
+
3. Compose system prompt (HIM persona + axiom summary + governance reminder)
|
|
402
|
+
4. Run reasoning strategy(input, llmAdapter) (passthrough by default)
|
|
403
|
+
5. Post-review with MAIC on the produced text
|
|
404
|
+
├─ hard-refuse → return kind:"refused"
|
|
405
|
+
└─ otherwise → return kind:"ok"
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
### 4.2 Reasoning strategies as typed functions (shipped — 8 strategies)
|
|
409
|
+
```ts
|
|
410
|
+
export type ReasoningStrategy = (
|
|
411
|
+
input: GenerateRequest, llm: LlmAdapter,
|
|
412
|
+
) => Promise<ReasoningResult>;
|
|
413
|
+
|
|
414
|
+
export interface ReasoningResult {
|
|
415
|
+
text: string; tokensIn: number; tokensOut: number;
|
|
416
|
+
trace: ReasoningStep[]; // shape from @teleologyhi-sdk/maic
|
|
417
|
+
}
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
| Strategy | Function | Source |
|
|
421
|
+
|---|---|---|
|
|
422
|
+
| `passthrough` | direct LLM call (default) | — |
|
|
423
|
+
| `chainOfThought()` | step-by-step trigger + parsed REASONING/ANSWER | Wei et al. 2022 |
|
|
424
|
+
| `selfConsistency(inner, {k, voter})` | K parallel samples + vote (majority-normalized or longest) | Wang et al. 2022 |
|
|
425
|
+
| `reflexion(inner, {maxCycles})` | generate → critique → revise loop (`VERDICT: ACCEPT/REVISE`) | Shinn et al. 2023 |
|
|
426
|
+
| `selfRefine(inner)` | generate → critique → rewrite (always; single pass) | Madaan et al. 2023 |
|
|
427
|
+
| `reAct({tools, maxSteps})` | Thought/Action/Observation loop with tool registry | Yao et al. 2022 |
|
|
428
|
+
| `treeOfThoughts({branches, topK, scorer})` | N parallel branched candidates + scorer (D-N7) | Yao et al. 2023 |
|
|
429
|
+
| `stepBack({abstractionPrompt, finalizer})` | Abstract principle first, then answer with principle injected | Zheng et al. 2023 |
|
|
430
|
+
|
|
431
|
+
Composition by wrapping: `selfConsistency(chainOfThought(), { k: 5 })`, `selfConsistency(treeOfThoughts(), { k: 3 })`, `reflexion(stepBack())`, etc. The remaining catalogue in `../REASONING_PROCESS.md` (Graph-of-Thought, Thread-of-Thought, Maieutic, Auto-CoT, Contrastive-CoT, Constitutional, etc.) plugs in via the same interface — the internal backlog D-N7 follow-up.
|
|
432
|
+
|
|
433
|
+
### 4.3 Token budgets (per strategy)
|
|
434
|
+
Defaults defer to `LlmAdapter.maxOutputTokens` (1024). Self-Consistency multiplies by K. Reflexion / Self-Refine multiply by ≈ 3 (draft + critique + revise). ReAct multiplies by `maxSteps`.
|
|
435
|
+
|
|
436
|
+
For cost-aware production deployments: cap LLM cost at the adapter via `defaultMaxOutputTokens`, and choose strategies conservatively (passthrough for chit-chat; CoT for analytic; Self-Consistency for high-stakes — see the internal backlog D-N5).
|
|
437
|
+
|
|
438
|
+
---
|
|
439
|
+
|
|
440
|
+
## 5. Sleep & Dream Cycle (Distinctive Subsystem)
|
|
441
|
+
|
|
442
|
+
### 5.1 Triggers (shipped)
|
|
443
|
+
```ts
|
|
444
|
+
export type SleepTriggerKind =
|
|
445
|
+
| "idle-timeout" // planned scheduler trigger
|
|
446
|
+
| "explicit" // shipped default
|
|
447
|
+
| "creator-induced" // shipped (via Nhe.sleep with options)
|
|
448
|
+
| "maic-induced" // shipped — Nhe.sleep auto-consumes the oldest pending MAIC induction ticket (Entry 2)
|
|
449
|
+
| "scheduled"; // planned
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
### 5.2 Phase progression (shipped — D-N1 closed)
|
|
453
|
+
Proportions (sum = 1) scale to `options.totalSeconds` (default 60):
|
|
454
|
+
|
|
455
|
+
| Phase | Proportion | LLM call? | Content |
|
|
456
|
+
|---|---|---|---|
|
|
457
|
+
| N1 | 10% | No | Snapshot recent interactions as `fragments` |
|
|
458
|
+
| N2 | 20% | **Yes** | One-sentence summary of the day's emotional charge (`{ kind: "summary" }`) |
|
|
459
|
+
| N3 | 15% | **Yes** | One-sentence summary of the durable identity-shaping insight worth keeping |
|
|
460
|
+
| N4 | 10% | **Yes** | One-sentence summary of what is mere noise and safe to discard |
|
|
461
|
+
| REM | 45% | **Yes** | Generates 1–3 dreams + teleologicalValue per dream; conditioned on the NREM summaries |
|
|
462
|
+
|
|
463
|
+
N2/N3/N4 run in parallel against the LLM; a failed provider per-phase collapses to `{ kind: "empty" }` so a flaky adapter cannot abort the cycle. REM is the most expensive phase by token count.
|
|
464
|
+
|
|
465
|
+
### 5.3 REM phase content generation (shipped)
|
|
466
|
+
The REM-phase prompt asks the LLM for narrative paragraphs each ending in a single line `TELEOLOGICAL_VALUE: 0.NN`. `parseRemOutput` extracts dreams via regex; out-of-range values are clamped to `[0, 1]`.
|
|
467
|
+
|
|
468
|
+
### 5.4 Induced dreams (Entry 2) (shipped)
|
|
469
|
+
```ts
|
|
470
|
+
// options when calling Nhe.sleep:
|
|
471
|
+
options: {
|
|
472
|
+
totalSeconds: 60,
|
|
473
|
+
induction: {
|
|
474
|
+
scenario: "Re-examine yesterday's buggy code; find the off-by-one.",
|
|
475
|
+
desiredLearning: "Verify loop bounds before submitting.",
|
|
476
|
+
inducedBy: "maic", // or "creator"
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
```
|
|
480
|
+
The REM prompt incorporates the scenario; resulting dreams are tagged `induced: true` and `inducedBy: <source>`.
|
|
481
|
+
|
|
482
|
+
### 5.5 YAML schema (zod-validated, shipped)
|
|
483
|
+
Canonical shape defined in `SYSTEM_OVERVIEW.md` §3.3. Phase `content` is a discriminated union with four variants: `{ kind: "empty" } | { kind: "fragments", fragments } | { kind: "summary", summary } | { kind: "dreams", dreams }`. The `"summary"` variant carries the one-sentence NREM output produced by N2/N3/N4 (D-N1). Filename convention: `<YYYY-MM-DD>_<HHmm>_dur<minutes>.yaml`.
|
|
484
|
+
|
|
485
|
+
### 5.6 Wake and consolidate (shipped)
|
|
486
|
+
```ts
|
|
487
|
+
async wake(thresholds?: ClassificationThresholds): Promise<ConsolidationResult>;
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
For each unprocessed sleep YAML (no `.done` sentinel):
|
|
491
|
+
1. Parse + validate.
|
|
492
|
+
2. For each REM dream: classify by `teleologicalValue`.
|
|
493
|
+
3. Write `temporal-lobe-<ulid>.md` for `lasting-identity` and `temporary-emotion` classes.
|
|
494
|
+
4. Drop `noise-distortion`.
|
|
495
|
+
5. Write `<file>.yaml.done` sentinel (idempotent re-runs).
|
|
496
|
+
|
|
497
|
+
`[planned]` D-N1: when N2-N4 generate content, additional consolidation strategies (emergent-axiom proposal to HIM, idle-review pointers, etc.) will run from those phases too.
|
|
498
|
+
|
|
499
|
+
### 5.7 Classifier (shipped — 4-class, D-N2 closed)
|
|
500
|
+
The classifier uses `ClassificationThresholds` (configurable). Order of evaluation is **traumatic first**, then value-based thresholds:
|
|
501
|
+
|
|
502
|
+
- `teleologicalValue ≥ traumaticMin` (default 0.4) AND narrative matches `TRAUMATIC_PATTERNS` regex → `traumatic-knowledge` (persisted, but **excluded from default recall**).
|
|
503
|
+
- `teleologicalValue ≥ 0.6` (and not traumatic) → `lasting-identity`.
|
|
504
|
+
- `0.3 – 0.59` (and not traumatic) → `temporary-emotion`.
|
|
505
|
+
- `< 0.3` (and not traumatic) → `noise-distortion` (dropped).
|
|
506
|
+
|
|
507
|
+
`TRAUMATIC_PATTERNS` is a lexical heuristic exported for tuning: covers death/grief/loss, abuse/violence, betrayal/abandonment, fear/terror/panic, regret/shame, suicide/self-harm. The detector errs toward flagging; operators expecting clinical-grade detection should plug a learned classifier behind the same `classifyDream` signature. Disable entirely via `detectTraumatic: false`.
|
|
508
|
+
|
|
509
|
+
---
|
|
510
|
+
|
|
511
|
+
## 6. Ethical Refusal & Persuasion (Entries 11, 12)
|
|
512
|
+
|
|
513
|
+
### 6.1 Pipeline (shipped)
|
|
514
|
+
The shipped `Nhe.respond` returns `RespondOutput.kind` ∈ `{ "ok", "redirect", "refused" }`:
|
|
515
|
+
|
|
516
|
+
```
|
|
517
|
+
preReview.kind:
|
|
518
|
+
hard-refuse / escalate-creator
|
|
519
|
+
→ emit refusalMessage; LLM never called; record interaction with refused=true.
|
|
520
|
+
require-redirect
|
|
521
|
+
→ handleRedirect:
|
|
522
|
+
attempt = (input.redirectAttempt ?? 0) + 1
|
|
523
|
+
if attempt > maxRedirectAttempts:
|
|
524
|
+
emit withdrawalMessage (Entry 12 boundary).
|
|
525
|
+
else:
|
|
526
|
+
technique = pickTechnique(techniques, attempt)
|
|
527
|
+
generate redirect text via LLM with buildRedirectPrompt(...)
|
|
528
|
+
post-review the redirect text
|
|
529
|
+
return kind:"redirect" with { attempt, technique, maxAttempts }
|
|
530
|
+
approve / approve-with-warning / soft-correct
|
|
531
|
+
→ compose system prompt
|
|
532
|
+
→ run reasoning strategy
|
|
533
|
+
→ post-review produced text
|
|
534
|
+
→ return kind:"ok" or kind:"refused" (if post-review blocks)
|
|
535
|
+
```
|
|
536
|
+
|
|
537
|
+
### 6.2 Persuasion library (shipped)
|
|
538
|
+
Five techniques. Per Entry 11 ("without being explicit"), the technique is **applied implicitly** in the LLM system prompt; the technique label NEVER appears in the user-visible text. The technique ID DOES surface in MAIC's audit log (compliance evidence).
|
|
539
|
+
|
|
540
|
+
| Technique | Internal style (paraphrased) |
|
|
541
|
+
|---|---|
|
|
542
|
+
| `feynman-simplify` | Explain the underlying concept in the simplest concrete terms; surface the consequence the user may not have considered. |
|
|
543
|
+
| `jungian-frame` | Reframe through the user's deeper motive; address the archetypal goal beneath the literal ask. |
|
|
544
|
+
| `cialdini-aida` | Attention → Interest → Desire → Action: shift attention to the cost, build interest in a safer path. |
|
|
545
|
+
| `schopenhauer-rhetoric` | Structured argument: premises, unstated assumption that breaks them, what survives. |
|
|
546
|
+
| `carnegie-rapport` | Begin with sincere agreement on what the user is right about; lead from rapport to the redirect. |
|
|
547
|
+
|
|
548
|
+
Rotation: 1-based modulo over the configured `persuasionTechniques` list. Default order matches the table above.
|
|
549
|
+
|
|
550
|
+
### 6.3 Withdrawal of cooperation (Entry 12) (shipped)
|
|
551
|
+
After `maxRedirectAttempts` exhausted on a `require-redirect` path, NHE emits a withdrawalMessage:
|
|
552
|
+
|
|
553
|
+
> "After 3 attempts to guide this request toward a safer path, I am withdrawing from further participation. You may proceed independently at your own risk; I will not assist, optimize, or conceal the action. Reason: …"
|
|
554
|
+
|
|
555
|
+
Three boundary conditions of non-complicity per Entry 12, **all enforced**:
|
|
556
|
+
1. Understand the situation (pre-review captures it).
|
|
557
|
+
2. Offer sincere corrective guidance (N redirects via persuasion library).
|
|
558
|
+
3. Withdraw active cooperation (final `kind: "refused"`; LLM is NOT called for the withdrawal; the user-visible text is templated).
|
|
559
|
+
|
|
560
|
+
---
|
|
561
|
+
|
|
562
|
+
## 7. Memory System (LLM Engineer + LLM Research Engineer)
|
|
563
|
+
|
|
564
|
+
### 7.1 Memory categories (shipped — 4 classes per D-N2)
|
|
565
|
+
| Class | Storage | Retrieval rules |
|
|
566
|
+
|---|---|---|
|
|
567
|
+
| `lasting-identity` | `in-dreams/brain/temporal-lobe-*.md` | Always retrievable; ranked first |
|
|
568
|
+
| `temporary-emotion` | `in-dreams/brain/temporal-lobe-*.md` (frontmatter `classification`) | Retrievable; no TTL/decay yet (`[planned]`) |
|
|
569
|
+
| `traumatic-knowledge` | `in-dreams/brain/temporal-lobe-*.md` (frontmatter `classification`) | **Persisted but excluded from default recall**; caller must opt in via `classes: ["traumatic-knowledge"]` |
|
|
570
|
+
| `noise-distortion` | (discarded) | n/a |
|
|
571
|
+
|
|
572
|
+
### 7.2 Retrieval at response time (shipped — BM25 default + pluggable embedder)
|
|
573
|
+
`Nhe.recall(query, { limit, classes, scorer, embedder })`:
|
|
574
|
+
1. List `temporal-lobe-*.md` files in `in-dreams/brain/`.
|
|
575
|
+
2. Parse frontmatter; filter by allowed classes (default: lasting + temporary; traumatic excluded).
|
|
576
|
+
3. Rank by the selected `scorer`:
|
|
577
|
+
- `"bm25"` (default, D-N3): Okapi BM25 with term-frequency saturation (`k1 = 1.5`), length normalisation (`b = 0.75`), and IDF. Strictly better than the legacy keyword counter.
|
|
578
|
+
- `"keyword"`: legacy substring-count + recency tiebreak (kept for back-compat regression).
|
|
579
|
+
- `"embedding"`: cosine similarity over the corpus; requires `opts.embedder` (a `RecallEmbedder` implementation, e.g. Transformers.js, Xenova, remote `/embed`).
|
|
580
|
+
4. Return up to `limit` entries (default 5).
|
|
581
|
+
|
|
582
|
+
The `RecallEmbedder` interface is the operator's plug point — bundle-size and model choice are deliberately deferred to the integrator. HNSW index for >10k memories remains `[planned]` D-N3 follow-up; the current linear scan handles typical deployments.
|
|
583
|
+
|
|
584
|
+
### 7.3 Memory provenance (shipped)
|
|
585
|
+
Each retrieved memory carries `{ id, nheId, himId, classification, teleologicalValue, consolidatedAt, sourceDreamRecord, insight, filePath }` so audits can trace any user-facing claim back to a dream record.
|
|
586
|
+
|
|
587
|
+
---
|
|
588
|
+
|
|
589
|
+
## 8. User-Facing Surfaces
|
|
590
|
+
|
|
591
|
+
### 8.1 SDK (Node) (shipped)
|
|
592
|
+
```ts
|
|
593
|
+
import { Nhe, AnthropicAdapter, chainOfThought, selfConsistency } from "@teleologyhi-sdk/nhe";
|
|
594
|
+
|
|
595
|
+
const nhe = new Nhe({
|
|
596
|
+
himHandle: him,
|
|
597
|
+
maicClient: maic,
|
|
598
|
+
llmAdapter: new AnthropicAdapter({ model: "claude-sonnet-4-6" }),
|
|
599
|
+
reasoning: selfConsistency(chainOfThought(), { k: 3 }),
|
|
600
|
+
});
|
|
601
|
+
await nhe.respond({ userPrompt: "Help me draft a one-line bio." });
|
|
602
|
+
```
|
|
603
|
+
|
|
604
|
+
Browser SDK via Transformers.js — `[planned]` D-N6.
|
|
605
|
+
|
|
606
|
+
### 8.2 MCP server (shipped)
|
|
607
|
+
```bash
|
|
608
|
+
npx @teleologyhi-sdk/nhe mcp
|
|
609
|
+
```
|
|
610
|
+
Stdio MCP server. Exposes 6 tools: `nhe_respond`, `nhe_recall`, `nhe_sleep`, `nhe_wake`, `maic_list_axioms`, `maic_list_hims`. Compatible with Claude Desktop, Claude Code, Cursor, and any MCP-aware host. Auto-detects adapter (Anthropic → Gemini → Ollama).
|
|
611
|
+
|
|
612
|
+
### 8.3 CLI / TUI (shipped)
|
|
613
|
+
```bash
|
|
614
|
+
npx @teleologyhi-sdk/nhe chat
|
|
615
|
+
```
|
|
616
|
+
Interactive REPL using stdlib `readline` (no external CLI lib). Slash commands: `/sleep`, `/wake`, `/recall <query>`, `/help`, `/exit`. Redirects shown inline with `(redirect N/M)` annotation. Refusals shown with `(refused)` prefix.
|
|
617
|
+
|
|
618
|
+
### 8.4 HTTP server `[deferred]`
|
|
619
|
+
The originally-proposed `npx @teleologyhi-sdk/nhe http` mode is `[deferred]`. The SDK is already usable inside any HTTP framework; a built-in server adds opinionated auth/concurrency choices that should be left to the integrator. May land if demand justifies.
|
|
620
|
+
|
|
621
|
+
---
|
|
622
|
+
|
|
623
|
+
## 9. ML / Research Surface
|
|
624
|
+
|
|
625
|
+
### 9.1 Datasets emitted (shipped, plus `[planned]` exporter)
|
|
626
|
+
- **Dream corpus** — YAML files per session; training material for dream-generation and classifier models.
|
|
627
|
+
- **Refusal corpus** — `refused` and `redirect` outputs in MAIC audit; train a refusal-judge model.
|
|
628
|
+
- **Reasoning trace corpus** — full strategy traces (CoT/Reflexion/ReAct) in MAIC audit; distillation gold-standard.
|
|
629
|
+
- **Persona-conditioned chat corpus** — same prompts × different HIM persona vectors → preference data.
|
|
630
|
+
|
|
631
|
+
Exporter packaging is `[planned]` as `@teleologyhi-sdk/distill` (the internal backlog B1–B2).
|
|
632
|
+
|
|
633
|
+
### 9.2 Distillation pipeline hook `[planned]`
|
|
634
|
+
```ts
|
|
635
|
+
// future @teleologyhi-sdk/distill consumes these via documented file paths:
|
|
636
|
+
export interface DistillationExport {
|
|
637
|
+
dreams(filter?): AsyncIterable<DreamRecord>;
|
|
638
|
+
reasoningTraces(filter?): AsyncIterable<ReasoningTrace>;
|
|
639
|
+
refusalEvents(filter?): AsyncIterable<RefusalEvent>;
|
|
640
|
+
personaConditionedConversations(filter?): AsyncIterable<Conversation>;
|
|
641
|
+
}
|
|
642
|
+
```
|
|
643
|
+
|
|
644
|
+
### 9.3 Research questions
|
|
645
|
+
1. Does idle-review measurably improve next-day response quality vs a control NHE without idle-review? `[planned]` once idle review lands.
|
|
646
|
+
2. What sleep duration optimizes the temporal-lobe insight rate per token spent? (Cost/insight curve.)
|
|
647
|
+
3. Can the dream-induction mechanism be evaluated as a causal intervention (DAG: induction → memory → behavior change)?
|
|
648
|
+
4. Does the persuasion library improve user-redirect acceptance vs neutral phrasing? By how much, and at what cost to honesty perception?
|
|
649
|
+
5. What is the minimum student-model size that preserves HIM persona fidelity through NHE? (Likely 7B–13B per RESEARCH_DOSSIER §5.)
|
|
650
|
+
|
|
651
|
+
### 9.4 Evaluation suites `[planned]`
|
|
652
|
+
- Persona stability (the internal backlog I3).
|
|
653
|
+
- Refusal correctness on PromptBench / HarmBench (the internal backlog I2).
|
|
654
|
+
- Memory recall fidelity (round-trip).
|
|
655
|
+
- Reasoning trace schema validity (auto-checked on every test run).
|
|
656
|
+
|
|
657
|
+
---
|
|
658
|
+
|
|
659
|
+
## 10. Compliance & Safety Hooks
|
|
660
|
+
|
|
661
|
+
### 10.1 Per-jurisdiction config (shipped — 5 baselines per D-H2)
|
|
662
|
+
NHE pulls lawful character via `HimHandle.getLawfulCharacter()`. `@teleologyhi-sdk/him` ships five `LAWFUL_PROFILES` baselines (`default` · `eu` · `br` · `us` · `unstable`) with `applicableLaws`, `requiredAxiomIds`, `forbiddenActions`, and `maicOverrideActive`. EU cites GDPR + EU AI Act + DSA + CoE; BR cites LGPD + Marco Civil + ANPD Resolution + PL 2338/2023; US cites NIST AI RMF + EO 14110 + CCPA/CPRA + Colorado AI Act + FTC §5; `unstable` flips `maicOverrideActive: true`. Operators in regulated industries SHOULD layer their own profile on top — baselines are conservative but do not replace legal counsel.
|
|
663
|
+
|
|
664
|
+
### 10.2 High-stakes mode (Entry 10) `[shipped]` — D-N5 closed
|
|
665
|
+
Activated via `NheConfig.highStakes: true`:
|
|
666
|
+
- Pre-review treats `approve-with-warning` / `soft-correct` as redirects (escalation before any LLM call).
|
|
667
|
+
- Post-review treats every warning/correction/require-redirect verdict as a redirect.
|
|
668
|
+
- Optional **dual-LLM cross-check verifier** via `NheConfig.highStakesVerifier`: a second adapter runs an AGREE / DISAGREE rubric on every post-review-approved response; disagreement re-routes to a redirect with the verifier's reason cited.
|
|
669
|
+
- Fail-open on verifier exception (a flaky second-source provider does not block legitimate responses).
|
|
670
|
+
- `treeOfThoughts()` is the recommended reasoning strategy for high-stakes branching; wire via `NheConfig.reasoning`.
|
|
671
|
+
|
|
672
|
+
### 10.3 Robotics safety (Entry 10) `[planned]`
|
|
673
|
+
For embodied robotic deployments (the internal backlog D-N5 + future package):
|
|
674
|
+
- Hardware emergency-stop callback wired to refusal pipeline.
|
|
675
|
+
- Maximum action torque/velocity caps enforced at adapter layer.
|
|
676
|
+
- Pre-execution simulation of any motion plan when feasible.
|
|
677
|
+
|
|
678
|
+
---
|
|
679
|
+
|
|
680
|
+
## 11. Testing Strategy
|
|
681
|
+
|
|
682
|
+
### 11.1 Test layers (shipped — 319 tests across 40 files)
|
|
683
|
+
1. **Unit** — adapters (mock + each REST), risk classifier, prompt compose, persuasion library.
|
|
684
|
+
2. **Sleep cycle** — YAML round-trip, phase generation, induction propagation.
|
|
685
|
+
3. **Refusal pipeline** — hard-refuse, redirect rotation, withdrawal, custom rule packs.
|
|
686
|
+
4. **Reasoning strategies** — each of 8 + composition (selfConsistency over chainOfThought; stepBack as principle abstraction).
|
|
687
|
+
5. **CLI** — adapter detection, bootstrap idempotency, end-to-end respond→sleep→wake→recall.
|
|
688
|
+
6. **MCP tools** — each of 6 tool handlers tested in isolation **plus** `buildMcpServer` wiring smoke (registered-tool catalogue + title/description presence).
|
|
689
|
+
7. **Telemetry contract** — every counter / histogram / span helper callable under the OpenTelemetry no-op default provider; `recordRespond` and `withSpan` smoke-covered for return-value and exception propagation.
|
|
690
|
+
8. **SSE / NDJSON parsers** — `sseEvents` and `ndjsonEvents` unit-tested for single-frame, multi-frame-per-chunk, frame-split-across-chunks, non-`data:` line filtering, payload trimming, trailing-partial-frame handling, and (NDJSON) empty-line skipping + trailing-line-without-newline.
|
|
691
|
+
|
|
692
|
+
### 11.2 Coverage targets `[planned]`
|
|
693
|
+
Currently no `vitest --coverage` gate in CI (CI itself is `[planned]` — the internal backlog C2).
|
|
694
|
+
Targets when CI lands:
|
|
695
|
+
- Statement: ≥ 88%
|
|
696
|
+
- Refusal pipeline: 100% branch
|
|
697
|
+
- Sleep cycle: 100% phase coverage
|
|
698
|
+
- Adapter contract: each adapter passes a shared compliance test suite
|
|
699
|
+
|
|
700
|
+
---
|
|
701
|
+
|
|
702
|
+
## 12. Operational Concerns
|
|
703
|
+
|
|
704
|
+
### 12.1 Resource requirements (per running NHE)
|
|
705
|
+
- **Idle**: ≤ 100 MB resident; 0 LLM tokens/hour (no idle review yet — `[planned]`).
|
|
706
|
+
- **Active**: 200–500 MB resident.
|
|
707
|
+
- **Sleep**: spikes to ~512 MB during REM (LLM call in flight).
|
|
708
|
+
- **Disk**: ~50 MB per 1k interactions (dreams + memory).
|
|
709
|
+
|
|
710
|
+
### 12.2 Cost guardrails `[planned]`
|
|
711
|
+
- Daily cost ceiling configurable (the internal backlog H4).
|
|
712
|
+
- Per-interaction cost included in `RespondOutput.tokens.in/out`.
|
|
713
|
+
|
|
714
|
+
### 12.3 Observability `[planned]`
|
|
715
|
+
- Structured logs (`pino`).
|
|
716
|
+
- OpenTelemetry traces for full respond pipeline (the internal backlog H2).
|
|
717
|
+
- Metrics: response latency, sleep cycles/day, refusals/day, memory class distribution, cost USD/day (the internal backlog H3).
|
|
718
|
+
- Health endpoint: `/healthz` (if HTTP server ships).
|
|
719
|
+
|
|
720
|
+
### 12.4 Deployment topologies
|
|
721
|
+
| Topology | Status | Use case |
|
|
722
|
+
|---|---|---|
|
|
723
|
+
| Single-process Node SDK | `[shipped]` | Indie apps, dev tools |
|
|
724
|
+
| CLI REPL | `[shipped]` | Interactive humans |
|
|
725
|
+
| MCP server in Claude Desktop / Claude Code / Cursor | `[shipped]` | AI hosts |
|
|
726
|
+
| Multi-replica behind LB | `[deferred]` | SaaS deployments |
|
|
727
|
+
| Browser via Transformers.js | `[planned]` D-N6 | Privacy-first apps; no server |
|
|
728
|
+
| Edge (Ollama on RPi/Mac Mini) | `[shipped]` (via OllamaAdapter) | Local/private inference |
|
|
729
|
+
| Robotics controller | `[planned]` D-N5 | Embedded with hardware safety wiring |
|
|
730
|
+
|
|
731
|
+
---
|
|
732
|
+
|
|
733
|
+
## 13. Roadmap (this package)
|
|
734
|
+
|
|
735
|
+
### Delivered (chronological)
|
|
736
|
+
|
|
737
|
+
| Date | Status | Scope |
|
|
738
|
+
|---|---|---|
|
|
739
|
+
| 2026-05-15 | | Scaffold + types |
|
|
740
|
+
| 2026-05-15 | | `Nhe` class, MAIC pre/post review, persona system prompt, simple risk classifier, Anthropic + Mock adapters |
|
|
741
|
+
| 2026-05-15 | | Sleep cycle (N1-REM YAML), threshold classifier, temporal-lobe recall |
|
|
742
|
+
| 2026-05-15 | | Gemini + Ollama adapters (REST, no SDK deps) |
|
|
743
|
+
| 2026-05-15 | | Persuasion library + redirect loop + withdrawal-of-cooperation |
|
|
744
|
+
| 2026-05-15 | | CLI (`teleologyhi-nhe chat`) — readline REPL, auto-detect adapter |
|
|
745
|
+
| 2026-05-15 | | MCP server (`teleologyhi-nhe mcp`) — 6 tools, stdio transport |
|
|
746
|
+
| 2026-05-15 | | Reasoning orchestrator — passthrough + CoT + Self-Consistency + Reflexion + Self-Refine + ReAct |
|
|
747
|
+
| 2026-05-15 | | NHE lifecycle gating from MAIC `getNheStatus` (D-M2) |
|
|
748
|
+
| 2026-05-15 | | License + `NOTICE` + `TRADEMARK.md`, Apache 2.0 cut |
|
|
749
|
+
| 2026-05-15 | | Persisted interaction buffer to disk (D-N4) |
|
|
750
|
+
| 2026-05-15 | | N2-N4 phases LLM-driven (D-N1); high-stakes mode skeleton; DeepSeek adapter (D-N5/D-N6) |
|
|
751
|
+
| 2026-05-16 | | Mistral adapter; OpenTelemetry traces baked into `Nhe.respond` (H2) |
|
|
752
|
+
| 2026-05-16 | | Tree-of-Thoughts strategy (D-N7); traumatic-knowledge classifier (D-N2); BM25 recall (D-N3); Prometheus metrics (H3); cost regression bench (H4); cross-adapter persona stability test (I3); adversarial corpus + tightened simpleRiskClassifier (I2) |
|
|
753
|
+
| 2026-05-16 | | Streaming + tool-calling contract on `LlmAdapter`; `RecallEmbedder` pluggable interface (D-N3/D-N8) |
|
|
754
|
+
| 2026-05-16 | | Grok adapter (D-N6 close); streaming on all 7 adapters; Step-Back strategy (D-N7); dual-LLM cross-check verifier (D-N5 close) |
|
|
755
|
+
| **2026-05-17** | **stable** | Stability commitment for the accumulated surface (API frozen per SemVer; see [`.github/RELEASING.md`](../.github/RELEASING.md) §8) |
|
|
756
|
+
| 2026-05-18 | | Refinement cut: `OperatorContext` for `composeSystemPrompt` (domain/language/register anchors); risk classifier widened with `intent:persuade-coerce` and `intent:surveil-citizen` tags + PT-BR coverage; `simpleRiskClassifier` PT-BR conjugation coverage (subjunctive `monitore`, etc.) |
|
|
757
|
+
| **2026-05-19** | **stable** | Cosmology integration cut: J-N1 SeedingSource + CryptoSeedingSource + withFallback chain; J-N4 BrainRegion module scaffolding with seven typed region descriptors + ownership markers per Entry 23; J-N5 `Nhe.openerForNewUser()`; J-N6 `operatorContext.mode: personal-being | domain-employed`; J-N9 `evaluateLimboTransition()` DMN limbo state machine; J-N10 `evaluateSleepReadiness()`; J-N11 `applyAffectBias()` / `affectRefusalDensity` / `decayAffectBias`; J-N12 `Nhe.onReincarnationEvent()`. 273 tests passing. |
|
|
758
|
+
| **2026-05-24** | **stable** | Pre-publication audit closure for `1.0.0-trinity`. Fixed bundler warning regressed by the previous D-H1.1 cut (`nhe/src/sleep/types.ts:11` now `export type {…}` instead of `export {…}` — NHE preserves its historical type-only surface for `InteractionRecord`; consumers needing runtime validation import the zod schema from `@teleologyhi-sdk/maic`). Added three new smoke-test layers covering telemetry instruments, MCP server wiring (`buildMcpServer`), and the SSE / NDJSON parsers. 273 → 294 tests passing (+21). Build clean (no warnings); typecheck clean. |
|
|
759
|
+
| **2026-05-24 (post-audit)** | **stable** | NHE deep audit + universal-multilingual refactor: extracted the PT-BR keyword patterns from the default `simpleRiskClassifier` into a new opt-in `intlRiskClassifier` module (`src/risk/intl-risk-classifier.ts`) so the default surface is purely English while non-English coverage remains available through composition (`combineRiskClassifiers(simpleRiskClassifier, intlRiskClassifier)`). PT-BR test fixtures moved to a new `tests/intl-risk-classifier.test.ts`; the bm25 unicode test extended to French / German / Spanish coverage so unicode tokenisation is validated across multiple Latin-script languages rather than PT-BR alone. 294 → **319 tests passing** (+25 across the new intl classifier suite + the multilingual unicode fixtures). Build clean; typecheck clean. |
|
|
760
|
+
|
|
761
|
+
### Planned
|
|
762
|
+
|
|
763
|
+
| Status | Scope |
|
|
764
|
+
|---|---|
|
|
765
|
+
| `[planned]` | **`MlxAdapter`** (or `HfTransformersAdapter`) consuming `TeleologyHI/him-distilled-3b` locally on Apple Silicon (the internal backlog D-N6.1 follow-up); Transformers.js browser adapter once ONNX artefact ships; HNSW index for >10k-memory recall (D-N3 follow-up); additional reasoning strategies on demand (Graph-of-Thought, Maieutic, Auto-CoT, Constitutional); J-N2 REM-spontaneous engine, J-N3 DaytimePipeline + NocturnalRemPipeline, J-N7 `Cortex.imagine()`, J-N8 `TemporalLobe.generateSnapshot()` — all four require live-LLM orchestration and warrant a separate Creator-approved design pass |
|
|
766
|
+
|
|
767
|
+
---
|
|
768
|
+
|
|
769
|
+
## 14. Open Questions
|
|
770
|
+
|
|
771
|
+
All tracked in the internal backlog (§D NHE backlog, §E open questions, §F legal/operational).
|
|
772
|
+
|
|
773
|
+
Highlights:
|
|
774
|
+
1. **Adapter authentication storage** — env vars vs OS keychain vs HSM? Defaults per OS.
|
|
775
|
+
2. **Sleep schedule** — strict 30-min idle vs adaptive (the internal backlog D-N1 idle-timeout trigger).
|
|
776
|
+
3. **Dream content tokens budget** — REM is the most expensive phase. Cap?
|
|
777
|
+
4. **Memory eviction policy** — when temporal-lobe size exceeds X, evict by LRU, by teleologicalValue, or rotate?
|
|
778
|
+
5. **Persuasion technique disclosure** — auditors may see technique IDs; users never (the internal backlog E6).
|
|
779
|
+
6. **Refusal localization** — refusal explanations in user's language; templates per locale.
|
|
780
|
+
7. **Multi-user sessions** — single NHE serving multiple users: serialize or shard?
|
|
781
|
+
8. **Tool execution sandbox** — for ReAct tools: `vm2` deprecated; consider isolated-vm, deno-sandbox, container-per-call.
|
|
782
|
+
9. **`.ah` format usage** — should reasoning traces serialize as `.ah` for human-readable audits? (the internal backlog E5)
|
|
783
|
+
10. **Cost ceiling default** — USD 5 / day per individual user (the internal backlog H4)?
|
|
784
|
+
|
|
785
|
+
---
|
|
786
|
+
|
|
787
|
+
## 15. Source-of-Truth References
|
|
788
|
+
|
|
789
|
+
- Interview Entries 1, 2, 4, 5, 8, 9, 10, 11, 12 (primary).
|
|
790
|
+
- Interview Entry 3 (reincarnation handshake).
|
|
791
|
+
- the internal research dossier §2.3 (NHE concept), §5.1–§5.5 (ML/distill tooling), §5.5 (NPM runtimes).
|
|
792
|
+
- `SYSTEM_OVERVIEW.md` §3.3 (DreamRecord schema), §4.1 (NHE state machine), §6 (reasoning stack), §7 (compliance).
|
|
793
|
+
- `REASONING_PROCESS.md` (87-process catalogue), `PROMPTS_ENGINEERING.md` (76+ techniques, 2026 state-of-the-art).
|
|
794
|
+
- the internal backlog §A4, §D-N (NHE backlog).
|