@qvac/core 1.0.0-rc.2 → 1.0.0-rc.3

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 (195) hide show
  1. package/CHANGELOG.md +158 -2
  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 +7 -2
  5. package/dist-lib/lib/api/chunk-delta.js +89 -7
  6. package/dist-lib/lib/api/chunks.js +14 -8
  7. package/dist-lib/lib/api/config.d.ts +4 -1
  8. package/dist-lib/lib/api/config.js +45 -4
  9. package/dist-lib/lib/api/custom-models.js +3 -5
  10. package/dist-lib/lib/api/devices.js +3 -1
  11. package/dist-lib/lib/api/files.d.ts +1 -1
  12. package/dist-lib/lib/api/files.js +65 -8
  13. package/dist-lib/lib/api/instrument.js +12 -4
  14. package/dist-lib/lib/api/members.js +7 -13
  15. package/dist-lib/lib/api/mesh.d.ts +1 -3
  16. package/dist-lib/lib/api/mesh.js +2 -2
  17. package/dist-lib/lib/api/notifications.js +8 -4
  18. package/dist-lib/lib/api/projects.js +5 -1
  19. package/dist-lib/lib/api/rag.js +32 -19
  20. package/dist-lib/lib/api/served.js +2 -1
  21. package/dist-lib/lib/api/workspace.js +0 -1
  22. package/dist-lib/lib/assistant/harness.d.ts +8 -2
  23. package/dist-lib/lib/assistant/harness.js +17 -4
  24. package/dist-lib/lib/assistant/history.d.ts +1 -1
  25. package/dist-lib/lib/assistant/history.js +19 -13
  26. package/dist-lib/lib/assistant/message.d.ts +6 -1
  27. package/dist-lib/lib/assistant/message.js +22 -2
  28. package/dist-lib/lib/assistant/model-round.d.ts +5 -0
  29. package/dist-lib/lib/assistant/model-round.js +46 -18
  30. package/dist-lib/lib/assistant/pdf-budget.d.ts +10 -2
  31. package/dist-lib/lib/assistant/pdf-budget.js +10 -11
  32. package/dist-lib/lib/assistant/pdf-pages.d.ts +25 -0
  33. package/dist-lib/lib/assistant/pdf-pages.js +75 -0
  34. package/dist-lib/lib/assistant/round-host.js +5 -3
  35. package/dist-lib/lib/assistant-client.d.ts +2 -1
  36. package/dist-lib/lib/assistant-client.js +24 -6
  37. package/dist-lib/lib/assistant-tree.d.ts +2 -0
  38. package/dist-lib/lib/assistant-tree.js +8 -5
  39. package/dist-lib/lib/assistant.d.ts +20 -7
  40. package/dist-lib/lib/assistant.js +468 -240
  41. package/dist-lib/lib/client.js +10 -1
  42. package/dist-lib/lib/compaction.d.ts +3 -1
  43. package/dist-lib/lib/compaction.js +18 -4
  44. package/dist-lib/lib/constants.d.ts +3 -0
  45. package/dist-lib/lib/constants.js +3 -0
  46. package/dist-lib/lib/core/chat-activity.js +13 -16
  47. package/dist-lib/lib/core/chat-copy.js +1 -1
  48. package/dist-lib/lib/core/chat-fork.js +2 -1
  49. package/dist-lib/lib/core/chat-move.d.ts +4 -0
  50. package/dist-lib/lib/core/chat-move.js +42 -11
  51. package/dist-lib/lib/core/chat.d.ts +0 -1
  52. package/dist-lib/lib/core/chat.js +1 -1
  53. package/dist-lib/lib/core/chunk.d.ts +14 -3
  54. package/dist-lib/lib/core/chunk.js +112 -22
  55. package/dist-lib/lib/core/collections.d.ts +12 -1
  56. package/dist-lib/lib/core/collections.js +12 -1
  57. package/dist-lib/lib/core/config-value.d.ts +6 -0
  58. package/dist-lib/lib/core/config-value.js +14 -0
  59. package/dist-lib/lib/core/contract/activity.d.ts +3 -2
  60. package/dist-lib/lib/core/contract/activity.js +39 -21
  61. package/dist-lib/lib/core/contract/agent-join.js +15 -19
  62. package/dist-lib/lib/core/contract/agents.js +8 -7
  63. package/dist-lib/lib/core/contract/chat.d.ts +3 -0
  64. package/dist-lib/lib/core/contract/chat.js +12 -1
  65. package/dist-lib/lib/core/contract/chunks.js +16 -6
  66. package/dist-lib/lib/core/contract/devices.js +6 -4
  67. package/dist-lib/lib/core/contract/index.js +2 -1
  68. package/dist-lib/lib/core/contract/invites.js +12 -31
  69. package/dist-lib/lib/core/contract/joined-meshes.js +2 -1
  70. package/dist-lib/lib/core/contract/mesh-rules.js +4 -0
  71. package/dist-lib/lib/core/contract/model-requests.js +7 -6
  72. package/dist-lib/lib/core/contract/project-device-keys.js +15 -6
  73. package/dist-lib/lib/core/contract/project-host-keys.js +5 -4
  74. package/dist-lib/lib/core/contract/projects.js +12 -13
  75. package/dist-lib/lib/core/contract/public-seeds.js +5 -5
  76. package/dist-lib/lib/core/contract/roles.js +2 -1
  77. package/dist-lib/lib/core/contract/run-requests.d.ts +1 -1
  78. package/dist-lib/lib/core/contract/run-requests.js +19 -13
  79. package/dist-lib/lib/core/contract/shipped-rows.d.ts +2 -0
  80. package/dist-lib/lib/core/contract/shipped-rows.js +22 -0
  81. package/dist-lib/lib/core/contract/workspace.js +4 -10
  82. package/dist-lib/lib/core/contract/writers.js +20 -6
  83. package/dist-lib/lib/core/devices.d.ts +2 -1
  84. package/dist-lib/lib/core/devices.js +4 -2
  85. package/dist-lib/lib/core/document-attachment.d.ts +6 -3
  86. package/dist-lib/lib/core/document-attachment.js +10 -5
  87. package/dist-lib/lib/core/files/attachments.js +3 -3
  88. package/dist-lib/lib/core/files/index.d.ts +8 -5
  89. package/dist-lib/lib/core/files/index.js +109 -18
  90. package/dist-lib/lib/core/files/processing/index.d.ts +1 -1
  91. package/dist-lib/lib/core/files/processing/index.js +8 -2
  92. package/dist-lib/lib/core/files/processing/source.d.ts +1 -0
  93. package/dist-lib/lib/core/files/processing/source.js +3 -0
  94. package/dist-lib/lib/core/files/processing/state.d.ts +1 -0
  95. package/dist-lib/lib/core/files/processing/state.js +7 -0
  96. package/dist-lib/lib/core/files/rag/build.d.ts +1 -0
  97. package/dist-lib/lib/core/files/rag/build.js +14 -1
  98. package/dist-lib/lib/core/files/rag/index.d.ts +1 -1
  99. package/dist-lib/lib/core/files/rag/index.js +1 -1
  100. package/dist-lib/lib/core/files/rag/produce.js +1 -0
  101. package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +2 -0
  102. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +7 -1
  103. package/dist-lib/lib/core/first-event.d.ts +6 -0
  104. package/dist-lib/lib/core/first-event.js +13 -0
  105. package/dist-lib/lib/core/invite.d.ts +2 -2
  106. package/dist-lib/lib/core/invite.js +5 -4
  107. package/dist-lib/lib/core/local/index.d.ts +10 -1
  108. package/dist-lib/lib/core/local/index.js +105 -10
  109. package/dist-lib/lib/core/mesh.d.ts +15 -4
  110. package/dist-lib/lib/core/mesh.js +128 -57
  111. package/dist-lib/lib/core/migrations/chat-chunks.d.ts +4 -0
  112. package/dist-lib/lib/core/migrations/chat-chunks.js +47 -0
  113. package/dist-lib/lib/core/migrations/file-index.d.ts +1 -1
  114. package/dist-lib/lib/core/migrations/file-index.js +6 -2
  115. package/dist-lib/lib/core/migrations/index.d.ts +2 -1
  116. package/dist-lib/lib/core/migrations/index.js +66 -9
  117. package/dist-lib/lib/core/notification.js +96 -23
  118. package/dist-lib/lib/core/pdf/figure.d.ts +22 -0
  119. package/dist-lib/lib/core/pdf/figure.js +64 -0
  120. package/dist-lib/lib/core/pdf/rasterize.d.ts +10 -1
  121. package/dist-lib/lib/core/pdf/rasterize.js +72 -22
  122. package/dist-lib/lib/core/peer-duplex.js +57 -5
  123. package/dist-lib/lib/core/public-mesh.js +8 -2
  124. package/dist-lib/lib/core/run-requests.js +2 -3
  125. package/dist-lib/lib/core/senders.d.ts +7 -6
  126. package/dist-lib/lib/core/senders.js +23 -30
  127. package/dist-lib/lib/core/shared-agents.d.ts +1 -0
  128. package/dist-lib/lib/core/shared-agents.js +47 -19
  129. package/dist-lib/lib/core/sync/background.d.ts +3 -15
  130. package/dist-lib/lib/core/sync/background.js +25 -102
  131. package/dist-lib/lib/core/sync/devices.js +6 -3
  132. package/dist-lib/lib/core/sync/index.js +12 -2
  133. package/dist-lib/lib/core/sync-status.js +3 -3
  134. package/dist-lib/lib/core.d.ts +59 -10
  135. package/dist-lib/lib/core.js +706 -174
  136. package/dist-lib/lib/credential-pusher.d.ts +11 -7
  137. package/dist-lib/lib/credential-pusher.js +40 -8
  138. package/dist-lib/lib/device-tree.d.ts +18 -0
  139. package/dist-lib/lib/device-tree.js +32 -0
  140. package/dist-lib/lib/doctor/redact.js +1 -1
  141. package/dist-lib/lib/errors.d.ts +4 -0
  142. package/dist-lib/lib/errors.js +4 -0
  143. package/dist-lib/lib/fs-drive-bridge.d.ts +7 -0
  144. package/dist-lib/lib/fs-drive-bridge.js +308 -30
  145. package/dist-lib/lib/integrations/setup-routes.js +7 -5
  146. package/dist-lib/lib/knowledge-search.d.ts +1 -1
  147. package/dist-lib/lib/knowledge-search.js +5 -46
  148. package/dist-lib/lib/kv-cache-retention.d.ts +35 -0
  149. package/dist-lib/lib/kv-cache-retention.js +98 -0
  150. package/dist-lib/lib/latest-frame.d.ts +10 -0
  151. package/dist-lib/lib/latest-frame.js +48 -0
  152. package/dist-lib/lib/model-download.d.ts +6 -1
  153. package/dist-lib/lib/model-download.js +32 -17
  154. package/dist-lib/lib/oauth/resolve.js +13 -5
  155. package/dist-lib/lib/reconnecting-watch.js +19 -11
  156. package/dist-lib/lib/run-notices.d.ts +2 -0
  157. package/dist-lib/lib/run-notices.js +5 -0
  158. package/dist-lib/lib/run-replay.d.ts +13 -1
  159. package/dist-lib/lib/run-replay.js +58 -21
  160. package/dist-lib/schema/build.js +66 -57
  161. package/dist-lib/schema/content.js +2 -1
  162. package/dist-lib/schema/engine.js +37 -5
  163. package/dist-lib/schema/field-order.js +7 -1
  164. package/dist-lib/schema/indexes.d.ts +4 -0
  165. package/dist-lib/schema/indexes.js +18 -0
  166. package/dist-lib/schema/local.js +9 -2
  167. package/dist-lib/schema/qvac.js +112 -4
  168. package/dist-lib/spec/bind.js +3 -0
  169. package/dist-lib/spec/calls.js +1 -0
  170. package/dist-lib/spec/capabilities.d.ts +2 -0
  171. package/dist-lib/spec/hrpc/hrpc.json +13 -0
  172. package/dist-lib/spec/hrpc/index.js +15 -3
  173. package/dist-lib/spec/hrpc/messages.js +1475 -1392
  174. package/dist-lib/spec/hyperschema/index.js +1475 -1392
  175. package/dist-lib/spec/hyperschema/schema.json +86 -1
  176. package/dist-lib/spec/hyperschema/types.d.ts +21 -1
  177. package/dist-lib/spec/local/hyperdb/messages.js +110 -54
  178. package/dist-lib/spec/local/hyperschema/index.js +56 -28
  179. package/dist-lib/spec/local/hyperschema/schema.json +55 -20
  180. package/dist-lib/spec/local/hyperschema/types.d.ts +8 -1
  181. package/dist-lib/spec/mesh/hyperdb/db.json +255 -51
  182. package/dist-lib/spec/mesh/hyperdb/index.js +1063 -404
  183. package/dist-lib/spec/mesh/hyperdb/messages.js +1647 -1214
  184. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +62 -56
  185. package/dist-lib/spec/mesh/hyperdispatch/index.js +156 -139
  186. package/dist-lib/spec/mesh/hyperdispatch/messages.js +787 -629
  187. package/dist-lib/spec/mesh/hyperschema/index.js +787 -629
  188. package/dist-lib/spec/mesh/hyperschema/schema.json +283 -174
  189. package/dist-lib/spec/mesh/hyperschema/types.d.ts +29 -1
  190. package/package.json +23 -17
  191. package/worker/assistant-sidecar.mjs +3 -0
  192. package/dist-lib/lib/core/files/processing/settle.d.ts +0 -13
  193. package/dist-lib/lib/core/files/processing/settle.js +0 -86
  194. package/dist-lib/lib/core/migrations/device-indexes.d.ts +0 -2
  195. package/dist-lib/lib/core/migrations/device-indexes.js +0 -7
