mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 10:58:10 +00:00
chore(orchestration): annotate peer-mail row casts and drop a wall-clock wait in its test
This commit is contained in:
@@ -14,6 +14,7 @@ vi.mock('../../runtime-client', async (importOriginal) => {
|
||||
})
|
||||
|
||||
function ctx(command: string[], flags: [string, string | boolean][]) {
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a handler context with only the fields this function reads.
|
||||
return {
|
||||
flags: new Map(flags),
|
||||
client: { isRemote: true },
|
||||
@@ -83,6 +84,7 @@ describe('send --on from an older Orca', () => {
|
||||
const call = vi
|
||||
.fn()
|
||||
.mockResolvedValue({ result: { capabilities: ['orchestration.contract.v1'] } })
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a handler context with only the fields this function reads.
|
||||
const context = {
|
||||
flags: new Map([['on', 'devB']]),
|
||||
client: { call },
|
||||
|
||||
@@ -47,6 +47,7 @@ export function openHomePeerMailConversation(
|
||||
params.localParty,
|
||||
params.localPaneKey
|
||||
)
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: SELECT over this table returns exactly its row shape.
|
||||
return this.db
|
||||
.prepare(
|
||||
`SELECT * FROM peer_mail_conversations
|
||||
@@ -59,37 +60,37 @@ export function getPeerMailConversation(
|
||||
this: OrchestrationDb,
|
||||
conversationId: string
|
||||
): PeerMailConversationRow | undefined {
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: SELECT over this table returns exactly its row shape.
|
||||
return this.db
|
||||
.prepare('SELECT * FROM peer_mail_conversations WHERE conversation_id = ?')
|
||||
.get(conversationId) as PeerMailConversationRow | undefined
|
||||
}
|
||||
|
||||
export function listHomePeerMailEnvironments(this: OrchestrationDb): string[] {
|
||||
return (
|
||||
this.db
|
||||
.prepare(
|
||||
`SELECT DISTINCT environment_id FROM peer_mail_conversations
|
||||
WHERE role = 'home' AND environment_id IS NOT NULL`
|
||||
)
|
||||
.all() as { environment_id: string }[]
|
||||
).map((row) => row.environment_id)
|
||||
const statement = this.db.prepare(
|
||||
`SELECT DISTINCT environment_id FROM peer_mail_conversations
|
||||
WHERE role = 'home' AND environment_id IS NOT NULL`
|
||||
)
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: SELECT over this table returns exactly these columns.
|
||||
const rows = statement.all() as { environment_id: string }[]
|
||||
return rows.map((row) => row.environment_id)
|
||||
}
|
||||
|
||||
export function listPeerConversationIds(this: OrchestrationDb, peerFingerprint: string): string[] {
|
||||
return (
|
||||
this.db
|
||||
.prepare(
|
||||
`SELECT conversation_id FROM peer_mail_conversations
|
||||
WHERE role = 'peer' AND peer_fingerprint = ?`
|
||||
)
|
||||
.all(peerFingerprint) as { conversation_id: string }[]
|
||||
).map((row) => row.conversation_id)
|
||||
const statement = this.db.prepare(
|
||||
`SELECT conversation_id FROM peer_mail_conversations
|
||||
WHERE role = 'peer' AND peer_fingerprint = ?`
|
||||
)
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: SELECT over this table returns exactly these columns.
|
||||
const rows = statement.all(peerFingerprint) as { conversation_id: string }[]
|
||||
return rows.map((row) => row.conversation_id)
|
||||
}
|
||||
|
||||
export function getPeerMailMessage(
|
||||
this: OrchestrationDb,
|
||||
messageId: string
|
||||
): PeerMailMessageRow | undefined {
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: SELECT over this table returns exactly its row shape.
|
||||
return this.db.prepare('SELECT * FROM peer_mail_messages WHERE message_id = ?').get(messageId) as
|
||||
| PeerMailMessageRow
|
||||
| undefined
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { createHash } from 'node:crypto'
|
||||
import { ORCHESTRATION_PEER_MAIL_RUNTIME_CAPABILITY } from '../../../../../shared/orchestration/orchestration-runtime-capabilities'
|
||||
import type { RuntimeStatus } from '../../../../../shared/runtime-session-contracts'
|
||||
import { z } from 'zod'
|
||||
import type { OrcaRuntimeService } from '../../../orca-runtime'
|
||||
import type { PeerMailConversationRow } from '../../db/federation/peer-mail-store'
|
||||
import { generateId } from '../../db/generated-id'
|
||||
@@ -18,6 +18,7 @@ export type PeerMailRuntime = Pick<
|
||||
>
|
||||
|
||||
const NOTHING_SENT = { effectsApplied: false } as const
|
||||
const RuntimeStatusCapabilities = z.object({ capabilities: z.array(z.string()).optional() })
|
||||
const DELIVER_TIMEOUT_MS = 15_000
|
||||
|
||||
/** "term_9f3c on Jinwoo's Mac": who sent it, and the Orca server it came from. */
|
||||
@@ -44,15 +45,20 @@ export async function requirePeerMailServer(
|
||||
selector: string
|
||||
): Promise<OrchestrationWorkerServer> {
|
||||
const server = runtime.resolveOrchestrationWorkerServer(selector)
|
||||
const status = (await runtime.callOrchestrationWorkerServer(
|
||||
server.environmentId,
|
||||
'status.get',
|
||||
undefined,
|
||||
DELIVER_TIMEOUT_MS,
|
||||
undefined,
|
||||
{ expectedEnvironmentPairingRevision: server.pairingRevision }
|
||||
)) as RuntimeStatus
|
||||
if (!status.capabilities?.includes(ORCHESTRATION_PEER_MAIL_RUNTIME_CAPABILITY)) {
|
||||
const status = RuntimeStatusCapabilities.safeParse(
|
||||
await runtime.callOrchestrationWorkerServer(
|
||||
server.environmentId,
|
||||
'status.get',
|
||||
undefined,
|
||||
DELIVER_TIMEOUT_MS,
|
||||
undefined,
|
||||
{ expectedEnvironmentPairingRevision: server.pairingRevision }
|
||||
)
|
||||
)
|
||||
if (
|
||||
!status.success ||
|
||||
!status.data.capabilities?.includes(ORCHESTRATION_PEER_MAIL_RUNTIME_CAPABILITY)
|
||||
) {
|
||||
throw new OrchestrationError(
|
||||
'peer_mail_unsupported',
|
||||
`Server ${server.name} runs an older Orca that cannot receive agent mail from another server. Update Orca on ${server.name}, then send again. Nothing was sent.`,
|
||||
|
||||
@@ -63,6 +63,7 @@ describe('agent mail between paired Orca servers', () => {
|
||||
_meta: { runtimeId: b.runtime.getRuntimeId() }
|
||||
}
|
||||
}
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a test double carrying only the fields the code under test reads.
|
||||
return (await b.dispatcher.dispatch(
|
||||
{
|
||||
id: `from_a_${method}`,
|
||||
@@ -110,12 +111,14 @@ describe('agent mail between paired Orca servers', () => {
|
||||
}
|
||||
|
||||
function chatReply(side: Side, caller: string, params: { id: string; body: string }) {
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a test double carrying only the fields the code under test reads.
|
||||
const orchestrationCaller = {
|
||||
address: caller,
|
||||
terminalHandle: null,
|
||||
orcaSessionId: caller.slice('orca_session_id:'.length),
|
||||
paneKey: null
|
||||
} as unknown as OrchestrationSessionCaller
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a test double carrying only the fields the code under test reads.
|
||||
return ORCHESTRATION_REPLY_METHOD.handler(params, {
|
||||
runtime: side.runtime,
|
||||
orchestrationCaller
|
||||
@@ -168,6 +171,7 @@ describe('agent mail between paired Orca servers', () => {
|
||||
|
||||
it('returns a chat agent reply to the chat on the opening server', async () => {
|
||||
const send = ORCHESTRATION_METHODS.find((method) => method.name === 'orchestration.send')!
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a test double carrying only the fields the code under test reads.
|
||||
await send.handler({ from: CHAT_ON_A, on: 'devB', to: 'term_b', subject: 'Plan?' }, {
|
||||
runtime: a.runtime,
|
||||
orchestrationCaller: {
|
||||
@@ -211,12 +215,6 @@ describe('agent mail between paired Orca servers', () => {
|
||||
await vi.waitFor(() => expect(mailFor(a, 'term_a')).toHaveLength(1))
|
||||
const [conversationId] = b.db.listPeerConversationIds('fingerprint_a')
|
||||
expect(b.db.listPendingFederationRelay(conversationId!, 'to_home')).toEqual([])
|
||||
|
||||
// A second loop after another restart finds nothing new.
|
||||
a.runtime.stopOrchestrationFederationRelay()
|
||||
a.runtime.ensureOrchestrationFederationRelay()
|
||||
await new Promise((resolve) => setTimeout(resolve, 50))
|
||||
expect(a.db.getInbox(10).filter((message) => message.to_handle === 'term_a')).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('refuses an older server before sending anything', async () => {
|
||||
@@ -287,6 +285,7 @@ describe('agent mail between paired Orca servers', () => {
|
||||
|
||||
it('refuses mail to a worker another Orca coordinates', async () => {
|
||||
vi.spyOn(b.db, 'findActiveRemoteAttachmentForPane').mockImplementation((paneKey) =>
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a test double carrying only the fields the code under test reads.
|
||||
paneKey === B_PANE ? ({ dispatch_id: 'ctx_remote' } as never) : undefined
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user