@vellumai/assistant 0.12.2-staging.5 → 0.12.2-staging.7

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 (169) hide show
  1. package/Dockerfile +7 -7
  2. package/docs/architecture/memory.md +11 -2
  3. package/docs/desktop-browser-cli.md +4 -2
  4. package/node_modules/@vellumai/environments/src/shell.test.ts +21 -0
  5. package/node_modules/@vellumai/environments/src/shell.ts +24 -0
  6. package/node_modules/@vellumai/gateway-client/src/inbound-contract.ts +8 -2
  7. package/openapi.yaml +157 -55
  8. package/package.json +5 -4
  9. package/scripts/bundled-plugin-packages.ts +154 -0
  10. package/scripts/generate-bundled-plugin-packages.ts +29 -0
  11. package/scripts/postinstall.ts +33 -0
  12. package/scripts/smoke-desktop-browser-cli.ts +1 -0
  13. package/scripts/test.ts +15 -13
  14. package/src/__tests__/agent-loop.test.ts +124 -0
  15. package/src/__tests__/approval-interception-trust-gates.test.ts +40 -0
  16. package/src/__tests__/channel-approval.test.ts +9 -14
  17. package/src/__tests__/conversation-agent-loop.test.ts +25 -0
  18. package/src/__tests__/db-conversation-tool-surface.test.ts +144 -0
  19. package/src/__tests__/managed-store.test.ts +121 -0
  20. package/src/__tests__/plugin-import-boundary-guard.test.ts +0 -1
  21. package/src/__tests__/run-conversation-turn-persistence.test.ts +138 -1
  22. package/src/__tests__/scaffold-managed-skill-tool.test.ts +88 -0
  23. package/src/__tests__/script-proxy-certs.test.ts +1 -1
  24. package/src/__tests__/subagent-tool-gate-mode.test.ts +169 -0
  25. package/src/__tests__/terminal-tools.test.ts +8 -0
  26. package/src/__tests__/unicode.test.ts +36 -0
  27. package/src/agent/loop.ts +19 -0
  28. package/src/api/events/desktop-activity-changed.ts +10 -0
  29. package/src/api/index.ts +6 -0
  30. package/src/approvals/approval-primitive.ts +5 -2
  31. package/src/approvals/scoped-approval-grants.ts +6 -2
  32. package/src/calls/__tests__/voice-control-protocol.test.ts +24 -2
  33. package/src/calls/__tests__/voice-session-bridge.test.ts +22 -2
  34. package/src/calls/voice-control-protocol.ts +13 -4
  35. package/src/calls/voice-session-bridge.ts +27 -6
  36. package/src/cli/commands/__tests__/plugins.test.ts +66 -0
  37. package/src/cli/commands/plugins.ts +50 -18
  38. package/src/cli/lib/__tests__/install-from-github.test.ts +67 -0
  39. package/src/cli/lib/__tests__/local-plugin-upgrade.test.ts +169 -0
  40. package/src/cli/lib/__tests__/plugin-catalog-cache.test.ts +57 -0
  41. package/src/cli/lib/__tests__/plugin-catalog-platform.test.ts +14 -0
  42. package/src/cli/lib/__tests__/plugin-catalog-resolve.test.ts +27 -1
  43. package/src/cli/lib/__tests__/plugin-details.test.ts +9 -2
  44. package/src/cli/lib/__tests__/plugin-marketplace.test.ts +20 -0
  45. package/src/cli/lib/__tests__/plugins-install-offline.test.ts +43 -0
  46. package/src/cli/lib/__tests__/search-plugins.test.ts +31 -0
  47. package/src/cli/lib/bundled-plugin-packages.json +4 -0
  48. package/src/cli/lib/bundled-plugin-packages.ts +87 -0
  49. package/src/cli/lib/diff-plugin.ts +1 -1
  50. package/src/cli/lib/inspect-plugin.ts +80 -12
  51. package/src/cli/lib/install-from-github.ts +133 -76
  52. package/src/cli/lib/plugin-catalog-cache.ts +22 -3
  53. package/src/cli/lib/plugin-catalog-local.ts +13 -3
  54. package/src/cli/lib/plugin-catalog-platform.ts +6 -1
  55. package/src/cli/lib/plugin-catalog-resolve.ts +20 -0
  56. package/src/cli/lib/plugin-details.ts +12 -0
  57. package/src/cli/lib/plugin-marketplace.ts +121 -21
  58. package/src/cli/lib/plugin-pin-history.ts +5 -2
  59. package/src/cli/lib/search-plugins.ts +58 -16
  60. package/src/cli/lib/upgrade-plugin.ts +48 -3
  61. package/src/config/bundled-skills/skill-management/SKILL.md +1 -1
  62. package/src/config/bundled-skills/skill-management/TOOLS.json +6 -6
  63. package/src/daemon/__tests__/conversation-tool-setup.test.ts +43 -0
  64. package/src/daemon/conversation-agent-loop.ts +2 -0
  65. package/src/daemon/conversation-tool-setup.ts +61 -23
  66. package/src/daemon/conversation.ts +17 -0
  67. package/src/daemon/daemon-control.ts +2 -6
  68. package/src/daemon/orphan-reaper.ts +4 -3
  69. package/src/daemon/tool-setup-types.ts +6 -0
  70. package/src/daemon/wake-conversation-ops.ts +38 -15
  71. package/src/desktop/desktop-automation-lease.test.ts +143 -0
  72. package/src/desktop/desktop-automation-lease.ts +39 -3
  73. package/src/live-voice/__tests__/live-voice-vad.test.ts +624 -3
  74. package/src/live-voice/__tests__/session-controls.test.ts +18 -0
  75. package/src/live-voice/live-voice-session.ts +575 -53
  76. package/src/live-voice/session-controls.ts +7 -3
  77. package/src/messaging/provider-message-metadata.ts +3 -3
  78. package/src/monitoring/plugin-auto-update.ts +6 -0
  79. package/src/notifications/__tests__/copy-composer.test.ts +70 -0
  80. package/src/notifications/copy-composer.ts +11 -3
  81. package/src/persistence/conversation-plugin-facade.ts +13 -0
  82. package/src/persistence/conversation-tool-surface.ts +86 -0
  83. package/src/persistence/migrations/378-create-conversation-tool-surfaces.test.ts +78 -0
  84. package/src/persistence/migrations/378-create-conversation-tool-surfaces.ts +29 -0
  85. package/src/persistence/schema/conversation-tool-surfaces.ts +22 -0
  86. package/src/persistence/schema/index.ts +1 -0
  87. package/src/persistence/steps.ts +2 -0
  88. package/src/plugin-api/conversation-turn.ts +31 -7
  89. package/src/plugin-api/index.ts +9 -1
  90. package/src/plugin-api/plugin-channel-turn-trust.test.ts +133 -0
  91. package/src/plugin-api/plugin-channel-turn-trust.ts +71 -0
  92. package/src/plugins/defaults/memory/AGENTS.md +14 -2
  93. package/src/plugins/defaults/memory/__tests__/buffer-file.test.ts +320 -0
  94. package/src/plugins/defaults/memory/__tests__/buffer-format.test.ts +43 -0
  95. package/src/plugins/defaults/memory/__tests__/fixtures/buffer-appender.ts +17 -0
  96. package/src/plugins/defaults/memory/__tests__/memory-retrospective-job.test.ts +46 -0
  97. package/src/plugins/defaults/memory/__tests__/memory-retrospective-prompt.test.ts +5 -0
  98. package/src/plugins/defaults/memory/__tests__/memory-run-evidence.test.ts +161 -0
  99. package/src/plugins/defaults/memory/buffer-file.ts +354 -0
  100. package/src/plugins/defaults/memory/buffer-format.ts +40 -0
  101. package/src/plugins/defaults/memory/context-search/agent-runner.ts +1 -2
  102. package/src/plugins/defaults/memory/context-search/format.ts +2 -1
  103. package/src/plugins/defaults/memory/context-search/sources/memory-v2.ts +2 -1
  104. package/src/plugins/defaults/memory/context-search/sources/workspace.ts +2 -1
  105. package/src/plugins/defaults/memory/graph/capability-seed.ts +1 -2
  106. package/src/plugins/defaults/memory/graph/tool-handlers.ts +1 -42
  107. package/src/plugins/defaults/memory/host-utils.ts +0 -10
  108. package/src/plugins/defaults/memory/injectors.ts +4 -3
  109. package/src/plugins/defaults/memory/memory-retrospective-job.ts +55 -181
  110. package/src/plugins/defaults/memory/memory-retrospective-prompt.ts +1 -1
  111. package/src/plugins/defaults/memory/memory-run-evidence.ts +213 -0
  112. package/src/plugins/defaults/memory/substrate/__tests__/consolidation-job.test.ts +407 -99
  113. package/src/plugins/defaults/memory/substrate/__tests__/consolidation-prompt-flag-gating-guard.test.ts +10 -0
  114. package/src/plugins/defaults/memory/substrate/__tests__/prompts-consolidation.test.ts +107 -7
  115. package/src/plugins/defaults/memory/substrate/consolidation-job.ts +307 -86
  116. package/src/plugins/defaults/memory/substrate/consolidation-tool-surface.ts +34 -0
  117. package/src/plugins/defaults/memory/substrate/page-index.ts +2 -1
  118. package/src/plugins/defaults/memory/substrate/prompts/consolidation.ts +89 -49
  119. package/src/plugins/defaults/memory/substrate/sweep-job.ts +1 -1
  120. package/src/plugins/defaults/memory/tools.ts +1 -1
  121. package/src/plugins/defaults/memory/v1/graph/consolidation.ts +2 -2
  122. package/src/plugins/defaults/memory/v1/graph/extraction.ts +2 -1
  123. package/src/plugins/defaults/memory/v1/graph/retriever.ts +1 -1
  124. package/src/plugins/defaults/memory/v2/__tests__/migration.test.ts +5 -0
  125. package/src/plugins/defaults/memory/v2/__tests__/reranker.test.ts +5 -2
  126. package/src/plugins/defaults/memory/v2/reranker.ts +2 -1
  127. package/src/plugins/defaults/memory/v3/__tests__/injection.test.ts +81 -1
  128. package/src/plugins/defaults/memory/v3/__tests__/orchestrate.test.ts +87 -0
  129. package/src/plugins/defaults/memory/v3/__tests__/shadow-plugin.test.ts +21 -0
  130. package/src/plugins/defaults/memory/v3/card.ts +2 -1
  131. package/src/plugins/defaults/memory/v3/injector.ts +212 -178
  132. package/src/plugins/defaults/memory/v3/orchestrate.ts +86 -22
  133. package/src/plugins/defaults/memory/v3/pool-select.ts +10 -7
  134. package/src/plugins/defaults/memory/v3/sections.ts +2 -1
  135. package/src/plugins/defaults/memory/v3/shadow-plugin.ts +10 -1
  136. package/src/plugins/defaults/tool-result-truncate/terminal.ts +1 -46
  137. package/src/runtime/AGENTS.md +1 -1
  138. package/src/runtime/__tests__/agent-wake.test.ts +86 -1
  139. package/src/runtime/agent-wake.ts +20 -4
  140. package/src/runtime/guardian-action-service.ts +2 -17
  141. package/src/runtime/guardian-reply-router.ts +1 -8
  142. package/src/runtime/routes/__tests__/plugins-routes.test.ts +102 -0
  143. package/src/runtime/routes/channel-route-shared.ts +1 -9
  144. package/src/runtime/routes/desktop-setup-routes.test.ts +2 -2
  145. package/src/runtime/routes/desktop-setup-routes.ts +7 -3
  146. package/src/runtime/routes/guardian-approval-interception.ts +24 -0
  147. package/src/runtime/routes/inbound-message-handler.ts +2 -3
  148. package/src/runtime/routes/inbound-stages/background-dispatch.test.ts +1 -1
  149. package/src/runtime/routes/inbound-stages/background-dispatch.ts +7 -4
  150. package/src/runtime/routes/plugins-routes.ts +69 -49
  151. package/src/schedule/run-script.ts +2 -2
  152. package/src/skills/managed-store.ts +98 -32
  153. package/src/tools/host-terminal/host-shell.ts +12 -6
  154. package/src/tools/shared/filesystem/file-ops-service.ts +1 -31
  155. package/src/tools/shared/shell-output.test.ts +10 -0
  156. package/src/tools/shared/shell-output.ts +14 -2
  157. package/src/tools/skills/find-similar-skills.test.ts +3 -0
  158. package/src/tools/skills/resolve-execute-invocation.ts +24 -0
  159. package/src/tools/skills/sandbox-runner.ts +13 -2
  160. package/src/tools/skills/scaffold-managed.ts +18 -14
  161. package/src/tools/terminal/__tests__/safe-env.test.ts +29 -0
  162. package/src/tools/terminal/safe-env.ts +30 -1
  163. package/src/tools/terminal/sanitized-bash.ts +15 -2
  164. package/src/tools/terminal/shell-launch.test.ts +162 -0
  165. package/src/tools/terminal/shell.test.ts +29 -0
  166. package/src/tools/terminal/shell.ts +13 -7
  167. package/src/util/host-process.test.ts +17 -1
  168. package/src/util/host-process.ts +24 -0
  169. package/src/util/unicode.ts +29 -0
