@phyzess/pappra 0.1.0-beta.13 → 0.1.0-beta.18
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/README.md +134 -15
- package/assets/integrations/skills/pappra/SKILL.md +28 -7
- package/assets/integrations/skills/pappra/references/collaboration.md +633 -0
- package/assets/integrations/skills/pappra/references/environment.md +360 -0
- package/assets/integrations/skills/pappra/references/hosts.md +20 -0
- package/assets/integrations/skills/pappra/references/operations.md +36 -5
- package/dist/adapters/pi/recovery-extension.d.ts +65 -0
- package/dist/adapters/pi/recovery-extension.js +238 -0
- package/dist/adapters/pi/session-transition-extension.d.ts +30 -0
- package/dist/adapters/pi/session-transition-extension.js +265 -0
- package/dist/app/automatic-handoff-events.d.ts +26 -0
- package/dist/app/automatic-handoff-events.js +134 -0
- package/dist/app/bounded-file.d.ts +2 -0
- package/dist/app/bounded-file.js +2 -0
- package/dist/app/capability-evidence/claude.d.ts +37 -0
- package/dist/app/capability-evidence/claude.js +206 -0
- package/dist/app/capability-evidence/codex.d.ts +35 -0
- package/dist/app/capability-evidence/codex.js +193 -0
- package/dist/app/capability-evidence/index.d.ts +33 -0
- package/dist/app/capability-evidence/index.js +117 -0
- package/dist/app/capability-evidence/pi.d.ts +73 -0
- package/dist/app/capability-evidence/pi.js +669 -0
- package/dist/app/capability-evidence/shared.d.ts +69 -0
- package/dist/app/capability-evidence/shared.js +195 -0
- package/dist/app/collab-auto-handoff.d.ts +55 -0
- package/dist/app/collab-auto-handoff.js +384 -0
- package/dist/app/collab-bindings.d.ts +57 -0
- package/dist/app/collab-bindings.js +1006 -0
- package/dist/app/collab-bootstrap-transport.d.ts +163 -0
- package/dist/app/collab-bootstrap-transport.js +769 -0
- package/dist/app/collab-bootstrap.d.ts +137 -0
- package/dist/app/collab-bootstrap.js +3455 -0
- package/dist/app/collab-handoff.d.ts +12 -0
- package/dist/app/collab-handoff.js +511 -0
- package/dist/app/collab-messages.d.ts +2 -0
- package/dist/app/collab-messages.js +2 -0
- package/dist/app/collab-notify.d.ts +156 -0
- package/dist/app/collab-notify.js +1208 -0
- package/dist/app/collab-preflight.d.ts +34 -0
- package/dist/app/collab-preflight.js +96 -0
- package/dist/app/collab-recovery-executor.d.ts +161 -0
- package/dist/app/collab-recovery-executor.js +1153 -0
- package/dist/app/collab-recovery-projection.d.ts +103 -0
- package/dist/app/collab-recovery-projection.js +659 -0
- package/dist/app/collab-recovery.d.ts +25 -0
- package/dist/app/collab-recovery.js +429 -0
- package/dist/app/collab-resources.d.ts +16 -0
- package/dist/app/collab-resources.js +278 -0
- package/dist/app/collab-role-events.d.ts +22 -0
- package/dist/app/collab-role-events.js +329 -0
- package/dist/app/collab-role-orchestration.d.ts +53 -0
- package/dist/app/collab-role-orchestration.js +601 -0
- package/dist/app/collab-role-plan.d.ts +81 -0
- package/dist/app/collab-role-plan.js +504 -0
- package/dist/app/collab-role-repair.d.ts +109 -0
- package/dist/app/collab-role-repair.js +821 -0
- package/dist/app/collab-rollover.d.ts +263 -0
- package/dist/app/collab-rollover.js +1112 -0
- package/dist/app/collab-route.d.ts +64 -0
- package/dist/app/collab-route.js +240 -0
- package/dist/app/collab-session-transition.d.ts +221 -0
- package/dist/app/collab-session-transition.js +3334 -0
- package/dist/app/collab-state.d.ts +7 -0
- package/dist/app/collab-state.js +162 -0
- package/dist/app/collab-task-store.d.ts +22 -0
- package/dist/app/collab-task-store.js +204 -0
- package/dist/app/collab-work-execution.d.ts +127 -0
- package/dist/app/collab-work-execution.js +1027 -0
- package/dist/app/collab-work-plan.d.ts +87 -0
- package/dist/app/collab-work-plan.js +437 -0
- package/dist/app/collaboration.d.ts +8 -2
- package/dist/app/collaboration.js +101 -12
- package/dist/app/doctor.js +108 -5
- package/dist/app/host-mcp.js +34 -8
- package/dist/app/host-probe.js +4 -4
- package/dist/app/init.d.ts +58 -4
- package/dist/app/init.js +768 -155
- package/dist/app/managed-pi-bootstrap-admission.d.ts +14 -0
- package/dist/app/managed-pi-bootstrap-admission.js +104 -0
- package/dist/app/managed-pi-bootstrap-refresh.d.ts +10 -0
- package/dist/app/managed-pi-bootstrap-refresh.js +78 -0
- package/dist/app/managed-pi-delivery.d.ts +8 -0
- package/dist/app/managed-pi-delivery.js +110 -0
- package/dist/app/managed-pi-handoff-receiver.d.ts +15 -0
- package/dist/app/managed-pi-handoff-receiver.js +263 -0
- package/dist/app/managed-pi-handoff-reference.d.ts +3 -0
- package/dist/app/managed-pi-handoff-reference.js +60 -0
- package/dist/app/managed-pi-herdr.d.ts +21 -0
- package/dist/app/managed-pi-herdr.js +185 -0
- package/dist/app/managed-pi-initial-admission.d.ts +28 -0
- package/dist/app/managed-pi-initial-admission.js +416 -0
- package/dist/app/managed-pi-initial-launch.d.ts +5 -0
- package/dist/app/managed-pi-initial-launch.js +61 -0
- package/dist/app/managed-pi-native-observation.d.ts +36 -0
- package/dist/app/managed-pi-native-observation.js +191 -0
- package/dist/app/managed-pi-readiness.d.ts +29 -0
- package/dist/app/managed-pi-readiness.js +288 -0
- package/dist/app/managed-pi-repair-stop.d.ts +21 -0
- package/dist/app/managed-pi-repair-stop.js +371 -0
- package/dist/app/managed-pi-resume-admission.d.ts +32 -0
- package/dist/app/managed-pi-resume-admission.js +618 -0
- package/dist/app/managed-pi-retirement.d.ts +28 -0
- package/dist/app/managed-pi-retirement.js +112 -0
- package/dist/app/managed-pi-sdk-control.d.ts +7 -0
- package/dist/app/managed-pi-sdk-control.js +169 -0
- package/dist/app/managed-pi-sdk-runtime.d.ts +3 -0
- package/dist/app/managed-pi-sdk-runtime.js +483 -0
- package/dist/app/managed-pi-source-admission.d.ts +14 -0
- package/dist/app/managed-pi-source-admission.js +323 -0
- package/dist/app/managed-pi-transition-host.d.ts +36 -0
- package/dist/app/managed-pi-transition-host.js +292 -0
- package/dist/app/managed-pi.d.ts +54 -0
- package/dist/app/managed-pi.js +248 -0
- package/dist/app/shell-readiness.d.ts +66 -0
- package/dist/app/shell-readiness.js +368 -0
- package/dist/app/task-pin.d.ts +2 -23
- package/dist/app/task-pin.js +2 -113
- package/dist/cli/commands/collaboration-binding.d.ts +13 -0
- package/dist/cli/commands/collaboration-binding.js +118 -0
- package/dist/cli/commands/collaboration-bootstrap.d.ts +10 -0
- package/dist/cli/commands/collaboration-bootstrap.js +84 -0
- package/dist/cli/commands/collaboration-capabilities.d.ts +13 -0
- package/dist/cli/commands/collaboration-capabilities.js +127 -0
- package/dist/cli/commands/collaboration-handoff.d.ts +13 -0
- package/dist/cli/commands/collaboration-handoff.js +102 -0
- package/dist/cli/commands/collaboration-message.d.ts +17 -0
- package/dist/cli/commands/collaboration-message.js +177 -0
- package/dist/cli/commands/collaboration-plan.d.ts +8 -0
- package/dist/cli/commands/collaboration-plan.js +108 -0
- package/dist/cli/commands/collaboration-preflight.d.ts +10 -0
- package/dist/cli/commands/collaboration-preflight.js +167 -0
- package/dist/cli/commands/collaboration-recovery-run.d.ts +46 -0
- package/dist/cli/commands/collaboration-recovery-run.js +224 -0
- package/dist/cli/commands/collaboration-recovery.d.ts +11 -0
- package/dist/cli/commands/collaboration-recovery.js +104 -0
- package/dist/cli/commands/collaboration-resources.d.ts +11 -0
- package/dist/cli/commands/collaboration-resources.js +96 -0
- package/dist/cli/commands/collaboration-role-plan.d.ts +9 -0
- package/dist/cli/commands/collaboration-role-plan.js +69 -0
- package/dist/cli/commands/collaboration-role-run.d.ts +11 -0
- package/dist/cli/commands/collaboration-role-run.js +128 -0
- package/dist/cli/commands/collaboration-rollover.d.ts +10 -0
- package/dist/cli/commands/collaboration-rollover.js +103 -0
- package/dist/cli/commands/collaboration-state.d.ts +10 -0
- package/dist/cli/commands/collaboration-state.js +96 -0
- package/dist/cli/commands/collaboration-transition.d.ts +10 -0
- package/dist/cli/commands/collaboration-transition.js +75 -0
- package/dist/cli/commands/collaboration-work-run.d.ts +34 -0
- package/dist/cli/commands/collaboration-work-run.js +178 -0
- package/dist/cli/commands/collaboration.d.ts +4 -0
- package/dist/cli/commands/collaboration.js +39 -4
- package/dist/cli/commands/continuity.js +2 -6
- package/dist/cli/commands/docs.d.ts +2 -0
- package/dist/cli/commands/docs.js +89 -15
- package/dist/cli/commands/host.d.ts +7 -1
- package/dist/cli/commands/host.js +122 -7
- package/dist/cli/commands/init.d.ts +20 -3
- package/dist/cli/commands/init.js +897 -232
- package/dist/cli/commands/mcp.d.ts +1 -0
- package/dist/cli/commands/mcp.js +11 -5
- package/dist/cli/commands/repair.d.ts +12 -0
- package/dist/cli/commands/repair.js +41 -0
- package/dist/cli/commands/runtime.d.ts +1 -1
- package/dist/cli/commands/runtime.js +139 -38
- package/dist/cli/commands/session.d.ts +7 -0
- package/dist/cli/commands/session.js +82 -0
- package/dist/cli/commands/task.js +19 -5
- package/dist/cli/run.js +866 -24
- package/dist/code/identity.d.ts +7 -0
- package/dist/code/identity.js +7 -3
- package/dist/code/registration.js +7 -0
- package/dist/code/service.d.ts +9 -0
- package/dist/code/service.js +16 -3
- package/dist/code/text.js +19 -7
- package/dist/collab/admission.d.ts +91 -66
- package/dist/collab/admission.js +21 -6
- package/dist/collab/canonical.d.ts +1 -1
- package/dist/collab/canonical.js +44 -3
- package/dist/collab/envelope.d.ts +2 -2
- package/dist/collab/envelope.js +12 -12
- package/dist/collab/first-work.d.ts +3 -0
- package/dist/collab/first-work.js +15 -0
- package/dist/collab/handoff.d.ts +16 -0
- package/dist/collab/handoff.js +39 -5
- package/dist/collab/index.d.ts +97 -1
- package/dist/collab/index.js +9 -1
- package/dist/collab/messages.js +126 -3
- package/dist/collab/persisted-messages.d.ts +123 -0
- package/dist/collab/persisted-messages.js +496 -0
- package/dist/collab/preflight.d.ts +1 -1
- package/dist/collab/resources.d.ts +13 -3
- package/dist/collab/resources.js +37 -6
- package/dist/collab/rollover.d.ts +311 -0
- package/dist/collab/rollover.js +1024 -0
- package/dist/collab/store.d.ts +1 -0
- package/dist/collab/store.js +1 -4
- package/dist/collab/types.d.ts +2 -354
- package/dist/collab/validation.d.ts +2 -10
- package/dist/collab/validation.js +2 -303
- package/dist/collab/work-plan.d.ts +105 -0
- package/dist/collab/work-plan.js +843 -0
- package/dist/continuity/context.d.ts +11 -2
- package/dist/continuity/context.js +100 -13
- package/dist/continuity/handoff-bundle.d.ts +296 -0
- package/dist/continuity/handoff-bundle.js +477 -0
- package/dist/continuity/index.d.ts +2 -0
- package/dist/continuity/index.js +1 -0
- package/dist/continuity/paths.js +10 -2
- package/dist/continuity/service.js +91 -15
- package/dist/continuity/task-pin.d.ts +24 -0
- package/dist/continuity/task-pin.js +590 -0
- package/dist/doctor.d.ts +1 -0
- package/dist/doctor.js +6 -0
- package/dist/experimental/index.d.ts +13 -0
- package/dist/experimental/index.js +25 -0
- package/dist/experimental/pi.d.ts +4 -0
- package/dist/experimental/pi.js +12 -0
- package/dist/hosts/claude/native.js +1 -1
- package/dist/hosts/codex/native.js +1 -1
- package/dist/hosts/codex/protocol.js +1 -1
- package/dist/hosts/integration/bootstrap-config.d.ts +58 -0
- package/dist/hosts/integration/bootstrap-config.js +254 -0
- package/dist/hosts/integration/bundle-export.d.ts +44 -0
- package/dist/hosts/integration/bundle-export.js +366 -3
- package/dist/hosts/integration/bundle.js +3 -0
- package/dist/hosts/integration/hook.js +36 -9
- package/dist/hosts/integration/pappra-bootstrap-extension.mjs +80 -0
- package/dist/hosts/integration/pappra-extension-runtime.mjs +59 -24631
- package/dist/hosts/integration/pappra-hook.mjs +61 -24749
- package/dist/hosts/integration/pappra-recovery-extension.mjs +259 -0
- package/dist/hosts/integration/pappra-session-transition-extension.mjs +276 -0
- package/dist/hosts/integration/pi-bootstrap-extension.d.ts +36 -0
- package/dist/hosts/integration/pi-bootstrap-extension.js +101 -0
- package/dist/hosts/integration/pi-bootstrap-guard.d.ts +125 -0
- package/dist/hosts/integration/pi-bootstrap-guard.js +438 -0
- package/dist/hosts/integration/pi-bootstrap-runtime.d.ts +63 -0
- package/dist/hosts/integration/pi-bootstrap-runtime.js +223 -0
- package/dist/hosts/integration/pi-native-events.d.ts +112 -0
- package/dist/hosts/integration/pi-native-events.js +348 -0
- package/dist/hosts/integration/pi-rollover-extension.d.ts +166 -0
- package/dist/hosts/integration/pi-rollover-extension.js +431 -0
- package/dist/hosts/integration/recovery-config.d.ts +48 -0
- package/dist/hosts/integration/recovery-config.js +117 -0
- package/dist/hosts/integration/service.d.ts +6 -1
- package/dist/hosts/integration/service.js +485 -35
- package/dist/hosts/integration/support.d.ts +7 -0
- package/dist/hosts/integration/support.js +9 -1
- package/dist/hosts/integration/templates.d.ts +3 -0
- package/dist/hosts/integration/templates.js +26 -3
- package/dist/hosts/pi/managed-host-identity.d.ts +2 -0
- package/dist/hosts/pi/managed-host-identity.js +40 -0
- package/dist/hosts/pi/managed-provider-diagnostics.d.ts +6 -0
- package/dist/hosts/pi/managed-provider-diagnostics.js +184 -0
- package/dist/hosts/pi/managed-sdk-session.d.ts +335 -0
- package/dist/hosts/pi/managed-sdk-session.js +1639 -0
- package/dist/hosts/pi/managed-session-extension.d.ts +20 -0
- package/dist/hosts/pi/managed-session-extension.js +84 -0
- package/dist/hosts/pi/managed-session.d.ts +177 -0
- package/dist/hosts/pi/managed-session.js +1557 -0
- package/dist/hosts/pi/protocol.js +1 -1
- package/dist/hosts/pi/query-result.js +1 -1
- package/dist/hosts/pi/resume-capability.d.ts +41 -0
- package/dist/hosts/pi/resume-capability.js +33 -0
- package/dist/hosts/pi/rpc.d.ts +182 -134
- package/dist/hosts/pi/rpc.js +230 -37
- package/dist/hosts/pilot.d.ts +97 -74
- package/dist/hosts/pilot.js +5 -1
- package/dist/hosts/version.d.ts +3 -0
- package/dist/hosts/version.js +18 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/infra/bounded-file.d.ts +19 -0
- package/dist/infra/bounded-file.js +66 -0
- package/dist/infra/fs.js +10 -2
- package/dist/infra/git.d.ts +29 -0
- package/dist/infra/git.js +45 -9
- package/dist/infra/json-property.d.ts +2 -0
- package/dist/infra/json-property.js +69 -0
- package/dist/infra/json.d.ts +4 -0
- package/dist/infra/json.js +24 -0
- package/dist/infra/owned-process-group.js +1 -1
- package/dist/infra/private-lock.d.ts +16 -0
- package/dist/infra/private-lock.js +148 -0
- package/dist/infra/private-state.d.ts +7 -0
- package/dist/infra/private-state.js +146 -0
- package/dist/infra/process-identity.d.ts +12 -0
- package/dist/infra/process-identity.js +142 -0
- package/dist/infra/validation.d.ts +5 -0
- package/dist/infra/validation.js +15 -0
- package/dist/init.d.ts +1 -0
- package/dist/init.js +6 -0
- package/dist/install/apply.d.ts +9 -1
- package/dist/install/apply.js +176 -12
- package/dist/install/dependencies.d.ts +38 -0
- package/dist/install/dependencies.js +282 -0
- package/dist/install/ownership.js +30 -1
- package/dist/install/references.d.ts +31 -0
- package/dist/install/references.js +208 -0
- package/dist/install/repair.d.ts +37 -0
- package/dist/install/repair.js +798 -0
- package/dist/install/reset-local.d.ts +16 -0
- package/dist/install/reset-local.js +304 -0
- package/dist/install/rollback.d.ts +3 -0
- package/dist/install/rollback.js +9 -3
- package/dist/install/runtime.d.ts +10 -6
- package/dist/install/runtime.js +299 -59
- package/dist/install/source-fingerprint.d.ts +2 -0
- package/dist/install/source-fingerprint.js +103 -0
- package/dist/knowledge/catalog.d.ts +1 -0
- package/dist/knowledge/catalog.js +143 -10
- package/dist/knowledge/journal.js +17 -7
- package/dist/knowledge/paths.js +2 -1
- package/dist/knowledge/refs.js +4 -0
- package/dist/knowledge/register-batch.d.ts +4 -0
- package/dist/knowledge/register-batch.js +225 -0
- package/dist/knowledge/relocate.d.ts +5 -0
- package/dist/knowledge/relocate.js +92 -0
- package/dist/knowledge/scan.js +103 -47
- package/dist/knowledge/service.d.ts +3 -0
- package/dist/knowledge/service.js +33 -1
- package/dist/knowledge/write.d.ts +4 -1
- package/dist/knowledge/write.js +1 -1
- package/dist/mcp/server.d.ts +4 -2
- package/dist/mcp/server.js +87 -136
- package/dist/schemas/collaboration-state-validation.d.ts +6 -0
- package/dist/schemas/collaboration-state-validation.js +342 -0
- package/dist/schemas/collaboration-state.d.ts +436 -0
- package/dist/schemas/collaboration-state.js +1 -0
- package/dist/schemas/collaboration.d.ts +335 -0
- package/dist/schemas/collaboration.js +272 -0
- package/dist/schemas/continuity.d.ts +8 -0
- package/dist/schemas/continuity.js +13 -3
- package/dist/schemas/doctor.d.ts +1 -0
- package/dist/schemas/doctor.js +1 -0
- package/dist/schemas/document.d.ts +61 -0
- package/dist/schemas/document.js +9 -0
- package/dist/schemas/errors.d.ts +1 -0
- package/dist/schemas/errors.js +1 -0
- package/dist/schemas/host.d.ts +24 -0
- package/dist/schemas/host.js +19 -0
- package/dist/schemas/install.d.ts +8 -2
- package/dist/schemas/install.js +11 -1
- package/dist/schemas/managed-pi-execution.d.ts +16 -0
- package/dist/schemas/managed-pi-execution.js +2 -0
- package/dist/schemas/outcome.d.ts +9 -5
- package/dist/workspace/config-file.js +1 -3
- package/dist/workspace/documents.js +12 -2
- package/dist/workspace/repositories.d.ts +7 -0
- package/dist/workspace/repositories.js +14 -5
- package/package.json +35 -35
- package/skills/pappra/SKILL.md +28 -7
- package/skills/pappra/references/collaboration.md +633 -0
- package/skills/pappra/references/environment.md +360 -0
- package/skills/pappra/references/hosts.md +20 -0
- package/skills/pappra/references/operations.md +36 -5
- package/dist/app/collab-from.d.ts +0 -35
- package/dist/app/collab-from.js +0 -84
|
@@ -0,0 +1,618 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { canonicalJson, readRegisteredMessage } from '../collab/envelope.js';
|
|
5
|
+
import { findAttempt } from '../collab/lifecycle.js';
|
|
6
|
+
import { isAssignment } from '../collab/contracts.js';
|
|
7
|
+
import { describeResource } from '../collab/canonical.js';
|
|
8
|
+
import { transact } from '../collab/store.js';
|
|
9
|
+
import { createLocalTaskRecordReader } from '../hosts/integration/pi-bootstrap-runtime.js';
|
|
10
|
+
import { assertManagedPiSdkResumeAuthorization, issueManagedPiResumeCapability, isManagedPiResumeCapabilityReserved, markManagedPiResumeCapabilityRegistered, revokeManagedPiResumeCapability, } from '../hosts/pi/resume-capability.js';
|
|
11
|
+
import { managedPiHostIdentity } from '../hosts/pi/managed-host-identity.js';
|
|
12
|
+
import { captureProcessIdentity, isLocalProcessIdentity, observeProcessIdentity, } from '../infra/process-identity.js';
|
|
13
|
+
import { readBoundedFile } from './bounded-file.js';
|
|
14
|
+
export { assertManagedPiSdkResumeAuthorization };
|
|
15
|
+
const AUTHORIZATIONS = new WeakMap();
|
|
16
|
+
const RESERVATIONS = 'managed_pi_resume_admissions';
|
|
17
|
+
const REGISTRY = 'managed_pi_runtime_registry';
|
|
18
|
+
const MAX_JOURNAL_BYTES = 32 * 1024 * 1024;
|
|
19
|
+
const MAX_TRANSCRIPT_BYTES = 64 * 1024 * 1024;
|
|
20
|
+
const object = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
21
|
+
const digest = (value) => crypto.createHash('sha256').update(value).digest('hex');
|
|
22
|
+
const fail = (code) => {
|
|
23
|
+
throw new Error(`managed_pi_resume_${code}`);
|
|
24
|
+
};
|
|
25
|
+
function parse(text) {
|
|
26
|
+
let value;
|
|
27
|
+
try {
|
|
28
|
+
value = JSON.parse(text);
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return fail('invalid_json');
|
|
32
|
+
}
|
|
33
|
+
return object(value) ? value : fail('invalid_record');
|
|
34
|
+
}
|
|
35
|
+
function pin(file, maxBytes, ownerOnly = true) {
|
|
36
|
+
const parent = path.dirname(file);
|
|
37
|
+
const directory = fs.lstatSync(parent);
|
|
38
|
+
const stat = fs.lstatSync(file);
|
|
39
|
+
if (!path.isAbsolute(file) ||
|
|
40
|
+
fs.realpathSync(file) !== file ||
|
|
41
|
+
fs.realpathSync(parent) !== parent ||
|
|
42
|
+
!directory.isDirectory() ||
|
|
43
|
+
(directory.mode & 0o022) !== 0 ||
|
|
44
|
+
!stat.isFile() ||
|
|
45
|
+
(stat.mode & (ownerOnly ? 0o077 : 0o022)) !== 0 ||
|
|
46
|
+
stat.uid !== process.getuid?.())
|
|
47
|
+
return fail('private_file_unsafe');
|
|
48
|
+
const loaded = readBoundedFile(file, maxBytes);
|
|
49
|
+
const after = fs.lstatSync(file);
|
|
50
|
+
const parentAfter = fs.lstatSync(parent);
|
|
51
|
+
if (!loaded.ok ||
|
|
52
|
+
after.dev !== stat.dev ||
|
|
53
|
+
after.ino !== stat.ino ||
|
|
54
|
+
after.size !== stat.size ||
|
|
55
|
+
after.mtimeMs !== stat.mtimeMs ||
|
|
56
|
+
after.ctimeMs !== stat.ctimeMs ||
|
|
57
|
+
parentAfter.dev !== directory.dev ||
|
|
58
|
+
parentAfter.ino !== directory.ino)
|
|
59
|
+
return fail('private_file_changed');
|
|
60
|
+
return {
|
|
61
|
+
pin: {
|
|
62
|
+
file,
|
|
63
|
+
dev: stat.dev,
|
|
64
|
+
ino: stat.ino,
|
|
65
|
+
digest: loaded.digest,
|
|
66
|
+
parentDev: directory.dev,
|
|
67
|
+
parentIno: directory.ino,
|
|
68
|
+
ownerOnly,
|
|
69
|
+
},
|
|
70
|
+
text: loaded.text,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function assertPinned(expected, maxBytes) {
|
|
74
|
+
const current = pin(expected.file, maxBytes, expected.ownerOnly).pin;
|
|
75
|
+
if (canonicalJson(current) !== canonicalJson(expected))
|
|
76
|
+
fail('source_file_changed');
|
|
77
|
+
}
|
|
78
|
+
function assertDead(pid, identity) {
|
|
79
|
+
if (!Number.isSafeInteger(pid) || pid <= 0)
|
|
80
|
+
fail('source_pid_invalid');
|
|
81
|
+
if (identity) {
|
|
82
|
+
if (identity.pid !== pid || observeProcessIdentity(identity) !== 'dead')
|
|
83
|
+
fail('source_still_alive_or_unverified');
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (pid === process.pid)
|
|
87
|
+
fail('source_pid_invalid');
|
|
88
|
+
try {
|
|
89
|
+
process.kill(pid, 0);
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
if (object(error) && error.code === 'ESRCH')
|
|
93
|
+
return;
|
|
94
|
+
return fail('source_death_unknown');
|
|
95
|
+
}
|
|
96
|
+
fail('source_still_alive');
|
|
97
|
+
}
|
|
98
|
+
function assertIdle(snapshot) {
|
|
99
|
+
if (!object(snapshot))
|
|
100
|
+
fail('checkpoint_inventory_missing');
|
|
101
|
+
const s = snapshot;
|
|
102
|
+
try {
|
|
103
|
+
const { queues, work, extensionDialogs, builtInUiApprovals } = s.ownedRuntime;
|
|
104
|
+
const internal = queues.internal;
|
|
105
|
+
const counts = [
|
|
106
|
+
queues.pendingMessageCount,
|
|
107
|
+
queues.steering.length,
|
|
108
|
+
queues.followUp.length,
|
|
109
|
+
internal.pendingNextTurn?.length,
|
|
110
|
+
internal.pendingCustom?.length,
|
|
111
|
+
internal.pendingBash?.length,
|
|
112
|
+
internal.deferredSettledActionCount,
|
|
113
|
+
internal.pendingUserInputs?.length,
|
|
114
|
+
internal.compactionMessages?.length,
|
|
115
|
+
internal.extensionDialogCount,
|
|
116
|
+
extensionDialogs.pending.length,
|
|
117
|
+
work.pendingToolCalls,
|
|
118
|
+
];
|
|
119
|
+
if (!s.ingress.healthy ||
|
|
120
|
+
s.ingress.state !== 'open' ||
|
|
121
|
+
queues.completeness !== 'complete' ||
|
|
122
|
+
work.observation !== 'complete' ||
|
|
123
|
+
extensionDialogs.completeness !== 'complete' ||
|
|
124
|
+
builtInUiApprovals.observation !== 'complete' ||
|
|
125
|
+
work.isIdle !== true ||
|
|
126
|
+
work.isStreaming !== false ||
|
|
127
|
+
work.isCompacting !== false ||
|
|
128
|
+
work.isBashRunning !== false ||
|
|
129
|
+
work.hasPendingBashMessages !== false ||
|
|
130
|
+
internal.activeSelector !== false ||
|
|
131
|
+
counts.some((count) => count !== 0))
|
|
132
|
+
fail('checkpoint_not_idle');
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
fail('checkpoint_not_idle');
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function sourceRuntime(record, sessionId) {
|
|
139
|
+
const rows = record[REGISTRY];
|
|
140
|
+
if (!Array.isArray(rows))
|
|
141
|
+
return fail('source_not_registered');
|
|
142
|
+
const matches = rows.filter((row) => object(row) && row.session_id === sessionId);
|
|
143
|
+
if (matches.length !== 1)
|
|
144
|
+
return fail('source_not_registered');
|
|
145
|
+
return matches[0];
|
|
146
|
+
}
|
|
147
|
+
/** Reused before opening, inside registry replacement, and at every resumed execution gate. */
|
|
148
|
+
function authority(record, scope) {
|
|
149
|
+
const attempt = findAttempt(record, scope.subtask_id, scope.attempt);
|
|
150
|
+
const latest = record.attempts
|
|
151
|
+
.filter((row) => row.subtask_id === scope.subtask_id)
|
|
152
|
+
.reduce((maximum, row) => Math.max(maximum, row.attempt), 0);
|
|
153
|
+
if (scope.schema !== 'pappra/managed-pi-execution-scope/1' ||
|
|
154
|
+
record.task_id !== scope.task_id ||
|
|
155
|
+
record.task_root !== scope.task_root ||
|
|
156
|
+
path.basename(scope.task_root) !== scope.task_id ||
|
|
157
|
+
record.coordinator?.owner_epoch !== scope.owner_epoch ||
|
|
158
|
+
record.dispatch_frozen === true ||
|
|
159
|
+
!attempt ||
|
|
160
|
+
latest !== scope.attempt ||
|
|
161
|
+
!['assigned', 'accepted', 'running'].includes(attempt.state) ||
|
|
162
|
+
attempt.awaiting !== undefined ||
|
|
163
|
+
attempt.cancel_requested_at !== undefined ||
|
|
164
|
+
attempt.closed_at !== undefined ||
|
|
165
|
+
attempt.owner_epoch !== scope.owner_epoch ||
|
|
166
|
+
attempt.assignee.session_id !== scope.session_id ||
|
|
167
|
+
attempt.assignee.harness !== 'pi' ||
|
|
168
|
+
attempt.assignee.cwd !== scope.cwd ||
|
|
169
|
+
!attempt.assignment_ref ||
|
|
170
|
+
!attempt.assignment_digest ||
|
|
171
|
+
!attempt.input_revision ||
|
|
172
|
+
record.uncertainty.some((row) => !row.resolved_at &&
|
|
173
|
+
row.subtask_id === scope.subtask_id &&
|
|
174
|
+
row.attempt === scope.attempt) ||
|
|
175
|
+
(record.rollover ?? []).some((row) => row.subtask_id === scope.subtask_id &&
|
|
176
|
+
row.attempt === scope.attempt &&
|
|
177
|
+
row.from.session_id === scope.session_id))
|
|
178
|
+
return fail('task_authority_stale');
|
|
179
|
+
const artifact = readBoundedFile(attempt.assignment_ref, 1024 * 1024);
|
|
180
|
+
if (!artifact.ok || artifact.digest !== attempt.assignment_digest)
|
|
181
|
+
return fail('assignment_changed');
|
|
182
|
+
const assignment = parse(artifact.text);
|
|
183
|
+
if (!isAssignment(assignment) ||
|
|
184
|
+
assignment.task_id !== scope.task_id ||
|
|
185
|
+
assignment.attempt !== scope.attempt ||
|
|
186
|
+
assignment.assignee.agent_id !== attempt.assignee.agent_id ||
|
|
187
|
+
assignment.assignee.session_id !== scope.session_id ||
|
|
188
|
+
assignment.assignee.cwd !== scope.cwd ||
|
|
189
|
+
assignment.coordinator.agent_id !== record.coordinator?.agent_id ||
|
|
190
|
+
assignment.coordinator.session_id !== record.coordinator?.session_id ||
|
|
191
|
+
assignment.coordinator.owner_epoch !== scope.owner_epoch)
|
|
192
|
+
return fail('assignment_identity_changed');
|
|
193
|
+
const admitted = record.messages.find((row) => row.message_id === attempt.assignment_message_id &&
|
|
194
|
+
row.state === 'applied');
|
|
195
|
+
const registered = readRegisteredMessage(record.task_root, attempt.assignment_message_id);
|
|
196
|
+
if (!admitted ||
|
|
197
|
+
!registered ||
|
|
198
|
+
registered.type !== 'ASSIGN' ||
|
|
199
|
+
admitted.content_digest !== registered.content_digest ||
|
|
200
|
+
registered.task_id !== scope.task_id ||
|
|
201
|
+
registered.subtask_id !== scope.subtask_id ||
|
|
202
|
+
registered.attempt !== scope.attempt ||
|
|
203
|
+
registered.owner_epoch !== scope.owner_epoch ||
|
|
204
|
+
registered.from.agent_id !== record.coordinator?.agent_id ||
|
|
205
|
+
registered.from.session_id !== record.coordinator?.session_id ||
|
|
206
|
+
registered.to.agent_id !== attempt.assignee.agent_id ||
|
|
207
|
+
registered.to.session_id !== scope.session_id ||
|
|
208
|
+
registered.to.harness !== 'pi' ||
|
|
209
|
+
registered.body.assignment_ref !== attempt.assignment_ref ||
|
|
210
|
+
registered.body.assignment_digest !== attempt.assignment_digest ||
|
|
211
|
+
registered.body.input_revision !== attempt.input_revision ||
|
|
212
|
+
canonicalJson(registered.body.approval ?? null) !==
|
|
213
|
+
canonicalJson(attempt.approval))
|
|
214
|
+
return fail('assignment_not_admitted');
|
|
215
|
+
const claims = record.claims.filter((claim) => claim.subtask_id === scope.subtask_id &&
|
|
216
|
+
claim.attempt === scope.attempt &&
|
|
217
|
+
!claim.released_at);
|
|
218
|
+
if (!scope.claim_epochs.length ||
|
|
219
|
+
claims.length !== scope.claim_epochs.length ||
|
|
220
|
+
new Set(scope.claim_epochs.map((row) => row.claim_id)).size !==
|
|
221
|
+
claims.length ||
|
|
222
|
+
claims.some((claim) => claim.cancel_pending ||
|
|
223
|
+
claim.holder.agent_id !== attempt.assignee.agent_id ||
|
|
224
|
+
claim.holder.session_id !== scope.session_id ||
|
|
225
|
+
claim.holder.harness !== 'pi' ||
|
|
226
|
+
!scope.claim_epochs.some((pin) => pin.claim_id === claim.claim_id &&
|
|
227
|
+
pin.resource_epoch === claim.resource_epoch)))
|
|
228
|
+
return fail('claim_authority_stale');
|
|
229
|
+
const used = new Set();
|
|
230
|
+
for (const resource of assignment.resources) {
|
|
231
|
+
const descriptor = describeResource({
|
|
232
|
+
path: resource.path,
|
|
233
|
+
...(resource.kind === undefined ? {} : { kind: resource.kind }),
|
|
234
|
+
...(resource.scope === undefined ? {} : { scope: resource.scope }),
|
|
235
|
+
});
|
|
236
|
+
const matched = claims.find((claim) => !used.has(claim.claim_id) &&
|
|
237
|
+
claim.path === descriptor.path &&
|
|
238
|
+
claim.access === resource.access &&
|
|
239
|
+
(resource.resource_epoch === undefined ||
|
|
240
|
+
resource.resource_epoch === claim.resource_epoch));
|
|
241
|
+
if (!matched)
|
|
242
|
+
return fail('assignment_claim_mismatch');
|
|
243
|
+
used.add(matched.claim_id);
|
|
244
|
+
}
|
|
245
|
+
if (used.size !== claims.length)
|
|
246
|
+
return fail('assignment_claim_mismatch');
|
|
247
|
+
return digest(canonicalJson({
|
|
248
|
+
coordinator: record.coordinator,
|
|
249
|
+
assignee: attempt.assignee,
|
|
250
|
+
input: attempt.input_revision,
|
|
251
|
+
assignment: artifact.digest,
|
|
252
|
+
approval: attempt.approval,
|
|
253
|
+
claims: scope.claim_epochs,
|
|
254
|
+
}));
|
|
255
|
+
}
|
|
256
|
+
function reservations(record) {
|
|
257
|
+
const value = record[RESERVATIONS];
|
|
258
|
+
if (value === undefined)
|
|
259
|
+
return [];
|
|
260
|
+
if (!Array.isArray(value) || value.length > 64 || !value.every(object))
|
|
261
|
+
return fail('reservation_registry_invalid');
|
|
262
|
+
return value;
|
|
263
|
+
}
|
|
264
|
+
/** Reclaim only owners proven dead; a reused or inaccessible PID stays reserved. */
|
|
265
|
+
function reservationOwnerDead(row) {
|
|
266
|
+
if (isLocalProcessIdentity(row.process_identity))
|
|
267
|
+
return (row.process_identity.pid === row.pid &&
|
|
268
|
+
observeProcessIdentity(row.process_identity) === 'dead');
|
|
269
|
+
if (!Number.isSafeInteger(row.pid) || Number(row.pid) <= 0)
|
|
270
|
+
return false;
|
|
271
|
+
if (typeof row.host_identity === 'string' &&
|
|
272
|
+
row.host_identity !== managedPiHostIdentity())
|
|
273
|
+
return false;
|
|
274
|
+
try {
|
|
275
|
+
process.kill(Number(row.pid), 0);
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
278
|
+
catch (error) {
|
|
279
|
+
return object(error) && error.code === 'ESRCH';
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
function validReservation(record, state, expectedStatus) {
|
|
283
|
+
const selected = reservations(record).filter((row) => row.reservation_id === state.reservationId);
|
|
284
|
+
return (selected.length === 1 &&
|
|
285
|
+
selected[0].pid === process.pid &&
|
|
286
|
+
selected[0].journal_path === state.newJournal &&
|
|
287
|
+
selected[0].status === expectedStatus &&
|
|
288
|
+
selected[0].source_run_id === state.source.controller_run_id);
|
|
289
|
+
}
|
|
290
|
+
function verifyJournal(text) {
|
|
291
|
+
if (!text.endsWith('\n'))
|
|
292
|
+
return fail('source_journal_partial');
|
|
293
|
+
let previous = '0'.repeat(64);
|
|
294
|
+
let journalRunId;
|
|
295
|
+
return text
|
|
296
|
+
.trimEnd()
|
|
297
|
+
.split('\n')
|
|
298
|
+
.map((line, index) => {
|
|
299
|
+
const row = parse(line);
|
|
300
|
+
if (index === 0) {
|
|
301
|
+
journalRunId = row.run_id;
|
|
302
|
+
if (typeof journalRunId !== 'string' ||
|
|
303
|
+
!/^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/.test(journalRunId))
|
|
304
|
+
return fail('source_journal_invalid');
|
|
305
|
+
}
|
|
306
|
+
const { digest: recorded, ...body } = row;
|
|
307
|
+
if (row.schema !== 'harness-collab/pi-managed-session/1' ||
|
|
308
|
+
row.run_id !== journalRunId ||
|
|
309
|
+
row.sequence !== index + 1 ||
|
|
310
|
+
row.previous_digest !== previous ||
|
|
311
|
+
recorded !== digest(JSON.stringify(body)))
|
|
312
|
+
return fail('source_journal_invalid');
|
|
313
|
+
previous = String(recorded);
|
|
314
|
+
return row;
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
export async function prepareManagedPiResume(request) {
|
|
318
|
+
const config = request.resume;
|
|
319
|
+
const scope = request.executionScope;
|
|
320
|
+
if (!config ||
|
|
321
|
+
!scope ||
|
|
322
|
+
!request.sessionId ||
|
|
323
|
+
scope.session_id !== request.sessionId ||
|
|
324
|
+
scope.cwd !== request.cwd ||
|
|
325
|
+
request.journalPath === config.sourceJournalPath ||
|
|
326
|
+
fs.existsSync(request.journalPath) ||
|
|
327
|
+
request.initialAssignment ||
|
|
328
|
+
request.automaticHandoffFile ||
|
|
329
|
+
request.bootstrapExtensionPath ||
|
|
330
|
+
request.handoffContextFile)
|
|
331
|
+
return fail('request_invalid');
|
|
332
|
+
const reader = createLocalTaskRecordReader(path.dirname(path.dirname(scope.task_root)), scope.task_id);
|
|
333
|
+
const record = reader.read();
|
|
334
|
+
const authorityDigest = authority(record, scope);
|
|
335
|
+
const source = sourceRuntime(record, scope.session_id);
|
|
336
|
+
if (source.pane_id &&
|
|
337
|
+
source.pane_id !==
|
|
338
|
+
(request.herdrPaneId ?? process.env.HERDR_PANE_ID ?? null))
|
|
339
|
+
return fail('source_pane_changed');
|
|
340
|
+
if (source.schema !== 'pappra/managed-pi-runtime/1' ||
|
|
341
|
+
source.controller_run_id !== config.sourceRunId ||
|
|
342
|
+
source.descriptor_file !== `${config.sourceJournalPath}.control.json` ||
|
|
343
|
+
canonicalJson(source.scope) !== canonicalJson(scope) ||
|
|
344
|
+
source.input_revision !==
|
|
345
|
+
findAttempt(record, scope.subtask_id, scope.attempt)?.input_revision)
|
|
346
|
+
return fail('source_registration_mismatch');
|
|
347
|
+
const journal = pin(config.sourceJournalPath, MAX_JOURNAL_BYTES);
|
|
348
|
+
const descriptor = pin(source.descriptor_file, 4096);
|
|
349
|
+
const control = parse(descriptor.text);
|
|
350
|
+
if (control.schema !== 'harness-collab/pi-managed-control/1' ||
|
|
351
|
+
control.runId !== source.controller_run_id ||
|
|
352
|
+
control.sessionId !== scope.session_id)
|
|
353
|
+
return fail('source_descriptor_mismatch');
|
|
354
|
+
// The private control endpoint and journal each generate their own run ID.
|
|
355
|
+
// Descriptor/registry pins bind the endpoint; the chain independently pins its run.
|
|
356
|
+
const rows = verifyJournal(journal.text);
|
|
357
|
+
const checkpointIndex = rows.findLastIndex((row) => row.kind === 'sdk_resume_checkpoint');
|
|
358
|
+
const checkpoint = rows[checkpointIndex];
|
|
359
|
+
if (!checkpoint ||
|
|
360
|
+
checkpoint.checkpoint_schema !== 'pappra/managed-pi-resume-checkpoint/1' ||
|
|
361
|
+
checkpoint.session_id !== scope.session_id ||
|
|
362
|
+
checkpoint.session_file !== config.sourceSessionFile ||
|
|
363
|
+
checkpoint.cwd !== scope.cwd ||
|
|
364
|
+
checkpoint.model !== request.model ||
|
|
365
|
+
checkpoint.process_pid !== source.pid ||
|
|
366
|
+
canonicalJson(checkpoint.execution_scope ?? null) !== canonicalJson(scope))
|
|
367
|
+
return fail('checkpoint_missing_or_mismatched');
|
|
368
|
+
assertIdle(checkpoint.snapshot);
|
|
369
|
+
const hostIdentity = managedPiHostIdentity();
|
|
370
|
+
const sourceProcessIdentity = isLocalProcessIdentity(checkpoint.process_identity)
|
|
371
|
+
? checkpoint.process_identity
|
|
372
|
+
: undefined;
|
|
373
|
+
if (!hostIdentity ||
|
|
374
|
+
(!sourceProcessIdentity && checkpoint.host_identity !== hostIdentity))
|
|
375
|
+
return fail('source_host_mismatch');
|
|
376
|
+
assertDead(source.pid, sourceProcessIdentity);
|
|
377
|
+
if (checkpoint.snapshot.session.sessionId !== scope.session_id ||
|
|
378
|
+
checkpoint.snapshot.session.sessionFile !== config.sourceSessionFile ||
|
|
379
|
+
checkpoint.snapshot.process.pid !== source.pid ||
|
|
380
|
+
checkpoint.snapshot.ingress.journalPath !== config.sourceJournalPath)
|
|
381
|
+
return fail('checkpoint_native_identity_mismatch');
|
|
382
|
+
for (const row of rows.slice(checkpointIndex + 1)) {
|
|
383
|
+
if (row.kind === 'sdk_queue_observed' &&
|
|
384
|
+
Array.isArray(row.steering) &&
|
|
385
|
+
row.steering.length === 0 &&
|
|
386
|
+
Array.isArray(row.follow_up) &&
|
|
387
|
+
row.follow_up.length === 0)
|
|
388
|
+
continue;
|
|
389
|
+
if (!['sdk_session_ready', 'sdk_resources_ready'].includes(String(row.kind)))
|
|
390
|
+
return fail('checkpoint_invalidated');
|
|
391
|
+
}
|
|
392
|
+
// Native Pi history follows the user's umask; it must not be writable by others.
|
|
393
|
+
// Authenticated journals and control descriptors retain owner-only access.
|
|
394
|
+
const transcript = pin(config.sourceSessionFile, MAX_TRANSCRIPT_BYTES, false);
|
|
395
|
+
const header = parse(transcript.text.split('\n')[0] ?? '');
|
|
396
|
+
if (header.type !== 'session' ||
|
|
397
|
+
header.id !== scope.session_id ||
|
|
398
|
+
header.cwd !== scope.cwd ||
|
|
399
|
+
checkpoint.transcript_digest !== transcript.pin.digest)
|
|
400
|
+
return fail('transcript_changed');
|
|
401
|
+
const messages = transcript.text
|
|
402
|
+
.trimEnd()
|
|
403
|
+
.split('\n')
|
|
404
|
+
.slice(1)
|
|
405
|
+
.map(parse)
|
|
406
|
+
.filter((entry) => entry.type === 'message' && object(entry.message));
|
|
407
|
+
const lastAssistant = messages.findLast((entry) => object(entry.message) && entry.message.role === 'assistant')?.message;
|
|
408
|
+
if (object(lastAssistant) && lastAssistant.stopReason !== 'stop')
|
|
409
|
+
return fail('native_outcome_unresolved');
|
|
410
|
+
const inputMap = new Map();
|
|
411
|
+
const inherited = checkpoint.registered_inputs ?? [];
|
|
412
|
+
if (!Array.isArray(inherited))
|
|
413
|
+
return fail('input_dedup_invalid');
|
|
414
|
+
const inputs = [
|
|
415
|
+
...inherited,
|
|
416
|
+
...rows.filter((row) => row.kind === 'sdk_control_input_reserved'),
|
|
417
|
+
];
|
|
418
|
+
for (const input of inputs) {
|
|
419
|
+
if (!object(input) ||
|
|
420
|
+
typeof input.request_id !== 'string' ||
|
|
421
|
+
!/^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/.test(input.request_id) ||
|
|
422
|
+
typeof input.input_digest !== 'string' ||
|
|
423
|
+
!/^[a-f0-9]{64}$/.test(input.input_digest) ||
|
|
424
|
+
(inputMap.has(input.request_id) &&
|
|
425
|
+
inputMap.get(input.request_id) !== input.input_digest))
|
|
426
|
+
return fail('input_dedup_invalid');
|
|
427
|
+
inputMap.set(input.request_id, input.input_digest);
|
|
428
|
+
}
|
|
429
|
+
if (inputMap.size > 256)
|
|
430
|
+
return fail('input_dedup_limit');
|
|
431
|
+
const frozenScope = JSON.parse(JSON.stringify(scope));
|
|
432
|
+
for (const claim of frozenScope.claim_epochs)
|
|
433
|
+
Object.freeze(claim);
|
|
434
|
+
Object.freeze(frozenScope.claim_epochs);
|
|
435
|
+
Object.freeze(frozenScope);
|
|
436
|
+
const authorization = issueManagedPiResumeCapability({
|
|
437
|
+
sessionId: scope.session_id,
|
|
438
|
+
cwd: scope.cwd,
|
|
439
|
+
sessionFile: config.sourceSessionFile,
|
|
440
|
+
transcriptDigest: transcript.pin.digest,
|
|
441
|
+
scope: frozenScope,
|
|
442
|
+
checkpointSequence: Number(checkpoint.sequence),
|
|
443
|
+
checkpointDigest: String(checkpoint.digest),
|
|
444
|
+
sourceRunId: source.controller_run_id,
|
|
445
|
+
sourcePid: source.pid,
|
|
446
|
+
...(sourceProcessIdentity ? { sourceProcessIdentity } : {}),
|
|
447
|
+
hostIdentity,
|
|
448
|
+
registeredInputs: Object.freeze([...inputMap].map(([requestId, inputDigest]) => Object.freeze({ requestId, inputDigest }))),
|
|
449
|
+
}, (expected) => assertCurrentResumeAdmission(authorization, expected));
|
|
450
|
+
const reservationId = digest(canonicalJson({
|
|
451
|
+
session: scope.session_id,
|
|
452
|
+
sourceRun: source.controller_run_id,
|
|
453
|
+
checkpoint: checkpoint.digest,
|
|
454
|
+
}));
|
|
455
|
+
const state = {
|
|
456
|
+
pid: process.pid,
|
|
457
|
+
reader,
|
|
458
|
+
journal: journal.pin,
|
|
459
|
+
descriptor: descriptor.pin,
|
|
460
|
+
transcript: transcript.pin,
|
|
461
|
+
source,
|
|
462
|
+
sourceJson: canonicalJson(source),
|
|
463
|
+
reservationId,
|
|
464
|
+
newJournal: request.journalPath,
|
|
465
|
+
authorityDigest,
|
|
466
|
+
};
|
|
467
|
+
const saved = transact(scope.task_root, {
|
|
468
|
+
actor: 'managed-pi-resume',
|
|
469
|
+
purpose: 'reserve-exact-session-resume',
|
|
470
|
+
apply(current) {
|
|
471
|
+
assertDead(source.pid, sourceProcessIdentity);
|
|
472
|
+
if (authority(current, scope) !== authorityDigest ||
|
|
473
|
+
canonicalJson(sourceRuntime(current, scope.session_id)) !==
|
|
474
|
+
state.sourceJson)
|
|
475
|
+
return { ok: false, reason: 'managed_pi_resume_authority_changed' };
|
|
476
|
+
const pending = reservations(current).filter((row) => !reservationOwnerDead(row));
|
|
477
|
+
if (pending.length >= 64 ||
|
|
478
|
+
pending.some((row) => row.source_run_id === source.controller_run_id))
|
|
479
|
+
return { ok: false, reason: 'managed_pi_resume_already_reserved' };
|
|
480
|
+
pending.push({
|
|
481
|
+
schema: 'pappra/managed-pi-resume-admission/1',
|
|
482
|
+
reservation_id: reservationId,
|
|
483
|
+
session_id: scope.session_id,
|
|
484
|
+
source_run_id: source.controller_run_id,
|
|
485
|
+
checkpoint_digest: checkpoint.digest,
|
|
486
|
+
pid: process.pid,
|
|
487
|
+
process_identity: captureProcessIdentity(),
|
|
488
|
+
host_identity: hostIdentity,
|
|
489
|
+
journal_path: request.journalPath,
|
|
490
|
+
status: 'reserved',
|
|
491
|
+
created_at: new Date().toISOString(),
|
|
492
|
+
});
|
|
493
|
+
current[RESERVATIONS] = pending;
|
|
494
|
+
return {
|
|
495
|
+
record: current,
|
|
496
|
+
op: 'reserve-managed-pi-resume',
|
|
497
|
+
summary: { session_id: scope.session_id },
|
|
498
|
+
result: null,
|
|
499
|
+
};
|
|
500
|
+
},
|
|
501
|
+
});
|
|
502
|
+
if (!saved.ok)
|
|
503
|
+
return fail('already_reserved_or_stale');
|
|
504
|
+
AUTHORIZATIONS.set(authorization, state);
|
|
505
|
+
try {
|
|
506
|
+
assertManagedPiSdkResumeAuthorization(authorization, {
|
|
507
|
+
sessionId: scope.session_id,
|
|
508
|
+
cwd: scope.cwd,
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
catch (error) {
|
|
512
|
+
releaseManagedPiResume(authorization);
|
|
513
|
+
throw error;
|
|
514
|
+
}
|
|
515
|
+
return authorization;
|
|
516
|
+
}
|
|
517
|
+
/** Release this process's exact reservation after startup failure or runtime shutdown. */
|
|
518
|
+
export function releaseManagedPiResume(value) {
|
|
519
|
+
const state = AUTHORIZATIONS.get(value);
|
|
520
|
+
if (!state || state.pid !== process.pid)
|
|
521
|
+
return true;
|
|
522
|
+
try {
|
|
523
|
+
const released = transact(value.scope.task_root, {
|
|
524
|
+
actor: 'managed-pi-resume',
|
|
525
|
+
purpose: 'release-exact-session-resume',
|
|
526
|
+
apply(record) {
|
|
527
|
+
record[RESERVATIONS] = reservations(record).filter((row) => row.reservation_id !== state.reservationId ||
|
|
528
|
+
row.pid !== state.pid ||
|
|
529
|
+
row.journal_path !== state.newJournal);
|
|
530
|
+
return {
|
|
531
|
+
record,
|
|
532
|
+
op: 'release-managed-pi-resume',
|
|
533
|
+
summary: { session_id: value.sessionId },
|
|
534
|
+
result: null,
|
|
535
|
+
};
|
|
536
|
+
},
|
|
537
|
+
});
|
|
538
|
+
if (!released.ok)
|
|
539
|
+
return false;
|
|
540
|
+
revokeManagedPiResumeCapability(value);
|
|
541
|
+
AUTHORIZATIONS.delete(value);
|
|
542
|
+
return true;
|
|
543
|
+
}
|
|
544
|
+
catch {
|
|
545
|
+
// Cleanup failure must not replace the original startup/runtime error.
|
|
546
|
+
// The exact dead process identity permits a later admission to reclaim it.
|
|
547
|
+
return false;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
function assertCurrentResumeAdmission(value, expected) {
|
|
551
|
+
const state = AUTHORIZATIONS.get(value);
|
|
552
|
+
if (!state ||
|
|
553
|
+
state.pid !== process.pid ||
|
|
554
|
+
!isManagedPiResumeCapabilityReserved(value) ||
|
|
555
|
+
value.sessionId !== expected.sessionId ||
|
|
556
|
+
value.cwd !== expected.cwd)
|
|
557
|
+
return fail('authorization_invalid');
|
|
558
|
+
if (managedPiHostIdentity() !== value.hostIdentity)
|
|
559
|
+
return fail('source_host_mismatch');
|
|
560
|
+
assertDead(value.sourcePid, value.sourceProcessIdentity);
|
|
561
|
+
assertPinned(state.journal, MAX_JOURNAL_BYTES);
|
|
562
|
+
assertPinned(state.descriptor, 4096);
|
|
563
|
+
assertPinned(state.transcript, MAX_TRANSCRIPT_BYTES);
|
|
564
|
+
const record = state.reader.read();
|
|
565
|
+
if (authority(record, value.scope) !== state.authorityDigest ||
|
|
566
|
+
!validReservation(record, state, 'reserved') ||
|
|
567
|
+
canonicalJson(sourceRuntime(record, value.sessionId)) !== state.sourceJson)
|
|
568
|
+
fail('authorization_stale');
|
|
569
|
+
}
|
|
570
|
+
/** Called only inside the task-store transaction that replaces the exact old registry entry. */
|
|
571
|
+
export function consumeManagedPiResumeRegistration(record, value, old, next) {
|
|
572
|
+
const state = AUTHORIZATIONS.get(value);
|
|
573
|
+
if (!state ||
|
|
574
|
+
!isManagedPiResumeCapabilityReserved(value) ||
|
|
575
|
+
state.pid !== process.pid ||
|
|
576
|
+
!validReservation(record, state, 'reserved') ||
|
|
577
|
+
authority(record, value.scope) !== state.authorityDigest ||
|
|
578
|
+
canonicalJson(old) !== state.sourceJson ||
|
|
579
|
+
next.session_id !== value.sessionId ||
|
|
580
|
+
next.pid !== process.pid ||
|
|
581
|
+
next.agent_id !== old.agent_id ||
|
|
582
|
+
next.input_revision !== old.input_revision ||
|
|
583
|
+
next.controller_run_id === old.controller_run_id ||
|
|
584
|
+
next.descriptor_file !== `${state.newJournal}.control.json` ||
|
|
585
|
+
canonicalJson(next.scope) !== canonicalJson(value.scope))
|
|
586
|
+
return fail('registration_not_authorized');
|
|
587
|
+
assertDead(value.sourcePid, value.sourceProcessIdentity);
|
|
588
|
+
assertPinned(state.journal, MAX_JOURNAL_BYTES);
|
|
589
|
+
assertPinned(state.descriptor, 4096);
|
|
590
|
+
const reservation = reservations(record).find((row) => row.reservation_id === state.reservationId);
|
|
591
|
+
reservation.status = 'registered';
|
|
592
|
+
reservation.controller_run_id = next.controller_run_id;
|
|
593
|
+
reservation.registered_at = new Date().toISOString();
|
|
594
|
+
}
|
|
595
|
+
/** Enable ongoing authority checks only after the durable new registry entry exists. */
|
|
596
|
+
export function canExecuteManagedPiResume(value, native) {
|
|
597
|
+
const state = AUTHORIZATIONS.get(value);
|
|
598
|
+
if (!state ||
|
|
599
|
+
state.pid !== process.pid ||
|
|
600
|
+
native.sessionId !== value.sessionId ||
|
|
601
|
+
path.resolve(native.cwd) !== value.cwd)
|
|
602
|
+
return false;
|
|
603
|
+
try {
|
|
604
|
+
const record = state.reader.read();
|
|
605
|
+
if (authority(record, value.scope) !== state.authorityDigest ||
|
|
606
|
+
!validReservation(record, state, 'registered'))
|
|
607
|
+
return false;
|
|
608
|
+
const current = sourceRuntime(record, value.sessionId);
|
|
609
|
+
const registered = current.pid === process.pid &&
|
|
610
|
+
current.descriptor_file === `${state.newJournal}.control.json`;
|
|
611
|
+
if (registered)
|
|
612
|
+
markManagedPiResumeCapabilityRegistered(value);
|
|
613
|
+
return registered;
|
|
614
|
+
}
|
|
615
|
+
catch {
|
|
616
|
+
return false;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ManagedPiSdkSessionController } from '../hosts/pi/managed-sdk-session.js';
|
|
2
|
+
import { type SessionTransitionDependencies, type SessionTransitionRequest } from './collab-session-transition.js';
|
|
3
|
+
type RetirementRuntime = Pick<ManagedPiSdkSessionController, 'observe' | 'snapshot' | 'stopOwnedRuntime' | 'isDisposed'>;
|
|
4
|
+
export interface ManagedPiRetirementReceipt {
|
|
5
|
+
readonly schema: 'pappra/managed-pi-retirement/1';
|
|
6
|
+
readonly transition_id: string;
|
|
7
|
+
readonly source_session_id: string;
|
|
8
|
+
readonly successor_session_id: string;
|
|
9
|
+
readonly status: 'runtime_disposed_and_transcript_retained';
|
|
10
|
+
readonly stop: {
|
|
11
|
+
readonly status: 'runtime_disposed';
|
|
12
|
+
readonly process_exit: 'not_owned';
|
|
13
|
+
};
|
|
14
|
+
readonly transcript: {
|
|
15
|
+
readonly source: string;
|
|
16
|
+
readonly ref: string;
|
|
17
|
+
readonly digest: string;
|
|
18
|
+
readonly original: 'retained-in-place';
|
|
19
|
+
};
|
|
20
|
+
readonly native_history: 'unchanged';
|
|
21
|
+
}
|
|
22
|
+
/** Native disposal and retention follow authoritative transfer + successor first work. */
|
|
23
|
+
export declare function retireManagedPiSource(request: SessionTransitionRequest, runtime: RetirementRuntime, dependencies?: SessionTransitionDependencies): Promise<{
|
|
24
|
+
readonly receipt: ManagedPiRetirementReceipt;
|
|
25
|
+
readonly receiptFile: string;
|
|
26
|
+
readonly duplicate: boolean;
|
|
27
|
+
}>;
|
|
28
|
+
export {};
|