Compare commits

...

3 Commits

Author SHA1 Message Date
41f014e7a7 ts_core: a framework-free TypeScript utility package
Some checks failed
palette drift / palette (push) Waiting to run
ts_core tests / test (push) Has been cancelled
`Clock` and a seeded mulberry32 RNG had each been reimplemented across the
game and web repos -- Clock twice (konbini-67, iron-and-anvil, doc comments
near-verbatim), the RNG four times. None of it is UI, so no existing shared
package owned it: `web_ui` ships `tokens.css` and React components, and a
game importing a UI package to get a seeded RNG is exactly the coupling
that makes people vendor their own copy instead.

The RNG deliberately exports **two** interfaces rather than reconciling
them into one. konbini-67 threaded mutable state through free functions;
sims3-clone and europe-county-map returned an object of closures. The
sequence is the contract here -- these repos have seeded tests and "same
seed -> identical world" guarantees, so changing how many times a helper
advances the generator silently re-rolls every saved world. A shared
package that did that would be worse than the duplication it replaces. The
core is shared, so both interfaces yield the same float sequence; that is
asserted, not assumed.

`rng.test.ts` pins the first five floats for seed 12345, captured from the
pre-extraction konbini-67 implementation rather than from this one -- a
golden test generated from the code under test only asserts that the code
matches itself. It earned its place immediately: the first constants I
wrote came from a truncated `toFixed(9)` probe and the test failed, which
is precisely the class of silent precision drift it exists to catch.

`hotline3d` is not a consumer and should not become one. Its generator uses
`%` and `Math.trunc` where mulberry32 uses `>>>`/`>>`; measured, it emits a
different sequence, so adopting this package would change its output.

29 tests, 100% branch/function/line/statement coverage, type-aware eslint,
and a committed dist/ with a CI freshness check -- the same bar web_ui
holds.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01U4Rj3s7K2qzrLNgJqqPhno
2026-08-15 15:42:41 +02:00
0dd4229432 unified-design-system: record the extraction audit, unstale the phase headings
Answers "did we extract every component we actually use?" now that Phases
1-3 have shipped.

The headline: the extractions hold up. No shipped component was a false
positive, and the reverse check -- were the donor copies actually deleted?
-- is clean except for billsplit's `_SectionHeader`, fixed separately in
testsAndMisc@54054e7. The one clear miss is `DeviceCodeDialog` x3, two
copies of which are byte-for-byte identical; it belongs in
`sync_settings_ui` rather than `design_system` because it depends on
GitHub auth types.

Also corrects two things the audit surfaced:

`nielsen-audit.md` described Phase 2 *and* Phase 3 as "not started". Both
shipped -- gatelock 0.5.0 and web_ui-v0.3.1. A planning doc that says "not
started" about finished work is worse than no doc, because the next
session reads it as a backlog and rebuilds what already exists. Both
headings now state what shipped, and the plan text is kept below as the
record of why. The Phase 2 note also spells out that screen-locker and
wake-alarm keeping local fan-out copies is deliberate (only two consumers
were bumped, to avoid restarting live systemd services) so it does not
read as a regression to be "fixed".

The audit also found a fourth shared package the phase briefs omit:
`sync_settings_ui`, consumed by four apps. It resolves most of the
`SettingsScreen` cluster, which the grid still listed as outstanding.

The remaining true duplicates (`_PrefsPersistence`, `Clock`, mulberry32
RNG) are non-UI utilities outside every package's remit, and are recorded
as such rather than as a reason to widen web_ui into a general utility
package.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01U4Rj3s7K2qzrLNgJqqPhno
2026-08-15 15:29:53 +02:00
ccef40e817 unified-design-system: check one palette across all four token sources
`tokens.md` freezes the palette as prose and three packages re-express it
as code -- `web_ui/src/tokens.css`, `design_system`'s `tokens.dart`, and
gatelock's `LockConfig`. Nothing compared them, so "one palette
everywhere" held only for as long as four hand-maintained copies happened
to agree. A hex edited in one stack drifted silently in the other three.

`palette_check.py` parses all four and runs two halves:

  * agreement -- every mapped token carries the same hex in every
    non-exempt stack
  * completeness -- every colour parsed from any source must be mapped or
    exempted with a stated reason

The second half is the load-bearing one. A pure agreement check passes
happily when someone adds a token to one stack and never touches the map,
which is the drift most likely to actually happen; an unmapped token is
therefore a failure, not a skip. Absences are declared per token per stack
with a reason (Tk has no light theme and no borders; the categorical ramp
is web-only) so "this stack doesn't have it" is on the record rather than
a silent pass.

It found one real gap on its first run: `tokens.css` sets `--surface-2` to
`paper-raised` in the light block -- the light theme collapses both
elevation steps onto one surface -- which `tokens.md` documented only in
prose and no map covered. Now an explicitly compared row, so if either
half of that pair moves, it fails.

Proven to fail, not just to pass: drift injected into each of the four
sources in turn, plus an unmapped token, was caught every time with the
offending stack named; re-proven after the 250-line-cap split into
`palette_map.py`. Runs from its own workflow with deliberately no `paths:`
filter -- drift is a disagreement *between* packages, so a path-scoped
trigger would skip exactly the edit that causes it. That was not
hypothetical: `ramp_check.py` was reachable only from the
`web_ui/**`-filtered workflow, so a ramp edit in `tokens.md` or
`design_system` never ran it. It moves here too.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01U4Rj3s7K2qzrLNgJqqPhno
2026-08-15 15:27:17 +02:00
32 changed files with 3136 additions and 3 deletions

33
.github/workflows/palette-drift.yml vendored Normal file
View File

@ -0,0 +1,33 @@
name: palette drift
# Deliberately NOT path-filtered. Every other workflow here scopes itself to
# one package, which is right for tests but wrong for this check: drift is a
# disagreement *between* packages, so editing design_system alone has to be
# able to fail it. A `paths:` filter would let exactly the edit that causes
# drift be the one that skips the check.
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
palette:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.13'
# Compares tokens.md (prose freeze), web_ui/src/tokens.css,
# design_system tokens.dart and gatelock's LockConfig. Fails on a hex
# that disagrees across stacks, and on a colour token that no stack's
# map accounts for.
- name: One palette across all four token sources
run: python3 unified-design-system/scripts/palette_check.py
- name: Categorical ramp still passes its constraints
run: python3 unified-design-system/scripts/ramp_check.py

48
.github/workflows/ts-core-tests.yml vendored Normal file
View File

@ -0,0 +1,48 @@
name: ts_core tests
on:
push:
branches: [main]
paths: ['ts_core/**']
pull_request:
branches: [main]
paths: ['ts_core/**']
jobs:
test:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ts_core
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 10
- uses: actions/setup-node@v4
with:
node-version: '24'
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Lint
run: pnpm lint
- name: Run tests with the 100% coverage gate
run: pnpm coverage
# The tag ships a prebuilt dist/ because Vite does not transpile
# dependencies. A committed dist/ that does not match src/ would ship
# stale code to every consumer with no error anywhere, so rebuild and
# fail if the result differs from what is checked in.
- name: dist/ is in sync with src/
run: |
pnpm build
if ! git diff --exit-code -- dist; then
echo "::error::dist/ is stale — run 'pnpm build' in ts_core and commit the result."
exit 1
fi

9
ts_core/.gitignore vendored Normal file
View File

