@lumenflow/cli 5.4.0 → 5.7.12

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 (227) hide show
  1. package/README.md +42 -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/gate-defaults.js +191 -9
  7. package/dist/gate-defaults.js.map +1 -1
  8. package/dist/gate-registry.js.map +1 -1
  9. package/dist/gates/monolithic-file-contention-guard.js +167 -0
  10. package/dist/gates/monolithic-file-contention-guard.js.map +1 -0
  11. package/dist/gates/prod-migration-drift.js +207 -0
  12. package/dist/gates/prod-migration-drift.js.map +1 -0
  13. package/dist/gates/test-over-deletion-guard.js +255 -0
  14. package/dist/gates/test-over-deletion-guard.js.map +1 -0
  15. package/dist/gates-runners.js +401 -2
  16. package/dist/gates-runners.js.map +1 -1
  17. package/dist/gates.js +349 -4
  18. package/dist/gates.js.map +1 -1
  19. package/dist/lumenflow-setup.js +144 -0
  20. package/dist/lumenflow-setup.js.map +1 -0
  21. package/dist/lumenflow-upgrade.js +2 -1
  22. package/dist/lumenflow-upgrade.js.map +1 -1
  23. package/dist/mem-create.js +10 -1
  24. package/dist/mem-create.js.map +1 -1
  25. package/dist/mem-signal.js +21 -4
  26. package/dist/mem-signal.js.map +1 -1
  27. package/dist/metrics-cli.js +19 -2
  28. package/dist/metrics-cli.js.map +1 -1
  29. package/dist/metrics-snapshot.js +25 -2
  30. package/dist/metrics-snapshot.js.map +1 -1
  31. package/dist/orchestrate-initiative.js +28 -3
  32. package/dist/orchestrate-initiative.js.map +1 -1
  33. package/dist/public-manifest.js +17 -0
  34. package/dist/public-manifest.js.map +1 -1
  35. package/dist/release.js +53 -18
  36. package/dist/release.js.map +1 -1
  37. package/dist/wu-done-gates.js +121 -8
  38. package/dist/wu-done-gates.js.map +1 -1
  39. package/dist/wu-done.js +30 -6
  40. package/dist/wu-done.js.map +1 -1
  41. package/dist/wu-edit-operations.js +74 -0
  42. package/dist/wu-edit-operations.js.map +1 -1
  43. package/dist/wu-edit-validators.js +58 -0
  44. package/dist/wu-edit-validators.js.map +1 -1
  45. package/dist/wu-edit.js +106 -4
  46. package/dist/wu-edit.js.map +1 -1
  47. package/dist/wu-prep.js +132 -8
  48. package/dist/wu-prep.js.map +1 -1
  49. package/dist/wu-recover.js +6 -0
  50. package/dist/wu-recover.js.map +1 -1
  51. package/dist/wu-release.js +120 -2
  52. package/dist/wu-release.js.map +1 -1
  53. package/dist/wu-sizing-validation.js +47 -17
  54. package/dist/wu-sizing-validation.js.map +1 -1
  55. package/dist/wu-status.js +33 -0
  56. package/dist/wu-status.js.map +1 -1
  57. package/package.json +13 -11
  58. package/packs/agent-runtime/package.json +1 -1
  59. package/packs/sidekick/package.json +1 -1
  60. package/packs/software-delivery/package.json +1 -1
  61. package/templates/core/AGENTS.md.template +162 -26
  62. package/templates/core/LUMENFLOW.md.template +381 -70
  63. package/templates/core/ai/onboarding/agent-invocation-guide.md.template +0 -5
  64. package/templates/core/ai/onboarding/agent-safety-card.md.template +63 -17
  65. package/templates/core/ai/onboarding/initiative-orchestration.md.template +4 -0
  66. package/templates/core/ai/onboarding/release-process.md.template +7 -7
  67. package/templates/core/ai/onboarding/vendor-support.md.template +74 -10
  68. package/templates/vendors/claude/.claude/skills/frontend-design/SKILL.md.template +1 -1
  69. package/templates/vendors/claude/.claude/skills/wu-lifecycle/SKILL.md.template +28 -0
  70. package/packs/agent-runtime/agent-heartbeat.ts +0 -163
  71. package/packs/agent-runtime/auto-session-integration.ts +0 -888
  72. package/packs/agent-runtime/capability-factory.ts +0 -104
  73. package/packs/agent-runtime/constants.ts +0 -21
  74. package/packs/agent-runtime/delegation-registry-schema.ts +0 -220
  75. package/packs/agent-runtime/delegation-registry-store.ts +0 -269
  76. package/packs/agent-runtime/delegation-tree.ts +0 -328
  77. package/packs/agent-runtime/index.ts +0 -20
  78. package/packs/agent-runtime/manifest.ts +0 -348
  79. package/packs/agent-runtime/memory-coordination-contract.ts +0 -86
  80. package/packs/agent-runtime/orchestration.ts +0 -2027
  81. package/packs/agent-runtime/pack-registration.ts +0 -110
  82. package/packs/agent-runtime/policy-factory.ts +0 -165
  83. package/packs/agent-runtime/remote-controls/index.ts +0 -7
  84. package/packs/agent-runtime/remote-controls/operations.ts +0 -405
  85. package/packs/agent-runtime/remote-controls/port.ts +0 -48
  86. package/packs/agent-runtime/remote-controls/state-store.ts +0 -258
  87. package/packs/agent-runtime/remote-controls/types.ts +0 -105
  88. package/packs/agent-runtime/session-schema.ts +0 -467
  89. package/packs/agent-runtime/tool-impl/agent-turn-tools.ts +0 -793
  90. package/packs/agent-runtime/tool-impl/index.ts +0 -6
  91. package/packs/agent-runtime/tool-impl/provider-adapters.ts +0 -1245
  92. package/packs/agent-runtime/tool-impl/remote-controls.mock.ts +0 -256
  93. package/packs/agent-runtime/tool-impl/remote-controls.ts +0 -273
  94. package/packs/agent-runtime/tools/index.ts +0 -4
  95. package/packs/agent-runtime/tools/types.ts +0 -47
  96. package/packs/agent-runtime/turn-lifecycle-events.ts +0 -590
  97. package/packs/agent-runtime/types.ts +0 -128
  98. package/packs/agent-runtime/vitest.config.ts +0 -11
  99. package/packs/sidekick/channel-ingress.ts +0 -137
  100. package/packs/sidekick/constants.ts +0 -10
  101. package/packs/sidekick/index.ts +0 -8
  102. package/packs/sidekick/manifest-schema.ts +0 -49
  103. package/packs/sidekick/manifest.ts +0 -512
  104. package/packs/sidekick/pack-registration.ts +0 -110
  105. package/packs/sidekick/policy-factory.ts +0 -38
  106. package/packs/sidekick/sidekick-events.ts +0 -694
  107. package/packs/sidekick/src/adapters/cloud-queue.ts +0 -101
  108. package/packs/sidekick/src/adapters/control-plane-bridge.adapter.ts +0 -386
  109. package/packs/sidekick/src/adapters/filesystem-bridge.adapter.ts +0 -228
  110. package/packs/sidekick/src/domain/channel.types.ts +0 -64
  111. package/packs/sidekick/src/ports/channel-bridge.port.ts +0 -92
  112. package/packs/sidekick/src/routines/commit.ts +0 -74
  113. package/packs/sidekick/tool-impl/channel-tools.ts +0 -577
  114. package/packs/sidekick/tool-impl/channel-transports.ts +0 -75
  115. package/packs/sidekick/tool-impl/index.ts +0 -29
  116. package/packs/sidekick/tool-impl/memory-tools.ts +0 -290
  117. package/packs/sidekick/tool-impl/routine-commit.ts +0 -102
  118. package/packs/sidekick/tool-impl/routine-tools.ts +0 -440
  119. package/packs/sidekick/tool-impl/runtime-context.ts +0 -28
  120. package/packs/sidekick/tool-impl/shared.ts +0 -125
  121. package/packs/sidekick/tool-impl/storage.ts +0 -325
  122. package/packs/sidekick/tool-impl/system-tools.ts +0 -160
  123. package/packs/sidekick/tool-impl/task-tools.ts +0 -506
  124. package/packs/sidekick/tools/channel-tools.ts +0 -53
  125. package/packs/sidekick/tools/index.ts +0 -9
  126. package/packs/sidekick/tools/memory-tools.ts +0 -53
  127. package/packs/sidekick/tools/routine-tools.ts +0 -53
  128. package/packs/sidekick/tools/system-tools.ts +0 -47
  129. package/packs/sidekick/tools/task-tools.ts +0 -61
  130. package/packs/sidekick/tools/types.ts +0 -57
  131. package/packs/sidekick/vitest.config.ts +0 -11
  132. package/packs/software-delivery/constants.ts +0 -10
  133. package/packs/software-delivery/extensions.ts +0 -140
  134. package/packs/software-delivery/gate-policies.ts +0 -134
  135. package/packs/software-delivery/index.ts +0 -8
  136. package/packs/software-delivery/manifest-schema.ts +0 -268
  137. package/packs/software-delivery/manifest.ts +0 -657
  138. package/packs/software-delivery/pack-registration.ts +0 -113
  139. package/packs/software-delivery/src/commands/index.ts +0 -5
  140. package/packs/software-delivery/src/config/delivery-review-contract.ts +0 -256
  141. package/packs/software-delivery/src/config/env-accessors.ts +0 -66
  142. package/packs/software-delivery/src/config/index.ts +0 -8
  143. package/packs/software-delivery/src/config/normalize-config-keys.ts +0 -9
  144. package/packs/software-delivery/src/config/schemas/lumenflow-config-schema-types.ts +0 -460
  145. package/packs/software-delivery/src/config/workspace-reader.ts +0 -375
  146. package/packs/software-delivery/src/constants/backlog-patterns.ts +0 -31
  147. package/packs/software-delivery/src/constants/client-ids.ts +0 -19
  148. package/packs/software-delivery/src/constants/config-contract.ts +0 -7
  149. package/packs/software-delivery/src/constants/docs-layout-presets.ts +0 -50
  150. package/packs/software-delivery/src/constants/duration-constants.ts +0 -20
  151. package/packs/software-delivery/src/constants/gate-constants.ts +0 -32
  152. package/packs/software-delivery/src/constants/index.ts +0 -29
  153. package/packs/software-delivery/src/constants/lock-constants.ts +0 -35
  154. package/packs/software-delivery/src/constants/object-guards.ts +0 -12
  155. package/packs/software-delivery/src/constants/section-headings.ts +0 -107
  156. package/packs/software-delivery/src/constants/wu-cli-constants.ts +0 -488
  157. package/packs/software-delivery/src/constants/wu-domain-constants.ts +0 -466
  158. package/packs/software-delivery/src/constants/wu-git-constants.ts +0 -7
  159. package/packs/software-delivery/src/constants/wu-id-format.ts +0 -327
  160. package/packs/software-delivery/src/constants/wu-paths-constants.ts +0 -384
  161. package/packs/software-delivery/src/constants/wu-statuses.ts +0 -287
  162. package/packs/software-delivery/src/constants/wu-type-helpers.ts +0 -67
  163. package/packs/software-delivery/src/constants/wu-ui-constants.ts +0 -267
  164. package/packs/software-delivery/src/constants/wu-validation-constants.ts +0 -73
  165. package/packs/software-delivery/src/domain/index.ts +0 -5
  166. package/packs/software-delivery/src/domain/orchestration.constants.ts +0 -166
  167. package/packs/software-delivery/src/domain/orchestration.schemas.ts +0 -238
  168. package/packs/software-delivery/src/domain/orchestration.types.ts +0 -176
  169. package/packs/software-delivery/src/methodology/incremental-test.ts +0 -122
  170. package/packs/software-delivery/src/methodology/index.ts +0 -6
  171. package/packs/software-delivery/src/methodology/manual-test-validator.ts +0 -292
  172. package/packs/software-delivery/src/policy/coverage-gate.ts +0 -270
  173. package/packs/software-delivery/src/policy/gates-agent-mode.ts +0 -223
  174. package/packs/software-delivery/src/policy/gates-config-internal.ts +0 -121
  175. package/packs/software-delivery/src/policy/gates-config.ts +0 -300
  176. package/packs/software-delivery/src/policy/gates-coverage.ts +0 -356
  177. package/packs/software-delivery/src/policy/gates-presets.ts +0 -134
  178. package/packs/software-delivery/src/policy/gates-schemas.ts +0 -173
  179. package/packs/software-delivery/src/policy/index.ts +0 -22
  180. package/packs/software-delivery/src/policy/package-manager-resolver.ts +0 -319
  181. package/packs/software-delivery/src/policy/resolve-policy.ts +0 -601
  182. package/packs/software-delivery/src/ports/config.ports.ts +0 -90
  183. package/packs/software-delivery/src/ports/dashboard-renderer.port.ts +0 -125
  184. package/packs/software-delivery/src/ports/index.ts +0 -10
  185. package/packs/software-delivery/src/ports/sync-validator.ports.ts +0 -59
  186. package/packs/software-delivery/src/ports/wu-helpers.ports.ts +0 -168
  187. package/packs/software-delivery/src/ports/wu-state.ports.ts +0 -241
  188. package/packs/software-delivery/src/primitives/index.ts +0 -5
  189. package/packs/software-delivery/src/runtime/index.ts +0 -6
  190. package/packs/software-delivery/src/runtime/work-classifier.ts +0 -561
  191. package/packs/software-delivery/src/sandbox/index.ts +0 -10
  192. package/packs/software-delivery/src/sandbox/sandbox-allowlist.ts +0 -118
  193. package/packs/software-delivery/src/sandbox/sandbox-backend-linux.ts +0 -88
  194. package/packs/software-delivery/src/sandbox/sandbox-backend-macos.ts +0 -154
  195. package/packs/software-delivery/src/sandbox/sandbox-backend-windows.ts +0 -47
  196. package/packs/software-delivery/src/sandbox/sandbox-profile.ts +0 -153
  197. package/packs/software-delivery/src/schemas/index.ts +0 -5
  198. package/packs/software-delivery/src/state/date-utils.ts +0 -158
  199. package/packs/software-delivery/src/state/index.ts +0 -15
  200. package/packs/software-delivery/src/state/state-machine.ts +0 -119
  201. package/packs/software-delivery/src/state/wu-doc-types.ts +0 -51
  202. package/packs/software-delivery/src/state/wu-paths.ts +0 -381
  203. package/packs/software-delivery/src/state/wu-schema.ts +0 -1139
  204. package/packs/software-delivery/src/state/wu-state-schema.ts +0 -255
  205. package/packs/software-delivery/src/state/wu-yaml.ts +0 -338
  206. package/packs/software-delivery/tool-impl/agent-tools.ts +0 -263
  207. package/packs/software-delivery/tool-impl/delegation-tools.ts +0 -66
  208. package/packs/software-delivery/tool-impl/flow-metrics-tools.ts +0 -219
  209. package/packs/software-delivery/tool-impl/git-runner.ts +0 -113
  210. package/packs/software-delivery/tool-impl/git-tools.ts +0 -316
  211. package/packs/software-delivery/tool-impl/index.ts +0 -15
  212. package/packs/software-delivery/tool-impl/initiative-orchestration-tools.ts +0 -720
  213. package/packs/software-delivery/tool-impl/lane-lock.ts +0 -246
  214. package/packs/software-delivery/tool-impl/memory-tools.ts +0 -470
  215. package/packs/software-delivery/tool-impl/pending-runtime-tools.ts +0 -21
  216. package/packs/software-delivery/tool-impl/runtime-cli-adapter.ts +0 -329
  217. package/packs/software-delivery/tool-impl/runtime-native-tools.ts +0 -687
  218. package/packs/software-delivery/tool-impl/worker-loader.ts +0 -52
  219. package/packs/software-delivery/tool-impl/worktree-tools.ts +0 -46
  220. package/packs/software-delivery/tool-impl/wu-lifecycle-tools.ts +0 -807
  221. package/packs/software-delivery/tools/delegation-tools.ts +0 -23
  222. package/packs/software-delivery/tools/git-tools.ts +0 -55
  223. package/packs/software-delivery/tools/index.ts +0 -8
  224. package/packs/software-delivery/tools/lane-lock-tool.ts +0 -37
  225. package/packs/software-delivery/tools/types.ts +0 -71
  226. package/packs/software-delivery/tools/worktree-tools.ts +0 -49
  227. package/packs/software-delivery/vitest.config.ts +0 -11