@@ -0,0 +1,133 @@
1
+ import { beforeEach, describe, expect, mock, test } from "bun:test";
2
+
3
+ type InboundTrustReadResult =
4
+ | {
5
+ ok: true;
6
+ verdict: {
7
+ trustClass: string;
8
+ canonicalSenderId: string | null;
9
+ contactId?: string;
10
+ status?: string;
11
+ resolutionFailed?: boolean;
12
+ };
13
+ admissionPolicy: string | null;
14
+ }
15
+ | { ok: false };
16
+
17
+ let nextRead: InboundTrustReadResult = { ok: false };
18
+
19
+ mock.module("../calls/inbound-trust-reader.js", () => ({
20
+ readInboundTrust: async () => nextRead,
21
+ }));
22
+
23
+ import {
24
+ PluginTurnNotAdmittedError,
25
+ resolvePluginChannelTurnTrust,
26
+ } from "./plugin-channel-turn-trust.js";
27
+
28
+ const CHANNEL = {
29
+ sourceChannel: "plugin" as const,
30
+ externalChatId: "imessage:+12025550142",
31
+ externalUserId: "imessage:+12025550142",
32
+ displayName: "Ada",
33
+ };
34
+
35
+ beforeEach(() => {
36
+ nextRead = { ok: false };
37
+ });
38
+
39
+ describe("resolvePluginChannelTurnTrust", () => {
40
+ test("returns the gateway verdict as the turn trust context", async () => {
41
+ nextRead = {
42
+ ok: true,
43
+ verdict: {
44
+ trustClass: "trusted_contact",
45
+ canonicalSenderId: "imessage:+12025550142",
46
+ contactId: "c-ada",
47
+ status: "active",
48
+ },
49
+ admissionPolicy: "trusted_contacts",
50
+ };
51
+
52
+ const trust = await resolvePluginChannelTurnTrust(CHANNEL);
53
+ expect(trust.trustClass).toBe("trusted_contact");
54
+ expect(trust.sourceChannel).toBe("plugin");
55
+ expect(trust.requesterChatId).toBe(CHANNEL.externalChatId);
56
+ });
57
+
58
+ test("fails closed when the gateway could not vouch for the sender", async () => {
59
+ nextRead = {
60
+ ok: true,
61
+ verdict: {
62
+ trustClass: "unknown",
63
+ canonicalSenderId: null,
64
+ resolutionFailed: true,
65
+ },
66
+ admissionPolicy: "strangers",
67
+ };
68
+
69
+ await expect(resolvePluginChannelTurnTrust(CHANNEL)).rejects.toMatchObject({
70
+ name: "PluginTurnNotAdmittedError",
71
+ reason: "trust_resolution_failed",
72
+ });
73
+ });
74
+
75
+ test("fails closed when the gateway trust read fails", async () => {
76
+ nextRead = { ok: false };
77
+ await expect(resolvePluginChannelTurnTrust(CHANNEL)).rejects.toMatchObject({
78
+ name: "PluginTurnNotAdmittedError",
79
+ reason: "trust_resolution_failed",
80
+ });
81
+ });
82
+
83
+ test("rejects a sender below the channel admission floor", async () => {
84
+ nextRead = {
85
+ ok: true,
86
+ verdict: { trustClass: "unknown", canonicalSenderId: null },
87
+ admissionPolicy: "guardian_only",
88
+ };
89
+
90
+ await expect(resolvePluginChannelTurnTrust(CHANNEL)).rejects.toEqual(
91
+ expect.objectContaining({
92
+ name: "PluginTurnNotAdmittedError",
93
+ reason: "admission_policy_guardian_only",
94
+ }),
95
+ );
96
+ });
97
+
98
+ test("rejects a blocked member regardless of floor", async () => {
99
+ nextRead = {
100
+ ok: true,
101
+ verdict: {
102
+ trustClass: "trusted_contact",
103
+ canonicalSenderId: "imessage:+12025550142",
104
+ contactId: "c-ada",
105
+ status: "blocked",
106
+ },
107
+ admissionPolicy: "strangers",
108
+ };
109
+
110
+ await expect(resolvePluginChannelTurnTrust(CHANNEL)).rejects.toMatchObject({
111
+ name: "PluginTurnNotAdmittedError",
112
+ reason: "member_blocked",
113
+ });
114
+ });
115
+
116
+ test("admits when the gateway reports no admission policy", async () => {
117
+ nextRead = {
118
+ ok: true,
119
+ verdict: { trustClass: "unknown", canonicalSenderId: null },
120
+ admissionPolicy: null,
121
+ };
122
+
123
+ const trust = await resolvePluginChannelTurnTrust(CHANNEL);
124
+ expect(trust.trustClass).toBe("unknown");
125
+ });
126
+
127
+ test("PluginTurnNotAdmittedError names the deny reason", () => {
128
+ const err = new PluginTurnNotAdmittedError("admission_policy_no_one");
129
+ expect(err).toBeInstanceOf(Error);
130
+ expect(err.reason).toBe("admission_policy_no_one");
131
+ expect(err.message).toContain("admission_policy_no_one");
132
+ });
133
+ });
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Gateway admission and trust for plugin turns addressed by a channel chat.
3
+ *
4
+ * A channel address means an inbound sender, not an internal plugin job.
5
+ * Trust comes from the gateway verdict (`resolve_inbound_trust`); the turn
6
+ * runs only when that sender clears the channel admission floor.
7
+ */
8
+
9
+ import { meetsAdmissionFloor } from "@vellumai/gateway-client";
10
+
11
+ import { readInboundTrust } from "../calls/inbound-trust-reader.js";
12
+ import type { ChannelId } from "../channels/types.js";
13
+ import type { TrustContext } from "../daemon/trust-context-types.js";
14
+ import { trustContextFromVerdict } from "../runtime/trust-verdict-consumer.js";
15
+
16
+ interface PluginChannelTurnAddress {
17
+ sourceChannel: ChannelId;
18
+ externalChatId: string;
19
+ externalUserId?: string | null;
20
+ displayName?: string | null;
21
+ username?: string | null;
22
+ }
23
+
24
+ export class PluginTurnNotAdmittedError extends Error {
25
+ readonly reason: string;
26
+
27
+ constructor(reason: string) {
28
+ super(`This inbound plugin turn was not admitted (${reason}).`);
29
+ this.name = "PluginTurnNotAdmittedError";
30
+ this.reason = reason;
31
+ }
32
+ }
33
+
34
+ export async function resolvePluginChannelTurnTrust(
35
+ channel: PluginChannelTurnAddress,
36
+ ): Promise<TrustContext> {
37
+ const actorExternalId = channel.externalUserId?.trim() || undefined;
38
+ const read = await readInboundTrust({
39
+ channelType: channel.sourceChannel,
40
+ actorExternalId,
41
+ });
42
+ if (!read.ok) {
43
+ throw new PluginTurnNotAdmittedError("trust_resolution_failed");
44
+ }
45
+
46
+ if (read.verdict.resolutionFailed) {
47
+ throw new PluginTurnNotAdmittedError("trust_resolution_failed");
48
+ }
49
+
50
+ const memberStatus = read.verdict.status;
51
+ if (memberStatus === "blocked" || memberStatus === "revoked") {
52
+ throw new PluginTurnNotAdmittedError(`member_${memberStatus}`);
53
+ }
54
+
55
+ const trustClass = read.verdict.trustClass;
56
+ if (
57
+ read.admissionPolicy != null &&
58
+ !meetsAdmissionFloor(read.admissionPolicy, trustClass)
59
+ ) {
60
+ throw new PluginTurnNotAdmittedError(
61
+ `admission_policy_${read.admissionPolicy}`,
62
+ );
63
+ }
64
+
65
+ return trustContextFromVerdict(read.verdict, {
66
+ sourceChannel: channel.sourceChannel,
67
+ conversationExternalId: channel.externalChatId,
68
+ actorDisplayName: channel.displayName ?? undefined,
69
+ actorUsername: channel.username ?? undefined,
70
+ });
71
+ }
@@ -68,6 +68,13 @@ Everything else under the plugin root is **spine**:
68
68
  the writer plus the one matcher every reader uses. It lives at the root