@ -0,0 +1,9 @@
# dist/ is deliberately NOT ignored here: consumers pull this package as a git
# dependency and Vite does not transpile dependencies, so the build must be
# committed in the tag. See README.md. The repo root ignores `dist/` globally,
# hence the negation below.
node_modules/
coverage/
*.tsbuildinfo
!dist/
!dist/**

77
ts_core/README.md Normal file
View File

@ -0,0 +1,77 @@
# `@kuhyx/ts-core`
Framework-free TypeScript utilities shared across kuhy's games and web apps.
Deliberately **no React and no CSS** — those live in
[`@kuhyx/web-ui`](../web_ui). A game that needs a seeded RNG should not have
to install a UI package to get one; that coupling is what makes people vendor
their own copy instead, which is how these ended up duplicated four ways.
## Install
**pnpm only.** npm cannot install a subdirectory of a git repo — it ignores
the `&path:` key, looks for `package.json` at the repo root and fails with
ENOENT. Every consumer of this package must be on pnpm.
```jsonc
// package.json
"dependencies": {
"@kuhyx/ts-core": "github:kuhyx/utils#ts_core-v0.1.0&path:/ts_core"
}
```
## What it ships
| Export | Notes |
| --- | --- |
| `Clock`, `createRealClock`, `realClock`, `createManualClock`, `ManualClock` | Injectable time source, so real-time mechanics are driven by a scripted clock in tests instead of racing the wall clock. |
| `Rng`, `createRng`, `nextFloat`, `nextInt`, `nextChance`, `pick` | Seeded mulberry32, free-function style over mutable state. |
| `SeededRng`, `createSeededRng` | The same core, closure style. |
### Why the RNG has two interfaces
The repos that had grown their own copy did not agree on shape: konbini-67
used mutable state plus free functions (`nextFloat(rng)`); sims3-clone and
europe-county-map returned an object of closures (`rng.next()` / `rng.float()`).
Both ship here rather than being reconciled into one, because **the sequence
is the contract**. These repos have seeded tests and "same seed → identical
world" guarantees, so changing how many times a helper advances the generator
silently re-rolls every saved world. A shared package that did that would be
worse than the duplication it replaced.
The generator core is identical in both, so a given seed produces the same
float sequence whichever interface you use — asserted in the tests.
`int` is inclusive of both bounds, matching sims3-clone and europe-county-map.
konbini-67's local helper took a single *exclusive* max; it maps to
`nextInt(rng, 0, max - 1)`.
### The golden-sequence test
`rng.test.ts` pins the first five floats for seed `12345`, captured from the
**pre-extraction** konbini-67 implementation. If that test fails, the shared
core has stopped reproducing the sequence every consumer's saved seeds depend
on. Regenerating those constants to make a failure go away defeats the entire
point of the test — fix the code instead.
### `hotline3d` is deliberately not a consumer
`hotline3d/hotline-kernel/src/core/rng.ts` looks like the same generator but
uses `%` and `Math.trunc` where mulberry32 uses `>>>`/`>>`. Measured, it
produces a **different sequence** — so adopting this package would change its
generated output. Left alone on purpose.
## Commands
```bash
pnpm install
pnpm test # vitest
pnpm coverage # 100% branches/functions/lines/statements, enforced
pnpm lint # tsc --noEmit + eslint (type-aware, strict)
pnpm build # tsc -> dist/ (ESM + .d.ts), committed to the tag
```
`dist/` is committed, like `web_ui`'s: Vite does not transpile dependencies,
so a consumer would otherwise need per-repo bundler config. CI fails if the
committed `dist/` does not match a fresh build.

33
ts_core/dist/clock.d.ts vendored Normal file
View File

@ -0,0 +1,33 @@
/**
* Injectable time source.
*
* All elapsed-time reads flow through this interface instead of calling
* `performance.now()`/`Date.now()` directly (banned by lint everywhere except
* this file), so real-time mechanics can be driven by a scripted clock in
* tests rather than racing the wall clock.
*/
export interface Clock {
/** Current time in milliseconds. Not comparable across different clocks. */
now: () => number;
}
/** A {@link Clock} backed by the browser's monotonic performance timer. */
export declare const createRealClock: () => Clock;
/**
* The one real clock instance UI code should pass around.
*
* A single shared instance (rather than calling {@link createRealClock} per
* component) keeps its identity stable across renders, which matters for
* hooks that take it as an effect dependency -- a fresh object each render
* re-fires the effect every time.
*/
export declare const realClock: Clock;
/** A {@link Clock} test double whose time only moves when told to. */
export interface ManualClock extends Clock {
/** Moves the clock forward by the given number of milliseconds. */
advance: (deltaMs: number) => void;
/** Jumps the clock to an absolute time in milliseconds. */
set: (timeMs: number) => void;
}
/** Creates a {@link ManualClock} starting at `startMs` (default 0). */
export declare const createManualClock: (startMs?: number) => ManualClock;
//# sourceMappingURL=clock.d.ts.map

1
ts_core/dist/clock.d.ts.map vendored Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"clock.d.ts","sourceRoot":"","sources":["../src/clock.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,WAAW,KAAK;IACpB,4EAA4E;IAC5E,GAAG,EAAE,MAAM,MAAM,CAAA;CAClB;AAED,2EAA2E;AAC3E,eAAO,MAAM,eAAe,QAAO,KAEjC,CAAA;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,SAAS,EAAE,KAAyB,CAAA;AAEjD,sEAAsE;AACtE,MAAM,WAAW,WAAY,SAAQ,KAAK;IACxC,mEAAmE;IACnE,OAAO,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAA;IAElC,2DAA2D;IAC3D,GAAG,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAA;CAC9B;AAED,uEAAuE;AACvE,eAAO,MAAM,iBAAiB,GAAI,gBAAW,KAAG,WAW/C,CAAA"}

27
ts_core/dist/clock.js vendored Normal file
View File

@ -0,0 +1,27 @@
/** A {@link Clock} backed by the browser's monotonic performance timer. */
export const createRealClock = () => ({
now: () => performance.now(),
});
/**
* The one real clock instance UI code should pass around.
*
* A single shared instance (rather than calling {@link createRealClock} per
* component) keeps its identity stable across renders, which matters for
* hooks that take it as an effect dependency -- a fresh object each render
* re-fires the effect every time.
*/
export const realClock = createRealClock();
/** Creates a {@link ManualClock} starting at `startMs` (default 0). */
export const createManualClock = (startMs = 0) => {
let current = startMs;
return {
now: () => current,
advance: (deltaMs) => {
current += deltaMs;
},
set: (timeMs) => {
current = timeMs;
},
};
};
//# sourceMappingURL=clock.js.map

1
ts_core/dist/clock.js.map vendored Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"clock.js","sourceRoot":"","sources":["../src/clock.ts"],"names":[],"mappings":"AAaA,2EAA2E;AAC3E,MAAM,CAAC,MAAM,eAAe,GAAG,GAAU,EAAE,CAAC,CAAC;IAC3C,GAAG,EAAE,GAAW,EAAE,CAAC,WAAW,CAAC,GAAG,EAAE;CACrC,CAAC,CAAA;AAEF;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,SAAS,GAAU,eAAe,EAAE,CAAA;AAWjD,uEAAuE;AACvE,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,OAAO,GAAG,CAAC,EAAe,EAAE;IAC5D,IAAI,OAAO,GAAG,OAAO,CAAA;IACrB,OAAO;QACL,GAAG,EAAE,GAAW,EAAE,CAAC,OAAO;QAC1B,OAAO,EAAE,CAAC,OAAe,EAAQ,EAAE;YACjC,OAAO,IAAI,OAAO,CAAA;QACpB,CAAC;QACD,GAAG,EAAE,CAAC,MAAc,EAAQ,EAAE;YAC5B,OAAO,GAAG,MAAM,CAAA;QAClB,CAAC;KACF,CAAA;AACH,CAAC,CAAA"}

11
ts_core/dist/index.d.ts vendored Normal file
View File

@ -0,0 +1,11 @@
/**
* Framework-free TypeScript utilities shared across kuhy's games and web apps.
*
* Deliberately no React and no CSS: those live in `@kuhyx/web-ui`. A game
* that needs a seeded RNG should not have to install a UI package to get one
* -- that is the coupling that makes people vendor their own copy instead,
* which is how these ended up duplicated four ways to begin with.
*/
export { createManualClock, createRealClock, realClock, type Clock, type ManualClock, } from './clock.ts';
export { createRng, createSeededRng, nextChance, nextFloat, nextInt, pick, type Rng, type SeededRng, } from './rng.ts';
//# sourceMappingURL=index.d.ts.map

1
ts_core/dist/index.d.ts.map vendored Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EACL,iBAAiB,EACjB,eAAe,EACf,SAAS,EACT,KAAK,KAAK,EACV,KAAK,WAAW,GACjB,MAAM,YAAY,CAAA;AACnB,OAAO,EACL,SAAS,EACT,eAAe,EACf,UAAU,EACV,SAAS,EACT,OAAO,EACP,IAAI,EACJ,KAAK,GAAG,EACR,KAAK,SAAS,GACf,MAAM,UAAU,CAAA"}

11
ts_core/dist/index.js vendored Normal file
View File

@ -0,0 +1,11 @@
/**
* Framework-free TypeScript utilities shared across kuhy's games and web apps.
*
* Deliberately no React and no CSS: those live in `@kuhyx/web-ui`. A game
* that needs a seeded RNG should not have to install a UI package to get one
* -- that is the coupling that makes people vendor their own copy instead,
* which is how these ended up duplicated four ways to begin with.
*/
export { createManualClock, createRealClock, realClock, } from "./clock.js";
export { createRng, createSeededRng, nextChance, nextFloat, nextInt, pick, } from "./rng.js";
//# sourceMappingURL=index.js.map

