pi-web-ui 0.116.0 → 0.117.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 +38 -4
- package/bin/device-hub-cli.mjs +26 -3
- package/bin/pi-web-ui.mjs +138 -70
- package/dist/server/agent-service-daemon.js +52 -6
- package/dist/server/agent-service.js +495 -141
- package/dist/server/canvas/canvas-workspace.js +18 -86
- package/dist/server/client-state.js +35 -6
- package/dist/server/compaction-markers.js +43 -24
- package/dist/server/dangling-tools.js +172 -16
- package/dist/server/device-center/bot-execution.js +48 -1
- package/dist/server/device-center/collab-service.js +26 -8
- package/dist/server/device-center/collab-tool.js +394 -18
- package/dist/server/device-center/connector.js +107 -37
- package/dist/server/device-center/delivery-authority.js +98 -0
- package/dist/server/device-center/delivery-subject.js +30 -0
- package/dist/server/device-center/device-integration.js +453 -183
- package/dist/server/device-center/execution-guides.js +15 -5
- package/dist/server/device-center/executor.js +47 -5
- package/dist/server/device-center/hub-conversations.js +14 -4
- package/dist/server/device-center/hub-deliveries.js +120 -15
- package/dist/server/device-center/hub-integration.js +655 -334
- package/dist/server/device-center/hub-page.js +5 -4
- package/dist/server/device-center/hub-setup.js +23 -1
- package/dist/server/device-center/hub-transaction-log.js +197 -0
- package/dist/server/device-center/hub.js +772 -354
- package/dist/server/device-center/integration-verification.js +113 -0
- package/dist/server/device-center/local-hub.js +6 -2
- package/dist/server/device-center/local-publication.js +423 -0
- package/dist/server/device-center/local-verification-job.js +97 -0
- package/dist/server/device-center/local-verification.js +274 -0
- package/dist/server/device-center/local.js +107 -12
- package/dist/server/device-center/main.js +4 -1
- package/dist/server/device-center/partition-log.js +62 -25
- package/dist/server/device-center/publication-journal.js +312 -0
- package/dist/server/device-center/publish-intent.js +379 -0
- package/dist/server/device-center/relay.js +134 -24
- package/dist/server/device-center/runtime.js +426 -42
- package/dist/server/device-center/store.js +225 -47
- package/dist/server/device-center/task-outcome.js +81 -0
- package/dist/server/device-center/task-recovery.js +603 -178
- package/dist/server/device-center/task-settlement.js +353 -0
- package/dist/server/device-center/task-workspace.js +443 -0
- package/dist/server/device-center/verification-limits.js +10 -0
- package/dist/server/device-center/work-contract.js +995 -0
- package/dist/server/dispatch-domain-handlers.js +15 -0
- package/dist/server/dsh/dsh-agent-service.js +9 -2
- package/dist/server/index.js +26 -1
- package/dist/server/patch-node-pty.js +3 -2
- package/dist/server/patch-remote-catalog.js +3 -2
- package/dist/server/preset-fields.js +17 -1
- package/dist/server/preset-share.js +1 -0
- package/dist/server/protocol-version.js +1 -1
- package/dist/server/quick-ask-settings.js +28 -0
- package/dist/server/quick-ask.js +164 -0
- package/dist/server/resolve-global-sdk.js +61 -11
- package/dist/server/scheduler-tasks.js +35 -8
- package/dist/server/session-list-scan.js +223 -0
- package/dist/server/session-open.js +93 -0
- package/dist/server/session-parser.js +4 -0
- package/dist/server/session-scan.js +90 -38
- package/dist/server/settings-service.js +9 -1
- package/dist/server/soft-cap.js +5 -0
- package/dist/server/transcript-prepare.js +73 -0
- package/dist/server/universal/drivers/claude-code.js +98 -34
- package/dist/server/universal/universal-session.js +4 -0
- package/dist/server/workspace-handle.js +428 -0
- package/docs/architecture-plugins.md +18 -5
- package/docs/device-center.md +30 -22
- package/package.json +3 -4
- package/plugin-sdk/README.md +16 -8
- package/plugin-sdk/client.d.ts +27 -0
- package/plugin-sdk/client.mjs +82 -0
- package/plugin-sdk/index.d.ts +10 -0
- package/plugin-sdk/index.mjs +4 -683
- package/plugin-sdk/package.json +15 -4
- package/plugin-sdk/testing.d.ts +10 -0
- package/plugin-sdk/testing.mjs +566 -0
- package/themes/flow-cristallo.css +168 -0
- package/themes/flow-ink.css +169 -0
- package/themes/flow-obsidian.css +169 -0
- package/themes/flow-zurich.css +169 -0
- package/web/dist/.build-assets.json +1 -1
- package/web/dist/assets/{BgTasksModal-BDoOpj-F.js → BgTasksModal-BjNRIUIc.js} +1 -1
- package/web/dist/assets/{BgTasksModal-C0i3O1FT.js → BgTasksModal-C0zuU8FE.js} +1 -1
- package/web/dist/assets/{CanvasStudio-B8thymBN.js → CanvasStudio-BWvdmJ0M.js} +3 -3
- package/web/dist/assets/{CanvasStudio-zSxyIx1R.js → CanvasStudio-VQFjmTGq.js} +3 -3
- package/web/dist/assets/DeviceCenter-DI9_0aBB.js +3 -0
- package/web/dist/assets/DeviceCenter-DsNtXqzY.js +3 -0
- package/web/dist/assets/{DeviceExecutionSettings-q5Ik8oXg.js → DeviceExecutionSettings-BLP1Su5G.js} +1 -1
- package/web/dist/assets/DeviceGroups-2Pvk0Cds.js +1 -0
- package/web/dist/assets/DeviceGroups-CP7CcyPD.js +1 -0
- package/web/dist/assets/{Dropdown-C4LIhshN.js → Dropdown-CQtstPGv.js} +1 -1
- package/web/dist/assets/FilePreview-BMB2tNyT.js +2 -0
- package/web/dist/assets/FilePreview-RGFQUbJo.js +2 -0
- package/web/dist/assets/{GlobalSearchModal-BWgU8yPd.js → GlobalSearchModal-BM5wxiqU.js} +1 -1
- package/web/dist/assets/{GlobalSearchModal-F7fNimKk.js → GlobalSearchModal-DGv_WINq.js} +1 -1
- package/web/dist/assets/GroupConversation-B7SnuXj1.js +61 -0
- package/web/dist/assets/GroupConversation-Dda69q-2.js +61 -0
- package/web/dist/assets/{HintTip-c6d0TJoh.js → HintTip-ChTW8o1m.js} +1 -1
- package/web/dist/assets/ModelConfigModal-C-0Z4hUZ.js +2 -0
- package/web/dist/assets/ModelConfigModal-yqeadyab.js +2 -0
- package/web/dist/assets/{PiSetupModal-BmMrrgB9.js → PiSetupModal-BX5rUuoI.js} +1 -1
- package/web/dist/assets/{ProviderOAuthControls-BGSBIKD7.js → ProviderOAuthControls-DY4SCfXg.js} +1 -1
- package/web/dist/assets/SCMPanel-C4KbQvvK.js +2 -0
- package/web/dist/assets/SCMPanel-DPAyY2sW.js +2 -0
- package/web/dist/assets/SettingsModal-DxZp6D_g.js +32 -0
- package/web/dist/assets/SettingsModal-WhQzaOPt.js +32 -0
- package/web/dist/assets/{TerminalPanel-DDavKvFr.js → TerminalPanel-D4By_RVc.js} +2 -2
- package/web/dist/assets/{TerminalPanel-0CYvD72B.js → TerminalPanel-lZw8-i3j.js} +2 -2
- package/web/dist/assets/{TuiOverlayModal-BsA-QTvN.js → TuiOverlayModal-C3pAVuC8.js} +1 -1
- package/web/dist/assets/{TuiOverlayModal-DzoVQBuG.js → TuiOverlayModal-D7_rEXcJ.js} +1 -1
- package/web/dist/assets/app-globals-KqOVtmp1.js +123 -0
- package/web/dist/assets/{canvas-client-CUnXlQYX.js → canvas-client-BY6ojmle.js} +1 -1
- package/web/dist/assets/{desktop-aAKpvKJV.js → desktop-CV6KKp9R.js} +1 -1
- package/web/dist/assets/index-BawF7ZP5.js +397 -0
- package/web/dist/assets/index-BoGCoq5X.js +397 -0
- package/web/dist/assets/index-CVW9FoY2.css +1 -0
- package/web/dist/assets/{plugin-host-BRWib9DF.js → plugin-host-Dn8Dd6SC.js} +1 -1
- package/web/dist/assets/{remote-ssh-client-CfYSMEZg.js → remote-ssh-client-Dzi-eEyE.js} +1 -1
- package/web/dist/assets/{theme-D2l_mLNo.js → theme-D_bihpdN.js} +1 -1
- package/web/dist/index.html +11 -11
- package/web/dist/sw.js +1 -1
- package/dist/gh-comment-781.md +0 -5
- package/dist/server/device-center/local-page.js +0 -17
- package/dist/server/device-center/smoke-preview.js +0 -3
- package/dist/server/durable/durable-capabilities.js +0 -11
- package/dist/server/markers/builtins/services.js +0 -123
- package/dist/server/markers/builtins/todo.js +0 -189
- package/dist/server/markers-list-tool.js +0 -34
- package/dist/server/orchestrator.js +0 -109
- package/dist/server/patch-turn-end-boundary.js +0 -77
- package/dist/server/system-prompt-soul.js +0 -41
- package/dist/server/univer-service.js +0 -448
- package/dist/server/universal/drivers/dsh-driver.js +0 -379
- package/dist/test-artifact.md +0 -9
- package/dist/test-preview-modal.md +0 -11
- package/dist/test-report.html +0 -20
- package/web/dist/assets/DeviceCenter-DfAHKcid.js +0 -3
- package/web/dist/assets/DeviceCenter-ydvOTlzE.js +0 -3
- package/web/dist/assets/DeviceGroups-CI14_SRb.js +0 -1
- package/web/dist/assets/DeviceGroups-DvsB5IEU.js +0 -1
- package/web/dist/assets/FilePreview-BgGongmv.js +0 -2
- package/web/dist/assets/FilePreview-D4NrhF-U.js +0 -2
- package/web/dist/assets/GroupConversation-Dh71Pdb-.js +0 -17
- package/web/dist/assets/GroupConversation-ZK3nRmNm.js +0 -17
- package/web/dist/assets/ModelConfigModal-YVEgyy34.js +0 -2
- package/web/dist/assets/ModelConfigModal-vWtg3srp.js +0 -2
- package/web/dist/assets/SCMPanel-B8_EDxNr.js +0 -2
- package/web/dist/assets/SCMPanel-BU58qk3T.js +0 -2
- package/web/dist/assets/SettingsModal-CMUybq3J.js +0 -32
- package/web/dist/assets/SettingsModal-OBmGTh-5.js +0 -32
- package/web/dist/assets/app-globals-CZxzGvIo.js +0 -123
- package/web/dist/assets/index-C6DwztqE.css +0 -1
- package/web/dist/assets/index-CuN802Sj.js +0 -93
- package/web/dist/assets/index-EH5Fw9fa.js +0 -93
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
import { applyBinaryPatch, canonicalPath, checkSignal, computeCanonicalRepositoryId, createDetachedWorktree, git, inside, removeDetachedWorktree, safeId, snapshotTree, verifyPatchProducesTree, verifyRepoRoot, verifyWorktreeIdentity, } from "../workspace-handle.js";
|
|
5
|
+
import { computeArtifactDigest, deepFreeze, validateContextManifest, validateDeviceArtifact, validateInputArtifacts, validateWorkContract, } from "./work-contract.js";
|
|
6
|
+
import { validateDeviceProject } from "./workspaces.js";
|
|
7
|
+
/** Maximum allowed raw bytes for artifact patches (128 KiB). */
|
|
8
|
+
export const MAX_TASK_ARTIFACT_PATCH_BYTES = 128 * 1024;
|
|
9
|
+
/** In-memory private registration table preventing forged JSON or unverified paths */
|
|
10
|
+
const workspaceRegistry = new Map();
|
|
11
|
+
const preparingBindings = new Set();
|
|
12
|
+
const trustedCapabilities = new WeakMap();
|
|
13
|
+
function bindingKey(binding) {
|
|
14
|
+
return createHash("sha256")
|
|
15
|
+
.update(JSON.stringify([binding.deviceId, binding.botId, binding.botRevision, binding.taskId, binding.attemptId]))
|
|
16
|
+
.digest("hex");
|
|
17
|
+
}
|
|
18
|
+
/** Only capabilities issued by this module and still registered are trusted. Never duck-type resolve(). */
|
|
19
|
+
export function isTrustedTaskWorkspaceCapability(value) {
|
|
20
|
+
if (!value || typeof value !== "object")
|
|
21
|
+
return false;
|
|
22
|
+
const trusted = trustedCapabilities.get(value);
|
|
23
|
+
return !!trusted && workspaceRegistry.has(trusted.handleId);
|
|
24
|
+
}
|
|
25
|
+
export async function resolveTrustedTaskWorkspace(capability, binding) {
|
|
26
|
+
if (!capability || typeof capability !== "object")
|
|
27
|
+
throw new Error("Untrusted task workspace capability");
|
|
28
|
+
const trusted = trustedCapabilities.get(capability);
|
|
29
|
+
if (!trusted)
|
|
30
|
+
throw new Error("Untrusted task workspace capability");
|
|
31
|
+
return resolveTaskWorkspace(trusted.handleId, binding);
|
|
32
|
+
}
|
|
33
|
+
function getRegisteredRecord(handle, binding) {
|
|
34
|
+
const handleId = typeof handle === "string" ? handle.trim() : handle?.handleId?.trim();
|
|
35
|
+
if (!handleId) {
|
|
36
|
+
throw new Error("Invalid task workspace handle: missing handleId");
|
|
37
|
+
}
|
|
38
|
+
const record = workspaceRegistry.get(handleId);
|
|
39
|
+
if (!record) {
|
|
40
|
+
throw new Error(`Unregistered or expired task workspace handle: ${handleId}`);
|
|
41
|
+
}
|
|
42
|
+
if (binding.deviceId !== record.deviceId ||
|
|
43
|
+
binding.botId !== record.botId ||
|
|
44
|
+
binding.botRevision !== record.botRevision ||
|
|
45
|
+
binding.taskId !== record.taskId ||
|
|
46
|
+
binding.attemptId !== record.attemptId) {
|
|
47
|
+
throw new Error(`Task workspace binding mismatch for handle ${handleId}: caller binding does not match registered capability`);
|
|
48
|
+
}
|
|
49
|
+
return record;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Prepares an isolated Git worktree for device bot task execution.
|
|
53
|
+
* - Verifies repoRoot is clean and matches baseline with strict repositoryId check
|
|
54
|
+
* - Binds contract & contextManifest
|
|
55
|
+
* - Validates input artifacts with scratch index to prove outputTree === patch applied to base
|
|
56
|
+
* - Composes non-empty inputArtifacts safely in ordered sequence
|
|
57
|
+
* - Detaches HEAD and records canonical worktree + gitdir in private registry
|
|
58
|
+
* - Entire flow wrapped in try/catch to ensure unmanaged worktrees are cleaned up on abort/failure
|
|
59
|
+
*/
|
|
60
|
+
export async function prepareTaskWorkspace(options) {
|
|
61
|
+
checkSignal(options.signal);
|
|
62
|
+
options = { ...options };
|
|
63
|
+
const key = bindingKey(options);
|
|
64
|
+
if (preparingBindings.has(key) || [...workspaceRegistry.values()].some((record) => record.bindingKey === key)) {
|
|
65
|
+
throw new Error("Task workspace binding is already preparing or registered");
|
|
66
|
+
}
|
|
67
|
+
preparingBindings.add(key);
|
|
68
|
+
try {
|
|
69
|
+
return await prepareRegisteredWorkspace(options, key);
|
|
70
|
+
}
|
|
71
|
+
finally {
|
|
72
|
+
preparingBindings.delete(key);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
async function prepareRegisteredWorkspace(options, key) {
|
|
76
|
+
const deviceId = safeId(options.deviceId, "deviceId");
|
|
77
|
+
const botId = safeId(options.botId, "botId");
|
|
78
|
+
const taskId = safeId(options.taskId, "taskId");
|
|
79
|
+
const attemptId = safeId(options.attemptId, "attemptId");
|
|
80
|
+
if (!Number.isSafeInteger(options.botRevision) || options.botRevision < 1) {
|
|
81
|
+
throw new Error(`Invalid botRevision: ${options.botRevision}`);
|
|
82
|
+
}
|
|
83
|
+
const contractCheck = validateWorkContract(deepFreeze(structuredClone(options.contract)));
|
|
84
|
+
if (!contractCheck.valid) {
|
|
85
|
+
throw new Error(`Invalid task contract: ${contractCheck.reason}`);
|
|
86
|
+
}
|
|
87
|
+
const contract = contractCheck.contract;
|
|
88
|
+
const manifestCheck = validateContextManifest(deepFreeze(structuredClone(options.contextManifest)), contract);
|
|
89
|
+
if (!manifestCheck.valid) {
|
|
90
|
+
throw new Error(`Invalid context manifest: ${manifestCheck.reason}`);
|
|
91
|
+
}
|
|
92
|
+
const contextManifest = manifestCheck.manifest;
|
|
93
|
+
// Validate repository admission and verify it is a valid Git repo
|
|
94
|
+
const validatedProjectRoot = await validateDeviceProject(options.repoRoot, options.dataDir);
|
|
95
|
+
const { repoRoot } = await verifyRepoRoot(validatedProjectRoot, options.signal);
|
|
96
|
+
if (validatedProjectRoot !== repoRoot) {
|
|
97
|
+
throw new Error("Task code workspace requires Bot.cwd at the Git repository root; subdirectory code workspaces are not supported");
|
|
98
|
+
}
|
|
99
|
+
// Ensure clean baseline in repoRoot before worktree creation
|
|
100
|
+
const baselineStatus = (await git(repoRoot, ["status", "--porcelain", "--untracked-files=all"], options.signal)).trim();
|
|
101
|
+
if (baselineStatus.length > 0) {
|
|
102
|
+
throw new Error("Device task workspace requires a clean baseline repository; commit or stash changes first");
|
|
103
|
+
}
|
|
104
|
+
// Determine and verify transportable identity even for callers without a baseline.
|
|
105
|
+
const canonicalRepoId = await computeCanonicalRepositoryId(repoRoot, options.signal);
|
|
106
|
+
let baseCommit;
|
|
107
|
+
if (contract.baseline) {
|
|
108
|
+
const candidateCommit = contract.baseline.commit.trim().toLowerCase();
|
|
109
|
+
try {
|
|
110
|
+
baseCommit = (await git(repoRoot, ["rev-parse", "--verify", `${candidateCommit}^{commit}`], options.signal)).trim();
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
checkSignal(options.signal);
|
|
114
|
+
throw new Error(`Baseline commit ${candidateCommit} does not exist in repository ${repoRoot}`, {
|
|
115
|
+
cause: error,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
// Strictly verify repository identity against canonical repository identity (no aliases)
|
|
119
|
+
const expectedRepoId = contract.baseline.repositoryId.trim();
|
|
120
|
+
if (expectedRepoId !== canonicalRepoId) {
|
|
121
|
+
throw new Error(`Repository identity mismatch: baseline specifies '${expectedRepoId}', local repository is '${canonicalRepoId}'`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
baseCommit = (await git(repoRoot, ["rev-parse", "HEAD"], options.signal)).trim();
|
|
126
|
+
}
|
|
127
|
+
// Both the binding hash and a fresh handle are part of every owned path/ref.
|
|
128
|
+
const handleId = randomUUID();
|
|
129
|
+
const refNamespace = `${key}/${handleId}`;
|
|
130
|
+
const resolvedDataDir = resolve(options.dataDir);
|
|
131
|
+
const worktreeDir = join(resolvedDataDir, "task-workspaces", handleId);
|
|
132
|
+
if (!inside(resolvedDataDir, worktreeDir)) {
|
|
133
|
+
throw new Error("Task workspace directory escapes dataDir");
|
|
134
|
+
}
|
|
135
|
+
if (inside(repoRoot, worktreeDir)) {
|
|
136
|
+
throw new Error("Task workspace dataDir must be outside the baseline repository");
|
|
137
|
+
}
|
|
138
|
+
if (existsSync(worktreeDir)) {
|
|
139
|
+
throw new Error(`Task workspace directory already exists: ${worktreeDir}; refusing overwrite`);
|
|
140
|
+
}
|
|
141
|
+
let creationAttempted = false;
|
|
142
|
+
let workspaceCommit;
|
|
143
|
+
const workspaceRef = `refs/pi-tasks/workspaces/${refNamespace}`;
|
|
144
|
+
const artifactRef = `refs/pi-tasks/artifacts/${refNamespace}`;
|
|
145
|
+
try {
|
|
146
|
+
// Create detached worktree
|
|
147
|
+
creationAttempted = true;
|
|
148
|
+
await createDetachedWorktree(repoRoot, worktreeDir, baseCommit, options.signal);
|
|
149
|
+
const identity = await verifyWorktreeIdentity(worktreeDir, repoRoot, options.signal);
|
|
150
|
+
const canonicalWorktreePath = identity.toplevel;
|
|
151
|
+
const canonicalGitDir = identity.gitDir;
|
|
152
|
+
let inputTree = (await git(repoRoot, ["rev-parse", `${baseCommit}^{tree}`], options.signal)).trim();
|
|
153
|
+
// Process inputArtifacts (safe ordered composition with independent outputTree verification)
|
|
154
|
+
if (contract.inputArtifacts && contract.inputArtifacts.length > 0) {
|
|
155
|
+
const synthCheck = validateInputArtifacts(contract.inputArtifacts, contract.baseline, contract.executionMode);
|
|
156
|
+
if (!synthCheck.valid) {
|
|
157
|
+
throw new Error(`Invalid contract inputArtifacts: ${synthCheck.reason}`);
|
|
158
|
+
}
|
|
159
|
+
const touched = new Set();
|
|
160
|
+
for (const artifact of contract.inputArtifacts) {
|
|
161
|
+
if (!artifact.baseline || !artifact.outputTree) {
|
|
162
|
+
throw new Error(`Input artifact ${artifact.id} requires baseline and outputTree proof`);
|
|
163
|
+
}
|
|
164
|
+
if (artifact.baseline.repositoryId !== canonicalRepoId ||
|
|
165
|
+
artifact.baseline.commit.toLowerCase() !== baseCommit) {
|
|
166
|
+
throw new Error(`Input artifact ${artifact.id} baseline mismatch`);
|
|
167
|
+
}
|
|
168
|
+
if (artifact.patchBase64 === undefined) {
|
|
169
|
+
throw new Error(`Input artifact ${artifact.id} has no patchBase64`);
|
|
170
|
+
}
|
|
171
|
+
const patchBytes = Buffer.from(artifact.patchBase64, "base64");
|
|
172
|
+
if (patchBytes.length > MAX_TASK_ARTIFACT_PATCH_BYTES) {
|
|
173
|
+
throw new Error(`Input artifact ${artifact.id} patch exceeds size limit of 128 KiB`);
|
|
174
|
+
}
|
|
175
|
+
const computedPatchSha256 = createHash("sha256").update(patchBytes).digest("hex");
|
|
176
|
+
if (computedPatchSha256.toLowerCase() !== artifact.patchSha256?.toLowerCase()) {
|
|
177
|
+
throw new Error(`Input artifact ${artifact.id} patchSha256 checksum mismatch`);
|
|
178
|
+
}
|
|
179
|
+
// Independent proof: verify using scratch index that applying patchBytes to baseCommit yields artifact.outputTree
|
|
180
|
+
await verifyPatchProducesTree(repoRoot, baseCommit, patchBytes, artifact.outputTree, resolvedDataDir, options.signal);
|
|
181
|
+
const paths = (await git(repoRoot, [
|
|
182
|
+
"diff",
|
|
183
|
+
"--no-ext-diff",
|
|
184
|
+
"--no-textconv",
|
|
185
|
+
"--no-renames",
|
|
186
|
+
"--name-only",
|
|
187
|
+
"-z",
|
|
188
|
+
baseCommit,
|
|
189
|
+
artifact.outputTree,
|
|
190
|
+
"--",
|
|
191
|
+
], options.signal))
|
|
192
|
+
.split("\0")
|
|
193
|
+
.filter(Boolean);
|
|
194
|
+
for (const path of paths) {
|
|
195
|
+
if ([...touched].some((other) => path === other || path.startsWith(`${other}/`) || other.startsWith(`${path}/`))) {
|
|
196
|
+
throw new Error(`Input artifact conflict: overlapping or ancestor input at ${path}; cumulative layers are not deduplicated`);
|
|
197
|
+
}
|
|
198
|
+
touched.add(path);
|
|
199
|
+
}
|
|
200
|
+
// Apply patch to the isolated worktree for ordered composition
|
|
201
|
+
await applyBinaryPatch(worktreeDir, patchBytes, options.signal);
|
|
202
|
+
}
|
|
203
|
+
// Snapshot composite inputTree after applying all input artifacts
|
|
204
|
+
inputTree = await snapshotTree(worktreeDir, options.dataDir, options.signal, join("task-workspaces", "input-indexes"));
|
|
205
|
+
}
|
|
206
|
+
// A commit retains both the composite input tree and the original baseline.
|
|
207
|
+
workspaceCommit = (await git(repoRoot, ["commit-tree", inputTree, "-p", baseCommit, "-m", "pi-task-workspace"], options.signal)).trim();
|
|
208
|
+
checkSignal(options.signal);
|
|
209
|
+
// Publication is one non-cancellable CAS; cleanup knows its exact expected value.
|
|
210
|
+
await git(repoRoot, ["update-ref", workspaceRef, workspaceCommit, "0".repeat(baseCommit.length)]);
|
|
211
|
+
const record = {
|
|
212
|
+
handleId,
|
|
213
|
+
deviceId,
|
|
214
|
+
botId,
|
|
215
|
+
botRevision: options.botRevision,
|
|
216
|
+
taskId,
|
|
217
|
+
attemptId,
|
|
218
|
+
repoRoot,
|
|
219
|
+
worktreePath: worktreeDir,
|
|
220
|
+
canonicalWorktreePath,
|
|
221
|
+
canonicalGitDir,
|
|
222
|
+
canonicalCommonDir: identity.commonDir,
|
|
223
|
+
bindingKey: key,
|
|
224
|
+
workspaceRef,
|
|
225
|
+
workspaceCommit,
|
|
226
|
+
artifactRef,
|
|
227
|
+
repositoryId: canonicalRepoId,
|
|
228
|
+
baseCommit,
|
|
229
|
+
contract,
|
|
230
|
+
contextManifest,
|
|
231
|
+
dataDir: resolvedDataDir,
|
|
232
|
+
inputTree,
|
|
233
|
+
createdAt: Date.now(),
|
|
234
|
+
};
|
|
235
|
+
workspaceRegistry.set(handleId, record);
|
|
236
|
+
return Object.freeze({ handleId, deviceId, botId, taskId, attemptId });
|
|
237
|
+
}
|
|
238
|
+
catch (error) {
|
|
239
|
+
// An abort can land after `worktree add` completed. Verify ownership on disk
|
|
240
|
+
// before removing anything, without reusing the aborted signal.
|
|
241
|
+
const failures = [];
|
|
242
|
+
if (creationAttempted && existsSync(worktreeDir)) {
|
|
243
|
+
try {
|
|
244
|
+
const identity = await verifyWorktreeIdentity(worktreeDir, repoRoot);
|
|
245
|
+
if (identity.toplevel !== worktreeDir || identity.headCommit !== baseCommit)
|
|
246
|
+
throw new Error("Failed workspace ownership cannot be verified; retained for inspection");
|
|
247
|
+
await removeDetachedWorktree(repoRoot, worktreeDir, { force: true });
|
|
248
|
+
}
|
|
249
|
+
catch (cleanupError) {
|
|
250
|
+
failures.push(cleanupError);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
if (workspaceCommit) {
|
|
254
|
+
try {
|
|
255
|
+
await git(repoRoot, ["update-ref", "-d", workspaceRef, workspaceCommit]);
|
|
256
|
+
}
|
|
257
|
+
catch (cleanupError) {
|
|
258
|
+
failures.push(cleanupError);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
if (failures.length)
|
|
262
|
+
throw new AggregateError([error, ...failures], "Workspace preparation failed; cleanup incomplete, retained for inspection", { cause: error });
|
|
263
|
+
throw error;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Resolves a previously registered workspace handle against caller binding and on-disk verification.
|
|
268
|
+
*/
|
|
269
|
+
export async function resolveTaskWorkspace(handle, binding) {
|
|
270
|
+
const record = getRegisteredRecord(handle, binding);
|
|
271
|
+
if (record.closing)
|
|
272
|
+
throw new Error("Task workspace cleanup is in progress");
|
|
273
|
+
const canonicalCurrent = await canonicalPath(record.worktreePath);
|
|
274
|
+
if (canonicalCurrent !== record.canonicalWorktreePath) {
|
|
275
|
+
throw new Error(`Task workspace path redirect detected: expected ${record.canonicalWorktreePath}, resolved to ${canonicalCurrent}`);
|
|
276
|
+
}
|
|
277
|
+
await verifyWorktreeIdentity(record.worktreePath, record.repoRoot, undefined, record.canonicalGitDir, record.canonicalCommonDir);
|
|
278
|
+
return {
|
|
279
|
+
cwd: record.worktreePath,
|
|
280
|
+
repoRoot: record.repoRoot,
|
|
281
|
+
worktreePath: record.worktreePath,
|
|
282
|
+
baseCommit: record.baseCommit,
|
|
283
|
+
contractDigest: record.contract.digest,
|
|
284
|
+
manifestDigest: record.contextManifest.digest,
|
|
285
|
+
inputTree: record.inputTree,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Returns an execution capability wrapper around a verified task workspace.
|
|
290
|
+
*/
|
|
291
|
+
export function getTaskWorkspaceCapability(handle, binding) {
|
|
292
|
+
const record = getRegisteredRecord(handle, binding);
|
|
293
|
+
const registeredBinding = Object.freeze({
|
|
294
|
+
deviceId: record.deviceId,
|
|
295
|
+
botId: record.botId,
|
|
296
|
+
botRevision: record.botRevision,
|
|
297
|
+
taskId: record.taskId,
|
|
298
|
+
attemptId: record.attemptId,
|
|
299
|
+
});
|
|
300
|
+
const capability = Object.freeze({
|
|
301
|
+
handleId: record.handleId,
|
|
302
|
+
binding: registeredBinding,
|
|
303
|
+
resolve: () => resolveTaskWorkspace(record.handleId, registeredBinding),
|
|
304
|
+
getCwd: async () => (await resolveTaskWorkspace(record.handleId, registeredBinding)).cwd,
|
|
305
|
+
getRepoRoot: async () => (await resolveTaskWorkspace(record.handleId, registeredBinding)).repoRoot,
|
|
306
|
+
captureArtifact: (signal) => captureTaskArtifact(record.handleId, registeredBinding, signal),
|
|
307
|
+
cleanup: (options) => cleanupTaskWorkspace(record.handleId, registeredBinding, options),
|
|
308
|
+
});
|
|
309
|
+
trustedCapabilities.set(capability, { handleId: record.handleId, binding: registeredBinding });
|
|
310
|
+
return capability;
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Captures task output artifact:
|
|
314
|
+
* - If already captured, returns the cached immutable artifact
|
|
315
|
+
* - Generates snapshot outputTree without altering user index
|
|
316
|
+
* - Produces cumulative binary patch from baseCommit to outputTree (bounded <=128 KiB raw)
|
|
317
|
+
* - Retains inputTree for audit
|
|
318
|
+
* - Anchors git object GC retention ref
|
|
319
|
+
*/
|
|
320
|
+
export async function captureTaskArtifact(handle, binding, signal) {
|
|
321
|
+
checkSignal(signal);
|
|
322
|
+
const record = getRegisteredRecord(handle, binding);
|
|
323
|
+
if (record.closing)
|
|
324
|
+
throw new Error("Task workspace cleanup is in progress");
|
|
325
|
+
if (record.artifactCaptured)
|
|
326
|
+
return record.artifactCaptured;
|
|
327
|
+
if (!record.captureFlight) {
|
|
328
|
+
record.captureFlight = captureRegisteredArtifact(record, signal).finally(() => {
|
|
329
|
+
record.captureFlight = undefined;
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
const artifact = await record.captureFlight;
|
|
333
|
+
checkSignal(signal);
|
|
334
|
+
return artifact;
|
|
335
|
+
}
|
|
336
|
+
async function captureRegisteredArtifact(record, signal) {
|
|
337
|
+
const canonicalCurrent = await canonicalPath(record.worktreePath);
|
|
338
|
+
if (canonicalCurrent !== record.canonicalWorktreePath) {
|
|
339
|
+
throw new Error(`Task workspace path redirect detected during capture: expected ${record.canonicalWorktreePath}, resolved to ${canonicalCurrent}`);
|
|
340
|
+
}
|
|
341
|
+
await verifyWorktreeIdentity(record.worktreePath, record.repoRoot, signal, record.canonicalGitDir, record.canonicalCommonDir);
|
|
342
|
+
const outputTree = await snapshotTree(record.worktreePath, record.dataDir, signal, join("task-workspaces", "output-indexes"));
|
|
343
|
+
await verifyWorktreeIdentity(record.worktreePath, record.repoRoot, signal, record.canonicalGitDir, record.canonicalCommonDir);
|
|
344
|
+
// Cumulative patch semantics: diff from baseCommit^{tree} to outputTree
|
|
345
|
+
const baseTree = (await git(record.repoRoot, ["rev-parse", `${record.baseCommit}^{tree}`], signal)).trim();
|
|
346
|
+
const patchStr = await git(record.worktreePath, ["diff", "--binary", "--full-index", "--no-ext-diff", "--no-textconv", baseTree, outputTree, "--"], signal);
|
|
347
|
+
const patchBytes = Buffer.from(patchStr, "utf8");
|
|
348
|
+
if (patchBytes.length > MAX_TASK_ARTIFACT_PATCH_BYTES) {
|
|
349
|
+
throw new Error(`Artifact patch size (${patchBytes.length} bytes) exceeds maximum limit of ${MAX_TASK_ARTIFACT_PATCH_BYTES} bytes (128 KiB)`);
|
|
350
|
+
}
|
|
351
|
+
const patchSha256 = createHash("sha256").update(patchBytes).digest("hex");
|
|
352
|
+
const patchBase64 = patchBytes.toString("base64");
|
|
353
|
+
// Git object GC retention commit and ref
|
|
354
|
+
const retentionCommit = (await git(record.repoRoot, [
|
|
355
|
+
"commit-tree",
|
|
356
|
+
outputTree,
|
|
357
|
+
"-p",
|
|
358
|
+
record.workspaceCommit,
|
|
359
|
+
"-m",
|
|
360
|
+
`pi-task-artifact: task=${record.taskId} attempt=${record.attemptId}`,
|
|
361
|
+
], signal)).trim();
|
|
362
|
+
const artifact = {
|
|
363
|
+
id: `artifact-${randomUUID()}`,
|
|
364
|
+
digest: "",
|
|
365
|
+
producer: {
|
|
366
|
+
deviceId: record.deviceId,
|
|
367
|
+
botId: record.botId,
|
|
368
|
+
taskId: record.taskId,
|
|
369
|
+
attemptId: record.attemptId,
|
|
370
|
+
contractDigest: record.contract.digest,
|
|
371
|
+
manifestDigest: record.contextManifest.digest,
|
|
372
|
+
},
|
|
373
|
+
baseline: { repositoryId: record.repositoryId, commit: record.baseCommit },
|
|
374
|
+
inputTree: record.inputTree,
|
|
375
|
+
outputTree,
|
|
376
|
+
patchBase64,
|
|
377
|
+
patchSha256,
|
|
378
|
+
createdAt: Date.now(),
|
|
379
|
+
};
|
|
380
|
+
artifact.digest = computeArtifactDigest(artifact);
|
|
381
|
+
const validated = validateDeviceArtifact(artifact);
|
|
382
|
+
if (!validated.valid) {
|
|
383
|
+
throw new Error(`Constructed invalid artifact: ${validated.reason}`);
|
|
384
|
+
}
|
|
385
|
+
const immutable = deepFreeze(structuredClone(validated.artifact));
|
|
386
|
+
checkSignal(signal);
|
|
387
|
+
await git(record.repoRoot, ["update-ref", record.artifactRef, retentionCommit, "0".repeat(record.baseCommit.length)]);
|
|
388
|
+
record.artifactCaptured = immutable;
|
|
389
|
+
return immutable;
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Conservative task workspace cleanup:
|
|
393
|
+
* - Refuses deletion if dirty uncommitted changes or additional commits exist (unless force=true)
|
|
394
|
+
* - Removes worktree cleanly and clears in-memory handle
|
|
395
|
+
*/
|
|
396
|
+
export async function cleanupTaskWorkspace(handle, binding, options = {}) {
|
|
397
|
+
checkSignal(options.signal);
|
|
398
|
+
const record = getRegisteredRecord(handle, binding);
|
|
399
|
+
if (!record.cleanupFlight) {
|
|
400
|
+
record.closing = true;
|
|
401
|
+
record.cleanupFlight = cleanupRegisteredWorkspace(record, options).finally(() => {
|
|
402
|
+
record.closing = false;
|
|
403
|
+
record.cleanupFlight = undefined;
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
return record.cleanupFlight;
|
|
407
|
+
}
|
|
408
|
+
async function cleanupRegisteredWorkspace(record, options) {
|
|
409
|
+
await record.captureFlight;
|
|
410
|
+
checkSignal(options.signal);
|
|
411
|
+
const canonicalCurrent = await canonicalPath(record.worktreePath);
|
|
412
|
+
if (canonicalCurrent !== record.canonicalWorktreePath) {
|
|
413
|
+
throw new Error(`Task workspace path redirect detected during cleanup: expected ${record.canonicalWorktreePath}, resolved to ${canonicalCurrent}`);
|
|
414
|
+
}
|
|
415
|
+
await verifyWorktreeIdentity(record.worktreePath, record.repoRoot, options.signal, record.canonicalGitDir, record.canonicalCommonDir);
|
|
416
|
+
if (!options.force) {
|
|
417
|
+
const head = (await git(record.worktreePath, ["rev-parse", "HEAD"], options.signal)).trim();
|
|
418
|
+
if (head !== record.baseCommit) {
|
|
419
|
+
throw new Error("Task workspace has additional commits; retained for inspection");
|
|
420
|
+
}
|
|
421
|
+
const status = (await git(record.worktreePath, ["status", "--porcelain", "--untracked-files=all"], options.signal)).trim();
|
|
422
|
+
if (status.length > 0) {
|
|
423
|
+
throw new Error("Task workspace has uncommitted changes; retained for inspection");
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
checkSignal(options.signal);
|
|
427
|
+
// Once deletion begins, finish bookkeeping without an abort between disk and registry.
|
|
428
|
+
await removeDetachedWorktree(record.repoRoot, record.worktreePath, { force: options.force });
|
|
429
|
+
try {
|
|
430
|
+
await git(record.repoRoot, ["update-ref", "-d", record.workspaceRef, record.workspaceCommit]);
|
|
431
|
+
}
|
|
432
|
+
finally {
|
|
433
|
+
workspaceRegistry.delete(record.handleId);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
/** Test inspection helper */
|
|
437
|
+
export function getRegisteredWorkspaceCount() {
|
|
438
|
+
return workspaceRegistry.size;
|
|
439
|
+
}
|
|
440
|
+
/** Test cleanup helper */
|
|
441
|
+
export function clearTaskWorkspaceRegistry() {
|
|
442
|
+
workspaceRegistry.clear();
|
|
443
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Shared deadlines for integration operations and each enclosing transport. */
|
|
2
|
+
export const INTEGRATION_RPC_TIMEOUT_MS = 60_000;
|
|
3
|
+
export const INTEGRATION_HTTP_TIMEOUT_MS = INTEGRATION_RPC_TIMEOUT_MS + 10_000;
|
|
4
|
+
export const INTEGRATION_UI_TIMEOUT_MS = INTEGRATION_HTTP_TIMEOUT_MS + 10_000;
|
|
5
|
+
export const VERIFICATION_COMMAND_TIMEOUT_MS = 300_000;
|
|
6
|
+
export const VERIFICATION_TOTAL_TIMEOUT_MS = 600_000;
|
|
7
|
+
export const VERIFICATION_STOP_TIMEOUT_MS = 10_000;
|
|
8
|
+
export const VERIFICATION_RPC_TIMEOUT_MS = VERIFICATION_TOTAL_TIMEOUT_MS + 3 * VERIFICATION_STOP_TIMEOUT_MS;
|
|
9
|
+
export const VERIFICATION_HTTP_TIMEOUT_MS = VERIFICATION_RPC_TIMEOUT_MS + 10_000;
|
|
10
|
+
export const VERIFICATION_UI_TIMEOUT_MS = VERIFICATION_HTTP_TIMEOUT_MS + 10_000;
|