Skip to content

feat: record run owners - #2

Merged
uniqueg merged 5 commits into
masterfrom
feat/gi18-run-owners
Nov 25, 2026
Merged

uniqueg merged 5 commits into
masterfrom
feat/gi18-run-owners

Conversation

@uniqueg

@uniqueg uniqueg commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Summary

Slice S2 of the Conquest auth design (generic-issues#18): runs record their owner, and repository reads are scoped to the owner carried in the request context. Nothing sets an owner yet, so behaviour is unchanged until token validation (next slice, S3) is switched on.

  • migrations/000009_run_owner: runs.owner TEXT NULL, runs.auth_context JSONB NULL (identifiers only, never tokens), partial index (owner, created_at DESC, run_id DESC). Tasks get no column: they belong to their run.
  • New package internal/ownership: an Owner in context.Context (NewContext, FromContext). No change to the repository interfaces, the controller or the handlers' signatures, to keep the edit to upstream code small (D-406).
  • internal/repository/pg: in a scoped context, Create stamps the owner; Get, GetStatus, List on runs and Get, GetByTESID, List, ListByRunID on tasks add an owner predicate (ownership.go). NULL owners never match, so runs from before auth belong to nobody (D-414). Unscoped contexts (auth off, reconcilers, dispatcher, runner callback routes) see everything, as today. Writes stay unscoped: callers read through a scoped query first.
  • internal/api/tes: public CreateTask with tag conquest_run_id now answers 404 when that run can't be read (before: the read error was ignored, and the task was attached to the run anyway, or failed later on the foreign key with 500). With owners in place this closes F3 of the design (task injection into someone else's run).
  • CI: new job integration ("Integration tests") with a PostgreSQL 16 service: migrate up, down 1, up, then go test -tags integration ./internal/repository/pg/ (the existing dispatch integration test now runs in CI too). Added to the CI passed gate's needs, so the ruleset needs no change.
  • README: "Run owners" section and how to run the integration tests.

Closes elixir-cloud-aai/generic-issues#145. Part of elixir-cloud-aai/generic-issues#18 (closed by the last slice).

