Commit Graph
4 Commits
Author SHA1 Message Date
yetoneandJoeyWu-tech 572a1757cc sessions: a path listed twice in one read is read once, so the start's warm-up no longer quits magpie (#2932)
Since c381e4cd1 (#2776) each file a refresh reads has an
entry in parsing, closed and deleted when its parse is published.
refresh checked parsing[f.path] while picking the files to read but set
the entry only later, while building the jobs, so a path listed twice
in one want passed the check twice: two jobs on one entry, and the
second publish closed a nil channel. The start's warm-up
(warmSessions -> StatsAndNative -> refresh) took magpie down a few
seconds after every start, in the GUI, `magpie web` and --gateway alike.
Reported by TimWongUp, nativeas, tianhao2316-beep, mxl2498, cunnak and
Tsuki-hash, on Windows and macOS.

Two sources of the duplicate were found in the thread:
- CODEBUDDY_CONFIG_DIR on WorkBuddy's folder (TimWongUp's .zshrc;
  on Windows WorkBuddy's installer, or its IDE host for its children,
  sets both variables to one .workbuddy): codebuddyFiles and
  workbuddyFiles listed each .jsonl once each.
- A Hermes state.db holding one session id as a TEXT and a BLOB row
  (nativeas): both scan to one string, one <db>#<id> path.

The change, JoeyWu-tech's fix/2932-rebased (7cc938ca) with two test
changes:
- refresh marks a file in parsing as it takes it, so a second listing
  of the path is left out (or waited on). This is the guard: whatever
  list repeats a path, no refresh reads it twice.
- allFiles and callSources keep each path once (distinctPaths), so a
  path is not counted twice either. OpenCode's list doubles the same
  TEXT/BLOB rows (tested); ZCode's and Alma's would too.
- hermesFiles keeps one file a session id, as late as its later row.
- codebuddyFiles lists nothing when CodeBuddy Code's folder is
  WorkBuddy's (os.SameFile: a link or Windows' case rules count too),
  so those sessions are WorkBuddy's on the Sessions page and in usage;
  Dirs names such a folder once. A session the CodeBuddy CLI wrote
  there shows as WorkBuddy's (agreed by TimWongUp on the issue).

Added here: TestCodeBuddyInWorkBuddysFolderIsReadOnce has a "both"
case, both variables on one folder as the Windows reports have it, and
runs StatsAndNative(0) first, the warm-up's own call.
TestOpenCodeIDStoredAsTextAndBlobIsListedOnce lists under mu with
useDBs, as List does: it left OpenCode's db open, and on Windows its
TempDir could not be removed ("being used by another process").

Red on origin/main (sessions code of 86254d404, these tests, a test-only
sameFolder shim): TestRefreshReadsAPathListedTwiceOnce,
TestCodeBuddyInWorkBuddysFolderIsReadOnce (named and both) and
TestHermesIDStoredAsTextAndBlobIsOneSession panic "close of nil
channel" at sessions.go:891 from refresh at :905; the "both" case from
stats.go:143 and :117, the reports' frames.
TestOpenCodeIDStoredAsTextAndBlobIsListedOnce: "allFiles lists the
session 2 times", the same for callSources. Same on the Windows box.
Broken by hand: with only the refresh marking undone, the refresh test
panics again; with codebuddyFiles' check off, the CodeBuddy test fails
"calls ... codebuddy:6, want WorkBuddy's 6" and "listed 3 WorkBuddy and
3 CodeBuddy Code sessions, want 3 and 0". Restored, green.