69
69
  precisely so `substrate/`, `graph/`, and `graph-topology/` can all reach it
70
70
  without a tier importing spine. Do not add a second matcher anywhere),
71
+ `buffer-file` (the two writers of `memory/buffer.md`: the append every
72
+ `remember()`-shaped path uses and the consume the consolidation job runs
73
+ after a pass. Do not write the buffer from anywhere else, and never
74
+ rewrite it from a read that is not inside `consumeBufferEntries`'s
75
+ synchronous critical section), `memory-run-evidence` (readers of what a
76
+ background memory run durably produced from its persisted messages; every
77
+ job that gates a state transition on a verified write uses them),
71
78
  `segmenter`, `message-media`, `worker`, `worker-control`,
72
79
  `memory-recall-log-store`, `activation-session-store` (the onboarding
73
80
  activation rail — **not** a memory tier despite the name),
@@ -407,8 +414,13 @@ per distinct matched section, at most `memory.v3.finderSectionsPerPage` in
407
414
  surfacing order (needle, dense, reply, span) plus its entity and rare-term
408
415
  lines, and selecting a line selects that section; the selection log keeps
409
416
  one row per slug, so the pool row is where the per-section verdicts live. A
410
- turn whose selector never judged a pool (the injection gate hard-skipped it,
411
- or nothing was pooled) persists an empty pool with `selector_ran = 0`, and a
417
+ turn whose selector never judged a pool persists it with `selector_ran = 0`:
418
+ an empty pool when the injection gate hard-skipped it or nothing was pooled,
419
+ and the pool as the selector was given it, stable-prefix cards chosen and
420
+ finder lines not, when the selector's provider failed and the orchestrator
421
+ kept that prefix unjudged. That turn writes no `memory_v3_selections` rows:
422
+ the hot set's frecency and the learned-edge graph read that table as
423
+ judgments, and an unjudged page is not one. A
412
424
  turn that logged no selections is still reachable by its stamped
