| 1 | package transcript |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "reasonix/internal/event" |
| 8 | "reasonix/internal/provider" |
| 9 | ) |
| 10 | |
| 11 | func TestHTTP2FailureHistoryKeepsTransportIdentity(t *testing.T) { |
| 12 | rows := History([]provider.Message{{ID: "failure", Role: provider.RoleTool, LocalOnly: true, |
| 13 | ToolCallID: provider.LocalOnlyToolID, Name: provider.LocalOnlyToolName, |
| 14 | InterruptedTurn: &provider.InterruptedTurnRecovery{Pending: true, TerminalStatus: "failed", |
| 15 | FailureDiagnostic: &provider.FailureDiagnostic{Kind: provider.FailureKindTransportProtocol, TransportCode: "PROTOCOL_ERROR", ProviderID: "saved-provider", ProviderDisplayName: "DeepSeek", Protocol: "openai"}}}}, HistoryOptions{}) |
| 16 | if len(rows) != 1 || rows[0].Code != event.NoticeCodeProviderRequestFailed || rows[0].Level != "warn" || rows[0].Diagnostic == nil || rows[0].Diagnostic.Kind != provider.FailureKindTransportProtocol || rows[0].Diagnostic.TransportCode != "PROTOCOL_ERROR" || !strings.Contains(rows[0].Detail, "saved-provider") { |
| 17 | t.Fatalf("history lost failure classification: %+v", rows) |
| 18 | } |
| 19 | if strings.Contains(rows[0].Detail, "PROTOCOL_ERROR") { |
| 20 | t.Fatal("history duplicated the structured transport code in prose") |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func TestHistoryIdentityAndLegacyPresentation(t *testing.T) { |
| 25 | raw := "[Pasted text #1 · 2 lines]\n--- Begin [Pasted text #1 · 2 lines] ---\none\ntwo\n--- End [Pasted text #1 · 2 lines] ---" |
| 26 | messages := []provider.Message{{ID: "user", Role: provider.RoleUser, Origin: provider.MessageOriginUser, Content: raw, RawContent: raw}, |
| 27 | {ID: "plan", Role: provider.RoleAssistant, Content: "plan and its final note", ToolCalls: []provider.ToolCall{{ID: "call", Name: "proxy", ResolvedName: "read_file", CapabilityID: "read", Arguments: "full args", Diff: "diff", Added: 1}}}} |
| 28 | rows := History(messages, HistoryOptions{SubmitContent: func(m provider.Message) string { return m.Content }, LegacyTurns: []LegacyDisplayTurn{{TurnID: "turn", UserMessageID: "user", Messages: []Message{{MessageID: "plan", Role: "assistant", Content: "plan"}}}}}) |
| 29 | if len(rows) != 2 || rows[0].Content != "[Pasted text #1 · 2 lines]" || rows[0].SubmitText != raw { |
| 30 | t.Fatalf("legacy display/replay mismatch: %+v", rows) |
| 31 | } |
| 32 | if rows[1].RecordID != "m:plan" || rows[1].Content != "plan and its final note" { |
| 33 | t.Fatalf("identified planner duplicated: %+v", rows) |
| 34 | } |
| 35 | call := rows[1].ToolCalls[0] |
| 36 | if call.ResolvedName != "read_file" || call.CapabilityID != "read" || call.Arguments != "full args" || call.Diff != "diff" { |
| 37 | t.Fatalf("tool metadata lost: %+v", call) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestHistorySteersRetainDistinctCanonicalMessageIdentities(t *testing.T) { |
| 42 | const content = "[Mid-turn steer queued by the user. Do not treat this as a new task; use it only as additional guidance for the current task after completing the current step.]\nSame guidance" |
| 43 | rows := History([]provider.Message{ |
| 44 | {ID: "first", Role: provider.RoleUser, Content: content}, |
| 45 | {ID: "second", Role: provider.RoleUser, Content: content}, |
| 46 | }, HistoryOptions{}) |
| 47 | if len(rows) != 2 { |
| 48 | t.Fatalf("same-text steers collapsed: %+v", rows) |
| 49 | } |
| 50 | for i, id := range []string{"first", "second"} { |
| 51 | if rows[i].MessageID != id || rows[i].RecordID != "m:"+id || rows[i].Content != "↪ Same guidance" || rows[i].Role != "notice" { |
| 52 | t.Fatalf("steer lost canonical identity: %+v", rows[i]) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func TestHistoryUnappliedSteerKeepsLegacyRecordAddressAndMessageIdentity(t *testing.T) { |
| 58 | const content = "[Mid-turn steer queued by the user. Do not treat this as a new task; use it only as additional guidance for the current task after completing the current step.]\nUse plan B" |
| 59 | rows := History([]provider.Message{{ID: "queued", Role: provider.RoleTool, LocalOnly: true, |
| 60 | ToolCallID: provider.LocalOnlyToolID, Name: provider.LocalOnlyToolName, Content: content}}, HistoryOptions{}) |
| 61 | if len(rows) != 1 || rows[0].Role != "notice" || rows[0].Code != event.NoticeCodeUnappliedSteer || |
| 62 | rows[0].MessageID != "queued" || rows[0].RecordID != "m:queued:notice:0" { |
| 63 | t.Fatalf("unapplied steer identity = %+v", rows) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func TestHistoryCancelledLegacySuppressesCanonicalLocalBody(t *testing.T) { |
| 68 | rows := History([]provider.Message{{ID: "u", Role: provider.RoleUser, Origin: provider.MessageOriginUser, Content: "q"}, |
| 69 | {ID: "partial", Role: provider.RoleAssistant, LocalOnly: true, Content: "must not duplicate"}, |
| 70 | {ID: "receipt", Role: provider.RoleAssistant, LocalOnly: true, DecisionReceipt: &provider.DecisionReceipt{ID: "decision"}}}, |
| 71 | HistoryOptions{LegacyTurns: []LegacyDisplayTurn{{UserMessageID: "u", TurnID: "t", Messages: []Message{{Role: "assistant", Content: "partial display"}, {Role: "notice", Code: event.NoticeCodeCancelledTurn}}}}}) |
| 72 | if len(rows) != 4 { |
| 73 | t.Fatalf("rows=%+v", rows) |
| 74 | } |
| 75 | for _, row := range rows { |
| 76 | if strings.Contains(row.Content, "must not duplicate") { |
| 77 | t.Fatal("canonical cancelled body duplicated") |
| 78 | } |
| 79 | } |
| 80 | if rows[3].DecisionReceipt == nil { |
| 81 | t.Fatal("independent receipt lost") |
| 82 | } |
| 83 | } |
| 84 |