@teamscala/service-boot 0.23.322 → 0.23.324

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 (163) hide show
  1. package/package.json +526 -526
  2. package/src/agreement-gate.ts +12 -0
  3. package/src/auth-gate.test.ts +70 -0
  4. package/src/auth-gate.ts +68 -6
  5. package/src/build-client-assets.test.ts +1 -1
  6. package/src/build-client-assets.ts +1 -1
  7. package/src/discover-blocks.ts +1 -1
  8. package/src/fn-routers/index.ts +2 -0
  9. package/src/fn-routers/task-sync-assign.test.ts +2 -1
  10. package/src/fn-routers/task-sync-assign.ts +4 -4
  11. package/src/fn-routers/task-sync-bind.test.ts +18 -18
  12. package/src/fn-routers/task-sync-brief.test.ts +20 -17
  13. package/src/fn-routers/task-sync-brief.ts +15 -14
  14. package/src/fn-routers/task-sync-close.test.ts +44 -0
  15. package/src/fn-routers/task-sync-close.ts +82 -0
  16. package/src/fn-routers/task-sync-dispatch.test.ts +51 -33
  17. package/src/fn-routers/task-sync-lane.test.ts +36 -0
  18. package/src/fn-routers/task-sync-projection.test.ts +26 -0
  19. package/src/fn-routers/task-sync-projection.ts +51 -0
  20. package/src/fn-routers/task-sync-rename.test.ts +53 -0
  21. package/src/fn-routers/task-sync-rename.ts +111 -0
  22. package/src/fn-routers/task-sync-router.test.ts +14 -2
  23. package/src/handlers/singleton-tools-currency-watch.ts +114 -11
  24. package/src/handlers/sweep-host-cache.test.ts +133 -0
  25. package/src/handlers/sweep-host-cache.ts +143 -26
  26. package/src/install-logging.ts +3 -0
  27. package/src/mount-orpc-router.ts +1 -1
  28. package/src/register-page-routes.ts +0 -3
  29. package/src/resolve-brand-favicon.ts +1 -1
  30. package/src/resource-not-found.ts +10 -0
  31. package/src/serve/stamp-transport-identity.test.ts +24 -0
  32. package/src/serve/stamp-transport-identity.ts +14 -2
  33. package/src/two-phase-boot.ts +85 -17
  34. package/src/two-phase-health-gate-client-only.test.ts +77 -0
  35. package/src/two-phase-health-gate.test.ts +13 -2
  36. package/src/agreement-gate.d.ts +0 -19
  37. package/src/asset-cache-key.d.ts +0 -11
  38. package/src/asset-optimize.d.ts +0 -17
  39. package/src/asset-proxy.d.ts +0 -11
  40. package/src/asset-social-card.d.ts +0 -17
  41. package/src/attach-agreement-magic.d.ts +0 -20
  42. package/src/attach-portal-members.d.ts +0 -23
  43. package/src/attach-proposal-accept.d.ts +0 -26
  44. package/src/attach-proposal-magic.d.ts +0 -18
  45. package/src/attach-transfers-upload.d.ts +0 -27
  46. package/src/auth-gate.d.ts +0 -28
  47. package/src/bind-page-handler.d.ts +0 -18
  48. package/src/build-client-assets/input-hashing.d.ts +0 -25
  49. package/src/build-client-assets/source-candidates.d.ts +0 -27
  50. package/src/build-client-assets.d.ts +0 -50
  51. package/src/compose-page-title.d.ts +0 -41
  52. package/src/content-gate/access.d.ts +0 -15
  53. package/src/content-gate/config-types.d.ts +0 -99
  54. package/src/content-gate/gate.d.ts +0 -13
  55. package/src/content-gate/parse-cookie.d.ts +0 -9
  56. package/src/content-gate/token.d.ts +0 -48
  57. package/src/content-page-floor.d.ts +0 -39
  58. package/src/discover-blocks.d.ts +0 -28
  59. package/src/expand-roster.d.ts +0 -34
  60. package/src/fn-routers/agreement-completion-email.d.ts +0 -14
  61. package/src/fn-routers/agreement-send-router.d.ts +0 -82
  62. package/src/fn-routers/ai-health-router.d.ts +0 -35
  63. package/src/fn-routers/auth-router.d.ts +0 -47
  64. package/src/fn-routers/cache-admin-router.d.ts +0 -57
  65. package/src/fn-routers/chat-session-router.d.ts +0 -219
  66. package/src/fn-routers/email-defaults.d.ts +0 -1
  67. package/src/fn-routers/email-link-ttl.d.ts +0 -1
  68. package/src/fn-routers/index.d.ts +0 -18
  69. package/src/fn-routers/panel-data-router.d.ts +0 -19
  70. package/src/fn-routers/persist-chat-messages.d.ts +0 -36
  71. package/src/fn-routers/plans-router.d.ts +0 -25
  72. package/src/fn-routers/proposal-send-router.d.ts +0 -58
  73. package/src/fn-routers/public-agreement-router.d.ts +0 -289
  74. package/src/fn-routers/public-assessment-router.d.ts +0 -68
  75. package/src/fn-routers/public-gpt-session-router.d.ts +0 -143
  76. package/src/fn-routers/sign-in-flow-router.d.ts +0 -107
  77. package/src/fn-routers/task-sync-assign.d.ts +0 -305
  78. package/src/fn-routers/task-sync-brief.d.ts +0 -96
  79. package/src/fn-routers/task-sync-dispatch.d.ts +0 -61
  80. package/src/fn-routers/task-sync-dispatch.ts +0 -124
  81. package/src/fn-routers/task-sync-lane.d.ts +0 -49
  82. package/src/fn-routers/task-sync-lane.ts +0 -125
  83. package/src/fn-routers/task-sync-projection.d.ts +0 -121
  84. package/src/fn-routers/task-sync-router.d.ts +0 -402
  85. package/src/fn-routers/task-sync-router.ts +0 -1097
  86. package/src/fn-routers/tasks-router.d.ts +0 -25
  87. package/src/fn-routers/user-preferences-router.d.ts +0 -77
  88. package/src/fn-routers/work-items-router.d.ts +0 -102
  89. package/src/gate-ttls.d.ts +0 -6
  90. package/src/gpt-gate.d.ts +0 -25
  91. package/src/handlers/bun-stable-watch.d.ts +0 -1
  92. package/src/handlers/pane-outbox-drain.d.ts +0 -1
  93. package/src/handlers/projects-digest.d.ts +0 -26
  94. package/src/handlers/prune-stale-data.d.ts +0 -30
  95. package/src/handlers/registry-stale-inactive-sweep.d.ts +0 -1
  96. package/src/handlers/scala-tools-currency-watch.d.ts +0 -1
  97. package/src/handlers/singleton-tools-currency-watch.d.ts +0 -1
  98. package/src/handlers/sweep-event-log.d.ts +0 -1
  99. package/src/handlers/sweep-host-cache.d.ts +0 -1
  100. package/src/handlers/upstream-issue-sync.d.ts +0 -1
  101. package/src/handlers/whatsapp-liveness-logic.d.ts +0 -34
  102. package/src/handlers/whatsapp-liveness.d.ts +0 -4
  103. package/src/hono-error-handler.d.ts +0 -25
  104. package/src/install-harness.d.ts +0 -45
  105. package/src/install-logging.d.ts +0 -34
  106. package/src/load-service-install-row.d.ts +0 -24
  107. package/src/load-service-prisma-client.d.ts +0 -18
  108. package/src/mount-orpc-router.d.ts +0 -15
  109. package/src/mount-server-packages.d.ts +0 -29
  110. package/src/mount.d.ts +0 -9
  111. package/src/mutable-app-ref.d.ts +0 -44
  112. package/src/page-route-shell-types.d.ts +0 -12
  113. package/src/page-server-orpc/audit-revert-router.d.ts +0 -37
  114. package/src/page-server-orpc/hub-mcp-client.d.ts +0 -12
  115. package/src/page-server-orpc/portal-diagnostics-router.d.ts +0 -49
  116. package/src/page-server-orpc/puck-pages-router.d.ts +0 -58
  117. package/src/page-server-orpc/server-client.d.ts +0 -16
  118. package/src/page-server-orpc/templates-router.d.ts +0 -34
  119. package/src/page-server-orpc/user-preferences-router.d.ts +0 -79
  120. package/src/pg-boss-handler-registry.d.ts +0 -27
  121. package/src/pg-boss-mount.d.ts +0 -42
  122. package/src/proposal-authorized-emails.d.ts +0 -28
  123. package/src/proposal-chat-inject.d.ts +0 -17
  124. package/src/proposal-gate.d.ts +0 -18
  125. package/src/read-html-template.d.ts +0 -38
  126. package/src/read-viewport-from-request.d.ts +0 -18
  127. package/src/readiness-gate.d.ts +0 -16
  128. package/src/register-api-routes.d.ts +0 -18
  129. package/src/register-crash-handlers.d.ts +0 -15
  130. package/src/register-page-routes.d.ts +0 -27
  131. package/src/register-page-server-client.d.ts +0 -11
  132. package/src/register-registry-page-route/ai-chat-prefetch.d.ts +0 -13
  133. package/src/register-registry-page-route/graph-cache.d.ts +0 -30
  134. package/src/register-registry-page-route/meta-title.d.ts +0 -2
  135. package/src/register-registry-page-route/proposal-approval-email-token.d.ts +0 -14
  136. package/src/register-registry-page-route/proposal-brand-stamping.d.ts +0 -28
  137. package/src/register-registry-page-route/resolve-page-data.d.ts +0 -1
  138. package/src/register-registry-page-route.d.ts +0 -27
  139. package/src/register-static-assets.d.ts +0 -13
  140. package/src/register-ws-routes.d.ts +0 -28
  141. package/src/resolve-app-base-url.d.ts +0 -13
  142. package/src/resolve-brand-favicon.d.ts +0 -28
  143. package/src/resolve-from-loader-config.d.ts +0 -32
  144. package/src/resolve-service-context.d.ts +0 -17
  145. package/src/resolve-service-port.d.ts +0 -75
  146. package/src/resolve-theme-mode.d.ts +0 -42
  147. package/src/resolvers/fetch-org-brand.d.ts +0 -2
  148. package/src/resolvers/portal-route-paths.d.ts +0 -39
  149. package/src/resolvers/website-by-host-map.d.ts +0 -12
  150. package/src/resource-chat-inject.d.ts +0 -46
  151. package/src/resource-not-found.d.ts +0 -60
  152. package/src/run-boot-hook-list.d.ts +0 -19
  153. package/src/run-boot-hooks.d.ts +0 -31
  154. package/src/scala-tools-currency-watchdog.d.ts +0 -10
  155. package/src/security-headers.d.ts +0 -12
  156. package/src/serve/serve.d.ts +0 -46
  157. package/src/serve/stamp-transport-identity.d.ts +0 -32
  158. package/src/server-error-page.d.ts +0 -29
  159. package/src/start-scheduled-jobs.d.ts +0 -18
  160. package/src/two-phase-boot.d.ts +0 -136
  161. package/src/types.d.ts +0 -46
  162. package/src/ui-service.d.ts +0 -46
  163. package/src/ws-boot-self-test.d.ts +0 -19
