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
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
// Generated from packages/operation-coordinator/assignment-observation.mjs. Do not edit directly.
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
|
-
import {
|
|
3
|
+
import { AssignmentInbox } from "./assignment-inbox.mjs";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
|
|
6
|
+
// Only first-party source SQL passes this facade; quoted values stay untouched.
|
|
7
|
+
const sourceTables = new Set(["schema_meta", "sqlite_master", "sqlite_schema", "assignment_change_source", "assignment_changes",
|
|
8
|
+
"waits", "wait_dependencies", "resume_claims", "operations", "coordinator_state", "assignment_outcomes",
|
|
9
|
+
"subagent_launch_intents", "background_input_versions", "background_parent_callbacks", "assignments", "assignment_bindings", "assignment_attempts"]);
|
|
10
|
+
function sourceSQL(sql) {
|
|
11
|
+
if (/^PRAGMA\s+data_version\s*$/i.test(sql)) return "PRAGMA coordinator_source.data_version";
|
|
12
|
+
return sql.replace(/'(?:''|[^'])*'|\b[A-Za-z_][A-Za-z_0-9]*\b/g, token => sourceTables.has(token) ? `coordinator_source.${token}` : token);
|
|
13
|
+
}
|
|
4
14
|
|
|
5
15
|
/** Read-only, bounded projection input. Never creates/migrates a database. */
|
|
6
16
|
export function readAssignmentWaits(path, owner, connection) {
|
|
@@ -44,42 +54,63 @@ export function readAssignmentWaits(path, owner, connection) {
|
|
|
44
54
|
}
|
|
45
55
|
|
|
46
56
|
/** A root read model owns this connection; ordinary observers remain one-shot. */
|
|
47
|
-
export function createAssignmentReader(path) {
|
|
57
|
+
export function createAssignmentReader(path, inboxPath = ":memory:", derivedConnection, options = {}) {
|
|
58
|
+
const inbox = new AssignmentInbox(inboxPath, derivedConnection);
|
|
48
59
|
let db;
|
|
49
60
|
let identity;
|
|
50
61
|
let snapshot = false;
|
|
62
|
+
const composed = options.composed === true;
|
|
63
|
+
const owner = derivedConnection;
|
|
64
|
+
const dropSource = () => {
|
|
65
|
+
if (composed) { if (db) owner.exec("DETACH DATABASE coordinator_source"); }
|
|
66
|
+
else db?.close();
|
|
67
|
+
db = undefined; statements.clear();
|
|
68
|
+
};
|
|
51
69
|
const statements = new Map();
|
|
52
|
-
|
|
70
|
+
const reader = {
|
|
53
71
|
revision() {
|
|
54
72
|
const { statSync } = createRequire(import.meta.url)("node:fs");
|
|
55
73
|
const stat = statSync(path, { bigint: true });
|
|
56
74
|
const next = `${stat.dev}:${stat.ino}`;
|
|
57
75
|
if (identity !== next) {
|
|
58
76
|
if (snapshot) throw new Error("Coordinator identity changed during observation");
|
|
59
|
-
|
|
60
|
-
db = undefined;
|
|
61
|
-
statements.clear();
|
|
77
|
+
dropSource();
|
|
62
78
|
identity = next;
|
|
63
79
|
}
|
|
64
80
|
const { DatabaseSync } = createRequire(import.meta.url)("node:sqlite");
|
|
65
|
-
|
|
81
|
+
if (!db) {
|
|
82
|
+
if (composed) {
|
|
83
|
+
const uri = pathToFileURL(path); uri.searchParams.set("mode", "ro");
|
|
84
|
+
owner.prepare("ATTACH DATABASE ? AS coordinator_source").run(uri.href);
|
|
85
|
+
db = { prepare: sql => owner.prepare(sourceSQL(sql)), get observationActive() { return snapshot; }, beginSnapshot: () => reader.beginSnapshot() };
|
|
86
|
+
} else db = new DatabaseSync(path, { readOnly: true });
|
|
87
|
+
}
|
|
66
88
|
return `${identity}:${db.prepare("PRAGMA data_version").get().data_version}`;
|
|
67
89
|
},
|
|
68
90
|
connection(refresh = true) { if (refresh) this.revision(); return db; },
|
|
91
|
+
changes() { this.revision(); return inbox.receive(db, identity); },
|
|
92
|
+
acknowledgeChanges(batch, complete) { inbox.acknowledge(batch, complete); },
|
|
93
|
+
get changeMetrics() { return inbox.metrics; },
|
|
94
|
+
get snapshotActive() { return snapshot; },
|
|
69
95
|
|
|
70
96
|
beginSnapshot() {
|
|
71
97
|
this.revision();
|
|
72
98
|
if (snapshot) throw new Error("Coordinator snapshot already open");
|
|
73
|
-
db
|
|
99
|
+
const transaction = composed ? owner : db;
|
|
100
|
+
transaction.exec("BEGIN");
|
|
74
101
|
snapshot = true;
|
|
75
102
|
const release = () => {
|
|
76
103
|
if (!snapshot) return;
|
|
77
|
-
try {
|
|
104
|
+
try { transaction.exec("ROLLBACK"); } finally { snapshot = false; }
|
|
78
105
|
};
|
|
79
106
|
try {
|
|
80
107
|
// Pin the WAL read view before recording its revision; other writers keep advancing.
|
|
81
108
|
db.prepare("SELECT count(*) FROM sqlite_schema").get();
|
|
82
|
-
return { revision: this.revision(), release
|
|
109
|
+
return { revision: this.revision(), release, commit: () => {
|
|
110
|
+
if (!snapshot) return;
|
|
111
|
+
transaction.exec("COMMIT");
|
|
112
|
+
snapshot = false;
|
|
113
|
+
} };
|
|
83
114
|
} catch (error) { release(); throw error; }
|
|
84
115
|
},
|
|
85
116
|
|
|
@@ -94,30 +125,6 @@ export function createAssignmentReader(path) {
|
|
|
94
125
|
return { verified: false, waits: [], reason: "Coordinator state is unverified; compatible supervision is unavailable." };
|
|
95
126
|
}
|
|
96
127
|
},
|
|
97
|
-
conversationRevisions() {
|
|
98
|
-
try {
|
|
99
|
-
this.revision();
|
|
100
|
-
if (!["1", "2", "3", "4"].includes(db.prepare("SELECT value FROM schema_meta WHERE key = 'schema_version'").get()?.value)) return undefined;
|
|
101
|
-
const hashes = new Map();
|
|
102
|
-
const add = (sessionId, workspace, value) => {
|
|
103
|
-
const key = JSON.stringify([sessionId, workspace]);
|
|
104
|
-
if (!hashes.has(key)) hashes.set(key, createHash("sha256"));
|
|
105
|
-
hashes.get(key).update(JSON.stringify(value));
|
|
106
|
-
};
|
|
107
|
-
// One source pass per changed database revision, instead of one
|
|
108
|
-
// projection/query/log parse for every historical attempt.
|
|
109
|
-
for (const row of db.prepare(`SELECT w.*, (SELECT json_group_array(attempt_id)
|
|
110
|
-
FROM resume_claims c WHERE c.wait_id = w.wait_id AND c.generation = w.generation) AS claimed_attempts
|
|
111
|
-
FROM waits w WHERE owner_kind = 'worker-conversation' ORDER BY wait_id, generation`).all()) {
|
|
112
|
-
add(row.owner_session_id, row.owner_workspace, row);
|
|
113
|
-
}
|
|
114
|
-
for (const row of db.prepare("SELECT record_json FROM assignment_outcomes ORDER BY assignment_id").all()) {
|
|
115
|
-
const record = JSON.parse(row.record_json);
|
|
116
|
-
add(record.owner.sessionId, record.owner.workspace, record);
|
|
117
|
-
}
|
|
118
|
-
return new Map([...hashes].map(([key, hash]) => [key, hash.digest("hex")]));
|
|
119
|
-
} catch { return undefined; }
|
|
120
|
-
},
|
|
121
128
|
upgradeObligations() {
|
|
122
129
|
try { this.revision(); } catch (error) {
|
|
123
130
|
if (error.code === "ENOENT") return [];
|
|
@@ -144,7 +151,8 @@ export function createAssignmentReader(path) {
|
|
|
144
151
|
return [...obligations, ...reasons];
|
|
145
152
|
},
|
|
146
153
|
|
|
147
|
-
close() { if (snapshot) { db.exec("ROLLBACK"); snapshot = false; }
|
|
154
|
+
close() { if (snapshot) { (composed ? owner : db).exec("ROLLBACK"); snapshot = false; } dropSource(); identity = undefined; inbox.close(); },
|
|
148
155
|
|
|
149
156
|
};
|
|
157
|
+
return reader;
|
|
150
158
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
1
2
|
import { execFileSync } from "node:child_process";
|
|
2
3
|
/**
|
|
3
4
|
* Process start-token reads.
|
|
@@ -20,11 +21,12 @@ const TOKEN_MEMO_TTL_MS = 1_000;
|
|
|
20
21
|
/** Bound on memo entries; the panel is small, but a long session is not. */
|
|
21
22
|
const TOKEN_MEMO_MAX_ENTRIES = 512;
|
|
22
23
|
const tokenMemo = new Map();
|
|
23
|
-
function readTokenUncached(pid) {
|
|
24
|
+
function readTokenUncached(pid, admission = false) {
|
|
24
25
|
if (!Number.isInteger(pid) || pid <= 0 || process.platform === "win32")
|
|
25
26
|
return undefined;
|
|
26
27
|
try {
|
|
27
|
-
const output = execFileSync("ps", ["-o", "lstart=", "-p", String(pid)], {
|
|
28
|
+
const output = execFileSync(admission ? "/bin/ps" : "ps", ["-o", "lstart=", "-p", String(pid)], {
|
|
29
|
+
env: admission ? { LC_ALL: "C", TZ: "UTC0", PATH: "/usr/bin:/bin" } : process.env,
|
|
28
30
|
encoding: "utf8",
|
|
29
31
|
stdio: ["ignore", "pipe", "ignore"],
|
|
30
32
|
timeout: 2_000,
|
|
@@ -35,6 +37,22 @@ function readTokenUncached(pid) {
|
|
|
35
37
|
return undefined;
|
|
36
38
|
}
|
|
37
39
|
}
|
|
40
|
+
// Admission crosses process environments; stored task tokens retain their original format.
|
|
41
|
+
export function readAdmissionStartToken(pid) {
|
|
42
|
+
if (process.platform !== "linux")
|
|
43
|
+
return readTokenUncached(pid, true);
|
|
44
|
+
if (!Number.isInteger(pid) || pid <= 0)
|
|
45
|
+
return undefined;
|
|
46
|
+
try {
|
|
47
|
+
const stat = readFileSync(`/proc/${pid}/stat`, "utf8");
|
|
48
|
+
// Fields after the parenthesized command start at field 3. Start time is field 22.
|
|
49
|
+
const start = stat.slice(stat.lastIndexOf(")") + 2).split(" ")[19];
|
|
50
|
+
return start && /^\d+$/.test(start) ? start : undefined;
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
38
56
|
function remember(pid, token, now) {
|
|
39
57
|
if (tokenMemo.size >= TOKEN_MEMO_MAX_ENTRIES) {
|
|
40
58
|
// Drop expired entries first; only evict live ones if that was not enough.
|
|
@@ -59,8 +59,8 @@ export function validateCommandSpec(spec) {
|
|
|
59
59
|
/** Convert a Windows path to the `/c/...` form MSYS bash resolves in redirections. Exposed for tests and reuse. */
|
|
60
60
|
export function toMsysPath(path) {
|
|
61
61
|
const forward = path.replace(/\\/g, "/");
|
|
62
|
-
const drive = /^([A-Za-z]):(\/.+)$/.exec(forward);
|
|
63
|
-
return drive ? `/${drive
|
|
62
|
+
const [, drive, rest] = /^([A-Za-z]):(\/.+)$/.exec(forward) ?? [];
|
|
63
|
+
return drive && rest ? `/${drive.toLowerCase()}${rest}` : forward;
|
|
64
64
|
}
|
|
65
65
|
/** Single-quote a value for safe literal use in a bash script line. Exposed for tests and reuse. */
|
|
66
66
|
export function bashSingleQuote(value) {
|
|
@@ -144,7 +144,7 @@ export function parseStepGateFailure(logText, id) {
|
|
|
144
144
|
if (!line.startsWith(marker) || !line.endsWith(" ---"))
|
|
145
145
|
continue;
|
|
146
146
|
const match = /^--- step gate \[[^\]]+\]: first failing step exit=(-?\d+); shell exit=(-?\d+): (.*) ---$/.exec(line);
|
|
147
|
-
if (!match)
|
|
147
|
+
if (!match || match[3] === undefined)
|
|
148
148
|
continue;
|
|
149
149
|
const stepExitCode = Number(match[1]);
|
|
150
150
|
const shellExitCode = Number(match[2]);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
|
-
import { mkdirSync, readFileSync, readdirSync, rmSync, statSync, unlinkSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, unlinkSync, writeFileSync } from "node:fs";
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { isTerminalStatus } from "./types.mjs";
|
|
@@ -64,7 +64,7 @@ export function writeMeta(meta) {
|
|
|
64
64
|
indexMeta(meta);
|
|
65
65
|
for (const listener of metaChangedListeners) {
|
|
66
66
|
try {
|
|
67
|
-
listener();
|
|
67
|
+
listener(meta);
|
|
68
68
|
}
|
|
69
69
|
catch { /* best effort */ }
|
|
70
70
|
}
|
|
@@ -162,9 +162,10 @@ export function removeMeta(meta) {
|
|
|
162
162
|
metaCache.delete(meta.id);
|
|
163
163
|
removeIndexEntry(originIndexDir(originOf(meta)), meta.id);
|
|
164
164
|
removeIndexEntry(originActiveIndexDir(originOf(meta)), meta.id);
|
|
165
|
+
removeIndexEntry(recentPath(originOf(meta)), meta.id);
|
|
165
166
|
for (const listener of metaChangedListeners) {
|
|
166
167
|
try {
|
|
167
|
-
listener();
|
|
168
|
+
listener(meta);
|
|
168
169
|
}
|
|
169
170
|
catch { /* best effort */ }
|
|
170
171
|
}
|
|
@@ -232,6 +233,7 @@ export function listActiveMetasForOrigin(origin) {
|
|
|
232
233
|
export function observeMetasForOrigin(origin, activeOnly = true) {
|
|
233
234
|
const directory = activeOnly ? originActiveIndexDir(origin) : originIndexDir(origin);
|
|
234
235
|
let ids;
|
|
236
|
+
registryIo.indexDirectoryReads++;
|
|
235
237
|
try {
|
|
236
238
|
ids = readdirSync(directory).filter(id => id !== ".initialized");
|
|
237
239
|
}
|
|
@@ -248,6 +250,50 @@ export function observeMetasForOrigin(origin, activeOnly = true) {
|
|
|
248
250
|
}).filter((meta) => !!meta && !!meta.callbackOrigin && belongsToOrigin(meta, origin)
|
|
249
251
|
&& (!activeOnly || meta.status === "running")).sort((a, b) => b.startedAt - a.startedAt);
|
|
250
252
|
}
|
|
253
|
+
const RECENT_LIMIT = 256;
|
|
254
|
+
const RECENT_MS = 30000;
|
|
255
|
+
function recentPath(origin) {
|
|
256
|
+
return join(baseDir(), "by-origin-recent", originKey(origin));
|
|
257
|
+
}
|
|
258
|
+
function recentEntries(origin) {
|
|
259
|
+
const directory = recentPath(origin);
|
|
260
|
+
try {
|
|
261
|
+
registryIo.indexDirectoryReads++;
|
|
262
|
+
const entries = readdirSync(directory).filter(id => !id.startsWith(".")).flatMap(id => {
|
|
263
|
+
try {
|
|
264
|
+
registryIo.indexRevisionChecks++;
|
|
265
|
+
return [{ id, observedAt: statSync(join(directory, id)).mtimeMs }];
|
|
266
|
+
}
|
|
267
|
+
catch (error) {
|
|
268
|
+
if (error.code === "ENOENT")
|
|
269
|
+
return [];
|
|
270
|
+
throw error;
|
|
271
|
+
}
|
|
272
|
+
}).sort((a, b) => b.observedAt - a.observedAt || b.id.localeCompare(a.id));
|
|
273
|
+
return entries.filter(entry => Date.now() - entry.observedAt < RECENT_MS).slice(0, RECENT_LIMIT);
|
|
274
|
+
}
|
|
275
|
+
catch (error) {
|
|
276
|
+
if (error.code === "ENOENT")
|
|
277
|
+
return [];
|
|
278
|
+
throw error;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
export function recentMetaDeadline(origin) {
|
|
282
|
+
return recentEntries(origin).map(entry => entry.observedAt + RECENT_MS);
|
|
283
|
+
}
|
|
284
|
+
/** Exact cooperative source identity; no metadata or historical enumeration. */
|
|
285
|
+
export function observationRevisionForOrigin(origin) {
|
|
286
|
+
return [originActiveIndexDir(origin), recentPath(origin)].map(indexDirectorySignature).join(";");
|
|
287
|
+
}
|
|
288
|
+
/** Bounded cooperative writes, including results written before the first reader. */
|
|
289
|
+
export function observeRecentMetasForOrigin(origin) {
|
|
290
|
+
return recentEntries(origin).map(entry => {
|
|
291
|
+
const record = inspectMeta(entry.id);
|
|
292
|
+
if (record.found && !record.meta)
|
|
293
|
+
throw new Error(`Unverified background task ${entry.id}: ${record.error}`);
|
|
294
|
+
return record.meta;
|
|
295
|
+
}).filter((meta) => !!meta && belongsToOrigin(meta, origin) && meta.dismissedAt === undefined);
|
|
296
|
+
}
|
|
251
297
|
function readMetaForSweep(id) {
|
|
252
298
|
const cached = metaCache.get(id);
|
|
253
299
|
if (cached && isTerminalStatus(cached.status))
|
|
@@ -296,6 +342,18 @@ function indexMeta(meta) {
|
|
|
296
342
|
writeIndexEntry(activeDirectory, meta.id);
|
|
297
343
|
else
|
|
298
344
|
removeIndexEntry(activeDirectory, meta.id);
|
|
345
|
+
const origin = originOf(meta), recentDirectory = recentPath(origin);
|
|
346
|
+
mkdirSync(recentDirectory, { recursive: true });
|
|
347
|
+
// Independent pointers preserve concurrent writers; prune only this bounded
|
|
348
|
+
// recent-write accelerator, never the authoritative metadata or history.
|
|
349
|
+
const candidates = readdirSync(recentDirectory).filter(id => !id.startsWith("."));
|
|
350
|
+
const retained = new Set(recentEntries(origin).filter(entry => entry.id !== meta.id).slice(0, RECENT_LIMIT - 1).map(entry => entry.id));
|
|
351
|
+
for (const id of candidates)
|
|
352
|
+
if (id !== meta.id && !retained.has(id))
|
|
353
|
+
removeIndexEntry(recentDirectory, id);
|
|
354
|
+
const temporary = join(recentDirectory, `.${meta.id}.${process.pid}.next`);
|
|
355
|
+
writeFileSync(temporary, "");
|
|
356
|
+
renameSync(temporary, join(recentDirectory, meta.id));
|
|
299
357
|
}
|
|
300
358
|
catch {
|
|
301
359
|
// Indexes are accelerators; meta.json remains authoritative.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Generated fixed launch domain from packages/pi-better-background-tasks/src/adapter-contract.ts. Do not edit directly.
|
|
2
|
-
import { getObservedWaitHost } from "./observed-
|
|
2
|
+
import { getObservedWaitHost } from "./observed-wait-client.domain.mjs";
|
|
3
3
|
import { inspectMeta } from "./registry.domain.mjs";
|
|
4
4
|
/** Optional-peer contract, mirrored by pi-better-sandbox/background-admission.ts. */
|
|
5
5
|
export const BACKGROUND_ADAPTER_REQUEST = "pi-better-background-tasks:adapter-request";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import { getObservedWaitHost } from "./observed-
|
|
2
|
+
import { getObservedWaitHost } from "./observed-wait-client.js";
|
|
3
3
|
import { inspectMeta } from "./registry.js";
|
|
4
4
|
|
|
5
5
|
/** Optional-peer contract, mirrored by pi-better-sandbox/background-admission.ts. */
|
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
// Generated fixed launch domain from packages/pi-better-background-tasks/src/goal-provider.ts. Do not edit directly.
|
|
2
2
|
import { listMetas, onMetaChanged } from "./registry.domain.mjs";
|
|
3
|
-
import { getObservedWaitHost } from "./observed-
|
|
3
|
+
import { getObservedWaitHost } from "./observed-wait-client.domain.mjs";
|
|
4
4
|
import { isTerminalStatus } from "./types.domain.mjs";
|
|
5
5
|
import { backgroundTaskProgressAt, observeBackgroundTaskStall } from "./stall.domain.mjs";
|
|
6
|
-
import {
|
|
6
|
+
import { backgroundGoalObservation, onBackgroundObservation } from "./work-observation.domain.mjs";
|
|
7
7
|
const GOAL_READY_EVENT = "pi-better-goal:ready";
|
|
8
8
|
const GOAL_REGISTER_PROVIDER_EVENT = "pi-better-goal:register-provider";
|
|
9
9
|
const readySubscriptions = new WeakSet();
|
|
10
10
|
export function collectBackgroundTaskGoalActivity(metas = listMetas(), ctx, now = Date.now(), durable = []) {
|
|
11
|
+
const items = [
|
|
12
|
+
...metas.filter((meta) => isVisibleGoalActivity(meta, ctx, now)).map(backgroundTaskToGoalItem),
|
|
13
|
+
...durable.filter((task) => isVisibleDurableGoalActivity(task, ctx, now)).map(durableTaskToGoalItem),
|
|
14
|
+
];
|
|
15
|
+
const deadlines = items.filter(item => item.terminal && typeof item.endedAt === "number")
|
|
16
|
+
.map(item => item.endedAt + TERMINAL_GOAL_ACTIVITY_RETENTION_MS).filter(deadline => deadline > now);
|
|
11
17
|
return {
|
|
12
18
|
providerId: "background-tasks",
|
|
13
19
|
label: "Background Tasks",
|
|
14
|
-
items
|
|
15
|
-
|
|
16
|
-
.filter((meta) => isVisibleGoalActivity(meta, ctx, now))
|
|
17
|
-
.map(backgroundTaskToGoalItem),
|
|
18
|
-
...durable
|
|
19
|
-
.filter((task) => isVisibleDurableGoalActivity(task, ctx, now))
|
|
20
|
-
.map(durableTaskToGoalItem),
|
|
21
|
-
],
|
|
20
|
+
items,
|
|
21
|
+
nextDeadline: deadlines.length ? Math.min(...deadlines) : null,
|
|
22
22
|
};
|
|
23
23
|
}
|
|
24
24
|
export function registerBackgroundTasksGoalProvider(pi) {
|
|
@@ -27,20 +27,35 @@ export function registerBackgroundTasksGoalProvider(pi) {
|
|
|
27
27
|
id: "background-tasks",
|
|
28
28
|
label: "Background Tasks",
|
|
29
29
|
scope: "session",
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
activityVersion: 1,
|
|
31
|
+
getActivity: async (ctx, options) => {
|
|
32
|
+
const snapshot = await backgroundGoalObservation(pi, ctx, options?.cachedOnly);
|
|
33
|
+
if (!snapshot && options?.cachedOnly)
|
|
34
|
+
return { providerId: "background-tasks", coverage: "unverified", items: [], nextDeadline: null };
|
|
32
35
|
if (snapshot.status !== "ready")
|
|
33
36
|
throw new Error(snapshot.reason ?? "Background activity is unverified");
|
|
34
37
|
const durable = snapshot.rows.filter((row) => row.meta.durable).map((row) => row.meta);
|
|
35
38
|
const legacy = snapshot.rows.filter((row) => !row.meta.durable).map((row) => row.meta);
|
|
36
|
-
|
|
39
|
+
const activity = collectBackgroundTaskGoalActivity(legacy, ctx, Date.now(), durable);
|
|
40
|
+
const deadlines = [activity.nextDeadline, snapshot.nextDeadline].filter((value) => typeof value === "number" && value > Date.now());
|
|
41
|
+
return { ...activity, nextDeadline: deadlines.length ? Math.min(...deadlines) : null };
|
|
37
42
|
},
|
|
38
43
|
// A durable operation change must refresh Goal activity even though it
|
|
39
44
|
// never writes a mutable legacy registry meta.
|
|
40
|
-
onActivityChanged: (notify) => {
|
|
45
|
+
onActivityChanged: (notify, ctx) => {
|
|
41
46
|
const offRuntime = onBackgroundObservation(pi, notify);
|
|
42
|
-
const offMeta = onMetaChanged(
|
|
43
|
-
|
|
47
|
+
const offMeta = onMetaChanged(meta => {
|
|
48
|
+
if (ctx && meta && !belongsToGoalActivitySession(meta, ctx))
|
|
49
|
+
return;
|
|
50
|
+
notify();
|
|
51
|
+
});
|
|
52
|
+
const offDurable = getObservedWaitHost(pi)?.onDurableTaskChange(change => {
|
|
53
|
+
if (ctx && (change.workspace !== ctx.cwd || change.sessionId !== ctx.sessionManager.getSessionId()))
|
|
54
|
+
return;
|
|
55
|
+
if (ctx && change.branchId && !ctx.sessionManager.getBranch().some(entry => entry.id === change.branchId))
|
|
56
|
+
return;
|
|
57
|
+
notify();
|
|
58
|
+
});
|
|
44
59
|
return () => {
|
|
45
60
|
offRuntime();
|
|
46
61
|
offMeta();
|
|
@@ -75,6 +90,8 @@ function belongsToGoalActivitySession(meta, ctx) {
|
|
|
75
90
|
if (origin) {
|
|
76
91
|
if (origin.cwd !== active.cwd)
|
|
77
92
|
return false;
|
|
93
|
+
if (origin.branchId && !ctx.sessionManager.getBranch().some(entry => entry.id === origin.branchId))
|
|
94
|
+
return false;
|
|
78
95
|
if (origin.sessionId || active.sessionId)
|
|
79
96
|
return origin.sessionId === active.sessionId;
|
|
80
97
|
return true;
|
|
@@ -100,6 +117,8 @@ function isVisibleDurableGoalActivity(task, ctx, now) {
|
|
|
100
117
|
const active = getGoalActivityOrigin(ctx);
|
|
101
118
|
if (task.owner.workspace !== active.cwd)
|
|
102
119
|
return false;
|
|
120
|
+
if (task.owner.branchId && !ctx.sessionManager.getBranch().some(entry => entry.id === task.owner.branchId))
|
|
121
|
+
return false;
|
|
103
122
|
if (active.sessionId || task.owner.sessionId) {
|
|
104
123
|
if (task.owner.sessionId !== active.sessionId)
|
|
105
124
|
return false;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import { listMetas, onMetaChanged } from "./registry.js";
|
|
3
|
-
import { getObservedWaitHost
|
|
3
|
+
import { getObservedWaitHost } from "./observed-wait-client.js";
|
|
4
|
+
import type { DurableTaskProjection } from "./observed-waits.js";
|
|
4
5
|
import type { BackgroundTaskMeta } from "./types.js";
|
|
5
6
|
import { isTerminalStatus } from "./types.js";
|
|
6
7
|
import { backgroundTaskProgressAt, observeBackgroundTaskStall } from "./stall.js";
|
|
7
|
-
import {
|
|
8
|
+
import { backgroundGoalObservation, onBackgroundObservation } from "./work-observation.js";
|
|
8
9
|
|
|
9
10
|
const GOAL_READY_EVENT = "pi-better-goal:ready";
|
|
10
11
|
const GOAL_REGISTER_PROVIDER_EVENT = "pi-better-goal:register-provider";
|
|
@@ -27,6 +28,7 @@ export interface GoalBackgroundProviderSnapshot {
|
|
|
27
28
|
providerId: string;
|
|
28
29
|
label?: string;
|
|
29
30
|
items: GoalBackgroundWorkItem[];
|
|
31
|
+
nextDeadline?: number | null;
|
|
30
32
|
}
|
|
31
33
|
|
|
32
34
|
export function collectBackgroundTaskGoalActivity(
|
|
@@ -35,17 +37,17 @@ export function collectBackgroundTaskGoalActivity(
|
|
|
35
37
|
now = Date.now(),
|
|
36
38
|
durable: readonly DurableTaskProjection[] = [],
|
|
37
39
|
): GoalBackgroundProviderSnapshot {
|
|
40
|
+
const items = [
|
|
41
|
+
...metas.filter((meta) => isVisibleGoalActivity(meta, ctx, now)).map(backgroundTaskToGoalItem),
|
|
42
|
+
...durable.filter((task) => isVisibleDurableGoalActivity(task, ctx, now)).map(durableTaskToGoalItem),
|
|
43
|
+
];
|
|
44
|
+
const deadlines = items.filter(item => item.terminal && typeof item.endedAt === "number")
|
|
45
|
+
.map(item => item.endedAt! + TERMINAL_GOAL_ACTIVITY_RETENTION_MS).filter(deadline => deadline > now);
|
|
38
46
|
return {
|
|
39
47
|
providerId: "background-tasks",
|
|
40
48
|
label: "Background Tasks",
|
|
41
|
-
items
|
|
42
|
-
|
|
43
|
-
.filter((meta) => isVisibleGoalActivity(meta, ctx, now))
|
|
44
|
-
.map(backgroundTaskToGoalItem),
|
|
45
|
-
...durable
|
|
46
|
-
.filter((task) => isVisibleDurableGoalActivity(task, ctx, now))
|
|
47
|
-
.map(durableTaskToGoalItem),
|
|
48
|
-
],
|
|
49
|
+
items,
|
|
50
|
+
nextDeadline: deadlines.length ? Math.min(...deadlines) : null,
|
|
49
51
|
};
|
|
50
52
|
}
|
|
51
53
|
|
|
@@ -55,19 +57,30 @@ export function registerBackgroundTasksGoalProvider(pi: ExtensionAPI): void {
|
|
|
55
57
|
id: "background-tasks",
|
|
56
58
|
label: "Background Tasks",
|
|
57
59
|
scope: "session",
|
|
58
|
-
|
|
59
|
-
|
|
60
|
+
activityVersion: 1,
|
|
61
|
+
getActivity: async (ctx: ExtensionContext, options?: { cachedOnly?: boolean }) => {
|
|
62
|
+
const snapshot = await backgroundGoalObservation(pi, ctx, options?.cachedOnly);
|
|
63
|
+
if (!snapshot && options?.cachedOnly) return { providerId: "background-tasks", coverage: "unverified", items: [], nextDeadline: null };
|
|
60
64
|
if (snapshot.status !== "ready") throw new Error(snapshot.reason ?? "Background activity is unverified");
|
|
61
65
|
const durable = snapshot.rows.filter((row: any) => row.meta.durable).map((row: any) => row.meta);
|
|
62
66
|
const legacy = snapshot.rows.filter((row: any) => !row.meta.durable).map((row: any) => row.meta);
|
|
63
|
-
|
|
67
|
+
const activity = collectBackgroundTaskGoalActivity(legacy, ctx, Date.now(), durable);
|
|
68
|
+
const deadlines = [activity.nextDeadline, snapshot.nextDeadline].filter((value): value is number => typeof value === "number" && value > Date.now());
|
|
69
|
+
return { ...activity, nextDeadline: deadlines.length ? Math.min(...deadlines) : null };
|
|
64
70
|
},
|
|
65
71
|
// A durable operation change must refresh Goal activity even though it
|
|
66
72
|
// never writes a mutable legacy registry meta.
|
|
67
|
-
onActivityChanged: (notify: () => void) => {
|
|
73
|
+
onActivityChanged: (notify: () => void, ctx?: ExtensionContext) => {
|
|
68
74
|
const offRuntime = onBackgroundObservation(pi, notify);
|
|
69
|
-
const offMeta = onMetaChanged(
|
|
70
|
-
|
|
75
|
+
const offMeta = onMetaChanged(meta => {
|
|
76
|
+
if (ctx && meta && !belongsToGoalActivitySession(meta, ctx)) return;
|
|
77
|
+
notify();
|
|
78
|
+
});
|
|
79
|
+
const offDurable = getObservedWaitHost(pi)?.onDurableTaskChange(change => {
|
|
80
|
+
if (ctx && (change.workspace !== ctx.cwd || change.sessionId !== ctx.sessionManager.getSessionId())) return;
|
|
81
|
+
if (ctx && change.branchId && !ctx.sessionManager.getBranch().some(entry => entry.id === change.branchId)) return;
|
|
82
|
+
notify();
|
|
83
|
+
});
|
|
71
84
|
return () => {
|
|
72
85
|
offRuntime();
|
|
73
86
|
offMeta();
|
|
@@ -100,6 +113,7 @@ function belongsToGoalActivitySession(meta: BackgroundTaskMeta, ctx: ExtensionCo
|
|
|
100
113
|
const origin = meta.callbackOrigin;
|
|
101
114
|
if (origin) {
|
|
102
115
|
if (origin.cwd !== active.cwd) return false;
|
|
116
|
+
if (origin.branchId && !ctx.sessionManager.getBranch().some(entry => entry.id === origin.branchId)) return false;
|
|
103
117
|
if (origin.sessionId || active.sessionId) return origin.sessionId === active.sessionId;
|
|
104
118
|
return true;
|
|
105
119
|
}
|
|
@@ -126,6 +140,7 @@ function isVisibleDurableGoalActivity(
|
|
|
126
140
|
if (ctx) {
|
|
127
141
|
const active = getGoalActivityOrigin(ctx);
|
|
128
142
|
if (task.owner.workspace !== active.cwd) return false;
|
|
143
|
+
if (task.owner.branchId && !ctx.sessionManager.getBranch().some(entry => entry.id === task.owner.branchId)) return false;
|
|
129
144
|
if (active.sessionId || task.owner.sessionId) {
|
|
130
145
|
if (task.owner.sessionId !== active.sessionId) return false;
|
|
131
146
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// Generated fixed launch domain from packages/pi-better-background-tasks/src/index.ts. Do not edit directly.
|
|
2
|
+
import { shouldRestoreWorkObservation } from "./shared-work-client.mjs";
|
|
2
3
|
import { initializeNavigatorPanels } from "./shared-navigator-panels.domain.mjs";
|
|
3
4
|
import { registerBackgroundTasksGoalProvider } from "./goal-provider.domain.mjs";
|
|
4
5
|
import { createBackgroundTasksNavigator } from "./navigator-provider.domain.mjs";
|
|
5
|
-
import { createBackgroundTaskRuntime } from "./runtime.domain.mjs";
|
|
6
6
|
import { createManagedBackgroundFrontend } from "./managed-frontend.domain.mjs";
|
|
7
7
|
import { observeForegroundSandboxPolicy } from "./sandbox.domain.mjs";
|
|
8
8
|
import { registerTools } from "./tools.domain.mjs";
|
|
@@ -10,10 +10,9 @@ import { deliverBackgroundTasksPrompt } from "./prompt.domain.mjs";
|
|
|
10
10
|
import { closeBackgroundObservation } from "./work-observation.domain.mjs";
|
|
11
11
|
export default function backgroundTasksExtension(pi) {
|
|
12
12
|
// The frontend owns native UI/delivery only. Mechanical tools run in the
|
|
13
|
-
// fixed server service
|
|
14
|
-
const runtime = createBackgroundTaskRuntime();
|
|
13
|
+
// fixed server service. Register their native definitions before the first turn.
|
|
15
14
|
const { host: observedWaits, proxy, ui } = createManagedBackgroundFrontend(pi);
|
|
16
|
-
const navigator = createBackgroundTasksNavigator(
|
|
15
|
+
const navigator = createBackgroundTasksNavigator(undefined, ui);
|
|
17
16
|
initializeNavigatorPanels(pi);
|
|
18
17
|
// Sections on a host that renders them; text only where it renders none.
|
|
19
18
|
// Returning a prompt on modern pi would set forceSystemPrompt, and the request
|
|
@@ -26,7 +25,6 @@ export default function backgroundTasksExtension(pi) {
|
|
|
26
25
|
observeForegroundSandboxPolicy(pi);
|
|
27
26
|
// Registered before registerTools(pi), so this runs before running tasks are resumed.
|
|
28
27
|
pi.on("session_start", async (_event, ctx) => {
|
|
29
|
-
runtime.resumeScheduledWork();
|
|
30
28
|
registerBackgroundTasksGoalProvider(pi);
|
|
31
29
|
navigator.ensureBackgroundTasksNavigator(ctx);
|
|
32
30
|
});
|
|
@@ -39,14 +37,13 @@ export default function backgroundTasksExtension(pi) {
|
|
|
39
37
|
closeBackgroundObservation(pi);
|
|
40
38
|
// /reload, session replacement, and quit load a fresh instance; stop this one's timers (#324).
|
|
41
39
|
observedWaits.close();
|
|
42
|
-
runtime.suspendScheduledWork();
|
|
43
40
|
navigator.clearBackgroundTasksNavigatorSession();
|
|
44
41
|
});
|
|
45
|
-
registerTools(proxy,
|
|
42
|
+
registerTools(proxy, undefined, navigator.refreshBackgroundTasksNavigator, { resumeLocal: false });
|
|
46
43
|
// Registered after registerTools so the callback batcher has its session
|
|
47
44
|
// context before a reconciled delivery can enqueue a parent notification.
|
|
48
|
-
pi.on("session_start",
|
|
49
|
-
observedWaits.start(ctx);
|
|
45
|
+
pi.on("session_start", (event, ctx) => {
|
|
46
|
+
observedWaits.start(ctx, shouldRestoreWorkObservation(event, ctx.sessionManager));
|
|
50
47
|
});
|
|
51
48
|
// A fork or branch navigation does not restart the session, but it changes
|
|
52
49
|
// which continuation may receive a queued callback. Update the host snapshot
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
import { shouldRestoreWorkObservation } from "./shared-work-client.mjs";
|
|
1
2
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
3
|
import { initializeNavigatorPanels } from "./shared-navigator-panels.ts";
|
|
3
4
|
import { registerBackgroundTasksGoalProvider } from "./goal-provider.js";
|
|
4
5
|
import { createBackgroundTasksNavigator } from "./navigator-provider.js";
|
|
5
|
-
import { createBackgroundTaskRuntime } from "./runtime.js";
|
|
6
6
|
import { createManagedBackgroundFrontend } from "./managed-frontend.js";
|
|
7
7
|
import { observeForegroundSandboxPolicy } from "./sandbox.js";
|
|
8
8
|
import { registerTools } from "./tools.js";
|
|
@@ -11,10 +11,9 @@ import { closeBackgroundObservation } from "./work-observation.js";
|
|
|
11
11
|
|
|
12
12
|
export default function backgroundTasksExtension(pi: ExtensionAPI): void {
|
|
13
13
|
// The frontend owns native UI/delivery only. Mechanical tools run in the
|
|
14
|
-
// fixed server service
|
|
15
|
-
const runtime = createBackgroundTaskRuntime();
|
|
14
|
+
// fixed server service. Register their native definitions before the first turn.
|
|
16
15
|
const {host: observedWaits, proxy, ui} = createManagedBackgroundFrontend(pi);
|
|
17
|
-
const navigator = createBackgroundTasksNavigator(
|
|
16
|
+
const navigator = createBackgroundTasksNavigator(undefined, ui);
|
|
18
17
|
initializeNavigatorPanels(pi);
|
|
19
18
|
// Sections on a host that renders them; text only where it renders none.
|
|
20
19
|
// Returning a prompt on modern pi would set forceSystemPrompt, and the request
|
|
@@ -27,7 +26,6 @@ export default function backgroundTasksExtension(pi: ExtensionAPI): void {
|
|
|
27
26
|
observeForegroundSandboxPolicy(pi);
|
|
28
27
|
// Registered before registerTools(pi), so this runs before running tasks are resumed.
|
|
29
28
|
pi.on("session_start", async (_event, ctx) => {
|
|
30
|
-
runtime.resumeScheduledWork();
|
|
31
29
|
registerBackgroundTasksGoalProvider(pi);
|
|
32
30
|
navigator.ensureBackgroundTasksNavigator(ctx);
|
|
33
31
|
});
|
|
@@ -40,14 +38,13 @@ export default function backgroundTasksExtension(pi: ExtensionAPI): void {
|
|
|
40
38
|
closeBackgroundObservation(pi);
|
|
41
39
|
// /reload, session replacement, and quit load a fresh instance; stop this one's timers (#324).
|
|
42
40
|
observedWaits.close();
|
|
43
|
-
runtime.suspendScheduledWork();
|
|
44
41
|
navigator.clearBackgroundTasksNavigatorSession();
|
|
45
42
|
});
|
|
46
|
-
registerTools(proxy,
|
|
43
|
+
registerTools(proxy, undefined, navigator.refreshBackgroundTasksNavigator, {resumeLocal:false});
|
|
47
44
|
// Registered after registerTools so the callback batcher has its session
|
|
48
45
|
// context before a reconciled delivery can enqueue a parent notification.
|
|
49
|
-
pi.on("session_start",
|
|
50
|
-
observedWaits.start(ctx);
|
|
46
|
+
pi.on("session_start", (event, ctx) => {
|
|
47
|
+
observedWaits.start(ctx, shouldRestoreWorkObservation(event, ctx.sessionManager));
|
|
51
48
|
});
|
|
52
49
|
// A fork or branch navigation does not restart the session, but it changes
|
|
53
50
|
// which continuation may receive a queued callback. Update the host snapshot
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { createHash, timingSafeEqual } from 'node:crypto';
|
|
3
3
|
import { lstatSync, readFileSync, realpathSync } from 'node:fs';
|
|
4
4
|
import { join } from 'node:path';
|
|
5
|
-
import { readProcessStartTokenFresh } from "./process-identity.domain.mjs";
|
|
5
|
+
import { readAdmissionStartToken, readProcessStartTokenFresh } from "./process-identity.domain.mjs";
|
|
6
6
|
const ordered = (value) => value === null || typeof value !== 'object' ? value : Array.isArray(value) ? value.map(ordered) : Object.fromEntries(Object.keys(value).sort().map(key => [key, ordered(value[key])]));
|
|
7
7
|
export const backgroundDigest = (value) => createHash('sha256').update(JSON.stringify(ordered(value))).digest('hex');
|
|
8
8
|
export function readBackgroundAuthority(root, instance, authority, requireLive = true) {
|
|
@@ -14,14 +14,17 @@ export function readBackgroundAuthority(root, instance, authority, requireLive =
|
|
|
14
14
|
|| !stat.isFile() || stat.isSymbolicLink() || stat.uid !== dir.uid || (stat.mode & 0o077) || stat.size > 1024 * 1024)
|
|
15
15
|
throw new Error('background-authority-refused');
|
|
16
16
|
const record = JSON.parse(readFileSync(path, 'utf8')), digest = createHash('sha256').update(authority.secret).digest('hex');
|
|
17
|
-
if (record.version !== 1 || record.instanceId !== instance || record.id !== authority.id || typeof record.secretHash !== 'string'
|
|
17
|
+
if ((record.version !== 1 && record.version !== 2) || record.instanceId !== instance || record.id !== authority.id || typeof record.secretHash !== 'string'
|
|
18
18
|
|| record.secretHash.length !== digest.length || !timingSafeEqual(Buffer.from(record.secretHash), Buffer.from(digest))
|
|
19
19
|
|| typeof record.key !== 'string' || !record.key || typeof record.tool !== 'string' || !record.owner?.sessionId
|
|
20
20
|
|| record.owner.workspace !== realpathSync(record.owner.workspace) || !Number.isSafeInteger(record.pid) || !record.start
|
|
21
21
|
|| !Array.isArray(record.branchIds) || record.branchIds.some((id) => typeof id !== 'string'))
|
|
22
22
|
throw new Error('background-authority-refused');
|
|
23
23
|
if (requireLive) {
|
|
24
|
-
|
|
24
|
+
const start = record.version === 2 ? readAdmissionStartToken(record.pid) : readProcessStartTokenFresh(record.pid);
|
|
25
|
+
if (!start)
|
|
26
|
+
throw new Error('background-authority-refused: cannot verify host start time');
|
|
27
|
+
if (start !== record.start)
|
|
25
28
|
throw new Error('background-authority-refused: host identity changed');
|
|
26
29
|
process.kill(record.pid, 0);
|
|
27
30
|
}
|