maka-agent 0.2.0-dev.14.20260902 → 0.2.0-dev.16.20260903

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 (160) hide show
  1. package/dist/cli-core.js +0 -1
  2. package/dist/pi-transcript.js +48 -0
  3. package/dist/pi-tui-form-interaction.js +497 -0
  4. package/dist/pi-tui-pickers.js +256 -55
  5. package/dist/pi-tui-runner.js +215 -12
  6. package/dist/runtime-host-cli.js +17 -42
  7. package/dist/runtime-host-lifecycle-transaction.js +31 -13
  8. package/dist/runtime-host-managed-lifecycle-manager.js +1 -1
  9. package/dist/runtime-host-onboarding.js +3 -1
  10. package/dist/runtime-host-openrc-service.js +469 -0
  11. package/dist/runtime-host-run-command.js +6 -2
  12. package/dist/runtime-host-service-command.js +3 -3
  13. package/dist/runtime-host-service-management-command.js +43 -15
  14. package/dist/runtime-host-session-channel.js +3 -0
  15. package/dist/runtime-host-session-driver.js +21 -6
  16. package/dist/runtime-host-setup-command.js +30 -31
  17. package/dist/tui-context-refresh.js +103 -0
  18. package/dist/tui-copy-catalog.js +73 -0
  19. package/native/runtime-host-peer/prebuilds/darwin-arm64/maka_runtime_host_peer.node +0 -0
  20. package/native/runtime-host-peer/prebuilds/linux-arm64/maka_runtime_host_peer.node +0 -0
  21. package/native/runtime-host-peer/prebuilds/linux-x64/maka_runtime_host_peer.node +0 -0
  22. package/native/runtime-host-peer/prebuilds/win32-x64/maka_runtime_host_peer.node +0 -0
  23. package/node_modules/@maka/core/dist/chat-model-choice.js +4 -0
  24. package/node_modules/@maka/core/dist/computer-use.js +27 -92
  25. package/node_modules/@maka/core/dist/interaction.js +577 -3
  26. package/node_modules/@maka/core/dist/llm-connections.js +4 -0
  27. package/node_modules/@maka/core/dist/local-memory.js +2 -2
  28. package/node_modules/@maka/core/dist/model-call-usage-projection.js +3 -0
  29. package/node_modules/@maka/core/dist/model-metadata.js +26 -7
  30. package/node_modules/@maka/core/dist/model-thinking.js +17 -0
  31. package/node_modules/@maka/core/dist/provider-registry.js +1 -0
  32. package/node_modules/@maka/core/dist/runtime-event.js +23 -1
  33. package/node_modules/@maka/core/dist/scheduled-task.js +4 -0
  34. package/node_modules/@maka/core/dist/session.js +95 -2
  35. package/node_modules/@maka/core/dist/text-sanitize.js +19 -0
  36. package/node_modules/@maka/core/dist/usage-ledger-merge.js +3 -0
  37. package/node_modules/@maka/core/dist/usage-record-schema.js +11 -5
  38. package/node_modules/@maka/core/dist/workhub-creation-intent.js +75 -9
  39. package/node_modules/@maka/core/dist/workhub-session-resolver.js +50 -0
  40. package/node_modules/@maka/core/package.json +2 -0
  41. package/node_modules/@maka/eval/dist/harbor-external-subject.js +2 -1
  42. package/node_modules/@maka/eval/dist/harness-executor.js +3 -2
  43. package/node_modules/@maka/eval/dist/install-preflight.js +39 -10
  44. package/node_modules/@maka/eval/dist/process-termination.js +83 -0
  45. package/node_modules/@maka/mcp/dist/sep-2243.js +3 -3
  46. package/node_modules/@maka/runtime/dist/agent-run.js +6 -3
  47. package/node_modules/@maka/runtime/dist/ai-sdk-backend.js +332 -118
  48. package/node_modules/@maka/runtime/dist/ai-sdk-compaction-contract.js +6 -1
  49. package/node_modules/@maka/runtime/dist/ai-sdk-compaction.js +171 -341
  50. package/node_modules/@maka/runtime/dist/bots/telegram-bridge.js +3 -26
  51. package/node_modules/@maka/runtime/dist/bundled-skill-catalog.generated.js +1 -1
  52. package/node_modules/@maka/runtime/dist/computer-use-codec.js +1 -141
  53. package/node_modules/@maka/runtime/dist/computer-use-tools.js +23 -105
  54. package/node_modules/@maka/runtime/dist/context-budget-policy.js +20 -26
  55. package/node_modules/@maka/runtime/dist/context-budget.js +18 -20
  56. package/node_modules/@maka/runtime/dist/conversation-copy.js +23 -19
  57. package/node_modules/@maka/runtime/dist/cua-frame-state.js +1 -69
  58. package/node_modules/@maka/runtime/dist/github-copilot-oauth-enrollment.js +267 -0
  59. package/node_modules/@maka/runtime/dist/history-compact-checkpoint.js +6 -9
  60. package/node_modules/@maka/runtime/dist/history-compact-summarizer.js +67 -23
  61. package/node_modules/@maka/runtime/dist/history-compact-summary-validation.js +11 -18
  62. package/node_modules/@maka/runtime/dist/history-compaction.js +66 -73
  63. package/node_modules/@maka/runtime/dist/interaction-authority.js +69 -0
  64. package/node_modules/@maka/runtime/dist/mcp-tools.js +13 -1
  65. package/node_modules/@maka/runtime/dist/memory-extraction.js +11 -11
  66. package/node_modules/@maka/runtime/dist/model-adapter.js +24 -6
  67. package/node_modules/@maka/runtime/dist/model-factory.js +14 -5
  68. package/node_modules/@maka/runtime/dist/oauth-provider-contracts.js +28 -1
  69. package/node_modules/@maka/runtime/dist/openai-codex-history-compactor.js +7 -6
  70. package/node_modules/@maka/runtime/dist/opencode-session-header.js +29 -0
  71. package/node_modules/@maka/runtime/dist/runtime-event-read-model.js +11 -1
  72. package/node_modules/@maka/runtime/dist/runtime-kernel.js +12 -11
  73. package/node_modules/@maka/runtime/dist/runtime-read-model.js +51 -13
  74. package/node_modules/@maka/runtime/dist/scheduled-task-tools.js +3 -0
  75. package/node_modules/@maka/runtime/dist/session-event-runtime-mapper.js +26 -0
  76. package/node_modules/@maka/runtime/dist/session-manager.js +1 -0
  77. package/node_modules/@maka/runtime/dist/session-projection-helpers.js +16 -1
  78. package/node_modules/@maka/runtime/dist/stream-graph-projection.js +6 -0
  79. package/node_modules/@maka/runtime/dist/stream-graph-read-model.js +5 -0
  80. package/node_modules/@maka/runtime/dist/stream-usage-fallback-fetch.js +74 -0
  81. package/node_modules/@maka/runtime/dist/subscription-credentials.js +51 -4
  82. package/node_modules/@maka/runtime/dist/subscription-model-fetch.js +47 -3
  83. package/node_modules/@maka/runtime/dist/system-prompt/main-session-prompt.js +20 -1
  84. package/node_modules/@maka/runtime/dist/test-connection.js +18 -11
  85. package/node_modules/@maka/runtime/dist/tool-runtime.js +183 -0
  86. package/node_modules/@maka/runtime/package.json +1 -0
  87. package/node_modules/@maka/runtime-host/dist/adapter/session-projector.js +14 -0
  88. package/node_modules/@maka/runtime-host/dist/client/client-capability-channel.js +84 -1
  89. package/node_modules/@maka/runtime-host/dist/client/host-profile.js +68 -51
  90. package/node_modules/@maka/runtime-host/dist/client/index.js +1 -1
  91. package/node_modules/@maka/runtime-host/dist/client/owner-connection-code.js +8 -5
  92. package/node_modules/@maka/runtime-host/dist/client/peer-client.js +342 -36
  93. package/node_modules/@maka/runtime-host/dist/client/reconnect-lifecycle.js +60 -4
  94. package/node_modules/@maka/runtime-host/dist/peer-mesh/index.js +3 -3
  95. package/node_modules/@maka/runtime-host/dist/peer-mesh/limits.js +1 -1
  96. package/node_modules/@maka/runtime-host/dist/peer-mesh/model.js +48 -60
  97. package/node_modules/@maka/runtime-host/dist/peer-mesh/node.js +847 -431
  98. package/node_modules/@maka/runtime-host/dist/peer-mesh/owner.js +33 -74
  99. package/node_modules/@maka/runtime-host/dist/peer-mesh/store.js +82 -69
  100. package/node_modules/@maka/runtime-host/dist/peer-reachability/index.js +21 -0
  101. package/node_modules/@maka/runtime-host/dist/peer-reachability/model.js +202 -0
  102. package/node_modules/@maka/runtime-host/dist/peer-reachability/owner.js +119 -0
  103. package/node_modules/@maka/runtime-host/dist/peer-reachability/publisher.js +245 -0
  104. package/node_modules/@maka/runtime-host/dist/protocol/agent-graph.js +4 -1
  105. package/node_modules/@maka/runtime-host/dist/protocol/client-capability.js +68 -0
  106. package/node_modules/@maka/runtime-host/dist/protocol/connection-effects.js +15 -8
  107. package/node_modules/@maka/runtime-host/dist/protocol/host-status.js +7 -23
  108. package/node_modules/@maka/runtime-host/dist/protocol/index.js +47 -1
  109. package/node_modules/@maka/runtime-host/dist/protocol/oauth.js +25 -1
  110. package/node_modules/@maka/runtime-host/dist/protocol/operations.js +1 -0
  111. package/node_modules/@maka/runtime-host/dist/protocol/peer-mesh.js +14 -31
  112. package/node_modules/@maka/runtime-host/dist/protocol/scheduled-task.js +19 -6
  113. package/node_modules/@maka/runtime-host/dist/protocol/session-collaboration.js +36 -1
  114. package/node_modules/@maka/runtime-host/dist/protocol/session-revision.js +11 -2
  115. package/node_modules/@maka/runtime-host/dist/protocol/usage-pricing.js +16 -2
  116. package/node_modules/@maka/runtime-host/dist/protocol/workhub-coordination.js +51 -2
  117. package/node_modules/@maka/runtime-host/dist/server/access-authority.js +35 -2
  118. package/node_modules/@maka/runtime-host/dist/server/access-credential-store.js +1 -0
  119. package/node_modules/@maka/runtime-host/dist/server/client-capability-coordinator.js +12 -10
  120. package/node_modules/@maka/runtime-host/dist/server/client-capability-invocation-broker.js +163 -30
  121. package/node_modules/@maka/runtime-host/dist/server/connection-effect-coordinator.js +38 -14
  122. package/node_modules/@maka/runtime-host/dist/server/daily-review-coordinator.js +45 -22
  123. package/node_modules/@maka/runtime-host/dist/server/execution-composition.js +78 -20
  124. package/node_modules/@maka/runtime-host/dist/server/execution-model-authority.js +4 -2
  125. package/node_modules/@maka/runtime-host/dist/server/execution-model-composition.js +2 -1
  126. package/node_modules/@maka/runtime-host/dist/server/execution-service.js +49 -28
  127. package/node_modules/@maka/runtime-host/dist/server/host-kernel.js +3 -6
  128. package/node_modules/@maka/runtime-host/dist/server/interaction-coordinator.js +130 -27
  129. package/node_modules/@maka/runtime-host/dist/server/interaction-projection.js +30 -1
  130. package/node_modules/@maka/runtime-host/dist/server/listener-set.js +2 -4
  131. package/node_modules/@maka/runtime-host/dist/server/message-coordinator.js +16 -3
  132. package/node_modules/@maka/runtime-host/dist/server/oauth-coordinator.js +66 -3
  133. package/node_modules/@maka/runtime-host/dist/server/oauth-execution-authority.js +20 -3
  134. package/node_modules/@maka/runtime-host/dist/server/peer-listener.js +8 -21
  135. package/node_modules/@maka/runtime-host/dist/server/peer-mesh-authority.js +1 -1
  136. package/node_modules/@maka/runtime-host/dist/server/root-turn-coordinator.js +6 -3
  137. package/node_modules/@maka/runtime-host/dist/server/scheduled-task-coordinator.js +72 -12
  138. package/node_modules/@maka/runtime-host/dist/server/session-revision-coordinator.js +48 -18
  139. package/node_modules/@maka/runtime-host/dist/server/session-turn-access-request-coordinator.js +43 -26
  140. package/node_modules/@maka/runtime-host/dist/server/usage-pricing-coordinator.js +15 -1
  141. package/node_modules/@maka/runtime-host/dist/server/workhub-coordination-action-gate.js +229 -4
  142. package/node_modules/@maka/runtime-host/dist/server/workhub-coordination-coordinator.js +146 -8
  143. package/node_modules/@maka/runtime-host/dist/transport/peer-native.js +32 -6
  144. package/node_modules/@maka/runtime-host/package.json +1 -0
  145. package/node_modules/@maka/storage/dist/agent-run-store.js +18 -5
  146. package/node_modules/@maka/storage/dist/codex-session-adapter.js +14 -0
  147. package/node_modules/@maka/storage/dist/execution-stores.js +5 -0
  148. package/node_modules/@maka/storage/dist/interaction-store.js +13 -1
  149. package/node_modules/@maka/storage/dist/runtime-policy/connection-catalog-document.js +9 -3
  150. package/node_modules/@maka/storage/dist/runtime-policy/coordinator.js +49 -19
  151. package/node_modules/@maka/storage/dist/runtime-policy/oauth-login-receipt-document.js +3 -1
  152. package/node_modules/@maka/storage/dist/runtime-policy/onboarding-transaction.js +13 -5
  153. package/node_modules/@maka/storage/dist/session-copy-cleanup.js +4 -2
  154. package/node_modules/@maka/storage/dist/session-store.js +39 -6
  155. package/node_modules/@maka/storage/dist/sqlite-runtime-store.js +16 -9
  156. package/node_modules/@maka/storage/dist/sqlite-session-metadata-schema.js +30 -3
  157. package/node_modules/@maka/storage/dist/sqlite-session-metadata-store.js +117 -0
  158. package/node_modules/@maka/storage/dist/sqlite-usage-store.js +29 -6
  159. package/node_modules/@maka/storage/dist/usage-stores.js +3 -1
  160. package/package.json +1 -1
