Files
magpie/internal/sessions/zcode.go
T
yetone c358e76607 zcode: on Windows, write where ZCode reads, %HOME% before %USERPROFILE%
Gearclickist on Discord: magpie connects ZCode without an error, but
ZCode on Windows never lists magpie as a provider; on a Mac it does.

ZCode 3.14.3 (the same JS in the Mac and Windows builds, diff -rq of the
extracted app.asar) picks its data folder from setting.json's dataBaseDir,
then ZCODE_DATA_BASE_DIR, then process.env.HOME, and only then
os.homedir() (USERPROFILE). magpie used Go's home, which on Windows is
USERPROFILE. With HOME set (Git Bash, MSYS2, Emacs and the like), ZCode
reads %HOME%\.zcode\v2\provider_config.json and magpie wrote
%USERPROFILE%\.zcode\v2\..., a folder ZCode never opens. On a Mac HOME is
Go's home too, so the two always met.

appdir.ZCodeHome gives %HOME% on Windows when it is set, absolute, and
the folder asked about is USERPROFILE (a folder given on purpose is
kept). The agent target, the ZCode credential and team readers and the
library target use it. ZCode's CLI database stays under USERPROFILE (seen
on Windows below), so the sessions reader is unchanged; its comment says
why.

Run:
- go test ./internal/agent/ -run TestZCode; the new
  TestZCodeOnWindowsUsesTheHomeZCodeReads forces zcodeOS=windows with
  HOME and USERPROFILE apart. Broken by hand (ZCodeDataIn(home) back in
  zcode()): red, "ZCode's folder on Windows with HOME set: .../001/.zcode
  ... want .../003/.zcode".
- gofmt -l; go vet for darwin, linux (-tags nogui) and windows.
- Windows box, sandbox folders only, HOME and USERPROFILE set apart:
  ZCode 3.14.3 for Windows, extracted and started there, kept its logs,
  workspace and provider_config.json under HOME\.zcode\v2 and only
  cli\db under USERPROFILE. The old magpie.exe's `zcode provider magpie`
  wrote profile\.zcode\v2\{config,provider_config}.json (the bug); this
  build's wrote home\.zcode\v2\..., beside ZCode's own files, and nothing
  under the profile.
- Full suite (go test -timeout 45m ./..., one at a time on the box):
  68 packages ok, exit 0, at d87be7f13 and again after rebasing onto
  b00c82acf (tree of this commit).

Not tried: the reporter's own machine (their HOME is inferred, not
confirmed); ZCode's picker on the Windows box (it ran over ssh, without
a desktop, and exited with the session), so "lists magpie" is read from
where ZCode keeps its files, not seen; the provider and library siblings
have no forced-OS test of their own; ZCODE_DATA_BASE_DIR and
ZCODE_DESKTOP_HOME_DIR are not followed.
2026-10-22 04:32:46 +08:00

51 lines
1.8 KiB
Go

package sessions
import (
"os"
"path/filepath"
)
// ZCode's CLI, OpenCode made over, keeps its sessions in OpenCode's tables
// in a database of its own, ~/.zcode/cli/db/db.sqlite: a session row with
// how its title came about (title_source), and its messages and parts as
// JSON documents. A message counts as OpenCode's does but for its tokens,
// the AI SDK's: input with the cache in it, output with the reasoning. Its
// model is modelId (the older ones say modelID too), and a user message
// says who it is from (semantics.origin), a prompt typed a real_user's. A
// subagent's work is a session whose parent is the one it ran in, as in
// OpenCode. ZCode is a desktop app: no command picks a session up again.
// ZCodeDir is ZCode's folder, ~/.zcode, under Go's home. Its CLI's
// database stays there, USERPROFILE on Windows, even where the app keeps
// the rest under a HOME the user set (appdir.ZCodeHome): ZCode 3.14.3 for
// Windows, run with both, wrote cli\db\db.sqlite under USERPROFILE and
// v2 under HOME.
func ZCodeDir() string {
home, _ := os.UserHomeDir()
return filepath.Join(home, ".zcode")
}
func zcodeDB() string { return filepath.Join(ZCodeDir(), "cli", "db", "db.sqlite") }
func zcodeFiles() []file {
if db := zcodeDB(); fileExists(db) {
return openCodeDBFiles("zcode", db)
}
return nil
}
// zcDB is ZCode's database: OpenCode's, with a session's title its own only
// when it was not the default one.
type zcDB struct{ ocDB }
func (s zcDB) info(sid string) (ocInfo, bool) {
var i ocInfo
var source string
err := s.db.QueryRow(`SELECT id, COALESCE(parent_id, ''), directory, title, COALESCE(title_source, ''), time_created, time_updated FROM session WHERE id = ?`, sid).
Scan(&i.ID, &i.ParentID, &i.Directory, &i.Title, &source, &i.Time.Created, &i.Time.Updated)
if source == "default" {
i.Title = ""
}
return i, err == nil
}