Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

7.5 KB
Raw Blame
import type { RpcClient } from './rpc-client'
import type { RpcMethodName, RpcParams, RpcSendParams } from './rpc-params-contract'
import {
  bindDeferredRpcOperation,
  captureRpcOperationSettlement,
  defineRpcOperation,
  runRpcOperation,
  startRpcOperation
} from './rpc-operation'
import { rpcResultVariants } from './rpc-operation-result-reader'
import {
  pushTestWithoutParams,
  workspaceListAtBarrier,
  workspaceListOrNull,
  workspaceRowsReader,
  worktreePsProbe,
  type WorkspaceRows
} from './rpc-operation-test-families'
import type {
  CapabilityProbeRpcDefinition,
  ObjectResultRpcDefinition,
  RequireResultRpcDefinition,
  RpcAcceptanceName,
  RpcCompatibleReader,
  RpcOperation
} from './rpc-operation-contract'

// Compile-only fences pin descriptor policy, barrier and sender params; mobile typecheck must consume every expect-error.

declare const client: RpcClient

// @ts-expect-error a variant reader combinator must have at least one reader
const _fenceEmptyVariantReaders = rpcResultVariants([])

export const fenceProbeWithReader: CapabilityProbeRpcDefinition<'worktree.ps', 'on-settle'> = {
  name: 'fence.probeWithReader',
  method: 'worktree.ps',
  acceptance: 'method-not-found-refusal',
  barrier: 'on-settle',
  // @ts-expect-error a refusal-code probe reads no payload, so it cannot carry a reader
  read: workspaceRowsReader
}

// @ts-expect-error 'require-result-or-throw' has no value to return without a reader
export const fenceDecodingWithoutReader: RequireResultRpcDefinition<
  'worktree.ps',
  'rows',
  WorkspaceRows,
  'on-settle'
> = {
  name: 'fence.decodingWithoutReader',
  method: 'worktree.ps',
  acceptance: 'require-result-or-throw',
  barrier: 'on-settle'
}

// @ts-expect-error only the named policies in rpc-acceptance-policies.ts are allowed
export const fenceInventedPolicy: RpcAcceptanceName = 'no-error-means-fine'

const fenceTextReader: RpcCompatibleReader<string, 'text', string> = (raw) => ({
  compatible: true,
  variant: 'text',
  value: raw,
  salvage: { droppedPaths: [], droppedCount: 0 }
})

export const fenceObjectPolicyWrongReader: ObjectResultRpcDefinition<
  'worktree.ps',
  'text',
  string,
  'on-settle'
> = {
  name: 'fence.objectPolicyWrongReader',
  method: 'worktree.ps',
  acceptance: 'object-result-or-null',
  barrier: 'on-settle',
  // @ts-expect-error the policy admits a non-null object, not the string this reader expects
  read: fenceTextReader
}

export const fenceDefineRejectsMismatch = defineRpcOperation({
  name: 'fence.defineRejectsMismatch',
  method: 'worktree.ps',
  // @ts-expect-error no overload of defineRpcOperation pairs a probe with a payload reader
  acceptance: 'method-not-found-refusal',
  barrier: 'on-settle',
  read: workspaceRowsReader
})

// @ts-expect-error only generated catalog method names are addressable
export const fenceUnknownMethod: RpcMethodName = 'worktree.nope'

// Pin outgoing params separately from parsed z.output and coercing z.input.

// Defaulted fields must stay optional for senders.
export const fenceOmitsDefaultedField: RpcSendParams<'files.searchPaths'> = { worktree: 'w' }

export const fenceRejectsWrongFieldType: RpcSendParams<'files.searchPaths'> = {
  // @ts-expect-error z.input of a z.unknown().transform builder admits any value; this does not
  worktree: 42
}

// @ts-expect-error `worktree` has neither a default nor an optional marker
export const fenceKeepsRequiredField: RpcSendParams<'files.searchPaths'> = { query: 'x' }

// Every handler-parsed shape must remain sendable across the catalog.
declare const fenceTighterThanParsed: {
  [Method in RpcMethodName]: RpcParams<Method> extends RpcSendParams<Method> ? never : Method
}[RpcMethodName] & {}
export const fenceNoTighterMethod: never = fenceTighterThanParsed

