Files
CatoandClaude Opus 5.5 d0e7fa68a7 Feedback inbox as one view; a Claude conversation that finds its log; follow-ups and voice chat in Loki (#1027)
* feat(feedback): one integrated inbox instead of stacked widgets

/feedback on a phone read as "a bunch of random elements": a lone bordered
Studio requests button, two rows of pills, a line restating the chip just
tapped, three bordered cards with grey headers, and rows whose status, context
and buttons sat in different places and came in three heights and two fills.

- Lens: Needs you · Under way · Shipped as one segmented control with counts.
  One list shows at a time; it opens on the first lens that holds something.
  An empty lens links to the lens that has work, never a dead end.
- Filters: one line. Project chips scroll; source is a compact select; the
  loop's numbers and the Studio requests link share the line under the chips.
- Rows: same anatomy for every phase. Context (status · project · page · who ·
  age), message, one "why" line, then an action bar of one height: the labelled
  decision leads, icon utilities (note, resolve, archive) sit at the trailing
  edge. Under-way Resolve becomes an icon; "What changed" becomes a link on
  the agent's line. The "Watching this run" strip is gone, because the Watch
  panel already holds Terminal and Chat.
- Dead .ui-inbox-fold rules removed; the fold now uses .ui-fb-fold.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TDTqcifctAar8o5hcbD3pE

* fix(terminal): the Claude conversation finds its log; a calmer phone chat

The /terminal conversation view sat on "No conversation from this session
yet" for sessions that were plainly running.

- Runner (0.8.38): claudeProjectSlug now matches Claude Code — every
  non-alphanumeric becomes "-", not just "/" and ".". Workspace and worktree
  names may contain "_", and for those the transcript stream, the usage
  collector and the auth canary all looked in a folder that never exists.
  Slugs past 200 chars (Claude truncates and hashes them) match on prefix.
- Runner: when it has the session but Claude has written no log, it says so
  once (an empty frame). The view tells apart "builder never answered",
  "Claude hasn't started here" and "nothing said yet", each with its own
  next step; the composer stays live in all of them.
- Answers in the Claude conversation get the same row as Loki's chat: Copy,
  Listen and — where the phone has a share sheet — Share (AnswerActions,
  shared with MessageTurn).
- Phone chrome: the Profile/Chat/Control/Terminal strip and its repeated
  project name are hidden on /terminal below md (the one-line header names
  the session); the agent chip drops in conversation view so the session name
  is readable; the Loki button says "Loki", not a second "Chat".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TDTqcifctAar8o5hcbD3pE

* feat(loki): suggested follow-ups and a hands-free voice conversation

Two things the reference chat apps have and Loki's chat did not.

- Suggested follow-ups: after an ordinary answer that ends the thread, up to
  three "↳ Make it longer" rows; a tap sends one as the next message.
  POST /api/loki/follow-ups asks the fast model once (feature
  "chat-follow-ups"); a slow or failed model answers [] and nothing shows.
  lib/loki/follow-ups.ts parses whatever comes back (numbering, bullets,
  quotes, a JSON array, a preamble) into clean tap targets — tested.
- Talk: a filled pill beside the composer's controls (only while the box is
  empty — the mic beside send still dictates). It opens a full-screen voice
  chat: listen until you pause, send what you said through the normal send
  path, read the answer aloud, listen again. The mic is closed while the
  answer is read, so the phone never answers itself; a failed turn goes back
  to listening rather than to silence; 20s of quiet pauses it. Endpointing
  (lib/loki/voice-turn.ts) works on waveform RMS against an adaptive noise
  floor and ends a turn only on a pause AFTER speech — tested. Transcription
  is the composer's own server leg (/api/control/transcribe).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TDTqcifctAar8o5hcbD3pE

* fix(loki): voice chat passes lint — no setState in an effect body, workspace under its line cap

The answer that arrives during a voice turn is read aloud from a deferred
callback rather than the effect body (react-hooks/set-state-in-effect), and
the voice screen takes the hook's state whole, so LokiWorkspace drops back
under its 510-line max-lines-per-function ratchet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TDTqcifctAar8o5hcbD3pE

* fix(loki): answer first, never a wall of projects; thumbs-down files feedback

From the owner's phone: "This all is making it unworkable. Flow should be
easy and enjoyable." A plain message ("let's keep improving this interface")
came back as "Which project should I run that on?" over seventeen two-column
project tiles, and the thread title ran across the back arrow.

