@mycadet/assistant 0.12.11 → 0.13.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 (176) hide show
  1. package/node_modules/@mycadet/ces-client/node_modules/@mycadet/service-contracts/src/__tests__/ingress.test.ts +2 -2
  2. package/node_modules/@mycadet/ces-client/node_modules/@mycadet/service-contracts/src/ingress.ts +2 -2
  3. package/node_modules/@mycadet/gateway-client/node_modules/@mycadet/service-contracts/src/__tests__/ingress.test.ts +2 -2
  4. package/node_modules/@mycadet/gateway-client/node_modules/@mycadet/service-contracts/src/ingress.ts +2 -2
  5. package/node_modules/@mycadet/gateway-client/src/gateway-ipc-contracts.ts +0 -20
  6. package/node_modules/@mycadet/service-contracts/src/__tests__/ingress.test.ts +2 -2
  7. package/node_modules/@mycadet/service-contracts/src/ingress.ts +2 -2
  8. package/openapi.yaml +2 -708
  9. package/package.json +2 -3
  10. package/scripts/sync-web-search-catalog.ts +1 -2
  11. package/src/__tests__/cloud-profile-guard.test.ts +2 -2
  12. package/src/__tests__/config-schema.test.ts +6 -38
  13. package/src/__tests__/conversation-queue.test.ts +45 -6
  14. package/src/__tests__/db-readiness-http-gate.test.ts +0 -6
  15. package/src/__tests__/persist-onboarding-artifacts.test.ts +0 -1
  16. package/src/__tests__/persona-resolver.test.ts +0 -51
  17. package/src/__tests__/provider-cloud-proxy-integration.test.ts +2 -110
  18. package/src/__tests__/run-due-schedules.test.ts +0 -4
  19. package/src/__tests__/schedule-store.test.ts +0 -12
  20. package/src/__tests__/stt-stream-session.test.ts +4 -4
  21. package/src/__tests__/telephony-tts-guard.test.ts +2 -50
  22. package/src/__tests__/voice-config-update.test.ts +12 -167
  23. package/src/__tests__/web-search-catalog-parity.test.ts +0 -2
  24. package/src/__tests__/workspace-migration-130-speech-mode-to-provider.test.ts +9 -7
  25. package/src/__tests__/workspace-migration-132-web-search-mode-to-provider.test.ts +6 -6
  26. package/src/calls/__tests__/voice-session-bridge.test.ts +1 -4
  27. package/src/calls/resolve-call-tts-provider.ts +6 -25
  28. package/src/calls/telephony-credential-preflight.ts +0 -10
  29. package/src/calls/telephony-tts-capability.ts +1 -17
  30. package/src/cli/commands/tts.ts +1 -2
  31. package/src/cli/index.help.ts +0 -2
  32. package/src/cli/program.ts +0 -2
  33. package/src/config/bundled-skills/settings/tools/voice-config-update.ts +4 -42
  34. package/src/config/schemas/__tests__/stt.test.ts +10 -14
  35. package/src/config/schemas/services.ts +5 -6
  36. package/src/config/schemas/stt.ts +1 -3
  37. package/src/config/schemas/tts.ts +1 -30
  38. package/src/daemon/daemon-readiness.ts +4 -29
  39. package/src/daemon/lifecycle.ts +0 -31
  40. package/src/daemon/providers-setup.ts +0 -8
  41. package/src/heartbeat/__tests__/heartbeat-service.test.ts +0 -1
  42. package/src/heartbeat/heartbeat-service.ts +1 -66
  43. package/src/ipc/__tests__/db-readiness-ipc-gate.test.ts +1 -9
  44. package/src/live-voice/__tests__/live-voice-credential-preflight.test.ts +0 -44
  45. package/src/live-voice/__tests__/live-voice-flux-turn-end.test.ts +4 -6
  46. package/src/live-voice/__tests__/live-voice-stt.test.ts +1 -1
  47. package/src/live-voice/live-voice-credential-preflight.ts +9 -33
  48. package/src/live-voice/live-voice-session.ts +10 -6
  49. package/src/live-voice/live-voice-tts.ts +1 -8
  50. package/src/plugins/defaults/memory/worker.ts +3 -3
  51. package/src/prompts/persona-resolver.ts +0 -35
  52. package/src/providers/__tests__/search-provider-catalog.test.ts +5 -8
  53. package/src/providers/__tests__/voice-error-copy.test.ts +9 -10
  54. package/src/providers/search-provider-catalog.ts +7 -39
  55. package/src/providers/speech-to-text/__tests__/provider-catalog.test.ts +3 -47
  56. package/src/providers/speech-to-text/__tests__/resolve.test.ts +6 -372
  57. package/src/providers/speech-to-text/provider-catalog.ts +2 -73
  58. package/src/providers/speech-to-text/resolve.ts +3 -120
  59. package/src/runtime/http-router-types.ts +1 -3
  60. package/src/runtime/http-server.ts +1 -1
  61. package/src/runtime/routes/__tests__/tts-routes.test.ts +2 -6
  62. package/src/runtime/routes/inbound-stages/transcribe-audio.test.ts +7 -7
  63. package/src/runtime/routes/index.ts +0 -4
  64. package/src/runtime/routes/live-voice-routes.test.ts +4 -11
  65. package/src/runtime/routes/live-voice-routes.ts +8 -11
  66. package/src/runtime/routes/secret-routes.ts +0 -6
  67. package/src/runtime/routes/tts-routes.ts +0 -38
  68. package/src/schedule/__tests__/plugin-schedule-reconciler.test.ts +1 -7
  69. package/src/schedule/__tests__/worker-mcp-readiness.test.ts +0 -4
  70. package/src/schedule/schedule-store.ts +0 -7
  71. package/src/schedule/scheduler.ts +0 -5
  72. package/src/schedule/worker.ts +1 -1
  73. package/src/stt/__tests__/daemon-batch-transcriber.test.ts +2 -10
  74. package/src/stt/daemon-batch-transcriber.ts +4 -38
  75. package/src/stt/types.ts +1 -9
  76. package/src/tools/network/__tests__/web-search.test.ts +3 -250
  77. package/src/tools/network/web-search.ts +4 -165
  78. package/src/tts/__tests__/provider-resolution.test.ts +0 -5
  79. package/src/tts/provider-catalog.ts +0 -4
  80. package/src/tts/types.ts +0 -1
  81. package/src/__tests__/cloud-speech-defaults.test.ts +0 -330
  82. package/src/__tests__/migration-cross-version-compatibility.test.ts +0 -1708
  83. package/src/__tests__/migration-export-http.test.ts +0 -557
  84. package/src/__tests__/migration-export-streaming.test.ts +0 -350
  85. package/src/__tests__/migration-export-to-gcs.test.ts +0 -706
  86. package/src/__tests__/migration-import-commit-http.test.ts +0 -1176
  87. package/src/__tests__/migration-import-from-gcs.test.ts +0 -534
  88. package/src/__tests__/migration-import-from-path.test.ts +0 -324
  89. package/src/__tests__/migration-import-from-url.test.ts +0 -588
  90. package/src/__tests__/migration-import-preflight-http.test.ts +0 -739
  91. package/src/__tests__/migration-jobs-status.test.ts +0 -139
  92. package/src/__tests__/migration-parity-persistence.test.ts +0 -1891
  93. package/src/__tests__/migration-transport.test.ts +0 -1089
  94. package/src/__tests__/migration-validate-http.test.ts +0 -634
  95. package/src/__tests__/migration-wizard.test.ts +0 -1395
  96. package/src/__tests__/rebind-secrets-screen.test.ts +0 -876
  97. package/src/__tests__/sanitize-config-for-transfer.test.ts +0 -259
  98. package/src/__tests__/transfer-progress-screen.test.ts +0 -1217
  99. package/src/__tests__/validation-results-screen.test.ts +0 -1130
  100. package/src/__tests__/vbundle-pax-and-symlink.test.ts +0 -147
  101. package/src/background-wake/background-wake-routes.test.ts +0 -906
  102. package/src/background-wake/cloud-client.test.ts +0 -328
  103. package/src/background-wake/cloud-client.ts +0 -196
  104. package/src/background-wake/next-wake.test.ts +0 -335
  105. package/src/background-wake/next-wake.ts +0 -188
  106. package/src/background-wake/publisher.ts +0 -93
  107. package/src/background-wake/wake-intent-hooks.test.ts +0 -234
  108. package/src/cli/commands/__tests__/backup.test.ts +0 -363
  109. package/src/cli/commands/backup.help.ts +0 -180
  110. package/src/cli/commands/backup.ts +0 -398
  111. package/src/cloud/cloud-speech.test.ts +0 -335
  112. package/src/cloud/cloud-speech.ts +0 -300
  113. package/src/config/cloud-speech-defaults.ts +0 -300
  114. package/src/config/sanitize-for-transfer.ts +0 -111
  115. package/src/live-voice/__tests__/live-voice-cloud-speech-fallback.test.ts +0 -250
  116. package/src/providers/speech-to-text/__tests__/cadet-cloud-realtime.test.ts +0 -457
  117. package/src/providers/speech-to-text/__tests__/cadet-cloud.test.ts +0 -271
  118. package/src/providers/speech-to-text/__tests__/cadet-speech-relay-connection.test.ts +0 -126
  119. package/src/providers/speech-to-text/cadet-cloud-flux-realtime.ts +0 -309
  120. package/src/providers/speech-to-text/cadet-cloud-realtime.ts +0 -315
  121. package/src/providers/speech-to-text/cadet-cloud.ts +0 -108
  122. package/src/providers/speech-to-text/cadet-speech-relay-connection.ts +0 -176
  123. package/src/runtime/migrations/__tests__/gcs-signed-url.test.ts +0 -254
  124. package/src/runtime/migrations/__tests__/job-registry.test.ts +0 -346
  125. package/src/runtime/migrations/__tests__/origin-mode.test.ts +0 -62
  126. package/src/runtime/migrations/__tests__/rebind-secrets-credentials.test.ts +0 -184
  127. package/src/runtime/migrations/__tests__/staged-import-path.test.ts +0 -110
  128. package/src/runtime/migrations/__tests__/v1-test-helpers.ts +0 -112
  129. package/src/runtime/migrations/__tests__/vbundle-builder-credentials.test.ts +0 -285
  130. package/src/runtime/migrations/__tests__/vbundle-builder-fd-leak.test.ts +0 -126
  131. package/src/runtime/migrations/__tests__/vbundle-builder-v1-shape.test.ts +0 -253
  132. package/src/runtime/migrations/__tests__/vbundle-import-credentials.test.ts +0 -208
  133. package/src/runtime/migrations/__tests__/vbundle-import-parity.test.ts +0 -485
  134. package/src/runtime/migrations/__tests__/vbundle-import-policy.test.ts +0 -349
  135. package/src/runtime/migrations/__tests__/vbundle-import-version-compat.test.ts +0 -196
  136. package/src/runtime/migrations/__tests__/vbundle-legacy-user-md.test.ts +0 -551
  137. package/src/runtime/migrations/__tests__/vbundle-metadata-merge-integration.test.ts +0 -428
  138. package/src/runtime/migrations/__tests__/vbundle-metadata-merge.test.ts +0 -220
  139. package/src/runtime/migrations/__tests__/vbundle-streaming-importer.test.ts +0 -1823
  140. package/src/runtime/migrations/__tests__/vbundle-streaming-validator.test.ts +0 -598
  141. package/src/runtime/migrations/__tests__/vbundle-symlink-importer.test.ts +0 -463
  142. package/src/runtime/migrations/__tests__/vbundle-symlink-streaming-importer.test.ts +0 -538
  143. package/src/runtime/migrations/__tests__/vbundle-symlink-streaming.test.ts +0 -515
  144. package/src/runtime/migrations/__tests__/vbundle-symlink-tar.test.ts +0 -453
  145. package/src/runtime/migrations/__tests__/vbundle-symlink-walker.test.ts +0 -319
  146. package/src/runtime/migrations/__tests__/vbundle-tar-stream.test.ts +0 -240
  147. package/src/runtime/migrations/__tests__/vbundle-validator-v1-schema.test.ts +0 -473
  148. package/src/runtime/migrations/gcs-signed-url.ts +0 -190
  149. package/src/runtime/migrations/job-registry.ts +0 -297
  150. package/src/runtime/migrations/migration-transport.ts +0 -612
  151. package/src/runtime/migrations/migration-wizard.ts +0 -747
  152. package/src/runtime/migrations/origin-mode.ts +0 -42
  153. package/src/runtime/migrations/rebind-secrets-screen.ts +0 -412
  154. package/src/runtime/migrations/staged-import-path.ts +0 -116
  155. package/src/runtime/migrations/transfer-progress-screen.ts +0 -323
  156. package/src/runtime/migrations/validation-results-screen.ts +0 -469
  157. package/src/runtime/migrations/vbundle-builder.ts +0 -1382
  158. package/src/runtime/migrations/vbundle-import-analyzer.ts +0 -405
  159. package/src/runtime/migrations/vbundle-import-policy.ts +0 -241
  160. package/src/runtime/migrations/vbundle-importer.ts +0 -826
  161. package/src/runtime/migrations/vbundle-metadata-merge.ts +0 -113
  162. package/src/runtime/migrations/vbundle-streaming-importer.ts +0 -2844
  163. package/src/runtime/migrations/vbundle-streaming-validator.ts +0 -426
  164. package/src/runtime/migrations/vbundle-tar-stream.ts +0 -244
  165. package/src/runtime/migrations/vbundle-validator.ts +0 -724
  166. package/src/runtime/routes/__tests__/migration-cadet-metadata-reconcile.test.ts +0 -227
  167. package/src/runtime/routes/__tests__/migration-export-secrets-redacted.test.ts +0 -54
  168. package/src/runtime/routes/__tests__/migration-import-allowed-hosts-env.test.ts +0 -70
  169. package/src/runtime/routes/__tests__/migration-import-credential-filter.test.ts +0 -257
  170. package/src/runtime/routes/background-wake-routes.ts +0 -387
  171. package/src/runtime/routes/migration-routes.ts +0 -2584
  172. package/src/tools/network/__tests__/cloud-search-proxy.test.ts +0 -282
  173. package/src/tools/network/cloud-search-proxy.ts +0 -211
  174. package/src/tts/__tests__/cadet-provider.test.ts +0 -253
  175. package/src/tts/providers/cadet-provider.ts +0 -244
  176. package/src/types/tar-stream.d.ts +0 -66
