Add task_id to spans and fix Investigate Traces button#197
Merged
declan-scale merged 8 commits intomainfrom Apr 17, 2026
Merged
Add task_id to spans and fix Investigate Traces button#197declan-scale merged 8 commits intomainfrom
declan-scale merged 8 commits intomainfrom
Conversation
6a84b54 to
1e16dad
Compare
danielmillerp
approved these changes
Apr 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
task_idcolumn to spans: New nullabletask_idforeign key on thespanstable with a migration that backfills existing spans wheretrace_idmatches a valid task ID. This lets the UI and API query spans by task directly instead of relying on the convention thattrace_idequals the task ID.trace_idfrom spans (fetched via the newtask_idquery) rather than assumingtask_id == trace_id. This makes the SGP Monitor link work correctly for agents whose trace IDs differ from their task IDs.contentandsummary— it showscontentwhen available, falling back tosummary.Backend changes
task_idcolumn tospanstable with FK totasks, backfills fromtrace_id, and adds indexSpanORM,SpanEntity, create/update schemas, and use case all supporttask_id/spanslist endpoint acceptstask_idquery parametertask_idon create, update, and null defaultFrontend changes
useSpanshook queries bytask_idfirst, falls back totrace_idfor backward compatInvestigateTracesButtonacceptstraceIdinstead oftaskIdTaskHeaderfetches spans and extracts the realtrace_idfor the buttonResolves AGX1-188
Follow-up required
Test plan
task_id/spans?task_id=<id>returns the correct spansmake testfor backend integration tests🤖 Generated with Claude Code
Greptile Summary
This PR adds a
task_idFK column to thespanstable, backfills it fromtrace_id, and threads the new field through the ORM, schemas, use-case, and API. The frontend is updated to query spans bytask_idfirst (falling back totrace_idfor backward compatibility), and the Investigate Traces button now uses the actualtrace_idfrom fetched spans rather than assumingtask_id == trace_id. Two additional UI fixes land alongside: reasoning content no longer concatenatescontent+summary, and the "Thinking..." indicator shows correctly after completed tool calls.The FK constraint issue in the integration tests (flagged in the prior review thread) is addressed by introducing
test_agentandtest_tasksfixtures that create real DB rows before inserting spans.Confidence Score: 4/5
Safe to merge with awareness of the outstanding P1 items from prior review threads (loading-state race on the Investigate Traces button, misleading batching comment in the migration).
No new P0/P1 issues found in this pass. The prior P1 threads (button shows wrong URL while spans load, misleading batching comment in migration) remain open; if those are accepted as-is the score would move to 5. All backend changes are consistent and well-tested.
agentex-ui/components/task-header/task-header.tsx (loading-state guard) and the migration file (batching comment) — both covered by prior threads.
Important Files Changed
Sequence Diagram
sequenceDiagram participant TH as TaskHeader participant TS as TracesSidebar participant US as useSpans hook participant API as /spans API participant DB as PostgreSQL TH->>US: useSpans(taskId) TS->>US: useSpans(taskId) Note over US: React Query deduplicates same queryKey US->>API: GET /spans?task_id={taskId} API->>DB: SELECT * FROM spans WHERE task_id = ? DB-->>API: spans[] API-->>US: spans (non-empty) US-->>TH: spans[0].trace_id → traceId US-->>TS: spans for display Note over US: If task_id query returns empty... US->>API: GET /spans?trace_id={taskId} API->>DB: SELECT * FROM spans WHERE trace_id = ? DB-->>API: spans[] (legacy) API-->>US: spans US-->>TH: spans[0].trace_id → traceId US-->>TS: spans for display TH->>TH: render InvestigateTracesButton(traceId)Comments Outside Diff (1)
agentex/src/adapters/orm.py, line 246 (link)ON DELETE SET NULLThe new foreign key has no
ON DELETEclause, so PostgreSQL defaults toNO ACTION(effectivelyRESTRICT). Any attempt to delete a task that has spans referencing it viatask_idwill raise a FK violation error. The migration backfills existing spans, so production databases may already have many tasks linked to spans after the upgrade.If task deletion is ever exercised — through a cleanup job, the API, or even test teardown — this constraint will block it silently.
Adding
ON DELETE SET NULLis the right semantic choice for a nullable FK: the span survives and simply loses itstask_idassociation.And the matching migration should also specify:
Prompt To Fix With AI
Reviews (5): Last reviewed commit: "Allow delete -> null" | Re-trigger Greptile