pi-better-server 0.5.0 → 0.6.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.
- package/package.json +4 -1
- package/scripts/copy-pi-test-providers.mjs +10 -0
- package/scripts/event-publication/README.md +26 -0
- package/scripts/event-publication/commit.mjs +21 -0
- package/scripts/event-publication/measure.mjs +37 -5
- package/scripts/event-publication/writer.mjs +3 -8
- package/scripts/measure-full-runtime/probe.mjs +4 -2
- package/scripts/measure-full-runtime/recovery-trace-loader.mjs +51 -0
- package/scripts/measure-full-runtime/recovery-trace.mjs +16 -0
- package/scripts/measure-shared-reads.mjs +2 -1
- package/scripts/private-runtime/package-lock.json +18 -0
- package/scripts/private-runtime/package.json +4 -1
- package/scripts/source-provenance.mjs +2 -2
- package/scripts/test-on-pi.sh +2 -9
- package/src/handshake.ts +1 -1
- package/src/hosted-session.ts +32 -4
- package/src/maintenance.ts +35 -7
- package/src/registry.ts +28 -4
- package/src/runtime/attempt-invalidation.ts +54 -0
- package/src/runtime/background-history.ts +151 -45
- package/src/runtime/background-policy.ts +9 -4
- package/src/runtime/background-tools.ts +131 -23
- package/src/runtime/background.ts +351 -68
- package/src/runtime/cancellation.ts +28 -7
- package/src/runtime/catalog-sources.ts +199 -0
- package/src/runtime/catalog.ts +53 -21
- package/src/runtime/client.ts +22 -2
- package/src/runtime/domain/assignment-inbox.mjs +119 -0
- package/src/runtime/domain/assignment-observation.d.mts +1 -1
- package/src/runtime/domain/assignment-observation.mjs +43 -35
- package/src/runtime/domain/background/process-identity.mjs +20 -2
- package/src/runtime/domain/background/process.mjs +3 -3
- package/src/runtime/domain/background/registry.mjs +61 -3
- package/src/runtime/domain/background/types.mjs +1 -0
- package/src/runtime/domain/background-service/adapter-contract.domain.mjs +1 -1
- package/src/runtime/domain/background-service/adapter-contract.ts +1 -1
- package/src/runtime/domain/background-service/goal-provider.domain.mjs +35 -16
- package/src/runtime/domain/background-service/goal-provider.ts +31 -16
- package/src/runtime/domain/background-service/index.domain.mjs +6 -9
- package/src/runtime/domain/background-service/index.ts +6 -9
- package/src/runtime/domain/background-service/managed-authority.domain.mjs +6 -3
- package/src/runtime/domain/background-service/managed-authority.ts +5 -3
- package/src/runtime/domain/background-service/managed-client.domain.mjs +21 -7
- package/src/runtime/domain/background-service/managed-client.ts +20 -7
- package/src/runtime/domain/background-service/managed-frontend.domain.mjs +75 -17
- package/src/runtime/domain/background-service/managed-frontend.ts +62 -17
- package/src/runtime/domain/background-service/navigator-provider.domain.mjs +16 -5
- package/src/runtime/domain/background-service/navigator-provider.ts +15 -5
- package/src/runtime/domain/background-service/observed-wait-client.domain.mjs +6 -0
- package/src/runtime/domain/background-service/observed-wait-client.ts +10 -0
- package/src/runtime/domain/background-service/observed-waits.domain.mjs +170 -27
- package/src/runtime/domain/background-service/observed-waits.ts +154 -30
- package/src/runtime/domain/background-service/process-identity.domain.mjs +20 -2
- package/src/runtime/domain/background-service/process-identity.ts +18 -2
- package/src/runtime/domain/background-service/process.domain.mjs +3 -3
- package/src/runtime/domain/background-service/process.ts +3 -3
- package/src/runtime/domain/background-service/registry.domain.mjs +61 -3
- package/src/runtime/domain/background-service/registry.ts +53 -5
- package/src/runtime/domain/background-service/runtime.domain.mjs +32 -16
- package/src/runtime/domain/background-service/runtime.ts +19 -9
- package/src/runtime/domain/background-service/sandbox.domain.mjs +14 -15
- package/src/runtime/domain/background-service/sandbox.ts +14 -11
- package/src/runtime/domain/background-service/shared-callback-batcher.domain.mjs +8 -1
- package/src/runtime/domain/background-service/shared-callback-batcher.ts +11 -2
- package/src/runtime/domain/background-service/shared-navigator-panels.domain.mjs +24 -2
- package/src/runtime/domain/background-service/shared-navigator-panels.ts +26 -4
- package/src/runtime/domain/background-service/shared-navigator.domain.mjs +218 -13
- package/src/runtime/domain/background-service/shared-navigator.ts +197 -14
- package/src/runtime/domain/background-service/shared-operation-coordinator/assignment-inbox.mjs +144 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/assignment-observation.mjs +53 -41
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/assignment-changes.domain.mjs +75 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/assignment-changes.ts +73 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/assignment-outcomes.domain.mjs +2 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/assignment-outcomes.ts +2 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/background-requests.domain.mjs +11 -2
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/background-requests.ts +14 -3
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/discovery.domain.mjs +4 -2
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/discovery.ts +5 -3
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/operations.domain.mjs +44 -4
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/operations.ts +50 -2
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/result-supervision.domain.mjs +12 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/result-supervision.ts +12 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/rows.domain.mjs +2 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/rows.ts +2 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/schema.domain.mjs +42 -21
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/schema.ts +40 -24
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/store.domain.mjs +21 -3
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/store.ts +34 -8
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/subagent-launch.domain.mjs +23 -1
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/subagent-launch.ts +24 -1
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/types.domain.mjs +1 -1
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/types.ts +5 -1
- package/src/runtime/domain/background-service/shared-sandbox-core.ts +1 -1
- package/src/runtime/domain/background-service/shared-work-client.mjs +174 -7
- package/src/runtime/domain/background-service/supervisor/generated/process-identity.mjs +20 -2
- package/src/runtime/domain/background-service/supervisor/generated/process.mjs +3 -3
- package/src/runtime/domain/background-service/task-diagnostics.domain.mjs +1 -1
- package/src/runtime/domain/background-service/task-diagnostics.ts +1 -1
- package/src/runtime/domain/background-service/tools.domain.mjs +31 -24
- package/src/runtime/domain/background-service/tools.ts +41 -38
- package/src/runtime/domain/background-service/types.domain.mjs +1 -0
- package/src/runtime/domain/background-service/types.ts +3 -1
- package/src/runtime/domain/background-service/work-observation.domain.mjs +46 -20
- package/src/runtime/domain/background-service/work-observation.ts +46 -19
- package/src/runtime/domain/catalog/catalog-store.d.mts +9 -1
- package/src/runtime/domain/catalog/catalog-store.mjs +20 -22
- package/src/runtime/domain/coordinator/src/assignment-changes.mjs +75 -0
- package/src/runtime/domain/coordinator/src/assignment-outcomes.mjs +2 -0
- package/src/runtime/domain/coordinator/src/background-requests.mjs +11 -2
- package/src/runtime/domain/coordinator/src/discovery.mjs +4 -2
- package/src/runtime/domain/coordinator/src/operations.mjs +44 -4
- package/src/runtime/domain/coordinator/src/result-supervision.mjs +12 -0
- package/src/runtime/domain/coordinator/src/rows.mjs +2 -0
- package/src/runtime/domain/coordinator/src/schema.mjs +42 -21
- package/src/runtime/domain/coordinator/src/store.mjs +21 -3
- package/src/runtime/domain/coordinator/src/subagent-launch.mjs +23 -1
- package/src/runtime/domain/coordinator/src/types.mjs +1 -1
- package/src/runtime/domain/health-observation.d.mts +3 -1
- package/src/runtime/domain/health-observation.mjs +189 -190
- package/src/runtime/domain/log-cursor.d.mts +2 -0
- package/src/runtime/domain/log-cursor.mjs +85 -16
- package/src/runtime/domain/parse.d.mts +4 -1
- package/src/runtime/domain/parse.mjs +50 -5
- package/src/runtime/domain/sandbox-policy/README.md +61 -63
- package/src/runtime/domain/sandbox-policy/commands.domain.mjs +11 -8
- package/src/runtime/domain/sandbox-policy/commands.ts +12 -7
- package/src/runtime/domain/sandbox-policy/index.domain.mjs +53 -9
- package/src/runtime/domain/sandbox-policy/index.ts +52 -9
- package/src/runtime/domain/sandbox-policy/package.json +1 -1
- package/src/runtime/domain/sandbox-policy/permission-settings.domain.mjs +5 -2
- package/src/runtime/domain/sandbox-policy/permission-settings.ts +9 -2
- package/src/runtime/domain/sandbox-policy/permissions-page.domain.mjs +56 -137
- package/src/runtime/domain/sandbox-policy/permissions-page.ts +52 -130
- package/src/runtime/domain/sandbox-policy/permissions.domain.mjs +1 -6
- package/src/runtime/domain/sandbox-policy/permissions.ts +1 -6
- package/src/runtime/domain/sandbox-policy/shared-sandbox-core.ts +1 -1
- package/src/runtime/domain/sandbox-policy/shared-task-exec.domain.mjs +14 -1
- package/src/runtime/domain/sandbox-policy/shared-task-exec.ts +12 -1
- package/src/runtime/domain/sandbox-policy/shared-task-process-identity.domain.mjs +140 -0
- package/src/runtime/domain/sandbox-policy/shared-task-process-identity.ts +126 -0
- package/src/runtime/domain/sandbox-policy/shared-task-process-list.domain.mjs +3 -2
- package/src/runtime/domain/sandbox-policy/shared-task-process-list.ts +8 -2
- package/src/runtime/domain/sandbox-policy/shared-task-sandbox.domain.mjs +4 -3
- package/src/runtime/domain/sandbox-policy/shared-task-sandbox.ts +5 -3
- package/src/runtime/domain/sandbox-policy/shared-task-tools.domain.mjs +8 -5
- package/src/runtime/domain/sandbox-policy/shared-task-tools.ts +8 -5
- package/src/runtime/domain/subagents/README.md +1 -1
- package/src/runtime/domain/subagents/assignment-outcomes.domain.mjs +12 -6
- package/src/runtime/domain/subagents/assignment-outcomes.ts +11 -5
- package/src/runtime/domain/subagents/assignment-report.domain.mjs +87 -24
- package/src/runtime/domain/subagents/assignment-report.ts +83 -19
- package/src/runtime/domain/subagents/catalog-host-admission.domain.mjs +21 -3
- package/src/runtime/domain/subagents/catalog-host-admission.ts +19 -3
- package/src/runtime/domain/subagents/catalog-preferences.ts +1 -1
- package/src/runtime/domain/subagents/catalog-resolver.domain.mjs +35 -1
- package/src/runtime/domain/subagents/catalog-resolver.ts +35 -1
- package/src/runtime/domain/subagents/catalog-store.domain.mjs +20 -22
- package/src/runtime/domain/subagents/catalog-store.ts +27 -20
- package/src/runtime/domain/subagents/child-apply-patch.domain.mjs +13 -0
- package/src/runtime/domain/subagents/child-apply-patch.ts +13 -0
- package/src/runtime/domain/subagents/child-dependencies.mjs +27 -15
- package/src/runtime/domain/subagents/child-operations.domain.mjs +80 -2
- package/src/runtime/domain/subagents/child-operations.ts +80 -1
- package/src/runtime/domain/subagents/child-preflight.mjs +9 -2
- package/src/runtime/domain/subagents/child-process-list.domain.mjs +7 -0
- package/src/runtime/domain/subagents/child-process-list.ts +8 -0
- package/src/runtime/domain/subagents/config.domain.mjs +49 -2
- package/src/runtime/domain/subagents/config.ts +46 -2
- package/src/runtime/domain/subagents/continuation-authority.domain.mjs +5 -1
- package/src/runtime/domain/subagents/continuation-authority.ts +3 -1
- package/src/runtime/domain/subagents/continuation-runtime.domain.mjs +89 -0
- package/src/runtime/domain/subagents/continuation-runtime.ts +88 -0
- package/src/runtime/domain/subagents/extensions.mjs +11 -13
- package/src/runtime/domain/subagents/finalization.domain.mjs +12 -0
- package/src/runtime/domain/subagents/finalization.ts +15 -0
- package/src/runtime/domain/subagents/goal-provider.domain.mjs +44 -2
- package/src/runtime/domain/subagents/goal-provider.ts +44 -2
- package/src/runtime/domain/subagents/health-observation.domain.mjs +189 -190
- package/src/runtime/domain/subagents/health-observation.ts +43 -48
- package/src/runtime/domain/subagents/index.domain.mjs +296 -123
- package/src/runtime/domain/subagents/index.ts +257 -98
- package/src/runtime/domain/subagents/launch-context.domain.mjs +24 -0
- package/src/runtime/domain/subagents/launch-context.ts +23 -0
- package/src/runtime/domain/subagents/launch-digest.mjs +6 -0
- package/src/runtime/domain/subagents/log-cursor.domain.mjs +85 -16
- package/src/runtime/domain/subagents/log-cursor.ts +91 -15
- package/src/runtime/domain/subagents/managed-launch-bootstrap.mjs +2 -2
- package/src/runtime/domain/subagents/managed-launch-client.domain.mjs +6 -3
- package/src/runtime/domain/subagents/managed-launch-client.ts +6 -3
- package/src/runtime/domain/subagents/managed-launch-host.domain.mjs +222 -14
- package/src/runtime/domain/subagents/managed-launch-host.ts +168 -15
- package/src/runtime/domain/subagents/managed-launch-journal.mjs +1 -5
- package/src/runtime/domain/subagents/managed-launch-port.ts +4 -0
- package/src/runtime/domain/subagents/operation-bridge-types.domain.mjs +27 -2
- package/src/runtime/domain/subagents/operation-bridge-types.ts +27 -2
- package/src/runtime/domain/subagents/owned-exit.domain.mjs +12 -0
- package/src/runtime/domain/subagents/owned-exit.ts +12 -0
- package/src/runtime/domain/subagents/package.json +1 -1
- package/src/runtime/domain/subagents/parse.domain.mjs +50 -5
- package/src/runtime/domain/subagents/parse.ts +48 -6
- package/src/runtime/domain/subagents/permission-policy.domain.mjs +32 -8
- package/src/runtime/domain/subagents/permission-policy.ts +41 -8
- package/src/runtime/domain/subagents/process-identity.mjs +33 -0
- package/src/runtime/domain/subagents/registry.domain.mjs +47 -33
- package/src/runtime/domain/subagents/registry.ts +54 -33
- package/src/runtime/domain/subagents/shared-callback-batcher.domain.mjs +8 -1
- package/src/runtime/domain/subagents/shared-callback-batcher.ts +11 -2
- package/src/runtime/domain/subagents/shared-catalog.domain.mjs +6 -23
- package/src/runtime/domain/subagents/shared-catalog.ts +6 -14
- package/src/runtime/domain/subagents/shared-navigator-panels.domain.mjs +24 -2
- package/src/runtime/domain/subagents/shared-navigator-panels.ts +26 -4
- package/src/runtime/domain/subagents/shared-navigator.domain.mjs +218 -13
- package/src/runtime/domain/subagents/shared-navigator.ts +197 -14
- package/src/runtime/domain/subagents/shared-operation-coordinator/assignment-inbox.mjs +144 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/assignment-observation.mjs +53 -41
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/assignment-changes.domain.mjs +75 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/assignment-changes.ts +73 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/assignment-outcomes.domain.mjs +2 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/assignment-outcomes.ts +2 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/background-requests.domain.mjs +11 -2
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/background-requests.ts +14 -3
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/discovery.domain.mjs +4 -2
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/discovery.ts +5 -3
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/operations.domain.mjs +44 -4
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/operations.ts +50 -2
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/result-supervision.domain.mjs +12 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/result-supervision.ts +12 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/rows.domain.mjs +2 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/rows.ts +2 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/schema.domain.mjs +42 -21
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/schema.ts +40 -24
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/store.domain.mjs +21 -3
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/store.ts +34 -8
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/subagent-launch.domain.mjs +23 -1
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/subagent-launch.ts +24 -1
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/types.domain.mjs +1 -1
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/types.ts +5 -1
- package/src/runtime/domain/subagents/shared-sandbox-core.ts +1 -1
- package/src/runtime/domain/subagents/shared-task-exec.domain.mjs +14 -1
- package/src/runtime/domain/subagents/shared-task-exec.ts +12 -1
- package/src/runtime/domain/subagents/shared-task-process-identity.domain.mjs +140 -0
- package/src/runtime/domain/subagents/shared-task-process-identity.ts +126 -0
- package/src/runtime/domain/subagents/shared-task-process-list.domain.mjs +3 -2
- package/src/runtime/domain/subagents/shared-task-process-list.ts +8 -2
- package/src/runtime/domain/subagents/shared-task-sandbox.domain.mjs +4 -3
- package/src/runtime/domain/subagents/shared-task-sandbox.ts +5 -3
- package/src/runtime/domain/subagents/shared-task-tools.domain.mjs +8 -5
- package/src/runtime/domain/subagents/shared-task-tools.ts +8 -5
- package/src/runtime/domain/subagents/shared-work-client.mjs +174 -7
- package/src/runtime/domain/subagents/subagent-tools.domain.mjs +22 -38
- package/src/runtime/domain/subagents/subagent-tools.ts +28 -34
- package/src/runtime/domain/subagents/task-policy.domain.mjs +1 -1
- package/src/runtime/domain/subagents/task-policy.ts +3 -1
- package/src/runtime/domain/subagents/timing.domain.mjs +10 -1
- package/src/runtime/domain/subagents/timing.ts +10 -1
- package/src/runtime/domain/subagents/work-observation.domain.mjs +77 -16
- package/src/runtime/domain/subagents/work-observation.ts +71 -16
- package/src/runtime/domain/subagents/worker-operations-host.domain.mjs +9 -4
- package/src/runtime/domain/subagents/worker-operations-host.ts +12 -4
- package/src/runtime/domain/subagents/worker-waits.domain.mjs +89 -35
- package/src/runtime/domain/subagents/worker-waits.ts +93 -34
- package/src/runtime/health-config.ts +27 -11
- package/src/runtime/log-checkpoints.ts +117 -0
- package/src/runtime/mutation-store.ts +5 -4
- package/src/runtime/queries.ts +25 -2
- package/src/runtime/query-executor.ts +149 -28
- package/src/runtime/query-worker.mjs +106 -21
- package/src/runtime/read-model.ts +846 -120
- package/src/runtime/read-watchdog-probe.ts +4 -0
- package/src/runtime/result-supervision.ts +188 -16
- package/src/runtime/source-watch.ts +175 -0
- package/src/runtime/subagent-launch.ts +11 -4
- package/src/runtime/work-budget.ts +130 -0
- package/src/runtime/work-client.d.mts +8 -0
- package/src/runtime/work-client.mjs +150 -7
- package/src/server.ts +104 -23
- package/src/session-activity.ts +26 -4
- package/src/version.ts +1 -1
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
*/
|
|
23
23
|
import { closeSync, openSync, readSync, statSync, } from "node:fs";
|
|
24
24
|
import { readBoundedTail, tailTerminalDisplay } from "./shared-log-utils.domain.mjs";
|
|
25
|
-
import { readAppendedLines } from "./log-cursor.domain.mjs";
|
|
25
|
+
import { readAppendedLines, parseLogEvent, logReadMetrics } from "./log-cursor.domain.mjs";
|
|
26
26
|
import { logPathFor } from "./registry.domain.mjs";
|
|
27
27
|
const DEFAULT_PARSE_TAIL_BYTES = 32 * 1024 * 1024; // 32 MiB
|
|
28
28
|
const DEFAULT_RAW_TAIL_BYTES = 256 * 1024; // 256 KiB
|
|
@@ -885,6 +885,15 @@ function peekType(line) {
|
|
|
885
885
|
/** Runs tracked at once; a backstop against unbounded state, not a policy. */
|
|
886
886
|
const MAX_TRACKED_PARSE_RUNS = 64;
|
|
887
887
|
const parseStates = new Map();
|
|
888
|
+
export function encodeParseLogState(state) {
|
|
889
|
+
return { ...state, openToolCalls: [...state.openToolCalls] };
|
|
890
|
+
}
|
|
891
|
+
export function decodeParseLogState(saved) {
|
|
892
|
+
if (!saved?.usage || !Array.isArray(saved.openToolCalls) || !Array.isArray(saved.toolCalls)
|
|
893
|
+
|| !Number.isSafeInteger(saved.cursor?.offset) || saved.cursor.offset < 0)
|
|
894
|
+
throw new Error("Invalid display checkpoint");
|
|
895
|
+
return { ...saved, openToolCalls: new Map(saved.openToolCalls) };
|
|
896
|
+
}
|
|
888
897
|
/** Forget a run's accumulated parse (or all of them). */
|
|
889
898
|
export function resetParseRunCursor(id) {
|
|
890
899
|
if (id === undefined)
|
|
@@ -974,7 +983,7 @@ function selectParsableLines(lines) {
|
|
|
974
983
|
}
|
|
975
984
|
function tryParse(line) {
|
|
976
985
|
try {
|
|
977
|
-
return
|
|
986
|
+
return parseLogEvent(line);
|
|
978
987
|
}
|
|
979
988
|
catch {
|
|
980
989
|
return undefined;
|
|
@@ -995,6 +1004,7 @@ function activityTextOf(e) {
|
|
|
995
1004
|
}
|
|
996
1005
|
/** Fold one event into the run's accumulated state. */
|
|
997
1006
|
function foldParseEvent(state, e) {
|
|
1007
|
+
logReadMetrics.displayFolded++;
|
|
998
1008
|
const type = e.type;
|
|
999
1009
|
state.sawAnyEvent = true;
|
|
1000
1010
|
// Authoritative final answer: the last assistant message at run end.
|
|
@@ -1093,16 +1103,29 @@ function foldParseEvent(state, e) {
|
|
|
1093
1103
|
*/
|
|
1094
1104
|
export function parseRun(id, options = {}) {
|
|
1095
1105
|
const key = options.logPath ?? id;
|
|
1096
|
-
const
|
|
1097
|
-
const
|
|
1106
|
+
const states = options.states ?? parseStates;
|
|
1107
|
+
const previous = states.get(key);
|
|
1108
|
+
const path = options.logPath ?? logPathFor(id);
|
|
1109
|
+
let catchUp = false;
|
|
1110
|
+
if (options.summaryOnly) {
|
|
1111
|
+
try {
|
|
1112
|
+
catchUp = statSync(path).size > 1024 * 1024;
|
|
1113
|
+
}
|
|
1114
|
+
catch { /* read reports errors */ }
|
|
1115
|
+
}
|
|
1116
|
+
const read = readAppendedLines(path, previous?.cursor, catchUp ? Math.min(maxParseBytes(), 1024 * 1024) : maxParseBytes(), catchUp);
|
|
1098
1117
|
const state = read.restarted || !previous ? freshState(read.cursor) : previous;
|
|
1099
1118
|
state.cursor = read.cursor;
|
|
1119
|
+
state.catchingUp = read.more;
|
|
1100
1120
|
if (read.totalBytes > 0)
|
|
1101
1121
|
state.totalBytes = read.totalBytes;
|
|
1102
1122
|
if (read.truncated)
|
|
1103
1123
|
state.windowTruncated = true;
|
|
1104
1124
|
state.error = read.error;
|
|
1105
|
-
|
|
1125
|
+
if (options.states)
|
|
1126
|
+
states.set(key, state);
|
|
1127
|
+
else
|
|
1128
|
+
rememberParseState(key, state);
|
|
1106
1129
|
if (read.error !== undefined) {
|
|
1107
1130
|
return {
|
|
1108
1131
|
finalText: "", lastActivity: "", toolCalls: [], unmatchedToolCalls: [], sawEnd: false,
|
|
@@ -1112,6 +1135,11 @@ export function parseRun(id, options = {}) {
|
|
|
1112
1135
|
}
|
|
1113
1136
|
for (const { event } of selectParsableLines(read.lines))
|
|
1114
1137
|
foldParseEvent(state, event);
|
|
1138
|
+
if (options.summaryOnly) {
|
|
1139
|
+
state.finalText = "";
|
|
1140
|
+
state.lastActivity = "";
|
|
1141
|
+
state.toolCalls = state.toolCalls.slice(-256);
|
|
1142
|
+
}
|
|
1115
1143
|
// A log whose final line was never terminated — a writer mid-event, or one
|
|
1116
1144
|
// that simply did not end with a newline — still has to be readable. Fold it
|
|
1117
1145
|
// into a COPY: the cursor has not consumed those bytes, so the same line
|
|
@@ -1123,6 +1151,10 @@ export function parseRun(id, options = {}) {
|
|
|
1123
1151
|
foldParseEvent(view, event);
|
|
1124
1152
|
}
|
|
1125
1153
|
const diagnostics = [];
|
|
1154
|
+
if (state.catchingUp)
|
|
1155
|
+
diagnostics.push(`Usage catch-up pending after byte ${state.cursor.offset} of ${state.totalBytes}.`);
|
|
1156
|
+
if (options.summaryOnly && state.cursor.missing)
|
|
1157
|
+
diagnostics.push(`Incomplete usage: missing bytes [${state.cursor.missing[0]}, ${state.cursor.missing[1]}).`);
|
|
1126
1158
|
if (view.windowTruncated) {
|
|
1127
1159
|
diagnostics.push(`Log truncated: this session began reading at the last ${fmtBytes(maxParseBytes())} ` +
|
|
1128
1160
|
`of ${fmtBytes(view.totalBytes)}. Events before that point are not counted in tokens/tools.`);
|
|
@@ -1149,6 +1181,19 @@ export function parseRun(id, options = {}) {
|
|
|
1149
1181
|
diagnostics,
|
|
1150
1182
|
};
|
|
1151
1183
|
}
|
|
1184
|
+
/** Keep only the incremental facts needed by task-list displays. Detail reads use their own cursor. */
|
|
1185
|
+
export function parseRunDisplay(id, options) {
|
|
1186
|
+
const parsed = parseRun(id, options);
|
|
1187
|
+
const state = options.states.get(options.logPath ?? id);
|
|
1188
|
+
if (state) {
|
|
1189
|
+
state.finalText = "";
|
|
1190
|
+
state.lastActivity = "";
|
|
1191
|
+
state.toolCalls = state.toolCalls.slice(-1);
|
|
1192
|
+
state.openToolCalls.clear();
|
|
1193
|
+
state.modelError = undefined;
|
|
1194
|
+
}
|
|
1195
|
+
return { usage: parsed.usage, tool: parsed.toolCalls.at(-1) ?? "", diagnostics: parsed.diagnostics };
|
|
1196
|
+
}
|
|
1152
1197
|
/** Build the human-readable body for subagent_output. Exported for unit testing. */
|
|
1153
1198
|
export function formatSubagentOutputBody(head, tools, parsedBody, rawTail, diagnostics) {
|
|
1154
1199
|
let body;
|
|
@@ -27,7 +27,7 @@ import {
|
|
|
27
27
|
statSync,
|
|
28
28
|
} from "node:fs";
|
|
29
29
|
import { readBoundedTail, tailTerminalDisplay } from "./shared-log-utils.ts";
|
|
30
|
-
import { readAppendedLines, type LogCursor } from "./log-cursor.ts";
|
|
30
|
+
import { readAppendedLines, parseLogEvent, logReadMetrics, type LogCursor } from "./log-cursor.ts";
|
|
31
31
|
import { logPathFor } from "./registry.ts";
|
|
32
32
|
|
|
33
33
|
interface ContentBlock { type: string; text?: string; thinking?: string; name?: string }
|
|
@@ -1098,6 +1098,7 @@ function peekType(line: string): string | undefined {
|
|
|
1098
1098
|
}
|
|
1099
1099
|
|
|
1100
1100
|
interface ParseState {
|
|
1101
|
+
catchingUp?: boolean;
|
|
1101
1102
|
cursor: LogCursor;
|
|
1102
1103
|
usage: Usage;
|
|
1103
1104
|
finalText: string;
|
|
@@ -1123,6 +1124,16 @@ const MAX_TRACKED_PARSE_RUNS = 64;
|
|
|
1123
1124
|
|
|
1124
1125
|
const parseStates = new Map<string, ParseState>();
|
|
1125
1126
|
|
|
1127
|
+
export function encodeParseLogState(state: ParseState): unknown {
|
|
1128
|
+
return { ...state, openToolCalls: [...state.openToolCalls] };
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
export function decodeParseLogState(saved: any): ParseState {
|
|
1132
|
+
if (!saved?.usage || !Array.isArray(saved.openToolCalls) || !Array.isArray(saved.toolCalls)
|
|
1133
|
+
|| !Number.isSafeInteger(saved.cursor?.offset) || saved.cursor.offset < 0) throw new Error("Invalid display checkpoint");
|
|
1134
|
+
return { ...saved, openToolCalls: new Map(saved.openToolCalls) };
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1126
1137
|
/** Forget a run's accumulated parse (or all of them). */
|
|
1127
1138
|
export function resetParseRunCursor(id?: string): void {
|
|
1128
1139
|
if (id === undefined) parseStates.clear();
|
|
@@ -1208,7 +1219,7 @@ function selectParsableLines(lines: readonly string[]): Array<{ index: number; e
|
|
|
1208
1219
|
|
|
1209
1220
|
function tryParse(line: string): Record<string, unknown> | undefined {
|
|
1210
1221
|
try {
|
|
1211
|
-
return
|
|
1222
|
+
return parseLogEvent(line) as Record<string, unknown>;
|
|
1212
1223
|
} catch {
|
|
1213
1224
|
return undefined;
|
|
1214
1225
|
}
|
|
@@ -1228,6 +1239,7 @@ function activityTextOf(e: Record<string, unknown>): string | undefined {
|
|
|
1228
1239
|
|
|
1229
1240
|
/** Fold one event into the run's accumulated state. */
|
|
1230
1241
|
function foldParseEvent(state: ParseState, e: Record<string, unknown>): void {
|
|
1242
|
+
logReadMetrics.displayFolded++;
|
|
1231
1243
|
const type = e.type as string | undefined;
|
|
1232
1244
|
state.sawAnyEvent = true;
|
|
1233
1245
|
|
|
@@ -1312,16 +1324,25 @@ function foldParseEvent(state: ParseState, e: Record<string, unknown>): void {
|
|
|
1312
1324
|
* rotated, truncated, or rewritten log is detected and re-read from a bounded
|
|
1313
1325
|
* tail (see log-cursor.ts).
|
|
1314
1326
|
*/
|
|
1315
|
-
export function parseRun(id: string, options: { logPath?: string } = {}): ParsedRun {
|
|
1327
|
+
export function parseRun(id: string, options: { logPath?: string; states?: Map<string, ParseState>; summaryOnly?: boolean } = {}): ParsedRun {
|
|
1316
1328
|
const key = options.logPath ?? id;
|
|
1317
|
-
const
|
|
1318
|
-
const
|
|
1329
|
+
const states = options.states ?? parseStates;
|
|
1330
|
+
const previous = states.get(key);
|
|
1331
|
+
const path = options.logPath ?? logPathFor(id);
|
|
1332
|
+
let catchUp = false;
|
|
1333
|
+
if (options.summaryOnly) {
|
|
1334
|
+
try { catchUp = statSync(path).size > 1024 * 1024; } catch { /* read reports errors */ }
|
|
1335
|
+
}
|
|
1336
|
+
const read = readAppendedLines(path, previous?.cursor,
|
|
1337
|
+
catchUp ? Math.min(maxParseBytes(), 1024 * 1024) : maxParseBytes(), catchUp);
|
|
1319
1338
|
const state = read.restarted || !previous ? freshState(read.cursor) : previous;
|
|
1320
1339
|
state.cursor = read.cursor;
|
|
1340
|
+
state.catchingUp = read.more;
|
|
1321
1341
|
if (read.totalBytes > 0) state.totalBytes = read.totalBytes;
|
|
1322
1342
|
if (read.truncated) state.windowTruncated = true;
|
|
1323
1343
|
state.error = read.error;
|
|
1324
|
-
|
|
1344
|
+
if (options.states) states.set(key, state);
|
|
1345
|
+
else rememberParseState(key, state);
|
|
1325
1346
|
|
|
1326
1347
|
if (read.error !== undefined) {
|
|
1327
1348
|
return {
|
|
@@ -1332,6 +1353,11 @@ export function parseRun(id: string, options: { logPath?: string } = {}): Parsed
|
|
|
1332
1353
|
}
|
|
1333
1354
|
|
|
1334
1355
|
for (const { event } of selectParsableLines(read.lines)) foldParseEvent(state, event);
|
|
1356
|
+
if (options.summaryOnly) {
|
|
1357
|
+
state.finalText = "";
|
|
1358
|
+
state.lastActivity = "";
|
|
1359
|
+
state.toolCalls = state.toolCalls.slice(-256);
|
|
1360
|
+
}
|
|
1335
1361
|
|
|
1336
1362
|
// A log whose final line was never terminated — a writer mid-event, or one
|
|
1337
1363
|
// that simply did not end with a newline — still has to be readable. Fold it
|
|
@@ -1344,6 +1370,8 @@ export function parseRun(id: string, options: { logPath?: string } = {}): Parsed
|
|
|
1344
1370
|
}
|
|
1345
1371
|
|
|
1346
1372
|
const diagnostics: string[] = [];
|
|
1373
|
+
if (state.catchingUp) diagnostics.push(`Usage catch-up pending after byte ${state.cursor.offset} of ${state.totalBytes}.`);
|
|
1374
|
+
if (options.summaryOnly && state.cursor.missing) diagnostics.push(`Incomplete usage: missing bytes [${state.cursor.missing[0]}, ${state.cursor.missing[1]}).`);
|
|
1347
1375
|
if (view.windowTruncated) {
|
|
1348
1376
|
diagnostics.push(
|
|
1349
1377
|
`Log truncated: this session began reading at the last ${fmtBytes(maxParseBytes())} ` +
|
|
@@ -1374,6 +1402,20 @@ export function parseRun(id: string, options: { logPath?: string } = {}): Parsed
|
|
|
1374
1402
|
};
|
|
1375
1403
|
}
|
|
1376
1404
|
|
|
1405
|
+
/** Keep only the incremental facts needed by task-list displays. Detail reads use their own cursor. */
|
|
1406
|
+
export function parseRunDisplay(id: string, options: { logPath?: string; states: Map<string, ParseState>; summaryOnly?: boolean }): { usage: Usage; tool: string; diagnostics: string[] } {
|
|
1407
|
+
const parsed = parseRun(id, options);
|
|
1408
|
+
const state = options.states.get(options.logPath ?? id);
|
|
1409
|
+
if (state) {
|
|
1410
|
+
state.finalText = "";
|
|
1411
|
+
state.lastActivity = "";
|
|
1412
|
+
state.toolCalls = state.toolCalls.slice(-1);
|
|
1413
|
+
state.openToolCalls.clear();
|
|
1414
|
+
state.modelError = undefined;
|
|
1415
|
+
}
|
|
1416
|
+
return { usage: parsed.usage, tool: parsed.toolCalls.at(-1) ?? "", diagnostics: parsed.diagnostics };
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1377
1419
|
/** Build the human-readable body for subagent_output. Exported for unit testing. */
|
|
1378
1420
|
export function formatSubagentOutputBody(
|
|
1379
1421
|
head: string,
|
|
@@ -109,25 +109,49 @@ export function currentSandboxPermissions(pi) {
|
|
|
109
109
|
return mirror?.policy;
|
|
110
110
|
}
|
|
111
111
|
/** Resolve before allocating a run. Absent settings require an explicit sandbox request. */
|
|
112
|
-
export function resolveSubagentPermissions(pi, requestedSandbox) {
|
|
112
|
+
export function resolveSubagentPermissions(pi, requestedSandbox, saved) {
|
|
113
113
|
const snapshot = currentSandboxPermissions(pi);
|
|
114
114
|
const tools = snapshot?.subagentTools ?? defaultSubagentTools();
|
|
115
115
|
if (snapshot?.permissions?.enabled && !snapshot.permissions.commands) {
|
|
116
116
|
throw new Error("Main sandbox profile disables commands. Enable commands in the sandbox UI before launching a subagent.");
|
|
117
117
|
}
|
|
118
118
|
const child = snapshot?.subagentPermissions;
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
119
|
+
const requested = saved?.sandbox ?? requestedSandbox;
|
|
120
|
+
if (child?.enabled && requested === false) {
|
|
121
|
+
if (saved) {
|
|
122
|
+
throw new Error(`Cannot continue ${saved.runId}: the saved launch recipe has confinement Off, but the current Subagents profile requires it On. Launch a new run with a new recipe.`);
|
|
123
|
+
}
|
|
122
124
|
throw new Error("Subagent sandbox is enforced by the human-enabled profile; sandbox:false cannot bypass it. Change Subagents permissions in the sandbox UI.");
|
|
123
125
|
}
|
|
124
|
-
const sandboxEnabled = child
|
|
125
|
-
|
|
126
|
-
|
|
126
|
+
const sandboxEnabled = child?.enabled === true || requested === true;
|
|
127
|
+
const activationSource = saved ? "saved launch recipe"
|
|
128
|
+
: child?.enabled ? "current Subagents profile"
|
|
129
|
+
: requestedSandbox !== undefined ? "launch request"
|
|
130
|
+
: child ? "current Subagents profile" : "default (no Subagents profile)";
|
|
131
|
+
if (!child || !sandboxEnabled)
|
|
132
|
+
return { sandboxEnabled, enforced: false, activationSource, tools };
|
|
133
|
+
if (!child.enabled)
|
|
134
|
+
return { sandboxEnabled, enforced: true, activationSource, inactiveProfile: true, tools };
|
|
127
135
|
// Starting Pi is runtime work. These capabilities constrain task tools;
|
|
128
136
|
// they do not disable provider transport or the fixed file-operation worker.
|
|
129
137
|
const { enabled: _enabled, ...permissions } = child;
|
|
130
|
-
return { sandboxEnabled
|
|
138
|
+
return { sandboxEnabled, enforced: true, activationSource, permissions, tools };
|
|
139
|
+
}
|
|
140
|
+
export function effectiveWorkerProcessAccess(plan) {
|
|
141
|
+
return plan.inactiveProfile ? "read" : plan.permissions?.processAccess ?? "off";
|
|
142
|
+
}
|
|
143
|
+
export function describeSubagentProcessPolicy(plan, platform) {
|
|
144
|
+
const activation = `Worker confinement: ${plan.sandboxEnabled ? "On" : "Off"} (${plan.activationSource}).`;
|
|
145
|
+
const refresh = plan.activationSource === "saved launch recipe"
|
|
146
|
+
? " Changing confinement requires a new run with a new recipe."
|
|
147
|
+
: "";
|
|
148
|
+
if (!plan.sandboxEnabled)
|
|
149
|
+
return `${activation} No worker task-sandbox process policy is applied.${refresh}`;
|
|
150
|
+
const access = effectiveWorkerProcessAccess(plan) === "read" ? "Read" : "Off";
|
|
151
|
+
const source = plan.inactiveProfile ? "task sandbox defaults; Subagents profile Off"
|
|
152
|
+
: plan.permissions ? "current Subagents profile" : "default (no Subagents profile)";
|
|
153
|
+
const macOS = platform === "darwin" ? " macOS Seatbelt can deny absolute /bin/ps even with Process Read." : "";
|
|
154
|
+
return `${activation} Worker Process access: ${access} (${source}).${refresh}${macOS}`;
|
|
131
155
|
}
|
|
132
156
|
/**
|
|
133
157
|
* Consumer capability handshake for the shell / direct-application / time controls.
|
|
@@ -145,30 +145,63 @@ export function currentSandboxPermissions(pi: unknown): PermissionSnapshot | und
|
|
|
145
145
|
return mirror?.policy;
|
|
146
146
|
}
|
|
147
147
|
|
|
148
|
-
|
|
149
|
-
|
|
148
|
+
type SavedConfinement = { runId: string; sandbox: boolean };
|
|
149
|
+
|
|
150
|
+
type SubagentPermissionPlan = {
|
|
150
151
|
sandboxEnabled: boolean;
|
|
151
152
|
enforced: boolean;
|
|
153
|
+
activationSource: "saved launch recipe" | "current Subagents profile" | "launch request" | "default (no Subagents profile)";
|
|
154
|
+
inactiveProfile?: true;
|
|
152
155
|
permissions?: Omit<PermissionProfile, "enabled">;
|
|
153
156
|
/** Which extension tools a confined child may use; defaults when no settings are published. */
|
|
154
157
|
tools: SubagentToolSettings;
|
|
155
|
-
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/** Resolve before allocating a run. Absent settings require an explicit sandbox request. */
|
|
161
|
+
export function resolveSubagentPermissions(
|
|
162
|
+
pi: unknown, requestedSandbox: boolean | undefined, saved?: SavedConfinement,
|
|
163
|
+
): SubagentPermissionPlan {
|
|
156
164
|
const snapshot = currentSandboxPermissions(pi);
|
|
157
165
|
const tools = snapshot?.subagentTools ?? defaultSubagentTools();
|
|
158
166
|
if (snapshot?.permissions?.enabled && !snapshot.permissions.commands) {
|
|
159
167
|
throw new Error("Main sandbox profile disables commands. Enable commands in the sandbox UI before launching a subagent.");
|
|
160
168
|
}
|
|
161
169
|
const child = snapshot?.subagentPermissions;
|
|
162
|
-
|
|
163
|
-
if (child
|
|
170
|
+
const requested = saved?.sandbox ?? requestedSandbox;
|
|
171
|
+
if (child?.enabled && requested === false) {
|
|
172
|
+
if (saved) {
|
|
173
|
+
throw new Error(`Cannot continue ${saved.runId}: the saved launch recipe has confinement Off, but the current Subagents profile requires it On. Launch a new run with a new recipe.`);
|
|
174
|
+
}
|
|
164
175
|
throw new Error("Subagent sandbox is enforced by the human-enabled profile; sandbox:false cannot bypass it. Change Subagents permissions in the sandbox UI.");
|
|
165
176
|
}
|
|
166
|
-
const sandboxEnabled = child
|
|
167
|
-
|
|
177
|
+
const sandboxEnabled = child?.enabled === true || requested === true;
|
|
178
|
+
const activationSource = saved ? "saved launch recipe"
|
|
179
|
+
: child?.enabled ? "current Subagents profile"
|
|
180
|
+
: requestedSandbox !== undefined ? "launch request"
|
|
181
|
+
: child ? "current Subagents profile" : "default (no Subagents profile)";
|
|
182
|
+
if (!child || !sandboxEnabled) return { sandboxEnabled, enforced: false, activationSource, tools };
|
|
183
|
+
if (!child.enabled) return { sandboxEnabled, enforced: true, activationSource, inactiveProfile: true, tools };
|
|
168
184
|
// Starting Pi is runtime work. These capabilities constrain task tools;
|
|
169
185
|
// they do not disable provider transport or the fixed file-operation worker.
|
|
170
186
|
const { enabled: _enabled, ...permissions } = child;
|
|
171
|
-
return { sandboxEnabled
|
|
187
|
+
return { sandboxEnabled, enforced: true, activationSource, permissions, tools };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function effectiveWorkerProcessAccess(plan: SubagentPermissionPlan): "off" | "read" {
|
|
191
|
+
return plan.inactiveProfile ? "read" : plan.permissions?.processAccess ?? "off";
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export function describeSubagentProcessPolicy(plan: SubagentPermissionPlan, platform: string): string {
|
|
195
|
+
const activation = `Worker confinement: ${plan.sandboxEnabled ? "On" : "Off"} (${plan.activationSource}).`;
|
|
196
|
+
const refresh = plan.activationSource === "saved launch recipe"
|
|
197
|
+
? " Changing confinement requires a new run with a new recipe."
|
|
198
|
+
: "";
|
|
199
|
+
if (!plan.sandboxEnabled) return `${activation} No worker task-sandbox process policy is applied.${refresh}`;
|
|
200
|
+
const access = effectiveWorkerProcessAccess(plan) === "read" ? "Read" : "Off";
|
|
201
|
+
const source = plan.inactiveProfile ? "task sandbox defaults; Subagents profile Off"
|
|
202
|
+
: plan.permissions ? "current Subagents profile" : "default (no Subagents profile)";
|
|
203
|
+
const macOS = platform === "darwin" ? " macOS Seatbelt can deny absolute /bin/ps even with Process Read." : "";
|
|
204
|
+
return `${activation} Worker Process access: ${access} (${source}).${refresh}${macOS}`;
|
|
172
205
|
}
|
|
173
206
|
|
|
174
207
|
/**
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Generated fixed launch domain from packages/pi-better-subagents/process-identity.mjs. Do not edit directly.
|
|
2
|
+
import { execFileSync } from "node:child_process";
|
|
3
|
+
import { readFileSync } from "node:fs";
|
|
4
|
+
/** Best-effort process start identity. Unavailable does not prove the pid is dead. */
|
|
5
|
+
export function processStartToken(pid) {
|
|
6
|
+
if (process.platform === "linux") {
|
|
7
|
+
try {
|
|
8
|
+
const stat = readFileSync(`/proc/${pid}/stat`, "utf-8");
|
|
9
|
+
const close = stat.lastIndexOf(") ");
|
|
10
|
+
if (close >= 0) {
|
|
11
|
+
const starttime = stat.slice(close + 2).split(" ")[19];
|
|
12
|
+
if (starttime && /^\d+$/.test(starttime))
|
|
13
|
+
return starttime;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
if (error.code !== "ENOENT" && error.code !== "EACCES")
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
const out = execFileSync("ps", ["-o", "lstart=", "-p", String(pid)], {
|
|
23
|
+
encoding: "utf-8",
|
|
24
|
+
timeout: 1_000,
|
|
25
|
+
env: { ...process.env, LC_ALL: "C" },
|
|
26
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
27
|
+
}).trim().replace(/\s+/g, " ");
|
|
28
|
+
return out === "" ? undefined : out;
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -13,10 +13,12 @@
|
|
|
13
13
|
* run directory does not delete them.
|
|
14
14
|
*/
|
|
15
15
|
import { assignmentRetainsEvidence } from "./assignment-outcomes.domain.mjs";
|
|
16
|
-
import {
|
|
16
|
+
import { processStartToken } from "./process-identity.mjs";
|
|
17
|
+
export { processStartToken } from "./process-identity.mjs";
|
|
17
18
|
import { createHash, randomBytes } from "node:crypto";
|
|
18
19
|
import { closeSync, existsSync, fsyncSync, linkSync, mkdirSync, openSync, readFileSync, readdirSync, realpathSync, renameSync, rmSync, statSync, unlinkSync, writeFileSync } from "node:fs";
|
|
19
20
|
import { createRequire } from "node:module";
|
|
21
|
+
import { readFile, readdir } from "node:fs/promises";
|
|
20
22
|
import { tmpdir } from "node:os";
|
|
21
23
|
import { basename, dirname, join } from "node:path";
|
|
22
24
|
import { processExists } from "./spawn.domain.mjs";
|
|
@@ -172,6 +174,33 @@ export function listParkRecordCandidates(metas = listMetas()) {
|
|
|
172
174
|
return metas.filter((meta) => meta.park !== undefined
|
|
173
175
|
|| (volatileIds.has(meta.id) && existsSync(parkRecordPath(meta.id))));
|
|
174
176
|
}
|
|
177
|
+
/** Read recovery sidecars in batches so a large registry does not block socket replies. */
|
|
178
|
+
export async function* scanParkRecords(metas = listMetas()) {
|
|
179
|
+
const runs = join(baseDir(), "runs");
|
|
180
|
+
const volatileIds = new Set(await readdir(runs).catch(() => []));
|
|
181
|
+
const batchSize = 32;
|
|
182
|
+
for (let offset = 0; offset < metas.length; offset += batchSize) {
|
|
183
|
+
const batch = await Promise.all(metas.slice(offset, offset + batchSize).map(async (meta) => {
|
|
184
|
+
if (!meta.park && !volatileIds.has(meta.id))
|
|
185
|
+
return;
|
|
186
|
+
try {
|
|
187
|
+
const record = parseParkRecord(JSON.parse(await readFile(join(runs, meta.id, "park.json"), "utf-8")));
|
|
188
|
+
if (record)
|
|
189
|
+
return { meta, record };
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
if (error.code === "ENOENT" && meta.park) {
|
|
193
|
+
const record = readMeta(meta.id)?.park;
|
|
194
|
+
if (record)
|
|
195
|
+
return { meta, record };
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}));
|
|
199
|
+
for (const entry of batch)
|
|
200
|
+
if (entry)
|
|
201
|
+
yield entry;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
175
204
|
/**
|
|
176
205
|
* How long a launch attempt that never produced a child is kept before a sweep
|
|
177
206
|
* reclaims it. Launch preparation takes seconds; the grace only exists so a
|
|
@@ -473,36 +502,6 @@ function lockTimeout(id) {
|
|
|
473
502
|
let ownStartKnown = false;
|
|
474
503
|
let ownStartCache;
|
|
475
504
|
const foreignStartCache = new Map();
|
|
476
|
-
/** Best-effort process start identity. Unavailable does not prove the pid is dead. */
|
|
477
|
-
export function processStartToken(pid) {
|
|
478
|
-
if (process.platform === "linux") {
|
|
479
|
-
try {
|
|
480
|
-
const stat = readFileSync(`/proc/${pid}/stat`, "utf-8");
|
|
481
|
-
const close = stat.lastIndexOf(") ");
|
|
482
|
-
if (close >= 0) {
|
|
483
|
-
const starttime = stat.slice(close + 2).split(" ")[19];
|
|
484
|
-
if (starttime && /^\d+$/.test(starttime))
|
|
485
|
-
return starttime;
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
catch (error) {
|
|
489
|
-
if (errnoOf(error) !== "ENOENT" && errnoOf(error) !== "EACCES")
|
|
490
|
-
return undefined;
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
try {
|
|
494
|
-
const out = execFileSync("ps", ["-o", "lstart=", "-p", String(pid)], {
|
|
495
|
-
encoding: "utf-8",
|
|
496
|
-
timeout: 1_000,
|
|
497
|
-
env: { ...process.env, LC_ALL: "C" },
|
|
498
|
-
stdio: ["ignore", "pipe", "ignore"],
|
|
499
|
-
}).trim().replace(/\s+/g, " ");
|
|
500
|
-
return out === "" ? undefined : out;
|
|
501
|
-
}
|
|
502
|
-
catch {
|
|
503
|
-
return undefined;
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
505
|
function ownStartToken() {
|
|
507
506
|
if (ownStartKnown)
|
|
508
507
|
return ownStartCache;
|
|
@@ -974,10 +973,22 @@ export function observeArchivePublication(id, error) {
|
|
|
974
973
|
catch { /* the journal is best-effort; retaining the run is the safety net */ }
|
|
975
974
|
}
|
|
976
975
|
export function writeMeta(meta, options = {}) {
|
|
976
|
+
if (options.patch && !readMeta(meta.id))
|
|
977
|
+
return;
|
|
977
978
|
mkdirSync(runDir(meta.id), { recursive: true });
|
|
979
|
+
let written = false;
|
|
978
980
|
withRunMetaLock(meta.id, (owns) => {
|
|
979
981
|
if (options.ifAbsent && existsSync(metaPathFor(meta.id)))
|
|
980
982
|
return;
|
|
983
|
+
if (options.patch) {
|
|
984
|
+
const current = readMeta(meta.id);
|
|
985
|
+
if (!current)
|
|
986
|
+
return;
|
|
987
|
+
const patch = options.patch(current);
|
|
988
|
+
if (!patch)
|
|
989
|
+
return;
|
|
990
|
+
meta = { ...current, ...patch, id: meta.id };
|
|
991
|
+
}
|
|
981
992
|
metaWriteBarrier?.();
|
|
982
993
|
if (!owns())
|
|
983
994
|
throw new MetadataLockLost();
|
|
@@ -999,10 +1010,13 @@ export function writeMeta(meta, options = {}) {
|
|
|
999
1010
|
}
|
|
1000
1011
|
}
|
|
1001
1012
|
owns();
|
|
1013
|
+
written = true;
|
|
1002
1014
|
});
|
|
1015
|
+
if (options.patch && !written)
|
|
1016
|
+
return;
|
|
1003
1017
|
for (const listener of metaChangedListeners) {
|
|
1004
1018
|
try {
|
|
1005
|
-
listener();
|
|
1019
|
+
listener(meta);
|
|
1006
1020
|
}
|
|
1007
1021
|
catch { /* best effort */ }
|
|
1008
1022
|
}
|
|
@@ -1054,7 +1068,7 @@ export function removeMetaArtifacts(meta, options = {}) {
|
|
|
1054
1068
|
removeIndexEntry(join(baseDir(), "by-origin", originKey(originOf(meta))), meta.id);
|
|
1055
1069
|
for (const listener of metaChangedListeners) {
|
|
1056
1070
|
try {
|
|
1057
|
-
listener();
|
|
1071
|
+
listener(meta);
|
|
1058
1072
|
}
|
|
1059
1073
|
catch { /* best effort */ }
|
|
1060
1074
|
}
|