// Only plugins.panelAction’s literal z.unknown() may admit wholly unknown params.
declare const fenceUnknownParams: {
  [Method in RpcMethodName]: unknown extends RpcSendParams<Method> ? Method : never
}[RpcMethodName] & {}
export const fenceOnlyDeclaredUnknown: 'plugins.panelAction' = fenceUnknownParams

export async function fenceBarrierAndParams(): Promise<void> {
  await runRpcOperation(
    client,
    // @ts-expect-error this family interprets after all requests, so it has no on-settle run
    workspaceListAtBarrier,
    {}
  )
  startRpcOperation(
    client,
    // @ts-expect-error an on-settle family must not be parked behind someone else's barrier
    worktreePsProbe,
    {}
  )
  await runRpcOperation(
    client,
    workspaceListOrNull,
    // @ts-expect-error worktree.ps takes a numeric limit
    { limit: 'ten' }
  )
}

// Keep explicit null for params-less methods: params: null and an omitted key are different wire frames.
export async function fenceParamlessSend(): Promise<void> {
  await runRpcOperation(client, pushTestWithoutParams, null)
  await runRpcOperation(client, pushTestWithoutParams, undefined)
  await runRpcOperation(client, pushTestWithoutParams)
  // @ts-expect-error a method that declares no params accepts none
  await runRpcOperation(client, pushTestWithoutParams, { path: 'main.js' })
}

export async function fenceVerdictTypes(): Promise<void> {
  // @ts-expect-error the probe's policy yields a boolean, not the other family's rows
  const rows: WorkspaceRows = await runRpcOperation(client, worktreePsProbe, {})
  void rows
}

// @ts-expect-error the public descriptor also requires decoding, even without the factory
export const fenceManualWithoutReader: RpcOperation<
  'worktree.ps',
  'require-result-or-throw',
  'rows',
  WorkspaceRows,
  'on-settle'
> = {
  name: 'fence.manual',
  method: 'worktree.ps',
  acceptance: 'require-result-or-throw',
  barrier: 'on-settle'
}

// @ts-expect-error widening the policy cannot disconnect it from its required reader
export const fenceBroadWithoutReader: RpcOperation<
  'worktree.ps',
  RpcAcceptanceName,
  'rows',
  WorkspaceRows,
  'on-settle'
> = {
  name: 'fence.broad',
  method: 'worktree.ps',
  acceptance: 'require-result-or-throw',
  barrier: 'on-settle',
  read: undefined
}

// @ts-expect-error object acceptance must decode, just like require-result acceptance
export const fenceObjectWithoutReader: RpcOperation<
  'worktree.ps',
  'object-result-or-null',
  'rows',
  WorkspaceRows,
  'on-settle'
> = {
  name: 'fence.object',
  method: 'worktree.ps',
  acceptance: 'object-result-or-null',
  barrier: 'on-settle'
}

const fenceDeferred = bindDeferredRpcOperation(
  defineRpcOperation({
    name: 'fence.deferred',
    method: 'files.searchPaths',
    acceptance: 'success-result-or-skip',
    barrier: 'after-caller-barrier',
    read: workspaceRowsReader
  })
)

export function fenceDeferredArguments(): void {
  // @ts-expect-error required params must not be omitted
  fenceDeferred.request(client)
  // @ts-expect-error request params are method-keyed, including deferred requests
  fenceDeferred.request(client, { worktree: 3 })
  // @ts-expect-error single-flight operations also require their method's params
  fenceDeferred.requestSingleFlight(client, 'host')
  // @ts-expect-error on-settle operations cannot defer interpretation behind a caller guard
  bindDeferredRpcOperation(workspaceListOrNull)
  // @ts-expect-error caller-barrier operations cannot interpret as each request settles
  runRpcOperation(client, fenceDeferred.operation, { worktree: 'w' })
  // @ts-expect-error a caller-barrier operation cannot acquire all-settled behavior implicitly
  startRpcOperation(client, fenceDeferred.operation, { worktree: 'w' })
  // @ts-expect-error capturing must not decode a caller-barrier operation before its guard
  captureRpcOperationSettlement(client, fenceDeferred.operation, { worktree: 'w' })
}