@@ -1,694 +0,0 @@
1
- // Copyright (c) 2026 Hellmai Ltd
2
- // SPDX-License-Identifier: LicenseRef-LumenFlow-Proprietary
3
-
4
- import { createHash, randomUUID } from 'node:crypto';
5
- import { appendFile, mkdir } from 'node:fs/promises';
6
- import path from 'node:path';
7
- import { getSidekickRuntimeContext } from './tool-impl/runtime-context.js';
8
- import {
9
- getStoragePort,
10
- type ChannelMessageRecord,
11
- type ChannelRecord,
12
- type MemoryRecord,
13
- type RoutineRecord,
14
- type SidekickStores,
15
- type StoreName,
16
- type TaskRecord,
17
- } from './tool-impl/storage.js';
18
-
19
- const SCHEMA_VERSION_V2 = 2 as const;
20
- const CONTENT_HASH_ALGORITHM = 'sha256';
21
- const CONTENT_HASH_ENCODING = 'hex';
22
- const CONTENT_HASH_PREFIX_LENGTH = 32;
23
- const OUTBOX_RELATIVE_PATH = path.join(
24
- '.lumenflow',
25
- 'state',
26
- 'conductor',
27
- 'outbox',
28
- 'sidekick-events.jsonl',
29
- );
30
-
31
- export const SIDEKICK_CHANNEL_ID = 'sidekick' as const;
32
-
33
- export const SIDEKICK_EVENT_KINDS = {
34
- TASK_CREATED: 'sidekick:task_created',
35
- TASK_COMPLETED: 'sidekick:task_completed',
36
- TASK_SCHEDULED: 'sidekick:task_scheduled',
37
- TASK_SNOOZED: 'sidekick:task_snoozed',
38
- MEMORY_STORED: 'sidekick:memory_stored',
39
- MEMORY_RECALLED: 'sidekick:memory_recalled',
40
- MEMORY_FORGOTTEN: 'sidekick:memory_forgotten',
41
- CHANNEL_MESSAGE_SENT: 'sidekick:channel_message_sent',
42
- CHANNEL_MESSAGE_RECEIVED: 'sidekick:channel_message_received',
43
- CHANNEL_BRIDGE_CONNECTED: 'sidekick:channel_bridge_connected',
44
- CHANNEL_BRIDGE_DISCONNECTED: 'sidekick:channel_bridge_disconnected',
45
- ROUTINE_PLANNED: 'sidekick:routine_planned',
46
- ROUTINE_COMMITTED: 'sidekick:routine_committed',
47
- ROUTINE_EXECUTED: 'sidekick:routine_executed',
48
- ROUTINE_STEP_FAILED: 'sidekick:routine_step_failed',
49
- /**
50
- * @deprecated WU-2830 (INIT-062 WU-D): `sidekick:state_rehydrated` is
51
- * unbounded and cannot stream. New emitters MUST use the chunked trio
52
- * (`STATE_REHYDRATION_STARTED`, `STATE_REHYDRATION_CHUNK`,
53
- * `STATE_REHYDRATION_COMPLETED`). The constant and type are retained for
54
- * backwards compatibility only.
55
- */
56
- STATE_REHYDRATED: 'sidekick:state_rehydrated',
57
- STATE_REHYDRATION_STARTED: 'sidekick:state_rehydration_started',
58
- STATE_REHYDRATION_CHUNK: 'sidekick:state_rehydration_chunk',
59
- STATE_REHYDRATION_COMPLETED: 'sidekick:state_rehydration_completed',
60
- } as const;
61
-
62
- export const SIDEKICK_EVENT_KIND_VALUES = [
63
- SIDEKICK_EVENT_KINDS.TASK_CREATED,
64
- SIDEKICK_EVENT_KINDS.TASK_COMPLETED,
65
- SIDEKICK_EVENT_KINDS.TASK_SCHEDULED,
66
- SIDEKICK_EVENT_KINDS.TASK_SNOOZED,
67
- SIDEKICK_EVENT_KINDS.MEMORY_STORED,
68
- SIDEKICK_EVENT_KINDS.MEMORY_RECALLED,
69
- SIDEKICK_EVENT_KINDS.MEMORY_FORGOTTEN,
70
- SIDEKICK_EVENT_KINDS.CHANNEL_MESSAGE_SENT,
71
- SIDEKICK_EVENT_KINDS.CHANNEL_MESSAGE_RECEIVED,
72
- SIDEKICK_EVENT_KINDS.CHANNEL_BRIDGE_CONNECTED,
73
- SIDEKICK_EVENT_KINDS.CHANNEL_BRIDGE_DISCONNECTED,
74
- SIDEKICK_EVENT_KINDS.ROUTINE_PLANNED,
75
- SIDEKICK_EVENT_KINDS.ROUTINE_COMMITTED,
76
- SIDEKICK_EVENT_KINDS.ROUTINE_EXECUTED,
77
- SIDEKICK_EVENT_KINDS.ROUTINE_STEP_FAILED,
78
- SIDEKICK_EVENT_KINDS.STATE_REHYDRATED,
79
- SIDEKICK_EVENT_KINDS.STATE_REHYDRATION_STARTED,
80
- SIDEKICK_EVENT_KINDS.STATE_REHYDRATION_CHUNK,
81
- SIDEKICK_EVENT_KINDS.STATE_REHYDRATION_COMPLETED,
82
- ] as const;
83
-
84
- export type SidekickEventKind = (typeof SIDEKICK_EVENT_KIND_VALUES)[number];
85
- export type SidekickBackpressurePolicy = 'queue-with-replay' | 'ephemeral';
86
-
87
- const QUEUE_WITH_REPLAY: SidekickBackpressurePolicy = 'queue-with-replay';
88
- const EPHEMERAL: SidekickBackpressurePolicy = 'ephemeral';
89
-
90
- export const SIDEKICK_EVENT_BACKPRESSURE_POLICY: Record<
91
- SidekickEventKind,
92
- SidekickBackpressurePolicy
93
- > = {
94
- [SIDEKICK_EVENT_KINDS.TASK_CREATED]: QUEUE_WITH_REPLAY,
95
- [SIDEKICK_EVENT_KINDS.TASK_COMPLETED]: QUEUE_WITH_REPLAY,
96
- [SIDEKICK_EVENT_KINDS.TASK_SCHEDULED]: QUEUE_WITH_REPLAY,
97
- [SIDEKICK_EVENT_KINDS.TASK_SNOOZED]: QUEUE_WITH_REPLAY,
98
- [SIDEKICK_EVENT_KINDS.MEMORY_STORED]: EPHEMERAL,
99
- [SIDEKICK_EVENT_KINDS.MEMORY_RECALLED]: EPHEMERAL,
100
- [SIDEKICK_EVENT_KINDS.MEMORY_FORGOTTEN]: EPHEMERAL,
101
- [SIDEKICK_EVENT_KINDS.CHANNEL_MESSAGE_SENT]: EPHEMERAL,
102
- [SIDEKICK_EVENT_KINDS.CHANNEL_MESSAGE_RECEIVED]: EPHEMERAL,
103
- [SIDEKICK_EVENT_KINDS.CHANNEL_BRIDGE_CONNECTED]: EPHEMERAL,
104
- [SIDEKICK_EVENT_KINDS.CHANNEL_BRIDGE_DISCONNECTED]: EPHEMERAL,
105
- [SIDEKICK_EVENT_KINDS.ROUTINE_PLANNED]: QUEUE_WITH_REPLAY,
106
- [SIDEKICK_EVENT_KINDS.ROUTINE_COMMITTED]: QUEUE_WITH_REPLAY,
107
- [SIDEKICK_EVENT_KINDS.ROUTINE_EXECUTED]: QUEUE_WITH_REPLAY,
108
- [SIDEKICK_EVENT_KINDS.ROUTINE_STEP_FAILED]: QUEUE_WITH_REPLAY,
109
- [SIDEKICK_EVENT_KINDS.STATE_REHYDRATED]: QUEUE_WITH_REPLAY,
110
- [SIDEKICK_EVENT_KINDS.STATE_REHYDRATION_STARTED]: QUEUE_WITH_REPLAY,
111
- [SIDEKICK_EVENT_KINDS.STATE_REHYDRATION_CHUNK]: QUEUE_WITH_REPLAY,
112
- [SIDEKICK_EVENT_KINDS.STATE_REHYDRATION_COMPLETED]: QUEUE_WITH_REPLAY,
113
- };
114
-
115
- interface SidekickEventEnvelope {
116
- schema_version: typeof SCHEMA_VERSION_V2;
117
- timestamp: string;
118
- event_id: string;
119
- channel_id: typeof SIDEKICK_CHANNEL_ID;
120
- seq: number;
121
- }
122
-
123
- export interface TaskCreatedEvent extends SidekickEventEnvelope {
124
- kind: typeof SIDEKICK_EVENT_KINDS.TASK_CREATED;
125
- task: TaskRecord;
126
- }
127
-
128
- export interface TaskCompletedEvent extends SidekickEventEnvelope {
129
- kind: typeof SIDEKICK_EVENT_KINDS.TASK_COMPLETED;
130
- task: TaskRecord;
131
- }
132
-
133
- export interface TaskScheduledEvent extends SidekickEventEnvelope {
134
- kind: typeof SIDEKICK_EVENT_KINDS.TASK_SCHEDULED;
135
- task: TaskRecord;
136
- }
137
-
138
- export interface TaskSnoozedEvent extends SidekickEventEnvelope {
139
- kind: typeof SIDEKICK_EVENT_KINDS.TASK_SNOOZED;
140
- task_id: string;
141
- previous_due_at: string | null;
142
- due_at: string;
143
- }
144
-
145
- export interface MemoryStoredEvent extends SidekickEventEnvelope {
146
- kind: typeof SIDEKICK_EVENT_KINDS.MEMORY_STORED;
147
- memory: MemoryRecord;
148
- }
149
-
150
- export interface MemoryRecalledEvent extends SidekickEventEnvelope {
151
- kind: typeof SIDEKICK_EVENT_KINDS.MEMORY_RECALLED;
152
- query: string | null;
153
- count: number;
154
- memory_ids: string[];
155
- }
156
-
157
- export interface MemoryForgottenEvent extends SidekickEventEnvelope {
158
- kind: typeof SIDEKICK_EVENT_KINDS.MEMORY_FORGOTTEN;
159
- memory_id: string;
160
- }
161
-
162
- export interface ChannelMessageSentEvent extends SidekickEventEnvelope {
163
- kind: typeof SIDEKICK_EVENT_KINDS.CHANNEL_MESSAGE_SENT;
164
- /**
165
- * WU-2830 (INIT-062 WU-D): typed as {@link ChannelMessageRecord} for
166
- * symmetry with {@link ChannelMessageReceivedEvent.messages} and to enable
167
- * static analysis cloud-side.
168
- */
169
- message: ChannelMessageRecord;
170
- }
171
-
172
- export interface ChannelMessageReceivedEvent extends SidekickEventEnvelope {
173
- kind: typeof SIDEKICK_EVENT_KINDS.CHANNEL_MESSAGE_RECEIVED;
174
- channel: string;
175
- /**
176
- * WU-2830 (INIT-062 WU-D): full {@link ChannelMessageRecord} payloads
177
- * replace the previous bare `count` field. Subscribers count via
178
- * `messages.length`; symmetric with {@link ChannelMessageSentEvent.message}.
179
- */
180
- messages: ChannelMessageRecord[];
181
- provider?: string;
182
- }
183
-
184
- export interface ChannelBridgeConnectedEvent extends SidekickEventEnvelope {
185
- kind: typeof SIDEKICK_EVENT_KINDS.CHANNEL_BRIDGE_CONNECTED;
186
- channel: ChannelRecord;
187
- }
188
-
189
- export interface ChannelBridgeDisconnectedEvent extends SidekickEventEnvelope {
190
- kind: typeof SIDEKICK_EVENT_KINDS.CHANNEL_BRIDGE_DISCONNECTED;
191
- bridge_id: string;
192
- deleted_message_count: number;
193
- }
194
-
195
- export interface RoutinePlannedEvent extends SidekickEventEnvelope {
196
- kind: typeof SIDEKICK_EVENT_KINDS.ROUTINE_PLANNED;
197
- routine: RoutineRecord;
198
- }
199
-
200
- export interface RoutineCommittedEvent extends SidekickEventEnvelope {
201
- kind: typeof SIDEKICK_EVENT_KINDS.ROUTINE_COMMITTED;
202
- routine: RoutineRecord;
203
- /**
204
- * WU-2738 (INIT-060, ADR-013 §6 governance): content-hash of the commit
205
- * envelope carried when the event is emitted by the governed
206
- * `sidekick:routine:commit_plan` tool. PII stays inside the local
207
- * envelope; cloud correlates on the hash. Optional so the existing
208
- * update-triggered emission (routine:update) stays PII-free.
209
- */
210
- envelope_content_hash?: string;
211
- }
212
-
213
- export interface RoutineExecutedEvent extends SidekickEventEnvelope {
214
- kind: typeof SIDEKICK_EVENT_KINDS.ROUTINE_EXECUTED;
215
- routine_id: string;
216
- name: string;
217
- step_count: number;
218
- plan_only: true;
219
- }
220
-
221
- export interface RoutineStepFailedEvent extends SidekickEventEnvelope {
222
- kind: typeof SIDEKICK_EVENT_KINDS.ROUTINE_STEP_FAILED;
223
- step_index: number;
224
- reason: string;
225
- routine_id?: string;
226
- routine_name?: string;
227
- }
228
-
229
- /**
230
- * @deprecated WU-2830 (INIT-062 WU-D): unbounded snapshots cannot stream.
231
- * Emit {@link StateRehydrationStartedEvent} +
232
- * {@link StateRehydrationChunkEvent} (one per store) +
233
- * {@link StateRehydrationCompletedEvent} instead. Retained for parity with
234
- * existing consumers and the compile-time parity tests.
235
- */
236
- export interface StateRehydratedEvent extends SidekickEventEnvelope {
237
- kind: typeof SIDEKICK_EVENT_KINDS.STATE_REHYDRATED;
238
- snapshot: SidekickStores;
239
- }
240
-
241
- export interface StateRehydrationStartedEvent extends SidekickEventEnvelope {
242
- kind: typeof SIDEKICK_EVENT_KINDS.STATE_REHYDRATION_STARTED;
243
- /** Correlation id shared by the started/chunk/completed trio. */
244
- rehydration_id: string;
245
- /** Total chunks the receiver should expect. */
246
- total_chunks: number;
247
- }
248
-
249
- export interface StateRehydrationChunkEvent extends SidekickEventEnvelope {
250
- kind: typeof SIDEKICK_EVENT_KINDS.STATE_REHYDRATION_CHUNK;
251
- /** Correlation id matching the {@link StateRehydrationStartedEvent}. */
252
- rehydration_id: string;
253
- /**
254
- * Monotonic zero-based index identifying this chunk's position within the
255
- * stream. Receivers reassemble ordered by `cursor`.
256
- */
257
- cursor: number;
258
- /** True for the final chunk in the stream (cursor === total_chunks - 1). */
259
- last_chunk: boolean;
260
- /**
261
- * Subset of {@link SidekickStores} keys carried by this chunk. Receivers
262
- * merge `stores` into the accumulating snapshot keyed on store name.
263
- */
264
- stores: Partial<SidekickStores>;
265
- }
266
-
267
- export interface StateRehydrationCompletedEvent extends SidekickEventEnvelope {
268
- kind: typeof SIDEKICK_EVENT_KINDS.STATE_REHYDRATION_COMPLETED;
269
- rehydration_id: string;
270
- chunk_count: number;
271
- }
272
-
273
- export type SidekickEvent =
274
- | TaskCreatedEvent
275
- | TaskCompletedEvent
276
- | TaskScheduledEvent
277
- | TaskSnoozedEvent
278
- | MemoryStoredEvent
279
- | MemoryRecalledEvent
280
- | MemoryForgottenEvent
281
- | ChannelMessageSentEvent
282
- | ChannelMessageReceivedEvent
283
- | ChannelBridgeConnectedEvent
284
- | ChannelBridgeDisconnectedEvent
285
- | RoutinePlannedEvent
286
- | RoutineCommittedEvent
287
- | RoutineExecutedEvent
288
- | RoutineStepFailedEvent
289
- | StateRehydratedEvent
290
- | StateRehydrationStartedEvent
291
- | StateRehydrationChunkEvent
292
- | StateRehydrationCompletedEvent;
293
-
294
- const channelSeqCounters = new Map<string, number>();
295
-
296
- type EventPayload = Record<string, unknown> & { kind: SidekickEventKind };
297
-
298
- function nextSeq(channelId: string): number {
299
- const current = channelSeqCounters.get(channelId) ?? 0;
300
- const next = current + 1;
301
- channelSeqCounters.set(channelId, next);
302
- return next;
303
- }
304
-
305
- function resolveTimestamp(timestamp?: string): string {
306
- return timestamp ?? new Date().toISOString();
307
- }
308
-
309
- function resolveEventId(
310
- kind: SidekickEventKind,
311
- payload: Record<string, unknown>,
312
- timestamp: string,
313
- idempotencyKey?: string,
314
- ): string {
315
- if (!idempotencyKey) {
316
- return randomUUID();
317
- }
318
- return createHash(CONTENT_HASH_ALGORITHM)
319
- .update(
320
- JSON.stringify({
321
- kind,
322
- payload,
323
- timestamp,
324
- idempotencyKey,
325
- }),
326
- )
327
- .digest(CONTENT_HASH_ENCODING)
328
- .slice(0, CONTENT_HASH_PREFIX_LENGTH);
329
- }
330
-
331
- function stampSidekickEvent<TEvent extends SidekickEvent>(
332
- payload: EventPayload,
333
- options?: { timestamp?: string; idempotencyKey?: string },
334
- ): TEvent {
335
- const timestamp = resolveTimestamp(options?.timestamp);
336
- const event_id = resolveEventId(payload.kind, payload, timestamp, options?.idempotencyKey);
337
- return {
338
- ...payload,
339
- schema_version: SCHEMA_VERSION_V2,
340
- timestamp,
341
- event_id,
342
- channel_id: SIDEKICK_CHANNEL_ID,
343
- seq: nextSeq(SIDEKICK_CHANNEL_ID),
344
- } as TEvent;
345
- }
346
-
347
- async function queueEvent(event: SidekickEvent, workspaceRoot: string): Promise<void> {
348
- const outboxPath = path.join(workspaceRoot, OUTBOX_RELATIVE_PATH);
349
- await mkdir(path.dirname(outboxPath), { recursive: true });
350
- await appendFile(outboxPath, `${JSON.stringify(event)}\n`, 'utf8');
351
- }
352
-
353
- export function resetSidekickSeqCounter(): void {
354
- channelSeqCounters.clear();
355
- }
356
-
357
- export function readSidekickOrgId(config: unknown): string | undefined {
358
- if (!config || typeof config !== 'object') {
359
- return undefined;
360
- }
361
- const controlPlane = (config as { control_plane?: unknown }).control_plane;
362
- if (!controlPlane || typeof controlPlane !== 'object') {
363
- return undefined;
364
- }
365
- const raw = (controlPlane as { org_id?: unknown }).org_id;
366
- if (typeof raw !== 'string') {
367
- return undefined;
368
- }
369
- const trimmed = raw.trim();
370
- return trimmed.length > 0 ? trimmed : undefined;
371
- }
372
-
373
- export async function emitSidekickEvent<TEvent extends SidekickEvent>(
374
- event: TEvent,
375
- ): Promise<TEvent> {
376
- const runtimeContext = getSidekickRuntimeContext();
377
- const policy = SIDEKICK_EVENT_BACKPRESSURE_POLICY[event.kind];
378
- const orgId = readSidekickOrgId(runtimeContext?.workspaceConfig);
379
- const sink = runtimeContext?.eventSink;
380
-
381
- if (policy === 'ephemeral') {
382
- if (!orgId || !sink) {
383
- return event;
384
- }
385
- try {
386
- await sink(event);
387
- } catch {
388
- // Ephemeral events fail silent when the control plane is unavailable.
389
- }
390
- return event;
391
- }
392
-
393
- if (orgId && sink) {
394
- try {
395
- await sink(event);
396
- return event;
397
- } catch {
398
- // Fall through to queued replay on transport failure.
399
- }
400
- }
401
-
402
- await queueEvent(event, runtimeContext?.workspaceRoot ?? process.cwd());
403
- return event;
404
- }
405
-
406
- export async function snapshotSidekickState(): Promise<SidekickStores> {
407
- const storage = getStoragePort();
408
- const [tasks, memories, channels, messages, routines] = await Promise.all([
409
- storage.readStore('tasks'),
410
- storage.readStore('memories'),
411
- storage.readStore('channels'),
412
- storage.readStore('messages'),
413
- storage.readStore('routines'),
414
- ]);
415
-
416
- return { tasks, memories, channels, messages, routines };
417
- }
418
-
419
- export function buildTaskCreatedEvent(task: TaskRecord): TaskCreatedEvent {
420
- return stampSidekickEvent<TaskCreatedEvent>({
421
- kind: SIDEKICK_EVENT_KINDS.TASK_CREATED,
422
- task,
423
- });
424
- }
425
-
426
- export function buildTaskCompletedEvent(task: TaskRecord): TaskCompletedEvent {
427
- return stampSidekickEvent<TaskCompletedEvent>({
428
- kind: SIDEKICK_EVENT_KINDS.TASK_COMPLETED,
429
- task,
430
- });
431
- }
432
-
433
- export function buildTaskScheduledEvent(task: TaskRecord): TaskScheduledEvent {
434
- return stampSidekickEvent<TaskScheduledEvent>({
435
- kind: SIDEKICK_EVENT_KINDS.TASK_SCHEDULED,
436
- task,
437
- });
438
- }
439
-
440
- export function buildTaskSnoozedEvent(input: {
441
- task_id: string;
442
- previous_due_at: string | null;
443
- due_at: string;
444
- }): TaskSnoozedEvent {
445
- return stampSidekickEvent<TaskSnoozedEvent>({
446
- kind: SIDEKICK_EVENT_KINDS.TASK_SNOOZED,
447
- ...input,
448
- });
449
- }
450
-
451
- export function buildMemoryStoredEvent(memory: MemoryRecord): MemoryStoredEvent {
452
- return stampSidekickEvent<MemoryStoredEvent>({
453
- kind: SIDEKICK_EVENT_KINDS.MEMORY_STORED,
454
- memory,
455
- });
456
- }
457
-
458
- export function buildMemoryRecalledEvent(input: {
459
- query: string | null;
460
- memories: MemoryRecord[];
461
- }): MemoryRecalledEvent {
462
- return stampSidekickEvent<MemoryRecalledEvent>({
463
- kind: SIDEKICK_EVENT_KINDS.MEMORY_RECALLED,
464
- query: input.query,
465
- count: input.memories.length,
466
- memory_ids: input.memories.map((memory) => memory.id),
467
- });
468
- }
469
-
470
- export function buildMemoryForgottenEvent(memory_id: string): MemoryForgottenEvent {
471
- return stampSidekickEvent<MemoryForgottenEvent>({
472
- kind: SIDEKICK_EVENT_KINDS.MEMORY_FORGOTTEN,
473
- memory_id,
474
- });
475
- }
476
-
477
- export function buildChannelMessageSentEvent(
478
- message: ChannelMessageRecord,
479
- ): ChannelMessageSentEvent {
480
- return stampSidekickEvent<ChannelMessageSentEvent>({
481
- kind: SIDEKICK_EVENT_KINDS.CHANNEL_MESSAGE_SENT,
482
- message,
483
- });
484
- }
485
-
486
- export function buildChannelMessageReceivedEvent(input: {
487
- channel: string;
488
- messages: ChannelMessageRecord[];
489
- provider?: string;
490
- }): ChannelMessageReceivedEvent {
491
- return stampSidekickEvent<ChannelMessageReceivedEvent>({
492
- kind: SIDEKICK_EVENT_KINDS.CHANNEL_MESSAGE_RECEIVED,
493
- channel: input.channel,
494
- messages: input.messages,
495
- ...(input.provider ? { provider: input.provider } : {}),
496
- });
497
- }
498
-
499
- export function buildChannelBridgeConnectedEvent(
500
- channel: ChannelRecord,
501
- ): ChannelBridgeConnectedEvent {
502
- return stampSidekickEvent<ChannelBridgeConnectedEvent>({
503
- kind: SIDEKICK_EVENT_KINDS.CHANNEL_BRIDGE_CONNECTED,
504
- channel,
505
- });
506
- }
507
-
508
- export function buildChannelBridgeDisconnectedEvent(input: {
509
- bridge_id: string;
510
- deleted_message_count: number;
511
- }): ChannelBridgeDisconnectedEvent {
512
- return stampSidekickEvent<ChannelBridgeDisconnectedEvent>({
513
- kind: SIDEKICK_EVENT_KINDS.CHANNEL_BRIDGE_DISCONNECTED,
514
- ...input,
515
- });
516
- }
517
-
518
- export function buildRoutinePlannedEvent(routine: RoutineRecord): RoutinePlannedEvent {
519
- return stampSidekickEvent<RoutinePlannedEvent>({
520
- kind: SIDEKICK_EVENT_KINDS.ROUTINE_PLANNED,
521
- routine,
522
- });
523
- }
524
-
525
- export function buildRoutineCommittedEvent(
526
- routine: RoutineRecord,
527
- options?: { envelope_content_hash?: string },
528
- ): RoutineCommittedEvent {
529
- return stampSidekickEvent<RoutineCommittedEvent>({
530
- kind: SIDEKICK_EVENT_KINDS.ROUTINE_COMMITTED,
531
- routine,
532
- ...(options?.envelope_content_hash
533
- ? { envelope_content_hash: options.envelope_content_hash }
534
- : {}),
535
- });
536
- }
537
-
538
- export function buildRoutineExecutedEvent(input: {
539
- routine_id: string;
540
- name: string;
541
- step_count: number;
542
- }): RoutineExecutedEvent {
543
- return stampSidekickEvent<RoutineExecutedEvent>({
544
- kind: SIDEKICK_EVENT_KINDS.ROUTINE_EXECUTED,
545
- routine_id: input.routine_id,
546
- name: input.name,
547
- step_count: input.step_count,
548
- plan_only: true,
549
- });
550
- }
551
-
552
- export function buildRoutineStepFailedEvent(input: {
553
- step_index: number;
554
- reason: string;
555
- routine_id?: string;
556
- routine_name?: string;
557
- }): RoutineStepFailedEvent {
558
- return stampSidekickEvent<RoutineStepFailedEvent>({
559
- kind: SIDEKICK_EVENT_KINDS.ROUTINE_STEP_FAILED,
560
- step_index: input.step_index,
561
- reason: input.reason,
562
- ...(input.routine_id ? { routine_id: input.routine_id } : {}),
563
- ...(input.routine_name ? { routine_name: input.routine_name } : {}),
564
- });
565
- }
566
-
567
- /**
568
- * @deprecated WU-2830 (INIT-062 WU-D): prefer
569
- * {@link buildStateRehydrationChunkEvents} which emits the bounded
570
- * started/chunk/completed trio. Retained for backwards compatibility.
571
- */
572
- export function buildStateRehydratedEvent(snapshot: SidekickStores): StateRehydratedEvent {
573
- return stampSidekickEvent<StateRehydratedEvent>({
574
- kind: SIDEKICK_EVENT_KINDS.STATE_REHYDRATED,
575
- snapshot,
576
- });
577
- }
578
-
579
- export function buildStateRehydrationStartedEvent(input: {
580
- rehydration_id: string;
581
- total_chunks: number;
582
- }): StateRehydrationStartedEvent {
583
- return stampSidekickEvent<StateRehydrationStartedEvent>({
584
- kind: SIDEKICK_EVENT_KINDS.STATE_REHYDRATION_STARTED,
585
- rehydration_id: input.rehydration_id,
586
- total_chunks: input.total_chunks,
587
- });
588
- }
589
-
590
- export function buildStateRehydrationChunkEvent(input: {
591
- rehydration_id: string;
592
- cursor: number;
593
- last_chunk: boolean;
594
- stores: Partial<SidekickStores>;
595
- }): StateRehydrationChunkEvent {
596
- return stampSidekickEvent<StateRehydrationChunkEvent>({
597
- kind: SIDEKICK_EVENT_KINDS.STATE_REHYDRATION_CHUNK,
598
- rehydration_id: input.rehydration_id,
599
- cursor: input.cursor,
600
- last_chunk: input.last_chunk,
601
- stores: input.stores,
602
- });
603
- }
604
-
605
- export function buildStateRehydrationCompletedEvent(input: {
606
- rehydration_id: string;
607
- chunk_count: number;
608
- }): StateRehydrationCompletedEvent {
609
- return stampSidekickEvent<StateRehydrationCompletedEvent>({
610
- kind: SIDEKICK_EVENT_KINDS.STATE_REHYDRATION_COMPLETED,
611
- rehydration_id: input.rehydration_id,
612
- chunk_count: input.chunk_count,
613
- });
614
- }
615
-
616
- /**
617
- * WU-2830 (INIT-062 WU-D): the store order used by the chunked rehydration
618
- * emitter. Exposed so receivers that care about deterministic reassembly
619
- * can cross-check cursor semantics.
620
- */
621
- export const SIDEKICK_REHYDRATION_STORE_ORDER = [
622
- 'tasks',
623
- 'memories',
624
- 'channels',
625
- 'messages',
626
- 'routines',
627
- ] as const satisfies readonly StoreName[];
628
-
629
- export type SidekickRehydrationStore = (typeof SIDEKICK_REHYDRATION_STORE_ORDER)[number];
630
-
631
- /**
632
- * WU-2830 (INIT-062 WU-D): emit the chunked rehydration stream.
633
- *
634
- * Emission order: `state_rehydration_started` → one
635
- * `state_rehydration_chunk` per store in
636
- * {@link SIDEKICK_REHYDRATION_STORE_ORDER} → `state_rehydration_completed`.
637
- * `cursor` is a zero-based monotonic index; `last_chunk` is true on the final
638
- * chunk so streaming receivers can close the reassembly window without
639
- * waiting for the completion event.
640
- */
641
- export async function emitSidekickStateRehydration(snapshot: SidekickStores): Promise<{
642
- rehydration_id: string;
643
- chunk_count: number;
644
- }> {
645
- const rehydration_id = randomUUID();
646
- const stores = SIDEKICK_REHYDRATION_STORE_ORDER;
647
- const total_chunks = stores.length;
648
-
649
- await emitSidekickEvent(buildStateRehydrationStartedEvent({ rehydration_id, total_chunks }));
650
-
651
- for (let cursor = 0; cursor < stores.length; cursor++) {
652
- const store = stores[cursor];
653
- if (store === undefined) {
654
- continue;
655
- }
656
- const last_chunk = cursor === stores.length - 1;
657
- const chunkStores: Partial<SidekickStores> = {};
658
- // Narrow the union so TS keeps the store-value type aligned with the key.
659
- switch (store) {
660
- case 'tasks':
661
- chunkStores.tasks = snapshot.tasks;
662
- break;
663
- case 'memories':
664
- chunkStores.memories = snapshot.memories;
665
- break;
666
- case 'channels':
667
- chunkStores.channels = snapshot.channels;
668
- break;
669
- case 'messages':
670
- chunkStores.messages = snapshot.messages;
671
- break;
672
- case 'routines':
673
- chunkStores.routines = snapshot.routines;
674
- break;
675
- }
676
- await emitSidekickEvent(
677
- buildStateRehydrationChunkEvent({
678
- rehydration_id,
679
- cursor,
680
- last_chunk,
681
- stores: chunkStores,
682
- }),
683
- );
684
- }
685
-
686
- await emitSidekickEvent(
687
- buildStateRehydrationCompletedEvent({
688
- rehydration_id,
689
- chunk_count: total_chunks,
690
- }),
691
- );
692
-
693
- return { rehydration_id, chunk_count: total_chunks };
694
- }