@songsid/agend 2.4.0-beta.22 → 2.4.0-beta.24

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.
Files changed (284) hide show
  1. package/dist/agent-cli-instructions.md +4 -3
  2. package/dist/agent-cli.js +32 -2
  3. package/dist/agent-cli.js.map +1 -1
  4. package/dist/agent-file-read.js +2 -2
  5. package/dist/agent-file-read.js.map +1 -1
  6. package/dist/agent-whats-new.js +17 -1
  7. package/dist/agent-whats-new.js.map +1 -1
  8. package/dist/auth-api.d.ts +2 -0
  9. package/dist/auth-api.js +5 -2
  10. package/dist/auth-api.js.map +1 -1
  11. package/dist/authority/web-authority.d.ts +17 -3
  12. package/dist/authority/web-authority.js +56 -3
  13. package/dist/authority/web-authority.js.map +1 -1
  14. package/dist/backend/claude-code.d.ts +15 -0
  15. package/dist/backend/claude-code.js +8 -3
  16. package/dist/backend/claude-code.js.map +1 -1
  17. package/dist/backend/claude-mcp-carry.d.ts +63 -0
  18. package/dist/backend/claude-mcp-carry.js +242 -0
  19. package/dist/backend/claude-mcp-carry.js.map +1 -0
  20. package/dist/build-provenance.json +1 -1
  21. package/dist/channel/mcp-tools.js +15 -7
  22. package/dist/channel/mcp-tools.js.map +1 -1
  23. package/dist/classic-channel-manager.d.ts +1 -1
  24. package/dist/classic-channel-manager.js +2 -2
  25. package/dist/classic-channel-manager.js.map +1 -1
  26. package/dist/config-validator.js +6 -0
  27. package/dist/config-validator.js.map +1 -1
  28. package/dist/contact-auth-api.js +3 -1
  29. package/dist/contact-auth-api.js.map +1 -1
  30. package/dist/contact-auth-service.js +1 -1
  31. package/dist/contact-auth-service.js.map +1 -1
  32. package/dist/contact-auth-store.d.ts +28 -2
  33. package/dist/contact-auth-store.js +72 -4
  34. package/dist/contact-auth-store.js.map +1 -1
  35. package/dist/contact-change-proposals.d.ts +18 -1
  36. package/dist/contact-change-proposals.js +38 -4
  37. package/dist/contact-change-proposals.js.map +1 -1
  38. package/dist/contact-state-view.d.ts +103 -0
  39. package/dist/contact-state-view.js +80 -0
  40. package/dist/contact-state-view.js.map +1 -0
  41. package/dist/daemon.d.ts +11 -0
  42. package/dist/daemon.js +66 -15
  43. package/dist/daemon.js.map +1 -1
  44. package/dist/dashboard-permissions.d.ts +8 -0
  45. package/dist/dashboard-permissions.js +8 -0
  46. package/dist/dashboard-permissions.js.map +1 -1
  47. package/dist/delivery-outbox.d.ts +15 -1
  48. package/dist/delivery-outbox.js +60 -2
  49. package/dist/delivery-outbox.js.map +1 -1
  50. package/dist/fleet-manager.d.ts +157 -15
  51. package/dist/fleet-manager.js +522 -78
  52. package/dist/fleet-manager.js.map +1 -1
  53. package/dist/general-knowledge/skills/admin/SKILL.md +52 -0
  54. package/dist/general-knowledge/skills/concepts/SKILL.md +31 -21
  55. package/dist/general-knowledge/skills/credential-profiles/SKILL.md +6 -0
  56. package/dist/general-knowledge/skills/cross-instance-messaging/SKILL.md +6 -3
  57. package/dist/general-knowledge/skills/fleet-config/SKILL.md +3 -3
  58. package/dist/general-knowledge/skills/hub/SKILL.md +6 -1
  59. package/dist/general-knowledge/skills/instance-lifecycle/SKILL.md +1 -1
  60. package/dist/general-knowledge/skills/task-board/SKILL.md +17 -0
  61. package/dist/general-knowledge/skills/web-appearance/SKILL.md +4 -0
  62. package/dist/general-knowledge/skills/web-dashboard/SKILL.md +75 -12
  63. package/dist/general-knowledge/skills/worker-collaboration/SKILL.md +1 -1
  64. package/dist/general-role.d.ts +1 -1
  65. package/dist/general-role.js +1 -0
  66. package/dist/general-role.js.map +1 -1
  67. package/dist/hub/client.js +6 -1
  68. package/dist/hub/client.js.map +1 -1
  69. package/dist/hub/compose/member.d.ts +2 -1
  70. package/dist/hub/compose/member.js +3 -2
  71. package/dist/hub/compose/member.js.map +1 -1
  72. package/dist/hub/compose/server.js +2 -1
  73. package/dist/hub/compose/server.js.map +1 -1
  74. package/dist/hub/http/compose.js +5 -0
  75. package/dist/hub/http/compose.js.map +1 -1
  76. package/dist/hub/protocol.d.ts +21 -0
  77. package/dist/hub/protocol.js +3 -0
  78. package/dist/hub/protocol.js.map +1 -1
  79. package/dist/hub/question-close-model.d.ts +17 -0
  80. package/dist/hub/question-close-model.js +7 -0
  81. package/dist/hub/question-close-model.js.map +1 -0
  82. package/dist/hub/representative.d.ts +5 -0
  83. package/dist/hub/representative.js +51 -1
  84. package/dist/hub/representative.js.map +1 -1
  85. package/dist/hub/rooms/direct-view.js +1 -0
  86. package/dist/hub/rooms/direct-view.js.map +1 -1
  87. package/dist/hub/rooms/types.d.ts +1 -1
  88. package/dist/hub/rooms.d.ts +3 -0
  89. package/dist/hub/rooms.js +27 -1
  90. package/dist/hub/rooms.js.map +1 -1
  91. package/dist/hub/server.js +6 -1
  92. package/dist/hub/server.js.map +1 -1
  93. package/dist/hub/types.d.ts +2 -2
  94. package/dist/human-delivery-receipts.d.ts +12 -2
  95. package/dist/human-delivery-receipts.js +19 -4
  96. package/dist/human-delivery-receipts.js.map +1 -1
  97. package/dist/instance-lifecycle.d.ts +2 -0
  98. package/dist/instance-lifecycle.js +29 -1
  99. package/dist/instance-lifecycle.js.map +1 -1
  100. package/dist/instructions.d.ts +1 -1
  101. package/dist/instructions.js +1 -1
  102. package/dist/instructions.js.map +1 -1
  103. package/dist/locale.js +110 -12
  104. package/dist/locale.js.map +1 -1
  105. package/dist/needs-you-hub.d.ts +3 -0
  106. package/dist/needs-you-hub.js +31 -5
  107. package/dist/needs-you-hub.js.map +1 -1
  108. package/dist/needs-you.d.ts +16 -1
  109. package/dist/needs-you.js +7 -1
  110. package/dist/needs-you.js.map +1 -1
  111. package/dist/office-share-page.js +2 -2
  112. package/dist/office-share-page.js.map +1 -1
  113. package/dist/office-share.js +2 -1
  114. package/dist/office-share.js.map +1 -1
  115. package/dist/outbound-handlers.d.ts +4 -0
  116. package/dist/outbound-handlers.js +69 -11
  117. package/dist/outbound-handlers.js.map +1 -1
  118. package/dist/outbound-schemas.d.ts +5 -1
  119. package/dist/outbound-schemas.js +10 -7
  120. package/dist/outbound-schemas.js.map +1 -1
  121. package/dist/public-web-gateway.js +21 -0
  122. package/dist/public-web-gateway.js.map +1 -1
  123. package/dist/reply-buttons-controller.d.ts +2 -1
  124. package/dist/reply-buttons-controller.js +1 -1
  125. package/dist/reply-buttons-controller.js.map +1 -1
  126. package/dist/request-context-compatibility.d.ts +20 -0
  127. package/dist/request-context-compatibility.js +74 -0
  128. package/dist/request-context-compatibility.js.map +1 -0
  129. package/dist/request-context-protocol.d.ts +137 -0
  130. package/dist/request-context-protocol.js +38 -0
  131. package/dist/request-context-protocol.js.map +1 -0
  132. package/dist/request-context-store.d.ts +25 -0
  133. package/dist/request-context-store.js +137 -0
  134. package/dist/request-context-store.js.map +1 -0
  135. package/dist/scheduler/db.js +26 -0
  136. package/dist/scheduler/db.js.map +1 -1
  137. package/dist/scheduler/task-board-text.d.ts +2 -0
  138. package/dist/scheduler/task-board-text.js +12 -0
  139. package/dist/scheduler/task-board-text.js.map +1 -1
  140. package/dist/scheduler/task-board.d.ts +102 -2
  141. package/dist/scheduler/task-board.js +242 -24
  142. package/dist/scheduler/task-board.js.map +1 -1
  143. package/dist/scheduler/task-files.d.ts +80 -0
  144. package/dist/scheduler/task-files.js +357 -0
  145. package/dist/scheduler/task-files.js.map +1 -0
  146. package/dist/settings-api.d.ts +2 -0
  147. package/dist/settings-api.js +6 -1
  148. package/dist/settings-api.js.map +1 -1
  149. package/dist/settings-baseline.d.ts +1 -0
  150. package/dist/settings-baseline.js +1 -1
  151. package/dist/settings-baseline.js.map +1 -1
  152. package/dist/settings-effect.d.ts +2 -0
  153. package/dist/settings-effect.js +5 -1
  154. package/dist/settings-effect.js.map +1 -1
  155. package/dist/task-attach-client.d.ts +43 -0
  156. package/dist/task-attach-client.js +125 -0
  157. package/dist/task-attach-client.js.map +1 -0
  158. package/dist/task-board-tool.d.ts +12 -0
  159. package/dist/task-board-tool.js +107 -6
  160. package/dist/task-board-tool.js.map +1 -1
  161. package/dist/task-file-limits.d.ts +15 -0
  162. package/dist/task-file-limits.js +21 -0
  163. package/dist/task-file-limits.js.map +1 -0
  164. package/dist/tool-permissions.d.ts +32 -0
  165. package/dist/tool-permissions.js +64 -1
  166. package/dist/tool-permissions.js.map +1 -1
  167. package/dist/types.d.ts +6 -0
  168. package/dist/ui/accounts-keepalive.js +2 -1
  169. package/dist/ui/agent-details-settings.js +7 -5
  170. package/dist/ui/agent-who-can-use-strings.js +125 -0
  171. package/dist/ui/agent-who-can-use.js +238 -0
  172. package/dist/ui/chat-activity.js +2 -1
  173. package/dist/ui/chat-pins.js +2 -1
  174. package/dist/ui/claude-credentials.js +2 -1
  175. package/dist/ui/contact-auth-strings.js +28 -17
  176. package/dist/ui/contact-authorizations.js +6 -4
  177. package/dist/ui/contact-needs.js +6 -1
  178. package/dist/ui/contact-permissions.js +6 -4
  179. package/dist/ui/contact-tool-groups.js +1 -1
  180. package/dist/ui/contact-verify-need.js +46 -0
  181. package/dist/ui/fleet-cache.js +2 -1
  182. package/dist/ui/fleet-office-card.js +23 -5
  183. package/dist/ui/fleet-office-editor.js +5 -5
  184. package/dist/ui/fleet-office-pack.d.ts +10 -0
  185. package/dist/ui/fleet-office-pack.js +65 -2
  186. package/dist/ui/fleet-office-skin.js +52 -14
  187. package/dist/ui/fleet-office-store.js +33 -2
  188. package/dist/ui/fleet-office-vehicles.js +130 -0
  189. package/dist/ui/fleet-office-zones.js +27 -10
  190. package/dist/ui/fleet-office.js +128 -40
  191. package/dist/ui/fleet-org.js +6 -1
  192. package/dist/ui/fleet-tab-icons.js +19 -0
  193. package/dist/ui/fleet-task-activity.js +11 -83
  194. package/dist/ui/fleet-task-archive.js +111 -0
  195. package/dist/ui/fleet-task-card.js +6 -4
  196. package/dist/ui/fleet-task-common.js +2 -2
  197. package/dist/ui/fleet-task-edit.js +8 -1
  198. package/dist/ui/fleet-task-files.js +196 -0
  199. package/dist/ui/fleet-task-md.js +98 -0
  200. package/dist/ui/fleet-task-menu.js +1 -1
  201. package/dist/ui/fleet-task-stats.js +2 -2
  202. package/dist/ui/fleet-task-view.js +14 -9
  203. package/dist/ui/fleet-tasks.js +42 -18
  204. package/dist/ui/fleet-usage.js +2 -1
  205. package/dist/ui/hub-admin-overview.js +2 -1
  206. package/dist/ui/hub-agent-intro.js +2 -1
  207. package/dist/ui/hub-assistant-admin.js +2 -1
  208. package/dist/ui/hub-directory-notices.js +2 -1
  209. package/dist/ui/hub-enrolment-code.js +2 -1
  210. package/dist/ui/hub-my-agents.js +2 -1
  211. package/dist/ui/hub-quick-pairing.js +2 -1
  212. package/dist/ui/hub-role-audit.js +2 -1
  213. package/dist/ui/hub-role-forms.js +3 -2
  214. package/dist/ui/hub-role-view.js +3 -2
  215. package/dist/ui/hub-roles.js +2 -1
  216. package/dist/ui/hub-room-agent-strings.js +2 -1
  217. package/dist/ui/hub-room-list.js +2 -1
  218. package/dist/ui/hub-room-owner-requests.js +2 -1
  219. package/dist/ui/hub-room-pins.js +2 -1
  220. package/dist/ui/hub-room-strings.js +2 -2
  221. package/dist/ui/hub-room-thread.js +1 -1
  222. package/dist/ui/log-row.js +4 -3
  223. package/dist/ui/office-share-dialog.js +2 -1
  224. package/dist/ui/office-share-page.js +2 -1
  225. package/dist/ui/page-share-dialog.js +3 -2
  226. package/dist/ui/panel-chat.js +2 -1
  227. package/dist/ui/panel-details.js +5 -2
  228. package/dist/ui/panel-fleet.js +10 -5
  229. package/dist/ui/panel-hub.js +2 -1
  230. package/dist/ui/panel-needs.js +34 -6
  231. package/dist/ui/panel-notices.js +2 -1
  232. package/dist/ui/panel-runtimes.js +2 -1
  233. package/dist/ui/panel-settings.js +15 -1
  234. package/dist/ui/panel-windows-cli.js +3 -2
  235. package/dist/ui/runtime-cli-install.js +2 -1
  236. package/dist/ui/settings-appearance.js +2 -1
  237. package/dist/ui/settings-guests.js +4 -3
  238. package/dist/ui/settings-hub-member-address.js +2 -1
  239. package/dist/ui/settings-hub-setup.js +2 -1
  240. package/dist/ui/settings-hub-web-address.js +2 -1
  241. package/dist/ui/settings-hub.js +2 -1
  242. package/dist/ui/settings-registry.js +1 -0
  243. package/dist/ui/settings-shared-strings.js +9 -0
  244. package/dist/ui/settings-shared.js +6 -1
  245. package/dist/ui/settings-update.js +2 -1
  246. package/dist/ui/settings-web-authority.js +2 -1
  247. package/dist/ui/shared/agent-sheet.js +2 -1
  248. package/dist/ui/shared/app-i18n.js +2 -2
  249. package/dist/ui/shared/app-needs.js +8 -1
  250. package/dist/ui/shared/app-session.js +4 -3
  251. package/dist/ui/shared/app-shell.js +6 -2
  252. package/dist/ui/shared/app.css +93 -11
  253. package/dist/ui/shared/chat-strings.js +4 -4
  254. package/dist/ui/shared/chat-time.js +7 -4
  255. package/dist/ui/shared/clock-text.js +57 -0
  256. package/dist/ui/shared/device-roles.js +6 -5
  257. package/dist/ui/shared/instance-nav.js +17 -22
  258. package/dist/ui/shared/task-board.css +64 -0
  259. package/dist/ui/shared/usage-dialog.js +2 -1
  260. package/dist/ui/shared/view-strings.js +0 -14
  261. package/dist/ui/shared/web-authorize.js +3 -2
  262. package/dist/ui/subagent-row.js +2 -1
  263. package/dist/web-api.d.ts +18 -2
  264. package/dist/web-api.js +159 -27
  265. package/dist/web-api.js.map +1 -1
  266. package/dist/web-login.d.ts +6 -1
  267. package/dist/web-login.js +8 -3
  268. package/dist/web-login.js.map +1 -1
  269. package/dist/web-office-acts.d.ts +16 -0
  270. package/dist/web-office-acts.js +27 -9
  271. package/dist/web-office-acts.js.map +1 -1
  272. package/dist/web-office-pack-read.js +1 -1
  273. package/dist/web-office-pack-read.js.map +1 -1
  274. package/dist/web-office-skins.d.ts +5 -3
  275. package/dist/web-office-skins.js +20 -16
  276. package/dist/web-office-skins.js.map +1 -1
  277. package/dist/web-session-authorization.d.ts +3 -0
  278. package/dist/web-session-authorization.js +1 -1
  279. package/dist/web-session-authorization.js.map +1 -1
  280. package/dist/web-settings-patch.d.ts +27 -0
  281. package/dist/web-settings-patch.js +67 -0
  282. package/dist/web-settings-patch.js.map +1 -1
  283. package/dist/workflow-templates/default.md +1 -0
  284. package/package.json +1 -1