1
ts_core/dist/index.js.map vendored Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EACL,iBAAiB,EACjB,eAAe,EACf,SAAS,GAGV,MAAM,YAAY,CAAA;AACnB,OAAO,EACL,SAAS,EACT,eAAe,EACf,UAAU,EACV,SAAS,EACT,OAAO,EACP,IAAI,GAGL,MAAM,UAAU,CAAA"}

74
ts_core/dist/rng.d.ts vendored Normal file
View File

@ -0,0 +1,74 @@
/**
* Seeded, deterministic PRNG (mulberry32).
*
* A given seed must always reproduce the exact same world, so consumers ban
* `Math.random` by lint outside this file.
*
* ## Why there are two interfaces
*
* The four repos that had grown their own copy did not agree on shape:
* konbini-67 used mutable state plus free functions (`nextFloat(rng)`),
* while sims3-clone and europe-county-map returned an object of closures
* (`rng.next()` / `rng.float()`). Both are exported here rather than
* reconciled into one, because the *sequence* is the contract: these repos
* have seeded tests and "same seed -> identical world" guarantees, and
* changing how many times a helper advances the generator silently changes
* every generated world. A shared package that quietly re-rolled saved
* worlds would be worse than the duplication it replaced.
*
* The generator core is identical in both, so a given seed produces the same
* float sequence whichever interface you use -- verified in the tests.
*
* Note `int` is inclusive of both bounds, matching sims3-clone and
* europe-county-map. konbini-67's local helper took a single *exclusive*
* max; it maps to `int(rng, 0, max - 1)`.
*/
/** Mutable state for a mulberry32 generator. */
export interface Rng {
/** Internal 32-bit state. Advanced by every draw. */
s: number;
}
/** Creates mutable generator state from a seed. */
export declare const createRng: (seed: number) => Rng;
/**
* Deterministic float in [0, 1). Advances the generator.
*
* This is the mulberry32 core; every other helper here is defined in terms
* of it, so they all consume exactly one step per call.
*/
export declare const nextFloat: (rng: Rng) => number;
/** Deterministic integer in [minInclusive, maxInclusive]. One step. */
export declare const nextInt: (rng: Rng, minInclusive: number, maxInclusive: number) => number;
/** True with the given probability in [0, 1]. One step. */
export declare const nextChance: (rng: Rng, probability: number) => boolean;
/**
* Deterministic pick from a non-empty tuple. One step.
*
* Iterates rather than indexing: under `noUncheckedIndexedAccess` an index
* read is `T | undefined`, and the undefined arm is unreachable here, so it
* could never be covered by a test.
*/
export declare const pick: <T>(rng: Rng, items: readonly [T, ...T[]]) => T;
/**
* The closure-style interface, for consumers that pass an `Rng` object
* around rather than threading mutable state through free functions.
*/
export interface SeededRng {
/** Uniform float in [0, 1). */
float: () => number;
/** Uniform integer in [minInclusive, maxInclusive]. */
int: (minInclusive: number, maxInclusive: number) => number;
/** Uniform element of a non-empty array. */
pick: <T>(items: readonly T[]) => T;
/** True with the given probability in [0, 1]. */
chance: (probability: number) => boolean;
}
/**
* Creates a {@link SeededRng} over the same mulberry32 core.
*
* `pick` throws on an empty array rather than returning `undefined`: the
* callers all treat their input as non-empty, and a silent `undefined`
* propagates into generated data where it is far harder to trace.
*/
export declare const createSeededRng: (seed: number) => SeededRng;
//# sourceMappingURL=rng.d.ts.map

1
ts_core/dist/rng.d.ts.map vendored Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"rng.d.ts","sourceRoot":"","sources":["../src/rng.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,gDAAgD;AAChD,MAAM,WAAW,GAAG;IAClB,qDAAqD;IACrD,CAAC,EAAE,MAAM,CAAA;CACV;AAED,mDAAmD;AACnD,eAAO,MAAM,SAAS,GAAI,MAAM,MAAM,KAAG,GAA0B,CAAA;AAEnE;;;;;GAKG;AACH,eAAO,MAAM,SAAS,GAAI,KAAK,GAAG,KAAG,MAMpC,CAAA;AAED,uEAAuE;AACvE,eAAO,MAAM,OAAO,GAClB,KAAK,GAAG,EACR,cAAc,MAAM,EACpB,cAAc,MAAM,KACnB,MAC4E,CAAA;AAE/E,2DAA2D;AAC3D,eAAO,MAAM,UAAU,GAAI,KAAK,GAAG,EAAE,aAAa,MAAM,KAAG,OAC7B,CAAA;AAE9B;;;;;;GAMG;AACH,eAAO,MAAM,IAAI,GAAI,CAAC,EAAE,KAAK,GAAG,EAAE,OAAO,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC,KAAG,CAW/D,CAAA;AAED;;;GAGG;AACH,MAAM,WAAW,SAAS;IACxB,+BAA+B;IAC/B,KAAK,EAAE,MAAM,MAAM,CAAA;IACnB,uDAAuD;IACvD,GAAG,EAAE,CAAC,YAAY,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,KAAK,MAAM,CAAA;IAC3D,4CAA4C;IAC5C,IAAI,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,SAAS,CAAC,EAAE,KAAK,CAAC,CAAA;IACnC,iDAAiD;IACjD,MAAM,EAAE,CAAC,WAAW,EAAE,MAAM,KAAK,OAAO,CAAA;CACzC;AAED;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,GAAI,MAAM,MAAM,KAAG,SAgB9C,CAAA"}

87
ts_core/dist/rng.js vendored Normal file
View File

@ -0,0 +1,87 @@
/**
* Seeded, deterministic PRNG (mulberry32).
*
* A given seed must always reproduce the exact same world, so consumers ban
* `Math.random` by lint outside this file.
*
* ## Why there are two interfaces
*
* The four repos that had grown their own copy did not agree on shape:
* konbini-67 used mutable state plus free functions (`nextFloat(rng)`),
* while sims3-clone and europe-county-map returned an object of closures
* (`rng.next()` / `rng.float()`). Both are exported here rather than
* reconciled into one, because the *sequence* is the contract: these repos
* have seeded tests and "same seed -> identical world" guarantees, and
* changing how many times a helper advances the generator silently changes
* every generated world. A shared package that quietly re-rolled saved
* worlds would be worse than the duplication it replaced.
*
* The generator core is identical in both, so a given seed produces the same
* float sequence whichever interface you use -- verified in the tests.
*
* Note `int` is inclusive of both bounds, matching sims3-clone and
* europe-county-map. konbini-67's local helper took a single *exclusive*
* max; it maps to `int(rng, 0, max - 1)`.
*/
/** Creates mutable generator state from a seed. */
export const createRng = (seed) => ({ s: seed >>> 0 });
/**
* Deterministic float in [0, 1). Advances the generator.
*
* This is the mulberry32 core; every other helper here is defined in terms
* of it, so they all consume exactly one step per call.
*/
export const nextFloat = (rng) => {
rng.s = (rng.s + 0x6d_2b_79_f5) >>> 0;
let t = rng.s;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4_294_967_296;
};
/** Deterministic integer in [minInclusive, maxInclusive]. One step. */
export const nextInt = (rng, minInclusive, maxInclusive) => minInclusive + Math.floor(nextFloat(rng) * (maxInclusive - minInclusive + 1));
/** True with the given probability in [0, 1]. One step. */
export const nextChance = (rng, probability) => nextFloat(rng) < probability;
/**
* Deterministic pick from a non-empty tuple. One step.
*
* Iterates rather than indexing: under `noUncheckedIndexedAccess` an index
* read is `T | undefined`, and the undefined arm is unreachable here, so it
* could never be covered by a test.
*/
export const pick = (rng, items) => {
const target = nextInt(rng, 0, items.length - 1);
let out = items[0];
let seen = 0;
for (const item of items) {
if (seen === target) {
out = item;
}
seen += 1;
}
return out;
};
/**
* Creates a {@link SeededRng} over the same mulberry32 core.
*
* `pick` throws on an empty array rather than returning `undefined`: the
* callers all treat their input as non-empty, and a silent `undefined`
* propagates into generated data where it is far harder to trace.
*/
export const createSeededRng = (seed) => {
const state = createRng(seed);
const float = () => nextFloat(state);
return {
float,
int: (minInclusive, maxInclusive) => nextInt(state, minInclusive, maxInclusive),
pick: (items) => {
const chosen = items[Math.floor(float() * items.length)];
if (chosen === undefined) {
throw new Error('pick() on an empty array');
}
return chosen;
},
chance: (probability) => float() < probability,
};
};
//# sourceMappingURL=rng.js.map

