create-caspian-app 1.6.1-alpha → 1.6.2-alpha

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.
@@ -1,7 +1,7 @@
1
- {
2
- "local": "http://localhost:5090",
3
- "external": "http://192.168.1.62:5090",
4
- "ui": "http://localhost:3001",
5
- "uiExternal": "http://192.168.1.62:3001",
6
- "backend": "http://localhost:5200"
1
+ {
2
+ "local": "http://localhost:5090",
3
+ "external": "http://192.168.1.16:5090",
4
+ "ui": "http://localhost:3001",
5
+ "uiExternal": "http://192.168.1.16:3001",
6
+ "backend": "http://localhost:5200"
7
7
  }
@@ -27,6 +27,7 @@ import {
27
27
  endBrowserLogSession,
28
28
  startBrowserLogSession,
29
29
  } from "./dev-log-bridge.js";
30
+ import { evaluateDevHold, isDevHoldActive } from "./dev-hold.js";
30
31
 
31
32
  const { __dirname } = getFileMeta();
32
33
  const bs: BrowserSyncInstance = browserSync.create();
@@ -253,6 +254,37 @@ const changeCoordinator = new SettledBatchWorker<string>(
253
254
  },
254
255
  1500,
255
256
  "bs-change-coordinator",
257
+ // The 1500 ms quiet period batches a human's burst-save, but an agent's gap
258
+ // between two edits is a tool round-trip and always exceeds it, so every edit
259
+ // would otherwise cost a full restart plus a reload of every open tab. While
260
+ // an agent holds the dev stack, keep queueing and drain the whole run at once.
261
+ () => isDevHoldActive(),
262
+ (deferring, pending) => {
263
+ if (deferring) {
264
+ const status = evaluateDevHold();
265
+ const owner = status.hold ? status.hold.owner : "agent";
266
+ console.log(
267
+ chalk.yellow(
268
+ `-> Dev hold active (${owner} editing); holding ${pending.size} change(s). ` +
269
+ "Run `npm run dev:resume` to reload now.",
270
+ ),
271
+ );
272
+ return;
273
+ }
274
+
275
+ const status = evaluateDevHold();
276
+ if (status.reason === "stale" || status.reason === "expired") {
277
+ console.log(
278
+ chalk.yellow(
279
+ `-> Dev hold ${status.reason === "stale" ? "went stale" : "hit its time cap"}; ` +
280
+ "resuming normal reloads.",
281
+ ),
282
+ );
283
+ return;
284
+ }
285
+
286
+ console.log(chalk.cyan("-> Dev hold released; applying held changes..."));
287
+ },
256
288
  );
257
289
 
