feat(compliance): derive agent_context.last_test_* from canonical runs#4268
Open
EmmaLouise2018 wants to merge 2 commits intoEmmaLouise2018/unification-pr3-backfill-and-drop-test-historyfrom
Conversation
PR 4 of the #4247 unification stack. Adds triggered_org_id to agent_compliance_runs so per-org scoping of the new agent_context_with_latest_test view is accurate. Without it, two orgs that own the same agent URL would conflate test history. Owner-test write path in evaluate_agent_quality populates it from the caller's organizationId; heartbeat/manual/webhook leave it NULL. agent_context_with_latest_test view: agent_contexts.* joined LATERAL to the latest non-dry-run agent_compliance_runs row scoped by (triggered_org_id, agent_url), plus COUNT for total_tests_run. agent-context-db.ts readers (getByOrganization, getById, getByOrgAndUrl) SELECT from the view and alias canonical_last_test_* → last_test_* so callers see no shape change. Legacy columns stay for backward compat — third-party recordTest() writes still hit them (session-scoped audit retained per PR 3). The columns + recordTest retire in the follow-up "final cleanup" PR that drops agent_test_history. Stacked on #4264 → #4263 → #4250.
12 tasks
6 tasks
Contributor
|
Code review (expert pass): solid, minor nits — but blocked by #4264 in the chain. Nits (non-blocking):
View is plain |
…cenario semantic shift in changeset
Contributor
|
Both nits addressed in 1103d56:
Blocked-by #4264 noted — this PR stays parked until the stack merges in order. Generated by Claude Code |
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.
PR 4 of the #4247 unification stack. Stacked on #4264 → #4263 → #4250.
Summary
Replaces direct reads of `agent_contexts.last_test_*` with a view that derives them from `agent_compliance_runs` — the canonical source PR #4250 unified onto. Adds `triggered_org_id` so the view's per-org scope is accurate.
Why
After PR 1-3, owner test runs land in `agent_compliance_runs` with `triggered_by = 'owner_test'`. But that table only carries `agent_url`, no org dimension. Today's `agent_contexts.last_test_*` columns ARE org-scoped (`agent_contexts` PK is `(organization_id, agent_url)`). To collapse one into the other without losing semantics, runs need a triggering-org dimension.
`triggered_org_id` (nullable; populated only for `triggered_by = 'owner_test'`) closes the gap. Heartbeat / manual / webhook writes have no org, so NULL is correct.
What changes
Backward compat
The legacy `agent_contexts.last_test_*` columns stay. Third-party (non-owner) `recordTest()` writes still update them — that's the session-scoped audit trail PR 3 retained for non-owner runs. The columns become dead-letter once:
This PR is the prep work that makes that final cleanup a no-op for readers.
Stacked on
Merge order: #4250 → #4263 → #4264 → this PR.
Test plan