@qvac/core 0.6.6 → 0.7.0

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 (123) hide show
  1. package/CHANGELOG.md +185 -1
  2. package/README.md +2 -0
  3. package/dist-lib/lib/api/app-config.d.ts +3 -1
  4. package/dist-lib/lib/api/app-config.js +15 -4
  5. package/dist-lib/lib/assistant/fake-models.d.ts +22 -0
  6. package/dist-lib/lib/assistant/fake-models.js +112 -0
  7. package/dist-lib/lib/assistant/harness.d.ts +10 -0
  8. package/dist-lib/lib/assistant/harness.js +26 -0
  9. package/dist-lib/lib/assistant/history.d.ts +5 -1
  10. package/dist-lib/lib/assistant/history.js +14 -2
  11. package/dist-lib/lib/assistant-tree.d.ts +4 -0
  12. package/dist-lib/lib/assistant-tree.js +45 -7
  13. package/dist-lib/lib/assistant.d.ts +16 -2
  14. package/dist-lib/lib/assistant.js +184 -44
  15. package/dist-lib/lib/constants.d.ts +6 -0
  16. package/dist-lib/lib/constants.js +6 -0
  17. package/dist-lib/lib/core/chat-activity.d.ts +1 -1
  18. package/dist-lib/lib/core/chat-activity.js +3 -0
  19. package/dist-lib/lib/core/collections.d.ts +1 -0
  20. package/dist-lib/lib/core/collections.js +1 -0
  21. package/dist-lib/lib/core/contract/compute.d.ts +1 -1
  22. package/dist-lib/lib/core/contract/compute.js +2 -1
  23. package/dist-lib/lib/core/contract/index.js +6 -1
  24. package/dist-lib/lib/core/contract/invites.d.ts +1 -11
  25. package/dist-lib/lib/core/contract/invites.js +3 -3
  26. package/dist-lib/lib/core/contract/mesh-seeds.d.ts +2 -0
  27. package/dist-lib/lib/core/contract/mesh-seeds.js +26 -0
  28. package/dist-lib/lib/core/contract/open-join.d.ts +2 -0
  29. package/dist-lib/lib/core/contract/open-join.js +57 -0
  30. package/dist-lib/lib/core/departures.d.ts +3 -0
  31. package/dist-lib/lib/core/departures.js +44 -1
  32. package/dist-lib/lib/core/fs-drives.js +9 -6
  33. package/dist-lib/lib/core/handoff-channel.d.ts +1 -0
  34. package/dist-lib/lib/core/handoff-channel.js +7 -2
  35. package/dist-lib/lib/core/harness-loan.d.ts +1 -0
  36. package/dist-lib/lib/core/harness-loan.js +7 -2
  37. package/dist-lib/lib/core/identity.d.ts +1 -1
  38. package/dist-lib/lib/core/identity.js +6 -4
  39. package/dist-lib/lib/core/invite.d.ts +2 -0
  40. package/dist-lib/lib/core/invite.js +6 -2
  41. package/dist-lib/lib/core/membership.d.ts +1 -12
  42. package/dist-lib/lib/core/membership.js +1 -94
  43. package/dist-lib/lib/core/mesh.d.ts +8 -13
  44. package/dist-lib/lib/core/mesh.js +41 -87
  45. package/dist-lib/lib/core/protocol-pairing.d.ts +11 -0
  46. package/dist-lib/lib/core/protocol-pairing.js +22 -0
  47. package/dist-lib/lib/core/sync/index.js +2 -16
  48. package/dist-lib/lib/core.d.ts +43 -49
  49. package/dist-lib/lib/core.js +451 -909
  50. package/dist-lib/lib/diagnostics.d.ts +7 -0
  51. package/dist-lib/lib/diagnostics.js +22 -0
  52. package/dist-lib/lib/doctor/checks.d.ts +15 -0
  53. package/dist-lib/lib/doctor/checks.js +269 -0
  54. package/dist-lib/lib/doctor/redact.d.ts +4 -0
  55. package/dist-lib/lib/doctor/redact.js +31 -0
  56. package/dist-lib/lib/doctor/render.d.ts +8 -0
  57. package/dist-lib/lib/doctor/render.js +83 -0
  58. package/dist-lib/lib/doctor/tree.d.ts +5 -0
  59. package/dist-lib/lib/doctor/tree.js +167 -0
  60. package/dist-lib/lib/downloads-allowed-pusher.d.ts +10 -0
  61. package/dist-lib/lib/downloads-allowed-pusher.js +37 -0
  62. package/dist-lib/lib/guards.d.ts +1 -0
  63. package/dist-lib/lib/guards.js +4 -0
  64. package/dist-lib/lib/inference-calls.js +1 -5
  65. package/dist-lib/lib/kv-cache-reclaim.d.ts +2 -1
  66. package/dist-lib/lib/kv-cache-reclaim.js +24 -0
  67. package/dist-lib/lib/mesh-scoped-engine.js +1 -0
  68. package/dist-lib/lib/model-download-status.d.ts +1 -1
  69. package/dist-lib/lib/model-download.d.ts +9 -1
  70. package/dist-lib/lib/model-download.js +48 -3
  71. package/dist-lib/lib/model-memory-cap.d.ts +29 -0
  72. package/dist-lib/lib/model-memory-cap.js +69 -0
  73. package/dist-lib/lib/models-watch.d.ts +1 -1
  74. package/dist-lib/lib/models-watch.js +4 -4
  75. package/dist-lib/lib/oauth/flow.js +1 -1
  76. package/dist-lib/lib/oauth/loopback.js +16 -2
  77. package/dist-lib/lib/oauth/redirect-types.d.ts +1 -0
  78. package/dist-lib/lib/run-replay.d.ts +26 -0
  79. package/dist-lib/lib/run-replay.js +107 -0
  80. package/dist-lib/schema/config.d.ts +1 -1
  81. package/dist-lib/schema/config.js +1 -1
  82. package/dist-lib/schema/content.js +9 -1
  83. package/dist-lib/schema/engine.js +30 -5
  84. package/dist-lib/schema/local.js +3 -2
  85. package/dist-lib/schema/qvac.js +18 -1
  86. package/dist-lib/spec/bind.js +3 -0
  87. package/dist-lib/spec/calls.js +1 -0
  88. package/dist-lib/spec/capabilities.d.ts +2 -0
  89. package/dist-lib/spec/config/hyperconf/index.js +1 -1
  90. package/dist-lib/spec/hrpc/hrpc.json +13 -0
  91. package/dist-lib/spec/hrpc/index.js +15 -3
  92. package/dist-lib/spec/hrpc/messages.js +646 -557
  93. package/dist-lib/spec/hyperschema/index.js +646 -557
  94. package/dist-lib/spec/hyperschema/schema.json +80 -0
  95. package/dist-lib/spec/hyperschema/types.d.ts +20 -3
  96. package/dist-lib/spec/local/hyperdb/messages.js +22 -10
  97. package/dist-lib/spec/local/hyperschema/index.js +16 -7
  98. package/dist-lib/spec/local/hyperschema/schema.json +10 -1
  99. package/dist-lib/spec/local/hyperschema/types.d.ts +3 -2
  100. package/dist-lib/spec/mesh/hyperdb/db.json +16 -1
  101. package/dist-lib/spec/mesh/hyperdb/index.js +71 -2
  102. package/dist-lib/spec/mesh/hyperdb/messages.js +477 -240
  103. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +13 -1
  104. package/dist-lib/spec/mesh/hyperdispatch/index.js +35 -1
  105. package/dist-lib/spec/mesh/hyperdispatch/messages.js +180 -7
  106. package/dist-lib/spec/mesh/hyperschema/index.js +180 -7
  107. package/dist-lib/spec/mesh/hyperschema/schema.json +98 -1
  108. package/dist-lib/spec/mesh/hyperschema/types.d.ts +26 -2
  109. package/package.json +9 -17
  110. package/tui/README.md +7 -1
  111. package/tui/app.mjs +245 -11
  112. package/tui/boot.mjs +1 -0
  113. package/tui/index.mjs +1 -0
  114. package/tui/lib/render.mjs +25 -1
  115. package/worker/assistant-sidecar.mjs +5 -2
  116. package/dist-lib/lib/core/device-invites.d.ts +0 -11
  117. package/dist-lib/lib/core/device-invites.js +0 -63
  118. package/dist-lib/lib/core/grant-expiry.d.ts +0 -11
  119. package/dist-lib/lib/core/grant-expiry.js +0 -66
  120. package/dist-lib/lib/core/mesh-seed-handoff.d.ts +0 -27
  121. package/dist-lib/lib/core/mesh-seed-handoff.js +0 -53
  122. package/dist-lib/lib/oauth/loopback.stub.d.ts +0 -3
  123. package/dist-lib/lib/oauth/loopback.stub.js +0 -6
