Commit Graph
3 Commits
Author SHA1 Message Date
yetoneandKoohoko 067f39f406 sessions: Desktop titles read once per second, from every handle Desktop keeps, and reach stats and Context (#1675)
Follow-up to Koohoko's 3d6f8b8e, after review.

- Cost: assemble asks DesktopTitles once per native Claude Desktop
  session, and each call globbed and stat'ed every local_*.json, so a
  list of N Cowork sessions looked at the metadata N times. The names
  are now one index, rebuilt at most once a second (desktopTitlesHeld);
  a rename in Desktop shows within that second. Files are read through
  filememo, the shared reader the usage resolver already uses for these
  same files, which also catches a same-size rewrite inside one mtime
  tick (the patch's own size+mtime cache didn't).
- A record that is there but doesn't parse (Desktop midway through a
  write) keeps its last parse instead of dropping to the bare id; a
  record gone still takes its names with it.
- Handles: Claude.app 2.7032's LocalSessionManager (app.asar, read-only)
  counts unarchivedCliSessionId as a live handle beside cliSessionId and
  preClearCliSessionId as lineage beside priorCliSessionIds, and runs a
  record with no cliSessionId under its sessionId without "local_".
  All are named now; the raw "local_..." id no longer is (no usage
  record carries it).
- Siblings: MergeExternalStats (the Sessions page's stats summaries)
  and the Context page named gateway-only Desktop sessions by the
  recorded prompt or nothing; both use the Desktop name now.
- docs/subsystems/gateway-sessions.md says all of the above.

Real layout: no Desktop session records exist on this Mac or the
Windows box (only claude_desktop_config.json), so field names and types
were checked against Claude.app 2.7032's own reader/writer code: a
string title, string cliSessionId/unarchivedCliSessionId/
preClearCliSessionId, an array priorCliSessionIds, files
<account>/<org>/local_*.json under claude-code-sessions and
local-agent-mode-sessions. Dirs come from desktopdir.Everywhere, the
resolver the other Desktop session readers use (Windows %APPDATA% and
MSIX LocalCache, Linux ~/.config); Windows/Linux not run.

Run: new tests TestDesktopSessionTitleHandles, TestDesktopSessionTitleInStats,
TestDesktopTitlesReadOncePerList, and context checks in
TestDesktopSessionTitlesOnPages. Each broken by hand went red:
rescan per call -> "metadata looked at 12 times, want once"; no
last-good -> "a half-written file lost its names"; no unarchived/stem ->
missing ids; stats without the name -> "stats titles map[cli-s:Recorded
prompt ...]"; Context without it -> "context titles map[...: ...]".
go test -race -count=20 -run TestDesktop ./internal/sessions;
go test -tags nogui ./... (sandbox HOME, pinned caches): all ok but
internal/plugin's TestPiPackage (pi-codemode export missing; passes
alone, doesn't import sessions or gui), filed as #1879; gofmt -l;
go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet
./...; go build, GOOS=linux go build -tags nogui, GOOS=windows go build.
GUI: routing-sessions, session-identity, sessions-overview,
sessions-bar-names, sessions-gateway-delete, sessions-manage,
context-window in Chromium and WebKit (84/84 each).
Real binary: magpie web under a sandbox HOME with Claude-3p Code and
Claude Cowork fixture records and five claude-desktop usage records;
the Sessions page (Chromium, WebKit) showed both titles on current,
prior and unarchived ids and the bare id on the unmatched one;
/api/gateway/session-titles named them; a rename showed after 1.3s;
a half-written file kept the name; deleting the record went back to
the id.

Co-authored-by: Koohoko <koohoko@gmail.com>
2026-10-13 07:14:22 +08:00
yetone a98ab77fde gui, gateway: a context window's part, cell or item clicked opens the text the request sent there (masaka)
masaka on Discord asked to see the whole context behind the context window
card: click the agent's prompt, or any other part, and read it.

What changed:
- gateway: the trace keeps the last 10 request bodies in memory only
  (promptBodies; at most 32 MiB each and 64 MiB together, oldest dropped
  first), masked as the vendor got them. For a model the user let have
  the request unmasked, the masked copy is kept all the same. Never
  written to disk, never in telemetry. Route.Prompt and the routing
  history keep names and sizes only, as before.
