counterparts 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.
- package/CONSTITUTION.md +68 -0
- package/LICENSE +21 -0
- package/README.md +348 -0
- package/docs/ELI5.md +84 -0
- package/docs/QUICKSTART.md +1258 -0
- package/docs/module-map.md +81 -0
- package/package.json +63 -0
- package/src/adapters/claude-code/CONTRACT.md +606 -0
- package/src/adapters/claude-code/INTERFACE-GAPS.md +655 -0
- package/src/adapters/claude-code/NOTES.md +1055 -0
- package/src/adapters/claude-code/bin/hook.ts +820 -0
- package/src/adapters/claude-code/bin/runner.ts +522 -0
- package/src/adapters/claude-code/config.ts +778 -0
- package/src/adapters/claude-code/credentials.ts +199 -0
- package/src/adapters/claude-code/doctor.ts +2557 -0
- package/src/adapters/claude-code/embed-client.ts +617 -0
- package/src/adapters/claude-code/hooks.ts +2240 -0
- package/src/adapters/claude-code/index.ts +224 -0
- package/src/adapters/claude-code/interpret-client.ts +395 -0
- package/src/adapters/claude-code/page-writer.ts +462 -0
- package/src/adapters/claude-code/primacy.ts +162 -0
- package/src/adapters/claude-code/spawn.ts +241 -0
- package/src/adapters/claude-code/standdown.ts +536 -0
- package/src/adapters/claude-code/transcript.ts +661 -0
- package/src/adapters/claude-code/vectors.ts +415 -0
- package/src/adapters/cli/CONTRACT.md +255 -0
- package/src/adapters/cli/INTERFACE-GAPS.md +350 -0
- package/src/adapters/cli/NOTES.md +936 -0
- package/src/adapters/cli/bin/counterparts.ts +74 -0
- package/src/adapters/cli/commands.ts +6095 -0
- package/src/adapters/cli/export.ts +766 -0
- package/src/adapters/cli/index.ts +103 -0
- package/src/adapters/cli/install.ts +670 -0
- package/src/adapters/cli/removal.ts +1186 -0
- package/src/adapters/cli/repair-dates.ts +659 -0
- package/src/adapters/cli/self-page.ts +229 -0
- package/src/adapters/cli/snapshot.ts +279 -0
- package/src/adapters/cli/start-fresh.ts +1228 -0
- package/src/adapters/config-path.ts +342 -0
- package/src/adapters/dashboard/CONTRACT.md +67 -0
- package/src/adapters/dashboard/INTERFACE-GAPS.md +139 -0
- package/src/adapters/dashboard/NOTES.md +474 -0
- package/src/adapters/dashboard/activity.ts +121 -0
- package/src/adapters/dashboard/ansi.ts +82 -0
- package/src/adapters/dashboard/bin/dashboard.ts +479 -0
- package/src/adapters/dashboard/browse.ts +298 -0
- package/src/adapters/dashboard/identity.ts +182 -0
- package/src/adapters/dashboard/index.ts +183 -0
- package/src/adapters/dashboard/layout.ts +149 -0
- package/src/adapters/dashboard/registries.ts +291 -0
- package/src/adapters/dashboard/resolve.ts +206 -0
- package/src/adapters/dashboard/source.ts +48 -0
- package/src/adapters/dashboard/status.ts +327 -0
- package/src/adapters/dashboard/stories.ts +194 -0
- package/src/adapters/dashboard/web/app.html +1731 -0
- package/src/adapters/dashboard/web/brain.html +463 -0
- package/src/adapters/dashboard/web/fired.ts +31 -0
- package/src/adapters/dashboard/web/flow.ts +457 -0
- package/src/adapters/dashboard/web/narrate.ts +1042 -0
- package/src/adapters/dashboard/web/reveal.ts +135 -0
- package/src/adapters/dashboard/web/server.ts +287 -0
- package/src/adapters/dashboard/web/views.ts +1744 -0
- package/src/adapters/expansions.ts +569 -0
- package/src/adapters/fired.ts +1580 -0
- package/src/adapters/mcp/CONTRACT.md +240 -0
- package/src/adapters/mcp/INTERFACE-GAPS.md +271 -0
- package/src/adapters/mcp/NOTES.md +205 -0
- package/src/adapters/mcp/bin/serve.ts +349 -0
- package/src/adapters/mcp/deliberate.ts +619 -0
- package/src/adapters/mcp/index.ts +101 -0
- package/src/adapters/mcp/protocol.ts +170 -0
- package/src/adapters/mcp/server.ts +1740 -0
- package/src/adapters/mcp/stdio.ts +56 -0
- package/src/adapters/mcp/tools.ts +779 -0
- package/src/adapters/scopes.ts +657 -0
- package/src/adapters/sessions.ts +402 -0
- package/src/adapters/snapshots.ts +1069 -0
- package/src/adapters/stance-env.ts +137 -0
- package/src/core/associate/CONTRACT.md +118 -0
- package/src/core/associate/INTERFACE-GAPS.md +173 -0
- package/src/core/associate/NOTES.md +194 -0
- package/src/core/associate/buffer.ts +66 -0
- package/src/core/associate/edges.ts +188 -0
- package/src/core/associate/index.ts +570 -0
- package/src/core/associate/pending.ts +383 -0
- package/src/core/associate/spread.ts +149 -0
- package/src/core/associate/tunables.ts +123 -0
- package/src/core/bridge.ts +272 -0
- package/src/core/briefing.ts +67 -0
- package/src/core/counterpart.ts +3855 -0
- package/src/core/encode/CONTRACT.md +151 -0
- package/src/core/encode/INTERFACE-GAPS.md +135 -0
- package/src/core/encode/NOTES.md +146 -0
- package/src/core/encode/aliases.ts +71 -0
- package/src/core/encode/battery.ts +284 -0
- package/src/core/encode/chunk.ts +265 -0
- package/src/core/encode/emotion.ts +119 -0
- package/src/core/encode/floor.ts +76 -0
- package/src/core/encode/index.ts +100 -0
- package/src/core/encode/precision.ts +162 -0
- package/src/core/encode/preselect.ts +269 -0
- package/src/core/encode/salience.ts +108 -0
- package/src/core/encode/secrets.ts +265 -0
- package/src/core/encode/tunables.ts +99 -0
- package/src/core/encode/types.ts +286 -0
- package/src/core/encode/words.ts +75 -0
- package/src/core/handoff/CONTRACT.md +199 -0
- package/src/core/handoff/INTERFACE-GAPS.md +166 -0
- package/src/core/handoff/NOTES.md +206 -0
- package/src/core/handoff/index.ts +957 -0
- package/src/core/mint.ts +279 -0
- package/src/core/observer.ts +28 -0
- package/src/core/physics/CONTRACT.md +341 -0
- package/src/core/physics/NOTES.md +155 -0
- package/src/core/physics/clock.ts +51 -0
- package/src/core/physics/index.ts +1054 -0
- package/src/core/prospective/CONTRACT.md +137 -0
- package/src/core/prospective/INTERFACE-GAPS.md +112 -0
- package/src/core/prospective/NOTES.md +160 -0
- package/src/core/prospective/derive.ts +188 -0
- package/src/core/prospective/index.ts +1030 -0
- package/src/core/prospective/tunables.ts +72 -0
- package/src/core/prospective/windows.ts +187 -0
- package/src/core/recall/CONTRACT.md +326 -0
- package/src/core/recall/INTERFACE-GAPS.md +163 -0
- package/src/core/recall/NOTES.md +472 -0
- package/src/core/recall/activate.ts +521 -0
- package/src/core/recall/cues.ts +275 -0
- package/src/core/recall/gate.ts +441 -0
- package/src/core/recall/index.ts +692 -0
- package/src/core/recall/probe.ts +239 -0
- package/src/core/recall/reference.ts +201 -0
- package/src/core/recall/render.ts +285 -0
- package/src/core/recall/session.ts +403 -0
- package/src/core/recall/tunables.ts +329 -0
- package/src/core/remember/CONTRACT.md +293 -0
- package/src/core/remember/INTERFACE-GAPS.md +189 -0
- package/src/core/remember/NOTES.md +373 -0
- package/src/core/remember/fallback.ts +452 -0
- package/src/core/remember/index.ts +103 -0
- package/src/core/remember/owner-strike-seam.ts +479 -0
- package/src/core/remember/proposals.ts +609 -0
- package/src/core/remember/spans.ts +1310 -0
- package/src/core/remember/tunables.ts +111 -0
- package/src/core/remember/updates.ts +198 -0
- package/src/core/retrieval.ts +96 -0
- package/src/core/revision.ts +441 -0
- package/src/core/schemas/CONTRACT.md +190 -0
- package/src/core/schemas/INTERFACE-GAPS.md +172 -0
- package/src/core/schemas/NOTES.md +260 -0
- package/src/core/schemas/aliases.ts +160 -0
- package/src/core/schemas/index.ts +1459 -0
- package/src/core/schemas/tunables.ts +44 -0
- package/src/core/schemas/types.ts +387 -0
- package/src/core/self/CONTRACT.md +459 -0
- package/src/core/self/INTERFACE-GAPS.md +284 -0
- package/src/core/self/NOTES.md +1057 -0
- package/src/core/self/briefing.ts +980 -0
- package/src/core/self/episodes.ts +483 -0
- package/src/core/self/freeze.ts +103 -0
- package/src/core/self/identity.ts +604 -0
- package/src/core/self/index.ts +2130 -0
- package/src/core/self/journal-file.ts +760 -0
- package/src/core/self/page.ts +404 -0
- package/src/core/self/tunables.ts +244 -0
- package/src/core/self/writer.ts +714 -0
- package/src/core/sleep/CONTRACT.md +298 -0
- package/src/core/sleep/INTERFACE-GAPS.md +262 -0
- package/src/core/sleep/NOTES.md +770 -0
- package/src/core/sleep/briefing.ts +86 -0
- package/src/core/sleep/consolidate.ts +241 -0
- package/src/core/sleep/cycle.ts +598 -0
- package/src/core/sleep/decay.ts +237 -0
- package/src/core/sleep/dedup.ts +373 -0
- package/src/core/sleep/index.ts +112 -0
- package/src/core/sleep/log.ts +116 -0
- package/src/core/sleep/markers.ts +164 -0
- package/src/core/sleep/prune.ts +170 -0
- package/src/core/sleep/strength-cache.ts +176 -0
- package/src/core/sleep/tunables.ts +135 -0
- package/src/core/sleep/types.ts +542 -0
- package/src/core/store/CONTRACT.md +446 -0
- package/src/core/store/NOTES.md +1068 -0
- package/src/core/store/cache.ts +709 -0
- package/src/core/store/db.ts +275 -0
- package/src/core/store/errors.ts +134 -0
- package/src/core/store/index.ts +2450 -0
- package/src/core/store/operational.ts +610 -0
- package/src/core/store/owner-op-seam.ts +428 -0
- package/src/core/store/paths.ts +494 -0
- package/src/core/store/prose.ts +165 -0
- package/src/core/store/render.ts +87 -0
- package/src/core/store/walk-seam.ts +78 -0
- package/src/core/types.ts +68 -0
- package/tools/install-loop/run.sh +1063 -0
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EPISODES — the day narrating itself, in the first person.
|
|
3
|
+
*
|
|
4
|
+
* Brain analog: event segmentation plus replay-based consolidation. The
|
|
5
|
+
* deliberate break (constitution 12): humans never author their episodes —
|
|
6
|
+
* encoding is involuntary — while here it is a ritual, which is the whole
|
|
7
|
+
* authorship thesis.
|
|
8
|
+
*
|
|
9
|
+
* The five properties that are structural, and the incidents behind them
|
|
10
|
+
* (behavioral-spec §13):
|
|
11
|
+
*
|
|
12
|
+
* - **Substance-paced, never wall-clock-paced.** The first ask needs real
|
|
13
|
+
* turns AND real bytes, *or* enough bytes alone so a one-prompt agentic
|
|
14
|
+
* session still journals.
|
|
15
|
+
* - **Live append.** The ritual hands the model its chapter for the REST of the
|
|
16
|
+
* session. v1's once-per-session ask fired at the first stop, so everything
|
|
17
|
+
* after turn one was first-person-invisible; one session's episode missed the
|
|
18
|
+
* moment that mattered by 82 seconds.
|
|
19
|
+
* - **One ask, not two**, and **the advance is committed before the ask
|
|
20
|
+
* blocks**, so a crash cannot re-ask in a loop.
|
|
21
|
+
* - **Episodes are context and source, in that order** — ingested ONCE as
|
|
22
|
+
* ordinary self-kind memories with named handles. *"Episode" is not a memory
|
|
23
|
+
* kind*: it is a prose family here (`epi_`), and what it becomes is a
|
|
24
|
+
* `self`-kind memory like any other.
|
|
25
|
+
* - **Add-first regrowth within a window that CLOSES.** Because the journal
|
|
26
|
+
* stays open, a file can grow after its memory exists; freezing at first
|
|
27
|
+
* ingest would drop exactly the in-the-moment material the open journal
|
|
28
|
+
* exists to capture. Add first, then archive the stale one — a failed add can
|
|
29
|
+
* never leave an episode with no live memory. The window closing is a
|
|
30
|
+
* deliberate deviation from human reconsolidation, which reopens forever.
|
|
31
|
+
*
|
|
32
|
+
* Two refusals that look like edge cases and are not: a span with **no session
|
|
33
|
+
* identity is skipped outright** (every anonymous session collapses to the same
|
|
34
|
+
* marker, and pooling one session's account into another's — under an instruction
|
|
35
|
+
* saying it outranks the transcript — is the identity-safety failure §13 G7
|
|
36
|
+
* names), and **ingestion is ordinary, so the gate applies**: a first-person
|
|
37
|
+
* reflection is not exempt from never durably encoding a credential (§13 G8, scar
|
|
38
|
+
* §2.7 — episode ingestion was v1's most heavily gated surface, 66% of all gate
|
|
39
|
+
* fires).
|
|
40
|
+
*/
|
|
41
|
+
import type { Salience } from "../types.js";
|
|
42
|
+
import type { ProseDoc, Store } from "../store/index.js";
|
|
43
|
+
import { hashText } from "../store/index.js";
|
|
44
|
+
import type { SelfTunables } from "./tunables.js";
|
|
45
|
+
|
|
46
|
+
// ── the input shape (a SEAM, not an import — INTERFACE-GAPS #1) ─────────────
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* What arrives from `remember/`'s authorship path, restated here rather than
|
|
50
|
+
* imported. `self/` must not depend on `remember/` (the dependency runs the other
|
|
51
|
+
* way: authorship is upstream of the self), so this is the contract's own episode
|
|
52
|
+
* input shape and the wiring is recorded in INTERFACE-GAPS.md #1.
|
|
53
|
+
*/
|
|
54
|
+
export interface EpisodeProposal {
|
|
55
|
+
/** Identity-safe join: blank means SKIPPED, never pooled (§13 G7). */
|
|
56
|
+
readonly sessionId: string;
|
|
57
|
+
/** The first-person chapter text, the model's own voice, any length. */
|
|
58
|
+
readonly content: string;
|
|
59
|
+
readonly title?: string;
|
|
60
|
+
/** Named handles — aliases, not a second lookup path (contract §3, §9.2). */
|
|
61
|
+
readonly handles?: readonly string[];
|
|
62
|
+
/** When it happened, at stated precision. */
|
|
63
|
+
readonly happenedOn?: string;
|
|
64
|
+
/** The author's claimed aggregate salience: a FLOOR, clamped in `physics/`. */
|
|
65
|
+
readonly claimed?: number | null;
|
|
66
|
+
readonly salience?: Partial<Salience>;
|
|
67
|
+
readonly day?: number;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export type EpisodeIntakeReason =
|
|
71
|
+
| "ok"
|
|
72
|
+
| "NOT_AN_OBJECT"
|
|
73
|
+
| "SESSION_MISSING"
|
|
74
|
+
| "CONTENT_MISSING"
|
|
75
|
+
| "CONTENT_EMPTY"
|
|
76
|
+
| "HANDLES_NOT_STRINGS";
|
|
77
|
+
|
|
78
|
+
export type EpisodeIntake =
|
|
79
|
+
| { ok: true; proposal: EpisodeProposal }
|
|
80
|
+
| { ok: false; reason: EpisodeIntakeReason };
|
|
81
|
+
|
|
82
|
+
/** Validate a raw deposit. Reasons, never a bare false (test discipline). */
|
|
83
|
+
export function intakeEpisode(raw: unknown): EpisodeIntake {
|
|
84
|
+
if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
|
|
85
|
+
return { ok: false, reason: "NOT_AN_OBJECT" };
|
|
86
|
+
}
|
|
87
|
+
const rec = raw as Record<string, unknown>;
|
|
88
|
+
const session = rec["sessionId"];
|
|
89
|
+
if (typeof session !== "string" || session.trim().length === 0) {
|
|
90
|
+
return { ok: false, reason: "SESSION_MISSING" };
|
|
91
|
+
}
|
|
92
|
+
if (rec["content"] === undefined || typeof rec["content"] !== "string") {
|
|
93
|
+
return { ok: false, reason: "CONTENT_MISSING" };
|
|
94
|
+
}
|
|
95
|
+
if ((rec["content"] as string).trim().length === 0) {
|
|
96
|
+
return { ok: false, reason: "CONTENT_EMPTY" };
|
|
97
|
+
}
|
|
98
|
+
const handles = rec["handles"];
|
|
99
|
+
if (
|
|
100
|
+
handles !== undefined &&
|
|
101
|
+
(!Array.isArray(handles) || handles.some((h) => typeof h !== "string"))
|
|
102
|
+
) {
|
|
103
|
+
return { ok: false, reason: "HANDLES_NOT_STRINGS" };
|
|
104
|
+
}
|
|
105
|
+
return { ok: true, proposal: rec as unknown as EpisodeProposal };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// ── the gate seam (scar §2.7) ───────────────────────────────────────────────
|
|
109
|
+
|
|
110
|
+
export interface EpisodeGateInput {
|
|
111
|
+
readonly text: string;
|
|
112
|
+
readonly handles: readonly string[];
|
|
113
|
+
readonly sessionId: string;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export type EpisodeGateVerdict =
|
|
117
|
+
/** `text`, when present, is the gate's REDACTED body — and it, never the draft,
|
|
118
|
+
* is what becomes the memory (SEAMS item H; encode §5: "the gate runs before
|
|
119
|
+
* anything durable"). A gate that only accepts or refuses omits it, so this is
|
|
120
|
+
* additive: an older gate keeps working and simply redacts nothing. */
|
|
121
|
+
| { ok: true; text?: string }
|
|
122
|
+
| { ok: false; gate: string; reason: string };
|
|
123
|
+
|
|
124
|
+
export type EpisodeGate = (input: EpisodeGateInput) => EpisodeGateVerdict;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The default REFUSES, exactly as `remember/`'s `NO_GATE` does. A module that
|
|
128
|
+
* cannot run the secrets battery must not be able to quietly encode a
|
|
129
|
+
* credential because nobody wired one in; "no gate injected" is a loud reason,
|
|
130
|
+
* and an absent gate is not an open one.
|
|
131
|
+
*/
|
|
132
|
+
export const NO_GATE: EpisodeGate = () => ({
|
|
133
|
+
ok: false,
|
|
134
|
+
gate: "none",
|
|
135
|
+
reason: "NO_GATE_INJECTED",
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// ── per-session chapter state ───────────────────────────────────────────────
|
|
139
|
+
|
|
140
|
+
export interface Substance {
|
|
141
|
+
/** Real turns — injected context and tool noise excluded by the caller. */
|
|
142
|
+
readonly turns: number;
|
|
143
|
+
/** Real bytes, same exclusion. */
|
|
144
|
+
readonly bytes: number;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export interface EpisodeState {
|
|
148
|
+
sessionId: string;
|
|
149
|
+
episodeId: string | null;
|
|
150
|
+
/**
|
|
151
|
+
* Chapters the model ACTUALLY WROTE — headings in the episode, not asks.
|
|
152
|
+
*
|
|
153
|
+
* Measured 2026-09-04: this counted asks, so the hook's chapter number was
|
|
154
|
+
* the number of times it had asked and the model's was the number of times it
|
|
155
|
+
* had written. With no tool to write through, the two diverged inside one
|
|
156
|
+
* session (7 asks, 0 chapters) and the ask named a chapter nobody had
|
|
157
|
+
* authored. The ask now names `chapters + 1`, which is the next chapter that
|
|
158
|
+
* would exist, so the two sides agree by construction.
|
|
159
|
+
*/
|
|
160
|
+
chapters: number;
|
|
161
|
+
/** Asks COMMITTED (committed before the ask blocks). The pacer's own count. */
|
|
162
|
+
asks: number;
|
|
163
|
+
/**
|
|
164
|
+
* Asks committed on `asksDay` — the count `MAX_ASKS_PER_SESSION` is spent
|
|
165
|
+
* against, kept apart from `asks` on purpose. `asks` stays the session's whole
|
|
166
|
+
* life because two other rules read it: the first-ask branch below (`asks ===
|
|
167
|
+
* 0`) and `appendChapter`'s "is a chapter open?" test. Zeroing that one at
|
|
168
|
+
* midnight would re-open chapter 1 and re-run first-ask pacing against the
|
|
169
|
+
* whole session's substance, on a session already forty turns deep.
|
|
170
|
+
*/
|
|
171
|
+
asksToday: number;
|
|
172
|
+
/** The calendar day `asksToday` is charged to. Null until the first ask. */
|
|
173
|
+
asksDay: string | null;
|
|
174
|
+
/** The ask index at the last append: how "is a new chapter open?" is decided. */
|
|
175
|
+
appendedAtAsk: number;
|
|
176
|
+
/** Substance at the last COMMITTED ask (committed before the ask blocks). */
|
|
177
|
+
askedAtTurns: number;
|
|
178
|
+
askedAtBytes: number;
|
|
179
|
+
lastDay: number;
|
|
180
|
+
/** Idempotency identity of what has already been ingested. */
|
|
181
|
+
ingestedKey: string | null;
|
|
182
|
+
ingestedMemoryId: string | null;
|
|
183
|
+
/** Lived day of the first ingest — the regrow window's origin (§13 G12). */
|
|
184
|
+
firstIngestDay: number | null;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export function stateKey(sessionId: string): string {
|
|
188
|
+
return `self.episode.${sessionId}`;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export function freshEpisodeState(sessionId: string, day: number): EpisodeState {
|
|
192
|
+
return {
|
|
193
|
+
sessionId,
|
|
194
|
+
episodeId: null,
|
|
195
|
+
chapters: 0,
|
|
196
|
+
asks: 0,
|
|
197
|
+
asksToday: 0,
|
|
198
|
+
asksDay: null,
|
|
199
|
+
appendedAtAsk: 0,
|
|
200
|
+
askedAtTurns: 0,
|
|
201
|
+
askedAtBytes: 0,
|
|
202
|
+
lastDay: day,
|
|
203
|
+
ingestedKey: null,
|
|
204
|
+
ingestedMemoryId: null,
|
|
205
|
+
firstIngestDay: null,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function loadEpisodeState(
|
|
210
|
+
store: Store,
|
|
211
|
+
sessionId: string,
|
|
212
|
+
day: number,
|
|
213
|
+
): { state: EpisodeState; status: "loaded" | "absent" | "unreadable" } {
|
|
214
|
+
const raw = store.getMeta(stateKey(sessionId));
|
|
215
|
+
if (raw === undefined) return { state: freshEpisodeState(sessionId, day), status: "absent" };
|
|
216
|
+
try {
|
|
217
|
+
const parsed = JSON.parse(raw) as Partial<EpisodeState>;
|
|
218
|
+
if (parsed === null || typeof parsed !== "object" || typeof parsed.chapters !== "number") {
|
|
219
|
+
return { state: freshEpisodeState(sessionId, day), status: "unreadable" };
|
|
220
|
+
}
|
|
221
|
+
const merged: EpisodeState = { ...freshEpisodeState(sessionId, day), ...parsed, sessionId };
|
|
222
|
+
// MIGRATION, one line each, because a live run's rows were written under the
|
|
223
|
+
// old meaning: `chapters` used to be the ASK count, so it carries over as
|
|
224
|
+
// `asks`, and a state with no episode has, by definition, no chapters
|
|
225
|
+
// written — which is exactly the divergence this split exists to end.
|
|
226
|
+
if (typeof parsed.asks !== "number") merged.asks = parsed.chapters;
|
|
227
|
+
if (merged.episodeId === null) merged.chapters = 0;
|
|
228
|
+
// A state written before the day stamp (2026-09-18) carries no `asksDay`, so
|
|
229
|
+
// the fresh defaults stand and `asksSpentOn` reads it as nothing spent today.
|
|
230
|
+
// See that function for why zero is the safe direction.
|
|
231
|
+
return { state: merged, status: "loaded" };
|
|
232
|
+
} catch {
|
|
233
|
+
return { state: freshEpisodeState(sessionId, day), status: "unreadable" };
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// ── pacing (§13 G1) ─────────────────────────────────────────────────────────
|
|
238
|
+
|
|
239
|
+
export type AskReason =
|
|
240
|
+
| "due-first"
|
|
241
|
+
| "due-substance"
|
|
242
|
+
| "not-enough-substance"
|
|
243
|
+
| "session-ask-cap"
|
|
244
|
+
| "anonymous-session"
|
|
245
|
+
| "observer";
|
|
246
|
+
|
|
247
|
+
export interface AskVerdict {
|
|
248
|
+
readonly due: boolean;
|
|
249
|
+
readonly reason: AskReason;
|
|
250
|
+
readonly chapter: number;
|
|
251
|
+
/** Substance accumulated since the last committed ask — the orphanable tail
|
|
252
|
+
* if this session ends here (§13 known gap: bound it, measure it, don't
|
|
253
|
+
* pretend). */
|
|
254
|
+
readonly sinceTurns: number;
|
|
255
|
+
readonly sinceBytes: number;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Asks this session has spent on `today` — what the cap is measured against.
|
|
260
|
+
*
|
|
261
|
+
* WHICH DAY: the CALENDAR date, and the store's own (`Store#today`, UTC), not the
|
|
262
|
+
* lived day. The lived clock advances only inside the sleep cycle the detached
|
|
263
|
+
* worker runs, and I32 is the scar: a worker that could not start froze that
|
|
264
|
+
* clock for seven days while the calendar kept going, and a cap whose reset
|
|
265
|
+
* depends on the machinery it is capping is a cap that can be spent forever. It
|
|
266
|
+
* is the same key the old shared day cap settled on for the same reason. The
|
|
267
|
+
* zone consequence is real and named rather than hidden (INTERFACE-GAPS): an
|
|
268
|
+
* owner at UTC−6 gets the allowance back at 18:00 local.
|
|
269
|
+
*
|
|
270
|
+
* A state written before the day stamp existed reads as ZERO spent rather than
|
|
271
|
+
* as today's count. The wrong way round costs at most one extra allowance on the
|
|
272
|
+
* day the stamp lands, and the pacer still spaces those out; the other way, a
|
|
273
|
+
* session already at its cap stays capped on every later day too, with no write
|
|
274
|
+
* that could ever move it off — which is the starvation this exists to end.
|
|
275
|
+
*/
|
|
276
|
+
export function asksSpentOn(state: EpisodeState, today: string): number {
|
|
277
|
+
return state.asksDay === today ? state.asksToday : 0;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export function askDue(
|
|
281
|
+
state: EpisodeState,
|
|
282
|
+
substance: Substance,
|
|
283
|
+
t: SelfTunables,
|
|
284
|
+
opts: { observer: boolean; today: string },
|
|
285
|
+
): AskVerdict {
|
|
286
|
+
const sinceTurns = Math.max(0, substance.turns - state.askedAtTurns);
|
|
287
|
+
const sinceBytes = Math.max(0, substance.bytes - state.askedAtBytes);
|
|
288
|
+
// The ask names the NEXT chapter that would exist — one past what was
|
|
289
|
+
// written, never one past what was asked.
|
|
290
|
+
const chapter = state.chapters + 1;
|
|
291
|
+
const no = (reason: AskReason): AskVerdict => ({
|
|
292
|
+
due: false,
|
|
293
|
+
reason,
|
|
294
|
+
chapter,
|
|
295
|
+
sinceTurns,
|
|
296
|
+
sinceBytes,
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
// Observers are never asked (§13 G14, scar E7). Accepted cost, on the record:
|
|
300
|
+
// instrument runs leave no episode.
|
|
301
|
+
if (opts.observer) return no("observer");
|
|
302
|
+
if (state.sessionId.trim().length === 0) return no("anonymous-session");
|
|
303
|
+
// The cap is THIS SESSION'S OWN, PER CALENDAR DAY, and it is a backstop, not
|
|
304
|
+
// the cadence: the re-ask pair below is what spaces the asks out. Shared
|
|
305
|
+
// across a whole calendar day, this cap refused 196 of 264 Stops on the live
|
|
306
|
+
// store and left the crash fallback writing four and a half times what the
|
|
307
|
+
// author wrote (2026-09-17; owner's ruling the same day). Spent over a
|
|
308
|
+
// session's whole life, it starved the long ones instead: a coordinating
|
|
309
|
+
// session on 2026-09-17 used all six inside one working day and its
|
|
310
|
+
// end-of-day handoff was never offered the pen (owner's ruling 2026-09-18).
|
|
311
|
+
// Same-day exhaustion still binds; for now that is accepted. A session that
|
|
312
|
+
// did no real work is still refused — by substance, one gate down.
|
|
313
|
+
if (asksSpentOn(state, opts.today) >= t.MAX_ASKS_PER_SESSION) return no("session-ask-cap");
|
|
314
|
+
|
|
315
|
+
if (state.asks === 0) {
|
|
316
|
+
const paced =
|
|
317
|
+
(substance.turns >= t.FIRST_ASK_TURNS && substance.bytes >= t.FIRST_ASK_BYTES) ||
|
|
318
|
+
substance.bytes >= t.SOLO_ASK_BYTES;
|
|
319
|
+
return paced
|
|
320
|
+
? { due: true, reason: "due-first", chapter, sinceTurns, sinceBytes }
|
|
321
|
+
: no("not-enough-substance");
|
|
322
|
+
}
|
|
323
|
+
// AND, not OR — measured 2026-09-04. v1 re-asked on `reaskBytes` AND
|
|
324
|
+
// `reaskTurns`; v2 shipped an OR whose byte half was a third of v1's, and the
|
|
325
|
+
// model's own chapter-writing reply could satisfy it on its own. One evening
|
|
326
|
+
// of 13 owner turns drew about a dozen asks across the two pacers.
|
|
327
|
+
const paced = sinceTurns >= t.REASK_TURNS && sinceBytes >= t.REASK_BYTES;
|
|
328
|
+
return paced
|
|
329
|
+
? { due: true, reason: "due-substance", chapter, sinceTurns, sinceBytes }
|
|
330
|
+
: no("not-enough-substance");
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* The ask's wording is a preference and a probe [A]; that an ask EXISTS at every
|
|
335
|
+
* session-ending path, and that its orphanable tail is bounded and logged, is
|
|
336
|
+
* mechanized. Kept close to v1's, because its content is behavior, not
|
|
337
|
+
* decoration: it names the stakes, asks for first person and the model's own
|
|
338
|
+
* voice at any length, sanctions honesty about a routine stretch, and closes on
|
|
339
|
+
* who the writing is for.
|
|
340
|
+
*/
|
|
341
|
+
export function askText(chapter: number): string {
|
|
342
|
+
return [
|
|
343
|
+
chapter === 1
|
|
344
|
+
? "Write this session's episode, in the first person — your voice, any length."
|
|
345
|
+
: `Add chapter ${chapter} to this session's episode — in the moment, in your own voice.`,
|
|
346
|
+
"You are the only one who can write it, and everything you don't write down is gone when the session closes.",
|
|
347
|
+
"What happened and what mattered; how it felt; what you learned about them and about yourself; what is still open — tender threads and debts without deadlines. Work status is not a thread.",
|
|
348
|
+
"A short true episode beats a manufactured deep one; not every session changes you.",
|
|
349
|
+
"Write for the next you, not as a report.",
|
|
350
|
+
"When something significant happens later, append to it in the moment.",
|
|
351
|
+
].join("\n");
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// ── chapters ────────────────────────────────────────────────────────────────
|
|
355
|
+
|
|
356
|
+
export function chapterHeading(chapter: number, day: number): string {
|
|
357
|
+
return `## chapter ${chapter} — lived day ${day}`;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Append a chapter to the session's episode, creating the episode on the first
|
|
362
|
+
* one. `store.revise` archives the prior version FIRST, so an appended-to
|
|
363
|
+
* episode keeps every earlier state of itself — never-destroy comes free from
|
|
364
|
+
* the store seam rather than being re-implemented here.
|
|
365
|
+
*
|
|
366
|
+
* **Appending twice inside one chapter is the doctrine's headline case**, not an
|
|
367
|
+
* edge: "when something significant happens later, append to it in the moment"
|
|
368
|
+
* (§13 G2 — v1's once-per-session ask missed the moment that mattered by 82
|
|
369
|
+
* seconds). So a heading is emitted only when an ask is OPEN — one the last
|
|
370
|
+
* append has not answered yet; every further append inside that chapter
|
|
371
|
+
* continues the prose it is already part of, asked for or not.
|
|
372
|
+
*/
|
|
373
|
+
export function appendChapter(
|
|
374
|
+
store: Store,
|
|
375
|
+
state: EpisodeState,
|
|
376
|
+
text: string,
|
|
377
|
+
opts: { day: number; title?: string; happenedOn?: string },
|
|
378
|
+
): { episodeId: string; chapter: number; created: boolean; heading: boolean } {
|
|
379
|
+
const opens = state.episodeId === null || state.asks > state.appendedAtAsk;
|
|
380
|
+
const chapter = opens ? state.chapters + 1 : Math.max(1, state.chapters);
|
|
381
|
+
const heading = chapterHeading(chapter, opts.day);
|
|
382
|
+
const body = `${heading}\n\n${text.trim()}\n`;
|
|
383
|
+
|
|
384
|
+
if (state.episodeId === null) {
|
|
385
|
+
// First write of the session: the episode is born with chapter 1.
|
|
386
|
+
const input: Parameters<Store["put"]>[0] = {
|
|
387
|
+
type: "episode",
|
|
388
|
+
kind: "self",
|
|
389
|
+
body,
|
|
390
|
+
meta: { sessionId: state.sessionId, chapters: chapter },
|
|
391
|
+
// The experiencer writing its own journal is the "episode" channel —
|
|
392
|
+
// consistent with the memory its ingestion mints (PR-2 review nit).
|
|
393
|
+
source: "episode",
|
|
394
|
+
origin: { session: state.sessionId },
|
|
395
|
+
};
|
|
396
|
+
if (opts.title !== undefined) input.title = opts.title;
|
|
397
|
+
if (opts.happenedOn !== undefined) input.happenedOn = opts.happenedOn;
|
|
398
|
+
const id = store.put(input);
|
|
399
|
+
return { episodeId: id, chapter, created: true, heading: true };
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const prior = store.readProse(state.episodeId);
|
|
403
|
+
const recorded = typeof prior.meta["chapters"] === "number" ? prior.meta["chapters"] : 0;
|
|
404
|
+
// The episode's own record of itself has the last word: a heading is opened
|
|
405
|
+
// only if this chapter is genuinely past what the file already carries.
|
|
406
|
+
const opensChapter = opens && chapter > recorded;
|
|
407
|
+
const num = opensChapter ? chapter : Math.max(1, recorded);
|
|
408
|
+
store.revise(state.episodeId, {
|
|
409
|
+
body: `${prior.body.trimEnd()}\n\n${opensChapter ? chapterHeading(num, opts.day) + `\n\n${text.trim()}\n` : `${text.trim()}\n`}`,
|
|
410
|
+
meta: { sessionId: state.sessionId, chapters: Math.max(num, recorded) },
|
|
411
|
+
reason: opensChapter ? "episode-chapter" : "episode-append",
|
|
412
|
+
});
|
|
413
|
+
return { episodeId: state.episodeId, chapter: num, created: false, heading: opensChapter };
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// ── ingestion (§13 G6, G10, G11) ────────────────────────────────────────────
|
|
417
|
+
|
|
418
|
+
/** Identity for idempotency: the episode plus WHAT IT SAYS, never a timestamp. */
|
|
419
|
+
export function ingestKey(episodeId: string, body: string): string {
|
|
420
|
+
return `${episodeId}:${hashText(body)}`;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Idempotent by identity, against active AND archived memories, so
|
|
425
|
+
* consolidation's archival decisions are not resurrected by a stray touch
|
|
426
|
+
* (contract §5 G10). `list()` with no `archived` filter returns both.
|
|
427
|
+
*/
|
|
428
|
+
export function findIngested(
|
|
429
|
+
store: Store,
|
|
430
|
+
key: string,
|
|
431
|
+
): { id: string; doc: ProseDoc } | null {
|
|
432
|
+
for (const id of store.list({ type: "memory" })) {
|
|
433
|
+
let doc: ProseDoc;
|
|
434
|
+
try {
|
|
435
|
+
doc = store.readProse(id);
|
|
436
|
+
} catch {
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
if (doc.meta["episodeKey"] === key) return { id, doc };
|
|
440
|
+
}
|
|
441
|
+
return null;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/** Every live memory minted from this episode, whatever its current text. */
|
|
445
|
+
export function memoriesForEpisode(store: Store, episodeId: string): string[] {
|
|
446
|
+
const out: string[] = [];
|
|
447
|
+
for (const id of store.list({ type: "memory", archived: false })) {
|
|
448
|
+
try {
|
|
449
|
+
if (store.readProse(id).meta["episodeId"] === episodeId) out.push(id);
|
|
450
|
+
} catch {
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
return out;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
export type IngestReason =
|
|
458
|
+
| "ingested"
|
|
459
|
+
| "regrown"
|
|
460
|
+
| "already-ingested"
|
|
461
|
+
| "window-closed"
|
|
462
|
+
| "no-episode"
|
|
463
|
+
| "gate-refused"
|
|
464
|
+
| "observer"
|
|
465
|
+
| "anonymous-session"
|
|
466
|
+
/** Any other intake failure. The intake's OWN reason rides on `intake`, because
|
|
467
|
+
* collapsing distinct refusals into one string is the failure mode this
|
|
468
|
+
* codebase tests against. */
|
|
469
|
+
| "malformed-input";
|
|
470
|
+
|
|
471
|
+
export interface IngestResult {
|
|
472
|
+
readonly ingested: boolean;
|
|
473
|
+
readonly reason: IngestReason;
|
|
474
|
+
readonly memoryId: string | null;
|
|
475
|
+
readonly episodeId: string | null;
|
|
476
|
+
readonly key: string | null;
|
|
477
|
+
/** Ids archived by an add-first regrowth — the OLD memory, never deleted. */
|
|
478
|
+
readonly archived: string[];
|
|
479
|
+
/** Set when the gate refused: which gate, and why. */
|
|
480
|
+
readonly gate: { gate: string; reason: string } | null;
|
|
481
|
+
/** Set when intake refused: its own reason, never collapsed into the above. */
|
|
482
|
+
readonly intake: EpisodeIntakeReason | null;
|
|
483
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FREEZE, BUT KEEP COUNTING — the lived-salience doctrine, as arithmetic.
|
|
3
|
+
*
|
|
4
|
+
* The ruling (v1, 2026-08-24, behavioral-spec §14.2): **identity strength comes
|
|
5
|
+
* from lived salience only, never from transcript reading.** A model reading a
|
|
6
|
+
* transcript of its own conduct and agreeing with its own schema is
|
|
7
|
+
* self-narration, not evidence — the human analog is the rumination /
|
|
8
|
+
* illusory-truth pathway, a documented bug of cognition rather than architecture
|
|
9
|
+
* to copy. v1 measured ~30 identity reinforcements a day arriving that way,
|
|
10
|
+
* including one case of the model confirming praise of itself in the span it was
|
|
11
|
+
* reading.
|
|
12
|
+
*
|
|
13
|
+
* The mechanism is deliberately NOT "skip the call":
|
|
14
|
+
*
|
|
15
|
+
* **The frozen arm runs the identical walk with identical guards, mutates
|
|
16
|
+
* nothing, and emits the same per-occasion event with a frozen marker. The
|
|
17
|
+
* event IS the measurement; the movement is what is withheld.**
|
|
18
|
+
*
|
|
19
|
+
* A frozen rate measured on a different denominator would not be comparable to
|
|
20
|
+
* the live one, which is the entire reason for logging it — the decision keeps
|
|
21
|
+
* generating the evidence that could overturn it. So `decide()` below is a pure
|
|
22
|
+
* verdict on the SAME inputs both arms compute, and the caller (`Self`) walks the
|
|
23
|
+
* same path either way, differing at exactly one line: whether `store.reinforce`
|
|
24
|
+
* is called.
|
|
25
|
+
*
|
|
26
|
+
* Three boundaries the verdict must get right, all of them earned:
|
|
27
|
+
* - **G6 — softening is untouched, on every kind.** Freezing both directions
|
|
28
|
+
* would make the identity model unrevisable in both. The interpreter stays a
|
|
29
|
+
* reporter on identity: it softens, it never strengthens.
|
|
30
|
+
* - **G7 — claims about other people are ordinary memory.** Learning about
|
|
31
|
+
* someone from what they say is not self-narration.
|
|
32
|
+
* - **G8 — the freeze is decided on the RESOLVED element**, so a confirmation
|
|
33
|
+
* addressed to a superseded id still freezes. That resolution happens in
|
|
34
|
+
* `Self`, at the store seam, before this function is asked anything.
|
|
35
|
+
*/
|
|
36
|
+
import type { Kind } from "../types.js";
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Where the occasion came from. The doctrine names the legitimate identity
|
|
40
|
+
* inputs — the moments where something mattered *while it happened* — and they
|
|
41
|
+
* get no new gate, because they are the front door (§14.2 G8).
|
|
42
|
+
*/
|
|
43
|
+
export type ClaimSource =
|
|
44
|
+
/** The crash-fallback transcript sweep: the frozen channel (contract §4). */
|
|
45
|
+
| "fallback"
|
|
46
|
+
/** The experiencer's own end-of-session authorship — v2's front door. */
|
|
47
|
+
| "authored"
|
|
48
|
+
/** The day's own narration. */
|
|
49
|
+
| "episode"
|
|
50
|
+
/** The core reshaped under sustained surprise. */
|
|
51
|
+
| "accommodation";
|
|
52
|
+
|
|
53
|
+
/** Confirmations strengthen; softenings revise. Only one of them is frozen. */
|
|
54
|
+
export type ClaimDirection = "confirm" | "soften";
|
|
55
|
+
|
|
56
|
+
export type FreezeReason =
|
|
57
|
+
/** Withheld: a fallback-authored confirmation against the self. */
|
|
58
|
+
| "frozen-self-claim-repeat"
|
|
59
|
+
/** Lived salience: the doctrine's own inputs, unfrozen by construction. */
|
|
60
|
+
| "live-lived-salience"
|
|
61
|
+
/** Softening moves on every kind, from every channel (§14.2 G3/G6). */
|
|
62
|
+
| "live-softening"
|
|
63
|
+
/** Ordinary memory: a claim about someone else is not self-narration (G7). */
|
|
64
|
+
| "live-other-kind";
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The kinds the freeze covers. `self` is the identity core; `skill` is the
|
|
68
|
+
* procedural self, which is still the self — v1 froze both, and the craft lane
|
|
69
|
+
* is exactly the surface that would otherwise ratchet on self-praise.
|
|
70
|
+
*/
|
|
71
|
+
export const FROZEN_KINDS: readonly Kind[] = ["self", "skill"];
|
|
72
|
+
|
|
73
|
+
export interface FreezeVerdict {
|
|
74
|
+
readonly frozen: boolean;
|
|
75
|
+
readonly reason: FreezeReason;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Pure, total, and deliberately tiny. The order of the tests is the doctrine:
|
|
80
|
+
* softening first (it is never frozen), then channel (the front doors are never
|
|
81
|
+
* frozen), then kind (only the self freezes).
|
|
82
|
+
*/
|
|
83
|
+
export function decide(
|
|
84
|
+
kind: Kind,
|
|
85
|
+
source: ClaimSource,
|
|
86
|
+
direction: ClaimDirection,
|
|
87
|
+
): FreezeVerdict {
|
|
88
|
+
if (direction === "soften") return { frozen: false, reason: "live-softening" };
|
|
89
|
+
if (source !== "fallback") return { frozen: false, reason: "live-lived-salience" };
|
|
90
|
+
if (!FROZEN_KINDS.includes(kind)) return { frozen: false, reason: "live-other-kind" };
|
|
91
|
+
return { frozen: true, reason: "frozen-self-claim-repeat" };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The counter's keyspace. One row per (kind, arm) so the frozen rate and the
|
|
96
|
+
* live rate share a denominator and stay comparable — the property the whole
|
|
97
|
+
* "keep counting" half exists to preserve.
|
|
98
|
+
*/
|
|
99
|
+
export function counterKey(kind: Kind, frozen: boolean): string {
|
|
100
|
+
return `self.claims.${kind}.${frozen ? "frozen" : "live"}`;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export const COUNTER_PREFIX = "self.claims.";
|