pi-better-server 0.2.4 → 0.4.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 (109) hide show
  1. package/package.json +6 -3
  2. package/scripts/measure-shared-reads/daemon.mjs +7 -0
  3. package/scripts/measure-shared-reads/probe.mjs +10 -0
  4. package/scripts/measure-shared-reads.mjs +165 -0
  5. package/scripts/test-on-pi.sh +21 -3
  6. package/src/cli.ts +39 -2
  7. package/src/client.ts +9 -4
  8. package/src/daemon-owner.ts +48 -6
  9. package/src/handshake.ts +8 -7
  10. package/src/lifecycle-config.ts +4 -0
  11. package/src/paths.ts +1 -11
  12. package/src/protocol.ts +7 -0
  13. package/src/registry.ts +2 -1
  14. package/src/runtime/background-history.ts +241 -0
  15. package/src/runtime/background.ts +182 -0
  16. package/src/runtime/cancellation.ts +99 -0
  17. package/src/runtime/catalog-admission.ts +50 -0
  18. package/src/runtime/catalog.ts +133 -0
  19. package/src/runtime/client.ts +111 -0
  20. package/src/runtime/domain/assignment-observation.d.mts +1 -0
  21. package/src/runtime/domain/assignment-observation.mjs +120 -0
  22. package/src/runtime/domain/assignment-projection.d.mts +1 -0
  23. package/src/runtime/domain/assignment-projection.mjs +245 -0
  24. package/src/runtime/domain/background/conditions.mjs +93 -0
  25. package/src/runtime/domain/background/process-identity.mjs +110 -0
  26. package/src/runtime/domain/background/process.mjs +511 -0
  27. package/src/runtime/domain/background/registry.mjs +374 -0
  28. package/src/runtime/domain/background/snapshot.mjs +73 -0
  29. package/src/runtime/domain/background/supervisor/identity.mjs +92 -0
  30. package/src/runtime/domain/background/supervisor/invocation.mjs +29 -0
  31. package/src/runtime/domain/background/supervisor/store.mjs +604 -0
  32. package/src/runtime/domain/background/supervisor/supervisor.mjs +720 -0
  33. package/src/runtime/domain/background/supervisor/types.mjs +47 -0
  34. package/src/runtime/domain/background/terminal-publication.mjs +58 -0
  35. package/src/runtime/domain/background/types.mjs +3 -0
  36. package/src/runtime/domain/catalog/agent-session-settings.d.mts +30 -0
  37. package/src/runtime/domain/catalog/agent-session-settings.mjs +112 -0
  38. package/src/runtime/domain/catalog/catalog-overlay.d.mts +3 -0
  39. package/src/runtime/domain/catalog/catalog-overlay.mjs +9 -0
  40. package/src/runtime/domain/catalog/catalog-preferences.d.mts +17 -0
  41. package/src/runtime/domain/catalog/catalog-preferences.mjs +164 -0
  42. package/src/runtime/domain/catalog/catalog-schema.d.mts +265 -0
  43. package/src/runtime/domain/catalog/catalog-schema.mjs +1331 -0
  44. package/src/runtime/domain/catalog/catalog-skill-edits.d.mts +42 -0
  45. package/src/runtime/domain/catalog/catalog-skill-edits.mjs +95 -0
  46. package/src/runtime/domain/catalog/catalog-store.d.mts +126 -0
  47. package/src/runtime/domain/catalog/catalog-store.mjs +810 -0
  48. package/src/runtime/domain/catalog/roles/role.architect.md +13 -0
  49. package/src/runtime/domain/catalog/roles/role.developer.md +16 -0
  50. package/src/runtime/domain/catalog/roles/role.explorer.md +13 -0
  51. package/src/runtime/domain/catalog/roles/role.product-manager.md +13 -0
  52. package/src/runtime/domain/catalog/roles/role.researcher.md +13 -0
  53. package/src/runtime/domain/catalog/roles/role.reviewer.md +13 -0
  54. package/src/runtime/domain/catalog/roles/role.writer.md +12 -0
  55. package/src/runtime/domain/catalog/thinking.d.mts +8 -0
  56. package/src/runtime/domain/catalog/thinking.mjs +22 -0
  57. package/src/runtime/domain/coordinator/index.mjs +50 -0
  58. package/src/runtime/domain/coordinator/src/assignment-outcomes.mjs +160 -0
  59. package/src/runtime/domain/coordinator/src/discovery.mjs +94 -0
  60. package/src/runtime/domain/coordinator/src/errors.mjs +12 -0
  61. package/src/runtime/domain/coordinator/src/operations.mjs +301 -0
  62. package/src/runtime/domain/coordinator/src/outbox.mjs +282 -0
  63. package/src/runtime/domain/coordinator/src/recovery.mjs +51 -0
  64. package/src/runtime/domain/coordinator/src/result-supervision.mjs +101 -0
  65. package/src/runtime/domain/coordinator/src/rows.mjs +277 -0
  66. package/src/runtime/domain/coordinator/src/schema.mjs +367 -0
  67. package/src/runtime/domain/coordinator/src/sqlite.mjs +193 -0
  68. package/src/runtime/domain/coordinator/src/store.mjs +245 -0
  69. package/src/runtime/domain/coordinator/src/task-cancellation.mjs +187 -0
  70. package/src/runtime/domain/coordinator/src/types.mjs +24 -0
  71. package/src/runtime/domain/coordinator/src/validate.mjs +233 -0
  72. package/src/runtime/domain/coordinator/src/waits.mjs +850 -0
  73. package/src/runtime/domain/health-observation.d.mts +3 -0
  74. package/src/runtime/domain/health-observation.mjs +613 -0
  75. package/src/runtime/domain/health-surface.mjs +279 -0
  76. package/src/runtime/domain/log-cursor.mjs +148 -0
  77. package/src/runtime/domain/log-paths.mjs +13 -0
  78. package/src/runtime/domain/parse.d.mts +2 -0
  79. package/src/runtime/domain/parse.mjs +1173 -0
  80. package/src/runtime/domain/process-status.d.mts +1 -0
  81. package/src/runtime/domain/process-status.mjs +25 -0
  82. package/src/runtime/domain/session-store.d.mts +2 -0
  83. package/src/runtime/domain/session-store.mjs +489 -0
  84. package/src/runtime/domain/shared-harness-settings.d.mts +1 -0
  85. package/src/runtime/domain/shared-harness-settings.mjs +126 -0
  86. package/src/runtime/domain/shared-log-utils.mjs +1223 -0
  87. package/src/runtime/domain/shared-stall-detector.mjs +37 -0
  88. package/src/runtime/domain/widget.d.mts +1 -0
  89. package/src/runtime/domain/widget.mjs +250 -0
  90. package/src/runtime/guardian.ts +189 -0
  91. package/src/runtime/health-config.ts +27 -0
  92. package/src/runtime/local-client.ts +34 -0
  93. package/src/runtime/mutation-store.ts +34 -0
  94. package/src/runtime/queries.ts +76 -0
  95. package/src/runtime/query-executor.ts +277 -0
  96. package/src/runtime/query-worker.mjs +119 -0
  97. package/src/runtime/read-model.ts +409 -0
  98. package/src/runtime/result-supervision.ts +63 -0
  99. package/src/runtime/root.ts +29 -0
  100. package/src/runtime/socket.ts +10 -0
  101. package/src/runtime/status-cli.ts +11 -0
  102. package/src/runtime/work-client.d.mts +14 -0
  103. package/src/runtime/work-client.mjs +102 -0
  104. package/src/server.ts +199 -14
  105. package/src/session-activity.ts +22 -2
  106. package/src/startup/activation.ts +32 -5
  107. package/src/startup/ensure.ts +13 -1
  108. package/src/startup/install.ts +2 -0
  109. package/src/version.ts +4 -0
