dual-brain 0.2.30 → 0.3.1

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 (312) hide show
  1. package/.dual-brain/docs/claude-code-extension-points.md +32 -0
  2. package/.dual-brain/docs/data-tools-capabilities.md +181 -0
  3. package/.dual-brain/docs/ecosystem-tools.md +91 -0
  4. package/.dual-brain/docs/panel-handoff.md +124 -0
  5. package/.dual-brain/docs/ruflo-analysis.md +48 -0
  6. package/bin/dual-brain.mjs +56 -56
  7. package/dist/mcp-server/index.d.ts +27 -0
  8. package/dist/mcp-server/index.js +359 -0
  9. package/dist/mcp-server/index.js.map +1 -0
  10. package/dist/src/agent-protocol.d.ts +163 -0
  11. package/dist/src/agent-protocol.js +368 -0
  12. package/dist/src/agent-protocol.js.map +1 -0
  13. package/dist/src/agents/registry.d.ts +52 -0
  14. package/dist/src/agents/registry.js +393 -0
  15. package/dist/src/agents/registry.js.map +1 -0
  16. package/dist/src/awareness.d.ts +93 -0
  17. package/dist/src/awareness.js +406 -0
  18. package/dist/src/awareness.js.map +1 -0
  19. package/dist/src/brief.d.ts +48 -0
  20. package/dist/src/brief.js +179 -0
  21. package/dist/src/brief.js.map +1 -0
  22. package/dist/src/calibration.d.ts +32 -0
  23. package/dist/src/calibration.js +133 -0
  24. package/dist/src/calibration.js.map +1 -0
  25. package/dist/src/checkpoint.d.ts +33 -0
  26. package/dist/src/checkpoint.js +99 -0
  27. package/dist/src/checkpoint.js.map +1 -0
  28. package/dist/src/ci-triage.d.ts +33 -0
  29. package/dist/src/ci-triage.js +193 -0
  30. package/dist/src/ci-triage.js.map +1 -0
  31. package/dist/src/cognitive-loop.d.ts +56 -0
  32. package/dist/src/cognitive-loop.js +495 -0
  33. package/dist/src/cognitive-loop.js.map +1 -0
  34. package/dist/src/collaboration.d.ts +147 -0
  35. package/dist/src/collaboration.js +438 -0
  36. package/dist/src/collaboration.js.map +1 -0
  37. package/dist/src/context-intel.d.ts +47 -0
  38. package/dist/src/context-intel.js +156 -0
  39. package/dist/src/context-intel.js.map +1 -0
  40. package/dist/src/context.d.ts +53 -0
  41. package/dist/src/context.js +332 -0
  42. package/dist/src/context.js.map +1 -0
  43. package/dist/src/continuity.d.ts +89 -0
  44. package/dist/src/continuity.js +230 -0
  45. package/dist/src/continuity.js.map +1 -0
  46. package/dist/src/cost-tracker.d.ts +47 -0
  47. package/dist/src/cost-tracker.js +170 -0
  48. package/dist/src/cost-tracker.js.map +1 -0
  49. package/dist/src/debrief.d.ts +53 -0
  50. package/dist/src/debrief.js +222 -0
  51. package/dist/src/debrief.js.map +1 -0
  52. package/dist/src/decide.d.ts +96 -0
  53. package/dist/src/decide.js +744 -0
  54. package/dist/src/decide.js.map +1 -0
  55. package/dist/src/decompose.d.ts +39 -0
  56. package/dist/src/decompose.js +218 -0
  57. package/dist/src/decompose.js.map +1 -0
  58. package/dist/src/detect.d.ts +91 -0
  59. package/dist/src/detect.js +544 -0
  60. package/dist/src/detect.js.map +1 -0
  61. package/dist/src/dispatch.d.ts +154 -0
  62. package/dist/src/dispatch.js +1306 -0
  63. package/dist/src/dispatch.js.map +1 -0
  64. package/dist/src/doctor.d.ts +421 -0
  65. package/dist/src/doctor.js +1689 -0
  66. package/dist/src/doctor.js.map +1 -0
  67. package/dist/src/engine.d.ts +70 -0
  68. package/dist/src/engine.js +155 -0
  69. package/dist/src/engine.js.map +1 -0
  70. package/dist/src/envelope.d.ts +36 -0
  71. package/dist/src/envelope.js +80 -0
  72. package/dist/src/envelope.js.map +1 -0
  73. package/dist/src/failure-memory.d.ts +55 -0
  74. package/dist/src/failure-memory.js +175 -0
  75. package/dist/src/failure-memory.js.map +1 -0
  76. package/dist/src/fx.d.ts +87 -0
  77. package/dist/src/fx.js +272 -0
  78. package/dist/src/fx.js.map +1 -0
  79. package/dist/src/governance.d.ts +93 -0
  80. package/dist/src/governance.js +261 -0
  81. package/dist/src/governance.js.map +1 -0
  82. package/dist/src/handoff.d.ts +11 -0
  83. package/dist/src/handoff.js +90 -0
  84. package/dist/src/handoff.js.map +1 -0
  85. package/dist/src/head-protocol.d.ts +76 -0
  86. package/dist/src/head-protocol.js +109 -0
  87. package/dist/src/head-protocol.js.map +1 -0
  88. package/dist/src/head.d.ts +222 -0
  89. package/dist/src/head.js +765 -0
  90. package/dist/src/head.js.map +1 -0
  91. package/dist/src/health.d.ts +132 -0
  92. package/dist/src/health.js +435 -0
  93. package/dist/src/health.js.map +1 -0
  94. package/dist/src/inbox.d.ts +70 -0
  95. package/dist/src/inbox.js +218 -0
  96. package/dist/src/inbox.js.map +1 -0
  97. package/dist/src/index.d.ts +33 -0
  98. package/dist/src/index.js +38 -0
  99. package/dist/src/index.js.map +1 -0
  100. package/dist/src/install-hooks.d.ts +13 -0
  101. package/dist/src/install-hooks.js +88 -0
  102. package/dist/src/install-hooks.js.map +1 -0
  103. package/dist/src/integrity.d.ts +59 -0
  104. package/dist/src/integrity.js +206 -0
  105. package/dist/src/integrity.js.map +1 -0
  106. package/dist/src/intelligence.d.ts +104 -0
  107. package/dist/src/intelligence.js +391 -0
  108. package/dist/src/intelligence.js.map +1 -0
  109. package/dist/src/ledger.d.ts +54 -0
  110. package/dist/src/ledger.js +179 -0
  111. package/dist/src/ledger.js.map +1 -0
  112. package/dist/src/living-docs.d.ts +14 -0
  113. package/dist/src/living-docs.js +197 -0
  114. package/dist/src/living-docs.js.map +1 -0
  115. package/dist/src/memory-tiers.d.ts +37 -0
  116. package/dist/src/memory-tiers.js +160 -0
  117. package/dist/src/memory-tiers.js.map +1 -0
  118. package/dist/src/model-profiles.d.ts +65 -0
  119. package/dist/src/model-profiles.js +568 -0
  120. package/dist/src/model-profiles.js.map +1 -0
  121. package/dist/src/models.d.ts +58 -0
  122. package/dist/src/models.js +327 -0
  123. package/dist/src/models.js.map +1 -0
  124. package/dist/src/narrative.d.ts +54 -0
  125. package/dist/src/narrative.js +163 -0
  126. package/dist/src/narrative.js.map +1 -0
  127. package/dist/src/nextstep.d.ts +16 -0
  128. package/dist/src/nextstep.js +103 -0
  129. package/dist/src/nextstep.js.map +1 -0
  130. package/dist/src/observer.d.ts +18 -0
  131. package/dist/src/observer.js +251 -0
  132. package/dist/src/observer.js.map +1 -0
  133. package/dist/src/outcome.d.ts +110 -0
  134. package/dist/src/outcome.js +377 -0
  135. package/dist/src/outcome.js.map +1 -0
  136. package/dist/src/pipeline.d.ts +167 -0
  137. package/dist/src/pipeline.js +1503 -0
  138. package/dist/src/pipeline.js.map +1 -0
  139. package/dist/src/playbook.d.ts +59 -0
  140. package/dist/src/playbook.js +238 -0
  141. package/dist/src/playbook.js.map +1 -0
  142. package/dist/src/pr-agent.d.ts +97 -0
  143. package/dist/src/pr-agent.js +195 -0
  144. package/dist/src/pr-agent.js.map +1 -0
  145. package/dist/src/predictive.d.ts +57 -0
  146. package/dist/src/predictive.js +230 -0
  147. package/dist/src/predictive.js.map +1 -0
  148. package/dist/src/profile.d.ts +294 -0
  149. package/dist/src/profile.js +1347 -0
  150. package/dist/src/profile.js.map +1 -0
  151. package/dist/src/prompt-audit.d.ts +22 -0
  152. package/dist/src/prompt-audit.js +194 -0
  153. package/dist/src/prompt-audit.js.map +1 -0
  154. package/dist/src/prompt-intel.d.ts +12 -0
  155. package/dist/src/prompt-intel.js +321 -0
  156. package/dist/src/prompt-intel.js.map +1 -0
  157. package/dist/src/provider-context.d.ts +121 -0
  158. package/dist/src/provider-context.js +222 -0
  159. package/dist/src/provider-context.js.map +1 -0
  160. package/dist/src/provider-manager.d.ts +92 -0
  161. package/dist/src/provider-manager.js +428 -0
  162. package/dist/src/provider-manager.js.map +1 -0
  163. package/dist/src/receipt.d.ts +87 -0
  164. package/dist/src/receipt.js +326 -0
  165. package/dist/src/receipt.js.map +1 -0
  166. package/dist/src/recommendations.d.ts +13 -0
  167. package/dist/src/recommendations.js +291 -0
  168. package/dist/src/recommendations.js.map +1 -0
  169. package/dist/src/redact.d.ts +15 -0
  170. package/dist/src/redact.js +129 -0
  171. package/dist/src/redact.js.map +1 -0
  172. package/dist/src/replit.d.ts +397 -0
  173. package/dist/src/replit.js +1160 -0
  174. package/dist/src/replit.js.map +1 -0
  175. package/dist/src/repo.d.ts +149 -0
  176. package/dist/src/repo.js +416 -0
  177. package/dist/src/repo.js.map +1 -0
  178. package/dist/src/revert.d.ts +30 -0
  179. package/dist/src/revert.js +166 -0
  180. package/dist/src/revert.js.map +1 -0
  181. package/dist/src/room.d.ts +102 -0
  182. package/dist/src/room.js +212 -0
  183. package/dist/src/room.js.map +1 -0
  184. package/dist/src/routing-advisor.d.ts +57 -0
  185. package/dist/src/routing-advisor.js +221 -0
  186. package/dist/src/routing-advisor.js.map +1 -0
  187. package/dist/src/self-correct.d.ts +40 -0
  188. package/dist/src/self-correct.js +137 -0
  189. package/dist/src/self-correct.js.map +1 -0
  190. package/dist/src/session-lock.d.ts +35 -0
  191. package/dist/src/session-lock.js +134 -0
  192. package/dist/src/session-lock.js.map +1 -0
  193. package/dist/src/session.d.ts +267 -0
  194. package/dist/src/session.js +1660 -0
  195. package/dist/src/session.js.map +1 -0
  196. package/dist/src/settings-tui.d.ts +5 -0
  197. package/dist/src/settings-tui.js +422 -0
  198. package/dist/src/settings-tui.js.map +1 -0
  199. package/dist/src/setup-flow.d.ts +63 -0
  200. package/dist/src/setup-flow.js +233 -0
  201. package/dist/src/setup-flow.js.map +1 -0
  202. package/dist/src/signal.d.ts +19 -0
  203. package/dist/src/signal.js +122 -0
  204. package/dist/src/signal.js.map +1 -0
  205. package/dist/src/simmer.d.ts +85 -0
  206. package/dist/src/simmer.js +224 -0
  207. package/dist/src/simmer.js.map +1 -0
  208. package/dist/src/state-export.d.ts +129 -0
  209. package/dist/src/state-export.js +233 -0
  210. package/dist/src/state-export.js.map +1 -0
  211. package/dist/src/strategy.d.ts +54 -0
  212. package/dist/src/strategy.js +95 -0
  213. package/dist/src/strategy.js.map +1 -0
  214. package/dist/src/subscription.d.ts +40 -0
  215. package/dist/src/subscription.js +189 -0
  216. package/dist/src/subscription.js.map +1 -0
  217. package/dist/src/templates.d.ts +208 -0
  218. package/dist/src/templates.js +238 -0
  219. package/dist/src/templates.js.map +1 -0
  220. package/dist/src/test.d.ts +9 -0
  221. package/dist/src/test.js +1173 -0
  222. package/dist/src/test.js.map +1 -0
  223. package/dist/src/think-engine.d.ts +67 -0
  224. package/dist/src/think-engine.js +412 -0
  225. package/dist/src/think-engine.js.map +1 -0
  226. package/dist/src/tui.d.ts +71 -0
  227. package/dist/src/tui.js +242 -0
  228. package/dist/src/tui.js.map +1 -0
  229. package/dist/src/types.d.ts +177 -0
  230. package/dist/src/types.js +6 -0
  231. package/dist/src/types.js.map +1 -0
  232. package/dist/src/update-check.d.ts +7 -0
  233. package/dist/src/update-check.js +36 -0
  234. package/dist/src/update-check.js.map +1 -0
  235. package/dist/src/wave-planner.d.ts +30 -0
  236. package/dist/src/wave-planner.js +281 -0
  237. package/dist/src/wave-planner.js.map +1 -0
  238. package/hooks/head-guard.sh +41 -0
  239. package/hooks/precompact.mjs +3 -3
  240. package/hooks/session-end.mjs +3 -3
  241. package/hooks/task-classifier.mjs +328 -0
  242. package/hooks/vibe-router.mjs +387 -0
  243. package/install.mjs +2 -2
  244. package/package.json +29 -153
  245. package/src/agents/registry.mjs +0 -405
  246. package/src/awareness.mjs +0 -425
  247. package/src/brief.mjs +0 -266
  248. package/src/calibration.mjs +0 -148
  249. package/src/checkpoint.mjs +0 -109
  250. package/src/ci-triage.mjs +0 -191
  251. package/src/cognitive-loop.mjs +0 -562
  252. package/src/collaboration.mjs +0 -545
  253. package/src/context-intel.mjs +0 -158
  254. package/src/context.mjs +0 -389
  255. package/src/continuity.mjs +0 -298
  256. package/src/cost-tracker.mjs +0 -184
  257. package/src/debrief.mjs +0 -228
  258. package/src/decide.mjs +0 -1099
  259. package/src/decompose.mjs +0 -331
  260. package/src/detect.mjs +0 -702
  261. package/src/dispatch.mjs +0 -1447
  262. package/src/doctor.mjs +0 -1607
  263. package/src/envelope.mjs +0 -139
  264. package/src/failure-memory.mjs +0 -178
  265. package/src/fx.mjs +0 -276
  266. package/src/governance.mjs +0 -279
  267. package/src/handoff.mjs +0 -87
  268. package/src/head-protocol.mjs +0 -128
  269. package/src/head.mjs +0 -952
  270. package/src/health.mjs +0 -528
  271. package/src/inbox.mjs +0 -195
  272. package/src/index.mjs +0 -44
  273. package/src/install-hooks.mjs +0 -100
  274. package/src/integrity.mjs +0 -245
  275. package/src/intelligence.mjs +0 -447
  276. package/src/ledger.mjs +0 -196
  277. package/src/living-docs.mjs +0 -210
  278. package/src/memory-tiers.mjs +0 -193
  279. package/src/models.mjs +0 -363
  280. package/src/narrative.mjs +0 -169
  281. package/src/nextstep.mjs +0 -100
  282. package/src/observer.mjs +0 -241
  283. package/src/outcome.mjs +0 -400
  284. package/src/pipeline.mjs +0 -1711
  285. package/src/playbook.mjs +0 -257
  286. package/src/pr-agent.mjs +0 -214
  287. package/src/predictive.mjs +0 -250
  288. package/src/profile.mjs +0 -1411
  289. package/src/prompt-audit.mjs +0 -231
  290. package/src/prompt-intel.mjs +0 -325
  291. package/src/provider-context.mjs +0 -257
  292. package/src/receipt.mjs +0 -344
  293. package/src/recommendations.mjs +0 -296
  294. package/src/redact.mjs +0 -192
  295. package/src/replit.mjs +0 -1210
  296. package/src/repo.mjs +0 -445
  297. package/src/revert.mjs +0 -149
  298. package/src/routing-advisor.mjs +0 -204
  299. package/src/self-correct.mjs +0 -147
  300. package/src/session-lock.mjs +0 -160
  301. package/src/session.mjs +0 -1655
  302. package/src/settings-tui.mjs +0 -373
  303. package/src/setup-flow.mjs +0 -223
  304. package/src/signal.mjs +0 -115
  305. package/src/simmer.mjs +0 -241
  306. package/src/strategy.mjs +0 -235
  307. package/src/subscription.mjs +0 -212
  308. package/src/templates.mjs +0 -260
  309. package/src/think-engine.mjs +0 -428
  310. package/src/tui.mjs +0 -276
  311. package/src/update-check.mjs +0 -35
  312. package/src/wave-planner.mjs +0 -294