1
ts_core/dist/rng.js.map vendored Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"rng.js","sourceRoot":"","sources":["../src/rng.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAQH,mDAAmD;AACnD,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,IAAY,EAAO,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,IAAI,KAAK,CAAC,EAAE,CAAC,CAAA;AAEnE;;;;;GAKG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,GAAQ,EAAU,EAAE;IAC5C,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,aAAa,CAAC,KAAK,CAAC,CAAA;IACrC,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC,CAAA;IACb,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAA;IACpC,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,CAAA;IACzC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,aAAa,CAAA;AACjD,CAAC,CAAA;AAED,uEAAuE;AACvE,MAAM,CAAC,MAAM,OAAO,GAAG,CACrB,GAAQ,EACR,YAAoB,EACpB,YAAoB,EACZ,EAAE,CACV,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,YAAY,GAAG,YAAY,GAAG,CAAC,CAAC,CAAC,CAAA;AAE/E,2DAA2D;AAC3D,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,GAAQ,EAAE,WAAmB,EAAW,EAAE,CACnE,SAAS,CAAC,GAAG,CAAC,GAAG,WAAW,CAAA;AAE9B;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG,CAAI,GAAQ,EAAE,KAA2B,EAAK,EAAE;IAClE,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;IAChD,IAAI,GAAG,GAAM,KAAK,CAAC,CAAC,CAAC,CAAA;IACrB,IAAI,IAAI,GAAG,CAAC,CAAA;IACZ,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,IAAI,IAAI,KAAK,MAAM,EAAE,CAAC;YACpB,GAAG,GAAG,IAAI,CAAA;QACZ,CAAC;QACD,IAAI,IAAI,CAAC,CAAA;IACX,CAAC;IACD,OAAO,GAAG,CAAA;AACZ,CAAC,CAAA;AAiBD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,IAAY,EAAa,EAAE;IACzD,MAAM,KAAK,GAAG,SAAS,CAAC,IAAI,CAAC,CAAA;IAC7B,MAAM,KAAK,GAAG,GAAW,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,CAAA;IAC5C,OAAO;QACL,KAAK;QACL,GAAG,EAAE,CAAC,YAAoB,EAAE,YAAoB,EAAU,EAAE,CAC1D,OAAO,CAAC,KAAK,EAAE,YAAY,EAAE,YAAY,CAAC;QAC5C,IAAI,EAAE,CAAI,KAAmB,EAAK,EAAE;YAClC,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAA;YACxD,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;gBACzB,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAA;YAC7C,CAAC;YACD,OAAO,MAAM,CAAA;QACf,CAAC;QACD,MAAM,EAAE,CAAC,WAAmB,EAAW,EAAE,CAAC,KAAK,EAAE,GAAG,WAAW;KAChE,CAAA;AACH,CAAC,CAAA"}

28
ts_core/eslint.config.js Normal file
View File

@ -0,0 +1,28 @@
// Type-aware flat config, mirroring web_ui's so the two shared TS packages
// hold an identical bar.
import js from "@eslint/js";
import tseslint from "typescript-eslint";
export default tseslint.config(
{ ignores: ["dist", "coverage", "eslint.config.js"] },
{ linterOptions: { reportUnusedDisableDirectives: "error" } },
js.configs.recommended,
...tseslint.configs.strictTypeChecked,
...tseslint.configs.stylisticTypeChecked,
{
files: ["**/*.ts"],
languageOptions: {
parserOptions: {
project: ["./tsconfig.lint.json"],
tsconfigRootDir: import.meta.dirname,
},
},
rules: {
// Every exported symbol crosses a package boundary, so an explicit
// return type is documentation, not ceremony.
"@typescript-eslint/explicit-function-return-type": "error",
},
},
);

39
ts_core/package.json Normal file
View File

@ -0,0 +1,39 @@
{
"name": "@kuhyx/ts-core",
"version": "0.1.0",
"private": false,
"type": "module",
"description": "Framework-free TypeScript utilities shared across kuhy's games and web apps: an injectable Clock and a seeded mulberry32 RNG. Deliberately no React and no CSS -- see @kuhyx/web-ui for the UI layer.",
"license": "MIT",
"engines": {
"node": ">=24",
"pnpm": ">=10"
},
"packageManager": "pnpm@10.13.1",
"files": [
"dist"
],
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"scripts": {
"build": "tsc -b",
"lint": "tsc --noEmit -p tsconfig.lint.json && eslint .",
"test": "vitest run",
"coverage": "vitest run --coverage"
},
"devDependencies": {
"@eslint/js": "^10.0.1",
"@types/node": "^26.1.1",
"@vitest/coverage-v8": "^4.1.10",
"eslint": "^10.7.0",
"typescript": "^6.0.3",
"typescript-eslint": "^8.64.0",
"vitest": "^4.1.10"
}
}

1603
ts_core/pnpm-lock.yaml Normal file

File diff suppressed because it is too large Load Diff

80
ts_core/src/clock.test.ts Normal file
View File

@ -0,0 +1,80 @@
import { describe, expect, it, vi } from 'vitest'
import {
createManualClock,
createRealClock,
realClock,
type Clock,
} from './clock.ts'
describe('createRealClock', () => {
it('reads from performance.now()', () => {
const spy = vi.spyOn(performance, 'now').mockReturnValue(1234)
expect(createRealClock().now()).toBe(1234)
spy.mockRestore()
})
it('advances as real time passes', () => {
const clock = createRealClock()
const first = clock.now()
expect(clock.now()).toBeGreaterThanOrEqual(first)
})
})
describe('realClock', () => {
it('is a stable shared instance', () => {
// The whole reason this export exists: a fresh object per render would
// re-fire every effect that takes the clock as a dependency.
expect(realClock).toBe(realClock)
expect(realClock).not.toBe(createRealClock())
})
it('satisfies the Clock interface', () => {
const asClock: Clock = realClock
expect(typeof asClock.now()).toBe('number')
})
})
describe('createManualClock', () => {
it('starts at 0 by default', () => {
expect(createManualClock().now()).toBe(0)
})
it('starts at the given time', () => {
expect(createManualClock(500).now()).toBe(500)
})
it('only moves when told to', () => {
const clock = createManualClock(100)
expect(clock.now()).toBe(100)
expect(clock.now()).toBe(100)
clock.advance(50)
expect(clock.now()).toBe(150)
})
it('accumulates repeated advances', () => {
const clock = createManualClock()
clock.advance(10)
clock.advance(15)
expect(clock.now()).toBe(25)
})
it('moves backwards on a negative advance', () => {
const clock = createManualClock(100)
clock.advance(-40)
expect(clock.now()).toBe(60)
})
it('jumps to an absolute time with set()', () => {
const clock = createManualClock(100)
clock.set(7)
expect(clock.now()).toBe(7)
})
it('keeps two instances independent', () => {
const a = createManualClock()
const b = createManualClock()
a.advance(99)
expect(b.now()).toBe(0)
})
})

50
ts_core/src/clock.ts Normal file
View File

@ -0,0 +1,50 @@
/**
* Injectable time source.
*
* All elapsed-time reads flow through this interface instead of calling
* `performance.now()`/`Date.now()` directly (banned by lint everywhere except
* this file), so real-time mechanics can be driven by a scripted clock in
* tests rather than racing the wall clock.
*/
export interface Clock {
/** Current time in milliseconds. Not comparable across different clocks. */
now: () => number
}
/** A {@link Clock} backed by the browser's monotonic performance timer. */
export const createRealClock = (): Clock => ({
now: (): number => performance.now(),
})
/**
* The one real clock instance UI code should pass around.
*
* A single shared instance (rather than calling {@link createRealClock} per
* component) keeps its identity stable across renders, which matters for
* hooks that take it as an effect dependency -- a fresh object each render
* re-fires the effect every time.
*/
export const realClock: Clock = createRealClock()
/** A {@link Clock} test double whose time only moves when told to. */
export interface ManualClock extends Clock {
/** Moves the clock forward by the given number of milliseconds. */
advance: (deltaMs: number) => void
/** Jumps the clock to an absolute time in milliseconds. */
set: (timeMs: number) => void
}
/** Creates a {@link ManualClock} starting at `startMs` (default 0). */
export const createManualClock = (startMs = 0): ManualClock => {
let current = startMs
return {
now: (): number => current,
advance: (deltaMs: number): void => {
current += deltaMs
},
set: (timeMs: number): void => {
current = timeMs
},
}
}

