| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "unicode/utf8" |
| 10 | |
| 11 | "reasonix/internal/agent" |
| 12 | "reasonix/internal/event" |
| 13 | goaldomain "reasonix/internal/goal" |
| 14 | "reasonix/internal/provider" |
| 15 | "reasonix/internal/session" |
| 16 | "reasonix/internal/tool" |
| 17 | ) |
| 18 | |
| 19 | func TestGoalDiagnosticExportReadsCompleteDurableV3Log(t *testing.T) { |
| 20 | service, err := session.NewService("desktop", session.NewFilesystemPersistence(t.TempDir())) |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | t.Cleanup(func() { _ = service.CloseAll(context.Background()) }) |
| 25 | runtime, err := service.Create(t.Context(), session.CreateOptions{SessionID: "goal-diagnostic"}) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | toolPayload := json.RawMessage(`{"id":"call-1","name":"bash","output":"full diagnostic output; Authorization: Bearer secret-token-123456; api_key=sk-proj-1234567890abcdef\nTOKEN=这是很长的中文测试凭证内容"}`) |
| 30 | if _, err := runtime.Session().AppendBatch(t.Context(), "tool-evidence", []session.Event{{Kind: "tool/result", Payload: toolPayload}}); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | machine := goaldomain.NewMachine(nil, func() string { return "goal-1" }) |
| 34 | if _, err := machine.Create(goaldomain.CreateRequest{Objective: "diagnose the goal"}); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | goalPayload, err := machine.Encode() |
| 38 | if err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | if _, err := runtime.Session().AppendBatch(t.Context(), "goal:goal-1:1:create", []session.Event{{Kind: "goal/state", Payload: goalPayload}}); err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | exec := agent.New(nil, tool.NewRegistry(), agent.NewSession("system"), agent.Options{}, event.Discard) |
| 45 | c := newOwnedTestController(t, Options{Executor: exec, Sink: event.Discard, SessionService: service, SessionRuntime: runtime, ExclusiveSession: true}) |
| 46 | t.Cleanup(c.Close) |
| 47 | c.recordProviderRequest("turn-diagnostic", provider.RequestObservation{ID: 1, Phase: "request_started"}) |
| 48 | c.recordProviderRequest("turn-diagnostic", provider.RequestObservation{ID: 1, Phase: "body_received", BodyBytes: 13740}) |
| 49 | payload, err := c.ExportGoalDiagnostics(t.Context(), GoalDiagnosticMetadata{ApplicationVersion: "1.2.3", BuildCommit: "abc", ProtocolVersion: 4, Capabilities: []string{"goal-lifecycle-v2"}}) |
| 50 | if err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | text := string(payload) |
| 54 | if !utf8.Valid(payload) { |
| 55 | t.Fatal("diagnostic export contains invalid UTF-8") |
| 56 | } |
| 57 | var document map[string]any |
| 58 | if err := json.Unmarshal(payload, &document); err != nil { |
| 59 | t.Fatalf("diagnostic export is not valid JSON: %v\n%s", err, text) |
| 60 | } |
| 61 | transport, ok := document["providerDiagnostics"].(map[string]any) |
| 62 | if !ok { |
| 63 | t.Fatal("missing provider diagnostics export") |
| 64 | } |
| 65 | requests, ok := transport["requests"].([]any) |
| 66 | if !ok || len(requests) != 1 { |
| 67 | t.Fatalf("transport requests = %#v", transport["requests"]) |
| 68 | } |
| 69 | request := requests[0].(map[string]any) |
| 70 | if request["turnId"] != "turn-diagnostic" || request["phase"] != "body_received" || request["bodyBytes"] != float64(13740) { |
| 71 | t.Fatalf("transport evidence changed during export: %#v", request) |
| 72 | } |
| 73 | for _, want := range []string{`"schemaVersion": 1`, `"applicationVersion": "1.2.3"`, `"sessionCodec": "` + session.Codec + `"`, `"full diagnostic output`, `"goal-lifecycle-v2"`, `"activationChanges"`, `"activation": "armed"`, `"inferred": true`, `"persistenceStatus": "ready"`, `"unavailable"`} { |
| 74 | if !strings.Contains(text, want) { |
| 75 | t.Fatalf("diagnostic export missing %s:\n%s", want, text) |
| 76 | } |
| 77 | } |
| 78 | for _, secret := range []string{"secret-token-123456", "sk-proj-1234567890abcdef"} { |
| 79 | if strings.Contains(text, secret) { |
| 80 | t.Fatalf("diagnostic export leaked credential %q", secret) |
| 81 | } |
| 82 | } |
| 83 | if strings.Contains(text, "accepted event traversal failed") { |
| 84 | t.Fatal("redaction truncated the accepted history") |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func TestDiagnosticFieldRedactionPreservesNumericCounters(t *testing.T) { |
| 89 | var output bytes.Buffer |
| 90 | err := writeGoalDiagnosticField(&output, "test", map[string]any{"token_count": uint64(9007199254740993), "api_key": "secret value"}, false) |
| 91 | if err != nil || !json.Valid([]byte("{"+output.String()+"}")) || !strings.Contains(output.String(), `"token_count": 9007199254740993`) || strings.Contains(output.String(), "secret value") { |
| 92 | t.Fatalf("invalid redacted diagnostic field: %s, %v", output.String(), err) |
| 93 | } |
| 94 | } |
| 95 |