@@ -0,0 +1,80 @@
1
+ import { TASK_FILE_LIMITS, TASK_FILE_MAX_PARTS, TASK_FILE_PART_BYTES } from "../task-file-limits.js";
2
+ export { TASK_FILE_LIMITS, TASK_FILE_MAX_PARTS, TASK_FILE_PART_BYTES };
3
+ /** A staged file nobody used is dropped after this long; a part never finished too. */
4
+ export declare const PENDING_TTL_MS: number;
5
+ /** Staged (finished, unused) files one instance may hold at once. */
6
+ export declare const MAX_STAGED_PER_INSTANCE = 20;
7
+ export declare class TaskFileError extends Error {
8
+ }
9
+ export interface StagedFile {
10
+ /** The id it will keep as an attachment: 32 hex. */
11
+ id: string;
12
+ instance: string;
13
+ name: string;
14
+ size: number;
15
+ mime: string;
16
+ kind: "photo" | "document";
17
+ sha256: string;
18
+ }
19
+ export declare class TaskFileStore {
20
+ readonly dir: string;
21
+ private readonly now;
22
+ private readonly staged;
23
+ private readonly sessions;
24
+ constructor(dir: string, now?: () => number);
25
+ private pendingDir;
26
+ private folder;
27
+ /**
28
+ * One part (or the whole file, when it fits one message) of a file an agent stages. Part 0 opens the upload and answers
29
+ * `{ upload }`; the last part finishes it and answers `{ upload, name, size, done: true }`, the same id. A file
30
+ * that is empty, over the limit, out of order or whose bytes and sha256 do not match what part 0 declared is refused,
31
+ * and the upload with it.
32
+ */
33
+ receive(instance: string, args: {
34
+ name?: unknown;
35
+ data_b64?: unknown;
36
+ part?: unknown;
37
+ }): {
38
+ upload: string;
39
+ name: string;
40
+ size?: number;
41
+ done: boolean;
42
+ };
43
+ private tooLarge;
44
+ private open;
45
+ private finish;
46
+ private drop;
47
+ /** Drop what waited too long: a part never finished, a staged file nobody used. */
48
+ sweep(): void;
49
+ /**
50
+ * The staged files `instance` no longer wants — the write that was to use them was refused, or the client gave up half-way: gone at once,
51
+ * not after the wait, so a refused write tried again and again never fills the instance's room (MAX_STAGED_PER_INSTANCE). Another
52
+ * instance's, an unknown id and one already used are left as they are. Returns how many were dropped.
53
+ */
54
+ release(instance: string, ids: readonly string[]): number;
55
+ /**
56
+ * The staged files `instance` names, checked — each one its own, finished, unused, not named twice — or an error that
57
+ * says which. Nothing moves yet (`commit`), so a refusal later in the same write leaves them staged.
58
+ */
59
+ adopt(instance: string, ids: readonly string[]): StagedFile[];
60
+ /**
61
+ * Move a staged file under its card; returns a function that moves it back (the write that linked it failed after).
62
+ * A staged file is the fleet's own regular file (`receive` wrote it with O_EXCL, 0600): a final check that it still is.
63
+ */
64
+ commit(taskUuid: string, file: StagedFile): () => void;
65
+ /** A card's file on disk, by the ids the table holds; null when either is not what it should be, or the file is gone. */
66
+ pathOf(taskUuid: string, id: string): {
67
+ path: string;
68
+ size: number;
69
+ } | null;
70
+ /** A card's folder and everything in it (the card was pruned). */
71
+ removeCard(taskUuid: string): void;
72
+ /** One file (its row is gone). */
73
+ removeFile(taskUuid: string, id: string): void;
74
+ /**
75
+ * Files on disk the table does not know — a folder for a card that no longer exists, a file whose row never committed,
76
+ * staged files from before a restart. `known(uuid)` lists the ids the table holds for a card, or null for a card the
77
+ * board no longer has.
78
+ */
79
+ sweepOrphans(known: (taskUuid: string) => ReadonlySet<string> | null): number;
80
+ }
@@ -0,0 +1,357 @@
1
+ /**
2
+ * Files on task cards. An agent hands the person what it made as a file ON the card — a design document, an
3
+ * HTML prototype, a screenshot — instead of a path on its own machine the person cannot open.
4
+ *
5
+ * Two doors, both on the agent's side first:
6
+ * 1. The agent's own process (the `agend-agent` CLI, or the instance's daemon for the MCP `task` tool) reads the file
7
+ * (readAgentFile: inside its working directory, no symlink, within the size limit) and STAGES it here in parts
8
+ * (`receive`): the fleet never opens a path an agent names. A staged file is one the fleet holds under an id.
9
+ * 2. The write that carries the card's comment (update, move, attach) names the ids it staged, and the board LINKS them
10
+ * to that comment (`adopt` checks them, `commit` moves them under the card). One write: the comment, the files, and
11
+ * a status change if it came with one, all or nothing.
12
+ *
13
+ * On disk: `<AGEND_HOME>/task-files/<card uuid>/<id>` (0600 in a 0700 folder). The name on disk is the id alone; the
14
+ * file's own name lives in the table (`task_attachments`) and is only ever a label. Staged files wait in
15
+ * `task-files/pending/` until used, and leave after PENDING_TTL_MS. Nothing here is served as a page: the dashboard
16
+ * sends a file as a download (an image only as an image), never as HTML (web-api.ts, GET /ui/tasks/files/<id>).
17
+ */
18
+ import { createHash, randomBytes } from "node:crypto";
19
+ import { closeSync, constants as fsConstants, fstatSync, mkdirSync, openSync, readdirSync, renameSync, rmSync, statSync, unlinkSync, writeSync } from "node:fs";
20
+ import { join } from "node:path";
21
+ import { TASK_FILE_LIMITS, TASK_FILE_MAX_PARTS, TASK_FILE_PART_BYTES } from "../task-file-limits.js";
22
+ import { UploadSniffer, displayName, isFileId } from "../web-upload.js";
23
+ export { TASK_FILE_LIMITS, TASK_FILE_MAX_PARTS, TASK_FILE_PART_BYTES };
24
+ const MiB = 1024 * 1024;
25
+ /** A staged file nobody used is dropped after this long; a part never finished too. */
26
+ export const PENDING_TTL_MS = 30 * 60_000;
27
+ /** Staged (finished, unused) files one instance may hold at once. */
28
+ export const MAX_STAGED_PER_INSTANCE = 20;
29
+ export class TaskFileError extends Error {
30
+ }
31
+ const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
32
+ const PENDING = "pending";
33
+ /** What a part says, checked: every field is plain data from an agent, never trusted. */
34
+ function partOf(raw) {
35
+ if (!raw || typeof raw !== "object" || Array.isArray(raw))
36
+ return null;
37
+ const p = raw;
38
+ const whole = (v) => typeof v === "number" && Number.isSafeInteger(v) && v >= 0;
39
+ if (!whole(p.index) || !whole(p.count) || !whole(p.bytes) || typeof p.sha256 !== "string" || !/^[0-9a-f]{64}$/.test(p.sha256))
40
+ return null;
41
+ if (p.upload !== undefined && (typeof p.upload !== "string" || !isFileId(p.upload)))
42
+ return null;
43
+ return { index: p.index, count: p.count, bytes: p.bytes, sha256: p.sha256, ...(p.upload ? { upload: p.upload } : {}) };
44
+ }
45
+ export class TaskFileStore {
46
+ dir;
47
+ now;
48
+ staged = new Map();
49
+ sessions = new Map(); // by instance: one file in parts at a time
50
+ constructor(dir, now = () => Date.now()) {
51
+ this.dir = dir;
52
+ this.now = now;
53
+ }
54
+ pendingDir() { return join(this.dir, PENDING); }
55
+ folder(path) { mkdirSync(path, { recursive: true, mode: 0o700 }); }
56
+ /**
57
+ * One part (or the whole file, when it fits one message) of a file an agent stages. Part 0 opens the upload and answers
58
+ * `{ upload }`; the last part finishes it and answers `{ upload, name, size, done: true }`, the same id. A file
59
+ * that is empty, over the limit, out of order or whose bytes and sha256 do not match what part 0 declared is refused,
60
+ * and the upload with it.
61
+ */
62
+ receive(instance, args) {
63
+ this.sweep();
64
+ if (typeof args.data_b64 !== "string")
65
+ throw new TaskFileError("data_b64 (the file's bytes, base64) is required");
66
+ const chunk = Buffer.from(args.data_b64, "base64");
67
+ const part = args.part === undefined ? null : partOf(args.part);
68
+ if (args.part !== undefined && !part)
69
+ throw new TaskFileError("the part is malformed");
70
+ if (!part) {
71
+ if (chunk.length > TASK_FILE_PART_BYTES)
72
+ throw new TaskFileError(`a file over ${TASK_FILE_PART_BYTES} bytes comes in parts`);
73
+ return this.finish(this.open(instance, args.name, 1, chunk.length, createHash("sha256").update(chunk).digest("hex")), chunk);
74
+ }
75
+ if (part.count < 1 || part.count > TASK_FILE_MAX_PARTS || part.index >= part.count)
76
+ throw new TaskFileError("the part count is out of range");
77
+ if (part.bytes < 1 || part.bytes > TASK_FILE_LIMITS.fileBytes)
78
+ throw new TaskFileError(this.tooLarge(part.bytes));
79
+ if (chunk.length > TASK_FILE_PART_BYTES)
80
+ throw new TaskFileError("a part is too large");
81
+ let session;
82
+ if (part.index === 0)
83
+ session = this.open(instance, args.name, part.count, part.bytes, part.sha256);
84
+ else {
85
+ session = this.sessions.get(instance);
86
+ if (!session || session.id !== part.upload || session.next !== part.index || session.count !== part.count || session.bytes !== part.bytes || session.sha256 !== part.sha256) {
87
+ if (session)
88
+ this.drop(session);
89
+ throw new TaskFileError("the upload is out of order or expired; start the file again");
90
+ }
91
+ }
92
+ return this.finish(session, chunk);
93
+ }
94
+ tooLarge(bytes) {
95
+ return `a file is at most ${Math.floor(TASK_FILE_LIMITS.fileBytes / MiB)} MiB (this one is ${(bytes / MiB).toFixed(1)} MiB)`;
96
+ }
97
+ open(instance, rawName, count, bytes, sha256) {
98
+ if (bytes < 1)
99
+ throw new TaskFileError("the file is empty");
100
+ if (bytes > TASK_FILE_LIMITS.fileBytes)
101
+ throw new TaskFileError(this.tooLarge(bytes));
102
+ let held = 0;
103
+ for (const f of this.staged.values())
104
+ if (f.instance === instance)
105
+ held++;
106
+ if (held >= MAX_STAGED_PER_INSTANCE)
107
+ throw new TaskFileError(`${held} files are waiting for a comment already; attach them first`);
108
+ const old = this.sessions.get(instance);
109
+ if (old)
110
+ this.drop(old);
111
+ this.folder(this.pendingDir());
112
+ const id = randomBytes(16).toString("hex");
113
+ const path = join(this.pendingDir(), `${id}.part`);
114
+ const fd = openSync(path, fsConstants.O_WRONLY | fsConstants.O_CREAT | fsConstants.O_EXCL | fsConstants.O_NOFOLLOW, 0o600);
115
+ const session = {
116
+ id, instance, name: displayName(typeof rawName === "string" ? rawName : "", "file"), count, bytes, sha256, next: 0, written: 0, fd, path,
117
+ sniffer: new UploadSniffer(), hash: createHash("sha256"), at: this.now(),
118
+ };
119
+ this.sessions.set(instance, session);
120
+ return session;
121
+ }
122
+ finish(session, chunk) {
123
+ try {
124
+ if (session.written + chunk.length > session.bytes)
125
+ throw new TaskFileError("more bytes than the file declared");
126
+ if (chunk.length) {
127
+ let off = 0;
128
+ while (off < chunk.length)
129
+ off += writeSync(session.fd, chunk, off, chunk.length - off);
130
+ session.sniffer.push(chunk);
131
+ session.hash.update(chunk);
132
+ session.written += chunk.length;
133
+ }
134
+ session.next++;
135
+ session.at = this.now();
136
+ if (session.next < session.count)
137
+ return { upload: session.id, name: session.name, done: false };
138
+ if (session.written !== session.bytes)
139
+ throw new TaskFileError("the parts do not add up to the file's size");
140
+ if (session.hash.digest("hex") !== session.sha256)
141
+ throw new TaskFileError("the file's checksum does not match");
142
+ const type = session.sniffer.result(session.name);
143
+ if (!type)
144
+ throw new TaskFileError("the file is empty");
145
+ closeSync(session.fd);
146
+ session.fd = -1;
147
+ const final = join(this.pendingDir(), session.id);
148
+ renameSync(session.path, final);
149
+ this.sessions.delete(session.instance);
150
+ this.staged.set(session.id, {
151
+ id: session.id, instance: session.instance, name: session.name, size: session.written, mime: type.mime, kind: type.kind, sha256: session.sha256,
152
+ expiresAt: this.now() + PENDING_TTL_MS,
153
+ });
154
+ return { upload: session.id, name: session.name, size: session.written, done: true };
155
+ }
156
+ catch (err) {
157
+ this.drop(session);
158
+ throw err;
159
+ }
160
+ }
161
+ drop(session) {
162
+ if (session.fd >= 0) {
163
+ try {
164
+ closeSync(session.fd);
165
+ }
166
+ catch { /* closed */ }
167
+ session.fd = -1;
168
+ }
169
+ try {
170
+ unlinkSync(session.path);
171
+ }
172
+ catch { /* gone */ }
173
+ if (this.sessions.get(session.instance) === session)
174
+ this.sessions.delete(session.instance);
175
+ }
176
+ /** Drop what waited too long: a part never finished, a staged file nobody used. */
177
+ sweep() {
178
+ const t = this.now();
179
+ for (const s of this.sessions.values())
180
+ if (t - s.at > PENDING_TTL_MS)
181
+ this.drop(s);
182
+ for (const [id, f] of this.staged) {
183
+ if (f.expiresAt > t)
184
+ continue;
185
+ this.staged.delete(id);
186
+ try {
187
+ unlinkSync(join(this.pendingDir(), id));
188
+ }
189
+ catch { /* gone */ }
190
+ }
191
+ }
192
+ /**
193
+ * The staged files `instance` no longer wants — the write that was to use them was refused, or the client gave up half-way: gone at once,
194
+ * not after the wait, so a refused write tried again and again never fills the instance's room (MAX_STAGED_PER_INSTANCE). Another
195
+ * instance's, an unknown id and one already used are left as they are. Returns how many were dropped.
196
+ */
197
+ release(instance, ids) {
198
+ let dropped = 0;
199
+ for (const id of ids) {
200
+ const f = isFileId(id) ? this.staged.get(id) : undefined;
201
+ if (!f || f.instance !== instance)
202
+ continue;
203
+ this.staged.delete(id);
204
+ try {
205
+ unlinkSync(join(this.pendingDir(), id));
206
+ }
207
+ catch { /* gone */ }
208
+ dropped++;
209
+ }
210
+ return dropped;
211
+ }
212
+ /**
213
+ * The staged files `instance` names, checked — each one its own, finished, unused, not named twice — or an error that
214
+ * says which. Nothing moves yet (`commit`), so a refusal later in the same write leaves them staged.
215
+ */
216
+ adopt(instance, ids) {
217
+ this.sweep();
218
+ if (!ids.length)
219
+ return [];
220
+ if (ids.length > TASK_FILE_LIMITS.perWrite)
221
+ throw new TaskFileError(`at most ${TASK_FILE_LIMITS.perWrite} files in one write`);
222
+ const seen = new Set();
223
+ return ids.map((id) => {
224
+ const f = isFileId(id) ? this.staged.get(id) : undefined;
225
+ if (!f || f.instance !== instance)
226
+ throw new TaskFileError("an attached file is no longer staged (it waited too long, or was used); attach it again");
227
+ if (seen.has(id))
228
+ throw new TaskFileError("the same file is named twice");
229
+ seen.add(id);
230
+ const { expiresAt: _e, ...file } = f;
231
+ return file;
232
+ });
233
+ }
234
+ /**
235
+ * Move a staged file under its card; returns a function that moves it back (the write that linked it failed after).
236
+ * A staged file is the fleet's own regular file (`receive` wrote it with O_EXCL, 0600): a final check that it still is.
237
+ */
238
+ commit(taskUuid, file) {
239
+ if (!UUID.test(taskUuid) || !isFileId(file.id))
240
+ throw new TaskFileError("not a card file");
241
+ const from = join(this.pendingDir(), file.id);
242
+ const folder = join(this.dir, taskUuid);
243
+ this.folder(folder);
244
+ const to = join(folder, file.id);
245
+ const fd = openSync(from, fsConstants.O_RDONLY | fsConstants.O_NOFOLLOW);
246
+ try {
247
+ if (!fstatSync(fd).isFile())
248
+ throw new TaskFileError("not a regular file");
249
+ }
250
+ finally {
251
+ closeSync(fd);
252
+ }
253
+ renameSync(from, to);
254
+ this.staged.delete(file.id);
255
+ return () => {
256
+ try {
257
+ renameSync(to, from);
258
+ }
259
+ catch { /* the sweep removes it */ }
260
+ this.staged.set(file.id, { ...file, expiresAt: this.now() + PENDING_TTL_MS });
261
+ };
262
+ }
263
+ /** A card's file on disk, by the ids the table holds; null when either is not what it should be, or the file is gone. */
264
+ pathOf(taskUuid, id) {
265
+ if (!UUID.test(taskUuid) || !isFileId(id))
266
+ return null;
267
+ const path = join(this.dir, taskUuid, id);
268
+ try {
269
+ const st = statSync(path);
270
+ return st.isFile() ? { path, size: st.size } : null;
271
+ }
272
+ catch {
273
+ return null;
274
+ }
275
+ }
276
+ /** A card's folder and everything in it (the card was pruned). */
277
+ removeCard(taskUuid) {
278
+ if (!UUID.test(taskUuid))
279
+ return;
280
+ try {
281
+ rmSync(join(this.dir, taskUuid), { recursive: true, force: true });
282
+ }
283
+ catch { /* the next sweep tries again */ }
284
+ }
285
+ /** One file (its row is gone). */
286
+ removeFile(taskUuid, id) {
287
+ if (!UUID.test(taskUuid) || !isFileId(id))
288
+ return;
289
+ try {
290
+ unlinkSync(join(this.dir, taskUuid, id));
291
+ }
292
+ catch { /* gone */ }
293
+ }
294
+ /**
295
+ * Files on disk the table does not know — a folder for a card that no longer exists, a file whose row never committed,
296
+ * staged files from before a restart. `known(uuid)` lists the ids the table holds for a card, or null for a card the
297
+ * board no longer has.
298
+ */
299
+ sweepOrphans(known) {
300
+ let removed = 0;
301
+ let entries = [];
302
+ try {
303
+ entries = readdirSync(this.dir);
304
+ }
305
+ catch {
306
+ return 0;
307
+ }
308
+ const held = new Set([...this.staged.keys(), ...[...this.sessions.values()].map((s) => `${s.id}.part`)]);
309
+ for (const name of entries) {
310
+ const folder = join(this.dir, name);
311
+ if (name === PENDING) {
312
+ let pend = [];
313
+ try {
314
+ pend = readdirSync(folder);
315
+ }
316
+ catch {
317
+ continue;
318
+ }
319
+ for (const f of pend) {
320
+ if (held.has(f))
321
+ continue;
322
+ try {
323
+ unlinkSync(join(folder, f));
324
+ removed++;
325
+ }
326
+ catch { /* gone */ }
327
+ }
328
+ continue;
329
+ }
330
+ if (!UUID.test(name))
331
+ continue;
332
+ const ids = known(name);
333
+ if (!ids) {
334
+ this.removeCard(name);
335
+ removed++;
336
+ continue;
337
+ }
338
+ let files = [];
339
+ try {
340
+ files = readdirSync(folder);
341
+ }
342
+ catch {
343
+ continue;
344
+ }
345
+ for (const f of files)
346
+ if (!ids.has(f)) {
347
+ try {
348
+ unlinkSync(join(folder, f));
349
+ removed++;
350
+ }
351
+ catch { /* gone */ }
352
+ }
353
+ }
354
+ return removed;
355
+ }
356
+ }
357
+ //# sourceMappingURL=task-files.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"task-files.js","sourceRoot":"","sources":["../../src/scheduler/task-files.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,SAAS,IAAI,WAAW,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAChK,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AACrG,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAExE,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,CAAC;AAEvE,MAAM,GAAG,GAAG,IAAI,GAAG,IAAI,CAAC;AAExB,uFAAuF;AACvF,MAAM,CAAC,MAAM,cAAc,GAAG,EAAE,GAAG,MAAM,CAAC;AAC1C,qEAAqE;AACrE,MAAM,CAAC,MAAM,uBAAuB,GAAG,EAAE,CAAC;AAE1C,MAAM,OAAO,aAAc,SAAQ,KAAK;CAAG;AA6B3C,MAAM,IAAI,GAAG,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AC,KAAK,GAAG,WAAW,CAAC,MAAM,CAAC,CAAC;YAAC,CAAC;YAAC,MAAM,CAAC;gBAAC,SAAS;YAAC,CAAC;YACxD,KAAK,MAAM,CAAC,IAAI,KAAK;gBAAE,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;oBAAC,IAAI,CAAC;wBAAC,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC;wBAAC,OAAO,EAAE,CAAC;oBAAC,CAAC;oBAAC,MAAM,CAAC,CAAC,UAAU,CAAC,CAAC;gBAAC,CAAC;QAClH,CAAC;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;CACF"}
@@ -94,6 +94,8 @@ export interface SettingsApiContext extends AppShellContext {
94
94
  }>;