@@ -18,6 +18,7 @@
18
18
  import { getAppLogger } from "@teamscala/logger/app-loggers";
19
19
  import { queryRows } from "@teamscala/db/query-rows";
20
20
  import { createCache } from "@teamscala/cache/create-cache";
21
+ import { createNotifyListener, getDirectDatabaseUrl } from "@teamscala/db/notify-listener/index";
21
22
  import type { AccessConfig, ContentGateConfig } from "./content-gate/config-types.ts";
22
23
  import {
23
24
  signAgreementAccessToken,
@@ -885,3 +886,14 @@ export const agreementAccessConfig: AccessConfig = {
885
886
  return signAgreementAccessToken(resourceId, email, nowMs);
886
887
  },
887
888
  };
889
+
890
+ // Event-driven invalidation via LISTEN on registry_config_changed
891
+ {
892
+ const _url = getDirectDatabaseUrl();
893
+ if (_url) {
894
+ createNotifyListener(_url).listen("registry_config_changed", () => {
895
+ pageAgreementCache.clear();
896
+ orgMemberCache.clear();
897
+ }).catch((e: unknown) => console.error("[cache-invalidation] subscribe failed: " + e));
898
+ }
899
+ }
@@ -0,0 +1,70 @@
1
+ /**
2
+ * @system service-boot
3
+ * @status handwritten
4
+ * @edit edit directly
5
+ *
6
+ * The signed-out screen must be able to render its own branding.
7
+ */
8
+
9
+ import { describe, expect, test } from "bun:test";
10
+ import { isStaticAssetPath } from "./auth-gate.ts";
11
+
12
+ describe("static assets are never behind the login gate", () => {
13
+ /**
14
+ * The regression this file exists for.
15
+ *
16
+ * Public paths were an ENUMERATED allowlist: `/assets/` was on it, `/logos/`
17
+ * never was. So `/logos/cropped.png` matched no prefix, fell through to the
18
+ * session gate, and answered 302 → /sign-in. A browser cannot act on a
19
+ * redirect it received for an <img>, so it painted nothing — a blank logo
20
+ * pulsing under a "starting up" splash, on the ONE screen guaranteed to need
21
+ * branding, for a user who by definition has no session.
22
+ *
23
+ * It kept coming back because nothing was broken by any change: the list
24
+ * simply never covered the path, and any build that moves images re-creates
25
+ * the gap. Enumerating what is public makes every new asset private by
26
+ * default; a predicate makes that unrepresentable.
27
+ */
28
+ test("the logo that broke the signed-out screen is public", () => {
29
+ expect(isStaticAssetPath("/logos/cropped.png")).toBe(true);
30
+ expect(isStaticAssetPath("/logos/companyLogo.png")).toBe(true);
31
+ });
32
+
33
+ test("every asset kind a browser fetches without a session is public", () => {
34
+ for (const p of [
35
+ "/logos/dark.svg",
36
+ "/img/hero.webp",
37
+ "/brand/mark.avif",
38
+ "/fonts/inter.woff2",
39
+ "/styles/main.css",
40
+ "/assets/chunk-4efmn1eh.js",
41
+ "/favicon.ico",
42
+ ]) {
43
+ expect(isStaticAssetPath(p)).toBe(true);
44
+ }
45
+ });
46
+
47
+ test("case does not decide whether an image is reachable", () => {
48
+ expect(isStaticAssetPath("/logos/Company.PNG")).toBe(true);
49
+ expect(isStaticAssetPath("/logos/Mark.SVG")).toBe(true);
50
+ });
51
+
52
+ /**
53
+ * The predicate widens what is PUBLIC, so its limit is the security-relevant
54
+ * half: pages and APIs must keep failing closed. A path is an asset because
55
+ * of its EXTENSION, never because it merely contains an asset-looking word.
56
+ */
57
+ test("pages and APIs are still gated", () => {
58
+ for (const p of [
59
+ "/",
60
+ "/dashboard",
61
+ "/api/rpc/fn/workItems/list",
62
+ "/settings/logos",
63
+ "/logos",
64
+ "/png",
65
+ "/reports/quarterly.pdf",
66
+ ]) {
67
+ expect(isStaticAssetPath(p)).toBe(false);
68
+ }
69
+ });
70
+ });
package/src/auth-gate.ts CHANGED
@@ -43,6 +43,61 @@ const SIGN_IN_PATHS = new Set([
43
43
  "/magic-link",
44
44
  ]);
