Skip to content

Commit 6ce7173

Browse files
committed
šŸ” Open one GitHub session per invocation, and prove what cancellation leaves
Continues the review of 891040c. The GitHub half of the lifetime finding, the cancellation and teardown evidence, and the shared-source evidence. **One access session per GitHub invocation.** `authorizedHeaders` read the credential on every request, so a pull request could be created under one identity and observed under another; the Issue middleware built its access once at installation, so memoizing there would have retained an identity across every Issue in the run. Both now hold a `GitHubSource`, which is credential-free and therefore safe to keep, and open a session per live invocation: PullRequest after its local push-evidence check, Issue after its ceiling. A session reads the credential once and dies with the invocation, so two calls are two identities and an interrupted attempt reacquires. **Cancellation invents nothing.** Four deterministic tests drive a halt through the two moments that matter — blocked inside the acquisition, and blocked inside the transport it authenticated — and read what is left: one session opened, one disposed, no remote contacted or nothing retained, no credential working directory surviving in the temporary directory, and the attempt after a cancelled one opening its own session rather than continuing under one nobody re-proved. Success and refusal are covered on the same terms. **The shared source, through both adapters.** A session reads once however many requests it makes; two invocations are two reads; a source that is merely held reads nothing at all; and an Issue outside the ceiling is refused before any session exists, so an identity is never established for a target this host never authorized. Still open from the review: the interrupted-Push crash fixture (3), and the `workflow-host.test.ts` Deno/compiled host-assembly regression (7). Refs #522
1 parent fb70f94 commit 6ce7173

11 files changed

Lines changed: 427 additions & 22 deletions

ā€Žpackages/workflow/src/deno/composition/github.tsā€Ž

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* request for the same branch.
3434
*/
3535

36-
import { ensure, scoped, until } from "effection";
36+
import { ensure, resource, scoped, until } from "effection";
3737
import type { Operation } from "effection";
3838
import { tmpdir } from "node:os";
3939
import process from "node:process";
@@ -213,6 +213,71 @@ export function denoGitHubLogin(
213213
};
214214
}
215215

