The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: a ready backend with an unreadable identity attaches within 3 s, not 60 s
Teknium1aa43ca9be90
diff --git a/apps/desktop/electron/backend-discovery.test.ts b/apps/desktop/electron/backend-discovery.test.tsindex d667ea4096c..593595370a2 100644--- a/apps/desktop/electron/backend-discovery.test.ts+++ b/apps/desktop/electron/backend-discovery.test.ts@@ -9,7 +9,7 @@ import { test } from 'vitest'import { spawnOrAttach } from './backend-discovery'import { resolveServedDashboardToken } from './dashboard-token'-import { attachOrReserveSpawn, attachToHostBackend } from './host-backend-attach'+import { attachOrReserveSpawn, attachToHostBackend, HOST_IDENTITY_RETRY_MS } from './host-backend-attach'import { claimHostSpawnGate } from './host-spawn-gate'import { runPrimaryBackendStartup } from './primary-backend-startup'@@ -653,6 +653,37 @@ test('an identity unreadable for the whole budget keeps the token attach, neverassert.equal(takes, 0, 'no spawn reservation may be taken beside a ready backend of unknown identity')})+/**+ * A token-valid ready backend with a persistently unreadable identity attaches+ * within a few seconds, not the whole spawn-gate wait, and the local commit+ * (`git rev-parse HEAD`) is resolved once per call, not once per retry round.+ */+test('a persistently unreadable identity attaches by token within its short retry budget', async () => {+ let clock = 0+ let headReads = 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 () => {+ headReads += 1++ return NEW_COMMIT+ }+ },+ { now: () => clock, read: () => null, take: () => () => {}, sleep: async ms => void (clock += ms) }+ )++ assert.equal('attached' in outcome && outcome.attached.token, 'served-token')+ assert.ok(clock <= HOST_IDENTITY_RETRY_MS, `attached after ${clock} ms, budget ${HOST_IDENTITY_RETRY_MS} ms`)+ assert.equal(headReads, 1, 'the expected commit is resolved once per attach call')+})+test('a definitive non-matching commit is still refused after the budget', async () => {let clock = 0const ledger = JSON.stringify([{ host: '127.0.0.1', pid: 4711, port: 65_238, purpose: 'serve', registered_at: 1 }])diff --git a/apps/desktop/electron/host-backend-attach.ts b/apps/desktop/electron/host-backend-attach.tsindex 222387080a0..d323ee775cd 100644--- a/apps/desktop/electron/host-backend-attach.ts+++ b/apps/desktop/electron/host-backend-attach.ts@@ -20,6 +20,14 @@ import {/** A gate held longer than this belongs to a spawner that never finished. */export const HOST_SPAWN_GATE_STALE_MS = 60_000+/**+ * How long a ready backend whose identity is unreadable is re-read before the+ * token attach. A slow /api/health is ours far more often than not, so it gets+ * a few retries, not the whole spawn-gate budget: a token-valid backend must+ * not hold startup for a minute.+ */+export const HOST_IDENTITY_RETRY_MS = 3_000+export interface AttachedBackend {baseUrl: stringpid: number@@ -210,14 +218,20 @@ export async function attachToHostBackend(options: { isolated: boolean; ledgerPath: string },deps: HostBackendAttachDeps): Promise<AttachedBackend | null> {- const found = await findHostBackend(options, deps)+ const found = await findHostBackend(options, deps, await expectedCommitFor(deps))return found === UNCONFIRMED ? null : found}+/** Resolved once per attach call: `git rev-parse HEAD` must not run per retry round. */+async function expectedCommitFor(deps: HostBackendAttachDeps): Promise<string | null> {+ return nonemptyToken(await deps.expectedCodeIdentity?.().catch(() => null))+}+async function findHostBackend({ isolated, ledgerPath }: { isolated: boolean; ledgerPath: string },deps: HostBackendAttachDeps,+ expectedCommit: string | null,unreadableIdentity: 'unconfirmed' | 'token-attach' = 'unconfirmed'): Promise<AttachedBackend | null | typeof UNCONFIRMED> {const records = parseSpawnLedger(deps.readLedger(ledgerPath))@@ -241,7 +255,6 @@ async function findHostBackend()]- const expectedCommit = nonemptyToken(await deps.expectedCodeIdentity?.().catch(() => null))let unconfirmed = falsefor (const record of ordered) {@@ -293,7 +306,8 @@ export async function attachOrReserveSpawn(gate: HostSpawnGateDeps,{ pollMs = 500, waitBudgetMs = HOST_SPAWN_GATE_STALE_MS }: { pollMs?: number; waitBudgetMs?: number } = {}): Promise<{ attached: AttachedBackend } | { reservation: SpawnReservation }> {- let found = await findHostBackend(options, deps)+ const expectedCommit = await expectedCommitFor(deps)+ let found = await findHostBackend(options, deps, expectedCommit)if (found && found !== UNCONFIRMED) {return { attached: found }@@ -304,8 +318,10 @@ export async function attachOrReserveSpawn(}const deadline = gate.now() + waitBudgetMs+ // An unreadable identity gets its own short budget, counted from first sight.+ let identityDeadline = found === UNCONFIRMED ? gate.now() + HOST_IDENTITY_RETRY_MS : Infinity- while (gate.now() < deadline) {+ while (gate.now() < (found === UNCONFIRMED ? Math.min(deadline, identityDeadline) : deadline)) {const gateState = gate.read()// A ready backend we could not identify may well be ours: re-read it@@ -331,17 +347,21 @@ export async function attachOrReserveSpawn()await gate.sleep(pollMs)- found = await findHostBackend(options, deps)+ found = await findHostBackend(options, deps, expectedCommit)if (found && found !== UNCONFIRMED) {return { attached: found }}++ if (found === UNCONFIRMED) {+ identityDeadline = Math.min(identityDeadline, gate.now() + HOST_IDENTITY_RETRY_MS)+ }}// Only a definitive non-matching commit is a mismatch: a ready backend whose- // identity stayed unreadable for the whole budget keeps the token attach.+ // identity stayed unreadable for its whole retry budget keeps the token attach.if (found === UNCONFIRMED) {- found = await findHostBackend(options, deps, 'token-attach')+ found = await findHostBackend(options, deps, expectedCommit, 'token-attach')if (found && found !== UNCONFIRMED) {return { attached: found }