413
425
  `message_id`, so the inspector shows negative verdicts too. The pool row and
414
426
  the turn's `memory_v3_selections` rows are
@@ -0,0 +1,320 @@
1
+ /**
2
+ * Tests for `buffer-file.ts`: the two writers of `memory/buffer.md`.
3
+ *
4
+ * The consume cases pin the invariant the module exists for: an entry saved
5
+ * at any point while a consolidation run is in flight is either the run's
6
+ * own material or still in the buffer afterwards, never destroyed. The
7
+ * cross-process case is the reason to believe it: a child process appends
8
+ * as fast as it can while this process consumes in a loop, and every tagged
9
+ * entry must end up consumed or present. The same harness run against a
10
+ * naive read-modify-write consume loses entries, which is what the
11
+ * mechanism is measured against.
12
+ */
13
+
14
+ import {
15
+ appendFileSync,
16
+ chmodSync,
17
+ closeSync,
18
+ existsSync,
19
+ mkdtempSync,
20
+ openSync,
21
+ readdirSync,
22
+ readFileSync,
23
+ rmSync,
24
+ writeFileSync,
25
+ } from "node:fs";
26
+ import { tmpdir } from "node:os";
27
+ import { join } from "node:path";
28
+ import {
29
+ afterAll,
30
+ beforeAll,
31
+ beforeEach,
32
+ describe,
33
+ expect,
34
+ test,
35
+ } from "bun:test";
36
+
37
+ import {
38
+ appendBufferAndArchive,
39
+ consumeBufferEntries,
40
+ } from "../buffer-file.js";
41
+ import {
42
+ type BufferEntryLines,
43
+ bufferEntryText,
44
+ formatRememberEntry,
45
+ joinBufferEntries,
46
+ splitBufferContent,
47
+ } from "../buffer-format.js";
48
+
49
+ let tmp: string;
50
+ beforeAll(() => {
51
+ tmp = mkdtempSync(join(tmpdir(), "memory-buffer-file-test-"));
52
+ });
53
+ afterAll(() => {
54
+ rmSync(tmp, { recursive: true, force: true });
55
+ });
56
+
57
+ let memoryDir: string;
58
+ let bufferPath: string;
59
+ beforeEach(() => {
60
+ memoryDir = mkdtempSync(join(tmp, "memory-"));
61
+ bufferPath = join(memoryDir, "buffer.md");
62
+ });
63
+
64
+ const A = "- [Apr 27, 9:00 AM] Alice prefers VS Code.";
65
+ const B = "- [Apr 27, 9:01 AM] Bob shared a snippet:\n line two\n line three";
66
+ const C = "- [Apr 27, 9:02 AM] Carol loves jazz.";
67
+ const D = "- [Apr 27, 9:03 AM] Dave runs marathons.";
68
+
69
+ function file(...entries: string[]): string {
70
+ return entries.map((e) => `${e}\n`).join("");
71
+ }
72
+ function entries(content: string): BufferEntryLines[] {
73
+ return splitBufferContent(content);
74
+ }
75
+ function texts(): string[] {
76
+ return entries(readFileSync(bufferPath, "utf-8")).map(bufferEntryText);
77
+ }
78
+
79
+ describe("appendBufferAndArchive", () => {
80
+ test("appends to the buffer and the dated archive, seeding the archive header once", () => {
81
+ const now = new Date(2026, 3, 27, 9, 0);
82
+ const entry = formatRememberEntry("Alice prefers VS Code.", now);
83
+ const first = appendBufferAndArchive({ rootDir: memoryDir, entry, now });
84
+ appendBufferAndArchive({ rootDir: memoryDir, entry, now });
85
+
86
+ expect(first.bufferPath).toBe(bufferPath);
87
+ expect(readFileSync(bufferPath, "utf-8")).toBe(entry + entry);
88
+ expect(first.archivePath).toBe(join(memoryDir, "archive", "2026-04-27.md"));
89
+ expect(readFileSync(first.archivePath, "utf-8")).toBe(
90
+ `# Apr 27, 2026\n\n${entry}${entry}`,
91
+ );
92
+ });
93
+ });
94
+
95
+ describe("consumeBufferEntries", () => {
96
+ test("removes exactly the consumed entries and keeps the rest verbatim and in order", async () => {
97
+ writeFileSync(bufferPath, file(A, B, C, D));
98
+ const pass = entries(file(A, B));
99
+
100
+ const result = await consumeBufferEntries(bufferPath, pass, {
101
+ lateAppendGraceMs: 0,
102
+ });
103
+
104
+ expect(result).toEqual({
105
+ removed: 2,
106
+ alreadyAbsent: 0,
107
+ lateAppendBytesRecovered: 0,
108
+ unrecoveredLateAppendBytes: 0,
109
+ lateAppendDrainFailed: false,
110
+ });
111
+ expect(readFileSync(bufferPath, "utf-8")).toBe(file(C, D));
112
+ });
113
+
114
+ test("entries appended after the snapshot survive, including one that duplicates a consumed entry", async () => {
115
+ // The snapshot held one copy of A; the same fact was remembered again
116
+ // during the run. Multiset semantics: one copy goes, one stays.
117
+ writeFileSync(bufferPath, file(A, B));
118
+ const pass = entries(file(A, B));
119
+ appendFileSync(bufferPath, file(C, A));
120
+
121
+ const result = await consumeBufferEntries(bufferPath, pass, {
122
+ lateAppendGraceMs: 0,
123
+ });
124
+
125
+ expect(result.removed).toBe(2);
126
+ expect(texts()).toEqual([C, A]);
127
+ });
128
+
129
+ test("a consumed entry missing from the live file is counted, not searched for", async () => {
130
+ writeFileSync(bufferPath, file(B, C));
131
+ const pass = entries(file(A, B));
132
+
133
+ const result = await consumeBufferEntries(bufferPath, pass, {
134
+ lateAppendGraceMs: 0,
135
+ });
136
+
137
+ expect(result).toMatchObject({ removed: 1, alreadyAbsent: 1 });
138
+ expect(texts()).toEqual([C]);
139
+ });
140
+
141
+ test("a missing buffer file consumes nothing and creates nothing", async () => {
142
+ const result = await consumeBufferEntries(bufferPath, entries(file(A)), {
143
+ lateAppendGraceMs: 0,
144
+ });
145
+
146
+ expect(result).toEqual({
147
+ removed: 0,
148
+ alreadyAbsent: 1,
149
+ lateAppendBytesRecovered: 0,
150
+ unrecoveredLateAppendBytes: 0,
151
+ lateAppendDrainFailed: false,
152
+ });
153
+ expect(existsSync(bufferPath)).toBe(false);
154
+ });
155
+
156
+ test("consuming everything leaves an empty file and no temp file behind", async () => {
157
+ writeFileSync(bufferPath, file(A, B));
158
+
159
+ await consumeBufferEntries(bufferPath, entries(file(A, B)), {
160
+ lateAppendGraceMs: 0,
161
+ });
162
+
163
+ expect(readFileSync(bufferPath, "utf-8")).toBe("");
164
+ expect(readdirSync(memoryDir)).toEqual(["buffer.md"]);
165
+ });
166
+
167
+ test("an append that landed on the replaced inode is drained back into the buffer", async () => {
168
+ // An appender that opened the file before the rename writes to the old
169
+ // inode. The consumer holds that inode open and copies the late bytes
170
+ // over. The test plays the appender: open before, write after the
171
+ // synchronous phase, before the grace-window drain.
172
+ writeFileSync(bufferPath, file(A, B));
173
+ const lateFd = openSync(bufferPath, "a");
174
+ const consuming = consumeBufferEntries(bufferPath, entries(file(A)), {
175
+ lateAppendGraceMs: 50,
176
+ });
177
+ // The synchronous phase (read, rewrite, rename) ran before the first
178
+ // await inside consume, so this write lands on the orphaned inode.
179
+ appendFileSync(lateFd, file(C));
180
+ closeSync(lateFd);
181
+
182
+ const result = await consuming;
183
+
184
+ expect(result.removed).toBe(1);
185
+ expect(result.lateAppendBytesRecovered).toBe(file(C).length);
186
+ expect(result.unrecoveredLateAppendBytes).toBe(0);
187
+ expect(texts()).toEqual([B, C]);
188
+ });
189
+
190
+ test("a late append the drain cannot copy back is reported, not thrown, and the pass stays consumed", async () => {
191
+ // The rename has committed when the drain runs, so a failing copy must
192
+ // not surface as "nothing happened". The test makes the rewritten
193
+ // buffer read-only before the late bytes land, so every copy attempt
194
+ // fails.
195
+ writeFileSync(bufferPath, file(A, B));
196
+ const lateFd = openSync(bufferPath, "a");
197
+ const consuming = consumeBufferEntries(bufferPath, entries(file(A)), {
198
+ lateAppendGraceMs: 50,
199
+ });
200
+ chmodSync(bufferPath, 0o444);
201
+ appendFileSync(lateFd, file(C));
202
+ closeSync(lateFd);
203
+
204
+ try {
205
+ const result = await consuming;
206
+ expect(result.removed).toBe(1);
207
+ expect(result.lateAppendBytesRecovered).toBe(0);
208
+ expect(result.unrecoveredLateAppendBytes).toBe(file(C).length);
209
+ expect(result.lateAppendDrainFailed).toBe(false);
210
+ expect(texts()).toEqual([B]);
211
+ } finally {
212
+ chmodSync(bufferPath, 0o644);
213
+ }
214
+ });
215
+ });
216
+
217
+ /**
218
+ * The cross-process invariant. A child appends `count` tagged entries as
219
+ * fast as it can through `appendFileSync`; this process consumes the leading
220
+ * entries (up to `passSize` at a time) in a loop through `consume`. Returns
221
+ * the tags that were neither consumed by a pass nor left in the buffer: the
222
+ * entries the consume destroyed.
223
+ */
224
+ async function lostUnder(
225
+ consume: (path: string, pass: BufferEntryLines[]) => Promise<unknown>,
226
+ count: number,
227
+ passSize: number,
228
+ ): Promise<{ lost: string[]; duplicated: string[]; passes: number }> {
229
+ const tag = `t${Math.random().toString(36).slice(2, 8)}`;
230
+ writeFileSync(bufferPath, "");
231
+ const child = Bun.spawn(
232
+ [
233
+ process.execPath,
234
+ join(import.meta.dir, "fixtures", "buffer-appender.ts"),
235
+ bufferPath,
236
+ String(count),
237
+ tag,
238
+ ],
239
+ { stdout: "ignore", stderr: "inherit" },
240
+ );
241
+ const consumed: string[] = [];
242
+ let passes = 0;
243
+ let childDone = false;
244
+ void child.exited.then(() => {
245
+ childDone = true;
246
+ });
247
+ while (!childDone) {
248
+ const snapshot = entries(readFileSync(bufferPath, "utf-8"));
249
+ const pass = snapshot.slice(0, passSize);
250
+ if (pass.length === 0) {
251
+ await Bun.sleep(1);
252
+ continue;
253
+ }
254
+ await consume(bufferPath, pass);
255
+ consumed.push(...pass.map(bufferEntryText));
256
+ passes += 1;
257
+ }
258
+ await child.exited;
259
+ const seen = new Map<string, number>();
260
+ for (const text of [...consumed, ...texts()]) {
261
+ seen.set(text, (seen.get(text) ?? 0) + 1);
262
+ }
263
+ const lost: string[] = [];
264
+ const duplicated: string[] = [];
265
+ for (let i = 0; i < count; i++) {
266
+ const text = `- [Apr 27, 9:00 AM] ${tag}-${i}`;
267
+ const n = seen.get(text) ?? 0;
268
+ if (n === 0) {
269
+ lost.push(text);
270
+ } else if (n > 1) {
271
+ duplicated.push(text);
272
+ }
273
+ }
274
+ return { lost, duplicated, passes };
275
+ }
276
+
277
+ /** The read-modify-write the production consume replaces. */
278
+ async function naiveConsume(
279
+ path: string,
280
+ pass: BufferEntryLines[],
281
+ ): Promise<void> {
282
+ const pending = pass.map(bufferEntryText);
283
+ const remaining = entries(readFileSync(path, "utf-8")).filter((entry) => {
284
+ const index = pending.indexOf(bufferEntryText(entry));
285
+ if (index === -1) {
286
+ return true;
287
+ }
288
+ pending.splice(index, 1);
289
+ return false;
290
+ });
291
+ writeFileSync(path, joinBufferEntries(remaining), "utf-8");
292
+ }
293
+
294
+ describe("consumeBufferEntries under a concurrent appender in another process", () => {
295
+ const APPENDS = 4000;
296
+
297
+ test("no entry appended during consumption is lost or duplicated", async () => {
298
+ // Runs the consume exactly as production does, grace window included:
299
+ // the window is what makes the invariant hold, and a loaded machine
300
+ // can deschedule the appender between its open and its write for
301
+ // longer than a token grace would cover. Larger passes keep the run
302
+ // short with the full grace paid per pass.
303
+ const outcome = await lostUnder(
304
+ (path, pass) => consumeBufferEntries(path, pass),
305
+ APPENDS,
306
+ 500,
307
+ );
308
+ expect(outcome.passes).toBeGreaterThan(0);
309
+ expect(outcome.lost).toEqual([]);
310
+ expect(outcome.duplicated).toEqual([]);
311
+ }, 60_000);
312
+
313
+ test("the naive read-modify-write consume loses entries under the same load", async () => {
314
+ // Sensitivity check for the harness: the mechanism above is only
315
+ // evidence if this shape, run identically, fails.
316
+ const outcome = await lostUnder(naiveConsume, APPENDS, 500);
317
+ expect(outcome.passes).toBeGreaterThan(0);
318
+ expect(outcome.lost.length).toBeGreaterThan(0);
319
+ }, 60_000);
320
+ });
@@ -10,10 +10,13 @@
10
10
  import { describe, expect, test } from "bun:test";
