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,778 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The adapter's configuration and its CAPABILITY REPORT.
|
|
3
|
+
*
|
|
4
|
+
* This file is where scar §2.18 lives — "a guarantee carried by something you
|
|
5
|
+
* don't own is not a guarantee". v1 encoded one host's 9,000-byte injection
|
|
6
|
+
* cliff as though it were physiology, discovered its socket ceiling by watching
|
|
7
|
+
* calls die, and inherited a credential from whatever shell happened to launch
|
|
8
|
+
* the session. All three are host facts. Here they are named, reported, and
|
|
9
|
+
* CHECKABLE — and the core is told what they are rather than assuming them.
|
|
10
|
+
*
|
|
11
|
+
* Two rules the CONTRACT states and this file mechanizes:
|
|
12
|
+
*
|
|
13
|
+
* §4 G4 — every host-dependent limit is discovered or asserted at runtime and
|
|
14
|
+
* surfaced as a checkable value. `capabilities()` returns one row per limit
|
|
15
|
+
* with `reported: false` where the host said nothing, so "we never asked" and
|
|
16
|
+
* "the host said zero" are different records (scar §2.4).
|
|
17
|
+
*
|
|
18
|
+
* §4 (drops) / scar §2.15 — every model SEAT has its own knob, holds a pinned
|
|
19
|
+
* identifier, and a placeholder EXPIRES. v1's single `models.maintainer` knob
|
|
20
|
+
* fed three call sites, so a decision that had been made could not be
|
|
21
|
+
* implemented; and its inert placeholder became production by silence.
|
|
22
|
+
*
|
|
23
|
+
* And one from observer-mode G5: an unreadable configuration resolves to
|
|
24
|
+
* OBSERVER, never to "encode anyway". `loadConfig` never throws.
|
|
25
|
+
*/
|
|
26
|
+
import { isAbsolute } from "node:path";
|
|
27
|
+
|
|
28
|
+
import { PAGE_WRITER_MODES } from "../../core/self/index.js";
|
|
29
|
+
import type { PageWriterMode } from "../../core/self/index.js";
|
|
30
|
+
import { DEFAULT_KEEP as SNAPSHOT_DEFAULT_KEEP } from "../snapshots.js";
|
|
31
|
+
|
|
32
|
+
import type { CredentialLoad } from "./credentials.js";
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
/** Every host-dependent limit this adapter depends on. One row each. */
|
|
36
|
+
export const CAPABILITIES = [
|
|
37
|
+
"injectionBudgetBytes",
|
|
38
|
+
"executionCeilingMs",
|
|
39
|
+
"socketLifetimeMs",
|
|
40
|
+
"credential",
|
|
41
|
+
] as const;
|
|
42
|
+
export type CapabilityName = (typeof CAPABILITIES)[number];
|
|
43
|
+
|
|
44
|
+
export interface CapabilityReport {
|
|
45
|
+
readonly name: CapabilityName;
|
|
46
|
+
/** False when the host told us nothing. Never silently defaulted. */
|
|
47
|
+
readonly reported: boolean;
|
|
48
|
+
readonly value: number | boolean | null;
|
|
49
|
+
readonly why: string;
|
|
50
|
+
/**
|
|
51
|
+
* WHICH SOURCE ANSWERED, name-level only. On the credential row: `"env"` when
|
|
52
|
+
* the process environment carried the name, `"file"` when the configured
|
|
53
|
+
* credentials file filled the gap, `"absent"` when nothing did. Never a value,
|
|
54
|
+
* never a hash of one.
|
|
55
|
+
*/
|
|
56
|
+
readonly detail?: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* One model seat. `id` is a PINNED identifier held in configuration, never an
|
|
61
|
+
* alias resolved at call time, and never shared with another seat.
|
|
62
|
+
*/
|
|
63
|
+
export interface ModelSeat {
|
|
64
|
+
readonly id: string;
|
|
65
|
+
/**
|
|
66
|
+
* True while this id is a stand-in nobody has decided on. A placeholder MUST
|
|
67
|
+
* carry `expires`; past that date `seatStatus()` reports `expired`, so
|
|
68
|
+
* "never decided" cannot masquerade as "decided" (scar §2.15c).
|
|
69
|
+
*/
|
|
70
|
+
readonly placeholder?: boolean;
|
|
71
|
+
/** ISO date. Required when `placeholder` is true. */
|
|
72
|
+
readonly expires?: string;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export type SeatStatus = "pinned" | "placeholder" | "expired" | "unbounded-placeholder";
|
|
76
|
+
|
|
77
|
+
export interface SeatVerdict {
|
|
78
|
+
readonly seat: string;
|
|
79
|
+
readonly id: string;
|
|
80
|
+
readonly status: SeatStatus;
|
|
81
|
+
readonly usable: boolean;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The default interpreter seat. It is the crash fallback's only model call, and
|
|
86
|
+
* the fallback is the path that must not lose the day, so it gets the strongest
|
|
87
|
+
* tier rather than the cheapest. PINNED, not an alias — recorded here so the
|
|
88
|
+
* decision has a place, per scar §2.15's first clause.
|
|
89
|
+
*/
|
|
90
|
+
export const DEFAULT_INTERPRET_MODEL = "claude-opus-5";
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The default EMBED seat. Its own knob, its own pinned id, its own provider —
|
|
94
|
+
* scar §2.15b is exactly the case where one knob fed several call sites, and an
|
|
95
|
+
* embedding model and an interpreter model are not interchangeable in any sense
|
|
96
|
+
* (different vendor, different credential, different failure mode).
|
|
97
|
+
*
|
|
98
|
+
* PINNED, not an alias: `voyage-3` and `voyage-3-large` are different spaces, and
|
|
99
|
+
* a store's vectors are only comparable to vectors from the generation that
|
|
100
|
+
* wrote them. The id is therefore part of the data's identity, not a preference.
|
|
101
|
+
*/
|
|
102
|
+
export const DEFAULT_EMBED_MODEL = "voyage-3-large";
|
|
103
|
+
|
|
104
|
+
/** The Messages API, raw. No SDK, no runtime dependency (constitution 10). */
|
|
105
|
+
export const ANTHROPIC_ENDPOINT = "https://api.anthropic.com/v1/messages";
|
|
106
|
+
export const ANTHROPIC_VERSION = "2023-06-01";
|
|
107
|
+
|
|
108
|
+
/** Voyage's embeddings endpoint, raw. Same rule: no SDK, no dependency. */
|
|
109
|
+
export const VOYAGE_ENDPOINT = "https://api.voyageai.com/v1/embeddings";
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* The ONE environment variable a credential may come from — and, when the
|
|
113
|
+
* environment is silent, the one file `credentialsFile` NAMES. See that knob for
|
|
114
|
+
* the whole rule; the short form is: the environment first, a file the config
|
|
115
|
+
* names second, a file found by convention never.
|
|
116
|
+
*/
|
|
117
|
+
export const API_KEY_ENV = "ANTHROPIC_API_KEY";
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* The embedder's ONE environment variable — the same name v1 reads, so an owner
|
|
121
|
+
* who already has a key in their environment does not learn a second name.
|
|
122
|
+
* v1 also accepted a `.env` FILE found by CONVENTION (`resolveVoyageKey`, in
|
|
123
|
+
* whatever directory the process happened to sit in); that half stays dropped.
|
|
124
|
+
* §2.18: the credential comes from one configured source the package names, and
|
|
125
|
+
* a file the process happens to FIND is not one — while a file the package's own
|
|
126
|
+
* configuration NAMES is (`credentialsFile`).
|
|
127
|
+
*/
|
|
128
|
+
export const EMBED_KEY_ENV = "VOYAGE_API_KEY";
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* The adapter's own tunables, in one visible place (the shape every core module
|
|
132
|
+
* uses). Nothing here is a memory property; all of it is host trivia.
|
|
133
|
+
*/
|
|
134
|
+
export const TUNABLES = {
|
|
135
|
+
/** Detached-worker watchdog, ms. Validated against `remember/`'s staleness
|
|
136
|
+
* window by `validateWatchdog` before any spawn (scars E4/E5). */
|
|
137
|
+
WATCHDOG_MS: 5 * 60_000,
|
|
138
|
+
/** The nightly page writer's own watchdog in `host` mode, ms. Longer than the
|
|
139
|
+
* worker's because the child is a whole host session — it loads MCP servers,
|
|
140
|
+
* runs SessionStart hooks and then makes a model call — and shorter than any
|
|
141
|
+
* person would wait, because a writer still running at the next boundary is a
|
|
142
|
+
* writer that failed. It bounds a process this package STARTED; unlike the
|
|
143
|
+
* worker's it is not validated against `remember/`'s claim staleness, because
|
|
144
|
+
* it claims no spans. */
|
|
145
|
+
PAGE_WRITER_MS: 10 * 60_000,
|
|
146
|
+
/** Output headroom for the interpreter. A truncated JSON response is a
|
|
147
|
+
* failure, not data (scar E2) — headroom is how you stop paying for one. */
|
|
148
|
+
MAX_OUTPUT_TOKENS: 16_000,
|
|
149
|
+
/** Reference resolution at a session-ending boundary (recall §9.2), ms. The
|
|
150
|
+
* resolver stops between candidates past it and the row says so
|
|
151
|
+
* (`recall.credit` reason `budget-exceeded`); nothing is truncated silently. */
|
|
152
|
+
CREDIT_BUDGET_MS: 150,
|
|
153
|
+
/** Identical consecutive spawn failures before the worker ESCALATES instead
|
|
154
|
+
* of re-logging the same line forever (scar E4's widening). */
|
|
155
|
+
ESCALATE_AFTER: 3,
|
|
156
|
+
/** Texts per embeddings request. The provider's documented ceiling, and the
|
|
157
|
+
* unit of FAILURE ISOLATION: one poisoned input fails its own chunk and
|
|
158
|
+
* leaves every sibling chunk's vectors standing (scar E1). v1 used the same
|
|
159
|
+
* 128. */
|
|
160
|
+
EMBED_BATCH_SIZE: 128,
|
|
161
|
+
// The Stop ask's pacing is NOT here any more, and that is the point: it was a
|
|
162
|
+
// second pacer beside `self/`'s, and two pacers on one blocked moment is how
|
|
163
|
+
// 13 owner turns drew about a dozen asks (2026-09-04). The one pair lives in
|
|
164
|
+
// `self/tunables.ts` — REASK_TURNS / REASK_BYTES — which is also where the
|
|
165
|
+
// numbers below were copied from when this knob was introduced.
|
|
166
|
+
} as const;
|
|
167
|
+
|
|
168
|
+
export interface AdapterConfig {
|
|
169
|
+
/** Where the memory lives. Pinned onto the child's environment LAST. */
|
|
170
|
+
readonly dataDir?: string;
|
|
171
|
+
/**
|
|
172
|
+
* THE CREDENTIAL FILE, and it is the source only because THIS FILE NAMES IT.
|
|
173
|
+
*
|
|
174
|
+
* Measured on day 0 of the parallel run: this host's hook processes carry
|
|
175
|
+
* neither documented name, even with both exported in the owner's shell rc —
|
|
176
|
+
* the host's process environment is not the login shell's. So a `process.env`
|
|
177
|
+
* that answers in a terminal answers nothing in a hook: the worker's sweep
|
|
178
|
+
* would skip itself at every boundary and the embedder would never open.
|
|
179
|
+
*
|
|
180
|
+
* The rules, mechanized in `credentials.ts`:
|
|
181
|
+
*
|
|
182
|
+
* - the file is `KEY=value` lines (`export KEY=value`, quotes and CRLF
|
|
183
|
+
* tolerated; blank and `#` lines skipped);
|
|
184
|
+
* - ONLY the two documented names are honored — `API_KEY_ENV` and
|
|
185
|
+
* `EMBED_KEY_ENV`. Anything else in the file is IGNORED AND COUNTED;
|
|
186
|
+
* - a value already present in `process.env` WINS. The environment stays the
|
|
187
|
+
* first source; the file only fills the gap;
|
|
188
|
+
* - values are never logged, never emitted, never hashed. Names and counts
|
|
189
|
+
* are the only things that leave.
|
|
190
|
+
*
|
|
191
|
+
* §2.18 permits exactly this and no more: "credentials come from one
|
|
192
|
+
* configured source the package owns". A file the package's own configuration
|
|
193
|
+
* NAMES is that source. A file found by CONVENTION — v1's `.env` in whatever
|
|
194
|
+
* directory the process sat in — is not, and stays forbidden.
|
|
195
|
+
*/
|
|
196
|
+
readonly credentialsFile?: string;
|
|
197
|
+
/** The host's reported injection ceiling, in bytes. NO DEFAULT (scar §2.18). */
|
|
198
|
+
readonly injectionBudgetBytes?: number;
|
|
199
|
+
/** How long the host lets a foreground hook run, ms. Reported, not assumed. */
|
|
200
|
+
readonly executionCeilingMs?: number;
|
|
201
|
+
/** How long a socket survives in this host, ms. The reason E3 rescoped here. */
|
|
202
|
+
readonly socketLifetimeMs?: number;
|
|
203
|
+
/** The detached worker's watchdog, ms. */
|
|
204
|
+
readonly watchdogMs?: number;
|
|
205
|
+
/** One knob per seat (scar §2.15b). Two seats, two knobs, two providers. */
|
|
206
|
+
readonly models?: { readonly interpret?: ModelSeat; readonly embed?: ModelSeat };
|
|
207
|
+
/**
|
|
208
|
+
* THE EGRESS KNOB, and it defaults to OFF.
|
|
209
|
+
*
|
|
210
|
+
* Embedding means sending the text of a memory to a third party. v2's
|
|
211
|
+
* "no-silent-egress" rescope (2026-08-25) is the reason this is a decision the
|
|
212
|
+
* owner makes rather than a capability a key in the environment switches on:
|
|
213
|
+
* a `VOYAGE_API_KEY` exported for some other tool must never be the thing that
|
|
214
|
+
* starts shipping this store's contents anywhere. Absent ⇒ no client is built,
|
|
215
|
+
* no socket is opened, and the brain runs exactly as it does today — blind,
|
|
216
|
+
* and countably so (`novelty.reason = "no-chunk-vector"`).
|
|
217
|
+
*/
|
|
218
|
+
readonly embedder?: { readonly enabled: boolean };
|
|
219
|
+
/**
|
|
220
|
+
* THE PARALLEL-RUN KNOB, and it defaults to ABSENT.
|
|
221
|
+
*
|
|
222
|
+
* Absent ⇒ v2 delivers on every delivering hook, which is what it does in
|
|
223
|
+
* every other situation and what every test that does not name this flag
|
|
224
|
+
* asserts. Present and true ⇒ every delivering hook first asks
|
|
225
|
+
* `primacy.ts` whether the OTHER system is speaking today, and stands down if
|
|
226
|
+
* the answer is anything but a clean "engram" (parallel-run G3). It is a knob
|
|
227
|
+
* rather than an inference for the same reason the egress knob is: the
|
|
228
|
+
* presence of v1's file on the machine must not be the thing that decides
|
|
229
|
+
* whether v2 speaks. RETIRED at PROMOTE.
|
|
230
|
+
*/
|
|
231
|
+
readonly parallel?: { readonly enabled: boolean };
|
|
232
|
+
/**
|
|
233
|
+
* THE DAILY ROTATING SNAPSHOT (owner ruling 3, 2026-09-18).
|
|
234
|
+
*
|
|
235
|
+
* All three sub-keys are optional and the block as a whole may be absent: the
|
|
236
|
+
* worker then keeps the newest `DEFAULT_KEEP` copies in a `snapshots/`
|
|
237
|
+
* directory beside the store. `dir` names one explicitly — required when the
|
|
238
|
+
* store is not the `store/` subdirectory of a base directory this package
|
|
239
|
+
* created, because in that case there is no base directory to put copies
|
|
240
|
+
* beside and helping ourselves to a sibling of somebody's own directory is not
|
|
241
|
+
* this package's to do (`adapters/snapshots.ts#resolveSnapshotsDir`).
|
|
242
|
+
*
|
|
243
|
+
* `mirror` is a second location that receives the same copy and rotates on its
|
|
244
|
+
* own terms; a mirror failure is reported and never fatal.
|
|
245
|
+
*
|
|
246
|
+
* **THIS ONE BLOCK IS READ LENIENTLY, AND IT IS THE ONLY ONE** (F2 review,
|
|
247
|
+
* MAJOR-2, coordinator's ruling 2026-09-18). Everything else in this file
|
|
248
|
+
* stands the whole configuration down to observer on a value it cannot read,
|
|
249
|
+
* and that is right for a knob whose wrong answer would make the adapter act:
|
|
250
|
+
* an egress switch, a path this package opens. It is wrong here. Measured on
|
|
251
|
+
* the branch: `"keep": 0` — or `"14"` with quotes, which is the likelier typo
|
|
252
|
+
* — returned `{ observer: true }` with `dataDir` GONE, so from the next hook on
|
|
253
|
+
* nothing was captured, nothing recalled, no row written, and the one stderr
|
|
254
|
+
* line a hook produces goes nowhere (I32). A backup preference is not worth
|
|
255
|
+
* memory. Each bad field falls back to its default, `ignored` records which and
|
|
256
|
+
* why, and doctor's Snapshot line says it out loud.
|
|
257
|
+
*/
|
|
258
|
+
readonly snapshots?: {
|
|
259
|
+
readonly dir?: string;
|
|
260
|
+
readonly keep?: number;
|
|
261
|
+
readonly mirror?: string;
|
|
262
|
+
/**
|
|
263
|
+
* What this file could not read inside the block, phrased for a person —
|
|
264
|
+
* "snapshots.keep was 0; using 14". Empty is absent. It is a REPORT, not a
|
|
265
|
+
* stance: nothing here changes what the adapter does beyond the default it
|
|
266
|
+
* fell back to, and doctor is what puts it in front of somebody.
|
|
267
|
+
*/
|
|
268
|
+
readonly ignored?: readonly string[];
|
|
269
|
+
};
|
|
270
|
+
/**
|
|
271
|
+
* THE NIGHTLY PAGE WRITER (2026-09-20, S2), and it defaults to `session`.
|
|
272
|
+
*
|
|
273
|
+
* `session` — the plan's fallback, and the one that needs no background
|
|
274
|
+
* process: the first session of the next day is asked, beside its wake, to
|
|
275
|
+
* revise the page from the day just gone. `host` — the owner's pick: a
|
|
276
|
+
* windowless `claude -p` started by the boundary's worker, woken by the
|
|
277
|
+
* ordinary SessionStart hook, with one pre-approved tool. `off` — nothing
|
|
278
|
+
* runs and nothing is written.
|
|
279
|
+
*
|
|
280
|
+
* **Absent means `session`, not off**, and that is a deliberate choice rather
|
|
281
|
+
* than an oversight: S2 is how a new user's page forms at all (plan §3), and
|
|
282
|
+
* a mechanism that only works for people who found a configuration key is not
|
|
283
|
+
* the product. The cost of the default being wrong is one extra block beside
|
|
284
|
+
* the wake, at most twice a day, deferred rather than truncated when the
|
|
285
|
+
* ceiling has no room for it — and `"mode": "off"` is one line.
|
|
286
|
+
*
|
|
287
|
+
* **Read LENIENTLY**, the second of the two blocks here that are (S2 review,
|
|
288
|
+
* 2026-09-20). It was written strict — `host` starts a process — and the
|
|
289
|
+
* argument does not hold on its own terms: `mode` is an exact-string
|
|
290
|
+
* allowlist, so a typo cannot resolve to `host` under a lenient reading
|
|
291
|
+
* either. Strictness bought nothing and cost the owner his memory for one
|
|
292
|
+
* misspelling in an optional block. The fallback is PINNED to `session`, each
|
|
293
|
+
* bad field names itself in `ignored`, and doctor's Page writer line says so.
|
|
294
|
+
*/
|
|
295
|
+
readonly pageWriter?: {
|
|
296
|
+
readonly mode: PageWriterMode;
|
|
297
|
+
/**
|
|
298
|
+
* The host CLI to launch in `host` mode. Absent ⇒ `claude`, resolved on the
|
|
299
|
+
* child's PATH. Named here because the one machine this has to work on is
|
|
300
|
+
* the owner's, and because a test proves the whole path against a stub.
|
|
301
|
+
*/
|
|
302
|
+
readonly command?: string;
|
|
303
|
+
/** The watchdog for that child, ms. Absent ⇒ `TUNABLES.PAGE_WRITER_MS`. */
|
|
304
|
+
readonly timeoutMs?: number;
|
|
305
|
+
/** What this file could not read inside the block, phrased for a person.
|
|
306
|
+
* Empty is absent. A REPORT, not a stance — doctor is what puts it in
|
|
307
|
+
* front of somebody. */
|
|
308
|
+
readonly ignored?: readonly string[];
|
|
309
|
+
};
|
|
310
|
+
/** Is this the owner's own session? Withholding is the safe direction. */
|
|
311
|
+
readonly owner?: boolean;
|
|
312
|
+
/** An instrument stands down. Fail direction: an unreadable config lands here. */
|
|
313
|
+
readonly observer?: boolean;
|
|
314
|
+
/** The identity core's name is the OWNER's; there is no default. */
|
|
315
|
+
readonly identity?: { readonly name: string; readonly aliases?: readonly string[] };
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
export interface LoadedConfig {
|
|
319
|
+
readonly config: AdapterConfig;
|
|
320
|
+
readonly ok: boolean;
|
|
321
|
+
readonly reason: "loaded" | "absent" | "unreadable";
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Read the host's configuration object. Never throws, and FAILS TOWARD STANDING
|
|
326
|
+
* DOWN: anything unreadable resolves to observer, because "encode anyway" on a
|
|
327
|
+
* configuration we could not read is how an instrument writes to a live store
|
|
328
|
+
* (observer-mode G5, scar E7).
|
|
329
|
+
*/
|
|
330
|
+
export function loadConfig(raw: unknown): LoadedConfig {
|
|
331
|
+
if (raw === undefined || raw === null) {
|
|
332
|
+
return { config: {}, ok: true, reason: "absent" };
|
|
333
|
+
}
|
|
334
|
+
if (typeof raw !== "object" || Array.isArray(raw)) {
|
|
335
|
+
return { config: { observer: true }, ok: false, reason: "unreadable" };
|
|
336
|
+
}
|
|
337
|
+
const rec = raw as Record<string, unknown>;
|
|
338
|
+
const out: {
|
|
339
|
+
dataDir?: string;
|
|
340
|
+
credentialsFile?: string;
|
|
341
|
+
injectionBudgetBytes?: number;
|
|
342
|
+
executionCeilingMs?: number;
|
|
343
|
+
socketLifetimeMs?: number;
|
|
344
|
+
watchdogMs?: number;
|
|
345
|
+
models?: { interpret?: ModelSeat; embed?: ModelSeat };
|
|
346
|
+
embedder?: { enabled: boolean };
|
|
347
|
+
parallel?: { enabled: boolean };
|
|
348
|
+
snapshots?: { dir?: string; keep?: number; mirror?: string; ignored?: string[] };
|
|
349
|
+
pageWriter?: { mode: PageWriterMode; command?: string; timeoutMs?: number; ignored?: string[] };
|
|
350
|
+
owner?: boolean;
|
|
351
|
+
observer?: boolean;
|
|
352
|
+
identity?: { name: string; aliases?: readonly string[] };
|
|
353
|
+
} = {};
|
|
354
|
+
let unreadable = false;
|
|
355
|
+
|
|
356
|
+
const num = (key: string): number | undefined => {
|
|
357
|
+
const value = rec[key];
|
|
358
|
+
if (value === undefined) return undefined;
|
|
359
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
|
|
360
|
+
unreadable = true;
|
|
361
|
+
return undefined;
|
|
362
|
+
}
|
|
363
|
+
return value;
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
if (typeof rec["dataDir"] === "string") out.dataDir = rec["dataDir"];
|
|
367
|
+
else if (rec["dataDir"] !== undefined) unreadable = true;
|
|
368
|
+
|
|
369
|
+
// Validated exactly like `dataDir`, and for the same reason: a path this
|
|
370
|
+
// package will OPEN is either a string the owner wrote or a configuration we
|
|
371
|
+
// did not understand, and the second one stands down rather than guessing.
|
|
372
|
+
if (typeof rec["credentialsFile"] === "string") out.credentialsFile = rec["credentialsFile"];
|
|
373
|
+
else if (rec["credentialsFile"] !== undefined) unreadable = true;
|
|
374
|
+
|
|
375
|
+
const injection = num("injectionBudgetBytes");
|
|
376
|
+
if (injection !== undefined) out.injectionBudgetBytes = injection;
|
|
377
|
+
const exec = num("executionCeilingMs");
|
|
378
|
+
if (exec !== undefined) out.executionCeilingMs = exec;
|
|
379
|
+
const socket = num("socketLifetimeMs");
|
|
380
|
+
if (socket !== undefined) out.socketLifetimeMs = socket;
|
|
381
|
+
const watchdog = num("watchdogMs");
|
|
382
|
+
if (watchdog !== undefined) out.watchdogMs = watchdog;
|
|
383
|
+
|
|
384
|
+
if (rec["owner"] !== undefined) {
|
|
385
|
+
if (typeof rec["owner"] !== "boolean") unreadable = true;
|
|
386
|
+
else out.owner = rec["owner"];
|
|
387
|
+
}
|
|
388
|
+
if (rec["observer"] !== undefined) {
|
|
389
|
+
if (typeof rec["observer"] !== "boolean") unreadable = true;
|
|
390
|
+
else out.observer = rec["observer"];
|
|
391
|
+
}
|
|
392
|
+
// One parser, both seats: a second spelling of "what a seat is" is how the two
|
|
393
|
+
// knobs drift apart while both look configured (scar §2.15b).
|
|
394
|
+
const readSeat = (raw: unknown): ModelSeat | undefined => {
|
|
395
|
+
if (raw === undefined) return undefined;
|
|
396
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
|
|
397
|
+
unreadable = true;
|
|
398
|
+
return undefined;
|
|
399
|
+
}
|
|
400
|
+
const s = raw as Record<string, unknown>;
|
|
401
|
+
if (typeof s["id"] !== "string" || s["id"].length === 0) {
|
|
402
|
+
unreadable = true;
|
|
403
|
+
return undefined;
|
|
404
|
+
}
|
|
405
|
+
const parsed: { id: string; placeholder?: boolean; expires?: string } = { id: s["id"] };
|
|
406
|
+
if (s["placeholder"] === true) parsed.placeholder = true;
|
|
407
|
+
if (typeof s["expires"] === "string") parsed.expires = s["expires"];
|
|
408
|
+
return parsed;
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
const models = rec["models"];
|
|
412
|
+
if (models !== undefined) {
|
|
413
|
+
if (typeof models !== "object" || models === null || Array.isArray(models)) {
|
|
414
|
+
unreadable = true;
|
|
415
|
+
} else {
|
|
416
|
+
const interpret = readSeat((models as Record<string, unknown>)["interpret"]);
|
|
417
|
+
const embed = readSeat((models as Record<string, unknown>)["embed"]);
|
|
418
|
+
if (interpret !== undefined || embed !== undefined) {
|
|
419
|
+
out.models = {
|
|
420
|
+
...(interpret === undefined ? {} : { interpret }),
|
|
421
|
+
...(embed === undefined ? {} : { embed }),
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
const embedder = rec["embedder"];
|
|
427
|
+
if (embedder !== undefined) {
|
|
428
|
+
// The egress knob is read STRICTLY. A misspelled or half-written `embedder`
|
|
429
|
+
// block must not resolve to "on" by accident, and the unreadable rule below
|
|
430
|
+
// sends the whole configuration to observer rather than guessing.
|
|
431
|
+
const e = embedder as Record<string, unknown>;
|
|
432
|
+
if (typeof embedder !== "object" || embedder === null || Array.isArray(embedder) || typeof e["enabled"] !== "boolean") {
|
|
433
|
+
unreadable = true;
|
|
434
|
+
} else {
|
|
435
|
+
out.embedder = { enabled: e["enabled"] };
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
const parallel = rec["parallel"];
|
|
439
|
+
if (parallel !== undefined) {
|
|
440
|
+
// Read as strictly as the egress knob, and for the same reason: a
|
|
441
|
+
// half-written `parallel` block must not resolve to "on" — and, this being
|
|
442
|
+
// the mute switch, it must not resolve to "off" by accident either. An
|
|
443
|
+
// unreadable one takes the whole configuration to observer below.
|
|
444
|
+
const p = parallel as Record<string, unknown>;
|
|
445
|
+
if (typeof parallel !== "object" || parallel === null || Array.isArray(parallel) || typeof p["enabled"] !== "boolean") {
|
|
446
|
+
unreadable = true;
|
|
447
|
+
} else {
|
|
448
|
+
out.parallel = { enabled: p["enabled"] };
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
// THE SECOND LENIENT BLOCK, and it became one under review (S2, 2026-09-20).
|
|
452
|
+
//
|
|
453
|
+
// It was written strict, beside the egress knob, on the argument that `host`
|
|
454
|
+
// STARTS A PROCESS and a half-written block must not resolve to it. The
|
|
455
|
+
// adversarial review took that argument apart on its own terms: `mode` is
|
|
456
|
+
// matched against an exact-string allowlist, so a typo cannot resolve to
|
|
457
|
+
// `host` under a lenient reading either — it can only resolve to the
|
|
458
|
+
// fallback. Strictness bought no protection against the stated risk and cost
|
|
459
|
+
// the owner his memory for one misspelling in an OPTIONAL block:
|
|
460
|
+
// `{"mode": "sesion"}` stood the whole configuration down to observer,
|
|
461
|
+
// `dataDir` went with it, and every session in that directory silently
|
|
462
|
+
// stopped remembering with one doctor line about the file as a whole.
|
|
463
|
+
// That is precisely the failure the F2 ruling moved `snapshots` out of
|
|
464
|
+
// strictness to avoid.
|
|
465
|
+
//
|
|
466
|
+
// So it falls back, and **the fallback is pinned to `session` and can never
|
|
467
|
+
// be `host`** — which is the one protection strictness was for, kept. Each
|
|
468
|
+
// bad field names itself in `ignored`, and doctor's Page writer line prints
|
|
469
|
+
// it. Nothing in here ever sets `unreadable`.
|
|
470
|
+
const pageWriter = rec["pageWriter"];
|
|
471
|
+
if (pageWriter !== undefined) {
|
|
472
|
+
out.pageWriter = readPageWriter(pageWriter);
|
|
473
|
+
}
|
|
474
|
+
// THE FIRST LENIENT BLOCK. See the `snapshots` knob above for why: a backup
|
|
475
|
+
// preference that could not be read must cost the backup preference and
|
|
476
|
+
// nothing else. Nothing in here ever sets `unreadable`.
|
|
477
|
+
const snapshots = rec["snapshots"];
|
|
478
|
+
if (snapshots !== undefined) {
|
|
479
|
+
out.snapshots = readSnapshots(snapshots);
|
|
480
|
+
}
|
|
481
|
+
const identity = rec["identity"];
|
|
482
|
+
if (identity !== undefined) {
|
|
483
|
+
const i = identity as Record<string, unknown>;
|
|
484
|
+
if (typeof i?.["name"] === "string" && i["name"].trim().length > 0) {
|
|
485
|
+
const aliases = i["aliases"];
|
|
486
|
+
out.identity = {
|
|
487
|
+
name: i["name"],
|
|
488
|
+
...(Array.isArray(aliases)
|
|
489
|
+
? { aliases: aliases.filter((a): a is string => typeof a === "string") }
|
|
490
|
+
: {}),
|
|
491
|
+
};
|
|
492
|
+
} else {
|
|
493
|
+
unreadable = true;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
if (unreadable) {
|
|
498
|
+
// Everything we did read is discarded along with the stance: a partially
|
|
499
|
+
// understood configuration is not a configuration.
|
|
500
|
+
return { config: { observer: true }, ok: false, reason: "unreadable" };
|
|
501
|
+
}
|
|
502
|
+
return { config: out, ok: true, reason: "loaded" };
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* THE FALLBACK MODE, and it is never `host`.
|
|
507
|
+
*
|
|
508
|
+
* A block this could not read resolves to the mode that needs no background
|
|
509
|
+
* process, no credential and no watchdog. "Fall back to the safe thing" and
|
|
510
|
+
* "fall back to the default" happen to be the same value today; they are
|
|
511
|
+
* written as one constant so they stay the same value if the default moves.
|
|
512
|
+
*/
|
|
513
|
+
export const PAGE_WRITER_FALLBACK_MODE: PageWriterMode = "session";
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* The `pageWriter` block, read leniently — every rejection names the field,
|
|
517
|
+
* what was in it, and what is being used instead.
|
|
518
|
+
*/
|
|
519
|
+
function readPageWriter(raw: unknown): {
|
|
520
|
+
mode: PageWriterMode;
|
|
521
|
+
command?: string;
|
|
522
|
+
timeoutMs?: number;
|
|
523
|
+
ignored?: string[];
|
|
524
|
+
} {
|
|
525
|
+
const raws: string[] = [];
|
|
526
|
+
const out: { mode: PageWriterMode; command?: string; timeoutMs?: number } = {
|
|
527
|
+
mode: PAGE_WRITER_FALLBACK_MODE,
|
|
528
|
+
};
|
|
529
|
+
const done = (): typeof out & { ignored?: string[] } =>
|
|
530
|
+
raws.length === 0 ? out : { ...out, ignored: tidy(raws) };
|
|
531
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
|
|
532
|
+
raws.push(`"pageWriter" was not an object; using mode ${PAGE_WRITER_FALLBACK_MODE}`);
|
|
533
|
+
return done();
|
|
534
|
+
}
|
|
535
|
+
const w = raw as Record<string, unknown>;
|
|
536
|
+
const mode = w["mode"];
|
|
537
|
+
if (typeof mode === "string" && (PAGE_WRITER_MODES as readonly string[]).includes(mode)) {
|
|
538
|
+
out.mode = mode as PageWriterMode;
|
|
539
|
+
} else if (mode !== undefined) {
|
|
540
|
+
raws.push(
|
|
541
|
+
`"pageWriter.mode" was ${JSON.stringify(mode)}, which is not ${PAGE_WRITER_MODES.join(", ")}; using ${PAGE_WRITER_FALLBACK_MODE}`,
|
|
542
|
+
);
|
|
543
|
+
}
|
|
544
|
+
const command = w["command"];
|
|
545
|
+
if (typeof command === "string" && command.trim().length > 0) out.command = command.trim();
|
|
546
|
+
else if (command !== undefined) {
|
|
547
|
+
raws.push(`"pageWriter.command" was ${JSON.stringify(command)}; using the default host command`);
|
|
548
|
+
}
|
|
549
|
+
const timeoutMs = w["timeoutMs"];
|
|
550
|
+
if (typeof timeoutMs === "number" && Number.isFinite(timeoutMs) && timeoutMs > 0) {
|
|
551
|
+
out.timeoutMs = timeoutMs;
|
|
552
|
+
} else if (timeoutMs !== undefined) {
|
|
553
|
+
raws.push(`"pageWriter.timeoutMs" was ${JSON.stringify(timeoutMs)}; using the default watchdog`);
|
|
554
|
+
}
|
|
555
|
+
// A key nobody here knows is named rather than passed over: a person who
|
|
556
|
+
// typed `"modes"` gets told so instead of watching the block do nothing.
|
|
557
|
+
for (const key of Object.keys(w)) {
|
|
558
|
+
if (key !== "mode" && key !== "command" && key !== "timeoutMs") {
|
|
559
|
+
raws.push(`"pageWriter.${key}" is not a setting this reads; it was ignored`);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
return done();
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* The `snapshots` block, read leniently — the FIRST place in this file that
|
|
567
|
+
* never stands the configuration down.
|
|
568
|
+
*
|
|
569
|
+
* Every rejection here names the field, what was in it, and what is being used
|
|
570
|
+
* instead, because "ignored silently" is the failure mode this block was moved
|
|
571
|
+
* out of strictness to avoid. `adapters/snapshots.ts#keepOf` owns the numeric
|
|
572
|
+
* fallback and is not restated; the two paths fall back to "no configured path",
|
|
573
|
+
* which means the default location beside the store.
|
|
574
|
+
*/
|
|
575
|
+
/** How much of one ignored line, and how many lines, ever leave this function. */
|
|
576
|
+
const IGNORED_LINE_CHARS = 120;
|
|
577
|
+
const IGNORED_LINES = 8;
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* The `ignored` phrases, made safe to print.
|
|
581
|
+
*
|
|
582
|
+
* They are built from the owner's own configuration file, and they end up on a
|
|
583
|
+
* doctor line in his terminal. A key carrying ANSI escapes was measured reaching
|
|
584
|
+
* that terminal raw, and a 200,000-character value made a 200,000-character line
|
|
585
|
+
* (second F2 review, MINOR-d). Control characters go, each line is capped, and
|
|
586
|
+
* the list is capped — sanitized HERE, at the one place the phrases are made,
|
|
587
|
+
* rather than at each of the surfaces that print them.
|
|
588
|
+
*/
|
|
589
|
+
function tidy(lines: readonly string[]): string[] {
|
|
590
|
+
const clean = lines.map((line) => {
|
|
591
|
+
const stripped = line.replace(/[\u0000-\u001f\u007f]/g, "");
|
|
592
|
+
return stripped.length > IGNORED_LINE_CHARS
|
|
593
|
+
? `${stripped.slice(0, IGNORED_LINE_CHARS - 1)}…`
|
|
594
|
+
: stripped;
|
|
595
|
+
});
|
|
596
|
+
if (clean.length <= IGNORED_LINES) return clean;
|
|
597
|
+
return [
|
|
598
|
+
...clean.slice(0, IGNORED_LINES),
|
|
599
|
+
`and ${String(clean.length - IGNORED_LINES)} more`,
|
|
600
|
+
];
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
function readSnapshots(raw: unknown): {
|
|
604
|
+
dir?: string;
|
|
605
|
+
keep?: number;
|
|
606
|
+
mirror?: string;
|
|
607
|
+
ignored?: string[];
|
|
608
|
+
} {
|
|
609
|
+
const raws: string[] = [];
|
|
610
|
+
const ignored = (line: string): void => {
|
|
611
|
+
raws.push(line);
|
|
612
|
+
};
|
|
613
|
+
const out: { dir?: string; keep?: number; mirror?: string; ignored?: string[] } = {};
|
|
614
|
+
const done = (): typeof out =>
|
|
615
|
+
raws.length === 0 ? out : { ...out, ignored: tidy(raws) };
|
|
616
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
|
|
617
|
+
ignored('"snapshots" was not an object; the whole block was ignored');
|
|
618
|
+
return done();
|
|
619
|
+
}
|
|
620
|
+
const sn = raw as Record<string, unknown>;
|
|
621
|
+
const known = new Set(["dir", "keep", "mirror"]);
|
|
622
|
+
for (const key of Object.keys(sn)) {
|
|
623
|
+
// An unknown sub-key is REPORTED rather than dropped in silence: a typo'd
|
|
624
|
+
// `"mirrors"` that quietly did nothing is how somebody believes they have a
|
|
625
|
+
// second copy and does not.
|
|
626
|
+
if (!known.has(key)) ignored(`"snapshots.${key}" is not a setting this reads`);
|
|
627
|
+
}
|
|
628
|
+
for (const key of ["dir", "mirror"] as const) {
|
|
629
|
+
const value = sn[key];
|
|
630
|
+
if (value === undefined) continue;
|
|
631
|
+
if (typeof value !== "string") {
|
|
632
|
+
ignored(`"snapshots.${key}" was not a path; using the default location`);
|
|
633
|
+
continue;
|
|
634
|
+
}
|
|
635
|
+
if (value.trim().length === 0) {
|
|
636
|
+
// Blank meaning "the default" was measured as a surprise: somebody who
|
|
637
|
+
// blanks the value to switch snapshots OFF gets the default location.
|
|
638
|
+
ignored(`"snapshots.${key}" was blank; using the default location`);
|
|
639
|
+
continue;
|
|
640
|
+
}
|
|
641
|
+
if (!isAbsolute(value)) {
|
|
642
|
+
// A relative path resolves against the WORKER's current directory, which
|
|
643
|
+
// is whatever directory the host session happened to be in — copies would
|
|
644
|
+
// scatter one per project and rotation would run inside each.
|
|
645
|
+
ignored(`"snapshots.${key}" must be an absolute path; using the default location`);
|
|
646
|
+
continue;
|
|
647
|
+
}
|
|
648
|
+
out[key] = value;
|
|
649
|
+
}
|
|
650
|
+
const keep = sn["keep"];
|
|
651
|
+
if (keep !== undefined) {
|
|
652
|
+
if (typeof keep !== "number" || !Number.isInteger(keep) || keep <= 0) {
|
|
653
|
+
ignored(
|
|
654
|
+
`"snapshots.keep" was ${JSON.stringify(keep)}; using ${String(SNAPSHOT_DEFAULT_KEEP)}`,
|
|
655
|
+
);
|
|
656
|
+
} else {
|
|
657
|
+
out.keep = keep;
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
return done();
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
/**
|
|
664
|
+
* The capability report — one row per host-dependent limit, each saying whether
|
|
665
|
+
* the host actually reported it. This is the "surfaced as a checkable value"
|
|
666
|
+
* half of §4 G4; the "exceeding one is an event" half lives at the call sites.
|
|
667
|
+
*
|
|
668
|
+
* The credential row also says WHICH SOURCE ANSWERED. `load` is the result of
|
|
669
|
+
* this process's `loadCredentials` call, passed in rather than looked up: the
|
|
670
|
+
* provenance of a credential is a fact about one process's startup, and a module
|
|
671
|
+
* that remembered it globally would leak between the runs of a test suite and
|
|
672
|
+
* lie about which source answered.
|
|
673
|
+
*/
|
|
674
|
+
/**
|
|
675
|
+
* WHICH MODE THE NIGHTLY PAGE WRITER RUNS IN — the one place the default lives.
|
|
676
|
+
*
|
|
677
|
+
* Absent ⇒ `session`: the fallback that needs no background process, so a store
|
|
678
|
+
* a stranger installed grows a page without them configuring anything. An
|
|
679
|
+
* observer is `off` whatever the file says, for the reason every stance check
|
|
680
|
+
* in this package gives: an instrument does not set writers going against a
|
|
681
|
+
* store it may not write.
|
|
682
|
+
*/
|
|
683
|
+
export function pageWriterMode(config: AdapterConfig): PageWriterMode {
|
|
684
|
+
if (config.observer === true) return "off";
|
|
685
|
+
return config.pageWriter?.mode ?? "session";
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
export function capabilities(
|
|
689
|
+
config: AdapterConfig,
|
|
690
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
691
|
+
load?: CredentialLoad,
|
|
692
|
+
): CapabilityReport[] {
|
|
693
|
+
const key = env[API_KEY_ENV];
|
|
694
|
+
const present = key !== undefined && key.trim().length > 0;
|
|
695
|
+
// Name-level only. "Which source" is the whole answer; the value never
|
|
696
|
+
// reaches this function's output in any form.
|
|
697
|
+
const source = !present ? "absent" : load?.loaded.includes(API_KEY_ENV) === true ? "file" : "env";
|
|
698
|
+
return [
|
|
699
|
+
{
|
|
700
|
+
name: "injectionBudgetBytes",
|
|
701
|
+
reported: config.injectionBudgetBytes !== undefined,
|
|
702
|
+
value: config.injectionBudgetBytes ?? null,
|
|
703
|
+
why: "The host's injection cliff. v1 hardcoded 90% of one host's, as though it were physiology.",
|
|
704
|
+
},
|
|
705
|
+
{
|
|
706
|
+
name: "executionCeilingMs",
|
|
707
|
+
reported: config.executionCeilingMs !== undefined,
|
|
708
|
+
value: config.executionCeilingMs ?? null,
|
|
709
|
+
why: "How long a foreground hook may run before the host gives up on it.",
|
|
710
|
+
},
|
|
711
|
+
{
|
|
712
|
+
name: "socketLifetimeMs",
|
|
713
|
+
reported: config.socketLifetimeMs !== undefined,
|
|
714
|
+
value: config.socketLifetimeMs ?? null,
|
|
715
|
+
why: "Why long calls stream (scar E3, rescoped to the adapter that owns the host).",
|
|
716
|
+
},
|
|
717
|
+
{
|
|
718
|
+
name: "credential",
|
|
719
|
+
reported: key !== undefined,
|
|
720
|
+
value: present,
|
|
721
|
+
why: "The detached worker starved for two days on a credential it expected to inherit (scar E4).",
|
|
722
|
+
detail: source,
|
|
723
|
+
},
|
|
724
|
+
];
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
/** The seat, and whether its identifier is a decision or a silence. */
|
|
728
|
+
export function seatStatus(
|
|
729
|
+
name: string,
|
|
730
|
+
seat: ModelSeat | undefined,
|
|
731
|
+
today: string,
|
|
732
|
+
/** The pinned id this seat falls back to when the host configured none. */
|
|
733
|
+
fallbackId: string = DEFAULT_INTERPRET_MODEL,
|
|
734
|
+
): SeatVerdict {
|
|
735
|
+
const resolved: ModelSeat = seat ?? { id: fallbackId };
|
|
736
|
+
if (resolved.placeholder !== true) {
|
|
737
|
+
return { seat: name, id: resolved.id, status: "pinned", usable: true };
|
|
738
|
+
}
|
|
739
|
+
if (resolved.expires === undefined) {
|
|
740
|
+
// A placeholder with no expiry is precisely the thing that becomes
|
|
741
|
+
// production by silence. It is refused, not warned about.
|
|
742
|
+
return { seat: name, id: resolved.id, status: "unbounded-placeholder", usable: false };
|
|
743
|
+
}
|
|
744
|
+
if (today > resolved.expires) {
|
|
745
|
+
return { seat: name, id: resolved.id, status: "expired", usable: false };
|
|
746
|
+
}
|
|
747
|
+
return { seat: name, id: resolved.id, status: "placeholder", usable: true };
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
/** The interpreter seat, resolved. One seat, one knob (scar §2.15b). */
|
|
751
|
+
export function interpretSeat(config: AdapterConfig, today: string): SeatVerdict {
|
|
752
|
+
return seatStatus("interpret", config.models?.interpret, today);
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
/** The embedder seat, resolved. Its own knob, its own pinned default. */
|
|
756
|
+
export function embedSeat(config: AdapterConfig, today: string): SeatVerdict {
|
|
757
|
+
return seatStatus("embed", config.models?.embed, today, DEFAULT_EMBED_MODEL);
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* Is the embedder switched on, and is its credential present? Both halves are
|
|
762
|
+
* REPORTED rather than inferred, so "the owner said no" and "the owner said yes
|
|
763
|
+
* and the key is missing" are different records — the second is a refusal worth
|
|
764
|
+
* an event, the first is not (scar §2.4).
|
|
765
|
+
*/
|
|
766
|
+
export function embedderState(
|
|
767
|
+
config: AdapterConfig,
|
|
768
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
769
|
+
): {
|
|
770
|
+
readonly enabled: boolean;
|
|
771
|
+
readonly credential: boolean;
|
|
772
|
+
} {
|
|
773
|
+
const key = env[EMBED_KEY_ENV];
|
|
774
|
+
return {
|
|
775
|
+
enabled: config.embedder?.enabled === true,
|
|
776
|
+
credential: key !== undefined && key.trim().length > 0,
|
|
777
|
+
};
|
|
778
|
+
}
|