@@ -1,453 +0,0 @@
1
- /**
2
- * In-memory tar emit / parse / validate coverage for the typeflag-2 symlink
3
- * entry shape introduced in the vbundle-symlinks plan (PR 2).
4
- *
5
- * These tests exercise the round-trip through `buildVBundle` →
6
- * `validateVBundle` plus three negative paths: traversal rejection, a
7
- * tampered `link_target` field, and a tampered `sha256` digest.
8
- */
9
-
10
- import { createHash, randomUUID } from "node:crypto";
11
- import { gunzipSync, gzipSync } from "node:zlib";
12
- import { describe, expect, test } from "bun:test";
13
-
14
- import { buildVBundle } from "../vbundle-builder.js";
15
- import {
16
- canonicalizeJson,
17
- computeManifestChecksum,
18
- validateVBundle,
19
- } from "../vbundle-validator.js";
20
- import { defaultV1Options } from "./v1-test-helpers.js";
21
-
22
- function sha256Hex(data: Uint8Array | string): string {
23
- return createHash("sha256").update(data).digest("hex");
24
- }
25
-
26
- /**
27
- * Decode the manifest from a built archive, run the supplied mutator, recompute
28
- * the manifest checksum, and rebuild the archive. Mirrors the
29
- * `dropFromManifestAndRepack` pattern from `vbundle-streaming-importer.test.ts`.
30
- *
31
- * Assumes the manifest is the first tar entry and has no PAX prefix.
32
- */
33
- function mutateManifestAndRepack(
34
- archive: Uint8Array,
35
- mutate: (
36
- contents: Array<{
37
- path: string;
38
- sha256: string;
39
- size_bytes: number;
40
- link_target?: string;
41
- }>,
42
- ) => void,
43
- ): Uint8Array {
44
- const raw = gunzipSync(archive);
45
- const sizeStr = new TextDecoder()
46
- .decode(raw.subarray(124, 136))
47
- .replace(/\0.*$/, "")
48
- .trim();
49
- const origSize = parseInt(sizeStr, 8);
50
- const manifestJson = new TextDecoder().decode(
51
- raw.subarray(512, 512 + origSize),
52
- );
53
- const manifest = JSON.parse(manifestJson) as {
54
- contents: Array<{
55
- path: string;
56
- sha256: string;
57
- size_bytes: number;
58
- link_target?: string;
59
- }>;
60
- checksum: string;
61
- [k: string]: unknown;
62
- };
63
-
64
- mutate(manifest.contents);
65
-
66
- // Recompute the v1 checksum: empty-string placeholder, then canonicalize.
67
- const withEmptyChecksum: Record<string, unknown> = {
68
- ...manifest,
69
- checksum: "",
70
- };
71
- manifest.checksum = sha256Hex(canonicalizeJson(withEmptyChecksum));
72
-
73
- const newJson = JSON.stringify(manifest);
74
- const newBytes = new TextEncoder().encode(newJson);
75
-
76
- const header = new Uint8Array(512);
77
- header.set(raw.subarray(0, 512), 0);
78
- const newSizeOctal = newBytes.length.toString(8).padStart(11, "0");
79
- for (let i = 0; i < 11; i++) {
80
- header[124 + i] = newSizeOctal.charCodeAt(i);
81
- }
82
- header[135] = 0;
83
- for (let i = 148; i < 156; i++) {
84
- header[i] = 0x20;
85
- }
86
- let sum = 0;
87
- for (let i = 0; i < 512; i++) {
88
- sum += header[i];
89
- }
90
- const cksum = sum.toString(8).padStart(6, "0");
91
- for (let i = 0; i < 6; i++) {
92
- header[148 + i] = cksum.charCodeAt(i);
93
- }
94
- header[154] = 0;
95
- header[155] = 0x20;
96
-
97
- const oldPaddedLen = 512 + Math.ceil(origSize / 512) * 512;
98
- const newPadded = Math.ceil(newBytes.length / 512) * 512;
99
- const out = new Uint8Array(
100
- header.length + newPadded + (raw.length - oldPaddedLen),
101
- );
102
- out.set(header, 0);
103
- out.set(newBytes, 512);
104
- out.set(raw.subarray(oldPaddedLen), 512 + newPadded);
105
- return gzipSync(out);
106
- }
107
-
108
- describe("vbundle symlink tar — emit / parse / validate", () => {
109
- test("round-trip: regular files and a typeflag-2 symlink validate cleanly", () => {
110
- const files = [
111
- {
112
- path: "workspace/skills/bar.md",
113
- data: new TextEncoder().encode("hello"),
114
- },
115
- {
116
- path: "workspace/skills/foo.md",
117
- data: new Uint8Array(0),
118
- linkTarget: "bar.md",
119
- },
120
- {
121
- path: "workspace/data/db/assistant.db",
122
- data: new TextEncoder().encode("db-bytes"),
123
- },
124
- ];
125
-
126
- const { archive, manifest } = buildVBundle({
127
- files,
128
- ...defaultV1Options(),
129
- });
130
- const result = validateVBundle(archive);
131
-
132
- expect(result.is_valid).toBe(true);
133
- expect(result.errors).toEqual([]);
134
-
135
- const symlinkEntry = result.entries!.get("workspace/skills/foo.md");
136
- expect(symlinkEntry).toBeDefined();
137
- expect(symlinkEntry!.linkname).toBe("bar.md");
138
- expect(symlinkEntry!.size).toBe(0);
139
-
140
- const symlinkContent = manifest.contents.find(
141
- (c) => c.path === "workspace/skills/foo.md",
142
- )!;
143
- expect(symlinkContent.link_target).toBe("bar.md");
144
- expect(symlinkContent.size_bytes).toBe(0);
145
- expect(symlinkContent.sha256).toBe(sha256Hex("bar.md"));
146
-
147
- // Regular file entries should NOT carry a linkname.
148
- const regularEntry = result.entries!.get("workspace/skills/bar.md");
149
- expect(regularEntry!.linkname).toBeUndefined();
150
- });
151
-
152
- test("symlink target that escapes the archive root is rejected", () => {
153
- const files = [
154
- {
155
- path: "workspace/skills/foo.md",
156
- data: new Uint8Array(0),
157
- linkTarget: "../../../etc/passwd",
158
- },
159
- {
160
- path: "workspace/data/db/assistant.db",
161
- data: new TextEncoder().encode("db-bytes"),
162
- },
163
- ];
164
-
165
- const { archive } = buildVBundle({ files, ...defaultV1Options() });
166
- const result = validateVBundle(archive);
167
-
168
- expect(result.is_valid).toBe(false);
169
- const traversal = result.errors.find(
170
- (e) => e.code === "SYMLINK_TARGET_ESCAPES_ARCHIVE",
171
- );
172
- expect(traversal).toBeDefined();
173
- expect(traversal!.path).toBe("workspace/skills/foo.md");
174
- });
175
-
176
- test("absolute symlink target is rejected as escaping the archive root", () => {
177
- // An absolute target like "/etc/passwd" is unconstrained by the bundle
178
- // root: the prior `posix.normalize(posix.join(dirname, target))` guard
179
- // returns "/etc/passwd" unchanged, which does not start with "../" and
180
- // would otherwise pass. The validator must catch absolute targets up
181
- // front and surface them with the same SYMLINK_TARGET_ESCAPES_ARCHIVE
182
- // code as `..`-based escapes.
183
- const files = [
184
- {
185
- path: "workspace/skills/foo.md",
186
- data: new Uint8Array(0),
187
- linkTarget: "/etc/passwd",
188
- },
189
- {
190
- path: "workspace/data/db/assistant.db",
191
- data: new TextEncoder().encode("db-bytes"),
192
- },
193
- ];
194
-
195
- const { archive } = buildVBundle({ files, ...defaultV1Options() });
196
- const result = validateVBundle(archive);
197
-
198
- expect(result.is_valid).toBe(false);
199
- const traversal = result.errors.find(
200
- (e) => e.code === "SYMLINK_TARGET_ESCAPES_ARCHIVE",
201
- );
202
- expect(traversal).toBeDefined();
203
- expect(traversal!.path).toBe("workspace/skills/foo.md");
204
- });
205
-
206
- test("manifest link_target tampered to a different value surfaces LINK_TARGET_MISMATCH", () => {
207
- const files = [
208
- {
209
- path: "workspace/skills/bar.md",
210
- data: new TextEncoder().encode("hello"),
211
- },
212
- {
213
- path: "workspace/skills/foo.md",
214
- data: new Uint8Array(0),
215
- linkTarget: "bar.md",
216
- },
217
- {
218
- path: "workspace/data/db/assistant.db",
219
- data: new TextEncoder().encode("db-bytes"),
220
- },
221
- ];
222
-
223
- const { archive } = buildVBundle({ files, ...defaultV1Options() });
224
-
225
- // Mutate the manifest entry so it points at a different target than the
226
- // one carried in the tar header. Recompute sha256 over the new target so
227
- // the checksum check passes and we exercise the linkname-mismatch branch.
228
- const tampered = mutateManifestAndRepack(archive, (contents) => {
229
- const entry = contents.find((c) => c.path === "workspace/skills/foo.md")!;
230
- const newTarget = "different.md";
231
- entry.link_target = newTarget;
232
- entry.sha256 = sha256Hex(newTarget);
233
- });
234
-
235
- const result = validateVBundle(tampered);
236
- expect(result.is_valid).toBe(false);
237
- const mismatch = result.errors.find(
238
- (e) => e.code === "LINK_TARGET_MISMATCH",
239
- );
240
- expect(mismatch).toBeDefined();
241
- expect(mismatch!.path).toBe("workspace/skills/foo.md");
242
- });
243
-
244
- test("typeflag-2 entry whose tar header declares a non-zero size surfaces FILE_SIZE_MISMATCH", () => {
245
- // Hand-craft a bundle so we can violate an invariant the buildVBundle
246
- // emit path enforces: a symlink header that lies about its size.
247
- //
248
- // The validator's downstream check `archiveEntry.size !== 0` only fires
249
- // if `parseTar` propagates the tar-declared size for typeflag-2 entries
250
- // — not if it forces 0. This regression test pins that contract.
251
- //
252
- // Layout:
253
- // - manifest.json header + body
254
- // - foo.md symlink header (typeflag '2', linkname=bar.md, size=0o123)
255
- // - end-of-archive (two zero blocks)
256
-
257
- const BLOCK = 512;
258
- const enc = new TextEncoder();
259
-
260
- function buildHeader(opts: {
261
- name: string;
262
- size: number;
263
- typeflag: string;
264
- linkname?: string;
265
- }): Uint8Array {
266
- const h = new Uint8Array(BLOCK);
267
- const nameBytes = enc.encode(opts.name);
268
- h.set(nameBytes.subarray(0, 100), 0);
269
- const writeOctal = (off: number, len: number, value: number) => {
270
- const s = value.toString(8).padStart(len - 1, "0");
271
- for (let i = 0; i < s.length; i++) {
272
- h[off + i] = s.charCodeAt(i);
273
- }
274
- h[off + len - 1] = 0;
275
- };
276
- writeOctal(100, 8, 0o644);
277
- writeOctal(108, 8, 0);
278
- writeOctal(116, 8, 0);
279
- writeOctal(124, 12, opts.size);
280
- writeOctal(136, 12, Math.floor(Date.now() / 1000));
281
- h[156] = opts.typeflag.charCodeAt(0);
282
- if (opts.linkname) {
283
- const lk = enc.encode(opts.linkname);
284
- h.set(lk.subarray(0, 100), 157);
285
- }
286
- const magic = enc.encode("ustar\0");
287
- h.set(magic, 257);
288
- h[263] = "0".charCodeAt(0);
289
- h[264] = "0".charCodeAt(0);
290
- // Checksum field starts as eight spaces per spec
291
- for (let i = 148; i < 156; i++) {
292
- h[i] = 0x20;
293
- }
294
- let sum = 0;
295
- for (let i = 0; i < BLOCK; i++) {
296
- sum += h[i];
297
- }
298
- const cksum = sum.toString(8).padStart(6, "0");
299
- for (let i = 0; i < 6; i++) {
300
- h[148 + i] = cksum.charCodeAt(i);
301
- }
302
- h[154] = 0;
303
- h[155] = 0x20;
304
- return h;
305
- }
306
-
307
- function padBlock(data: Uint8Array): Uint8Array {
308
- const rem = data.length % BLOCK;
309
- if (rem === 0) {
310
- return data;
311
- }
312
- const padded = new Uint8Array(data.length + (BLOCK - rem));
313
- padded.set(data);
314
- return padded;
315
- }
316
-
317
- // Build manifest declaring foo.md as a symlink with size_bytes 0,
318
- // sha256 over the link target string. That matches what buildVBundle
319
- // would emit; the discrepancy lives in the tar header alone.
320
- const linkTarget = "bar.md";
321
- const symlinkSha = sha256Hex(linkTarget);
322
- const manifest = {
323
- schema_version: 1,
324
- bundle_id: randomUUID(),
325
- created_at: new Date().toISOString(),
326
- assistant: { id: "self", name: "Test", runtime_version: "0.0.0-test" },
327
- origin: { mode: "self-hosted-local" },
328
- compatibility: {
329
- min_runtime_version: "0.0.0-test",
330
- max_runtime_version: null,
331
- },
332
- contents: [
333
- {
334
- path: "workspace/skills/foo.md",
335
- sha256: symlinkSha,
336
- size_bytes: 0,
337
- link_target: linkTarget,
338
- },
339
- {
340
- path: "workspace/data/db/assistant.db",
341
- sha256: sha256Hex(enc.encode("db-bytes")),
342
- size_bytes: 8,
343
- },
344
- ],
345
- checksum: "",
346
- secrets_redacted: false,
347
- export_options: {
348
- include_logs: false,
349
- include_browser_state: false,
350
- include_memory_vectors: false,
351
- },
352
- };
353
- manifest.checksum = computeManifestChecksum(manifest);
354
-
355
- const manifestBytes = enc.encode(JSON.stringify(manifest));
356
- const manifestHeader = buildHeader({
357
- name: "manifest.json",
358
- size: manifestBytes.length,
359
- typeflag: "0",
360
- });
361
- const manifestPart = new Uint8Array(BLOCK + padBlock(manifestBytes).length);
362
- manifestPart.set(manifestHeader, 0);
363
- manifestPart.set(padBlock(manifestBytes), BLOCK);
364
-
365
- // Symlink header: declares size=0o123 (= 83 decimal). parseTar must
366
- // propagate this 83 so the FILE_SIZE_MISMATCH check fires; if it forces
367
- // 0, the bug is silent. We pad one block of zero body bytes after the
368
- // header (the `Math.ceil(83/512) = 1` blocks parseTar will skip) so the
369
- // following DB entry header lands at the correct block boundary.
370
- const symlinkHeader = buildHeader({
371
- name: "workspace/skills/foo.md",
372
- size: 0o123,
373
- typeflag: "2",
374
- linkname: linkTarget,
375
- });
376
- const symlinkBodyPadding = new Uint8Array(BLOCK);
377
- const symlinkPart = new Uint8Array(
378
- symlinkHeader.length + symlinkBodyPadding.length,
379
- );
380
- symlinkPart.set(symlinkHeader, 0);
381
- symlinkPart.set(symlinkBodyPadding, symlinkHeader.length);
382
-
383
- // Regular file body for the DB entry so the manifest's required-DB
384
- // refine is satisfied.
385
- const dbBytes = enc.encode("db-bytes");
386
- const dbHeader = buildHeader({
387
- name: "workspace/data/db/assistant.db",
388
- size: dbBytes.length,
389
- typeflag: "0",
390
- });
391
- const dbPart = new Uint8Array(BLOCK + padBlock(dbBytes).length);
392
- dbPart.set(dbHeader, 0);
393
- dbPart.set(padBlock(dbBytes), BLOCK);
394
-
395
- const eoa = new Uint8Array(BLOCK * 2);
396
-
397
- const total =
398
- manifestPart.length + symlinkPart.length + dbPart.length + eoa.length;
399
- const tar = new Uint8Array(total);
400
- let off = 0;
401
- tar.set(manifestPart, off);
402
- off += manifestPart.length;
403
- tar.set(symlinkPart, off);
404
- off += symlinkPart.length;
405
- tar.set(dbPart, off);
406
- off += dbPart.length;
407
- tar.set(eoa, off);
408
-
409
- const archive = gzipSync(tar);
410
- const result = validateVBundle(archive);
411
-
412
- expect(result.is_valid).toBe(false);
413
- const sizeMismatch = result.errors.find(
414
- (e) =>
415
- e.code === "FILE_SIZE_MISMATCH" && e.path === "workspace/skills/foo.md",
416
- );
417
- expect(sizeMismatch).toBeDefined();
418
- });
419
-
420
- test("manifest sha256 tampered for a symlink entry surfaces FILE_CHECKSUM_MISMATCH", () => {
421
- const files = [
422
- {
423
- path: "workspace/skills/bar.md",
424
- data: new TextEncoder().encode("hello"),
425
- },
426
- {
427
- path: "workspace/skills/foo.md",
428
- data: new Uint8Array(0),
429
- linkTarget: "bar.md",
430
- },
431
- {
432
- path: "workspace/data/db/assistant.db",
433
- data: new TextEncoder().encode("db-bytes"),
434
- },
435
- ];
436
-
437
- const { archive } = buildVBundle({ files, ...defaultV1Options() });
438
-
439
- const wrongDigest = "0".repeat(64);
440
- const tampered = mutateManifestAndRepack(archive, (contents) => {
441
- const entry = contents.find((c) => c.path === "workspace/skills/foo.md")!;
442
- entry.sha256 = wrongDigest;
443
- });
444
-
445
- const result = validateVBundle(tampered);
446
- expect(result.is_valid).toBe(false);
447
- const checksum = result.errors.find(
448
- (e) => e.code === "FILE_CHECKSUM_MISMATCH",
449
- );
450
- expect(checksum).toBeDefined();
451
- expect(checksum!.path).toBe("workspace/skills/foo.md");
452
- });
453
- });