Commit Graph
9 Commits
Author SHA1 Message Date
yetone b8270850df sessions: a kept parse's rarely set fields live in a StateMore made only when one is set (#2278)
Each of the ~18k files the Sessions cache keeps had a 760-byte state, and
~330 bytes of it were fields almost no file sets: the Codex spawn and agent
maps, Named/Custom/First, the dsh and Claude conversation lists, Root/Leaf,
Compacts, and the six per-agent tails (Grok, Droid, Kimi, Agy, Dsh, Cline).
On the owner's real cache (18,331 files) Root and Leaf are set in 150,
Named in 117, Compacts in 78, First in 35. They move to an embedded
*StateMore, nil unless something is in it: reads go through more() (a
shared empty one when nil), writes through own(). The JSON is the same
(embedded fields are inlined), so the cache version stays.

- parseWith drops an extra that only had empty values written to it, and
  share() drops one the decode made empty.
- clone() deep-copies the extra, so growing a clone leaves the kept state
  alone. Copies that never write (withoutRevisions, the Agent rename)
  share it, as they shared the maps before.

Run on mote at this tree:
- gofmt -l internal/sessions: clean. go vet ./internal/sessions/ on
  darwin, GOOS=linux and GOOS=windows: ok.
- go test -count=1 ./internal/sessions/: ok (26s).
- Every selector was checked by renaming the embedding to a named field
  and compiling with -gcflags=-e until only the struct literals were left,
  so no field is reached through a nil *StateMore.
- New tests in state_more_test.go: zero() knows every field (reflect), a
  read makes no extra and a write never reaches the shared empty one, a
  clone owns its extra, and a cache round trip keeps no empty extra.
  Broken by hand: dropping Cline from zero() and sharing the extra in
  clone() each turn their test red; restored.
- Real-account idle heap (copy of the owner's config and 24.6MB sessions
  cache, gateway idle 180s, heap after GC): decodeCache's retained heap
  35.0MB -> 22.5MB, reflect.unsafe_New 20.0MB -> 10.5MB. The total
  (128.7MB -> 133.6MB) moves by more than that between runs of one binary
  (118-140MB), from late passes that happened to be live.

Not tried: the full ./... suite (state is unexported; only internal/sessions
uses it, and the whole binary builds).
2026-11-09 18:08:28 +08:00
yetone 532d054bee sessions: every Cursor row has a delete, and the right-hand column lines up
ysm119 (Discord), Sessions page, Cursor tab: some rows had a delete and
some had none, and the time / "N sessions" column stood at a different x
from row to row.

Causes:
- Delete: listAgentWith set Deletable = Deletable(agent) && !ReadOnly, so
  every editor-transcript row (read only) came back deletable:false, and
  client.js drew .sm-del only for deletable rows.
- Alignment: a folder adds its ⋯ only when it has actions, and a session
  row's Resume / terminal / Continue in / delete come and go per row, so
  .sm-when, .cost and .num moved with them. "Copied" is wider than
  "Resume", and at <=600px the cost line wrapped at different places.

Change:
- rowDeletable decides per row. An editor transcript the editor no longer
  keeps (no state.vscdb, or it was read and has no composer for that id)
  is "loose" and deletable: Delete trashes only its <id>/ folder or
  <id>.jsonl/.txt, never state.vscdb. A chat the editor keeps, or a store
  that can't be read, stays undeletable with undeletable:"cursor-editor".
- In an agent magpie deletes from, every row has its delete. One it can't
  delete is aria-disabled, says why in its title, and a click shows the
  reason in the status line and sends nothing (en, zh, zh-TW, ja, de).
- A row without Resume, Continue in or a terminal, and a folder without
  its ⋯, keep that room with an inert, aria-hidden slot. Resume reserves
  the wider of Resume and Copied; at <=600px the cost keeps a min width.
- docs/subsystems/gui-shell.md says so.

