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
@@ -0,0 +1,116 @@
1
+ // an object of a thread superseded by a newer one (wiki tools/shipkit/threads-asks.md, Superseded objects). Decided at
2
+ // write time, never inferred: a question, a change or words name the object they supersede in `replaces`; an ask's
3
+ // instructions are superseded by the next instructions in the store's order (the head's words by the first), and each
4
+ // instructions message names in `folds` the questions it folds with the old ones. A superseded object waits on nobody,
5
+ // folds to one line in the thread and keeps its answers. A question an `askBar` message names is the ask bar's own: it
6
+ // waits on nobody and draws nowhere. Pure and browser safe
7
+ import { askOpen } from './answer-kinds.ts'
8
+ import { isAskItem, partyOf, proposeOpen, type Message, type ReviewItem } from './model.ts'
9
+
10
+ /** the store's order of a thread's messages: their ids, which the store makes in the order it writes them */
11
+ const byId = (a: Message, b: Message): number => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0)
12
+
13
+ /** an object an agent posts that a newer one may supersede: a question, a proposed change, its words */
14
+ const isObject = (m: Message): boolean =>
15
+ partyOf(m.who) === 'agent' && m.op === undefined && m.verdict === undefined && m.instructions === undefined && (m.ask !== undefined || m.propose !== undefined || (m.text !== undefined && m.update === undefined && m.askState === undefined))
16
+
17
+ type Instructions = Message & { instructions: NonNullable<Message['instructions']> }
18
+
19
+ /** an ask thread's instructions messages, in the store's order */
20
+ const instructionsOf = (members: readonly ReviewItem[]): Instructions[] =>
21
+ members
22
+ .flatMap((x) => x.thread)
23
+ .filter((m): m is Instructions => m.instructions !== undefined && partyOf(m.who) === 'agent')
24
+ .sort(byId)
25
+
26
+ /** each superseded object of a thread (its members), by its id: the message that supersedes it. The first that names
27
+ * it wins, so a race of two leaves one answer; a `replaces` that names no agent object of the thread is no supersession */
28
+ export function replacersOf(members: readonly ReviewItem[]): Map<string, Message> {
29
+ const out = new Map<string, Message>()
30
+ const all = members.flatMap((x) => x.thread).sort(byId)
31
+ const objects = new Set(all.filter(isObject).map((m) => m.id))
32
+ const set = (id: string, by: Message): void => {
33
+ if (!out.has(id)) out.set(id, by)
34
+ }
35
+ const chain = instructionsOf(members)
36
+ const head = members.find((x) => isAskItem(x.item))
37
+ chain.forEach((m, i) => {
38
+ // a head with no words of its own (an ask made with its instructions as a message) has nothing to supersede
39
+ set(i === 0 ? (head?.item.what?.trim() ? head.item.id : '') : chain[i - 1]!.id, m)
40
+ for (const id of m.instructions.folds ?? []) if (objects.has(id)) set(id, m)
41
+ })
42
+ for (const m of all) if (m.replaces !== undefined && isObject(m) && objects.has(m.replaces) && m.replaces !== m.id) set(m.replaces, m)
43
+ // an agent's withdraw: of one question or change (`re`), or of the card it sits on, every question and change of the
44
+ // card it left open before it. Either waits on nobody from then, read only, its line « withdrawn: <why> »
45
+ for (const x of members)
46
+ for (const w of x.thread) {
47
+ if (w.op !== 'withdraw' || partyOf(w.who) !== 'agent') continue
48
+ if (w.re !== undefined) {
49
+ if (objects.has(w.re)) set(w.re, w)
50
+ continue
51
+ }
52
+ for (const m of x.thread) if ((m.ask !== undefined || m.propose !== undefined) && objects.has(m.id) && m.id < w.id && (m.propose !== undefined ? proposeOpen(x.thread, m) : askOpen(x.thread, m))) set(m.id, w)
53
+ }
54
+ for (const m of all) if (m.askBar !== undefined && partyOf(m.who) === 'agent') for (const id of m.askBar) if (objects.has(id)) set(id, m)
55
+ out.delete('')
56
+ return out
57
+ }
58
+
59
+ /** the questions of an ask's thread that its bar answers (an `askBar` message names them): no cell draws them */
60
+ export const barOwnOf = (members: readonly ReviewItem[]): Set<string> => new Set([...replacersOf(members)].flatMap(([id, m]) => (m.askBar !== undefined ? [id] : [])))
61
+
62
+ /** the words a superseded object's line names its successor by: an ask's title, else the first line of its words */
63
+ export function titleOf(m: Message): string {
64
+ if (m.instructions) return m.instructions.title
65
+ if (m.op === 'withdraw') return `withdrawn: ${m.note ?? 'by its agent'}`
66
+ const l = (m.text ?? '').trim().split('\n')[0]?.trim() ?? ''
67
+ return l.length > 80 ? `${l.slice(0, 79)}…` : l || (m.propose ? `a change of ${m.propose.on}` : 'a newer one')
68
+ }
69
+
70
+ /** an ask thread's newest instructions, null while its first (the head's words) stand */
71
+ export const currentInstructions = (members: readonly ReviewItem[]): Instructions | null => instructionsOf(members).at(-1) ?? null
72
+
73
+ /** an item's title as the reader shows it: an ask's newest instructions, else its own */
74
+ export const itemTitle = (x: ReviewItem): string => currentInstructions([x])?.instructions.title ?? x.item.title
75
+
76
+ /** how long after its instructions a question still counts as posted with them: `sk ask` writes them in one go */
77
+ const WITH_MS = 2_000
78
+
79
+ /** the questions and changes the standing instructions were posted with (their author's, within WITH_MS of them) and
80
+ * that nothing superseded yet: what new instructions fold. A question posted later by hand stays, unless named */
81
+ export function foldsOf(members: readonly ReviewItem[]): string[] {
82
+ const cur = currentInstructions(members)
83
+ const head = members.find((x) => isAskItem(x.item))
84
+ const from = cur ? { at: Date.parse(cur.at), who: cur.who } : head ? { at: Date.parse(head.item.created), who: head.item.origin.by === 'agent' ? (head.item.origin.lane ?? 'agent') : '' } : null
85
+ if (from === null) return []
86
+ const gone = replacersOf(members)
87
+ return members
88
+ .flatMap((x) => x.thread)
89
+ .filter((m) => (m.ask !== undefined || m.propose !== undefined) && m.instructions === undefined && m.who === from.who && !gone.has(m.id) && Date.parse(m.at) >= from.at && Date.parse(m.at) - from.at <= WITH_MS)
90
+ .sort(byId)
91
+ .map((m) => m.id)
92
+ }
93
+
94
+ /** why a new object may not supersede `target` in this thread, null when it may. `holds`: the caller holds the thread or
95
+ * is the session of the lane that does, or nobody holds it (rule 4, review/parties.ts maySee): a courtesy between
96
+ * agents, never a lock, since any write makes its writer the holder */
97
+ export function replaceRefusal(members: readonly ReviewItem[], target: string, o: { holds: boolean }): string | null {
98
+ if (!o.holds) return `--replaces ${target}: only the agent that holds the thread, or its session, supersedes an object in it (sk thread take <address> first)`
99
+ const head = members.find((x) => x.item.id === target)
100
+ const m = members.flatMap((x) => x.thread).find((y) => y.id === target)
101
+ if (head) return isAskItem(head.item) ? `--replaces ${target}: an ask's instructions are superseded by new ones: sk ask "<title>" --replaces ${head.item.on ?? target}` : `--replaces ${target}: a version is followed by the next one (sk thread post), never superseded`
102
+ if (!m) return `--replaces ${target}: no object of this thread has that id (sk thread show <address> lists them)`
103
+ if (m.instructions !== undefined) return `--replaces ${target}: an ask's instructions are superseded by new ones: sk ask "<title>" --replaces <ask>`
104
+ if (!isObject(m)) return `--replaces ${target}: ${partyOf(m.who) === 'owner' ? 'the owner\'s words stay as written' : 'that message is no object: a question, a proposed change or words'}`
105
+ const by = replacersOf(members).get(target)
106
+ if (by) return `--replaces ${target}: superseded already by ${by.id} « ${titleOf(by)} »: supersede that one`
107
+ return null
108
+ }
109
+
110
+ /** why the owner's answer on the object `re` of item `id` is refused because a newer object superseded it, null when it is not:
111
+ * a superseded object is read only, the owner's answers on it kept as they were */
112
+ export function supersededRefusal(threadOf: (id: string) => readonly ReviewItem[] | null, id: string, re: string): string | null {
113
+ const members = threadOf(id)
114
+ const by = members === null ? undefined : replacersOf(members).get(re)
115
+ return by ? `${re} was superseded by « ${titleOf(by)} »: answer that one` : null
116
+ }
@@ -0,0 +1,52 @@
1
+ // a resumed lane takes its threads back at its first `sk` call (wiki tools/shipkit/threads-how-it-fits.md, The parties):
2
+ // its session heard his message on them after it ended (agent-time/registry.ts, Handed), and resumed it instead of
3
+ // taking them itself. Runs at every call's boot (cli.ts), so the gate is one file read; the store is read only past it
4
+ // the one constraint a reader breaks first: it never fails the command it runs before, and never writes to stdout
5
+
6
+ /** what the take back did, for the notice: the addresses it holds again, and those another party took meanwhile */
7
+ export interface TakenBack {
8
+ lane: string
9
+ held: string[]
10
+ gone: { address: string; by: string }[]
11
+ }
12
+
13
+ export async function takeBack(env: Record<string, string | undefined> = process.env): Promise<TakenBack | null> {
14
+ const lane = env.SK_AGENT?.trim()
15
+ const session = env.CLAUDE_CODE_SESSION_ID?.trim()
16
+ if (!lane || !session) return null
17
+ const { clearHanded, readHanded } = await import('../agent-time/registry.ts')
18
+ const handed = readHanded(session, lane)
19
+ if (handed === null) return null
20
+ const { appendMessage, readAll, specOf } = await import('./store.ts')
21
+ const { holderOf, threadOfItem, threadsOf } = await import('./threads.ts')
22
+ const out: TakenBack = { lane, held: [], gone: [] }
23
+ for (const x of handed.threads) {
24
+ const t = threadOfItem(x.head, threadsOf(readAll(x.dir).items, specOf(x.dir)))
25
+ if (t === null) continue
26
+ const h = holderOf(t)
27
+ if (h?.lane === lane && h.session === session) out.held.push(x.address)
28
+ else if (h?.lane === undefined && (h === null || h.session === session)) {
29
+ // its session wrote in it (a reply) after the lane ended: the resume hands it back
30
+ appendMessage(x.dir, t.head.item.id, { who: lane, op: 'take' })
31
+ out.held.push(x.address)
32
+ } else out.gone.push({ address: x.address, by: h?.lane ?? `session ${h?.session?.slice(0, 8) ?? '?'}` })
33
+ }
34
+ clearHanded(session, lane)
35
+ return out
36
+ }
37
+
38
+ export const takeBackText = (r: TakenBack, bin = 'sk'): string =>
39
+ [
40
+ ...(r.held.length > 0 ? [`${r.lane}: you hold ${r.held.join(', ')} again, his reply came while you were away: ${bin} thread show ${r.held[0]}; hear the next one: SK_AGENT=${r.lane} ${bin} thread watch --mine under a monitor`] : []),
41
+ ...r.gone.map((g) => `${r.lane}: ${g.address} is ${g.by}'s now, leave it`),
42
+ ].join('\n')
43
+
44
+ /** the boot step: the take back and its notice on stderr; a failure is one WARN line, and the command runs as asked */
45
+ export async function takeBackAtBoot(): Promise<void> {
46
+ try {
47
+ const r = await takeBack()
48
+ if (r !== null && (r.held.length > 0 || r.gone.length > 0)) process.stderr.write(`${takeBackText(r)}\n`)
49
+ } catch (e) {
50
+ process.stderr.write(`sk: WARN your threads were not taken back (${(e as Error).message.split('\n')[0]}): sk thread take <address> for each\n`)
51
+ }
52
+ }
@@ -6,21 +6,27 @@ import { ownAddressOf, type ReviewItem, type Spec } from './model.ts'
6
6
  import { readAll, specOf, updateHead } from './store.ts'