Run:
- Full suite, `go test -timeout 45m ./...` (pipefail, log kept whole),
  darwin arm64, go1.26.8, at 31136c446 (86254d404 + these two
  changes, before the OpenCode test's useDBs change): 68 packages ok,
  no FAIL; internal/sessions 158s, internal/gateway 579s. No suite lock
  exists on this machine; no other full suite was running.
- At the pushed head (rebased on ec7e50b82, a redact-only change):
  go test ./internal/sessions/ ok, -race ./internal/sessions/ ok
  (109s); -race -count=5 on the four new tests and
  TestReadNotHeldByAnotherRead ok earlier.
- Windows 11 box (cross-compiled test binary, testenv's sandbox HOME):
  the whole internal/sessions package PASS with the fix, the linked
  case included; the new tests red on main's code as above.
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows:
  ok.

Not tried: a real WorkBuddy/CodeBuddy install, a real Hermes state.db
with a BLOB id, or the app's GUI start on Windows; the tests use the
fixtures under the reporters' settings. Not covered: two agents told
to share a folder under different spellings other than CodeBuddy and
WorkBuddy (Pi and omp, Qoder and Qoder CN) still list it twice, without
the crash: #3258.

Co-authored-by: JoeyWu-tech <71250493+JoeyWu-tech@users.noreply.github.com>
2026-10-22 18:06:07 +08:00
yetone 71d2da1f42 catalog, usage, gateway, gui: a price can have a long-context tier and a 1-hour cache write, and the price boxes take decimals (PAMI on Discord)
OpenAI bills gpt-6-astra, gpt-6.1-sol and gpt-6-luna over 272K input at 2× input and cache and 1.5× output, for the whole request. A price now has tiers: `magpie model price <m> 10,50,1,12.5 --tier 272k 20,75,2,25` (--tier can repeat). A tier applies when input + cache reads + cache writes is over its size, so 272,000 is not over and 272,001 is. The built-in prices take tiers only where models.dev lists one (cost.tiers of type "context"), and it does list one for those three. The ledger, each request and OTLP are costed at the tier the call reached. Session and day totals sum many calls, so they stay at the base price.

Anthropic bills a cache write kept for an hour at 2× input and one kept for 5 minutes at 1.25×. Its usage reports which is which in cache_creation.ephemeral_5m/1h_input_tokens. The gateway (API and Claude subscription) and Claude Code's transcripts now keep that split. A price's fifth part is the 1-hour write. When it isn't given, it is 2× input: that is Anthropic's rule, and only Anthropic reports 1-hour writes. Claude list prices carry the 2× too. cache_write_tokens is still the total, and the usage CSV adds cache_write_5m_tokens and cache_write_1h_tokens at the end. The app shows the split on a request's Cache and on the usage tooltips. Old prices and old records, which have no 1-hour part, read as before. The sessions cache (18) and the call cache (calls-v8) are re-read once.

In the GUI, the price boxes were number inputs and dropped what they didn't parse ("0,25"). They are now decimal text boxes that take both "0.25" and "0,25". A model's price row has a 1-hour box (empty means 2× input) and a quiet Long-context price row. That row shows the list's tier greyed in, or sits behind a link when the list has none. A bad size is refused by name. en/zh/ja/de.

Tests:
- New: TestPriceTiers, TestCostSplit, TestPriceJSON, TestModelPriceTiers, TestModelPriceTierCmd, TestAnthropicCacheWriteSplit and TestClaudeCacheWriteSplit. The ledger CSV test expects the new columns.
- model-price-tiers.test.cjs is new; model-price-edit and provider-price-rate are updated. All of them fail against the old assets and pass in WebKit. Chromium couldn't be launched here.
- go vet, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass.
2026-10-05 21:56:59 +08:00
yetone d079dc6223 usage: an OpenCode session's row shows the effort and the model it asked for (#680, Sun1090: 数据不完整,推理强度是有的)
OpenCode rows read from its own data (opencode.db or storage/) had 请求模型 and 推理强度 as —. OpenCode keeps both on each reply (packages/schema/src/v1/session.ts, AssistantMessage): variant, the effort picked in its model menu for the prompt (high, max, low, xhigh, none, thinking … as the provider's variants name them; prompt.ts copies lastUser.model.variant onto every reply), and modelID, the model the reply was sent to. OpenCode 2 (session_message) keeps the variant in model.variant (Model.Ref). It records no model the response named, so modelID is what was asked for, as a Codex rollout's model is.

Now a call's Effort is the reply's variant ("default" or none is no effort picked); a compaction in OpenCode 2 takes the effort last replied with, as it takes the model. In the ledger an OpenCode call is told as a Codex one: requested = its model, served left empty rather than claimed. The call cache moves to calls-v5 so calls read before carry the effort.

TestOpenCodeCallsJSON/DB/V2 (fixtures with variant high, default, xhigh and none) and TestOpenCodeSessionCallIsWhatItAskedFor fail on the old code and pass. go vet ./internal/..., builds (nogui, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass. Not tried against a real OpenCode database with variants (this machine's OpenCode data predates them).
2026-10-03 23:44:06 +08:00
yetone b8fdba5d64 usage: OpenCode's own calls are counted, as Claude Code's and Codex's are (#680, Sun1090: cc-switch 会统计 OpenCode 的用量,我这样不会被统计吗?) — OpenCode used on its own providers (OpenCode Zen/Go, its own keys), not through magpie's gateway, never reached the Usage page: the local calls were Claude Code's and Codex's session files only. Now each OpenCode assistant message is a call (agent opencode): its model, provider (as the session provider), input, output with the reasoning in it (reasoning kept apart too, as Codex's), cache read and write, its end time and how long it took, the session it ran under (a subagent's in its parent's), and a reply that failed with no tokens as a failed call (a stopped one is none). They are read where the Sessions page reads OpenCode: opencode.db — OpenCode 2's session_v2/session_message (a compaction on the reply before it) or the older session/message, the old ones not yet copied while OpenCode 2 migrates — else the JSON files under storage/. A reply through magpie's gateway (provider "magpie", or "dial") is left out: the gateway logged it already. Each session is a call source of its own, read again whole only when it changed (its size is the messages' count plus the sum of their time_updated, so an older message written again tells too) and kept in the call cache on disk as a file's calls are; the database listing is kept while opencode.db and its WAL are unchanged, and a read-only handle is shared between sources and closed after 10 s unused. The Sessions listing is unchanged (allFiles still takes callFiles; Calls and CallSources take callSources). ContentOf says no place for an OpenCode call. OpenCode's own cost isn't used: calls are priced at magpie's effective prices, as the others are. TestOpenCodeCallsJSON, TestOpenCodeCallsDB (cache kept unchanged and across a restart, an updated message read again, a failed and a stopped reply) and TestOpenCodeCallsV2 (compactions follow the reply before them, magpie's skipped) fail on the old code (no OpenCode calls). With a read-only copy of this computer's ~/.local/share/opencode (JSON storage, 3 OpenCode Zen kimi-k2.5-free replies): 3 calls, input 12281, output 235 (123 + 112 reasoning), cache read 24573 — the same as jq over the message files and as SQL SUM over an opencode.db built from them. go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass. 2026-10-03 21:27:17 +08:00