@@ -17,7 +17,7 @@
17
17
  * under the License.
18
18
  */
19
19
  import { randomUUID } from 'node:crypto';
20
- import { CLIENT_CAPABILITY_MAX_RESULT_BYTES, CLIENT_CAPABILITY_RESULT_CHUNK_MAX_BYTES, decodeClientCapabilityReplaceInput, decodeClientCapabilityResult, } from '../protocol/index.js';
20
+ import { CLIENT_CAPABILITY_MAX_RESULT_BYTES, CLIENT_CAPABILITY_RESULT_CHUNK_MAX_BYTES, decodeClientCapabilityClientFrame, decodeClientCapabilityReplaceInput, decodeClientCapabilityResult, } from '../protocol/index.js';
21
21
  /** Client-owned registration, reverse invocation, and teardown lifecycle. */
22
22
  export class ClientCapabilityChannel {
23
23
  #options;
@@ -110,6 +110,7 @@ export class ClientCapabilityChannel {
110
110
  invocation.released = true;
111
111
  invocation.controller.abort(new DOMException('Client Capability invocation was cancelled', 'AbortError'));
112
112
  invocation.admission?.reject(capabilityInvocationAbortReason(invocation));
113
+ invocation.interaction?.reject(capabilityInvocationAbortReason(invocation));
113
114
  return;
114
115
  }
115
116
  case 'client.capability.release': {
@@ -119,6 +120,7 @@ export class ClientCapabilityChannel {
119
120
  invocation.released = true;
120
121
  invocation.controller.abort(new DOMException('Client Capability invocation was released', 'AbortError'));
121
122
  invocation.admission?.reject(capabilityInvocationAbortReason(invocation));
123
+ invocation.interaction?.reject(capabilityInvocationAbortReason(invocation));
122
124
  this.#invocations.delete(frame.invocationId);
123
125
  return;
124
126
  }
@@ -133,6 +135,16 @@ export class ClientCapabilityChannel {
133
135
  }
134
136
  return;
135
137
  }
138
+ case 'client.capability.interaction_result': {
139
+ const invocation = this.#invocations.get(frame.invocationId);
140
+ const interaction = invocation?.interaction;
141
+ if (!interaction ||
142
+ interaction.interactionId !== frame.interactionId ||
143
+ !interaction.resolve(frame.result)) {
144
+ throw new Error('Runtime Host returned an unmatched capability interaction result');
145
+ }
146
+ return;
147
+ }
136
148
  }
