[P2] fix(persistence): tolerate null worktreeMeta entries in linked-item normalization (#11614)

* fix(persistence): tolerate null worktreeMeta entries in linked-item normalization

Co-authored-by: Orca <help@stably.ai>

* fix(persistence): drop null worktreeMeta entries instead of retaining them

gcStaleWorktreeMeta never reaps a timestamp-less key, so a skipped null meta
would persist forever and reach unguarded consumers (reassignSshTargetId,
push-target cleanup, disconnected-SSH listing, usage metadata).

Co-authored-by: Orca <help@stably.ai>

* fix(persistence): drop every corrupt worktreeMeta shape with its lineage rows

Widen the guard past null so scalars/arrays can't survive load into a
Record<string, WorktreeMeta> that consumers trust, self-heal a whole-map
"worktreeMeta": null instead of leaning on gcStaleWorktreeMeta running next,
and clear the two lineage companions the way gcStaleWorktreeMeta and
removeWorktreeMeta already do.

Co-authored-by: Orca <help@stably.ai>

* fix(persistence): persist whole-map metadata repairs

---------

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Neil
2026-07-30 17:55:54 -07:00
committed by GitHub
co-authored by Orca
parent 3b7ea59c5b
commit 8e9dc4ecf2
2 changed files with 78 additions and 3 deletions
+57 -2
View File
@@ -4930,6 +4930,59 @@ describe('Store', () => {
expect(store.getWorktreeMeta('wt-malformed')?.linkedTaskSourceContext).toBeNull()
})
it('drops corrupt worktreeMeta entries while still normalizing valid siblings', async () => {
writeDataFile({
schemaVersion: 1,
repos: [],
worktreeLineageById: { 'r1::/tmp/wt': { parentWorktreeId: 'wt-sibling' } },
workspaceLineageByChildKey: {
'worktree:r1::/tmp/wt': { parentWorkspaceKey: 'worktree:wt-sibling' }
},
worktreeMeta: {
'r1::/tmp/wt': null,
'r1::/tmp/scalar': 5,
'wt-sibling': {
linkedWorkItem: {
provider: 'jira',
type: 'issue',
number: 0,
title: 'ORCA-123 Link Jira',
url: 'https://company.atlassian.net/browse/ORCA-123',
jiraIdentifier: 'ORCA-123'
},
linkedTaskSourceContext: {
kind: 'task-source',
provider: 'jira',
projectId: 'project-1',
hostId: 'local',
accountLabel: 44,
providerIdentity: {
provider: 'jira',
siteId: 'site-1',
siteUrl: 'https://company.atlassian.net',
projectKey: 'ORCA'
}
}
}
}
})
const store = await createStore()
expect(store.getWorktreeMeta('wt-sibling')?.linkedWorkItem?.jiraIdentifier).toBe('ORCA-123')
expect(store.getWorktreeMeta('wt-sibling')?.linkedTaskSourceContext).toBeNull()
// Corrupt entries must not survive: gcStaleWorktreeMeta keeps timestamp-less keys, and downstream
// consumers deref worktreeMeta values unguarded (also keeps a rollback to an older build loadable).
expect(store.getAllWorktreeMeta()).not.toHaveProperty('r1::/tmp/wt')
expect(store.getAllWorktreeMeta()).not.toHaveProperty('r1::/tmp/scalar')
expect(store.getWorktreeLineage('r1::/tmp/wt')).toBeUndefined()
store.flush()
const persisted = readDataFile() as PersistedState
expect(persisted.worktreeMeta).not.toHaveProperty('r1::/tmp/wt')
expect(persisted.worktreeMeta).not.toHaveProperty('r1::/tmp/scalar')
expect(persisted.workspaceLineageByChildKey).not.toHaveProperty('worktree:r1::/tmp/wt')
})
it('creates and updates folder workspaces from folder-backed project groups', async () => {
const store = await createStore()
const group = store.createProjectGroup({
@@ -6046,10 +6099,12 @@ describe('Store', () => {
})
})
it('tolerates a null worktreeMeta map in the durable file', async () => {
writeDataFile({ worktreeMeta: null })
it.each([null, [], 5])('repairs a corrupt worktreeMeta map (%#)', async (worktreeMeta) => {
writeDataFile({ worktreeMeta })
const store = await createStore()
expect(store.getAllWorktreeMeta()).toEqual({})
store.flush()
expect((readDataFile() as PersistedState).worktreeMeta).toEqual({})
})
// ── GitHub cache sidecar ───────────────────────────────────────────
+21 -1
View File
@@ -425,7 +425,27 @@ function gcStaleWorktreeMeta(state: PersistedState): number {
function normalizeWorktreeLinkedItemMetadata(state: PersistedState): boolean {
let changed = false
for (const meta of Object.values(state.worktreeMeta ?? {})) {
const rawWorktreeMeta = state.worktreeMeta as unknown
if (
typeof rawWorktreeMeta !== 'object' ||
rawWorktreeMeta === null ||
Array.isArray(rawWorktreeMeta)
) {
state.worktreeMeta = {}
changed = rawWorktreeMeta !== undefined
}
for (const [key, meta] of Object.entries(state.worktreeMeta)) {
// Why: hand-corrupted non-object entries are a real input class; drop them here because gcStaleWorktreeMeta
// keeps timestamp-less keys forever and every downstream consumer trusts the Record<string, WorktreeMeta> type.
if (typeof meta !== 'object' || meta === null || Array.isArray(meta)) {
delete state.worktreeMeta[key]
// Companions go with it, matching gcStaleWorktreeMeta/removeWorktreeMeta; a stranded lineage row would
// otherwise re-attach to a worktree recreated at the same repoId::path.
delete state.worktreeLineageById[key]
delete state.workspaceLineageByChildKey[worktreeWorkspaceKey(key)]
changed = true
continue
}
const linkedWorkItem = normalizeWorkspaceLinkedItem(meta.linkedWorkItem)
const sourceContext = normalizeStoredTaskSourceContext(meta.linkedTaskSourceContext)
const linkedTaskSourceContext = isWorkspaceLinkedItemSourceContextMatch(