11
11
 
12
12
  import {
13
+ bufferEntryText,
13
14
  formatBufferTimestamp,
14
15
  formatRememberEntry,
15
16
  isBufferEntryStart,
17
+ joinBufferEntries,
16
18
  matchBufferEntryStart,
19
+ splitBufferContent,
17
20
  splitBufferEntries,
18
21
  } from "../buffer-format.js";
19
22
 
@@ -202,3 +205,43 @@ describe("splitBufferEntries", () => {
202
205
  expect(splitBufferEntries([])).toEqual([]);
203
206
  });
204
207
  });
208
+
209
+ describe("splitBufferContent / joinBufferEntries", () => {
210
+ const content =
211
+ formatRememberEntry("Alice prefers dark mode", new Date(2026, 0, 1, 9, 0)) +
212
+ formatRememberEntry(
213
+ "Bob's plan:\n- [ ] step one\n\nend",
214
+ new Date(2026, 0, 1, 9, 1),
215
+ );
216
+
217
+ test("round-trips newline-terminated content byte for byte", () => {
218
+ expect(joinBufferEntries(splitBufferContent(content))).toBe(content);
219
+ });
220
+
221
+ test("an entry's text is the same whether or not it is last in the file", () => {
222
+ const [first] = splitBufferContent(content);
223
+ const [alone] = splitBufferContent(
224
+ formatRememberEntry(
225
+ "Alice prefers dark mode",
226
+ new Date(2026, 0, 1, 9, 0),
227
+ ),
228
+ );
229
+ expect(bufferEntryText(alone!)).toBe(bufferEntryText(first!));
230
+ expect(bufferEntryText(alone!)).toBe(
231
+ "- [Jan 1, 9:00 AM] Alice prefers dark mode",
232
+ );
233
+ });
234
+
235
+ test("empty content has no entries and joins back to empty", () => {
236
+ expect(splitBufferContent("")).toEqual([]);
237
+ expect(joinBufferEntries([])).toBe("");
238
+ });
239
+
240
+ test("content without a terminating newline keeps its last line intact", () => {
241
+ const entries = splitBufferContent("- [Jan 1, 9:00 AM] partial appe");
242
+ expect(entries).toHaveLength(1);
243
+ expect(bufferEntryText(entries[0]!)).toBe(
244
+ "- [Jan 1, 9:00 AM] partial appe",
245
+ );
246
+ });
247
+ });
@@ -0,0 +1,17 @@
1
+ // Child process for the buffer-file cross-process test: appends `count`
2
+ // uniquely tagged entries to `bufferPath` as fast as it can, through the same
3
+ // `appendFileSync` call the production appender uses. Hand-formats the entry
4
+ // line so the fixture imports nothing from `src/`.
5
+ //
6
+ // usage: bun buffer-appender.ts <bufferPath> <count> <tag>
7
+
8
+ import { appendFileSync } from "node:fs";
9
+
10
+ const [bufferPath, countArg, tag] = process.argv.slice(2);
11
+ if (!bufferPath || !countArg || !tag) {
12
+ throw new Error("usage: buffer-appender <bufferPath> <count> <tag>");
13
+ }
14
+ const count = Number.parseInt(countArg, 10);
15
+ for (let i = 0; i < count; i++) {
16
+ appendFileSync(bufferPath, `- [Apr 27, 9:00 AM] ${tag}-${i}\n`, "utf-8");
17
+ }
@@ -117,6 +117,17 @@ let loadedConversations: Record<string, { processing: boolean }> = {};
117
117
  // the stale-flag override's age check. Absent ids read as null (stampless).
