Skip to content
Python

fix(observability): one model, one name when recordings are read (T3-4)

Devika Verma7703132a17cb
diff --git a/coworker/observability/calculator.py b/coworker/observability/calculator.py
index 674a7f09..452db233 100644
--- a/coworker/observability/calculator.py
+++ b/coworker/observability/calculator.py
@@ -10,6 +10,8 @@ from __future__ import annotations
from typing import Any, Iterable, Optional
+from .model_names import canonical_model
+
BUCKET_ORDER = ["waiting", "tools", "model", "auto_approver", "summary", "sandbox"]
@@ -60,7 +62,7 @@ def usage(recording: dict[str, Any], session_id: Optional[str] = None,
total += t
p = attrs.get("purpose") or "unattributed" # the format's word for a call with no purpose (V3-19)
by_purpose[p] = by_purpose.get(p, 0) + t
- key = attrs.get("model", "?") + ("" if p == "main" else f" ({p})")
+ key = canonical_model(attrs.get("model") or "?") + ("" if p == "main" else f" ({p})") # T3-4
by_model[key] = by_model.get(key, 0) + t
u = attrs["usage"]
if u.get("cache_read") is not None: # a call with no cache count stays out of the share
diff --git a/coworker/observability/model_names.py b/coworker/observability/model_names.py
new file mode 100644
index 00000000..24ca352a
--- /dev/null
+++ b/coworker/observability/model_names.py
@@ -0,0 +1,35 @@
+"""One name per model when recordings are read and grouped (T3-4).
+
+Sessions saved before the model-prefix fix (OPE-247, PR #837) recorded the same OpenAI model as
+both `openai:gpt-5.6-sol` and `gpt-5.6-sol`, so Usage showed it twice and split its tokens.
+Recordings are never rewritten; they are read through this rule.
+
+`canonical_model` is the same rule, line for line, as `coworker/providers/router.py`
+`canonical_model` on issue/ope-247-openai-model-prefix (and the GUI's `canonicalModel` in
+`surfaces/gui/src/modelNames.ts`), so the two agree when both land. Once #837 is merged this
+module can import it from there.
+"""
+
+from __future__ import annotations
+
+# The provider a model id with no `provider:` prefix runs on.
+DEFAULT_PROVIDER = "openai"
+
+
+def canonical_model(model: str) -> str:
+ """The one spelling of a model id: a prefix naming the default provider says nothing a
+ bare id doesn't (`openai:gpt-5.5` and `gpt-5.5` both run on OpenAI), so it is dropped.
+ Every other prefix (`anthropic:`, `ollama:` ...) is kept."""
+ model = (model or "").strip()
+ prefix = DEFAULT_PROVIDER + ":"
+ if model.startswith(prefix) and len(model) > len(prefix):
+ return model[len(prefix):]
+ return model
+
+
+# The same rule in SQL, for grouping the usage ledger by model (`u.model`).
+# (an exact, case-sensitive prefix test, as the Python rule; SQLite's LIKE ignores case)
+_PREFIX_LEN = len(DEFAULT_PROVIDER) + 1
+CANONICAL_MODEL_SQL = (f"CASE WHEN substr(u.model, 1, {_PREFIX_LEN}) = '{DEFAULT_PROVIDER}:' "
+ f"AND length(u.model) > {_PREFIX_LEN} THEN substr(u.model, {_PREFIX_LEN + 1}) "
+ "ELSE u.model END")
diff --git a/coworker/observability/read_api.py b/coworker/observability/read_api.py
index 911ab026..0e57f07d 100644
--- a/coworker/observability/read_api.py
+++ b/coworker/observability/read_api.py
@@ -18,6 +18,7 @@ from typing import Any, Callable, Optional
from . import calculator
from .capture import rebuild_request
+from .model_names import canonical_model
MESSAGE_CUT = 4096
@@ -168,7 +169,7 @@ def usage(recording: dict[str, Any], session_id: str) -> dict[str, Any]:
if s.get("kind") != "model" or not u.get("reported") or a.get("error") or a.get("purpose") == "proxy":
continue
p = a.get("purpose") or "unattributed"
- key = a.get("model", "?") + ("" if p == "main" else f" ({p})")
+ key = canonical_model(a.get("model") or "?") + ("" if p == "main" else f" ({p})") # T3-4
row = parts.setdefault(key, {"input": 0, "cache": 0, "output": 0})
for target in (row, totals):
target["input"] += calculator.input_total(u)
diff --git a/coworker/observability/rollups.py b/coworker/observability/rollups.py
index c13201d6..0f89e116 100644
--- a/coworker/observability/rollups.py
+++ b/coworker/observability/rollups.py
@@ -27,6 +27,8 @@ from __future__ import annotations
import json
from typing import Any, Iterable, Optional
+from .model_names import CANONICAL_MODEL_SQL
+
TOKEN_PARTS = ("input_fresh", "cache_read", "cache_write", "output")
@@ -198,7 +200,8 @@ GROUPS = {
"persona_id": "u.persona_id",
"machine": "u.machine",
"purpose": "COALESCE(u.purpose, 'unattributed')",
- "model": "COALESCE(u.model, 'unknown')",
+ # T3-4: one name per model, as `model_names.canonical_model` (openai: dropped on read)
+ "model": "COALESCE(" + CANONICAL_MODEL_SQL + ", 'unknown')",
"wake_reason": "COALESCE(u.wake_reason, 'unknown')",
"team_role": "u.team_role",
"team_id": "u.team_id",
diff --git a/coworker/observability/store.py b/coworker/observability/store.py
index aa9f85ba..f57f2304 100644
--- a/coworker/observability/store.py
+++ b/coworker/observability/store.py
@@ -29,6 +29,8 @@ from hashlib import sha256
from pathlib import Path
from typing import Any, Iterable, Iterator, Optional
+from .model_names import canonical_model
+
log = logging.getLogger(__name__)
FILENAME = "recordings.db"
@@ -883,11 +885,13 @@ class RecordingStore:
d["total"] += tot
if r["purpose"] is not None:
d["by_purpose"][r["purpose"]] = d["by_purpose"].get(r["purpose"], 0) + tot
- if r["model"] and r["model"] not in d["models"] and (r["calls"] or r["unreported"]):
- d["models"].append(r["model"])
- if (r["model"] and r["purpose"] in ("main", "helper") and r["model"] not in d["own_models"]
+ # T3-4: one name per model (openai: dropped), however the turn recorded it
+ model = canonical_model(r["model"]) if r["model"] else None
+ if model and model not in d["models"] and (r["calls"] or r["unreported"]):
+ d["models"].append(model)
+ if (model and r["purpose"] in ("main", "helper") and model not in d["own_models"]
and (r["calls"] or r["unreported"])):
- d["own_models"].append(r["model"])
+ d["own_models"].append(model)
for d in out.values():
d["at_least"] = d["unreported"] > 0
d["active_ms"] = None if d["duration_ms"] is None else d["duration_ms"] - (d["waiting_ms"] or 0)
diff --git a/coworker/observability/usage_api.py b/coworker/observability/usage_api.py
index eb57c922..4d6ac880 100644
--- a/coworker/observability/usage_api.py
+++ b/coworker/observability/usage_api.py
@@ -14,6 +14,7 @@ from typing import Any, Callable, Iterable, Optional
from . import scopes
from . import signals as _signals
+from .model_names import canonical_model
from .rollups import STEP_GROUPS, median
MIN_RUNS_TO_COMPARE = 10 # TD3-14
@@ -93,11 +94,6 @@ def heaviest(store: Any, *, start: int, end: int, limit: int = 20,
ASKED = ("unsure", "context_changed", "could_not_run")
-def canonical_model(model: str) -> str:
- """One name per model (T3-16, T3-4): OpenAI is the default provider, so `openai:gpt-5.6-sol`
- and `gpt-5.6-sol` are the same model. Only the `openai:` prefix is dropped, the same rule
- as `canonical_model` on issue/ope-247-openai-model-prefix; every other provider keeps its."""
- return model[len("openai:"):] if isinstance(model, str) and model.startswith("openai:") else model
def approver(store: Any, *, start: int, end: int, persona_id: Optional[str] = None) -> dict[str, Any]:
diff --git a/surfaces/gui/src/components/StepsPanel.tsx b/surfaces/gui/src/components/StepsPanel.tsx
index 046395d7..abf6b11f 100644
--- a/surfaces/gui/src/components/StepsPanel.tsx
+++ b/surfaces/gui/src/components/StepsPanel.tsx
@@ -22,6 +22,7 @@ import {
type TurnIssues,
} from "../api";
import { formatTokens } from "../usage";
+import { canonicalModel } from "../modelNames";
import { turnTexts, wakeKey } from "../obs3";
import { TurnList, TurnStrip } from "./TurnPicker";
@@ -107,7 +108,7 @@ export function stampLine(stamp: Record<string, any> | undefined, t: T): string
}
if (stamp.persona_fp) parts.push(`ⓕ${stamp.persona_fp}`);
const md = stamp.model;
- if (md?.id) parts.push([md.id, md.provider_title].filter(Boolean).join(" · "));
+ if (md?.id) parts.push([canonicalModel(md.id), md.provider_title].filter(Boolean).join(" · ")); // T3-4
return parts.join(" · ");
}
@@ -1053,7 +1054,7 @@ function Inspector({ d, turn, steps, sessionId, t }: { d: any; turn: RecordedTur
<>
<h3 className="steps-h3"><span className={`steps-kind k-${kind}`} /><Help text={kindHelp(d, t)}>{title}</Help><span className="steps-pill">{t("steps.insp.step", { n: d.seq ?? "—" })}</span></h3>
<div className="steps-meta">
- {[a.provider_title || a.provider ? t("steps.insp.via", { model: a.model, provider: a.provider_title || a.provider }) : a.model, t("steps.insp.started", { at: started }), why].filter(Boolean).join(" · ")}
+ {[a.provider_title || a.provider ? t("steps.insp.via", { model: canonicalModel(a.model), provider: a.provider_title || a.provider }) : canonicalModel(a.model), t("steps.insp.started", { at: started }), why].filter(Boolean).join(" · ")}
</div>
<Kv rows={[
[t("steps.detail.duration"), <>{formatDuration(d.duration_ms)}{a.ttft_ms != null && <span className="steps-faint"> ({t("steps.insp.first_reply", { t: formatDuration(a.ttft_ms) })})</span>}</>, H(t, "detail.duration")],
diff --git a/surfaces/gui/src/components/UsagePage.test.tsx b/surfaces/gui/src/components/UsagePage.test.tsx
index 84c5ac18..c5bdb86a 100644
--- a/surfaces/gui/src/components/UsagePage.test.tsx
+++ b/surfaces/gui/src/components/UsagePage.test.tsx
@@ -90,6 +90,14 @@ describe("UsageBody", () => {
expect(screen.getByTestId("usage-open-steps").textContent).toBe("Trace");
});
+ it("shows one model once in Heaviest turns, however an older turn recorded it (T3-4)", () => {
+ const heavy = [{ ...data().heavy[0], model_ids: ["openai:model-x", "model-x"] }];
+ render(<UsageBody data={data({ heavy })} period="7d" t={t} />);
+ const cell = screen.getByTestId("usage-heavy-model");
+ expect(cell.textContent).toBe("model-x");
+ expect(cell.getAttribute("title")).toBe("model-x");
+ });
+
it("lists machines, an offline one by its cached answer's time", () => {
render(<UsageBody data={data()} period="7d" t={t} />);
const m = screen.getByTestId("usage-machines").textContent || "";
diff --git a/surfaces/gui/src/components/UsagePage.tsx b/surfaces/gui/src/components/UsagePage.tsx
index f8ea0220..6629f4dd 100644
--- a/surfaces/gui/src/components/UsagePage.tsx
+++ b/surfaces/gui/src/components/UsagePage.tsx
@@ -19,6 +19,7 @@ import {
} from "../api";
import { formatTokens } from "../usage";
import { listPersonaName } from "../personaScope";
+import { canonicalModels } from "../modelNames";
import { formatDuration } from "./StepsPanel";
import {
approverPart,
@@ -296,8 +297,10 @@ export function UsageBody({ data, period, t, onOpenTurn, onOpenPersona, picked =
const title = h.session_deleted ? t("p3.usage.deleted_session") : (h.title || h.session_id);
const turn = h.seq != null ? t("p3.usage.turn_n", { n: h.seq }) : "";
const who = (h.persona_id && names[h.persona_id]) || h.persona_id || "—";
- const models = h.model_ids.join(", ") || "—";
- const shortModels = h.model_ids.map(shortModel).join(", ") || "—";
+ // T3-4: one name per model, however an older turn recorded it
+ const ids = canonicalModels(h.model_ids);
+ const models = ids.join(", ") || "—";
+ const shortModels = ids.map(shortModel).join(", ") || "—";
const issues = h.checks.map((c) => t(`steps.issues.title.${c}`, { defaultValue: c }));
return (
<tr key={h.turn_id}>
diff --git a/surfaces/gui/src/modelNames.canonical.test.ts b/surfaces/gui/src/modelNames.canonical.test.ts
new file mode 100644
index 00000000..61e0f531
--- /dev/null
+++ b/surfaces/gui/src/modelNames.canonical.test.ts
@@ -0,0 +1,25 @@
+// T3-4: one name per model when recordings are shown. Turns saved before the model-prefix fix
+// (#837) recorded one OpenAI model as both "openai:model-x" and "model-x"; the rule drops the
+// default provider's prefix only, as the server's `canonical_model`.
+import { describe, expect, it } from "vitest";
+import i18n from "i18next";
+import { canonicalModel, canonicalModels, sameModel } from "./modelNames";
+import { stampLine } from "./components/StepsPanel";
+
+const t = i18n.t.bind(i18n) as any;
+
+describe("canonicalModel", () => {
+ it("drops the openai: prefix only, and keeps every other prefix", () => {
+ expect(canonicalModel("openai:model-x")).toBe("model-x");
+ expect(canonicalModel(" model-x ")).toBe("model-x");
+ expect(canonicalModel("anthropic:model-y")).toBe("anthropic:model-y");
+ expect(canonicalModel("openai:")).toBe("openai:");
+ expect(sameModel("openai:model-x", "model-x")).toBe(true);
+ expect(canonicalModels(["openai:model-x", "model-x", "ollama:model-q:0.6b"])).toEqual(["model-x", "ollama:model-q:0.6b"]);
+ });
+
+ it("the Trace panel's stamp line names the model once", () => {
+ expect(stampLine({ model: { id: "openai:model-x" } }, t)).toContain("model-x");
+ expect(stampLine({ model: { id: "openai:model-x" } }, t)).not.toContain("openai:");
+ });
+});
diff --git a/surfaces/gui/src/modelNames.ts b/surfaces/gui/src/modelNames.ts
index 8a092aa6..0ec0cdf8 100644
--- a/surfaces/gui/src/modelNames.ts
+++ b/surfaces/gui/src/modelNames.ts
@@ -16,3 +16,25 @@ export function modelHover(id: string, labels?: Record<string, string>): string
const label = labels?.[id];
return label && label !== id ? `${label} (${id})` : id;
}
+
+// One spelling per model (same rule as the server, `coworker/providers/router.py`
+// `canonical_model`). A model id with no "provider:" prefix runs on the default
+// provider, so "openai:gpt-5.5" and "gpt-5.5" are the same model. The machine's model
+// list spells OpenAI models bare; coworker manifests often write "openai:…". Compare
+// and store the bare form. Every other prefix ("anthropic:", "ollama:" …) is kept.
+export const DEFAULT_PROVIDER = "openai";
+
+export function canonicalModel(id: string): string {
+ const m = (id || "").trim();
+ const prefix = `${DEFAULT_PROVIDER}:`;
+ return m.startsWith(prefix) && m.length > prefix.length ? m.slice(prefix.length) : m;
+}
+
+export function sameModel(a: string, b: string): boolean {
+ return canonicalModel(a) === canonicalModel(b);
+}
+
+/** The list in canonical form, first spelling of each model kept, order kept. */
+export function canonicalModels(ids: string[]): string[] {
+ return Array.from(new Set(ids.map(canonicalModel).filter(Boolean)));
+}
diff --git a/tests/test_observability_phase3_manual_fixes.py b/tests/test_observability_phase3_manual_fixes.py
index 2f559ada..52f6d0b5 100644
--- a/tests/test_observability_phase3_manual_fixes.py
+++ b/tests/test_observability_phase3_manual_fixes.py
@@ -213,3 +213,36 @@ def test_a_versions_models_are_its_own_work_only(tmp_path):
assert usage_api.canonical_model("openai:model-x") == "model-x"
assert usage_api.canonical_model("openrouter:openai/model-x") == "openrouter:openai/model-x"
store.close()
+
+
+# -- T3-4 -----------------------------------------------------------------------------------------
+def test_one_model_is_read_and_grouped_under_one_name(tmp_path):
+ """Turns saved before the model-prefix fix (#837) recorded one OpenAI model as both
+ `openai:model-x` and `model-x`. Read and grouped, it is one model; other prefixes stay."""
+ from coworker.observability import calculator, read_api, signals
+ from coworker.observability.model_names import canonical_model
+
+ assert canonical_model("openai:model-x") == "model-x"
+ assert canonical_model(" model-x ") == "model-x"
+ assert canonical_model("anthropic:model-y") == "anthropic:model-y"
+ assert canonical_model("openai:") == "openai:" # nothing after the prefix: kept as is
+ assert canonical_model("OpenAI:model-x") == "OpenAI:model-x" # the exact prefix only
+
+ store = RecordingStore(tmp_path / "recordings.db", background=False)
+ usage = {"reported": True, "input_fresh": 100, "cache_read": 0, "cache_write": 0, "output": 10}
+ for i, model in enumerate(["openai:model-x", "model-x", "anthropic:model-y"]):
+ tid = f"T{i}"
+ store.write_turn(_turn(tid, "s1", i + 1, RECENT + i * 1000))
+ store.write_step(_step(tid, "s1", 1, RECENT + i * 1000, "model", {"purpose": "main", "model": model, "usage": usage}))
+ store.write_issues(tid, {"signals_version": signals.version(), "warn": 0, "info": 0, "issues": [], "fine": []}, seen=True)
+ store.flush()
+ span = (RECENT - 1, RECENT + 60_000)
+ groups = {g["key"]["model"]: g["total"] for g in store.usage_rollup(["model"], *span)}
+ assert groups == {"model-x": 220, "anthropic:model-y": 110}
+ assert [t["models"] for t in store.usage_turns(*span)] == [["model-x"], ["model-x"], ["anthropic:model-y"]]
+ heavy = usage_api.heaviest(store, start=span[0], end=span[1])["turns"]
+ assert sorted(m for t in heavy for m in t["model_ids"]) == ["anthropic:model-y", "model-x", "model-x"]
+ rec = store.load_recording("s1")
+ assert calculator.usage(rec)["by_model"] == {"model-x": 220, "anthropic:model-y": 110}
+ assert set(read_api.usage(rec, "s1")["by_model_parts"]) == {"model-x", "anthropic:model-y"}
+ store.close()