pi-better-server 0.7.0 → 0.7.2

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 (105) hide show
  1. package/package.json +1 -1
  2. package/src/codex/return.ts +3 -2
  3. package/src/detach/extension.ts +20 -3
  4. package/src/event-log.ts +40 -12
  5. package/src/hosted-session.ts +11 -5
  6. package/src/remote-client/packages.ts +1 -4
  7. package/src/runtime/background-tools.ts +2 -1
  8. package/src/runtime/domain/background/registry.mjs +3 -0
  9. package/src/runtime/domain/background/shared-operation-coordinator/src/task-lifetime.mjs +6 -0
  10. package/src/runtime/domain/background/terminal-publication.mjs +5 -2
  11. package/src/runtime/domain/background-service/managed-authority.domain.mjs +13 -0
  12. package/src/runtime/domain/background-service/managed-authority.ts +16 -0
  13. package/src/runtime/domain/background-service/managed-client.domain.mjs +2 -2
  14. package/src/runtime/domain/background-service/managed-client.ts +2 -2
  15. package/src/runtime/domain/background-service/observed-waits.domain.mjs +8 -4
  16. package/src/runtime/domain/background-service/observed-waits.ts +8 -4
  17. package/src/runtime/domain/background-service/registry.domain.mjs +3 -0
  18. package/src/runtime/domain/background-service/registry.ts +2 -0
  19. package/src/runtime/domain/background-service/runtime.domain.mjs +3 -2
  20. package/src/runtime/domain/background-service/runtime.ts +3 -2
  21. package/src/runtime/domain/background-service/shared-harness-settings.domain.mjs +2 -3
  22. package/src/runtime/domain/background-service/shared-harness-settings.ts +2 -5
  23. package/src/runtime/domain/background-service/shared-operation-coordinator/src/expiry.domain.mjs +44 -0
  24. package/src/runtime/domain/background-service/shared-operation-coordinator/src/expiry.ts +44 -0
  25. package/src/runtime/domain/background-service/shared-operation-coordinator/src/schema.domain.mjs +8 -1
  26. package/src/runtime/domain/background-service/shared-operation-coordinator/src/schema.ts +8 -1
  27. package/src/runtime/domain/background-service/shared-operation-coordinator/src/store.domain.mjs +3 -0
  28. package/src/runtime/domain/background-service/shared-operation-coordinator/src/store.ts +5 -0
  29. package/src/runtime/domain/background-service/shared-operation-coordinator/src/task-lifetime.domain.mjs +7 -0
  30. package/src/runtime/domain/background-service/shared-operation-coordinator/src/task-lifetime.ts +7 -0
  31. package/src/runtime/domain/background-service/shared-operation-coordinator/src/waits.domain.mjs +22 -13
  32. package/src/runtime/domain/background-service/shared-operation-coordinator/src/waits.ts +40 -32
  33. package/src/runtime/domain/background-service/shared-sandbox-diagnostics.domain.mjs +12 -19
  34. package/src/runtime/domain/background-service/shared-sandbox-diagnostics.ts +12 -16
  35. package/src/runtime/domain/background-service/shared-ssh-core/index.domain.mjs +1 -1
  36. package/src/runtime/domain/background-service/shared-ssh-core/index.ts +1 -1
  37. package/src/runtime/domain/background-service/supervisor/emit-runner.mjs +1 -0
  38. package/src/runtime/domain/background-service/supervisor/generated/runner.mjs +12 -7
  39. package/src/runtime/domain/background-service/supervisor/generated/task-lifetime.mjs +9 -0
  40. package/src/runtime/domain/background-service/supervisor/runner.domain.mjs +12 -7
  41. package/src/runtime/domain/background-service/supervisor/runner.ts +12 -7
  42. package/src/runtime/domain/background-service/supervisor/types.ts +1 -1
  43. package/src/runtime/domain/background-service/tools.domain.mjs +3 -2
  44. package/src/runtime/domain/background-service/tools.ts +3 -2
  45. package/src/runtime/domain/catalog/catalog-preferences.mjs +1 -3
  46. package/src/runtime/domain/coordinator/src/expiry.mjs +43 -0
  47. package/src/runtime/domain/coordinator/src/schema.mjs +8 -1
  48. package/src/runtime/domain/coordinator/src/store.mjs +3 -0
  49. package/src/runtime/domain/coordinator/src/task-lifetime.mjs +6 -0
  50. package/src/runtime/domain/coordinator/src/waits.mjs +22 -13
  51. package/src/runtime/domain/sandbox-policy/package.json +1 -1
  52. package/src/runtime/domain/sandbox-policy/shared-harness-settings.domain.mjs +2 -3
  53. package/src/runtime/domain/sandbox-policy/shared-harness-settings.ts +2 -5
  54. package/src/runtime/domain/sandbox-policy/shared-sandbox-diagnostics.domain.mjs +12 -19
  55. package/src/runtime/domain/sandbox-policy/shared-sandbox-diagnostics.ts +12 -16
  56. package/src/runtime/domain/shared-harness-settings.mjs +2 -3
  57. package/src/runtime/domain/subagents/README.md +2 -2
  58. package/src/runtime/domain/subagents/catalog-preferences.domain.mjs +1 -3
  59. package/src/runtime/domain/subagents/catalog-preferences.ts +1 -5
  60. package/src/runtime/domain/subagents/cleanup.domain.mjs +5 -2
  61. package/src/runtime/domain/subagents/cleanup.ts +4 -1
  62. package/src/runtime/domain/subagents/config.ts +1 -1
  63. package/src/runtime/domain/subagents/extensions.mjs +1 -0
  64. package/src/runtime/domain/subagents/finalization.domain.mjs +34 -30
  65. package/src/runtime/domain/subagents/finalization.ts +30 -28
  66. package/src/runtime/domain/subagents/git-workspace.domain.mjs +51 -6
  67. package/src/runtime/domain/subagents/git-workspace.ts +48 -6
  68. package/src/runtime/domain/subagents/index.domain.mjs +99 -54
  69. package/src/runtime/domain/subagents/index.ts +100 -51
  70. package/src/runtime/domain/subagents/isolated-workspace.domain.mjs +7 -14
  71. package/src/runtime/domain/subagents/isolated-workspace.ts +6 -12
  72. package/src/runtime/domain/subagents/managed-launch-host.domain.mjs +28 -2
  73. package/src/runtime/domain/subagents/managed-launch-host.ts +26 -2
  74. package/src/runtime/domain/subagents/managed-launch-port.ts +2 -0
  75. package/src/runtime/domain/subagents/package.json +1 -1
  76. package/src/runtime/domain/subagents/registry-cleanup-worker.mjs +24 -0
  77. package/src/runtime/domain/subagents/registry.domain.mjs +40 -14
  78. package/src/runtime/domain/subagents/registry.ts +26 -12
  79. package/src/runtime/domain/subagents/settings-page.domain.mjs +10 -10
  80. package/src/runtime/domain/subagents/settings-page.ts +10 -10
  81. package/src/runtime/domain/subagents/shared-harness-settings.domain.mjs +2 -3
  82. package/src/runtime/domain/subagents/shared-harness-settings.ts +2 -5
  83. package/src/runtime/domain/subagents/shared-operation-coordinator/src/expiry.domain.mjs +44 -0
  84. package/src/runtime/domain/subagents/shared-operation-coordinator/src/expiry.ts +44 -0
  85. package/src/runtime/domain/subagents/shared-operation-coordinator/src/schema.domain.mjs +8 -1
  86. package/src/runtime/domain/subagents/shared-operation-coordinator/src/schema.ts +8 -1
  87. package/src/runtime/domain/subagents/shared-operation-coordinator/src/store.domain.mjs +3 -0
  88. package/src/runtime/domain/subagents/shared-operation-coordinator/src/store.ts +5 -0
  89. package/src/runtime/domain/subagents/shared-operation-coordinator/src/task-lifetime.domain.mjs +7 -0
  90. package/src/runtime/domain/subagents/shared-operation-coordinator/src/task-lifetime.ts +7 -0
  91. package/src/runtime/domain/subagents/shared-operation-coordinator/src/waits.domain.mjs +22 -13
  92. package/src/runtime/domain/subagents/shared-operation-coordinator/src/waits.ts +40 -32
  93. package/src/runtime/domain/subagents/shared-sandbox-diagnostics.domain.mjs +12 -19
  94. package/src/runtime/domain/subagents/shared-sandbox-diagnostics.ts +12 -16
  95. package/src/runtime/domain/subagents/stop.domain.mjs +7 -5
  96. package/src/runtime/domain/subagents/stop.ts +7 -5
  97. package/src/runtime/domain/subagents/timing.domain.mjs +7 -3
  98. package/src/runtime/domain/subagents/timing.ts +11 -4
  99. package/src/runtime/domain/subagents/workspace-files.domain.mjs +6 -6
  100. package/src/runtime/domain/subagents/workspace-files.ts +6 -6
  101. package/src/runtime/domain/subagents/workspace-prepare-worker.mjs +12 -0
  102. package/src/runtime/task-expiry.ts +52 -0
  103. package/src/server.ts +4 -0
  104. package/src/shared-ssh-core/index.ts +1 -1
  105. package/src/version.ts +1 -1
