3535 */
3636
3737import { createApi } from "@effectionx/context-api" ;
38- import { scoped } from "effection" ;
39- import type { Operation } from "effection" ;
40- import { getExpansion } from "@executablemd/core" ;
38+ import { Err , Ok , scoped } from "effection" ;
39+ import type { Operation , Result } from "effection" ;
40+ import { DocumentOutput , getExpansion } from "@executablemd/core" ;
4141import type { JournalAdmission } from "@executablemd/core/host" ;
4242import type { DurableEvent } from "@executablemd/durable-streams" ;
4343import { StaleInputError } from "@executablemd/durable-streams" ;
44- import type { BindOperations } from "@executablemd/git" ;
44+ import type { BindOperations , PullRequestReadOperations } from "@executablemd/git" ;
45+ import { PULL_REQUEST_CHECKS_WATCH } from "@executablemd/git/api" ;
46+ import type {
47+ CheckWatchInvocation ,
48+ CheckWatchResult ,
49+ PullRequestChecksWatchApi ,
50+ } from "@executablemd/git/api" ;
51+ import { parseCheckWatchResult } from "@executablemd/git" ;
52+ import type { CheckWatchReads } from "@executablemd/git" ;
4553import { PULL_REQUEST_API } from "@executablemd/git/api" ;
4654import type {
4755 PullRequestApi ,
@@ -68,6 +76,17 @@ import type { PullRequestResult } from "@executablemd/git";
6876import { canonicalPullRequestUrl , pullRequestProviderName } from "@executablemd/git" ;
6977import type { DenoPullRequestOperations } from "@executablemd/git/deno" ;
7078import { PULL_REQUEST_READ , retainPullRequestRead } from "@executablemd/git/deno" ;
79+ import {
80+ checksWatchConfiguration ,
81+ checksWatchRequest ,
82+ parseChecksWatchDeadline ,
83+ parseChecksWatchHead ,
84+ parseChecksWatchRequest ,
85+ PULL_REQUEST_CHECKS_WATCH_DEADLINE ,
86+ PULL_REQUEST_CHECKS_WATCH_EFFECTS ,
87+ PULL_REQUEST_CHECKS_WATCH_HEAD ,
88+ retainPullRequestChecksWatch ,
89+ } from "@executablemd/git/deno" ;
7190import { gitOperationFingerprint } from "@executablemd/git/deno" ;
7291
7392/** Why a retained read history cannot be continued. */
@@ -130,6 +149,13 @@ function* admitReplPullRequestReads(
130149 retained : readonly DurableEvent [ ] ,
131150) : Operation < void > {
132151 for ( const event of retained ) {
152+ if (
153+ event . type === "yield" &&
154+ PULL_REQUEST_CHECKS_WATCH_EFFECTS . includes ( event . description . type )
155+ ) {
156+ admitRetainedWatch ( owner , event ) ;
157+ continue ;
158+ }
133159 if ( event . type !== "yield" || event . description . type !== PULL_REQUEST_READ ) {
134160 continue ;
135161 }
@@ -172,6 +198,60 @@ function* admitReplPullRequestReads(
172198 }
173199}
174200
201+ /**
202+ * Hold one retained watch checkpoint to the question it was made under.
203+ *
204+ * The same four refusals an evidence read has: the record states its complete
205+ * question, names this entry's owner, is journaled under the name that question
206+ * produces, and — when it succeeded — holds the checkpoint its type retains.
207+ */
208+ function admitRetainedWatch ( owner : string , event : Extract < DurableEvent , { type : "yield" } > ) : void {
209+ const question = parseChecksWatchRequest ( event . description . input ) ;
210+ if ( question === undefined ) {
211+ throw new ReplPullRequestReadHistoryError (
212+ "malformed-record" ,
213+ "a retained pull-request checks watch in this entry's history does not state the " +
214+ "complete question it was made under." ,
215+ ) ;
216+ }
217+ if ( question . runId !== owner ) {
218+ throw new ReplPullRequestReadHistoryError (
219+ "foreign-owner" ,
220+ "a retained pull-request checks watch in this entry's history was made under another " +
221+ "owner." ,
222+ ) ;
223+ }
224+ const configuration = checksWatchConfiguration ( question ) ;
225+ if (
226+ event . description . configuration !== configuration ||
227+ event . description . name !== `${ question . expansionId } :${ configuration } `
228+ ) {
229+ throw new ReplPullRequestReadHistoryError (
230+ "request-fingerprint" ,
231+ "a retained pull-request checks watch is journaled under a name its own question does " +
232+ "not produce." ,
233+ ) ;
234+ }
235+ if ( event . result . status !== "ok" ) {
236+ return ;
237+ }
238+ const value = event . result . value ;
239+ const type = event . description . type ;
240+ const valid =
241+ type === PULL_REQUEST_CHECKS_WATCH_DEADLINE
242+ ? parseChecksWatchDeadline ( value ) !== undefined
243+ : type === PULL_REQUEST_CHECKS_WATCH_HEAD
244+ ? parseChecksWatchHead ( value ) !== undefined
245+ : parseCheckWatchResult ( value ) !== undefined ;
246+ if ( ! valid ) {
247+ throw new ReplPullRequestReadHistoryError (
248+ "malformed-evidence" ,
249+ "a retained pull-request checks watch holds a value that is not the checkpoint its " +
250+ "record type retains." ,
251+ ) ;
252+ }
253+ }
254+
175255/**
176256 * The exact configuration fingerprint one question produces.
177257 *
@@ -234,9 +314,124 @@ function parseRetainedQuestion(value: unknown): RetainedReadQuestion | undefined
234314export function replPullRequestReadPolicy (
235315 owner : string ,
236316 pullRequests : ( ) => DenoPullRequestOperations ,
237- ) : BindOperations < PullRequestOperationsApi [ "read" ] > {
238- return ( site : string ) => ( invocation : PullRequestReadInvocation ) =>
239- read ( owner , site , pullRequests , invocation ) ;
317+ ) : BindOperations < PullRequestReadOperations > {
318+ return ( site : string ) => ( {
319+ read : ( invocation : PullRequestReadInvocation ) => read ( owner , site , pullRequests , invocation ) ,
320+ watch : ( invocation : CheckWatchInvocation ) => watch ( owner , site , pullRequests , invocation ) ,
321+ } ) ;
322+ }
323+
324+ /**
325+ * One checks watch, through the semantic watch chain and this invocation's own
326+ * retention.
327+ *
328+ * The same layering a read has: a document's watch middleware composes around
329+ * an invocation-local terminal, and only that terminal reaches the transport.
330+ */
331+ function watch (
332+ owner : string ,
333+ site : string ,
334+ pullRequests : ( ) => DenoPullRequestOperations ,
335+ invocation : CheckWatchInvocation ,
336+ ) : Operation < CheckWatchResult > {
337+ return scoped ( function * ( ) : Operation < CheckWatchResult > {
338+ const local = createApi < PullRequestChecksWatchApi > ( PULL_REQUEST_CHECKS_WATCH , {
339+ watch : ( request : CheckWatchInvocation ) => watched ( owner , site , pullRequests , request ) ,
340+ } ) ;
341+ return yield * local . operations . watch ( invocation ) ;
342+ } ) ;
343+ }
344+
345+ /**
346+ * The watch the semantic chain delegated, retained under this entry's owner and
347+ * this invocation's site.
348+ *
349+ * Each cycle is an ordinary read through this invocation's transport chain, so
350+ * matching and target parsing are the snapshot's and are decided live on every
351+ * cycle. What differs is what happens once a cycle is admitted: it is performed
352+ * afresh rather than retained, because the watch retains its checkpoints and its
353+ * result rather than each poll.
354+ */
355+ function * watched (
356+ owner : string ,
357+ site : string ,
358+ pullRequests : ( ) => DenoPullRequestOperations ,
359+ invocation : CheckWatchInvocation ,
360+ ) : Operation < CheckWatchResult > {
361+ const domain = pullRequests ( ) ;
362+ const expansion = yield * getExpansion ( ) ;
363+ const reads : CheckWatchReads = {
364+ read : ( kind ) => cycleRead ( domain , invocation , kind ) ,
365+ } ;
366+ return yield * retainPullRequestChecksWatch (
367+ checksWatchRequest ( invocation . url , invocation . provider , invocation . timeoutSeconds , owner , site ) ,
368+ expansion . position ,
369+ reads ,
370+ ( line ) => DocumentOutput . operations . output ( `${ line } \n` ) ,
371+ ) ;
372+ }
373+
374+ /**
375+ * What an admitted cycle raised, kept apart from a refusal made before admission.
376+ *
377+ * A failure after admission is a read the watch may retry; one before it is a
378+ * question this host will not ask, which waiting does not change.
379+ */
380+ class WatchReadFailure extends Error {
381+ override name = "WatchReadFailure" ;
382+
383+ readonly failure : Error ;
384+
385+ constructor ( failure : Error ) {
386+ super ( failure . message ) ;
387+ this . failure = failure ;
388+ }
389+ }
390+
391+ /** One live cycle read, in a scope of its own so its session closes with it. */
392+ function cycleRead (
393+ domain : DenoPullRequestOperations ,
394+ invocation : CheckWatchInvocation ,
395+ kind : "checks" | "details" ,
396+ ) : Operation < Result < PullRequestReadResult > > {
397+ return scoped ( function * ( ) : Operation < Result < PullRequestReadResult > > {
398+ const local = createApi < PullRequestApi > ( PULL_REQUEST_API , {
399+ read : ( url : string , options : PullRequestReadOptions ) =>
400+ domain . read ( url , options , liveAttempt , declinedRead ) ,
401+ upsert : refusingUpsert ( ) ,
402+ } ) ;
403+ let answered : PullRequestReadResult ;
404+ try {
405+ answered = yield * local . operations . read ( invocation . url , {
406+ kind,
407+ ...( invocation . provider === undefined ? { } : { provider : invocation . provider } ) ,
408+ } ) ;
409+ } catch ( error ) {
410+ if ( error instanceof WatchReadFailure ) {
411+ return Err ( error . failure ) ;
412+ }
413+ throw error ;
414+ }
415+ const complete = parseCompletePullRequestReadResult ( answered , kind ) ;
416+ if ( complete === undefined ) {
417+ return Err (
418+ new Error ( "a watch cycle was answered with something other than the read it asked for." ) ,
419+ ) ;
420+ }
421+ return Ok ( complete ) ;
422+ } ) ;
423+ }
424+
425+ /** Perform an admitted cycle afresh, marking what it raised as a read failure. */
426+ function * liveAttempt (
427+ _admitted : unknown ,
428+ perform : ( ) => Operation < PullRequestReadResult > ,
429+ ) : Operation < PullRequestReadResult > {
430+ try {
431+ return yield * perform ( ) ;
432+ } catch ( error ) {
433+ throw new WatchReadFailure ( error instanceof Error ? error : new Error ( String ( error ) ) ) ;
434+ }
240435}
241436
242437/**
0 commit comments