@qvac/core 0.10.4 → 0.11.1

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 (104) hide show
  1. package/CHANGELOG.md +114 -1
  2. package/dist-lib/index.d.ts +1 -0
  3. package/dist-lib/index.js +1 -0
  4. package/dist-lib/lib/api/chunk-delta.d.ts +6 -2
  5. package/dist-lib/lib/api/chunk-delta.js +69 -7
  6. package/dist-lib/lib/api/chunks.js +6 -2
  7. package/dist-lib/lib/api/config.d.ts +4 -1
  8. package/dist-lib/lib/api/config.js +23 -4
  9. package/dist-lib/lib/api/devices.js +3 -1
  10. package/dist-lib/lib/api/rag.js +4 -2
  11. package/dist-lib/lib/assistant/harness.d.ts +3 -1
  12. package/dist-lib/lib/assistant/harness.js +10 -0
  13. package/dist-lib/lib/assistant/history.d.ts +2 -1
  14. package/dist-lib/lib/assistant/history.js +39 -6
  15. package/dist-lib/lib/assistant/message.d.ts +3 -0
  16. package/dist-lib/lib/assistant/message.js +10 -2
  17. package/dist-lib/lib/assistant/pdf-budget.d.ts +3 -0
  18. package/dist-lib/lib/assistant/pdf-budget.js +13 -0
  19. package/dist-lib/lib/assistant-tree.d.ts +1 -0
  20. package/dist-lib/lib/assistant-tree.js +10 -3
  21. package/dist-lib/lib/assistant.d.ts +5 -1
  22. package/dist-lib/lib/assistant.js +112 -37
  23. package/dist-lib/lib/client.js +10 -1
  24. package/dist-lib/lib/core/archived-mesh.js +1 -0
  25. package/dist-lib/lib/core/chunk.d.ts +5 -2
  26. package/dist-lib/lib/core/chunk.js +74 -18
  27. package/dist-lib/lib/core/collections.d.ts +1 -0
  28. package/dist-lib/lib/core/collections.js +1 -0
  29. package/dist-lib/lib/core/config-value.d.ts +6 -0
  30. package/dist-lib/lib/core/config-value.js +14 -0
  31. package/dist-lib/lib/core/contract/chat.d.ts +1 -0
  32. package/dist-lib/lib/core/contract/chat.js +8 -1
  33. package/dist-lib/lib/core/contract/chunks.js +20 -6
  34. package/dist-lib/lib/core/contract/compute.d.ts +1 -1
  35. package/dist-lib/lib/core/contract/compute.js +2 -1
  36. package/dist-lib/lib/core/contract/devices.js +3 -2
  37. package/dist-lib/lib/core/departures.js +7 -0
  38. package/dist-lib/lib/core/devices.d.ts +2 -1
  39. package/dist-lib/lib/core/devices.js +4 -2
  40. package/dist-lib/lib/core/document-attachment.d.ts +7 -0
  41. package/dist-lib/lib/core/document-attachment.js +6 -0
  42. package/dist-lib/lib/core/files/index.d.ts +1 -1
  43. package/dist-lib/lib/core/files/index.js +1 -1
  44. package/dist-lib/lib/core/files/rag/index.d.ts +1 -1
  45. package/dist-lib/lib/core/files/rag/index.js +1 -1
  46. package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +1 -0
  47. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +4 -1
  48. package/dist-lib/lib/core/identity.d.ts +2 -0
  49. package/dist-lib/lib/core/identity.js +19 -0
  50. package/dist-lib/lib/core/inference-calls-receiver.js +2 -2
  51. package/dist-lib/lib/core/known-sender.d.ts +3 -0
  52. package/dist-lib/lib/core/known-sender.js +26 -0
  53. package/dist-lib/lib/core/local/index.d.ts +2 -0
  54. package/dist-lib/lib/core/local/index.js +17 -10
  55. package/dist-lib/lib/core/mesh.d.ts +5 -1
  56. package/dist-lib/lib/core/mesh.js +6 -6
  57. package/dist-lib/lib/core/migrations/chat-chunks.d.ts +4 -0
  58. package/dist-lib/lib/core/migrations/chat-chunks.js +43 -0
  59. package/dist-lib/lib/core/pdf/rasterize.d.ts +0 -1
  60. package/dist-lib/lib/core/pdf/rasterize.js +0 -1
  61. package/dist-lib/lib/core/skill-overlay-receiver.js +2 -2
  62. package/dist-lib/lib/core/skill-secrets.js +2 -2
  63. package/dist-lib/lib/core.d.ts +12 -1
  64. package/dist-lib/lib/core.js +75 -11
  65. package/dist-lib/lib/credential-pusher.d.ts +1 -0
  66. package/dist-lib/lib/credential-pusher.js +34 -48
  67. package/dist-lib/lib/device-tree.d.ts +18 -0
  68. package/dist-lib/lib/device-tree.js +32 -0
  69. package/dist-lib/lib/errors.d.ts +8 -0
  70. package/dist-lib/lib/errors.js +8 -0
  71. package/dist-lib/lib/oauth/flow-core.js +2 -0
  72. package/dist-lib/lib/oauth/loopback.d.ts +1 -0
  73. package/dist-lib/lib/oauth/loopback.js +31 -1
  74. package/dist-lib/lib/oauth/register.js +33 -18
  75. package/dist-lib/lib/oauth/request.d.ts +10 -0
  76. package/dist-lib/lib/oauth/request.js +22 -0
  77. package/dist-lib/lib/oauth/token-exchange-core.js +29 -32
  78. package/dist-lib/lib/oauth/types.d.ts +3 -0
  79. package/dist-lib/lib/oauth.d.ts +2 -1
  80. package/dist-lib/lib/oauth.js +1 -0
  81. package/dist-lib/lib/run-replay.d.ts +8 -2
  82. package/dist-lib/lib/run-replay.js +29 -11
  83. package/dist-lib/schema/engine.js +20 -3
  84. package/dist-lib/schema/field-order.js +8 -1
  85. package/dist-lib/schema/local.js +7 -3
  86. package/dist-lib/schema/qvac.js +19 -2
  87. package/dist-lib/spec/hrpc/messages.js +1236 -1184
  88. package/dist-lib/spec/hyperschema/index.js +1236 -1184
  89. package/dist-lib/spec/hyperschema/schema.json +47 -0
  90. package/dist-lib/spec/hyperschema/types.d.ts +11 -0
  91. package/dist-lib/spec/local/hyperdb/messages.js +70 -20
  92. package/dist-lib/spec/local/hyperschema/index.js +36 -11
  93. package/dist-lib/spec/local/hyperschema/schema.json +21 -1
  94. package/dist-lib/spec/local/hyperschema/types.d.ts +5 -1
  95. package/dist-lib/spec/mesh/hyperdb/db.json +17 -1
  96. package/dist-lib/spec/mesh/hyperdb/index.js +83 -2
  97. package/dist-lib/spec/mesh/hyperdb/messages.js +573 -255
  98. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +7 -1
  99. package/dist-lib/spec/mesh/hyperdispatch/index.js +18 -1
  100. package/dist-lib/spec/mesh/hyperdispatch/messages.js +121 -8
  101. package/dist-lib/spec/mesh/hyperschema/index.js +121 -8
  102. package/dist-lib/spec/mesh/hyperschema/schema.json +69 -1
  103. package/dist-lib/spec/mesh/hyperschema/types.d.ts +19 -1
  104. package/package.json +12 -10
