sessions: a Kimi Code session whose state.json names no folder takes it from session_index.jsonl (#2103, infinitr0us)
Early protocol 1.4 sessions and those migrated from the Python kimi-cli
(ses_* folders) have neither cwd nor workDir in state.json, so they were
listed under "No folder" and resumed with a bare `kimi --session <id>`.
Kimi Code's session_index.jsonl, at the root of its home beside sessions/,
has {sessionId, sessionDir, workDir} for every session (Kimi's
docs/en/configuration/data-locations.md). parseKimi now falls back to the
workDir of the index line whose sessionId is the session folder's name;
a folder state.json names still wins. The index is read again only when
its size or mtime changes. parseKimi is the only place a Kimi session's
folder is derived (the resume command is built from it).
Run:
- TestKimiCodeSessionFolderFromIndex (new): the reporter's 1.4 and
migrated sessions get the index's folder and a `cd '<folder>' &&`
resume; state.json's cwd beats a different index line; a session with
no folder anywhere stays without one. With kimi.go put back it fails:
`june folder ""`.
- go test -tags nogui ./internal/sessions/... under a sandbox HOME with
pinned caches: ok.
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./...: clean.
Not tried: a real Kimi Code home with an index (this machine's
~/.kimi-code has no sessions); the format is from Kimi's docs, the
reporter's line on the issue and Coffee-CLI's reader of the same file.
Not run: the full go test ./... suite.
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -20,7 +21,9 @@ import (
|
||||
//
|
||||
// - title, a name the user gave when isCustomTitle is true (or titleKind
|
||||
// is "custom", or an old customTitle is set), else Kimi's own;
|
||||
// - cwd, or workDir before protocol 1.5;
|
||||
// - cwd, or workDir before protocol 1.5; a state.json with neither (an
|
||||
// early 1.4 session, one migrated from the Python kimi-cli) has its
|
||||
// folder in session_index.jsonl at the root of Kimi's home (#2103);
|
||||
// - createdAt and updatedAt, epoch milliseconds from 1.5 on, RFC 3339
|
||||
// strings before;
|
||||
// - forkedFrom, the session a fork was made from.
|
||||
@@ -101,6 +104,46 @@ func kimiStateOf(wire string) (kimiState, bool) {
|
||||
return st, err == nil && json.Unmarshal(b, &st) == nil
|
||||
}
|
||||
|
||||
// kimiIndex is Kimi Code's session_index.jsonl, read again only when it
|
||||
// changes. Kimi documents it (docs/en/configuration/data-locations.md): a
|
||||
// line a session, {"sessionId","sessionDir","workDir"}, at the root of its
|
||||
// home beside sessions/.
|
||||
var kimiIndex struct {
|
||||
sync.Mutex
|
||||
path string
|
||||
size int64
|
||||
modified time.Time
|
||||
workDirs map[string]string
|
||||
}
|
||||
|
||||
// kimiIndexedWorkDir is the folder session_index.jsonl gives the session a
|
||||
// main wire file is in, for a state.json that names none (#2103,
|
||||
// infinitr0us). "" when the index has no line for it.
|
||||
func kimiIndexedWorkDir(wire string) string {
|
||||
dir := filepath.Dir(filepath.Dir(filepath.Dir(wire)))
|
||||
path := filepath.Join(filepath.Dir(filepath.Dir(filepath.Dir(dir))), "session_index.jsonl")
|
||||
fi, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
kimiIndex.Lock()
|
||||
defer kimiIndex.Unlock()
|
||||
if kimiIndex.path != path || kimiIndex.size != fi.Size() || !kimiIndex.modified.Equal(fi.ModTime()) {
|
||||
workDirs := map[string]string{}
|
||||
scan(path, 0, func(b []byte) {
|
||||
var l struct {
|
||||
SessionID string `json:"sessionId"`
|
||||
WorkDir string `json:"workDir"`
|
||||
}
|
||||
if json.Unmarshal(b, &l) == nil && l.SessionID != "" && l.WorkDir != "" {
|
||||
workDirs[l.SessionID] = l.WorkDir
|
||||
}
|
||||
})
|
||||
kimiIndex.path, kimiIndex.size, kimiIndex.modified, kimiIndex.workDirs = path, fi.Size(), fi.ModTime(), workDirs
|
||||
}
|
||||
return kimiIndex.workDirs[filepath.Base(dir)]
|
||||
}
|
||||
|
||||
// names are the user's name for the session and Kimi's own, as Kimi's
|
||||
// normalizeSessionTitle tells them apart.
|
||||
func (st kimiState) names() (custom, named string) {
|
||||
@@ -166,7 +209,7 @@ func parseKimi(f file) *state {
|
||||
since = created
|
||||
}
|
||||
if f.main {
|
||||
s.Cwd = cmp.Or(st.Cwd, st.WorkDir)
|
||||
s.Cwd = cmp.Or(st.Cwd, st.WorkDir, kimiIndexedWorkDir(f.path))
|
||||
s.Custom, s.Named = st.names()
|
||||
s.Custom, s.Named = title(s.Custom), title(s.Named)
|
||||
s.saw(created, true)
|
||||
|
||||
@@ -127,6 +127,53 @@ func TestKimiCodeSessions(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// #2103 (infinitr0us): a session whose state.json names no folder (1.4's
|
||||
// early ones, those migrated from the Python kimi-cli) takes it from
|
||||
// session_index.jsonl, as Kimi documents it; a folder state.json names wins.
|
||||
func TestKimiCodeSessionFolderFromIndex(t *testing.T) {
|
||||
inZone(t, 0)
|
||||
claude, _ := setup(t)
|
||||
dir := filepath.Join(filepath.Dir(claude), "home", ".kimi-code")
|
||||
kimiSessionsIn(t, dir)
|
||||
sessions := filepath.Join(dir, "sessions")
|
||||
line := func(slug, id, work string) string {
|
||||
b, _ := json.Marshal(map[string]string{"sessionId": id, "sessionDir": filepath.Join(sessions, slug, id), "workDir": work})
|
||||
return string(b) + "\n"
|
||||
}
|
||||
index := line("wd_v14-june-subagent_000000000001", kimiJune, "/Users/me/Desktop/june") +
|
||||
line("wd_v10-migrated_000000000006", kimiOld, "/Users/me/Desktop/test") +
|
||||
line("wd_v15-early-no-agentid_000000000007", kimiEarly, "/elsewhere")
|
||||
if err := os.WriteFile(filepath.Join(dir, "session_index.jsonl"), []byte(index), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ss := List(0)
|
||||
j := find(t, ss, "kimi", kimiJune)
|
||||
if j.Cwd != "/Users/me/Desktop/june" {
|
||||
t.Fatalf("june folder %q", j.Cwd)
|
||||
}
|
||||
want := "cd '/Users/me/Desktop/june' && kimi --session " + kimiJune
|
||||
if runtime.GOOS == "windows" {
|
||||
want = "Set-Location -LiteralPath '/Users/me/Desktop/june'; kimi --session " + kimiJune
|
||||
}
|
||||
if j.Resume != want {
|
||||
t.Fatalf("june resume %q", j.Resume)
|
||||
}
|
||||
if o := find(t, ss, "kimi", kimiOld); o.Cwd != "/Users/me/Desktop/test" {
|
||||
t.Fatalf("migrated folder %q", o.Cwd)
|
||||
}
|
||||
if e := find(t, ss, "kimi", kimiEarly); e.Cwd != "/work/kimi" {
|
||||
t.Fatalf("state.json's cwd lost to the index: %q", e.Cwd)
|
||||
}
|
||||
// state.json's workDir, no line in the index
|
||||
if c := find(t, ss, "kimi", kimiCancel); c.Cwd != "/work/kimi-old" {
|
||||
t.Fatalf("cancel folder %q", c.Cwd)
|
||||
}
|
||||
// no folder anywhere: still none
|
||||
if n := find(t, ss, "kimi", "session_7d423a2a-9c30-4478-8fb0-ceb78f9dbb6a"); n.Cwd != "" {
|
||||
t.Fatalf("no-turn folder %q", n.Cwd)
|
||||
}
|
||||
}
|
||||
|
||||
// The prompts typed (not a subagent's task, not a skill's), the replies that
|
||||
// end a turn and the name the user gave, from the main file.
|
||||
func TestKimiCodeSessionCounts(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user