@@ -16,30 +16,13 @@ import {
1616} from "~/services/dashboardAgentWatchToken.server" ;
1717
1818/**
19- * `POST /api/v1/dashboard-agent/watches/:watchId/check` — private per-watch check.
20- * The watcher task calls it once per tick with the watch's token.
21- *
22- * Security model: the token only names a watch; the row is the authority on
23- * lifecycle and on the immutable project/environment/user snapshot; the user is
24- * re-authorized against that snapshot on every call, and a revoked user gets the
25- * watch cancelled here before any environment data is read.
26- *
27- * The route never transitions the watch to fired/expired and never advances the
28- * tick counter. The watcher task owns both, so exactly one component decides when
29- * the user is told and nothing here can fork the tick chain.
30- *
31- * It also hands a watch over to its group's batch chain: it makes sure a chain is
32- * running for the watch's (environment, cadence) group and answers
33- * `batched: true` when one is, at which point the caller stops rescheduling its
34- * own per-watch chain.
19+ * Private per-watch check. The token only names a watch; the row is the authority on
20+ * lifecycle and its snapshot, and this route transitions nothing and advances no tick.
3521 */
3622
3723const ParamsSchema = z . object ( { watchId : z . string ( ) . min ( 1 ) } ) ;
3824
39- /**
40- * Best-effort: the verdict is what this route owes its caller, so a chain that
41- * couldn't be armed returns `false` and the next check tries again.
42- */
25+ /** Best-effort: a chain that couldn't be armed returns `false` and is retried next check. */
4326async function ensureBatchChain ( watch : {
4427 id : string ;
4528 environmentId : string ;
@@ -91,8 +74,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
9174 ) ;
9275 }
9376
94- // A valid token for a different watch is 403, not 401: the caller is
95- // authenticated, just not for this resource.
77+ // A valid token for a different watch is 403, not 401.
9678 if ( claims . watchId !== watchId ) {
9779 return json ( { error : "Not allowed for this watch" , code : "watch_mismatch" } , { status : 403 } ) ;
9880 }
@@ -129,8 +111,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
129111 const now = new Date ( ) ;
130112 const expired = watch . expiresAt . getTime ( ) <= now . getTime ( ) ;
131113 if ( expired ) {
132- // Past the deadline only the final evaluation is allowed, and only inside the
133- // grace window the token is valid for.
114+ // Past the deadline only the final evaluation is allowed, inside the token's grace.
134115 const graceEnds = watch . expiresAt . getTime ( ) + WATCH_TOKEN_GRACE_MS ;
135116 if ( body . final !== true || now . getTime ( ) > graceEnds ) {
136117 return json (
@@ -140,11 +121,8 @@ export async function action({ request, params }: ActionFunctionArgs) {
140121 }
141122 }
142123
143- // Everything below reads or writes a tenant's data, so failures are logged with
144- // whose tick failed, then rethrown unchanged.
145124 try {
146- // Re-authorize the initiating user before any environment data is read, so a
147- // revoked user's tick can't observe anything.
125+ // Re-authorize the initiating user before any environment data is read.
148126 const authorization = await authorizeWatchEnvironment ( {
149127 userId : watch . userId ,
150128 organizationId : watch . organizationId ,
@@ -153,8 +131,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
153131 } ) ;
154132
155133 if ( ! authorization . ok ) {
156- // The watch must not survive the access it was created with. Cancellation is
157- // never notified, so `deliveryStatus` stays `not_required`.
134+ // A watch must not outlive the access it was created with. Never notified.
158135 await cancelWatch ( dashboardAgentDb , { id : watchId , reason : "access_revoked" } ) ;
159136 return json (
160137 { error : "Access to this environment was revoked" , code : "access_revoked" } ,
@@ -166,8 +143,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
166143 const outcome = await checkWatch (
167144 watch . spec ,
168145 watchCheckDeps ( authorization . environment , now ) ,
169- // `previous` is the last check's facts, off the row. A tick that couldn't read
170- // anything freezes a streak instead of resetting it.
146+ // A tick that couldn't read anything freezes a streak instead of resetting it.
171147 { now, since, previous : previousCheckFacts ( watch . lastResult ) } ,
172148 ( error ) =>
173149 logger . error ( "Dashboard agent watch check failed" , {
@@ -180,8 +156,8 @@ export async function action({ request, params }: ActionFunctionArgs) {
180156 } )
181157 ) ;
182158
183- // Recorded even on the final evaluation: `lastResult` is what the notification
184- // reads. Guarded on `active`, so a concurrent fire/expire wins and this no-ops.
159+ // Recorded even on the final evaluation. Guarded on `active`, so a concurrent
160+ // fire/expire wins and this no-ops.
185161 await recordWatchCheck ( dashboardAgentDb , {
186162 id : watchId ,
187163 lastResult : {
@@ -194,8 +170,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
194170
195171 const batched = await ensureBatchChain ( watch ) ;
196172
197- // `observed` travels with the verdict: the task writes it onto the row in the
198- // same statement as the resolution, so no delivery surface re-reads the source.
173+ // `observed` travels with the verdict so no delivery surface re-reads the source.
199174 return json ( {
200175 result : outcome . result ,
201176 facts : outcome . facts ,
0 commit comments