Run (on mote via mote-run, base f44fa2ef6; origin/main 93d5c76c0's 3
commits since touch none of these files):
- go test ./internal/sessions: ok. go test ./internal/gui: ok except
  TestMainWindowCorners/macOS_26_SDK, an AppKit titlebar red on macOS 27
  already filed as #3949.
- gofmt -l clean; go vet native, GOOS=linux -tags nogui, GOOS=windows ok.
- New sessions-cursor-align.test.cjs: 10/10 (chromium + webkit x en, zh,
  zh-TW, ja, de; widths 1100/660/420, hover, Copied).
- Sessions GUI set (sessions-*, session-*, gateway-conversations,
  gateway-mode-sessions, account-mask-sessions, header-session-hint,
  routing-session*, routing-ctx-sessions, usage-session*, i18n-keys,
  gui-de, gui-ja, gui-zh-tw): 728/728, and 728/728 again after the
  rebase onto 93d5c76c0 (which changed app.css and i18n.js).

Broken by hand:
- rowDeletable forced to "read only": all 3 new Go tests red.
- sessionPaths' editor-path branch off: TestCursorLooseFlatTranscript red
  (it trashed agent-transcripts/ itself).
- Old client.js/sessions.css/i18n.js: new GUI test 10/10 red. Folder slot
  alone removed: red at 1100 (when 1071 vs 1037). Lanes removed: cost
  edges differ. Before the Copied and de 420 fixes, those cases were red.

Not tried: a real Cursor editor or a real ~/.cursor (every delete ran on
fixture copies in a sandbox HOME); the Windows and Linux apps; deleting a
chat from Cursor's own UI and seeing magpie's list follow.
2026-11-04 21:15:33 +08:00
yetone af8f3a9399 sessions: a new install reads no agent's session history until the user allows it (RustCats on X)
RustCats on X: a fresh magpie showed every agent's session history on
the Sessions page before anyone agreed to it, and asked for a per-agent
choice instead.

- sessions.Reads(agent) (internal/sessions/access.go) is the one gate,
  from settings.SessionReads {Asked, All, Agents}. Every lister asks it,
  the aggregators (allFiles, callFilesWith, callSourcesWith,
  traceLineFiles, wslFiles, DesktopDataDirs) filter by it again, and
  readers of an already-listed session (TranscriptOf, ContentOf,
  SearchSession, SubagentTranscript, CarryOver, ReadCallSource) return
  ErrNotRead, so a cache from before a switch was turned off goes no
  further. The Sessions list, stats, /api/sessions/overview, the call and
  trace indexes, Usage's agent-file calls and the Codex/Pi title readers
  all pass it.
- SettleReads decides once and saves: an install with sessions.json or
  a calls-v* cache read sessions before and keeps all on; a fresh one
  reads none, not asked. A saved value is never replaced; save, sync and
  restore keep it (KeepOwn); only SetSessionReads writes it. An
  unwritable settings.json reads nothing this run.
- GUI: GET/POST /api/sessions/reads. While not asked, the Sessions page
  shows a consent card: each agent found (by a stat of its folder only)
  with a switch, Allow selected and Allow all. Settings > Privacy >
  Agents' session history has "Agents found later" and a row per agent.
  Strings in en, zh, zh-TW, ja, de.
- CLI: magpie sessions read [on|off all|<agent>...]; magpie sessions
  says on stderr when nothing is allowed yet; the TUI's empty Sessions
  page says how.

