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,143 @@
|
|
|
1
|
+
/** Cached owner observations. Refresh on state transitions, never on terminal redraw. */
|
|
2
|
+
import type { DashboardSourceSnapshot, DashboardContribution } from "../src/kernel/dashboard-observation.ts";
|
|
3
|
+
import type { InitiativeCoordinator } from "../src/governance/initiative-coordinator.ts";
|
|
4
|
+
import type { InitiativeTask, InitiativeTaskIdentity, InitiativeObservation } from "../src/kernel/initiative.ts";
|
|
5
|
+
import { artifactHash } from "../src/governance/operation-artifact-io.ts";
|
|
6
|
+
export function initiativeDashboard(
|
|
7
|
+
coordinator: InitiativeCoordinator,
|
|
8
|
+
tasks: InitiativeTask[],
|
|
9
|
+
now: () => number = Date.now,
|
|
10
|
+
) {
|
|
11
|
+
let sequence = 0,
|
|
12
|
+
snapshot: DashboardSourceSnapshot | undefined,
|
|
13
|
+
closed = false;
|
|
14
|
+
let pending: Promise<void> | undefined;
|
|
15
|
+
const metadata = coordinator.metadata();
|
|
16
|
+
const createdAt = new Date(now()).toISOString();
|
|
17
|
+
const last = new Map<
|
|
18
|
+
string,
|
|
19
|
+
{
|
|
20
|
+
revision: string;
|
|
21
|
+
observedAt: string;
|
|
22
|
+
stale: boolean;
|
|
23
|
+
observation: Omit<InitiativeObservation, "assertCurrent">;
|
|
24
|
+
}
|
|
25
|
+
>();
|
|
26
|
+
const selected = tasks.slice(0, 5).map((task) => structuredClone(task));
|
|
27
|
+
const key = (task: InitiativeTaskIdentity | InitiativeTask) => JSON.stringify([task.taskId, task.taskRevision]);
|
|
28
|
+
return {
|
|
29
|
+
/** Only a real owner read may supply facts; this cache is never consulted by admission or tools. */
|
|
30
|
+
record(
|
|
31
|
+
input: InitiativeTaskIdentity,
|
|
32
|
+
observation: Awaited<ReturnType<InitiativeCoordinator["observe"]>>,
|
|
33
|
+
revision: string,
|
|
34
|
+
) {
|
|
35
|
+
if (closed || input.initiativeId !== metadata.initiativeId || !selected.some((task) => key(task) === key(input)))
|
|
36
|
+
return;
|
|
37
|
+
if (observation.status !== "observed") {
|
|
38
|
+
last.delete(key(input));
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const { assertCurrent: recheck, ...facts } = observation;
|
|
42
|
+
void recheck;
|
|
43
|
+
last.set(key(input), {
|
|
44
|
+
revision,
|
|
45
|
+
observedAt: new Date(now()).toISOString(),
|
|
46
|
+
stale: false,
|
|
47
|
+
observation: structuredClone(facts),
|
|
48
|
+
});
|
|
49
|
+
},
|
|
50
|
+
invalidate(input?: InitiativeTaskIdentity) {
|
|
51
|
+
for (const [id, entry] of last) if (!input || id === key(input)) entry.stale = true;
|
|
52
|
+
if (snapshot)
|
|
53
|
+
snapshot = {
|
|
54
|
+
...snapshot,
|
|
55
|
+
items: snapshot.items.map((item) =>
|
|
56
|
+
item.kind === "work" && (!input || item.id === input.taskId.slice(0, 160))
|
|
57
|
+
? {
|
|
58
|
+
...item,
|
|
59
|
+
state: "unavailable",
|
|
60
|
+
details: item.details?.map((text, i) => (i === 3 ? `Stale: ${text}`.slice(0, 240) : text)),
|
|
61
|
+
}
|
|
62
|
+
: item,
|
|
63
|
+
),
|
|
64
|
+
};
|
|
65
|
+
},
|
|
66
|
+
snapshot: () => snapshot,
|
|
67
|
+
close() {
|
|
68
|
+
closed = true;
|
|
69
|
+
if (snapshot) snapshot = { ...snapshot, availability: "closed" };
|
|
70
|
+
},
|
|
71
|
+
refresh(): Promise<void> {
|
|
72
|
+
if (closed) return Promise.resolve();
|
|
73
|
+
if (pending) return pending;
|
|
74
|
+
pending = (async () => {
|
|
75
|
+
const items: DashboardContribution[] = [];
|
|
76
|
+
const meta = coordinator.metadata();
|
|
77
|
+
for (const task of selected) {
|
|
78
|
+
const retained = last.get(key(task));
|
|
79
|
+
const known = retained?.observation;
|
|
80
|
+
const stale =
|
|
81
|
+
retained?.stale ||
|
|
82
|
+
retained?.revision !== coordinator.revision() ||
|
|
83
|
+
(!!retained && now() - Date.parse(retained.observedAt) > 120000);
|
|
84
|
+
items.push({
|
|
85
|
+
id: task.taskId.slice(0, 160),
|
|
86
|
+
kind: "work",
|
|
87
|
+
label: task.taskId.slice(0, 180),
|
|
88
|
+
state:
|
|
89
|
+
!known || stale
|
|
90
|
+
? "unavailable"
|
|
91
|
+
: known.reconciliationRequired
|
|
92
|
+
? "unknown"
|
|
93
|
+
: known.execution === "running"
|
|
94
|
+
? "running"
|
|
95
|
+
: known.execution === "settled"
|
|
96
|
+
? "settled"
|
|
97
|
+
: "observed",
|
|
98
|
+
details: [
|
|
99
|
+
`Initiative scope: ${metadata.initiativeId.slice(0, 180)}`,
|
|
100
|
+
`Task revision: ${task.taskRevision}`,
|
|
101
|
+
`Last owner generation: ${known?.ownerGeneration ?? "unobserved"}; claim: ${known?.claim ?? "unavailable"}`,
|
|
102
|
+
retained
|
|
103
|
+
? `Last observed ${retained.observedAt}${stale ? "; stale" : ""}; effect ${task.effectScope}`
|
|
104
|
+
: `Not yet observed; effect ${task.effectScope}`,
|
|
105
|
+
...(known?.effectObservation
|
|
106
|
+
? [
|
|
107
|
+
`Target ${known.effectObservation.target.kind}: ${known.effectObservation.outcome}; ${known.effectObservation.provenance}`,
|
|
108
|
+
`Target identity: ${known.effectObservation.target.reference.slice(0, 215)}`,
|
|
109
|
+
]
|
|
110
|
+
: []),
|
|
111
|
+
],
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
if (closed) return;
|
|
115
|
+
items.unshift({
|
|
116
|
+
id: "scope",
|
|
117
|
+
kind: "status",
|
|
118
|
+
label: `Initiative ${metadata.initiativeId.slice(0, 190)}`,
|
|
119
|
+
state: "observed",
|
|
120
|
+
details: [
|
|
121
|
+
`${coordinator.recoveryOnly ? "Recovery-only observer" : "One-host coordinator"} generation ${meta.generation}`,
|
|
122
|
+
`${meta.knownSessions} known session(s); ${meta.recordedAttempts} recorded attempt(s)`,
|
|
123
|
+
`${tasks.length} selected task(s); first ${Math.min(5, tasks.length)} shown`,
|
|
124
|
+
"Last-observed facts only, not task acceptance or execution authority. Releases belong to their owner.",
|
|
125
|
+
],
|
|
126
|
+
});
|
|
127
|
+
snapshot = {
|
|
128
|
+
producer: "daddy-initiative",
|
|
129
|
+
producerVersion: "v1",
|
|
130
|
+
generation: `${meta.generation}:${artifactHash(metadata.initiativeId)}`,
|
|
131
|
+
sequence: ++sequence,
|
|
132
|
+
observedAt: [...last.values()].map((item) => item.observedAt).sort()[0] ?? createdAt,
|
|
133
|
+
staleAfterMs: 120000,
|
|
134
|
+
availability: "observed",
|
|
135
|
+
items,
|
|
136
|
+
};
|
|
137
|
+
})().finally(() => {
|
|
138
|
+
pending = undefined;
|
|
139
|
+
});
|
|
140
|
+
return pending;
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
}
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
import { initiativeDashboard } from "./initiative-dashboard.ts";
|
|
2
|
+
/** Trusted selected-work-map connector. A selection permits association only, never dispatch or tool authority. */
|
|
3
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
import { Type } from "typebox";
|
|
5
|
+
import { agentDir } from "../src/kernel/project-paths.ts";
|
|
6
|
+
import { validOperationId, type OperationRequest } from "../src/kernel/dispatch-operation.ts";
|
|
7
|
+
import {
|
|
8
|
+
validInitiativeTask,
|
|
9
|
+
type InitiativeTask,
|
|
10
|
+
type InitiativeTaskIdentity,
|
|
11
|
+
type InitiativeEffectReconciliation,
|
|
12
|
+
} from "../src/kernel/initiative.ts";
|
|
13
|
+
import { readInitiativeState } from "../src/governance/initiative-state.ts";
|
|
14
|
+
import { acquireInitiativeCoordinator, type InitiativeCoordinator } from "../src/governance/initiative-coordinator.ts";
|
|
15
|
+
import { openOperationArtifacts } from "../src/governance/operation-artifacts.ts";
|
|
16
|
+
import { readRuntimeExecutionSettlement } from "../src/governance/runtime-settlement.ts";
|
|
17
|
+
import type { GrantsSession } from "./session.ts";
|
|
18
|
+
export const INITIATIVE_EVENT = "pi-daddy:initiative:v1";
|
|
19
|
+
export interface InitiativeOperationBinding {
|
|
20
|
+
capture(request: OperationRequest):
|
|
21
|
+
| {
|
|
22
|
+
begin(): Promise<void>;
|
|
23
|
+
assertCurrent(scope?: { cwd: string; workspaceId: string | null }): Promise<void>;
|
|
24
|
+
notAdmitted(): Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
| undefined;
|
|
27
|
+
}
|
|
28
|
+
type Owner = { sessionId: string; cwd: string; lifecycle: object };
|
|
29
|
+
export function registerInitiativeRuntime(
|
|
30
|
+
pi: ExtensionAPI,
|
|
31
|
+
session: GrantsSession,
|
|
32
|
+
currentOwner: () => Owner | undefined,
|
|
33
|
+
) {
|
|
34
|
+
// Optional composition is unavailable on minimal/legacy hosts; ordinary Daddy startup remains valid.
|
|
35
|
+
if (typeof pi.events?.on !== "function") return { async reset() {}, async close() {} };
|
|
36
|
+
let selected:
|
|
37
|
+
| {
|
|
38
|
+
initiativeId: string;
|
|
39
|
+
owner: Owner;
|
|
40
|
+
coordinator: InitiativeCoordinator;
|
|
41
|
+
tasks: InitiativeTask[];
|
|
42
|
+
dashboard: ReturnType<typeof initiativeDashboard>;
|
|
43
|
+
associations: Map<string, InitiativeTask>;
|
|
44
|
+
assertSelection(): void | Promise<void>;
|
|
45
|
+
assertTaskReady(task: { taskId: string; taskRevision: string }): void | Promise<void>;
|
|
46
|
+
assertExecutionScope?: (scope: {
|
|
47
|
+
taskId: string;
|
|
48
|
+
taskRevision: string;
|
|
49
|
+
cwd: string;
|
|
50
|
+
workspaceId: string | null;
|
|
51
|
+
}) => void | Promise<void>;
|
|
52
|
+
}
|
|
53
|
+
| undefined;
|
|
54
|
+
let selectionEpoch = 0;
|
|
55
|
+
let lastDisplay: { owner: Owner; dashboard: ReturnType<typeof initiativeDashboard> } | undefined;
|
|
56
|
+
session.dashboardInitiative = () =>
|
|
57
|
+
currentOwner() === lastDisplay?.owner ? lastDisplay?.dashboard.snapshot() : undefined;
|
|
58
|
+
session.refreshInitiative = () => {
|
|
59
|
+
selected?.dashboard.invalidate();
|
|
60
|
+
void selected?.dashboard.refresh().catch(() => undefined);
|
|
61
|
+
};
|
|
62
|
+
const stillSelected = async (s: NonNullable<typeof selected>) => {
|
|
63
|
+
if (selected !== s || currentOwner() !== s.owner) throw Error("initiative selection changed");
|
|
64
|
+
try {
|
|
65
|
+
// The coordinator invokes this selection's same bound assertAuthority callback, with generation checks.
|
|
66
|
+
await s.coordinator.assertCurrent();
|
|
67
|
+
} catch (error) {
|
|
68
|
+
s.dashboard.invalidate();
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
if (selected !== s || currentOwner() !== s.owner) throw Error("initiative selection changed");
|
|
72
|
+
};
|
|
73
|
+
const stillObserved = async (s: NonNullable<typeof selected>) => {
|
|
74
|
+
if (selected !== s || currentOwner() !== s.owner) throw Error("initiative selection changed");
|
|
75
|
+
try {
|
|
76
|
+
await s.coordinator.assertObservationCurrent();
|
|
77
|
+
} catch (error) {
|
|
78
|
+
s.dashboard.invalidate();
|
|
79
|
+
throw error;
|
|
80
|
+
}
|
|
81
|
+
if (selected !== s || currentOwner() !== s.owner) throw Error("initiative selection changed");
|
|
82
|
+
};
|
|
83
|
+
const associate = async (
|
|
84
|
+
s: NonNullable<typeof selected>,
|
|
85
|
+
input: InitiativeTaskIdentity & { operationId: string },
|
|
86
|
+
) => {
|
|
87
|
+
await stillSelected(s);
|
|
88
|
+
if (s.coordinator.recoveryOnly)
|
|
89
|
+
throw Error("Recovery-only owner cannot associate or dispatch; reconcile then release and acquire.");
|
|
90
|
+
const task = s.tasks.find((t) => t.taskId === input.taskId && t.taskRevision === input.taskRevision);
|
|
91
|
+
if (input.initiativeId !== s.initiativeId || !task || !validOperationId(input.operationId))
|
|
92
|
+
throw Error("task is outside the selected work map");
|
|
93
|
+
await s.assertTaskReady({ taskId: task.taskId, taskRevision: task.taskRevision });
|
|
94
|
+
await stillSelected(s);
|
|
95
|
+
const previous = s.associations.get(input.operationId);
|
|
96
|
+
if (previous && (previous.taskId !== task.taskId || previous.taskRevision !== task.taskRevision))
|
|
97
|
+
throw Error("operation is already associated with another task");
|
|
98
|
+
await s.coordinator.claim(task);
|
|
99
|
+
await stillSelected(s);
|
|
100
|
+
if (s.associations.size >= 1024 && !previous) throw Error("initiative association bound reached");
|
|
101
|
+
const currentAssociation = s.associations.get(input.operationId);
|
|
102
|
+
if (
|
|
103
|
+
currentAssociation &&
|
|
104
|
+
(currentAssociation.taskId !== task.taskId || currentAssociation.taskRevision !== task.taskRevision)
|
|
105
|
+
)
|
|
106
|
+
throw Error("operation association changed concurrently");
|
|
107
|
+
s.associations.set(input.operationId, task);
|
|
108
|
+
await s.dashboard.refresh();
|
|
109
|
+
return {
|
|
110
|
+
associated: true,
|
|
111
|
+
ownerGeneration: s.coordinator.generation,
|
|
112
|
+
operationId: input.operationId,
|
|
113
|
+
taskId: task.taskId,
|
|
114
|
+
taskRevision: task.taskRevision,
|
|
115
|
+
dispatchStarted: false,
|
|
116
|
+
executionAuthorized: false,
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
const binding: InitiativeOperationBinding = {
|
|
120
|
+
capture(request) {
|
|
121
|
+
const s = selected,
|
|
122
|
+
task = s?.associations.get(request.operationId);
|
|
123
|
+
if (!s || !task) return undefined;
|
|
124
|
+
return {
|
|
125
|
+
async begin() {
|
|
126
|
+
await stillSelected(s);
|
|
127
|
+
await s.assertTaskReady({ taskId: task.taskId, taskRevision: task.taskRevision });
|
|
128
|
+
await stillSelected(s);
|
|
129
|
+
await s.coordinator.begin(task, request);
|
|
130
|
+
await stillSelected(s);
|
|
131
|
+
},
|
|
132
|
+
async assertCurrent(scope) {
|
|
133
|
+
await stillSelected(s);
|
|
134
|
+
await s.assertTaskReady({ taskId: task.taskId, taskRevision: task.taskRevision });
|
|
135
|
+
await stillSelected(s);
|
|
136
|
+
if (scope) {
|
|
137
|
+
if (!s.assertExecutionScope) throw Error("trusted execution scope unavailable");
|
|
138
|
+
await s.assertExecutionScope({ taskId: task.taskId, taskRevision: task.taskRevision, ...scope });
|
|
139
|
+
await stillSelected(s);
|
|
140
|
+
await s.coordinator.assertPriorAttemptsSettled(task, request);
|
|
141
|
+
await stillSelected(s);
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
notAdmitted: () => s.coordinator.notAdmitted(request),
|
|
145
|
+
};
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
session.initiativeOperations = binding;
|
|
149
|
+
const off = pi.events.on(INITIATIVE_EVENT, (value: unknown) => {
|
|
150
|
+
const r = value as {
|
|
151
|
+
version?: number;
|
|
152
|
+
requestId?: string;
|
|
153
|
+
action?: string;
|
|
154
|
+
sessionId?: string;
|
|
155
|
+
cwd?: string;
|
|
156
|
+
initiativeId?: string;
|
|
157
|
+
expectedRevision?: string | null;
|
|
158
|
+
tasks?: InitiativeTask[];
|
|
159
|
+
assertSelection?: () => void | Promise<void>;
|
|
160
|
+
assertObservationAuthority?: () => void | Promise<void>;
|
|
161
|
+
assertExecutionScope?: (scope: {
|
|
162
|
+
taskId: string;
|
|
163
|
+
taskRevision: string;
|
|
164
|
+
cwd: string;
|
|
165
|
+
workspaceId: string | null;
|
|
166
|
+
}) => void | Promise<void>;
|
|
167
|
+
assertEffectReconciled?: (input: InitiativeEffectReconciliation) => void | Promise<void>;
|
|
168
|
+
assertTaskReady?: (task: { taskId: string; taskRevision: string }) => void | Promise<void>;
|
|
169
|
+
reply?: (value: unknown) => void;
|
|
170
|
+
};
|
|
171
|
+
if (
|
|
172
|
+
r?.version !== 1 ||
|
|
173
|
+
typeof r.requestId !== "string" ||
|
|
174
|
+
r.requestId.length > 512 ||
|
|
175
|
+
typeof r.reply !== "function"
|
|
176
|
+
)
|
|
177
|
+
return;
|
|
178
|
+
void (async () => {
|
|
179
|
+
const owner = currentOwner();
|
|
180
|
+
if (!owner || r.sessionId !== owner.sessionId || r.cwd !== owner.cwd) throw Error("initiative owner unavailable");
|
|
181
|
+
const envelope = {
|
|
182
|
+
version: 1,
|
|
183
|
+
requestId: r.requestId,
|
|
184
|
+
sessionId: owner.sessionId,
|
|
185
|
+
cwd: owner.cwd,
|
|
186
|
+
initiativeId: r.initiativeId,
|
|
187
|
+
};
|
|
188
|
+
if (r.action === "status") {
|
|
189
|
+
const snapshot = readInitiativeState(agentDir(), r.initiativeId!);
|
|
190
|
+
r.reply!({
|
|
191
|
+
...envelope,
|
|
192
|
+
ok: true,
|
|
193
|
+
revision: snapshot?.revision ?? null,
|
|
194
|
+
generation: snapshot?.record.generation ?? null,
|
|
195
|
+
state: snapshot?.record.state ?? "absent",
|
|
196
|
+
});
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (
|
|
200
|
+
!["acquire", "recover"].includes(r.action ?? "") ||
|
|
201
|
+
typeof r.assertSelection !== "function" ||
|
|
202
|
+
typeof r.assertTaskReady !== "function" ||
|
|
203
|
+
(r.assertObservationAuthority !== undefined && typeof r.assertObservationAuthority !== "function") ||
|
|
204
|
+
(r.action === "acquire" && typeof r.assertExecutionScope !== "function") ||
|
|
205
|
+
!Array.isArray(r.tasks) ||
|
|
206
|
+
!r.tasks.length ||
|
|
207
|
+
r.tasks.length > 256 ||
|
|
208
|
+
!r.tasks.every(validInitiativeTask) ||
|
|
209
|
+
new Set(r.tasks.map((t) => t.taskId)).size !== r.tasks.length ||
|
|
210
|
+
!(r.expectedRevision === null || typeof r.expectedRevision === "string")
|
|
211
|
+
)
|
|
212
|
+
throw Error("invalid selected initiative request");
|
|
213
|
+
if (selected) throw Error("release the current initiative before selecting another");
|
|
214
|
+
const epoch = ++selectionEpoch,
|
|
215
|
+
assertSelection = r.assertSelection;
|
|
216
|
+
const assertAuthority = async () => {
|
|
217
|
+
if (selectionEpoch !== epoch || currentOwner() !== owner) throw Error("initiative owner changed");
|
|
218
|
+
await assertSelection();
|
|
219
|
+
if (selectionEpoch !== epoch || currentOwner() !== owner) throw Error("initiative owner changed");
|
|
220
|
+
};
|
|
221
|
+
const observeAuthority = r.assertObservationAuthority ?? assertSelection;
|
|
222
|
+
const assertObservationAuthority = async () => {
|
|
223
|
+
if (selectionEpoch !== epoch || currentOwner() !== owner) throw Error("initiative owner changed");
|
|
224
|
+
await observeAuthority();
|
|
225
|
+
if (selectionEpoch !== epoch || currentOwner() !== owner) throw Error("initiative owner changed");
|
|
226
|
+
};
|
|
227
|
+
const coordinator = await acquireInitiativeCoordinator(
|
|
228
|
+
agentDir(),
|
|
229
|
+
{
|
|
230
|
+
initiativeId: r.initiativeId!,
|
|
231
|
+
owner,
|
|
232
|
+
expectedRevision: r.expectedRevision,
|
|
233
|
+
recoveryOnly: r.action === "recover",
|
|
234
|
+
},
|
|
235
|
+
{
|
|
236
|
+
assertAuthority,
|
|
237
|
+
assertObservationAuthority,
|
|
238
|
+
assertEffectReconciled:
|
|
239
|
+
typeof r.assertEffectReconciled === "function"
|
|
240
|
+
? async (input) => {
|
|
241
|
+
await assertObservationAuthority();
|
|
242
|
+
await r.assertEffectReconciled!(structuredClone(input));
|
|
243
|
+
await assertObservationAuthority();
|
|
244
|
+
}
|
|
245
|
+
: undefined,
|
|
246
|
+
async resolveAttempt(attempt) {
|
|
247
|
+
const original = openOperationArtifacts(agentDir(), attempt.owner).history(attempt.operationId, false);
|
|
248
|
+
if (!original) return attempt.state === "not-admitted" ? "not-started" : "unknown";
|
|
249
|
+
if (
|
|
250
|
+
original.operation.executionId !== attempt.executionId ||
|
|
251
|
+
original.requestDigest !== attempt.requestDigest
|
|
252
|
+
)
|
|
253
|
+
return "unknown";
|
|
254
|
+
if (
|
|
255
|
+
original.operation.state === "not-started" &&
|
|
256
|
+
original.operation.runtime?.cleanup?.state === "not-started"
|
|
257
|
+
)
|
|
258
|
+
return "not-started";
|
|
259
|
+
const live =
|
|
260
|
+
attempt.owner.sessionId === owner.sessionId && attempt.owner.cwd === owner.cwd
|
|
261
|
+
? await session.autoMode?.operations?.read(attempt.operationId)
|
|
262
|
+
: undefined;
|
|
263
|
+
if (live?.executionId === attempt.executionId && ["admitting", "running"].includes(live.state))
|
|
264
|
+
return "running";
|
|
265
|
+
try {
|
|
266
|
+
return await readRuntimeExecutionSettlement(
|
|
267
|
+
agentDir(),
|
|
268
|
+
attempt.owner.sessionId,
|
|
269
|
+
attempt.owner.cwd,
|
|
270
|
+
attempt.executionId,
|
|
271
|
+
);
|
|
272
|
+
} catch {
|
|
273
|
+
return "unknown";
|
|
274
|
+
}
|
|
275
|
+
},
|
|
276
|
+
},
|
|
277
|
+
);
|
|
278
|
+
try {
|
|
279
|
+
await assertAuthority();
|
|
280
|
+
} catch (error) {
|
|
281
|
+
await coordinator.close();
|
|
282
|
+
throw error;
|
|
283
|
+
}
|
|
284
|
+
const s = {
|
|
285
|
+
initiativeId: r.initiativeId!,
|
|
286
|
+
owner,
|
|
287
|
+
coordinator,
|
|
288
|
+
tasks: structuredClone(r.tasks),
|
|
289
|
+
dashboard: initiativeDashboard(coordinator, structuredClone(r.tasks)),
|
|
290
|
+
associations: new Map<string, InitiativeTask>(),
|
|
291
|
+
assertSelection,
|
|
292
|
+
assertTaskReady: r.assertTaskReady!,
|
|
293
|
+
assertExecutionScope: r.assertExecutionScope,
|
|
294
|
+
};
|
|
295
|
+
selected = s;
|
|
296
|
+
lastDisplay = { owner, dashboard: s.dashboard };
|
|
297
|
+
try {
|
|
298
|
+
await s.dashboard.refresh();
|
|
299
|
+
await stillSelected(s);
|
|
300
|
+
r.reply!({
|
|
301
|
+
...envelope,
|
|
302
|
+
ok: true,
|
|
303
|
+
generation: coordinator.generation,
|
|
304
|
+
recoveryOnly: coordinator.recoveryOnly,
|
|
305
|
+
revision: coordinator.revision(),
|
|
306
|
+
handle: {
|
|
307
|
+
recoveryOnly: coordinator.recoveryOnly,
|
|
308
|
+
pendingEffects: async () => {
|
|
309
|
+
await stillSelected(s);
|
|
310
|
+
return coordinator.pendingEffects();
|
|
311
|
+
},
|
|
312
|
+
reconcileEffect: async (input: InitiativeEffectReconciliation) => {
|
|
313
|
+
await stillSelected(s);
|
|
314
|
+
const result = await coordinator.reconcileEffect(input);
|
|
315
|
+
await s.dashboard.refresh();
|
|
316
|
+
return result;
|
|
317
|
+
},
|
|
318
|
+
associate: (input: InitiativeTaskIdentity & { operationId: string }) => associate(s, input),
|
|
319
|
+
observe: async (input: InitiativeTaskIdentity) => {
|
|
320
|
+
await stillObserved(s);
|
|
321
|
+
const task = s.tasks.find((t) => t.taskId === input.taskId && t.taskRevision === input.taskRevision);
|
|
322
|
+
if (!task) return { status: "unavailable" };
|
|
323
|
+
const revision = coordinator.revision();
|
|
324
|
+
try {
|
|
325
|
+
const observed = await coordinator.observe(input, task);
|
|
326
|
+
if (selected !== s || currentOwner() !== s.owner) throw Error("initiative selection changed");
|
|
327
|
+
s.dashboard.record(input, observed, revision);
|
|
328
|
+
await s.dashboard.refresh();
|
|
329
|
+
return observed;
|
|
330
|
+
} catch (error) {
|
|
331
|
+
s.dashboard.invalidate(input);
|
|
332
|
+
throw error;
|
|
333
|
+
}
|
|
334
|
+
},
|
|
335
|
+
async release() {
|
|
336
|
+
await stillSelected(s);
|
|
337
|
+
await coordinator.release();
|
|
338
|
+
if (selected === s) selected = undefined;
|
|
339
|
+
s.dashboard.close();
|
|
340
|
+
},
|
|
341
|
+
async close() {
|
|
342
|
+
s.dashboard.close();
|
|
343
|
+
if (selected === s) {
|
|
344
|
+
selected = undefined;
|
|
345
|
+
++selectionEpoch;
|
|
346
|
+
}
|
|
347
|
+
await coordinator.close();
|
|
348
|
+
},
|
|
349
|
+
},
|
|
350
|
+
});
|
|
351
|
+
} catch (error) {
|
|
352
|
+
s.dashboard.close();
|
|
353
|
+
if (selected === s) selected = undefined;
|
|
354
|
+
await coordinator.close();
|
|
355
|
+
throw error;
|
|
356
|
+
}
|
|
357
|
+
})().catch(() => {
|
|
358
|
+
try {
|
|
359
|
+
r.reply!({
|
|
360
|
+
version: 1,
|
|
361
|
+
requestId: r.requestId,
|
|
362
|
+
sessionId: r.sessionId,
|
|
363
|
+
cwd: r.cwd,
|
|
364
|
+
initiativeId: r.initiativeId,
|
|
365
|
+
ok: false,
|
|
366
|
+
reason: "initiative unavailable, conflicted, or reconciliation required",
|
|
367
|
+
});
|
|
368
|
+
} catch {}
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
if (session.mayDelegate)
|
|
372
|
+
pi.registerTool?.({
|
|
373
|
+
name: "initiative_task",
|
|
374
|
+
label: "Associate initiative task",
|
|
375
|
+
description:
|
|
376
|
+
"Associate one exact task from the human-selected work map with a planned operation_id. Does not dispatch or grant permissions.",
|
|
377
|
+
parameters: Type.Object({
|
|
378
|
+
taskId: Type.String({ maxLength: 256 }),
|
|
379
|
+
taskRevision: Type.String({ maxLength: 71 }),
|
|
380
|
+
operationId: Type.String({ maxLength: 160 }),
|
|
381
|
+
}),
|
|
382
|
+
async execute(_id, args) {
|
|
383
|
+
const s = selected;
|
|
384
|
+
if (!s) throw Error("No trusted initiative selected. Select it with the work-map owner first.");
|
|
385
|
+
const result = await associate(s, { ...args, initiativeId: s.initiativeId });
|
|
386
|
+
return { content: [{ type: "text" as const, text: JSON.stringify(result) }], details: result };
|
|
387
|
+
},
|
|
388
|
+
});
|
|
389
|
+
return {
|
|
390
|
+
async reset() {
|
|
391
|
+
++selectionEpoch;
|
|
392
|
+
const old = selected;
|
|
393
|
+
selected = undefined;
|
|
394
|
+
old?.dashboard.close();
|
|
395
|
+
await old?.coordinator.close();
|
|
396
|
+
},
|
|
397
|
+
async close() {
|
|
398
|
+
off?.();
|
|
399
|
+
++selectionEpoch;
|
|
400
|
+
const old = selected;
|
|
401
|
+
selected = undefined;
|
|
402
|
+
old?.dashboard.close();
|
|
403
|
+
await old?.coordinator.close();
|
|
404
|
+
},
|
|
405
|
+
};
|
|
406
|
+
}
|