fix(secrets): collapse legacy scope duplicates on rollback and verify repo worktrees

Rolling back the scope-index migration now keeps the newest of a colliding global and Assistant-repo secret, matching the legacy last-write-wins upsert, so the legacy unique index can be recreated. The repo-env fallback now returns a registered repo only when the directory shares its Git metadata, so a separate clone of the same origin no longer receives the registered repo's secrets; external worktrees still resolve. The migration uses a named export.
This commit is contained in:
Chris Scott committed 2026-10-24 11:06:30 -04:00
1 parent e3557f8eec
commit 21c60edd09
6 files changed
+116 -10

No files matched your search

@@ -11,14 +11,47 @@ function rebuildScopeIndex(db: Database, globalScopeId: number): void {
)
}
const migration: Migration = {
/**
* Deletes the rows the legacy scope key cannot hold.
*
* The legacy index merged global rows (`repo_id IS NULL`) with the Assistant repository's scope
* (`repo_id = 0`), and the old upsert let the last write win, so the newest row of each merged
* group is the state legacy code would hold. Ordering by `updated_at`, then `id`, keeps that row
* and deletes the rest, which lets the legacy unique index be recreated.
*/
function collapseLegacyScopeDuplicates(db: Database): void {
db.run(`
DELETE FROM secrets
WHERE (repo_id IS NULL OR repo_id = ${LEGACY_GLOBAL_SCOPE_ID})
AND EXISTS (
SELECT 1
FROM secrets AS newer
WHERE (newer.repo_id IS NULL OR newer.repo_id = ${LEGACY_GLOBAL_SCOPE_ID})
AND newer.kind = secrets.kind
AND newer.environment = secrets.environment
AND newer.name = secrets.name
AND (
newer.updated_at > secrets.updated_at
OR (newer.updated_at = secrets.updated_at AND newer.id > secrets.id)
)
)
`)
}
export const secretsGlobalScopeIndexMigration: Migration = {
id: '202610181410-secrets-global-scope-index',
up(db) {
rebuildScopeIndex(db, GLOBAL_SECRETS_SCOPE_ID)
},
down(db) {
rebuildScopeIndex(db, LEGACY_GLOBAL_SCOPE_ID)
db.run('BEGIN TRANSACTION')
try {
collapseLegacyScopeDuplicates(db)
rebuildScopeIndex(db, LEGACY_GLOBAL_SCOPE_ID)
db.run('COMMIT')
} catch (error) {
db.run('ROLLBACK')
throw error
}
},
}
export default migration
+2 -2
View File
@@ -38,7 +38,7 @@ import migration202610181300 from './202610181300-session-server-scope'
import migration202610181310 from './202610181310-multi-run-targets'
import migration202610181320 from './202610181320-remote-schedule-targets'
import migration202610181400 from './202610181400-secrets'
import migration202610181410 from './202610181410-secrets-global-scope-index'
import { secretsGlobalScopeIndexMigration } from './202610181410-secrets-global-scope-index'
export const allMigrations: Migration[] = [
migration001,
@@ -80,5 +80,5 @@ export const allMigrations: Migration[] = [
migration202610181310,
migration202610181320,
migration202610181400,
migration202610181410,
secretsGlobalScopeIndexMigration,
]