25
ts_core/src/index.ts Normal file
View File

@ -0,0 +1,25 @@
/**
* Framework-free TypeScript utilities shared across kuhy's games and web apps.
*
* Deliberately no React and no CSS: those live in `@kuhyx/web-ui`. A game
* that needs a seeded RNG should not have to install a UI package to get one
* -- that is the coupling that makes people vendor their own copy instead,
* which is how these ended up duplicated four ways to begin with.
*/
export {
createManualClock,
createRealClock,
realClock,
type Clock,
type ManualClock,
} from './clock.ts'
export {
createRng,
createSeededRng,
nextChance,
nextFloat,
nextInt,
pick,
type Rng,
type SeededRng,
} from './rng.ts'

157
ts_core/src/rng.test.ts Normal file
View File

@ -0,0 +1,157 @@
import { describe, expect, it } from 'vitest'
import {
createRng,
createSeededRng,
nextChance,
nextFloat,
nextInt,
pick,
} from './rng.ts'
/**
* The golden sequence for seed 12345, captured from the *existing* konbini-67
* and sims3-clone implementations before extraction.
*
* This is the load-bearing test in the package. These repos guarantee "same
* seed -> identical world"; if the shared core ever stops reproducing this
* exact sequence, every saved seed in every consumer silently generates a
* different world. Regenerating these numbers to make a failure go away
* would defeat the entire point of the test.
*/
const GOLDEN_12345 = [
0.979_728_267_760_947_3, 0.306_752_264_499_664_3, 0.484_205_421_525_985,
0.817_934_412_509_203, 0.509_428_369_347_006_1,
]
describe('mulberry32 core', () => {
it('reproduces the pre-extraction sequence for a known seed', () => {
const rng = createRng(12_345)
const seen = Array.from({ length: 5 }, () => nextFloat(rng))
expect(seen).toEqual(GOLDEN_12345)
})
it('gives both interfaces the same sequence', () => {
const free = createRng(999)
const closure = createSeededRng(999)
const a = Array.from({ length: 8 }, () => nextFloat(free))
const b = Array.from({ length: 8 }, () => closure.float())
expect(a).toEqual(b)
})
it('is reproducible: same seed, same draws', () => {
const a = createRng(7)
const b = createRng(7)
expect(nextFloat(a)).toBe(nextFloat(b))
})
it('differs between seeds', () => {
expect(nextFloat(createRng(1))).not.toBe(nextFloat(createRng(2)))
})
it('stays within [0, 1) across many draws', () => {
const rng = createRng(42)
for (let i = 0; i < 1000; i += 1) {
const value = nextFloat(rng)
expect(value).toBeGreaterThanOrEqual(0)
expect(value).toBeLessThan(1)
}
})
it('handles a seed of 0 and a negative seed', () => {
expect(nextFloat(createRng(0))).toBeGreaterThanOrEqual(0)
expect(nextFloat(createRng(-1))).toBeGreaterThanOrEqual(0)
})
})
describe('nextInt', () => {
it('is inclusive of both bounds', () => {
const rng = createRng(3)
const seen = new Set<number>()
for (let i = 0; i < 500; i += 1) {
seen.add(nextInt(rng, 1, 3))
}
expect([...seen].sort()).toEqual([1, 2, 3])
})
it('returns the only value when min equals max', () => {
expect(nextInt(createRng(5), 4, 4)).toBe(4)
})
it('consumes exactly one step, like a raw float draw', () => {
const a = createRng(11)
const b = createRng(11)
nextInt(a, 0, 9)
nextFloat(b)
expect(nextFloat(a)).toBe(nextFloat(b))
})
})
describe('nextChance', () => {
it('is always true at probability 1 and never at 0', () => {
const rng = createRng(8)
expect(nextChance(rng, 1)).toBe(true)
expect(nextChance(rng, 0)).toBe(false)
})
it('lands near the requested probability over many draws', () => {
const rng = createRng(2024)
let hits = 0
for (let i = 0; i < 4000; i += 1) {
if (nextChance(rng, 0.25)) {
hits += 1
}
}
expect(hits / 4000).toBeGreaterThan(0.2)
expect(hits / 4000).toBeLessThan(0.3)
})
})
describe('pick', () => {
it('returns the single element of a one-item tuple', () => {
expect(pick(createRng(1), ['only'])).toBe('only')
})
it('eventually returns every element', () => {
const rng = createRng(4)
const seen = new Set<string>()
for (let i = 0; i < 500; i += 1) {
seen.add(pick(rng, ['a', 'b', 'c']))
}
expect([...seen].sort()).toEqual(['a', 'b', 'c'])
})
it('is deterministic for a given seed', () => {
const items: readonly [string, ...string[]] = ['w', 'x', 'y', 'z']
expect(pick(createRng(77), items)).toBe(pick(createRng(77), items))
})
})
describe('createSeededRng', () => {
it('picks uniformly and deterministically', () => {
expect(createSeededRng(5).pick(['p', 'q', 'r'])).toBe(
createSeededRng(5).pick(['p', 'q', 'r']),
)
})
it('throws rather than returning undefined on an empty array', () => {
expect(() => createSeededRng(1).pick([])).toThrow('pick() on an empty array')
})
it('matches europe-county-map: pick indexes by floor(float * len)', () => {
// The old local implementation indexed directly off the float rather than
// going through int(). Same step count, and algebraically identical for
// 0..len-1 -- pinned here so a future "simplification" to int() cannot
// silently shift every pick by an off-by-one.
const probe = createRng(31)
const expectedIndex = Math.floor(nextFloat(probe) * 5)
const items = ['a', 'b', 'c', 'd', 'e']
expect(createSeededRng(31).pick(items)).toBe(items[expectedIndex])
})
it('exposes int and chance over the same stream', () => {
const rng = createSeededRng(64)
expect(rng.int(10, 10)).toBe(10)
expect(typeof rng.chance(0.5)).toBe('boolean')
})
})

120
ts_core/src/rng.ts Normal file
View File

@ -0,0 +1,120 @@
/**
* Seeded, deterministic PRNG (mulberry32).
*
* A given seed must always reproduce the exact same world, so consumers ban
* `Math.random` by lint outside this file.
*
* ## Why there are two interfaces
*
* The four repos that had grown their own copy did not agree on shape:
* konbini-67 used mutable state plus free functions (`nextFloat(rng)`),
* while sims3-clone and europe-county-map returned an object of closures
* (`rng.next()` / `rng.float()`). Both are exported here rather than
* reconciled into one, because the *sequence* is the contract: these repos
* have seeded tests and "same seed -> identical world" guarantees, and
* changing how many times a helper advances the generator silently changes
* every generated world. A shared package that quietly re-rolled saved
* worlds would be worse than the duplication it replaced.
*
* The generator core is identical in both, so a given seed produces the same
* float sequence whichever interface you use -- verified in the tests.
*
* Note `int` is inclusive of both bounds, matching sims3-clone and
* europe-county-map. konbini-67's local helper took a single *exclusive*
* max; it maps to `int(rng, 0, max - 1)`.
*/
/** Mutable state for a mulberry32 generator. */
export interface Rng {
/** Internal 32-bit state. Advanced by every draw. */
s: number
}
/** Creates mutable generator state from a seed. */
export const createRng = (seed: number): Rng => ({ s: seed >>> 0 })
/**
* Deterministic float in [0, 1). Advances the generator.
*
* This is the mulberry32 core; every other helper here is defined in terms
* of it, so they all consume exactly one step per call.
*/
export const nextFloat = (rng: Rng): number => {
rng.s = (rng.s + 0x6d_2b_79_f5) >>> 0
let t = rng.s
t = Math.imul(t ^ (t >>> 15), t | 1)
t ^= t + Math.imul(t ^ (t >>> 7), t | 61)
return ((t ^ (t >>> 14)) >>> 0) / 4_294_967_296
}
/** Deterministic integer in [minInclusive, maxInclusive]. One step. */
export const nextInt = (
rng: Rng,
minInclusive: number,
maxInclusive: number,
): number =>
minInclusive + Math.floor(nextFloat(rng) * (maxInclusive - minInclusive + 1))
/** True with the given probability in [0, 1]. One step. */
export const nextChance = (rng: Rng, probability: number): boolean =>
nextFloat(rng) < probability
/**
* Deterministic pick from a non-empty tuple. One step.
*
* Iterates rather than indexing: under `noUncheckedIndexedAccess` an index
* read is `T | undefined`, and the undefined arm is unreachable here, so it
* could never be covered by a test.
*/
export const pick = <T>(rng: Rng, items: readonly [T, ...T[]]): T => {
const target = nextInt(rng, 0, items.length - 1)
let out: T = items[0]
let seen = 0
for (const item of items) {
if (seen === target) {
out = item
}
seen += 1
}
return out
}
/**
* The closure-style interface, for consumers that pass an `Rng` object
* around rather than threading mutable state through free functions.
*/
export interface SeededRng {
/** Uniform float in [0, 1). */
float: () => number
/** Uniform integer in [minInclusive, maxInclusive]. */
int: (minInclusive: number, maxInclusive: number) => number
/** Uniform element of a non-empty array. */
pick: <T>(items: readonly T[]) => T
/** True with the given probability in [0, 1]. */
chance: (probability: number) => boolean
}
/**
* Creates a {@link SeededRng} over the same mulberry32 core.
*
* `pick` throws on an empty array rather than returning `undefined`: the
* callers all treat their input as non-empty, and a silent `undefined`
* propagates into generated data where it is far harder to trace.
*/
export const createSeededRng = (seed: number): SeededRng => {
const state = createRng(seed)
const float = (): number => nextFloat(state)
return {
float,
int: (minInclusive: number, maxInclusive: number): number =>
nextInt(state, minInclusive, maxInclusive),
pick: <T>(items: readonly T[]): T => {
const chosen = items[Math.floor(float() * items.length)]
if (chosen === undefined) {
throw new Error('pick() on an empty array')
}
return chosen
},
chance: (probability: number): boolean => float() < probability,
}
}

