neurastream 0.3.18 → 0.3.21

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 (66) hide show
  1. package/README.md +88 -490
  2. package/package.json +1 -1
  3. package/src/adapters/index.ts +3 -2
  4. package/src/adapters/omp/adapter.ts +2 -1
  5. package/src/adapters/omp/auto-close.ts +64 -0
  6. package/src/adapters/omp/chat-commands.ts +161 -0
  7. package/src/adapters/omp/inbox.ts +221 -0
  8. package/src/adapters/omp/index.ts +138 -1050
  9. package/src/adapters/omp/launch.ts +35 -9
  10. package/src/adapters/omp/rate-limit-controller.ts +196 -0
  11. package/src/adapters/omp/rollover-controller.ts +334 -0
  12. package/src/adapters/omp/types.ts +143 -0
  13. package/src/cli/claim.ts +1 -3
  14. package/src/cli/commands.ts +33 -2
  15. package/src/cli/dispatch.ts +55 -30
  16. package/src/cli/guard.ts +0 -3
  17. package/src/cli/incident.ts +2 -2
  18. package/src/cli/metrics.ts +1 -1
  19. package/src/cli/recall.ts +6 -8
  20. package/src/cli/results-build.ts +2 -4
  21. package/src/cli/workspace.ts +13 -5
  22. package/src/core/adapter.ts +16 -0
  23. package/src/core/autonomy.ts +24 -21
  24. package/src/core/blockers.ts +326 -30
  25. package/src/core/file-lock.ts +71 -12
  26. package/src/core/guards/plan-edit.ts +338 -0
  27. package/src/core/guards/planner.ts +70 -0
  28. package/src/core/guards/review.ts +391 -0
  29. package/src/core/guards/spawn.ts +699 -0
  30. package/src/core/guards/state.ts +581 -0
  31. package/src/core/guards/stop.ts +235 -0
  32. package/src/core/guards/tool.ts +541 -0
  33. package/src/core/guards.ts +12 -2661
  34. package/src/core/incidents.ts +134 -99
  35. package/src/core/machine-guard.ts +23 -5
  36. package/src/core/main-start.ts +11 -9
  37. package/src/core/mr-sweep.ts +4 -2
  38. package/src/core/notify.ts +15 -15
  39. package/src/core/owner-log.ts +19 -17
  40. package/src/core/owner-topics.ts +38 -48
  41. package/src/core/owner-ui/auth.ts +164 -0
  42. package/src/core/owner-ui/data.ts +437 -0
  43. package/src/core/owner-ui/handlers.ts +250 -0
  44. package/src/core/owner-ui/messages.ts +209 -0
  45. package/src/core/owner-ui/render.ts +714 -0
  46. package/src/core/owner-ui/results.ts +266 -0
  47. package/src/core/owner-ui/server.ts +142 -0
  48. package/src/core/owner-ui.ts +11 -2118
  49. package/src/core/page-notes.ts +25 -0
  50. package/src/core/pages.ts +452 -78
  51. package/src/core/plan-approval.ts +24 -26
  52. package/src/core/queue/lib.ts +170 -137
  53. package/src/core/queue-cancel.ts +29 -15
  54. package/src/core/queue-check.ts +5 -7
  55. package/src/core/queue-close.ts +54 -29
  56. package/src/core/queue-finished.ts +9 -10
  57. package/src/core/queue-migrate.ts +1 -3
  58. package/src/core/queue-trim.ts +2 -4
  59. package/src/core/results.ts +0 -2
  60. package/src/core/rollover.ts +2 -3
  61. package/src/core/secret-redact.ts +65 -14
  62. package/src/core/tailscale.ts +79 -3
  63. package/src/core/team-client.ts +18 -6
  64. package/src/core/uninstall.ts +2 -0
  65. package/src/core/watch.ts +3 -2
  66. package/src/core/queue/env.ts +0 -14
@@ -4,6 +4,7 @@
4
4
  // has no record with the plan's current hash; a plan with no page predates the convention and is left alone, unless it is stamped at or after approval.since (plan 20261029_1455 P3), when it is refused until its page is published.
