castle-web-cli 0.4.217 → 0.4.218

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.
@@ -0,0 +1,14 @@
1
+ export interface GitHeadChangedMessage {
2
+ type: 'git_head_changed';
3
+ }
4
+ /**
5
+ * Where the deck's HEAD points, as `<ref> <sha>` (or a bare sha when detached),
6
+ * or null when the deck has no git repo. Unborn branches read as the ref alone.
7
+ */
8
+ export declare function readGitHead(projectDir: string): string | null;
9
+ /** Broadcasts `git_head_changed` whenever the deck's HEAD moves. Returns a stop function. */
10
+ export declare function installGitHeadWatcher(opts: {
11
+ projectDir: string;
12
+ broadcast: (msg: GitHeadChangedMessage) => void;
13
+ pollMs?: number;
14
+ }): () => void;
@@ -0,0 +1,106 @@
1
+ // Serve-side "the deck's git history moved" broadcast, for the Versions panel.
2
+ //
3
+ // The files-changed watcher ignores `.git` on purpose, so a commit, a background
4
+ // `castle-web git sync` or a merge that moves `main` without touching a file the
5
+ // panel reports never reached it: the panel showed the old list until a reload.
6
+ // Those all run in other processes (the agent's commit, ghost's sync notify), so
7
+ // the serve cannot be told; it reads where HEAD points instead. That is two small
8
+ // file reads, so a poll is cheaper and sturdier than watching a `.git` that a
9
+ // cold resume recreates underneath us.
10
+ import * as fs from 'fs';
11
+ import * as path from 'path';
12
+ const POLL_MS = 2_000;
13
+ // A ref name as git writes it: slash-separated components, none empty, none `.`/`..`.
14
+ const REF_RE = /^ref: (refs(?:\/[A-Za-z0-9_+@-][A-Za-z0-9._+@-]*)+)$/;
15
+ const SHA_RE = /^[0-9a-f]{40}(?:[0-9a-f]{24})?$/;
16
+ // HEAD and a loose ref are one line; packed-refs grows with tags, so it gets more room.
17
+ const MAX_REF_BYTES = 4 * 1024;
18
+ const MAX_PACKED_REFS_BYTES = 1024 * 1024;
19
+ // The deck is written by the agent and whoever pushes to it, so a ref file can be a
20
+ // symlink, a pipe or something huge. Only a small regular file under a chain of real
21
+ // directories is read; anything else reads as absent. The file itself is lstat'd
22
+ // first, because Windows has neither O_NOFOLLOW nor O_NONBLOCK (Node turns them into
23
+ // 0); where they exist they also close the gap between that check and the open, and
24
+ // the fstat check refuses anything that changed in between before any read.
25
+ function readText(gitDir, rel, maxBytes) {
26
+ const parts = rel.split('/');
27
+ try {
28
+ let dir = gitDir;
29
+ for (const part of [null, ...parts.slice(0, -1)]) {
30
+ if (part !== null)
31
+ dir = path.join(dir, part);
32
+ if (!fs.lstatSync(dir).isDirectory())
33
+ return null;
34
+ }
35
+ const file = fs.lstatSync(path.join(gitDir, ...parts));
36
+ if (!file.isFile() || file.size > maxBytes)
37
+ return null;
38
+ }
39
+ catch {
40
+ return null;
41
+ }
42
+ let fd = null;
43
+ try {
44
+ fd = fs.openSync(path.join(gitDir, ...parts), fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW | fs.constants.O_NONBLOCK);
45
+ const stat = fs.fstatSync(fd);
46
+ if (!stat.isFile() || stat.size > maxBytes)
47
+ return null;
48
+ const buffer = Buffer.alloc(stat.size);
49
+ const read = fs.readSync(fd, buffer, 0, stat.size, 0);
50
+ return buffer.subarray(0, read).toString('utf8');
51
+ }
52
+ catch {
53
+ return null;
54
+ }
55
+ finally {
56
+ if (fd !== null)
57
+ fs.closeSync(fd);
58
+ }
59
+ }
60
+ function packedRef(gitDir, ref) {
61
+ const packed = readText(gitDir, 'packed-refs', MAX_PACKED_REFS_BYTES);
62
+ if (!packed)
63
+ return null;
64
+ for (const line of packed.split('\n')) {
65
+ const [sha, name] = line.trim().split(' ');
66
+ if (name === ref && sha && SHA_RE.test(sha))
67
+ return sha;
68
+ }
69
+ return null;
70
+ }
71
+ /**
72
+ * Where the deck's HEAD points, as `<ref> <sha>` (or a bare sha when detached),
73
+ * or null when the deck has no git repo. Unborn branches read as the ref alone.
74
+ */
75
+ export function readGitHead(projectDir) {
76
+ const gitDir = path.join(projectDir, '.git');
77
+ const head = readText(gitDir, 'HEAD', MAX_REF_BYTES)?.trim();
78
+ if (!head)
79
+ return null;
80
+ if (SHA_RE.test(head))
81
+ return head;
82
+ const ref = REF_RE.exec(head)?.[1];
83
+ if (!ref)
84
+ return null;
85
+ const loose = readText(gitDir, ref, MAX_REF_BYTES)?.trim();
86
+ const sha = loose && SHA_RE.test(loose) ? loose : packedRef(gitDir, ref);
87
+ return sha ? `${ref} ${sha}` : ref;
88
+ }
89
+ /** Broadcasts `git_head_changed` whenever the deck's HEAD moves. Returns a stop function. */
90
+ export function installGitHeadWatcher(opts) {
91
+ let last = readGitHead(opts.projectDir);
92
+ const timer = setInterval(() => {
93
+ const now = readGitHead(opts.projectDir);
94
+ if (now === last)
95
+ return;
96
+ last = now;
97
+ try {
98
+ opts.broadcast({ type: 'git_head_changed' });
99
+ }
100
+ catch {
101
+ // A broadcast is never worth crashing the serve for.
102
+ }
103
+ }, opts.pollMs ?? POLL_MS);
104
+ timer.unref?.();
105
+ return () => clearInterval(timer);
106
+ }
package/dist/serve.js CHANGED
@@ -17,6 +17,7 @@ import { externalSourcemapsPlugin } from './externalSourcemaps.js';
17
17
  import { fontFaceImportsPlugin } from './fontFaceImports.js';
18
18
  import { createScriptErrors, scriptErrorsPlugin } from './scriptErrors.js';
19
19
  import { installFilesChangedWatcher } from './filesChanged.js';
20
+ import { installGitHeadWatcher } from './gitHeadWatcher.js';
20
21
  import { applyDeckWrite, createTuneLedger } from './paramsTunes.js';
21
22
  import { autoUpdateImports, IMPORTS_DIR, lockImportTree } from './imports.js';
22
23
  import { ensureAgentsSymlink } from './init.js';
@@ -1318,6 +1319,9 @@ export async function serve(dir, options = {}) {
1318
1319
  log: (line) => appendCapped(logFile, line),
1319
1320
  claimTunes: (relPath, event) => tuneLedger.claim(relPath, event),
1320
1321
  });
1322
+ // `.git` is outside that watcher, so a commit or a background sync that moves the
1323
+ // deck's history reaches the Versions panel through this instead.
1324
+ installGitHeadWatcher({ projectDir, broadcast });
1321
1325
  // Bring content up first so `--open` cannot race an iframe against a port
1322
1326
  // that is not listening yet. Vite opens the shell from inside `vite.listen()`.
1323
1327
  await vite.listen();