shreni 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (269) hide show
  1. package/ARCHITECTURE.md +260 -0
  2. package/LICENSE +201 -0
  3. package/README.md +519 -0
  4. package/TRADEMARK.md +97 -0
  5. package/dist/agents/parikshaka.d.ts +11 -0
  6. package/dist/agents/parikshaka.d.ts.map +1 -0
  7. package/dist/agents/parikshaka.js +80 -0
  8. package/dist/agents/parikshaka.js.map +1 -0
  9. package/dist/agents/providers/claude.d.ts +3 -0
  10. package/dist/agents/providers/claude.d.ts.map +1 -0
  11. package/dist/agents/providers/claude.js +100 -0
  12. package/dist/agents/providers/claude.js.map +1 -0
  13. package/dist/agents/providers/codex.d.ts +3 -0
  14. package/dist/agents/providers/codex.d.ts.map +1 -0
  15. package/dist/agents/providers/codex.js +123 -0
  16. package/dist/agents/providers/codex.js.map +1 -0
  17. package/dist/agents/providers/gemini.d.ts +3 -0
  18. package/dist/agents/providers/gemini.d.ts.map +1 -0
  19. package/dist/agents/providers/gemini.js +77 -0
  20. package/dist/agents/providers/gemini.js.map +1 -0
  21. package/dist/agents/providers/index.d.ts +6 -0
  22. package/dist/agents/providers/index.d.ts.map +1 -0
  23. package/dist/agents/providers/index.js +35 -0
  24. package/dist/agents/providers/index.js.map +1 -0
  25. package/dist/agents/providers/registry.d.ts +20 -0
  26. package/dist/agents/providers/registry.d.ts.map +1 -0
  27. package/dist/agents/providers/registry.js +91 -0
  28. package/dist/agents/providers/registry.js.map +1 -0
  29. package/dist/agents/providers/types.d.ts +42 -0
  30. package/dist/agents/providers/types.d.ts.map +1 -0
  31. package/dist/agents/providers/types.js +61 -0
  32. package/dist/agents/providers/types.js.map +1 -0
  33. package/dist/agents/runner.d.ts +6 -0
  34. package/dist/agents/runner.d.ts.map +1 -0
  35. package/dist/agents/runner.js +159 -0
  36. package/dist/agents/runner.js.map +1 -0
  37. package/dist/agents/silpi.d.ts +3 -0
  38. package/dist/agents/silpi.d.ts.map +1 -0
  39. package/dist/agents/silpi.js +101 -0
  40. package/dist/agents/silpi.js.map +1 -0
  41. package/dist/agents/viharapala.d.ts +5 -0
  42. package/dist/agents/viharapala.d.ts.map +1 -0
  43. package/dist/agents/viharapala.js +128 -0
  44. package/dist/agents/viharapala.js.map +1 -0
  45. package/dist/cli/agents.d.ts +14 -0
  46. package/dist/cli/agents.d.ts.map +1 -0
  47. package/dist/cli/agents.js +47 -0
  48. package/dist/cli/agents.js.map +1 -0
  49. package/dist/cli/detect-toolchain.d.ts +10 -0
  50. package/dist/cli/detect-toolchain.d.ts.map +1 -0
  51. package/dist/cli/detect-toolchain.js +77 -0
  52. package/dist/cli/detect-toolchain.js.map +1 -0
  53. package/dist/cli/index.d.ts +3 -0
  54. package/dist/cli/index.d.ts.map +1 -0
  55. package/dist/cli/index.js +342 -0
  56. package/dist/cli/index.js.map +1 -0
  57. package/dist/cli/init-kshetra.d.ts +59 -0
  58. package/dist/cli/init-kshetra.d.ts.map +1 -0
  59. package/dist/cli/init-kshetra.js +531 -0
  60. package/dist/cli/init-kshetra.js.map +1 -0
  61. package/dist/cli/list.d.ts +11 -0
  62. package/dist/cli/list.d.ts.map +1 -0
  63. package/dist/cli/list.js +45 -0
  64. package/dist/cli/list.js.map +1 -0
  65. package/dist/cli/logs.d.ts +20 -0
  66. package/dist/cli/logs.d.ts.map +1 -0
  67. package/dist/cli/logs.js +144 -0
  68. package/dist/cli/logs.js.map +1 -0
  69. package/dist/cli/migrate.d.ts +8 -0
  70. package/dist/cli/migrate.d.ts.map +1 -0
  71. package/dist/cli/migrate.js +116 -0
  72. package/dist/cli/migrate.js.map +1 -0
  73. package/dist/cli/pause.d.ts +24 -0
  74. package/dist/cli/pause.d.ts.map +1 -0
  75. package/dist/cli/pause.js +41 -0
  76. package/dist/cli/pause.js.map +1 -0
  77. package/dist/cli/phalaka-autostart.d.ts +11 -0
  78. package/dist/cli/phalaka-autostart.d.ts.map +1 -0
  79. package/dist/cli/phalaka-autostart.js +28 -0
  80. package/dist/cli/phalaka-autostart.js.map +1 -0
  81. package/dist/cli/phalaka-server.d.ts +2 -0
  82. package/dist/cli/phalaka-server.d.ts.map +1 -0
  83. package/dist/cli/phalaka-server.js +20 -0
  84. package/dist/cli/phalaka-server.js.map +1 -0
  85. package/dist/cli/phalaka.d.ts +30 -0
  86. package/dist/cli/phalaka.d.ts.map +1 -0
  87. package/dist/cli/phalaka.js +60 -0
  88. package/dist/cli/phalaka.js.map +1 -0
  89. package/dist/cli/pid.d.ts +9 -0
  90. package/dist/cli/pid.d.ts.map +1 -0
  91. package/dist/cli/pid.js +66 -0
  92. package/dist/cli/pid.js.map +1 -0
  93. package/dist/cli/provider-preflight.d.ts +11 -0
  94. package/dist/cli/provider-preflight.d.ts.map +1 -0
  95. package/dist/cli/provider-preflight.js +70 -0
  96. package/dist/cli/provider-preflight.js.map +1 -0
  97. package/dist/cli/register.d.ts +8 -0
  98. package/dist/cli/register.d.ts.map +1 -0
  99. package/dist/cli/register.js +34 -0
  100. package/dist/cli/register.js.map +1 -0
  101. package/dist/cli/run.d.ts +3 -0
  102. package/dist/cli/run.d.ts.map +1 -0
  103. package/dist/cli/run.js +40 -0
  104. package/dist/cli/run.js.map +1 -0
  105. package/dist/cli/start.d.ts +11 -0
  106. package/dist/cli/start.d.ts.map +1 -0
  107. package/dist/cli/start.js +26 -0
  108. package/dist/cli/start.js.map +1 -0
  109. package/dist/cli/status.d.ts +39 -0
  110. package/dist/cli/status.d.ts.map +1 -0
  111. package/dist/cli/status.js +207 -0
  112. package/dist/cli/status.js.map +1 -0
  113. package/dist/cli/stop.d.ts +13 -0
  114. package/dist/cli/stop.d.ts.map +1 -0
  115. package/dist/cli/stop.js +18 -0
  116. package/dist/cli/stop.js.map +1 -0
  117. package/dist/cli/sync.d.ts +6 -0
  118. package/dist/cli/sync.d.ts.map +1 -0
  119. package/dist/cli/sync.js +31 -0
  120. package/dist/cli/sync.js.map +1 -0
  121. package/dist/cli/tail.d.ts +6 -0
  122. package/dist/cli/tail.d.ts.map +1 -0
  123. package/dist/cli/tail.js +170 -0
  124. package/dist/cli/tail.js.map +1 -0
  125. package/dist/cli/telemetry.d.ts +2 -0
  126. package/dist/cli/telemetry.d.ts.map +1 -0
  127. package/dist/cli/telemetry.js +42 -0
  128. package/dist/cli/telemetry.js.map +1 -0
  129. package/dist/cli/verify-hooks.d.ts +12 -0
  130. package/dist/cli/verify-hooks.d.ts.map +1 -0
  131. package/dist/cli/verify-hooks.js +38 -0
  132. package/dist/cli/verify-hooks.js.map +1 -0
  133. package/dist/cli/worker.d.ts +2 -0
  134. package/dist/cli/worker.d.ts.map +1 -0
  135. package/dist/cli/worker.js +211 -0
  136. package/dist/cli/worker.js.map +1 -0
  137. package/dist/kshetra/config.d.ts +64 -0
  138. package/dist/kshetra/config.d.ts.map +1 -0
  139. package/dist/kshetra/config.js +173 -0
  140. package/dist/kshetra/config.js.map +1 -0
  141. package/dist/kshetra/registry.d.ts +5 -0
  142. package/dist/kshetra/registry.d.ts.map +1 -0
  143. package/dist/kshetra/registry.js +69 -0
  144. package/dist/kshetra/registry.js.map +1 -0
  145. package/dist/kshetra/state.d.ts +57 -0
  146. package/dist/kshetra/state.d.ts.map +1 -0
  147. package/dist/kshetra/state.js +195 -0
  148. package/dist/kshetra/state.js.map +1 -0
  149. package/dist/kshetra/toolchain.d.ts +20 -0
  150. package/dist/kshetra/toolchain.d.ts.map +1 -0
  151. package/dist/kshetra/toolchain.js +146 -0
  152. package/dist/kshetra/toolchain.js.map +1 -0
  153. package/dist/phalaka/api.d.ts +123 -0
  154. package/dist/phalaka/api.d.ts.map +1 -0
  155. package/dist/phalaka/api.js +196 -0
  156. package/dist/phalaka/api.js.map +1 -0
  157. package/dist/phalaka/beads-read.d.ts +49 -0
  158. package/dist/phalaka/beads-read.d.ts.map +1 -0
  159. package/dist/phalaka/beads-read.js +174 -0
  160. package/dist/phalaka/beads-read.js.map +1 -0
  161. package/dist/phalaka/pid.d.ts +6 -0
  162. package/dist/phalaka/pid.d.ts.map +1 -0
  163. package/dist/phalaka/pid.js +48 -0
  164. package/dist/phalaka/pid.js.map +1 -0
  165. package/dist/phalaka/server.d.ts +10 -0
  166. package/dist/phalaka/server.d.ts.map +1 -0
  167. package/dist/phalaka/server.js +30 -0
  168. package/dist/phalaka/server.js.map +1 -0
  169. package/dist/phalaka/token.d.ts +5 -0
  170. package/dist/phalaka/token.d.ts.map +1 -0
  171. package/dist/phalaka/token.js +40 -0
  172. package/dist/phalaka/token.js.map +1 -0
  173. package/dist/phalaka/ui.d.ts +15 -0
  174. package/dist/phalaka/ui.d.ts.map +1 -0
  175. package/dist/phalaka/ui.js +233 -0
  176. package/dist/phalaka/ui.js.map +1 -0
  177. package/dist/sthapathi/activity-log.d.ts +68 -0
  178. package/dist/sthapathi/activity-log.d.ts.map +1 -0
  179. package/dist/sthapathi/activity-log.js +57 -0
  180. package/dist/sthapathi/activity-log.js.map +1 -0
  181. package/dist/sthapathi/beads.d.ts +27 -0
  182. package/dist/sthapathi/beads.d.ts.map +1 -0
  183. package/dist/sthapathi/beads.js +153 -0
  184. package/dist/sthapathi/beads.js.map +1 -0
  185. package/dist/sthapathi/branch.d.ts +5 -0
  186. package/dist/sthapathi/branch.d.ts.map +1 -0
  187. package/dist/sthapathi/branch.js +20 -0
  188. package/dist/sthapathi/branch.js.map +1 -0
  189. package/dist/sthapathi/dispatch.d.ts +14 -0
  190. package/dist/sthapathi/dispatch.d.ts.map +1 -0
  191. package/dist/sthapathi/dispatch.js +326 -0
  192. package/dist/sthapathi/dispatch.js.map +1 -0
  193. package/dist/sthapathi/errors.d.ts +27 -0
  194. package/dist/sthapathi/errors.d.ts.map +1 -0
  195. package/dist/sthapathi/errors.js +174 -0
  196. package/dist/sthapathi/errors.js.map +1 -0
  197. package/dist/sthapathi/gh.d.ts +19 -0
  198. package/dist/sthapathi/gh.d.ts.map +1 -0
  199. package/dist/sthapathi/gh.js +69 -0
  200. package/dist/sthapathi/gh.js.map +1 -0
  201. package/dist/sthapathi/git.d.ts +41 -0
  202. package/dist/sthapathi/git.d.ts.map +1 -0
  203. package/dist/sthapathi/git.js +199 -0
  204. package/dist/sthapathi/git.js.map +1 -0
  205. package/dist/sthapathi/guard.d.ts +24 -0
  206. package/dist/sthapathi/guard.d.ts.map +1 -0
  207. package/dist/sthapathi/guard.js +64 -0
  208. package/dist/sthapathi/guard.js.map +1 -0
  209. package/dist/sthapathi/health.d.ts +23 -0
  210. package/dist/sthapathi/health.d.ts.map +1 -0
  211. package/dist/sthapathi/health.js +142 -0
  212. package/dist/sthapathi/health.js.map +1 -0
  213. package/dist/sthapathi/index.d.ts +21 -0
  214. package/dist/sthapathi/index.d.ts.map +1 -0
  215. package/dist/sthapathi/index.js +88 -0
  216. package/dist/sthapathi/index.js.map +1 -0
  217. package/dist/sthapathi/lifecycle.d.ts +7 -0
  218. package/dist/sthapathi/lifecycle.d.ts.map +1 -0
  219. package/dist/sthapathi/lifecycle.js +43 -0
  220. package/dist/sthapathi/lifecycle.js.map +1 -0
  221. package/dist/sthapathi/lint.d.ts +8 -0
  222. package/dist/sthapathi/lint.d.ts.map +1 -0
  223. package/dist/sthapathi/lint.js +33 -0
  224. package/dist/sthapathi/lint.js.map +1 -0
  225. package/dist/sthapathi/merge.d.ts +18 -0
  226. package/dist/sthapathi/merge.d.ts.map +1 -0
  227. package/dist/sthapathi/merge.js +259 -0
  228. package/dist/sthapathi/merge.js.map +1 -0
  229. package/dist/sthapathi/notifications.d.ts +14 -0
  230. package/dist/sthapathi/notifications.d.ts.map +1 -0
  231. package/dist/sthapathi/notifications.js +57 -0
  232. package/dist/sthapathi/notifications.js.map +1 -0
  233. package/dist/sthapathi/parikshaka-dispatch.d.ts +12 -0
  234. package/dist/sthapathi/parikshaka-dispatch.d.ts.map +1 -0
  235. package/dist/sthapathi/parikshaka-dispatch.js +0 -0
  236. package/dist/sthapathi/parikshaka-dispatch.js.map +1 -0
  237. package/dist/sthapathi/pickup.d.ts +13 -0
  238. package/dist/sthapathi/pickup.d.ts.map +1 -0
  239. package/dist/sthapathi/pickup.js +144 -0
  240. package/dist/sthapathi/pickup.js.map +1 -0
  241. package/dist/sthapathi/recover.d.ts +16 -0
  242. package/dist/sthapathi/recover.d.ts.map +1 -0
  243. package/dist/sthapathi/recover.js +149 -0
  244. package/dist/sthapathi/recover.js.map +1 -0
  245. package/dist/sthapathi/retry.d.ts +11 -0
  246. package/dist/sthapathi/retry.d.ts.map +1 -0
  247. package/dist/sthapathi/retry.js +42 -0
  248. package/dist/sthapathi/retry.js.map +1 -0
  249. package/dist/sthapathi/self-heal.d.ts +19 -0
  250. package/dist/sthapathi/self-heal.d.ts.map +1 -0
  251. package/dist/sthapathi/self-heal.js +46 -0
  252. package/dist/sthapathi/self-heal.js.map +1 -0
  253. package/dist/sthapathi/types.d.ts +56 -0
  254. package/dist/sthapathi/types.d.ts.map +1 -0
  255. package/dist/sthapathi/types.js +4 -0
  256. package/dist/sthapathi/types.js.map +1 -0
  257. package/dist/sthapathi/watchdog.d.ts +27 -0
  258. package/dist/sthapathi/watchdog.d.ts.map +1 -0
  259. package/dist/sthapathi/watchdog.js +145 -0
  260. package/dist/sthapathi/watchdog.js.map +1 -0
  261. package/dist/telemetry/telemetry.d.ts +42 -0
  262. package/dist/telemetry/telemetry.d.ts.map +1 -0
  263. package/dist/telemetry/telemetry.js +189 -0
  264. package/dist/telemetry/telemetry.js.map +1 -0
  265. package/dist/test-setup.d.ts +2 -0
  266. package/dist/test-setup.d.ts.map +1 -0
  267. package/dist/test-setup.js +16 -0
  268. package/dist/test-setup.js.map +1 -0
  269. package/package.json +71 -0
