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,155 @@
|
|
|
1
|
+
# `physics/` — NOTES
|
|
2
|
+
|
|
3
|
+
Working notes beside the contract. **`CONTRACT.md` is the spec; this file never
|
|
4
|
+
edits it.** Where the contract is silent or its prose and its equations disagree,
|
|
5
|
+
the choice made in `index.ts` is recorded here so the next reader does not have to
|
|
6
|
+
re-derive it — and so the owner can overrule any of it cheaply (constitution
|
|
7
|
+
line 13: decisions are defaults).
|
|
8
|
+
|
|
9
|
+
## Implementation choices
|
|
10
|
+
|
|
11
|
+
*(First build, 2026-08-25. Every item is a place the contract did not decide, or
|
|
12
|
+
decided twice.)*
|
|
13
|
+
|
|
14
|
+
1. **The claimed-salience floor is stored, not applied to the dimensions.**
|
|
15
|
+
§5.1 makes a claimed salience a floor clamped "at the proposal→memory seam,"
|
|
16
|
+
but guarantee 2 also fixes the dimensions at birth, and §5.1 forbids ever
|
|
17
|
+
defaulting a null novelty. Lifting a dimension to satisfy a claim would break
|
|
18
|
+
one of those. So `Salience` gained an optional `claimed` field: the dimensions
|
|
19
|
+
are stored exactly as authored, the claim is stored beside them, and
|
|
20
|
+
`sal(m) = max(mean(dims), claimed)`. `sal(m)` therefore stays reproducible from
|
|
21
|
+
stored state alone, the lift is inspectable forever rather than only in the
|
|
22
|
+
event log, and `clampSalienceAtSeam()` still emits `salience.lifted` at the
|
|
23
|
+
seam (guarantee 2's event).
|
|
24
|
+
|
|
25
|
+
2. **`base` is not clamped; only `strength` is.** §5.2 clamps at `strength`
|
|
26
|
+
and nowhere else, so `base` can exceed 1 (salience 1.0 plus the consolidation
|
|
27
|
+
bonus reads 1.2). Kept literal. Consequence: `THETA_ID` is compared against an
|
|
28
|
+
unclamped `base`, which is the only reading under which the threshold can
|
|
29
|
+
discriminate at the top of the range at all.
|
|
30
|
+
|
|
31
|
+
3. **Promotion counts a new field, `reinforcedDays`.** §5.3 gates promotion on
|
|
32
|
+
"reinforcement on ≥ N = 3 **distinct lived days**," which `uses` cannot
|
|
33
|
+
reconstruct — `uses` is a weighted sum (a 0.25 surfaced credit and four of
|
|
34
|
+
them are indistinguishable). `MemoryPhysics` therefore gained
|
|
35
|
+
`reinforcedDays`, incremented only by `creditUse()`. It is **optional** in
|
|
36
|
+
`types.ts` (absent reads as 0) so that adding it breaks no other module's
|
|
37
|
+
construction sites; 0 is the promotion-*blocking* direction, so an un-migrated
|
|
38
|
+
row can never be promoted by accident.
|
|
39
|
+
|
|
40
|
+
4. **Any credited tier counts a reinforced day.** A 0.25 "surfaced-unused"
|
|
41
|
+
credit is still a lived day on which the memory was reinforced. The contract
|
|
42
|
+
distinguishes the tiers by weight, not by whether they count as an occasion.
|
|
43
|
+
|
|
44
|
+
5. **Pressure has its own stability constant, `S_PRESSURE = 60`.** §5.6 says P
|
|
45
|
+
decays "like everything else — the same curve family, keyed to the last
|
|
46
|
+
challenge day." *Family*, not the same constant: reusing the target's `S(m)`
|
|
47
|
+
would couple how long a challenge persists to the target's own use history and
|
|
48
|
+
kind, which is machinery nobody asked for (Amendment 15). One flat constant,
|
|
49
|
+
no κ, no `uses` coupling.
|
|
50
|
+
|
|
51
|
+
6. **Pressure decays even against a decay-exempt identity element.** `D = 1`
|
|
52
|
+
exempts the *memory* (§5.4's named deviation); nothing exempts the *pressure
|
|
53
|
+
standing against it*. Otherwise a stale challenge on an identity element would
|
|
54
|
+
wait forever — exactly the ambush §5.6 says pressure decay exists to prevent.
|
|
55
|
+
|
|
56
|
+
7. **Order of operations in `applyChallenge`: decay P to `d` → add F → compare.**
|
|
57
|
+
The alternative (compare, then decay) would let a challenge win on pressure it
|
|
58
|
+
no longer has.
|
|
59
|
+
|
|
60
|
+
8. **The credited challenge is the FIRST of the lived day, not the strongest.**
|
|
61
|
+
§5.6 says "one credited challenge per target per lived day" without saying
|
|
62
|
+
which. First-wins needs no lookahead and no re-scoring, and mirrors §5.5's
|
|
63
|
+
occasion rule, which is also first-wins.
|
|
64
|
+
|
|
65
|
+
9. **`REVISE` is strict `>`.** The contract writes `P(old) > ι × strength(old,d)`.
|
|
66
|
+
Ties hold.
|
|
67
|
+
|
|
68
|
+
10. **Novelty is clamped to [0, 1].** `1 − cos` exceeds 1 for a negative cosine;
|
|
69
|
+
the contract is silent. Clamped, because every other salience dimension is
|
|
70
|
+
0–1 and the mean would otherwise be out of range.
|
|
71
|
+
|
|
72
|
+
11. **`successorSeed()` inherits identity by default.** §5.3(a) says a declared
|
|
73
|
+
revision landing on an identity element hands membership to the successor;
|
|
74
|
+
§5.3 also says identity is "demoted only by revision." Both are true with an
|
|
75
|
+
explicit `inheritIdentity: false` opt-out, so demotion is possible but never
|
|
76
|
+
accidental.
|
|
77
|
+
|
|
78
|
+
12. **The prune record carries exactly the contract's fields** — day, kind, band,
|
|
79
|
+
birth day, last-used day, uses, strength. No id, no hash, no body (scar
|
|
80
|
+
§2.20). A caller that wants to know *which* memory it just pruned already
|
|
81
|
+
knows: it passed it in. Attaching identity to telemetry is the caller's
|
|
82
|
+
decision, made in the open.
|
|
83
|
+
|
|
84
|
+
13. **All three decay shapes ship behind `TUNABLES.DECAY_SHAPE`, defaulting to
|
|
85
|
+
exponential.** Open question 1 names `tools/replay` as the deciding consumer
|
|
86
|
+
and says all three are one line — they are. Only the default is tested for
|
|
87
|
+
behavior; the alternates are tested only for `D(0) = 1` and monotonicity.
|
|
88
|
+
Choosing between them is replay's job, not this module's.
|
|
89
|
+
|
|
90
|
+
14. **`livedDay()` is a function of an active-day list, not of elapsed calendar
|
|
91
|
+
days.** The lived-day integer is "how many distinct active days came before
|
|
92
|
+
this one," which makes a week away one interval and not seven, and makes a key
|
|
93
|
+
that is not yet in the list total rather than a special case.
|
|
94
|
+
|
|
95
|
+
15. **Every verdict names its reason, and every gate names *all* its failures.**
|
|
96
|
+
`blockedBy` on prune and promotion lists every failing condition, not just the
|
|
97
|
+
first, so a log line can never read "refused" without saying by what. Reasons
|
|
98
|
+
are a closed union, so an unhandled case is a type error rather than a string
|
|
99
|
+
nobody greps for.
|
|
100
|
+
|
|
101
|
+
16. **An UNCLAIMED memory takes its channel's default floor, and a default is
|
|
102
|
+
never a lift.** (2026-09-04, measured on the live parallel-run store.) All 48
|
|
103
|
+
authored memories carried `relevance = emotional = predictive = 0` and most
|
|
104
|
+
carried no claim, so `sal(m) = max(0, null ?? 0) = 0`: the lived channel's own
|
|
105
|
+
deposits were the weakest things in the store, first to decay, and — since
|
|
106
|
+
`challengeForce = strength × sal` — every revision they declared pushed with
|
|
107
|
+
ZERO force. That inverts the authorship doctrine it was supposed to serve.
|
|
108
|
+
`clampSalienceAtSeam()` therefore takes a fourth argument, `defaultClaim`,
|
|
109
|
+
applied only when `claimed === null`; `mint.ts` passes
|
|
110
|
+
`TUNABLES.AUTHORED_DEFAULT_CLAIM` on the `authored` channel and `null` on every
|
|
111
|
+
other, engine-set off the channel exactly as `SWEEP_CLAIM_CEILING` is. Three
|
|
112
|
+
properties are deliberate:
|
|
113
|
+
- **0.25, and the number is arithmetic.** `0.25 + CONS_BONUS = 0.45 <
|
|
114
|
+
THETA_SEM = 0.5`, and `max(ω_sal) = 1.0`, so a defaulted memory cannot reach
|
|
115
|
+
the semantic band on the default alone even after consolidation — it needs
|
|
116
|
+
3 credited days of use (`rep = 0.36`) or an actual claim. That is the
|
|
117
|
+
structural form of the F5 scar (a self-claimed 0.8 parked ~75% of the store
|
|
118
|
+
above `THETA_SEM`). It also sits below 0.34, the measured mean of the claims
|
|
119
|
+
authors did make, so silence says strictly less than speaking, and below
|
|
120
|
+
`SWEEP_CLAIM_CEILING = 0.6`, so the floor is not a promotion over the
|
|
121
|
+
retelling channel.
|
|
122
|
+
- **A default is recorded as `defaulted`, never as `lifted`.** The lift metric
|
|
123
|
+
is "how often does an author's claim out-rank the computed dimensions"; a
|
|
124
|
+
defaulted floor folded into it would read every silent note as a claim and
|
|
125
|
+
destroy the number the ceiling decision needs. `SeamClamp` gained
|
|
126
|
+
`defaulted` and `defaultEvent` (`salience.defaulted`) for that reason, and
|
|
127
|
+
the separation is load-bearing rather than tidy: `tools/replay/baselines.ts`
|
|
128
|
+
computes three watch metrics off `salience.lifted` and `mint.proposal`'s
|
|
129
|
+
`lifted` flag (lift rate, mean lift size, capped share). A default entering
|
|
130
|
+
either would move all three silently, against F5's own +0.150 / 97.9%
|
|
131
|
+
baselines. Defaults stay out of both by construction.
|
|
132
|
+
- **An explicit claim, however low, is never overridden** — the branch is on
|
|
133
|
+
`claimed === null` and nothing else. An explicit `0` is testimony.
|
|
134
|
+
The FALLBACK channel is untouched: its ceiling and its interpreter-supplied
|
|
135
|
+
dimensions are exactly what they were. CAL, and a working default.
|
|
136
|
+
|
|
137
|
+
## Observations for the owner (arithmetic vs prose)
|
|
138
|
+
|
|
139
|
+
- **§5.6's "a slow kind cannot cross from rest in fewer than ~3 lived days" is
|
|
140
|
+
an approximation, not a bound.** The maximum single-day force is
|
|
141
|
+
`strength(new) × sal(new) = 1.0` (a challenger with all four dimensions at 1.0),
|
|
142
|
+
while the highest possible self-kind bar is `ι × strength(old) = 0.9 × 1.0 =
|
|
143
|
+
0.9`. So one *flawless* challenge can cross a self belief in one day. The
|
|
144
|
+
per-day cap bounds **spam**, not a single perfect claim. The equations win over
|
|
145
|
+
the prose (they are what §5 says to implement exactly), so nothing was "fixed"
|
|
146
|
+
in code — but if the owner meant the prose as the guarantee, the cheapest fix is
|
|
147
|
+
a per-day force cap (`min(F, F_day_max)`), not a change to ι.
|
|
148
|
+
`test/physics.test.ts` documents the boundary by using salience 0.9 (F = 0.81)
|
|
149
|
+
for its "one loud claim" spam, which is below the 0.855 bar it tests against.
|
|
150
|
+
|
|
151
|
+
- **`TAU_DUP`, `PHI_PRUNE`, `D_FLOOR_DAYS`, `POWER_LAW_PSI`, the symmetry ratio,
|
|
152
|
+
and the whole per-kind table are calibration-required** and marked as such in
|
|
153
|
+
the `TUNABLES` table. Per scar §2.8 and guarantee 11 they ship with a recorded
|
|
154
|
+
measurement or they ship disabled; `PHI_PRUNE` and `D_FLOOR_DAYS` are the two
|
|
155
|
+
with no ancestry at all (open question 5 — v1 never pruned).
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The active-day clock — the ONE lived-day function (CONTRACT §5.4, scar E8).
|
|
3
|
+
*
|
|
4
|
+
* Every site that needs "which day is this" routes through here. This is the only
|
|
5
|
+
* file in `physics/` allowed to touch `Date`; `test/physics.test.ts` asserts that
|
|
6
|
+
* by scanning the sources (the totality test of guarantee 6 / scar §2.4).
|
|
7
|
+
*
|
|
8
|
+
* Rollover is at a LOCAL boundary hour, not UTC: a UTC rollover splits one lived
|
|
9
|
+
* evening into two days and corrupts every occasion count downstream [v1 §11 G2].
|
|
10
|
+
*
|
|
11
|
+
* Pure: no I/O, no ambient clock reads. The caller supplies the instant.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** TUNABLE. Re-exported through `TUNABLES.BOUNDARY_HOUR` — this is the same number. */
|
|
15
|
+
export const BOUNDARY_HOUR_DEFAULT = 4;
|
|
16
|
+
|
|
17
|
+
function pad(n: number): string {
|
|
18
|
+
return n < 10 ? `0${n}` : `${n}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The local calendar day an instant belongs to, given the boundary hour.
|
|
23
|
+
* 2026-08-25 01:30 local with boundary 4 is still the lived day 2026-08-24.
|
|
24
|
+
* Returns a sortable `YYYY-MM-DD` key (lexical order === chronological order).
|
|
25
|
+
*/
|
|
26
|
+
export function dayKey(at: Date | number, boundaryHour: number = BOUNDARY_HOUR_DEFAULT): string {
|
|
27
|
+
const t = typeof at === "number" ? new Date(at) : new Date(at.getTime());
|
|
28
|
+
t.setHours(t.getHours() - boundaryHour);
|
|
29
|
+
return `${t.getFullYear()}-${pad(t.getMonth() + 1)}-${pad(t.getDate())}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The lived-day integer for `key`: the number of DISTINCT active days that came
|
|
34
|
+
* before it. Days not lived do not advance the clock — a week away must not decay
|
|
35
|
+
* a week's worth [engram E8]. A key not in `activeDays` gets the index it would
|
|
36
|
+
* take if appended, so "today, first event of the day" is total, not a special case.
|
|
37
|
+
*/
|
|
38
|
+
export function livedDay(activeDays: readonly string[], key: string): number {
|
|
39
|
+
let n = 0;
|
|
40
|
+
for (const k of new Set(activeDays)) if (k < key) n++;
|
|
41
|
+
return n;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Lived days elapsed between two active-day keys (never negative). */
|
|
45
|
+
export function livedDaysBetween(
|
|
46
|
+
activeDays: readonly string[],
|
|
47
|
+
fromKey: string,
|
|
48
|
+
toKey: string,
|
|
49
|
+
): number {
|
|
50
|
+
return Math.max(0, livedDay(activeDays, toKey) - livedDay(activeDays, fromKey));
|
|
51
|
+
}
|