42
ts_core/tsconfig.json Normal file
View File

@ -0,0 +1,42 @@
{
"compilerOptions": {
"target": "ES2023",
// DOM is here only for `performance.now()` in clock.ts -- the package
// ships no DOM *code*. Without it the build fails while the lint config
// (which adds @types/node) passes, so the two must not be trusted to
// agree by accident.
"lib": ["ESNext", "DOM"],
"module": "ESNext",
"moduleResolution": "bundler",
/* Strictness: everything on, matching the consumers so a file that
compiles here compiles there. */
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"exactOptionalPropertyTypes": true,
"useUnknownInCatchVariables": true,
"verbatimModuleSyntax": true,
"isolatedModules": true,
"erasableSyntaxOnly": true,
/* Ships a prebuilt dist/ (ESM + .d.ts) because consumers use Vite, which
does not transpile dependencies. */
"rewriteRelativeImportExtensions": true,
"allowImportingTsExtensions": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"outDir": "dist",
"rootDir": "src",
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src"],
"exclude": ["src/**/*.test.ts"]
}

View File

@ -0,0 +1,13 @@
{
// tsconfig.json excludes tests so they never land in the published dist/.
// Type-aware linting still needs them in a project, so this config widens
// the include for eslint and `tsc --noEmit` only. It emits nothing.
"extends": "./tsconfig.json",
"compilerOptions": {
"noEmit": true,
"rootDir": ".",
"types": ["vitest/globals", "node"]
},
"include": ["src", "*.config.ts"],
"exclude": []
}

19
ts_core/vitest.config.ts Normal file
View File

@ -0,0 +1,19 @@
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
coverage: {
exclude: ["src/index.ts", "src/**/*.d.ts"],
include: ["src/**/*.ts"],
provider: "v8",
// Same bar as design_system and web_ui: a shared layer several repos
// depend on does not get to be the least-tested code in the tree.
thresholds: { branches: 100, functions: 100, lines: 100, statements: 100 },
},
// No jsdom: this package is framework-free. `performance.now()` exists in
// plain node, which is the only browser-ish API it touches.
environment: "node",
globals: true,
include: ["src/**/*.test.ts"],
},
});

View File