95
95
  /** #1920 phase 3: Settings → Web sign-ins → Guests. */
96
96
  guestOverview?(): unknown;
97
+ /** M-3734: each guest's derived agents now (Start from today's guests is checked against them). */
98
+ guestDerivedScopes?(): Record<string, string[]>;
97
99
  /** #2667: the ClassicBot rooms' instance names — a guest grant may name one, so a save's validation knows them. */
98
100
  classicRoomNames?(): string[];
99
101
  revokeGuestWebSessions?(owner: {
@@ -20,7 +20,7 @@ import { isSettingsReplay, settingsRequestExecution, settingsRequestLocalAdmin,
20
20
  import { noteSettingsWrite, settingsUndo, undoSettingsPaths } from "./settings-transaction.js";
21
21
  import { readBoundedWebBody } from "./web-body.js";
22
22
  import { gatewayRequestContext } from "./web-request-context.js";
23
- import { applyAppearanceChanges, applyGuestsPatch, applyVerifiedAuthorityPatch, parseAppearanceSettingsBody, parseWebSettingsBody } from "./web-settings-patch.js";
23
+ import { applyAppearanceChanges, applyGuestsPatch, applyVerifiedAuthorityPatch, guestsConversionFailure, guestsPreconditionFailure, parseAppearanceSettingsBody, parseWebSettingsBody } from "./web-settings-patch.js";
24
24
  import { APPEARANCE_BOUNDS, APPEARANCE_DEFAULTS, effectiveAppearanceLimits } from "./web-appearance-limits.js";
25
25
  import { permitWebContinuation } from "./web-continuation.js";
26
26
  import { parseRuntimesSettingsBody, runtimeSettingsView } from "./runtime-switches.js";
@@ -2299,6 +2299,11 @@ export function handleSettingsRequest(req, res, url, ctx) {
2299
2299
  }
2300
2300
  // #1920 phase 3: guest sign-in (Settings → Web sign-ins → Guests); widening it is confirmed (settings-change.ts).
2301
2301
  if (parsed.guests) {
2302
+ // M-3734: checked again as it applies (a confirmation's replay comes here): what the page read must still be saved.
2303
+ const stale = guestsPreconditionFailure(cfg.web?.guests, parsed.guests)
2304
+ ?? guestsConversionFailure(parsed.guests, parsed.guests.if_grants === "unset" ? ctx.guestDerivedScopes?.() : undefined);
2305
+ if (stale)
2306
+ return json(res, 409, { error: "This person's guest access changed since the page read it. Read it again and retry.", key: "guest_grant_changed", ...(stale.account ? { account: stale.account } : {}) });
2302
2307
  // #2348: a new grant names someone AgEnD has seen in chat, on a connection of this fleet, who is not a fleet admin
2303
2308
  // (admins sign in as admins). One already granted may be edited or removed whatever the list says now.
2304
2309
  for (const [key, grant] of Object.entries(parsed.guests.grants ?? {})) {