118
118
  let processingStartedAtById: Record<string, number | null> = {};
119
119
 
120
+ // The source conversation's recorded wire tool surface (what its last live
121
+ // turn sent to the provider). `null` = no live turn has recorded one yet.
122
+ let mockSourceToolSurface: Array<Record<string, unknown>> | null = null;
123
+ let toolSurfaceReads: string[] = [];
124
+ mock.module("../../../../persistence/conversation-tool-surface.js", () => ({
125
+ getConversationToolSurface: (conversationId: string) => {
126
+ toolSurfaceReads.push(conversationId);
127
+ return mockSourceToolSurface;
128
+ },
129
+ }));
130
+
120
131
  const watchdogEvents: Array<{
121
132
  checkName: string;
122
133
  value?: number | null;
@@ -509,6 +520,8 @@ describe("memoryRetrospectiveJob", () => {
509
520
  resolveUserSlugCalls = [];
510
521
  mockV3TierActive = true;
511
522
  mockSkillImprovementActive = false;
523
+ mockSourceToolSurface = null;
524
+ toolSurfaceReads = [];
512
525
  });
513
526
 
514
527
  test("first-run happy path: no state row, no prior retrospective, both pointer fields set on success", async () => {
@@ -1842,6 +1855,39 @@ describe("memoryRetrospectiveJob", () => {
1842
1855
  });
1843
1856
  });
1844
1857
 
1858
+ test("replays the source's recorded wire tool surface verbatim on the fork wake", async () => {
1859
+ mockSourceToolSurface = [
1860
+ { name: "bash", description: "Run", input_schema: {} },
1861
+ { name: "remember", description: "Save", input_schema: {} },
1862
+ { name: "bell_jingle", description: "Ring", input_schema: {} },
1863
+ { type: "web_search_20250305", name: "web_search", max_uses: 5 },
1864
+ ];
1865
+
1866
+ await memoryRetrospectiveJob(makeJob(), stubConfig);
1867
+
1868
+ expect(toolSurfaceReads).toEqual(["src-conv-1"]);
1869
+ expect(wakeCalls).toHaveLength(1);
1870
+ // The exact recorded array rides to the wake; the fork sends it in place
1871
+ // of the surface it would resolve for itself.
1872
+ expect(wakeCalls[0]!.opts.wireToolDefinitions).toEqual(
1873
+ mockSourceToolSurface,
1874
+ );
1875
+ // The execution-side pin still rides alongside it.
1876
+ expect(wakeCalls[0]!.opts.toolGateMode).toBe("execution");
1877
+ expect(wakeCalls[0]!.opts.toolContextPin).toBeDefined();
1878
+ });
1879
+
1880
+ test("no recorded source surface → no wireToolDefinitions; the pin alone shapes the wire", async () => {
1881
+ mockSourceToolSurface = null;
1882
+
1883
+ await memoryRetrospectiveJob(makeJob(), stubConfig);
1884
+
1885
+ expect(toolSurfaceReads).toEqual(["src-conv-1"]);
1886
+ expect(wakeCalls).toHaveLength(1);
1887
+ expect("wireToolDefinitions" in wakeCalls[0]!.opts).toBe(false);
1888
+ expect(wakeCalls[0]!.opts.toolContextPin).toBeDefined();
1889
+ });
1890
+
1845
1891
  test("execution mode is unconditional → toolContextPin rides even without a resolved profile", async () => {
1846
1892
  await memoryRetrospectiveJob(
1847
1893
  makeJob(),