Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions mobile/src/tasks/worktree-create-retry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,44 @@ describe('createWorktreeWithNameRetry', () => {
expect(attempts.map((attempt) => attempt.params.name)).toEqual(['nautilus-2', 'nautilus-3'])
})

it('routes to the name the host actually created, not the one this client proposed', async () => {
// The host advances past a retired cwd on its own, so the created workspace can carry a later
// candidate than the one sent. Routing on the proposal titles the workspace wrongly until the
// live-name hook catches up.
const client = {
sendRequest: async () => ({
id: '1',
ok: true,
result: { worktree: { id: 'wt-1', displayName: 'nautilus-4' } },
_meta: { runtimeId: 'r' }
})
} as unknown as RpcClient

await expect(
createWorktreeWithNameRetry({
client,
baseName: 'nautilus',
nameWasGenerated: true,
buildParams: (name) => ({ repo: 'id:r', name, nameWasGenerated: true }),
supportsIdempotentCutoverRetry: false
})
).resolves.toEqual({ worktreeId: 'wt-1', name: 'nautilus-4' })
})

it('falls back to the proposed name when the host answers without one', async () => {
const attempts: Attempt[] = []
const client = scriptedClient([{ id: 'wt-1' }], attempts)

await expect(
createWorktreeWithNameRetry({
client,
baseName: 'otter',
buildParams: (name) => ({ repo: 'id:r', name }),
supportsIdempotentCutoverRetry: false
})
).resolves.toEqual({ worktreeId: 'wt-1', name: 'otter' })
})

it('does not replay an ambiguous cutover when the host lacks idempotency support', async () => {
const attempts: Attempt[] = []
const client = scriptedClient([{ throws: new LogicalClientCutoverError() }], attempts)
Expand Down
13 changes: 11 additions & 2 deletions mobile/src/tasks/worktree-create-retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,17 @@ export async function createWorktreeWithNameRetry(
supportsIdempotentCutoverRetry
)
if (response.ok) {
const result = (response as RpcSuccess).result as { worktree: { id: string } }
return { worktreeId: result.worktree.id, name: candidateName }
const result = (response as RpcSuccess).result as {
worktree: { id: string; displayName?: unknown }
}
// Why not `candidateName`: the host advances past a retired cwd on its own, so the workspace
// it created can carry a later candidate than the one proposed here. Routing on the proposal
// titles the new workspace wrongly until the live-name hook corrects it.
const createdName = result.worktree.displayName
return {
worktreeId: result.worktree.id,
name: typeof createdName === 'string' && createdName ? createdName : candidateName
}
}
lastError = response.error.message
if (!isRetryableWorktreeCreateConflict(lastError ?? '')) {
Expand Down
33 changes: 32 additions & 1 deletion mobile/src/worktree/use-retired-worktree-names.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,20 @@ function mountNames() {
renderer.update(createElement(Probe, props))
})
},
/** Full RPC envelopes on purpose: a bare `{ result }` is not what the transport delivers, and
* faking one is what let a resolved *error* envelope read as an empty registry. */
async settle(
index: number,
retiredNamesByRepo: Record<string, unknown>,
retiredNameTiersByRepo: Record<string, unknown> = {}
) {
await act(async () => {
pending[index]!.resolve({ result: { retiredNamesByRepo, retiredNameTiersByRepo } })
pending[index]!.resolve({
id: '1',
ok: true,
result: { retiredNamesByRepo, retiredNameTiersByRepo },
_meta: { runtimeId: 'r' }
})
await Promise.resolve()
})
},
Expand All @@ -61,6 +68,18 @@ function mountNames() {
pending[index]!.reject(new Error('host unreachable'))
await Promise.resolve()
})
},
/** A host-side error: the RPC *resolves* carrying an error envelope, it does not reject. */
async refuse(index: number) {
await act(async () => {
pending[index]!.resolve({
id: '1',
ok: false,
error: { code: 'internal_error', message: 'registry unavailable' },
_meta: { runtimeId: 'r' }
})
await Promise.resolve()
})
}
}
}
Expand Down Expand Up @@ -111,6 +130,18 @@ describe('useRetiredWorktreeNames', () => {
expect(probe.names).toEqual(['nautilus'])
})

// Regression: an RPC error envelope resolves rather than rejecting, so it slipped past the
// `.catch` and was read as "this repo has retired nothing", blanking the cache.
it('keeps previously loaded names when the host answers with an error', async () => {
const probe = mountNames()
await probe.settle(0, { 'repo-1': ['nautilus'] }, { 'repo-1': 2 })

probe.rerender({ repoId: 'repo-1', refreshKey: 'b' })
await probe.refuse(1)

expect(probe.registry).toEqual({ exhaustedTiers: 2, names: ['nautilus'] })
})

// Regression: the effect used to depend on [client, repoId] only, so a workspace created while
// the sheet was open never made it into the pool.
it('refetches when the workspace list changes', async () => {
Expand Down
7 changes: 4 additions & 3 deletions mobile/src/worktree/use-retired-worktree-names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ export function useRetiredWorktreeNames(
}
void client
.sendRequest('worktree.listRetiredNames', { repo: `id:${activeRepoId}` })
// Why the envelope check and not just `.catch`: an RPC error *resolves* carrying
// `ok: false`, so a host-side failure used to read as "this repo has retired nothing" and
// blank the cache — un-retiring every name until the next successful refresh.
.then((response) =>
settle(
readRetiredNameRegistryForRepo((response as { result?: unknown }).result, activeRepoId)
)
settle(response.ok ? readRetiredNameRegistryForRepo(response.result, activeRepoId) : null)
)
.catch(() => settle(null))
return () => {
Expand Down
Loading