@@ -0,0 +1,206 @@
1
+ /**
2
+ * integrity.ts — State integrity primitives for dual-brain
3
+ *
4
+ * Provides:
5
+ * - atomicWriteJson / readJsonSafe — safe JSON file I/O with schema versioning
6
+ * - acquireLock / releaseLock / withLock — advisory file locks
7
+ * - lockedUpdate — locked atomic read-modify-write
8
+ * - atomicAppend — append-only ledger with lock
9
+ */
10
+ import { appendFileSync, existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from 'node:fs';
11
+ import { dirname } from 'node:path';
12
+ /**
13
+ * Write JSON to filePath atomically via a temp file + rename.
14
+ * Adds _schemaVersion and _writtenAt to plain objects.
15
+ */
16
+ export function atomicWriteJson(filePath, data, opts = {}) {
17
+ const { schemaVersion = 1, backup = false } = opts;
18
+ // Stamp schema version onto plain objects
19
+ if (data && typeof data === 'object' && !Array.isArray(data)) {
20
+ data._schemaVersion = schemaVersion;
21
+ data._writtenAt = new Date().toISOString();
22
+ }
23
+ const dir = dirname(filePath);
24
+ mkdirSync(dir, { recursive: true });
25
+ const tmpPath = `${filePath}.tmp.${process.pid}.${Date.now()}`;
26
+ const json = JSON.stringify(data, null, 2) + '\n';
27
+ // Write to temp file
28
+ writeFileSync(tmpPath, json);
29
+ // Validate the temp file is parseable before committing
30
+ try {
31
+ JSON.parse(readFileSync(tmpPath, 'utf8'));
32
+ }
33
+ catch (err) {
34
+ unlinkSync(tmpPath);
35
+ throw new Error(`atomicWrite: validation failed for ${filePath}: ${err.message}`);
36
+ }
37
+ // Optionally back up the existing file
38
+ if (backup && existsSync(filePath)) {
39
+ const backupPath = filePath + '.bak';
40
+ try {
41
+ renameSync(filePath, backupPath);
42
+ }
43
+ catch { }
44
+ }
45
+ // Atomic rename — either fully succeeds or the original is untouched
46
+ renameSync(tmpPath, filePath);
47
+ }
48
+ /**
49
+ * Read and parse a JSON file safely, with optional schema migration.
50
+ * Falls back to a .bak copy on parse failure.
51
+ * Returns null when the file is absent or unrecoverable.
52
+ */
53
+ export function readJsonSafe(filePath, opts = {}) {
54
+ const { expectedVersion, migrate } = opts;
55
+ if (!existsSync(filePath))
56
+ return null;
57
+ let data;
58
+ try {
59
+ data = JSON.parse(readFileSync(filePath, 'utf8'));
60
+ }
61
+ catch {
62
+ // Primary file corrupt — try backup
63
+ const bakPath = filePath + '.bak';
64
+ if (existsSync(bakPath)) {
65
+ try {
66
+ data = JSON.parse(readFileSync(bakPath, 'utf8'));
67
+ }
68
+ catch {
69
+ return null;
70
+ }
71
+ }
72
+ else {
73
+ return null;
74
+ }
75
+ }
76
+ // Schema version check with optional migration
77
+ if (expectedVersion !== undefined && data?._schemaVersion !== expectedVersion) {
78
+ if (migrate && typeof migrate === 'function') {
79
+ data = migrate(data, data?._schemaVersion, expectedVersion);
80
+ }
81
+ // Tolerant read: return data even without a migrator
82
+ }
83
+ return data;
84
+ }
85
+ // ---------------------------------------------------------------------------
86
+ // 2. Advisory file locks
87
+ // ---------------------------------------------------------------------------
88
+ const LOCK_TIMEOUT_MS = 10_000; // stale lock threshold
89
+ const LOCK_RETRY_MS = 50; // busy-wait interval
90
+ const LOCK_MAX_RETRIES = 100; // max retries (~5 s)
91
+ /**
92
+ * Acquire an advisory lock for filePath by creating filePath.lock.
93
+ * Stale locks (> LOCK_TIMEOUT_MS old) are cleared automatically.
94
+ */
95
+ export function acquireLock(filePath) {
96
+ const lockPath = filePath + '.lock';
97
+ // Clear stale or corrupt lock
98
+ if (existsSync(lockPath)) {
99
+ try {
100
+ const lockData = JSON.parse(readFileSync(lockPath, 'utf8'));
101
+ const age = Date.now() - (lockData.createdAt || 0);
102
+ if (age > LOCK_TIMEOUT_MS) {
103
+ unlinkSync(lockPath);
104
+ }
105
+ }
106
+ catch {
107
+ try {
108
+ unlinkSync(lockPath);
109
+ }
110
+ catch { }
111
+ }
112
+ }
113
+ // Spin-try to create the lock exclusively
114
+ let retries = 0;
115
+ while (retries < LOCK_MAX_RETRIES) {
116
+ try {
117
+ writeFileSync(lockPath, JSON.stringify({
118
+ pid: process.pid,
119
+ createdAt: Date.now(),
120
+ holder: process.argv[1] || 'unknown',
121
+ }), { flag: 'wx' }); // 'wx' = exclusive create, EEXIST if present
122
+ return { acquired: true, lockPath };
123
+ }
124
+ catch (err) {
125
+ if (err.code === 'EEXIST') {
126
+ retries++;
127
+ // Synchronous busy-wait — intentional; only triggered under contention
128
+ const start = Date.now();
129
+ while (Date.now() - start < LOCK_RETRY_MS) { }
130
+ continue;
131
+ }
132
+ throw err; // Unexpected error — propagate
133
+ }
134
+ }
135
+ return { acquired: false, lockPath, reason: 'timeout' };
136
+ }
137
+ /**
138
+ * Release a previously acquired lock.
139
+ */
140
+ export function releaseLock(lockResult) {
141
+ if (lockResult?.lockPath) {
142
+ try {
143
+ unlinkSync(lockResult.lockPath);
144
+ }
145
+ catch { }
146
+ }
147
+ }
148
+ /**
149
+ * Run fn while holding an advisory lock on filePath.
150
+ * Throws if the lock cannot be acquired within the retry window.
151
+ */
152
+ export function withLock(filePath, fn) {
153
+ const lock = acquireLock(filePath);
154
+ if (!lock.acquired) {
155
+ throw new Error(`Could not acquire lock for ${filePath}: ${lock.reason}`);
156
+ }
157
+ try {
158
+ return fn();
159
+ }
160
+ finally {
161
+ releaseLock(lock);
162
+ }
163
+ }
164
+ /**
165
+ * Locked atomic read-modify-write.
166
+ * Reads the current JSON, passes it to updateFn, then writes the result.
167
+ * If updateFn returns undefined the file is left unchanged.
168
+ */
169
+ export function lockedUpdate(filePath, updateFn, opts = {}) {
170
+ return withLock(filePath, () => {
171
+ const current = readJsonSafe(filePath, opts);
172
+ const updated = updateFn(current);
173
+ if (updated !== undefined) {
174
+ atomicWriteJson(filePath, updated, opts);
175
+ }
176
+ return updated;
177
+ });
178
+ }
179
+ // ---------------------------------------------------------------------------
180
+ // 3. Append-only ledger with lock
181
+ // ---------------------------------------------------------------------------
182
+ /**
183
+ * Append a NDJSON record to filePath under an advisory lock.
184
+ * On lock failure the write is attempted without a lock (best-effort).
185
+ */
186
+ export async function atomicAppend(filePath, record) {
187
+ const line = JSON.stringify(record) + '\n';
188
+ const lock = acquireLock(filePath);
189
+ if (!lock.acquired) {
190
+ // Non-fatal: best-effort append without lock
191
+ try {
192
+ mkdirSync(dirname(filePath), { recursive: true });
193
+ appendFileSync(filePath, line);
194
+ }
195
+ catch { }
196
+ return;
197
+ }
198
+ try {
199
+ mkdirSync(dirname(filePath), { recursive: true });
200
+ appendFileSync(filePath, line);
201
+ }
202
+ finally {
203
+ releaseLock(lock);
204
+ }
205
+ }
206
+ //# sourceMappingURL=integrity.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"integrity.js","sourceRoot":"","sources":["../../src/integrity.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,UAAU,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACrH,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAsBpC;;;GAGG;AACH,MAAM,UAAU,eAAe,CAAC,QAAgB,EAAE,IAAa,EAAE,OAAwB,EAAE;IACzF,MAAM,EAAE,aAAa,GAAG,CAAC,EAAE,MAAM,GAAG,KAAK,EAAE,GAAG,IAAI,CAAC;IAEnD,0CAA0C;IAC1C,IAAI,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;QAC5D,IAAgC,CAAC,cAAc,GAAG,aAAa,CAAC;QAChE,IAAgC,CAAC,UAAU,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IAC1E,CAAC;IAED,MAAM,GAAG,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC9B,SAAS,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IAEpC,MAAM,OAAO,GAAG,GAAG,QAAQ,QAAQ,OAAO,CAAC,GAAG,IAAI,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC;IAC/D,M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AI,CAAC;YACH,SAAS,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;YAClD,cAAc,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;QACjC,CAAC;QAAC,MAAM,CAAC,CAAA,CAAC;QACV,OAAO;IACT,CAAC;IAED,IAAI,CAAC;QACH,SAAS,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAClD,cAAc,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;IACjC,CAAC;YAAS,CAAC;QACT,WAAW,CAAC,IAAI,CAAC,CAAC;IACpB,CAAC;AACH,CAAC"}
@@ -0,0 +1,104 @@
1
+ /**
2
+ * intelligence.ts — Situational awareness for every pipeline run.
3
+ * Reads project reality fresh, derives task context, and detects contradictions
4
+ * between what an agent plans to do and what is actually true.
5
+ */
6
+ interface ParsedFailure {
7
+ prompt: string;
8
+ error: string;
9
+ approach: string;
10
+ timestamp: number;
11
+ }
12
+ interface ParsedOutcome {
13
+ task: string;
14
+ success: boolean;
15
+ timestamp: number;
16
+ }
17
+ export interface ProjectBrief {
18
+ packageName: string;
19
+ version: string;
20
+ versionMajor: number;
21
+ description: string;
22
+ branch: string;
23
+ dirty: boolean;
24
+ dirtyFiles: string[];
25
+ recentCommits: string[];
26
+ aheadOfRemote: number;
27
+ brandName: string;
28
+ cliCommand: string;
29
+ moduleType: string;
30
+ entryPoint: string;
31
+ testCommand: string | null;
32
+ protectedPaths: string[];
33
+ recentFailures: ParsedFailure[];
34
+ recentOutcomes: ParsedOutcome[];
35
+ derivedAt: number;
36
+ }
37
+ /**
38
+ * Read project reality fresh. No cache. Returns a ProjectBrief.
39
+ */
40
+ export declare function deriveProjectState(cwd?: string): ProjectBrief;
41
+ interface PriorAttempt {
42
+ approach?: string;
43
+ tier?: string;
44
+ failed: boolean;
45
+ reason?: string;
46
+ files?: string[];
47
+ filesChanged?: string[];
48
+ status?: string;
49
+ error?: string;
50
+ type?: string;
51
+ }
52
+ export interface TaskBrief {
53
+ task: string;
54
+ priorAttempts: Array<{
55
+ approach: string;
56
+ failed: boolean;
57
+ reason: string;
58
+ }>;
59
+ activeConstraints: string[];
60
+ filesInScope: string[];
61
+ filesOutOfScope: string[];
62
+ escalationLevel: 'normal' | 'elevated' | 'critical';
63
+ }
64
+ /**
65
+ * Derive task-scoped context from the current prompt and optional session events.
66
+ */
67
+ export declare function deriveTaskContext(task?: string, recentEvents?: Array<PriorAttempt | null>): TaskBrief;
68
+ interface Plan {
69
+ description?: string;
70
+ assumptions?: Record<string, string> | string;
71
+ targetFiles?: string[];
72
+ }
73
+ interface Contradiction {
74
+ type: string;
75
+ severity: 'block' | 'warn';
76
+ message: string;
77
+ evidence: {
78
+ expected: string;
79
+ actual: string;
80
+ };
81
+ }
82
+ /**
83
+ * Detect contradictions between project reality, task context, and a proposed plan.
84
+ * Returns an array of contradiction objects.
85
+ */
86
+ export declare function detectContradictions(projectBrief: ProjectBrief, taskBrief: TaskBrief | null, plan?: Plan): Contradiction[];
87
+ interface SessionContext {
88
+ relatedSessions?: unknown[];
89
+ riskSignals?: unknown;
90
+ priorAttempts?: Array<{
91
+ failed?: boolean;
92
+ status?: string;
93
+ daysAgo?: number;
94
+ when?: string;
95
+ error?: string;
96
+ reason?: string;
97
+ }>;
98
+ relevantFiles?: string[];
99
+ }
100
+ /**
101
+ * Format a compact situational awareness summary (max 15 lines) for agent prompts.
102
+ */
103
+ export declare function formatBrief(projectBrief: ProjectBrief, taskBrief: TaskBrief | null, sessionContext?: SessionContext | null): string;
104
+ export {};