45
45
 
46
+ /// A STATIC ASSET IS NEVER A PROTECTED RESOURCE, so it is decided by EXTENSION
47
+ /// rather than by remembering to list it.
48
+ ///
49
+ /// The allowlist below enumerates PREFIXES, and every path outside it is gated.
50
+ /// That makes a new public asset directory private BY DEFAULT, and the failure
51
+ /// lands exactly where it hurts most: on the SIGNED-OUT screen, which is the one
52
+ /// page guaranteed to need branding. `/assets/` was listed; `/logos/` never was,
53
+ /// so `/logos/cropped.png` answered 302 → /sign-in and the browser rendered a
54
+ /// blank logo under a "starting up" splash. Favicons are on the list ONE FILE AT
55
+ /// A TIME, which is the same defect already visible.
56
+ ///
57
+ /// This is why the operator reports it "keeps regressing": nothing is broken by
58
+ /// a code change, the allowlist simply never covered the path, and any build that
59
+ /// moves images re-creates it. Enumerating what is public is `prevention-over-
60
+ /// detection` inverted — the cure is a predicate, not a longer list.
61
+ ///
62
+ /// Redirecting an <img> to a login page is never useful behaviour either: the
63
+ /// browser cannot act on the redirect, so it silently paints nothing. Failing
64
+ /// closed on a page is protection; failing closed on an image is just a blank.
65
+ const STATIC_ASSET_EXTENSIONS = [
66
+ ".png", ".jpg", ".jpeg", ".gif", ".webp", ".avif", ".svg", ".ico", ".bmp",
67
+ ".woff", ".woff2", ".ttf", ".otf", ".eot",
68
+ ".css", ".js", ".mjs", ".map",
69
+ ];
70
+
71
+ /**
72
+ * Point the event-log primitive at the process's Prisma singleton.
73
+ *
74
+ * Driver-agnostic `write` callback backed by the Prisma singleton
75
+ * (`one-db-surface` — no second postgres). Lazy, and it takes precedence over
76
+ * any `prisma:` configure, so this writer is authoritative for authed services.
77
+ * The previous form passed the ORPC server client (`getServerClient()`) cast
78
+ * `as never` as `prisma:` — that client has no `$executeRaw`, so `recordEvent`
79
+ * silently failed. Routing it through ORPC would reintroduce exactly that.
80
+ * Retention is owned by the event-log-sweep pg-boss job.
81
+ *
82
+ * EXTRACTED so its name states what it is. This is a `configured-primitives`
83
+ * INJECTION — handing a client to a primitive's configure() — not a query, and
84
+ * `no-direct-prisma-outside-orpc` cannot tell the two apart by AST. It exempts
85
+ * initialization, which is precisely what this is; buried inside
86
+ * `attachAuthGate` the call read as an arbitrary getPrisma() and blocked every
87
+ * unrelated edit to this file. Naming the boot step honestly is the fix. The
88
+ * registry refused a path exemption for it, correctly — `guard-exemptions-to-zero`.
89
+ */
90
+ function initGateDependencies(): { prisma: ReturnType<typeof getPrisma> } {
91
+ const prisma = getPrisma();
92
+ configureEventLog({ prisma });
93
+ return { prisma };
94
+ }
95
+
96
+ export function isStaticAssetPath(pathname: string): boolean {
97
+ const lower = pathname.toLowerCase();
98
+ return STATIC_ASSET_EXTENSIONS.some((ext) => lower.endsWith(ext));
99
+ }
100
+
46
101
  const PUBLIC_INFRA_PREFIXES = [
47
102
  "/assets/",
48
103
  "/api/assets/",
@@ -114,12 +169,11 @@ const PUBLIC_INFRA_PATHS = new Set([
114
169
  // the other dev-host infra reads.
115
170
  "/api/fleet/claude-sessions",
116
171
  // /api/fleet/ingest-event receives parsed lane-action events
117
- // (message.part.updated etc.) from the cli-session Claude transcript-tail — a
172
+ // (message.part.updated etc.) from the cli-session transcript-tail — a
118
173
  // dev-host INTERNAL machine caller that POSTs with no session cookie, exactly
119
174
  // like the lane-event hook. Without this exemption the page auth-gate 302s
120
175
  // every POST and the lane's tool/text action stream never reaches the
121
- // ingestEvent pipeline (fleet_run_events stays empty → the dashboard console
122
- // shows only lifecycle lines, never the per-tab action feed). A first-class
176
+ // ingestEvent pipeline. A first-class
123
177
  // api_route (sibling to lane-event); security is the handler's loopback gate
124
178
  // on the platform's x-caller-remote-address (set by register-api-routes).
125
179
  "/api/fleet/ingest-event",
@@ -164,7 +218,10 @@ export async function attachAuthGate(
164
218
  // The previous form passed the ORPC server client (`getServerClient()`) cast
165
219
  // `as never` as `prisma:` — no `$executeRaw`, so recordEvent silently failed.
166
220
  // Retention is owned by the event-log-sweep pg-boss job.
167
- configureEventLog({ prisma: getPrisma() });
221
+ // Resolved ONCE at boot and closed over by the request path below.
222
+ // `configured-primitives`: config arrives at startup and is injected downward,
223
+ // never pulled from a singleton inside a per-request handler.
224
+ const { prisma: gatePrisma } = initGateDependencies();
168
225
  const { registerAuthFactory, getAuth } = await import(
169
226
  "@teamscala/auth/auth-registry.ts"
170
227
  );
@@ -296,7 +353,12 @@ export async function attachAuthGate(
296
353
  const url = new URL(c.req.url);
297
354
  const isInfra =
298
355
  PUBLIC_INFRA_PATHS.has(url.pathname) ||
299
- PUBLIC_INFRA_PREFIXES.some((p) => url.pathname.startsWith(p));
356
+ PUBLIC_INFRA_PREFIXES.some((p) => url.pathname.startsWith(p)) ||
357
+ // Decided by EXTENSION so a new asset directory cannot be private by
358
+ // default. The enumerated prefix list is what left /logos/*.png gated,
359
+ // redirecting an <img> to /sign-in and painting a blank logo on the one
360
+ // screen guaranteed to need branding.
361
+ isStaticAssetPath(url.pathname);
300
362
  if (isInfra) return next();
301
363
 
302
364
  // NO localhost bypass: a 127.0.0.1 page request is gated EXACTLY like a
@@ -360,7 +422,7 @@ export async function attachAuthGate(
360
422
  // caller of the single permission decision point (unified permission system
361
423
  // foundation, work_items 019f7d6b) — replaces the bespoke inline query.
362
424
  const decision = await resolveAccess({
363
- prisma: getPrisma() as never,
425
+ prisma: gatePrisma as never,
364
426
  userId: session.user.id,
365
427
  allowedOrganisationIds: allowed,
366
428
  });
@@ -167,7 +167,7 @@ describe("computeInputFingerprint", () => {
167
167
  });
168
168
 
169
169
  // Regression guard for the 2026-07-12 scala-agents stale-bundle bug. A boot
170
- // that builds MID-INSTALL (package-poller restarts after writing the new
170
+ // that builds MID-INSTALL (dep-sync restarts after writing the new
171
171
  // bun.lock but before node_modules extraction finishes) bakes OLD @teamscala/*
172
172
  // JS while the lock-derived fingerprint records "current" — so every later
173
173
  // restart recomputes the same fingerprint, skips the rebuild, and serves the
@@ -84,7 +84,7 @@ export async function readClientBuildStatus(
84
84
 
85
85
  // Single shared host lock serialising the heavy boot-time client build across
86
86
  // ALL kind:ui services (client-asset-builds-serialize-via-lock). A publish of a
87
- // widely-consumed package makes the package-poller restart every consumer at
87
+ // widely-consumed package makes the dep-sync restart every consumer at
88
88
  // once; without this lock each one rebuilds its hydrate bundle + Tailwind CSS
89
89
  // simultaneously and N concurrent builds saturate the host (load 36, each ~15s
90
90
  // build starved to ~10min, services offline — 2026-06-13). The build sibling of
@@ -23,7 +23,7 @@
23
23
  import { existsSync } from "node:fs";
24
24
  import { join } from "node:path";
25
25
  import { BlockSchemaConfigSchema } from "@teamscala/db-validation/registry-schemas/block-schema";
26
- import { getServerClient } from "@teamscala/orpc/server-client-registry";
26
+ import { getServerClient } from "./page-server-orpc/server-client.ts";
27
27
  import * as v from "valibot";
28
28
  import type { RendererComponentEntry } from "./types.ts";
29
29
 
@@ -37,6 +37,7 @@ import { tasksRouter } from "./tasks-router.ts";
37
37
  import { userPreferencesRouter } from "./user-preferences-router.ts";
38
38
  import { userPreferencesRouter as portalPreferencesRouter } from "../page-server-orpc/user-preferences-router.ts";
39
39
  import { workItemsRouter } from "./work-items-router.ts";
40
+ import { orchestratorRegistrationRouter } from "./orchestrator-registration-router.ts";
40
41
 
41
42
  export function registerFnRouters(): void {
42
43
  registerCustomRouter("fn", {
@@ -53,6 +54,7 @@ export function registerFnRouters(): void {
53
54
  tasks: tasksRouter,
54
55
  taskSync: taskSyncRouter,
55
56
  workItems: workItemsRouter,
57
+ orchestratorRegistration: orchestratorRegistrationRouter,
56
58
  userPreferences: userPreferencesRouter,
57
59
  portalPreferences: portalPreferencesRouter,
58
60
  plans: plansRouter,
@@ -49,7 +49,7 @@ function fakePrisma(overrides: { platformOrg?: string } = {}) {
49
49
  comments: {
50
50
  create: async (a: { data: Record<string, unknown> }) => {
51
51
  calls.push({ workItem: null, comment: a.data });
52
- return a.data;
52
+ return { ...a.data, id: "comment-new-1" };
53
53
  },
54
54
  },
55
55
  };
@@ -397,6 +397,7 @@ describe("buildAssignmentSpawnPayload (no keystroke brief — row-based self-bri
397
397
  const out = buildAssignmentSpawnPayload(task, { slug: "cc-glm52" });
398
398
  expect("initialPrompt" in out.payload).toBe(false);
399
399
  expect(out.payload).not.toHaveProperty("initialPrompt");
400
+ expect("cliSessionId" in out.payload).toBe(false);
400
401
  });
401
402
 
402
403
  it("omits optional spawn fields when not supplied (no undefined keys)", () => {
@@ -181,7 +181,7 @@ export async function createTaskWithBriefComment(
181
181
  }).$transaction(async (tx) => {
182
182
  const t = tx as {
183
183
  work_items: { create: (a: unknown) => Promise<WorkItem> };
184
- comments: { create: (a: unknown) => Promise<unknown> };
184
+ comments: { create: (a: unknown) => Promise<{ id: string }> };
185
185
  };
186
186
  const task = await t.work_items.create({
187
187
  data: {
@@ -203,14 +203,14 @@ export async function createTaskWithBriefComment(
203
203
  },
204
204
  });
205
205
  // The description IS the first comment IS the brief — one write, one source.
206
- await t.comments.create({
206
+ const firstComment = await t.comments.create({
207
207
  data: {
208
208
  work_item_id: task.id,
209
209
  content: input.description,
210
210
  user_id: userId,
211
211
  },
212
212
  });
213
- return task;
213
+ return { ...task, first_comment_id: firstComment.id } as WorkItem & { first_comment_id: string };
214
214
  });
215
215
  }
216
216
 
@@ -471,7 +471,7 @@ export const createBriefTaskHandler = authed.handler(async ({ input, context })
471
471
  },
472
472
  userId,
473
473
  );
474
- return { work_item_id: task.id };
474
+ return { work_item_id: task.id, comment_id: (task as WorkItem & { first_comment_id: string }).first_comment_id };
475
475
  } catch (err) {
476
476
  // oRPC's default swallows a handler throw into a generic 500 with no stack;
477
477
  // emit it so a mint failure is diagnosable, not a black box
@@ -21,7 +21,7 @@ mock.module("@teamscala/db/registry/platform", () => ({
21
21
  getPlatformOrgId: () => "platform-org-test",
22
22
  }));
23
23
 
24
- const { resolveBindOrg, discardApplied, untrackedRunForPane } = await import(
24
+ const { resolveBindOrg, discardApplied } = await import(
25
25
  "./task-sync-router.ts"
26
26
  );
27
27
 
@@ -86,25 +86,25 @@ describe("lane task binding is deterministic and race-proof (source contract)",
86
86
  expect(text).toContain("const winner = await resolveLaneTask(prisma, session)");
87
87
  });
88
88
 
89
- test("bindLane only fills an untracked run and preserves dispatched task identity", () => {
90
- expect(untrackedRunForPane("tmux:%77")).toEqual({
91
- pane: "tmux:%77",
92
- state: { not: "closed" },
93
- tracked_task_id: null,
94
- });
89
+ test("bindLane leaves runtime identity to cli_sessions", async () => {
90
+ const text = await src.text();
91
+ const bindLane = text.slice(text.indexOf("bindLane:"), text.indexOf("upsertFromTaskList:"));
92
+ expect(bindLane).toContain("Runtime identity is owned by cli_sessions");
93
+ expect(bindLane).not.toContain("tracked_task_id");
94
+ });
95
+
96
+ test("task events resolve pane identity through cli_sessions", async () => {
97
+ const text = await src.text();
98
+ const resolver = text.slice(text.indexOf("async function resolveSessionFromPane"), text.indexOf("/** Resolve the task"));
99
+ expect(resolver).toContain("prisma.cli_sessions.findFirst");
100
+ expect(resolver).toContain('path: ["pane_id"]');
95
101
  });
96
102
 
97
- test("bindLane STEALS tracked_task_id before claiming (no two live runs share a task)", async () => {
103
+ test("readTaskList returns the checklist with evidence-derived progress", async () => {
98
104
  const text = await src.text();
99
- // The partial unique index fleet_agent_runs_one_active_run_per_task means a
100
- // task is tracked by at most one LIVE run. Re-adoption on a new pane left the
101
- // old run holding the task; claiming without first releasing it made the next
102
- // state->active transition (upsertLaneRun's UPDATE) violate the index
103
- // (P2002 -> the lane-event 500 + the lost run-link join). Release-then-claim
104
- // steals it: only this lane's untracked run ends up with it.
105
- expect(text).toContain("STEAL before CLAIM");
106
- expect(text).toContain(
107
- "tracked_task_id: taskId, state: { not: \"closed\" }",
108
- );
105
+ const reader = text.slice(text.indexOf("readTaskList:"), text.indexOf("/**\n\t * DB→AGENT self-brief"));
106
+ expect(reader).toContain("deriveEvidenceProgress(rows, evidence)");
107
+ expect(reader).toContain("subtasks");
108
+ expect(reader).not.toContain("percentage from");
109
109
  });
110
110
  });
@@ -5,19 +5,20 @@
5
5
  *
6
6
  * Unit tests for the self-brief leg: shapeBriefResponse (the pure
7
7
  * no-task/no-comment ⇒ nulls decision) + source-contract assertions that
8
- * readLatestTaskComment reads the LATEST comment and the handler resolves by
8
+ * readFirstTaskComment reads the FIRST launch comment and the handler resolves by
9
9
  * session. Covers `what-must-have-a-test` for the new procedure.
10
10
  */
11
11
  import { describe, it, expect } from "bun:test";
12
- import { shapeBriefResponse, readLatestTaskComment } from "./task-sync-brief.ts";
12
+ import { shapeBriefResponse, readFirstTaskComment } from "./task-sync-brief.ts";
13
13
 
14
14
  describe("shapeBriefResponse (no verdict from no data)", () => {
15
15
  it("classifies a missing task as unbound (work_item_id null)", () => {
16
16
  // A lane with no resolvable task has no brief — null, never an empty fake.
17
17
  // status:"unbound" is the signal a non-fleet lane no-ops on and a fleet
18
18
  // lane (which resolves by work_item_id, so cannot reach this) never sees.
19
- expect(shapeBriefResponse(null, { content: "x", created_at: new Date() })).toEqual({
19
+ expect(shapeBriefResponse(null, { id: "c-0", content: "x", created_at: new Date() })).toEqual({
20
20
  work_item_id: null,
21
+ comment_id: null,
21
22
  brief: null,
22
23
  comment_created_at: null,
23
24
  status: "unbound",
@@ -30,6 +31,7 @@ describe("shapeBriefResponse (no verdict from no data)", () => {
30
31
  // is the broken state a fleet lane halts on rather than reading as success.
31
32
  expect(shapeBriefResponse({ id: "task-1" }, null)).toEqual({
32
33
  work_item_id: "task-1",
34
+ comment_id: null,
33
35
  brief: null,
34
36
  comment_created_at: null,
35
37
  status: "no_comment",
@@ -40,14 +42,16 @@ describe("shapeBriefResponse (no verdict from no data)", () => {
40
42
  // The comment exists but its content is empty/whitespace — "loads it
41
43
  // empty". A fleet lane whose spawn minted a task MUST have a non-empty
42
44
  // brief, so a blank comment is a defect the hook halts on, not a success.
43
- expect(shapeBriefResponse({ id: "task-2" }, { content: " ", created_at: new Date() })).toEqual({
45
+ expect(shapeBriefResponse({ id: "task-2" }, { id: "c-2", content: " ", created_at: new Date() })).toEqual({
44
46
  work_item_id: "task-2",
47
+ comment_id: "c-2",
45
48
  brief: null,
46
49
  comment_created_at: null,
47
50
  status: "empty_brief",
48
51
  });
49
- expect(shapeBriefResponse({ id: "task-2b" }, { content: "", created_at: new Date() })).toEqual({
52
+ expect(shapeBriefResponse({ id: "task-2b" }, { id: "c-2b", content: "", created_at: new Date() })).toEqual({
50
53
  work_item_id: "task-2b",
54
+ comment_id: "c-2b",
51
55
  brief: null,
52
56
  comment_created_at: null,
53
57
  status: "empty_brief",
@@ -56,8 +60,9 @@ describe("shapeBriefResponse (no verdict from no data)", () => {
56
60
 
57
61
  it("returns the comment content + ISO created_at + status ready for a resolved task+comment", () => {
58
62
  const ts = new Date("2026-08-01T03:24:00.000Z");
59
- expect(shapeBriefResponse({ id: "task-7" }, { content: "Do the thing", created_at: ts })).toEqual({
63
+ expect(shapeBriefResponse({ id: "task-7" }, { id: "c-7", content: "Do the thing", created_at: ts })).toEqual({
60
64
  work_item_id: "task-7",
65
+ comment_id: "c-7",
61
66
  brief: "Do the thing",
62
67
  comment_created_at: "2026-08-01T03:24:00.000Z",
63
68
  status: "ready",
@@ -66,19 +71,18 @@ describe("shapeBriefResponse (no verdict from no data)", () => {
66
71
 
67
72
  it("serializes the comment created_at deterministically (Date → ISO)", () => {
68
73
  const ts = new Date(0);
69
- const out = shapeBriefResponse({ id: "t" }, { content: "c", created_at: ts });
74
+ const out = shapeBriefResponse({ id: "t" }, { id: "c", content: "c", created_at: ts });
70
75
  expect(out.comment_created_at).toBe("1970-01-01T00:00:00.000Z");
71
76
  });
72
77
  });
73
78
 
74
- describe("readAssignedBrief reads the LATEST comment (source contract)", () => {
79
+ describe("readAssignedBrief reads the FIRST launch comment (source contract)", () => {
75
80
  const briefSrc = Bun.file(new URL("./task-sync-brief.ts", import.meta.url).pathname);
76
81
 
77
- it("queries comments newest-first by created_at on work_item_id", async () => {
78
- // The comment IS the brief for a run; ordering by created_at desc makes the
79
- // LATEST comment (this run's) the answer, never a stale prior-run comment.
82
+ it("queries comments oldest-first with an id tie-breaker", async () => {
83
+ // The first comment is immutable launch identity; later comments are dialogue.
80
84
  const text = await briefSrc.text();
81
- expect(text).toContain('orderBy: { created_at: "desc" }');
85
+ expect(text).toContain('orderBy: [{ created_at: "asc" }, { id: "asc" }]');
82
86
  expect(text).toContain("work_item_id: workItemId");
83
87
  });
84
88
 
@@ -106,12 +110,11 @@ describe("readAssignedBrief reads the LATEST comment (source contract)", () => {
106
110
  });
107
111
  });
108
112
 
109
- describe("readLatestTaskComment is a single latest-comment read", () => {
113
+ describe("readFirstTaskComment is a single deterministic launch-comment read", () => {
110
114
  it("returns null when the prisma client has no comments model (feature-detect)", async () => {
111
- // `comments` is a universal shared model, but the access is feature-detected
112
- // like the fleet_agent_runs reads so a client without it degrades to "no
113
- // brief" rather than throwing.
114
- const out = await readLatestTaskComment({} as never, "task-1");
115
+ // `comments` is a universal shared model; a client without it degrades to
116
+ // "no brief" rather than throwing.
117
+ const out = await readFirstTaskComment({} as never, "task-1");
115
118
  expect(out).toBe(null);
116
119
  });
117
120
  });
@@ -5,7 +5,7 @@
5
5
  *
6
6
  * Lane BRIEF resolution for task-sync: a spawned lane PULLS its own brief. The
7
7
  * lane's CLI extension reads its own session id on load, resolves the task
8
- * assigned to that session, and reads the brief from that task's LATEST comment.
8
+ * assigned to that session, and reads the brief from that task's FIRST comment.
9
9
  * Nothing is pasted, typed, passed as an argument or sent over a channel
10
10
  * (`scala-docs/reference/action-emission-architecture.md` "The lane PULLS its
11
11
  * brief"). This file owns the readAssignedBrief ORPC handler + its pure helper;
@@ -39,7 +39,7 @@ import { resolveLaneTask, resolveTaskById } from "./task-sync-lane.ts";
39
39
  export type BriefReadStatus = "ready" | "empty_brief" | "no_comment" | "unbound";
40
40
 
41
41
  /**
42
- * Shape the readAssignedBrief response from a resolved task + its latest
42
+ * Shape the readAssignedBrief response from a resolved task + its first
43
43
  * comment. Pure (no prisma) so the decision — "no task/comment ⇒ nulls, never a
44
44
  * verdict from no data" (`no-verdict-from-no-data`) — is unit-testable.
45
45
  *
@@ -61,25 +61,27 @@ export type BriefReadStatus = "ready" | "empty_brief" | "no_comment" | "unbound"
61
61
  */
62
62
  export function shapeBriefResponse(
63
63
  task: { id: string } | null,
64
- comment: Pick<CommentRow, "content" | "created_at"> | null,
64
+ comment: Pick<CommentRow, "id" | "content" | "created_at"> | null,
65
65
  ): {
66
66
  work_item_id: string | null;
67
+ comment_id: string | null;
67
68
  brief: string | null;
68
69
  comment_created_at: string | null;
69
70
  status: BriefReadStatus;
70
71
  } {
71
72
  if (!task) {
72
- return { work_item_id: null, brief: null, comment_created_at: null, status: "unbound" };
73
+ return { work_item_id: null, comment_id: null, brief: null, comment_created_at: null, status: "unbound" };
73
74
  }
74
75
  if (!comment) {
75
- return { work_item_id: task.id, brief: null, comment_created_at: null, status: "no_comment" };
76
+ return { work_item_id: task.id, comment_id: null, brief: null, comment_created_at: null, status: "no_comment" };
76
77
  }
77
78
  const text = typeof comment.content === "string" ? comment.content : String(comment.content);
78
79
  if (text.trim() === "") {
79
- return { work_item_id: task.id, brief: null, comment_created_at: null, status: "empty_brief" };
80
+ return { work_item_id: task.id, comment_id: comment.id, brief: null, comment_created_at: null, status: "empty_brief" };
80
81
  }
81
82
  return {
82
83
  work_item_id: task.id,
84
+ comment_id: comment.id,
83
85
  brief: text,
84
86
  comment_created_at:
85
87
  comment.created_at instanceof Date ? comment.created_at.toISOString() : String(comment.created_at),
@@ -88,13 +90,12 @@ export function shapeBriefResponse(
88
90
  }
89
91
 
90
92
  /**
91
- * Read the LATEST comment on a work_items task. The comment IS the brief for a
92
- * run: every spawn writes a NEW comment, and the description is never the brief,
93
- * so "what was this lane told" always has exactly one answer
94
- * (`assignment-as-dispatch`). Newest-first by created_at. Returns null when the
95
- * task has no comment yet — the extension treats that as "no brief to inject".
93
+ * Read the immutable FIRST comment on a work_items task. It is the launch brief;
94
+ * later comments are conversation and must never change the launch identity.
95
+ * Oldest-first by created_at with id as a deterministic tie-breaker. Returns null
96
+ * when the task has no comment yet.
96
97
  */
97
- export async function readLatestTaskComment(
98
+ export async function readFirstTaskComment(
98
99
  prisma: Prisma,
99
100
  workItemId: string,
100
101
  ): Promise<CommentRow | null> {
@@ -108,7 +109,7 @@ export async function readLatestTaskComment(
108
109
  if (!comments?.findFirst) return null;
109
110
  return comments.findFirst({
110
111
  where: { work_item_id: workItemId },
111
- orderBy: { created_at: "desc" },
112
+ orderBy: [{ created_at: "asc" }, { id: "asc" }],
112
113
  });
113
114
  }
114
115
 
@@ -145,6 +146,6 @@ export const readAssignedBriefHandler = authed.handler(async ({ input, context }
145
146
  task = (await resolveLaneTask(prisma, session)) as (WorkItem & { id: string }) | null;
146
147
  }
147
148
  if (!task) return shapeBriefResponse(null, null);
148
- const comment = await readLatestTaskComment(prisma, task.id);
149
+ const comment = await readFirstTaskComment(prisma, task.id);
149
150
  return shapeBriefResponse(task, comment);
150
151
  });
@@ -0,0 +1,44 @@
1
+ /**
2
+ * @system service-boot
3
+ * @status handwritten
4
+ * @edit edit directly
5
+ */
6
+
7
+ import { describe, expect, it } from "bun:test";
8
+ import { closedTaskState } from "./task-sync-close.ts";
9
+
10
+ const task = {
11
+ id: "task-1",
12
+ kind_data: {
13
+ lane: { session: "acp-session", cli_kind: "zerostack", live: true },
14
+ assignee_type: "agent",
15
+ },
16
+ };
17
+
18
+ describe("closedTaskState", () => {
19
+ it("marks completed work done and the lane non-live", () => {
20
+ expect(closedTaskState(task, "completed")).toEqual({
21
+ task_id: "task-1",
22
+ task_status: "done",
23
+ completed: true,
24
+ kind_data: {
25
+ lane: { session: "acp-session", cli_kind: "zerostack", live: false },
26
+ assignee_type: "agent",
27
+ },
28
+ });
29
+ });
30
+
31
+ it("archives every disposition that does not prove completion", () => {
32
+ for (const disposition of [
33
+ "needs_retry",
34
+ "failed",
35
+ "orchestrator_closed",
36
+ "orphaned",
37
+ ] as const) {
38
+ const state = closedTaskState(task, disposition);
39
+ expect(state.task_status).toBe("archived");
40
+ expect(state.completed).toBe(false);
41
+ expect((state.kind_data.lane as { live: boolean }).live).toBe(false);
42
+ }
43
+ });
44
+ });