Verification

  • go build ./..., go vet ./..., go vet -tags integration ./internal/repository/pg/, go test -race -count=1 ./...: pass locally (Go 1.25.5).
  • New unit tests: internal/ownership, internal/repository/pg/ownership_test.go (predicates and placeholder numbering), internal/api/tes/run_tag_test.go (fails without the handler fix: 200 instead of 404/500).
  • Integration tests (ownership_integration_test.go: stamping, scoped Get/GetStatus/GetByTESID/ListByRunID for owner, other owner, empty owner, NULL-owner run, unscoped; keyset pages per owner for runs, tasks and ListByRunID) did not run locally (no PostgreSQL available to the agent); they run in the new CI job.
  • gofmt -s -l reports only internal/backend/k8s/workspace.go, which is untouched (local gofmt version differs from CI's).
  • Reviews: adversarial and quality reviewer, two rounds; all high/medium findings fixed.
  • Sourcery: 2 findings (owner only from the context; refuse an empty scoped owner) fixed in 40d9968, threads resolved.
  • CI: all checks green on 40d9968 (run 45575065579), including the new Integration tests job (the first run found the page-token bug below).

Unrelated fixes

  • fix: decode page tokens to timestamps (separate commit): List/ListByRunID passed the page token's Unix nanoseconds as an integer where created_at (timestamptz) is compared; pgx can't encode that, so every second page of runs or tasks answered 500. Found by the new integration tests; parseCursor now returns a time.Time (unit test added).

Declined review findings

  • Index for scoped TaskStore.List (low): the owner predicate on public TES ListTasks is an EXISTS per task without a supporting index. Fine at current volumes; revisit (join via idx_runs_owner_created or an owner column on tasks) if task tables grow large.
  • CI job display name and go install of migrate (low): kept; name: follows the org's job naming rule, and go install …@v4.17.0 is checksum-verified via the Go module proxy (same version as docker/server/Dockerfile).

Open questions

None.

🤖 Generated with Claude Code

https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5

uniqueg and others added 3 commits November 25, 2026 05:25
Add an owner and an auth context to runs (migration 000009) and scope
repository reads to the owner carried in the request context (new
package internal/ownership). New runs are stamped with that owner;
Get, GetStatus and List on runs and Get, GetByTESID, List and
ListByRunID on tasks return only that owner's runs and their tasks.
Runs without an owner never match an owner.

Nothing sets an owner yet, so behaviour is unchanged: an unscoped
context sees everything, as internal callers keep doing. Token
validation (next slice) will scope API requests.

Add an integration CI job that runs the PostgreSQL repository tests
and rolls the newest migration down and up again.

Part of elixir-cloud-aai/generic-issues#18.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
- TES CreateTask answers 404 when the run in conquest_run_id can't be
  read (missing, or hidden by the owner scope) instead of attaching the
  task to it; other store errors give 500.
- Owner filters append to the query's arguments and number their
  placeholder from them, instead of hand-counted numbers.
- Run.Owner and Run.AuthContext are never serialised.
- Integration tests cancel their tasks, so that they stay out of the
  dispatch queue; cover paged ListByRunID and an owner without runs.
- README: scoping covers reads only; runnable integration test snippet.

Part of elixir-cloud-aai/generic-issues#18.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
@sourcery-ai

sourcery-ai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Schema and migrations

Add nullable run owner and authentication metadata columns, plus an owner-oriented index, so runs can be persisted and queried by ownership.

  • migrations/000009_run_owner.down.sql L1-7
  • migrations/000009_run_owner.up.sql L1-18

Data structures

Add non-serialized owner and authentication context fields to the run model, and define the context value used to carry caller ownership.

  • internal/domain/run.go L37-42
  • internal/ownership/ownership.go L1-40

API

Make TES task creation reject a tagged run that is missing, unreadable under the caller scope, or unavailable due to a store error.

  • internal/api/tes/handlers.go L167-178

Implementation

Add shared owner predicates for run and task queries, including argument-safe placeholder numbering and owner-based task-to-run matching.

  • internal/repository/pg/ownership.go L1-42

Stamp newly created runs only from scoped context, persist and scan ownership metadata, and apply ownership filters to run reads and paginated listing while decoding cursors as timestamps.

  • internal/repository/pg/runs.go L31-45, L106-111, L355-370 (+11 small edits)

Apply ownership predicates to task lookups and list queries, including run-specific task pagination.

  • internal/repository/pg/tasks.go L60-68, L81-86, L92-97 (+2 small edits)

Define the repository error returned when a scoped run creation has an empty owner ID.

  • internal/repository/pg/errors.go L13-16

Configuration

Run PostgreSQL integration tests in CI, verify the latest migration can roll back and reapply, and require the integration job for the CI success gate.

  • .github/workflows/ci.yaml L49-89, L114

Documentation

Document owner-scoped run behavior and explain how to run the PostgreSQL integration tests locally.

  • README.md L272-286, L473-484

Tests

Test owner context behavior, PostgreSQL owner filters and empty-owner rejection, and TES handling of readable, unreadable, and failed run lookups.

  • internal/api/tes/run_tag_test.go L1-70
  • internal/ownership/ownership_test.go L1-32
  • internal/repository/pg/ownership_test.go L1-62

Exercise owner stamping, scoped and unscoped repository reads, and owner-specific pagination against PostgreSQL; also verify cursor parsing and malformed tokens.

  • internal/repository/pg/cursor_test.go L1-28
  • internal/repository/pg/ownership_integration_test.go L1-269

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

List and ListByRunID compared created_at (timestamptz) with the token's
Unix nanoseconds as an integer, which pgx can't encode: every second
page of runs or tasks failed with 500. parseCursor now returns a
time.Time. Found by the new integration tests; unrelated to run owners.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
@uniqueg
uniqueg marked this pull request as ready for review November 25, 2026 07:37

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="internal/repository/pg/runs.go" line_range="36-41" />
<code_context>
+// and run.Owner and run.AuthContext are set to what was stored. An empty
+// owner ID is stored as NULL: such a run belongs to nobody.
 func (s *RunStore) Create(ctx context.Context, run *domain.Run) error {
+	if owner, ok := ownership.FromContext(ctx); ok {
+		run.Owner, run.AuthContext = owner.ID, owner.Context
+	}
+	var owner *string
+	if run.Owner != "" {
+		owner = &run.Owner
+	}
 	wsKind, wsClaim, wsAccess, wsPV := flattenWorkspaceBinding(run.Workspace)
</code_context>
<issue_to_address>
**Unscoped runs can be assigned owners**

When an unscoped repository caller supplies a run whose `Owner` is non-empty, an unscoped caller that passes a run with a non-empty `run.Owner` leaves that value intact, and `Create` persists it. The run then appears to belong to that owner despite having been created without an owner context.

Clear owner and auth context for unscoped creates, or reject pre-populated ownership unless it came from the scoped context.
</issue_to_address>

### Comment 2
<location path="internal/repository/pg/runs.go" line_range="40" />
<code_context>
+		run.Owner, run.AuthContext = owner.ID, owner.Context
+	}
+	var owner *string
+	if run.Owner != "" {
+		owner = &run.Owner
+	}
</code_context>
<issue_to_address>
**Empty owner creates inaccessible runs**

When a scoped context is created with an empty owner ID, such as when the authentication layer receives a missing identity claim, `RunStore.Create` stores an empty scoped owner as SQL `NULL`, while `runOwnerFilter` later compares `owner = ''`; the insert succeeds but the creating caller cannot read the run, so the run becomes inaccessible to that caller.

Reject creation with a scoped empty owner ID before inserting the run.

Also at `internal/repository/pg/ownership.go:28-29`.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 2 findings to address first, and when a request context carries an owner, the new repository filters determine which runs and tasks that caller can read, and the TES run-tag check relies on that result; a missed or incorrect filter could expose another owner’s records. Reverting removes the behavior but cannot undo data already exposed, though the README notes that no application path currently sets an owner.

Blocking findings: internal/repository/pg/runs.go:41, internal/repository/pg/runs.go:40


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread internal/repository/pg/runs.go Resolved
Comment thread internal/repository/pg/runs.go Resolved
Create ignores an Owner the caller set on the run and refuses a context
scoped to an empty owner ID (ErrEmptyOwner), whose caller could never
read the run. Addresses Sourcery review findings.

Part of elixir-cloud-aai/generic-issues#18.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
@uniqueg
uniqueg merged commit 019ecf7 into master Nov 25, 2026
17 checks passed
@uniqueg
uniqueg deleted the feat/gi18-run-owners branch November 25, 2026 11:01
uniqueg added a commit that referenced this pull request Nov 25, 2026
Keep both integration jobs (PostgreSQL from #2, storage) and both
README paragraphs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Conquest: record run owners and scope reads to them

1 participant