@@ -0,0 +1,260 @@
1
+ # Architecture
2
+
3
+ This document describes how Shreni works internally — the components, the worker
4
+ lifecycle, the git workflow, and the resilience machinery that lets it run
5
+ unattended. For a quick orientation and setup, see the [README](README.md).
6
+
7
+ Shreni is an **autonomous coding-agent harness**: it picks up structured tasks,
8
+ writes and reviews code, runs tests, and merges approved changes to `main`
9
+ without a human in the inner loop. The design goal throughout is *unattended
10
+ correctness* — the system must make progress on its own and, when it can't, fail
11
+ loudly and recover cleanly rather than wedge silently.
12
+
13
+ ## Components
14
+
15
+ Shreni names its parts in Sanskrit; each maps to a directory under `src/`.
16
+
17
+ | Component | Code | Role |
18
+ |---|---|---|
19
+ | **Sthapathi** (architect) | [`src/sthapathi/`](src/sthapathi/) | Orchestrator. Owns the task lifecycle, the git workflow, and the poll loop. The only caller of `bd --claim` / `bd close`. |
20
+ | **Silpi** (craftsman) | [`src/agents/silpi.ts`](src/agents/silpi.ts) | Coding agent. Given a task with injected context, writes implementation + unit tests, runs lint/tests, submits for review. |
21
+ | **Viharapala** (guardian) | [`src/agents/viharapala.ts`](src/agents/viharapala.ts) | Review agent. Judges Silpi's output against acceptance criteria, quality, and coverage; returns `APPROVE` / `REJECT` with structured feedback. |
22
+ | **Parikshaka** (examiner) | [`src/agents/parikshaka.ts`](src/agents/parikshaka.ts) | Test agent. Runs asynchronously after merge; backfills tests for shipped work and files coverage-gap beads. Read-only w.r.t. source. |
23
+ | **Phalaka** (panel) | [`src/phalaka/`](src/phalaka/) | Loopback dashboard. Serves worker status, task progress, and stuck-state alerts across all Kshetras. |
24
+
25
+ The three worker agents (Silpi, Viharapala, Parikshaka) are **never allowed to
26
+ touch the task tracker** — Sthapathi is the sole authority over bead state. Agents
27
+ receive everything they need as injected prompt context and return structured
28
+ output.
29
+
30
+ ## The Kshetra: the unit of isolation
31
+
32
+ Every project Shreni manages is a **Kshetra** (Sanskrit: *field*). A Kshetra owns:
33
+
34
+ - its own git repository,
35
+ - its own `bd` (Beads) task database,
36
+ - its own configuration, and
37
+ - its own worker process and phase state.
38
+
39
+ Kshetras are fully isolated — no shared context, tasks, or git state. A crash in
40
+ one never affects another.
41
+
42
+ **Configuration has exactly one source of truth per Kshetra:**
43
+ `<repo>/.shreni/kshetra.yaml`, validated by a Zod schema
44
+ ([`src/kshetra/config.ts`](src/kshetra/config.ts)). A single global registry,
45
+ `~/.shreni/registry.json`, is the only thing that resolves `id → configPath`
46
+ ([`src/kshetra/registry.ts`](src/kshetra/registry.ts)). Paths in the config are
47
+ absolute and used verbatim as the cwd for git/exec.
48
+
49
+ Runtime state (worker phase, pause/stuck flags, stall counters) lives under
50
+ `~/.shreni/` — nothing is emitted off-box.
51
+
52
+ ## Process model
53
+
54
+ `shreni start` launches **one worker process per Kshetra**
55
+ ([`src/cli/worker.ts`](src/cli/worker.ts)). Each worker has its own PID and logs
56
+ under `~/.shreni/`, so one Kshetra hanging or crashing never takes the others
57
+ down. The CLI ([`src/cli/index.ts`](src/cli/index.ts)) is the entry point for all
58
+ commands (`start`, `stop`, `status`, `pause`, `resume`, `run`, `sync`,
59
+ `init-kshetra`, `register`, `phalaka`, …).
60
+
61
+ ## The worker lifecycle
62
+
63
+ The heart of the system is a small phase machine
64
+ ([`src/sthapathi/index.ts`](src/sthapathi/index.ts)). A worker polls every 30s
65
+ (`DEFAULT_INTERVAL_MS`) and runs one cycle at a time:
66
+
67
+ ```mermaid
68
+ stateDiagram-v2
69
+ [*] --> IDLE
70
+ IDLE --> SELECTING: poll tick
71
+ SELECTING --> IDLE: no ready work
72
+ SELECTING --> PREPARING: task selected
73
+ PREPARING --> IDLE: preflight/health rejected
74
+ PREPARING --> WORKING: claimed & work-tree ready
75
+ WORKING --> IDLE: cycle complete (merged, rejected, or error-handled)
76
+ ```
77
+
78
+ This diagram is not just documentation: the same edges are the typed transition
79
+ table in [`src/sthapathi/lifecycle.ts`](src/sthapathi/lifecycle.ts), and
80
+ `setPhase` consults its `canTransition` guard on every phase change. An illegal
81
+ jump — most importantly a *write-only latch* (a phase with no edge back to
82
+ `IDLE`, the Watchdog-ARD bug class) — is a unit-test failure and a runtime
83
+ warning. It is deliberately a lightweight table, not a state-machine library: bd,
84
+ git, and `state.json` stay the durable sources of truth; a heavier engine is
85
+ deferred to post-launch.
86
+
87
+ Two invariants make this safe under a repeating timer:
88
+
89
+ 1. **One task at a time is structural, not emergent.** A cycle runs *only* from
90
+ `IDLE`, and the phase is advanced synchronously before the first `await`, so an
91
+ overlapping tick for the same Kshetra is an immediate no-op. A single-flight
92
+ latch in `scheduleLoop` additionally skips ticks while a cycle is running.
93
+ 2. **SELECT is read-only; PREPARE is the only mutator.** Choosing the next task
94
+ performs no git operations and no claim, so polling for work can never check
95
+ out `main` underneath an in-flight agent. Only once a task advances to PREPARE
96
+ does the worker touch the work tree. This separation is what eliminates the
97
+ class of "agent knocked off its branch" failures.
98
+
99
+ ### SELECT — pick the next task
100
+
101
+ [`selectNext`](src/sthapathi/pickup.ts) reads `bd ready` and picks the
102
+ highest-priority bead (P0 first, then FIFO within a priority). No side effects.
103
+
104
+ ### PREPARE — claim and set up the work tree
105
+
106
+ [`prepareTask`](src/sthapathi/pickup.ts) is the only mutator in the pickup path.
107
+ In order, it:
108
+
109
+ 1. syncs the beads DB (commit local → pull --rebase → push),
110
+ 2. runs **preflight**: `checkout main`, guard against a dirty tree, `pull --rebase`,
111
+ and guard against a leftover `bead-{id}/{slug}` branch,
112
+ 3. runs the **health gate**: a feature task only starts when the base test suite
113
+ is green (modulo an accepted baseline). A red base does **not** start the task —
114
+ it queues a P0 `[shreni-health]` repair bead (which is exempt from the gate),
115
+ 4. claims the bead (`bd update --claim`).
116
+
117
+ Every rejection is logged and recorded as a *stall* so the watchdog can trip if
118
+ the same rejection repeats — a wedge is never silent.
119
+
120
+ ### WORK — the Silpi ↔ Viharapala loop
121
+
122
+ [`runSilpiViharapalaLoop`](src/sthapathi/dispatch.ts) runs the review loop for the
123
+ prepared task on its own `bead-{id}/{slug}` branch:
124
+
125
+ 1. **Silpi** implements the task (code + unit tests) and runs the configured lint
126
+ and test gates.
127
+ 2. **Viharapala** reviews the branch and returns `APPROVE` or `REJECT` with
128
+ structured feedback.
129
+ 3. On `REJECT`, Silpi is re-dispatched with the feedback, up to
130
+ `agents.maxRoundsPerBead` rounds (default 3).
131
+ 4. On `APPROVE`, the outcome depends on `repo.mergePolicy`
132
+ ([`src/sthapathi/merge.ts`](src/sthapathi/merge.ts)):
133
+ - `push` (default): the branch is squash-merged to `main` and the bead is closed.
134
+ - `pr`: the branch is pushed and a pull request is opened; the bead is kept open
135
+ (labelled `awaiting-merge`) so dependents stay blocked, and is closed later by
136
+ the reconcile pass only when its PR actually merges. `resolveMergePolicy` lets
137
+ `SHRENI_MERGE_POLICY` override the config at runtime.
138
+
139
+ After a successful merge, **Parikshaka** is dispatched asynchronously
140
+ ([`src/sthapathi/parikshaka-dispatch.ts`](src/sthapathi/parikshaka-dispatch.ts)) —
141
+ it backfills tests and files coverage-gap beads without blocking the loop.
142
+
143
+ ## The git workflow
144
+
145
+ Sthapathi owns all git operations ([`src/sthapathi/git.ts`](src/sthapathi/git.ts))
146
+ so agents never manipulate history directly:
147
+
148
+ - Each task gets an isolated branch `bead-{id}/{slug}`.
149
+ - Approved work is **squash-merged** to `main` — one clean commit per bead — or,
150
+ under `mergePolicy: pr`, opened as a pull request and reconciled on merge.
151
+ - `safePush` handles a non-fast-forward push by `pull --rebase`-ing and retrying.
152
+ - Merge conflicts are triaged
153
+ ([`handleMergeConflict`](src/sthapathi/merge.ts)): conflicts confined to the
154
+ task's own files re-dispatch Silpi with conflict context; conflicts in
155
+ out-of-scope files flag the bead and pause for a human, because that signals the
156
+ agent drifted.
157
+ - A [branch-isolation guard](src/sthapathi/guard.ts) enforces that agents cannot
158
+ land commits directly on `main`.
159
+
160
+ ## Task tracking (Beads)
161
+
162
+ Tasks are **beads**, tracked by the `bd` CLI in an embedded, git-synced database
163
+ ([`src/sthapathi/beads.ts`](src/sthapathi/beads.ts)). The wrapper is internal-only
164
+ — agents never call `bd`. Sthapathi is the sole caller of `--claim` and `close`,
165
+ which keeps task-state transitions single-writer and auditable. Interactive
166
+ sessions (e.g. Claude Code) may *file* tasks but cannot claim or close them.
167
+
168
+ ## Provider abstraction
169
+
170
+ Shreni is model-agnostic behind a small adapter seam
171
+ ([`src/agents/providers/types.ts`](src/agents/providers/types.ts)):
172
+
173
+ ```ts
174
+ interface ProviderAdapter {
175
+ readonly name: Provider; // 'anthropic' | 'gemini' | 'openai'
176
+ buildSpawn(opts: AgentRunnerOpts): SpawnSpec; // how to launch the CLI
177
+ createParser(opts, emit): StreamParser; // how to parse its stream
178
+ }
179
+ ```
180
+
181
+ Adapters (`claude` / `codex` / `gemini`) live behind a registry
182
+ ([`src/agents/providers/registry.ts`](src/agents/providers/registry.ts)) and are
183
+ selected per Kshetra via `agents.provider` / `agents.model`. Execution is
184
+ **native**: the provider's own CLI loads the repo's instruction files (skills,
185
+ rules, conventions), and Shreni injects only what the provider can't load itself
186
+ (e.g. a reviewer-only review guide). A `StreamParser` turns the CLI's streamed
187
+ output into a structured result and counts tool calls; `finalize` throws on
188
+ transport/agent error so the dispatcher's retry logic can react.
189
+
190
+ ## Resilience: staying unattended
191
+
192
+ Running without a human in the loop means the system must detect and recover from
193
+ its own failures.
194
+
195
+ ### Liveness and the watchdog
196
+
197
+ Each worker stamps a **heartbeat file** on a fixed cadence while a phase is active
198
+ ([`src/sthapathi/activity-log.ts`](src/sthapathi/activity-log.ts)). Liveness is the
199
+ age of that file's mtime — deliberately decoupled from agent output, so a long
200
+ silent tool call doesn't read as a hang.
201
+
202
+ The **watchdog** ([`src/sthapathi/watchdog.ts`](src/sthapathi/watchdog.ts)) runs
203
+ every 60s and trips on either of two independent conditions:
204
+
205
+ - **Liveness timeout** — no heartbeat for `stuckThresholdMs` (default 20m) *while a
206
+ phase is active*.
207
+ - **Stall loop** — the same non-advancing outcome repeated `maxOutcomeRepeat`
208
+ times (default 5), e.g. the same preflight rejection every poll.
209
+
210
+ A Kshetra that is simply **idle with an empty queue never trips** — "nothing to
211
+ do" is distinguished from "hung" by probing the raw ready queue. On a genuine
212
+ trip, the watchdog sets a stuck banner, pauses for manual resume, and emits an
213
+ operator notification with concrete remediation steps.
214
+
215
+ ### Errors
216
+
217
+ Cycle errors are classified and handled
218
+ ([`src/sthapathi/errors.ts`](src/sthapathi/errors.ts)): `API_DOWN` pauses with a
219
+ cooldown and retries; `AGENT_FAILED` / `MALFORMED_OUTPUT` flag the bead and clean
220
+ the branch; `GIT_FAILED` keeps the branch for inspection and pauses for a human;
221
+ `BD_FAILED` pauses. Every terminal state also emits an operator notification. The
222
+ notification feed is durable and per-Kshetra — Phalaka polls it.
223
+
224
+ ### Recovery and self-heal
225
+
226
+ State can drift across a crash/restart along four axes: the working tree, stale
227
+ `bead-*` branches, orphaned `in_progress` beads, and the persisted phase.
228
+ **RECOVER** ([`src/sthapathi/recover.ts`](src/sthapathi/recover.ts)) reconciles all
229
+ four back to a clean `IDLE` and reopens stranded WIP for a fresh, gated pickup. It
230
+ runs at startup *before the poll loop is armed*, so recovery never races a poll
231
+ tick.
232
+
233
+ The same machinery powers **in-process self-heal**
234
+ ([`src/sthapathi/self-heal.ts`](src/sthapathi/self-heal.ts)): when `shreni resume`
235
+ clears a stuck worker's pause, an ordered sequence — refresh liveness → abort the
236
+ hung provider subprocess → await the run fully unwinding → RECOVER → refresh
237
+ liveness — heals a wedged worker without a restart. The ordering is load-bearing:
238
+ liveness is refreshed first so the watchdog can't re-trip mid-heal, and a `healing`
239
+ gate makes SELECT return `null` so no poll cycle mutates the tree during recovery.
240
+
241
+ ## Local observability: Phalaka
242
+
243
+ [`Phalaka`](src/phalaka/) is a single-file dashboard served on loopback
244
+ (`127.0.0.1`) by Fastify. It reads worker state, bead data, and the notification
245
+ feed, and renders status/progress/stuck alerts across all Kshetras. It is
246
+ token-authenticated (`~/.shreni/shreni.token`) and serves no data off-box. The CLI
247
+ also exposes the same information textually (`shreni status`, `shreni agents`,
248
+ `shreni logs`).
249
+
250
+ ## Directory map
251
+
252
+ ```
253
+ src/
254
+ ├── cli/ # command entry points; worker.ts drives one Kshetra
255
+ ├── sthapathi/ # orchestrator: scheduler, pickup, dispatch, git, merge,
256
+ │ # watchdog, recover, self-heal, errors, beads wrapper
257
+ ├── agents/ # silpi, viharapala, parikshaka + provider adapters
258
+ ├── kshetra/ # config schema, registry, runtime state, toolchain defaults
259
+ └── phalaka/ # loopback dashboard (server, api, ui)
260
+ ```
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or Derivative
95
+ Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 TeakWood
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.