package/CHANGELOG.md CHANGED
@@ -13,6 +13,188 @@ release.
13
13
 
14
14
  ## [Unreleased]
15
15
 
16
+ ## [0.7.0] - 2026-10-09
17
+
18
+ ### Added
19
+
20
+ - Model downloads can wait to be asked for, so a phone on cellular does not
21
+ fetch gigabytes on its own. Pass `autoDownloadModels: false` to the Core
22
+ constructor so it holds from the first boot, or toggle it later. A run that
23
+ needs a missing model then waits as `awaiting-download` instead of fetching it,
24
+ and so do image, music, transcription, speech and embedding loads in the
25
+ harness (#332, #333):
26
+
27
+ ```js
28
+ await engine.setAutoDownloadModels({ enabled: false })
29
+ for await (const { downloads } of engine.modelDownloadsWatch({})) {
30
+ const held = (downloads ?? []).find((row) => row.phase === 'held')
31
+ if (held) {
32
+ await engine.retryModelDownload({ name: held.name })
33
+ break
34
+ }
35
+ }
36
+ ```
37
+
38
+ - The OAuth loopback serves callbacks under Bare too, so a mobile worklet can
39
+ complete sign-in on the same `127.0.0.1` redirect desktop registers. (#329)
40
+ - `openOAuthLoopback` takes an optional `returnTo`: the callback redirects the
41
+ browser there instead of showing the done page. (#329)
42
+ - Several models stay loaded when memory allows (QVAC-25735). On desktop the
43
+ assistant pushes `model.memoryCapPercent` to the harness: loaded models may
44
+ fill that share of the memory usable for models, 85% by default, so two chat
45
+ models that fit stay loaded and switching chats between them loads neither
46
+ again. A load that would not fit unloads idle models one at a time, other
47
+ kinds before chat models and the least recently used first. It applies with
48
+ no restart; the minimum keeps one chat model loaded, and a mobile device
49
+ always does. `listModels` and `modelsWatch` carry the device's `memory`
50
+ (`usableBytes`, `capPercent`, `capBytes`, `usedBytes`, and `overCap` when a
51
+ model larger than the cap loaded anyway) and each loaded row its
52
+ `memoryBytes`, so a Models screen shows used against the cap. Needs the
53
+ `@qvac/harness` release with `setModelMemoryCap` and `memoryUsage`; an older
54
+ one keeps one chat model loaded and lists no `memory`. (#343)
55
+
56
+ ```js
57
+ import { MODEL_MEMORY_CAP_KEY } from '@qvac/core/constants'
58
+
59
+ await engine.configSet({ key: MODEL_MEMORY_CAP_KEY, value: 70 })
60
+ const { memory } = await engine.listModels({ cachedOnly: true })
61
+ ```
62
+
63
+ - A device pairs and follows its member with no other device online. A device
64
+ invite carries a keypair attested into the inviter's member, so the joiner
65
+ links itself, and the account holds every shared mesh's seed (the new
66
+ `@qvac/put-mesh-seed` route), so a new device opens them all from a blind
67
+ peer alone. A device invite minted by 0.6.6 still admits a device but no
68
+ longer links it into the member. `meshInvite` refuses a device invite from a
69
+ device whose proof chain is too long to extend, naming another device or the
70
+ phrase instead. (#342)
71
+ - `diagnostics` carries `backendUse`: per tool, the backends its recent runs
72
+ on this device reported, newest last. A run that asked for the gpu and ran
73
+ on the cpu is the fallback to look for. (#22)
74
+
75
+ ```js
76
+ const { stats } = await engine.diagnostics({})
77
+ stats.backendUse.generate_music.at(-1)
78
+ // { requestedGpu: true, device: 'cpu', backend: 'cpu' }
79
+ ```
80
+
81
+ - `duck`: `/doctor` checks this device (hardware, engine, model backends, mesh,
82
+ network) and `/doctor debug` shows the raw diagnostics frame behind it.
83
+ `ctrl+y` copies either as plain text, with the mesh seed and any invite
84
+ redacted and keys truncated. (#22)
85
+
86
+ ### Changed
87
+
88
+ - An invite is a password again, as it was before 0.5.0. A member invite mints
89
+ no grant and a device invite no expiring token: either link is the mesh
90
+ seed, reusable for good, and the joining device admits itself with the new
91
+ `@qvac/open-join` route; `joinMesh` and `recoverAccount` return once the
92
+ join is recorded, appending it in the background, with the account reporting
93
+ `joining` until the mesh reaches it. A device invite pairs with the inviting
94
+ device offline, and `recoverAccount` needs no other device online. A removed
95
+ device comes back the next time it joins. Every route that shipped keeps its
96
+ 0.6.6 meaning, and a 0.6.6 or 0.6.7 replica halts on the new one, so this
97
+ raises the floor to `>=0.19.1`. (#342)
98
+
99
+ ```js
100
+ const { invite } = await engine.meshInvite({ member: true })
101
+ await engine.joinMesh({ invite }) // any number of times, by anyone holding it
102
+ ```
103
+
104
+ - `joinMesh` and `recoverAccount` fail with "This device has no device
105
+ details: pass `device` when creating Core" on a Core created without
106
+ `device` and with none stored, and a `meshSeed` boot reports it as its
107
+ status error: a mesh admits only a device it can list and remove. (#342)
108
+
109
+ - A mesh still `joining` refuses a write into it at once with "This mesh is
110
+ still joining: it accepts writes once it is joined", since nothing written
111
+ there reaches disk until a peer serves the mesh. Reads keep working; retry
112
+ the write once the mesh lists `joined`. (#342)
113
+
114
+ - `listMeshes` and `meshesWatch` rows no longer set `reason` or
115
+ `awaitingFrom`: no other device admits a joining one any more. Read
116
+ `state: 'joining'` alone. (#342)
117
+
118
+ ### Deprecated
119
+
120
+ - `meshEntry.reason` and `meshEntry.awaitingFrom`, which are never set. (#342)
121
+
122
+ ### Removed
123
+
124
+ - The `grantWritableMs`, `grantVisibleMs` and `accountWritableMs` Core options.
125
+ Nothing on a join, a mint or a logout waits for a mesh to go writable any
126
+ more: a departure the mesh cannot take yet is revoked by the departures
127
+ outbox once it can, and a mesh minted before the account can write is
128
+ registered there once it can. (#342)
129
+
130
+ ### Fixed
131
+
132
+ - A chat's KV cache no longer grows by duplicated history: each run is replayed
133
+ to the model exactly as it was sent. (#338)
134
+ - A chat handed to another agent on the same device, by `setChatAgent` or a
135
+ `sendMessage` naming another `agentId`, no longer leaves the old agent's KV
136
+ cache file on disk until the chat is deleted. (#330)
137
+ - Reopening the account mesh after its local database died clears "the local
138
+ database died" error once the reopened mesh is healthy. (#340)
139
+ - `duck`: `/agent new` gives the new agent a real model in a mesh with no agent
140
+ yet, and `/agent use` hands the open chat to the agent it selects. (#348, #331)
141
+
142
+ ## [0.6.7] - 2026-10-06
143
+
144
+ ### Added
145
+
146
+ - A `quack` model for testing mesh, sync and chat flows without inference. An
147
+ agent on it replies with `quack` repeated 1 to 500 times, counted from a hash
148
+ of the last user turn, so the same prompt always gets the same reply. Every
149
+ other model still runs on the real harness. The device-local
150
+ `debug.fakeModels` key (`FAKE_MODELS_KEY` in `@qvac/core/constants`, beside the `QUACK_MODEL` name) turns it
151
+ on and off live, which is what an app's Debug toggle writes. `assistantChild`
152
+ and `assistantTree` take `fakeModels: true` to force it on, which is what duck
153
+ does with its `--fake-models` flag or `QVAC_FAKE_MODELS=1`.
154
+
155
+ ```js
156
+ await engine.configSet({ key: FAKE_MODELS_KEY, value: true })
157
+ ```
158
+
159
+ ```sh
160
+ QVAC_MODEL=quack duck --temp --fake-models
161
+ ```
162
+
163
+ - `Core` accepts `store`, `swarm` and `wakeup`, so a host that is already a
164
+ peer-to-peer application passes the ones it runs rather than having Core
165
+ stand up a second of each. Core never closes, destroys or suspends what it
166
+ was passed, takes the device identity from a passed swarm's keypair, and
167
+ leaves replication and wakeup on a passed swarm to the host. `swarm`
168
+ requires `store` and `wakeup`. The host suspends Core before its store, and
169
+ resumes its store before resuming or closing Core. `storagePath` is
170
+ unchanged and still holds the device-local database. See
171
+ [the decision record](docs/decisions/2026-09-28-a-host-passes-core-its-corestore-and-swarm.md).
172
+
173
+ ```js
174
+ const core = new Core(transport, {
175
+ storagePath,
176
+ store: host.rootCorestore.namespace('qvac'),
177
+ swarm: host.swarm,
178
+ wakeup: host.wakeup
179
+ })
180
+ ```
181
+
182
+ (#303)
183
+
184
+ ### Changed
185
+
186
+ - `@qvac/skills` is pinned at 0.1.11. 0.1.12 grants the `pptx`, `docx`, `xlsx`
187
+ and `transcribe_music` tools, which `@qvac/harness` does not register yet, so
188
+ a range would let a fresh install read presentations, word and excel as
189
+ unsupported. (#321)
190
+
191
+ ### Fixed
192
+
193
+ - The assistant reports readiness for an agent newly hosted in the account as
194
+ soon as it lands there, as it already did in a shared mesh, so a credential
195
+ handed off to an agent created from another device no longer waits on it.
196
+ (#320)
197
+
16
198
  ## [0.6.6] - 2026-10-05
17
199
 
18
200
  ### Changed
@@ -1234,7 +1416,9 @@ Published under the `release-0.1` dist-tag.
1234
1416
  await assistant.ready()
1235
1417
  ```
1236
1418
 
1237
- [Unreleased]: https://github.com/tetherto/qvac-core/compare/v0.6.6...HEAD
1419
+ [Unreleased]: https://github.com/tetherto/qvac-core/compare/v0.7.0...HEAD
1420
+ [0.7.0]: https://github.com/tetherto/qvac-core/compare/v0.6.7...v0.7.0
1421
+ [0.6.7]: https://github.com/tetherto/qvac-core/compare/v0.6.6...v0.6.7
1238
1422
  [0.6.6]: https://github.com/tetherto/qvac-core/compare/v0.6.5...v0.6.6
1239
1423
  [0.6.5]: https://github.com/tetherto/qvac-core/compare/v0.6.4...v0.6.5
1240
1424
  [0.6.4]: https://github.com/tetherto/qvac-core/compare/v0.6.3...v0.6.4
package/README.md CHANGED
@@ -279,6 +279,8 @@ replicated runs:
279
279
  - `generating-image` / `generating-audio` / `transcribing-audio` — the active run
280
280
  refined by the media it is producing.
281
281
  - `awaiting-approval` — the active run paused on the user.
282
+ - `awaiting-download` — the active run waits for a model the user has not let
283
+ download yet (`autoDownloadModels` is off).
282
284
 
283
285
  Every value is a mutually-exclusive state, not a parallel flag. Runs serialize
284
286
  per chat, so at most one is executing. The list carries only active chats — a
@@ -1,5 +1,7 @@
1
+ import type { AssistantAppConfig } from '../../spec/hyperschema/types.d.ts';
1
2
  import type { openLocalStore } from '../core/local/index.ts';
2
3
  import type { CapabilityHandlers } from '../../spec/capabilities.d.ts';
3
4
  type LocalStore = Awaited<ReturnType<typeof openLocalStore>>;
4
- export declare function createAppConfigApi(state: LocalStore): Pick<CapabilityHandlers, 'getConfig' | 'setConfig' | 'setModelPaused' | 'setModelReleased' | 'configWatchTyped'>;
5
+ export declare function writeAutoDownloadModels(state: LocalStore, enabled: boolean): Promise<AssistantAppConfig>;
6
+ export declare function createAppConfigApi(state: LocalStore): Pick<CapabilityHandlers, 'getConfig' | 'setConfig' | 'setModelPaused' | 'setModelReleased' | 'setAutoDownloadModels' | 'configWatchTyped'>;
5
7
  export {};
@@ -22,7 +22,8 @@ const DEFAULT_CONFIG = {
22
22
  onboardingCompleted: false,
23
23
  delegation: EMPTY_DELEGATION,
24
24
  pausedModels: [],
25
- releasedModels: []
25
+ releasedModels: [],
26
+ autoDownloadModels: true
26
27
  };
27
28
  const DelegationSchema = z.object({
28
29
  enabled: z.boolean().optional(),
@@ -71,7 +72,8 @@ function toConfig(row) {
71
72
  onboardingCompleted: row.onboardingCompleted ?? false,
72
73
  delegation: normalizeDelegation(row.delegation),
73
74
  pausedModels: row.pausedModels ?? [],
74
- releasedModels: row.releasedModels ?? []
75
+ releasedModels: row.releasedModels ?? [],
76
+ autoDownloadModels: !row.manualModelDownloads
75
77
  };
76
78
  }
77
79
  function applyWrite(current, update) {
@@ -84,7 +86,8 @@ function applyWrite(current, update) {
84
86
  onboardingCompleted: update.onboardingCompleted ?? false,
85
87
  delegation: normalizeDelegation(update.delegation),
86
88
  pausedModels: current.pausedModels,
87
- releasedModels: current.releasedModels
89
+ releasedModels: current.releasedModels,
90
+ autoDownloadModels: current.autoDownloadModels
88
91
  };
89
92
  }
90
93
  function toggled(names, name, on) {
@@ -110,9 +113,14 @@ function toRow(config) {
110
113
  onboardingCompleted: config.onboardingCompleted,
111
114
  delegation: config.delegation,
112
115
  pausedModels: config.pausedModels,
113
- releasedModels: config.releasedModels
116
+ releasedModels: config.releasedModels,
117
+ manualModelDownloads: !config.autoDownloadModels
114
118
  };
115
119
  }
120
+ export async function writeAutoDownloadModels(state, enabled) {
121
+ const row = await state.updateAppConfigRow((current) => toRow({ ...toConfig(current), autoDownloadModels: enabled }));
122
+ return toConfig(row);
123
+ }
116
124
  export function createAppConfigApi(state) {
117
125
  return {
118
126
  getConfig(_) {
@@ -142,6 +150,9 @@ export function createAppConfigApi(state) {
142
150
  });
143
151
  return toConfig(row);
144
152
  },
153
+ setAutoDownloadModels(input) {
154
+ return writeAutoDownloadModels(state, input.enabled ?? false);
155
+ },
145
156
  configWatchTyped: watchable(() => state, () => readConfig(state), { name: 'configWatchTyped' })
146
157
  };
147
158
  }
@@ -0,0 +1,22 @@
1
+ import type { openLocalStore } from '../core/local/index.ts';
2
+ import type { Harness } from './harness.ts';
3
+ export declare const MAX_QUACKS = 500;
4
+ type LocalStore = Awaited<ReturnType<typeof openLocalStore>>;
5
+ export type FakeModelsSource = Pick<LocalStore, 'get' | 'watch' | 'unwatch'>;
6
+ interface Logger {
7
+ warn(...args: unknown[]): void;
8
+ }
9
+ export interface FakeModelsOptions {
10
+ enabled?: () => boolean;
11
+ paceMs?: number;
12
+ }
13
+ export declare function quackCount(prompt: string): number;
14
+ export declare function readFakeModels(state: Pick<FakeModelsSource, 'get'>): Promise<boolean>;
15
+ export interface FakeModelsWatchOptions {
16
+ initial: boolean;
17
+ onChange: (enabled: boolean) => void;
18
+ logger?: Logger | null;
19
+ }
20
+ export declare function watchFakeModels(state: FakeModelsSource, opts: FakeModelsWatchOptions): () => Promise<void>;
21
+ export declare function withFakeModels(harness: Harness, opts?: FakeModelsOptions): Harness;
22
+ export {};
@@ -0,0 +1,112 @@
1
+ import b4a from 'b4a';
2
+ import crypto from 'hypercore-crypto';
3
+ import ScopeLock from 'scope-lock';
4
+ import { FAKE_MODELS_KEY, QUACK_MODEL } from '../constants.js';
5
+ import { errorMessage } from '../guards.js';
6
+ export const MAX_QUACKS = 500;
7
+ const QUACK_ROW = {
8
+ name: QUACK_MODEL,
9
+ type: 'llm',
10
+ taskType: 'llm',
11
+ registryPath: QUACK_MODEL,
12
+ registrySource: 'fake',
13
+ isCached: true,
14
+ isLoaded: true,
15
+ expectedSize: 0,
16
+ supportsImages: false
17
+ };
18
+ export function quackCount(prompt) {
19
+ return 1 + (b4a.readUInt32LE(crypto.hash(b4a.from(prompt)), 0) % MAX_QUACKS);
20
+ }
21
+ export async function readFakeModels(state) {
22
+ return (await state.get(FAKE_MODELS_KEY)) === true;
23
+ }
24
+ export function watchFakeModels(state, opts) {
25
+ const { onChange, logger } = opts;
26
+ const lock = new ScopeLock({ debounce: true });
27
+ let disposed = false;
28
+ let reported = opts.initial;
29
+ const schedule = async () => {
30
+ if (disposed || !(await lock.lock()))
31
+ return;
32
+ try {
33
+ const enabled = await readFakeModels(state);
34
+ if (!disposed && enabled !== reported) {
35
+ reported = enabled;
36
+ onChange(enabled);
37
+ }
38
+ }
39
+ catch (err) {
40
+ logger?.warn('fake-models', 'reading the switch failed', errorMessage(err));
41
+ }
42
+ finally {
43
+ lock.unlock();
44
+ }
45
+ };
46
+ const listener = () => void schedule();
47
+ state.watch(listener);
48
+ return async () => {
49
+ disposed = true;
50
+ state.unwatch(listener);
51
+ lock.destroy();
52
+ await lock.flush();
53
+ };
54
+ }
55
+ export function withFakeModels(harness, opts = {}) {
56
+ const { enabled = () => true, paceMs = 10 } = opts;
57
+ const isQuack = (model) => model === QUACK_MODEL && enabled();
58
+ const quacking = {
59
+ ...modelCalls(harness, isQuack),
60
+ completion: (context) => isQuack(context.model) ? quack(context, paceMs) : harness.completion(context)
61
+ };
62
+ return new Proxy(harness, {
63
+ get(target, key) {
64
+ if (Object.hasOwn(quacking, key)) {
65
+ return Reflect.get(quacking, key);
66
+ }
67
+ const value = Reflect.get(target, key);
68
+ return typeof value === 'function' ? value.bind(target) : value;
69
+ }
70
+ });
71
+ }
72
+ function modelCalls(harness, isQuack) {
73
+ const list = harness.list?.bind(harness);
74
+ const warmModel = harness.warmModel?.bind(harness);
75
+ const loadModel = harness.loadModel?.bind(harness);
76
+ const unloadModel = harness.unloadModel?.bind(harness);
77
+ const deleteModel = harness.deleteModel?.bind(harness);
78
+ return {
79
+ list: list &&
80
+ (async (options) => {
81
+ const rows = await list(options);
82
+ return isQuack(QUACK_MODEL) ? withQuackRow(rows, options?.type) : rows;
83
+ }),
84
+ warmModel: warmModel && ((context) => (isQuack(context.model) ? Promise.resolve() : warmModel(context))),
85
+ loadModel: loadModel &&
86
+ ((context) => isQuack(context.model) ? Promise.resolve({ loaded: true }) : loadModel(context)),
87
+ unloadModel: unloadModel &&
88
+ ((context) => isQuack(context.model) ? Promise.resolve({ unloaded: true }) : unloadModel(context)),
89
+ deleteModel: deleteModel &&
90
+ ((name) => isQuack(name) ? Promise.resolve({ name, removed: 0, freedBytes: 0 }) : deleteModel(name))
91
+ };
92
+ }
93
+ function withQuackRow(rows, type) {
94
+ return type && type !== 'llm' ? rows : [...rows, QUACK_ROW];
95
+ }
96
+ async function* quack(context, paceMs) {
97
+ const prompt = context.messages.findLast((turn) => turn.role === 'user')?.content ?? '';
98
+ const count = quackCount(prompt);
99
+ for (let i = 0; i < count; i++) {
100
+ if (i > 0) {
101
+ await pace(paceMs);
102
+ }
103
+ if (context.signal?.aborted) {
104
+ yield { type: 'aborted' };
105
+ return;
106
+ }
107
+ yield { type: 'content', text: i === 0 ? 'quack' : ' quack' };
108
+ }
109
+ }
110
+ function pace(ms) {
111
+ return new Promise((resolve) => setTimeout(resolve, ms));
112
+ }
@@ -49,6 +49,7 @@ export interface ModelInfo {
49
49
  expectedSize: number;
50
50
  actualSize?: number;
51
51
  taskType?: string;
52
+ memoryBytes?: number;
52
53
  }
53
54
  export interface ListModelsOptions {
54
55
  type?: ModelType;
@@ -220,6 +221,13 @@ export interface Harness {
220
221
  contextLength?: number;
221
222
  }>): void | Promise<void>;
222
223
  setModelIdleTimeout?(ms: number): void | Promise<void>;
224
+ setModelMemoryCap?(percent: number): void | Promise<void>;
225
+ memoryUsage?(): Promise<{
226
+ usedBytes: number;
227
+ }>;
228
+ setDownloadsAllowed?(request: {
229
+ allowed: boolean;
230
+ }): void | Promise<void>;
223
231
  previewModelSource?(input: PreviewModelSourceContext): Promise<ModelSourcePreview>;
224
232
  on?(event: 'model' | 'status', listener: (...args: never[]) => void): unknown;
225
233
  off?(event: 'model' | 'status', listener: (...args: never[]) => void): unknown;
@@ -271,4 +279,6 @@ export declare function harnessSpeakStream(harness: Pick<Harness, 'speakStream'>
271
279
  export declare function harnessDeathReason(status: unknown): string;
272
280
  export declare function harnessDied(status: unknown): boolean;
273
281
  export declare function modelCatalogChanged(state: unknown): boolean;
282
+ export declare function heldLoadModel(state: unknown): string | null;
283
+ export declare function droppedHeldLoadModel(state: unknown): string | null;
274
284
  export declare function imageDownloadProgress(state: unknown): ImageDownloadObservation | null;
@@ -164,6 +164,32 @@ export function harnessDied(status) {
164
164
  export function modelCatalogChanged(state) {
165
165
  return isRecord(state) && state.catalogChanged === true;
166
166
  }
167
+ export function heldLoadModel(state) {
168
+ if (typeof state !== 'object' ||
169
+ state === null ||
170
+ !('status' in state) ||
171
+ state.status !== 'held' ||
172
+ !('model' in state) ||
173
+ typeof state.model !== 'string') {
174
+ return null;
175
+ }
176
+ return state.model;
177
+ }
178
+ export function droppedHeldLoadModel(state) {
179
+ if (typeof state !== 'object' ||
180
+ state === null ||
181
+ !('status' in state) ||
182
+ state.status !== 'error' ||
183
+ !('message' in state) ||
184
+ state.message !== 'aborted' ||
185
+ !('addon' in state) ||
186
+ typeof state.addon !== 'string' ||
187
+ !('model' in state) ||
188
+ typeof state.model !== 'string') {
189
+ return null;
190
+ }
191
+ return state.model;
192
+ }
167
193
  export function imageDownloadProgress(state) {
168
194
  if (typeof state !== 'object' ||
169
195
  state === null ||
@@ -34,7 +34,11 @@ export interface SenderDevice {
34
34
  }
35
35
  export declare function senderNamesFrom(chunks: ReadonlyArray<AssistantChunk>, members: ReadonlyArray<SenderMember>, devices: ReadonlyArray<SenderDevice>): ReadonlyMap<string, string>;
36
36
  export declare function senderAttribution(names: ReadonlyMap<string, string>): string;
37
- export declare function historyFromChunks(chunks: ReadonlyArray<AssistantChunk>, stale?: Partial<StaleToolRuns>, senderNames?: ReadonlyMap<string, string>): ChatMessage[];
37
+ export interface HistoryReplay {
38
+ readonly senderNames?: ReadonlyMap<string, string>;
39
+ readonly sentRuns?: ReadonlyMap<string, ReadonlyArray<ChatMessage>>;
40
+ }
41
+ export declare function historyFromChunks(chunks: ReadonlyArray<AssistantChunk>, stale?: Partial<StaleToolRuns>, { senderNames, sentRuns }?: HistoryReplay): ChatMessage[];
38
42
  export declare function inputAvailableToRun(chunk: AssistantChunk, requestSeq: number): boolean;
39
43
  export declare function normalizeQuestion(text: string): string;
40
44
  export declare const GENERIC_ACKNOWLEDGEMENTS: Set<string>;
@@ -76,7 +76,7 @@ export function senderAttribution(names) {
76
76
  const distinct = [...new Set(names.values())].sort();
77
77
  return distinct.length > 1 ? distinct.join('\u0000') : '';
78
78
  }
79
- export function historyFromChunks(chunks, stale = {}, senderNames = new Map()) {
79
+ export function historyFromChunks(chunks, stale = {}, { senderNames = new Map(), sentRuns = new Map() } = {}) {
80
80
  const attemptStarts = latestAttemptStarts(chunks);
81
81
  const shared = new Set(senderNames.values()).size > 1;
82
82
  const staleRunIds = stale.runIds ?? new Set();
@@ -100,11 +100,20 @@ export function historyFromChunks(chunks, stale = {}, senderNames = new Map()) {
100
100
  history.push({ role: 'user', content: compactionTurn(folded.summary) });
101
101
  }
102
102
  const byRun = new Map();
103
+ const kept = new Set();
103
104
  const anchor = (runId) => {
104
105
  let turn = byRun.get(runId);
105
106
  if (!turn) {
106
107
  turn = { role: 'assistant', content: '' };
107
108
  byRun.set(runId, turn);
109
+ const sent = runId && !staleRunIds.has(runId) ? sentRuns.get(runId) : undefined;
110
+ if (sent) {
111
+ const replayed = sent.map((message) => ({ ...message }));
112
+ history.push(...replayed);
113
+ for (const message of [...replayed, turn]) {
114
+ kept.add(message);
115
+ }
116
+ }
108
117
  history.push(turn);
109
118
  }
110
119
  return turn;
@@ -148,6 +157,9 @@ export function historyFromChunks(chunks, stale = {}, senderNames = new Map()) {
148
157
  anchor(chunk.runId);
149
158
  continue;
150
159
  }
160
+ if (chunk.toolResult && chunk.runId && sentRuns.has(chunk.runId)) {
161
+ continue;
162
+ }
151
163
  if (chunk.toolResult?.name === SKILL_TOOL) {
152
164
  if (chunk.runId && staleRunIds.has(chunk.runId)) {
153
165
  continue;
@@ -188,7 +200,7 @@ export function historyFromChunks(chunks, stale = {}, senderNames = new Map()) {
188
200
  }
189
201
  anchor(chunk.runId).content += chunk.content.text;
190
202
  }
191
- return history.filter((turn) => turn.role === 'user' || turn.content.length > 0);
203
+ return history.filter((turn) => turn.role === 'user' || turn.content.length > 0 || kept.has(turn));
192
204
  }
193
205
  export function inputAvailableToRun(chunk, requestSeq) {
194
206
  const userInput = Boolean(chunk.userMessage || (chunk.attachment && !chunk.runId));
@@ -22,6 +22,7 @@ export interface AssistantTreeOptions {
22
22
  logger?: Logger | null;
23
23
  imageCacheDir?: string;
24
24
  inferenceCallsDir?: string;
25
+ runReplayDir?: string;
25
26
  imageCacheMaxBytes?: number;
26
27
  imageCacheMaxAgeMs?: number;
27
28
  embedder?: EmbedderFactory;
@@ -29,6 +30,7 @@ export interface AssistantTreeOptions {
29
30
  onHarness?: OnHarness;
30
31
  platform?: string;
31
32
  preloadCurrentAgentModel?: boolean;
33
+ fakeModels?: boolean;
32
34
  }
33
35
  export declare function assistantTree(opts: AssistantTreeOptions): Supervisor;
34
36
  export interface AssistantChildOptions {
@@ -37,12 +39,14 @@ export interface AssistantChildOptions {
37
39
  logger?: Logger | null;
38
40
  imageCacheDir?: string;
39
41
  inferenceCallsDir?: string;
42
+ runReplayDir?: string;
40
43
  imageCacheMaxBytes?: number;
41
44
  imageCacheMaxAgeMs?: number;
42
45
  embedder?: EmbedderFactory;
43
46
  extractors?: RagExtractors;
44
47
  onHarness?: OnHarness;
45
48
  preloadCurrentAgentModel?: boolean;
49
+ fakeModels?: boolean;
46
50
  restart?: 'always' | 'never';
47
51
  maxRestarts?: number;
48
52
  window?: number;