pi-web-ui 0.115.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 +65 -2
- package/bin/device-hub-cli.mjs +26 -3
- package/bin/pi-web-ui.mjs +159 -71
- package/dist/server/agent-service-daemon.js +136 -8
- package/dist/server/agent-service.js +594 -159
- package/dist/server/canvas/canvas-workspace.js +18 -86
- package/dist/server/client-state.js +52 -8
- package/dist/server/compaction-markers.js +43 -24
- package/dist/server/dangling-tools.js +172 -16
- package/dist/server/device-center/bot-execution.js +77 -2
- package/dist/server/device-center/collab-service.js +61 -12
- package/dist/server/device-center/collab-tool.js +482 -21
- 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 +17 -7
- package/dist/server/device-center/execution-instructions.js +2 -1
- package/dist/server/device-center/execution-settings.js +138 -25
- package/dist/server/device-center/executor.js +51 -6
- package/dist/server/device-center/file-sync.js +14 -13
- package/dist/server/device-center/group-context-message.js +3 -1
- package/dist/server/device-center/group-context.js +73 -3
- package/dist/server/device-center/hub-conversations.js +71 -6
- 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-locks.js +320 -0
- package/dist/server/device-center/hub-page.js +16 -35
- 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-webhooks.js +165 -0
- package/dist/server/device-center/hub.js +1531 -305
- package/dist/server/device-center/integration-verification.js +113 -0
- package/dist/server/device-center/local-hub.js +8 -3
- 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 +181 -13
- package/dist/server/device-center/main.js +4 -1
- package/dist/server/device-center/partition-log.js +330 -0
- 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 +475 -42
- package/dist/server/device-center/store.js +366 -89
- 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-scheduler.js +48 -3
- 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/transfers.js +41 -15
- 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 +29 -3
- package/dist/server/dsh/dsh-agent-service.js +9 -2
- package/dist/server/engine-probe.js +112 -1
- package/dist/server/global-loader.js +119 -19
- package/dist/server/index.js +159 -17
- package/dist/server/managed.js +2 -0
- package/dist/server/model-admin.js +48 -6
- package/dist/server/model-enrich.js +10 -1
- package/dist/server/patch-node-pty.js +3 -2
- package/dist/server/patch-remote-catalog.js +3 -2
- package/dist/server/plugins.js +64 -7
- package/dist/server/preset-fields.js +17 -1
- package/dist/server/preset-share.js +1 -0
- package/dist/server/prompt-composer.js +39 -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/reasoning-registry.js +207 -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/startup-banner.js +35 -0
- package/dist/server/subagents.js +2 -1
- package/dist/server/tool-manager.js +17 -9
- package/dist/server/transcript-prepare.js +73 -0
- package/dist/server/unauthorized-page.js +319 -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 +959 -0
- package/docs/device-center.md +34 -20
- package/package.json +4 -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-BYzPgmoq.js → BgTasksModal-BjNRIUIc.js} +1 -1
- package/web/dist/assets/{BgTasksModal-ni0VTmAQ.js → BgTasksModal-C0zuU8FE.js} +1 -1
- package/web/dist/assets/{CanvasStudio-DNmDNBod.js → CanvasStudio-BWvdmJ0M.js} +3 -3
- package/web/dist/assets/{CanvasStudio-BCUeEf25.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-BLP1Su5G.js +1 -0
- package/web/dist/assets/DeviceGroups-2Pvk0Cds.js +1 -0
- package/web/dist/assets/DeviceGroups-CP7CcyPD.js +1 -0
- package/web/dist/assets/{Dropdown-CGXD0DfT.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-mevF-ySi.js → GlobalSearchModal-BM5wxiqU.js} +1 -1
- package/web/dist/assets/{GlobalSearchModal-qL99FN_F.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-LHwkS2jV.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-DE5-gEtI.js → PiSetupModal-BX5rUuoI.js} +1 -1
- package/web/dist/assets/{ProviderOAuthControls-6UMARucA.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-CTp_BZ4S.js → TerminalPanel-D4By_RVc.js} +2 -2
- package/web/dist/assets/{TerminalPanel-CkYxC-Gh.js → TerminalPanel-lZw8-i3j.js} +2 -2
- package/web/dist/assets/{TuiOverlayModal-1cRLP8IF.js → TuiOverlayModal-C3pAVuC8.js} +1 -1
- package/web/dist/assets/{TuiOverlayModal-2xlptqWX.js → TuiOverlayModal-D7_rEXcJ.js} +1 -1
- package/web/dist/assets/app-globals-KqOVtmp1.js +123 -0
- package/web/dist/assets/{canvas-client-Bye9nSFO.js → canvas-client-BY6ojmle.js} +1 -1
- package/web/dist/assets/{desktop-kjBoE3Nu.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-C8mNgghx.js → plugin-host-Dn8Dd6SC.js} +1 -1
- package/web/dist/assets/{remote-ssh-client-CeP3CLUW.js → remote-ssh-client-Dzi-eEyE.js} +1 -1
- package/web/dist/assets/{theme-BXGpyTMh.js → theme-D_bihpdN.js} +1 -1
- package/web/dist/index.html +11 -11
- package/web/dist/sw.js +1 -1
- package/dist/server/patch-turn-end-boundary.js +0 -77
- package/web/dist/assets/DeviceCenter-BcBhRgL6.js +0 -3
- package/web/dist/assets/DeviceCenter-CmL5FZmt.js +0 -3
- package/web/dist/assets/DeviceGroups-CLdpdnI7.js +0 -1
- package/web/dist/assets/DeviceGroups-CgBaDPrD.js +0 -1
- package/web/dist/assets/FilePreview-B8iqnBjy.js +0 -2
- package/web/dist/assets/FilePreview-BqviZKCN.js +0 -2
- package/web/dist/assets/GroupConversation-B88Yy-lt.js +0 -3
- package/web/dist/assets/GroupConversation-BwP5NVOE.js +0 -3
- package/web/dist/assets/ModelConfigModal-Do3Fe3jH.js +0 -2
- package/web/dist/assets/SCMPanel-Hi6QqC1Q.js +0 -2
- package/web/dist/assets/SCMPanel-ezY_ZtCu.js +0 -2
- package/web/dist/assets/SettingsModal-6HgeEsJ8.js +0 -20
- package/web/dist/assets/SettingsModal-CF-7MwqS.js +0 -20
- package/web/dist/assets/app-globals-DZNa9v8u.js +0 -325
- package/web/dist/assets/index-CsFs_HMb.css +0 -1
- package/web/dist/assets/index-DHeeHS_l.js +0 -85
- package/web/dist/assets/index-DTZch8uz.js +0 -85
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { sealPublishIntent, validatePublishIntent, validatePublicationResult } from "./publish-intent.js";
|
|
2
3
|
import { getAcceptedDeliveryForIntegration } from "./hub-deliveries.js";
|
|
4
|
+
import { assertDeliveryAuthority, assertNoApplyingIntegration, hasOpenPublication, isLegacyPublicationUnknown, } from "./delivery-authority.js";
|
|
5
|
+
import { artifactRefsMatch, deliveryReviewMatches } from "./delivery-subject.js";
|
|
6
|
+
import { computeVerificationDigest, freezeVerificationPlan, hasVerifiedIntegration, validateVerificationFacts, } from "./integration-verification.js";
|
|
7
|
+
import { canonicalizeJson } from "./work-contract.js";
|
|
3
8
|
function fail(status, message) {
|
|
4
9
|
throw Object.assign(new Error(message), { status });
|
|
5
10
|
}
|
|
@@ -7,16 +12,15 @@ function text(value, max, name) {
|
|
|
7
12
|
if (typeof value !== "string" ||
|
|
8
13
|
!value.trim() ||
|
|
9
14
|
value.length > max ||
|
|
10
|
-
/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/.test(value))
|
|
15
|
+
/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/.test(value))
|
|
11
16
|
fail(400, `Invalid ${name}`);
|
|
12
|
-
}
|
|
13
17
|
return value.trim();
|
|
14
18
|
}
|
|
15
19
|
function sha(value) {
|
|
16
20
|
const result = text(value, 64, "commit");
|
|
17
|
-
if (!/^(?:[a-f\d]{40}|[a-f\d]{64})
|
|
18
|
-
fail(
|
|
19
|
-
return result
|
|
21
|
+
if (!/^(?:[a-f\d]{40}|[a-f\d]{64})$/.test(result))
|
|
22
|
+
fail(502, "Full commit SHA required");
|
|
23
|
+
return result;
|
|
20
24
|
}
|
|
21
25
|
function safeBranch(value) {
|
|
22
26
|
const result = text(value, 256, "branch");
|
|
@@ -25,16 +29,12 @@ function safeBranch(value) {
|
|
|
25
29
|
result.includes("..") ||
|
|
26
30
|
result.startsWith("/") ||
|
|
27
31
|
result.endsWith("/") ||
|
|
28
|
-
result.endsWith(".lock"))
|
|
32
|
+
result.endsWith(".lock"))
|
|
29
33
|
fail(400, "Invalid branch name");
|
|
30
|
-
}
|
|
31
34
|
return result;
|
|
32
35
|
}
|
|
33
36
|
function sanitizePublicError(error) {
|
|
34
|
-
|
|
35
|
-
return "Integration operation failed";
|
|
36
|
-
const raw = error instanceof Error ? error.message : String(error);
|
|
37
|
-
return raw
|
|
37
|
+
return (error instanceof Error ? error.message : String(error))
|
|
38
38
|
.replace(/\/\/[^@\s]+@/g, "//***@")
|
|
39
39
|
.split("\n")[0]
|
|
40
40
|
.trim()
|
|
@@ -42,410 +42,731 @@ function sanitizePublicError(error) {
|
|
|
42
42
|
}
|
|
43
43
|
function wrapRpcError(error) {
|
|
44
44
|
const err = error instanceof Error ? error : new Error(String(error));
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
msg.includes("custom") ||
|
|
52
|
-
msg.includes("mismatch") ||
|
|
53
|
-
msg.includes("offline") ||
|
|
54
|
-
msg.includes("cannot") ||
|
|
55
|
-
msg.includes("rejected") ||
|
|
56
|
-
msg.includes("conflict")) {
|
|
57
|
-
Object.assign(err, { status: 409 });
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
Object.assign(err, { status: 502 });
|
|
61
|
-
}
|
|
62
|
-
}
|
|
45
|
+
if (!err.status)
|
|
46
|
+
Object.assign(err, {
|
|
47
|
+
status: /checked out|moved from|uncommitted|dirty|custom|mismatch|offline|cannot|rejected|conflict/i.test(err.message)
|
|
48
|
+
? 409
|
|
49
|
+
: 502,
|
|
50
|
+
});
|
|
63
51
|
return err;
|
|
64
52
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (!
|
|
68
|
-
fail(
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
if (
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
53
|
+
function actorTask(state, roomId, actor) {
|
|
54
|
+
const room = state.rooms.find((r) => r.id === roomId && r.accountId === actor.accountId);
|
|
55
|
+
if (!room)
|
|
56
|
+
fail(404, "Room missing");
|
|
57
|
+
if (actor.originalBot)
|
|
58
|
+
fail(403, "Historical actor is read-only");
|
|
59
|
+
if (!actor.bot)
|
|
60
|
+
return undefined;
|
|
61
|
+
const bot = state.bots.find((b) => b.id === actor.bot.id &&
|
|
62
|
+
b.accountId === actor.accountId &&
|
|
63
|
+
b.enabled &&
|
|
64
|
+
b.revision === actor.bot.revision &&
|
|
65
|
+
b.deviceId === actor.bot.deviceId);
|
|
66
|
+
if (!bot ||
|
|
67
|
+
!room.botIds.includes(bot.id) ||
|
|
68
|
+
!state.devices.some((d) => d.id === bot.deviceId && d.accountId === actor.accountId))
|
|
69
|
+
fail(403, "Actor Bot membership or revision changed");
|
|
70
|
+
const task = state.tasks.find((t) => t.accountId === actor.accountId &&
|
|
71
|
+
t.roomId === roomId &&
|
|
72
|
+
t.botId === bot.id &&
|
|
73
|
+
t.deviceId === bot.deviceId &&
|
|
74
|
+
t.botRevision === bot.revision &&
|
|
75
|
+
t.status === "running" &&
|
|
76
|
+
!t.cancelRequested &&
|
|
77
|
+
state.conversations.some((c) => c.id === t.conversationId &&
|
|
78
|
+
c.latestTaskId === t.id &&
|
|
79
|
+
!c.closed &&
|
|
80
|
+
c.botId === bot.id &&
|
|
81
|
+
c.botRevision === bot.revision &&
|
|
82
|
+
c.deviceId === bot.deviceId));
|
|
83
|
+
if (!task)
|
|
84
|
+
fail(403, "Current running Bot task required");
|
|
85
|
+
return task.id;
|
|
86
|
+
}
|
|
87
|
+
function recordFor(state, roomId, conversationId, id, actor) {
|
|
88
|
+
const record = state.integrations.find((i) => i.id === id && i.accountId === actor.accountId && i.roomId === roomId && i.conversationId === conversationId);
|
|
89
|
+
if (!record)
|
|
90
|
+
fail(404, "Integration task not found");
|
|
91
|
+
return record;
|
|
92
|
+
}
|
|
93
|
+
function latestDelivery(state, roomId, conversationId, accountId, deliveryId) {
|
|
94
|
+
if (!state.conversations.some((c) => c.id === conversationId && c.roomId === roomId && c.accountId === accountId && !c.closed))
|
|
95
|
+
fail(404, "Conversation missing or closed");
|
|
96
|
+
const delivery = state.deliveries
|
|
97
|
+
.filter((d) => d.accountId === accountId && d.roomId === roomId && d.conversationId === conversationId)
|
|
98
|
+
.at(-1);
|
|
99
|
+
if (!delivery || delivery.id !== deliveryId)
|
|
100
|
+
fail(409, "Only the latest delivery can be integrated");
|
|
101
|
+
assertDeliveryAuthority(state, delivery);
|
|
102
|
+
return delivery;
|
|
103
|
+
}
|
|
104
|
+
function targetBot(state, record, allowLegacyCancellation = false) {
|
|
105
|
+
const room = state.rooms.find((r) => r.id === record.roomId && r.accountId === record.accountId);
|
|
106
|
+
const bot = state.bots.find((b) => b.id === record.targetBotId &&
|
|
107
|
+
b.accountId === record.accountId &&
|
|
108
|
+
b.enabled &&
|
|
109
|
+
b.revision === record.targetBotRevision &&
|
|
110
|
+
(b.deviceId === record.targetDeviceId || (allowLegacyCancellation && record.targetDeviceId === undefined)));
|
|
111
|
+
if (!bot ||
|
|
112
|
+
!room?.botIds.includes(bot.id) ||
|
|
113
|
+
!state.devices.some((d) => d.id === bot.deviceId && d.accountId === record.accountId))
|
|
114
|
+
fail(409, "Target Bot membership, revision or device changed");
|
|
115
|
+
return bot;
|
|
116
|
+
}
|
|
117
|
+
function authority(state, record, accepted = false) {
|
|
118
|
+
const delivery = latestDelivery(state, record.roomId, record.conversationId, record.accountId, record.deliveryId);
|
|
119
|
+
if (delivery.artifactRef
|
|
120
|
+
? !artifactRefsMatch(delivery.artifactRef, record.artifactRef)
|
|
121
|
+
: !!record.artifactRef || !delivery.commit || delivery.commit !== record.commit)
|
|
122
|
+
fail(409, "Delivery subject changed");
|
|
123
|
+
let review;
|
|
124
|
+
if (accepted || !delivery.artifactRef) {
|
|
125
|
+
review = getAcceptedDeliveryForIntegration(state, record.conversationId, delivery.id).review;
|
|
126
|
+
if (delivery.artifactRef) {
|
|
127
|
+
const task = assertDeliveryAuthority(state, delivery);
|
|
128
|
+
const criteria = task.contract.acceptanceCriteria ?? [];
|
|
129
|
+
if (!deliveryReviewMatches(delivery, review) ||
|
|
130
|
+
review.integrationId !== record.id ||
|
|
131
|
+
review.verificationDigest !== record.verification?.digest ||
|
|
132
|
+
!review.criteriaResults ||
|
|
133
|
+
review.criteriaResults.length !== criteria.length ||
|
|
134
|
+
new Set(review.criteriaResults.map((c) => c.criterionIndex)).size !== criteria.length ||
|
|
135
|
+
review.criteriaResults.some((c) => c.verdict !== "met" ||
|
|
136
|
+
!Number.isInteger(c.criterionIndex) ||
|
|
137
|
+
c.criterionIndex < 0 ||
|
|
138
|
+
c.criterionIndex >= criteria.length ||
|
|
139
|
+
!c.evidence.trim()))
|
|
140
|
+
fail(409, "Artifact accepted review must match verification and every sealed criterion");
|
|
79
141
|
}
|
|
80
|
-
|
|
142
|
+
if (record.reviewId && record.reviewId !== review.id)
|
|
143
|
+
fail(409, "Accepted review invalidated");
|
|
81
144
|
}
|
|
82
|
-
|
|
83
|
-
if (obj.diff !== undefined) {
|
|
84
|
-
diff = text(obj.diff, 64 * 1024, "diff");
|
|
85
|
-
}
|
|
86
|
-
return {
|
|
87
|
-
status: obj.status,
|
|
88
|
-
expectedTargetSha,
|
|
89
|
-
conflicts,
|
|
90
|
-
diff,
|
|
91
|
-
};
|
|
145
|
+
return { delivery, review, bot: targetBot(state, record) };
|
|
92
146
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const obj = raw;
|
|
99
|
-
if (obj.status !== "verified" && obj.status !== "failed") {
|
|
100
|
-
fail(502, "Invalid verification status from device");
|
|
147
|
+
function cancellationDevice(state, record, actor) {
|
|
148
|
+
if (!actor.bot && record.targetDeviceId) {
|
|
149
|
+
if (!state.devices.some((d) => d.id === record.targetDeviceId && d.accountId === actor.accountId))
|
|
150
|
+
fail(409, "Cleanup device is no longer owned by this account");
|
|
151
|
+
return record.targetDeviceId;
|
|
101
152
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
153
|
+
return targetBot(state, record, true).deviceId;
|
|
154
|
+
}
|
|
155
|
+
function stableSubject(record) {
|
|
156
|
+
return canonicalizeJson({
|
|
157
|
+
id: record.id,
|
|
158
|
+
accountId: record.accountId,
|
|
159
|
+
roomId: record.roomId,
|
|
160
|
+
conversationId: record.conversationId,
|
|
161
|
+
deliveryId: record.deliveryId,
|
|
162
|
+
commit: record.commit ?? null,
|
|
163
|
+
artifactRef: record.artifactRef ?? null,
|
|
164
|
+
reviewId: record.reviewId ?? null,
|
|
165
|
+
targetBotId: record.targetBotId,
|
|
166
|
+
targetBotRevision: record.targetBotRevision,
|
|
167
|
+
targetDeviceId: record.targetDeviceId ?? null,
|
|
168
|
+
targetBranch: record.targetBranch,
|
|
169
|
+
expectedTargetSha: record.expectedTargetSha,
|
|
170
|
+
materializedCommit: record.materializedCommit ?? null,
|
|
171
|
+
preparedTree: record.preparedTree ?? null,
|
|
172
|
+
verificationPlan: record.verificationPlan ?? null,
|
|
173
|
+
verification: record.verification ?? null,
|
|
174
|
+
verificationMayBeRunning: record.verificationMayBeRunning ?? false,
|
|
116
175
|
});
|
|
117
|
-
return {
|
|
118
|
-
status: obj.status,
|
|
119
|
-
validationCommands,
|
|
120
|
-
};
|
|
121
176
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
if (
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
if (
|
|
129
|
-
fail(
|
|
130
|
-
|
|
131
|
-
return
|
|
132
|
-
status: "applied",
|
|
133
|
-
newCommitSha: sha(obj.newCommitSha),
|
|
134
|
-
cleanupPending: !!obj.cleanupPending,
|
|
135
|
-
};
|
|
177
|
+
function recheck(state, before, roomId, conversationId, actor, taskId, status, accepted = false) {
|
|
178
|
+
const record = recordFor(state, roomId, conversationId, before.id, actor);
|
|
179
|
+
if (record.status !== status ||
|
|
180
|
+
record.currentOpId !== before.currentOpId ||
|
|
181
|
+
stableSubject(record) !== stableSubject(before))
|
|
182
|
+
fail(409, "Integration operation generation or subject changed");
|
|
183
|
+
if (actorTask(state, roomId, actor) !== taskId)
|
|
184
|
+
fail(403, "Integration actor task changed");
|
|
185
|
+
authority(state, record, accepted);
|
|
186
|
+
return record;
|
|
136
187
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
188
|
+
async function failed(store, before, status, error) {
|
|
189
|
+
await store.transact((draft) => {
|
|
190
|
+
const record = draft.integrations.find((i) => i.id === before.id);
|
|
191
|
+
if (record?.publishReceipt)
|
|
192
|
+
return;
|
|
193
|
+
if (record?.publishIntent && before.publishIntent?.digest === record.publishIntent.digest) {
|
|
194
|
+
// An RPC/Hub failure says nothing about whether the reserved CAS happened.
|
|
195
|
+
record.error = `Publication outcome unknown: ${sanitizePublicError(error)}`.slice(0, 300);
|
|
196
|
+
record.updatedAt = Date.now();
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (record?.status === status && record.currentOpId === before.currentOpId) {
|
|
200
|
+
record.status = "failed";
|
|
201
|
+
record.error = sanitizePublicError(error);
|
|
202
|
+
if (error?.stopUnconfirmed) {
|
|
203
|
+
record.cleanupPending = true;
|
|
204
|
+
record.verificationMayBeRunning = true;
|
|
205
|
+
}
|
|
206
|
+
if (status === "applying") {
|
|
207
|
+
record.cleanupPending = true;
|
|
208
|
+
record.error = `Apply failed; check target outcome: ${record.error}`.slice(0, 300);
|
|
209
|
+
}
|
|
210
|
+
record.updatedAt = Date.now();
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
throw wrapRpcError(error);
|
|
214
|
+
}
|
|
215
|
+
function driverRequired(driver) {
|
|
216
|
+
if (!driver?.callDeviceCommand)
|
|
145
217
|
fail(409, "Target device offline or command unavailable; Hub local execution is forbidden");
|
|
146
|
-
|
|
218
|
+
return driver;
|
|
219
|
+
}
|
|
220
|
+
export async function hubPrepareIntegration(store, roomId, conversationId, actor, input, driver) {
|
|
221
|
+
const rpc = driverRequired(driver);
|
|
147
222
|
const deliveryId = text(input.deliveryId, 128, "deliveryId");
|
|
148
223
|
const targetBotId = text(input.targetBotId, 128, "targetBotId");
|
|
149
|
-
const targetBranch = safeBranch(input.targetBranch
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
fail(403, "Target Bot is not a room member");
|
|
224
|
+
const targetBranch = safeBranch(input.targetBranch ?? "main");
|
|
225
|
+
const { before, taskId, artifact } = await store.transact((draft) => {
|
|
226
|
+
const taskId = actorTask(draft, roomId, actor);
|
|
227
|
+
assertNoApplyingIntegration(draft, actor.accountId, roomId, conversationId);
|
|
228
|
+
const delivery = latestDelivery(draft, roomId, conversationId, actor.accountId, deliveryId);
|
|
229
|
+
const review = delivery.artifactRef
|
|
230
|
+
? undefined
|
|
231
|
+
: getAcceptedDeliveryForIntegration(draft, conversationId, deliveryId).review;
|
|
158
232
|
const bot = draft.bots.find((b) => b.id === targetBotId && b.accountId === actor.accountId && b.enabled);
|
|
159
233
|
if (!bot)
|
|
160
234
|
fail(404, "Target Bot not found or disabled");
|
|
161
|
-
const
|
|
162
|
-
|
|
163
|
-
id: integrationId,
|
|
235
|
+
const record = {
|
|
236
|
+
id: randomUUID(),
|
|
164
237
|
accountId: actor.accountId,
|
|
165
238
|
roomId,
|
|
166
239
|
conversationId,
|
|
167
240
|
deliveryId,
|
|
168
|
-
commit:
|
|
169
|
-
reviewId: review
|
|
241
|
+
commit: delivery.commit,
|
|
242
|
+
reviewId: review?.id,
|
|
243
|
+
artifactRef: delivery.artifactRef,
|
|
170
244
|
targetBotId,
|
|
171
245
|
targetBotRevision: bot.revision,
|
|
246
|
+
targetDeviceId: bot.deviceId,
|
|
172
247
|
targetBranch,
|
|
173
248
|
expectedTargetSha: "",
|
|
174
249
|
status: "preparing",
|
|
250
|
+
currentOpId: randomUUID(),
|
|
175
251
|
validationCommands: [],
|
|
176
252
|
createdAt: Date.now(),
|
|
177
253
|
updatedAt: Date.now(),
|
|
178
254
|
};
|
|
179
|
-
draft
|
|
180
|
-
|
|
255
|
+
targetBot(draft, record);
|
|
256
|
+
draft.integrations.push(record);
|
|
257
|
+
return { before: structuredClone(record), taskId, artifact: assertDeliveryAuthority(draft, delivery)?.artifact };
|
|
181
258
|
});
|
|
182
|
-
// Step 2: 事务外异步调用设备 RPC
|
|
183
259
|
try {
|
|
184
|
-
const
|
|
185
|
-
integrationId,
|
|
260
|
+
const raw = await rpc.callDeviceCommand(before.targetDeviceId, "integration_prepare", {
|
|
261
|
+
integrationId: before.id,
|
|
262
|
+
accountId: before.accountId,
|
|
263
|
+
conversationId,
|
|
264
|
+
targetDeviceId: before.targetDeviceId,
|
|
186
265
|
roomId,
|
|
187
266
|
targetBotId,
|
|
188
|
-
targetBotRevision:
|
|
267
|
+
targetBotRevision: before.targetBotRevision,
|
|
189
268
|
targetBranch,
|
|
190
|
-
commit:
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
269
|
+
commit: before.commit,
|
|
270
|
+
artifactRef: before.artifactRef,
|
|
271
|
+
artifact,
|
|
272
|
+
});
|
|
273
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
274
|
+
fail(502, "Invalid device prepare response");
|
|
275
|
+
const obj = raw;
|
|
276
|
+
if (obj.status !== "prepared" && obj.status !== "conflict")
|
|
277
|
+
fail(502, "Invalid integration status from device");
|
|
278
|
+
if (obj.objectFormat !== "sha1" && obj.objectFormat !== "sha256")
|
|
279
|
+
fail(502, "Missing repository object format");
|
|
280
|
+
if (before.artifactRef && obj.objectFormat !== "sha1")
|
|
281
|
+
fail(409, "SHA-256 artifact baselines are unsupported");
|
|
282
|
+
const repositoryId = text(obj.repositoryId, 1024, "repositoryId");
|
|
283
|
+
if (before.artifactRef && repositoryId !== before.artifactRef.repositoryId)
|
|
284
|
+
fail(502, "Repository identity mismatch");
|
|
285
|
+
const expectedTargetSha = sha(obj.expectedTargetSha);
|
|
286
|
+
if (expectedTargetSha.length !== (obj.objectFormat === "sha1" ? 40 : 64))
|
|
287
|
+
fail(502, "Object format mismatch");
|
|
288
|
+
if (before.artifactRef && expectedTargetSha !== before.artifactRef.baselineCommit)
|
|
289
|
+
fail(409, "Artifact target must equal sealed baseline commit");
|
|
290
|
+
const materializedCommit = obj.status === "prepared" ? sha(obj.materializedCommit) : undefined;
|
|
291
|
+
const preparedTree = obj.status === "prepared" ? sha(obj.preparedTree) : undefined;
|
|
292
|
+
if ([materializedCommit, preparedTree].some((value) => value && value.length !== expectedTargetSha.length))
|
|
293
|
+
fail(502, "Prepared object format mismatch");
|
|
294
|
+
if (before.artifactRef && obj.status === "prepared" && preparedTree !== before.artifactRef.outputTree)
|
|
295
|
+
fail(502, "Prepared artifact tree mismatch");
|
|
296
|
+
if (obj.conflicts !== undefined && (!Array.isArray(obj.conflicts) || obj.conflicts.length > 256))
|
|
297
|
+
fail(502, "Invalid conflicts");
|
|
298
|
+
const conflicts = obj.conflicts?.map((c) => text(c, 1024, "conflict"));
|
|
299
|
+
const diff = obj.diff === undefined || obj.diff === "" ? undefined : text(obj.diff, 64 * 1024, "diff");
|
|
195
300
|
return await store.transact((draft) => {
|
|
196
|
-
const
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
fail(404, "Integration record missing");
|
|
210
|
-
if (record.status !== "preparing") {
|
|
211
|
-
fail(409, `Integration generation collision; expected 'preparing' but found '${record.status}'`);
|
|
212
|
-
}
|
|
213
|
-
record.status = deviceResult.status;
|
|
214
|
-
record.expectedTargetSha = deviceResult.expectedTargetSha;
|
|
215
|
-
record.conflicts = deviceResult.conflicts;
|
|
216
|
-
record.diff = deviceResult.diff;
|
|
217
|
-
record.updatedAt = Date.now();
|
|
218
|
-
return { ...record };
|
|
301
|
+
const record = recheck(draft, before, roomId, conversationId, actor, taskId, "preparing");
|
|
302
|
+
Object.assign(record, {
|
|
303
|
+
status: obj.status,
|
|
304
|
+
expectedTargetSha,
|
|
305
|
+
materializedCommit,
|
|
306
|
+
preparedTree,
|
|
307
|
+
objectFormat: obj.objectFormat,
|
|
308
|
+
repositoryId,
|
|
309
|
+
conflicts,
|
|
310
|
+
diff,
|
|
311
|
+
updatedAt: Date.now(),
|
|
312
|
+
});
|
|
313
|
+
return structuredClone(record);
|
|
219
314
|
});
|
|
220
315
|
}
|
|
221
316
|
catch (error) {
|
|
222
|
-
|
|
223
|
-
await store.transact((draft) => {
|
|
224
|
-
const record = draft.integrations.find((it) => it.id === integrationId);
|
|
225
|
-
if (record && record.status === "preparing") {
|
|
226
|
-
record.status = "failed";
|
|
227
|
-
record.error = errMsg;
|
|
228
|
-
record.updatedAt = Date.now();
|
|
229
|
-
}
|
|
230
|
-
});
|
|
231
|
-
throw wrapRpcError(error);
|
|
317
|
+
return failed(store, before, "preparing", error);
|
|
232
318
|
}
|
|
233
319
|
}
|
|
234
|
-
/**
|
|
235
|
-
* Hub 端运行显式验证命令
|
|
236
|
-
*/
|
|
237
320
|
export async function hubVerifyIntegration(store, roomId, conversationId, integrationId, actor, input, driver) {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
// Step 1: 同步 reservation
|
|
248
|
-
const { targetBot } = await store.transact((draft) => {
|
|
249
|
-
const record = draft.integrations.find((it) => it.id === integrationId &&
|
|
250
|
-
it.roomId === roomId &&
|
|
251
|
-
it.conversationId === conversationId &&
|
|
252
|
-
it.accountId === actor.accountId);
|
|
253
|
-
if (!record)
|
|
254
|
-
fail(404, "Integration task not found");
|
|
255
|
-
if (record.status !== "prepared" && record.status !== "verified") {
|
|
321
|
+
const rpc = driverRequired(driver);
|
|
322
|
+
const { before, taskId } = await store.transact((draft) => {
|
|
323
|
+
const taskId = actorTask(draft, roomId, actor);
|
|
324
|
+
const record = recordFor(draft, roomId, conversationId, integrationId, actor);
|
|
325
|
+
if (record.publishIntent || isLegacyPublicationUnknown(record))
|
|
326
|
+
fail(409, "Publication outcome is unresolved; manual recovery or read-only reconcile required");
|
|
327
|
+
if (record.verification)
|
|
328
|
+
fail(409, "Successful verification is immutable; prepare a new integration to verify again");
|
|
329
|
+
if (record.status !== "prepared" && record.status !== "failed")
|
|
256
330
|
fail(409, `Cannot verify integration in status '${record.status}'`);
|
|
331
|
+
authority(draft, record);
|
|
332
|
+
if (!record.preparedTree || !record.materializedCommit)
|
|
333
|
+
fail(409, "Missing prepared tree and materialized commit proof");
|
|
334
|
+
if (actor.bot) {
|
|
335
|
+
if (!record.verificationPlan || record.verificationPlan.authorizedByAccountId !== actor.accountId)
|
|
336
|
+
fail(403, "Only the account owner may authorize verification commands");
|
|
337
|
+
if (input.commands !== undefined &&
|
|
338
|
+
canonicalizeJson(input.commands) !== canonicalizeJson(record.verificationPlan.commands))
|
|
339
|
+
fail(403, "Bot cannot change owner-authorized verification commands");
|
|
257
340
|
}
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
}
|
|
263
|
-
const bot = draft.bots.find((b) => b.id === record.targetBotId && b.accountId === actor.accountId && b.enabled);
|
|
264
|
-
if (!bot || bot.revision !== record.targetBotRevision) {
|
|
265
|
-
fail(409, "Target Bot revision mismatch or disabled");
|
|
341
|
+
else if (record.verificationPlan) {
|
|
342
|
+
if (input.commands !== undefined &&
|
|
343
|
+
canonicalizeJson(input.commands) !== canonicalizeJson(record.verificationPlan.commands))
|
|
344
|
+
fail(409, "Verification plan is frozen; prepare a new integration to change commands");
|
|
266
345
|
}
|
|
346
|
+
else
|
|
347
|
+
record.verificationPlan = freezeVerificationPlan(input.commands, actor.accountId);
|
|
267
348
|
record.status = "verifying";
|
|
268
|
-
record.
|
|
349
|
+
record.verificationMayBeRunning = true;
|
|
350
|
+
record.currentOpId = randomUUID();
|
|
351
|
+
record.error = undefined;
|
|
269
352
|
record.updatedAt = Date.now();
|
|
270
|
-
return {
|
|
353
|
+
return { before: structuredClone(record), taskId };
|
|
271
354
|
});
|
|
272
|
-
// Step 2: 事务外调用设备 RPC
|
|
273
355
|
try {
|
|
274
|
-
const
|
|
356
|
+
const raw = await rpc.callDeviceCommand(before.targetDeviceId, "integration_verify", {
|
|
275
357
|
integrationId,
|
|
276
358
|
roomId,
|
|
277
|
-
targetBotId:
|
|
278
|
-
targetBotRevision:
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
359
|
+
targetBotId: before.targetBotId,
|
|
360
|
+
targetBotRevision: before.targetBotRevision,
|
|
361
|
+
verificationPlan: before.verificationPlan,
|
|
362
|
+
artifactRef: before.artifactRef,
|
|
363
|
+
materializedCommit: before.materializedCommit,
|
|
364
|
+
preparedTree: before.preparedTree,
|
|
365
|
+
expectedTargetSha: before.expectedTargetSha,
|
|
366
|
+
});
|
|
367
|
+
const { status, facts } = validateVerificationFacts(raw, before);
|
|
284
368
|
return await store.transact((draft) => {
|
|
285
|
-
const record = draft
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
369
|
+
const record = recheck(draft, before, roomId, conversationId, actor, taskId, "verifying");
|
|
370
|
+
record.status = status;
|
|
371
|
+
record.verificationMayBeRunning = false;
|
|
372
|
+
record.validationCommands = facts.results.map((r) => ({
|
|
373
|
+
command: r.command,
|
|
374
|
+
status: r.status,
|
|
375
|
+
exitCode: r.exitCode,
|
|
376
|
+
output: r.outputPreview,
|
|
377
|
+
}));
|
|
378
|
+
if (status === "verified")
|
|
379
|
+
record.verification = { ...facts, digest: computeVerificationDigest(record, facts), verifiedAt: Date.now() };
|
|
293
380
|
record.updatedAt = Date.now();
|
|
294
|
-
return
|
|
381
|
+
return structuredClone(record);
|
|
295
382
|
});
|
|
296
383
|
}
|
|
297
384
|
catch (error) {
|
|
298
|
-
|
|
299
|
-
await store.transact((draft) => {
|
|
300
|
-
const record = draft.integrations.find((it) => it.id === integrationId);
|
|
301
|
-
if (record && record.status === "verifying" && record.currentOpId === opId) {
|
|
302
|
-
record.status = "failed";
|
|
303
|
-
record.error = errMsg;
|
|
304
|
-
record.updatedAt = Date.now();
|
|
305
|
-
}
|
|
306
|
-
});
|
|
307
|
-
throw wrapRpcError(error);
|
|
385
|
+
return failed(store, before, "verifying", error);
|
|
308
386
|
}
|
|
309
387
|
}
|
|
310
|
-
/**
|
|
311
|
-
* Hub 端确认写入目标分支
|
|
312
|
-
*/
|
|
313
388
|
export async function hubConfirmIntegration(store, roomId, conversationId, integrationId, actor, input, driver) {
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
}
|
|
329
|
-
const { delivery, review } = getAcceptedDeliveryForIntegration(draft, conversationId, record.deliveryId);
|
|
330
|
-
if (delivery.commit !== record.commit) {
|
|
331
|
-
fail(409, "Delivery commit changed; integration rejected");
|
|
332
|
-
}
|
|
333
|
-
if (review.id !== record.reviewId || review.verdict !== "accepted") {
|
|
334
|
-
fail(409, "Delivery accepted review invalidated; integration rejected");
|
|
335
|
-
}
|
|
336
|
-
const expSha = text(input.expectedTargetSha || record.expectedTargetSha, 64, "expectedTargetSha");
|
|
337
|
-
if (expSha !== record.expectedTargetSha) {
|
|
389
|
+
// The synchronous WAL callback seals data only, before the first possible RPC.
|
|
390
|
+
const { before, dispatch } = await store.transact((draft) => {
|
|
391
|
+
const record = recordFor(draft, roomId, conversationId, integrationId, actor);
|
|
392
|
+
if (actor.originalBot)
|
|
393
|
+
fail(403, "Historical actor is read-only");
|
|
394
|
+
if (record.publishIntent || record.status === "applied")
|
|
395
|
+
return { before: structuredClone(record), dispatch: false };
|
|
396
|
+
driverRequired(driver);
|
|
397
|
+
const taskId = actorTask(draft, roomId, actor);
|
|
398
|
+
if (record.status !== "verified" || !hasVerifiedIntegration(record))
|
|
399
|
+
fail(409, "Apply requires successful real verification proof");
|
|
400
|
+
const { delivery, review } = authority(draft, record, true);
|
|
401
|
+
const expected = text(input.expectedTargetSha ?? record.expectedTargetSha, 64, "expectedTargetSha");
|
|
402
|
+
if (expected !== record.expectedTargetSha)
|
|
338
403
|
fail(409, "Expected target SHA mismatch");
|
|
404
|
+
if (!record.repositoryId || !record.objectFormat)
|
|
405
|
+
fail(409, "Prepare must capture repository identity and object format");
|
|
406
|
+
const createdAt = Date.now();
|
|
407
|
+
record.publishIntent = sealPublishIntent({
|
|
408
|
+
version: 1,
|
|
409
|
+
id: randomUUID(),
|
|
410
|
+
accountId: record.accountId,
|
|
411
|
+
roomId,
|
|
412
|
+
conversationId,
|
|
413
|
+
integrationId,
|
|
414
|
+
targetDeviceId: record.targetDeviceId,
|
|
415
|
+
targetBotId: record.targetBotId,
|
|
416
|
+
targetBotRevision: record.targetBotRevision,
|
|
417
|
+
actor: {
|
|
418
|
+
accountId: actor.accountId,
|
|
419
|
+
...(actor.bot
|
|
420
|
+
? { botId: actor.bot.id, botRevision: actor.bot.revision, botDeviceId: actor.bot.deviceId, taskId }
|
|
421
|
+
: {}),
|
|
422
|
+
},
|
|
423
|
+
repositoryId: record.repositoryId,
|
|
424
|
+
objectFormat: record.objectFormat,
|
|
425
|
+
targetRef: record.targetBranch.startsWith("refs/heads/")
|
|
426
|
+
? record.targetBranch
|
|
427
|
+
: `refs/heads/${record.targetBranch}`,
|
|
428
|
+
expectedOld: record.expectedTargetSha,
|
|
429
|
+
preparedTree: record.preparedTree,
|
|
430
|
+
materializedCommit: record.materializedCommit,
|
|
431
|
+
createdAt,
|
|
432
|
+
delivery: {
|
|
433
|
+
id: delivery.id,
|
|
434
|
+
...(delivery.commit ? { commit: delivery.commit } : {}),
|
|
435
|
+
...(delivery.artifactRef ? { artifactRef: structuredClone(delivery.artifactRef) } : {}),
|
|
436
|
+
},
|
|
437
|
+
review: structuredClone(review),
|
|
438
|
+
verificationPlan: structuredClone(record.verificationPlan),
|
|
439
|
+
verification: structuredClone(record.verification),
|
|
440
|
+
});
|
|
441
|
+
record.reviewId = review.id;
|
|
442
|
+
record.status = "applying";
|
|
443
|
+
record.currentOpId = randomUUID();
|
|
444
|
+
record.updatedAt = createdAt;
|
|
445
|
+
return { before: structuredClone(record), dispatch: true };
|
|
446
|
+
});
|
|
447
|
+
if (!dispatch)
|
|
448
|
+
return before;
|
|
449
|
+
return dispatchPublication(store, before, driverRequired(driver), "first");
|
|
450
|
+
}
|
|
451
|
+
export async function hubCancelIntegration(store, roomId, conversationId, integrationId, actor, driver) {
|
|
452
|
+
const existing = recordFor(store.snapshot(), roomId, conversationId, integrationId, actor);
|
|
453
|
+
if (existing.publishIntent) {
|
|
454
|
+
if (actor.originalBot)
|
|
455
|
+
fail(403, "Historical actor is read-only");
|
|
456
|
+
if (actor.bot)
|
|
457
|
+
actorTask(store.snapshot(), roomId, actor);
|
|
458
|
+
if (existing.publishReceipt && !hasOpenPublication(existing))
|
|
459
|
+
return existing;
|
|
460
|
+
const rpc = driverRequired(driver);
|
|
461
|
+
const before = await store.transact((draft) => {
|
|
462
|
+
const record = recordFor(draft, roomId, conversationId, integrationId, actor);
|
|
463
|
+
if (record.publishIntent?.digest !== existing.publishIntent.digest)
|
|
464
|
+
fail(409, "Publication intent changed");
|
|
465
|
+
if (!record.publishReceipt && hasOpenPublication(record))
|
|
466
|
+
record.status = "cancelling";
|
|
467
|
+
if (record.publishObservation)
|
|
468
|
+
record.publishObservation.retryable = false;
|
|
469
|
+
return structuredClone(record);
|
|
470
|
+
});
|
|
471
|
+
if (!hasOpenPublication(before))
|
|
472
|
+
return before;
|
|
473
|
+
try {
|
|
474
|
+
const raw = await rpc.callDeviceCommand(before.publishIntent.targetDeviceId, "integration_cancel", {
|
|
475
|
+
...publicationPayload(before),
|
|
476
|
+
ownerCleanup: !actor.bot,
|
|
477
|
+
});
|
|
478
|
+
return await mergePublicationResult(store, before, raw);
|
|
339
479
|
}
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
fail(409, "Target Bot revision mismatch or disabled");
|
|
480
|
+
catch (error) {
|
|
481
|
+
return failed(store, before, "cancelling", error);
|
|
343
482
|
}
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
record
|
|
347
|
-
|
|
483
|
+
}
|
|
484
|
+
const { before, taskId, priorStatus } = await store.transact((draft) => {
|
|
485
|
+
const record = recordFor(draft, roomId, conversationId, integrationId, actor);
|
|
486
|
+
if (record.status === "applied")
|
|
487
|
+
fail(409, "Already applied integration cannot be cancelled");
|
|
488
|
+
if (isLegacyPublicationUnknown(record))
|
|
489
|
+
fail(409, "Legacy publication outcome unknown; manual investigation required");
|
|
490
|
+
const taskId = actor.bot ? actorTask(draft, roomId, actor) : undefined;
|
|
491
|
+
driverRequired(driver);
|
|
492
|
+
const priorStatus = record.status;
|
|
493
|
+
if (["verifying", "failed", "verified"].includes(record.status) && !hasVerifiedIntegration(record))
|
|
494
|
+
record.verificationMayBeRunning = true;
|
|
495
|
+
const deviceId = cancellationDevice(draft, record, actor);
|
|
496
|
+
// Legacy jobs may be cleaned on their still-bound Bot/device without inventing verification proof.
|
|
497
|
+
record.targetDeviceId ??= deviceId;
|
|
498
|
+
if (record.status !== "cancelled") {
|
|
499
|
+
record.status = "cancelling";
|
|
500
|
+
record.currentOpId = randomUUID();
|
|
501
|
+
record.updatedAt = Date.now();
|
|
502
|
+
}
|
|
503
|
+
return { before: structuredClone(record), taskId, priorStatus };
|
|
348
504
|
});
|
|
349
|
-
|
|
505
|
+
if (before.status === "cancelled")
|
|
506
|
+
return before;
|
|
350
507
|
try {
|
|
351
|
-
const
|
|
508
|
+
const raw = await driverRequired(driver).callDeviceCommand(before.targetDeviceId, "integration_cancel", {
|
|
352
509
|
integrationId,
|
|
353
510
|
roomId,
|
|
354
|
-
targetBotId:
|
|
355
|
-
targetBotRevision:
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
};
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
// Step 3: 同步 commit
|
|
511
|
+
targetBotId: before.targetBotId,
|
|
512
|
+
targetBotRevision: before.targetBotRevision,
|
|
513
|
+
priorStatus,
|
|
514
|
+
verificationMayBeRunning: before.verificationMayBeRunning === true,
|
|
515
|
+
ownerCleanup: !actor.bot,
|
|
516
|
+
});
|
|
517
|
+
if (!raw || typeof raw !== "object" || raw.status !== "cancelled")
|
|
518
|
+
fail(502, "Device has not confirmed integration stop and cleanup");
|
|
363
519
|
return await store.transact((draft) => {
|
|
364
|
-
const record = draft
|
|
365
|
-
if (
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
fail(409, "
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
record
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
520
|
+
const record = recordFor(draft, roomId, conversationId, integrationId, actor);
|
|
521
|
+
if (record.status !== "cancelling" ||
|
|
522
|
+
record.currentOpId !== before.currentOpId ||
|
|
523
|
+
stableSubject(record) !== stableSubject(before))
|
|
524
|
+
fail(409, "Cancellation generation changed");
|
|
525
|
+
if (actor.bot && actorTask(draft, roomId, actor) !== taskId)
|
|
526
|
+
fail(403, "Integration actor task changed");
|
|
527
|
+
if (cancellationDevice(draft, record, actor) !== before.targetDeviceId)
|
|
528
|
+
fail(409, "Cleanup device changed");
|
|
529
|
+
record.status = "cancelled";
|
|
530
|
+
record.verificationMayBeRunning = false;
|
|
531
|
+
record.cleanupPending = false;
|
|
375
532
|
record.updatedAt = Date.now();
|
|
376
|
-
return
|
|
533
|
+
return structuredClone(record);
|
|
377
534
|
});
|
|
378
535
|
}
|
|
379
536
|
catch (error) {
|
|
380
|
-
|
|
381
|
-
await store.transact((draft) => {
|
|
382
|
-
const record = draft.integrations.find((it) => it.id === integrationId);
|
|
383
|
-
if (record && record.status === "applying" && record.currentOpId === opId) {
|
|
384
|
-
record.status = "failed";
|
|
385
|
-
record.error = errMsg;
|
|
386
|
-
record.updatedAt = Date.now();
|
|
387
|
-
}
|
|
388
|
-
});
|
|
389
|
-
throw wrapRpcError(error);
|
|
537
|
+
return failed(store, before, "cancelling", error);
|
|
390
538
|
}
|
|
391
539
|
}
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
540
|
+
function publicationPayload(record) {
|
|
541
|
+
const intent = validatePublishIntent(record.publishIntent);
|
|
542
|
+
return {
|
|
543
|
+
integrationId: intent.integrationId,
|
|
544
|
+
accountId: intent.accountId,
|
|
545
|
+
roomId: intent.roomId,
|
|
546
|
+
conversationId: intent.conversationId,
|
|
547
|
+
targetBotId: intent.targetBotId,
|
|
548
|
+
targetBotRevision: intent.targetBotRevision,
|
|
549
|
+
publishIntent: intent,
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
async function mergePublicationResult(store, before, raw) {
|
|
553
|
+
const intent = validatePublishIntent(before.publishIntent);
|
|
554
|
+
const result = validatePublicationResult(raw, intent);
|
|
555
|
+
return store.transact((draft) => {
|
|
556
|
+
const record = draft.integrations.find((i) => i.id === intent.integrationId &&
|
|
557
|
+
i.accountId === intent.accountId &&
|
|
558
|
+
i.roomId === intent.roomId &&
|
|
559
|
+
i.conversationId === intent.conversationId);
|
|
560
|
+
if (!record || record.publishIntent?.digest !== intent.digest || record.publishIntent.id !== intent.id)
|
|
561
|
+
fail(409, "Publication intent changed");
|
|
562
|
+
validatePublishIntent(record.publishIntent);
|
|
563
|
+
// Match only the sealed operation. Current membership/review/task may have been revoked after CAS.
|
|
564
|
+
if (result.publishReceipt && !record.publishReceipt)
|
|
565
|
+
record.publishReceipt = result.publishReceipt;
|
|
566
|
+
if (result.publishObservation &&
|
|
567
|
+
(!record.publishObservation || result.publishObservation.observedAt >= record.publishObservation.observedAt))
|
|
568
|
+
record.publishObservation = result.publishObservation;
|
|
569
|
+
if (result.publishStop) {
|
|
570
|
+
const previous = record.publishStop;
|
|
571
|
+
if (!previous)
|
|
572
|
+
record.publishStop = result.publishStop;
|
|
573
|
+
else if (!previous.stopConfirmed && result.publishStop.stopConfirmed)
|
|
574
|
+
record.publishStop = {
|
|
575
|
+
...result.publishStop,
|
|
576
|
+
closed: previous.closed || result.publishStop.closed,
|
|
577
|
+
fencedAt: Math.min(previous.fencedAt, result.publishStop.fencedAt),
|
|
578
|
+
};
|
|
579
|
+
else if (!previous.closed && result.publishStop.closed)
|
|
580
|
+
record.publishStop = {
|
|
581
|
+
...previous,
|
|
582
|
+
closed: true,
|
|
583
|
+
fencedAt: Math.min(previous.fencedAt, result.publishStop.fencedAt),
|
|
584
|
+
};
|
|
406
585
|
}
|
|
407
|
-
if (record.
|
|
408
|
-
|
|
586
|
+
if (record.publishReceipt) {
|
|
587
|
+
record.status = "applied";
|
|
588
|
+
record.newCommitSha = intent.candidateSha;
|
|
589
|
+
if (record.publishStop)
|
|
590
|
+
record.publishStop.pastEffect = "applied";
|
|
591
|
+
if (record.publishObservation)
|
|
592
|
+
record.publishObservation.retryable = false;
|
|
593
|
+
record.error = undefined;
|
|
594
|
+
}
|
|
595
|
+
else if (record.publishStop?.closed && record.publishStop.stopConfirmed) {
|
|
596
|
+
record.status = record.publishStop.pastEffect === "not_applied" ? "cancelled" : "cancelling";
|
|
597
|
+
if (record.publishObservation)
|
|
598
|
+
record.publishObservation.retryable = false;
|
|
599
|
+
record.error =
|
|
600
|
+
record.publishStop.pastEffect === "unknown"
|
|
601
|
+
? "Publication closed for future writes; historical outcome remains unknown"
|
|
602
|
+
: undefined;
|
|
409
603
|
}
|
|
410
|
-
|
|
411
|
-
|
|
604
|
+
else {
|
|
605
|
+
record.status = record.status === "cancelling" ? "cancelling" : "applying";
|
|
606
|
+
if (record.status === "cancelling" && record.publishObservation)
|
|
607
|
+
record.publishObservation.retryable = false;
|
|
608
|
+
record.error = result.publishObservation?.error;
|
|
412
609
|
}
|
|
413
|
-
|
|
610
|
+
if (result.cleanupPending !== undefined)
|
|
611
|
+
record.cleanupPending = result.cleanupPending;
|
|
414
612
|
record.updatedAt = Date.now();
|
|
415
|
-
|
|
416
|
-
return { targetBot: bot ? { ...bot } : undefined };
|
|
613
|
+
return structuredClone(record);
|
|
417
614
|
});
|
|
418
|
-
|
|
419
|
-
|
|
615
|
+
}
|
|
616
|
+
async function dispatchPublication(store, before, driver, mode) {
|
|
420
617
|
try {
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
618
|
+
const raw = await driver.callDeviceCommand(before.publishIntent.targetDeviceId, "integration_confirm", {
|
|
619
|
+
...publicationPayload(before),
|
|
620
|
+
mode,
|
|
621
|
+
deliveryId: before.deliveryId,
|
|
622
|
+
commit: before.commit,
|
|
623
|
+
expectedTargetSha: before.expectedTargetSha,
|
|
624
|
+
artifactRef: before.artifactRef,
|
|
625
|
+
materializedCommit: before.materializedCommit,
|
|
626
|
+
verificationDigest: before.verification.digest,
|
|
627
|
+
});
|
|
628
|
+
return await mergePublicationResult(store, before, raw);
|
|
425
629
|
}
|
|
426
630
|
catch (error) {
|
|
427
|
-
|
|
631
|
+
return failed(store, before, "applying", error);
|
|
428
632
|
}
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
633
|
+
}
|
|
634
|
+
function historicalActor(record, actor) {
|
|
635
|
+
const bot = actor.originalBot ?? actor.bot;
|
|
636
|
+
if (!bot)
|
|
637
|
+
return;
|
|
638
|
+
const intent = record.publishIntent;
|
|
639
|
+
if (!intent ||
|
|
640
|
+
bot.accountId !== intent.accountId ||
|
|
641
|
+
!((bot.id === intent.targetBotId && bot.deviceId === intent.targetDeviceId) ||
|
|
642
|
+
(bot.id === intent.actor.botId && bot.deviceId === intent.actor.botDeviceId)))
|
|
643
|
+
fail(403, "Historical publication actor scope mismatch");
|
|
644
|
+
}
|
|
645
|
+
export async function hubReconcileIntegration(store, roomId, conversationId, actor, id, driver) {
|
|
646
|
+
const before = recordFor(store.snapshot(), roomId, conversationId, id, actor);
|
|
647
|
+
historicalActor(before, actor);
|
|
648
|
+
// Legacy records carry no journal identity. Never synthesize a receipt or retry them.
|
|
649
|
+
if (!before.publishIntent)
|
|
650
|
+
return structuredClone(before);
|
|
651
|
+
try {
|
|
652
|
+
const raw = await driverRequired(driver).callDeviceCommand(before.publishIntent.targetDeviceId, "integration_publication_status", publicationPayload(before));
|
|
653
|
+
return await mergePublicationResult(store, before, raw);
|
|
654
|
+
}
|
|
655
|
+
catch (error) {
|
|
656
|
+
return failed(store, before, before.status, error);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
export async function hubResumeIntegration(store, roomId, conversationId, actor, id, input, driver) {
|
|
660
|
+
if (actor.bot || actor.originalBot)
|
|
661
|
+
fail(403, "Only the human account owner may resume publication");
|
|
662
|
+
const before = await store.transact((draft) => {
|
|
663
|
+
actorTask(draft, roomId, actor);
|
|
664
|
+
const record = recordFor(draft, roomId, conversationId, id, actor);
|
|
665
|
+
const intent = validatePublishIntent(record.publishIntent);
|
|
666
|
+
if (!input ||
|
|
667
|
+
input.intentId !== intent.id ||
|
|
668
|
+
input.intentDigest !== intent.digest ||
|
|
669
|
+
input.candidateSha !== intent.candidateSha)
|
|
670
|
+
fail(409, "Resume must bind exact publication intent");
|
|
671
|
+
if (record.publishReceipt)
|
|
672
|
+
return structuredClone(record);
|
|
673
|
+
if (record.publishStop?.closed)
|
|
674
|
+
fail(409, "Publication is closed for future writes");
|
|
675
|
+
if (record.status === "cancelling")
|
|
676
|
+
fail(409, "Publication cancellation is in progress");
|
|
677
|
+
if (!record.publishObservation?.retryable ||
|
|
678
|
+
!["prepared", "cas_rejected"].includes(record.publishObservation.phase))
|
|
679
|
+
fail(409, "Publication is not proven safe to resume; reconcile first");
|
|
680
|
+
validatePublicationResult({ status: "pending", publishObservation: record.publishObservation }, intent);
|
|
681
|
+
const { review } = authority(draft, record, true);
|
|
682
|
+
if (review?.id !== intent.review.id ||
|
|
683
|
+
!hasVerifiedIntegration(record) ||
|
|
684
|
+
record.verification?.digest !== intent.verification.digest ||
|
|
685
|
+
record.repositoryId !== intent.repositoryId ||
|
|
686
|
+
record.objectFormat !== intent.objectFormat ||
|
|
687
|
+
record.expectedTargetSha !== intent.expectedOld ||
|
|
688
|
+
record.preparedTree !== intent.preparedTree ||
|
|
689
|
+
record.materializedCommit !== intent.materializedCommit ||
|
|
690
|
+
record.targetDeviceId !== intent.targetDeviceId ||
|
|
691
|
+
record.targetBotId !== intent.targetBotId ||
|
|
692
|
+
record.targetBotRevision !== intent.targetBotRevision ||
|
|
693
|
+
(record.targetBranch.startsWith("refs/heads/") ? record.targetBranch : `refs/heads/${record.targetBranch}`) !==
|
|
694
|
+
intent.targetRef)
|
|
695
|
+
fail(409, "Current publication authority or verification changed");
|
|
696
|
+
driverRequired(driver);
|
|
697
|
+
record.status = "applying";
|
|
698
|
+
record.currentOpId = randomUUID();
|
|
699
|
+
// Consume the Hub's retry capability before dispatch. Device independently checks the durable journal.
|
|
700
|
+
record.publishObservation.retryable = false;
|
|
701
|
+
record.updatedAt = Date.now();
|
|
702
|
+
return structuredClone(record);
|
|
443
703
|
});
|
|
444
|
-
if (
|
|
445
|
-
|
|
446
|
-
return
|
|
704
|
+
if (before.publishReceipt)
|
|
705
|
+
return before;
|
|
706
|
+
return dispatchPublication(store, before, driverRequired(driver), "resume");
|
|
707
|
+
}
|
|
708
|
+
const pendingRecovery = new WeakMap();
|
|
709
|
+
const recoveryBatches = new WeakMap();
|
|
710
|
+
const recoveryCursors = new WeakMap();
|
|
711
|
+
/** Startup/ready recovery only issues status queries. No implicit publication retry. */
|
|
712
|
+
export async function reconcilePendingIntegrations(store, driver, options = {}) {
|
|
713
|
+
const active = recoveryBatches.get(store);
|
|
714
|
+
if (active)
|
|
715
|
+
return active;
|
|
716
|
+
const batch = runPendingRecovery(store, driver, options);
|
|
717
|
+
recoveryBatches.set(store, batch);
|
|
718
|
+
try {
|
|
719
|
+
return await batch;
|
|
720
|
+
}
|
|
721
|
+
finally {
|
|
722
|
+
if (recoveryBatches.get(store) === batch)
|
|
723
|
+
recoveryBatches.delete(store);
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
async function runPendingRecovery(store, driver, options) {
|
|
727
|
+
const limit = Number.isFinite(options.limit) ? Math.max(1, Math.min(64, Math.floor(options.limit))) : 16;
|
|
728
|
+
const all = store
|
|
729
|
+
.snapshot()
|
|
730
|
+
.integrations.filter((i) => hasOpenPublication(i) && (!options.deviceId || i.publishIntent.targetDeviceId === options.deviceId));
|
|
731
|
+
let cursors = recoveryCursors.get(store);
|
|
732
|
+
if (!cursors) {
|
|
733
|
+
cursors = new Map();
|
|
734
|
+
recoveryCursors.set(store, cursors);
|
|
735
|
+
}
|
|
736
|
+
const scope = options.deviceId ?? "";
|
|
737
|
+
const start = (cursors.get(scope) ?? 0) % Math.max(1, all.length);
|
|
738
|
+
const pending = [...all.slice(start), ...all.slice(0, start)].slice(0, limit);
|
|
739
|
+
cursors.set(scope, (start + pending.length) % Math.max(1, all.length));
|
|
740
|
+
let running = pendingRecovery.get(store);
|
|
741
|
+
if (!running) {
|
|
742
|
+
running = new Map();
|
|
743
|
+
pendingRecovery.set(store, running);
|
|
744
|
+
}
|
|
745
|
+
const results = [];
|
|
746
|
+
let cursor = 0;
|
|
747
|
+
await Promise.all(Array.from({ length: Math.min(4, pending.length) }, async () => {
|
|
748
|
+
while (cursor < pending.length) {
|
|
749
|
+
const record = pending[cursor++];
|
|
750
|
+
const key = record.publishIntent.id;
|
|
751
|
+
let operation = running.get(key);
|
|
752
|
+
if (!operation) {
|
|
753
|
+
operation = hubReconcileIntegration(store, record.roomId, record.conversationId, { accountId: record.accountId }, record.id, driver);
|
|
754
|
+
running.set(key, operation);
|
|
755
|
+
}
|
|
756
|
+
try {
|
|
757
|
+
results.push(await operation);
|
|
758
|
+
}
|
|
759
|
+
catch {
|
|
760
|
+
/* Keep unresolved evidence for the next read-only probe. */
|
|
761
|
+
}
|
|
762
|
+
finally {
|
|
763
|
+
if (running.get(key) === operation)
|
|
764
|
+
running.delete(key);
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
}));
|
|
768
|
+
return results;
|
|
447
769
|
}
|
|
448
|
-
/** 查询会话的集成历史记录 */
|
|
449
770
|
export function listIntegrations(state, conversationId) {
|
|
450
|
-
return state.integrations.filter((
|
|
771
|
+
return state.integrations.filter((i) => i.conversationId === conversationId);
|
|
451
772
|
}
|