@lumenflow/cli 5.5.0 → 5.7.14

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 (229) hide show
  1. package/README.md +41 -40
  2. package/dist/db-journal-recover.js +400 -0
  3. package/dist/db-journal-recover.js.map +1 -0
  4. package/dist/docs-sync.js +8 -3
  5. package/dist/docs-sync.js.map +1 -1
  6. package/dist/doctor.js +11 -0
  7. package/dist/doctor.js.map +1 -1
  8. package/dist/gate-defaults.js +37 -0
  9. package/dist/gate-defaults.js.map +1 -1
  10. package/dist/gates/monolithic-file-contention-guard.js +167 -0
  11. package/dist/gates/monolithic-file-contention-guard.js.map +1 -0
  12. package/dist/gates/prod-migration-drift.js +207 -0
  13. package/dist/gates/prod-migration-drift.js.map +1 -0
  14. package/dist/gates/test-over-deletion-guard.js +280 -0
  15. package/dist/gates/test-over-deletion-guard.js.map +1 -0
  16. package/dist/gates-runners.js +44 -3
  17. package/dist/gates-runners.js.map +1 -1
  18. package/dist/gates.js +3 -2
  19. package/dist/gates.js.map +1 -1
  20. package/dist/hooks/config-resolver.js +16 -1
  21. package/dist/hooks/config-resolver.js.map +1 -1
  22. package/dist/hooks/dirty-guard.js +43 -2
  23. package/dist/hooks/dirty-guard.js.map +1 -1
  24. package/dist/hooks/git-status-parser.js +22 -8
  25. package/dist/hooks/git-status-parser.js.map +1 -1
  26. package/dist/init-templates.js +241 -0
  27. package/dist/init-templates.js.map +1 -1
  28. package/dist/init.js +122 -16
  29. package/dist/init.js.map +1 -1
  30. package/dist/lumenflow-setup.js +144 -0
  31. package/dist/lumenflow-setup.js.map +1 -0
  32. package/dist/lumenflow-upgrade.js +43 -1
  33. package/dist/lumenflow-upgrade.js.map +1 -1
  34. package/dist/mem-create.js +10 -1
  35. package/dist/mem-create.js.map +1 -1
  36. package/dist/mem-signal.js +21 -4
  37. package/dist/mem-signal.js.map +1 -1
  38. package/dist/orchestrate-initiative.js +28 -3
  39. package/dist/orchestrate-initiative.js.map +1 -1
  40. package/dist/public-manifest.js +17 -7
  41. package/dist/public-manifest.js.map +1 -1
  42. package/dist/release.js +53 -18
  43. package/dist/release.js.map +1 -1
  44. package/dist/wu-done-gates.js +13 -9
  45. package/dist/wu-done-gates.js.map +1 -1
  46. package/dist/wu-done.js +14 -2
  47. package/dist/wu-done.js.map +1 -1
  48. package/dist/wu-edit-operations.js +74 -0
  49. package/dist/wu-edit-operations.js.map +1 -1
  50. package/dist/wu-edit-validators.js +58 -0
  51. package/dist/wu-edit-validators.js.map +1 -1
  52. package/dist/wu-edit.js +106 -4
  53. package/dist/wu-edit.js.map +1 -1
  54. package/dist/wu-prep.js +57 -9
  55. package/dist/wu-prep.js.map +1 -1
  56. package/dist/wu-recover.js +6 -0
  57. package/dist/wu-recover.js.map +1 -1
  58. package/dist/wu-release.js +120 -2
  59. package/dist/wu-release.js.map +1 -1
  60. package/dist/wu-sizing-validation.js +47 -17
  61. package/dist/wu-sizing-validation.js.map +1 -1
  62. package/dist/wu-status.js +33 -0
  63. package/dist/wu-status.js.map +1 -1
  64. package/package.json +13 -12
  65. package/packs/agent-runtime/package.json +1 -1
  66. package/packs/sidekick/package.json +1 -1
  67. package/packs/software-delivery/package.json +1 -1
  68. package/templates/core/AGENTS.md.template +67 -3
  69. package/templates/core/LUMENFLOW.md.template +196 -47
  70. package/dist/distribution-preflight.js +0 -230
  71. package/dist/distribution-preflight.js.map +0 -1
  72. package/packs/agent-runtime/agent-heartbeat.ts +0 -163
  73. package/packs/agent-runtime/auto-session-integration.ts +0 -888
  74. package/packs/agent-runtime/capability-factory.ts +0 -104
  75. package/packs/agent-runtime/constants.ts +0 -21
  76. package/packs/agent-runtime/delegation-registry-schema.ts +0 -220
  77. package/packs/agent-runtime/delegation-registry-store.ts +0 -269
  78. package/packs/agent-runtime/delegation-tree.ts +0 -328
  79. package/packs/agent-runtime/index.ts +0 -20
  80. package/packs/agent-runtime/manifest.ts +0 -348
  81. package/packs/agent-runtime/memory-coordination-contract.ts +0 -86
  82. package/packs/agent-runtime/orchestration.ts +0 -2027
  83. package/packs/agent-runtime/pack-registration.ts +0 -110
  84. package/packs/agent-runtime/policy-factory.ts +0 -165
  85. package/packs/agent-runtime/remote-controls/index.ts +0 -7
  86. package/packs/agent-runtime/remote-controls/operations.ts +0 -405
  87. package/packs/agent-runtime/remote-controls/port.ts +0 -48
  88. package/packs/agent-runtime/remote-controls/state-store.ts +0 -258
  89. package/packs/agent-runtime/remote-controls/types.ts +0 -105
  90. package/packs/agent-runtime/session-schema.ts +0 -467
  91. package/packs/agent-runtime/tool-impl/agent-turn-tools.ts +0 -793
  92. package/packs/agent-runtime/tool-impl/index.ts +0 -6
  93. package/packs/agent-runtime/tool-impl/provider-adapters.ts +0 -1245
  94. package/packs/agent-runtime/tool-impl/remote-controls.mock.ts +0 -256
  95. package/packs/agent-runtime/tool-impl/remote-controls.ts +0 -273
  96. package/packs/agent-runtime/tools/index.ts +0 -4
  97. package/packs/agent-runtime/tools/types.ts +0 -47
  98. package/packs/agent-runtime/turn-lifecycle-events.ts +0 -590
  99. package/packs/agent-runtime/types.ts +0 -128
  100. package/packs/agent-runtime/vitest.config.ts +0 -11
  101. package/packs/sidekick/channel-ingress.ts +0 -137
  102. package/packs/sidekick/constants.ts +0 -10
  103. package/packs/sidekick/index.ts +0 -8
  104. package/packs/sidekick/manifest-schema.ts +0 -49
  105. package/packs/sidekick/manifest.ts +0 -512
  106. package/packs/sidekick/pack-registration.ts +0 -110
  107. package/packs/sidekick/policy-factory.ts +0 -38
  108. package/packs/sidekick/sidekick-events.ts +0 -694
  109. package/packs/sidekick/src/adapters/cloud-queue.ts +0 -101
  110. package/packs/sidekick/src/adapters/control-plane-bridge.adapter.ts +0 -386
  111. package/packs/sidekick/src/adapters/filesystem-bridge.adapter.ts +0 -228
  112. package/packs/sidekick/src/domain/channel.types.ts +0 -64
  113. package/packs/sidekick/src/ports/channel-bridge.port.ts +0 -92
  114. package/packs/sidekick/src/routines/commit.ts +0 -74
  115. package/packs/sidekick/tool-impl/channel-tools.ts +0 -577
  116. package/packs/sidekick/tool-impl/channel-transports.ts +0 -75
  117. package/packs/sidekick/tool-impl/index.ts +0 -29
  118. package/packs/sidekick/tool-impl/memory-tools.ts +0 -290
  119. package/packs/sidekick/tool-impl/routine-commit.ts +0 -102
  120. package/packs/sidekick/tool-impl/routine-tools.ts +0 -440
  121. package/packs/sidekick/tool-impl/runtime-context.ts +0 -28
  122. package/packs/sidekick/tool-impl/shared.ts +0 -125
  123. package/packs/sidekick/tool-impl/storage.ts +0 -325
  124. package/packs/sidekick/tool-impl/system-tools.ts +0 -160
  125. package/packs/sidekick/tool-impl/task-tools.ts +0 -506
  126. package/packs/sidekick/tools/channel-tools.ts +0 -53
  127. package/packs/sidekick/tools/index.ts +0 -9
  128. package/packs/sidekick/tools/memory-tools.ts +0 -53
  129. package/packs/sidekick/tools/routine-tools.ts +0 -53
  130. package/packs/sidekick/tools/system-tools.ts +0 -47
  131. package/packs/sidekick/tools/task-tools.ts +0 -61
  132. package/packs/sidekick/tools/types.ts +0 -57
  133. package/packs/sidekick/vitest.config.ts +0 -11
  134. package/packs/software-delivery/constants.ts +0 -10
  135. package/packs/software-delivery/extensions.ts +0 -140
  136. package/packs/software-delivery/gate-policies.ts +0 -134
  137. package/packs/software-delivery/index.ts +0 -8
  138. package/packs/software-delivery/manifest-schema.ts +0 -268
  139. package/packs/software-delivery/manifest.ts +0 -657
  140. package/packs/software-delivery/pack-registration.ts +0 -113
  141. package/packs/software-delivery/src/commands/index.ts +0 -5
  142. package/packs/software-delivery/src/config/delivery-review-contract.ts +0 -256
  143. package/packs/software-delivery/src/config/env-accessors.ts +0 -66
  144. package/packs/software-delivery/src/config/index.ts +0 -8
  145. package/packs/software-delivery/src/config/normalize-config-keys.ts +0 -9
  146. package/packs/software-delivery/src/config/schemas/lumenflow-config-schema-types.ts +0 -460
  147. package/packs/software-delivery/src/config/workspace-reader.ts +0 -375
  148. package/packs/software-delivery/src/constants/backlog-patterns.ts +0 -31
  149. package/packs/software-delivery/src/constants/client-ids.ts +0 -19
  150. package/packs/software-delivery/src/constants/config-contract.ts +0 -7
  151. package/packs/software-delivery/src/constants/docs-layout-presets.ts +0 -50
  152. package/packs/software-delivery/src/constants/duration-constants.ts +0 -20
  153. package/packs/software-delivery/src/constants/gate-constants.ts +0 -32
  154. package/packs/software-delivery/src/constants/index.ts +0 -29
  155. package/packs/software-delivery/src/constants/lock-constants.ts +0 -35
  156. package/packs/software-delivery/src/constants/object-guards.ts +0 -12
  157. package/packs/software-delivery/src/constants/section-headings.ts +0 -107
  158. package/packs/software-delivery/src/constants/wu-cli-constants.ts +0 -500
  159. package/packs/software-delivery/src/constants/wu-domain-constants.ts +0 -466
  160. package/packs/software-delivery/src/constants/wu-git-constants.ts +0 -7
  161. package/packs/software-delivery/src/constants/wu-id-format.ts +0 -327
  162. package/packs/software-delivery/src/constants/wu-paths-constants.ts +0 -384
  163. package/packs/software-delivery/src/constants/wu-statuses.ts +0 -287
  164. package/packs/software-delivery/src/constants/wu-type-helpers.ts +0 -67
  165. package/packs/software-delivery/src/constants/wu-ui-constants.ts +0 -267
  166. package/packs/software-delivery/src/constants/wu-validation-constants.ts +0 -73
  167. package/packs/software-delivery/src/domain/index.ts +0 -5
  168. package/packs/software-delivery/src/domain/orchestration.constants.ts +0 -166
  169. package/packs/software-delivery/src/domain/orchestration.schemas.ts +0 -238
  170. package/packs/software-delivery/src/domain/orchestration.types.ts +0 -176
  171. package/packs/software-delivery/src/methodology/incremental-test.ts +0 -122
  172. package/packs/software-delivery/src/methodology/index.ts +0 -6
  173. package/packs/software-delivery/src/methodology/manual-test-validator.ts +0 -292
  174. package/packs/software-delivery/src/policy/coverage-gate.ts +0 -270
  175. package/packs/software-delivery/src/policy/gates-agent-mode.ts +0 -223
  176. package/packs/software-delivery/src/policy/gates-config-internal.ts +0 -121
  177. package/packs/software-delivery/src/policy/gates-config.ts +0 -300
  178. package/packs/software-delivery/src/policy/gates-coverage.ts +0 -356
  179. package/packs/software-delivery/src/policy/gates-presets.ts +0 -134
  180. package/packs/software-delivery/src/policy/gates-schemas.ts +0 -173
  181. package/packs/software-delivery/src/policy/index.ts +0 -22
  182. package/packs/software-delivery/src/policy/package-manager-resolver.ts +0 -319
  183. package/packs/software-delivery/src/policy/resolve-policy.ts +0 -601
  184. package/packs/software-delivery/src/ports/config.ports.ts +0 -90
  185. package/packs/software-delivery/src/ports/dashboard-renderer.port.ts +0 -125
  186. package/packs/software-delivery/src/ports/index.ts +0 -10
  187. package/packs/software-delivery/src/ports/sync-validator.ports.ts +0 -59
  188. package/packs/software-delivery/src/ports/wu-helpers.ports.ts +0 -168
  189. package/packs/software-delivery/src/ports/wu-state.ports.ts +0 -241
  190. package/packs/software-delivery/src/primitives/index.ts +0 -5
  191. package/packs/software-delivery/src/runtime/index.ts +0 -6
  192. package/packs/software-delivery/src/runtime/work-classifier.ts +0 -561
  193. package/packs/software-delivery/src/sandbox/index.ts +0 -10
  194. package/packs/software-delivery/src/sandbox/sandbox-allowlist.ts +0 -118
  195. package/packs/software-delivery/src/sandbox/sandbox-backend-linux.ts +0 -88
  196. package/packs/software-delivery/src/sandbox/sandbox-backend-macos.ts +0 -154
  197. package/packs/software-delivery/src/sandbox/sandbox-backend-windows.ts +0 -47
  198. package/packs/software-delivery/src/sandbox/sandbox-profile.ts +0 -153
  199. package/packs/software-delivery/src/schemas/index.ts +0 -5
  200. package/packs/software-delivery/src/state/date-utils.ts +0 -158
  201. package/packs/software-delivery/src/state/index.ts +0 -15
  202. package/packs/software-delivery/src/state/state-machine.ts +0 -119
  203. package/packs/software-delivery/src/state/wu-doc-types.ts +0 -51
  204. package/packs/software-delivery/src/state/wu-paths.ts +0 -381
  205. package/packs/software-delivery/src/state/wu-schema.ts +0 -1139
  206. package/packs/software-delivery/src/state/wu-state-schema.ts +0 -255
  207. package/packs/software-delivery/src/state/wu-yaml.ts +0 -338
  208. package/packs/software-delivery/tool-impl/agent-tools.ts +0 -263
  209. package/packs/software-delivery/tool-impl/delegation-tools.ts +0 -66
  210. package/packs/software-delivery/tool-impl/flow-metrics-tools.ts +0 -219
  211. package/packs/software-delivery/tool-impl/git-runner.ts +0 -113
  212. package/packs/software-delivery/tool-impl/git-tools.ts +0 -316
  213. package/packs/software-delivery/tool-impl/index.ts +0 -15
  214. package/packs/software-delivery/tool-impl/initiative-orchestration-tools.ts +0 -720
  215. package/packs/software-delivery/tool-impl/lane-lock.ts +0 -246
  216. package/packs/software-delivery/tool-impl/memory-tools.ts +0 -470
  217. package/packs/software-delivery/tool-impl/pending-runtime-tools.ts +0 -21
  218. package/packs/software-delivery/tool-impl/runtime-cli-adapter.ts +0 -329
  219. package/packs/software-delivery/tool-impl/runtime-native-tools.ts +0 -687
  220. package/packs/software-delivery/tool-impl/worker-loader.ts +0 -52
  221. package/packs/software-delivery/tool-impl/worktree-tools.ts +0 -46
  222. package/packs/software-delivery/tool-impl/wu-lifecycle-tools.ts +0 -807
  223. package/packs/software-delivery/tools/delegation-tools.ts +0 -23
  224. package/packs/software-delivery/tools/git-tools.ts +0 -55
  225. package/packs/software-delivery/tools/index.ts +0 -8
  226. package/packs/software-delivery/tools/lane-lock-tool.ts +0 -37
  227. package/packs/software-delivery/tools/types.ts +0 -71
  228. package/packs/software-delivery/tools/worktree-tools.ts +0 -49
  229. package/packs/software-delivery/vitest.config.ts +0 -11