@@ -1,6 +1,6 @@
1
1
  import type { Harness } from './assistant.ts';
2
2
  import { type ConfigEngine } from './oauth.ts';
3
- import type { OAuthRouteLike } from './oauth/keys.ts';
3
+ import type { OAuthKeySpec, OAuthRouteLike } from './oauth/keys.ts';
4
4
  export interface CredentialEntry {
5
5
  key: string;
6
6
  token?: string | null;
@@ -14,19 +14,21 @@ export interface SettingEntry {
14
14
  value: string;
15
15
  agentId?: string | null;
16
16
  }
17
+ type PusherRoute = OAuthRouteLike & {
18
+ fields?: ReadonlyArray<{
19
+ key: string;
20
+ secret?: boolean;
21
+ }>;
22
+ };
17
23
  export interface PusherSkill {
18
24
  name: string;
25
+ origin?: string;
19
26
  credentials?: readonly string[];
20
27
  requires?: {
21
28
  credentials?: readonly string[];
22
29
  };
23
30
  setup?: {
24
- routes?: ReadonlyArray<OAuthRouteLike & {
25
- fields?: ReadonlyArray<{
26
- key: string;
27
- secret?: boolean;
28
- }>;
29
- }>;
31
+ routes?: ReadonlyArray<PusherRoute>;
30
32
  };
31
33
  }
32
34
  export interface CredentialEngine extends ConfigEngine {
@@ -67,6 +69,8 @@ export type CredentialPusherHandle = (() => Promise<void>) & {
67
69
  };
68
70
  export declare function nextRefreshDelay(entries: ReadonlyArray<Pick<CredentialEntry, 'expiresAt'>>, now?: number): number;
69
71
  export declare function credentialOwners(catalog: ReadonlyArray<PusherSkill>): ReadonlyMap<string, readonly string[]>;
72
+ export declare function plainCredentialKeys(catalog: ReadonlyArray<PusherSkill>, specs: ReadonlyArray<OAuthKeySpec>): ReadonlyArray<string>;
70
73
  export declare function settingFields(catalog: ReadonlyArray<PusherSkill>): ReadonlyArray<Pick<SettingEntry, 'skill' | 'key'>>;
71
74
  export declare function settingConfigKey(agentId: string | undefined, skill: string, key: string): string;
72
75
  export declare function startCredentialPusher(harness: CredentialTarget, engine: CredentialEngine, logger?: CredentialPusherLogger | null): CredentialPusherHandle;
76
+ export {};
@@ -8,6 +8,7 @@ import ScopeLock from 'scope-lock';
8
8
  import { credentialSlots } from '@qvac/harness/skill-manifest';
9
9
  import { errorMessage } from './guards.js';
10
10
  const PLAIN_KEYS = ['github_access_token', OBSIDIAN_VAULT_PATH_CREDENTIAL_KEY];
11
+ const PLAIN_KEY = /^[A-Za-z0-9_-]+$/;
11
12
  const REFRESH_INTERVAL = 10 * 60 * 1000;
12
13
  const MIN_REFRESH_DELAY = 30_000;
13
14
  function storeKey(key, agentId) {
@@ -69,7 +70,7 @@ async function plainEntry(engine, key, agentId) {
69
70
  }
70
71
  export function credentialOwners(catalog) {
71
72
  const owners = new Map();
72
- for (const skill of catalog) {
73
+ for (const skill of trusted(catalog)) {
73
74
  for (const key of credentialSlots(skill).flat()) {
74
75
  const list = owners.get(key) ?? [];
75
76
  if (!list.includes(skill.name)) {
@@ -80,6 +81,37 @@ export function credentialOwners(catalog) {
80
81
  }
81
82
  return owners;
82
83
  }
84
+ function trusted(catalog) {
85
+ return catalog.filter((skill) => skill.origin !== 'overlay');
86
+ }
87
+ export function plainCredentialKeys(catalog, specs) {
88
+ const oauth = new Set(specs.map((spec) => spec.credentialKey));
89
+ const reserved = new Set();
90
+ const declared = new Set(PLAIN_KEYS);
91
+ for (const skill of trusted(catalog)) {
92
+ for (const key of credentialSlots(skill).flat()) {
93
+ declared.add(key);
94
+ }
95
+ for (const route of skill.setup?.routes ?? []) {
96
+ routeKeys(route, route.oauth ? reserved : declared);
97
+ }
98
+ }
99
+ const allowed = (key) => PLAIN_KEYS.includes(key) || (!reserved.has(key) && PLAIN_KEY.test(key));
100
+ return [...declared].filter((key) => !oauth.has(key) && allowed(key));
101
+ }
102
+ function routeKeys(route, into) {
103
+ if (route.oauth?.stateKey) {
104
+ into.add(route.oauth.stateKey);
105
+ }
106
+ if (route.credentialKey && !route.oauth) {
107
+ into.add(route.credentialKey);
108
+ }
109
+ for (const field of route.fields ?? []) {
110
+ if (route.oauth || field.secret === true) {
111
+ into.add(field.key);
112
+ }
113
+ }
114
+ }
83
115
  export function settingFields(catalog) {
84
116
  const fields = [];
85
117
  for (const skill of catalog) {
@@ -118,15 +150,14 @@ async function resolveEntry({ mk, failure, read }, { last, owners, logger }) {
118
150
  }
119
151
  }
120
152
  async function resolveScope(agentId, resolution) {
121
- const { engine, specs } = resolution;
122
- const specKeys = new Set(specs.map((spec) => spec.credentialKey));
153
+ const { engine, specs, plainKeys } = resolution;
123
154
  const pending = [
124
155
  ...specs.map((spec) => ({
125
156
  mk: storeKey(spec.credentialKey, agentId),
126
157
  failure: `credential resolve failed for ${spec.credentialKey}`,
127
158
  read: () => oauthEntry(engine, spec, agentId)
128
159
  })),
129
- ...PLAIN_KEYS.filter((key) => !specKeys.has(key)).map((key) => ({
160
+ ...plainKeys.map((key) => ({
130
161
  mk: storeKey(key, agentId),
131
162
  failure: `credential read failed for ${key}`,
132
163
  read: () => plainEntry(engine, key, agentId)
@@ -192,10 +223,10 @@ export function startCredentialPusher(harness, engine, logger) {
192
223
  logger?.warn('credential-pusher', `credential watch ended for ${key}`, errorMessage(err));
193
224
  });
194
225
  };
195
- const syncWatches = (oauthKeys, scopes) => {
226
+ const syncWatches = (pushedKeys, scopes) => {
196
227
  if (disposed)
197
228
  return;
198
- const keys = new Set([...oauthKeys, ...PLAIN_KEYS]);
229
+ const keys = new Set(pushedKeys);
199
230
  const wanted = new Set(scopes.flatMap((agentId) => [...keys].map((key) => scopedKey(agentId, key))));
200
231
  for (const [key, stream] of watches) {
201
232
  if (wanted.has(key))
@@ -272,10 +303,11 @@ export function startCredentialPusher(harness, engine, logger) {
272
303
  }
273
304
  const owners = credentialOwners(skills);
274
305
  const specs = oauthSpecs(skills);
306
+ const plainKeys = plainCredentialKeys(skills, specs);
275
307
  const { meshId, scopes } = await accountScopes(engine);
276
- syncWatches(specs.map((spec) => spec.credentialKey), scopes);
308
+ syncWatches([...specs.map((spec) => spec.credentialKey), ...plainKeys], scopes);
277
309
  syncAgentsWatch(meshId);
278
- const resolution = { engine, last, owners, specs, logger };
310
+ const resolution = { engine, last, owners, specs, plainKeys, logger };
279
311
  const resolved = await Promise.all(scopes.map((agentId) => resolveScope(agentId, resolution)));
280
312
  entries.push(...resolved.flat());
281
313
  if (disposed) {
@@ -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
+ }
@@ -25,7 +25,7 @@ export function redact(value) {
25
25
  }
26
26
  const out = {};
27
27
  for (const [key, child] of Object.entries(value)) {
28
- out[key] = REDACTED_KEYS.has(key) ? REDACTED : redact(child);
28
+ out[redactString(key)] = REDACTED_KEYS.has(key) ? REDACTED : redact(child);
29
29
  }
30
30
  return out;
31
31
  }
@@ -751,6 +751,10 @@ declare const DEFINITIONS: {
751
751
  readonly code: 101304;
752
752
  readonly message: () => string;
753
753
  };
754
+ readonly SIDECAR_BOOT_FAILED: {
755
+ readonly code: 101305;
756
+ readonly message: (reason: string) => string;
757
+ };
754
758
  readonly RUN_LOG_NOT_ASSIGNED: {
755
759
  readonly code: 101401;
756
760
  readonly message: (runId: string) => string;
@@ -666,6 +666,10 @@ const DEFINITIONS = {
666
666
  code: 101304,
667
667
  message: () => 'assistant is closing'
668
668
  },
669
+ SIDECAR_BOOT_FAILED: {
670
+ code: 101305,
671
+ message: (reason) => `the sidecar failed to start: ${reason}`
672
+ },
669
673
  RUN_LOG_NOT_ASSIGNED: {
670
674
  code: 101401,
671
675
  message: (runId) => `run ${runId} is not one this device runs`
@@ -1,8 +1,14 @@
1
+ import { type AbortSignal } from 'bare-abort-controller';
1
2
  import type { DriveOpFn, GrantedDrive } from '@qvac/harness';
2
3
  import type { AcquiredDrive, DriveHost } from './core/fs-drives.ts';
3
4
  import type { FsGrant } from './fs-grants.ts';
4
5
  export declare const DRIVE_OP_TIMEOUT_MS = 15000;
5
6
  export declare function sweepDriveStaging(now?: number): Promise<void>;
7
+ interface PipeCappedOptions {
8
+ readonly cap: number;
9
+ readonly signal: AbortSignal;
10
+ }
11
+ export declare function pipeCapped(source: AsyncIterable<Uint8Array>, target: string, { cap, signal }: PipeCappedOptions): Promise<boolean>;
6
12
  export declare function createDriveOp(drives: ReadonlyMap<string, AcquiredDrive>): DriveOpFn;
7
13
  export interface ResolvedFsGrants {
8
14
  readonly grantedDirs: string[];
@@ -10,3 +16,4 @@ export interface ResolvedFsGrants {
10
16
  readonly driveOp?: DriveOpFn;
11
17
  }
12
18
  export declare function resolveFsGrants(grants: readonly FsGrant[], driveHost: DriveHost | undefined, onDropped?: (key: string, err: unknown) => void): Promise<ResolvedFsGrants>;
19
+ export {};
@@ -10,6 +10,64 @@ const MAX_DRIVE_BYTES = 8 * 1024 * 1024;
10
10
  const STAGING_DIR = userTmpPath('qvac-fs-drive-host');
11
11
  const STAGING_MAX_AGE_MS = 60 * 60 * 1000;
12
12
  const noop = () => { };
13
+ function waitFor(promise, signal) {
14
+ if (signal.aborted) {
15
+ return Promise.reject(signal.reason ?? new Error('drive operation cancelled'));
16
+ }
17
+ return new Promise((resolve, reject) => {
18
+ let settled = false;
19
+ const finish = (result) => {
20
+ if (settled)
21
+ return;
22
+ settled = true;
23
+ signal.removeEventListener('abort', onAbort);
24
+ if (result.ok) {
25
+ resolve(result.value);
26
+ }
27
+ else {
28
+ reject(result.error);
29
+ }
30
+ };
31
+ const onAbort = () => finish({ ok: false, error: signal.reason ?? new Error('drive operation cancelled') });
32
+ signal.addEventListener('abort', onAbort, { once: true });
33
+ promise.then((value) => finish({ ok: true, value }), (error) => finish({ ok: false, error }));
34
+ });
35
+ }
36
+ async function* abortableIterable(source, signal) {
37
+ const iterator = source[Symbol.asyncIterator]();
38
+ let returning = null;
39
+ const returnIterator = () => {
40
+ if (!returning) {
41
+ returning = Promise.resolve(iterator.return?.());
42
+ }
43
+ return returning;
44
+ };
45
+ const cancel = () => {
46
+ if ('destroy' in source && typeof source.destroy === 'function') {
47
+ source.destroy();
48
+ }
49
+ void returnIterator().catch(noop);
50
+ };
51
+ signal.addEventListener('abort', cancel, { once: true });
52
+ try {
53
+ while (true) {
54
+ const next = await waitFor(iterator.next(), signal);
55
+ if (next.done) {
56
+ return;
57
+ }
58
+ yield next.value;
59
+ }
60
+ }
61
+ finally {
62
+ signal.removeEventListener('abort', cancel);
63
+ if (signal.aborted) {
64
+ cancel();
65
+ }
66
+ else {
67
+ await returnIterator().catch(noop);
68
+ }
69
+ }
70
+ }
13
71
  export async function sweepDriveStaging(now = Date.now()) {
14
72
  const names = await fs.promises.readdir(STAGING_DIR).catch(() => []);
15
73
  await Promise.all(names.map(async (name) => {
@@ -45,38 +103,141 @@ async function stagedPath(prefix) {
45
103
  function stripMetadata(entry) {
46
104
  return { ...entry, value: { ...entry.value, metadata: null } };
47
105
  }
48
- async function pipeCapped(source, target, { cap, signal }) {
106
+ export async function pipeCapped(source, target, { cap, signal }) {
107
+ const iterator = source[Symbol.asyncIterator]();
49
108
  const sink = fs.createWriteStream(target, { mode: 0o600 });
109
+ const sinkClosed = new Promise((resolve) => sink.once('close', resolve));
50
110
  let sinkError = null;
111
+ let rejectSink = null;
112
+ const sinkFailure = new Promise((_, reject) => {
113
+ rejectSink = reject;
114
+ });
115
+ void sinkFailure.catch(noop);
51
116
  sink.on('error', (err) => {
52
117
  sinkError = err;
118
+ rejectSink?.(err);
53
119
  });
54
120
  let written = 0;
55
121
  let truncated = false;
122
+ let completed = false;
123
+ let returning = null;
124
+ const returnIterator = () => {
125
+ if (!returning) {
126
+ returning = Promise.resolve(iterator.return?.());
127
+ }
128
+ return returning;
129
+ };
130
+ const abortError = () => signal.reason instanceof Error ? signal.reason : new Error('drive read aborted');
131
+ const abort = () => {
132
+ if ('destroy' in source && typeof source.destroy === 'function') {
133
+ source.destroy(abortError());
134
+ }
135
+ void returnIterator().catch(noop);
136
+ sink.destroy(abortError());
137
+ };
138
+ const next = async () => {
139
+ if (signal.aborted) {
140
+ throw abortError();
141
+ }
142
+ let onAbort = null;
143
+ try {
144
+ return await Promise.race([
145
+ iterator.next(),
146
+ sinkFailure,
147
+ new Promise((_, reject) => {
148
+ onAbort = () => reject(abortError());
149
+ signal.addEventListener('abort', onAbort, { once: true });
150
+ })
151
+ ]);
152
+ }
153
+ finally {
154
+ if (onAbort) {
155
+ signal.removeEventListener('abort', onAbort);
156
+ }
157
+ }
158
+ };
159
+ signal.addEventListener('abort', abort, { once: true });
160
+ let failure = null;
56
161
  try {
57
- for await (const chunk of source) {
58
- if (signal.aborted)
162
+ while (true) {
163
+ if (signal.aborted) {
164
+ throw abortError();
165
+ }
166
+ const item = await next();
167
+ if (signal.aborted) {
168
+ throw abortError();
169
+ }
170
+ if (item.done) {
59
171
  break;
60
- if (sinkError)
172
+ }
173
+ const chunk = item.value;
174
+ if (sinkError) {
61
175
  throw sinkError;
176
+ }
62
177
  if (written >= cap) {
63
178
  truncated = true;
64
179
  break;
65
180
  }
66
181
  const room = cap - written;
67
182
  const slice = chunk.byteLength > room ? chunk.subarray(0, room) : chunk;
68
- sink.write(slice);
183
+ if (!sink.write(slice)) {
184
+ await new Promise((resolve, reject) => {
185
+ const onDrain = () => {
186
+ sink.off('error', onError);
187
+ signal.removeEventListener('abort', onAbort);
188
+ resolve();
189
+ };
190
+ const onError = (err) => {
191
+ sink.off('drain', onDrain);
192
+ signal.removeEventListener('abort', onAbort);
193
+ reject(err);
194
+ };
195
+ const onAbort = () => {
196
+ sink.off('drain', onDrain);
197
+ sink.off('error', onError);
198
+ reject(abortError());
199
+ };
200
+ sink.once('drain', onDrain);
201
+ sink.once('error', onError);
202
+ signal.addEventListener('abort', onAbort, { once: true });
203
+ });
204
+ }
69
205
  written += slice.byteLength;
70
206
  if (slice.byteLength < chunk.byteLength) {
71
207
  truncated = true;
208
+ break;
72
209
  }
73
210
  }
211
+ completed = true;
212
+ }
213
+ catch (err) {
214
+ failure = err;
215
+ if ('destroy' in source && typeof source.destroy === 'function') {
216
+ source.destroy(err instanceof Error ? err : new Error(String(err)));
217
+ }
74
218
  }
75
219
  finally {
76
- await new Promise((resolve) => sink.end(resolve));
220
+ signal.removeEventListener('abort', abort);
221
+ if (!completed || truncated) {
222
+ await returnIterator().catch((err) => (failure ??= err));
223
+ }
224
+ if (signal.aborted || sinkError || sink.destroyed) {
225
+ if (!sink.destroyed) {
226
+ sink.destroy();
227
+ }
228
+ await sinkClosed;
229
+ }
230
+ else {
231
+ sink.end();
232
+ await sinkClosed;
233
+ }
77
234
  }
78
235
  if (sinkError) {
79
- throw sinkError;
236
+ failure ??= sinkError;
237
+ }
238
+ if (failure) {
239
+ await fs.promises.unlink(target).catch(noop);
240
+ throw failure;
80
241
  }
81
242
  return truncated;
82
243
  }
@@ -84,42 +245,138 @@ function folderRange(folder) {
84
245
  const trimmed = folder === '/' ? '' : folder.replace(/\/+$/, '');
85
246
  return { gt: `${trimmed}/`, lt: `${trimmed}0` };
86
247
  }
248
+ function childDirKey(folder, key) {
249
+ const prefix = folder === '/' ? '/' : `${folder.replace(/\/+$/, '')}/`;
250
+ const child = key.slice(prefix.length);
251
+ const slash = child.indexOf('/');
252
+ return slash === -1 ? null : `${prefix}${child.slice(0, slash)}`;
253
+ }
254
+ function directChild(folder, entry) {
255
+ const dir = childDirKey(folder, entry.key);
256
+ return dir ? { key: dir, dir: true } : stripMetadata(entry);
257
+ }
258
+ async function* childKeys(drive, options) {
259
+ const { folder } = options;
260
+ let range = options.start;
261
+ while (true) {
262
+ let past = null;
263
+ for await (const raw of abortableIterable(drive.entries(range, { ignore: options.ignore }), options.signal)) {
264
+ yield raw;
265
+ const dir = childDirKey(folder, raw.key);
266
+ if (dir) {
267
+ past = `${dir}0`;
268
+ break;
269
+ }
270
+ }
271
+ if (!past) {
272
+ return;
273
+ }
274
+ range = { gte: past, lt: options.range.lt };
275
+ }
276
+ }
277
+ async function shallowEntries(drive, request, options) {
278
+ const { signal, range } = options;
279
+ const entries = [];
280
+ let current = null;
281
+ let currentLastKey = null;
282
+ let lastEmittedKey = null;
283
+ let cursor = null;
284
+ const cursorDir = request.cursor ? childDirKey(request.folder, request.cursor) : null;
285
+ const start = cursorDir
286
+ ? { gte: `${cursorDir}0`, lt: range.lt }
287
+ : { gt: request.cursor ?? range.gt, lt: range.lt };
288
+ for await (const raw of childKeys(drive, { ...options, folder: request.folder, start })) {
289
+ if (signal.aborted) {
290
+ throw new Error('drive list cancelled');
291
+ }
292
+ const child = directChild(request.folder, raw);
293
+ if (current && current.key !== child.key) {
294
+ if (entries.length >= request.limit) {
295
+ cursor = lastEmittedKey;
296
+ break;
297
+ }
298
+ entries.push(current);
299
+ lastEmittedKey = currentLastKey;
300
+ current = child;
301
+ }
302
+ current ??= child;
303
+ currentLastKey = raw.key;
304
+ }
305
+ if (!current || cursor) {
306
+ return { entries, cursor };
307
+ }
308
+ if (entries.length >= request.limit) {
309
+ return { entries, cursor: lastEmittedKey };
310
+ }
311
+ entries.push(current);
312
+ return { entries, cursor };
313
+ }
87
314
  async function handleRequest(acquired, request, signal) {
88
315
  const drive = acquired.drive;
89
316
  switch (request.op) {
90
317
  case 'entry': {
91
- const entry = await drive.entry(request.key, { timeout: DRIVE_OP_TIMEOUT_MS, wait: true });
318
+ if (signal.aborted) {
319
+ throw new Error('drive entry cancelled');
320
+ }
321
+ const entry = await waitFor(drive.entry(request.key, { timeout: DRIVE_OP_TIMEOUT_MS, wait: true }), signal);
322
+ if (signal.aborted) {
323
+ throw new Error('drive entry cancelled');
324
+ }
92
325
  return { entry: entry ? stripMetadata(entry) : null };
93
326
  }
94
327
  case 'get': {
95
- const entry = await drive.entry(request.key, { timeout: DRIVE_OP_TIMEOUT_MS, wait: true });
328
+ const entry = await waitFor(drive.entry(request.key, { timeout: DRIVE_OP_TIMEOUT_MS, wait: true }), signal);
329
+ if (signal.aborted) {
330
+ throw new Error('drive get cancelled');
331
+ }
96
332
  if (!entry) {
97
333
  return { path: null };
98
334
  }
99
335
  if ((entry.value.blob?.byteLength ?? 0) > MAX_DRIVE_BYTES) {
100
336
  return { error: `get exceeds ${MAX_DRIVE_BYTES} bytes: ${request.key}` };
101
337
  }
102
- const buffer = await drive.get(request.key, { timeout: DRIVE_OP_TIMEOUT_MS, wait: true });
338
+ const buffer = await waitFor(drive.get(request.key, { timeout: DRIVE_OP_TIMEOUT_MS, wait: true }), signal);
339
+ if (signal.aborted) {
340
+ throw new Error('drive get cancelled');
341
+ }
103
342
  if (!buffer) {
104
343
  return { path: null };
105
344
  }
106
345
  const target = await stagedPath('drive-get');
107
- signal.addEventListener('abort', () => void fs.promises.unlink(target).catch(noop), {
108
- once: true
109
- });
110
- await fs.promises.writeFile(target, buffer, { mode: 0o600 });
111
- return { path: target };
346
+ try {
347
+ if (signal.aborted) {
348
+ throw new Error('drive get cancelled');
349
+ }
350
+ await fs.promises.writeFile(target, buffer, { mode: 0o600 });
351
+ if (signal.aborted) {
352
+ throw new Error('drive get cancelled');
353
+ }
354
+ return { path: target };
355
+ }
356
+ catch (err) {
357
+ await fs.promises.unlink(target).catch(noop);
358
+ throw err;
359
+ }
112
360
  }
113
361
  case 'read': {
114
362
  const target = await stagedPath('drive-read');
115
- signal.addEventListener('abort', () => void fs.promises.unlink(target).catch(noop), {
116
- once: true
117
- });
118
- const truncated = await pipeCapped(drive.createReadStream(request.key), target, {
119
- cap: MAX_DRIVE_BYTES,
120
- signal
121
- });
122
- return { path: target, truncated };
363
+ try {
364
+ if (signal.aborted) {
365
+ throw new Error('drive read cancelled');
366
+ }
367
+ const truncated = await pipeCapped(drive.createReadStream(request.key), target, {
368
+ cap: MAX_DRIVE_BYTES,
369
+ signal
370
+ });
371
+ if (signal.aborted) {
372
+ throw new Error('drive read cancelled');
373
+ }
374
+ return { path: target, truncated };
375
+ }
376
+ catch (err) {
377
+ await fs.promises.unlink(target).catch(noop);
378
+ throw err;
379
+ }
123
380
  }
124
381
  case 'list': {
125
382
  const skip = new Set(request.skip);
@@ -128,7 +385,12 @@ async function handleRequest(acquired, request, signal) {
128
385
  const ignore = (key) => key.split('/').some((seg) => skip.has(seg));
129
386
  const range = folderRange(request.folder);
130
387
  const gt = request.cursor ?? range.gt;
131
- for await (const raw of drive.entries({ gt, lt: range.lt }, { ignore })) {
388
+ if (request.recursive === false) {
389
+ return shallowEntries(drive, request, { signal, ignore, range });
390
+ }
391
+ for await (const raw of abortableIterable(drive.entries({ gt, lt: range.lt }, { ignore }), signal)) {
392
+ if (signal.aborted)
393
+ throw new Error('drive list cancelled');
132
394
  entries.push(stripMetadata(raw));
133
395
  if (entries.length >= request.limit) {
134
396
  cursor = raw.key;
@@ -141,18 +403,34 @@ async function handleRequest(acquired, request, signal) {
141
403
  if (!acquired.writable) {
142
404
  return { error: `drive is read-only: cannot write ${request.key}` };
143
405
  }
406
+ const stagingRoot = await fs.promises
407
+ .realpath(HARNESS_STAGING_DIR)
408
+ .catch(() => path.resolve(HARNESS_STAGING_DIR));
144
409
  const resolvedPath = path.resolve(request.path);
145
- if (path.relative(HARNESS_STAGING_DIR, resolvedPath).startsWith('..')) {
410
+ const realPath = await fs.promises.realpath(resolvedPath);
411
+ if (path.relative(stagingRoot, realPath).startsWith('..') || realPath === stagingRoot) {
146
412
  return { error: `write path is outside the staging directory: ${request.path}` };
147
413
  }
148
- const source = Readable.from(fs.createReadStream(resolvedPath));
414
+ if (signal.aborted) {
415
+ throw new Error('drive write cancelled');
416
+ }
417
+ const source = Readable.from(fs.createReadStream(realPath));
149
418
  const sink = drive.createWriteStream(request.key, request.executable ? { executable: true } : undefined);
150
- signal.addEventListener('abort', () => {
419
+ const abort = () => {
151
420
  source.destroy();
152
421
  sink.destroy();
153
- }, { once: true });
154
- await pipelinePromise(source, sink);
155
- await fs.promises.unlink(resolvedPath).catch(noop);
422
+ };
423
+ signal.addEventListener('abort', abort, { once: true });
424
+ try {
425
+ await pipelinePromise(source, sink);
426
+ if (signal.aborted) {
427
+ throw new Error('drive write cancelled');
428
+ }
429
+ await fs.promises.unlink(realPath).catch(noop);
430
+ }
431
+ finally {
432
+ signal.removeEventListener('abort', abort);
433
+ }
156
434
  return {};
157
435
  }
158
436
  case 'search': {