137
149
  }
138
150
  close(error) {
@@ -143,6 +155,7 @@ export class ClientCapabilityChannel {
143
155
  invocation.released = true;
144
156
  invocation.controller.abort(error);
145
157
  invocation.admission?.reject(error);
158
+ invocation.interaction?.reject(error);
146
159
  }
147
160
  this.#invocations.clear();
148
161
  const providers = new Set([...this.#registrations.values()].map((registration) => registration.provider));
@@ -254,13 +267,53 @@ export class ClientCapabilityChannel {
254
267
  })
255
268
  .catch((error) => this.#options.onFailure(asError(error)));
256
269
  };
270
+ const requestInteraction = async (request) => {
271
+ if (!accepted) {
272
+ throw new Error('Client Capability interaction requires an admitted invocation');
273
+ }
274
+ if (invocation.released)
275
+ throw capabilityInvocationAbortReason(invocation);
276
+ if (invocation.interaction) {
277
+ throw new Error('Client Capability invocation already has a pending interaction');
278
+ }
279
+ const interactionId = randomUUID();
280
+ const frame = decodeClientCapabilityClientFrame({
281
+ kind: 'client.capability.interaction_request',
282
+ invocationId,
283
+ interactionId,
284
+ request,
285
+ });
286
+ if (frame.kind !== 'client.capability.interaction_request') {
287
+ throw new Error('Client Capability interaction request was not canonical');
288
+ }
289
+ const interaction = createClientCapabilityPendingInteraction(interactionId);
290
+ invocation.interaction = interaction;
291
+ try {
292
+ try {
293
+ await this.#options.write(frame);
294
+ }
295
+ catch (error) {
296
+ interaction.reject(error);
297
+ throw error;
298
+ }
299
+ return await interaction.promise;
300
+ }
301
+ finally {
302
+ if (invocation.interaction === interaction)
303
+ invocation.interaction = undefined;
304
+ }
305
+ };
257
306
  const result = decodeClientCapabilityResult(await execute({
258
307
  signal: invocation.controller.signal,
259
308
  accept,
260
309
  progress,
310
+ requestInteraction,
261
311
  }));
262
312
  if (invocation.released)
263
313
  return;
314
+ if (invocation.interaction) {
315
+ throw new Error('Client Capability provider returned with a pending interaction');
316
+ }
264
317
  await accept({ kind: 'none' });
265
318
  await this.#sendResult(invocationId, result, invocation);
266
319
  }
@@ -366,6 +419,36 @@ function createClientCapabilityAdmission() {
366
419
  },
367
420
  };
