Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix(computer): link Claude Code and Pi history to the Run that made it

LeoXing5f6ecdb8cfa3
diff --git a/apps/computer/local-daemon/internal/linger/runtimehistory/claude.go b/apps/computer/local-daemon/internal/linger/runtimehistory/claude.go
index d668cb23..b1eed9f7 100644
--- a/apps/computer/local-daemon/internal/linger/runtimehistory/claude.go
+++ b/apps/computer/local-daemon/internal/linger/runtimehistory/claude.go
@@ -124,6 +124,7 @@ func inspectClaude(ctx context.Context, request Request) (Page, error) {
matched := false
instructions := Instructions{Status: "unavailable", Source: "claude_transcript", Reason: "Native transcript does not contain an instruction snapshot", Blocks: []InstructionBlock{}}
instructionBytes := 0
+ turn := ""
for i := range records {
item := &records[i]
id := stringValue(item.original, "sessionId", 256)
@@ -134,9 +135,15 @@ func inspectClaude(ctx context.Context, request Request) (Page, error) {
kind := stringValue(item.original, "type", 256)
message := objectValue(item.original["message"])
item.history.Role = stringValue(message, "role", 64)
- item.history.TurnID = stringValue(item.original, "uuid", 256)
+ // Claude stamps the prompt a turn answers only on its user records;
+ // carried forward, it gives every record of the turn one turn ID.
+ if prompt := stringValue(item.original, "promptId", 256); prompt != "" {
+ turn = prompt
+ }
+ item.history.TurnID = first(turn, stringValue(item.original, "uuid", 256))
if kind == "user" || kind == "assistant" {
item.history.Category = "message"
+ item.history.ToolCallID = claudeToolCallID(message)
}
if kind == "system" {
item.history.Category = "lifecycle"
@@ -210,6 +217,22 @@ func inspectClaude(ctx context.Context, request Request) (Page, error) {
return Page{Status: "available", ProviderKind: "claude", Source: "claude_transcript", Instructions: instructions, Snapshot: &snapshot, Records: selected, PreviousCursor: previous}, nil
}
+// claudeToolCallID is the call a message's first tool block makes or answers.
+// It is the native call ID the Run published, so it links the turn to the Run.
+func claudeToolCallID(message map[string]any) string {
+ blocks, _ := message["content"].([]any)
+ for _, value := range blocks {
+ block := objectValue(value)
+ switch block["type"] {
+ case "tool_use":
+ return stringValue(block, "id", 256)
+ case "tool_result":
+ return stringValue(block, "tool_use_id", 256)
+ }
+ }
+ return ""
+}
+
// claudeCompactionRecord reports a record a compaction wrote: its boundary, its
// summary, and the /compact command and its output that Claude records as user
// messages.
diff --git a/apps/computer/local-daemon/internal/linger/runtimehistory/claude_test.go b/apps/computer/local-daemon/internal/linger/runtimehistory/claude_test.go
index c1c186d2..87473627 100644
--- a/apps/computer/local-daemon/internal/linger/runtimehistory/claude_test.go
+++ b/apps/computer/local-daemon/internal/linger/runtimehistory/claude_test.go
@@ -78,3 +78,34 @@ func TestClaudeHistoryCategorizesCompactionRecords(t *testing.T) {
t.Fatalf("categories = %v, want %v (page %+v)", got, want, page)
}
}
+
+func TestClaudeHistoryLinksATurnByItsPromptAndCalls(t *testing.T) {
+ home := t.TempDir()
+ session := "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa"
+ project := filepath.Join(home, ".claude", "projects", "workspace")
+ if err := os.MkdirAll(project, 0700); err != nil {
+ t.Fatal(err)
+ }
+ line := func(record string) string { return strings.ReplaceAll(record, "SESSION", session) + "\n" }
+ content := line(`{"type":"queue-operation","sessionId":"SESSION","operation":"enqueue"}`) +
+ line(`{"type":"user","sessionId":"SESSION","uuid":"u1","promptId":"p1","message":{"role":"user","content":"list it"}}`) +
+ line(`{"type":"assistant","sessionId":"SESSION","uuid":"a1","message":{"role":"assistant","content":[{"type":"tool_use","id":"toolu_1","name":"Bash","input":{"command":"ls"}}]}}`) +
+ line(`{"type":"user","sessionId":"SESSION","uuid":"u2","promptId":"p1","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_1","content":"a.md"}]}}`) +
+ line(`{"type":"assistant","sessionId":"SESSION","uuid":"a2","message":{"role":"assistant","content":[{"type":"text","text":"Done."}]}}`) +
+ line(`{"type":"user","sessionId":"SESSION","uuid":"u3","promptId":"p2","message":{"role":"user","content":"again"}}`)
+ if err := os.WriteFile(filepath.Join(project, session+".jsonl"), []byte(content), 0600); err != nil {
+ t.Fatal(err)
+ }
+ page := InspectClaude(context.Background(), Request{CodexHome: home, ThreadID: session})
+ var turns, calls []string
+ for _, item := range page.Records {
+ turns = append(turns, item.TurnID)
+ calls = append(calls, item.ToolCallID)
+ }
+ if got, want := strings.Join(turns, ","), ",p1,p1,p1,p1,p2"; got != want {
+ t.Fatalf("turns = %s, want %s", got, want)
+ }
+ if got, want := strings.Join(calls, ","), ",,toolu_1,toolu_1,,"; got != want {
+ t.Fatalf("calls = %s, want %s", got, want)
+ }
+}
diff --git a/apps/computer/local-daemon/internal/linger/runtimehistory/pi.go b/apps/computer/local-daemon/internal/linger/runtimehistory/pi.go
index 381127e9..692c85d3 100644
--- a/apps/computer/local-daemon/internal/linger/runtimehistory/pi.go
+++ b/apps/computer/local-daemon/internal/linger/runtimehistory/pi.go
@@ -171,13 +171,20 @@ func inspectPi(ctx context.Context, request Request) (Page, error) {
records = branch
instructions := Instructions{Status: "unavailable", Source: "pi_session", Reason: "Native session has no instruction snapshot", Blocks: []InstructionBlock{}}
sections := make(map[string]InstructionBlock)
+ turn := ""
for i := range records {
item := &records[i]
kind := stringValue(item.original, "type", 256)
message := objectValue(item.original["message"])
item.history.NativeType = kind
- item.history.TurnID = stringValue(item.original, "id", 256)
item.history.Role = stringValue(message, "role", 64)
+ // A turn is everything after one user message, so its records share
+ // that message's ID.
+ id := stringValue(item.original, "id", 256)
+ if kind == "message" && item.history.Role == "user" {
+ turn = id
+ }
+ item.history.TurnID = first(turn, id)
item.history.Category = "lifecycle"
if kind == "message" {
switch item.history.Role {
diff --git a/apps/computer/local-daemon/internal/linger/runtimehistory/pi_test.go b/apps/computer/local-daemon/internal/linger/runtimehistory/pi_test.go
index 29532a17..7e7ba8ea 100644
--- a/apps/computer/local-daemon/internal/linger/runtimehistory/pi_test.go
+++ b/apps/computer/local-daemon/internal/linger/runtimehistory/pi_test.go
@@ -47,6 +47,10 @@ func TestPiHistoryCurrentBranchFrozenPaginationAndRedaction(t *testing.T) {
t.Fatal("abandoned branch leaked")
}
}
+ // The answer and its tool result belong to the turn the question opened.
+ if turn := all.Records[len(all.Records)-2].TurnID; turn != "question" {
+ t.Fatalf("tool result turn = %q", turn)
+ }
if got := InspectPi(context.Background(), Request{CodexHome: t.TempDir(), ThreadID: id}); got.Status != "unavailable" {
t.Fatal("cross Context history")
}