- gateway: Server.PromptText re-reads a part's items and their text from
  that body (prompt_text.go). promptBuilder gains a keep mode that records
  each item's pieces: system text, tool definitions laid out as JSON, each
  turn's user/thinking/assistant/call pieces, tool results, and images
  or sealed reasoning named with their size. Sizes are unchanged.
- gui: GET /api/context/text?id=&kind=[&item=] (no-store; 404 "not kept"
  when the body is gone or another process serves the gateway).
- gui: in the card, a legend part, a waffle cell or a content row
  (click, Enter or Space) opens a wide modal: one item alone with "All of
  {part}", or the part's list 100 rows at a time, each item's text
  fetched when it is opened and shown 20000 characters at a time, with
  copy buttons. Strings in zh, zh-TW, ja and de.
- docs: gateway-routing and gui-shell subsystem pages; the site's start
  page (en, zh, ja) says the text is kept for the last 10 requests in
  memory and sent nowhere. The README Privacy is unchanged, since nothing
  more is sent.

What was run:
- go test -tags nogui ./... (temp HOME). internal/gateway hit the 10m
  default under the full parallel run, in TestRequestTailAfterOldLimit,
  which takes 57s alone on the base commit too; the package run alone
  with -timeout 30m: ok (700s, the machine shared with other runs). Every other package: ok.
- New tests TestPromptTextIsWhatWasSent, ...KeepsOnlyTheLastBodies,
  ...ChatPieces, TestContextTextAPI. Broken by hand: keeping the unmasked
  body fails (the key shows), and dropping keepBody fails ("not kept").
- gofmt -l; go vet, GOOS=linux and GOOS=windows go vet on internal/gateway
  and internal/gui.
- GUI: tests/context-text.test.cjs in Chromium and WebKit, en/zh/zh-TW/
  ja/de, at 1100 and 420px: legend, row, cell and keyboard open the text,
  long text grows on demand, the page never scrolls, and a gone body is
  named. It fails with the click wiring removed. Under a load average of
  ~140 one case failed once: the test read the list while the dialog
  still said "Reading…". Reproduced with a 400ms reply, fixed by waiting
  for the list; the mock now always answers after 200ms. A second case
  ("a click doesn't move the page", 662 vs 605 on the contents row) came
  from taking the page's top while a wheel scroll was still easing: the
  test now wheels to the row like the other cases, and wheelTo waits for
  three equal looks. Four full runs at load ~150 then passed (80 cases). context-window,
  i18n-keys, held-animations and the modal tests (dialog-backdrop-drag,
  editor-short-window, readable-focus, ux-safety, whats-new, add-sheet,
  api-key-theme) pass in both engines.
- Real use: a sandbox magpie (own HOME, ports 3486/3487, the Codex login)
  served a real `claude -p` (Claude Code) request on codex/gpt-6-luna
  that read a file. In its web UI, Usage > Context: each of System
  prompt, Tools, Memory, Files and Conversation, a waffle cell and a
  contents row opened the request's real text (Claude Code's system
  prompt, 21 tool definitions, the CLAUDE.md reminder, main.go's Read
  result, the turn with its thinking and call). No page errors.
2026-10-11 00:49:01 +08:00
yetone 586f2bce8f context: magpie inspects the context window of every request, session and agent
The gateway reads each request's prompt into its parts (system prompt,
tools and MCP servers, memory, files, tool results, chat), measured
against the model's window and calibrated to the tokens the vendor
counted. Routing shows a request's window as a card: a 400-cell grid by
part, a hover naming what is largest in a cell, and the contents by part.
The Usage page's Context tab scores each agent out of 100 (cache reuse,
how lean it starts, how fast it grows, how reliably it answers), tags it
(lean, cache-friendly, mcp-heavy, compacts, near-full…), and draws each
session's growth with its latest request's card.

Older Safari (15) gets media-query and solid-colour fallbacks for the
card's container queries and color-mix.
2026-10-08 05:36:17 +08:00