@andorra-labs/code 0.1.16 → 0.1.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (236) hide show
  1. package/dist/src/acp-agent.d.ts +6 -0
  2. package/dist/src/acp-agent.js +15 -4
  3. package/dist/src/agent-execution.js +108 -19
  4. package/dist/src/agent-mcp-catalog.js +1 -0
  5. package/dist/src/agent-mcp.d.ts +3 -0
  6. package/dist/src/agent-mcp.js +33 -1
  7. package/dist/src/andorra/entry.d.ts +10 -0
  8. package/dist/src/andorra/entry.js +18 -4
  9. package/dist/src/andorra/errors.js +3 -0
  10. package/dist/src/andorra/hosted.d.ts +2 -1
  11. package/dist/src/andorra/hosted.js +2 -1
  12. package/dist/src/andorra/notices.d.ts +5 -0
  13. package/dist/src/andorra/notices.js +7 -0
  14. package/dist/src/andorra/state.d.ts +3 -2
  15. package/dist/src/andorra/state.js +4 -4
  16. package/dist/src/andorra/terminal.js +7 -2
  17. package/dist/src/anthropic-credentials.js +2 -0
  18. package/dist/src/app-server/error.d.ts +5 -0
  19. package/dist/src/app-server/error.js +7 -0
  20. package/dist/src/app-server/protocol.d.ts +30 -44
  21. package/dist/src/app-server/protocol.js +5 -1
  22. package/dist/src/app-server/workspace.d.ts +108 -0
  23. package/dist/src/app-server/workspace.js +608 -11
  24. package/dist/src/app-server.d.ts +390 -33
  25. package/dist/src/app-server.js +3033 -565
  26. package/dist/src/attachments.d.ts +10 -0
  27. package/dist/src/attachments.js +102 -7
  28. package/dist/src/automation-tool.js +2 -1
  29. package/dist/src/bridge/cloud.d.ts +27 -0
  30. package/dist/src/bridge/cloud.js +187 -0
  31. package/dist/src/bridge/command.d.ts +30 -0
  32. package/dist/src/bridge/command.js +359 -0
  33. package/dist/src/bridge/control.d.ts +70 -0
  34. package/dist/src/bridge/control.js +238 -0
  35. package/dist/src/bridge/engines.d.ts +90 -0
  36. package/dist/src/bridge/engines.js +601 -0
  37. package/dist/src/bridge/github.d.ts +108 -0
  38. package/dist/src/bridge/github.js +378 -0
  39. package/dist/src/bridge/host-ops.d.ts +9 -0
  40. package/dist/src/bridge/host-ops.js +139 -0
  41. package/dist/src/bridge/host.d.ts +47 -0
  42. package/dist/src/bridge/host.js +543 -0
  43. package/dist/src/bridge/link.d.ts +38 -0
  44. package/dist/src/bridge/link.js +508 -0
  45. package/dist/src/bridge/login-link.d.ts +29 -0
  46. package/dist/src/bridge/login-link.js +54 -0
  47. package/dist/src/bridge/protocol.d.ts +136 -0
  48. package/dist/src/bridge/protocol.js +11 -0
  49. package/dist/src/bridge/store.d.ts +12 -0
  50. package/dist/src/bridge/store.js +61 -0
  51. package/dist/src/bridge/wording.d.ts +6 -0
  52. package/dist/src/bridge/wording.js +10 -0
  53. package/dist/src/browser-server.js +7 -1
  54. package/dist/src/browser-ui/tokens.css +1 -1
  55. package/dist/src/bug-report-attachments.d.ts +48 -0
  56. package/dist/src/bug-report-attachments.js +327 -0
  57. package/dist/src/bug-report.d.ts +23 -3
  58. package/dist/src/bug-report.js +72 -9
  59. package/dist/src/change-authors.d.ts +41 -0
  60. package/dist/src/change-authors.js +146 -0
  61. package/dist/src/cli.d.ts +1 -3
  62. package/dist/src/cli.js +191 -35
  63. package/dist/src/codex-provider.js +1 -1
  64. package/dist/src/coding-tools.js +24 -17
  65. package/dist/src/command-screens.d.ts +6 -0
  66. package/dist/src/command-screens.js +14 -11
  67. package/dist/src/commands.d.ts +15 -3
  68. package/dist/src/commands.js +2 -1
  69. package/dist/src/compaction-phrases.js +1 -1
  70. package/dist/src/completion.js +89 -8
  71. package/dist/src/config.d.ts +71 -3
  72. package/dist/src/config.js +205 -78
  73. package/dist/src/context-budget.js +15 -2
  74. package/dist/src/deadline.js +13 -1
  75. package/dist/src/decision/DecisionEngine.d.ts +2 -0
  76. package/dist/src/decision/DecisionManager.d.ts +11 -0
  77. package/dist/src/decision/DecisionManager.js +36 -0
  78. package/dist/src/decision/applyTaskKind.d.ts +8 -0
  79. package/dist/src/decision/applyTaskKind.js +53 -0
  80. package/dist/src/decision/checks.js +1 -1
  81. package/dist/src/decision/hostedPolicy.js +2 -1
  82. package/dist/src/decision/providers/AndorraDecisionProvider.d.ts +2 -0
  83. package/dist/src/decision/providers/AndorraDecisionProvider.js +12 -0
  84. package/dist/src/decision/requestPhrases.js +0 -1
  85. package/dist/src/decision/taskKind.d.ts +52 -0
  86. package/dist/src/decision/taskKind.js +76 -0
  87. package/dist/src/decision/types.d.ts +1 -1
  88. package/dist/src/default-graph.js +1 -1
  89. package/dist/src/efficiency.d.ts +5 -0
  90. package/dist/src/efficiency.js +5 -1
  91. package/dist/src/effort-routing.d.ts +27 -0
  92. package/dist/src/effort-routing.js +34 -0
  93. package/dist/src/evidence.d.ts +110 -0
  94. package/dist/src/evidence.js +195 -0
  95. package/dist/src/file-diff.d.ts +4 -0
  96. package/dist/src/file-diff.js +2 -1
  97. package/dist/src/github-app/agent-writes.d.ts +102 -0
  98. package/dist/src/github-app/agent-writes.js +320 -0
  99. package/dist/src/github-app/client.d.ts +368 -0
  100. package/dist/src/github-app/client.js +830 -0
  101. package/dist/src/github-app/event-source.d.ts +208 -0
  102. package/dist/src/github-app/event-source.js +684 -0
  103. package/dist/src/github-app/metrics.d.ts +32 -0
  104. package/dist/src/github-app/metrics.js +49 -0
  105. package/dist/src/github-app/review-loop.d.ts +274 -0
  106. package/dist/src/github-app/review-loop.js +1177 -0
  107. package/dist/src/github-app/review-node.d.ts +81 -0
  108. package/dist/src/github-app/review-node.js +205 -0
  109. package/dist/src/github-app/review-post.d.ts +73 -0
  110. package/dist/src/github-app/review-post.js +139 -0
  111. package/dist/src/github-app/review-text.d.ts +107 -0
  112. package/dist/src/github-app/review-text.js +1133 -0
  113. package/dist/src/github-app/review-threads.d.ts +47 -0
  114. package/dist/src/github-app/review-threads.js +107 -0
  115. package/dist/src/github-app/reviewer.d.ts +121 -0
  116. package/dist/src/github-app/reviewer.js +598 -0
  117. package/dist/src/github-app/store.d.ts +59 -0
  118. package/dist/src/github-app/store.js +148 -0
  119. package/dist/src/github-app/triggers.d.ts +141 -0
  120. package/dist/src/github-app/triggers.js +441 -0
  121. package/dist/src/github-names.d.ts +15 -0
  122. package/dist/src/github-names.js +15 -0
  123. package/dist/src/glob-search.d.ts +61 -0
  124. package/dist/src/glob-search.js +180 -0
  125. package/dist/src/graph-health.js +6 -1
  126. package/dist/src/graph-manager-chat.js +1 -1
  127. package/dist/src/graph-preview.d.ts +48 -0
  128. package/dist/src/graph-preview.js +56 -0
  129. package/dist/src/graph-provider-portability.js +2 -1
  130. package/dist/src/graph-runner.js +65 -4
  131. package/dist/src/graph-server.js +7 -5
  132. package/dist/src/graph-sync.js +3 -3
  133. package/dist/src/graph-ui/app.js +121 -7
  134. package/dist/src/graph-ui/icons.js +2 -1
  135. package/dist/src/graph-ui/studio.js +15 -1
  136. package/dist/src/graph-ui/tokens.css +1 -1
  137. package/dist/src/graphs.d.ts +40 -1
  138. package/dist/src/graphs.js +164 -15
  139. package/dist/src/hardened-runtime.js +32 -21
  140. package/dist/src/image-input.d.ts +16 -0
  141. package/dist/src/image-input.js +41 -0
  142. package/dist/src/membership.d.ts +10 -0
  143. package/dist/src/membership.js +19 -4
  144. package/dist/src/model-health-cache.js +1 -1
  145. package/dist/src/model-metadata.d.ts +6 -0
  146. package/dist/src/model-metadata.js +35 -15
  147. package/dist/src/model-override.d.ts +2 -1
  148. package/dist/src/model-override.js +9 -0
  149. package/dist/src/model-relay/local.d.ts +116 -0
  150. package/dist/src/model-relay/local.js +468 -0
  151. package/dist/src/model-relay/remote.d.ts +129 -0
  152. package/dist/src/model-relay/remote.js +252 -0
  153. package/dist/src/model-relay/runs.d.ts +34 -0
  154. package/dist/src/model-relay/runs.js +146 -0
  155. package/dist/src/model-relay/wire.d.ts +5 -0
  156. package/dist/src/model-relay/wire.js +5 -0
  157. package/dist/src/model-tiers.d.ts +3 -0
  158. package/dist/src/model-tiers.js +3 -3
  159. package/dist/src/project-registry.d.ts +21 -0
  160. package/dist/src/project-registry.js +74 -0
  161. package/dist/src/provider-engine.d.ts +13 -1
  162. package/dist/src/provider-engine.js +74 -0
  163. package/dist/src/provider-import.d.ts +0 -12
  164. package/dist/src/provider-import.js +7 -195
  165. package/dist/src/providers.d.ts +28 -2
  166. package/dist/src/providers.js +59 -12
  167. package/dist/src/pull-request-logs.d.ts +12 -1
  168. package/dist/src/pull-request-logs.js +14 -3
  169. package/dist/src/pull-requests.d.ts +338 -8
  170. package/dist/src/pull-requests.js +482 -53
  171. package/dist/src/react-runtime.js +99 -16
  172. package/dist/src/remote/engine-pool.d.ts +88 -4
  173. package/dist/src/remote/engine-pool.js +367 -90
  174. package/dist/src/remote/roots.d.ts +19 -0
  175. package/dist/src/remote/roots.js +45 -7
  176. package/dist/src/remote/types.d.ts +0 -44
  177. package/dist/src/run-control.d.ts +7 -0
  178. package/dist/src/run-control.js +23 -0
  179. package/dist/src/run-records.d.ts +7 -2
  180. package/dist/src/run-records.js +71 -6
  181. package/dist/src/runtime.d.ts +34 -2
  182. package/dist/src/runtime.js +18 -2
  183. package/dist/src/session-changes.d.ts +35 -0
  184. package/dist/src/session-changes.js +194 -6
  185. package/dist/src/session-monitor.js +2 -1
  186. package/dist/src/session-reaper.d.ts +1 -1
  187. package/dist/src/session-reaper.js +6 -1
  188. package/dist/src/session-summary.d.ts +2 -1
  189. package/dist/src/session-summary.js +2 -1
  190. package/dist/src/sessions.d.ts +35 -1
  191. package/dist/src/sessions.js +70 -4
  192. package/dist/src/skills/bundled/bug-report/SKILL.md +51 -7
  193. package/dist/src/skills/bundled/diagram/SKILL.md +5 -3
  194. package/dist/src/skills/bundled/graph-manager/SKILL.md +7 -4
  195. package/dist/src/skills/bundled/provenance.json +3 -3
  196. package/dist/src/skills/runtime-skills.js +1 -1
  197. package/dist/src/task-checkout.d.ts +141 -0
  198. package/dist/src/task-checkout.js +558 -0
  199. package/dist/src/telemetry/emitter.d.ts +15 -0
  200. package/dist/src/telemetry/emitter.js +35 -1
  201. package/dist/src/telemetry/schema.d.ts +25 -2
  202. package/dist/src/telemetry/schema.js +52 -4
  203. package/dist/src/telemetry/surface.d.ts +2 -1
  204. package/dist/src/telemetry/surface.js +4 -1
  205. package/dist/src/terminal-ui.d.ts +1 -0
  206. package/dist/src/terminal-ui.js +10 -3
  207. package/dist/src/test-edit-guard.d.ts +37 -0
  208. package/dist/src/test-edit-guard.js +216 -3
  209. package/dist/src/thinking-level-detect.d.ts +11 -0
  210. package/dist/src/thinking-level-detect.js +47 -0
  211. package/dist/src/thinking-levels.d.ts +30 -0
  212. package/dist/src/thinking-levels.js +124 -0
  213. package/dist/src/timers.d.ts +3 -0
  214. package/dist/src/tool-catalog.js +1 -1
  215. package/dist/src/tools.js +59 -9
  216. package/dist/src/transcript.js +1 -1
  217. package/dist/src/types.d.ts +34 -0
  218. package/package.json +6 -1
  219. package/dist/src/remote/cert.d.ts +0 -11
  220. package/dist/src/remote/cert.js +0 -87
  221. package/dist/src/remote/client.d.ts +0 -119
  222. package/dist/src/remote/client.js +0 -544
  223. package/dist/src/remote/command.d.ts +0 -4
  224. package/dist/src/remote/command.js +0 -194
  225. package/dist/src/remote/errors.d.ts +0 -11
  226. package/dist/src/remote/errors.js +0 -14
  227. package/dist/src/remote/network.d.ts +0 -22
  228. package/dist/src/remote/network.js +0 -102
  229. package/dist/src/remote/pairing.d.ts +0 -63
  230. package/dist/src/remote/pairing.js +0 -299
  231. package/dist/src/remote/pem.d.ts +0 -2
  232. package/dist/src/remote/pem.js +0 -7
  233. package/dist/src/remote/serve.d.ts +0 -68
  234. package/dist/src/remote/serve.js +0 -244
  235. package/dist/src/remote/server.d.ts +0 -21
  236. package/dist/src/remote/server.js +0 -406