@@ -1,325 +0,0 @@
1
- // Copyright (c) 2026 Hellmai Ltd
2
- // SPDX-License-Identifier: LicenseRef-LumenFlow-Proprietary
3
-
4
- import { randomBytes } from 'node:crypto';
5
- import { appendFile, mkdir, open, readFile, rename, rm, stat, writeFile } from 'node:fs/promises';
6
- import path from 'node:path';
7
- import { getSidekickRuntimeContext, runWithSidekickRuntimeContext } from './runtime-context.js';
8
-
9
- // ---------------------------------------------------------------------------
10
- // Constants
11
- // ---------------------------------------------------------------------------
12
-
13
- const UTF8_ENCODING = 'utf8';
14
- const DEFAULT_LOCK_TIMEOUT_MS = 10_000;
15
- const LOCK_RETRY_INTERVAL_MS = 25;
16
- const STALE_LOCK_MS = 30_000;
17
- const RANDOM_BYTES_LENGTH = 4;
18
-
19
- // ---------------------------------------------------------------------------
20
- // Domain types
21
- // ---------------------------------------------------------------------------
22
-
23
- export type TaskPriority = 'P0' | 'P1' | 'P2' | 'P3';
24
- export type TaskStatus = 'pending' | 'done' | 'canceled';
25
- export type MemoryType = 'fact' | 'preference' | 'note' | 'snippet';
26
-
27
- export interface TaskRecord {
28
- id: string;
29
- title: string;
30
- description?: string;
31
- priority: TaskPriority;
32
- status: TaskStatus;
33
- tags: string[];
34
- due_at?: string;
35
- cron?: string;
36
- note?: string;
37
- created_at: string;
38
- updated_at: string;
39
- completed_at?: string;
40
- canceled_at?: string;
41
- }
42
-
43
- export interface MemoryRecord {
44
- id: string;
45
- type: MemoryType;
46
- content: string;
47
- tags: string[];
48
- created_at: string;
49
- updated_at: string;
50
- }
51
-
52
- export interface ChannelRecord {
53
- id: string;
54
- name: string;
55
- created_at: string;
56
- updated_at: string;
57
- }
58
-
59
- export interface ChannelMessageRecord {
60
- id: string;
61
- channel_id: string;
62
- sender: string;
63
- content: string;
64
- created_at: string;
65
- }
66
-
67
- export interface RoutineStepRecord {
68
- tool: string;
69
- input: Record<string, unknown>;
70
- }
71
-
72
- export interface RoutineRecord {
73
- id: string;
74
- name: string;
75
- steps: RoutineStepRecord[];
76
- cron?: string;
77
- enabled: boolean;
78
- created_at: string;
79
- updated_at: string;
80
- }
81
-
82
- export interface SidekickStores {
83
- tasks: TaskRecord[];
84
- memories: MemoryRecord[];
85
- channels: ChannelRecord[];
86
- messages: ChannelMessageRecord[];
87
- routines: RoutineRecord[];
88
- }
89
-
90
- export type StoreName = keyof SidekickStores;
91
-
92
- // ---------------------------------------------------------------------------
93
- // Audit types
94
- // ---------------------------------------------------------------------------
95
-
96
- export interface AuditEvent {
97
- id: string;
98
- ts: string;
99
- tool: string;
100
- op: 'create' | 'read' | 'update' | 'delete' | 'execute' | 'export';
101
- actor?: string;
102
- ids?: string[];
103
- details?: Record<string, unknown>;
104
- }
105
-
106
- // ---------------------------------------------------------------------------
107
- // StoragePort (hexagonal port)
108
- // ---------------------------------------------------------------------------
109
-
110
- export interface StoragePort {
111
- getRootDir(): string;
112
- withLock<T>(fn: () => Promise<T>): Promise<T>;
113
- readStore<K extends StoreName>(store: K): Promise<SidekickStores[K]>;
114
- writeStore<K extends StoreName>(store: K, data: SidekickStores[K]): Promise<void>;
115
- appendAudit(event: AuditEvent): Promise<void>;
116
- readAuditEvents(): Promise<AuditEvent[]>;
117
- }
118
-
119
- // ---------------------------------------------------------------------------
120
- // File-path mapping
121
- // ---------------------------------------------------------------------------
122
-
123
- const STORE_FILE_PATHS: Record<StoreName, string> = {
124
- tasks: 'tasks/tasks.json',
125
- memories: 'memory/memories.json',
126
- channels: 'channels/channels.json',
127
- messages: 'channels/messages.json',
128
- routines: 'routines/routines.json',
129
- };
130
-
131
- const STORE_DEFAULTS: SidekickStores = {
132
- tasks: [],
133
- memories: [],
134
- channels: [],
135
- messages: [],
136
- routines: [],
137
- };
138
-
139
- const AUDIT_FILE_PATH = 'audit/events.jsonl';
140
- const LOCK_FILE_PATH = '.lock';
141
-
142
- // ---------------------------------------------------------------------------
143
- // Internal helpers
144
- // ---------------------------------------------------------------------------
145
-
146
- function cloneStore<K extends StoreName>(store: K, value: SidekickStores[K]): SidekickStores[K] {
147
- return structuredClone(value) as SidekickStores[K];
148
- }
149
-
150
- function randomToken(): string {
151
- return randomBytes(RANDOM_BYTES_LENGTH).toString('hex');
152
- }
153
-
154
- function nowMs(): number {
155
- return Date.now();
156
- }
157
-
158
- function sleep(ms: number): Promise<void> {
159
- return new Promise((resolve) => {
160
- setTimeout(resolve, ms);
161
- });
162
- }
163
-
164
- async function ensureParentDir(filePath: string): Promise<void> {
165
- await mkdir(path.dirname(filePath), { recursive: true });
166
- }
167
-
168
- async function writeJsonAtomic(filePath: string, data: unknown): Promise<void> {
169
- await ensureParentDir(filePath);
170
- const tmpPath = `${filePath}.tmp-${process.pid}-${randomToken()}`;
171
- await writeFile(tmpPath, `${JSON.stringify(data, null, 2)}\n`, UTF8_ENCODING);
172
- await rename(tmpPath, filePath);
173
- }
174
-
175
- async function readJsonFile<T>(filePath: string, fallback: T): Promise<T> {
176
- try {
177
- const raw = await readFile(filePath, UTF8_ENCODING);
178
- return JSON.parse(raw) as T;
179
- } catch (error) {
180
- const nodeError = error as NodeJS.ErrnoException;
181
- if (nodeError.code === 'ENOENT') {
182
- await writeJsonAtomic(filePath, fallback);
183
- return fallback;
184
- }
185
- throw error;
186
- }
187
- }
188
-
189
- async function maybeRemoveStaleLock(lockPath: string): Promise<void> {
190
- try {
191
- const metadata = await stat(lockPath);
192
- const ageMs = nowMs() - metadata.mtimeMs;
193
- if (ageMs > STALE_LOCK_MS) {
194
- await rm(lockPath, { force: true });
195
- }
196
- } catch {
197
- // Ignore races for lock cleanup.
198
- }
199
- }
200
-
201
- async function withFileLock<T>(lockPath: string, fn: () => Promise<T>): Promise<T> {
202
- const deadline = nowMs() + DEFAULT_LOCK_TIMEOUT_MS;
203
- let handle: Awaited<ReturnType<typeof open>> | null = null;
204
-
205
- while (nowMs() < deadline) {
206
- try {
207
- await ensureParentDir(lockPath);
208
- handle = await open(lockPath, 'wx');
209
- await handle.writeFile(`${process.pid}:${nowMs()}`, UTF8_ENCODING);
210
- break;
211
- } catch (error) {
212
- const nodeError = error as NodeJS.ErrnoException;
213
- if (nodeError.code !== 'EEXIST') {
214
- throw error;
215
- }
216
- await maybeRemoveStaleLock(lockPath);
217
- await sleep(LOCK_RETRY_INTERVAL_MS);
218
- }
219
- }
220
-
221
- if (!handle) {
222
- throw new Error(`Timed out waiting for sidekick storage lock at ${lockPath}.`);
223
- }
224
-
225
- try {
226
- return await fn();
227
- } finally {
228
- await handle.close();
229
- await rm(lockPath, { force: true });
230
- }
231
- }
232
-
233
- // ---------------------------------------------------------------------------
234
- // FsStoragePort (filesystem adapter)
235
- // ---------------------------------------------------------------------------
236
-
237
- export class FsStoragePort implements StoragePort {
238
- private readonly rootDir: string;
239
-
240
- constructor(rootDir = path.resolve(process.cwd(), '.sidekick')) {
241
- this.rootDir = rootDir;
242
- }
243
-
244
- getRootDir(): string {
245
- return this.rootDir;
246
- }
247
-
248
- async withLock<T>(fn: () => Promise<T>): Promise<T> {
249
- return withFileLock(path.join(this.rootDir, LOCK_FILE_PATH), fn);
250
- }
251
-
252
- async readStore<K extends StoreName>(store: K): Promise<SidekickStores[K]> {
253
- const storePath = path.join(this.rootDir, STORE_FILE_PATHS[store]);
254
- const fallback = cloneStore(store, STORE_DEFAULTS[store]);
255
- const data = await readJsonFile(storePath, fallback);
256
- return cloneStore(store, data);
257
- }
258
-
259
- async writeStore<K extends StoreName>(store: K, data: SidekickStores[K]): Promise<void> {
260
- const storePath = path.join(this.rootDir, STORE_FILE_PATHS[store]);
261
- await writeJsonAtomic(storePath, data);
262
- }
263
-
264
- async appendAudit(event: AuditEvent): Promise<void> {
265
- const auditPath = path.join(this.rootDir, AUDIT_FILE_PATH);
266
- await ensureParentDir(auditPath);
267
- await appendFile(auditPath, `${JSON.stringify(event)}\n`, UTF8_ENCODING);
268
- }
269
-
270
- async readAuditEvents(): Promise<AuditEvent[]> {
271
- const auditPath = path.join(this.rootDir, AUDIT_FILE_PATH);
272
- let raw: string;
273
- try {
274
- raw = await readFile(auditPath, UTF8_ENCODING);
275
- } catch (error) {
276
- const nodeError = error as NodeJS.ErrnoException;
277
- if (nodeError.code === 'ENOENT') {
278
- return [];
279
- }
280
- throw error;
281
- }
282
-
283
- const events: AuditEvent[] = [];
284
- for (const line of raw.split('\n')) {
285
- const trimmed = line.trim();
286
- if (trimmed.length === 0) {
287
- continue;
288
- }
289
- try {
290
- events.push(JSON.parse(trimmed) as AuditEvent);
291
- } catch {
292
- // Skip malformed audit lines and keep the stream readable.
293
- }
294
- }
295
- return events;
296
- }
297
- }
298
-
299
- // ---------------------------------------------------------------------------
300
- // Injection helpers (AsyncLocalStorage-based)
301
- // ---------------------------------------------------------------------------
302
-
303
- let defaultStoragePort: StoragePort = new FsStoragePort();
304
-
305
- export function setDefaultStoragePort(port: StoragePort): void {
306
- defaultStoragePort = port;
307
- }
308
-
309
- export function getStoragePort(): StoragePort {
310
- return getSidekickRuntimeContext()?.storagePort ?? defaultStoragePort;
311
- }
312
-
313
- export async function runWithStoragePort<T>(port: StoragePort, fn: () => Promise<T>): Promise<T> {
314
- const existingContext = getSidekickRuntimeContext();
315
- return runWithSidekickRuntimeContext(
316
- {
317
- storagePort: port,
318
- channelTransports: existingContext?.channelTransports ?? new Map(),
319
- workspaceRoot: existingContext?.workspaceRoot,
320
- workspaceConfig: existingContext?.workspaceConfig,
321
- eventSink: existingContext?.eventSink,
322
- },
323
- fn,
324
- );
325
- }
@@ -1,160 +0,0 @@
1
- // Copyright (c) 2026 Hellmai Ltd
2
- // SPDX-License-Identifier: LicenseRef-LumenFlow-Proprietary
3
-
4
- import { getStoragePort } from './storage.js';
5
- import {
6
- asNonEmptyString,
7
- buildAuditEvent,
8
- failure,
9
- nowIso,
10
- success,
11
- toRecord,
12
- type ToolContextLike,
13
- type ToolOutput,
14
- } from './shared.js';
15
- import { emitSidekickStateRehydration, snapshotSidekickState } from '../sidekick-events.js';
16
-
17
- // ---------------------------------------------------------------------------
18
- // Constants
19
- // ---------------------------------------------------------------------------
20
-
21
- const TOOL_NAMES = {
22
- INIT: 'sidekick:init',
23
- STATUS: 'sidekick:status',
24
- EXPORT: 'sidekick:export',
25
- } as const;
26
-
27
- const STORE_NAMES = ['tasks', 'memories', 'channels', 'messages', 'routines'] as const;
28
-
29
- // ---------------------------------------------------------------------------
30
- // sidekick:init (idempotent)
31
- // ---------------------------------------------------------------------------
32
-
33
- async function initTool(context?: ToolContextLike): Promise<ToolOutput> {
34
- const storage = getStoragePort();
35
-
36
- // Touch every store to ensure directories and files are created
37
- for (const store of STORE_NAMES) {
38
- await storage.readStore(store);
39
- }
40
-
41
- await storage.appendAudit(
42
- buildAuditEvent({
43
- tool: TOOL_NAMES.INIT,
44
- op: 'create',
45
- context,
46
- details: { root_dir: storage.getRootDir() },
47
- }),
48
- );
49
-
50
- // WU-2830 (INIT-062 WU-D): chunked rehydration replaces the monolithic
51
- // `state_rehydrated` emission — unbounded snapshots cannot stream.
52
- await emitSidekickStateRehydration(await snapshotSidekickState());
53
-
54
- return success({
55
- initialized: true,
56
- root_dir: storage.getRootDir(),
57
- });
58
- }
59
-
60
- // ---------------------------------------------------------------------------
61
- // sidekick:status
62
- // ---------------------------------------------------------------------------
63
-
64
- async function statusTool(context?: ToolContextLike): Promise<ToolOutput> {
65
- const storage = getStoragePort();
66
- const [tasks, memories, channels, messages, routines, audit] = await Promise.all([
67
- storage.readStore('tasks'),
68
- storage.readStore('memories'),
69
- storage.readStore('channels'),
70
- storage.readStore('messages'),
71
- storage.readStore('routines'),
72
- storage.readAuditEvents(),
73
- ]);
74
-
75
- const pendingTasks = tasks.filter((task) => task.status === 'pending').length;
76
- const completedTasks = tasks.filter((task) => task.status === 'done').length;
77
-
78
- await storage.appendAudit(
79
- buildAuditEvent({
80
- tool: TOOL_NAMES.STATUS,
81
- op: 'read',
82
- context,
83
- details: { task_count: tasks.length },
84
- }),
85
- );
86
-
87
- return success({
88
- task_count: tasks.length,
89
- pending_tasks: pendingTasks,
90
- completed_tasks: completedTasks,
91
- memory_entries: memories.length,
92
- channels: channels.length,
93
- messages: messages.length,
94
- routines: routines.length,
95
- audit_events: audit.length,
96
- });
97
- }
98
-
99
- // ---------------------------------------------------------------------------
100
- // sidekick:export (READ-ONLY -- returns data, no file write)
101
- // ---------------------------------------------------------------------------
102
-
103
- async function exportTool(input: unknown, context?: ToolContextLike): Promise<ToolOutput> {
104
- const parsed = toRecord(input);
105
- const includeAudit = parsed.include_audit !== false;
106
-
107
- const storage = getStoragePort();
108
- const [tasks, memories, channels, messages, routines, audit] = await Promise.all([
109
- storage.readStore('tasks'),
110
- storage.readStore('memories'),
111
- storage.readStore('channels'),
112
- storage.readStore('messages'),
113
- storage.readStore('routines'),
114
- storage.readAuditEvents(),
115
- ]);
116
-
117
- await storage.appendAudit(
118
- buildAuditEvent({
119
- tool: TOOL_NAMES.EXPORT,
120
- op: 'export',
121
- context,
122
- details: { include_audit: includeAudit },
123
- }),
124
- );
125
-
126
- return success({
127
- exported_at: nowIso(),
128
- version: '0.1.0',
129
- data: {
130
- tasks,
131
- memories,
132
- channels,
133
- messages,
134
- routines,
135
- ...(includeAudit ? { audit } : {}),
136
- },
137
- });
138
- }
139
-
140
- // ---------------------------------------------------------------------------
141
- // Router (default export)
142
- // ---------------------------------------------------------------------------
143
-
144
- export default async function systemTools(
145
- input: unknown,
146
- context?: ToolContextLike,
147
- ): Promise<ToolOutput> {
148
- const toolName = asNonEmptyString(context?.tool_name) ?? '';
149
-
150
- switch (toolName) {
151
- case TOOL_NAMES.INIT:
152
- return initTool(context);
153
- case TOOL_NAMES.STATUS:
154
- return statusTool(context);
155
- case TOOL_NAMES.EXPORT:
156
- return exportTool(input, context);
157
- default:
158
- return failure('UNKNOWN_TOOL', `Unknown system tool: ${toolName || 'unknown'}`);
159
- }
160
- }