@@ -80,6 +80,13 @@ export interface CoreOptions {
80
80
  readonly suspend: () => Promise<void>;
81
81
  readonly resume: () => Promise<void>;
82
82
  };
83
+ network?: NetworkGate;
84
+ }
85
+ export interface NetworkConnection {
86
+ destroy(): void;
87
+ }
88
+ export interface NetworkGate {
89
+ readonly onConnection?: (conn: NetworkConnection) => void;
83
90
  }
84
91
  export declare const DEVICE_UNKNOWN = "This device has no device details: pass `device` when creating Core";
85
92
  export interface SharedMesh {
@@ -131,6 +138,7 @@ export declare class Core extends ReadyResource {
131
138
  private readonly _log;
132
139
  private readonly _spec;
133
140
  private readonly _bootstrapNodes;
141
+ private readonly _network;
134
142
  private readonly _device;
135
143
  private readonly _app;
136
144
  private _minSupportedVersion;
@@ -235,7 +243,7 @@ export declare class Core extends ReadyResource {
235
243
  private _systemInfo;
236
244
  private _statusError;
237
245
  private _upgradeRequired;
238
- constructor(transport: Transport, { storagePath, store, swarm, wakeup, logger, spec, bootstrapNodes, device, app, minSupportedVersion, otaConfigCore, blindPeerKeys, blindPeeringOpts, fileGc, autoDownloadModels, meshSeed, meshKey, additionalNodes, listModels, deleteModel, clearKvCache, deleteChatKvCache, compactChat, modelCatalogWatch, retryModelDownload, transcribe, detectLanguage, previewModelSource, speak, speakStream, embedder, extractors, skills, skillCatalog, tools, hostsModelSource, vectorIndex, apiLifecycle }: CoreOptions);
246
+ constructor(transport: Transport, { storagePath, store, swarm, wakeup, logger, spec, bootstrapNodes, device, app, minSupportedVersion, otaConfigCore, blindPeerKeys, blindPeeringOpts, fileGc, autoDownloadModels, meshSeed, meshKey, additionalNodes, listModels, deleteModel, clearKvCache, deleteChatKvCache, compactChat, modelCatalogWatch, retryModelDownload, transcribe, detectLanguage, previewModelSource, speak, speakStream, embedder, extractors, skills, skillCatalog, tools, hostsModelSource, vectorIndex, apiLifecycle, network }: CoreOptions);
239
247
  get devicePublicKey(): Buffer | null;
240
248
  private _requireDeviceKeyPair;
241
249
  get fsDrives(): DriveHost | null;
@@ -371,6 +379,7 @@ export declare class Core extends ReadyResource {
371
379
  private _pendingUpdatesFor;
372
380
  private _fileIndexFor;
373
381
  private _indexMeshFiles;
382
+ private _backfillChatChunks;
374
383
  private _servedFile;
375
384
  clearOldFiles({ olderThanMs, includeOwn }: ClearOldFilesOptions): Promise<number>;
376
385
  private _startFileGc;
@@ -431,11 +440,13 @@ export declare class Core extends ReadyResource {
431
440
  private _assertMeshIsHere;
432
441
  getMeshInvite(opts?: MeshInviteOptions): Promise<MeshInvite | null>;
433
442
  private _delegatePairing;
443
+ private _shortestAnchorProof;
434
444
  previewMeshInvite(invite: string): MeshInvitePreview;
435
445
  private _closeSwarm;
436
446
  pushSkillSecret(request: SecretRequest): Promise<void>;
437
447
  private _storeHandedOffSecret;
438
448
  private _followJoinedMeshes;
449
+ private _followJoinedMeshesPass;
439
450
  private _followMeshRows;
440
451
  private _trackFollow;
441
452
  private _retireLeftMesh;
@@ -40,11 +40,12 @@ import { createInferenceCallsReceiver } from './core/inference-calls-receiver.js
40
40
  import { SkillOverlayHandoff } from './core/skill-overlay-handoff.js';
41
41
  import { createSkillOverlayReceiver } from './core/skill-overlay-receiver.js';
42
42
  import { createSkillSecretReceiver } from './core/skill-secrets.js';
43
- import { attestDeviceProof, attestMemberClaim, claimAttestation, delegationFits, openIdentity } from './core/identity.js';
43
+ import { attestDeviceProof, attestMemberClaim, chainTo, claimAttestation, delegationFits, openIdentity, pairingAnchorSeed } from './core/identity.js';
44
44
  import { openRecoveryCore, publishRecovery, readRecovery, recoveryKeys } from './core/account-recovery.js';
45
45
  import { claimOwnMembership, legacyNameStamp, namedAt, renameFoundedMember } from './core/membership.js';
46
46
  import { runMigrations } from './core/migrations/index.js';
47
47
  import { backfillFileIndex } from './core/migrations/file-index.js';
48
+ import { REINDEX_FROM_APP_VERSION, backfillChatChunks, floorExcludesBefore } from './core/migrations/chat-chunks.js';
48
49
  import { advertiseDeviceVersion, retractInventory } from './core/devices.js';
49
50
  import { DeviceControl } from './core/device-control.js';
50
51
  import { forwardToDevices, servedToSiblings } from './api/device-routes.js';
@@ -89,6 +90,7 @@ export class Core extends ReadyResource {
89
90
  _log;
90
91
  _spec;
91
92
  _bootstrapNodes;
93
+ _network;
92
94
  _device;
93
95
  _app;
94
96
  _minSupportedVersion;
@@ -193,7 +195,7 @@ export class Core extends ReadyResource {
193
195
  _systemInfo;
194
196
  _statusError;
195
197
  _upgradeRequired;
196
- constructor(transport, { storagePath, store = undefined, swarm = undefined, wakeup = undefined, logger = null, spec = openLocalSpec, bootstrapNodes = undefined, device = undefined, app = undefined, minSupportedVersion = undefined, otaConfigCore = undefined, blindPeerKeys = undefined, blindPeeringOpts = undefined, fileGc = null, autoDownloadModels = undefined, meshSeed = undefined, meshKey = undefined, additionalNodes = undefined, listModels = undefined, deleteModel = undefined, clearKvCache = undefined, deleteChatKvCache = undefined, compactChat = undefined, modelCatalogWatch = undefined, retryModelDownload = undefined, transcribe = undefined, detectLanguage = undefined, previewModelSource = undefined, speak = undefined, speakStream = undefined, embedder = undefined, extractors = undefined, skills = undefined, skillCatalog = undefined, tools = undefined, hostsModelSource = false, vectorIndex = undefined, apiLifecycle = undefined }) {
198
+ constructor(transport, { storagePath, store = undefined, swarm = undefined, wakeup = undefined, logger = null, spec = openLocalSpec, bootstrapNodes = undefined, device = undefined, app = undefined, minSupportedVersion = undefined, otaConfigCore = undefined, blindPeerKeys = undefined, blindPeeringOpts = undefined, fileGc = null, autoDownloadModels = undefined, meshSeed = undefined, meshKey = undefined, additionalNodes = undefined, listModels = undefined, deleteModel = undefined, clearKvCache = undefined, deleteChatKvCache = undefined, compactChat = undefined, modelCatalogWatch = undefined, retryModelDownload = undefined, transcribe = undefined, detectLanguage = undefined, previewModelSource = undefined, speak = undefined, speakStream = undefined, embedder = undefined, extractors = undefined, skills = undefined, skillCatalog = undefined, tools = undefined, hostsModelSource = false, vectorIndex = undefined, apiLifecycle = undefined, network = undefined }) {
197
199
  super();
198
200
  this._transport = transport;
199
201
  this._apiLifecycle = apiLifecycle ?? {
@@ -219,6 +221,7 @@ export class Core extends ReadyResource {
219
221
  this.state = QvacState.RESUME;
220
222
  this._spec = spec;
221
223
  this._bootstrapNodes = bootstrapNodes;
224
+ this._network = network;
222
225
  this._device = device;
223
226
  this._app = app;
224
227
  this._minSupportedVersion = minSupportedVersion;
@@ -774,6 +777,9 @@ export class Core extends ReadyResource {
774
777
  if (withheld) {
775
778
  void this._reopenMesh();
776
779
  }
780
+ for (const { mesh } of this._meshes.values()) {
781
+ void this._backfillChatChunks(mesh);
782
+ }
777
783
  return;
778
784
  }
779
785
  for (const { mesh } of [...this._meshes.values()]) {
@@ -984,6 +990,10 @@ export class Core extends ReadyResource {
984
990
  bootstrap: this._bootstrapNodes,
985
991
  relayThrough: this._bootstrapNodes ? undefined : () => BLIND_RELAY_KEYS
986
992
  });
993
+ const onConnection = this._network?.onConnection;
994
+ if (onConnection) {
995
+ this._swarm.on('connection', (conn) => onConnection(conn));
996
+ }
987
997
  this._swarm.on('connection', this._onSwarmConnection);
988
998
  for (const conn of this._swarm.connections) {
989
999
  if (isPeerConnection(conn)) {
@@ -1050,6 +1060,7 @@ export class Core extends ReadyResource {
1050
1060
  writerSeed: session.writerSeed,
1051
1061
  logger: this._log,
1052
1062
  blindPeerKeys: this._resolveBlindPeerKeys(blindPeerKeys),
1063
+ bootFrom: session.bootFrom,
1053
1064
  trustedDevice: (deviceId) => this._isOurMemberDevice(deviceId),
1054
1065
  wakeup: this._wakeup,
1055
1066
  attachmentUrl: (attachment) => this._linkAttachment(attachment, mesh),
@@ -2075,7 +2086,10 @@ export class Core extends ReadyResource {
2075
2086
  }
2076
2087
  }
2077
2088
  async _joinCandidate(invite, source, deviceId) {
2078
- const session = this.local.createCandidateSession(invite.seed, invite.baseKey, invite.compute);
2089
+ const session = {
2090
+ ...this.local.createCandidateSession(invite.seed, invite.baseKey, invite.compute),
2091
+ bootFrom: invite.fastForward
2092
+ };
2079
2093
  let candidate;
2080
2094
  try {
2081
2095
  candidate = await this._openCandidateMesh(invite, session);
@@ -2283,6 +2297,9 @@ export class Core extends ReadyResource {
2283
2297
  const held = key ? (this._pooled(b4a.toHex(key)) ?? this._opening.get(b4a.toHex(key))) : null;
2284
2298
  const mesh = key ? await this._openPooledMesh(b4a.toHex(key)).catch(noop) : null;
2285
2299
  if (mesh && !mesh.ourMemberId) {
2300
+ if (mesh.writable) {
2301
+ await mesh.autobee.update();
2302
+ }
2286
2303
  await mesh.refreshOurMember();
2287
2304
  }
2288
2305
  if (!mesh?.writable) {
@@ -2304,7 +2321,7 @@ export class Core extends ReadyResource {
2304
2321
  writerSeed: session.writerSeed,
2305
2322
  logger: this._log,
2306
2323
  blindPeerKeys: this._resolveBlindPeerKeys(invite.blindPeers),
2307
- bootFrom: invite.fastForward,
2324
+ bootFrom: session.bootFrom,
2308
2325
  trustedDevice: (deviceId) => this._isOurMemberDevice(deviceId),
2309
2326
  wakeup: this._wakeup,
2310
2327
  attachmentUrl: (attachment) => this._linkAttachment(attachment, candidate),
@@ -2431,11 +2448,11 @@ export class Core extends ReadyResource {
2431
2448
  }
2432
2449
  mesh.on('members-dirty', () => void this._revalidateLoans());
2433
2450
  mesh.on('members-dirty', () => void this._deviceControl.revalidate());
2451
+ mesh.on('members-dirty', () => void this._revokeInMeshes());
2434
2452
  mesh.on('members-dirty', () => {
2435
2453
  if (!this._isAccount(mesh))
2436
2454
  return;
2437
2455
  void this._followAccountName(mesh);
2438
- void this._revokeInMeshes();
2439
2456
  });
2440
2457
  mesh.on('meshes-dirty', (changed) => {
2441
2458
  if (changed || entry.meshesPending) {
@@ -2474,6 +2491,7 @@ export class Core extends ReadyResource {
2474
2491
  this._upgradeRequired = null;
2475
2492
  if (mesh.writable) {
2476
2493
  this._kicked = false;
2494
+ void this.local.markAwaitingAdmission(mesh.key, { awaitingAdmission: false }).catch(noop);
2477
2495
  }
2478
2496
  this._notifyStatus();
2479
2497
  }
@@ -2599,6 +2617,21 @@ export class Core extends ReadyResource {
2599
2617
  }
2600
2618
  }
2601
2619
  }
2620
+ async _backfillChatChunks(mesh) {
2621
+ if (!floorExcludesBefore(this._minSupportedVersion, REINDEX_FROM_APP_VERSION)) {
2622
+ return;
2623
+ }
2624
+ try {
2625
+ await mesh.waitForWritable();
2626
+ await mesh.autobee.update();
2627
+ await backfillChatChunks(mesh);
2628
+ }
2629
+ catch (err) {
2630
+ if (!mesh.closing) {
2631
+ this._log?.warn(['p2p', 'mesh'], 'copying chats into chat-chunks failed', err);
2632
+ }
2633
+ }
2634
+ }
2602
2635
  _servedFile(mesh, coreKey, blockOffset) {
2603
2636
  this.local.files
2604
2637
  .touch({ meshKey: mesh.key, coreKey, blockOffset })
@@ -2767,6 +2800,7 @@ export class Core extends ReadyResource {
2767
2800
  }
2768
2801
  };
2769
2802
  void this._indexMeshFiles(mesh).then(() => Promise.all([reclaim(), sweep()]));
2803
+ void this._backfillChatChunks(mesh);
2770
2804
  mesh.autobee.on('move-to', () => void sweep());
2771
2805
  entry.hostedAgents = new HostedAgents({
2772
2806
  mesh,
@@ -3622,17 +3656,36 @@ export class Core extends ReadyResource {
3622
3656
  async _delegatePairing(account) {
3623
3657
  const keyPair = this._requireDeviceKeyPair();
3624
3658
  const own = await account.view.get(DEVICES, { id: keyPair.publicKey });
3625
- if (!own?.memberProof) {
3659
+ if (!own?.memberId || !own.memberProof) {
3626
3660
  return null;
3627
3661
  }
3628
- const deviceSeed = crypto.randomBytes(32);
3629
- const delegate = crypto.keyPair(deviceSeed);
3630
- const deviceProof = await attestDeviceProof(delegate.publicKey, keyPair, own.memberProof);
3631
- if (!(await delegationFits(deviceProof, delegate))) {
3662
+ const deviceSeed = pairingAnchorSeed(account.seed, own.memberId);
3663
+ const anchor = crypto.keyPair(deviceSeed);
3664
+ const deviceProof = await this._shortestAnchorProof(account, {
3665
+ anchor,
3666
+ memberId: own.memberId,
3667
+ memberProof: own.memberProof
3668
+ });
3669
+ if (!(await delegationFits(deviceProof, anchor))) {
3632
3670
  throw new CoreError('DEVICE_INVITE_CHAIN_TOO_LONG');
3633
3671
  }
3634
3672
  return { deviceSeed, deviceProof };
3635
3673
  }
3674
+ async _shortestAnchorProof(account, { anchor, memberId, memberProof }) {
3675
+ const keyPair = this._requireDeviceKeyPair();
3676
+ let shortest = await attestDeviceProof(anchor.publicKey, keyPair, memberProof);
3677
+ for await (const row of account.view.find(DEVICES)) {
3678
+ if (row.revoked || !row.memberId || !row.memberProof)
3679
+ continue;
3680
+ if (!b4a.equals(row.memberId, memberId))
3681
+ continue;
3682
+ const cut = chainTo(row.memberProof, anchor.publicKey);
3683
+ if (cut && cut.byteLength < shortest.byteLength) {
3684
+ shortest = cut;
3685
+ }
3686
+ }
3687
+ return shortest;
3688
+ }
3636
3689
  previewMeshInvite(invite) {
3637
3690
  const decoded = decodeMeshInvite(invite);
3638
3691
  if (!decoded) {
@@ -3707,7 +3760,18 @@ export class Core extends ReadyResource {
3707
3760
  });
3708
3761
  return receive(request, fromDeviceId);
3709
3762
  }
3710
- async _followJoinedMeshes(memberMesh) {
3763
+ _followJoinedMeshes(memberMesh) {
3764
+ return this._followJoinedMeshesPass(memberMesh).catch((err) => {
3765
+ const entry = this._meshes.get(this._meshIdOf(memberMesh));
3766
+ if (entry?.mesh === memberMesh) {
3767
+ entry.meshesPending = true;
3768
+ }
3769
+ if (!this.closing) {
3770
+ this._log?.warn(['p2p', 'mesh'], 'following the joined meshes failed', err);
3771
+ }
3772
+ });
3773
+ }
3774
+ async _followJoinedMeshesPass(memberMesh) {
3711
3775
  if (this.closing || !this._isPooled(memberMesh))
3712
3776
  return;
3713
3777
  const entry = this._meshes.get(this._meshIdOf(memberMesh));
@@ -63,6 +63,7 @@ export interface CredentialPusherLogger {
63
63
  }
64
64
  export type CredentialPusherHandle = (() => Promise<void>) & {
65
65
  sync(): Promise<void>;
66
+ ready(): Promise<void>;
66
67
  };
67
68
  export declare function nextRefreshDelay(entries: ReadonlyArray<Pick<CredentialEntry, 'expiresAt'>>, now?: number): number;
68
69
  export declare function credentialOwners(catalog: ReadonlyArray<PusherSkill>): ReadonlyMap<string, readonly string[]>;
@@ -101,52 +101,39 @@ function fanOut(entry, owners) {
101
101
  const skills = owners.get(entry.key) ?? [];
102
102
  return skills.length > 0 ? skills.map((skill) => ({ ...entry, skill })) : [entry];
103
103
  }
104
- async function resolveScope(agentId, { engine, last, owners, specs, logger }) {
105
- const entries = [];
106
- for (const spec of specs) {
107
- const mk = storeKey(spec.credentialKey, agentId);
108
- try {
109
- const entry = await oauthEntry(engine, spec, agentId);
110
- if (entry) {
111
- last.set(mk, entry);
112
- entries.push(...fanOut(entry, owners));
113
- }
114
- else {
115
- last.delete(mk);
116
- }
117
- }
118
- catch (err) {
119
- logger?.warn('credential-pusher', `credential resolve failed for ${spec.credentialKey}`, errorMessage(err));
120
- const kept = last.get(mk);
121
- if (kept) {
122
- entries.push(...fanOut(kept, owners));
123
- }
104
+ async function resolveEntry({ mk, failure, read }, { last, owners, logger }) {
105
+ try {
106
+ const entry = await read();
107
+ if (!entry) {
108
+ last.delete(mk);
109
+ return [];
124
110
  }
111
+ last.set(mk, entry);
112
+ return fanOut(entry, owners);
125
113
  }
126
- const specKeys = new Set(specs.map((spec) => spec.credentialKey));
127
- for (const key of PLAIN_KEYS) {
128
- if (specKeys.has(key))
129
- continue;
130
- const mk = storeKey(key, agentId);
131
- try {
132
- const entry = await plainEntry(engine, key, agentId);
133
- if (entry) {
134
- last.set(mk, entry);
135
- entries.push(...fanOut(entry, owners));
136
- }
137
- else {
138
- last.delete(mk);
139
- }
140
- }
141
- catch (err) {
142
- logger?.warn('credential-pusher', `credential read failed for ${key}`, errorMessage(err));
143
- const kept = last.get(mk);
144
- if (kept) {
145
- entries.push(...fanOut(kept, owners));
146
- }
147
- }
114
+ catch (err) {
115
+ logger?.warn('credential-pusher', failure, errorMessage(err));
116
+ const kept = last.get(mk);
117
+ return kept ? fanOut(kept, owners) : [];
148
118
  }
149
- return entries;
119
+ }
120
+ async function resolveScope(agentId, resolution) {
121
+ const { engine, specs } = resolution;
122
+ const specKeys = new Set(specs.map((spec) => spec.credentialKey));
123
+ const pending = [
124
+ ...specs.map((spec) => ({
125
+ mk: storeKey(spec.credentialKey, agentId),
126
+ failure: `credential resolve failed for ${spec.credentialKey}`,
127
+ read: () => oauthEntry(engine, spec, agentId)
128
+ })),
129
+ ...PLAIN_KEYS.filter((key) => !specKeys.has(key)).map((key) => ({
130
+ mk: storeKey(key, agentId),
131
+ failure: `credential read failed for ${key}`,
132
+ read: () => plainEntry(engine, key, agentId)
133
+ }))
134
+ ];
135
+ const resolved = await Promise.all(pending.map((entry) => resolveEntry(entry, resolution)));
136
+ return resolved.flat();
150
137
  }
151
138
  async function resolveSettings(engine, scopes, { catalog, logger }) {
152
139
  const fields = settingFields(catalog);
@@ -289,9 +276,8 @@ export function startCredentialPusher(harness, engine, logger) {
289
276
  syncWatches(specs.map((spec) => spec.credentialKey), scopes);
290
277
  syncAgentsWatch(meshId);
291
278
  const resolution = { engine, last, owners, specs, logger };
292
- for (const agentId of scopes) {
293
- entries.push(...(await resolveScope(agentId, resolution)));
294
- }
279
+ const resolved = await Promise.all(scopes.map((agentId) => resolveScope(agentId, resolution)));
280
+ entries.push(...resolved.flat());
295
281
  if (disposed) {
296
282
  return;
297
283
  }
@@ -346,7 +332,7 @@ export function startCredentialPusher(harness, engine, logger) {
346
332
  pushLock.unlock();
347
333
  }
348
334
  };
349
- schedule();
335
+ const first = schedule();
350
336
  const stop = async () => {
351
337
  if (disposed)
352
338
  return;
@@ -366,6 +352,6 @@ export function startCredentialPusher(harness, engine, logger) {
366
352
  pushLock.destroy();
367
353
  await pushLock.flush();
368
354
  };
369
- return Object.assign(stop, { sync });
355
+ return Object.assign(stop, { sync, ready: () => first });
370
356
  }
371
357
  function noop() { }
@@ -0,0 +1,18 @@
1
+ import Supervisor from 'bare-supervisor';
2
+ import type { Transport } from '@qvac/harness/rpc';
3
+ import type { LoggerInterface } from '@qvac/logging';
4
+ import { type AssistantChildOptions } from './assistant-tree.ts';
5
+ import { Core, type CoreOptions } from './core.ts';
6
+ export interface DeviceTreeOptions {
7
+ core: Omit<CoreOptions, 'apiLifecycle'>;
8
+ assistant: Omit<AssistantChildOptions, 'core'>;
9
+ office?: {
10
+ close: () => unknown;
11
+ };
12
+ logger?: LoggerInterface | null;
13
+ }
14
+ export interface DeviceTree {
15
+ supervisor: Supervisor;
16
+ core: Core;
17
+ }
18
+ export declare function deviceTree(transport: Transport, opts: DeviceTreeOptions): DeviceTree;
@@ -0,0 +1,32 @@
1
+ import Supervisor from 'bare-supervisor';
2
+ import { assistantChild } from './assistant-tree.js';
3
+ import { Core } from './core.js';
4
+ export function deviceTree(transport, opts) {
5
+ const { office, logger = null } = opts;
6
+ const supervisor = new Supervisor();
7
+ const core = new Core(transport, {
8
+ ...opts.core,
9
+ apiLifecycle: {
10
+ suspend: () => supervisor.suspend(),
11
+ resume: () => supervisor.resume()
12
+ }
13
+ });
14
+ if (office) {
15
+ supervisor.add('office', { start: () => office });
16
+ }
17
+ supervisor.add('core', {
18
+ deps: office ? ['office'] : [],
19
+ start: () => core,
20
+ presuspend: (handle) => handle.presuspend(),
21
+ suspend: (handle) => handle.suspend(),
22
+ wakeup: (handle) => handle.wakeup(),
23
+ resume: (handle) => handle.resume()
24
+ });
25
+ supervisor.add('assistant', assistantChild({ logger, ...opts.assistant }));
26
+ for (const event of ['child-died', 'child-restarting', 'stall']) {
27
+ supervisor.on(event, (payload) => logger?.warn('device-tree', event, { ...payload, error: payload.error?.message }));
28
+ }
29
+ supervisor.on('gave-up', ({ name, error }) => logger?.error('device-tree', `${name} gave up`, error?.message));
30
+ supervisor.on('power', (payload) => logger?.info('device-tree', 'power', payload));
31
+ return { supervisor, core };
32
+ }
@@ -588,6 +588,14 @@ declare const DEFINITIONS: {
588
588
  readonly code: 101218;
589
589
  readonly message: (status: number, text: string) => string;
590
590
  };
591
+ readonly OAUTH_TOKEN_EXCHANGE_TIMEOUT: {
592
+ readonly code: 101219;
593
+ readonly message: () => string;
594
+ };
595
+ readonly OAUTH_REGISTRATION_TIMEOUT: {
596
+ readonly code: 101220;
597
+ readonly message: () => string;
598
+ };
591
599
  readonly SIDECAR_ENTRY_MISSING: {
592
600
  readonly code: 101301;
593
601
  readonly message: () => string;
@@ -518,6 +518,14 @@ const DEFINITIONS = {
518
518
  code: 101218,
519
519
  message: (status, text) => `Token refresh failed (${status}): ${text}`
520
520
  },
521
+ OAUTH_TOKEN_EXCHANGE_TIMEOUT: {
522
+ code: 101219,
523
+ message: () => 'OAuth token exchange timed out'
524
+ },
525
+ OAUTH_REGISTRATION_TIMEOUT: {
526
+ code: 101220,
527
+ message: () => 'OAuth client registration timed out'
528
+ },
521
529
  SIDECAR_ENTRY_MISSING: {
522
530
  code: 101301,
523
531
  message: () => 'sidecar mode needs a stowed bundle entry'
@@ -32,10 +32,12 @@ export async function runOAuthFlowWithRuntime(config, deps, runtime) {
32
32
  state,
33
33
  pkce
34
34
  });
35
+ deps.onStage?.('awaiting-callback');
35
36
  const callback = await redirect.authorize(`${config.authUrl}?${authParams.toString()}`, deps.timeoutMs, state);
36
37
  if (callback.state !== state) {
37
38
  throw new CoreError('OAUTH_STATE_MISMATCH');
38
39
  }
40
+ deps.onStage?.('exchanging-code');
39
41
  return exchangeCodeWithFetch(config, {
40
42
  clientId,
41
43
  redirectUri: redirect.redirectUri,
@@ -1,3 +1,4 @@
1
1
  import type { OAuthLoopbackServer, OAuthLoopbackTarget } from './redirect-types.ts';
2
2
  export declare function abortOAuthLoopback(credentialKey: string): Promise<boolean>;
3
+ export declare function deliverOAuthCallback(address: string): boolean;
3
4
  export declare function openOAuthLoopback(port: number, target: OAuthLoopbackTarget): Promise<OAuthLoopbackServer>;
@@ -1,7 +1,7 @@
1
1
  import { createServer } from 'http';
2
2
  import { OAUTH_CALLBACK_PATH, OAUTH_WAIT_FOR_CODE_TIMEOUT_MS, oauthRedirectUri } from './keys.js';
3
3
  import { CoreError } from '../errors.js';
4
- const DONE_PAGE = '<html><body><p>Authorization complete. You may close this tab.</p>' +
4
+ const DONE_PAGE = '<html><body><p>Authorization complete. Return to QV.AC to finish connecting. You may close this tab.</p>' +
5
5
  '<script>window.close();</script></body></html>';
6
6
  const STALE_PAGE = '<html><body><p>This sign-in link is from an earlier attempt. ' +
7
7
  'You can close this tab.</p></body></html>';
@@ -18,6 +18,36 @@ function abortFor(credentialKey, reason) {
18
18
  }
19
19
  return pending.length > 0;
20
20
  }
21
+ export function deliverOAuthCallback(address) {
22
+ const params = callbackParams(address);
23
+ const state = params?.get('state');
24
+ const waiter = state ? pendingWaiter(state) : undefined;
25
+ if (!params || !state || !waiter) {
26
+ return false;
27
+ }
28
+ settle(waiter, callbackSettlement(params, state));
29
+ return true;
30
+ }
31
+ function callbackParams(address) {
32
+ let url;
33
+ try {
34
+ url = new URL(address.trim());
35
+ }
36
+ catch {
37
+ return null;
38
+ }
39
+ const finished = url.searchParams.has('code') || url.searchParams.has('error');
40
+ return url.pathname === OAUTH_CALLBACK_PATH && finished ? url.searchParams : null;
41
+ }
42
+ function pendingWaiter(state) {
43
+ for (const loopback of loopbacks) {
44
+ const waiter = loopback.waiters.get(state);
45
+ if (waiter && waiter.settled === null) {
46
+ return waiter;
47
+ }
48
+ }
49
+ return undefined;
50
+ }
21
51
  export async function openOAuthLoopback(port, target) {
22
52
  if (!Number.isInteger(port) || port < 0 || port > 65535) {
23
53
  throw new CoreError('OAUTH_LOOPBACK_PORT_INVALID', [port]);
@@ -1,36 +1,51 @@
1
1
  import * as z from 'zod';
2
+ import { providerRequest } from './request.js';
2
3
  import { CoreError } from '../errors.js';
3
4
  const RegistrationResponseSchema = z.object({
4
5
  client_id: z.string().min(1)
5
6
  });
6
- export async function registerOAuthClient(config, redirectUri, fetchImpl = globalThis.fetch) {
7
+ export function registerOAuthClient(config, redirectUri, fetchImpl) {
8
+ return register(config, redirectUri, { fetchImpl });
9
+ }
10
+ async function register(config, redirectUri, { fetchImpl = globalThis.fetch, timeoutMs }) {
7
11
  if (!config.registrationEndpoint) {
8
12
  throw new CoreError('OAUTH_REGISTRATION_UNAVAILABLE');
9
13
  }
10
- const response = await fetchImpl(config.registrationEndpoint, {
11
- method: 'POST',
12
- headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
13
- body: JSON.stringify({
14
- client_name: 'QV.AC',
15
- redirect_uris: [redirectUri],
16
- grant_types: ['authorization_code', 'refresh_token'],
17
- response_types: ['code'],
18
- token_endpoint_auth_method: 'none',
19
- application_type: 'native'
20
- })
14
+ return await providerRequest({
15
+ fetchImpl,
16
+ url: config.registrationEndpoint,
17
+ init: {
18
+ method: 'POST',
19
+ headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
20
+ body: JSON.stringify({
21
+ client_name: 'QV.AC',
22
+ redirect_uris: [redirectUri],
23
+ grant_types: ['authorization_code', 'refresh_token'],
24
+ response_types: ['code'],
25
+ token_endpoint_auth_method: 'none',
26
+ application_type: 'native'
27
+ })
28
+ },
29
+ timeoutMs,
30
+ timeout: () => new CoreError('OAUTH_REGISTRATION_TIMEOUT'),
31
+ read: async (response) => {
32
+ if (!response.ok) {
33
+ const text = await response.text().catch(() => '');
34
+ throw new CoreError('OAUTH_REGISTRATION_FAILED', [response.status, text]);
35
+ }
36
+ return RegistrationResponseSchema.parse(await response.json()).client_id;
37
+ }
21
38
  });
22
- if (!response.ok) {
23
- const text = await response.text().catch(() => '');
24
- throw new CoreError('OAUTH_REGISTRATION_FAILED', [response.status, text]);
25
- }
26
- return RegistrationResponseSchema.parse(await response.json()).client_id;
27
39
  }
28
40
  export async function resolveDynamicClientId(config, redirectUri, deps, fetchImpl) {
29
41
  const cached = await deps.getClientId?.();
30
42
  if (cached) {
31
43
  return cached;
32
44
  }
33
- const clientId = await registerOAuthClient(config, redirectUri, fetchImpl);
45
+ const clientId = await register(config, redirectUri, {
46
+ fetchImpl,
47
+ timeoutMs: deps.requestTimeoutMs
48
+ });
34
49
  await deps.saveClientId?.(clientId);
35
50
  return clientId;
36
51
  }
@@ -0,0 +1,10 @@
1
+ import type { OAuthFetch } from './types.ts';
2
+ export interface ProviderRequest<T> {
3
+ readonly fetchImpl: OAuthFetch;
4
+ readonly url: string;
5
+ readonly init: RequestInit;
6
+ readonly timeoutMs?: number | undefined;
7
+ readonly timeout: () => Error;
8
+ readonly read: (response: Response) => Promise<T>;
9
+ }
10
+ export declare function providerRequest<T>({ fetchImpl, url, init, timeoutMs, timeout, read }: ProviderRequest<T>): Promise<T>;
@@ -0,0 +1,22 @@
1
+ import { AbortController as BareAbortController } from 'bare-abort-controller';
2
+ const PROVIDER_REQUEST_TIMEOUT_MS = 30_000;
3
+ const AbortController = globalThis.AbortController ?? BareAbortController;
4
+ export async function providerRequest({ fetchImpl, url, init, timeoutMs = PROVIDER_REQUEST_TIMEOUT_MS, timeout, read }) {
5
+ const controller = new AbortController();
6
+ let timer;
7
+ const expired = new Promise((_resolve, reject) => {
8
+ timer = globalThis.setTimeout(() => {
9
+ controller.abort();
10
+ reject(timeout());
11
+ }, timeoutMs);
12
+ });
13
+ try {
14
+ return await Promise.race([
15
+ fetchImpl(url, { ...init, signal: controller.signal }).then(read),
16
+ expired
17
+ ]);
18
+ }
19
+ finally {
20
+ globalThis.clearTimeout(timer);
21
+ }
22
+ }