neurastream 0.2.19 → 0.2.20

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.
package/README.md CHANGED
@@ -176,8 +176,8 @@ neurastream notify test # posts one line to every configured channel; exit 1 w
176
176
  Guard rules around it:
177
177
 
178
178
  - No agent (Main included) can read a configured secret file, list its directory, or glob over it — unlock or not. Writes to the secret files and to `~/.config/neurastream/config.yml` need the owner's unlock like any guard file.
179
- - A stop the guard blocks posts nothing; when Main continues and really hands back later, that later stop posts the final text. A subagent session never posts, and a relaunch (the launcher's rollover) posts nothing.
180
- - The post never delays the stop decision: it times out at 5 seconds per chunk, makes one attempt, and writes one guard-log line when a channel fails. Long replies are split per channel (Discord 2000, Slack 3000 characters) on line boundaries where possible.
179
+ - A blocked stop still posts its final reply once when it has text; agent_end skips that blocked stop to avoid a duplicate. Every post is serialized in call order; Discord 429 responses wait for `retry_after` (or `Retry-After`, default 1 s; capped at 10 s) and retry the chunk once.
180
+ - Posts time out at 5 seconds per attempt. Long replies are split per channel (Discord 2000, Slack 3000 characters) on line boundaries where possible; failures are logged without delaying the stop decision.
181
181
 
182
182
  "Needs you" pings use the same channels so the owner learns on the phone that a decision is his: when a Main turn ends with an open plan whose `WAITING on owner:` line was not pinged before, neurastream posts `🔔 Needs you — <plan>: <question>` (with the `neurastream ui` phone-page link when `ui.enabled: true`), and when the guard refuses a Main tool call because it touches a protected guard file, it posts `🔓 Main needs an unlock to continue (<file>)` — the unlock ping at most once per 30 minutes, each waiting question once until its line changes or disappears, remembered in `<queue_state>/notify-waiting.json`. Every decision logs one `notify: needs-you posted|skipped (<why>)` guard-log line.
183
183
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neurastream",
3
- "version": "0.2.19",
3
+ "version": "0.2.20",
4
4
  "description": "Guardrails for running many AI coding agents at once: plan queue, blockers, call budgets, provider caps, scoring, merge-verified close.",
5
5
  "keywords": [
6
6
  "ai-agents",
@@ -578,10 +578,11 @@ export function createOmpAdapter(pi: ExtensionHost, cfg: Config): void {
578
578
  adapterLog(cfg, "notify: needs-you skipped (no new waiting lines)");
579
579
  return;
580
580
  }
581
- for (const p of fresh) {
581
+ const lines = fresh.map(p => {
582
582
  const title = p.text.match(/^# (.+)$/m)?.[1] ?? basename(p.file);
583
- needsYouPost(`🔔 Needs you — ${title}: ${p.waitingLine}.${phonePage()}`, `waiting: ${basename(p.file)}`);
584
- }
583
+ return `- ${title}: ${p.waitingLine}`;
584
+ });
585
+ needsYouPost(`🔔 Needs you (${fresh.length}):\n${lines.join("\n")}${phonePage()}`, `waiting: ${fresh.length} plans`);
585
586
  };
586
587
  // UNLOCK: a protected-file refusal of a Main tool call means Main sits blocked until the owner
587
588
  // unlocks — ping, at most once per 30 min.
@@ -795,7 +796,7 @@ export function createOmpAdapter(pi: ExtensionHost, cfg: Config): void {
795
796
  const snap = rolloverSnapshot(rc);
796
797
  if (snap.state === "need-note") {
797
798
  adapterLog(cfg, "BLOCK stop: rollover need-note");
798
- adapterLog(cfg, "notify: skipped (stop blocked: rollover need-note)");
799
+ notifyHandback(reply, "blocked session_stop");
799
800
  stopBlocked = true;
800
801
  return { decision: "block", reason: noteRequestFor(rc, snap) };
801
802
  }
@@ -816,7 +817,7 @@ export function createOmpAdapter(pi: ExtensionHost, cfg: Config): void {
816
817
  notifyHandback(reply, "allowed session_stop");
817
818
  return undefined;
818
819
  }
819
- adapterLog(cfg, "notify: skipped (stop blocked by guard)");
820
+ notifyHandback(reply, "blocked session_stop");
820
821
  stopBlocked = true;
821
822
  return { decision: "block", reason: d.reason };
822
823
  });
@@ -159,39 +159,64 @@ function webhookUrl(p: NotifyProvider): { url: string; why: string } {
159
159
  async function postChunks(provider: NotifyProvider, url: string, chunks: string[]): Promise<NotifyPostResult> {
160
160
  const key = provider.name === "discord" ? "content" : "text";
161
161
  for (const [i, chunk] of chunks.entries()) {
162
- try {
163
- const r = await fetch(url, {
164
- method: "POST",
165
- headers: { "content-type": "application/json" },
166
- body: JSON.stringify({ [key]: chunk }),
167
- signal: AbortSignal.timeout(POST_TIMEOUT_MS),
168
- });
169
- if (!r.ok) {
162
+ for (let attempt = 0; attempt < 2; attempt++) {
163
+ try {
164
+ const r = await fetch(url, {
165
+ method: "POST",
166
+ headers: { "content-type": "application/json" },
167
+ body: JSON.stringify({ [key]: chunk }),
168
+ signal: AbortSignal.timeout(POST_TIMEOUT_MS),
169
+ });
170
+ if (r.ok) break;
171
+ if (r.status === 429 && attempt === 0) {
172
+ let retryAfter: number | undefined;
173
+ try {
174
+ const body: unknown = await r.clone().json();
175
+ if (body && typeof body === "object" && "retry_after" in body && typeof body.retry_after === "number") retryAfter = body.retry_after;
176
+ } catch {}
177
+ if (retryAfter === undefined) {
178
+ const rawHeader = r.headers.get("retry-after");
179
+ const header = rawHeader === null ? Number.NaN : Number(rawHeader);
180
+ if (Number.isFinite(header) && header >= 0) retryAfter = header;
181
+ }
182
+ const { promise, resolve } = Promise.withResolvers<void>();
183
+ setTimeout(resolve, Math.min(retryAfter ?? 1, 10) * 1000);
184
+ await promise;
185
+ continue;
186
+ }
170
187
  return { provider: provider.name, posted: false, chunks: i + 1, error: `${provider.name} answered ${r.status} on chunk ${i + 1}/${chunks.length}` };
188
+ } catch (err) {
189
+ return { provider: provider.name, posted: false, chunks: i + 1, error: `${provider.name} chunk ${i + 1}/${chunks.length} failed: ${err instanceof Error ? err.message : String(err)}` };
171
190
  }
172
- } catch (err) {
173
- return { provider: provider.name, posted: false, chunks: i + 1, error: `${provider.name} chunk ${i + 1}/${chunks.length} failed: ${err instanceof Error ? err.message : String(err)}` };
174
191
  }
175
192
  }
176
193
  return { provider: provider.name, posted: true, chunks: chunks.length, error: "" };
177
194
  }
178
195
 
179
- /** Post Main's final reply to every configured provider, chunks in order, one attempt per chunk
180
- * (5 s timeout). Never throws and never retries: a failed or slow post comes back as a failed
181
- * result for the caller to log. */
182
- export async function postReply(text: string, cfg: Config): Promise<NotifyPostResult[]> {
183
- const providers = notifyProviders(cfg);
184
- const body = cfg.notify.prefix ? `${cfg.notify.prefix} ${text}` : text;
185
- if (body.trim() === "") return providers.map(p => ({ provider: p.name, posted: false, chunks: 0, error: "empty reply; nothing posted" }));
186
- const out: NotifyPostResult[] = [];
187
- for (const p of providers) {
188
- const { url, why } = webhookUrl(p);
189
- if (url === "") {
190
- out.push({ provider: p.name, posted: false, chunks: 0, error: why });
191
- continue;
196
+ let postQueue: Promise<void> = Promise.resolve();
197
+ const enqueuePost = <T>(operation: () => Promise<T>): Promise<T> => {
198
+ const result = postQueue.then(operation);
199
+ postQueue = result.then(() => undefined, () => undefined);
200
+ return result;
201
+ }
202
+
203
+ /** Post Main's final reply to every configured provider, serially across calls. 429s retry once
204
+ * after Discord's requested delay (capped at 10 seconds); other failures return to the caller. */
205
+ export function postReply(text: string, cfg: Config): Promise<NotifyPostResult[]> {
206
+ return enqueuePost(async () => {
207
+ const providers = notifyProviders(cfg);
208
+ const body = cfg.notify.prefix ? `${cfg.notify.prefix} ${text}` : text;
209
+ if (body.trim() === "") return providers.map(p => ({ provider: p.name, posted: false, chunks: 0, error: "empty reply; nothing posted" }));
210
+ const out: NotifyPostResult[] = [];
211
+ for (const p of providers) {
212
+ const { url, why } = webhookUrl(p);
213
+ if (url === "") {
214
+ out.push({ provider: p.name, posted: false, chunks: 0, error: why });
215
+ continue;
216
+ }
217
+ const limit = p.name === "discord" ? DISCORD_LIMIT : SLACK_LIMIT;
218
+ out.push(await postChunks(p, url, notifyChunks(body, limit)));
192
219
  }
193
- const limit = p.name === "discord" ? DISCORD_LIMIT : SLACK_LIMIT;
194
- out.push(await postChunks(p, url, notifyChunks(body, limit)));
195
- }
196
- return out;
220
+ return out;
221
+ });
197
222
  }