@@ -0,0 +1,52 @@
1
+ import type { acquireMutationStore } from "./mutation-store.ts";
2
+ // @ts-expect-error Generated canonical supervisor.
3
+ import { createLocalSupervisor, defaultSupervisorDirectory } from "./domain/background/supervisor/supervisor.mjs";
4
+ // @ts-expect-error Generated durable stop request.
5
+ import { writeStopRequest } from "./domain/background/supervisor/store.mjs";
6
+ // @ts-expect-error Generated supervisor identity and format.
7
+ import { SUPERVISOR_RECORD_VERSION, SUPERVISOR_TASK_ID_PATTERN } from "./domain/background/supervisor/types.mjs";
8
+ // @ts-expect-error Generated canonical terminal evidence rules.
9
+ import { supervisorTerminalPublication } from "./domain/background/terminal-publication.mjs";
10
+
11
+ const reason = "24-hour task lifetime exceeded";
12
+
13
+ /** Repair records left behind by offline or older task owners. */
14
+ export function startTaskExpiry(control: ReturnType<typeof acquireMutationStore>, log: (message: string) => void) {
15
+ const { store, budget } = control;
16
+ let timer: NodeJS.Timeout;
17
+ const sweep = () => {
18
+ const release = budget.enter("repair");
19
+ let delay = 30_000;
20
+ try {
21
+ if (!release) { delay = 1000; return; }
22
+ const overdue = store.overdueOperations();
23
+ if (store.expireWaits() || overdue.length) delay = 1000;
24
+ for (const op of overdue) {
25
+ try {
26
+ let publication;
27
+ if (op.observer.kind === "local-supervisor" && SUPERVISOR_TASK_ID_PATTERN.test(op.taskId)) {
28
+ const grant = store.resultSupervision(op.operationId);
29
+ const directory = grant ? grant.directory : defaultSupervisorDirectory();
30
+ const inspection = createLocalSupervisor({ directory }).snapshot(op.taskId);
31
+ if (inspection?.token === op.launchToken && (!grant || grant.launchToken === op.launchToken)) {
32
+ if (inspection.terminal) publication = supervisorTerminalPublication(op, inspection);
33
+ else writeStopRequest(directory, { version: SUPERVISOR_RECORD_VERSION, taskId: op.taskId,
34
+ token: op.launchToken, requestedAt: Date.now(), reason });
35
+ }
36
+ }
37
+ store.recordTerminalOutcome(publication ?? { operationId: op.operationId, owner: op.owner,
38
+ eventId: "task-lifetime-expired", outcome: "failed", reason });
39
+ } catch (error) {
40
+ if (store.getOperation(op.operationId)?.phase === "running") log(`Task expiry for ${op.operationId}: ${String(error)}`);
41
+ }
42
+ }
43
+ } catch (error) { log(`Task expiry: ${String(error)}`); }
44
+ finally {
45
+ release?.();
46
+ timer = setTimeout(sweep, delay);
47
+ timer.unref();
48
+ }
49
+ };
50
+ sweep();
51
+ return () => clearTimeout(timer);
52
+ }
package/src/server.ts CHANGED
@@ -20,6 +20,7 @@ import type { IdleSessionsLease, UpgradeRequest } from "./startup/managed-types.
20
20
  import { HostedSession } from "./hosted-session.ts";