@@ -0,0 +1,129 @@
1
+ import { type ProviderConfig } from '../providers.js';
2
+ import type { Message, ModelAdapter, ModelRequestContext, ModelResponse, ModelRetry, ToolSchema } from '../types.js';
3
+ /** One model request's JSON payload may be this large; with its envelope it stays under the relay's 16 MiB frame. */
4
+ export declare const RELAY_PAYLOAD_LIMIT_BYTES: number;
5
+ /** An unclaimed request is announced again this often, so a desktop that attaches later still sees it. */
6
+ export declare const RELAY_REANNOUNCE_MS = 5000;
7
+ /** A claim with no progress for this long is released and the request is announced again. */
8
+ export declare const RELAY_CLAIM_SILENCE_MS = 45000;
9
+ /** A request without a sign of life from a desktop for this long shows the run as awaiting-desktop. */
10
+ export declare const RELAY_AWAY_MS = 600000;
11
+ /** One reasoning delta may be this large; a longer one is cut to whole characters that fit. */
12
+ export declare const RELAY_DELTA_LIMIT_BYTES: number;
13
+ export declare const RELAY_PAYLOAD_TOO_LARGE = "This step's context is too large to send to your computer (limit 12 MiB).";
14
+ export interface RelayWire {
15
+ id: string;
16
+ runId: string;
17
+ provider: string;
18
+ request: {
19
+ model: string;
20
+ messages: Message[];
21
+ tools: ToolSchema[];
22
+ maxTokens?: number;
23
+ mutableTail?: number;
24
+ toolChoice?: 'required';
25
+ context?: ModelRequestContext;
26
+ /** The effort in force for this call (the run's or node's choice), not the provider's saved default. */
27
+ reasoningEffort?: string;
28
+ /** The run's session, for a provider that groups a session's calls; the desktop passes it on only in the form the provider takes. */
29
+ sessionId?: string;
30
+ };
31
+ }
32
+ export type RelayErrorKind = 'retryable' | 'provider-request' | 'other';
33
+ /**
34
+ * A failed model call as the desktop reports it. `contextOverflow` marks the input exceeding the model's window, as
35
+ * `ProviderRequestError.contextOverflow` does locally; `code` is the failure's telemetry code (`http_429`).
36
+ */
37
+ export interface RelayFailure {
38
+ message: string;
39
+ kind: RelayErrorKind;
40
+ contextOverflow?: true;
41
+ code?: string;
42
+ }
43
+ /** The form a relayed failure's telemetry code must have. */
44
+ export declare const RELAY_FAILURE_CODE: RegExp;
45
+ export type RelayRunState = 'awaiting-desktop' | 'running';
46
+ export interface ModelRelayDeps {
47
+ emit(event: string, data: Record<string, unknown>): void;
48
+ setRunState(runId: string, state: RelayRunState): void;
49
+ /** Holds the run's wall-time budgets; the returned function releases the hold. */
50
+ suspendBudget(runId: string): () => void;
51
+ now?: () => number;
52
+ timers?: typeof globalThis;
53
+ }
54
+ /**
55
+ * What a machine announces for a request: no payload, only what a desktop needs to decide whether to claim it. The
56
+ * payload follows once, in the answer to a successful claim.
57
+ */
58
+ export interface RelayAnnounce {
59
+ id: string;
60
+ runId: string;
61
+ provider: string;
62
+ model: string;
63
+ bytes: number;
64
+ }
65
+ /** The answer to `model.relay.claim`. */
66
+ export type RelayClaimAnswer = {
67
+ claimed: true;
68
+ payload: RelayWire['request'];
69
+ } | {
70
+ claimed: false;
71
+ };
72
+ /**
73
+ * The remote engine's half of the model relay: a relayed provider's `generate()` becomes a
74
+ * `model.relay.request` event that a desktop claims and answers with `model.relay.result`.
75
+ * A request never fails because no desktop is there; it waits, and the run shows awaiting-desktop.
76
+ */
77
+ export declare class ModelRelayBroker {
78
+ private readonly deps;
79
+ private readonly pending;
80
+ private readonly timers;
81
+ constructor(deps: ModelRelayDeps);
82
+ adapter(provider: ProviderConfig, runId: string, sessionId?: string): ModelAdapter;
83
+ private generate;
84
+ /** Takes a request for `claimant`; the payload is handed over only here, once the claim holds. */
85
+ claim(params: {
86
+ id: string;
87
+ runId: string;
88
+ claimant: string;
89
+ }): RelayClaimAnswer;
90
+ progress(params: {
91
+ id: string;
92
+ runId: string;
93
+ claimant: string;
94
+ kind: 'alive' | 'request' | 'reasoning' | 'retry' | 'recovered-error';
95
+ delta?: string;
96
+ retry?: ModelRetry;
97
+ code?: string;
98
+ }): {
99
+ ok: boolean;
100
+ };
101
+ result(params: {
102
+ id: string;
103
+ runId: string;
104
+ claimant: string;
105
+ ok: boolean;
106
+ response?: ModelResponse;
107
+ error?: RelayFailure;
108
+ }): {
109
+ ok: boolean;
110
+ };
111
+ /** Whether `runId` has a request no desktop has shown a sign of life for in RELAY_AWAY_MS. */
112
+ awaiting(runId: string): boolean;
113
+ /** Rejects every wait of `runId` as cancelled and tells the desktops to stop. */
114
+ endRun(runId: string): void;
115
+ close(): void;
116
+ private lookup;
117
+ private owned;
118
+ /**
119
+ * Announces an unclaimed request now and every RELAY_REANNOUNCE_MS, holding the run's budgets meanwhile. The
120
+ * announce carries no payload, so repeating it costs little; the payload goes once, to the claim that holds.
121
+ */
122
+ private announce;
123
+ /** (Re)starts the claimant's silence clock; when it runs out the claim is released and the request announced again. */
124
+ private listen;
125
+ /** A sign of life: restarts the away clock, and a returning desktop brings the run back to running. */
126
+ private touch;
127
+ private reportBack;
128
+ private finish;
129
+ }
@@ -0,0 +1,252 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import { ProviderRequestError, RetryableProviderError } from '../providers.js';
3
+ /** One model request's JSON payload may be this large; with its envelope it stays under the relay's 16 MiB frame. */
4
+ export const RELAY_PAYLOAD_LIMIT_BYTES = 12 * 1024 * 1024;
5
+ /** An unclaimed request is announced again this often, so a desktop that attaches later still sees it. */
6
+ export const RELAY_REANNOUNCE_MS = 5_000;
7
+ /** A claim with no progress for this long is released and the request is announced again. */
8
+ export const RELAY_CLAIM_SILENCE_MS = 45_000;
9
+ /** A request without a sign of life from a desktop for this long shows the run as awaiting-desktop. */
10
+ export const RELAY_AWAY_MS = 600_000;
11
+ /** One reasoning delta may be this large; a longer one is cut to whole characters that fit. */
12
+ export const RELAY_DELTA_LIMIT_BYTES = 64 * 1024;
13
+ export const RELAY_PAYLOAD_TOO_LARGE = 'This step\'s context is too large to send to your computer (limit 12 MiB).';
14
+ const CANCELLED = 'Provider request cancelled.';
15
+ /** The form a relayed failure's telemetry code must have. */
16
+ export const RELAY_FAILURE_CODE = /^[a-z0-9_]{1,64}$/;
17
+ function wirePayload(provider, request, sessionId) {
18
+ return {
19
+ model: request.model, messages: request.messages, tools: request.tools,
20
+ ...(request.maxTokens === undefined ? {} : { maxTokens: request.maxTokens }),
21
+ ...(request.mutableTail === undefined ? {} : { mutableTail: request.mutableTail }),
22
+ ...(request.toolChoice === undefined ? {} : { toolChoice: request.toolChoice }),
23
+ ...(request.context === undefined ? {} : { context: request.context }),
24
+ ...(provider.reasoningEffort === undefined ? {} : { reasoningEffort: provider.reasoningEffort }),
25
+ ...(sessionId === undefined ? {} : { sessionId }),
26
+ };
27
+ }
28
+ function readableToolCall(call) {
29
+ if (!call || typeof call !== 'object' || Array.isArray(call))
30
+ return false;
31
+ const value = call;
32
+ return typeof value.name === 'string' && typeof value.arguments === 'string' && (value.id === undefined || typeof value.id === 'string');
33
+ }
34
+ function readable(response) {
35
+ if (!response || typeof response !== 'object' || Array.isArray(response))
36
+ return false;
37
+ const value = response;
38
+ return typeof value.text === 'string' && Array.isArray(value.toolCalls) && value.toolCalls.every(readableToolCall);
39
+ }
40
+ /** `delta` cut to the whole characters that fit in RELAY_DELTA_LIMIT_BYTES. */
41
+ function boundedDelta(delta) {
42
+ if (Buffer.byteLength(delta) <= RELAY_DELTA_LIMIT_BYTES)
43
+ return delta;
44
+ return delta.slice(0, new TextEncoder().encodeInto(delta, new Uint8Array(RELAY_DELTA_LIMIT_BYTES)).read);
45
+ }
46
+ function failure(error) {
47
+ const message = typeof error?.message === 'string' && error.message.trim() ? error.message : 'The model request failed on your computer.';
48
+ const code = typeof error?.code === 'string' && RELAY_FAILURE_CODE.test(error.code) ? error.code : undefined;
49
+ let failed;
50
+ if (error?.kind === 'retryable')
51
+ failed = new RetryableProviderError(message);
52
+ else if (error?.kind === 'provider-request')
53
+ failed = new ProviderRequestError(message, error.contextOverflow === true);
54
+ else
55
+ return new Error(message);
56
+ if (code !== undefined)
57
+ failed.telemetryCode = code;
58
+ return failed;
59
+ }
60
+ /**
61
+ * The remote engine's half of the model relay: a relayed provider's `generate()` becomes a
62
+ * `model.relay.request` event that a desktop claims and answers with `model.relay.result`.
63
+ * A request never fails because no desktop is there; it waits, and the run shows awaiting-desktop.
64
+ */
65
+ export class ModelRelayBroker {
66
+ deps;
67
+ pending = new Map();
68
+ timers;
69
+ constructor(deps) {
70
+ this.deps = deps;
71
+ this.timers = deps.timers ?? globalThis;
72
+ }
73
+ adapter(provider, runId, sessionId) {
74
+ return { generate: request => this.generate(provider, runId, request, sessionId) };
75
+ }
76
+ generate(provider, runId, request, sessionId) {
77
+ if (request.signal.aborted)
78
+ return Promise.reject(new Error(CANCELLED));
79
+ const payload = wirePayload(provider, request, sessionId);
80
+ const bytes = Buffer.byteLength(JSON.stringify(payload));
81
+ if (bytes > RELAY_PAYLOAD_LIMIT_BYTES)
82
+ return Promise.reject(new Error(RELAY_PAYLOAD_TOO_LARGE));
83
+ return new Promise((resolve, reject) => {
84
+ const entry = {
85
+ id: randomUUID(), runId, provider: provider.id, payload, bytes, request, away: false,
86
+ resolve: response => { this.finish(entry); resolve(response); },
87
+ reject: error => { this.finish(entry); reject(error); },
88
+ onAbort: () => {
89
+ if (!this.pending.has(entry.id))
90
+ return;
91
+ this.deps.emit('model.relay.cancel', { id: entry.id, runId });
92
+ entry.reject(new Error(CANCELLED));
93
+ },
94
+ };
95
+ this.pending.set(entry.id, entry);
96
+ request.signal.addEventListener('abort', entry.onAbort, { once: true });
97
+ this.touch(entry);
98
+ this.announce(entry);
99
+ });
100
+ }
101
+ /** Takes a request for `claimant`; the payload is handed over only here, once the claim holds. */
102
+ claim(params) {
103
+ const entry = this.lookup(params.id, params.runId);
104
+ if (!entry || (entry.claimant !== undefined && entry.claimant !== params.claimant))
105
+ return { claimed: false };
106
+ if (entry.claimant === undefined) {
107
+ entry.claimant = params.claimant;
108
+ if (entry.announceTimer !== undefined) {
109
+ this.timers.clearInterval(entry.announceTimer);
110
+ entry.announceTimer = undefined;
111
+ }
112
+ entry.resumeBudget?.();
113
+ entry.resumeBudget = undefined;
114
+ }
115
+ this.touch(entry);
116
+ this.listen(entry);
117
+ return { claimed: true, payload: entry.payload };
118
+ }
119
+ progress(params) {
120
+ const entry = this.owned(params);
121
+ if (!entry)
122
+ return { ok: false };
123
+ this.touch(entry);
124
+ this.listen(entry);
125
+ const { request } = entry;
126
+ if (params.kind === 'request')
127
+ request.onRequest?.();
128
+ else if (params.kind === 'reasoning' && typeof params.delta === 'string')
129
+ request.onReasoning?.(boundedDelta(params.delta));
130
+ // A provider failure the desktop's adapter recovered from (a forced tool choice or streaming refused, then resent):
131
+ // only its telemetry code travels, in the form a relayed failure's code has.
132
+ else if (params.kind === 'recovered-error') {
133
+ if (typeof params.code === 'string' && RELAY_FAILURE_CODE.test(params.code))
134
+ request.onRecoveredError?.(params.code);
135
+ }
136
+ else if (params.kind === 'retry' && params.retry && typeof params.retry === 'object') {
137
+ // As with a local adapter, a retry listener that throws ends the call with its error.
138
+ Promise.resolve().then(() => request.onRetry?.(params.retry)).catch((error) => {
139
+ if (!this.pending.has(entry.id))
140
+ return;
141
+ this.deps.emit('model.relay.cancel', { id: entry.id, runId: entry.runId });
142
+ entry.reject(error instanceof Error ? error : new Error(String(error)));
143
+ });
144
+ }
145
+ return { ok: true };
146
+ }
147
+ result(params) {
148
+ const entry = this.owned(params);
149
+ if (!entry)
150
+ return { ok: false };
151
+ if (params.ok) {
152
+ if (readable(params.response))
153
+ entry.resolve(params.response);
154
+ else
155
+ entry.reject(new Error('Your computer sent a model reply this machine could not read.'));
156
+ }
157
+ else
158
+ entry.reject(failure(params.error));
159
+ return { ok: true };
160
+ }
161
+ /** Whether `runId` has a request no desktop has shown a sign of life for in RELAY_AWAY_MS. */
162
+ awaiting(runId) {
163
+ return [...this.pending.values()].some(entry => entry.runId === runId && entry.away);
164
+ }
165
+ /** Rejects every wait of `runId` as cancelled and tells the desktops to stop. */
166
+ endRun(runId) {
167
+ // The run is ending: its own final state follows, so its requests are not reported back to running.
168
+ for (const entry of [...this.pending.values()])
169
+ if (entry.runId === runId) {
170
+ entry.away = false;
171
+ entry.onAbort();
172
+ }
173
+ }
174
+ close() {
175
+ for (const entry of [...this.pending.values()]) {
176
+ entry.away = false;
177
+ entry.reject(new Error(CANCELLED));
178
+ }
179
+ }
180
+ lookup(id, runId) {
181
+ const entry = typeof id === 'string' ? this.pending.get(id) : undefined;
182
+ return entry && entry.runId === runId ? entry : undefined;
183
+ }
184
+ owned(params) {
185
+ const entry = this.lookup(params.id, params.runId);
186
+ return entry && entry.claimant !== undefined && entry.claimant === params.claimant ? entry : undefined;
187
+ }
188
+ /**
189
+ * Announces an unclaimed request now and every RELAY_REANNOUNCE_MS, holding the run's budgets meanwhile. The
190
+ * announce carries no payload, so repeating it costs little; the payload goes once, to the claim that holds.
191
+ */
192
+ announce(entry) {
193
+ entry.resumeBudget ??= this.deps.suspendBudget(entry.runId);
194
+ const announced = { id: entry.id, runId: entry.runId, provider: entry.provider, model: entry.payload.model, bytes: entry.bytes };
195
+ const emit = () => this.deps.emit('model.relay.request', { ...announced });
196
+ emit();
197
+ entry.announceTimer = this.timers.setInterval(emit, RELAY_REANNOUNCE_MS);
198
+ entry.announceTimer.unref?.();
199
+ }
200
+ /** (Re)starts the claimant's silence clock; when it runs out the claim is released and the request announced again. */
201
+ listen(entry) {
202
+ if (entry.silenceTimer !== undefined)
203
+ this.timers.clearTimeout(entry.silenceTimer);
204
+ entry.silenceTimer = this.timers.setTimeout(() => {
205
+ entry.silenceTimer = undefined;
206
+ if (!this.pending.has(entry.id))
207
+ return;
208
+ entry.claimant = undefined;
209
+ this.announce(entry);
210
+ }, RELAY_CLAIM_SILENCE_MS);
211
+ entry.silenceTimer.unref?.();
212
+ }
213
+ /** A sign of life: restarts the away clock, and a returning desktop brings the run back to running. */
214
+ touch(entry) {
215
+ if (entry.awayTimer !== undefined)
216
+ this.timers.clearTimeout(entry.awayTimer);
217
+ if (entry.away && entry.claimant !== undefined) {
218
+ entry.away = false;
219
+ this.reportBack(entry.runId);
220
+ }
221
+ entry.awayTimer = this.timers.setTimeout(() => {
222
+ entry.awayTimer = undefined;
223
+ if (!this.pending.has(entry.id) || entry.away)
224
+ return;
225
+ const shown = [...this.pending.values()].some(other => other.runId === entry.runId && other.away);
226
+ entry.away = true;
227
+ if (!shown)
228
+ this.deps.setRunState(entry.runId, 'awaiting-desktop');
229
+ }, RELAY_AWAY_MS);
230
+ entry.awayTimer.unref?.();
231
+ }
232
+ reportBack(runId) {
233
+ if (![...this.pending.values()].some(other => other.runId === runId && other.away))
234
+ this.deps.setRunState(runId, 'running');
235
+ }
236
+ finish(entry) {
237
+ if (!this.pending.delete(entry.id))
238
+ return;
239
+ entry.request.signal.removeEventListener('abort', entry.onAbort);
240
+ if (entry.announceTimer !== undefined)
241
+ this.timers.clearInterval(entry.announceTimer);
242
+ if (entry.silenceTimer !== undefined)
243
+ this.timers.clearTimeout(entry.silenceTimer);
244
+ if (entry.awayTimer !== undefined)
245
+ this.timers.clearTimeout(entry.awayTimer);
246
+ entry.resumeBudget?.();
247
+ entry.resumeBudget = undefined;
248
+ // A request that ends while away (its node or branch was stopped) no longer keeps the run shown as awaiting-desktop.
249
+ if (entry.away)
250
+ this.reportBack(entry.runId);
251
+ }
252
+ }
@@ -0,0 +1,34 @@
1
+ /** At most this many runs are remembered per machine; recording another drops the oldest. */
2
+ export declare const MODEL_RELAY_RUNS_LIMIT = 500;
3
+ /** By account subject, then by machine: the runs this computer started or resumed there, most recent last. */
4
+ export type ModelRelayRuns = Record<string, Record<string, string[]>>;
5
+ export declare function modelRelayRunsPath(home: string): string;
6
+ /**
7
+ * The runs whose model requests this computer answers: those it started or resumed on a machine, per account, in
8
+ * `<home>/model-relay-runs.json`. Writes are serialized and go through a temporary file and a rename.
9
+ */
10
+ export declare class ModelRelayRunStore {
11
+ private readonly home;
12
+ private chain;
13
+ private cache;
14
+ /** How many times the file was read; it is read once and then kept. */
15
+ fileReads: number;
16
+ constructor(home: string);
17
+ /** Whether `subject` started or resumed `runId` on `env` from this computer. */
18
+ has(subject: string | undefined, env: string, runId: string): Promise<boolean>;
19
+ /** Records `runId` as started or resumed on `env` from this computer, as the most recent. */
20
+ record(subject: string, env: string, runId: string): Promise<void>;
21
+ /** Forgets every run of `env`, for every account: the machine was removed. */
22
+ forget(env: string): Promise<void>;
23
+ /** Forgets `subject`'s runs on each of `envs`: they left that account's list. */
24
+ forgetFor(subject: string, envs: Iterable<string>): Promise<void>;
25
+ /** Resolves once every write begun so far has finished. */
26
+ settled(): Promise<void>;
27
+ /**
28
+ * The file's contents, read once and kept: only this process writes the file, and each write replaces the copy.
29
+ * A file that is missing is empty; one that cannot be read is read again next time.
30
+ */
31
+ private read;
32
+ private load;
33
+ private change;
34
+ }
@@ -0,0 +1,146 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
3
+ import { dirname, join } from 'node:path';
4
+ /** At most this many runs are remembered per machine; recording another drops the oldest. */
5
+ export const MODEL_RELAY_RUNS_LIMIT = 500;
6
+ const ENVIRONMENT_ID = /^env_[A-Za-z0-9_-]{22}$/;
7
+ const ACCOUNT_SUBJECT = /^[A-Za-z0-9-]{1,128}$/;
8
+ const MAX_RUN_ID = 128;
9
+ export function modelRelayRunsPath(home) {
10
+ return join(home, 'model-relay-runs.json');
11
+ }
12
+ function usableRun(runId) {
13
+ return typeof runId === 'string' && runId.length > 0 && runId.length <= MAX_RUN_ID;
14
+ }
15
+ /** The saved file, keeping only well-formed entries; a missing or unreadable file is empty. */
16
+ function parse(text) {
17
+ let value;
18
+ try {
19
+ value = JSON.parse(text);
20
+ }
21
+ catch {
22
+ return {};
23
+ }
24
+ const runs = {};
25
+ if (!value || typeof value !== 'object' || Array.isArray(value))
26
+ return runs;
27
+ for (const [subject, machines] of Object.entries(value)) {
28
+ if (!ACCOUNT_SUBJECT.test(subject) || !machines || typeof machines !== 'object' || Array.isArray(machines))
29
+ continue;
30
+ const kept = {};
31
+ for (const [env, ids] of Object.entries(machines)) {
32
+ if (!ENVIRONMENT_ID.test(env) || !Array.isArray(ids))
33
+ continue;
34
+ kept[env] = ids.filter(usableRun).slice(-MODEL_RELAY_RUNS_LIMIT);
35
+ }
36
+ runs[subject] = kept;
37
+ }
38
+ return runs;
39
+ }
40
+ /**
41
+ * The runs whose model requests this computer answers: those it started or resumed on a machine, per account, in
42
+ * `<home>/model-relay-runs.json`. Writes are serialized and go through a temporary file and a rename.
43
+ */
44
+ export class ModelRelayRunStore {
45
+ home;
46
+ chain = Promise.resolve();
47
+ cache;
48
+ /** How many times the file was read; it is read once and then kept. */
49
+ fileReads = 0;
50
+ constructor(home) {
51
+ this.home = home;
52
+ }
53
+ /** Whether `subject` started or resumed `runId` on `env` from this computer. */
54
+ async has(subject, env, runId) {
55
+ if (subject === undefined)
56
+ return false;
57
+ const runs = await this.read();
58
+ return Object.hasOwn(runs, subject) && Object.hasOwn(runs[subject], env) && runs[subject][env].includes(runId);
59
+ }
60
+ /** Records `runId` as started or resumed on `env` from this computer, as the most recent. */
61
+ record(subject, env, runId) {
62
+ if (!ACCOUNT_SUBJECT.test(subject) || !ENVIRONMENT_ID.test(env) || !usableRun(runId))
63
+ return Promise.resolve();
64
+ return this.change(runs => {
65
+ const machines = Object.hasOwn(runs, subject) ? runs[subject] : (runs[subject] = {});
66
+ const list = (Object.hasOwn(machines, env) ? machines[env] : []).filter(id => id !== runId);
67
+ list.push(runId);
68
+ machines[env] = list.slice(-MODEL_RELAY_RUNS_LIMIT);
69
+ return true;
70
+ });
71
+ }
72
+ /** Forgets every run of `env`, for every account: the machine was removed. */
73
+ forget(env) {
74
+ return this.change(runs => {
75
+ let changed = false;
76
+ for (const machines of Object.values(runs))
77
+ if (Object.hasOwn(machines, env)) {
78
+ delete machines[env];
79
+ changed = true;
80
+ }
81
+ return changed;
82
+ });
83
+ }
84
+ /** Forgets `subject`'s runs on each of `envs`: they left that account's list. */
85
+ forgetFor(subject, envs) {
86
+ return this.change(runs => {
87
+ if (!Object.hasOwn(runs, subject))
88
+ return false;
89
+ let changed = false;
90
+ for (const env of envs)
91
+ if (Object.hasOwn(runs[subject], env)) {
92
+ delete runs[subject][env];
93
+ changed = true;
94
+ }
95
+ return changed;
96
+ });
97
+ }
98
+ /** Resolves once every write begun so far has finished. */
99
+ async settled() {
100
+ await this.chain.catch(() => undefined);
101
+ }
102
+ /**
103
+ * The file's contents, read once and kept: only this process writes the file, and each write replaces the copy.
104
+ * A file that is missing is empty; one that cannot be read is read again next time.
105
+ */
106
+ async read() {
107
+ await this.chain.catch(() => undefined);
108
+ return this.load();
109
+ }
110
+ async load() {
111
+ if (this.cache !== undefined)
112
+ return this.cache;
113
+ this.fileReads += 1;
114
+ try {
115
+ this.cache = parse(await readFile(modelRelayRunsPath(this.home), 'utf8'));
116
+ }
117
+ catch (error) {
118
+ if (error.code !== 'ENOENT')
119
+ return {};
120
+ this.cache = {};
121
+ }
122
+ return this.cache;
123
+ }
124
+ change(apply) {
125
+ const next = this.chain.catch(() => undefined).then(async () => {
126
+ const path = modelRelayRunsPath(this.home);
127
+ // A copy: the kept contents change only once the write has landed.
128
+ const runs = structuredClone(await this.load());
129
+ if (!apply(runs))
130
+ return;
131
+ await mkdir(dirname(path), { recursive: true });
132
+ const temporary = `${path}.${randomUUID()}.tmp`;
133
+ try {
134
+ await writeFile(temporary, `${JSON.stringify(runs, null, 2)}\n`, { encoding: 'utf8', mode: 0o600 });
135
+ await rename(temporary, path);
136
+ }
137
+ catch (error) {
138
+ await rm(temporary, { force: true }).catch(() => undefined);
139
+ throw error;
140
+ }
141
+ this.cache = runs;
142
+ });
143
+ this.chain = next;
144
+ return next;
145
+ }
146
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * The connection ids a desktop engine opens for itself to answer a machine's model requests start with this. The
3
+ * machine's engine pool sends such a connection only `model.relay.*` events, and no replay of its runs.
4
+ */
5
+ export declare const MODEL_RELAY_CONNECTION_PREFIX = "relay-";
@@ -0,0 +1,5 @@
1
+ /**
2
+ * The connection ids a desktop engine opens for itself to answer a machine's model requests start with this. The
3
+ * machine's engine pool sends such a connection only `model.relay.*` events, and no replay of its runs.
4
+ */
5
+ export const MODEL_RELAY_CONNECTION_PREFIX = 'relay-';
@@ -38,6 +38,9 @@ export interface TierDecision {
38
38
  reason: EfficiencyReason;
39
39
  evidence: 'policy' | 'pinned' | 'hint' | 'lexical' | 'escalation' | 'none';
40
40
  }
41
+ export declare const RISK_WORDS: RegExp;
42
+ export declare const MECHANICAL_WORDS: RegExp;
43
+ export declare const MECHANICAL_MAX_CHARS = 400;
41
44
  export declare function classifyTier(task: string, hints: DecisionBatch | undefined): {
42
45
  tier: ModelTier;
43
46
  evidence: 'hint' | 'lexical';
@@ -1,8 +1,8 @@
1
1
  import { DecisionPolicy } from './decision/DecisionPolicy.js';
2
2
  import { MODEL_TIERS } from './efficiency.js';
3
- const RISK_WORDS = /\b(security|auth(?:entication|orization|n)?|credential|secret|token|crypt|permission|sandbox|race condition|deadlock|memory leak|crash|segfault|corrupt|data loss|architecture|architectural|redesign|migration|migrate|concurrency|thread|lock|regression|flaky|intermittent|nondeterministic|root cause)\b/i;
4
- const MECHANICAL_WORDS = /\b(rename|typo|comment|docstring|jsdoc|format(?:ting)?|reformat|prettier|lint(?: fix)?|bump (?:the )?version|changelog|readme|whitespace|trailing|reorder imports|sort imports|unused import|remove unused|add (?:a )?log|console\.log|copyright|license header|spelling|wording)\b/i;
5
- const MECHANICAL_MAX_CHARS = 400;
3
+ export const RISK_WORDS = /\b(security|auth(?:entication|orization|n)?|credential|secret|token|crypt|permission|sandbox|race condition|deadlock|memory leak|crash|segfault|corrupt|data loss|architecture|architectural|redesign|migration|migrate|concurrency|thread|lock|regression|flaky|intermittent|nondeterministic|root cause)\b/i;
4
+ export const MECHANICAL_WORDS = /\b(rename|typo|comment|docstring|jsdoc|format(?:ting)?|reformat|prettier|lint(?: fix)?|bump (?:the )?version|changelog|readme|whitespace|trailing|reorder imports|sort imports|unused import|remove unused|add (?:a )?log|console\.log|copyright|license header|spelling|wording)\b/i;
5
+ export const MECHANICAL_MAX_CHARS = 400;
6
6
  const ESCALATION_REASONS = new Set(['repeatedToolFailure', 'maxIterations', 'maxCompletionRepairs', 'Prepared-step metadata allowance exhausted']);
7
7
  function configuredTarget(target, configured) {
8
8
  if (!target)
@@ -0,0 +1,21 @@
1
+ export declare function addProjectEntry(path: string, options?: {
2
+ name?: string;
3
+ directory?: string;
4
+ }): Promise<void>;
5
+ export declare function relinkProjectEntry(from: string, to: string, options?: {
6
+ name?: string;
7
+ directory?: string;
8
+ }): Promise<boolean>;
9
+ /**
10
+ * The id `path` answers to, storing `candidate` when it has none yet and returning whichever id is actually on the entry.
11
+ * Two tasks created at the same moment on a folder that has just been added would otherwise each store their own id, and
12
+ * the loser keeps an identity the registry never accepted, splitting the project's browser profiles for good.
13
+ */
14
+ export declare function ensureProjectIdentity(path: string, candidate: string, directory?: string): Promise<string | undefined>;
15
+ /**
16
+ * Remembers the project id a folder answers to, so a task started after a rename or a
17
+ * merge belongs to the same project as the tasks that were already there.
18
+ * An id already on the entry wins: a second rename must not restart the project's identity.
19
+ */
20
+ export declare function rememberProjectIdentity(path: string, id: string, directory?: string): Promise<void>;
21
+ export declare function removeProjectEntry(path: string, directory?: string): Promise<boolean>;