7
7
  import { ownAddressesOf, threadsOf, type Thread } from './threads.ts'
8
8
 
9
- /** one link the fold writes: `id` (a thread's oldest version) supersedes `supersedes` (the newest of the one before) */
9
+ /** one link the fold writes: `id` supersedes `supersedes` (the newest of the thread before); `was`: the version it
10
+ * superseded before, on a fork the one the two heads share, and the write holds only while it still does */
10
11
  export interface FoldLink {
11
12
  address: string
12
13
  id: string
13
14
  supersedes: string
15
+ was?: string
14
16
  }
15
17
 
16
18
  const created = (t: Thread): string => t.head.item.created
17
19
 
20
+ /** a trailer thread whose every version names exactly one shot: a refilm of that shot he judged alone, its own subject */
21
+ const oneShot = (t: Thread): boolean => t.versions.every((v) => v.item.trailer !== undefined && v.item.trailer.shots.length === 1)
22
+
18
23
  /** the links that make each address one thread, pure: agents' threads only; a note joins at read time already. A
19
- * thread whose oldest version a link already names, or that shares a version with the one before (a fork), is left */
24
+ * thread whose oldest version a link already names is left; two heads that share older versions (a fork) are folded:
25
+ * the later head's first version of its own supersedes the other head in place of the version they share */
20
26
  export function foldPlan(items: readonly ReviewItem[], spec: Spec): FoldLink[] {
21
27
  const by = new Map<string, Thread[]>()
22
28
  for (const t of threadsOf(items, spec)) {
23
- if (t.head.item.origin.by !== 'agent') continue
29
+ if (t.head.item.origin.by !== 'agent' || oneShot(t)) continue
24
30
  const a = ownAddressOf(t.head.item, spec) ?? ownAddressesOf(t, spec)[0]
25
31
  if (a === undefined) continue
26
32
  by.set(a, [...(by.get(a) ?? []), t])
@@ -31,9 +37,16 @@ export function foldPlan(items: readonly ReviewItem[], spec: Spec): FoldLink[] {
31
37
  const sorted = [...ts].sort((a, b) => created(a).localeCompare(created(b)) || a.head.item.id.localeCompare(b.head.item.id))
32
38
  for (let i = 1; i < sorted.length; i++) {
33
39
  const prev = sorted[i - 1]!
34
- const tail = sorted[i]!.versions.at(-1)!
35
- if (tail.item.supersedes !== undefined || prev.versions.some((v) => v.item.id === tail.item.id)) continue
36
- out.push({ address, id: tail.item.id, supersedes: prev.head.item.id })
40
+ const cur = sorted[i]!
41
+ const shared = new Set(prev.versions.map((v) => v.item.id))
42
+ const own = [...cur.versions].reverse().find((v) => !shared.has(v.item.id))
43
+ if (own === undefined) continue
44
+ const was = own.item.supersedes
45
+ if (was !== undefined && shared.has(was)) {
46
+ if (was !== prev.head.item.id) out.push({ address, id: own.item.id, supersedes: prev.head.item.id, was })
47
+ continue
48
+ }
49
+ if (was === undefined) out.push({ address, id: own.item.id, supersedes: prev.head.item.id })
37
50
  }
38
51
  }
39
52
  return out
@@ -46,6 +59,6 @@ export function foldStore(dir: string, o: { go: boolean }): { links: FoldLink[];
46
59
  const links = foldPlan(items, spec)
47
60
  const before = threadsOf(items, spec).length
48
61
  if (!o.go) return { links, before, after: before - links.length }
49
- for (const l of links) updateHead(dir, l.id, (cur) => (cur.supersedes === undefined ? { ...cur, supersedes: l.supersedes } : cur))
62
+ for (const l of links) updateHead(dir, l.id, (cur) => (cur.supersedes === l.was ? { ...cur, supersedes: l.supersedes } : cur))
50
63
  return { links, before, after: threadsOf(readAll(dir).items, spec).length }
51
64
  }
@@ -3,20 +3,25 @@
3
3
  // their status, who acts now, who holds them, their conversation and order, and the thread an address already has.
4
4
  // Pure, over the items of review/model.ts
5
5
  import {
6
+ canonicalAddress,
6
7
  coversCard,
7
8
  type WatchFilter,
8
9
  addressesOf,
9
10
  ownAddressOf,
10
11
  parentAddress,
11
12
  inWork,
13
+ isOpening,
12
14
  moveOf,
13
15
  isReply,
14
16
  partyOf,
17
+ proposeFields,
18
+ proposeOpen,
15
19
  reworkOf,
16
20
  stateOf,
17
21
  subjectKey,
18
22
  takenBackOf,
19
23
  unreadBy,
24
+ updatesOf,
20
25
  verdictOf,
21
26
  withdrawnOf,
22
27
  wordsOf,
@@ -26,7 +31,15 @@ import {
26
31
  type Spec,
27
32
  type Subject,
28
33
  NO_SPEC,
34
+ isAskItem,
29
35
  } from './model.ts'
36
+ import { addressKindProblem } from './kinds.ts'
37
+ import { heldOpen, holdMoveOf } from './holds.ts'
38
+ import { laneAskOf } from './lane-ask.ts'
39
+ import { askOpen } from './answer-kinds.ts'
40
+ import { currentInstructions, replacersOf } from './supersede.ts'
41
+ import { openVerdictAsks } from './verdict-items.ts'
42
+ import { hearingOf, heardBy, maySee, NO_PARTIES, sessionLabel, type Hearing, type Holder, type Me, type Parties } from './parties.ts'
30
43
 
31
44
  /** one thread of versions: a head no later card supersedes, then the chain it supersedes back to the first, and the
32
45
  * raised items any of them answers */
@@ -119,7 +132,7 @@ export const ownAddressesOf = (t: Thread, spec: Spec = NO_SPEC): string[] => [..
119
132
 
120
133
  /** every address a thread names: the ones it owns, then each object it is about or a message attached (links) */
121
134
  export const addressesOfThread = (t: Thread, spec: Spec = NO_SPEC): string[] =>
122
- [...new Set([...ownAddressesOf(t, spec), ...membersOf(t).flatMap((x) => [...addressesOf(x.item, spec), ...x.thread.flatMap((m) => m.about ?? [])])])]
135
+ [...new Set([...ownAddressesOf(t, spec), ...membersOf(t).flatMap((x) => [...addressesOf(x.item, spec), ...x.thread.flatMap((m) => (m.about ?? []).map((a) => canonicalAddress(a) ?? a))])])]
123
136
 
124
137
  const built = new WeakMap<readonly ReviewItem[], { spec: Spec; threads: Thread[] }>()
125
138
 
@@ -176,7 +189,8 @@ function upFrom(a: string | null): string[] {
176
189
 
177
190
  /** the thread that owns this address, null when none does: the newest one, whatever its state. `up`: else the one
178
191
  * that owns an address above it, where his note on a part of an object goes */
179
- export function threadOfAddress(address: string, items: readonly ReviewItem[], spec: Spec = NO_SPEC, o: { up?: boolean } = {}): Thread | null {
192
+ export function threadOfAddress(raw: string, items: readonly ReviewItem[], spec: Spec = NO_SPEC, o: { up?: boolean } = {}): Thread | null {
193
+ const address = canonicalAddress(raw) ?? raw
180
194
  const threads = threadsOf(items, spec)
181
195
  for (const a of o.up ? upFrom(address) : [address]) {
182
196
  const t = threads.filter((x) => ownAddressesOf(x, spec).includes(a)).sort((x, y) => newest(x.head, y.head))[0]
@@ -186,8 +200,10 @@ export function threadOfAddress(address: string, items: readonly ReviewItem[], s
186
200
  }
187
201
 
188
202
  /** every thread that names this address or one under it, as its own or as a link: the threads of an object */
189
- export const threadsNaming = (address: string, items: readonly ReviewItem[], spec: Spec = NO_SPEC): Thread[] =>
190
- threadsOf(items, spec).filter((t) => addressesOfThread(t, spec).some((a) => a === address || a.startsWith(`${address}/`)))
203
+ export function threadsNaming(raw: string, items: readonly ReviewItem[], spec: Spec = NO_SPEC): Thread[] {
204
+ const address = canonicalAddress(raw) ?? raw
205
+ return threadsOf(items, spec).filter((t) => addressesOfThread(t, spec).some((a) => a === address || a.startsWith(`${address}/`)))
206
+ }
191
207
 
192
208
  /** the newest card answering this raised item, null when none does */
193
209
  export const answererOf = (id: string, items: readonly ReviewItem[]): ReviewItem | null => linksOf(items).answerers.get(id)?.[0] ?? null
@@ -197,6 +213,19 @@ const supersederOf = (id: string, items: readonly ReviewItem[]): ReviewItem | nu
197
213
  /** a later card follows the item: a new version of it, or a card answering it (stateOf's `superseded`) */
198
214
  export const isFollowed = (id: string, items: readonly ReviewItem[]): boolean => supersederOf(id, items) !== null || answererOf(id, items) !== null
199
215
 
216
+ /** the title a thread shows: an ask's newest instructions, else its head's */
217
+ export const threadTitle = (t: Thread): string => currentInstructions(membersOf(t))?.instructions.title ?? t.head.item.title
218
+
219
+ /** the ask facts that stand on an ask's thread: its newest instructions', else its head's; null on another thread */
220
+ export const askFactsOf = (t: Thread): Omit<NonNullable<ReviewItem['item']['ask']>, 'created' | 'pinged'> | null =>
221
+ isAskItem(t.head.item) && t.head.item.ask !== undefined ? (currentInstructions(membersOf(t))?.instructions.ask ?? t.head.item.ask) : null
222
+
223
+ /** the members of the thread an item reads in, null when none */
224
+ export const membersOfItem = (items: readonly ReviewItem[]) => (id: string): ReviewItem[] | null => {
225
+ const t = threadOfItem(id, threadsOf(items))
226
+ return t === null ? null : membersOf(t)
227
+ }
228
+
200
229
  /** the thread an item reads in: its own, the one whose history holds it, or the card's that answers it */
201
230
  export const threadOfItem = (id: string, threads: readonly Thread[]): Thread | null =>
202
231
  threads.find((t) => t.head.item.id === id) ?? threads.find((t) => membersOf(t).some((x) => x.item.id === id)) ?? null
@@ -205,11 +234,23 @@ export const threadOfItem = (id: string, threads: readonly Thread[]): Thread | n
205
234
  * « no » until a new version or `wontfix`; reading the verdict is never doing it; an approval needs nothing), or a
206
235
  * lane reworking it; what he or a player raised, until it is closed */
207
236
  export function isActive(t: Thread, now = new Date()): boolean {
208
- if (openAsksOf(t).length > 0) return true
237
+ return heldOpen(membersOf(t)).length > 0 || owesWork(t, now)
238
+ }
239
+
240
+ /** an agent owes the thread its next move, the objects that hold it open aside */
241
+ function owesWork(t: Thread, now: Date): boolean {
242
+ // his request for a lane waits on the orchestrator until an agent acts in the thread (review/lane-ask.ts)
243
+ if (laneAskOf(membersOf(t)) !== null) return true
244
+ if (openAsksOf(t).length > 0 || openItemVerdictsOf(t).length > 0) return true
209
245
  if (t.answers.some((x) => t.joined.has(x.item.id) && stateOf(x, false).state !== 'closed')) return true
210
246
  if (t.versions.some((x) => unreadBy(x, 'agent').some((m) => m !== openingOf(x)))) return true
211
247
  if (t.answers.some((x) => unreadBy(x, 'agent').some((m) => m !== openingOf(x)))) return true
212
248
  if (t.head.item.origin.by !== 'agent') return stateOf(t.head, false).state !== 'closed'
249
+ // opened with words: in work while the agent's latest update says so; a plain word owes nothing
250
+ if (isOpening(t.head.item)) {
251
+ const last = updatesOf(t.head).at(-1)?.update?.state
252
+ return last === 'broken' || last === 'better'
253
+ }
213
254
  if (reworkOf(t.head, false, now)) return true
214
255
  const f = verdictOf(t.head)
215
256
  if (!f) return withdrawnOf(t.head) === undefined
@@ -226,7 +267,7 @@ export function conversationOf(t: Thread): (Message & { on: string })[] {
226
267
  }
227
268
 
228
269
  /** a head waits for the owner's verdict: an agent's card, none yet, not withdrawn, not a draft */
229
- export const waits = (x: ReviewItem): boolean => x.item.origin.by === 'agent' && !inWork(x) && verdictOf(x) === null && withdrawnOf(x) === undefined
270
+ export const waits = (x: ReviewItem): boolean => x.item.origin.by === 'agent' && (!isOpening(x.item) || x.item.asksVerdict === true) && !inWork(x) && verdictOf(x) === null && withdrawnOf(x) === undefined
230
271
 
231
272
  /** the active pending heads first in the order they were sent, then every other thread, newest first */
232
273
  export function orderedThreads(items: readonly ReviewItem[]): Thread[] {
@@ -289,6 +330,8 @@ export function statusOf(x: ReviewItem, items: readonly ReviewItem[]): ShownStat
289
330
  if (v) return v.status
290
331
  if (withdrawnOf(x) !== undefined) return 'withdrawn'
291
332
  if (inWork(x)) return 'wip'
333
+ // a thread opened with words that its agent called fixed: nothing waits
334
+ if (isOpening(x.item) && x.item.asksVerdict !== true) return 'fixed'
292
335
  return supersederOf(x.item.id, items) ? 'superseded' : 'pending'
293
336
  }
294
337
 
@@ -310,45 +353,112 @@ export function unseen(items: readonly ReviewItem[]): ReviewItem[] {
310
353
  .map((u) => u.x)
311
354
  }
312
355
 
313
- /** what waits on him: the cards waiting for the owner's verdict, and what he raised that asks him a question or is blocked on him */
314
- export function waitingOnOwner(items: readonly ReviewItem[]): Thread[] {
315
- const followed = followedIds(items)
316
- return orderedThreads(items).filter((t) => waits(t.head) || (t.head.item.origin.by !== 'agent' && ['question', 'blocked'].includes(stateOf(t.head, followed.has(t.head.item.id)).state)))
317
- }
356
+ /** what waits on him: the threads whose turn is his (turnOf), the one count the hub, the tree's pill and `sk needs` show */
357
+ export const waitingOnOwner = (items: readonly ReviewItem[], now = new Date()): Thread[] =>
358
+ // an ask's thread is counted as its ask, once (wiki tools/shipkit/threads-asks.md, An ask's thread)
359
+ orderedThreads(items).filter((t) => turnOf(t, now) === 'yours' && !isAskItem(t.head.item))
318
360
 
319
- /** who acts now on a thread (wiki tools/shipkit/threads.md, The model) */
320
- export type Turn = 'yours' | 'working' | 'done'
361
+ /** who acts now on a thread (wiki tools/shipkit/threads-how-it-fits.md, The model): his turn, his turn set aside, an
362
+ * agent's move (whether one works it now is the band's, band.ts), nothing owed */
363
+ export type Turn = 'yours' | 'aside' | 'agents' | 'done'
321
364
 
322
- /** each question and proposed change of the agents that no message of his answers yet (`re` names it), oldest first */
365
+ /** each question and proposed change of the agents that no message of his answers yet (`re` names it) and no newer
366
+ * object supersedes (review/supersede.ts), oldest first */
323
367
  export function openAsksOf(t: Thread): (Message & { on: string })[] {
324
- const all = membersOf(t).flatMap((x) => x.thread.map((m) => ({ ...m, on: x.item.id })))
325
- const answered = new Set(all.flatMap((m) => (partyOf(m.who) === 'owner' && m.re !== undefined ? [m.re] : [])))
326
- return all.filter((m) => (m.ask !== undefined || m.propose !== undefined) && partyOf(m.who) === 'agent' && !answered.has(m.id)).sort((a, b) => a.at.localeCompare(b.at))
368
+ if (askStateOf(t)?.state === 'done' || askStateOf(t)?.state === 'declined') return []
369
+ const members = membersOf(t)
370
+ const gone = replacersOf(members)
371
+ const out = members.flatMap((x) => {
372
+ return x.thread.filter((m) => partyOf(m.who) === 'agent' && !gone.has(m.id) && (m.propose !== undefined ? proposeOpen(x.thread, m) : askOpen(x.thread, m))).map((m) => ({ ...m, on: x.item.id }))
373
+ })
374
+ return afterClose(t, out).sort((a, b) => a.at.localeCompare(b.at))
375
+ }
376
+
377
+ /** each message of the thread that asks the owner's verdict and has none, oldest first; a closed ask's thread asks nothing */
378
+ export function openItemVerdictsOf(t: Thread): (Message & { on: string })[] {
379
+ if (askStateOf(t)?.state === 'done' || askStateOf(t)?.state === 'declined') return []
380
+ const members = membersOf(t)
381
+ return afterClose(t, openVerdictAsks(members, new Set(replacersOf(members).keys())))
382
+ }
383
+
384
+ /** when he closed the thread: his act after which its head stays closed (his OK or archive, his close), null while it is
385
+ * open or when an agent or the machine closed it (a check's thread `fixed` by a run moots nothing); an ask's thread
386
+ * closes by its ask. A reopen or a new version opens the head, and its requests wait again (wiki
387
+ * tools/shipkit/threads-how-it-fits.md, The model) */
388
+ export function closedAt(t: Thread): string | null {
389
+ const x = t.head
390
+ if (isAskItem(x.item)) {
391
+ const a = askStateOf(t)
392
+ return a !== null && (a.state === 'done' || a.state === 'declined') ? a.at : null
393
+ }
394
+ if (stateOf(x, false).state !== 'closed') return null
395
+ for (let i = x.thread.length - 1; i >= 0; i--) {
396
+ if (stateOf({ ...x, thread: x.thread.slice(0, i) }, false).state === 'closed') continue
397
+ const m = x.thread[i]!
398
+ return partyOf(m.who) === 'owner' ? m.at : null
399
+ }
400
+ return null
327
401
  }
328
402
 
329
- /** yours: a version waits for the owner's verdict, a question or a proposed change for the owner's answer, or a note
330
- * the owner raised is the owner's move; working: an agent owes the next move; done: nothing is owed */
403
+ /** a request written before his close is moot: it waits on nobody, its cell reads « closed, not answered » */
404
+ export const isMoot = (t: Thread, m: Pick<Message, 'at'>): boolean => {
405
+ const cut = closedAt(t)
406
+ return cut !== null && m.at <= cut
407
+ }
408
+
409
+ const afterClose = <M extends Pick<Message, 'at'>>(t: Thread, ms: M[]): M[] => (ms.length === 0 ? ms : ms.filter((m) => !isMoot(t, m)))
410
+
411
+ /** yours: a version waits for the owner's verdict, a question or a proposed change for the owner's answer, a note the
412
+ * owner raised is the owner's move, or only an object whose move is the owner's holds it open (a branch to merge);
413
+ * agents: an agent owes the next move; done: nothing is owed */
331
414
  export function turnOf(t: Thread, now = new Date()): Turn {
332
- if (waits(t.head) || openAsksOf(t).length > 0) return 'yours'
415
+ const base = baseTurnOf(t, now)
416
+ return base === 'yours' && asideUntil(t, now) !== null ? 'aside' : base
417
+ }
418
+
419
+ /** the newest ask state written in the thread with its time, null on a thread that is no ask's or never said one */
420
+ export function askStateOf(t: Thread): (NonNullable<Message['askState']> & { at: string }) | null {
421
+ let last: (NonNullable<Message['askState']> & { at: string }) | null = null
422
+ for (const x of membersOf(t)) for (const m of x.thread) if (m.askState && (last === null || m.at >= last.at)) last = { ...m.askState, at: m.at }
423
+ return last
424
+ }
425
+
426
+ /** when a thread set aside comes back, null when it is not set aside now: the newest « set aside » still ahead, unless
427
+ * he acted on the thread after it, or an agent sent an urgent message after it */
428
+ export function asideUntil(t: Thread, now = new Date()): string | null {
429
+ let snooze: { at: string; until: string | null } | null = null
430
+ const msgs = membersOf(t).flatMap((x) => x.thread)
431
+ for (const m of msgs) if (m.snooze && (snooze === null || m.at >= snooze.at)) snooze = { at: m.at, until: m.snooze.until }
432
+ if (snooze === null || snooze.until === null || Date.parse(snooze.until) <= now.getTime()) return null
433
+ const after = msgs.filter((m) => m.at > snooze!.at)
434
+ if (after.some((m) => m.urgent === true && partyOf(m.who) === 'agent')) return null
435
+ if (after.some((m) => partyOf(m.who) === 'owner' && m.op !== 'seen' && m.snooze === undefined)) return null
436
+ return snooze.until
437
+ }
438
+
439
+ function baseTurnOf(t: Thread, now: Date): 'yours' | 'agents' | 'done' {
440
+ if (waits(t.head) || openAsksOf(t).length > 0 || openItemVerdictsOf(t).length > 0) return 'yours'
441
+ // an open ask with nothing open: its bar waits on him (« done ✓ », his hands until its check passes), unless his
442
+ // words came last, which an agent reads and answers
443
+ if (isAskItem(t.head.item) && askStateOf(t)?.state !== 'done' && askStateOf(t)?.state !== 'declined') {
444
+ const said = membersOf(t).flatMap((x) => x.thread).filter((m) => m.op !== 'seen' && m.op !== 'working' && m.askBar === undefined && m.snooze === undefined && m.askState === undefined)
445
+ const last = said.sort((a, b) => a.at.localeCompare(b.at)).at(-1)
446
+ return last !== undefined && partyOf(last.who) === 'owner' && last.answer === undefined && last.verdict === undefined ? 'agents' : 'yours'
447
+ }
333
448
  const notes = [...(t.head.item.origin.by === 'agent' ? [] : [t.head]), ...t.answers.filter((x) => t.joined.has(x.item.id))]
334
449
  if (notes.some((x) => moveOf(stateOf(x, false).state) === 'owner')) return 'yours'
335
- return isActive(t, now) ? 'working' : 'done'
450
+ if (owesWork(t, now)) return 'agents'
451
+ const held = heldOpen(membersOf(t))
452
+ if (held.length === 0) return 'done'
453
+ return held.some((h) => holdMoveOf(h) === 'owner') ? 'yours' : 'agents'
336
454
  }
337
455
 
338
- /** the agent that holds a thread: its newest act in it (a version, an update, a reply, a question, a claim; never a read
339
- * mark), by its lane and its session. Null on a thread no agent touched */
340
- export interface Holder {
341
- lane?: string
342
- session?: string
343
- at: string
344
- }
456
+ /** the agent that holds a thread: its newest act in it (a version, an update, a reply, a question, a claim, a take;
457
+ * never a read mark), by its lane and its session. Null on a thread no agent touched. Who hears it and who may mark
458
+ * it seen follow from it (review/parties.ts) */
459
+ export type { Holder } from './parties.ts'
345
460
 
346
461
  export function holderOf(t: Thread): Holder | null {
347
- // a live claim holds it, as it holds who answers (model.ts notAnswererOf)
348
- for (const x of membersOf(t)) {
349
- const st = stateOf(x, false)
350
- if (st.state === 'claimed' && st.since !== undefined && st.worker !== undefined) return { lane: st.worker, at: st.since }
351
- }
352
462
  const acts: Holder[] = []
353
463
  for (const x of membersOf(t)) {
354
464
  const o = x.item.origin
@@ -356,10 +466,13 @@ export function holderOf(t: Thread): Holder | null {
356
466
  for (const m of x.thread)
357
467
  if (partyOf(m.who) === 'agent' && m.op !== 'seen') acts.push({ ...(m.who !== 'agent' ? { lane: m.who } : {}), ...(m.session ? { session: m.session } : {}), at: m.at })
358
468
  }
359
- // an act that names nobody (an older message of an unnamed agent) holds nothing
360
- return acts.filter((a) => a.lane !== undefined || a.session !== undefined).sort((a, b) => b.at.localeCompare(a.at))[0] ?? null
469
+ // an act that names nobody (an older message of an unnamed agent) holds nothing; on one instant the later written wins
470
+ return acts.filter((a) => a.lane !== undefined || a.session !== undefined).reduce<Holder | null>((best, a) => (best === null || a.at >= best.at ? a : best), null)
361
471
  }
362
472
 
473
+ /** `me` holds it by the act alone, no registry at hand: its lane when the holder names one, else its session */
474
+ const isHolder = (me: Me, h: Holder): boolean => (h.lane !== undefined ? me.lane === h.lane && (me.session === null || h.session === undefined || me.session === h.session) : me.session !== null && me.session === h.session)
475
+
363
476
  /** each item's thread by its id, built once for a pass over many items */
364
477
  export function threadByItem(threads: readonly Thread[]): Map<string, Thread> {
365
478
  const by = new Map<string, Thread>()
@@ -367,12 +480,96 @@ export function threadByItem(threads: readonly Thread[]): Map<string, Thread> {
367
480
  return by
368
481
  }
369
482
 
370
- /** a watch or an inbox with this filter hears what lands on `x`: the thread's holder is its lane or its session, so an
371
- * answer wakes whoever holds the thread, not only the session that opened it. A thread nobody holds: its maker's */
372
- export function hears(f: WatchFilter, x: ReviewItem, byItem: ReadonlyMap<string, Thread>): boolean {
483
+ /** who hears his answer on a thread now, its light and its words (rule 3, review/parties.ts hearingOf) */
484
+ export const hearingOfThread = (t: Thread, p: Parties, repo: string): Hearing => hearingOf(holderOf(t), p, repo)
485
+
486
+ /** a watch or an inbox with this filter hears what lands on `x`. With the registry: the party that hears the thread
487
+ * now (its holder, else its parent session for it); with none, its holder by the act alone. A bare filter hears every
488
+ * thread (the dispatcher); a thread nobody holds, its maker's */
489
+ export function hears(f: WatchFilter, x: ReviewItem, byItem: ReadonlyMap<string, Thread>, p: Parties | null = null): boolean {
373
490
  if (f.session === null && f.lane === null) return true
374
491
  const t = byItem.get(x.item.id)
375
492
  const h = t ? holderOf(t) : null
376
493
  if (h === null) return coversCard(f, x)
377
- return (f.session === null || f.session === h.session) && (f.lane === null || f.lane === h.lane)
494
+ return p === null ? (f.session === null || f.session === h.session) && (f.lane === null || f.lane === h.lane) : heardBy(f, hearingOf(h, p, ''), p)
495
+ }
496
+
497
+ /** the live watches that read his answer on `x`: the holder's, and a bare one, which hears every thread */
498
+ export const listenersOf = <W extends WatchFilter>(watchers: readonly W[], x: ReviewItem, byItem: ReadonlyMap<string, Thread>, p: Parties | null = null): W[] => watchers.filter((w) => hears(w, x, byItem, p))
499
+
500
+ /** as a hook (`inbox --hook`) of `me`, a session: `mine` when `me` hears the thread now (it holds it, or its lane that
501
+ * holds it does not hear), `nobody` when no live party does (every session of its repo sees it, until one takes it) */
502
+ export function hookHears(me: Me, x: ReviewItem, byItem: ReadonlyMap<string, Thread>, p: Parties, repo: string): 'mine' | 'nobody' | null {
503
+ const t = byItem.get(x.item.id)
504
+ const hearing = hearingOf(t ? holderOf(t) : null, p, repo)
505
+ if (hearing.light === 'nobody') return 'nobody'
506
+ return heardBy(me, hearing, p) ? 'mine' : null
507
+ }
508
+
509
+ /** why `me` may not mark the owner's answer on `x` seen, null when it may (rule 4: the holder or an ancestor, its
510
+ * session). A word is never refused: writing in a thread, or `sk thread take`, is how an agent takes it. A plain
511
+ * approval owes nothing, anyone reads it. An item he or a player raised follows the claim rules */
512
+ export function seenRefusal(x: ReviewItem, t: Thread | null, me: Me, p: Parties = NO_PARTIES): string | null {
513
+ if (x.item.origin.by !== 'agent') return null
514
+ const s = stateOf(x, false)
515
+ if (s.state === 'closed' && s.why === 'approved') return null
516
+ const h = t ? holderOf(t) : null
517
+ if (h === null || maySee(me, h, p) || isHolder(me, h)) return null
518
+ const hearing = hearingOf(h, p, '')
519
+ const holder = h.lane !== undefined ? `lane ${h.lane} of ${sessionLabel(h.session, p)}` : sessionLabel(h.session, p)
520
+ const address = t ? (ownAddressesOf(t)[0] ?? x.item.id) : x.item.id
521
+ return `${x.item.id} is held by ${holder} (${hearing.words}): that agent marks it seen, or its session; to act on it yourself, sk thread take ${address}, then mark it seen`
522
+ }
523
+
524
+ /** what moved since the owner's last act in the store (a verdict, a word, a note, an answer; never a read: the hub marks
525
+ * what he sees read a moment after the page opens, which would empty the line): the threads an agent wrote in since,
526
+ * and how many of them are his turn. Null before his first act. The line he reads on waking */
527
+ export function sinceHeLeft(items: readonly ReviewItem[], now = new Date()): { at: string; moved: number; yours: number } | null {
528
+ let at = ''
529
+ for (const x of items) {
530
+ if (x.item.origin.by === 'owner' && x.item.created > at) at = x.item.created
531
+ for (const m of x.thread) if (m.who === 'owner' && m.op !== 'seen' && m.at > at) at = m.at
532
+ }
533
+ if (at === '') return null
534
+ const moved = threadsOf(items).filter((t) => membersOf(t).some((x) => (x.item.origin.by === 'agent' && x.item.created > at) || x.thread.some((m) => partyOf(m.who) === 'agent' && m.op !== 'seen' && m.at > at)))
535
+ return { at, moved: moved.length, yours: moved.filter((t) => turnOf(t, now) === 'yours').length }
536
+ }
537
+
538
+ /** what « it is fixed » does on a thread (`sk thread fixed`): `asked` a note of the owner, by its id, closes it; else the owner's one open note in
539
+ * the thread closes (two or more are named, one picked by its id); else the newest version he answered « ok with a fix »
540
+ * closes (done), and one he rejected or that is in work asks the owner's verdict again (an update `fixed`) */
541
+ export function fixedPlan(t: Thread, asked: ReviewItem, o: { byAddress?: boolean } = {}): { op: 'fixed' | 'done' | 'update'; id: string } | { error: string } {
542
+ // an address names the thread, never its head note: a closed note heading it stands for nothing
543
+ if (asked.item.origin.by !== 'agent' && !o.byAddress) return { op: 'fixed', id: asked.item.id }
544
+ const notes = [...(t.head.item.origin.by === 'agent' ? [] : [t.head]), ...t.answers.filter((x) => t.joined.has(x.item.id))].filter((x) => stateOf(x, false).state !== 'closed')
545
+ if (notes.length === 1) return { op: 'fixed', id: notes[0]!.item.id }
546
+ if (notes.length > 1) return { error: `${notes.length} open notes of his in this thread: sk thread fixed <id> "<what was done>" on the one you fixed: ${notes.map((x) => `${x.item.id} "${x.item.title}"`).join('; ')}` }
547
+ const h = t.head
548
+ const st = stateOf(h, false)
549
+ if (st.state === 'claimed' && verdictOf(h)?.status === 'approved-with-fix') return { op: 'done', id: h.item.id }
550
+ if (st.state === 'claimed' || st.state === 'draft') return { op: 'update', id: h.item.id }
551
+ if (st.state === 'asks') return { error: `${h.item.id} waits on the verdict of the owner already: nothing to say fixed` }
552
+ return { error: `${h.item.id} is closed already (${st.why ?? 'closed'}): nothing to say fixed` }
553
+ }
554
+
555
+ /** the views of an item its messages hold, never the version's own (wiki tools/shipkit/threads-store.md, A message's
556
+ * views): an update's media, a question's options, a proposed sound */
557
+ export function messageViewsOf(x: ReviewItem): Set<number> {
558
+ const out = new Set<number>()
559
+ for (const m of x.thread) {
560
+ if (partyOf(m.who) !== 'agent') continue
561
+ if (m.update?.view !== undefined) out.add(m.update.view)
562
+ for (const v of m.ask?.views ?? []) if (v !== null) out.add(v)
563
+ if (m.propose) for (const f of proposeFields(m.propose)) if (f.view !== undefined) out.add(f.view)
564
+ }
565
+ return out
566
+ }
567
+
568
+ /** why an agent may not start a thread or a link on an address, or null: its kind word is one a kind declares
569
+ * (review/kinds.ts), or a thread already names it (a note of his from a page's thread control, an older thread), so a
570
+ * link or a reply to it forks nothing */
571
+ export function addressRefusal(address: string, items: readonly ReviewItem[], spec: Spec = NO_SPEC): string | null {
572
+ const why = addressKindProblem(address, spec)
573
+ if (why === null || threadsNaming(address, items, spec).length > 0) return null
574
+ return why
378
575
  }