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 336 lines 13.5 KB
Raw
1 import { Effect, Schema } from 'effect';
2 import { getPackages } from '@manypkg/get-packages';
3 import semver from 'semver';
4 import { PositiveInt, RunId, SDK_RELEASE_REPOSITORY, Sha } from './github';
5 import { CHANNELS } from './channel';
6 import { PYTHON_RELEASE_MARKER, type Language } from './lanes';
7 import { SdkReleasePlan } from './plan';
8 import { PROJECT_VERSION, RUNTIME_VERSION, SETUP_VERSION } from './python-metadata';
9 import { ensure, fail, fromPromise } from './runtime';
10
11 /** One release request: who asked, the frozen source, and the planned versions. */
12 export const ReleaseBatch = Schema.Struct({
13 id: RunId,
14 /** The protected `next` commit the release is planned from. */
15 inputBase: Sha,
16 /** The member who requested this release; they may approve it only as a repository admin. */
17 requestedBy: Schema.String,
18 /** The issue that tracks this release. */
19 issue: PositiveInt,
20 date: Schema.String.check(Schema.isPattern(/^\d{4}-\d{2}-\d{2}$/)),
21 plan: SdkReleasePlan,
22 });
23 export type ReleaseBatch = typeof ReleaseBatch.Type;
24 export const BATCH_PATH = '.github/sdk-release-plan.json';
25
26 /** The tag a release batch publishes from. */
27 export const releaseTag = (id: string) => `sdk-release/${id}`;
28
29 /** What the batch's channel changes about its release. */
30 export const channelOf = (batch: Pick<ReleaseBatch, 'plan'>) =>
31 CHANNELS[batch.plan.choices.channel];
32
33 export const issueUrl = (batch: ReleaseBatch) =>
34 `https://github.com/${SDK_RELEASE_REPOSITORY}/issues/${batch.issue}`;
35
36 export const batchLanguages = (batch: {
37 readonly plan: { readonly releases: ReadonlyArray<{ readonly language: Language }> };
38 }) => new Set(batch.plan.releases.map(release => release.language));
39
40 export const ReleaseFile = Schema.Struct({
41 path: Schema.String,
42 before: Schema.NullOr(Schema.String),
43 after: Schema.NullOr(Schema.String),
44 });
45 export type ReleaseFile = typeof ReleaseFile.Type;
46
47 export const ReleasePatch = Schema.Struct({
48 files: Schema.Array(ReleaseFile).check(Schema.isMinLength(1)),
49 });
50 export type ReleasePatch = typeof ReleasePatch.Type;
51
52 type DiffPlan = Pick<SdkReleasePlan, 'releases' | 'changesets' | 'retainedChangesets'>;
53 type Bump = SdkReleasePlan['releases'][number]['type'];
54
55 const updatedDependencyRange = (range: string, version: string, type: Bump) =>
56 Effect.gen(function* () {
57 const workspace = range.startsWith('workspace:');
58 const value = workspace ? range.slice('workspace:'.length) : range;
59 const sets = new semver.Range(value).set;
60 const comparators = sets.length === 1 ? sets[0] : [];
61 const bounded = comparators.length === 2 && value.includes('>') && value.includes('<');
62 const updated = bounded
63 ? yield* boundedRange(comparators, version, type)
64 : `${['^', '~', '>=', '<=', '>'].find(prefix => value.startsWith(prefix)) ?? ''}${version}`;
65 return workspace ? `workspace:${updated}` : updated;
66 });
67
68 /** Moves a `>=lower <upper` range to the new version, widening the upper bound only when needed. */
69 const boundedRange = (comparators: ReadonlyArray<semver.Comparator>, version: string, type: Bump) =>
70 Effect.gen(function* () {
71 const lower = comparators.find(comparator => comparator.operator.startsWith('>'));
72 const upper = comparators.find(comparator => comparator.operator.startsWith('<'));
73 if (!lower || !upper) return yield* fail('Unsupported SDK dependency range');
74 const upperBound = semver.satisfies(version, upper.value)
75 ? upper.value
76 : `<${semver.inc(version, type)}`;
77 return comparators[0] === lower ? `>=${version} ${upperBound}` : `${upperBound} >=${version}`;
78 });
79
80 const Manifest = Schema.StructWithRest(
81 Schema.Struct({ name: Schema.String, version: Schema.String }),
82 [Schema.Record(Schema.String, Schema.Unknown)]
83 );
84 const decodeManifest = (contents: string) =>
85 Effect.map(Schema.decodeUnknownEffect(Schema.fromJsonString(Manifest))(contents), manifest => ({
86 ...manifest,
87 }));
88 const Dependencies = Schema.Record(Schema.String, Schema.String);
89 const DEPENDENCY_FIELDS = [
90 'dependencies',
91 'devDependencies',
92 'peerDependencies',
93 'optionalDependencies',
94 ] as const;
95
96 /** A release may change only its own version and the ranges that point at released SDK packages. */
97 const validateManifest = (before: string, after: string, plan: DiffPlan, path: string) =>
98 Effect.gen(function* () {
99 const old = yield* decodeManifest(before);
100 const next: Record<string, unknown> = yield* decodeManifest(after);
101 const release = plan.releases.find(candidate => candidate.name === old.name);
102 if (release) {
103 yield* ensure(
104 old.version === release.oldVersion && next.version === release.newVersion,
105 `Wrong target version: ${path}`
106 );
107 next.version = old.version;
108 }
109 for (const field of DEPENDENCY_FIELDS) {
110 if (old[field] === undefined && next[field] === undefined) continue;
111 const previous = yield* Schema.decodeUnknownEffect(Dependencies)(old[field]);
112 const updated = { ...(yield* Schema.decodeUnknownEffect(Dependencies)(next[field])) };
113 yield* ensure(
114 JSON.stringify(Object.keys(previous).sort()) ===
115 JSON.stringify(Object.keys(updated).sort()),
116 `Release changes dependency inventory: ${path}`
117 );
118 for (const [name, range] of Object.entries(previous)) {
119 if (updated[name] === range) continue;
120 const dependency = plan.releases.find(candidate => candidate.name === name);
121 const dependencyRange = range.replace(/^workspace:/, '');
122 const planned =
123 dependency !== undefined &&
124 semver.validRange(dependencyRange) !== null &&
125 !(
126 field === 'peerDependencies' && semver.satisfies(dependency.newVersion, dependencyRange)
127 ) &&
128 updated[name] ===
129 (yield* updatedDependencyRange(range, dependency.newVersion, dependency.type));
130 yield* ensure(planned, `Unplanned dependency change: ${path} ${name}`);
131 updated[name] = range;
132 }
133 next[field] = updated;
134 }
135 yield* ensure(
136 JSON.stringify(old) === JSON.stringify(next),
137 `Release changes package behavior: ${path}`
138 );
139 });
140
141 /** Python files the synchronized train rewrites, and the version pattern each one carries. */
142 const PYTHON_VERSION_FILES: ReadonlyArray<{ readonly suffix: string; readonly pattern: RegExp }> = [
143 { suffix: 'pyproject.toml', pattern: PROJECT_VERSION },
144 { suffix: 'setup.py', pattern: SETUP_VERSION },
145 { suffix: '__version__.py', pattern: RUNTIME_VERSION },
146 ];
147 const PYTHON_METADATA_PATH =
148 /^python\/(?:providers\/[^/]+\/)?(?:pyproject\.toml|setup\.py)$|^python\/composio\/__version__\.py$/;
149
150 const pythonRelease = (plan: DiffPlan) =>
151 plan.releases.find(release => release.name === PYTHON_RELEASE_MARKER);
152
153 const expectedPythonMetadata = (before: string, plan: DiffPlan, path: string) =>
154 Effect.gen(function* () {
155 const release = pythonRelease(plan);
156 if (!release) return before;
157 const { pattern } =
158 PYTHON_VERSION_FILES.find(file => path.endsWith(file.suffix)) ?? PYTHON_VERSION_FILES[2];
159 const current = before.match(pattern)?.[3];
160 yield* ensure(
161 current === undefined || current === release.oldVersion,
162 `Python baseline changed: ${path}`
163 );
164 return before.replace(
165 pattern,
166 (_match, prefix: string, quote: string) => `${prefix}${quote}${release.newVersion}${quote}`
167 );
168 });
169
170 const WORKSPACE_LOCK_SOURCE =
171 /^source = \{ (?:editable|virtual) = "python(?:\/providers\/[^"/]+)?" \}$/m;
172 const LOCK_VERSION = /^version = "([^"]+)"$/m;
173
174 const expectedUvLock = (before: string, plan: DiffPlan) =>
175 Effect.gen(function* () {
176 const release = pythonRelease(plan);
177 if (!release) return before;
178 const blocks = before.split('[[package]]');
179 const workspaceVersions = blocks
180 .filter(block => WORKSPACE_LOCK_SOURCE.test(block))
181 .map(block => block.match(LOCK_VERSION)?.[1])
182 .filter(version => version !== undefined);
183 yield* ensure(
184 workspaceVersions.every(version => version === release.oldVersion),
185 'Python lock baseline changed'
186 );
187 return blocks
188 .map(block =>
189 WORKSPACE_LOCK_SOURCE.test(block)
190 ? block.replace(LOCK_VERSION, `version = "${release.newVersion}"`)
191 : block
192 )
193 .join('[[package]]');
194 });
195
196 /** Only a released package's own peer-context version may change in the pnpm lockfile. */
197 const normalizePnpmContexts = (contents: string, plan: DiffPlan) =>
198 plan.releases
199 .filter(release => release.language === 'typescript')
200 .reduce(
201 (result, release) =>
202 result.replaceAll(
203 `${release.name}@${release.newVersion}`,
204 `${release.name}@${release.oldVersion}`
205 ),
206 contents
207 );
208
209 const retainedNote = (note: DiffPlan['retainedChangesets'][number]) =>
210 `---\n${note.releases.map(entry => `${JSON.stringify(entry.name)}: ${entry.type}`).join('\n')}\n---\n\n${note.summary}\n`;
211
212 const GENERATED_NOTE = /^sdk-maintenance-(typescript|python)$/;
213
214 const plannedVersionMetadata = (
215 files: ReadonlyArray<ReleaseFile>,
216 release: { name: string; newVersion: string }
217 ) =>
218 files.some(file => {
219 if (!file.path.endsWith('/package.json') || file.after === null) return false;
220 const manifest = Schema.decodeUnknownOption(Manifest)(JSON.parse(file.after));
221 return (
222 manifest._tag === 'Some' &&
223 manifest.value.name === release.name &&
224 manifest.value.version === release.newVersion
225 );
226 });
227
228 /** Checks one changed file against the release outputs it may legitimately belong to. */
229 const validateReleaseFile = (
230 file: ReleaseFile,
231 plan: DiffPlan,
232 outputs: ReadonlyArray<string>,
233 notes: ReadonlySet<string>,
234 selected: ReadonlySet<string>
235 ) =>
236 Effect.gen(function* () {
237 const { path, before, after } = file;
238 if (path === BATCH_PATH) return;
239 yield* ensure(
240 !path.includes('..') && !path.startsWith('/') && !path.includes('\\'),
241 'Invalid release path'
242 );
243 if (
244 outputs.some(output => path === output || (output.endsWith('/') && path.startsWith(output)))
245 )
246 return;
247 if (notes.has(path)) {
248 const retained = plan.retainedChangesets.find(note => path === `.changeset/${note.id}.md`);
249 yield* ensure(
250 after === (retained ? retainedNote(retained) : null),
251 `Selected release note changed: ${path}`
252 );
253 return;
254 }
255 if (path === 'uv.lock' || path === 'pnpm-lock.yaml') {
256 const unchanged =
257 before !== null &&
258 after !== null &&
259 (path === 'uv.lock'
260 ? (yield* expectedUvLock(before, plan)) === after
261 : normalizePnpmContexts(before, plan) === normalizePnpmContexts(after, plan));
262 yield* ensure(unchanged, `Release changes dependency resolution: ${path}`);
263 return;
264 }
265 if (/^(ts\/packages\/.*|python)\/package\.json$/.test(path) && before && after)
266 return yield* validateManifest(before, after, plan, path);
267 if (selected.has(PYTHON_RELEASE_MARKER) && PYTHON_METADATA_PATH.test(path) && before && after) {
268 yield* ensure(
269 (yield* expectedPythonMetadata(before, plan, path)) === after,
270 `Python source changed: ${path}`
271 );
272 return;
273 }
274 return yield* fail(`Not a planned release output: ${path}`);
275 });
276
277 /**
278 * A beta release PR commits only its plan: its versions are applied where the candidates are
279 * built, so `next` keeps its stable versions and every changeset.
280 */
281 const validatePlanOnlyDiff = (files: ReadonlyArray<ReleaseFile>) =>
282 ensure(
283 files.length === 1 && files[0].path === BATCH_PATH,
284 `A beta release PR changes only ${BATCH_PATH}`
285 );
286
287 /** Checks the release PR's diff against what its channel lets it commit. */
288 export const validateBatchDiff = (
289 files: ReadonlyArray<ReleaseFile>,
290 batch: ReleaseBatch,
291 outputs: ReadonlyArray<string>
292 ) =>
293 channelOf(batch).commitsVersions
294 ? validateReleaseDiff(files, batch.plan, outputs)
295 : validatePlanOnlyDiff(files);
296
297 /** Every changed file must be a planned version, note, lockfile, or generated release output. */
298 export const validateReleaseDiff = (
299 files: ReadonlyArray<ReleaseFile>,
300 plan: DiffPlan,
301 outputs: ReadonlyArray<string>
302 ) =>
303 Effect.gen(function* () {
304 for (const release of plan.releases)
305 yield* ensure(
306 plannedVersionMetadata(files, release),
307 `Missing planned version metadata: ${release.name}`
308 );
309 for (const note of plan.changesets)
310 yield* ensure(
311 GENERATED_NOTE.test(note.id) ||
312 files.some(file => file.path === `.changeset/${note.id}.md`),
313 `Selected release note was not consumed: ${note.id}`
314 );
315 const selected = new Set(plan.releases.map(release => release.name));
316 const notes = new Set(plan.changesets.map(note => `.changeset/${note.id}.md`));
317 for (const file of files) yield* validateReleaseFile(file, plan, outputs, notes, selected);
318 });
319
320 /** The changelog and reference paths a release may write besides version metadata. */
321 export const releaseOutputs = (cwd: string, batch: ReleaseBatch) =>
322 Effect.gen(function* () {
323 if (!channelOf(batch).commitsVersions) return [];
324 const { packages } = yield* fromPromise(() => getPackages(cwd));
325 const names = new Set(batch.plan.releases.map(release => release.name));
326 const [year, month, day] = batch.date.split('-');
327 return [
328 ...packages
329 .filter(pkg => names.has(pkg.packageJson.name))
330 .map(pkg => `${pkg.dir.slice(cwd.length + 1)}/CHANGELOG.md`),
331 `docs/content/changelog/${month}-${day}-${year.slice(2)}-sdk-${batch.id}.mdx`,
332 ...[...batchLanguages(batch)].map(
333 language => `docs/content/reference/sdk-reference/${language}/`
334 ),
335 ].sort();
336 });