Read either way, because none of it lists or shows a session: the
gateway's own usage ledger, its projections and the conversations it
recorded (magpie's own records, so usage and cost keep working); the
agents' config and sign-in files; Claude Code's and Codex's
history.jsonl last-used time and a session file's mtime for a quota's
first use; Codex's newest rollout's last line before magpie restarts
Codex (codexTurnOpen).

Tests: TestFreshInstallReadsNoAgentUntilAllowed,
TestSettleReadsKeepsWhatWasThere, TestSessionReadsRoutes,
TestSessionsRead, TestSessionsNotAskedSaysHow, sessions-reads.test.cjs
(26 cases: consent and Settings in 5 languages, Chromium and WebKit,
390 to 900 wide).

Run on mote:
- full Go suite (gofmt -l, go vet ./..., GOOS=linux go vet -tags nogui,
  GOOS=windows go vet, go build, go test -timeout 45m ./...) at this
  change on 8c63c5b78: 79 packages ok; internal/gui red on
  TestSettingsSaveKeepsWhatItDoesNotSend (mine: SessionReads is written
  by SetSessionReads only, the test now sets it so; green after) and
  TestMainWindowCorners (the same on origin/main c1da9ccda, #3949).
- whole GUI suite (make test-ui, concurrency 2) at this change on
  8c63c5b78: 14985 tests, 276 failed in 44 files. Mine: i18n-keys
  (comments named "Allow all"/"Allow selected"; lowercased) and
  account-discovery (read the last Privacy heading; now the one above
  its list); both green after. The 44 files again on origin/main
  c1da9ccda: 36 red there too (#4304, with #3218, #3412, #3414, #3795,
  #4120, #4125). stays-strict green alone; gateway-key-models webkit red
  1 of 3 alone on main too; plugin-github de 440 red on main (#3218).
- at the pushed head (rebased on cd10dfbe6): go build ./...; go test
  internal/sessions, settings, cli, tui ok; internal/gui
  TestSettingsSave*, TestSessionReads*, TestSessions* ok; i18n-keys,
  account-discovery, sessions-reads: 62 pass, 0 fail.

Broken by hand:
- the aggregator filter off with the leaf gates on: green (the leaf
  gates hold); both off: TestFreshInstall red on List, Agents, Calls,
  CallSources, traces, stats, TranscriptOf and Search.
- consentApplies=false and Settings' old save({all}) without the rows:
  all 13 Chromium cases of sessions-reads red.
- sessNothing's hint removed: TestSessionsNotAskedSaysHow red.

Real binary in a sandbox HOME on mote (removed after): a fresh
`magpie sessions` is empty with the hint on stderr; `sessions read`
says not asked, claude off; `read --help` prints usage and saves
nothing; `read on claude` lists the one session; `read off all`
empties it again.

Not tried: the GUI and real app on Windows and Linux; WSL agents in the
consent list (WSL distros are not looked into; Allow all covers them);
a fresh install of a signed release build.
2026-10-29 23:56:05 +08:00
yetone 4c1fb7758b sessions: copies an older magpie left are swept when a live store is read in place (#1988)
1dc20e73f reads a live Cursor's state.vscdb in place, but the sweep of
magpie-cursor-* folders older than an hour ran only when a copy was
made. A user upgrading from a version that copied keeps whatever it
left (Echo-Huang0010 had ~7.1 GiB of such copies) for as long as Cursor
runs. openCursorEditorStore now runs the same once-per-process sweep
first.

Run at this head:
- mote: go vet ./internal/sessions for darwin, linux, windows ok;
  go test -count=3 -run Cursor ./internal/sessions ok.
- xiaomi-gpu (win-run): go test -count=3 -run Cursor ./internal/sessions ok.
- Broken by hand: sweep line removed, TestCursorEditorReadInPlaceWhileLive
  fails with magpie-cursor-left still in the temp folder.
2026-10-28 14:10:42 +08:00
yetone 1dc20e73fc sessions: a live Cursor's state.vscdb is read in place, not copied (#1988)
Beside a running Cursor, every listing, count, transcript and search
read the editor's state.vscdb from a copy in the temp folder, made again
each time Cursor had written since the last. The store is often several
GB, and on Windows, where no clone makes a copy free, Echo-Huang0010
measured ~10.6 GB written every few minutes.

While the store has a non-empty -wal and a -shm beside it, Cursor's own
processes share it as readers through the -shm, and magpie now opens it
the same way, mode=ro with a 3s busy timeout: no copy. Nothing magpie
shows comes from the store's file times (each chat carries
lastUpdatedAt), so a reader's mark in -shm changes nothing it lists.
With no -shm beside a live -wal, or if that open fails, it falls back to
the copy as before. CLI chat stores keep the copy path. OpenUntouched's
open is factored out as openSQLite.

Thanks to Echo-Huang0010 for the report and the measurements; their
patch capped the copy at 64 MiB, which would leave a larger history
unread, so this takes the in-place read instead.

The Cursor tests set TMPDIR only, which Windows' os.TempDir doesn't read
(TMP, TEMP), so TestCursorLeftCopiesSwept's folder landed in the real
temp folder and the next test counted it. cursorTempDir sets all three.

Run at this head:
- mote: go test -count=3 -run Cursor ./internal/sessions ok;
  go test -timeout 45m ./internal/sessions ./internal/agent ok;
  go vet ./internal/sessions for darwin, linux, windows ok; gofmt clean.
- xiaomi-gpu (win-run): go test -count=3 -run Cursor ./internal/sessions
  ok (red before cursorTempDir, from the stray folder).
- Broken by hand: in-place reads off, TestCursorEditorReadInPlaceWhileLive
  fails "a live state.vscdb was copied 3 times, want 0".
- Windows, two processes: a writer holding a WAL store (~40 MB) with
  busy_timeout(0) wrote 200 times while a magpie process listed 40 times
  and read transcripts. 0 failed writes, every list saw the latest title
  (40 of 40), 0 copies in the temp folder.

Not tried: a real Cursor install. Neither test machine has one, so the
writer was a stand-in process, not Cursor itself.
2026-10-28 14:09:05 +08:00
yetone 11dcc743f4 sessions: a listed file is 96 bytes, what only some agents' files have kept behind one pointer
A Sessions poll copies every agent's list of ~20k files several times
(listedRollouts, heldFiles' clone, callFilesWith, allFiles, byKey,
statsRead). Each file was 232 bytes, most of it fields only OpenCode,
ZCode, Hermes, Alma, agy, Cursor's editor or WSL files fill. Those move
into fileMore behind file.more (nil when a file has none); reads go
through m(), writes through w(), which copies first, so copies of a file
never see each other's writes, as before.

A/B with the real ~/.codex and ~/.claude data (ab5.sh, 8 polls 15s
apart, alloc_space between heap-0 and heap-1), base f51ab771f vs this:
- sessionRoutes 848 MB -> 731 MB
- allFiles 426 MB -> 281 MB, heldFiles 322 MB -> 216 MB
- callFilesWith 199 MB -> 109 MB, byKey 86 MB -> 56 MB
- statsRead 467 MB -> 375 MB, listSessions 456 MB -> 352 MB
- all HTTP handlers 1422 MB -> 1379 MB; the profile's total rose
  (2392 -> 2570 MB) in usage's ledger reads, a package this doesn't
  touch, on live data that grew between the runs.
Footprint was not compared: a GUI suite and Go tests ran beside it.

Run before the rebase, at f51ab771f plus this change:
go test -timeout 45m ./internal/sessions/... ./internal/gui/
./internal/usage/... ./internal/cli/ ./internal/tui/ ./internal/otlp/
./internal/tune/ ./internal/agent/ ./internal/gateway/: all ok.
Run at the pushed head (rebased on 6a6c06868, which touches no
sessions file): go test ./internal/sessions/... ok; gofmt clean;
GOOS=linux, windows, darwin go vet -tags nogui ./internal/sessions/
./internal/gui/ ok; the whole tree vetted on all three before the rebase.
Not run: the GUI JS suite (no GUI file changes).
2026-10-27 12:40:00 +08:00
yetone 9bfbd6e45e sessions: Sessions loads again with a large Cursor store, which every listing read whole from disk (#1988, kangxi94)
v0.1.1227 (0e39c8e7e) reads a live Cursor state.vscdb from an APFS clone
made whenever Cursor has written, which stopped the repeated copies. But
the editor's chat list was found with `key LIKE 'composerData:%'`. LIKE is
case-insensitive, so SQLite can't use the key's index and scans the whole
table: every bubble and checkpoint, often several GB. Before, the copy had
just been written and sat in the page cache; a fresh clone doesn't, so
each listing read the whole store from disk again. Cursor writes every few
seconds, so nearly every listing got a fresh clone, and /api/sessions and
/api/sessions/manage took longer than the pages' polls: Sessions and Usage >
Sessions showed skeletons for good.

The list is now read by a range on the key's index (cursorComposersQuery,
key >= 'composerData:' AND key < 'composerData;'), which reads only the
chats' rows.

Run:
- TestCursorComposersQueryUsesTheKeyIndex: Cursor's own schema, keys around
  the range's edges; the plan must be a search on the index and only
  composerData: keys come back. Red with the LIKE put back ("SCAN
  cursorDiskKV").
- sqlite3 on a fresh clone of a 2.2 GB live-written store: LIKE 3.74s cold,
  0.11s warm; the range 0.06s. A second repro (4.3 GB store, 1M small keys,
  500 chats): range 0.01s vs LIKE 3.8s; on main /api/sessions took 40-60s
  and /api/sessions/manage 86-121s every round.
- Built binaries against the 2.2 GB store with a writer every 2s, same
  sandbox, machine at load ~25 from other work: /api/sessions 17/9/3.7s and
  manage 16s with the fix, 34/26/9.4s and 31s on main. The rest is not
  the Cursor scan (a sample shows no sqlite work); not measured on an idle
  machine.
- go test ./internal/sessions ./internal/gui ok; gofmt; go vet darwin,
  linux, windows.
Not tried on the reporter's machine or a real Cursor store.
2026-10-14 04:37:37 +08:00
yetoneandliubing 9935119f1d sessions: search finds Cursor and OpenCode sessions read from their stores, and a Cursor chat's system prompt isn't shown as said (#1743)
Follow-up to liubing's #1743 patch, which put cursor and opencode in
HasTranscript. That moved their search from magpie's recorded
transcript to SearchSession, whose byte prefilter (mayHold) reads
s.Path. For these agents s.Path isn't the conversation:
- OpenCode's JSON storage: the session info file, without messages,
  so every word missed;
- OpenCode's and Cursor's SQLite stores: the newest pages are in the
  -wal, so a live chat's latest turns missed.
linesFile now skips the prefilter for them (a Cursor .jsonl still
gets it).

cursorTranscript also handed the CLI store's system message ("You
are a coding agent.") to the transcript as a said part; system
messages are skipped, as the listing already does.

The patch's tests commit left TestSearchSessionWithoutATranscript
red (it used cursor as the agent without a transcript; now gemini).
sessions-empty-range.test.cjs runs in every language, de at 420px.

Run: go test -tags nogui -count=1 ./internal/sessions/ ok, and -race
ok; sessions-empty-range 10/10 (Chromium+WebKit, en zh zh-TW ja de);
sessions-today/overview/toolbar/search/manage/calendar-fill/paging
GUI tests pass.
Broken by hand: prefilter always on -> TestSearchOpenCodeFiles and
TestSearchCursorLiveChat red; system skip removed ->
TestSearchCursorLiveChat red. Also broke the patch itself: editor
files not appended, readOnly Delete guard, stats message dates, CLI
dedupe, HasTranscript entries, parseCursor readOnly dispatch, and
the "All sessions" button (via MAGPIE_TEST_APP_JS); each went red.

Co-authored-by: liubing <lb0806@foxmail.com>
2026-10-13 06:43:50 +08:00
liubing 1cd8140047 sessions: read Cursor editor history and Cursor/OpenCode conversations (#1743)
Discover editor project transcripts and cursorDiskKV histories without modifying the source. Share one SQLite/WAL snapshot across a scan, refresh metadata changes, deduplicate CLI histories, and reject deletion of read-only sessions.

Count message-only sessions and expose an all-dates action when a filter hides existing history. Read Cursor CLI text, reasoning and tool parts, and OpenCode v1/v2 transcripts through the existing transcript API.
2026-10-13 06:43:50 +08:00