Repository navigation
fix(machines): show a dead link within seconds and queue new conversations for an offline machine #721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
fix(machines): show a dead link within seconds and queue new conversations for an offline machine #721
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
65781a2
fix(machines): show a dead link within seconds and queue new conversa…
klNuno 78a150e
fix(machines): keep queued drafts safe across crashes and deletes, an…
klNuno 59136cc
fix(machines): repeat a draft's start exactly, keep agent boxes onlin…
klNuno 5b7f893
fix(machines): keep a draft's asked start across restarts and never t…
klNuno c0e546b
fix(machines): let a refused draft start be taken back
klNuno 6cd6a0f
fix(machines): unlock a draft start whose conversation was deleted
klNuno 7b1f6c5
fix(machines): send a draft again under a fresh id once its conversat…
klNuno 14d8a31
docs(machines): say a deleted start goes again under a fresh id
klNuno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import type { RpcParams, ThreadId, ThreadSummary } from '@boite/contracts'; | ||
| import { invalidParams, refused } from '../errors.ts'; | ||
|
|
||
| /** How long a creation's request id answers with the thread it made: well past a reconnect. */ | ||
| const RECEIPT_MS = 60 * 60_000; | ||
| /** Receipts kept at most; the oldest leaves first. */ | ||
| const RECEIPT_LIMIT = 512; | ||
| const REQUEST_ID_MAX = 128; | ||
|
|
||
| type Receipt = { expiresAt: number; key: string; created: Promise<ThreadId> }; | ||
|
|
||
| /** | ||
| * `threads.create` under a `clientRequestId`. A client that lost the answer | ||
| * with its connection asks again under the same id and gets the conversation | ||
| * the first call made, not a second one: a conversation queued on a device | ||
| * while its machine was away starts once however often the link drops on the | ||
| * way. Kept in memory, so a core restart forgets them. | ||
| */ | ||
| export class CreationRequests { | ||
| private readonly receipts = new Map<string, Receipt>(); | ||
|
|
||
| async once(params: RpcParams<'threads.create'>, create: () => Promise<ThreadSummary>, lookup: (threadId: ThreadId) => ThreadSummary | null): Promise<ThreadSummary> { | ||
| const id = params.clientRequestId; | ||
| if (id === undefined) return create(); | ||
| if (typeof id !== 'string' || id.length === 0 || id.length > REQUEST_ID_MAX) { | ||
| throw invalidParams(`clientRequestId must be a string of 1 to ${REQUEST_ID_MAX} characters`, { field: 'clientRequestId', expected: `1 to ${REQUEST_ID_MAX} characters` }); | ||
| } | ||
| // Everything that shapes the conversation: a repeat that differs is another request, refused. | ||
| const key = JSON.stringify([params.projectId, params.providerId, params.accountId, params.title ?? null, params.cwd ?? null, | ||
| params.model ?? null, params.effort ?? null, params.speed ?? null, params.permissionMode ?? 'default', params.worktree ?? null, | ||
| params.incognito === true, params.context ?? null]); | ||
| const now = Date.now(); | ||
| for (const [stale, receipt] of this.receipts) if (receipt.expiresAt <= now) this.receipts.delete(stale); | ||
| const previous = this.receipts.get(id); | ||
| if (previous) { | ||
| if (previous.key !== key) throw refused('clientRequestId was already used for another conversation', { field: 'clientRequestId', expected: 'a new id for a different conversation' }); | ||
| const threadId = await previous.created; | ||
| const thread = lookup(threadId); | ||
| if (thread === null) throw refused('the conversation this request started was deleted since', { field: 'clientRequestId', threadId, reason: 'deleted' }); | ||
| return thread; | ||
| } | ||
| const made = create(); | ||
| const receipt: Receipt = { expiresAt: now + RECEIPT_MS, key, created: made.then(thread => thread.id) }; | ||
| this.receipts.set(id, receipt); | ||
| while (this.receipts.size > RECEIPT_LIMIT) this.receipts.delete(this.receipts.keys().next().value!); | ||
| // A creation that failed made nothing: the same id may try again. | ||
| receipt.created.catch(() => { if (this.receipts.get(id) === receipt) this.receipts.delete(id); }); | ||
| return made; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { afterEach, beforeEach, expect, test } from 'bun:test'; | ||
| import { RpcErrorCode, type RpcParams } from '@boite/contracts'; | ||
| import type { CoreClient } from '../src/client.ts'; | ||
| import { startTestCore, type TestCore } from './harness.ts'; | ||
| import { rejection } from './settle.ts'; | ||
|
|
||
| let h: TestCore; | ||
| let choice: RpcParams<'threads.create'>; | ||
| beforeEach(async () => { | ||
| h = await startTestCore({ settings: { warmProcessMinutes: 0 } }); | ||
| const client = await h.connect(); | ||
| const project = await client.call('projects.drafts', {}); | ||
| const account = (await client.call('accounts.list', {})).find(account => account.providerId === 'echo')!; | ||
| choice = { projectId: project.id, providerId: 'echo', accountId: account.id, title: 'Queued while away' }; | ||
| client.close(); | ||
| }); | ||
| afterEach(async () => { await h.stop(); }); | ||
|
|
||
| const titled = async (client: CoreClient, title: string) => (await client.call('threads.list', {})).filter(thread => thread.title === title); | ||
|
|
||
| test('a creation asked again under its request id, on a new connection, answers with the conversation it made', async () => { | ||
| const first = await h.connect(); | ||
| const made = await first.call('threads.create', { ...choice, clientRequestId: 'req_queued_1' }); | ||
| // The answer was lost with the link: the device reconnects and asks again. | ||
| first.close(); | ||
| const second = await h.connect(); | ||
| const again = await second.call('threads.create', { ...choice, clientRequestId: 'req_queued_1' }); | ||
| expect(again.id).toBe(made.id); | ||
| expect(await titled(second, 'Queued while away')).toHaveLength(1); | ||
| // No mode is the default mode, as the core stores it. | ||
| const explicit = await second.call('threads.create', { ...choice, permissionMode: 'default', clientRequestId: 'req_queued_1' }); | ||
| expect(explicit.id).toBe(made.id); | ||
|
|
||
| // Asked twice at once, it is still one conversation. | ||
| const [a, b] = await Promise.all([ | ||
| second.call('threads.create', { ...choice, title: 'Twice at once', clientRequestId: 'req_queued_2' }), | ||
| second.call('threads.create', { ...choice, title: 'Twice at once', clientRequestId: 'req_queued_2' }) | ||
| ]); | ||
| expect(a.id).toBe(b.id); | ||
| expect(await titled(second, 'Twice at once')).toHaveLength(1); | ||
|
|
||
| // Without an id, or under a new one, every call makes its own. | ||
| await second.call('threads.create', { ...choice, title: 'Plain' }); | ||
| await second.call('threads.create', { ...choice, title: 'Plain' }); | ||
| expect(await titled(second, 'Plain')).toHaveLength(2); | ||
| second.close(); | ||
| }); | ||
|
|
||
| test('a request id names one conversation: reused for another it is refused, and a refused creation may try again', async () => { | ||
| const client = await h.connect(); | ||
| await client.call('threads.create', { ...choice, clientRequestId: 'req_once' }); | ||
| const reused = await rejection(client.call('threads.create', { ...choice, title: 'Something else', clientRequestId: 'req_once' })); | ||
| expect((reused.reason as { rpc: unknown }).rpc).toMatchObject({ code: RpcErrorCode.Refused, data: { field: 'clientRequestId' } }); | ||
| // Every parameter names the conversation, the mode and the effort included. | ||
| const otherMode = await rejection(client.call('threads.create', { ...choice, permissionMode: 'bypassPermissions', clientRequestId: 'req_once' })); | ||
| expect((otherMode.reason as { rpc: unknown }).rpc).toMatchObject({ code: RpcErrorCode.Refused, data: { field: 'clientRequestId' } }); | ||
| const otherEffort = await rejection(client.call('threads.create', { ...choice, effort: 'low', clientRequestId: 'req_once' })); | ||
| expect((otherEffort.reason as { rpc: unknown }).rpc).toMatchObject({ code: RpcErrorCode.Refused, data: { field: 'clientRequestId' } }); | ||
|
|
||
| const missing = await rejection(client.call('threads.create', { ...choice, accountId: 'acc_missing', clientRequestId: 'req_retry' })); | ||
| expect(missing.reason).toBeDefined(); | ||
| // Nothing was made under that id: the corrected call takes it. | ||
| const retried = await client.call('threads.create', { ...choice, title: 'After a refusal', clientRequestId: 'req_retry' }); | ||
| expect(retried.title).toBe('After a refusal'); | ||
|
|
||
| const bad = await rejection(client.call('threads.create', { ...choice, clientRequestId: 'x'.repeat(129) })); | ||
| expect((bad.reason as { rpc: unknown }).rpc).toMatchObject({ code: RpcErrorCode.InvalidParams, data: { field: 'clientRequestId' } }); | ||
| client.close(); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.