* fix(scheduler): fence late launch bookkeeping by claim generation An expired launch claim can be requeued and immediately reclaimed by the same scheduler process. Because requeue_claimed_run and fail_launching_run only matched status == launching and the lease owner, the original attempt's late busy-thread requeue or launch failure could still clear the fresh claim's lease and release its global execution-budget slot. Another queued occurrence could then claim that slot while the fresh launch was still in flight. attempt_count already increments on every claim. Capture it from claim_queued_run and pass it to both slot-releasing writes, which now match on the generation as well. The lease-owner check remains; a stale attempt becomes a no-op, while the current generation can still release its own claim. The fence is optional so legacy/custom repositories that do not expose attempt_count keep the previous behavior. Tests: a SQLite/Postgres repository test recovers an expired claim, reclaims it with the same owner, then replays the stale requeue and failure and asserts the fresh row plus its global slot survive. Service tests pin that the claim attempt is passed on both the busy-thread and pre-launch-failure paths. * fix(scheduler): log lost requeue claims after a newer generation wins The busy-thread requeue path discarded requeue_claimed_run()'s boolean, so a generation-guard rejection was indistinguishable from a normal requeue in logs while the attempt no longer owned the occurrence. Capture the result and emit the same lost-launch-claim warning used by the failure path; add a regression test that simulates a newer claim winning. * docs(scheduler): keep persistence guidance within its soft limit Compress the generation-fence note so the shared persistence AGENTS.md chain stays below the soft size limit. * fix(scheduler): omit generation kwarg for legacy claim repos Both late-bookkeeping calls passed expected_attempt_count=None even when the claim had no generation. Custom repositories that still implement the pre-fence signatures reject that keyword with TypeError. Build the kwargs conditionally so the argument is only sent when the claim actually carries a generation; add a legacy repo regression for both the busy-requeue and pre-launch-failure paths. --------- Co-authored-by: Tong-bit-art <Tong-bit-art@users.noreply.github.com>
6.0 KiB
Persistence (deerflow/persistence/)
The SQL engine, repositories and models for runs, threads, scheduled tasks, durable MCP tasks, batches and extension leases, on SQLite and PostgreSQL. Tables, columns and status vocabularies are in the model modules. Schema changes follow the migrations guide. Background: thread lifecycle, MCP control leases, multi-instance settings.
Verify
cd backend
python -m pytest tests -q -k "persistence or repository or scheduled_task or lease or mcp_task or thread_meta"
make test-blocking-io
Set TEST_POSTGRES_URI to run the dialect-sensitive cases on PostgreSQL. A
change to a query, a lock or a JSON predicate is verified on both dialects.
Both dialects
- Write JSON columns through
dumps_json_safe. PostgreSQLjsonbrejectsNaNandInfinity, and SQLite accepts them, so the fault shows only in production. Every engine that writes JSON is wired to it. - JSON filters mean the same in memory, SQLite and PostgreSQL. Missing differs from null, and a boolean differs from an integer. Stored JSON integers can exceed 64 bits, so compare without casting a stored number to a bounded type.
BEGIN IMMEDIATElocks the whole SQLite database for writes. Do slow work before you open the transaction, and lock only to revalidate and publish. Otherwise unrelated writers fail withdatabase is locked.FOR UPDATE SKIP LOCKEDdoes not serialize SQLite readers. A claim on SQLite reserves the writer before it selects.- Managed subagent updates lock before reading
updated_at. Peer registry caches depend on each committed update advancing that timestamp. - A create-or-update by unique key must survive two first writers. Both can
miss the SELECT. Use the dialect's
on_conflict_do_update, or roll back and retry the select-then-write. - Every engine uses the same
search_pathwhendatabase.postgres_schemais set, including the synchronous engines for agents and skills. - URL normalization never drops an authentication or TLS requirement. A conflicting or unsupported option is an error.
- Create the database only on SQLSTATE
3D000. A missing relation, column or role also reads as "does not exist", and aCREATE DATABASEretry would hide it.
Bootstrap locks
- Poll
pg_try_advisory_lock. A blocking acquire is cut off by the statement timeout, and a second instance then fails to start while a peer migrates. - The bootstrap owns its lock until the migration worker thread finishes and the unlock completes. Drain both through cancellation. Otherwise a second bootstrap overlaps a running migration, or a pooled session keeps the lock.
Managed subagent file creation
Stage and fsync complete definitions before publishing them. The RLock is local
to one process, so create claims the destination with os.link and maps only
FileExistsError to a name conflict. Updates retain os.replace. Discard staging
with best-effort cleanup, preserving the original publication outcome.
Leases, claims and terminal writes
- A scan selects candidates; it grants nothing. After you take the row locks,
check status and lease expiry again. The row can have been requeued and
reclaimed while recovery waited, also by the same process.
Late launch writes require the
attempt_countgeneration fromclaim_queued_run. - Check expiry against the database clock after the lock wait. The transaction-start time is already stale. A lease that expires exactly at scan time is still live.
- An owned run write requires the owner and a live lease. This covers terminal
writes and
merge_owned_metadata. Take the change clock before the run row lock; the documented lock order has no exception. - A heartbeat store declares the owner-fenced keywords explicitly. A method
that accepts
**kwargsgives no atomic guarantee, so the manager skips the call and reports it. An unsupported owned call fails closed. - These fences are scoped guards. They cannot tell a deleted and recreated run with the same ID, owner and outcome from the original.
- An
ExecutionFenceis data, not authority. The Gateway binds it to the current plugin and user. When the extension generation is stale, a fenced cancel never falls back to a local cancel. - Lease renewal changes the expiry only. It must not touch
updated_ator overwrite a cancellation that arrived in between.
Scheduled tasks and MCP tasks
- One method owns reactivation:
ScheduledTaskRepository.update. A new path that enables a task goes through it, so the end-condition check cannot be skipped. - At most one active occurrence exists per task, enforced by a unique index. The queue survives a restart, and only a lease-fenced launch calls the Gateway.
- Recovery never takes over a live run. It finalizes an occurrence whose durable run row is already terminal, and it returns expired launch claims to the queue.
- Late launch reconciliation keeps a terminal occurrence terminal when a
newer occurrence owns the active slot; attach only
run_id/started_at, since reviving would violate the one-active index. - The scheduler is single-instance by default. Multi-instance mode needs shared PostgreSQL and the settings that startup validates.
- Run-history filters are occurrence states, not task states. Apply the owner check before reading history and the predicates before pagination.
- A claimed MCP notification always carries a dispatch event. A pending snapshot whose condition resolved before delivery is consumed at claim time.
- The database is the source of truth for durable MCP tasks. The agent only submits. The remote handle and the polling stay out of the agent loop.
- A paused batch is reconciled but not advanced. Lease recovery includes a paused parent with expired items, and skips promotion and claiming.