Skip to content

store: implement TaskApplier in memory and postgres backends - #630

Merged
siyuanfoundation merged 2 commits into
google:mainfrom
siyuanfoundation:store
Nov 17, 2026
Merged

siyuanfoundation merged 2 commits into
google:mainfrom
siyuanfoundation:store

Conversation

@siyuanfoundation

@siyuanfoundation siyuanfoundation commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Implements store.TaskApplier (Apply and GetTaskRow) across the PostgreSQL and in-memory store backends (building on #591, Sub-Workstream A of the M0 runner plan) while keeping all existing store.Store task methods (SaveTask, GetTask, ListTasks, UpdateTaskStatus, SetTaskDeleteTime, DeleteTask) working unchanged.

Key Changes

  • Shared row normalization (internal/store/task_row_normalize.go): Added PrepareTaskRowForApply and OverlayTaskGeneration so both backends enforce identical metadata defaulting, store.StoredStatus scrubbing (#515), and Generation advancement (1 on insert, stored.Generation + 1 on update).
  • PostgreSQL backend (internal/store/postgres):
    • Added the 9 additive TaskRow columns (generation, actor_uid, runnable, active_run_id, run_seq, runner_token_hash, runner_token_confirmed, claim_nonce, last_acked_runner_seq) to tasks via ALTER TABLE tasks ADD COLUMN IF NOT EXISTS in schema.sql (schema.go updated alongside).
    • Implemented GetTaskRow and transactional Apply (SELECT ... FOR UPDATE, single retry on concurrent insert 23505 unique violation so losing creators observe the winner's row, and saved_at list-order preservation on updates).
  • In-memory backend (internal/store/memory): Backed MemoryStore.tasks with *store.TaskRow and implemented GetTaskRow and Apply.
  • Tests (internal/store/{memory,postgres}/store_test.go): Added TestTaskApplier covering missing-row no-ops, nil-Task rejection, 8-way concurrent insert races, StoredStatus scrubbing of live vs. AX-owned states, in-place current mutation protection, saved_at list ordering on updates, and DeleteTaskRow pointer-identity deletion.

Testing

  • make verify (buf format, buf lint, buf generate, go mod tidy, go fmt, golangci-lint — 0 issues)
  • go build ./cmd/ax ./cmd/ax-server ./cmd/ax-task-runner
  • AX_TEST_POSTGRES_URL='postgres://...' go test -v -race -count=1 ./internal/store/postgres/... (postgres:17-alpine — all 9 tests pass)
  • go test -race -count=1 ./... (all test packages pass)

@yongruilin

Copy link
Copy Markdown
Collaborator

@siyuanfoundation

Copy link
Copy Markdown
Collaborator Author

Have you tried directly stacked on common-ground branch? docs.github.com/en/pull-requests/get-started/about-stacked-prs

gh stack only supports same-repo PRs. This PR is created from my fork

- Add additive runner control columns (generation, actor_uid, runnable, active_run_id, run_seq, runner_token_hash, runner_token_confirmed, claim_nonce, last_acked_runner_seq) to tasks in schema.sql.
- Add PrepareTaskRowForApply and OverlayTaskGeneration helpers in internal/store so both backends normalize metadata, scrub Task.Status via StoredStatus, and advance Generation identically.
- Implement TaskApplier (Apply and GetTaskRow) on memory.MemoryStore and postgres.Store, preserving saved_at list ordering on updates and retrying concurrent insert races.
- Add unit and Postgres integration tests covering concurrent inserts, StoredStatus scrubbing, no-op current returns, saved_at ordering, and DeleteTaskRow sentinel deletion.

Signed-off-by: siyuanfoundation <[email protected]>
@siyuanfoundation

Copy link
Copy Markdown
Collaborator Author

/cc @mayawang

@siyuanfoundation

Copy link
Copy Markdown
Collaborator Author

/cc @itssimrank

@mayawang mayawang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, solid locking and the migration is rollout-safe. One thing to fix before the runner handler starts calling Apply: in Postgres, legacy SaveTask/UpdateTaskStatus bump Status.Generation in the JSON but not the generation column, and scanTaskRow then overwrites the JSON value with the column (OverlayTaskGeneration). So a task at gen 3 reads back as 1, and the first Apply writes 2. Taking max(column, json) in scanTaskRow would fix it, plus a test for legacy write then Apply. Memory already gets this right.

Signed-off-by: siyuanfoundation <[email protected]>
@siyuanfoundation

Copy link
Copy Markdown
Collaborator Author

LGTM, solid locking and the migration is rollout-safe. One thing to fix before the runner handler starts calling Apply: in Postgres, legacy SaveTask/UpdateTaskStatus bump Status.Generation in the JSON but not the generation column, and scanTaskRow then overwrites the JSON value with the column (OverlayTaskGeneration). So a task at gen 3 reads back as 1, and the first Apply writes 2. Taking max(column, json) in scanTaskRow would fix it, plus a test for legacy write then Apply. Memory already gets this right.

Good catch! Fixed in the 2nd commit

@siyuanfoundation
siyuanfoundation merged commit d128a4b into google:main Nov 17, 2026
10 checks passed
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.

3 participants