368
421
  }
422
+ function createClientCapabilityPendingInteraction(interactionId) {
423
+ let state = 'pending';
424
+ let resolvePromise;
425
+ let rejectPromise;
426
+ const promise = new Promise((resolve, reject) => {
427
+ resolvePromise = resolve;
428
+ rejectPromise = reject;
429
+ });
430
+ // A buggy provider may start but not await the callback. The invocation still
431
+ // fails closed, while channel teardown must not create an unhandled rejection.
432
+ void promise.catch(() => undefined);
433
+ return {
434
+ interactionId,
435
+ promise,
436
+ resolve: (result) => {
437
+ if (state !== 'pending')
438
+ return false;
439
+ state = 'resolved';
440
+ resolvePromise(result);
441
+ return true;
442
+ },
443
+ reject: (error) => {
444
+ if (state !== 'pending')
445
+ return false;
446
+ state = 'rejected';
447
+ rejectPromise(error);
448
+ return true;
449
+ },
450
+ };
451
+ }
369
452
  function capabilityInvocationAbortReason(invocation) {
370
453
  return (invocation.controller.signal.reason ??
371
454
  new DOMException('Client Capability invocation was released', 'AbortError'));
@@ -26,21 +26,19 @@ import { INTERACTIVE_RUNTIME_HOST_COMPOSITION_ID, isCanonicalRuntimeHostWebSocke
26
26
  import { connectRemoteRuntimeHost, connectRuntimeHostMessageTransport, normalizeRemoteRuntimeHostUrl, } from './connection.js';
27
27
  import { FramedByteStreamTransport } from '../transport/framed-byte-stream-transport.js';
28
28
  import { RuntimeHostPeerByteStream, RuntimeHostPeerError, readRuntimeHostPeerAuthenticationResult, writeRuntimeHostPeerAuthentication, } from '../transport/peer-native.js';
29
+ import { decodeSignedPeerReachabilityLease, isPeerReachabilityLeaseRecoverable, } from '../peer-reachability/model.js';
29
30
  import { RuntimeHostPermanentReconnectError } from './reconnect-lifecycle.js';
30
31
  import { RuntimeHostRemoteCompatibilityError } from './remote-compatibility-error.js';
31
32
  import { normalizeRuntimeHostSshDestination, openRuntimeHostSshTunnel, } from './ssh-tunnel.js';
32
33
  import { activateRuntimeHostSshOperator } from './ssh-operator-activation.js';
33
34
  import { waitForRuntimeHostReady } from './wait-for-ready.js';
34
35
  import { connectRuntimeHostWslEnvironment, normalizeRuntimeHostWslDistribution, normalizeRuntimeHostWslOperatorPath, } from './wsl-environment.js';
35
- const PROFILE_SCHEMA_VERSION = 3;
36
+ const PROFILE_SCHEMA_VERSION = 4;
36
37
  const CLIENT_PROFILE_DOCUMENT_NAME = 'runtime-host-profiles.json';
37
38
  const PROFILE_DOCUMENT_MAX_BYTES = 64 * 1024;
38
39
  const PROFILE_COUNT_MAX = 32;
39
40
  const PROFILE_NAME_MAX_BYTES = 128;
40
41
  const PROFILE_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
41
- const PEER_ID_MAX_BYTES = 160;
42
- const PEER_ADDRESS_MAX_BYTES = 2 * 1024;
43
- const PEER_ROUTE_MAX = 16;
44
42
  const DEFAULT_PEER_HANDSHAKE_TIMEOUT_MS = 5_000;
45
43
  const PROFILE_CREDENTIAL_RECORD_PREFIX = 'maka-runtime-host-profile-credential-v1:';
46
44
  const PROFILE_INCARNATION_ID_MAX_BYTES = 128;
@@ -72,8 +70,8 @@ export function sameRemoteRuntimeHostProfileTarget(left, right) {
72
70
  }
73
71
  export class RuntimeHostProfileConnectionError extends RuntimeHostPermanentReconnectError {
74
72
  reason;
75
- constructor(reason, message) {
76
- super(message);
73
+ constructor(reason, message, options) {
74
+ super(message, options);
77
75
  this.reason = reason;
78
76
  this.name = 'RuntimeHostProfileConnectionError';
79
77
  }
@@ -279,13 +277,42 @@ function requireConnectOnlySshEndpoint(transport) {
279
277
  export async function connectPeerRuntimeHost(input) {
280
278
  input.signal?.throwIfAborted();
281
279
  const handshakeTimeoutMs = input.handshakeTimeoutMs ?? DEFAULT_PEER_HANDSHAKE_TIMEOUT_MS;
282
- const stream = await input.peerClient.connect({
283
- peerId: input.transport.peerId,
284
- routeHints: input.transport.routeHints,
285
- coordinationRelays: input.transport.coordinationRelays,
286
- directDeadlineMs: Math.min(input.connectTimeoutMs ?? 40_000, 120_000),
287
- ...(input.refreshPeerRoutes === undefined ? {} : { refreshRoutes: input.refreshPeerRoutes }),
288
- }, input.signal, input.onConnectionPhase);
280
+ const peerId = input.transport.reachability.lease.peerId;
281
+ let reachability;
282
+ try {
283
+ reachability = input.peerClient.observeAuthenticatedReachability({
284
+ expectedPeerId: peerId,
285
+ value: input.transport.reachability,
286
+ allowHistorical: true,
287
+ });
288
+ }
289
+ catch (cause) {
290
+ throw new RuntimeHostProfileConnectionError('target_mismatch', `Runtime Host profile ${input.profileId} contains invalid peer reachability evidence`, { cause });
291
+ }
292
+ const bootstrap = isPeerReachabilityLeaseRecoverable(reachability.lease, Date.now())
293
+ ? reachability.lease
294
+ : undefined;
295
+ let stream;
296
+ try {
297
+ stream = await input.peerClient.connect({
298
+ peerId,
299
+ routeHints: bootstrap?.directRoutes ?? [],
300
+ coordinationRelays: bootstrap?.coordinationRoutes ?? [],
301
+ directDeadlineMs: Math.min(input.connectTimeoutMs ?? 40_000, 120_000),
302
+ ...(input.refreshPeerRoutes === undefined
303
+ ? {}
304
+ : { refreshRoutes: input.refreshPeerRoutes }),
305
+ }, input.signal, input.onConnectionPhase);
306
+ }
307
+ catch (cause) {
308
+ if (cause instanceof RuntimeHostPeerError && cause.code === 'peer_identity_mismatch') {
309
+ throw new RuntimeHostProfileConnectionError('target_mismatch', `Runtime Host profile ${input.profileId} resolved to a different peer identity`, { cause });
310
+ }
311
+ if (cause instanceof RuntimeHostPeerError && cause.code === 'peer_native_unavailable') {
312
+ throw runtimeHostPeerUnavailableError(cause);
313
+ }
314
+ throw cause;
315
+ }
289
316
  const abort = () => stream.abort();
290
317
  input.signal?.addEventListener('abort', abort, { once: true });
291
318
  if (input.signal?.aborted)
@@ -312,8 +339,11 @@ export async function connectPeerRuntimeHost(input) {
312
339
  handshakeTimeoutMs,
313
340
  onHostStatus: (status) => {
314
341
  const endpoint = status.peerEndpoint;
315
- if (endpoint?.peerId === input.transport.peerId) {
316
- input.peerClient.observeAuthenticatedRoutes(endpoint);
342
+ if (endpoint) {
343
+ input.peerClient.observeAuthenticatedReachability({
344
+ value: endpoint,
345
+ expectedPeerId: peerId,
346
+ });
317
347
  }
318
348
  input.onHostStatus?.(status);
319
349
  },
@@ -353,7 +383,10 @@ function notifyConnectionPhase(observer, phase) {
353
383
  function requireRuntimeHostPeerClient(peerClient) {
354
384
  if (peerClient)
355
385
  return peerClient;
356
- throw new RuntimeHostPeerError('peer_native_unavailable', 'Experimental direct peer requires a Client peer endpoint owner');
386
+ throw runtimeHostPeerUnavailableError(new RuntimeHostPeerError('peer_native_unavailable', 'Experimental direct peer requires a Client peer endpoint owner'));
387
+ }
388
+ function runtimeHostPeerUnavailableError(cause) {
389
+ return new RuntimeHostPermanentReconnectError('Runtime Host peer networking is unavailable in this Maka build', { cause });
357
390
  }
358
391
  export function remoteRuntimeHostUnavailableError(subject, reason) {
359
392
  let message;
@@ -385,6 +418,7 @@ export function decodeRuntimeHostProfileDocument(value) {
385
418
  ]);
386
419
  if (record.schemaVersion !== 1 &&
387
420
  record.schemaVersion !== 2 &&
421
+ record.schemaVersion !== 3 &&
388
422
  record.schemaVersion !== PROFILE_SCHEMA_VERSION) {
389
423
  throw new Error('Runtime Host profile document has an unsupported schema');
390
424
  }
@@ -397,10 +431,14 @@ export function decodeRuntimeHostProfileDocument(value) {
397
431
  (profile.transport.kind === 'ssh' && profile.transport.activation !== undefined))) {
398
432
  throw new Error('Runtime Host profile schema 1 cannot contain activation');
399
433
  }
400
- if (record.schemaVersion !== PROFILE_SCHEMA_VERSION &&
434
+ if (record.schemaVersion < 3 &&
401
435
  profiles.some((profile) => profile.kind === 'remote' && profile.access === 'session_guest')) {
402
436
  throw new Error('Runtime Host profile schema 3 is required for restricted access');
403
437
  }
438
+ if (record.schemaVersion < 4 &&
439
+ profiles.some((profile) => profile.kind === 'remote' && profile.transport.kind === 'libp2p-direct')) {
440
+ throw new Error('Runtime Host profile schema 4 is required for Direct peer reachability');
441
+ }
404
442
  const ids = new Set();
405
443
  for (const profile of profiles) {
406
444
  if (ids.has(profile.id))
@@ -820,21 +858,16 @@ export function decodeRuntimeHostRemoteTransport(value) {
820
858
  if (kind === 'libp2p-direct') {
821
859
  const record = requireExactRecord(value, 'Runtime Host direct peer transport', [
822
860
  'kind',
823
- 'peerId',
824
- 'routeHints',
825
- 'coordinationRelays',
861
+ 'reachability',
826
862
  ]);
827
- const peerId = requireBoundedToken(record.peerId, 'Runtime Host peer id', PEER_ID_MAX_BYTES);
828
- const routeHints = requirePeerAddresses(record.routeHints, 'Runtime Host peer route hints');
829
- const coordinationRelays = requirePeerAddresses(record.coordinationRelays, 'Runtime Host coordination relays');
830
- if (routeHints.length === 0 && coordinationRelays.length === 0) {
863
+ const reachability = decodeSignedPeerReachabilityLease(record.reachability);
864
+ if (reachability.lease.directRoutes.length === 0 &&
865
+ reachability.lease.coordinationRoutes.length === 0) {
831
866
  throw new Error('Runtime Host direct peer transport requires at least one route');
832
867
  }
833
868
  return Object.freeze({
834
869
  kind: 'libp2p-direct',
835
- peerId,
836
- routeHints,
837
- coordinationRelays,
870
+ reachability,
838
871
  });
839
872
  }
840
873
  throw new Error('Runtime Host transport kind is invalid');
@@ -976,26 +1009,8 @@ function transportCredentialBinding(transport) {
976
1009
  ? `${transport.destination}\0${transport.sshPort ?? ''}\0activate\0${transport.activation.operatorPath}`
977
1010
  : `${transport.destination}\0${transport.sshPort ?? ''}\0${transport.remotePort}\0${transport.websocketPath}`;
978
1011
  case 'libp2p-direct':
979
- return transport.peerId;
980
- }
981
- }
982
- function requirePeerAddresses(value, label) {
983
- if (!Array.isArray(value) || value.length > PEER_ROUTE_MAX) {
984
- throw new Error(`${label} must be an array with at most ${PEER_ROUTE_MAX} entries`);
985
- }
986
- const addresses = value.map((entry) => {
987
- const address = requireString(entry, label);
988
- if (!address.startsWith('/') ||
989
- Buffer.byteLength(address, 'utf8') > PEER_ADDRESS_MAX_BYTES ||
990
- /[\s\u0000-\u001f\u007f]/u.test(address)) {
991
- throw new Error(`${label} contains an invalid multiaddr`);
992
- }
993
- return address;
994
- });
995
- if (new Set(addresses).size !== addresses.length) {
996
- throw new Error(`${label} contains duplicates`);
1012
+ return transport.reachability.lease.peerId;
997
1013
  }
998
- return Object.freeze(addresses);
999
1014
  }
1000
1015
  function requireBoundedToken(value, label, maxBytes) {
1001
1016
  const token = requireString(value, label);
@@ -1092,12 +1107,14 @@ function emptyProfileDocument() {
1092
1107
  return Object.freeze({ schemaVersion: PROFILE_SCHEMA_VERSION, profiles: Object.freeze([]) });
1093
1108
  }
1094
1109
  async function writeProfileDocument(path, document) {
1095
- const schemaVersion = document.profiles.some((profile) => profile.kind === 'remote' && profile.access === 'session_guest')
1110
+ const schemaVersion = document.profiles.some((profile) => profile.kind === 'remote' && profile.transport.kind === 'libp2p-direct')
1096
1111
  ? PROFILE_SCHEMA_VERSION
1097
- : document.profiles.some((profile) => profile.kind === 'environment' ||
1098
- (profile.transport.kind === 'ssh' && profile.transport.activation !== undefined))
1099
- ? 2
1100
- : 1;
1112
+ : document.profiles.some((profile) => profile.kind === 'remote' && profile.access === 'session_guest')
1113
+ ? 3
1114
+ : document.profiles.some((profile) => profile.kind === 'environment' ||
1115
+ (profile.transport.kind === 'ssh' && profile.transport.activation !== undefined))
1116
+ ? 2
1117
+ : 1;
1101
1118
  const encoded = `${JSON.stringify({ ...document, schemaVersion }, null, 2)}\n`;
1102
1119
  if (Buffer.byteLength(encoded, 'utf8') > PROFILE_DOCUMENT_MAX_BYTES) {
1103
1120
  throw new Error('Runtime Host profile document exceeds its size limit');
@@ -42,5 +42,5 @@ export { projectSessionCatalogSummary } from './session-catalog-summary.js';
42
42
  export { consumeAccessCredentialDelivery } from '../control/access-credential-delivery.js';
43
43
  export { decodeRuntimeHostOwnerConnectionCode, encodeRuntimeHostOwnerConnectionCode, issueRuntimeHostOwnerConnectionCode, REMOTE_DESKTOP_OWNER_ACCESS_POLICY, } from './owner-connection-code.js';
44
44
  export { ensureRuntimeHostPeerIdentity } from '../transport/peer-native.js';
45
- export { createRuntimeHostPeerClient, createRuntimeHostPeerClientFromEnvironment, } from './peer-client.js';
45
+ export { createRuntimeHostPeerClient, createRuntimeHostPeerClientFromEnvironment, RuntimeHostPeerReachabilityUnavailableError, } from './peer-client.js';
46
46
  export { createOAuthPresentationClientProvider, } from './oauth-presentation.js';
@@ -20,7 +20,7 @@ import { z } from 'zod';
20
20
  import { consumeAccessCredentialDelivery } from '../control/access-credential-delivery.js';
21
21
  import { REMOTE_OWNER_OPERATION_GRANTS, requireHostRootId } from '../protocol/index.js';
22
22
  import { decodeRuntimeHostRemoteTransport, RUNTIME_HOST_ACCESS_CREDENTIAL_MAX_BYTES, } from './host-profile.js';
23
- const PREFIX = 'maka-runtime-host:connect:v1:';
23
+ const PREFIX = 'maka-runtime-host:connect:v2:';
24
24
  const ENCODED_MAX_BYTES = 48 * 1024;
25
25
  export const REMOTE_DESKTOP_OWNER_ACCESS_POLICY = Object.freeze({
26
26
  principalKind: 'remote_owner',
@@ -35,7 +35,7 @@ const boundedString = (maxBytes) => z
35
35
  const nameSchema = boundedString(128);
36
36
  const payloadSchema = z
37
37
  .object({
38
- schemaVersion: z.literal(1),
38
+ schemaVersion: z.literal(2),
39
39
  name: boundedString(128),
40
40
  rootId: z.string().refine((value) => {
41
41
  try {
@@ -61,10 +61,13 @@ export async function issueRuntimeHostOwnerConnectionCode(input) {
61
61
  const endpoint = (await input.client.status()).peerEndpoint;
62
62
  if (!endpoint)
63
63
  throw new Error('Runtime Host Direct peer is not available');
64
- if (input.expectedPeerId && endpoint.peerId !== input.expectedPeerId) {
64
+ if (input.expectedPeerId && endpoint.lease.peerId !== input.expectedPeerId) {
65
65
  throw new Error('Runtime Host Direct peer identity changed');
66
66
  }
67
- const transport = requireDirectPeerTransport({ kind: 'libp2p-direct', ...endpoint });
67
+ const transport = requireDirectPeerTransport({
68
+ kind: 'libp2p-direct',
69
+ reachability: endpoint,
70
+ });
68
71
  const prepared = await input.client.request('access.credential.prepare', {
69
72
  ...REMOTE_DESKTOP_OWNER_ACCESS_POLICY,
70
73
  principalId: input.principalId,
@@ -75,7 +78,7 @@ export async function issueRuntimeHostOwnerConnectionCode(input) {
75
78
  }
76
79
  export function encodeRuntimeHostOwnerConnectionCode(input) {
77
80
  const transport = requireDirectPeerTransport(input.transport);
78
- const payload = payloadSchema.parse({ schemaVersion: 1, ...input, transport });
81
+ const payload = payloadSchema.parse({ schemaVersion: 2, ...input, transport });
79
82
  const encoded = Buffer.from(JSON.stringify(payload), 'utf8').toString('base64url');
80
83
  if (Buffer.byteLength(encoded, 'utf8') > ENCODED_MAX_BYTES) {
81
84
  throw new RangeError('Runtime Host connection code is too large');