Repository navigation
perf: index lease references and scope hot inserts to the agent row - #646
Closed
ksarangmath wants to merge 1 commit into
Closed
ksarangmath wants to merge 1 commit into
ksarangmath wants to merge 1 commit into
Conversation
- Index tool_calls.runtime_lock_id so the foreign-key check on every lease delete no longer scans all tool-call history. - Index received content inputs per agent for the execution backlog. - Replace the org and project references on model contexts, processes and process actions with one org-qualified reference to the agent row, which the writer already holds locked, and drop the redundant project reference on agent inputs. Inserts stop key-share locking the shared org and project rows. Config and model-revision references stay. - Build indexes concurrently and validate every replacement before dropping the old constraints. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Contributor
Author
|
Folding this into #631: the changes move into its cutover migration 052. |
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
Stacked on #631. Migration 054 removes two per-step costs found by the agent-execution load test, and drops redundant foreign keys on hot insert paths.
tool_calls.runtime_lock_idreferencesagent_runtime_locks(id), but its only index was partial (state = 'running') and led withagent_id. So the foreign-key check on every lease delete was a sequential scan oftool_calls, and every release got slower as history grew.tool_calls(runtime_lock_id) WHERE runtime_lock_id IS NOT NULL, it reads 2 buffers and takes 0.1 ms, regardless of history.model_call_contexts,processesandprocess_actionsbecome one org-qualified reference to the agent row:(org_id, project_id, agent_id) → agents(org_id, project_id, id). The writer already holds that row locked.agent_inputs.project_id → projectsis dropped, because(project_id, agent_id) → agentsalready guarantees it.Cross-org and cross-project rows are still rejected by the database for every replaced constraint. No Go code or queries change.
Migration
The migration is non-transactional so its indexes can be built concurrently. In order:
NOT VALID.Validation holds no exclusive table lock; adding and dropping constraints take brief metadata locks. The migration is not atomic. If it is interrupted mid-way, an invalid concurrent index may need dropping before a rerun.
Testing
TestExecutionLeaseDeleteHistoryBound: lease deletes use the index, and their cost stays flat from 1,000 to 100,000 tool calls. A live reference still blocks the delete.TestExecutionHotPathPlans: the backlog uses the new index within a bounded number of buffers, including under a generic plan.TestContextInsertAvoidsSharedScopeLocks: overlapping context inserts create no MultiXact on the org or project row.TestExecutionHotPathScopeConstraints: wrong-org and wrong-project rows are rejected for contexts, processes, process actions and inputs.TestHotPathMigrationConcurrentBuildAndValidation: on 50,000 agents and contexts, validation lets concurrent writers proceed.make golangci-lint,make verify-static, and the sqlc, migration and migration-compat targets.The end-to-end load benchmark is still to run, on a quiet machine. The lease fix matters most on long histories, which the load test doesn't build up, because it starts from a fresh database every run.
🤖 Generated with Claude Code