@@ -0,0 +1,93 @@
1
+ export function validateCondition(condition) {
2
+ if ((condition.type === "json_path_equals" || condition.type === "json_path_exists") && !parseJsonPath(condition.path)) {
3
+ return `unsupported JSON path: ${condition.path}. Use a root-prefixed path such as $.status or $.terminalFailure.`;
4
+ }
5
+ return undefined;
6
+ }
7
+ export function evaluateCondition(condition, result) {
8
+ switch (condition.type) {
9
+ case "exit_code":
10
+ return { matched: result.exitCode === condition.equals, value: result.exitCode };
11
+ case "stdout_contains":
12
+ return { matched: result.stdout.includes(condition.value) };
13
+ case "stderr_contains":
14
+ return { matched: result.stderr.includes(condition.value) };
15
+ case "json_path_equals": {
16
+ const resolved = resolveJsonPath(result.stdout, condition.path);
17
+ if (!resolved.ok)
18
+ return { matched: false, error: resolved.error };
19
+ return { matched: deepEqual(resolved.value, condition.value), value: resolved.value };
20
+ }
21
+ case "json_path_exists": {
22
+ const resolved = resolveJsonPath(result.stdout, condition.path);
23
+ return resolved.ok ? { matched: true, value: resolved.value } : { matched: false, error: resolved.error };
24
+ }
25
+ }
26
+ }
27
+ export function resolveJsonPath(jsonText, path) {
28
+ let current;
29
+ try {
30
+ current = JSON.parse(jsonText);
31
+ }
32
+ catch (error) {
33
+ return { ok: false, error: `stdout is not valid JSON: ${error instanceof Error ? error.message : String(error)}` };
34
+ }
35
+ const tokens = parseJsonPath(path);
36
+ if (!tokens)
37
+ return { ok: false, error: `unsupported JSON path: ${path}` };
38
+ for (const token of tokens) {
39
+ if (typeof token === "number") {
40
+ if (!Array.isArray(current) || token < 0 || token >= current.length) {
41
+ return { ok: false, error: `JSON path not found: ${path}` };
42
+ }
43
+ current = current[token];
44
+ continue;
45
+ }
46
+ if (current === null || typeof current !== "object" || !(token in current)) {
47
+ return { ok: false, error: `JSON path not found: ${path}` };
48
+ }
49
+ current = current[token];
50
+ }
51
+ return { ok: true, value: current };
52
+ }
53
+ function parseJsonPath(path) {
54
+ if (!path.startsWith("$"))
55
+ return undefined;
56
+ const tokens = [];
57
+ let i = 1;
58
+ while (i < path.length) {
59
+ if (path[i] === ".") {
60
+ i += 1;
61
+ const start = i;
62
+ while (i < path.length && /[A-Za-z0-9_$-]/.test(path[i]))
63
+ i += 1;
64
+ if (i === start)
65
+ return undefined;
66
+ tokens.push(path.slice(start, i));
67
+ continue;
68
+ }
69
+ if (path[i] === "[") {
70
+ const end = path.indexOf("]", i);
71
+ if (end === -1)
72
+ return undefined;
73
+ const raw = path.slice(i + 1, end).trim();
74
+ const quoted = raw.match(/^['\"](.+)['\"]$/);
75
+ if (quoted) {
76
+ tokens.push(quoted[1]);
77
+ }
78
+ else if (/^\d+$/.test(raw)) {
79
+ tokens.push(Number(raw));
80
+ }
81
+ else {
82
+ return undefined;
83
+ }
84
+ i = end + 1;
85
+ continue;
86
+ }
87
+ return undefined;
88
+ }
89
+ return tokens;
90
+ }
91
+ function deepEqual(a, b) {
92
+ return JSON.stringify(a) === JSON.stringify(b);
93
+ }
@@ -0,0 +1,110 @@
1
+ import { execFileSync } from "node:child_process";
2
+ /**
3
+ * Process start-token reads.
4
+ *
5
+ * `ps` is a process spawn. Reading it on a rendering path — once per task, per
6
+ * frame, for every terminal task kept in the panel — is a measurable CPU drain
7
+ * on its own, and it competes with the very commands the panel is showing.
8
+ *
9
+ * Two rules keep that bounded:
10
+ *
11
+ * 1. A pid's start token is immutable for the life of that pid, so a short-lived
12
+ * memo is safe. Entries expire so a recycled pid is re-read rather than
13
+ * mistaking the previous owner's token for the current one, and failures are
14
+ * memoized too, so a pid that cannot be read is not retried on every frame.
15
+ * 2. Callers that decide to *signal* a pid can ask for a fresh read. Liveness
16
+ * display may use the memo; authorization may not.
17
+ */
18
+ /** How long one pid→token answer is trusted. Short enough to bound pid reuse. */
19
+ const TOKEN_MEMO_TTL_MS = 1_000;
20
+ /** Bound on memo entries; the panel is small, but a long session is not. */
21
+ const TOKEN_MEMO_MAX_ENTRIES = 512;
22
+ const tokenMemo = new Map();
23
+ function readTokenUncached(pid) {
24
+ if (!Number.isInteger(pid) || pid <= 0 || process.platform === "win32")
25
+ return undefined;
26
+ try {
27
+ const output = execFileSync("ps", ["-o", "lstart=", "-p", String(pid)], {
28
+ encoding: "utf8",
29
+ stdio: ["ignore", "pipe", "ignore"],
30
+ timeout: 2_000,
31
+ }).trim();
32
+ return output || undefined;
33
+ }
34
+ catch {
35
+ return undefined;
36
+ }
37
+ }
38
+ function remember(pid, token, now) {
39
+ if (tokenMemo.size >= TOKEN_MEMO_MAX_ENTRIES) {
40
+ // Drop expired entries first; only evict live ones if that was not enough.
41
+ for (const [candidate, entry] of tokenMemo) {
42
+ if (now - entry.readAt >= TOKEN_MEMO_TTL_MS)
43
+ tokenMemo.delete(candidate);
44
+ }
45
+ if (tokenMemo.size >= TOKEN_MEMO_MAX_ENTRIES) {
46
+ const oldest = tokenMemo.keys().next();
47
+ if (!oldest.done)
48
+ tokenMemo.delete(oldest.value);
49
+ }
50
+ }
51
+ tokenMemo.set(pid, { token, readAt: now });
52
+ }
53
+ /**
54
+ * Start token for `pid`, memoized for {@link TOKEN_MEMO_TTL_MS}.
55
+ *
56
+ * Suitable for liveness and display. A caller that is about to send a signal
57
+ * must use {@link readProcessStartTokenFresh} instead.
58
+ */
59
+ export function readProcessStartToken(pid) {
60
+ if (!Number.isInteger(pid) || pid <= 0 || process.platform === "win32")
61
+ return undefined;
62
+ const now = Date.now();
63
+ const entry = tokenMemo.get(pid);
64
+ if (entry && now - entry.readAt < TOKEN_MEMO_TTL_MS)
65
+ return entry.token;
66
+ const token = readTokenUncached(pid);
67
+ remember(pid, token, now);
68
+ return token;
69
+ }
70
+ /**
71
+ * Start token for `pid`, always read from the OS.
72
+ *
73
+ * Used where the answer authorizes a signal, so a recycled pid cannot inherit
74
+ * a memoized identity.
75
+ */
76
+ export function readProcessStartTokenFresh(pid) {
77
+ if (!Number.isInteger(pid) || pid <= 0 || process.platform === "win32")
78
+ return undefined;
79
+ const token = readTokenUncached(pid);
80
+ remember(pid, token, Date.now());
81
+ return token;
82
+ }
83
+ /** Drop every memoized identity. Tests and reloads start from a clean slate. */
84
+ export function clearProcessIdentityMemo() {
85
+ tokenMemo.clear();
86
+ }
87
+ const selfStartToken = readTokenUncached(process.pid);
88
+ export function currentProcessStartToken() {
89
+ return selfStartToken;
90
+ }
91
+ export function processIdentityAlive(pid, recordedStartToken, recordedAt) {
92
+ if (!pid || pid <= 0)
93
+ return false;
94
+ try {
95
+ process.kill(pid, 0);
96
+ }
97
+ catch (error) {
98
+ if (error.code !== "EPERM")
99
+ return false;
100
+ }
101
+ const current = readProcessStartToken(pid);
102
+ if (recordedStartToken && current)
103
+ return recordedStartToken === current;
104
+ if (!recordedStartToken && current && recordedAt !== undefined) {
105
+ const currentStartedAt = Date.parse(current);
106
+ if (Number.isFinite(currentStartedAt) && currentStartedAt > recordedAt + 2_000)
107
+ return false;
108
+ }
109
+ return true;
110
+ }