pi-web-ui 0.114.3 → 0.116.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 +62 -1
- package/bin/pi-web-ui.mjs +90 -8
- package/dist/gh-comment-781.md +5 -0
- package/dist/server/agent-service-daemon.js +522 -32
- package/dist/server/agent-service.js +954 -402
- package/dist/server/canvas/canvas-dispatch.js +13 -1
- package/dist/server/client-state.js +42 -2
- package/dist/server/communicate-tool.js +2 -0
- package/dist/server/conversation-communication.js +14 -0
- package/dist/server/conversation-identity.js +27 -0
- package/dist/server/device-center/approval-summary.js +31 -0
- package/dist/server/device-center/bot-catalog.js +21 -7
- package/dist/server/device-center/bot-execution.js +100 -9
- package/dist/server/device-center/bot-presets.js +46 -0
- package/dist/server/device-center/collab-service.js +362 -13
- package/dist/server/device-center/collab-tool.js +824 -13
- package/dist/server/device-center/connector.js +359 -23
- package/dist/server/device-center/desktop-page.js +22 -16
- package/dist/server/device-center/device-integration.js +595 -0
- package/dist/server/device-center/execution-guides.js +32 -0
- package/dist/server/device-center/execution-instructions.js +20 -0
- package/dist/server/device-center/execution-settings.js +213 -0
- package/dist/server/device-center/execution-system-prompt.js +38 -0
- package/dist/server/device-center/execution-workspace.js +20 -0
- package/dist/server/device-center/executor.js +392 -40
- package/dist/server/device-center/file-sync-paths.js +171 -0
- package/dist/server/device-center/file-sync.js +186 -96
- package/dist/server/device-center/group-context-delta.js +37 -0
- package/dist/server/device-center/group-context-message.js +210 -0
- package/dist/server/device-center/group-context.js +179 -0
- package/dist/server/device-center/group-events.js +136 -0
- package/dist/server/device-center/group-plan-guard.js +138 -0
- package/dist/server/device-center/hub-conversations.js +233 -0
- package/dist/server/device-center/hub-deliveries.js +277 -0
- package/dist/server/device-center/hub-integration.js +451 -0
- package/dist/server/device-center/hub-locks.js +320 -0
- package/dist/server/device-center/hub-page.js +13 -33
- package/dist/server/device-center/hub-webhooks.js +165 -0
- package/dist/server/device-center/hub.js +2379 -107
- package/dist/server/device-center/human-interaction.js +232 -0
- package/dist/server/device-center/integration-errors.js +25 -0
- package/dist/server/device-center/local-hub.js +20 -1
- package/dist/server/device-center/local-page.js +17 -0
- package/dist/server/device-center/local.js +430 -41
- package/dist/server/device-center/main.js +11 -1
- package/dist/server/device-center/partition-log.js +293 -0
- package/dist/server/device-center/path-portal.js +11 -3
- package/dist/server/device-center/relay.js +364 -35
- package/dist/server/device-center/report-metadata.js +66 -0
- package/dist/server/device-center/report-upload.js +63 -0
- package/dist/server/device-center/runtime.js +598 -41
- package/dist/server/device-center/smoke-preview.js +3 -0
- package/dist/server/device-center/store.js +203 -41
- package/dist/server/device-center/task-context-budget.js +161 -0
- package/dist/server/device-center/task-journal.js +385 -0
- package/dist/server/device-center/task-plan.js +33 -0
- package/dist/server/device-center/task-progress.js +141 -0
- package/dist/server/device-center/task-recovery.js +452 -0
- package/dist/server/device-center/task-report-outbox.js +216 -0
- package/dist/server/device-center/task-report.js +384 -0
- package/dist/server/device-center/task-scheduler.js +445 -0
- package/dist/server/device-center/task-state.js +333 -0
- package/dist/server/device-center/transfer-stream.js +1006 -0
- package/dist/server/device-center/transfers.js +841 -8
- package/dist/server/dispatch-domain-handlers.js +35 -6
- package/dist/server/dsh/dsh-agent-service.js +6 -0
- package/dist/server/durable/durable-capabilities.js +11 -0
- package/dist/server/engine-probe.js +112 -1
- package/dist/server/global-loader.js +119 -19
- package/dist/server/goal-service.js +10 -5
- package/dist/server/index.js +222 -29
- package/dist/server/managed.js +2 -0
- package/dist/server/markers/builtins/services.js +123 -0
- package/dist/server/markers/builtins/todo.js +189 -0
- package/dist/server/markers-list-tool.js +34 -0
- package/dist/server/message-deletion.js +40 -0
- package/dist/server/message-window.js +9 -0
- package/dist/server/model-admin.js +48 -6
- package/dist/server/model-enrich.js +10 -1
- package/dist/server/orchestrator.js +109 -0
- package/dist/server/patch-executable-lookup.js +396 -0
- package/dist/server/payload-limits.js +2 -0
- package/dist/server/plugin-llm.js +100 -9
- package/dist/server/plugins.js +65 -7
- package/dist/server/preset-fields.js +24 -0
- package/dist/server/preset-share.js +2 -0
- package/dist/server/prompt-base-store.js +85 -0
- package/dist/server/prompt-composer.js +44 -2
- package/dist/server/protocol-version.js +1 -1
- package/dist/server/protocol.js +2 -2
- package/dist/server/reasoning-registry.js +207 -0
- package/dist/server/remote-ssh-service.js +22 -9
- package/dist/server/resolve-global-sdk.js +2 -0
- package/dist/server/session-search.js +1 -1
- package/dist/server/settings-service.js +90 -39
- package/dist/server/skill-content-filler.js +29 -8
- package/dist/server/skill-tool.js +12 -3
- package/dist/server/startup-banner.js +35 -0
- package/dist/server/subagent-helpers.js +4 -1
- package/dist/server/subagents.js +52 -37
- package/dist/server/system-prompt-soul.js +41 -0
- package/dist/server/tool-host-factories.js +1 -0
- package/dist/server/tool-manager.js +17 -9
- package/dist/server/unauthorized-page.js +319 -0
- package/dist/server/univer-service.js +448 -0
- package/dist/server/universal/drivers/dsh-driver.js +379 -0
- package/dist/server/universal/universal-session.js +5 -0
- package/dist/server/webui-context.js +25 -5
- package/dist/test-artifact.md +9 -0
- package/dist/test-preview-modal.md +11 -0
- package/dist/test-report.html +20 -0
- package/docs/README.md +94 -0
- package/docs/architecture-plugins.md +946 -0
- package/docs/device-center.md +106 -12
- package/docs/dsh-engine.md +10 -10
- package/extensions/webui.ts +43 -1
- package/package.json +2 -1
- package/themes/aetheris.css +5 -0
- package/themes/ayu-light.css +6 -0
- package/themes/bases/flow-base.css +68 -68
- package/themes/catppuccin-latte.css +6 -0
- package/themes/catppuccin.css +6 -0
- package/themes/claude-code-dark.css +5 -0
- package/themes/codex.css +6 -0
- package/themes/cyberpunk.css +6 -0
- package/themes/dark-teal.css +6 -1
- package/themes/dazzle.css +6 -0
- package/themes/everforest-light.css +6 -0
- package/themes/flow-light.css +8 -1
- package/themes/flow.css +6 -1
- package/themes/geist.css +6 -0
- package/themes/gruvbox-light.css +6 -0
- package/themes/kanagawa-lotus.css +6 -0
- package/themes/md-preview.css +6 -0
- package/themes/mist.css +6 -0
- package/themes/nord.css +6 -0
- package/themes/one-dark.css +6 -0
- package/themes/paper.css +6 -0
- package/themes/rose-pine-dawn.css +6 -0
- package/themes/sakura.css +6 -0
- package/themes/solarized-light.css +6 -0
- package/themes/tokyo-night.css +6 -0
- package/themes/translucent.css +6 -0
- package/themes/transparent.css +6 -0
- package/themes/vs-code-dark.css +5 -0
- package/themes/wallpaper-flow.css +5 -1
- package/themes/wallpaper-obsidian.css +6 -0
- package/themes/white.css +6 -0
- package/themes/zhujian-dark.css +6 -1
- package/themes/zhujian.css +6 -1
- package/themes/zhupi-dark.css +6 -1
- package/themes/zhupi.css +6 -1
- package/web/dist/.build-assets.json +1 -1
- package/web/dist/assets/BgTasksModal-BDoOpj-F.js +1 -0
- package/web/dist/assets/BgTasksModal-C0i3O1FT.js +1 -0
- package/web/dist/assets/{CanvasStudio-BSg9ghPV.js → CanvasStudio-B8thymBN.js} +3 -3
- package/web/dist/assets/{CanvasStudio-DxW7C3Sz.js → CanvasStudio-zSxyIx1R.js} +3 -3
- package/web/dist/assets/DeviceCenter-DfAHKcid.js +3 -0
- package/web/dist/assets/DeviceCenter-ydvOTlzE.js +3 -0
- package/web/dist/assets/DeviceExecutionSettings-q5Ik8oXg.js +1 -0
- package/web/dist/assets/DeviceGroups-CI14_SRb.js +1 -0
- package/web/dist/assets/DeviceGroups-DvsB5IEU.js +1 -0
- package/web/dist/assets/Dropdown-C4LIhshN.js +1 -0
- package/web/dist/assets/FilePreview-BgGongmv.js +2 -0
- package/web/dist/assets/FilePreview-D4NrhF-U.js +2 -0
- package/web/dist/assets/GlobalSearchModal-BWgU8yPd.js +1 -0
- package/web/dist/assets/GlobalSearchModal-F7fNimKk.js +1 -0
- package/web/dist/assets/GroupConversation-Dh71Pdb-.js +17 -0
- package/web/dist/assets/GroupConversation-ZK3nRmNm.js +17 -0
- package/web/dist/assets/HintTip-c6d0TJoh.js +1 -0
- package/web/dist/assets/ModelConfigModal-YVEgyy34.js +2 -0
- package/web/dist/assets/ModelConfigModal-vWtg3srp.js +2 -0
- package/web/dist/assets/PiSetupModal-BmMrrgB9.js +1 -0
- package/web/dist/assets/ProviderOAuthControls-BGSBIKD7.js +1 -0
- package/web/dist/assets/SCMPanel-B8_EDxNr.js +2 -0
- package/web/dist/assets/SCMPanel-BU58qk3T.js +2 -0
- package/web/dist/assets/SettingsModal-CMUybq3J.js +32 -0
- package/web/dist/assets/SettingsModal-OBmGTh-5.js +32 -0
- package/web/dist/assets/TerminalPanel-0CYvD72B.js +3 -0
- package/web/dist/assets/TerminalPanel-DDavKvFr.js +3 -0
- package/web/dist/assets/TuiOverlayModal-BsA-QTvN.js +1 -0
- package/web/dist/assets/TuiOverlayModal-DzoVQBuG.js +1 -0
- package/web/dist/assets/app-globals-CZxzGvIo.js +123 -0
- package/web/dist/assets/canvas-client-CUnXlQYX.js +1 -0
- package/web/dist/assets/{desktop-7EvUHhKQ.js → desktop-aAKpvKJV.js} +1 -1
- package/web/dist/assets/group-view-state-CGCzYF8k.js +1 -0
- package/web/dist/assets/index-C6DwztqE.css +1 -0
- package/web/dist/assets/index-CuN802Sj.js +93 -0
- package/web/dist/assets/index-EH5Fw9fa.js +93 -0
- package/web/dist/assets/plugin-host-BRWib9DF.js +1 -0
- package/web/dist/assets/remote-ssh-client-CfYSMEZg.js +1 -0
- package/web/dist/assets/theme-D2l_mLNo.js +1 -0
- package/web/dist/index.html +10 -9
- package/web/dist/sw.js +1 -1
- package/web/dist/assets/BgTasksModal-BvnUHjFe.js +0 -1
- package/web/dist/assets/BgTasksModal-DSC8ncW3.js +0 -1
- package/web/dist/assets/DeviceCenter-Bmgr-Bwm.js +0 -3
- package/web/dist/assets/DeviceCenter-Cl9MBXaH.js +0 -3
- package/web/dist/assets/DeviceGroups-aeLD2msl.js +0 -1
- package/web/dist/assets/Dropdown-D1Jl35dw.js +0 -1
- package/web/dist/assets/FilePreview-6CqoDF_P.js +0 -2
- package/web/dist/assets/FilePreview-CJa27G_Q.js +0 -2
- package/web/dist/assets/GlobalSearchModal-CJX35rZL.js +0 -1
- package/web/dist/assets/GlobalSearchModal-CMW0xz0P.js +0 -1
- package/web/dist/assets/GroupConversation-Cq4daBFb.js +0 -1
- package/web/dist/assets/GroupConversation-DoELa83U.js +0 -1
- package/web/dist/assets/ModelConfigModal-Bp7tfKYh.js +0 -2
- package/web/dist/assets/PiSetupModal-DD8srwBJ.js +0 -1
- package/web/dist/assets/ProviderOAuthControls-C6tfXb88.js +0 -1
- package/web/dist/assets/SCMPanel-CghgsyBN.js +0 -2
- package/web/dist/assets/SCMPanel-D8-8aqwC.js +0 -2
- package/web/dist/assets/SettingsModal-BbBe2rYo.js +0 -20
- package/web/dist/assets/SettingsModal-qETiVPKj.js +0 -20
- package/web/dist/assets/TerminalPanel-Da-Xs4Wk.js +0 -3
- package/web/dist/assets/TerminalPanel-DnS4tFQO.js +0 -3
- package/web/dist/assets/TuiOverlayModal-CGyGkKYl.js +0 -1
- package/web/dist/assets/TuiOverlayModal-CpBKjG6x.js +0 -1
- package/web/dist/assets/app-globals-DcyO0UPi.js +0 -1
- package/web/dist/assets/fi-NyN-ZrgD.js +0 -325
- package/web/dist/assets/index-DK3F5ZJo.js +0 -85
- package/web/dist/assets/index-DQMii2he.js +0 -85
- package/web/dist/assets/index-vwBnwzkr.css +0 -1
- package/web/dist/assets/plugin-host-BCnX3P8U.js +0 -1
- package/web/dist/assets/theme-D2DMH6tH.js +0 -1
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, readdirSync, existsSync, rmSync, statSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
import { writeAtomicSync, writeJsonAtomicSync } from "../atomic-file.js";
|
|
5
|
+
export const TASK_REPORT_BYTES = 16 * 1024;
|
|
6
|
+
export const MAX_TASK_FULL_REPORT_BYTES = 4 * 1024 * 1024; // 4 MiB hard quota
|
|
7
|
+
export const MAX_TASK_REPORT_CHUNK_BYTES = 64 * 1024; // 64 KiB per transport chunk
|
|
8
|
+
/** Keep the opening context and final conclusions within the wire report budget. */
|
|
9
|
+
export function boundTaskReport(text, limit = TASK_REPORT_BYTES) {
|
|
10
|
+
const bytes = Buffer.from(text, "utf8");
|
|
11
|
+
if (bytes.length <= limit)
|
|
12
|
+
return text;
|
|
13
|
+
if (!Number.isSafeInteger(limit) || limit < 256)
|
|
14
|
+
throw new Error("Invalid report byte limit");
|
|
15
|
+
const marker = `\n[Report excerpt: ${bytes.length} original UTF-8 bytes; middle omitted]\n`;
|
|
16
|
+
const budget = limit - Buffer.byteLength(marker);
|
|
17
|
+
let start = Math.floor(budget / 2);
|
|
18
|
+
let end = bytes.length - (budget - start);
|
|
19
|
+
// UTF-8 continuation bytes cannot begin either retained boundary.
|
|
20
|
+
while (start > 0 && (bytes[start] & 0xc0) === 0x80)
|
|
21
|
+
start--;
|
|
22
|
+
while (end < bytes.length && (bytes[end] & 0xc0) === 0x80)
|
|
23
|
+
end++;
|
|
24
|
+
return bytes.subarray(0, start).toString("utf8") + marker + bytes.subarray(end).toString("utf8");
|
|
25
|
+
}
|
|
26
|
+
export class TaskReportQuotaError extends Error {
|
|
27
|
+
totalBytes;
|
|
28
|
+
quotaLimit;
|
|
29
|
+
constructor(message, totalBytes, quotaLimit) {
|
|
30
|
+
super(message);
|
|
31
|
+
this.totalBytes = totalBytes;
|
|
32
|
+
this.quotaLimit = quotaLimit;
|
|
33
|
+
this.name = "TaskReportQuotaError";
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export class TaskReportConflictError extends Error {
|
|
37
|
+
constructor(message) {
|
|
38
|
+
super(message);
|
|
39
|
+
this.name = "TaskReportConflictError";
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function sha256Hex(data) {
|
|
43
|
+
return createHash("sha256").update(data).digest("hex");
|
|
44
|
+
}
|
|
45
|
+
function safePathKey(taskId, attemptId) {
|
|
46
|
+
const raw = `${taskId}:${attemptId}`;
|
|
47
|
+
return sha256Hex(raw).slice(0, 32);
|
|
48
|
+
}
|
|
49
|
+
export function createTaskReportStore(baseDir, role = "hub") {
|
|
50
|
+
const reportsRoot = join(baseDir, "task-reports");
|
|
51
|
+
function attemptDir(taskId, attemptId) {
|
|
52
|
+
const key = safePathKey(taskId, attemptId);
|
|
53
|
+
return join(reportsRoot, key);
|
|
54
|
+
}
|
|
55
|
+
function readMeta(dir) {
|
|
56
|
+
const metaPath = join(dir, "report-meta.json");
|
|
57
|
+
if (!existsSync(metaPath))
|
|
58
|
+
return null;
|
|
59
|
+
try {
|
|
60
|
+
return JSON.parse(readFileSync(metaPath, "utf8"));
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
throw new Error("Report metadata is corrupted");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
removeReport(taskId, attemptId, accountId, roomId) {
|
|
68
|
+
const dir = attemptDir(taskId, attemptId);
|
|
69
|
+
const meta = readMeta(dir);
|
|
70
|
+
if (!meta || meta.accountId !== accountId || meta.roomId !== roomId)
|
|
71
|
+
return;
|
|
72
|
+
rmSync(dir, { recursive: true });
|
|
73
|
+
},
|
|
74
|
+
saveFullReport(taskId, attemptId, roomId, accountId, text, quotaLimit = MAX_TASK_FULL_REPORT_BYTES) {
|
|
75
|
+
if (!taskId || !attemptId || !roomId || !accountId) {
|
|
76
|
+
throw new Error("Missing required binding fields for task report");
|
|
77
|
+
}
|
|
78
|
+
const bytes = Buffer.byteLength(text, "utf8");
|
|
79
|
+
const dir = attemptDir(taskId, attemptId);
|
|
80
|
+
const ref = `${safePathKey(taskId, attemptId)}`;
|
|
81
|
+
const now = Date.now();
|
|
82
|
+
const existing = readMeta(dir);
|
|
83
|
+
if (existing && (existing.accountId !== accountId || existing.roomId !== roomId))
|
|
84
|
+
throw new TaskReportConflictError("Report account or room binding conflict");
|
|
85
|
+
if (existing?.complete) {
|
|
86
|
+
if (existing.sha256 === sha256Hex(text))
|
|
87
|
+
return existing;
|
|
88
|
+
throw new TaskReportConflictError("Completed task report cannot be overwritten");
|
|
89
|
+
}
|
|
90
|
+
// Pre-flight quota check before writing any bytes to disk
|
|
91
|
+
if (bytes > quotaLimit) {
|
|
92
|
+
mkdirSync(dir, { recursive: true });
|
|
93
|
+
const meta = {
|
|
94
|
+
taskId,
|
|
95
|
+
attemptId,
|
|
96
|
+
roomId,
|
|
97
|
+
accountId,
|
|
98
|
+
createdAt: now,
|
|
99
|
+
updatedAt: now,
|
|
100
|
+
totalBytes: bytes,
|
|
101
|
+
totalChunks: 1,
|
|
102
|
+
receivedChunks: 0,
|
|
103
|
+
storageLocation: { role, reportRef: ref },
|
|
104
|
+
quotaLimit,
|
|
105
|
+
quotaExceeded: true,
|
|
106
|
+
rejected: true,
|
|
107
|
+
complete: false,
|
|
108
|
+
};
|
|
109
|
+
writeJsonAtomicSync(join(dir, "report-meta.json"), meta);
|
|
110
|
+
throw new TaskReportQuotaError(`Task report exceeds maximum quota (${bytes} > ${quotaLimit} bytes). Rejected without partial truncation disguise.`, bytes, quotaLimit);
|
|
111
|
+
}
|
|
112
|
+
mkdirSync(dir, { recursive: true });
|
|
113
|
+
const hash = sha256Hex(text);
|
|
114
|
+
const meta = {
|
|
115
|
+
taskId,
|
|
116
|
+
attemptId,
|
|
117
|
+
roomId,
|
|
118
|
+
accountId,
|
|
119
|
+
createdAt: existing?.createdAt ?? now,
|
|
120
|
+
updatedAt: now,
|
|
121
|
+
totalBytes: bytes,
|
|
122
|
+
totalChunks: Math.max(1, Math.ceil(bytes / (32 * 1024))),
|
|
123
|
+
receivedChunks: Math.max(1, Math.ceil(bytes / (32 * 1024))),
|
|
124
|
+
storageLocation: { role, reportRef: ref },
|
|
125
|
+
quotaLimit,
|
|
126
|
+
quotaExceeded: false,
|
|
127
|
+
rejected: false,
|
|
128
|
+
complete: true,
|
|
129
|
+
sha256: hash,
|
|
130
|
+
};
|
|
131
|
+
writeAtomicSync(join(dir, "full-report.txt"), text);
|
|
132
|
+
writeJsonAtomicSync(join(dir, "report-meta.json"), meta);
|
|
133
|
+
return meta;
|
|
134
|
+
},
|
|
135
|
+
appendReportChunk(input, roomId, accountId, quotaLimit = MAX_TASK_FULL_REPORT_BYTES) {
|
|
136
|
+
const { taskId, attemptId, chunkIndex, totalChunks, text, sha256 } = input;
|
|
137
|
+
if (!taskId || !attemptId || !roomId || !accountId) {
|
|
138
|
+
throw new Error("Missing required binding fields for report chunk");
|
|
139
|
+
}
|
|
140
|
+
if (!Number.isSafeInteger(chunkIndex) || chunkIndex < 0)
|
|
141
|
+
throw new Error("Invalid chunkIndex");
|
|
142
|
+
if (!Number.isSafeInteger(totalChunks) || totalChunks <= 0 || totalChunks > 4096 || chunkIndex >= totalChunks) {
|
|
143
|
+
throw new Error("Invalid totalChunks");
|
|
144
|
+
}
|
|
145
|
+
const chunkBytes = Buffer.byteLength(text, "utf8");
|
|
146
|
+
if (chunkBytes > MAX_TASK_REPORT_CHUNK_BYTES) {
|
|
147
|
+
throw new Error(`Report chunk exceeds single chunk budget (${chunkBytes} > ${MAX_TASK_REPORT_CHUNK_BYTES})`);
|
|
148
|
+
}
|
|
149
|
+
const chunkHash = sha256Hex(text);
|
|
150
|
+
if (sha256 && sha256 !== chunkHash) {
|
|
151
|
+
throw new Error("Chunk integrity hash mismatch");
|
|
152
|
+
}
|
|
153
|
+
const dir = attemptDir(taskId, attemptId);
|
|
154
|
+
const chunksDir = join(dir, "chunks");
|
|
155
|
+
const ref = safePathKey(taskId, attemptId);
|
|
156
|
+
const existing = readMeta(dir);
|
|
157
|
+
if (existing) {
|
|
158
|
+
if (existing.totalChunks !== totalChunks)
|
|
159
|
+
throw new TaskReportConflictError("Report chunk count changed");
|
|
160
|
+
if (existing.accountId !== accountId || existing.roomId !== roomId) {
|
|
161
|
+
throw new Error("Account or room mismatch for report binding");
|
|
162
|
+
}
|
|
163
|
+
if (existing.complete) {
|
|
164
|
+
// Check idempotence
|
|
165
|
+
const chunkFile = join(chunksDir, `chunk-${String(chunkIndex).padStart(4, "0")}.txt`);
|
|
166
|
+
if (existsSync(chunkFile)) {
|
|
167
|
+
const prev = readFileSync(chunkFile, "utf8");
|
|
168
|
+
if (sha256Hex(prev) === chunkHash)
|
|
169
|
+
return existing;
|
|
170
|
+
}
|
|
171
|
+
throw new TaskReportConflictError("Cannot overwrite chunk of an already completed report");
|
|
172
|
+
}
|
|
173
|
+
if (existing.quotaExceeded || existing.rejected) {
|
|
174
|
+
throw new TaskReportQuotaError("Task report was already rejected due to quota overflow", existing.totalBytes, quotaLimit);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
// Pre-flight check cumulative bytes with new chunk before disk write
|
|
178
|
+
mkdirSync(chunksDir, { recursive: true });
|
|
179
|
+
const chunkFile = join(chunksDir, `chunk-${String(chunkIndex).padStart(4, "0")}.txt`);
|
|
180
|
+
if (existsSync(chunkFile)) {
|
|
181
|
+
const existingText = readFileSync(chunkFile, "utf8");
|
|
182
|
+
if (sha256Hex(existingText) !== chunkHash)
|
|
183
|
+
throw new TaskReportConflictError(`Conflicting content for chunk ${chunkIndex}`);
|
|
184
|
+
// Rebuild metadata below: a process may have stopped after writing the chunk.
|
|
185
|
+
}
|
|
186
|
+
let currentAccumulated = 0;
|
|
187
|
+
const chunkFiles = readdirSync(chunksDir).filter((f) => f.startsWith("chunk-") && f.endsWith(".txt"));
|
|
188
|
+
for (const f of chunkFiles) {
|
|
189
|
+
currentAccumulated += Buffer.byteLength(readFileSync(join(chunksDir, f), "utf8"), "utf8");
|
|
190
|
+
}
|
|
191
|
+
const addedBytes = existsSync(chunkFile) ? 0 : chunkBytes;
|
|
192
|
+
if (currentAccumulated + addedBytes > quotaLimit) {
|
|
193
|
+
const meta = {
|
|
194
|
+
taskId,
|
|
195
|
+
attemptId,
|
|
196
|
+
roomId,
|
|
197
|
+
accountId,
|
|
198
|
+
createdAt: existing?.createdAt ?? Date.now(),
|
|
199
|
+
updatedAt: Date.now(),
|
|
200
|
+
totalBytes: currentAccumulated + chunkBytes,
|
|
201
|
+
totalChunks,
|
|
202
|
+
receivedChunks: chunkFiles.length,
|
|
203
|
+
storageLocation: { role, reportRef: ref },
|
|
204
|
+
quotaLimit,
|
|
205
|
+
quotaExceeded: true,
|
|
206
|
+
rejected: true,
|
|
207
|
+
complete: false,
|
|
208
|
+
};
|
|
209
|
+
writeJsonAtomicSync(join(dir, "report-meta.json"), meta);
|
|
210
|
+
throw new TaskReportQuotaError(`Accumulated report chunks exceed quota (${currentAccumulated + chunkBytes} > ${quotaLimit})`, currentAccumulated + chunkBytes, quotaLimit);
|
|
211
|
+
}
|
|
212
|
+
// Safe atomic write chunk
|
|
213
|
+
writeAtomicSync(chunkFile, text);
|
|
214
|
+
// Count all chunks
|
|
215
|
+
const allChunkFiles = readdirSync(chunksDir)
|
|
216
|
+
.filter((f) => f.startsWith("chunk-") && f.endsWith(".txt"))
|
|
217
|
+
.sort();
|
|
218
|
+
let totalBytes = 0;
|
|
219
|
+
const chunkContents = [];
|
|
220
|
+
let complete = false;
|
|
221
|
+
if (allChunkFiles.length === totalChunks) {
|
|
222
|
+
let allPresent = true;
|
|
223
|
+
for (let i = 0; i < totalChunks; i++) {
|
|
224
|
+
const cPath = join(chunksDir, `chunk-${String(i).padStart(4, "0")}.txt`);
|
|
225
|
+
if (!existsSync(cPath)) {
|
|
226
|
+
allPresent = false;
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
const content = readFileSync(cPath, "utf8");
|
|
230
|
+
chunkContents.push(content);
|
|
231
|
+
totalBytes += Buffer.byteLength(content, "utf8");
|
|
232
|
+
}
|
|
233
|
+
if (allPresent && chunkContents.length === totalChunks) {
|
|
234
|
+
complete = true;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
for (const f of allChunkFiles) {
|
|
239
|
+
totalBytes += Buffer.byteLength(readFileSync(join(chunksDir, f), "utf8"), "utf8");
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
let fullHash;
|
|
243
|
+
if (complete) {
|
|
244
|
+
const assembled = chunkContents.join("");
|
|
245
|
+
fullHash = sha256Hex(assembled);
|
|
246
|
+
if ((input.fullSha256 && input.fullSha256 !== fullHash) ||
|
|
247
|
+
(input.totalBytes !== undefined && input.totalBytes !== totalBytes))
|
|
248
|
+
throw new TaskReportConflictError("Assembled full report integrity mismatch");
|
|
249
|
+
writeAtomicSync(join(dir, "full-report.txt"), assembled);
|
|
250
|
+
}
|
|
251
|
+
const meta = {
|
|
252
|
+
taskId,
|
|
253
|
+
attemptId,
|
|
254
|
+
roomId,
|
|
255
|
+
accountId,
|
|
256
|
+
createdAt: existing?.createdAt ?? Date.now(),
|
|
257
|
+
updatedAt: Date.now(),
|
|
258
|
+
totalBytes,
|
|
259
|
+
totalChunks,
|
|
260
|
+
receivedChunks: allChunkFiles.length,
|
|
261
|
+
storageLocation: { role, reportRef: ref },
|
|
262
|
+
quotaLimit,
|
|
263
|
+
quotaExceeded: false,
|
|
264
|
+
rejected: false,
|
|
265
|
+
complete,
|
|
266
|
+
uploadPending: !complete,
|
|
267
|
+
missing: false,
|
|
268
|
+
...(fullHash ? { sha256: fullHash } : {}),
|
|
269
|
+
};
|
|
270
|
+
writeJsonAtomicSync(join(dir, "report-meta.json"), meta);
|
|
271
|
+
return meta;
|
|
272
|
+
},
|
|
273
|
+
getReportMeta(taskId, attemptId, accountId, roomId) {
|
|
274
|
+
const dir = attemptDir(taskId, attemptId);
|
|
275
|
+
const meta = readMeta(dir);
|
|
276
|
+
if (!meta)
|
|
277
|
+
return null;
|
|
278
|
+
if (meta.accountId !== accountId || meta.roomId !== roomId)
|
|
279
|
+
return null;
|
|
280
|
+
if (meta.complete) {
|
|
281
|
+
try {
|
|
282
|
+
const file = statSync(join(dir, "full-report.txt"));
|
|
283
|
+
if (!file.isFile() || file.size !== meta.totalBytes)
|
|
284
|
+
return { ...meta, complete: false, missing: true, uploadPending: false };
|
|
285
|
+
}
|
|
286
|
+
catch {
|
|
287
|
+
return { ...meta, complete: false, missing: true, uploadPending: false };
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return meta;
|
|
291
|
+
},
|
|
292
|
+
getReport(taskId, attemptId, accountId, roomId, options) {
|
|
293
|
+
const meta = this.getReportMeta(taskId, attemptId, accountId, roomId);
|
|
294
|
+
if (!meta)
|
|
295
|
+
return null;
|
|
296
|
+
const dir = attemptDir(taskId, attemptId);
|
|
297
|
+
const filePath = join(dir, "full-report.txt");
|
|
298
|
+
let text;
|
|
299
|
+
let hasMore = false;
|
|
300
|
+
let nextOffset = null;
|
|
301
|
+
let actualOffset = 0;
|
|
302
|
+
let limit = 65536;
|
|
303
|
+
let digest;
|
|
304
|
+
let sha256 = meta.sha256;
|
|
305
|
+
if (existsSync(filePath)) {
|
|
306
|
+
const buf = readFileSync(filePath);
|
|
307
|
+
const totalBytes = buf.length;
|
|
308
|
+
if (!sha256) {
|
|
309
|
+
sha256 = sha256Hex(buf);
|
|
310
|
+
}
|
|
311
|
+
digest = createHash("sha256")
|
|
312
|
+
.update(JSON.stringify([taskId, attemptId, options?.revision ?? 0, totalBytes, sha256]))
|
|
313
|
+
.digest("hex");
|
|
314
|
+
if (options?.expectedDigest && options.expectedDigest !== digest) {
|
|
315
|
+
return { meta, versionMismatch: true, digest, sha256 };
|
|
316
|
+
}
|
|
317
|
+
if (options?.offset !== undefined || options?.limit !== undefined) {
|
|
318
|
+
actualOffset = Math.max(0, options.offset ?? 0);
|
|
319
|
+
while (actualOffset > 0 && (buf[actualOffset] & 0xc0) === 0x80) {
|
|
320
|
+
actualOffset--;
|
|
321
|
+
}
|
|
322
|
+
limit = Math.max(1, Math.min(options.limit ?? 65536, 262144));
|
|
323
|
+
let end = Math.min(actualOffset + limit, totalBytes);
|
|
324
|
+
while (end > actualOffset && (buf[end] & 0xc0) === 0x80) {
|
|
325
|
+
end--;
|
|
326
|
+
}
|
|
327
|
+
if (end === actualOffset && actualOffset < totalBytes) {
|
|
328
|
+
end++;
|
|
329
|
+
while (end < totalBytes && (buf[end] & 0xc0) === 0x80)
|
|
330
|
+
end++;
|
|
331
|
+
}
|
|
332
|
+
text = buf.subarray(actualOffset, end).toString("utf8");
|
|
333
|
+
hasMore = end < totalBytes;
|
|
334
|
+
nextOffset = hasMore ? end : null;
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
if (totalBytes > 65536) {
|
|
338
|
+
let end = 65536;
|
|
339
|
+
while (end > 0 && (buf[end] & 0xc0) === 0x80)
|
|
340
|
+
end--;
|
|
341
|
+
text = buf.subarray(0, end).toString("utf8");
|
|
342
|
+
hasMore = true;
|
|
343
|
+
nextOffset = end;
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
text = buf.toString("utf8");
|
|
347
|
+
hasMore = false;
|
|
348
|
+
nextOffset = null;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return { meta, text, hasMore, nextOffset, offset: actualOffset, limit, digest, sha256 };
|
|
353
|
+
},
|
|
354
|
+
getReportChunk(taskId, attemptId, accountId, roomId, chunkIndex) {
|
|
355
|
+
const meta = this.getReportMeta(taskId, attemptId, accountId, roomId);
|
|
356
|
+
if (!meta || !Number.isSafeInteger(chunkIndex) || chunkIndex < 0 || chunkIndex >= meta.totalChunks)
|
|
357
|
+
return null;
|
|
358
|
+
const dir = attemptDir(taskId, attemptId);
|
|
359
|
+
const chunksDir = join(dir, "chunks");
|
|
360
|
+
const chunkFile = join(chunksDir, `chunk-${String(chunkIndex).padStart(4, "0")}.txt`);
|
|
361
|
+
let text;
|
|
362
|
+
if (existsSync(chunkFile)) {
|
|
363
|
+
text = readFileSync(chunkFile, "utf8");
|
|
364
|
+
}
|
|
365
|
+
else {
|
|
366
|
+
const fullPath = join(dir, "full-report.txt");
|
|
367
|
+
if (existsSync(fullPath)) {
|
|
368
|
+
const buf = readFileSync(fullPath);
|
|
369
|
+
const CHUNK_SIZE = 32 * 1024;
|
|
370
|
+
let start = chunkIndex * CHUNK_SIZE;
|
|
371
|
+
let end = Math.min((chunkIndex + 1) * CHUNK_SIZE, buf.length);
|
|
372
|
+
while (start > 0 && (buf[start] & 0xc0) === 0x80)
|
|
373
|
+
start--;
|
|
374
|
+
while (end > start && (buf[end] & 0xc0) === 0x80)
|
|
375
|
+
end--;
|
|
376
|
+
if (start < buf.length) {
|
|
377
|
+
text = buf.subarray(start, end).toString("utf8");
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return { meta, text: text ?? "" };
|
|
382
|
+
},
|
|
383
|
+
};
|
|
384
|
+
}
|