| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "reasonix/internal/agent" |
| 10 | "reasonix/internal/event" |
| 11 | goaldomain "reasonix/internal/goal" |
| 12 | "reasonix/internal/session" |
| 13 | "reasonix/internal/tool" |
| 14 | ) |
| 15 | |
| 16 | func TestGoalDiagnosticExportReadsCompleteDurableV3Log(t *testing.T) { |
| 17 | service, err := session.NewService("desktop", session.NewFilesystemPersistence(t.TempDir())) |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | t.Cleanup(func() { _ = service.CloseAll(context.Background()) }) |
| 22 | runtime, err := service.Create(t.Context(), session.CreateOptions{SessionID: "goal-diagnostic"}) |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | toolPayload := json.RawMessage(`{"id":"call-1","name":"bash","output":"full diagnostic output; Authorization: Bearer secret-token-123456; api_key=sk-proj-1234567890abcdef"}`) |
| 27 | if _, err := runtime.Session().AppendBatch(t.Context(), "tool-evidence", []session.Event{{Kind: "tool/result", Payload: toolPayload}}); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | machine := goaldomain.NewMachine(nil, func() string { return "goal-1" }) |
| 31 | if _, err := machine.Create(goaldomain.CreateRequest{Objective: "diagnose the goal"}); err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | goalPayload, err := machine.Encode() |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | if _, err := runtime.Session().AppendBatch(t.Context(), "goal:goal-1:1:create", []session.Event{{Kind: "goal/state", Payload: goalPayload}}); err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | exec := agent.New(nil, tool.NewRegistry(), agent.NewSession("system"), agent.Options{}, event.Discard) |
| 42 | c := newOwnedTestController(t, Options{Executor: exec, Sink: event.Discard, SessionService: service, SessionRuntime: runtime, ExclusiveSession: true}) |
| 43 | t.Cleanup(c.Close) |
| 44 | payload, err := c.ExportGoalDiagnostics(t.Context(), GoalDiagnosticMetadata{ApplicationVersion: "1.2.3", BuildCommit: "abc", ProtocolVersion: 4, Capabilities: []string{"goal-lifecycle-v2"}}) |
| 45 | if err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | text := string(payload) |
| 49 | var document map[string]any |
| 50 | if err := json.Unmarshal(payload, &document); err != nil { |
| 51 | t.Fatalf("diagnostic export is not valid JSON: %v\n%s", err, text) |
| 52 | } |
| 53 | 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"`} { |
| 54 | if !strings.Contains(text, want) { |
| 55 | t.Fatalf("diagnostic export missing %s:\n%s", want, text) |
| 56 | } |
| 57 | } |
| 58 | for _, secret := range []string{"secret-token-123456", "sk-proj-1234567890abcdef"} { |
| 59 | if strings.Contains(text, secret) { |
| 60 | t.Fatalf("diagnostic export leaked credential %q", secret) |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 |