rvlib-shipkit 2.27.0 → 2.28.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.
Files changed (338) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/package.json +1 -1
  3. package/src/agent-time/hooks.ts +10 -3
  4. package/src/agent-time/live.ts +11 -5
  5. package/src/agent-time/registry.ts +289 -0
  6. package/src/agent-time/store.ts +2 -2
  7. package/src/annex/born.ts +2 -2
  8. package/src/annex/offload.ts +12 -11
  9. package/src/asks/ask-after.ts +6 -2
  10. package/src/asks/ask-bar-fold.ts +57 -0
  11. package/src/asks/ask-migrate.ts +280 -0
  12. package/src/asks/ask-steps-fold.ts +166 -0
  13. package/src/asks/ask-store.ts +586 -0
  14. package/src/assets/kits/identity-kit.ts +106 -38
  15. package/src/assets/kits/poster-thumbnail.ts +282 -0
  16. package/src/checks/SK003-claude-md-shape.check.ts +92 -25
  17. package/src/checks/SK004-gitignore-policy.check.ts +8 -4
  18. package/src/checks/SK013-one-way-in-policy.check.ts +10 -0
  19. package/src/checks/SK026-asks-autoclose.check.ts +9 -23
  20. package/src/checks/SK038-speaks-as.check.ts +3 -3
  21. package/src/checks/SK042-post-kinds.check.ts +26 -24
  22. package/src/checks/SK049-thread-answers-hook.check.ts +81 -0
  23. package/src/checks/SK050-retired-commands.check.ts +127 -0
  24. package/src/cli/cmd_Asks.ts +204 -53
  25. package/src/cli/cmd_Backup.ts +151 -8
  26. package/src/cli/cmd_Calls.ts +1 -0
  27. package/src/cli/cmd_Commands.ts +140 -34
  28. package/src/cli/cmd_Contracts.ts +1 -0
  29. package/src/cli/cmd_Keys.ts +1 -0
  30. package/src/cli/cmd_Lanes.ts +15 -4
  31. package/src/cli/cmd_Messages.ts +152 -1
  32. package/src/cli/cmd_Needs.ts +1 -0
  33. package/src/cli/cmd_Review.ts +931 -229
  34. package/src/cli/cmd_Secret.ts +255 -34
  35. package/src/cli/cmd_Social.ts +36 -33
  36. package/src/cli/cmd_SocialDrafts.ts +235 -0
  37. package/src/cli/cmd_Sound.ts +22 -1
  38. package/src/cli/cmd_Start.ts +3 -2
  39. package/src/cli/cmd_ThreadHolds.ts +34 -0
  40. package/src/cli/cmd_ThreadKinds.ts +146 -0
  41. package/src/cli/cmd_ThreadReflect.ts +83 -0
  42. package/src/cli/cmd_ThreadSyncNeeds.ts +62 -0
  43. package/src/cli/cmd_Vault.ts +1 -1
  44. package/src/cli/cmd_When.ts +1 -0
  45. package/src/cli/family-help.ts +2 -2
  46. package/src/cli/family-pages.ts +1 -1
  47. package/src/cli/kit/builtins.ts +1 -0
  48. package/src/cli/kit/verb.ts +6 -2
  49. package/src/cli/registry.ts +21 -2
  50. package/src/cli.ts +5 -2
  51. package/src/collections/actions.ts +25 -74
  52. package/src/collections/collection-types.ts +9 -5
  53. package/src/collections/md-body.ts +5 -3
  54. package/src/collections/pings.ts +13 -6
  55. package/src/collections/providers.ts +53 -61
  56. package/src/collections/reflections.ts +9 -4
  57. package/src/core/discover-checks.ts +18 -3
  58. package/src/core/git.ts +31 -0
  59. package/src/core/house-layout.ts +17 -0
  60. package/src/core/layout.ts +10 -5
  61. package/src/core/locked-write.ts +4 -1
  62. package/src/core/machine-db.ts +5 -3
  63. package/src/core/migrate-layout.ts +8 -10
  64. package/src/home/activity.ts +8 -15
  65. package/src/home/daemon-jobs.ts +13 -1
  66. package/src/home/home-paths.ts +6 -3
  67. package/src/home/home-routes.ts +8 -0
  68. package/src/home/inbox-shape.ts +233 -100
  69. package/src/home/inbox.ts +26 -71
  70. package/src/home/machine-fold.ts +10 -15
  71. package/src/home/machine-host.ts +19 -1
  72. package/src/home/other-macs.ts +2 -2
  73. package/src/home/serve-web.ts +6 -5
  74. package/src/home/space-page.ts +1 -1
  75. package/src/home/vault-page.ts +51 -17
  76. package/src/home/vault-words.ts +2 -2
  77. package/src/hooks/one-way-in-guard.ts +24 -2
  78. package/src/hq/files-send.ts +27 -28
  79. package/src/hq/hq-db.ts +4 -1
  80. package/src/hq/hq-local.ts +37 -4
  81. package/src/hq/hq-routes.ts +10 -2
  82. package/src/hq/hq-store.ts +6 -2
  83. package/src/hq/kinds/ask.ts +6 -6
  84. package/src/hq/outbox.ts +5 -3
  85. package/src/hq/replica.ts +1 -2
  86. package/src/hq/vault-route.ts +112 -0
  87. package/src/identities/collection.ts +2 -2
  88. package/src/identities/identities.ts +9 -8
  89. package/src/identities/machine-route.ts +8 -3
  90. package/src/ledger/db.ts +8 -1
  91. package/src/media-store/backup/daemon.ts +48 -8
  92. package/src/media-store/backup/keys.ts +1 -1
  93. package/src/media-store/backup/manifest.ts +20 -2
  94. package/src/media-store/backup/prune.ts +365 -0
  95. package/src/media-store/backup/retention.ts +122 -0
  96. package/src/media-store/backup/run.ts +14 -8
  97. package/src/media-store/backup/sources.ts +18 -17
  98. package/src/media-store/backup/thread-copy.ts +340 -0
  99. package/src/media-store/copy/config.ts +3 -4
  100. package/src/media-store/copy/store.ts +2 -2
  101. package/src/media-store/db.ts +2 -2
  102. package/src/media-store/forget.ts +118 -0
  103. package/src/media-store/purge.ts +3 -2
  104. package/src/media-store/space-listing.ts +20 -4
  105. package/src/media-store/space.ts +6 -5
  106. package/src/messages/guard.ts +49 -0
  107. package/src/messages/model.ts +1 -1
  108. package/src/messages/objects.ts +201 -0
  109. package/src/messages/routes.ts +2 -2
  110. package/src/messages/score.ts +1 -1
  111. package/src/messages/write.ts +285 -0
  112. package/src/pages/frame-client.ts +181 -4
  113. package/src/reflections/reflections-queue.ts +1 -1
  114. package/src/release/gate-checkout.ts +2 -1
  115. package/src/release/git-facts.ts +3 -2
  116. package/src/release/public-clean.ts +3 -2
  117. package/src/repo-config.ts +40 -18
  118. package/src/review/anim.ts +1 -1
  119. package/src/review/answer-kinds.ts +131 -0
  120. package/src/review/band.ts +53 -0
  121. package/src/review/branch-hold.ts +84 -0
  122. package/src/review/card-db.ts +16 -6
  123. package/src/review/card-rows.ts +4 -3
  124. package/src/review/cells.ts +297 -65
  125. package/src/review/close.ts +92 -0
  126. package/src/review/draft-card.ts +8 -16
  127. package/src/review/draft-model.ts +2 -2
  128. package/src/review/events.ts +171 -0
  129. package/src/review/format.ts +128 -14
  130. package/src/review/glyphs.ts +63 -0
  131. package/src/review/gone.ts +1 -1
  132. package/src/review/holds-sync.ts +62 -0
  133. package/src/review/holds.ts +85 -0
  134. package/src/review/house-kind-defs.ts +41 -0
  135. package/src/review/inbox-hook.ts +22 -9
  136. package/src/review/inbox-read.ts +201 -0
  137. package/src/review/instructions.ts +199 -0
  138. package/src/review/kind-apply.ts +61 -0
  139. package/src/review/kind-catalog.ts +96 -0
  140. package/src/review/kinds.ts +278 -0
  141. package/src/review/lane-ask.ts +109 -0
  142. package/src/review/model.ts +396 -86
  143. package/src/review/move.ts +3 -3
  144. package/src/review/need-address.ts +15 -0
  145. package/src/review/need-threads.ts +93 -0
  146. package/src/review/object-card.ts +48 -0
  147. package/src/review/object-read.ts +31 -0
  148. package/src/review/owner-verdict.ts +36 -0
  149. package/src/review/parties.ts +182 -0
  150. package/src/review/previews.ts +3 -1
  151. package/src/review/projections.ts +235 -0
  152. package/src/review/referring.ts +54 -0
  153. package/src/review/reflection-object.ts +195 -0
  154. package/src/review/routes.ts +137 -32
  155. package/src/review/secret-route.ts +80 -0
  156. package/src/review/snooze.ts +38 -0
  157. package/src/review/stage-card.ts +4 -4
  158. package/src/review/store.ts +366 -85
  159. package/src/review/supersede.ts +116 -0
  160. package/src/review/take-back.ts +52 -0
  161. package/src/review/thread-fold.ts +20 -7
  162. package/src/review/threads.ts +236 -39
  163. package/src/review/traces.ts +218 -0
  164. package/src/review/trailer-card.ts +2 -2
  165. package/src/review/unit.ts +71 -5
  166. package/src/review/verdict-items.ts +73 -0
  167. package/src/review/verdict-write.ts +15 -0
  168. package/src/review/wait.ts +85 -0
  169. package/src/review/watchers.ts +22 -9
  170. package/src/scaffold/files/claude-settings-local.json +2 -0
  171. package/src/secrets/archive-mark.ts +52 -0
  172. package/src/secrets/archive.ts +83 -0
  173. package/src/secrets/copy.ts +14 -14
  174. package/src/secrets/keychain-write.ts +48 -0
  175. package/src/secrets/keychain.ts +8 -5
  176. package/src/secrets/lapse-mark.ts +33 -0
  177. package/src/secrets/lapse.ts +60 -0
  178. package/src/secrets/list.ts +16 -22
  179. package/src/secrets/manager.ts +31 -16
  180. package/src/secrets/read.ts +161 -33
  181. package/src/secrets/tail.ts +31 -0
  182. package/src/serve/hub-app/asks-page.tsx +2 -1
  183. package/src/serve/hub-app/auto-text.tsx +14 -6
  184. package/src/serve/hub-app/benign-errors.ts +15 -1
  185. package/src/serve/hub-app/card-head.tsx +49 -0
  186. package/src/serve/hub-app/check-view.tsx +3 -2
  187. package/src/serve/hub-app/code-copy.ts +35 -0
  188. package/src/serve/hub-app/col-card.tsx +31 -40
  189. package/src/serve/hub-app/col-list-page.tsx +5 -2
  190. package/src/serve/hub-app/col-tables-view.tsx +7 -4
  191. package/src/serve/hub-app/col-tables.ts +5 -5
  192. package/src/serve/hub-app/cols-svc.ts +4 -79
  193. package/src/serve/hub-app/commands/commands-page.tsx +2 -1
  194. package/src/serve/hub-app/controls.css +83 -0
  195. package/src/serve/hub-app/frames-st.ts +17 -1
  196. package/src/serve/hub-app/git-view.tsx +3 -2
  197. package/src/serve/hub-app/home-st.ts +57 -6
  198. package/src/serve/hub-app/home-strip.css +1 -6
  199. package/src/serve/hub-app/home-strip.tsx +2 -1
  200. package/src/serve/hub-app/home.html +3 -0
  201. package/src/serve/hub-app/house-wiki-view.tsx +20 -0
  202. package/src/serve/hub-app/hub-app.tsx +48 -32
  203. package/src/serve/hub-app/hub-types.ts +2 -0
  204. package/src/serve/hub-app/hub.css +50 -61
  205. package/src/serve/hub-app/icons.tsx +14 -4
  206. package/src/serve/hub-app/identities-view.tsx +4 -3
  207. package/src/serve/hub-app/inbox-st.ts +203 -42
  208. package/src/serve/hub-app/inbox-view.tsx +189 -101
  209. package/src/serve/hub-app/inbox.css +47 -44
  210. package/src/serve/hub-app/index.html +3 -0
  211. package/src/serve/hub-app/instructions.css +30 -0
  212. package/src/serve/hub-app/keys.ts +5 -3
  213. package/src/serve/hub-app/machine-st.ts +2 -2
  214. package/src/serve/hub-app/machines-view.tsx +4 -11
  215. package/src/serve/hub-app/machines.css +0 -9
  216. package/src/serve/hub-app/markdown.css +51 -0
  217. package/src/serve/hub-app/md-copy.tsx +18 -40
  218. package/src/serve/hub-app/md.ts +25 -9
  219. package/src/serve/hub-app/media-view.tsx +2 -2
  220. package/src/serve/hub-app/messages/messages-page.tsx +2 -1
  221. package/src/serve/hub-app/messages/messages-st.ts +1 -1
  222. package/src/serve/hub-app/messages.css +0 -1
  223. package/src/serve/hub-app/objects.css +51 -0
  224. package/src/serve/hub-app/page-keys.ts +1 -1
  225. package/src/serve/hub-app/pages-frames.tsx +11 -1
  226. package/src/serve/hub-app/palette-st.ts +4 -4
  227. package/src/serve/hub-app/pane-boundary.tsx +72 -0
  228. package/src/serve/hub-app/pane-errors.ts +54 -0
  229. package/src/serve/hub-app/plan-view.tsx +3 -2
  230. package/src/serve/hub-app/planned-view.tsx +3 -3
  231. package/src/serve/hub-app/planned.css +0 -4
  232. package/src/serve/hub-app/processes-view.tsx +3 -2
  233. package/src/serve/hub-app/project-list.css +5 -13
  234. package/src/serve/hub-app/project-list.tsx +3 -6
  235. package/src/serve/hub-app/projects-svc.ts +6 -2
  236. package/src/serve/hub-app/readme.css +9 -15
  237. package/src/serve/hub-app/reflections-page.tsx +54 -1
  238. package/src/serve/hub-app/releases/releases-page.tsx +3 -2
  239. package/src/serve/hub-app/releases.css +0 -1
  240. package/src/serve/hub-app/review/code-colors.ts +30 -0
  241. package/src/serve/hub-app/review/diff-view.tsx +75 -0
  242. package/src/serve/hub-app/review/dock.tsx +104 -0
  243. package/src/serve/hub-app/review/draft-card.tsx +2 -2
  244. package/src/serve/hub-app/review/group-block.tsx +209 -0
  245. package/src/serve/hub-app/review/instructions-view.tsx +116 -0
  246. package/src/serve/hub-app/review/item-verdict.tsx +38 -0
  247. package/src/serve/hub-app/review/kind-states.tsx +193 -0
  248. package/src/serve/hub-app/review/kind-view.tsx +1 -37
  249. package/src/serve/hub-app/review/kit.tsx +57 -54
  250. package/src/serve/hub-app/review/lane-bar.tsx +64 -0
  251. package/src/serve/hub-app/review/object-card.tsx +62 -0
  252. package/src/serve/hub-app/review/object-panel.tsx +179 -0
  253. package/src/serve/hub-app/review/question-views.tsx +385 -0
  254. package/src/serve/hub-app/review/review-cells.tsx +86 -105
  255. package/src/serve/hub-app/review/review-list.tsx +104 -47
  256. package/src/serve/hub-app/review/review-page.tsx +5 -4
  257. package/src/serve/hub-app/review/review-st.ts +68 -28
  258. package/src/serve/hub-app/review/review-thread.tsx +171 -40
  259. package/src/serve/hub-app/review/review-views.tsx +35 -31
  260. package/src/serve/hub-app/review/thread-close.tsx +124 -0
  261. package/src/serve/hub-app/review/thread-control.tsx +4 -4
  262. package/src/serve/hub-app/review/thread-st.ts +222 -22
  263. package/src/serve/hub-app/review/trailer-card.tsx +1 -1
  264. package/src/serve/hub-app/review/trailer-timeline.tsx +2 -2
  265. package/src/serve/hub-app/review.css +216 -57
  266. package/src/serve/hub-app/section-head.tsx +51 -0
  267. package/src/serve/hub-app/social/calendar.tsx +5 -11
  268. package/src/serve/hub-app/social/comms-page.tsx +23 -10
  269. package/src/serve/hub-app/social/kit.tsx +8 -15
  270. package/src/serve/hub-app/social/next-release.tsx +3 -5
  271. package/src/serve/hub-app/social/post-card.tsx +3 -0
  272. package/src/serve/hub-app/social/post-page.tsx +80 -0
  273. package/src/serve/hub-app/social/problems.tsx +5 -9
  274. package/src/serve/hub-app/social/social-model.ts +13 -5
  275. package/src/serve/hub-app/social/social-page.tsx +1 -1
  276. package/src/serve/hub-app/social/social-st.ts +42 -5
  277. package/src/serve/hub-app/social.css +7 -17
  278. package/src/serve/hub-app/space.css +1 -1
  279. package/src/serve/hub-app/stores/stores-page.tsx +6 -2
  280. package/src/serve/hub-app/thread-row.tsx +101 -0
  281. package/src/serve/hub-app/time-ago.tsx +3 -2
  282. package/src/serve/hub-app/time-view.tsx +2 -1
  283. package/src/serve/hub-app/time.css +2 -5
  284. package/src/serve/hub-app/tree-row.tsx +1 -9
  285. package/src/serve/hub-app/vault-view.tsx +4 -4
  286. package/src/serve/hub-app/veille/veille-page.tsx +2 -11
  287. package/src/serve/hub-app/veille.css +0 -1
  288. package/src/serve/hub-app/waiting-svc.ts +4 -4
  289. package/src/serve/hub-app/when-view.tsx +6 -3
  290. package/src/serve/hub-app/wiki-st.ts +30 -4
  291. package/src/serve/hub-app/wikis-view.tsx +4 -3
  292. package/src/serve/hub-runtime.ts +20 -6
  293. package/src/serve/project-gutter-route.ts +16 -6
  294. package/src/serve/project-gutter.ts +6 -53
  295. package/src/serve/same-origin.ts +3 -0
  296. package/src/serve/secret-gate.ts +21 -0
  297. package/src/serve/serve-hub.ts +29 -20
  298. package/src/serve/tailnet-caller.ts +117 -0
  299. package/src/shipkit.ts +2 -2
  300. package/src/site/desk.ts +2 -2
  301. package/src/smoke/run-smoke.ts +3 -4
  302. package/src/social/brands.ts +4 -0
  303. package/src/social/card-drafts.ts +11 -19
  304. package/src/social/desk-index.ts +34 -13
  305. package/src/social/drafts.ts +4 -4
  306. package/src/social/import-drafts.ts +244 -0
  307. package/src/social/kind-pages.ts +3 -3
  308. package/src/social/kinds.ts +1 -1
  309. package/src/social/load.ts +89 -64
  310. package/src/social/media-kind.ts +68 -161
  311. package/src/social/media-of.ts +5 -4
  312. package/src/social/open.ts +3 -2
  313. package/src/social/post-object.ts +120 -0
  314. package/src/social/post-sources.ts +136 -0
  315. package/src/social/post-text.ts +103 -0
  316. package/src/social/posts.ts +378 -0
  317. package/src/social/presence.ts +7 -5
  318. package/src/social/publish/index.ts +17 -5
  319. package/src/social/publish/secret.ts +2 -2
  320. package/src/social/release-stage.ts +58 -57
  321. package/src/social/routes.ts +64 -24
  322. package/src/social/stage.ts +42 -57
  323. package/src/social/text.ts +4 -3
  324. package/src/social/thumbnail.ts +135 -43
  325. package/src/social/tracker.ts +1 -1
  326. package/src/social/writes.ts +12 -29
  327. package/src/stores/desk.ts +16 -11
  328. package/src/stores/unit.ts +2 -2
  329. package/src/trailer/sound/eleven.ts +33 -3
  330. package/src/urls/rows.ts +1 -1
  331. package/src/vault/vault-http.ts +11 -3
  332. package/src/vault/vault-import.ts +6 -1
  333. package/src/veille/identity.ts +2 -2
  334. package/src/wiki/graph-pages.ts +13 -1
  335. package/src/wiki/scan-md.ts +1 -1
  336. package/src/asks/asks-queue.ts +0 -424
  337. package/src/messages/suggest.ts +0 -96
  338. package/src/social/migrate.ts +0 -123