21
21
  import { readLifecycleConfig, type LifecycleConfig } from "./lifecycle-config.ts";
22
22
  import { runtimeRoot } from "./runtime/root.ts";
23
+ import { startTaskExpiry } from "./runtime/task-expiry.ts";
23
24
  import { RuntimeCancellation } from "./runtime/cancellation.ts";
24
25
  import { RuntimeResultSupervision } from "./runtime/result-supervision.ts";
25
26
  import { SharedCatalog } from "./runtime/catalog.ts";
@@ -594,6 +595,7 @@ export async function startDaemon(options: DaemonOptions = {}): Promise<Daemon>
594
595
  let timer: ReturnType<typeof setInterval> | undefined;
595
596
  let finishClosed!: () => void;
596
597
  const closed = new Promise<void>(resolve => { finishClosed = resolve; });
598
+ const stopTaskExpiry = startTaskExpiry(controlStore, log);
597
599
  const daemon: Daemon = {
598
600
  serverDir,
599
601
  socketPath,
@@ -625,6 +627,7 @@ export async function startDaemon(options: DaemonOptions = {}): Promise<Daemon>
625
627
  // Unknown observation fails closed for an irreversible activation.
626
628
  upgradeBlockers = await work().upgradeObligations();
627
629
  if (upgradeBlockers.length) throw new Error(`Waiting for runtime work: ${JSON.stringify(upgradeBlockers)}`);
630
+ if (launches?.cleaningRegistry) throw new Error("Waiting for subagent registry cleanup");
628
631
  if (inFlight > 0 || cancellations?.busy) throw new Error("Waiting for in-flight runtime admission");
629
632
  commitActivationStop(request, lease, () => { draining = true; upgradeCommitted = true; });
630
633
  for (const hosted of live) {
@@ -701,6 +704,7 @@ export async function startDaemon(options: DaemonOptions = {}): Promise<Daemon>
701
704
  writeFileSync(temporary, JSON.stringify({ token: instanceId, pid: process.pid }), { mode: 0o600 });
702
705
  renameSync(temporary, join(serverDir, "idle-stop-intent.json"));
703
706
  }
707
+ stopTaskExpiry();
704
708
  if (timer) clearInterval(timer);
705
709
  await pendingMaintenance?.catch(error => log(`maintenance interrupted during shutdown: ${String(error)}`));
706
710
  if (upgradeCommitted) {
@@ -328,7 +328,7 @@ export function wrapRemoteBashCommand(input: RemoteBashCommandInput): string {
328
328
 
329
329
  export function defaultSshControlPathRoot(): string {
330
330
  const agentDirectory = process.env.PI_CODING_AGENT_DIR ?? join(homedir(), ".pi", "agent");
331
- const root = join(agentDirectory, "ssh-control");
331
+ const root = resolve(agentDirectory, "ssh-control");
332
332
  // A long agent directory would push the socket past the unix path limit; fall back to a short per-user root.
333
333
  if (Buffer.byteLength(join(root, MUX_CONTROL_PATH_NAME_TEMPLATE)) <= MAX_SSH_CONTROL_PATH_BYTES) return root;
334
334
  return join("/tmp", `pi-ssh-${process.getuid?.() ?? "user"}`);
package/src/version.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  /** Kept equal to package.json `version`; a test pins that. */
2
- export const SERVER_VERSION = "0.7.0";
2
+ export const SERVER_VERSION = "0.7.2";
3
3
 
4
4
  export const SERVER_EXTENSION_VERSIONS: Readonly<Record<string, string>> = { "pi-better-server": SERVER_VERSION };