258
290
  function isIgnoredPublicPath(absPath: string): boolean {
@@ -0,0 +1,223 @@
1
+ /**
2
+ * Dev-stack reload hold.
3
+ *
4
+ * The file watcher cannot tell an agent's write from a human's save — chokidar
5
+ * sees an inode change, not a writer. So the agent declares itself instead: a
6
+ * hold file marks "an agent is mid-run", and while it is active the change
7
+ * coordinator in `bs-config.ts` keeps queueing changes without restarting the
8
+ * Python server or reloading the browser. Releasing the hold drains the whole
9
+ * run as ONE restart and ONE reload.
10
+ *
11
+ * Why this matters: the coordinator batches on a 1500 ms quiet period, which is
12
+ * tuned for a human's burst-save. An agent's gap between two edits is a tool
13
+ * round-trip — always wider than that window — so without a hold every single
14
+ * edit costs a full process restart plus a reload of every open tab, and each
15
+ * reload re-runs the route's Prisma queries against a pool that was just
16
+ * discarded. Against a remote database that is the churn this file prevents.
17
+ *
18
+ * The signal is set by Claude Code hooks (`PreToolUse` acquires and refreshes,
19
+ * `Stop`/`SessionEnd` release), so it does not depend on a model remembering to
20
+ * call anything. The manual escape hatches are `npm run dev:hold`,
21
+ * `npm run dev:resume`, and `npm run dev:hold:status`.
22
+ *
23
+ * This module is deliberately dependency-free and written in erasable-only
24
+ * TypeScript so Node can run it directly (`node settings/dev-hold.ts acquire`)
25
+ * with a cold start fast enough to sit in front of every agent edit.
26
+ */
27
+
28
+ import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
29
+ import { dirname, join } from "node:path";
30
+ import { fileURLToPath } from "node:url";
31
+
32
+ /**
33
+ * A hold that stops being refreshed is treated as abandoned. This is the valve
34
+ * that keeps a crashed agent or a killed session from freezing the dev stack:
35
+ * the worst case degrades to today's behaviour, never to a dead server.
36
+ */
37
+ export const STALE_HOLD_MS = 120_000;
38
+
39
+ /**
40
+ * Even a continuously refreshed hold drains eventually, so a very long agent run
41
+ * still gets periodic restarts instead of none at all.
42
+ */
43
+ export const MAX_HOLD_MS = 600_000;
44
+
45
+ const MODULE_DIR = dirname(fileURLToPath(import.meta.url));
46
+
47
+ /** Resolved from this file, not from `cwd`, so hook shells cannot misplace it. */
48
+ export const WORKSPACE_ROOT = join(MODULE_DIR, "..");
49
+
50
+ /**
51
+ * Lives in `.casp/` on purpose: `npm run dev` deletes that directory at startup,
52
+ * so a fresh dev stack can never inherit a stale hold from a previous session.
53
+ *
54
+ * Resolved per call rather than at import so `CASPIAN_DEV_HOLD_PATH` can point a
55
+ * test (or a second stack) at its own file instead of the live one.
56
+ */
57
+ export function getHoldPath(): string {
58
+ return (
59
+ process.env.CASPIAN_DEV_HOLD_PATH ||
60
+ join(WORKSPACE_ROOT, ".casp", "dev-hold.json")
61
+ );
62
+ }
63
+
64
+ export type DevHold = {
65
+ /** Who asked for the hold, for the terminal message only. */
66
+ owner: string;
67
+ /** Process that acquired it, for debugging a hold nobody expected. */
68
+ pid: number;
69
+ /** First acquire — drives the absolute cap. */
70
+ acquiredAt: number;
71
+ /** Most recent acquire — drives the stale check. */
72
+ touchedAt: number;
73
+ /** How many edits this run has queued, so the terminal can show progress. */
74
+ edits: number;
75
+ };
76
+
77
+ export type DevHoldStatus =
78
+ | { active: false; reason: "none"; hold: null }
79
+ | { active: false; reason: "stale" | "expired"; hold: DevHold }
80
+ | { active: true; reason: "held"; hold: DevHold };
81
+
82
+ function isDevHold(value: unknown): value is DevHold {
83
+ if (typeof value !== "object" || value === null) return false;
84
+ const candidate = value as Partial<DevHold>;
85
+ return (
86
+ typeof candidate.owner === "string" &&
87
+ typeof candidate.pid === "number" &&
88
+ typeof candidate.acquiredAt === "number" &&
89
+ typeof candidate.touchedAt === "number" &&
90
+ typeof candidate.edits === "number"
91
+ );
92
+ }
93
+
94
+ /** Returns the parsed hold, or null when absent or unreadable. */
95
+ export function readDevHold(): DevHold | null {
96
+ try {
97
+ const parsed: unknown = JSON.parse(readFileSync(getHoldPath(), "utf-8"));
98
+ return isDevHold(parsed) ? parsed : null;
99
+ } catch {
100
+ // Missing, half-written, or corrupt all mean the same thing to a caller:
101
+ // there is no hold it can trust, so fall through to normal reloading.
102
+ return null;
103
+ }
104
+ }
105
+
106
+ /**
107
+ * Decide whether the coordinator should defer. Both expiry rules fail open —
108
+ * an unreadable, stale, or over-long hold reports inactive rather than blocking
109
+ * the dev stack forever.
110
+ */
111
+ export function evaluateDevHold(now: number = Date.now()): DevHoldStatus {
112
+ const hold = readDevHold();
113
+ if (!hold) return { active: false, reason: "none", hold: null };
114
+
115
+ if (now - hold.touchedAt > STALE_HOLD_MS) {
116
+ return { active: false, reason: "stale", hold };
117
+ }
118
+
119
+ if (now - hold.acquiredAt > MAX_HOLD_MS) {
120
+ return { active: false, reason: "expired", hold };
121
+ }
122
+
123
+ return { active: true, reason: "held", hold };
124
+ }
125
+
126
+ /** True when reloads should be deferred right now. */
127
+ export function isDevHoldActive(now: number = Date.now()): boolean {
128
+ return evaluateDevHold(now).active;
129
+ }
130
+
131
+ /**
132
+ * Create the hold, or refresh an existing one. Refreshing keeps `acquiredAt` so
133
+ * the absolute cap measures the whole run rather than restarting on every edit.
134
+ *
135
+ * A capped-out hold is deliberately still carried over. If a new edit started a
136
+ * fresh window instead, an agent editing steadily would re-hold before the
137
+ * coordinator's next 1500 ms tick and the cap would reset forever without ever
138
+ * forcing a drain. Carrying it keeps the run expired — so reloads resume — until
139
+ * an explicit release ends the run. A stale hold is a different case: nothing
140
+ * refreshed it, so its owner is gone and the next edit legitimately starts over.
141
+ */
142
+ export function acquireDevHold(
143
+ owner: string = "agent",
144
+ now: number = Date.now(),
145
+ ): DevHold {
146
+ const previous = evaluateDevHold(now);
147
+ const carryOver =
148
+ previous.reason === "held" || previous.reason === "expired"
149
+ ? previous.hold
150
+ : null;
151
+
152
+ const hold: DevHold = {
153
+ owner,
154
+ pid: process.pid,
155
+ acquiredAt: carryOver ? carryOver.acquiredAt : now,
156
+ touchedAt: now,
157
+ edits: carryOver ? carryOver.edits + 1 : 1,
158
+ };
159
+
160
+ mkdirSync(dirname(getHoldPath()), { recursive: true });
161
+ writeFileSync(getHoldPath(), `${JSON.stringify(hold, null, 2)}\n`, "utf-8");
162
+ return hold;
163
+ }
164
+
165
+ /** Drop the hold. Idempotent, so a duplicate `Stop` hook is harmless. */
166
+ export function releaseDevHold(): DevHold | null {
167
+ const hold = readDevHold();
168
+ rmSync(getHoldPath(), { force: true });
169
+ return hold;
170
+ }
171
+
172
+ function describeStatus(status: DevHoldStatus): string {
173
+ if (status.reason === "none") return "inactive - no hold file";
174
+
175
+ const ageSeconds = Math.round((Date.now() - status.hold.acquiredAt) / 1000);
176
+ const summary = `owner=${status.hold.owner} pid=${status.hold.pid} edits=${status.hold.edits} age=${ageSeconds}s`;
177
+
178
+ if (status.reason === "stale") {
179
+ return `inactive - hold went stale (no refresh for over ${STALE_HOLD_MS / 1000}s); ${summary}`;
180
+ }
181
+ if (status.reason === "expired") {
182
+ return `inactive - hold passed the ${MAX_HOLD_MS / 1000}s cap; ${summary}`;
183
+ }
184
+ return `ACTIVE - browser reloads and Python restarts are deferred; ${summary}`;
185
+ }
186
+
187
+ function runCli(argv: string[]): number {
188
+ const command = argv[0] ?? "status";
189
+ const quiet = argv.includes("--quiet");
190
+ const log = (message: string) => {
191
+ if (!quiet) console.log(message);
192
+ };
193
+
194
+ if (command === "acquire") {
195
+ const hold = acquireDevHold(process.env.CASPIAN_DEV_HOLD_OWNER || "agent");
196
+ log(`[dev-hold] Held after ${hold.edits} edit(s); reloads deferred.`);
197
+ return 0;
198
+ }
199
+
200
+ if (command === "release") {
201
+ const hold = releaseDevHold();
202
+ log(
203
+ hold
204
+ ? `[dev-hold] Released after ${hold.edits} edit(s); the dev stack will reload once.`
205
+ : "[dev-hold] No hold was active.",
206
+ );
207
+ return 0;
208
+ }
209
+
210
+ if (command === "status") {
211
+ console.log(`[dev-hold] ${describeStatus(evaluateDevHold())}`);
212
+ return 0;
213
+ }
214
+
215
+ console.error(`[dev-hold] Unknown command: ${command}`);
216
+ console.error("[dev-hold] Usage: dev-hold.ts <acquire|release|status> [--quiet]");
217
+ return 1;
218
+ }
219
+
220
+ // Only act as a CLI when executed directly; `bs-config.ts` imports the helpers.
221
+ if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
222
+ process.exit(runCli(process.argv.slice(2)));
223
+ }
@@ -143,17 +143,29 @@ export class DebouncedWorker {
143
143
  * period has elapsed. `onSettled` only runs when no newer work is pending, so
144
144
  * callers can safely perform one user-visible action (such as a browser
145
145
  * reload) after the final batch instead of after every intermediate batch.
146
+ *
147
+ * An optional `shouldDefer` predicate suspends draining entirely without losing
148
+ * work: pending items keep accumulating and the quiet period re-arms, so once
149
+ * the predicate clears, everything collected in the meantime drains as a single
150
+ * batch with a single settle. That is what lets an agent's whole editing run
151
+ * cost one restart and one reload instead of one of each per edit.
146
152
  */
147
153
  export class SettledBatchWorker<T> {
148
154
  private timer: NodeJS.Timeout | null = null;
149
155
  private pending = new Set<T>();
150
156
  private running = false;
157
+ private deferring = false;
151
158
 
152
159
  constructor(
153
160
  private work: (items: ReadonlySet<T>) => Promise<void> | void,
154
161
  private onSettled: () => Promise<void> | void,
155
162
  private quietMs = 1200,
156
163
  private name = "batch",
164
+ private shouldDefer?: (pending: ReadonlySet<T>) => boolean,
165
+ private onDeferStateChange?: (
166
+ deferring: boolean,
167
+ pending: ReadonlySet<T>,
168
+ ) => void,
157
169
  ) {}
158
170
 
159
171
  schedule(item: T) {
@@ -173,6 +185,15 @@ export class SettledBatchWorker<T> {
173
185
  if (this.running) return;
174
186
  if (this.pending.size === 0) return;
175
187
 
188
+ // Checked before anything is consumed, so a deferred batch keeps every
189
+ // queued item and simply waits for another quiet period to re-test.
190
+ if (this.shouldDefer?.(this.pending)) {
191
+ this.setDeferring(true);
192
+ this.armQuietPeriod();
193
+ return;
194
+ }
195
+ this.setDeferring(false);
196
+
176
197
  const batch = new Set(this.pending);
177
198
  this.pending.clear();
178
199
  this.running = true;
@@ -196,6 +217,13 @@ export class SettledBatchWorker<T> {
196
217
  console.error(`[${this.name}] settle error:`, err);
197
218
  }
198
219
  }
220
+
221
+ /** Reports edges only, so a long hold logs once instead of every quiet period. */
222
+ private setDeferring(deferring: boolean) {
223
+ if (this.deferring === deferring) return;
224
+ this.deferring = deferring;
225
+ this.onDeferStateChange?.(deferring, this.pending);
226
+ }
199
227
  }
200
228
 
201
229
  export function createRestartableProcess(spec: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-caspian-app",
3
- "version": "1.6.1-alpha",
3
+ "version": "1.6.2-alpha",
4
4
  "description": "Scaffold a new Caspian project (FastAPI-powered reactive Python framework).",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",