Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(trajectory): pair only comparable observations

garrytane9ab750c593d
diff --git a/docs/architecture/key-files/commands-3.md b/docs/architecture/key-files/commands-3.md
index fd0b6588e5..33839d3f2b 100644
--- a/docs/architecture/key-files/commands-3.md
+++ b/docs/architecture/key-files/commands-3.md
@@ -21,7 +21,7 @@ Current behavior and load-bearing invariants; history belongs in Git and CHANGEL
- `src/commands/eval-suspected-contradictions.ts` + `src/core/eval-contradictions/{judge,runner,types,date-filter,cost-tracker,cache,severity-classify,cross-source,trends,calibration,judge-errors,auto-supersession,scoped-read}.ts` — `gbrain eval suspected-contradictions [run|trend|review]`. Probe samples top-K retrieval pairs per query (cross-slug + intra-page chunk-vs-take), date pre-filters (3-rule layered — same-paragraph-dual-date overrides separation rule), LLM judge (query-conditioned; UTF-8-safe truncation; confidence-floor double-enforcement; resolution_kind output drives paste-ready commands), persistent cache keyed on `(chunk_a_hash, chunk_b_hash, model_id, prompt_version, truncation_policy)` (prompt edits cleanly invalidate prior verdicts), Wilson 95% CI calibration on the headline percentage with `small_sample_note` when n<30, judge_errors as first-class typed counters (parse_fail/refusal/timeout/http_5xx/unknown — avoids bias from silent skip), trend writes to `eval_contradictions_runs`, source-tier breakdown reuses `DEFAULT_SOURCE_BOOSTS` prefix logic, deterministic sampling (combined_score DESC + lex tiebreaker for stable cache hit-rate). Hermetic via `judgeFn` + `searchFn` DI in the runner; never touches the real gateway in tests. Engine surface: `BrainEngine.listActiveTakesForPages` (batched), `writeContradictionsRun` + `loadContradictionsTrend`, `getContradictionCacheEntry` + `putContradictionCacheEntry` + `sweepContradictionCache`. Schema migrations v51 + v52. Pair members carry only content effective dates; a `fallback` date becomes null (`(date unknown)`). MCP op `find_contradictions` (read scope, NOT localOnly, NOT in subagent allowlist — user-initiated only; the stored report as written answers only trusted local callers without a user-selected source or trust floor, keyed on `ctx.localSourceImplicit` from `makeContext`; every other caller goes through `scoped-read.ts` `resolveScopedFindings`, which re-reads each side by chunk or take id under `pageReadFilter(live)`, current text projection, safe chunks, holder allow-list and floor, drops a finding unless both sides are visible and unchanged, rebuilds the resolution command from live rows and withholds a supersede of a higher-trust take). Doctor check surfaces high-severity findings with paste-ready resolution commands; synthesize phase pre-fetches the latest probe's top-5-by-severity findings and threads them into `buildSynthesisPrompt` as an informational block. Architecture doc: `docs/contradictions.md`.
-- `src/commands/eval-trajectory.ts` + `src/commands/founder-scorecard.ts` + `src/core/trajectory.ts` — temporal trajectory + founder scorecard. `gbrain eval trajectory <entity>` shows the chronological typed-claim history (mrr/arr/team_size/etc) with regressions auto-flagged inline; `gbrain founder scorecard <entity>` rolls up claim_accuracy / consistency / growth_trajectory / red_flags into one JSON. Pure-function math in `trajectory.ts`: `detectRegressions(points, threshold)` walks consecutive metric-value pairs per metric (10% drop default, env override `GBRAIN_TRAJECTORY_REGRESSION_THRESHOLD`); `computeDriftScore(points)` returns `1 - mean(cosine(emb[i], emb[i-1]))` over existing embeddings (null when <3 embedded points). Backed by `BrainEngine.findTrajectory(opts)` — both Postgres and PGLite, single SQL query that selects the NEWEST `limit` points (`ORDER BY valid_from DESC, id DESC`) and returns them chronologically, so a capped series keeps its latest value. Source-scoped via the `sourceId` scalar / `sourceIds` array dual pattern; visibility-filtered for remote callers. MCP op `find_trajectory` (read scope, NOT localOnly) registered after `find_experts`. Migration v67 adds optional typed-claim columns (`claim_metric`, `claim_value`, `claim_unit`, `claim_period`) + a partial index on `(entity_slug, claim_metric, valid_from) WHERE claim_metric IS NOT NULL`. Fence widens from 10 to 14 cells when any row has typed data; renderer stays at 10 cells when none do (no churn diff on existing fences). Metric labels normalize to lowercase snake_case via `normalizeMetricLabel` (15-entry seed map). The `consolidate` cycle phase does semantic upsert keyed on `(page_id, claim, since_date)` (so re-running the cycle after `extract_facts` clears `consolidated_at` cannot append duplicates via `MAX(row_num)+1`) and writes chronological `valid_until` on each cluster's older facts. The `extract_facts` cycle phase batch-embeds via `gateway.embed()` before insert AND threads `pages.effective_date` as the `pageEffectiveDate` fallback for `valid_from` (precedence: fence-row > pageEffectiveDate > now()). The contradiction probe MUST NOT write `valid_until` — grep guard at `test/eval-contradictions/no-valid-until-write.test.ts`. Haiku extraction lives in `src/core/facts/extract.ts` (not the `extract-facts.ts` cycle phase); its output cap is config `facts.extraction_max_tokens` (default 4000), a `stopReason: 'length'` response retries once at 2× the cap, and persistent truncation warns loudly on stderr instead of silently extracting zero facts. Mixed extractor arrays salvage valid candidates and warn with the dropped malformed count; all-malformed output still fails retryably, while an explicitly empty facts array remains a successful empty result. `pageEffectiveDate` is OPTIONAL because `fence-write.ts` callers have no Page object. Migration v89 adds a nullable `event_type TEXT` column on `facts` so the substrate carries event-shaped rows (`event_type='meeting'` / `'job_change'` / `'location_change'`) alongside metric rows. `TrajectoryPoint.event_type: string | null` projected by both engines. `TrajectoryOpts.kind?: 'metric' | 'event' | 'all'` filter (default `'all'`); `founder-scorecard` + `eval-trajectory` pass `kind: 'metric'` explicitly. Back-compat pinned by `test/regressions/v0_40_2_0-trajectory-backcompat.test.ts` (byte-identical `computeFounderScorecard` + `computeTrajectoryStats` with and without event rows); engine parity in `test/engine-parity-event-type.test.ts`.
+- `src/commands/eval-trajectory.ts` + `src/commands/founder-scorecard.ts` + `src/core/trajectory.ts` — temporal trajectory + founder scorecard. `gbrain eval trajectory <entity>` shows the chronological typed-claim history (mrr/arr/team_size/etc) with regressions auto-flagged inline; `gbrain founder scorecard <entity>` rolls up claim_accuracy / consistency / growth_trajectory / red_flags into one JSON. Pure-function math in `trajectory.ts`: `detectRegressions(points, threshold)` walks consecutive comparable pairs from `comparablePairs` (same metric, unit and period, strictly later valid_from; values that disagree at one valid_from break the chain) (10% drop default, env override `GBRAIN_TRAJECTORY_REGRESSION_THRESHOLD`); `computeDriftScore(points)` returns `1 - mean(cosine(emb[i], emb[i-1]))` over existing embeddings (null when <3 embedded points). Backed by `BrainEngine.findTrajectory(opts)` — both Postgres and PGLite, single SQL query that selects the NEWEST `limit` points (`ORDER BY valid_from DESC, id DESC`) and returns them chronologically, so a capped series keeps its latest value. Source-scoped via the `sourceId` scalar / `sourceIds` array dual pattern; visibility-filtered for remote callers. MCP op `find_trajectory` (read scope, NOT localOnly) registered after `find_experts`. Migration v67 adds optional typed-claim columns (`claim_metric`, `claim_value`, `claim_unit`, `claim_period`) + a partial index on `(entity_slug, claim_metric, valid_from) WHERE claim_metric IS NOT NULL`. Fence widens from 10 to 14 cells when any row has typed data; renderer stays at 10 cells when none do (no churn diff on existing fences). Metric labels normalize to lowercase snake_case via `normalizeMetricLabel` (15-entry seed map). The `consolidate` cycle phase does semantic upsert keyed on `(page_id, claim, since_date)` (so re-running the cycle after `extract_facts` clears `consolidated_at` cannot append duplicates via `MAX(row_num)+1`) and writes chronological `valid_until` on each cluster's older facts. The `extract_facts` cycle phase batch-embeds via `gateway.embed()` before insert AND threads `pages.effective_date` as the `pageEffectiveDate` fallback for `valid_from` (precedence: fence-row > pageEffectiveDate > now()). The contradiction probe MUST NOT write `valid_until` — grep guard at `test/eval-contradictions/no-valid-until-write.test.ts`. Haiku extraction lives in `src/core/facts/extract.ts` (not the `extract-facts.ts` cycle phase); its output cap is config `facts.extraction_max_tokens` (default 4000), a `stopReason: 'length'` response retries once at 2× the cap, and persistent truncation warns loudly on stderr instead of silently extracting zero facts. Mixed extractor arrays salvage valid candidates and warn with the dropped malformed count; all-malformed output still fails retryably, while an explicitly empty facts array remains a successful empty result. `pageEffectiveDate` is OPTIONAL because `fence-write.ts` callers have no Page object. Migration v89 adds a nullable `event_type TEXT` column on `facts` so the substrate carries event-shaped rows (`event_type='meeting'` / `'job_change'` / `'location_change'`) alongside metric rows. `TrajectoryPoint.event_type: string | null` projected by both engines. `TrajectoryOpts.kind?: 'metric' | 'event' | 'all'` filter (default `'all'`); `founder-scorecard` + `eval-trajectory` pass `kind: 'metric'` explicitly. Back-compat pinned by `test/regressions/v0_40_2_0-trajectory-backcompat.test.ts` (byte-identical `computeFounderScorecard` + `computeTrajectoryStats` with and without event rows); engine parity in `test/engine-parity-event-type.test.ts`.
- `src/commands/eval-whoknows.ts` — `gbrain eval whoknows <fixture.jsonl> [--json] [--skip-replay]`: two-layer eval gate. Layer 1 quality (hand-labeled fixture, top-3 hit rate ≥ 0.8). Layer 2 regression (`eval_candidates` replay set-Jaccard@3 ≥ 0.4). Sparseness fallback: < 20 replay-eligible rows → Layer 2 auto-skips with stderr warning. Stable JSON envelope with `schema_version: 1`; exit 0/1/2 for pass/fail/usage. `WhoknowsFn` callable abstraction makes the gates impl-agnostic; `runEvalWhoknows(engine: BrainEngine | null, args)` picks the impl at entry — thin-client mode (`isThinClient(cfg)`) routes per-query through `callRemoteTool(cfg, 'find_experts', {topic, limit})`, local mode calls `findExperts(engine, ...)` directly. cli.ts adds a thin-client bypass before `connectEngine` (dispatch shape under `src/commands/eval.ts`); the regression gate auto-skips in thin-client mode (no DB access to `eval_candidates`). Public exports `jaccardAtK`, `topKHit`, `readFixture`, `WhoknowsFn`, threshold constants pinned by `test/eval-whoknows.test.ts` (25 cases incl. null-engine signature contract).
diff --git a/docs/architecture/key-files/graph-and-facts.md b/docs/architecture/key-files/graph-and-facts.md
index d4b0b1db1a..9c2ec1b97f 100644
--- a/docs/architecture/key-files/graph-and-facts.md
+++ b/docs/architecture/key-files/graph-and-facts.md
@@ -107,4 +107,4 @@ rejection of serve-only flags before a brain opens.
- `src/core/timeline-dedup-repair.ts` — schema-drift self-heal for `idx_timeline_dedup`. A master-merge migration renumbering can leave a brain's version counter stamped past the index change while the index keeps an old shape — and every `addTimelineEntry` batch then fails its `ON CONFLICT` inference, silently breaking timeline writes brain-wide. The version counter can't detect this, so the repair is keyed off the actual index SHAPE. Canonical shape is `(page_id, date, md5(summary), source)` — md5-keyed (migration v138) because a raw long/incompressible summary would overflow the btree v4 row cap (~2704 bytes) and abort every timeline insert for that page; both engines' insert sites infer `ON CONFLICT` on the md5 tuple, so `EXPECTED_COLUMNS` MUST carry the md5 form (a raw-shape expectation would make the repair revert v138 on every migrate pass). `checkTimelineDedupIndex(engine)` returns `{tablePresent, indexPresent, columns, needsRepair}` (read-only; powers the `timeline_dedup_index` doctor check; the indexdef column parser is paren-depth-aware so `md5(summary)` — or any future expression column — parses as ONE column instead of flagging a correct index as drifted forever) and `repairTimelineDedupIndex(engine)` dedupes-then-rebuilds to the canonical shape (raw-summary grouping ⟺ md5 grouping modulo negligible collisions). `runMigrations` invokes the repair on every pass (including the no-pending early-return path); idempotent no-op when the index already matches. `gbrain apply-migrations --force-schema` triggers it on demand. Pinned by `test/timeline-dedup-repair.test.ts`.
-- `src/core/trajectory-format.ts` — shared `formatTrajectoryBlock(points, entitySlug, opts)` consumed by both `gbrain think` (production) and the LongMemEval harness (benchmark). Groups by `(metric ?? event_type)`, per-metric cap 20, total cap 100, knowledge_update intent annotates value-change rows with `(superseded prior)`. Emits a `<trajectory entity="...">` XML envelope — `INJECTION_PATTERNS` in `src/core/think/sanitize.ts` escapes `</trajectory>`, `<trajectory ...>` open tags, and attribute injection so adversarial fact text can't break out. Pinned by `test/trajectory-format.test.ts`.
+- `src/core/trajectory-format.ts` — shared `formatTrajectoryBlock(points, entitySlug, opts)` consumed by both `gbrain think` (production) and the LongMemEval harness (benchmark). Groups by `(metric ?? event_type)`, per-metric cap 20, total cap 100, knowledge_update intent annotates a row with `(superseded prior)` when its value differs from the prior comparable observation (`comparablePairs`). Emits a `<trajectory entity="...">` XML envelope — `INJECTION_PATTERNS` in `src/core/think/sanitize.ts` escapes `</trajectory>`, `<trajectory ...>` open tags, and attribute injection so adversarial fact text can't break out. Pinned by `test/trajectory-format.test.ts`.
diff --git a/src/core/trajectory-format.ts b/src/core/trajectory-format.ts
index 4bc09f010c..01ac2d3336 100644
--- a/src/core/trajectory-format.ts
+++ b/src/core/trajectory-format.ts
@@ -19,7 +19,8 @@
* - Per-metric cap (default 20) + total cap (default 100) bound the
* prompt budget. 100 points × ~75 tokens/point ≈ 7.5K tokens — fits
* comfortably alongside calibration + retrieval blocks.
- * - `knowledge_update` intent annotates value-change rows with
+ * - `knowledge_update` intent annotates value-change rows (against the
+ * prior comparable observation, `comparablePairs`) with
* `(superseded prior)` — the explicit signal Codex flagged was
* missing from default RRF-ordered retrieval. Other intents skip
* the annotation to keep the block compact.
@@ -31,6 +32,7 @@
import type { TrajectoryPoint } from './engine.ts';
import { INJECTION_PATTERNS } from './think/sanitize.ts';
+import { comparablePairs } from './trajectory.ts';
export type TrajectoryIntent = 'temporal' | 'knowledge_update' | 'other';
@@ -124,8 +126,9 @@ function formatGroup(
const lines: string[] = [];
let sanitizedCount = 0;
- let priorValue: number | null = null;
- const annotateSupersession = opts.intent === 'knowledge_update' && isMetric;
+ const superseding = opts.intent === 'knowledge_update' && isMetric
+ ? new Set(comparablePairs(points).filter(([older, newer]) => older.value !== newer.value).map(([, newer]) => newer))
+ : new Set<TrajectoryPoint>();
for (const p of points) {
const { text, matched } = sanitizeRowText(p.text);
@@ -135,18 +138,13 @@ function formatGroup(
const provenance = p.source_session ?? p.source_markdown_slug ?? null;
const provSuffix = provenance ? ` (source: ${provenance})` : '';
- let suffix = '';
- if (annotateSupersession && p.value !== null && priorValue !== null && p.value !== priorValue) {
- suffix = ' (superseded prior)';
- }
+ const suffix = superseding.has(p) ? ' (superseded prior)' : '';
lines.push(
isMetric
? ` as of ${date}: ${valueStr} — ${text}${suffix}${provSuffix}`
: ` as of ${date}: ${text}${suffix}${provSuffix}`,
);
-
- if (p.value !== null) priorValue = p.value;
}
const block = `<trajectory entity="${entitySlug}" ${attr}>\n${lines.join('\n')}\n</trajectory>`;
@@ -172,11 +170,11 @@ export function formatTrajectoryBlock(
const perMetricCap = opts.perMetricCap ?? DEFAULT_PER_METRIC_CAP;
const totalCap = opts.totalCap ?? DEFAULT_TOTAL_CAP;
- // Group by metric/event_type. Points with neither are silently dropped.
+ // Group by metric/event_type. Points with neither, or with no finite valid_from, are silently dropped.
const groups = new Map<string, TrajectoryPoint[]>();
for (const p of points) {
const key = groupKey(p);
- if (key === null) continue;
+ if (key === null || !Number.isFinite(p.valid_from.getTime())) continue;
const arr = groups.get(key);
if (arr) arr.push(p);
else groups.set(key, [p]);
diff --git a/src/core/trajectory.ts b/src/core/trajectory.ts
index 7e2af0fb02..5b197317c6 100644
--- a/src/core/trajectory.ts
+++ b/src/core/trajectory.ts
@@ -9,7 +9,8 @@
* Locked specs from the plan:
*
* - Regression detection (D-ENG-2): a regression fires for every
- * consecutive (metric, value) pair where the newer value is at least
+ * consecutive comparable pair (same metric, unit and period, strictly
+ * later; see comparablePairs) where the newer value is at least
* 10% lower than the prior value. Threshold is configurable via the
* env var `GBRAIN_TRAJECTORY_REGRESSION_THRESHOLD` (default 0.10).
* Only points with `claim_value !== null` participate.
@@ -78,52 +79,69 @@ function cosineSim(a: Float32Array, b: Float32Array): number {
}
/**
- * Detect chronological regressions in a sorted trajectory.
- *
- * Iterates per-metric (so trajectories that interleave mrr + arr + team_size
- * don't trip false regressions across metric boundaries). Within each metric,
- * walks consecutive value pairs; a pair fires when the newer value is lower
- * than the older value by at least the threshold. The relative delta uses
- * `abs(older)` as the denominator so negative-valued metrics (net income,
- * cash flow, etc.) do not invert improvement and regression.
+ * Consecutive comparable observations, oldest first: the same metric, unit
+ * and period (so a monthly figure never pairs with an annual one, nor USD
+ * with EUR), the newer strictly later. Values that disagree at one
+ * valid_from say nothing certain about that time, so that time is skipped
+ * and the chain bridges it; equal ones count once. A point with no valid
+ * date is left out.
*
* Pre-condition: caller passed points sorted by (valid_from ASC, fact_id ASC).
* The engine's `findTrajectory` enforces this. No re-sort here.
*/
+export function comparablePairs(points: TrajectoryPoint[]): Array<[TrajectoryPoint, TrajectoryPoint]> {
+ const series = new Map<string, TrajectoryPoint[]>();
+ for (const p of points) {
+ if (p.metric === null || p.value === null || !Number.isFinite(p.value) || !Number.isFinite(p.valid_from.getTime())) continue;
+ const key = JSON.stringify([p.metric, p.unit?.trim().toLowerCase() ?? null, p.period?.trim().toLowerCase() ?? null]);
+ const s = series.get(key);
+ if (s) s.push(p);
+ else series.set(key, [p]);
+ }
+ const pairs: Array<[TrajectoryPoint, TrajectoryPoint]> = [];
+ for (const s of series.values()) {
+ let prior: TrajectoryPoint | null = null;
+ for (let i = 0; i < s.length;) {
+ let j = i + 1;
+ while (j < s.length && s[j].valid_from.getTime() === s[i].valid_from.getTime()) j++;
+ if (s.slice(i, j).every(p => p.value === s[i].value)) {
+ if (prior) pairs.push([prior, s[j - 1]]);
+ prior = s[j - 1];
+ }
+ i = j;
+ }
+ }
+ return pairs;
+}
+
+/**
+ * Detect chronological regressions in a sorted trajectory: every
+ * comparablePairs pair whose newer value is lower than the older one by at
+ * least the threshold. The relative delta uses `abs(older)` as the
+ * denominator so negative-valued metrics (net income, cash flow, etc.) do
+ * not invert improvement and regression.
+ */
export function detectRegressions(
points: TrajectoryPoint[],
threshold: number = DEFAULT_REGRESSION_THRESHOLD,
): TrajectoryRegression[] {
const out: TrajectoryRegression[] = [];
- // Group by metric so each metric's regression detection is independent.
- const byMetric = new Map<string, TrajectoryPoint[]>();
- for (const p of points) {
- if (p.metric === null || p.value === null) continue;
- if (!Number.isFinite(p.value)) continue;
- if (!byMetric.has(p.metric)) byMetric.set(p.metric, []);
- byMetric.get(p.metric)!.push(p);
- }
-
- for (const [metric, series] of byMetric) {
- for (let i = 1; i < series.length; i++) {
- const older = series[i - 1];
- const newer = series[i];
- const oldVal = older.value!;
- const newVal = newer.value!;
- // Guard against division-by-zero: a metric starting at exactly 0
- // can't compute a relative delta. Skip.
- if (oldVal === 0) continue;
- const delta = (newVal - oldVal) / Math.abs(oldVal);
- if (delta <= -threshold) {
- out.push({
- metric,
- from_value: oldVal,
- from_date: toISODate(older.valid_from),
- to_value: newVal,
- to_date: toISODate(newer.valid_from),
- delta_pct: delta,
- });
- }
+ for (const [older, newer] of comparablePairs(points)) {
+ const oldVal = older.value!;
+ const newVal = newer.value!;
+ // Guard against division-by-zero: a metric starting at exactly 0
+ // can't compute a relative delta. Skip.
+ if (oldVal === 0) continue;
+ const delta = (newVal - oldVal) / Math.abs(oldVal);
+ if (delta <= -threshold) {
+ out.push({
+ metric: older.metric!,
+ from_value: oldVal,
+ from_date: toISODate(older.valid_from),
+ to_value: newVal,
+ to_date: toISODate(newer.valid_from),
+ delta_pct: delta,
+ });
}
}
return out;
diff --git a/test/trajectory-format.test.ts b/test/trajectory-format.test.ts
index 5b7fc46400..4a2035c2dd 100644
--- a/test/trajectory-format.test.ts
+++ b/test/trajectory-format.test.ts
@@ -171,6 +171,32 @@ describe('formatTrajectoryBlock — supersession annotation', () => {
expect(r.rendered).toContain('as of 2026-09-01: 3 — CTO at acme (superseded prior)');
});
+ test('#8547: a value in another unit or period, or at the same time, does not supersede', () => {
+ const points = [
+ mkMetricPoint({ id: 1, date: '2026-01-01', metric: 'mrr', value: 600000, period: 'annual', text: 'annual run rate' }),
+ mkMetricPoint({ id: 2, date: '2026-02-01', metric: 'mrr', value: 50000, text: 'monthly figure' }),
+ mkMetricPoint({ id: 3, date: '2026-02-01', metric: 'mrr', value: 52000, text: 'second report same day' }),
+ mkMetricPoint({ id: 4, date: '2026-03-01', metric: 'mrr', value: 48000, unit: 'EUR', text: 'euro figure' }),
+ mkMetricPoint({ id: 5, date: '2026-04-01', metric: 'mrr', value: 55000, text: 'later monthly figure' }),
+ mkMetricPoint({ id: 6, date: '2026-05-01', metric: 'mrr', value: 60000, text: 'latest monthly figure' }),
+ ];
+ const r = formatTrajectoryBlock(points, 'companies/acme-example', { intent: 'knowledge_update' });
+ for (const text of ['monthly figure', 'second report same day', 'euro figure', 'later monthly figure']) expect(r.rendered).not.toContain(`— ${text} (superseded prior)`);
+ expect(r.rendered).toContain('— latest monthly figure (superseded prior)');
+ });
+
+ test('#8547: a point with no finite valid_from is left out instead of dropping the whole block', () => {
+ const points = [
+ mkMetricPoint({ id: 1, date: '2026-01-01', metric: 'mrr', value: 50000, text: 'monthly figure' }),
+ mkMetricPoint({ id: 2, date: 'infinity', metric: 'mrr', value: 1, text: 'undated figure' }),
+ mkMetricPoint({ id: 3, date: '2026-02-01', metric: 'mrr', value: 55000, text: 'later monthly figure' }),
+ ];
+ const r = formatTrajectoryBlock(points, 'companies/acme-example', { intent: 'knowledge_update' });
+ expect(r.rendered).toContain('— later monthly figure (superseded prior)');
+ expect(r.rendered).not.toContain('undated figure');
+ expect(r.emittedPoints).toBe(2);
+ });
+
test('temporal intent does NOT annotate supersession', () => {
const points = [
mkMetricPoint({ id: 1, date: '2026-01-01', metric: 'role', value: 1, text: 'engineer', unit: null, period: null }),
diff --git a/test/trajectory.test.ts b/test/trajectory.test.ts
index 879e3b8bdb..0fba5327c8 100644
--- a/test/trajectory.test.ts
+++ b/test/trajectory.test.ts
@@ -26,7 +26,59 @@ function point(args: {
};
}
+function observed(id: number, value: number, date: string, unit: string | null, period: string | null): TrajectoryPoint {
+ return { ...point({ id, metric: 'mrr', value, date }), unit, period };
+}
+
describe('detectRegressions', () => {
+ test('#8547: a point without a valid date is left out, not looped on', () => {
+ const regs = detectRegressions([
+ observed(1, 50000, '2026-01-01', 'USD', 'monthly'),
+ { ...observed(2, 1, '2026-01-15', 'USD', 'monthly'), valid_from: new Date('infinity') },
+ observed(3, 40000, '2026-02-01', 'USD', 'monthly'),
+ ], DEFAULT_REGRESSION_THRESHOLD);
+ expect(regs).toHaveLength(1);
+ expect(regs[0]).toMatchObject({ from_value: 50000, to_value: 40000 });
+ });
+
+ test('#8547: compares only points with the same unit and period', () => {
+ expect(detectRegressions([
+ observed(1, 600000, '2026-01-01', 'USD', 'annual'),
+ observed(2, 50000, '2026-02-01', 'USD', 'monthly'),
+ ], DEFAULT_REGRESSION_THRESHOLD)).toEqual([]);
+ expect(detectRegressions([
+ observed(1, 50000, '2026-01-01', 'USD', 'monthly'),
+ observed(2, 40000, '2026-02-01', 'EUR', 'monthly'),
+ ], DEFAULT_REGRESSION_THRESHOLD)).toEqual([]);
+ const regs = detectRegressions([
+ observed(1, 50000, '2026-01-01', 'USD', 'monthly'),
+ observed(2, 600000, '2026-01-15', 'USD', 'annual'),
+ observed(3, 40000, '2026-02-01', 'USD', 'monthly'),
+ ], DEFAULT_REGRESSION_THRESHOLD);
+ expect(regs).toHaveLength(1);
+ expect(regs[0]).toMatchObject({ from_value: 50000, to_value: 40000, from_date: '2026-01-01', to_date: '2026-02-01' });
+ });
+
+ test('#8547: two values at the same valid_from are not a regression; the chain bridges that date', () => {
+ expect(detectRegressions([
+ observed(1, 50000, '2026-02-01', 'USD', 'monthly'),
+ observed(2, 30000, '2026-02-01', 'USD', 'monthly'),
+ ], DEFAULT_REGRESSION_THRESHOLD)).toEqual([]);
+ const bridged = detectRegressions([
+ observed(1, 90000, '2026-01-01', 'USD', 'monthly'),
+ observed(2, 50000, '2026-02-01', 'USD', 'monthly'),
+ observed(3, 30000, '2026-02-01', 'USD', 'monthly'),
+ observed(4, 20000, '2026-03-01', 'USD', 'monthly'),
+ ], DEFAULT_REGRESSION_THRESHOLD);
+ expect(bridged).toHaveLength(1);
+ expect(bridged[0]).toMatchObject({ from_value: 90000, to_value: 20000, from_date: '2026-01-01', to_date: '2026-03-01' });
+ expect(detectRegressions([
+ observed(1, 50000, '2026-01-01', 'USD', 'monthly'),
+ observed(2, 40000, '2026-02-01', 'USD', 'monthly'),
+ observed(3, 40000, '2026-02-01', 'USD', 'monthly'),
+ ], DEFAULT_REGRESSION_THRESHOLD)).toHaveLength(1);
+ });
+
test('keeps existing positive-valued drop behavior', () => {
const regs = detectRegressions([
point({ id: 1, metric: 'mrr', value: 200000, date: '2026-01-01' }),