- Loki always answers. When the message also reads like work, the answer
  carries one closed line under it — "Run this on a project…" (or "No
  project called X — run this on another…") — which opens the picker only
  when tapped. The blocking needs-project turn is gone; older threads that
  contain one still render it.
- The picker is small wrapping chips, not 44px tiles in a grid.
- Thread header: the title truncates instead of overflowing both edges (a
  centred flex child is as wide as its text, so `truncate` never engaged).
- Thumbs-down under Loki's answers: one optional line on what was wrong,
  then Send. POST /api/loki/answer-feedback files it into Loki's own project
  (FEEDBACK_WIDGET_TOKEN, where the dogfood widget files), with the question
  and answer attached so it reads on its own in /feedback, and the reporter
  recorded so it shows in their /my-feedback. Not on the Claude Code
  conversation, which is not Loki's answer. Says plainly when the token is
  not provisioned. Report text: lib/loki/answer-report.ts, tested.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TDTqcifctAar8o5hcbD3pE

* fix(feedback): scale widths, not arbitrary ones, in the row context line

max-w-48 / max-w-40 are the same 12rem / 10rem on Tailwind's scale; the
arbitrary values raised design-principles' one-off-layout ratchet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TDTqcifctAar8o5hcbD3pE

* fix(loki): answer report takes optional note/question, as the route sends them

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TDTqcifctAar8o5hcbD3pE

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-03 04:24:15 -07:00

118 lines
4.8 KiB
TypeScript

// Pure unit test: transcript usage collection + model pricing + display format.
// No DB, no server — auto-discovered by scripts/test-unit.ts.
import assert from "node:assert";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { claudeProjectSlug, collectClaudeUsage } from "../../src/lib/usage/claude-transcript-usage";
import { priceUsage, rateForModel } from "../../src/config/model-pricing";
import { formatRunUsage, formatTokens, formatCostUsd } from "../../src/lib/usage/format";
// --- claudeProjectSlug: dots AND slashes become dashes (worktree paths!) ---
assert.equal(claudeProjectSlug("/home/g/dev/app"), "-home-g-dev-app");
assert.equal(
claudeProjectSlug("/home/g/dev/app/.claude/worktrees/x"),
"-home-g-dev-app--claude-worktrees-x",
);
// Every non-alphanumeric becomes "-", as Claude Code does — "_" and "~" too.
// The old rule kept them and looked in folders that never exist.
assert.equal(claudeProjectSlug("/home/loki/dev/my_app"), "-home-loki-dev-my-app");
assert.equal(claudeProjectSlug("/w/solon~ab12cd34"), "-w-solon-ab12cd34");
// --- collectClaudeUsage over a fixture transcript ---
const home = fs.mkdtempSync(path.join(os.tmpdir(), "fc-usage-test-"));
const dir = "/home/g/dev/app/.claude/worktrees/x";
const projDir = path.join(home, ".claude", "projects", claudeProjectSlug(dir));
fs.mkdirSync(projDir, { recursive: true });
const T0 = Date.parse("2026-08-02T10:00:00Z");
const line = (offsetMs: number, msgId: string, model: string, usage: Record<string, number>) =>
JSON.stringify({
type: "assistant",
uuid: `u-${msgId}-${offsetMs}`,
timestamp: new Date(T0 + offsetMs).toISOString(),
message: { id: msgId, model, usage },
});
fs.writeFileSync(
path.join(projDir, "session-a.jsonl"),
[
// Before the window — excluded.
line(-60_000, "m0", "claude-opus-5-20260501", { input_tokens: 999, output_tokens: 999 }),
// In window.
line(10_000, "m1", "claude-opus-5-20260501", {
input_tokens: 1000,
output_tokens: 500,
cache_read_input_tokens: 200_000,
cache_creation_input_tokens: 4000,
}),
// Same message id repeated (multi-block turn) — must dedupe, not double.
line(10_500, "m1", "claude-opus-5-20260501", {
input_tokens: 1000,
output_tokens: 500,
cache_read_input_tokens: 200_000,
cache_creation_input_tokens: 4000,
}),
// Second model in window, unknown to pricing.
line(20_000, "m2", "future-model-x", { input_tokens: 100, output_tokens: 50 }),
// Non-assistant line with usage-looking content — ignored.
JSON.stringify({
type: "user",
timestamp: new Date(T0 + 21_000).toISOString(),
message: { usage: { input_tokens: 5 } },
}),
// Malformed line — skipped without throwing.
"{not json",
// After the window — excluded.
line(600_000, "m3", "claude-opus-5-20260501", { input_tokens: 777, output_tokens: 777 }),
].join("\n"),
);
const usage = collectClaudeUsage(dir, T0, T0 + 120_000, home);
assert.ok(usage, "usage should not be null");
assert.equal(usage.totals.input, 1100);
assert.equal(usage.totals.output, 550);
assert.equal(usage.totals.cacheRead, 200_000);
assert.equal(usage.totals.cacheWrite, 4000);
assert.equal(usage.messageCount, 2);
assert.deepEqual(usage.sessionIds, ["session-a"]);
assert.equal(Object.keys(usage.models).length, 2);
// Missing project dir → null (non-Claude agent), not a throw.
assert.equal(collectClaudeUsage("/nowhere/at/all", T0, T0 + 1000, home), null);
// --- pricing ---
assert.ok(rateForModel("claude-opus-5-20260501"), "opus 5 must be priced");
assert.equal(rateForModel("totally-unknown"), null);
const priced = priceUsage(usage.models);
// opus-5: 1000 in @$5/M + 500 out @$25/M + 200k cacheRead @$0.5/M + 4k cacheWrite @$6.25/M
const expected = (1000 / 1e6) * 5 + (500 / 1e6) * 25 + (200_000 / 1e6) * 0.5 + (4000 / 1e6) * 6.25;
assert.ok(
priced.costUsd !== null && Math.abs(priced.costUsd - Math.round(expected * 1e4) / 1e4) < 1e-9,
);
assert.deepEqual(priced.unpricedModels, ["future-model-x"]);
// Nothing priced at all → null cost, not $0 (an honest "don't know").
assert.equal(
priceUsage({ mystery: { input: 10, output: 10, cacheRead: 0, cacheWrite: 0 } }).costUsd,
null,
);
// --- display format ---
assert.equal(formatTokens(950), "950");
assert.equal(formatTokens(132_400), "132k");
assert.equal(formatTokens(1_240_000), "1.2M");
assert.equal(formatCostUsd(0.421), "~$0.42");
assert.equal(formatCostUsd(12.4), "~$12");
assert.equal(formatCostUsd(0.001), "<$0.01");
assert.equal(formatCostUsd(null), null);
assert.equal(
formatRunUsage({ tokensIn: 1100, tokensOut: 550, tokensCacheRead: 200_000, costUsd: 0.13 }),
"201k → 550 tok · ~$0.13",
);
assert.equal(formatRunUsage({ tokensIn: null, tokensOut: null, costUsd: null }), null);
fs.rmSync(home, { recursive: true, force: true });
console.log("run-usage: all assertions passed");