Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion internal/llmadapters/subprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -1817,12 +1817,30 @@ func parseClaudeBGActiveJobs(data []byte) (map[string]bool, error) {
}
activeJobs := map[string]bool{}
collectClaudeBGActiveJobs(jsonRoot, 0, activeJobs)
if len(activeJobs) == 0 && claudeBGJSONShapeNonEmpty(jsonRoot) {
if len(activeJobs) == 0 && claudeBGJSONShapeNonEmpty(jsonRoot) && !isClaudeSessionListing(jsonRoot) {
return nil, errors.New("unrecognized non-empty JSON shape")
}
return activeJobs, nil
}

// isClaudeSessionListing reports whether value is the session listing
// `claude agents --json` prints: an array of session objects, each carrying a
// sessionId. Only background sessions carry a job id, so a listing of
// interactive sessions alone is a recognized shape with no active jobs.
func isClaudeSessionListing(value any) bool {
sessions, ok := value.([]any)
if !ok || len(sessions) == 0 {
return false
}
for _, session := range sessions {
fields, ok := session.(map[string]any)
if !ok || claudeBGStateString(fields, "sessionId") == "" {
return false
}
}
return true
}

func collectClaudeBGActiveJobs(value any, depth int, activeJobs map[string]bool) {
if depth > claudeBGInspectMaxDepth {
return
Expand Down
28 changes: 28 additions & 0 deletions internal/llmadapters/subprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,34 @@ func TestParseClaudeBGActiveJobs(t *testing.T) {
}
})

t.Run("accepts a session listing with only interactive sessions", func(t *testing.T) {
payload, err := os.ReadFile(filepath.Join("testdata", "claude_agents_interactive_only.json"))
if err != nil {
t.Fatalf("read fixture: %v", err)
}
activeJobs, err := parseClaudeBGActiveJobs(payload)
if err != nil {
t.Fatalf("parseClaudeBGActiveJobs: %v", err)
}
if len(activeJobs) != 0 {
t.Fatalf("activeJobs = %#v, want empty", activeJobs)
}
})

t.Run("collects background ids from a mixed session listing", func(t *testing.T) {
payload, err := os.ReadFile(filepath.Join("testdata", "claude_agents_mixed.json"))
if err != nil {
t.Fatalf("read fixture: %v", err)
}
activeJobs, err := parseClaudeBGActiveJobs(payload)
if err != nil {
t.Fatalf("parseClaudeBGActiveJobs: %v", err)
}
if len(activeJobs) != 1 || !activeJobs["de41e310"] {
t.Fatalf("activeJobs = %#v, want only de41e310", activeJobs)
}
})

t.Run("allows empty arrays", func(t *testing.T) {
activeJobs, err := parseClaudeBGActiveJobs([]byte(`[]`))
if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions internal/llmadapters/testdata/claude_agents_interactive_only.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"pid": 10240,
"cwd": "/home/dev/projects/service-a",
"kind": "interactive",
"startedAt": 1789704757845,
"sessionId": "0a119af3-fa96-44b8-b975-e69936e7d0e2",
"name": "service-a-3b",
"status": "idle"
},
{
"pid": 75433,
"cwd": "/home/dev/projects/service-b",
"kind": "interactive",
"startedAt": 1789732852316,
"sessionId": "ad2fba54-17c1-4fe0-af6b-1722fbf5a636",
"name": "service-b-9f",
"status": "busy"
}
]
22 changes: 22 additions & 0 deletions internal/llmadapters/testdata/claude_agents_mixed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"pid": 10240,
"cwd": "/home/dev/projects/service-a",
"kind": "interactive",
"startedAt": 1789704757845,
"sessionId": "0a119af3-fa96-44b8-b975-e69936e7d0e2",
"name": "service-a-3b",
"status": "idle"
},
{
"pid": 7035,
"id": "de41e310",
"cwd": "/home/dev/.local/share/cr/runs/run-1/workbench/reviewers/reviewer-a/repo",
"kind": "background",
"startedAt": 1790101202780,
"sessionId": "de41e310-0409-418b-bc03-809db309cd71",
"name": "read cr prompt",
"status": "busy",
"state": "working"
}
]
Loading