Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

refactor([issue-2800]): keep tally/render stages module-private

[._.]/ Adam Eivy8bf87db471c0
diff --git a/server/lib/README.md b/server/lib/README.md
index c42a1e90d9..93d7627012 100644
--- a/server/lib/README.md
+++ b/server/lib/README.md
@@ -202,7 +202,7 @@ The barrel `server/lib/index.js` is a machine-checkable enumeration of every pub
| `htmlToText.js` | Shared HTML → plain-text converter. `htmlToText(html, { extraEntities?, paragraphBreak?, collapseSpaces? })` — strips script/style/head/noscript blocks, converts `<br>`/block closes to newlines, strips remaining tags, decodes entities via `decodeXmlEntities`, collapses 3+ newlines, trims. Options preserve caller-specific output (Gmail: `paragraphBreak: '\n\n'` + `collapseSpaces`; SongBook import: defaults — space runs preserved for tab alignment). |
| `jsonExtract.js` | Pull JSON blocks out of LLM responses. |
| `taskParser.js` | Parse `TASKS.md` format. |
-| `taxonomyTally.js` | Generic taxonomy tally + top-N-line render engine shared by the two Layered Intelligence leaf taxonomies (`services/layeredIntelligenceRejections.js`, `layeredIntelligenceExecutionFailures.js`). `normalizeToken(value)` (lowercase + separator-collapse for label/category matching); `formatTaxonomyToken(token, labels)` (gloss-map render, nullish→'', unglossed passthrough); `tallyTaxonomy(records, {predicate, select, vocabulary, sentinel, field})` → three-bucket `{entries, unknown, unclassified, diagnosed, total}` with commonest-first + taxonomy-order tie-break; `renderTallyLine(summary, {field, glossFn, limit, gapWording})` → one prompt line; `createTaxonomyTally(config)` binds a full config into `{summarize, format}`. Pure leaf — imports nothing from the LI graph. |
+| `taxonomyTally.js` | Generic taxonomy tally + top-N-line render engine shared by the two Layered Intelligence leaf taxonomies (`services/layeredIntelligenceRejections.js`, `layeredIntelligenceExecutionFailures.js`). `createTaxonomyTally({predicate, select, field, vocabulary, sentinel, glossFn, gapWording})` is the single composed seam — it binds a taxonomy config into `{summarize, format}`, where `summarize(records)` yields the three-bucket `{entries, unknown, unclassified, diagnosed, total}` tally (commonest-first + taxonomy-order tie-break) and `format(records, limit)` renders one prompt line naming every non-zero gap. Also exports the two leaf utilities the classifiers use directly: `normalizeToken(value)` (lowercase + separator-collapse for label/category matching) and `formatTaxonomyToken(token, labels)` (gloss-map render, nullish→'', unglossed passthrough). Pure leaf — imports nothing from the LI graph. |
| `xmlEntities.js` | Shared dependency-free XML/HTML entity decoder. `decodeXmlEntities(str, extraEntities?)` — single-pass (double-decode-safe) decode of the five predefined named entities + decimal/hex numeric refs, with an optional caller-supplied extra-entity map (e.g. `{ nbsp: ' ', zwnj: '' }`). Unknown/out-of-range refs left untouched. Used by the Apple Health XML parser, Claude changelog feed, Pinterest RSS, generic feeds, and Gmail HTML-to-text. |
## Curated static data
diff --git a/server/lib/taxonomyTally.js b/server/lib/taxonomyTally.js
index a4835cc185..943b10b838 100644
--- a/server/lib/taxonomyTally.js
+++ b/server/lib/taxonomyTally.js
@@ -64,8 +64,11 @@ export function formatTaxonomyToken(token, labels) {
*
* `unknown`/`unclassified` stay OUT of `entries` so they can't crowd real diagnoses
* out of a caller's top-N list — they measure missing data, they are not findings.
+ *
+ * Internal stage of `createTaxonomyTally` (the module's single composed seam), kept
+ * unexported so the tally/render pipeline can evolve without a barrel-visible change.
*/
-export function tallyTaxonomy(records, { predicate, select, vocabulary, sentinel, field }) {
+function tallyTaxonomy(records, { predicate, select, vocabulary, sentinel, field }) {
const counts = new Map();
let unknown = 0;
let unclassified = 0;
@@ -102,8 +105,11 @@ export function tallyTaxonomy(records, { predicate, select, vocabulary, sentinel
* - `gapWording` — `{ unknown(n, total), unclassified(n, total) }`, each returning the
* gap clause for that bucket. Both taxonomies phrase `unclassified` the same but
* `unknown` differently ("no recorded reason" vs "no recognized cause").
+ *
+ * Internal stage of `createTaxonomyTally` (the module's single composed seam), kept
+ * unexported so the tally/render pipeline can evolve without a barrel-visible change.
*/
-export function renderTallyLine(summary, { field, glossFn, limit = 3, gapWording }) {
+function renderTallyLine(summary, { field, glossFn, limit = 3, gapWording }) {
const { entries, unknown, unclassified, total } = summary;
if (total === 0) return '';
const listed = entries
diff --git a/server/lib/taxonomyTally.test.js b/server/lib/taxonomyTally.test.js
index 3c3c449a59..39e1ec870a 100644
--- a/server/lib/taxonomyTally.test.js
+++ b/server/lib/taxonomyTally.test.js
@@ -2,18 +2,17 @@ import { describe, it, expect } from 'vitest';
import {
normalizeToken,
formatTaxonomyToken,
- tallyTaxonomy,
- renderTallyLine,
createTaxonomyTally
} from './taxonomyTally.js';
-// A tiny fixture taxonomy exercising the engine directly (independent of the two LI
-// consumers, whose own suites pin the domain-facing contracts).
+// A tiny fixture taxonomy exercising the engine through its single public seam
+// (`createTaxonomyTally`), independent of the two LI consumers whose own suites pin
+// the domain-facing contracts.
const VOCAB = ['alpha', 'beta', 'gamma'];
const SENTINEL = 'unknown-x';
const LABELS = { alpha: 'the alpha cause', beta: 'the beta cause', gamma: 'the gamma cause', [SENTINEL]: 'no known cause' };
const gloss = (t) => formatTaxonomyToken(t, LABELS);
-const config = {
+const engine = createTaxonomyTally({
predicate: (r) => r.kind === 'counted',
select: (r) => r.token,
field: 'token',
@@ -24,7 +23,7 @@ const config = {
unknown: (n, total) => `${n} of ${total} with no known cause`,
unclassified: (n, total) => `${n} of ${total} not yet classified`
}
-};
+});
const rec = (token) => ({ kind: 'counted', token });
describe('normalizeToken', () => {
@@ -54,11 +53,10 @@ describe('formatTaxonomyToken', () => {
});
});
-describe('tallyTaxonomy', () => {
+describe('createTaxonomyTally — summarize', () => {
it('counts only population records, commonest first, and keys entries by field', () => {
- const { entries, diagnosed, total } = tallyTaxonomy(
- [rec('beta'), rec('beta'), rec('alpha'), { kind: 'ignored', token: 'beta' }],
- config
+ const { entries, diagnosed, total } = engine.summarize(
+ [rec('beta'), rec('beta'), rec('alpha'), { kind: 'ignored', token: 'beta' }]
);
expect(entries).toEqual([{ token: 'beta', count: 2 }, { token: 'alpha', count: 1 }]);
expect(diagnosed).toBe(2 + 1);
@@ -66,16 +64,15 @@ describe('tallyTaxonomy', () => {
});
it('breaks count ties by vocabulary order, independent of record order', () => {
- const forward = tallyTaxonomy([rec('gamma'), rec('alpha')], config);
- const reverse = tallyTaxonomy([rec('alpha'), rec('gamma')], config);
+ const forward = engine.summarize([rec('gamma'), rec('alpha')]);
+ const reverse = engine.summarize([rec('alpha'), rec('gamma')]);
expect(forward.entries.map((e) => e.token)).toEqual(['alpha', 'gamma']);
expect(forward.entries).toEqual(reverse.entries);
});
it('separates the sentinel (unknown) from absent/unrecognized (unclassified)', () => {
- const { entries, unknown, unclassified, diagnosed, total } = tallyTaxonomy(
- [rec('alpha'), rec(SENTINEL), rec(null), rec('not-in-vocab')],
- config
+ const { entries, unknown, unclassified, diagnosed, total } = engine.summarize(
+ [rec('alpha'), rec(SENTINEL), rec(null), rec('not-in-vocab')]
);
expect(entries).toEqual([{ token: 'alpha', count: 1 }]);
expect(unknown).toBe(1);
@@ -85,46 +82,36 @@ describe('tallyTaxonomy', () => {
});
it('tolerates junk input and nullish records', () => {
- expect(tallyTaxonomy(null, config).total).toBe(0);
- expect(tallyTaxonomy([null, undefined, 'x'], config).total).toBe(0);
+ expect(engine.summarize(null).total).toBe(0);
+ expect(engine.summarize([null, undefined, 'x']).total).toBe(0);
+ // Default arg: called with no records at all.
+ expect(engine.summarize().total).toBe(0);
});
});
-describe('renderTallyLine', () => {
+describe('createTaxonomyTally — format', () => {
it('returns empty ONLY when the population is empty', () => {
- expect(renderTallyLine(tallyTaxonomy([], config), config)).toBe('');
+ expect(engine.format([])).toBe('');
+ expect(engine.format()).toBe('');
});
it('lists glossed diagnoses with counts, then names every non-zero gap', () => {
- const summary = tallyTaxonomy([rec('alpha'), rec('alpha'), rec(SENTINEL), rec(null)], config);
- const line = renderTallyLine(summary, config);
+ const line = engine.format([rec('alpha'), rec('alpha'), rec(SENTINEL), rec(null)]);
expect(line).toBe('the alpha cause (2) — 1 of 4 with no known cause — 1 of 4 not yet classified');
});
it('caps listed diagnoses at the limit but still counts the gap over everything', () => {
- const summary = tallyTaxonomy([rec('alpha'), rec('beta'), rec('gamma'), rec(SENTINEL)], config);
- const line = renderTallyLine(summary, { ...config, limit: 2 });
+ const line = engine.format([rec('alpha'), rec('beta'), rec('gamma'), rec(SENTINEL)], 2);
expect(line.split(' — ')[0].split(';').length).toBe(2);
expect(line).toContain('1 of 4 with no known cause');
});
it('never falls silent on an all-undiagnosed population', () => {
- const summary = tallyTaxonomy([rec(null), rec(null)], config);
- expect(renderTallyLine(summary, config)).toBe('2 of 2 not yet classified');
- });
-});
-
-describe('createTaxonomyTally', () => {
- it('binds a config into a { summarize, format } pair', () => {
- const { summarize, format } = createTaxonomyTally(config);
- expect(summarize([rec('beta'), rec('beta')]).entries).toEqual([{ token: 'beta', count: 2 }]);
- expect(format([rec('alpha')])).toBe('the alpha cause (1)');
- expect(format([])).toBe('');
+ expect(engine.format([rec(null), rec(null)])).toBe('2 of 2 not yet classified');
});
- it('threads the limit through format', () => {
- const { format } = createTaxonomyTally(config);
- const line = format([rec('alpha'), rec('beta'), rec('gamma')], 1);
- expect(line.split(';').length).toBe(1);
+ it('defaults the limit to 3 listed diagnoses', () => {
+ const line = engine.format([rec('alpha'), rec('beta'), rec('gamma')]);
+ expect(line.split(';').length).toBe(3);
});
});