216+
/**
217+
* Where a live invocation gets its access, without holding one.
218+
*
219+
* A source is credential-free and long-lived: an installed middleware or a
220+
* provider module may hold one for as long as it likes, because there is nothing
221+
* in one to retain. A *session* is what has an identity, and one is opened per
222+
* live invocation — after that invocation's ceiling and local authority checks
223+
* — and disposed with it. Two calls are two sessions, so an observation and the
224+
* mutation it decided go out under one identity while two unrelated invocations
225+
* never share one.
226+
*/
227+
export interface GitHubSource {
228+
readonly endpoint: string;
229+
open(): Operation<GitHubAccess>;
230+
}
231+
232+
/**
233+
* One invocation's access over this source.
234+
*
235+
* The credential is read no earlier than the first request that needs one, and
236+
* then not again: every request this invocation makes carries the identity its
237+
* first one established. Nothing outlives the session — the next invocation
238+
* reads whatever the host holds then, which is what makes an interrupted attempt
239+
* reacquire rather than resume under an identity nobody re-proved.
240+
*/
241+
function accessSession(access: GitHubAccess): GitHubAccess {
242+
let read = false;
243+
let held: string | undefined;
244+
return {
245+
endpoint: access.endpoint,
246+
*token(): Operation<string | undefined> {
247+
if (!read) {
248+
held = yield* access.token();
249+
read = true;
250+
}
251+
return held;
252+
},
253+
send(request: GitHubHttpRequest): Operation<GitHubHttpResponse> {
254+
return access.send(request);
255+
},
256+
};
257+
}
258+
259+
/** A source over one access, opening a session per invocation. */
260+
export function gitHubSource(access: GitHubAccess): GitHubSource {
261+
return {
262+
endpoint: access.endpoint,
263+
open(): Operation<GitHubAccess> {
264+
return resource(function* (provide) {
265+
// A resource rather than a value, so the session ends with the scope
266+
// that opened it whether that scope returned, refused or was cancelled.
267+
yield* provide(accessSession(access));
268+
});
269+
},
270+
};
271+
}
272+
273+
/** The shipped source: the platform's transport and this host's credentials. */
274+
export function denoGitHubSource(
275+
endpoint: string = GITHUB_API,
276+
options: GitHubAccessOptions = {},
277+
): GitHubSource {
278+
return gitHubSource(denoGitHubAccess(endpoint, options));
279+
}
280+
216281
export interface GitHubAccessOptions {
217282
/** Where the two explicit variables are read from. */
218283
readonly environment?: Readonly<Record<string, string | undefined>>;

ā€Žpackages/workflow/src/deno/composition/provider.tsā€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ import { createGitAdd } from "./add.ts";
6161
import { createGitCommit } from "./commit.ts";
6262
import { createGitPush } from "./push.ts";
6363
import { upsertPullRequest } from "./pull-request.ts";
64-
import type { GitHubAccess } from "./github.ts";
64+
import type { GitHubSource } from "./github.ts";
6565

6666
export { WORKSPACE_REPOSITORY, WORKSPACE_WORKTREE } from "./effects.ts";
6767
export { WORKSPACE_GIT_SWITCH } from "./switch.ts";
@@ -92,7 +92,7 @@ export interface CompositionProviderOptions {
9292
* observe or route through. Absent, the provider builds the default access,
9393
* which reaches `api.github.com` and the process environment.
9494
*/
95-
readonly gitHub?: GitHubAccess;
95+
readonly gitHub?: GitHubSource;
9696
/**
9797
* What the host lends a Git command that transports to a remote.
9898
*

ā€Žpackages/workflow/src/deno/composition/pull-request.tsā€Ž

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,11 @@ import type { WorkflowRunDatabase } from "../../storage/api.ts";
8181
import { transactWorkspaceRoots } from "../workspace/private.ts";
8282
import { currentBranch, gitSession, resolveCommit } from "./git.ts";
8383
import {
84-
denoGitHubAccess,
84+
denoGitHubSource,
8585
gitHubPullRequests,
8686
parseGitHubRepository,
8787
type GitHubAccess,
88+
type GitHubSource,
8889
} from "./github.ts";
8990
import type { RepositoryHost } from "./host.ts";
9091
import {
@@ -354,7 +355,7 @@ export function* upsertPullRequest(
354355
database: WorkflowRunDatabase,
355356
host: RepositoryHost,
356357
request: PullRequestRequest,
357-
access: GitHubAccess = denoGitHubAccess(),
358+
source: GitHubSource = denoGitHubSource(),
358359
): Operation<PullRequestOutcome> {
359360
// Admission takes a snapshot, and the snapshot is what the operation runs on.
360361
// A caller's request and the record inside it are its own objects, and this
@@ -404,6 +405,13 @@ export function* upsertPullRequest(
404405
// closure and reaches no durable or public value.
405406
const locator = selection.repository.locator;
406407

408+
// One access session for this whole reconciliation, opened after the local
409+
// authority check above and shared by its observations and its mutation, so
410+
// a pull request is not created under one identity and observed under
411+
// another. It is disposed with the scope below; a later attempt on an
412+
// interrupted request opens its own.
413+
const access = yield* source.open();
414+
407415
const record = yield* withGitHostProvider(
408416
pullRequestProvider(access, locator, inputs),
409417
reconcileGitHostEffect({

ā€Žpackages/workflow/src/deno/issue/github.tsā€Ž

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@ import type { Operation } from "effection";
5353
import { canonicalFingerprint } from "@executablemd/core";
5454
import {
5555
authorizedHeaders,
56-
denoGitHubAccess,
56+
denoGitHubSource,
5757
member,
5858
nextPage,
5959
nonEmpty,
6060
readJson,
6161
PAGE_LIMIT,
6262
PAGE_SIZE,
6363
type GitHubAccess,
64+
type GitHubSource,
6465
type GitHubHttpResponse,
6566
} from "../composition/github.ts";
6667
import { IssueApi } from "../../issue/api.ts";
@@ -305,7 +306,7 @@ export interface GitHubIssuesOptions {
305306
* document can write widens it.
306307
*/
307308
readonly ceiling: readonly string[];
308-
readonly access?: GitHubAccess;
309+
readonly access?: GitHubSource;
309310
}
310311

311312
/**
@@ -317,7 +318,10 @@ export interface GitHubIssuesOptions {
317318
* `IssueApi`'s own base error to report that nothing handled the request.
318319
*/
319320
export function* useGitHubIssues(options: GitHubIssuesOptions): Operation<void> {
320-
const access = options.access ?? denoGitHubAccess();
321+
// A source rather than an access: it is credential-free, so holding one for
322+
// the middleware's whole lifetime retains nothing. A session — which does have
323+
// an identity — is opened per request below, after that request's ceiling.
324+
const source = options.access ?? denoGitHubSource();
321325

322326
yield* IssueApi.around(
323327
{
@@ -338,7 +342,9 @@ export function* useGitHubIssues(options: GitHubIssuesOptions): Operation<void>
338342
if (issue === undefined) {
339343
throw new IssueUnavailableError();
340344
}
341-
return yield* observed(access, issue, url);
345+
// After the ceiling, never before: a session opened first would be an
346+
// identity established for a target this host had not authorized.
347+
return yield* observed(yield* source.open(), issue, url);
342348
},
343349

344350
*upsert([issue, upsert], next): Operation<IssueReference> {
@@ -361,7 +367,7 @@ export function* useGitHubIssues(options: GitHubIssuesOptions): Operation<void>
361367
if (name === undefined) {
362368
throw new IssueUnavailableError();
363369
}
364-
return yield* reconcile(access, name, issue, upsert);
370+
return yield* reconcile(yield* source.open(), name, issue, upsert);
365371
},
366372
},
367373
{ at: "min" },

0 commit comments

Comments
Ā (0)