pi-daddy 0.49.4 → 0.50.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/CHANGELOG.md +56 -1
- package/README.md +218 -28
- package/dist/governance/initiative-coordinator.d.ts +58 -0
- package/dist/governance/initiative-coordinator.d.ts.map +1 -0
- package/dist/governance/initiative-coordinator.js +348 -0
- package/dist/governance/initiative-coordinator.js.map +1 -0
- package/dist/governance/initiative-effects.d.ts +20 -0
- package/dist/governance/initiative-effects.d.ts.map +1 -0
- package/dist/governance/initiative-effects.js +108 -0
- package/dist/governance/initiative-effects.js.map +1 -0
- package/dist/governance/initiative-state.d.ts +13 -0
- package/dist/governance/initiative-state.d.ts.map +1 -0
- package/dist/governance/initiative-state.js +122 -0
- package/dist/governance/initiative-state.js.map +1 -0
- package/dist/governance/operation-artifact-io.d.ts +8 -0
- package/dist/governance/operation-artifact-io.d.ts.map +1 -0
- package/dist/governance/operation-artifact-io.js +95 -0
- package/dist/governance/operation-artifact-io.js.map +1 -0
- package/dist/governance/operation-artifact-scan.d.ts +13 -0
- package/dist/governance/operation-artifact-scan.d.ts.map +1 -0
- package/dist/governance/operation-artifact-scan.js +71 -0
- package/dist/governance/operation-artifact-scan.js.map +1 -0
- package/dist/governance/operation-artifacts.d.ts +53 -0
- package/dist/governance/operation-artifacts.d.ts.map +1 -0
- package/dist/governance/operation-artifacts.js +309 -0
- package/dist/governance/operation-artifacts.js.map +1 -0
- package/dist/governance/runtime-settlement.d.ts +2 -0
- package/dist/governance/runtime-settlement.d.ts.map +1 -1
- package/dist/governance/runtime-settlement.js +26 -0
- package/dist/governance/runtime-settlement.js.map +1 -1
- package/dist/governance/workspace-acceptance.d.ts +5 -0
- package/dist/governance/workspace-acceptance.d.ts.map +1 -1
- package/dist/governance/workspace-acceptance.js +16 -0
- package/dist/governance/workspace-acceptance.js.map +1 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -1
- package/dist/kernel/dashboard-observation.d.ts +76 -0
- package/dist/kernel/dashboard-observation.d.ts.map +1 -0
- package/dist/kernel/dashboard-observation.js +49 -0
- package/dist/kernel/dashboard-observation.js.map +1 -0
- package/dist/kernel/dispatch-operation.d.ts +2 -0
- package/dist/kernel/dispatch-operation.d.ts.map +1 -1
- package/dist/kernel/dispatch-operation.js.map +1 -1
- package/dist/kernel/initiative.d.ts +94 -0
- package/dist/kernel/initiative.d.ts.map +1 -0
- package/dist/kernel/initiative.js +38 -0
- package/dist/kernel/initiative.js.map +1 -0
- package/dist/kernel/operation-artifact.d.ts +49 -0
- package/dist/kernel/operation-artifact.d.ts.map +1 -0
- package/dist/kernel/operation-artifact.js +4 -0
- package/dist/kernel/operation-artifact.js.map +1 -0
- package/dist/kernel/principal-binding.d.ts +4 -0
- package/dist/kernel/principal-binding.d.ts.map +1 -0
- package/dist/kernel/principal-binding.js +94 -0
- package/dist/kernel/principal-binding.js.map +1 -0
- package/dist/kernel/project-paths.d.ts +6 -0
- package/dist/kernel/project-paths.d.ts.map +1 -1
- package/dist/kernel/project-paths.js +12 -0
- package/dist/kernel/project-paths.js.map +1 -1
- package/dist/kernel/selected-definitions.d.ts.map +1 -1
- package/dist/kernel/selected-definitions.js +5 -91
- package/dist/kernel/selected-definitions.js.map +1 -1
- package/dist/kernel/workspace-diagnostics.d.ts +24 -0
- package/dist/kernel/workspace-diagnostics.d.ts.map +1 -0
- package/dist/kernel/workspace-diagnostics.js +81 -0
- package/dist/kernel/workspace-diagnostics.js.map +1 -0
- package/dist/products/activity-storage.d.ts +2 -0
- package/dist/products/activity-storage.d.ts.map +1 -0
- package/dist/products/activity-storage.js +119 -0
- package/dist/products/activity-storage.js.map +1 -0
- package/dist/products/activity-timeline.d.ts +4 -0
- package/dist/products/activity-timeline.d.ts.map +1 -1
- package/dist/products/activity-timeline.js +12 -0
- package/dist/products/activity-timeline.js.map +1 -1
- package/dist/products/dashboard-cli.d.ts +2 -0
- package/dist/products/dashboard-cli.d.ts.map +1 -1
- package/dist/products/dashboard-cli.js +41 -9
- package/dist/products/dashboard-cli.js.map +1 -1
- package/dist/products/dashboard-display-controls.d.ts.map +1 -1
- package/dist/products/dashboard-display-controls.js +30 -0
- package/dist/products/dashboard-display-controls.js.map +1 -1
- package/dist/products/dashboard-file-cache.d.ts +10 -0
- package/dist/products/dashboard-file-cache.d.ts.map +1 -0
- package/dist/products/dashboard-file-cache.js +38 -0
- package/dist/products/dashboard-file-cache.js.map +1 -0
- package/dist/products/dashboard-panels.d.ts +6 -0
- package/dist/products/dashboard-panels.d.ts.map +1 -0
- package/dist/products/dashboard-panels.js +179 -0
- package/dist/products/dashboard-panels.js.map +1 -0
- package/dist/products/dashboard-screen.d.ts +8 -0
- package/dist/products/dashboard-screen.d.ts.map +1 -1
- package/dist/products/dashboard-screen.js +86 -15
- package/dist/products/dashboard-screen.js.map +1 -1
- package/dist/products/dashboard-session-client.d.ts +16 -2
- package/dist/products/dashboard-session-client.d.ts.map +1 -1
- package/dist/products/dashboard-session-client.js +64 -11
- package/dist/products/dashboard-session-client.js.map +1 -1
- package/dist/products/dashboard-settings.d.ts.map +1 -1
- package/dist/products/dashboard-settings.js +4 -6
- package/dist/products/dashboard-settings.js.map +1 -1
- package/dist/products/ecosystem-versions.d.ts +1 -1
- package/dist/products/ecosystem-versions.d.ts.map +1 -1
- package/extensions/activity-timeline.ts +3 -1
- package/extensions/artifact-bridge.ts +130 -0
- package/extensions/dashboard-live.ts +63 -0
- package/extensions/dashboard-observation.ts +39 -0
- package/extensions/dashboard-session-server.ts +45 -3
- package/extensions/dashboard-sources.ts +124 -0
- package/extensions/delegation-artifacts.ts +125 -0
- package/extensions/delegation-operations.ts +100 -42
- package/extensions/ecosystem-versions.ts +12 -5
- package/extensions/grants-command.ts +34 -6
- package/extensions/grants.ts +9 -20
- package/extensions/initiative-dashboard.ts +143 -0
- package/extensions/initiative-runtime.ts +406 -0
- package/extensions/runtime-snapshot.ts +121 -2
- package/extensions/session-environment.ts +13 -1
- package/extensions/session.ts +28 -10
- package/extensions/workspace-diagnostics.ts +90 -0
- package/package.json +1 -1
- package/src/governance/initiative-coordinator.ts +400 -0
- package/src/governance/initiative-effects.ts +146 -0
- package/src/governance/initiative-state.ts +144 -0
- package/src/governance/operation-artifact-io.ts +103 -0
- package/src/governance/operation-artifact-scan.ts +77 -0
- package/src/governance/operation-artifacts.ts +363 -0
- package/src/governance/runtime-settlement.ts +28 -0
- package/src/governance/workspace-acceptance.ts +18 -0
- package/src/index.ts +13 -0
- package/src/kernel/dashboard-observation.ts +109 -0
- package/src/kernel/dispatch-operation.ts +2 -0
- package/src/kernel/initiative.ts +126 -0
- package/src/kernel/operation-artifact.ts +46 -0
- package/src/kernel/principal-binding.ts +104 -0
- package/src/kernel/project-paths.ts +16 -0
- package/src/kernel/selected-definitions.ts +5 -100
- package/src/kernel/workspace-diagnostics.ts +110 -0
- package/src/products/activity-storage.ts +128 -0
- package/src/products/activity-timeline.ts +11 -0
- package/src/products/dashboard-cli.ts +44 -9
- package/src/products/dashboard-display-controls.ts +27 -0
- package/src/products/dashboard-file-cache.ts +38 -0
- package/src/products/dashboard-panels.ts +217 -0
- package/src/products/dashboard-screen.ts +107 -17
- package/src/products/dashboard-session-client.ts +78 -14
- package/src/products/dashboard-settings.ts +4 -6
- package/src/products/ecosystem-versions.ts +2 -1
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/** Small private, immutable files. Cooperative local integrity, not a hostile same-uid boundary. */
|
|
2
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
3
|
+
import {
|
|
4
|
+
constants,
|
|
5
|
+
openSync,
|
|
6
|
+
closeSync,
|
|
7
|
+
fstatSync,
|
|
8
|
+
fsyncSync,
|
|
9
|
+
mkdirSync,
|
|
10
|
+
realpathSync,
|
|
11
|
+
writeFileSync,
|
|
12
|
+
linkSync,
|
|
13
|
+
unlinkSync,
|
|
14
|
+
lstatSync,
|
|
15
|
+
} from "node:fs";
|
|
16
|
+
import { join, dirname } from "node:path";
|
|
17
|
+
import { readBoundedTextSync } from "../kernel/bounded-read-sync.ts";
|
|
18
|
+
import type { OperationArtifactContent } from "../kernel/operation-artifact.ts";
|
|
19
|
+
export const artifactHash = (text: string) => createHash("sha256").update(text, "utf8").digest("hex");
|
|
20
|
+
export const artifactJson = (value: unknown) => JSON.stringify(value) + "\n";
|
|
21
|
+
export function privateArtifactDirectory(path: string, create = false): string {
|
|
22
|
+
if (create) {
|
|
23
|
+
const first = mkdirSync(path, { recursive: true, mode: 0o700 });
|
|
24
|
+
if (first) {
|
|
25
|
+
let parent = dirname(path);
|
|
26
|
+
while (true) {
|
|
27
|
+
const fd = openSync(parent, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW);
|
|
28
|
+
try {
|
|
29
|
+
fsyncSync(fd);
|
|
30
|
+
} finally {
|
|
31
|
+
closeSync(fd);
|
|
32
|
+
}
|
|
33
|
+
if (parent === dirname(first)) break;
|
|
34
|
+
parent = dirname(parent);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (realpathSync(path) !== path) throw Error("operation artifact directory must be canonical without symlinks");
|
|
39
|
+
const info = lstatSync(path);
|
|
40
|
+
if (!info.isDirectory() || info.uid !== process.getuid?.() || info.mode & 0o077)
|
|
41
|
+
throw Error("operation artifact directory must be private and owned by this uid");
|
|
42
|
+
return path;
|
|
43
|
+
}
|
|
44
|
+
export function readArtifactBytes(reference: OperationArtifactContent, root: string, maxBytes: number): string {
|
|
45
|
+
if (
|
|
46
|
+
!reference ||
|
|
47
|
+
typeof reference.path !== "string" ||
|
|
48
|
+
!/^[a-f0-9]{64}$/.test(reference.sha256) ||
|
|
49
|
+
!Number.isSafeInteger(reference.bytes) ||
|
|
50
|
+
reference.bytes < 0 ||
|
|
51
|
+
reference.bytes > maxBytes
|
|
52
|
+
)
|
|
53
|
+
throw Error("invalid or oversized operation artifact reference");
|
|
54
|
+
const text = readBoundedTextSync(reference.path, { maxBytes, confinedRoot: root }).text;
|
|
55
|
+
if (Buffer.byteLength(text) !== reference.bytes || artifactHash(text) !== reference.sha256)
|
|
56
|
+
throw Error("operation artifact content identity mismatch");
|
|
57
|
+
return text;
|
|
58
|
+
}
|
|
59
|
+
/** Publish without overwrite. A losing writer may reuse only byte-identical content. */
|
|
60
|
+
export function publishArtifact(path: string, name: string, text: string, reuse = true): OperationArtifactContent {
|
|
61
|
+
privateArtifactDirectory(path);
|
|
62
|
+
const fd = openSync(path, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW);
|
|
63
|
+
const held = `/proc/self/fd/${fd}`;
|
|
64
|
+
const temporary = `${name}.${randomUUID()}.tmp`;
|
|
65
|
+
try {
|
|
66
|
+
const info = fstatSync(fd);
|
|
67
|
+
if (!info.isDirectory() || info.uid !== process.getuid?.() || info.mode & 0o077)
|
|
68
|
+
throw Error("invalid artifact owner");
|
|
69
|
+
const file = openSync(
|
|
70
|
+
join(held, temporary),
|
|
71
|
+
constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW,
|
|
72
|
+
0o600,
|
|
73
|
+
);
|
|
74
|
+
try {
|
|
75
|
+
writeFileSync(file, text);
|
|
76
|
+
fsyncSync(file);
|
|
77
|
+
} finally {
|
|
78
|
+
closeSync(file);
|
|
79
|
+
}
|
|
80
|
+
const reference = { path: join(path, name), sha256: artifactHash(text), bytes: Buffer.byteLength(text) };
|
|
81
|
+
try {
|
|
82
|
+
linkSync(join(held, temporary), join(held, name));
|
|
83
|
+
} catch (error) {
|
|
84
|
+
if ((error as NodeJS.ErrnoException).code !== "EEXIST" || !reuse) throw error;
|
|
85
|
+
readArtifactBytes(reference, path, reference.bytes);
|
|
86
|
+
}
|
|
87
|
+
fsyncSync(fd);
|
|
88
|
+
const current = lstatSync(path);
|
|
89
|
+
if (realpathSync(path) !== path || current.ino !== info.ino || current.dev !== info.dev)
|
|
90
|
+
throw Error("artifact directory changed before publication");
|
|
91
|
+
return reference;
|
|
92
|
+
} finally {
|
|
93
|
+
try {
|
|
94
|
+
try {
|
|
95
|
+
unlinkSync(join(held, temporary));
|
|
96
|
+
} catch (error) {
|
|
97
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
98
|
+
}
|
|
99
|
+
} finally {
|
|
100
|
+
closeSync(fd);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/** Bounded traversal of one trusted producer owner. No recursive discovery or dispatch. */
|
|
2
|
+
import { opendirSync, realpathSync, statSync, type Dir } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { operationArtifactsDir } from "../kernel/project-paths.ts";
|
|
5
|
+
import {
|
|
6
|
+
MAX_OPERATION_MANIFEST_BYTES,
|
|
7
|
+
type OperationArtifactOwner,
|
|
8
|
+
type OperationArtifactRef,
|
|
9
|
+
} from "../kernel/operation-artifact.ts";
|
|
10
|
+
import { readBoundedTextSync } from "../kernel/bounded-read-sync.ts";
|
|
11
|
+
import { artifactHash, privateArtifactDirectory } from "./operation-artifact-io.ts";
|
|
12
|
+
import { openOperationArtifacts } from "./operation-artifacts.ts";
|
|
13
|
+
export function operationArtifactInstance(agentDirectory: string): string {
|
|
14
|
+
const canonical = realpathSync(agentDirectory),
|
|
15
|
+
identity = statSync(canonical, { bigint: true });
|
|
16
|
+
return artifactHash(
|
|
17
|
+
JSON.stringify([canonical, identity.dev.toString(), identity.ino.toString(), identity.birthtimeNs.toString()]),
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
export function openOperationArtifactScan(agentDirectory: string, owner: OperationArtifactOwner) {
|
|
21
|
+
const store = openOperationArtifacts(agentDirectory, owner);
|
|
22
|
+
const root = join(
|
|
23
|
+
operationArtifactsDir(realpathSync(agentDirectory)),
|
|
24
|
+
artifactHash(JSON.stringify([owner.sessionId, owner.cwd])),
|
|
25
|
+
);
|
|
26
|
+
let directory: Dir | undefined,
|
|
27
|
+
complete = false;
|
|
28
|
+
try {
|
|
29
|
+
privateArtifactDirectory(root);
|
|
30
|
+
directory = opendirSync(root, { bufferSize: 64 });
|
|
31
|
+
} catch (error) {
|
|
32
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
33
|
+
complete = true;
|
|
34
|
+
}
|
|
35
|
+
const close = () => {
|
|
36
|
+
directory?.closeSync();
|
|
37
|
+
directory = undefined;
|
|
38
|
+
complete = true;
|
|
39
|
+
};
|
|
40
|
+
return {
|
|
41
|
+
close,
|
|
42
|
+
next(limit = 32) {
|
|
43
|
+
if (!Number.isSafeInteger(limit) || limit < 1 || limit > 64) throw Error("invalid artifact scan limit");
|
|
44
|
+
const references: OperationArtifactRef[] = [];
|
|
45
|
+
let scanned = 0,
|
|
46
|
+
uncertain = 0,
|
|
47
|
+
issues = 0;
|
|
48
|
+
while (!complete && scanned < limit) {
|
|
49
|
+
const entry = directory!.readSync();
|
|
50
|
+
if (!entry) {
|
|
51
|
+
close();
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
scanned++;
|
|
55
|
+
try {
|
|
56
|
+
if (!entry.isDirectory() || !/^([a-f0-9]{64})$/.test(entry.name)) throw Error("invalid entry");
|
|
57
|
+
const path = join(root, entry.name);
|
|
58
|
+
privateArtifactDirectory(path);
|
|
59
|
+
const admission = JSON.parse(
|
|
60
|
+
readBoundedTextSync(join(path, "admission.json"), {
|
|
61
|
+
maxBytes: MAX_OPERATION_MANIFEST_BYTES,
|
|
62
|
+
confinedRoot: path,
|
|
63
|
+
}).text,
|
|
64
|
+
);
|
|
65
|
+
const id = admission?.request?.operationId;
|
|
66
|
+
if (typeof id !== "string" || artifactHash(id) !== entry.name) throw Error("invalid identity");
|
|
67
|
+
const retained = store.history(id, false);
|
|
68
|
+
if (retained?.artifact) references.push(retained.artifact);
|
|
69
|
+
else uncertain++;
|
|
70
|
+
} catch {
|
|
71
|
+
issues++;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return { references, scanned, uncertain, issues, complete };
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
/** Durable producer outcomes; opening/reading never starts governance, work, or Bookie registration. */
|
|
2
|
+
import { realpathSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { readBoundedTextSync } from "../kernel/bounded-read-sync.ts";
|
|
5
|
+
import { operationArtifactsDir } from "../kernel/project-paths.ts";
|
|
6
|
+
import { validOperationId, type OperationRequest, type OperationReference } from "../kernel/dispatch-operation.ts";
|
|
7
|
+
import {
|
|
8
|
+
MAX_OPERATION_ARTIFACT_BYTES,
|
|
9
|
+
MAX_OPERATION_MANIFEST_BYTES,
|
|
10
|
+
type OperationArtifactOwner,
|
|
11
|
+
type OperationArtifactRef,
|
|
12
|
+
type OperationArtifactManifest,
|
|
13
|
+
type OperationArtifactOutcome,
|
|
14
|
+
type RetainedOperationFinal,
|
|
15
|
+
} from "../kernel/operation-artifact.ts";
|
|
16
|
+
import {
|
|
17
|
+
artifactHash,
|
|
18
|
+
artifactJson,
|
|
19
|
+
privateArtifactDirectory,
|
|
20
|
+
publishArtifact,
|
|
21
|
+
readArtifactBytes,
|
|
22
|
+
} from "./operation-artifact-io.ts";
|
|
23
|
+
const object = (v: unknown): v is Record<string, any> => !!v && typeof v === "object" && !Array.isArray(v);
|
|
24
|
+
const bounded = (v: unknown, max = 4096): v is string => typeof v === "string" && v.length > 0 && v.length <= max;
|
|
25
|
+
const digest = (v: unknown): v is string => typeof v === "string" && /^[a-f0-9]{64}$/.test(v);
|
|
26
|
+
function ownerCheck(owner: OperationArtifactOwner) {
|
|
27
|
+
if (!object(owner) || !bounded(owner.sessionId, 512) || !bounded(owner.cwd) || !owner.cwd.startsWith("/"))
|
|
28
|
+
throw Error("invalid operation artifact owner");
|
|
29
|
+
}
|
|
30
|
+
function requestCheck(request: OperationRequest) {
|
|
31
|
+
if (
|
|
32
|
+
!object(request) ||
|
|
33
|
+
!validOperationId(request.operationId) ||
|
|
34
|
+
!digest(request.requestDigest) ||
|
|
35
|
+
!bounded(request.executionId, 128) ||
|
|
36
|
+
!request.executionId.startsWith("exec:") ||
|
|
37
|
+
!bounded(request.cwd) ||
|
|
38
|
+
!request.cwd.startsWith("/") ||
|
|
39
|
+
!(request.workspaceId === null || bounded(request.workspaceId, 512))
|
|
40
|
+
)
|
|
41
|
+
throw Error("invalid operation artifact request");
|
|
42
|
+
}
|
|
43
|
+
const requestFields = ["operationId", "executionId", "requestDigest", "cwd", "workspaceId"] as const;
|
|
44
|
+
const sameRequest = (a: OperationRequest, b: OperationRequest) => requestFields.every((key) => a?.[key] === b?.[key]);
|
|
45
|
+
const cleanRequest = (request: OperationRequest): OperationRequest => ({
|
|
46
|
+
operationId: request.operationId,
|
|
47
|
+
executionId: request.executionId,
|
|
48
|
+
requestDigest: request.requestDigest,
|
|
49
|
+
cwd: request.cwd,
|
|
50
|
+
workspaceId: request.workspaceId,
|
|
51
|
+
});
|
|
52
|
+
interface ResolvedScope {
|
|
53
|
+
version: 1;
|
|
54
|
+
executionId: string;
|
|
55
|
+
cwd: string;
|
|
56
|
+
workspaceId: string | null;
|
|
57
|
+
}
|
|
58
|
+
function resolvedScope(path: string, admission: Admission): ResolvedScope | null {
|
|
59
|
+
try {
|
|
60
|
+
const scope = JSON.parse(
|
|
61
|
+
readBoundedTextSync(join(path, "scope.json"), {
|
|
62
|
+
maxBytes: MAX_OPERATION_MANIFEST_BYTES,
|
|
63
|
+
confinedRoot: path,
|
|
64
|
+
}).text,
|
|
65
|
+
);
|
|
66
|
+
if (
|
|
67
|
+
!object(scope) ||
|
|
68
|
+
scope.version !== 1 ||
|
|
69
|
+
scope.executionId !== admission.request.executionId ||
|
|
70
|
+
!bounded(scope.cwd) ||
|
|
71
|
+
!scope.cwd.startsWith("/") ||
|
|
72
|
+
scope.workspaceId !== admission.request.workspaceId ||
|
|
73
|
+
(scope.workspaceId === null && scope.cwd !== admission.request.cwd)
|
|
74
|
+
)
|
|
75
|
+
throw Error("invalid resolved operation scope");
|
|
76
|
+
return scope as ResolvedScope;
|
|
77
|
+
} catch (error) {
|
|
78
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return null;
|
|
79
|
+
throw error;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function location(directory: string | undefined, owner: OperationArtifactOwner, operationId: string) {
|
|
83
|
+
ownerCheck(owner);
|
|
84
|
+
if (!validOperationId(operationId)) throw Error("invalid operation artifact operationId");
|
|
85
|
+
const root = operationArtifactsDir(directory === undefined ? undefined : realpathSync(directory));
|
|
86
|
+
return join(root, artifactHash(JSON.stringify([owner.sessionId, owner.cwd])), artifactHash(operationId));
|
|
87
|
+
}
|
|
88
|
+
interface Admission {
|
|
89
|
+
version: 1;
|
|
90
|
+
producer: "pi-daddy";
|
|
91
|
+
owner: OperationArtifactOwner;
|
|
92
|
+
request: OperationRequest;
|
|
93
|
+
admittedAt: string;
|
|
94
|
+
}
|
|
95
|
+
function parseAdmission(text: string, owner: OperationArtifactOwner, id: string): Admission {
|
|
96
|
+
const v = JSON.parse(text);
|
|
97
|
+
if (
|
|
98
|
+
!object(v) ||
|
|
99
|
+
v.version !== 1 ||
|
|
100
|
+
v.producer !== "pi-daddy" ||
|
|
101
|
+
v.owner?.sessionId !== owner.sessionId ||
|
|
102
|
+
v.owner?.cwd !== owner.cwd ||
|
|
103
|
+
v.request?.operationId !== id ||
|
|
104
|
+
!bounded(v.admittedAt, 64)
|
|
105
|
+
)
|
|
106
|
+
throw Error("invalid operation admission manifest");
|
|
107
|
+
requestCheck(v.request);
|
|
108
|
+
return v as Admission;
|
|
109
|
+
}
|
|
110
|
+
function admissionAt(path: string, owner: OperationArtifactOwner, id: string): Admission | null {
|
|
111
|
+
try {
|
|
112
|
+
privateArtifactDirectory(path);
|
|
113
|
+
return parseAdmission(
|
|
114
|
+
readBoundedTextSync(join(path, "admission.json"), {
|
|
115
|
+
maxBytes: MAX_OPERATION_MANIFEST_BYTES,
|
|
116
|
+
confinedRoot: path,
|
|
117
|
+
}).text,
|
|
118
|
+
owner,
|
|
119
|
+
id,
|
|
120
|
+
);
|
|
121
|
+
} catch (error) {
|
|
122
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return null;
|
|
123
|
+
throw error;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function manifestCheck(v: unknown, admission: Admission, path: string): OperationArtifactManifest {
|
|
127
|
+
if (
|
|
128
|
+
!object(v) ||
|
|
129
|
+
v.version !== 1 ||
|
|
130
|
+
v.producer !== "pi-daddy" ||
|
|
131
|
+
v.owner?.sessionId !== admission.owner.sessionId ||
|
|
132
|
+
v.owner?.cwd !== admission.owner.cwd ||
|
|
133
|
+
!sameRequest(v.request, admission.request) ||
|
|
134
|
+
!object(v.operation) ||
|
|
135
|
+
v.operation.operationId !== admission.request.operationId ||
|
|
136
|
+
v.operation.executionId !== admission.request.executionId ||
|
|
137
|
+
!["settled", "not-started", "uncertain"].includes(v.operation.state) ||
|
|
138
|
+
!bounded(v.observedAt, 64) ||
|
|
139
|
+
!bounded(v.operation.cwd) ||
|
|
140
|
+
!(v.operation.workspaceId === null || bounded(v.operation.workspaceId, 512)) ||
|
|
141
|
+
!bounded(v.operation.startedAt, 64) ||
|
|
142
|
+
!bounded(v.operation.updatedAt, 64) ||
|
|
143
|
+
v.operation.artifact !== undefined
|
|
144
|
+
)
|
|
145
|
+
throw Error("invalid operation result manifest");
|
|
146
|
+
const expectedScope = resolvedScope(path, admission) ?? admission.request;
|
|
147
|
+
if (v.operation.cwd !== expectedScope.cwd || v.operation.workspaceId !== expectedScope.workspaceId)
|
|
148
|
+
throw Error("operation result scope does not match its admitted execution");
|
|
149
|
+
if (v.final) {
|
|
150
|
+
const observed = v.operation.runtime?.final;
|
|
151
|
+
if (
|
|
152
|
+
!object(v.final) ||
|
|
153
|
+
v.final.state !== "complete" ||
|
|
154
|
+
!digest(v.final.sha256) ||
|
|
155
|
+
!["sessionId", "messageId", "leafId"].every((k) => bounded(v.final[k], 512)) ||
|
|
156
|
+
v.final.content?.path !== join(path, "final.txt") ||
|
|
157
|
+
v.final.content?.sha256 !== v.final.sha256 ||
|
|
158
|
+
observed?.state !== "complete" ||
|
|
159
|
+
!["sessionId", "messageId", "leafId", "sha256"].every((k) => observed[k] === v.final[k])
|
|
160
|
+
)
|
|
161
|
+
throw Error("operation native final identity mismatch");
|
|
162
|
+
}
|
|
163
|
+
if (v.partial && v.partial.path !== join(path, "partial.txt")) throw Error("invalid partial artifact path");
|
|
164
|
+
if (v.final && v.partial) throw Error("exact final and partial evidence cannot be conflated");
|
|
165
|
+
return v as OperationArtifactManifest;
|
|
166
|
+
}
|
|
167
|
+
function refFor(manifest: OperationArtifactManifest, content: OperationArtifactRef["manifest"]): OperationArtifactRef {
|
|
168
|
+
return {
|
|
169
|
+
version: 1,
|
|
170
|
+
producer: "pi-daddy",
|
|
171
|
+
owner: { ...manifest.owner },
|
|
172
|
+
operationId: manifest.request.operationId,
|
|
173
|
+
executionId: manifest.request.executionId,
|
|
174
|
+
manifest: content,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
export interface ReadOperationArtifactOptions {
|
|
178
|
+
/** Trusted selected agent directory, never inferred from the reference. */
|
|
179
|
+
agentDirectory?: string;
|
|
180
|
+
/** Maximum returned authored bytes. Metadata has its own 64 KiB bound. */
|
|
181
|
+
maxBytes?: number;
|
|
182
|
+
}
|
|
183
|
+
/** Exact immutable read; a successful read is not current applicability or execution authority. */
|
|
184
|
+
export function readOperationArtifact(reference: OperationArtifactRef, options: ReadOperationArtifactOptions = {}) {
|
|
185
|
+
if (!object(reference) || reference.version !== 1 || reference.producer !== "pi-daddy")
|
|
186
|
+
throw Error("unsupported operation artifact reference");
|
|
187
|
+
const path = location(options.agentDirectory, reference.owner, reference.operationId);
|
|
188
|
+
if (reference.manifest?.path !== join(path, "result.json"))
|
|
189
|
+
throw Error("operation artifact path does not match owner");
|
|
190
|
+
const admission = admissionAt(path, reference.owner, reference.operationId);
|
|
191
|
+
if (!admission || reference.executionId !== admission.request.executionId)
|
|
192
|
+
throw Error("operation artifact admission identity mismatch");
|
|
193
|
+
const manifest = manifestCheck(
|
|
194
|
+
JSON.parse(readArtifactBytes(reference.manifest, path, MAX_OPERATION_MANIFEST_BYTES)),
|
|
195
|
+
admission,
|
|
196
|
+
path,
|
|
197
|
+
);
|
|
198
|
+
const maxBytes = options.maxBytes ?? MAX_OPERATION_ARTIFACT_BYTES;
|
|
199
|
+
if (!Number.isSafeInteger(maxBytes) || maxBytes < 0 || maxBytes > MAX_OPERATION_ARTIFACT_BYTES)
|
|
200
|
+
throw Error("invalid operation artifact read limit");
|
|
201
|
+
let final: RetainedOperationFinal | undefined;
|
|
202
|
+
let partial: { text: string; sha256: string } | undefined;
|
|
203
|
+
if (manifest.final) {
|
|
204
|
+
const { content, ...identity } = manifest.final;
|
|
205
|
+
final = { ...identity, text: readArtifactBytes(content, path, maxBytes) };
|
|
206
|
+
}
|
|
207
|
+
if (manifest.partial)
|
|
208
|
+
partial = { text: readArtifactBytes(manifest.partial, path, maxBytes), sha256: manifest.partial.sha256 };
|
|
209
|
+
return { manifest, ...(final ? { final } : {}), ...(partial ? { partial } : {}) };
|
|
210
|
+
}
|
|
211
|
+
export interface OperationArtifactHistory {
|
|
212
|
+
operation: OperationReference;
|
|
213
|
+
requestDigest: string;
|
|
214
|
+
reason?: string;
|
|
215
|
+
artifact?: OperationArtifactRef;
|
|
216
|
+
final?: RetainedOperationFinal;
|
|
217
|
+
partial?: { text: string; sha256: string };
|
|
218
|
+
}
|
|
219
|
+
export function openOperationArtifacts(agentDirectory: string, owner: OperationArtifactOwner) {
|
|
220
|
+
ownerCheck(owner);
|
|
221
|
+
const directory = realpathSync(agentDirectory);
|
|
222
|
+
privateArtifactDirectory(operationArtifactsDir(directory), true);
|
|
223
|
+
const history = (operationId: string, includeFinal = true): OperationArtifactHistory | null => {
|
|
224
|
+
const path = location(directory, owner, operationId);
|
|
225
|
+
const admission = admissionAt(path, owner, operationId);
|
|
226
|
+
if (!admission) return null;
|
|
227
|
+
let text: string;
|
|
228
|
+
try {
|
|
229
|
+
text = readBoundedTextSync(join(path, "result.json"), {
|
|
230
|
+
maxBytes: MAX_OPERATION_MANIFEST_BYTES,
|
|
231
|
+
confinedRoot: path,
|
|
232
|
+
}).text;
|
|
233
|
+
} catch (error) {
|
|
234
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
235
|
+
const scope = resolvedScope(path, admission) ?? admission.request;
|
|
236
|
+
const operation: OperationReference = {
|
|
237
|
+
operationId,
|
|
238
|
+
executionId: admission.request.executionId,
|
|
239
|
+
cwd: scope.cwd,
|
|
240
|
+
workspaceId: scope.workspaceId,
|
|
241
|
+
state: "uncertain",
|
|
242
|
+
startedAt: admission.admittedAt,
|
|
243
|
+
updatedAt: admission.admittedAt,
|
|
244
|
+
};
|
|
245
|
+
return {
|
|
246
|
+
operation,
|
|
247
|
+
requestDigest: admission.request.requestDigest,
|
|
248
|
+
reason: "admission retained without a published result; no retry or no-start proof",
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
const manifest = manifestCheck(JSON.parse(text), admission, path);
|
|
252
|
+
const artifact = refFor(manifest, {
|
|
253
|
+
path: join(path, "result.json"),
|
|
254
|
+
sha256: artifactHash(text),
|
|
255
|
+
bytes: Buffer.byteLength(text),
|
|
256
|
+
});
|
|
257
|
+
const content = includeFinal ? readOperationArtifact(artifact, { agentDirectory: directory }) : undefined;
|
|
258
|
+
return {
|
|
259
|
+
operation: { ...manifest.operation, artifact },
|
|
260
|
+
artifact,
|
|
261
|
+
requestDigest: admission.request.requestDigest,
|
|
262
|
+
...(content?.final ? { final: content.final } : {}),
|
|
263
|
+
...(content?.partial ? { partial: content.partial } : {}),
|
|
264
|
+
};
|
|
265
|
+
};
|
|
266
|
+
return {
|
|
267
|
+
owner: Object.freeze({ ...owner }),
|
|
268
|
+
history,
|
|
269
|
+
admit(request: OperationRequest) {
|
|
270
|
+
requestCheck(request);
|
|
271
|
+
request = cleanRequest(request);
|
|
272
|
+
const existing = history(request.operationId, false);
|
|
273
|
+
if (existing) {
|
|
274
|
+
if (existing.requestDigest !== request.requestDigest)
|
|
275
|
+
throw Error("Operation identity conflict with retained request");
|
|
276
|
+
return { created: false, ...existing };
|
|
277
|
+
}
|
|
278
|
+
const path = location(directory, owner, request.operationId);
|
|
279
|
+
privateArtifactDirectory(path, true);
|
|
280
|
+
const admission: Admission = {
|
|
281
|
+
version: 1,
|
|
282
|
+
producer: "pi-daddy",
|
|
283
|
+
owner: { ...owner },
|
|
284
|
+
request: { ...request },
|
|
285
|
+
admittedAt: new Date().toISOString(),
|
|
286
|
+
};
|
|
287
|
+
try {
|
|
288
|
+
publishArtifact(path, "admission.json", artifactJson(admission), false);
|
|
289
|
+
} catch (error) {
|
|
290
|
+
const winner = history(request.operationId, false);
|
|
291
|
+
if (!winner) throw error;
|
|
292
|
+
if (winner.requestDigest !== request.requestDigest)
|
|
293
|
+
throw Error("Operation identity conflict with retained request");
|
|
294
|
+
return { created: false, ...winner };
|
|
295
|
+
}
|
|
296
|
+
return { created: true };
|
|
297
|
+
},
|
|
298
|
+
started(request: OperationRequest, cwd: string, workspaceId: string | null) {
|
|
299
|
+
requestCheck(request);
|
|
300
|
+
const path = location(directory, owner, request.operationId),
|
|
301
|
+
admission = admissionAt(path, owner, request.operationId);
|
|
302
|
+
if (
|
|
303
|
+
!admission ||
|
|
304
|
+
!sameRequest(admission.request, request) ||
|
|
305
|
+
!bounded(cwd) ||
|
|
306
|
+
!cwd.startsWith("/") ||
|
|
307
|
+
workspaceId !== request.workspaceId ||
|
|
308
|
+
(workspaceId === null && cwd !== request.cwd)
|
|
309
|
+
)
|
|
310
|
+
throw Error("resolved operation scope does not match its admitted request");
|
|
311
|
+
publishArtifact(
|
|
312
|
+
path,
|
|
313
|
+
"scope.json",
|
|
314
|
+
artifactJson({ version: 1, executionId: request.executionId, cwd, workspaceId }),
|
|
315
|
+
);
|
|
316
|
+
},
|
|
317
|
+
finish(request: OperationRequest, outcome: OperationArtifactOutcome): OperationArtifactRef {
|
|
318
|
+
requestCheck(request);
|
|
319
|
+
request = cleanRequest(request);
|
|
320
|
+
const path = location(directory, owner, request.operationId);
|
|
321
|
+
const admission = admissionAt(path, owner, request.operationId);
|
|
322
|
+
if (!admission || !sameRequest(admission.request, request))
|
|
323
|
+
throw Error("operation result does not own its admission");
|
|
324
|
+
const { artifact: _artifact, ...operation } = outcome.operation as OperationReference;
|
|
325
|
+
const manifest: OperationArtifactManifest = {
|
|
326
|
+
version: 1,
|
|
327
|
+
producer: "pi-daddy",
|
|
328
|
+
owner: { ...owner },
|
|
329
|
+
request: { ...request },
|
|
330
|
+
operation,
|
|
331
|
+
observedAt: operation.updatedAt,
|
|
332
|
+
};
|
|
333
|
+
if (outcome.final) {
|
|
334
|
+
const { text, ...identity } = outcome.final;
|
|
335
|
+
if (
|
|
336
|
+
typeof text !== "string" ||
|
|
337
|
+
Buffer.byteLength(text) > MAX_OPERATION_ARTIFACT_BYTES ||
|
|
338
|
+
artifactHash(text) !== identity.sha256
|
|
339
|
+
)
|
|
340
|
+
throw Error("invalid or oversized exact final");
|
|
341
|
+
manifest.final = {
|
|
342
|
+
...identity,
|
|
343
|
+
content: { path: join(path, "final.txt"), sha256: identity.sha256, bytes: Buffer.byteLength(text) },
|
|
344
|
+
};
|
|
345
|
+
} else if (outcome.partial !== undefined) {
|
|
346
|
+
if (typeof outcome.partial !== "string" || Buffer.byteLength(outcome.partial) > MAX_OPERATION_ARTIFACT_BYTES)
|
|
347
|
+
throw Error("oversized partial output");
|
|
348
|
+
manifest.partial = {
|
|
349
|
+
path: join(path, "partial.txt"),
|
|
350
|
+
sha256: artifactHash(outcome.partial),
|
|
351
|
+
bytes: Buffer.byteLength(outcome.partial),
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
manifestCheck(manifest, admission, path);
|
|
355
|
+
if (manifest.final) publishArtifact(path, "final.txt", outcome.final!.text);
|
|
356
|
+
if (manifest.partial) publishArtifact(path, "partial.txt", outcome.partial!);
|
|
357
|
+
const text = artifactJson(manifest);
|
|
358
|
+
if (Buffer.byteLength(text) > MAX_OPERATION_MANIFEST_BYTES) throw Error("oversized operation manifest");
|
|
359
|
+
return refFor(manifest, publishArtifact(path, "result.json", text));
|
|
360
|
+
},
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
export type OperationArtifactStore = ReturnType<typeof openOperationArtifacts>;
|
|
@@ -269,3 +269,31 @@ export async function openRuntimeSettlement(
|
|
|
269
269
|
throw error;
|
|
270
270
|
}
|
|
271
271
|
}
|
|
272
|
+
|
|
273
|
+
/** Read-only evidence for one exact historical execution. Does not adopt its session or rewrite the journal. */
|
|
274
|
+
export async function readRuntimeExecutionSettlement(
|
|
275
|
+
directory: string,
|
|
276
|
+
sessionId: string,
|
|
277
|
+
cwd: string,
|
|
278
|
+
executionId: string,
|
|
279
|
+
) {
|
|
280
|
+
const root = join(realpathSync(directory), "pi-daddy", "runtime-settlement");
|
|
281
|
+
if (realpathSync(root) !== root) throw Error("runtime settlement root changed");
|
|
282
|
+
const info = lstatSync(root);
|
|
283
|
+
if (!info.isDirectory() || info.uid !== process.getuid?.() || info.mode & 0o077)
|
|
284
|
+
throw Error("runtime settlement root must remain private");
|
|
285
|
+
const path = join(root, settlementHash([sessionId, cwd]) + ".json");
|
|
286
|
+
const text = readBoundedTextSync(path, { maxBytes: MAX_RECORD, confinedRoot: root }).text;
|
|
287
|
+
const journal = validate(JSON.parse(text), sessionId, cwd);
|
|
288
|
+
const e = journal.executions.find((item) => item.id === executionId);
|
|
289
|
+
if (!e || e.problem) return "unknown" as const;
|
|
290
|
+
let result: "settled" | "not-started" | "unknown" = "unknown";
|
|
291
|
+
if (e.state === "not-started" && !e.identity) result = "not-started";
|
|
292
|
+
else if (e.identity) {
|
|
293
|
+
const receipt = await readCapturedWorkerReceipt(e.identity);
|
|
294
|
+
if (receipt && (!e.receiptSha256 || e.receiptSha256 === settlementHash(receipt))) result = "settled";
|
|
295
|
+
}
|
|
296
|
+
if (readBoundedTextSync(path, { maxBytes: MAX_RECORD, confinedRoot: root }).text !== text)
|
|
297
|
+
throw Error("runtime settlement evidence changed during read");
|
|
298
|
+
return result;
|
|
299
|
+
}
|
|
@@ -116,6 +116,24 @@ export async function reconcileAcceptedWorkspaces(
|
|
|
116
116
|
};
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
+
/** Read-only inspection: unlike reconciliation, absence never creates first-use acceptance. */
|
|
120
|
+
export async function inspectAcceptedWorkspaces(
|
|
121
|
+
registryPath: string,
|
|
122
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
123
|
+
): Promise<{ state: "absent" | "invalid" | "recorded"; accepted: string[] }> {
|
|
124
|
+
let text: string;
|
|
125
|
+
try {
|
|
126
|
+
text = await readAuthorityText(acceptedWorkspacesPath(registryPath, env));
|
|
127
|
+
} catch (error) {
|
|
128
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return { state: "absent", accepted: [] };
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
const stored = parse(text);
|
|
132
|
+
return stored && stored.registry === registryPath
|
|
133
|
+
? { state: "recorded", accepted: [...stored.accepted] }
|
|
134
|
+
: { state: "invalid", accepted: [] };
|
|
135
|
+
}
|
|
136
|
+
|
|
119
137
|
/** Accept the registry's current ids, replacing what was accepted before. `/grants` drives this. */
|
|
120
138
|
export async function acceptWorkspaces(
|
|
121
139
|
registryPath: string,
|
package/src/index.ts
CHANGED
|
@@ -193,3 +193,16 @@ export {
|
|
|
193
193
|
} from "./kernel/workspace-pin.ts";
|
|
194
194
|
|
|
195
195
|
export { exportDiagnostics, parseDiagnosticSelection, type DiagnosticSelection } from "./products/diagnostic-export.ts";
|
|
196
|
+
|
|
197
|
+
export * from "./kernel/operation-artifact.ts";
|
|
198
|
+
export * from "./governance/operation-artifacts.ts";
|
|
199
|
+
|
|
200
|
+
export { openOperationArtifactScan, operationArtifactInstance } from "./governance/operation-artifact-scan.ts";
|
|
201
|
+
|
|
202
|
+
export * from "./kernel/dashboard-observation.ts";
|
|
203
|
+
|
|
204
|
+
export * from "./kernel/initiative.ts";
|
|
205
|
+
export * from "./governance/initiative-state.ts";
|
|
206
|
+
export * from "./governance/initiative-coordinator.ts";
|
|
207
|
+
|
|
208
|
+
export { inspectWorkspaceRouting, type WorkspaceRoutingObservation } from "./kernel/workspace-diagnostics.ts";
|