Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 180 lines 6.76 KB
Raw
1 import { Effect, FileSystem, Option, Schema, Stream } from 'effect';
2 import { FetchHttpClient, HttpClient } from 'effect/http';
3 import { GitHub, PositiveInt, REPOSITORY_PATH, Sha, paginate } from './github';
4 import { Language } from './lanes';
5 import { ensure, fail, run, sha256Hex, withoutReleaseTokens } from './runtime';
6
7 const Digest = Schema.String.check(Schema.isPattern(/^sha256:[a-f0-9]{64}$/));
8 const IsoDate = Schema.String.check(Schema.isPattern(/^\d{4}-\d{2}-\d{2}T/));
9 const ArchiveEntry = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
10
11 export const CandidateFile = Schema.Struct({
12 name: Schema.String.check(Schema.isPattern(ArchiveEntry)),
13 sha256: Schema.String.check(Schema.isPattern(/^[a-f0-9]{64}$/)),
14 size: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
15 });
16 export type CandidateFile = typeof CandidateFile.Type;
17
18 /** The release-PR candidate whose checked bytes a source-tag run publishes. */
19 export const ApprovedCandidate = Schema.Struct({
20 run: PositiveInt,
21 artifactId: PositiveInt,
22 digest: Digest,
23 });
24 export type ApprovedCandidate = typeof ApprovedCandidate.Type;
25
26 /** The approved candidate of each selected language, pinned once per tag run. */
27 export const ApprovedCandidates = Schema.Struct({
28 typescript: Schema.optionalKey(ApprovedCandidate),
29 python: Schema.optionalKey(ApprovedCandidate),
30 });
31 export type ApprovedCandidates = typeof ApprovedCandidates.Type;
32
33 export const CandidateManifest = Schema.Struct({
34 sourceSha: Sha,
35 producerRun: PositiveInt,
36 language: Language,
37 files: Schema.Array(CandidateFile).check(
38 Schema.isMinLength(1),
39 Schema.makeFilter(files =>
40 new Set(files.map(file => file.name)).size === files.length
41 ? undefined
42 : 'Candidate filenames must be unique'
43 )
44 ),
45 });
46 export type CandidateManifest = typeof CandidateManifest.Type;
47
48 /** Name of the manifest each candidate artifact carries next to its distributions. */
49 export const CANDIDATE_MANIFEST = 'candidate.json';
50 export const candidateArtifactName = (language: Language) => `sdk-candidate-${language}`;
51
52 export const ActionArtifact = Schema.Struct({
53 id: PositiveInt,
54 name: Schema.String,
55 digest: Digest,
56 expired: Schema.Boolean,
57 created_at: IsoDate,
58 expires_at: IsoDate,
59 workflow_run: Schema.Struct({ id: PositiveInt, head_sha: Sha }),
60 });
61 export type CandidateArtifact = typeof ActionArtifact.Type;
62
63 const ArtifactsPage = Schema.Struct({ artifacts: Schema.Array(ActionArtifact) });
64
65 export const normalizeArtifactDigest = (value: unknown) =>
66 Effect.gen(function* () {
67 const digest = yield* Schema.decodeUnknownEffect(Schema.String)(value);
68 return yield* Schema.decodeUnknownEffect(Digest)(
69 digest.startsWith('sha256:') ? digest : `sha256:${digest}`
70 );
71 });
72
73 /** An artifact is usable while GitHub still retains it. */
74 const isRetained = (artifact: CandidateArtifact, now: number) =>
75 !artifact.expired && Date.parse(artifact.expires_at) > now;
76
77 /** The run's single artifact with this name, if it was produced from this source and is still retained. */
78 export const reuseCandidate = (
79 artifacts: ReadonlyArray<CandidateArtifact>,
80 name: string,
81 producerRun: number,
82 sourceSha: string,
83 now = Date.now()
84 ) =>
85 Effect.gen(function* () {
86 const matching = artifacts.filter(artifact => artifact.name === name);
87 yield* ensure(matching.length <= 1, 'Multiple immutable candidates have the same name');
88 const [artifact] = matching;
89 if (!artifact) return Option.none<CandidateArtifact>();
90 yield* ensure(
91 artifact.workflow_run.id === producerRun && artifact.workflow_run.head_sha === sourceSha,
92 'Candidate source or producer does not match'
93 );
94 yield* ensure(
95 isRetained(artifact, now),
96 'Original candidate unavailable; add changesets and request a new release'
97 );
98 return Option.some(yield* Schema.decodeUnknownEffect(ActionArtifact)(artifact));
99 });
100
101 export const verifyArchive = (bytes: Uint8Array, digest: string) =>
102 Effect.gen(function* () {
103 yield* ensure(
104 `sha256:${yield* sha256Hex(bytes)}` === digest,
105 'Actions artifact archive digest mismatch'
106 );
107 });
108
109 export const verifyCandidateFiles = (
110 actual: ReadonlyMap<string, Uint8Array>,
111 expected: ReadonlyArray<CandidateFile>
112 ) =>
113 Effect.gen(function* () {
114 yield* ensure(actual.size === expected.length, 'Candidate file inventory mismatch');
115 for (const file of expected) {
116 const bytes = actual.get(file.name);
117 yield* ensure(
118 bytes !== undefined &&
119 bytes.length === file.size &&
120 (yield* sha256Hex(bytes)) === file.sha256,
121 `Candidate checksum mismatch: ${file.name}`
122 );
123 }
124 });
125
126 export const readCandidateFiles = (directory: string) =>
127 Effect.gen(function* () {
128 const fs = yield* FileSystem.FileSystem;
129 const names = (yield* fs.readDirectory(directory)).filter(name => name !== CANDIDATE_MANIFEST);
130 return new Map(
131 yield* Effect.forEach(names, name =>
132 Effect.map(fs.readFile(`${directory}/${name}`), bytes => [name, bytes] as const)
133 )
134 );
135 });
136
137 export const originalCandidates = (run: number) =>
138 Stream.runCollect(
139 paginate(
140 `${REPOSITORY_PATH}/actions/runs/${run}/artifacts`,
141 ArtifactsPage,
142 body => body.artifacts
143 )
144 );
145
146 /** Downloads an original Actions artifact, checks its digest and archive paths, and unpacks it into `directory`. */
147 export const downloadCandidate = (artifact: { id: number; digest: string }, directory: string) =>
148 Effect.gen(function* () {
149 const github = yield* GitHub;
150 const fs = yield* FileSystem.FileSystem;
151 const client = yield* HttpClient.HttpClient;
152 const location = yield* github.redirectLocation(
153 `${REPOSITORY_PATH}/actions/artifacts/${artifact.id}/zip`
154 );
155 const url = yield* Option.match(location, {
156 onNone: () => fail('Artifact download has no archive location'),
157 onSome: value => Effect.try(() => new URL(value)),
158 });
159 yield* ensure(url.protocol === 'https:', 'Expected an HTTPS artifact archive');
160 const archive = yield* client
161 .get(url)
162 .pipe(Effect.provideService(FetchHttpClient.RequestInit, { redirect: 'error' }));
163 yield* ensure(
164 archive.status >= 200 && archive.status < 300,
165 'Original artifact is unavailable'
166 );
167 const bytes = new Uint8Array(yield* archive.arrayBuffer);
168 yield* verifyArchive(bytes, artifact.digest);
169 const zip = `${directory}.zip`;
170 yield* fs.makeDirectory(directory, { recursive: true });
171 yield* fs.writeFile(zip, bytes);
172 const paths = (yield* run('unzip', ['-Z1', zip], { env: withoutReleaseTokens }))
173 .trim()
174 .split('\n');
175 yield* ensure(
176 new Set(paths).size === paths.length && paths.every(path => ArchiveEntry.test(path)),
177 'Unexpected artifact archive path'
178 );
179 yield* run('unzip', ['-q', zip, '-d', directory], { env: withoutReleaseTokens });
180 });