sessions: OpenCode 2 subagents show their type, and a Kimi Code subagent opens on its task, not a context chip (#2290, infinitr0us)

OpenCode 2.0.29 keeps a child session's agent in session_v2.agent and
titles it with the task alone, so ocSubagentTitle found no kind and every
OpenCode subagent row showed only its title and time. The kind now comes
from session_v2.agent when set (ocV2DB.agent, kept once found; a missing
column or NULL reads as none and isn't kept), and the "(@agent subagent)"
title split stays the fallback for older versions.

A Kimi Code subagent's own wire.jsonl starts with the task its parent
gave it, a turn.prompt whose origin is {kind: system_trigger, name:
subagent}; the transcript folded every system_trigger as context. In a
subagent's own wire (not agents/main) that prompt now opens as its first
user message (kimiTask). Other system triggers, and a "subagent" one in
the session's main wire, stay folded.

Fixtures follow the reporter's described shapes: ocMakeV2's real
session_v2 schema with agent 'explore' and a task-only title beside a
child with no agent and an older suffixed title; a 1.5-shaped subagent
wire (agentId, promptId, turnId) with the subagent trigger, a reply and a
background_task trigger, plus a subagent trigger appended to main.
No real OpenCode child sessions or Kimi Code subagents on this machine.
No GUI change: the Sessions client already draws a subagent's kind.

Run: TestOpenCodeV2SubagentKind and TestKimiSubagentTask fail on the old
subagents.go/transcript_kimi.go (no kind; user/context first), and
TestKimiSubagentTask fails with the agents/main scope check dropped.
go test -p 1 -tags nogui ./internal/sessions/ ok at the rebased head;
gofmt -l clean; go vet -tags nogui ./internal/sessions/ for darwin,
GOOS=linux and GOOS=windows.
Full suite: SUITE_LINE
This commit is contained in:
yetone
2026-10-15 23:08:01 +08:00
parent 8820646f7c
commit f15542d2ea
4 changed files with 130 additions and 4 deletions
+10 -3
View File
@@ -425,19 +425,26 @@ for the session) and `start`/`last`; its file is never sent.
`ocRoots` records its parent. Its title splits 1.x's
`<description> (@<agent> subagent)`; 2.x titles it the description alone
(task tool, opencode 2.0.25), and a default `Child session - <time>` is no
title. ZCode shares the parser but has no transcript, so lists none.
title. OpenCode 2 names the agent in `session_v2.agent` (2.0.29), which
is the kind when set, the title's `@<agent>` otherwise (`ocV2DB.agent`,
kept once found; #2290). ZCode shares the parser but has no transcript,
so lists none.
- Kimi Code: each `agents/agent-<n>/wire.jsonl`. 1.5's `subagent.spawned`
record, written in the starting agent's wire, gives `subagentName`,
`description` and `parentAgentId` (`kimiWireOf`, read again only when the
file's size or time changes); 1.4 writes none, and the title is the
subagent's first `turn.prompt`.
subagent's first `turn.prompt`. In a subagent's own wire, a `turn.prompt`
from a `system_trigger` named `subagent` is the task it was given and
opens as its first user message, not folded context (`kimiTask`, #2290);
other system triggers, and any in `agents/main`, stay folded.
These are read beside the parsed-session cache, not in it, so its version
is unchanged. `GET /api/sessions/transcript` takes `sub=<id>` for a
subagent's conversation; the id is looked up among the session's own
`subagents` (`sessions.SubagentTranscript`), never used as a path, and one
not listed is an error. See `TestClaudeSubagents`, `TestQoderSubagents`,
`TestOpenCodeSubagents`, `TestKimiSubagents` and
`TestOpenCodeSubagents`, `TestOpenCodeV2SubagentKind`, `TestKimiSubagents`,
`TestKimiSubagentTask` and
`TestSessionSubagentTranscript`.
The Sessions page draws only main sessions on top. A session's subagents are
+24
View File
@@ -3,6 +3,7 @@ package sessions
import (
"bytes"
"cmp"
"database/sql"
"encoding/json"
"errors"
"os"
@@ -63,6 +64,11 @@ func subagentsOf(s Session, fs []file) []Subagent {
}
sa.Title, sa.Kind = ocSubagentTitle(st.Named)
sa.Title = cmp.Or(sa.Title, st.Title, st.First)
// OpenCode 2 names the agent in the session's row, and its
// titles no longer do (#2290)
if v2, ok := f.oc.(ocV2DB); ok {
sa.Kind = cmp.Or(v2.agent(f.path, f.sid), sa.Kind)
}
case "kimi":
sa.ID = filepath.Base(filepath.Dir(f.path))
if spawned == nil {
@@ -176,6 +182,24 @@ func ocSubagentTitle(named string) (string, string) {
return named, ""
}
// agent is the agent OpenCode 2 ran a session as, session_v2.agent
// ("general", "explore"), "" when it names none or the column isn't there.
// One found is kept: a session's agent is set as it starts.
func (s ocV2DB) agent(path, sid string) string {
if v, ok := ocAgents.Load(path); ok {
return v.(string)
}
var a sql.NullString
if s.db.QueryRow(`SELECT agent FROM session_v2 WHERE id = ?`, sid).Scan(&a) != nil || a.String == "" {
return ""
}
k := a.String[:min(len(a.String), 128)]
ocAgents.Store(path, k)
return k
}
var ocAgents sync.Map
// kimiSpawn is Kimi Code's subagent.spawned record, which the agent that
// started a subagent writes in its own wire.jsonl.
type kimiSpawn struct {
+80
View File
@@ -161,3 +161,83 @@ func TestKimiSubagents(t *testing.T) {
t.Fatalf("1.5 subagents %+v", s.Subagents)
}
}
// #2290 (infinitr0us): OpenCode 2.0.29 names a child session's agent in
// session_v2.agent and titles it with the task alone ("Port Phase2
// physics", no "(@general subagent)"), so the type comes from the column.
// A child whose row names no agent still takes it from an older title.
func TestOpenCodeV2SubagentKind(t *testing.T) {
inZone(t, 0)
data, _ := setupMore(t)
db := ocMakeV2(t, filepath.Join(data, "opencode", "opencode.db"))
const older = "ses_1a2b3c4d5e6fEEEEEEEEEEEEEE"
for _, q := range []string{
`UPDATE session_v2 SET agent = 'build' WHERE id = '` + ocMain + `'`,
`UPDATE session_v2 SET agent = 'explore', title = 'Port Phase2 physics', version = '2.0.29' WHERE id = '` + ocChild + `'`,
`INSERT INTO session_v2 (id, project_id, parent_id, slug, directory, title, version, time_created, time_updated) VALUES
('` + older + `', 'global', '` + ocMain + `', 'old-heron', '/work/oc', 'Find the flaky test (@general subagent)', '2.0.18', 1790416870000, 1790416870000)`,
} {
if _, err := db.Exec(q); err != nil {
t.Fatal(err)
}
}
s := find(t, List(0), "opencode", ocMain)
var got []string
for _, sa := range s.Subagents {
got = append(got, sa.ID+"|"+sa.Title+"|"+sa.Kind)
}
if want := ocChild + "|Port Phase2 physics|explore," + older + "|Find the flaky test|general"; strings.Join(got, ",") != want {
t.Fatalf("subagents %q, want %q", got, want)
}
}
// #2290 (infinitr0us): a Kimi Code 2.1.1 subagent's own wire file starts
// with the task its parent gave it, a turn.prompt from a system_trigger
// named subagent. It opens as the subagent's first message, as a Claude
// Code or OpenCode subagent's task does, not as folded context; another
// system_trigger in that file, and one in the session's own file, stay
// folded. The records are in the shape of Kimi Code's 1.5 wire files.
func TestKimiSubagentTask(t *testing.T) {
inZone(t, 0)
claude, _ := setup(t)
dir := filepath.Join(filepath.Dir(claude), "home", ".kimi-code")
kimiSessionsIn(t, dir)
s := find(t, List(0), "kimi", kimiJune)
agents := filepath.Dir(filepath.Dir(s.Subagents[0].Path))
writeFile(t, filepath.Join(agents, "agent-1", "wire.jsonl"), strings.Join([]string{
`{"type":"metadata","protocol_version":"1.5","created_at":1781503100000}`,
`{"type":"turn.prompt","agentId":"agent-1","input":[{"type":"text","text":"Map every caller of the physics step"}],"origin":{"kind":"system_trigger","name":"subagent"},"promptId":"p1","turnId":0,"time":1781503100010}`,
`{"type":"context.append_message","agentId":"agent-1","message":{"role":"user","content":[{"type":"text","text":"Map every caller of the physics step"}],"toolCalls":[],"origin":{"kind":"system_trigger","name":"subagent"}},"time":1781503100010}`,
`{"type":"context.append_loop_event","agentId":"agent-1","event":{"type":"content.part","uuid":"u1","turnId":"0","step":1,"stepUuid":"s1","part":{"type":"text","text":"Three callers."}},"time":1781503100500}`,
`{"type":"turn.prompt","agentId":"agent-1","input":[{"type":"text","text":"a background task finished"}],"origin":{"kind":"system_trigger","name":"background_task"},"promptId":"p2","turnId":1,"time":1781503100600}`,
}, "\n")+"\n")
f, err := os.OpenFile(filepath.Join(agents, "main", "wire.jsonl"), os.O_APPEND|os.O_WRONLY, 0)
if err != nil {
t.Fatal(err)
}
f.WriteString(`{"type":"subagent.spawned","subagentId":"agent-1","subagentName":"explore","parentToolCallId":"call_2","parentAgentId":"main","description":"Map the callers","runInBackground":true,"time":1781503100000}` + "\n")
f.WriteString(`{"type":"turn.prompt","agentId":"main","input":[{"type":"text","text":"subagent agent-1 finished"}],"origin":{"kind":"system_trigger","name":"subagent"},"promptId":"p3","turnId":9,"time":1781503200000}` + "\n")
f.Close()
Reset()
s = find(t, List(0), "kimi", kimiJune)
tr, err := SubagentTranscript(s, "agent-1")
if err != nil {
t.Fatal(err)
}
var got []string
for _, p := range tr.Parts {
got = append(got, p.Role+"/"+p.Kind+"|"+p.Text)
}
want := "user/text|Map every caller of the physics step,assistant/text|Three callers.,user/context|a background task finished"
if strings.Join(got, ",") != want {
t.Fatalf("subagent transcript %q, want %q", got, want)
}
main, err := TranscriptOf(s)
if err != nil {
t.Fatal(err)
}
last := main.Parts[len(main.Parts)-1]
if last.Kind != "context" || last.Text != "subagent agent-1 finished" {
t.Fatalf("the session's own system_trigger: %+v", last)
}
}
+16 -1
View File
@@ -4,12 +4,14 @@ import (
"bytes"
"cmp"
"encoding/json"
"path/filepath"
)
// Kimi Code's conversation, read from a session's main wire.jsonl (#2056,
// infinitr0us). From protocol 1.4 on a turn is its turn.prompt (the user's
// input; one whose origin isn't the user, a skill's activation or a
// subagent's system_trigger, is shown folded) and the loop events written as it
// system_trigger, is shown folded, but for a subagent's task in its own
// file) and the loop events written as it
// runs: content.part (text or think), tool.call (name, args, toolCallId)
// and tool.result (toolCallId, result.output). These alone are read, since
// they are there for every 1.4 and 1.5 session, finished or still running:
@@ -38,6 +40,7 @@ type kimiLine struct {
} `json:"input"`
Origin struct {
Kind string `json:"kind"`
Name string `json:"name"`
} `json:"origin"`
Event struct {
Type string `json:"type"`
@@ -94,6 +97,9 @@ func kimiTranscript(path string, keep func([]byte) bool, add func(bool, Part) bo
if json.Unmarshal(b, &l) != nil {
return true
}
if kimiTask(path, l) {
l.Origin.Kind = "user"
}
for _, p := range kimiParts(l, names) {
if !add(p.Role != "assistant", p) {
return false
@@ -104,6 +110,15 @@ func kimiTranscript(path string, keep func([]byte) bool, add func(bool, Part) bo
return err
}
// kimiTask is whether a line is the task a subagent was given: in its own
// wire file (agents/agent-<n>), a turn.prompt from Kimi Code's system_trigger
// named subagent (#2290), its first message, as a Claude Code or OpenCode
// subagent's is.
func kimiTask(path string, l kimiLine) bool {
return l.Type == "turn.prompt" && l.Origin.Kind == "system_trigger" && l.Origin.Name == "subagent" &&
filepath.Base(filepath.Dir(path)) != "main"
}
// kimiParts are what one line of a wire file said.
func kimiParts(l kimiLine, names map[string]string) []Part {
switch l.Type {