@@ -16,13 +16,18 @@ import { anyWebp, exactWebp, webpable } from '../media-store/webp.ts'
16
16
  import { mediaDb, tryWrite } from '../media-store/db.ts'
17
17
  import { cardsInDb, cardsMoving, cardsOn } from '../hq/switch.ts'
18
18
  import { rowsAll, rowsIO, rowsStamp, type CardBackend } from './card-rows.ts'
19
- import { moveFolder, spoolOf } from './card-db.ts'
19
+ import { moveFolder, repoIdOf, spoolOf } from './card-db.ts'
20
+ import { machineDbPath } from '../core/machine-db.ts'
20
21
  import { jsonText, revOf } from './rev.ts'
21
22
  import { GONE, goneEntryFor } from './gone.ts'
22
23
  import {
23
- ADDRESS_RE,
24
+ addressOrThrow,
25
+ ANSWER_KINDS,
26
+ type AnswerKind,
27
+ type MessageAsk,
24
28
  ownAddressOf,
25
29
  partyOf,
30
+ proposeOpen,
26
31
  AGENT_RE,
27
32
  allowed,
28
33
  FILE_RE,
@@ -74,10 +79,23 @@ import {
74
79
  mediaFilesOf,
75
80
  threadMediaOf,
76
81
  isGallery,
82
+ diffTypeOf,
83
+ DIFF_TYPES,
84
+ proposeFields,
85
+ type DiffType,
86
+ type MessagePropose,
87
+ type ProposeField,
88
+ type StoredState,
89
+ type AskHead,
77
90
  } from './model.ts'
91
+ import { kindOfValue, valueKindOf } from './kinds.ts'
92
+ import { askOpen, linesOf } from './answer-kinds.ts'
93
+ import { choiceName } from './instructions.ts'
94
+ import { SECRET_NAME } from '../secrets/name.ts'
78
95
  import { beatRole, roleOf } from './role.ts'
79
96
  import { chainOf, coveringHead, followedIds, isFollowed, threadOfAddress } from './threads.ts'
80
97
  import { liveWatchers } from './watchers.ts'
98
+ import { noteSession } from '../agent-time/registry.ts'
81
99
 
82
100
  /** the store of a repo */
83
101
  export const storeDir = (root: string): string => join(root, LAYOUT.review)
@@ -182,6 +200,9 @@ export interface ViewInput {
182
200
  note?: string
183
201
  }
184
202
 
203
+ /** the most one side of a version of an object weighs, as json */
204
+ const STATE_MAX = 256 * 1024
205
+
185
206
  export interface AddInput {
186
207
  title: string
187
208
  what: string
@@ -200,6 +221,8 @@ export interface AddInput {
200
221
  commit?: string
201
222
  /** the lane that made it; else SK_AGENT */
202
223
  lane?: string
224
+ /** written by the machine (a check run, the reflections queue): no lane and no session, so nobody holds it */
225
+ machine?: true
203
226
  supersedes?: string
204
227
  /** the raised items (a report, his note) this card answers, by id */
205
228
  answers?: string[]
@@ -223,6 +246,8 @@ export interface AddInput {
223
246
  wip?: { live: string }
224
247
  /** every view is an option he picks one of */
225
248
  choice?: boolean
249
+ /** it waits for the owner's verdict (`--verdict`): an opening of words asks it only so, a version always */
250
+ asksVerdict?: true
226
251
  /** on a choice: the option the agent suggests, by its label or its letter */
227
252
  agentPick?: string
228
253
  /** a trailer card: the cut the hub plays in its body; its one view is the cut's strip */
@@ -231,6 +256,12 @@ export interface AddInput {
231
256
  draft?: DraftCard
232
257
  /** every picture as given, never a webp: the file itself kept, where the card holds only its webp otherwise */
233
258
  keep?: boolean
259
+ /** a thread opened with words on its address (`on`): no media, nothing he judges (openThread) */
260
+ opening?: boolean
261
+ /** a version of an object of a kind (`on`): its two states as the kind read them; it holds no view */
262
+ states?: { before?: StoredState; after: StoredState }
263
+ /** an ask's facts, on the head of its thread `ask:<id>` (asks/ask-store.ts) */
264
+ ask?: AskHead
234
265
  now?: Date
235
266
  }
236
267
 
@@ -394,14 +425,14 @@ const prefix = (n: number): string => (n > 0 ? `v${n}-` : '')
394
425
  /** the file name of tile `k` of gallery view `n` */
395
426
  const tileName = (n: number, k: number, path: string, side = ''): string => `v${n}-g${k}${side}${extname(path).toLowerCase()}`
396
427
 
397
- function viewOf(v: Loaded, n: number, marks: readonly ReviewMark[] = [], note?: string): ReviewView {
428
+ function viewOf(v: Loaded, n: number, marks: readonly ReviewMark[] = [], note?: string, message?: true): ReviewView {
398
429
  if (v.tiles)
399
430
  return {
400
431
  label: v.label,
401
432
  gallery: v.tiles.map((t, k): GalleryTile => ({ file: tileName(n, k, t.path), label: t.label, size: t.size, ...(t.before ? { before: tileName(n, k, t.before, '-before') } : {}), ...(t.group ? { group: t.group } : {}) })),
402
433
  }
403
434
  const audio = v.audio ? { audio: { duration: v.audio.duration, wave: `${prefix(n)}wave.png`, ...(v.audio.before ? { waveBefore: `${prefix(n)}before-wave.png` } : {}) } } : {}
404
- return { label: v.label, files: filesOf(v, n), size: v.size, ...clipOf(v, n), ...audio, ...(marks.length > 0 ? { marks: [...marks] } : {}), ...(note?.trim() ? { note: note.trim() } : {}) }
435
+ return { label: v.label, files: filesOf(v, n), size: v.size, ...clipOf(v, n), ...audio, ...(marks.length > 0 ? { marks: [...marks] } : {}), ...(note?.trim() ? { note: note.trim() } : {}), ...(message ? { message } : {}) }
405
436
  }
406
437
 
407
438
  /** the file names of view `n` in the folder: `image.png` on the first, `v2-before.png` on a later one */
@@ -418,37 +449,49 @@ async function writePoster(v: Loaded, n: number, folder: string): Promise<void>
418
449
  renameSync(`${to}.tmp`, to)
419
450
  }
420
451
 
421
- /** why an active card refuses a new one, and every way out */
452
+ /** why an active thread refuses a version that names no address, and every way out */
422
453
  function coveredText(c: { head: ReviewItem; by: 'subject' | 'near' | 'title' }): string {
423
454
  const r = c.head.item
424
455
  const by = c.by === 'subject' ? 'the same subject' : c.by === 'near' ? 'the same topic and a subject near it' : 'a title this close'
425
456
  return [
426
- `one subject is one card: the active card ${r.id} "${r.title}" (${r.topic ?? 'no topic'}) already covers it, ${by}.`,
427
- ` a new version of it: --supersedes ${r.id}`,
457
+ `one subject is one thread: the active thread ${r.id} "${r.title}" (${r.topic ?? 'no topic'}) already covers it, ${by}.`,
458
+ ` its next version: sk thread post ${r.id} …`,
428
459
  ` another angle of it: sk thread add-view ${r.id} "<label>" <image> [<after>]`,
429
- ` truly another subject: --new --why "<reason>"`,
460
+ ` truly another subject: sk thread post <kind>:<key> … with its own address`,
430
461
  ].join('\n')
431
462
  }
432
463
 
433
464
  /** the thread a new version goes into (wiki tools/shipkit/threads-store.md, The one thread guard): the one that owns its
434
- * address. No link given: it supersedes that thread's newest version, or answers the note at its head, and answers his
435
- * notes the thread took in that are not closed. A link given must be that one, else it is refused with the thread
436
- * named. `thread` null: its address has no thread yet, or it names none (the older guard decides) */
465
+ * address. No link given: it supersedes that thread's newest version; a thread his note heads takes it in at read time,
466
+ * with no link. It never answers his notes: a version closes one only when its agent names it (`answers`). A link given
467
+ * must be the newest version, else it is refused with the thread named. `thread` null: its address has no thread yet,
468
+ * or it names none (the older guard decides) */
437
469
  export function threadLink(
438
470
  dir: string,
439
471
  card: Pick<Item, 'subject' | 'on' | 'trailer'>,
440
472
  given: { supersedes?: string; answers: readonly string[] },
441
473
  spec: Spec = specOf(dir),
442
- ): { thread: string | null; supersedes?: string; answers: string[] } {
474
+ ): { own: string | null; thread: string | null; supersedes?: string; answers: string[] } {
443
475
  const own = ownAddressOf({ ...card, origin: { by: 'agent' } } as Item, spec)
444
476
  const t = own === null ? null : threadOfAddress(own, readAll(dir).items, spec)
445
- if (!t) return { thread: null, ...given, answers: [...given.answers] }
477
+ if (!t) return { own, thread: null, ...given, answers: [...given.answers] }
446
478
  const head = t.head.item
447
- const openNotes = t.answers.filter((x) => t.joined.has(x.item.id) && stateOf(x, false).state !== 'closed').map((x) => x.item.id)
448
- const want = head.origin.by === 'agent' ? { supersedes: head.id, answers: openNotes } : { answers: [head.id, ...openNotes] }
449
- if (given.supersedes !== undefined && given.supersedes !== want.supersedes)
450
- throw new Error(`${own} already has its thread ${head.id} "${head.title}": a new version of it supersedes its newest version${want.supersedes ? ` ${want.supersedes}` : `, here the note it answers: --answers ${head.id}`}, never ${given.supersedes}; or leave --supersedes out and the tool links it`)
451
- return { thread: head.id, ...(want.supersedes !== undefined ? { supersedes: want.supersedes } : {}), answers: [...new Set([...given.answers, ...want.answers])] }
479
+ const want = head.origin.by === 'agent' ? head.id : undefined
480
+ if (given.supersedes !== undefined && given.supersedes !== want)
481
+ throw new Error(`${own} already has its thread ${head.id} "${head.title}": a new version of it supersedes its newest version${want ? ` ${want}` : ', here none: its head is his note'}, never ${given.supersedes}; or leave --supersedes out and the tool links it`)
482
+ return { own, thread: head.id, ...(want !== undefined ? { supersedes: want } : {}), answers: [...given.answers] }
483
+ }
484
+
485
+ /** a thread opened with words on an address no thread owns: its head holds the address and a title, no media, and
486
+ * waits on no verdict; the question, the proposed change or the line goes in it next. Refused when a thread owns it */
487
+ export async function openThread(dir: string, address: string, o: { title: string; what?: string; lane?: string; machine?: true; now?: Date; ask?: AskHead; asksVerdict?: true }): Promise<Item> {
488
+ // an ask is made with its facts (asks/ask-store.ts createAsk): a bare ask: thread would wait on him and no count would hold it
489
+ if (address.startsWith('ask:') && o.ask === undefined) throw new Error(`${address}: an ask is made with sk ask, its thread opened with its facts`)
490
+ const own = threadOfAddress(address, readAll(dir).items, specOf(dir))
491
+ if (own) throw new Error(`${address} already has its thread ${own.head.item.id} "${own.head.item.title}": write in it`)
492
+ const first = o.title.trim().split('\n')[0] ?? ''
493
+ const title = first.length > 80 ? `${first.slice(0, 79).trimEnd()}…` : first
494
+ return addItem(dir, { title, what: o.what?.trim() || (o.ask ? '' : `the thread of ${address}`), subject: { kind: 'object', address }, on: address, opening: true, ...(o.lane ? { lane: o.lane } : {}), ...(o.machine ? { machine: true } : {}), ...(o.now ? { now: o.now } : {}), ...(o.ask ? { ask: o.ask } : {}), ...(o.asksVerdict ? { asksVerdict: o.asksVerdict } : {}) })
452
495
  }
453
496
 
454
497
  /** the Claude Code session that runs this process, null outside one */
@@ -461,13 +504,18 @@ export const agentName = (): string => process.env.SK_AGENT?.trim() || 'agent'
461
504
  export async function addItem(dir: string, input: AddInput): Promise<Item> {
462
505
  const spec = specOf(dir)
463
506
  if (!input.title.trim()) throw new Error('--title: required')
464
- if (!input.what.trim()) throw new Error('--what: required, what changed and why')
507
+ // an ask's head holds its facts alone: its words are its instructions, a message (asks/ask-store.ts createAsk)
508
+ if (!input.what.trim() && input.ask === undefined) throw new Error('--what: required, what changed and why')
465
509
  const first = input.image !== undefined || input.video !== undefined || input.audio !== undefined || input.before !== undefined || input.after !== undefined
466
510
  const given: ViewInput[] = [...(first ? [{ label: input.label ?? '', image: input.image, video: input.video, audio: input.audio, before: input.before, after: input.after }] : []), ...(input.views ?? [])].map(
467
511
  (v) => Object.fromEntries(Object.entries(v).filter(([, x]) => x !== undefined)) as unknown as ViewInput,
468
512
  )
469
- if (input.draft && given.length > 0) throw new Error('--draft: the card shows the draft itself, no image, clip, sound or view beside it')
470
- if (given.length === 0 && !input.draft) throw new Error('give --image, --video or --audio, or --before and --after, or --view "<label>" <image>')
513
+ if (input.draft && given.length > 0) throw new Error('--draft: the version shows the draft itself, no image, clip, sound or view beside it')
514
+ if (input.opening && (given.length > 0 || input.draft || input.trailer)) throw new Error('a thread opened with words holds no media: a version with media goes into it after')
515
+ if (input.opening && !input.on) throw new Error('a thread opened with words names its address')
516
+ if (input.states && (given.length > 0 || input.draft || input.trailer || !input.on)) throw new Error('a version of an object shows its two states alone: its address (on), no image, clip, sound or view beside it')
517
+ for (const [side, st] of Object.entries(input.states ?? {})) if (st && JSON.stringify(st).length > STATE_MAX) throw new Error(`--${side}: the state is ${Math.round(JSON.stringify(st).length / 1024)} KB, over ${STATE_MAX / 1024} KB: a kind reads what the owner judges, not the whole repo`)
518
+ if (given.length === 0 && !input.draft && !input.opening && !input.states) throw new Error('give --image, --video or --audio, or --before and --after, or --view "<label>" <image>')
471
519
  for (const [i, v] of given.entries()) if (i > 0 && !v.label.trim()) throw new Error('--view: every view after the first needs its label')
472
520
  if (input.choice && given.length < 2) throw new Error('--choice: a choice has two options or more, one --view each')
473
521
  if (input.agentPick !== undefined && !input.choice) throw new Error('--pick: only on a --choice card')
@@ -490,72 +538,112 @@ export async function addItem(dir: string, input: AddInput): Promise<Item> {
490
538
  if (wrong.length > 0) throw new Error(wrong.join('; '))
491
539
  if (input.wip && (given.length > 2 || given.slice(1).some((v) => !v.gallery))) throw new Error('--wip: the card shows the live frame, and its history as a gallery, nothing else')
492
540
  if (input.marks && input.marks.length > given.length) throw new Error(`marks: ${input.marks.length} lists for ${given.length} views`)
493
- const about = [...new Set((input.about ?? []).map((a) => a.trim()))]
494
- const on = input.on?.trim() || undefined
495
- for (const a of [...(on ? [on] : []), ...about]) if (!ADDRESS_RE.test(a)) throw new Error(`${a}: an address is <kind>:<key>, like page:review or trailer:<name>/<cut>`)
496
- const link = threadLink(dir, { subject, ...(on ? { on } : {}), ...(input.trailer ? { trailer: input.trailer } : {}) }, { ...(input.supersedes !== undefined ? { supersedes: input.supersedes } : {}), answers }, spec)
497
- input = { ...input, ...(link.supersedes !== undefined ? { supersedes: link.supersedes } : {}) }
498
- answers.splice(0, answers.length, ...link.answers)
499
- if (input.supersedes !== undefined && !input.keeps?.trim()) {
500
- const all = readAll(dir).items
501
- const old = all.find((x) => x.item.id === input.supersedes)
502
- const answered = old ? chainOf(old, all).find((x) => verdictOf(x)) : undefined
503
- const v = answered ? verdictOf(answered) : null
504
- if (answered && v && isApproved(v.status))
505
- throw new Error(
506
- `${answered.item.id} "${answered.item.title}" was approved: an approval locks the look. Keep what he approved, say what stays with --keeps "<what of the approved version stays>", and say the change in --what`,
507
- )
508
- }
509
- if (input.supersedes === undefined && link.thread === null) {
510
- if (input.fresh === true) {
511
- if (!input.why?.trim()) throw new Error('--new needs --why "<reason>": why this is another subject than the active card next to it')
512
- } else {
513
- const others = readAll(dir).items.filter((x) => !answers.includes(x.item.id))
514
- const c = coveringHead({ title: input.title, ...(topic ? { topic } : {}), subject }, others, spec)
515
- if (c) throw new Error(coveredText(c))
541
+ const about = [...new Set((input.about ?? []).map(addressOrThrow))]
542
+ const on = input.on?.trim() ? addressOrThrow(input.on) : undefined
543
+ if (on?.startsWith('ask:') && input.ask === undefined && !threadOfAddress(on, readAll(dir).items, spec)) throw new Error(`${on}: no ask has this address: an ask is made with sk ask, its thread opened with its facts`)
544
+ const given0 = { ...(input.supersedes !== undefined ? { supersedes: input.supersedes } : {}), answers: [...answers] }
545
+ /** the thread it goes into and the refusals that read the store: asked once before the media are written, so a
546
+ * refusal costs no write, and again under the address's lock, where the version is written */
547
+ const settle = (): { own: string | null; supersedes?: string; answers: string[] } => {
548
+ const link = threadLink(dir, { subject, ...(on ? { on } : {}), ...(input.trailer ? { trailer: input.trailer } : {}) }, given0, spec)
549
+ if (link.supersedes !== undefined && !input.keeps?.trim()) {
550
+ const all = readAll(dir).items
551
+ const old = all.find((x) => x.item.id === link.supersedes)
552
+ const answered = old ? chainOf(old, all).find((x) => verdictOf(x)) : undefined
553
+ const v = answered ? verdictOf(answered) : null
554
+ if (answered && v && isApproved(v.status))
555
+ throw new Error(
556
+ `${answered.item.id} "${answered.item.title}" was approved: an approval locks the look. Keep what he approved, say what stays with --keeps "<what of the approved version stays>", and say the change in --what`,
557
+ )
516
558
  }
559
+ // an address decides alone: the older guard reads titles only for a version that names none
560
+ if (link.supersedes === undefined && link.thread === null && link.own === null) {
561
+ if (input.fresh === true) {
562
+ if (!input.why?.trim()) throw new Error('--new needs --why "<reason>": why this is another subject than the active card next to it')
563
+ } else {
564
+ const others = readAll(dir).items.filter((x) => !link.answers.includes(x.item.id))
565
+ const c = coveringHead({ title: input.title, ...(topic ? { topic } : {}), subject }, others, spec)
566
+ if (c) throw new Error(coveredText(c))
567
+ }
568
+ }
569
+ return link
517
570
  }
518
- const lane = input.lane?.trim() || process.env.SK_AGENT?.trim() || undefined
571
+ const early = settle()
572
+ const lane = input.machine ? undefined : input.lane?.trim() || process.env.SK_AGENT?.trim() || undefined
519
573
  if (lane !== undefined && !AGENT_RE.test(lane)) throw new Error(`--lane: « ${lane} » is no lane id (${AGENT_RE.source})`)
520
574
  mkdirSync(dir, { recursive: true })
521
575
  const now = input.now ?? new Date()
522
576
  const base = idOf(now, input.title)
523
577
  let id = base
524
578
  for (let n = 2; c.taken(id); n++) id = `${base}-${n}`
525
- const session = sessionId()
526
- const item: Item = {
579
+ const session = input.machine ? null : sessionId()
580
+ if (session) noteSession(dir)
581
+ const itemOf =(link: { supersedes?: string; answers: string[] }): Item => ({
527
582
  id,
528
583
  created: now.toISOString(),
529
584
  subject,
530
585
  origin: { by: 'agent', ...(lane ? { lane } : {}), ...(session ? { session } : {}) },
531
586
  title: input.title.trim(),
532
- what: input.what.trim(),
587
+ ...(input.what.trim() !== '' ? { what: input.what.trim() } : {}),
533
588
  ...(input.post?.trim() ? { post: input.post.trim() } : {}),
534
589
  ...(input.changelog ? { changelog: { category: input.changelog.category.trim(), line: input.changelog.line.trim(), ...(input.changelog.rating ? { rating: input.changelog.rating } : {}) } } : {}),
535
590
  ...(topic ? { topic } : {}),
536
591
  ...(input.commit ? { commit: input.commit } : {}),
537
592
  views: loaded.map((v, n) => viewOf(v, n, input.marks?.[n], given[n]?.note)),
538
- ...(input.supersedes ? { supersedes: input.supersedes } : {}),
539
- ...(answers.length > 0 ? { answers } : {}),
593
+ ...(link.supersedes ? { supersedes: link.supersedes } : {}),
594
+ ...(link.answers.length > 0 ? { answers: link.answers } : {}),
540
595
  ...(on ? { on } : {}),
541
596
  ...(about.length > 0 ? { about } : {}),
542
597
  ...(input.fresh && input.why ? { why: input.why.trim() } : {}),
543
- ...(input.supersedes && input.keeps?.trim() ? { keeps: input.keeps.trim() } : {}),
598
+ ...(link.supersedes && input.keeps?.trim() ? { keeps: input.keeps.trim() } : {}),
544
599
  ...(input.light ? { light: input.light } : {}),
545
600
  ...(input.wip ? { wip: { live: input.wip.live, frame: 1 } } : {}),
546
601
  ...(input.choice ? { choice: true as const } : {}),
602
+ ...(input.asksVerdict ? { asksVerdict: true as const } : {}),
547
603
  ...(agentPick !== undefined ? { agentPick } : {}),
548
604
  ...(input.trailer ? { trailer: input.trailer } : {}),
549
605
  ...(input.draft ? { draft: input.draft } : {}),
550
- }
551
- const problems = itemProblems(item)
606
+ ...(input.states ? { states: input.states } : {}),
607
+ ...(input.ask ? { ask: input.ask } : {}),
608
+ })
609
+ const problems = itemProblems(itemOf(early))
552
610
  if (problems.length > 0) throw new Error(problems.join('; '))
553
611
  const tmp = tmpOf(c, id)
554
612
  loaded.forEach((v, n) => copyFiles(v, n, tmp))
555
613
  await Promise.all(loaded.map((v, n) => Promise.all([writePoster(v, n, tmp), writeWaves(v, n, tmp)])))
556
- const head: Item = { ...item, media: await factsOf(tmp, viewFilesOf(item)) }
557
- createAtCommit(dir, c, id, tmp, head)
558
- return head
614
+ const media = await factsOf(tmp, viewFilesOf(itemOf(early)))
615
+ try {
616
+ // the link read and the version written under one lock: two posts at once on one address make one thread
617
+ return addressLocked(dir, early.own, () => {
618
+ const head: Item = { ...itemOf(settle()), media }
619
+ createAtCommit(dir, c, id, tmp, head)
620
+ return head
621
+ })
622
+ } catch (e) {
623
+ rmSync(tmp, { recursive: true, force: true })
624
+ throw e
625
+ }
626
+ }
627
+
628
+ /** where the lock of one own address lives: the store's `.locks/` while the cards are files (a store per checkout),
629
+ * else beside the machine db, keyed by the repo the rows belong to, so every worktree of a repo takes one lock */
630
+ export function addressLockPath(dir: string, address: string): string {
631
+ const rows = cardsInDb()
632
+ const key = new Bun.CryptoHasher('sha1').update(rows ? `${repoIdOf(dir)}\n${address}` : address).digest('hex').slice(0, 16)
633
+ return join(rows ? join(dirname(machineDbPath()), 'thread-locks') : join(dir, '.locks'), key)
634
+ }
635
+
636
+ /** the lock of one own address while its thread is read and a version written into it (the item lock's atomic folder);
637
+ * no address, no lock */
638
+ function addressLocked<T>(dir: string, address: string | null, fn: () => T): T {
639
+ if (address === null) return fn()
640
+ const at = addressLockPath(dir, address)
641
+ mkdirSync(at, { recursive: true })
642
+ try {
643
+ return locked(dirname(at), basename(at), fn, 2000)
644
+ } catch (e) {
645
+ throw (e as Error).message.endsWith('locked by another process') ? new Error(`${address}: another post on it holds its lock past 2 s; post again`) : e
646
+ }
559
647
  }
560
648
 
561
649
  /** the empty folder a new card's files are written in, beside the folder they become: the repo's store, or the spool */
@@ -602,10 +690,10 @@ const updateLine = (id: string): string => `sk thread update ${id} <${UPDATE_STA
602
690
 
603
691
  /** another angle of an open card's subject, appended as its next view; refused once he answered, except the view of an
604
692
  * update (`update`), which goes on at any time */
605
- export async function addView(dir: string, id: string, v: ViewInput, o: { marks?: ReviewMark[]; light?: 'review'; update?: true; keep?: boolean } = {}): Promise<Item> {
693
+ export async function addView(dir: string, id: string, v: ViewInput, o: { marks?: ReviewMark[]; light?: 'review'; message?: true; keep?: boolean } = {}): Promise<Item> {
606
694
  const x = readItem(dir, id)
607
695
  if (!x) throw new Error(`no card ${id}`)
608
- if (verdictOf(x) && !o.update) throw new Error(`${id} has his answer already: a picture of the work since goes on as an update, ${updateLine(id)}; a new version passes --supersedes ${id}`)
696
+ if (verdictOf(x) && !o.message) throw new Error(`${id} has the owner's answer already: a picture of the work since goes on as an update, ${updateLine(id)}; a new version passes --supersedes ${id}`)
609
697
  if (v.note !== undefined && v.gallery) throw new Error(`"${v.label}": a note goes on an image, a clip or a sound, never a gallery`)
610
698
  if (!v.label.trim()) throw new Error('add-view: the view needs its label')
611
699
  const loaded = await loadView(v, `the view "${v.label}"`, { keep: o.keep === true })
@@ -616,7 +704,7 @@ export async function addView(dir: string, id: string, v: ViewInput, o: { marks?
616
704
  copyFiles(loaded, n, folder)
617
705
  await writePoster(loaded, n, folder)
618
706
  await writeWaves(loaded, n, folder)
619
- const view = viewOf(loaded, n, o.marks, v.note)
707
+ const view = viewOf(loaded, n, o.marks, v.note, o.message)
620
708
  const facts = await factsOf(folder, viewFilesOf({ ...x.item, views: [view] }))
621
709
  return updateHead(dir, id, (cur) => {
622
710
  if (cur.views.length !== n) throw new Error(`${id}: another view landed while this one was written: add it again`)
@@ -859,7 +947,7 @@ function readThread(dir: string, id: string): Message[] {
859
947
  }
860
948
 
861
949
  /** appends one message to an item's thread, its image copied in beside it; the id never collides */
862
- export function appendMessage(dir: string, id: string, m: Omit<Message, 'id' | 'at'>, o: { image?: string; now?: Date } = {}): Message {
950
+ export function appendMessage(dir: string, id: string, m: Omit<Message, 'id' | 'at'>, o: { image?: string; now?: Date; machine?: true } = {}): Message {
863
951
  const now = o.now ?? new Date()
864
952
  if (o.image !== undefined) {
865
953
  if (!existsSync(o.image)) throw new Error(`${o.image}: no such file`)
@@ -879,7 +967,8 @@ export function appendMessage(dir: string, id: string, m: Omit<Message, 'id' | '
879
967
  mkdirSync(folder, { recursive: true })
880
968
  copyFileSync(o.image, join(folder, file))
881
969
  }
882
- const session = partyOf(m.who) === 'agent' && m.session === undefined ? sessionId() : null
970
+ const session = !o.machine && partyOf(m.who) === 'agent' && m.session === undefined ? sessionId() : null
971
+ if (session) noteSession(dir)
883
972
  const out: Message = { id: mid, at: now.toISOString(), ...m, ...(session ? { session } : {}), ...(file ? { image: file } : {}) }
884
973
  try {
885
974
  c.append(id, out)
@@ -903,36 +992,157 @@ export function addMessage(dir: string, id: string, who: string, text: unknown,
903
992
  return appendMessage(dir, id, { who, text: t.text, ...(typeof re === 'string' ? { re } : {}) }, { ...(image !== undefined ? { image } : {}), now })
904
993
  }
905
994
 
906
- /** an agent's question to the owner on a thread's item: its words, two options or more, the option it picks; the
907
- * owner answers it in one click (answerAsk) */
908
- export function postAsk(dir: string, id: string, q: { who: string; text: unknown; options: readonly string[]; pick?: number; now?: Date }): Message {
909
- if (!readItem(dir, id)) throw new Error(`no item ${id}`)
995
+ /** one option of a question: its words, and its media, one file (a picture, a clip, a sound) or a before and an after */
996
+ export interface AskOption {
997
+ label: string
998
+ files?: readonly string[]
999
+ }
1000
+
1001
+ /** an agent's question to the owner on a thread's item: its words, two options or more, each words or a media, the
1002
+ * option it picks; each option's media becomes a view the message holds (addView `message`), and the owner answers in
1003
+ * one click (answerAsk) */
1004
+ export async function postAsk(dir: string, id: string, q: { who: string; text: unknown; options?: readonly (string | AskOption)[]; pick?: number; kind?: AnswerKind; secret?: { name: string; login?: boolean }; done?: readonly number[]; now?: Date; replaces?: string; step?: { of: string; i: number } }): Promise<Message> {
1005
+ const x = readItem(dir, id)
1006
+ if (!x) throw new Error(`no item ${id}`)
910
1007
  const t = textOf(q.text)
911
1008
  if ('error' in t) throw new Error(`the question: ${t.error.replace(/^text: /, '')}`)
912
- const options = q.options.map((o) => o.trim()).filter((o) => o !== '')
1009
+ const kind = q.kind ?? 'choice'
1010
+ if (!ANSWER_KINDS.includes(kind)) throw new Error(`a question's kind: one of ${ANSWER_KINDS.join(', ')}`)
1011
+ const replaces = { ...(q.replaces !== undefined ? { replaces: q.replaces } : {}), ...(q.step !== undefined ? { step: q.step } : {}) }
1012
+ if (kind !== 'choice') return appendMessage(dir, id, { who: q.who, text: t.text, ask: kindAsk(dir, x, kind, q), ...replaces }, q.now ? { now: q.now } : {})
1013
+ if (q.options === undefined) throw new Error('a question offers two options or more: --option "<a>" --option "<b>"')
1014
+ const options = q.options.map((o) => (typeof o === 'string' ? { label: o } : o)).map((o) => ({ ...o, label: o.label.trim() })).filter((o) => o.label !== '' || (o.files?.length ?? 0) > 0)
913
1015
  if (options.length < 2) throw new Error('a question offers two options or more: --option "<a>" --option "<b>"')
914
1016
  if (q.pick !== undefined && !(Number.isInteger(q.pick) && q.pick >= 0 && q.pick < options.length)) throw new Error(`--pick: an option's number, 1 to ${options.length}`)
915
- return appendMessage(dir, id, { who: q.who, text: t.text, ask: { options, ...(q.pick !== undefined ? { pick: q.pick } : {}) } }, q.now ? { now: q.now } : {})
1017
+ for (const f of options.flatMap((o) => o.files ?? [])) if (!existsSync(f)) throw new Error(`${f}: no such file`)
1018
+ const labels = options.map((o, i) => o.label || basename(o.files![o.files!.length - 1]!).replace(/\.[^.]+$/, '') || `option ${i + 1}`)
1019
+ const views: (number | null)[] = []
1020
+ for (const [i, o] of options.entries()) {
1021
+ if (!o.files?.length) {
1022
+ views.push(null)
1023
+ continue
1024
+ }
1025
+ const item = await addView(dir, id, filesView(labels[i]!, o.files, `option ${i + 1}: one file, or a before and an after`), { message: true })
1026
+ views.push(item.views.length - 1)
1027
+ }
1028
+ const media = views.some((v) => v !== null) ? { views } : {}
1029
+ return appendMessage(dir, id, { who: q.who, text: t.text, ask: { options: labels, ...(q.pick !== undefined ? { pick: q.pick } : {}), ...media }, ...replaces }, q.now ? { now: q.now } : {})
916
1030
  }
917
1031
 
918
- /** an agent's proposed change of an object's text: the owner applies it, or applies his edit of it (answerAsk), and the
919
- * agent lands what he applied */
920
- export function postPropose(dir: string, id: string, p: { who: string; text: unknown; on: string; before?: string; after: string; now?: Date }): Message {
1032
+ /** the longest text or url he answers with */
1033
+ export const ANSWER_VALUE_MAX = 20_000
1034
+
1035
+ export const isWebUrl = (s: string): boolean => {
1036
+ try {
1037
+ const u = new URL(s)
1038
+ return (u.protocol === 'https:' || u.protocol === 'http:') && u.hostname !== ''
1039
+ } catch {
1040
+ return false
1041
+ }
1042
+ }
1043
+
1044
+ /** a question of another kind than a choice, as the store keeps it: a checklist's lines (words alone, no media), a
1045
+ * secret's name; a secret is refused on an object whose kind frames a page, which shares the hub's origin
1046
+ * (wiki tools/shipkit/threads-asks.md, The secret field) */
1047
+ function kindAsk(dir: string, x: ReviewItem, kind: AnswerKind, q: { options?: readonly (string | AskOption)[]; secret?: { name: string; login?: boolean }; done?: readonly number[] }): MessageAsk {
1048
+ if (kind === 'checklist') {
1049
+ const lines = (q.options ?? []).map((o) => (typeof o === 'string' ? o : o.label).replace(/\s+/g, ' ').trim())
1050
+ if (lines.length === 0 || lines.some((l) => l === '')) throw new Error('a checklist holds one line or more, each words: --checklist "<step>" …')
1051
+ if ((q.options ?? []).some((o) => typeof o !== 'string' && (o.files?.length ?? 0) > 0)) throw new Error('a checklist line is words: a picture goes as its own message before it')
1052
+ const done = [...new Set(q.done ?? [])].sort((a, b) => a - b)
1053
+ if (done.some((i) => !Number.isInteger(i) || i < 0 || i >= lines.length)) throw new Error(`a line done: one of 0 to ${lines.length - 1}`)
1054
+ return { kind, options: lines, ...(done.length > 0 ? { done } : {}) }
1055
+ }
1056
+ if ((q.options ?? []).length > 0) throw new Error(`a ${kind} question holds no options: --option goes with a choice`)
1057
+ if (kind !== 'secret') return { kind, options: [] }
1058
+ const name = q.secret?.name.trim() ?? ''
1059
+ if (!SECRET_NAME.test(name)) throw new Error(`--secret: ${name === '' ? 'the secret\'s name' : `${name} is no secret name`} (letters, digits, . _ @ / -, no leading dash), as the registry names it: rv/<service>/<what>`)
1060
+ const spec = specOf(dir)
1061
+ const own = ownAddressOf(x.item, spec)
1062
+ const def = own == null ? undefined : spec.kinds[own.slice(0, own.indexOf(':'))]
1063
+ if (def?.view !== undefined || def?.diff?.page !== undefined)
1064
+ throw new Error(`--secret on ${own}: its kind frames a page of the repo in the hub, and a secret field never sits beside one; ask it on the ask's own thread, ask:<id> (wiki tools/shipkit/threads-asks.md, The secret field)`)
1065
+ return { kind, options: [], secret: { name, ...(q.secret?.login ? { login: true as const } : {}) } }
1066
+ }
1067
+
1068
+ /** one field of a proposed change as an agent sends it: its key (empty for a change of one), its values, how it draws */
1069
+ export interface ProposeInput {
1070
+ key?: string
1071
+ before?: string
1072
+ after: string
1073
+ type?: DiffType
1074
+ lang?: string
1075
+ }
1076
+
1077
+ /** an agent's proposed change of an object, one field or several: the owner applies each field alone, as it is or
1078
+ * after the owner's edit (answerAsk), and the agent lands what he applied. A field draws as the agent names it, else as the
1079
+ * object's kind declares (`diff`), else as its values read; a sound field's two files become a view the message holds */
1080
+ export async function postPropose(dir: string, id: string, p: { who: string; text: unknown; on: string; fields: readonly ProposeInput[]; now?: Date; replaces?: string }): Promise<Message> {
921
1081
  if (!readItem(dir, id)) throw new Error(`no item ${id}`)
922
- if (!ADDRESS_RE.test(p.on)) throw new Error(`${p.on}: an address is <kind>:<key>`)
923
- if (p.after.trim() === '') throw new Error('--after: the text it proposes')
924
- const t = textOf(p.text ?? `a change of ${p.on}`)
1082
+ const on = addressOrThrow(p.on)
1083
+ if (p.fields.length === 0) throw new Error('--after: the value it proposes')
1084
+ const many = p.fields.length > 1
1085
+ const keys = p.fields.map((f) => f.key?.trim() ?? '')
1086
+ if (many && keys.some((k) => k === '')) throw new Error('a change of several fields names each: --field <key> before its --before and --after')
1087
+ if (new Set(keys).size !== keys.length) throw new Error(`each field once: ${keys.filter((k, i) => keys.indexOf(k) !== i).join(', ')} twice`)
1088
+ const kind = specOf(dir).kinds[on.slice(0, on.indexOf(':'))]?.diff
1089
+ const fields: ProposeField[] = []
1090
+ for (const [i, f] of p.fields.entries()) {
1091
+ const key = keys[i]!
1092
+ if (f.after.trim() === '') throw new Error(`${key ? `--field ${key}: ` : ''}--after: the value it proposes`)
1093
+ if (f.type !== undefined && !DIFF_TYPES.includes(f.type)) throw new Error(`${key ? `--field ${key}: ` : ''}--type: one of ${DIFF_TYPES.join(', ')}`)
1094
+ const declared = kind?.fields?.[key] !== undefined ? valueKindOf(kind.fields[key]!) : null
1095
+ const type: DiffType = f.type ?? declared?.type ?? (f.lang ? 'code' : diffTypeOf(f))
1096
+ const lang = f.lang ?? (type === 'code' ? declared?.lang : undefined)
1097
+ let view: number | undefined
1098
+ if (kindOfValue(type).file) {
1099
+ const files = [...(f.before !== undefined ? [f.before] : []), f.after]
1100
+ for (const x of files) if (!existsSync(x)) throw new Error(`${key ? `--field ${key}: ` : ''}${x}: no such file (a ${kindOfValue(type).label} field takes its files)`)
1101
+ const item = await addView(dir, id, filesView(key || kindOfValue(type).label, files, `a ${kindOfValue(type).label} field: one file, or a before and an after`), { message: true })
1102
+ view = item.views.length - 1
1103
+ }
1104
+ fields.push({ key, after: f.after, ...(f.before !== undefined ? { before: f.before } : {}), ...(type !== 'text' ? { type } : {}), ...(type === 'code' && lang ? { lang } : {}), ...(view !== undefined ? { view } : {}) })
1105
+ }
1106
+ const t = textOf(p.text ?? `a change of ${on}`)
925
1107
  if ('error' in t) throw new Error(t.error)
926
- return appendMessage(dir, id, { who: p.who, text: t.text, propose: { on: p.on, ...(p.before !== undefined ? { before: p.before } : {}), after: p.after } }, p.now ? { now: p.now } : {})
1108
+ const { key: _key, ...flat } = fields[0]!
1109
+ // one field keeps its key when it names one: an object's kind applies it by that name (review/kind-apply.ts)
1110
+ const propose: MessagePropose = many ? { on, after: fields.map((f) => `${f.key}: ${f.after}`).join('\n'), fields } : keys[0] ? { on, ...flat, fields } : { on, ...flat }
1111
+ if (kind?.page) propose.page = kind.page
1112
+ return appendMessage(dir, id, { who: p.who, text: t.text, propose, ...(p.replaces !== undefined ? { replaces: p.replaces } : {}) }, p.now ? { now: p.now } : {})
927
1113
  }
928
1114
 
929
- /** the owner's answer to a question (an option) or to a proposed change (apply, the owner's edit in `text`), with words */
930
- export function answerAsk(dir: string, id: string, a: { re: string; option?: number; apply?: boolean; text?: string; now?: Date }): Message {
1115
+ /** the owner's answer to a question (an option) or to a proposed change (apply one field, the value as proposed or the owner's
1116
+ * edit of it in `value`), with the owner's words */
1117
+ export function answerAsk(dir: string, id: string, a: { re: string; option?: number; apply?: boolean; field?: string; value?: string; text?: string; tick?: boolean; pick?: number; opens?: number; now?: Date }): Message {
931
1118
  const x = readItem(dir, id)
932
1119
  if (!x) throw new Error(`no item ${id}`)
933
1120
  const m = x.thread.find((y) => y.id === a.re)
934
1121
  if (!m || (m.ask === undefined && m.propose === undefined)) throw new Error(`re: ${a.re} is no question nor proposed change of ${id}`)
935
1122
  const words = a.text?.trim() ?? ''
1123
+ const at = a.now ? { now: a.now } : {}
1124
+ const kind = m.ask?.kind ?? 'choice'
1125
+ if (m.ask && kind === 'secret') throw new Error('a secret is typed into its own field of the hub, and its value never goes into a thread')
1126
+ if (m.ask && kind === 'file') throw new Error('a file question is answered by the file he drops')
1127
+ if (m.ask && (kind === 'checklist' || kind === 'confirm')) {
1128
+ const lines = linesOf(m)
1129
+ const o = kind === 'confirm' ? 0 : a.option
1130
+ if (o === undefined || !Number.isInteger(o) || o < 0 || o >= lines.length) throw new Error(`option: the line, one of 0 to ${lines.length - 1}`)
1131
+ const tick = a.tick ?? true
1132
+ const choice = m.ask.choices?.[o] ?? null
1133
+ if (a.pick !== undefined && choice === null) throw new Error(`pick: the step « ${lines[o]} » holds no choice`)
1134
+ if (a.pick !== undefined && !(Number.isInteger(a.pick) && a.pick >= 0 && a.pick < choice!.options.length && tick)) throw new Error(`pick: one of 0 to ${choice!.options.length - 1}, with a tick`)
1135
+ const picked = a.pick !== undefined ? `: ${choiceName(choice!.options[a.pick]!)}` : ''
1136
+ return appendMessage(dir, id, { who: 'owner', re: a.re, answer: { option: o, tick, ...(a.pick !== undefined ? { pick: a.pick } : {}) }, text: words === '' ? `${tick ? '✓' : '✗'} ${lines[o]}${picked}` : words }, at)
1137
+ }
1138
+ if (m.ask && (kind === 'text' || kind === 'url')) {
1139
+ const value = a.value?.trim() ?? ''
1140
+ if (value === '') throw new Error(`value: the ${kind} he gives`)
1141
+ if (value.length > ANSWER_VALUE_MAX) throw new Error(`value: at most ${ANSWER_VALUE_MAX} characters`)
1142
+ if (kind === 'url' && !isWebUrl(value)) throw new Error('value: a link that starts with http:// or https://')
1143
+ const opens = kind === 'url' && a.opens !== undefined && Number.isInteger(a.opens) && a.opens >= 0 ? { opens: a.opens } : {}
1144
+ return appendMessage(dir, id, { who: 'owner', re: a.re, answer: { value, ...opens }, text: words === '' ? value : words }, at)
1145
+ }
936
1146
  if (m.ask) {
937
1147
  const o = a.option
938
1148
  if (o === undefined || !Number.isInteger(o) || o < 0 || o >= m.ask.options.length) throw new Error(`option: one of 0 to ${m.ask.options.length - 1}`)
@@ -940,14 +1150,48 @@ export function answerAsk(dir: string, id: string, a: { re: string; option?: num
940
1150
  return appendMessage(dir, id, { who: 'owner', re: a.re, answer: { option: o }, text: words === '' ? label : `${label}: ${words}` }, a.now ? { now: a.now } : {})
941
1151
  }
942
1152
  if (a.apply !== true) throw new Error('a proposed change is answered by apply, or by a reply')
943
- return appendMessage(dir, id, { who: 'owner', re: a.re, answer: { apply: true }, text: words === '' ? m.propose!.after : words }, a.now ? { now: a.now } : {})
1153
+ const fields = proposeFields(m.propose!)
1154
+ const named = a.field === undefined || a.field === '' ? undefined : a.field
1155
+ if (named === undefined && fields.length > 1) throw new Error(`field: one of ${fields.map((y) => y.key).join(', ')}; « apply all » is one answer a field`)
1156
+ const f = named === undefined ? fields[0]! : fields.find((y) => y.key === named)
1157
+ if (!f) throw new Error(`field: one of ${fields.map((y) => y.key).join(', ')}`)
1158
+ const value = a.value !== undefined && a.value.trim() !== '' ? a.value : f.after
1159
+ return appendMessage(dir, id, { who: 'owner', re: a.re, answer: { apply: true, ...(named !== undefined ? { field: named } : {}), value }, text: words === '' ? value : words }, a.now ? { now: a.now } : {})
1160
+ }
1161
+
1162
+ /** the open secret question `re` of item `id`, or why there is none; its name and whether an account goes with it */
1163
+ export function secretQuestion(dir: string, id: string, re: string): { name: string; login: boolean } | { error: string } {
1164
+ const x = readItem(dir, id)
1165
+ const m = x?.thread.find((y) => y.id === re)
1166
+ if (!x || !m || m.ask?.kind !== 'secret' || !m.ask.secret) return { error: `${re} is no secret question of ${id}` }
1167
+ if (!askOpen(x.thread, m)) return { error: `${m.ask.secret.name} is no longer asked here` }
1168
+ return { name: m.ask.secret.name, login: m.ask.secret.login === true }
1169
+ }
1170
+
1171
+ /** the owner's answer to a secret question once its value is in the house secret manager: the name and the time, never
1172
+ * the value, which this function never sees */
1173
+ export function answerSecret(dir: string, id: string, a: { re: string; now?: Date }): Message {
1174
+ const q = secretQuestion(dir, id, a.re)
1175
+ if ('error' in q) throw new Error(q.error)
1176
+ return appendMessage(dir, id, { who: 'owner', re: a.re, answer: { stored: q.name }, text: `stored as ${q.name}` }, a.now ? { now: a.now } : {})
1177
+ }
1178
+
1179
+ /** the owner's click on one of the kind's actions on the thread's object: a message the holder's watch prints */
1180
+ export function postAction(dir: string, id: string, a: { name: string; text?: string; re?: string; now?: Date }): Message {
1181
+ const x = readItem(dir, id)
1182
+ if (!x) throw new Error(`no item ${id}`)
1183
+ if (!/^[a-z][a-z0-9-]*$/.test(a.name)) throw new Error(`action: ${a.name} is no action name`)
1184
+ // `re`: the link of an object in the thread the action is on (a reflection's box), so the box reads its own answer
1185
+ if (a.re !== undefined && !x.thread.some((m) => m.id === a.re && m.about !== undefined)) throw new Error(`re: ${a.re} is no link of ${id}`)
1186
+ const words = a.text?.trim() ?? ''
1187
+ return appendMessage(dir, id, { who: 'owner', action: { name: a.name }, text: words === '' ? a.name : words, ...(a.re !== undefined ? { re: a.re } : {}) }, a.now ? { now: a.now } : {})
944
1188
  }
945
1189
 
946
1190
  /** a link from a thread to another object: the thread names it, the object's thread control finds the thread */
947
- export function attachTo(dir: string, id: string, who: string, address: string, now = new Date()): Message {
1191
+ export function attachTo(dir: string, id: string, who: string, address: string, now = new Date(), o: { asksVerdict?: true } = {}): Message {
948
1192
  if (!readItem(dir, id)) throw new Error(`no item ${id}`)
949
- if (!ADDRESS_RE.test(address)) throw new Error(`${address}: an address is <kind>:<key>`)
950
- return appendMessage(dir, id, { who, text: `attached ${address}`, about: [address] }, { now })
1193
+ const a = addressOrThrow(address)
1194
+ return appendMessage(dir, id, { who, text: `attached ${a}`, about: [a], ...o }, { now })
951
1195
  }
952
1196
 
953
1197
  /** a view of files as typed: one file an image, a clip or a sound (by its extension), two a before and an after; loadView
@@ -969,6 +1213,8 @@ export interface UpdateInput {
969
1213
  /** its pictures as given, never a webp */
970
1214
  keep?: boolean
971
1215
  now?: Date
1216
+ /** written by the machine: no session stamped, so it holds nothing */
1217
+ machine?: true
972
1218
  }
973
1219
 
974
1220
  /** an agent's update on its card, at any time, before or after his answer: its file, or its pair, becomes the card's next view
@@ -977,7 +1223,7 @@ export interface UpdateInput {
977
1223
  export async function postUpdate(dir: string, id: string, u: UpdateInput): Promise<{ item: Item; message: Message; tag: string; view: number | null }> {
978
1224
  const x = readItem(dir, id)
979
1225
  if (!x) throw new Error(`no card ${id}`)
980
- if (x.item.origin.by !== 'agent') throw new Error(`${id} was raised, not sent by an agent: an update goes on the card that answers it`)
1226
+ if (x.item.origin.by !== 'agent') return lineOnNote(dir, x, u)
981
1227
  if (x.item.wip) throw new Error(`${id} is a draft on a live shot: its frame refreshes itself, sk thread ready ${id} when it is done`)
982
1228
  if (!UPDATE_STATES.includes(u.state as UpdateState)) throw new Error(`the state: one of ${UPDATE_STATES.join(', ')}, not « ${u.state} »`)
983
1229
  const state = u.state as UpdateState
@@ -987,12 +1233,28 @@ export async function postUpdate(dir: string, id: string, u: UpdateInput): Promi
987
1233
  for (const f of files) if (!existsSync(f)) throw new Error(`${f}: no such file`)
988
1234
  const k = updatesOf(x).length + 1
989
1235
  const label = `update ${k}: ${UPDATE_WORDS[state]}`
990
- const item = files.length === 0 ? x.item : await addView(dir, id, { ...filesView(label, files, `an update holds one file, or a before and an after: ${updateLine(id)}`), note: t.text }, { update: true, ...(u.keep ? { keep: true } : {}) })
1236
+ const item = files.length === 0 ? x.item : await addView(dir, id, { ...filesView(label, files, `an update holds one file, or a before and an after: ${updateLine(id)}`), note: t.text }, { message: true, ...(u.keep ? { keep: true } : {}) })
991
1237
  const view = files.length === 0 ? null : item.views.length - 1
992
- const message = appendMessage(dir, id, { who: u.who, text: t.text, update: { state, ...(view !== null ? { view } : {}) } }, u.now ? { now: u.now } : {})
1238
+ const message = appendMessage(dir, id, { who: u.who, text: t.text, update: { state, ...(view !== null ? { view } : {}) } }, { ...(u.now ? { now: u.now } : {}), ...(u.machine ? { machine: true } : {}) })
993
1239
  return { item, message, tag: `update ${k} (${UPDATE_WORDS[state]})`, view }
994
1240
  }
995
1241
 
1242
+ /** an update on an item he raised (a thread his note heads): a line on it, its one picture the line's image; the note
1243
+ * keeps its own state. A clip or a pair is a version of the address: sk thread post */
1244
+ async function lineOnNote(dir: string, x: ReviewItem, u: UpdateInput): Promise<{ item: Item; message: Message; tag: string; view: number | null }> {
1245
+ if (!UPDATE_STATES.includes(u.state as UpdateState)) throw new Error(`the state: one of ${UPDATE_STATES.join(', ')}, not « ${u.state} »`)
1246
+ const state = u.state as UpdateState
1247
+ const t = textOf(u.text)
1248
+ if ('error' in t) throw new Error(`the line: ${t.error.replace(/^text: /, '')}`)
1249
+ const files = u.files ?? []
1250
+ const own = ownAddressOf(x.item, specOf(dir))
1251
+ if (files.length > 1 || files.some((f) => !/\.(png|jpe?g|webp)$/i.test(f))) throw new Error(`a line on his note takes one picture; a clip or a before and an after is a version: sk thread post ${own ?? '<kind>:<key>'} …`)
1252
+ for (const f of files) if (!existsSync(f)) throw new Error(`${f}: no such file`)
1253
+ const k = updatesOf(x).length + 1
1254
+ const message = appendMessage(dir, x.item.id, { who: u.who, text: t.text, update: { state } }, { ...(files[0] ? { image: files[0] } : {}), ...(u.now ? { now: u.now } : {}) })
1255
+ return { item: x.item, message, tag: `update ${k} (${UPDATE_WORDS[state]})`, view: null }
1256
+ }
1257
+
996
1258
  /** `party` read what the other party wrote on an item: one `seen` message naming them; how many it read. `only`: the
997
1259
  * messages a page drew, so a word that came after it is never marked read */
998
1260
  export function markRead(dir: string, id: string, party: Party, who: string = party === 'owner' ? 'owner' : agentName(), now = new Date(), only?: readonly string[]): number {
@@ -1182,6 +1444,25 @@ export function withdraw(dir: string, id: string, why: string, now = new Date())
1182
1444
  return {}
1183
1445
  }
1184
1446
 
1447
+ /** takes back one open question or proposed change of an agent (`re`, its message id), with the reason: it waits on
1448
+ * nobody from then, read only, its line « withdrawn: <why> » (review/supersede.ts). The words he said elsewhere (in
1449
+ * chat) go in the reason, quoted, so a question answered outside the hub never stays open */
1450
+ export function withdrawObject(dir: string, re: string, why: string, now = new Date()): { item: string } | { refused: string } {
1451
+ const x = readAll(dir).items.find((y) => y.thread.some((m) => m.id === re))
1452
+ const m = x?.thread.find((y) => y.id === re)
1453
+ if (!x || !m) return { refused: `no message ${re} (sk thread show <address> lists them)` }
1454
+ if (partyOf(m.who) !== 'agent' || (m.ask === undefined && m.propose === undefined) || m.instructions !== undefined)
1455
+ return { refused: `${re} is no question or proposed change of an agent: withdraw takes those back (an ask's instructions: sk ask … --replaces, or its bar)` }
1456
+ if (m.ask !== undefined ? !askOpen(x.thread, m) : !proposeOpen(x.thread, m)) return { refused: `${re}: he answered it already, so it waits on him no more` }
1457
+ const before = x.thread.find((y) => y.op === 'withdraw' && y.re === re)
1458
+ if (before) return { refused: `${re}: withdrawn already by ${before.who} (${before.note ?? ''})` }
1459
+ appendMessage(dir, x.item.id, { who: agentName(), op: 'withdraw', re, note: why }, { now })
1460
+ return { item: x.item.id }
1461
+ }
1462
+
1463
+ /** the reason a question answered in chat is withdrawn with: the owner's words, quoted */
1464
+ export const answeredInChat = (words: string): string => `answered in chat by the owner: « ${words.replace(/\s+/g, ' ').trim()} »`
1465
+
1185
1466
  /** an answered card waits for his answer again: its verdict stays in the thread, before the reopen. `who`: an agent
1186
1467
  * (the CLI), or the owner taking the verdict back (the hub). The verdict reopened, null when it has none */
1187
1468
  export function reopen(dir: string, id: string, o: { who?: string; now?: Date } = {}): Verdict | null {
@@ -1324,7 +1605,7 @@ function locked<T>(dir: string, id: string, fn: () => T, waitMs = 2000): T {
1324
1605
  /** a state change on an item by `who`, checked by `allowed` under the item's lock, so two workers claiming at once
1325
1606
  * never both win; throws with the reason it is refused. The dispatcher's ops need the repo's unit to declare one, and
1326
1607
  * its holder; a topic is one the unit declares */
1327
- export function act(dir: string, id: string, op: Exclude<Op, 'seen'>, who: string, o: { note?: string; text?: string; commit?: string; topic?: string; now?: Date } = {}): Message {
1608
+ export function act(dir: string, id: string, op: Exclude<Op, 'seen'>, who: string, o: { note?: string; text?: string; commit?: string; topic?: string; now?: Date; holds?: boolean } = {}): Message {
1328
1609
  const spec = specOf(dir)
1329
1610
  if (who !== 'owner' && who !== 'agent' && !AGENT_RE.test(who)) throw new Error(`« ${who} » is no agent name (${AGENT_RE.source})`)
1330
1611
  if (!ID_RE.test(id) || !readItem(dir, id)) throw new Error(`no item ${id}`)
@@ -1340,7 +1621,7 @@ export function act(dir: string, id: string, op: Exclude<Op, 'seen'>, who: strin
1340
1621
  const x = readItem(dir, id) as ReviewItem
1341
1622
  const superseded = isFollowed(id, readAll(dir).items)
1342
1623
  const role = spec.dispatcher ? (roleOf(dir)?.name ?? null) : null
1343
- const why = op === 'claim' && superseded && x.item.origin.by === 'agent' ? 'a newer card redoes it: claim that one' : allowed(stateOf(x, superseded), op, who, role, spec.dispatcher)
1624
+ const why = op === 'claim' && superseded && x.item.origin.by === 'agent' ? 'a newer card redoes it: claim that one' : allowed(stateOf(x, superseded), op, who, role, spec.dispatcher, o.holds === true)
1344
1625
  if (why) throw new Error(`${id}: ${why}`)
1345
1626
  const m = appendMessage(
1346
1627
  dir,