5
5
  import { appendFileSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
6
6
  import { basename, join, resolve } from "node:path";
7
+ import { withFileLock, writeAtomicSync } from "./file-lock.ts";
7
8
  import * as q from "./queue/lib.ts";
8
9
  import type { Config } from "./config.ts";
9
10
 
@@ -83,44 +84,41 @@ export interface PlanApproval {
83
84
  page: string;
84
85
  }
85
86
 
86
- function planApprovalsFile(): string {
87
- return join(q.STATE_DIR, "plan-approvals.json");
87
+ function planApprovalsFile(paths: Pick<q.QueuePaths, "queue_state">): string {
88
+ return join(paths.queue_state, "plan-approvals.json");
88
89
  }
89
90
 
90
- function readPlanApprovals(): Record<string, PlanApproval> {
91
+ function readPlanApprovals(paths: Pick<q.QueuePaths, "queue_state">): Record<string, PlanApproval> {
91
92
  try {
92
- const parsed = JSON.parse(readFileSync(planApprovalsFile(), "utf8"));
93
+ const parsed = JSON.parse(readFileSync(planApprovalsFile(paths), "utf8"));
93
94
  if (parsed?.approvals && typeof parsed.approvals === "object") return parsed.approvals as Record<string, PlanApproval>;
94
95
  } catch {
95
96
  }
96
97
  return {};
97
98
  }
98
99
 
99
- function writePlanApprovals(approvals: Record<string, PlanApproval>): boolean {
100
+ function readPlanApproval(file: string, paths: Pick<q.QueuePaths, "queue_state">): PlanApproval | null {
101
+ return readPlanApprovals(paths)[file] ?? null;
102
+ }
103
+
104
+ /** Record one approval atomically under lock. Throws when the queue state cannot be written, so nobody reports "approved" for a record that is not there. */
105
+ function recordApproval(file: string, approval: PlanApproval, paths: Pick<q.QueuePaths, "queue_state">): void {
106
+ const target = planApprovalsFile(paths);
100
107
  try {
101
- mkdirSync(q.STATE_DIR, { recursive: true });
102
- writeFileSync(planApprovalsFile(), JSON.stringify({ approvals }, null, 1));
103
- return true;
108
+ withFileLock(`${target}.lock`, () => {
109
+ const approvals = readPlanApprovals(paths);
110
+ approvals[file] = approval;
111
+ writeAtomicSync(target, JSON.stringify({ approvals }, null, 1));
112
+ });
104
113
  } catch {
105
- return false;
114
+ throw new Error(`cannot write ${target}`);
106
115
  }
107
116
  }
108
117
 
109
- function readPlanApproval(file: string): PlanApproval | null {
110
- return readPlanApprovals()[file] ?? null;
111
- }
112
-
113
- /** Record one approval. Throws when the queue state cannot be written, so nobody reports "approved" for a record that is not there. */
114
- function recordApproval(file: string, approval: PlanApproval): void {
115
- const approvals = readPlanApprovals();
116
- approvals[file] = approval;
117
- if (!writePlanApprovals(approvals)) throw new Error(`cannot write ${planApprovalsFile()}`);
118
- }
119
-
120
118
  /** The owner's click on a plan page: record a hash of the plan text in the queue state and return the page it approved. The plan file itself is never edited. */
121
- export function approvePlanFile(file: string, page: string, now = new Date()): string {
119
+ export function approvePlanFile(file: string, page: string, paths: Pick<q.QueuePaths, "queue_state">, now = new Date()): string {
122
120
  const text = readFileSync(file, "utf8");
123
- recordApproval(file, { hash: reviewHash(text), at: now.getTime(), via: "page", page });
121
+ recordApproval(file, { hash: reviewHash(text), at: now.getTime(), via: "page", page }, paths);
124
122
  return `approved ${page}`;
125
123
  }
126
124
 
@@ -139,12 +137,12 @@ export function chatApprove(text: string, cfg: Config, source: string): ChatAppr
139
137
  const m = APPROVE_CMD.exec(text.trim());
140
138
  if (!m) return { applied: false };
141
139
  const stamp = m[1];
142
- const matches = q.activePlans().filter(p => p.status === "open" && basename(p.file).startsWith(`${stamp}_`));
140
+ const matches = q.activePlans(cfg.paths).filter(p => p.status === "open" && basename(p.file).startsWith(`${stamp}_`));
143
141
  const [plan, second] = matches;
144
142
  if (!plan) return { applied: false, problem: `no open plan ${stamp}` };
145
143
  if (second) return { applied: false, problem: `${matches.length} open plans match ${stamp}; nothing approved` };
146
144
  try {
147
- recordApproval(plan.file, { hash: reviewHash(plan.text), at: Date.now(), via: "chat", page: pageOfPlan(cfg.pages.dir, plan.file) ?? "" });
145
+ recordApproval(plan.file, { hash: reviewHash(plan.text), at: Date.now(), via: "chat", page: pageOfPlan(cfg.pages.dir, plan.file) ?? "" }, cfg.paths);
148
146
  } catch (e) {
149
147
  return { applied: false, problem: `plan ${stamp} not approved: ${e instanceof Error ? e.message : String(e)}` };
150
148
  }
@@ -155,7 +153,7 @@ export function chatApprove(text: string, cfg: Config, source: string): ChatAppr
155
153
  }
156
154
 
157
155
  /** Why a spawn may not run on this plan, or null. Approval off (`since === ""`) approves everything, page or not (plan 20261031_1207 P5: the reviewer agent replaced the hard gate). Otherwise a plan with a page in Base needs a record with its current hash, and a plan stamped at or after `since` needs a page too (plan 20261029_1455 P3): with no page it is refused until the page is published. A file with no stamp counts as stamped after `since`. */
158
- export function approvalProblem(text: string, file: string, pagesDir: string, since: string): string | null {
156
+ export function approvalProblem(text: string, file: string, pagesDir: string, since: string, paths: Pick<q.QueuePaths, "queue_state">): string | null {
159
157
  if (since === "") return null;
160
158
  const page = pageOfPlan(pagesDir, file);
161
159
  const stamp = /^\d{8}_\d{4}/.exec(basename(file))?.[0] ?? null;
@@ -164,7 +162,7 @@ export function approvalProblem(text: string, file: string, pagesDir: string, si
164
162
  if (!gated) return null; // predates the convention
165
163
  return `plan ${file} has no page: publish its page with neurastream pages publish <page.html> --plan ${file}`;
166
164
  }
167
- const rec = readPlanApproval(file);
165
+ const rec = readPlanApproval(file, paths);
168
166
  if (rec !== null && rec.hash === reviewHash(text)) return null;
169
167
  const hint = stamp !== null ? `, or types: approve ${stamp}` : "";
170
168
  return `plan ${file} has no approval record. The owner approves it from ${page} in Base${hint}.`;