@ -0,0 +1,124 @@
# Extraction audit — did we extract everything? (2026-08-15)
Answers "are all the components we actually use extracted?" across all three
shared packages, after Phases 1–3 shipped. Companion to `nielsen-audit.md`
(the grid) and `phase3-record.md` (Phase 3's verdicts).
**Short answer: yes, with one leftover — now fixed — and one clear miss.**
The three packages' extractions hold up. No shipped component turned out to be
a false positive, and the reverse check (did the donor copies actually get
deleted?) is clean except for one case.
## There is a fourth shared package
The phase briefs name three. There are four:
| Package | Stack | Consumers |
| --- | --- | --- |
| `design_system` | Dart/Flutter | 6 repos |
| `gatelock` | Python/Tk | 4 guard apps (2 bumped) |
| `web_ui` | TS/React | dufs-cloud, awesome-mcp-explorer |
| **`sync_settings_ui`** | Dart/Flutter | todo, home_inventory, diet-guard, wake-alarm |
`sync_settings_ui` (`backup_slot.dart`, `firebase_sync_controller.dart`,
`sync_settings_screen.dart`) changes the coverage answer for the whole sync
cluster — four of the five `SettingsScreen`s already delegate to it, so that
cluster is largely resolved rather than outstanding.
## Fixed in this pass
**billsplit's `_SectionHeader`** — a genuine leftover donor copy.
`billsplit/lib/ui/people_screen.dart` imported `design_system` while still
defining and using its own `_SectionHeader`. The shared `SectionHeader`'s
own doc names billsplit as a donor and sizes `defaultPadding` as
`fromLTRB(md, md, md, xs)` precisely to reproduce billsplit's `(16,16,16,4)`.
The union shipped; this donor was never deleted. Retired in
`testsAndMisc@54054e7`, verified geometrically identical.
(untools' `_SectionHeading`, the other named donor, *was* correctly deleted.)
## The one clear miss — `DeviceCodeDialog` ×3
The strongest finding. A modal showing a GitHub device-flow user code:
copies to clipboard, launches the verification URL, polls for the token,
pops with it or renders an error.
| Repo | Path | Lines |
| --- | --- | --- |
| todo | `lib/ui/device_code_dialog.dart` | 110 (public) |
| wake-alarm | `phone_app/lib/screens/github_mirror_screen.dart:157-240` | ~84 (private) |
| diet-guard | `app/lib/screens/github_mirror_screen.dart:398-481` | ~84 (private) |
**The wake-alarm and diet-guard copies are byte-for-byte identical** (`diff`
returns empty). todo's differs only by being public/keyed with doc comments.
Same domain types (`DeviceCodeResponse`, `GitHubDeviceAuth`), same props, same
`String? _error` state, same `initState`→`_poll` lifecycle.
**Belongs in `sync_settings_ui`, not `design_system`** — it depends on GitHub
auth types, so it is not a generic widget. Not yet extracted.
Note this is a *leaf* of the `GitHubMirrorScreen` ×4 cluster, which is
deliberately app-local (each copy carries the "connecting here also triggers
an actual sync" comment). The screen stays local; the dialog need not.
## True duplicates outside every package's remit
Real duplication, but none of it is UI, so no existing package owns it.
**These are not a reason to widen `web_ui`'s remit** — it is a UI package, and
the games were explicitly out of Phase 3 scope. A separate shared utility
module would be the vehicle, in a future phase.
- **`_PrefsPersistence` ×3** — todo, home_inventory, diet-guard, all
`implements LogPersistence`, all at the identical line number. Belongs to
`crdt_sync_dart`'s remit.
- **`Clock` / `createRealClock` / `createManualClock` ×2** — konbini-67 (59
lines) and iron-and-anvil (49). Same interface, same `realClock` shared
instance with the same rationale, doc comments near-verbatim.
- **mulberry32 seeded RNG ×4** — konbini-67, sims3-clone, europe-county-map,
hotline3d. Identical `0x6d2b79f5` core in all four, but the *interfaces*
genuinely differ (closure-returning-methods vs mutable-state-plus-free-
functions; `float`/`next` naming; `chance` in only one). Extractable, but
it is an API reconciliation, not a lift-and-shift.
## Correctly not extracted (name collisions)
Confirmed structurally disjoint — do not re-raise these:
- **`SettingsScreen` ×5** — four already delegate to `sync_settings_ui`;
dufs-cloud's is a WebDAV connection form (`_url`/`_user`/`_pass`), not sync
preferences.
- **`FilterSheet` ×3** — todo and home_inventory are Stateful, dufs-cloud is
Stateless. The split alone rules out a common component.
- **`FilterBar`** — settled in `phase3-record.md`.
- **`EntryTile` ×2** — kuhylog log entries vs dufs-cloud `DirEntry` rows.
- **`HomeShell` ×2**, **`HomeScreen` ×2** — unrelated shells/screens.
- **`formatDuration` ×2** — personal-website takes a `Duration` (CV date
range), dufs-cloud takes `ms: number` (media length). Disjoint signatures.
- **`ConfirmDialog`, `PromptDialog`, `StatBar`, `SummaryCards`** — single-use.
## Reviewed and judged benign
**leetcode-guard's five raw `tk.Button(` sites** (`_view.py:160,190,247`,
`_escape_flow.py:219`, `_study_strip.py:184`) persist despite the repo
importing `make_button`. All five sit inside the gate/lock-window path marked
do-not-touch, and each already applies the `fg=config.on_fill` contrast
discipline manually with an explanatory comment — the exact bug `make_button`'s
`variant` API exists to prevent. Worth migrating opportunistically; the
invariant is currently upheld.
## Not fully verified
`WrapperServer` ×4 (habit_stack 155 / home_inventory 217 / todo 223 /
diet-guard 254) was assessed from line counts and in-file architectural
comments, not a full body diff. If a hard verdict is wanted, that one still
needs opening.
## Phase 4 inputs (deferred, per the Q2 decision)
`personal-website`, `konbini-67`, `europe-county-map`, `sims3-clone`,
`steam-backlog-enforcer/web` and `iron-and-anvil` do not consume
`@kuhyx/web-ui`. All the `Clock`/RNG duplication lives entirely in this group.
`europe-county-map` and `iron-and-anvil` still define **zero** custom
properties, so they are token-adoption candidates independent of any component
work.

View File

@ -178,7 +178,15 @@ dep, as todo, untools, focus_owner, kuhylog, epopeja_karta and billsplit now
do. These six were never off-palette — they transcribe the right values by
hand — so this is duplication cleanup, not a visual fix.
### Phase 2 — Python/Tk (`~/utils/gatelock`), not started
### Phase 2 — Python/Tk (`~/utils/gatelock`), SHIPPED (gatelock 0.5.0)
> **Status: done.** `make_button`, `heading`/`row`, `ScrollableSurface` and
> `WidgetGroup` all ship in gatelock 0.5.0 (`c963cd8`, `d82107a`, `d25f288`);
> diet-guard and leetcode-guard are the migrated proof consumers.
> screen-locker (v0.4.0) and wake-alarm (v0.4.1) keep local fan-out copies
> **by design** — only two consumers were bumped to avoid restarting live
> systemd services. That asymmetry is not a regression; do not "fix" it.
> Everything below this line is the original plan, kept as the record of why.
Not a new package: gatelock already owns the tokens (`LockConfig`), the Tk
plumbing and 4 consumers. Add composite widgets, which is where its scope
@ -208,7 +216,12 @@ gain.
> path. Then launch each gate under `xvfb-run -s "-screen 0 1366x768x24"` and
> confirm buttons render and **`<Return>` activates** them.
### Phase 3 — TypeScript (`~/utils/web_ui`), not started
### Phase 3 — TypeScript (`~/utils/web_ui`), SHIPPED (web_ui-v0.3.1)
> **Status: done** — see `phase3-record.md` for what shipped and, importantly,
> why `FilterBar` was *not* extracted (a name collision, not a duplicate).
> Both proof consumers (dufs-cloud, awesome-mcp-explorer) are migrated.
> Everything below this line is the original plan, kept as the record of why.
Lowest confirmed duplication (2 components, 2 repos) and the only stack needing
a brand-new consumption mechanism, so it carries the worst effort-to-payoff

View File

@ -0,0 +1,231 @@
"""Verify one palette across all four token sources.
`tokens.md` freezes the palette as prose, and three packages re-express it as
code: `web_ui/src/tokens.css` (CSS custom properties), `design_system`
(Dart constants) and `gatelock` (a `LockConfig` dataclass). Nothing until now
compared them, so a hex edited in one stack drifted silently in the other
three -- the exact failure "one palette everywhere" is supposed to exclude.
The check is deliberately bidirectional. Comparing only the tokens named in
the map below would pass silently the moment someone adds a token to one
source and forgets the map, which is the drift most likely to actually
happen. So:
* every token parsed out of every source must be accounted for -- either
mapped, or named in `EXEMPT`; an unmapped token is a failure, not a skip
* every mapped token must resolve in every stack that is not exempt for it
Exemptions are declared per token per stack, with a reason. A silent skip is
the fail-open path this script exists to close.
Run: python3 unified-design-system/scripts/palette_check.py
Exit 0 = all sources agree and are complete; 1 = drift or an unmapped token.
"""
from __future__ import annotations
import re
import sys
from dataclasses import dataclass, field
from pathlib import Path
from palette_map import NON_COLOUR_CSS, PALETTE, STACKS
# Repo root: this file is <root>/unified-design-system/scripts/palette_check.py
ROOT = Path(__file__).resolve().parents[2]
TOKENS_MD = ROOT / "unified-design-system" / "tokens.md"
TOKENS_CSS = ROOT / "web_ui" / "src" / "tokens.css"
TOKENS_DART = ROOT / "design_system" / "lib" / "src" / "tokens.dart"
LOCKCONFIG_PY = ROOT / "gatelock" / "gatelock" / "_window.py"
@dataclass
class Result:
"""One check outcome."""
ok: bool
detail: str
@dataclass
class Sources:
"""Parsed colour tables, one per stack."""
md: dict[str, str] = field(default_factory=dict)
css: dict[str, str] = field(default_factory=dict)
dart: dict[str, str] = field(default_factory=dict)
tk: dict[str, str] = field(default_factory=dict)
def _norm(value: str) -> str:
"""Normalise a hex colour to lowercase `#rrggbb` for comparison."""
return "#" + value.lstrip("#").lower()
def parse_md(text: str) -> dict[str, str]:
"""Pull `| \\`token\\` | \\`#HEX\\` |` rows out of the frozen prose table."""
found: dict[str, str] = {}
row = re.compile(r"^\|\s*`([a-z0-9-]+)`\s*\|\s*`(#[0-9A-Fa-f]{6})`")
for line in text.splitlines():
m = row.match(line.strip())
if m:
found[m.group(1)] = _norm(m.group(2))
return found
def parse_css(text: str) -> dict[str, str]:
"""Parse `--token: #hex;`, tagging light-theme overrides with `@light`.
The light palette lives in a `prefers-color-scheme: light` block that
redefines the same custom-property names, so the names alone collide. The
`@light` suffix keeps both halves addressable from one map.
"""
found: dict[str, str] = {}
# Split off the light-theme block; everything before it is the dark root.
light_at = text.find("prefers-color-scheme: light")
dark_text = text[:light_at] if light_at != -1 else text
light_text = ""
if light_at != -1:
rest = text[light_at:]
# The light block ends at the next top-level @media.
nxt = rest.find("@media", 1)
light_text = rest[:nxt] if nxt != -1 else rest
decl = re.compile(r"(--[a-z0-9-]+)\s*:\s*(#[0-9A-Fa-f]{6})\s*;")
for name, value in decl.findall(dark_text):
found[name] = _norm(value)
for name, value in decl.findall(light_text):
found[f"{name}@light"] = _norm(value)
return found
def parse_dart(text: str) -> dict[str, str]:
"""Parse `static const Color name = Color(0xFFRRGGBB);` and aliases."""
found: dict[str, str] = {}
literal = re.compile(
r"static const Color (\w+) = Color\(0x[fF]{2}([0-9A-Fa-f]{6})\)"
)
for name, value in literal.findall(text):
found[name] = _norm(value)
# Aliases: `static const Color info = accent;`
alias = re.compile(r"static const Color (\w+) = (\w+);")
for name, target in alias.findall(text):
if target in found:
found[name] = found[target]
return found
def parse_tk(text: str) -> dict[str, str]:
"""Parse `field: str = "#RRGGBB"` defaults out of the LockConfig block."""
found: dict[str, str] = {}
start = text.find("class LockConfig")
if start == -1:
return found
body = text[start:]
decl = re.compile(r'^\s{4}(\w+):\s*str\s*=\s*"(#[0-9A-Fa-f]{6})"', re.MULTILINE)
for name, value in decl.findall(body):
found[name] = _norm(value)
return found
def load() -> Sources:
"""Read and parse all four token sources."""
return Sources(
md=parse_md(TOKENS_MD.read_text(encoding="utf-8")),
css=parse_css(TOKENS_CSS.read_text(encoding="utf-8")),
dart=parse_dart(TOKENS_DART.read_text(encoding="utf-8")),
tk=parse_tk(LOCKCONFIG_PY.read_text(encoding="utf-8")),
)
def check_agreement(src: Sources) -> list[Result]:
"""Every mapped token must carry the same hex in every non-exempt stack."""
results: list[Result] = []
for token in PALETTE:
seen: dict[str, str] = {}
missing: list[str] = []
for stack in STACKS:
name = getattr(token, stack)
if name is None:
continue # declared absence, justified in `why`
table: dict[str, str] = getattr(src, stack)
if name not in table:
missing.append(f"{stack}:{name}")
else:
seen[stack] = table[name]
if missing:
results.append(
Result(False, f"{token.canonical:<14} MISSING {', '.join(missing)}")
)
continue
values = set(seen.values())
if len(values) > 1:
detail = ", ".join(f"{s}={v}" for s, v in sorted(seen.items()))
results.append(Result(False, f"{token.canonical:<14} DRIFT {detail}"))
else:
only = next(iter(values))
results.append(
Result(
True,
f"{token.canonical:<14} {only} across {len(seen)} stack(s)",
)
)
return results
def check_completeness(src: Sources) -> list[Result]:
"""Every colour a source defines must be mapped -- no silent extras.
This is the half that catches "someone added a token and never touched
the map", which a pure agreement check passes happily.
"""
results: list[Result] = []
for stack in STACKS:
mapped = {getattr(t, stack) for t in PALETTE if getattr(t, stack) is not None}
table: dict[str, str] = getattr(src, stack)
extras = sorted(
name
for name in table
if name not in mapped and not NON_COLOUR_CSS.match(name)
)
if extras:
results.append(
Result(
False,
f"{stack:<4} defines unmapped colour(s): {', '.join(extras)} "
"-- add them to PALETTE (or declare an exemption)",
)
)
else:
results.append(
Result(
True, f"{stack:<4} all {len(table)} parsed token(s) accounted for"
)
)
return results
def main() -> int:
"""Run both halves of the check and report."""
src = load()
print("== agreement: one hex per token, across stacks ==")
results = check_agreement(src)
for r in results:
print(("PASS " if r.ok else "FAIL ") + r.detail)
print("\n== completeness: no unmapped colour in any source ==")
completeness = check_completeness(src)
for r in completeness:
print(("PASS " if r.ok else "FAIL ") + r.detail)
failures = sum(not r.ok for r in (*results, *completeness))
print(f"\n{failures} failing check(s)")
return 1 if failures else 0
if __name__ == "__main__":
sys.exit(main())

View File

@ -0,0 +1,162 @@
"""The canonical palette table: one row per colour, named in each stack.
Split out of `palette_check.py` to hold both files under the 250-line cap.
This module is pure data -- the parsing and comparison logic lives in the
checker. Adding a token to any stack means adding a row here; the checker's
completeness half fails on a token no row accounts for, which is what stops
this table from quietly falling behind the sources it describes.
"""
from __future__ import annotations
import re
from dataclasses import dataclass
STACKS = ("md", "css", "dart", "tk")
@dataclass(frozen=True)
class Token:
"""One canonical colour token and its name in each stack.
A `None` name means the token is intentionally absent from that stack;
`why` records the reason so an absence is a decision on the record rather
than an oversight.
"""
canonical: str
md: str | None
css: str | None
dart: str | None
tk: str | None
why: str = ""
# The canonical palette: one row per colour, named per stack.
#
# Deliberate absences (the `None`s) and their reasons:
# * paper/paper-raised/line-light/text-on-light/muted-on-light -- the light
# theme. CSS carries it in a `prefers-color-scheme` block (parsed
# separately below); Dart names them as constants; Tk has no light theme
# at all, because a locker surface is always dark.
# * cat-1..6 -- the categorical ramp shipped for web only. Dart and Tk have
# no chart/tag surfaces yet. Adding one means adding it here too.
# * on-scrim -- introduced by the web layer for translucent overlays; the
# other two stacks have no scrim surface.
PALETTE: tuple[Token, ...] = (
# --- neutrals, dark (all three code stacks) ---------------------------
Token("ink", "ink", "--bg", "ink", "bg"),
Token("ink-raised-1", "ink-raised-1", "--surface-1", "inkRaised1", "field_bg"),
Token(
"ink-raised-2",
"ink-raised-2",
"--surface-2",
"inkRaised2",
None,
why="Tk gates use a single raised step (field_bg); no second elevation.",
),
Token(
"line-dark",
"line-dark",
"--border",
"lineDark",
None,
why="Tk gates draw no borders; elevation is a fill step.",
),
Token("text-on-dark", "text-on-dark", "--text", "textOnDark", "fg"),
Token("muted-on-dark", "muted-on-dark", "--text-muted", "mutedOnDark", "muted"),
# --- neutrals, light --------------------------------------------------
Token(
"paper",
"paper",
"--bg@light",
"paper",
None,
why="Tk: locker surfaces are always dark; there is no light theme.",
),
Token(
"paper-raised",
"paper-raised",
"--surface-1@light",
"paperRaised",
None,
why="Tk: no light theme.",
),
# The light theme collapses both dark elevation steps onto one raised
# surface: `--surface-2` is redefined to `paper-raised` rather than
# getting a value of its own. Mapped explicitly (not exempted) so it is
# still compared -- if either half of the pair moves, this row fails.
Token(
"paper-raised-2",
"paper-raised",
"--surface-2@light",
"paperRaised",
None,
why="Light theme has one raised step; Tk has no light theme.",
),
Token(
"line-light",
"line-light",
"--border@light",
"lineLight",
None,
why="Tk: no light theme, and no borders.",
),
Token(
"text-on-light",
"text-on-light",
"--text@light",
"textOnLight",
None,
why="Tk: no light theme.",
),
Token(
"muted-on-light",
"muted-on-light",
"--text-muted@light",
"mutedOnLight",
None,
why="Tk: no light theme.",
),
# --- accent + semantic roles (all three) -------------------------------
Token("accent", "accent", "--accent", "accent", "accent"),
Token(
"info",
"info",
"--info",
"info",
None,
why="Tk: no informational role distinct from accent in the gates.",
),
Token("success", "success", "--success", "success", "success"),
Token("warning", "warning", "--warning", "warning", "warning"),
Token("danger", "danger", "--danger", "danger", "danger"),
Token("on-fill", "on-fill", "--on-fill", "onFill", "on_fill"),
Token("focus-ring", "focus-ring", "--focus-ring", None, "focus_ring"),
Token(
"on-scrim",
None,
"--on-scrim",
None,
None,
why="Web-only: no scrim/overlay surface in the Flutter or Tk layers.",
),
# --- categorical ramp (web only, for now) ------------------------------
*(
Token(
f"cat-{i}",
f"cat-{i}",
f"--cat-{i}",
None,
None,
why="Ramp shipped for web only; no chart/tag surface in Dart or Tk yet.",
)
for i in range(1, 7)
),
)
# Tokens a source legitimately defines that are not colours in the palette --
# structural/typographic values checked by the scale check, not the hex check.
NON_COLOUR_CSS = re.compile(
r"^--(radius|space|text|tracking|font|measure|shadow)-|^--(font-sans|font-mono|measure)$"
)

View File

@ -3,7 +3,18 @@
Concrete instantiation of `safe-design-rules`' 28 rules. One identical
palette across every repo/stack (Flutter, web, Python/Tkinter) — not just
shared rules, the same hex values everywhere, including Flutter launcher-icon
glyphs (`app-icon` skill). Anchored on the shared icon charcoal `#211D1B` so
glyphs (`app-icon` skill).
> **This table is checked, not trusted.**
> `scripts/palette_check.py` parses all four token sources — this file,
> `web_ui/src/tokens.css`, `design_system/lib/src/tokens.dart` and gatelock's
> `LockConfig` — and fails on any hex that disagrees across them, *including a
> hex edited only here*. It also fails on a colour token that no stack's map
> accounts for, so adding a token to one stack and forgetting the others is a
> CI failure rather than silent drift. Runs in `.github/workflows/palette-drift.yml`,
> deliberately with no `paths:` filter: drift is a disagreement *between*
> packages, so a path-scoped trigger would skip exactly the edit that causes it.
> Editing any value below means editing it in all four places. Anchored on the shared icon charcoal `#211D1B` so
the UI palette and the icon family cohere instead of adding a second
"near-black." Warm-tinted throughout (rule 10) — superseded from an earlier
cool-tinted draft; every neutral is a byte-reversed (RRGGBB→BBGGRR) mirror of