Skip to content

The agent that grows with you

fix(update): a backend identity unreadable for the whole wait keeps the token attach

Teknium58d86507c347
diff --git a/apps/desktop/electron/backend-discovery.test.ts b/apps/desktop/electron/backend-discovery.test.ts
index d9dae184dc7..d667ea4096c 100644
--- a/apps/desktop/electron/backend-discovery.test.ts
+++ b/apps/desktop/electron/backend-discovery.test.ts
@@ -614,3 +614,59 @@ test('a transient identity read failure retries the running backend instead of s
server.close()
}
})
+
+/**
+ * An identity that stays unreadable for the whole wait budget is "unknown",
+ * never a mismatch: startup keeps the token-validated attach instead of
+ * spawning a second backend beside a correct but slow one.
+ */
+test('an identity unreadable for the whole budget keeps the token attach, never a second backend', async () => {
+ let clock = 0
+ let takes = 0
+ const ledger = JSON.stringify([{ host: '127.0.0.1', pid: 4711, port: 65_238, purpose: 'serve', registered_at: 1 }])
+
+ const outcome = await attachOrReserveSpawn(
+ { isolated: false, ledgerPath: '/ledger.json' },
+ {
+ ...attachDeps(ledger),
+ backendCodeIdentity: async () => {
+ throw new Error('The operation was aborted due to timeout')
+ },
+ expectedCodeIdentity: async () => NEW_COMMIT
+ },
+ {
+ now: () => clock,
+ read: () => null,
+ take: () => {
+ takes += 1
+
+ return () => {}
+ },
+ sleep: async ms => {
+ clock += ms
+ }
+ },
+ { pollMs: 500, waitBudgetMs: 2_000 }
+ )
+
+ assert.equal('attached' in outcome && outcome.attached.token, 'served-token')
+ assert.equal(takes, 0, 'no spawn reservation may be taken beside a ready backend of unknown identity')
+})
+
+test('a definitive non-matching commit is still refused after the budget', async () => {
+ let clock = 0
+ const ledger = JSON.stringify([{ host: '127.0.0.1', pid: 4711, port: 65_238, purpose: 'serve', registered_at: 1 }])
+
+ const outcome = await attachOrReserveSpawn(
+ { isolated: false, ledgerPath: '/ledger.json' },
+ {
+ ...attachDeps(ledger),
+ backendCodeIdentity: async () => OLD_COMMIT,
+ expectedCodeIdentity: async () => NEW_COMMIT
+ },
+ { now: () => clock, read: () => null, take: () => () => {}, sleep: async ms => void (clock += ms) },
+ { pollMs: 500, waitBudgetMs: 2_000 }
+ )
+
+ assert.equal('reservation' in outcome, true)
+})
diff --git a/apps/desktop/electron/host-backend-attach.ts b/apps/desktop/electron/host-backend-attach.ts
index 7b7cd6c06fc..222387080a0 100644
--- a/apps/desktop/electron/host-backend-attach.ts
+++ b/apps/desktop/electron/host-backend-attach.ts
@@ -125,7 +125,8 @@ function nonemptyToken(value: string | null | undefined): string | null {
async function validate(
record: HostBackendRecord,
deps: HostBackendAttachDeps,
- expectedCommit: string | null
+ expectedCommit: string | null,
+ unreadableIdentity: 'unconfirmed' | 'token-attach'
): Promise<AttachedBackend | null | typeof UNCONFIRMED> {
const baseUrl = recordBaseUrl(record)
const servedToken = nonemptyToken(await deps.resolveServedToken(baseUrl).catch(() => null))
@@ -168,7 +169,13 @@ async function validate(
`[attach] ${baseUrl} (pid ${record.pid}) is ready but its code identity is unreadable: ${(error as Error).message}`
)
- return UNCONFIRMED
+ if (unreadableIdentity === 'unconfirmed') {
+ return UNCONFIRMED
+ }
+
+ // Retries exhausted: an unreadable identity is still not a mismatch, so
+ // keep the token-validated attach rather than spawn a second backend.
+ theirs = expectedCommit
}
if (theirs?.toLowerCase() !== expectedCommit.toLowerCase()) {
@@ -210,7 +217,8 @@ export async function attachToHostBackend(
async function findHostBackend(
{ isolated, ledgerPath }: { isolated: boolean; ledgerPath: string },
- deps: HostBackendAttachDeps
+ deps: HostBackendAttachDeps,
+ unreadableIdentity: 'unconfirmed' | 'token-attach' = 'unconfirmed'
): Promise<AttachedBackend | null | typeof UNCONFIRMED> {
const records = parseSpawnLedger(deps.readLedger(ledgerPath))
const decision = spawnOrAttach({ isolated, records, isPidAlive: deps.isPidAlive })
@@ -237,7 +245,7 @@ async function findHostBackend(
let unconfirmed = false
for (const record of ordered) {
- const attached = await validate(record, deps, expectedCommit)
+ const attached = await validate(record, deps, expectedCommit, unreadableIdentity)
if (attached === UNCONFIRMED) {
unconfirmed = true
@@ -330,6 +338,16 @@ export async function attachOrReserveSpawn(
}
}
+ // Only a definitive non-matching commit is a mismatch: a ready backend whose
+ // identity stayed unreadable for the whole budget keeps the token attach.
+ if (found === UNCONFIRMED) {
+ found = await findHostBackend(options, deps, 'token-attach')
+
+ if (found && found !== UNCONFIRMED) {
+ return { attached: found }
+ }
+ }
+
// Preserve the bounded startup fallback when a stale/unreadable gate cannot
// be claimed. The no-op reservation owns no file and therefore removes none.
return { reservation: { release: gate.take() ?? (() => {}) } }