Merge branch 'feat/edge-glyph'
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
/**
|
||||
* Tests for src/core/cost-summary.ts — the graph cost pill's pure fold.
|
||||
* Same laws as the money channel everywhere: dollars INFERRED at read
|
||||
* time (core/prices.ts), unpriced models excluded-but-counted (render
|
||||
* "—", never a silent zero), tokens always sum.
|
||||
*/
|
||||
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it as test } from "node:test";
|
||||
import {
|
||||
graphCostSummary,
|
||||
type CostSummaryInput,
|
||||
type GraphCostSummary,
|
||||
} from "../src/core/cost-summary.ts";
|
||||
import type { CallUsage, ExtractionRunUsage, NamingRunUsage } from "../src/core/contracts.ts";
|
||||
import { PRICES, callCost } from "../src/core/prices.ts";
|
||||
|
||||
/** A model with full published pricing (from the generated registry). */
|
||||
const PRICED = "z-ai/glm-5.3-flash";
|
||||
assert.ok(PRICES[PRICED], "fixture model must exist in PRICES");
|
||||
|
||||
const step = (input: number, output: number): CallUsage => ({
|
||||
inputTokens: input,
|
||||
outputTokens: output,
|
||||
cacheReadTokens: 0,
|
||||
cacheWriteTokens: undefined,
|
||||
reasoningTokens: 0,
|
||||
});
|
||||
|
||||
const extractionRun = (
|
||||
startedAt: number,
|
||||
extractorModel: string,
|
||||
auditorModel: string,
|
||||
stats?: ExtractionRunUsage["stats"],
|
||||
): ExtractionRunUsage => ({
|
||||
startedAt,
|
||||
at: new Date(startedAt).toISOString(),
|
||||
durationMs: 10_000,
|
||||
stages: {
|
||||
extractor: { model: extractorModel, steps: [step(1000, 200)] },
|
||||
auditor: { model: auditorModel, steps: [step(2000, 100)] },
|
||||
},
|
||||
...(stats ? { stats } : {}),
|
||||
});
|
||||
|
||||
const namingRun = (startedAt: number, model: string, appended: number): NamingRunUsage => ({
|
||||
startedAt,
|
||||
at: new Date(startedAt).toISOString(),
|
||||
durationMs: 4_000,
|
||||
model,
|
||||
steps: [step(500, 50)],
|
||||
stats: { detected: 8, claimed: 6, named: 2, appended },
|
||||
});
|
||||
|
||||
describe("graphCostSummary (the cost pill's fold)", () => {
|
||||
test("folds all three channels: stage totals, tokens, newest-first runs", () => {
|
||||
const input: CostSummaryInput = {
|
||||
chats: [{ title: "Testing session", extractions: [extractionRun(100, PRICED, PRICED)] }],
|
||||
sources: [{ filePath: "/vault/modal.md", extractions: [extractionRun(200, PRICED, PRICED)] }],
|
||||
naming: [namingRun(300, PRICED, 2)],
|
||||
};
|
||||
const s = graphCostSummary(input);
|
||||
const extStep = callCost(step(1000, 200), PRICED)!;
|
||||
const audStep = callCost(step(2000, 100), PRICED)!;
|
||||
assert.equal(s.stages.extraction.dollars, extStep * 2);
|
||||
assert.equal(s.stages.audit.dollars, audStep * 2);
|
||||
assert.equal(s.stages.naming.dollars, callCost(step(500, 50), PRICED));
|
||||
assert.equal(s.stages.extraction.runs, 2);
|
||||
assert.equal(s.stages.naming.runs, 1);
|
||||
assert.equal(s.stages.extraction.tokens.inputTokens, 2000);
|
||||
assert.equal(s.stages.audit.tokens.outputTokens, 200);
|
||||
assert.equal(s.totalDollars, extStep * 2 + audStep * 2 + callCost(step(500, 50), PRICED)!);
|
||||
// All three runs, newest first, titles joined per front door.
|
||||
assert.deepEqual(
|
||||
s.runs.map((r) => r.title),
|
||||
["Cluster naming", "/vault/modal.md", "Testing session"],
|
||||
);
|
||||
assert.equal(s.runs[0]!.labels, 2);
|
||||
assert.equal(s.runs[0]!.kind, "naming");
|
||||
assert.equal(s.unpricedRuns, 0);
|
||||
});
|
||||
|
||||
test("unpriced models: excluded dollars, counted honestly, run null only when BOTH stages unpriced", () => {
|
||||
const s: GraphCostSummary = graphCostSummary({
|
||||
chats: [{ title: "partial", extractions: [extractionRun(100, PRICED, "unknown-model")] }],
|
||||
sources: [{ filePath: "/vault/x.md", extractions: [extractionRun(200, "unknown-model", "unknown-model")] }],
|
||||
naming: [],
|
||||
});
|
||||
assert.equal(s.stages.audit.unpricedRuns, 2);
|
||||
assert.equal(s.stages.audit.dollars, 0); // never a silent zero: unpricedRuns says it
|
||||
assert.equal(s.stages.extraction.unpricedRuns, 1);
|
||||
// The partial run still prices its extractor side; the fully
|
||||
// unpriced run renders "—". Newest first: 200 (both unpriced), 100.
|
||||
assert.equal(s.runs[0]!.dollars, null);
|
||||
assert.notEqual(s.runs[1]!.dollars, null);
|
||||
assert.equal(s.unpricedRuns, 1); // run-level: only the both-stages run
|
||||
});
|
||||
|
||||
test("recent runs cap: newest survive", () => {
|
||||
const extractions = [1, 2, 3, 4].map((n) => extractionRun(n * 100, PRICED, PRICED));
|
||||
const s = graphCostSummary({
|
||||
chats: [{ title: "chat", extractions }],
|
||||
sources: [],
|
||||
naming: [],
|
||||
recentCap: 2,
|
||||
});
|
||||
assert.equal(s.runs.length, 2);
|
||||
assert.deepEqual(s.runs.map((r) => r.startedAt), [400, 300]);
|
||||
// stage totals still count EVERY run, cap is presentational.
|
||||
assert.equal(s.stages.extraction.runs, 4);
|
||||
});
|
||||
|
||||
test("failed runs carry no merged stats; empty input folds to zero", () => {
|
||||
const s = graphCostSummary({
|
||||
chats: [{ title: "failed", extractions: [extractionRun(100, PRICED, PRICED)] }],
|
||||
sources: [],
|
||||
naming: [],
|
||||
});
|
||||
assert.equal(s.runs[0]!.merged, undefined);
|
||||
const empty = graphCostSummary({ chats: [], sources: [], naming: [] });
|
||||
assert.equal(empty.totalDollars, 0);
|
||||
assert.equal(empty.runs.length, 0);
|
||||
assert.equal(empty.stages.naming.runs, 0);
|
||||
assert.equal(empty.stages.extraction.tokens.inputTokens, undefined);
|
||||
});
|
||||
});
|
||||
@@ -33,6 +33,7 @@ import type {
|
||||
GraphEdge,
|
||||
GraphNode,
|
||||
LedgerEvent,
|
||||
NamingRunUsage,
|
||||
NodeMapping,
|
||||
NotesSource,
|
||||
SessionCheckpoint,
|
||||
@@ -70,6 +71,7 @@ function memoryLedgers() {
|
||||
const coverage: CoverageEntry[] = [];
|
||||
const sources = new Map<string, NotesSource>();
|
||||
const clusterLabels: ClusterLabeling[] = [];
|
||||
const namingRuns: NamingRunUsage[] = [];
|
||||
const positions = new Map<string, { x: number; y: number }>();
|
||||
const embeddings = {
|
||||
model: "",
|
||||
@@ -111,6 +113,11 @@ function memoryLedgers() {
|
||||
if (!clusterLabels.some((x) => x.id === l.id)) clusterLabels.push(l);
|
||||
},
|
||||
readClusterLabels: () => clusterLabels.map((l) => ({ ...l, core: [...l.core] })),
|
||||
// Cluster-naming run usage — mirrors the real store: dedupe per startedAt.
|
||||
appendNamingUsage: (entry: NamingRunUsage) => {
|
||||
if (!namingRuns.some((r) => r.startedAt === entry.startedAt)) namingRuns.push(entry);
|
||||
},
|
||||
readNamingRuns: () => namingRuns.map((r) => ({ ...r, steps: [...r.steps], stats: { ...r.stats } })),
|
||||
// The layout ledger — settleGraphLayout writes it after each merge.
|
||||
readPositions: () => Object.fromEntries([...positions].map(([id, p]) => [id, { ...p }])),
|
||||
writePositions: (next) => {
|
||||
@@ -799,6 +806,18 @@ describe("cluster naming (the post-extraction garnish)", () => {
|
||||
assert.equal(again.appended, 0);
|
||||
assert.ok(again.claimed >= 1);
|
||||
assert.equal(ledgers.readClusterLabels().length, 1, "no duplicate records");
|
||||
// Money channel (2026-09 cost metrics): the naming call billed —
|
||||
// ONE run record with the faux model, deterministic > 0 usage, and
|
||||
// the refresh stats. The second refresh named nothing (no model
|
||||
// call — early return), so no second record.
|
||||
const namingRuns = ledgers.readNamingRuns();
|
||||
assert.equal(namingRuns.length, 1, "one naming run billed");
|
||||
assert.equal(namingRuns[0]!.model, labeler.model.id);
|
||||
assert.ok(namingRuns[0]!.steps.length >= 1, "at least one billed step");
|
||||
assert.ok((namingRuns[0]!.steps[0]!.inputTokens ?? 0) > 0, "usage estimated > 0");
|
||||
assert.equal(namingRuns[0]!.stats.appended, 1);
|
||||
assert.equal(namingRuns[0]!.stats.named, 1);
|
||||
assert.ok(namingRuns[0]!.durationMs >= 0);
|
||||
});
|
||||
|
||||
it("naming failure never fails the run", async () => {
|
||||
|
||||
+24
-1
@@ -18,7 +18,7 @@ import assert from "node:assert/strict";
|
||||
import * as fs from "node:fs";
|
||||
import { after, describe, it } from "node:test";
|
||||
import type { UIMessage } from "ai";
|
||||
import type { AuditResult } from "../src/core/contracts.ts";
|
||||
import type { AuditResult, NamingRunUsage } from "../src/core/contracts.ts";
|
||||
import type { QuestionAnswerData } from "../src/core/question-core.ts";
|
||||
import { clusterContentId, getGraph, graphSnapshot, mergeFindings, type GraphDeps } from "../src/core/graph.ts";
|
||||
import { graphStore } from "../src/store/graph.ts";
|
||||
@@ -592,6 +592,29 @@ describe("graph ledger store", () => {
|
||||
assert.deepEqual(nodes[0]!.provenance, [{ chatId: "chat_c", messageId: "m1" }]);
|
||||
});
|
||||
|
||||
it("appendNamingUsage is idempotent per startedAt and strict-reads foreign shapes", () => {
|
||||
const run: NamingRunUsage = {
|
||||
startedAt: 1789800000000,
|
||||
at: "2026-09-17T00:00:00.000Z",
|
||||
durationMs: 1234,
|
||||
model: "z-ai/glm-5.3-flash",
|
||||
steps: [{ inputTokens: 10, outputTokens: 2, cacheReadTokens: 0, cacheWriteTokens: undefined, reasoningTokens: 0 }],
|
||||
stats: { detected: 4, claimed: 3, named: 1, appended: 1 },
|
||||
};
|
||||
graphStore.appendNamingUsage(run);
|
||||
graphStore.appendNamingUsage(run); // re-append: same startedAt — no-op
|
||||
// A torn append + a foreign shape (missing stats) — skipped by the guard.
|
||||
fs.appendFileSync(
|
||||
LEDGER_FILES.namingRuns,
|
||||
`{not json}\n${JSON.stringify({ startedAt: 1789800000001, at: "x", durationMs: 1, model: "m", steps: [] })}\n`,
|
||||
"utf-8",
|
||||
);
|
||||
const runs = graphStore.readNamingRuns().filter((r) => r.startedAt >= 1789800000000);
|
||||
assert.equal(runs.length, 1, "one record — idempotent append, corrupt/foreign lines skipped");
|
||||
assert.deepEqual(runs[0]!.stats, { detected: 4, claimed: 3, named: 1, appended: 1 });
|
||||
assert.equal(runs[0]!.steps.length, 1);
|
||||
});
|
||||
|
||||
it("returns RAW node lines — repeated ids (link-appends) are each their own line; getGraph merges", () => {
|
||||
const line = (msgId: string) =>
|
||||
JSON.stringify({
|
||||
|
||||
Reference in New Issue
Block a user