Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 03:37 UTC
| 1 | import { describe, expect, test } from 'bun:test'; |
| 2 | import { ConfigProvider, Effect, Layer, Option } from 'effect'; |
| 3 | import { Digest } from 'effect/crypto'; |
| 4 | import { Base64 } from 'effect/encoding'; |
| 5 | import semver from 'semver'; |
| 6 | import { readReleasePull } from '../scripts/sdk-release/approval'; |
| 7 | import { |
| 8 | authorizeSourceTag, |
| 9 | executeReleaseRequest, |
| 10 | openReleasePull, |
| 11 | } from '../scripts/sdk-release/batch'; |
| 12 | import { NpmCli, publishNpm, type Distribution } from '../scripts/sdk-release/registry'; |
| 13 | import { stageOutcome } from '../scripts/sdk-release/report'; |
| 14 | import { CommandError, sha256Hex } from '../scripts/sdk-release/runtime'; |
| 15 | import type { ReleaseBatch } from '../scripts/sdk-release/source'; |
| 16 | import { STAGES, type ReleaseState, type StateName } from '../scripts/sdk-release/state'; |
| 17 | import { |
| 18 | base, |
| 19 | batch, |
| 20 | betaBatch, |
| 21 | betaFiles, |
| 22 | fakeController, |
| 23 | head, |
| 24 | releaseFiles, |
| 25 | } from './sdk-release-controller-fixture'; |
| 26 | import { resolvePath, runOnTestClock, runRelease, type Fetch } from './sdk-release-harness'; |
| 27 | |
| 28 | const NPM = 'https://registry.npmjs.org'; |
| 29 | const MINUTE = 60_000; |
| 30 | const ID_TOKEN_REQUEST_URL = 'https://token.actions.example.test/oidc?api-version=2.0'; |
| 31 | |
| 32 | /** |
| 33 | * A fake npm registry with its dist-tags, and the npm CLI that writes to it. Uploads record SLSA |
| 34 | * provenance the way trusted publishing does, from `ref` (the release tag unless a test forges |
| 35 | * another one). The trusted-publishing login succeeds. With `uploaded`, an earlier run already |
| 36 | * uploaded those bytes for the staged version, so npm refuses another upload of it; npm hides |
| 37 | * every version and its provenance until the test clock reaches `scanUntil`. |
| 38 | */ |
| 39 | function npmRegistry( |
| 40 | tags: Record<string, string>, |
| 41 | options: { ref?: string; otherBytes?: boolean; uploaded?: Uint8Array; scanUntil?: number } = {} |
| 42 | ) { |
| 43 | const versions = new Map<string, Uint8Array>(); |
| 44 | const commands: string[] = []; |
| 45 | const pending = new Map<string, Distribution>(); |
| 46 | let uploads = 0; |
| 47 | let clock = () => 0; |
| 48 | const sha512 = (bytes: Uint8Array) => |
| 49 | runRelease( |
| 50 | Effect.gen(function* () { |
| 51 | return yield* (yield* Digest.Digest).digestHex(Digest.sha512, bytes); |
| 52 | }) |
| 53 | ); |
| 54 | const provenance = new Map<string, string>(); |
| 55 | const actionsEnv = ConfigProvider.layer( |
| 56 | ConfigProvider.fromUnknown({ |
| 57 | ACTIONS_ID_TOKEN_REQUEST_URL: ID_TOKEN_REQUEST_URL, |
| 58 | ACTIONS_ID_TOKEN_REQUEST_TOKEN: 'request-token', |
| 59 | }) |
| 60 | ); |
| 61 | /** Stores `bytes` as `version` under the release-only tag, with provenance for those bytes. */ |
| 62 | const record = async (version: string, bytes: Uint8Array, releaseOnly: string) => { |
| 63 | versions.set(version, bytes); |
| 64 | tags[releaseOnly] = version; |
| 65 | const statement = { |
| 66 | subject: [ |
| 67 | { digest: { sha512: await sha512(options.otherBytes ? new Uint8Array([1]) : bytes) } }, |
| 68 | ], |
| 69 | predicate: { |
| 70 | buildDefinition: { |
| 71 | externalParameters: { |
| 72 | workflow: { |
| 73 | ref: |
| 74 | options.ref ?? `refs/tags/sdk-release/${releaseOnly.slice('sdk-release-'.length)}`, |
| 75 | repository: 'https://github.com/ComposioHQ/composio', |
| 76 | path: '.github/workflows/sdk.release.yml', |
| 77 | }, |
| 78 | }, |
| 79 | }, |
| 80 | }, |
| 81 | }; |
| 82 | provenance.set(version, Base64.encode(JSON.stringify(statement))); |
| 83 | }; |
| 84 | const npmCli = Layer.succeed(NpmCli, { |
| 85 | run: (args: ReadonlyArray<string>) => |
| 86 | Effect.promise(async () => { |
| 87 | commands.push(args.join(' ')); |
| 88 | const [command, target] = args; |
| 89 | if (command === 'publish') { |
| 90 | const file = [...pending.values()].find(candidate => target.endsWith(candidate.file)); |
| 91 | if (!file) throw new Error(`Unexpected upload ${target}`); |
| 92 | // npm 11.21.0 refuses a version the registry already holds, scanned or not. |
| 93 | if (versions.has(file.version)) |
| 94 | return new CommandError({ |
| 95 | message: [ |
| 96 | 'npm failed: npm error code E403', |
| 97 | `npm error 403 403 Forbidden - PUT ${NPM}/@composio%2fcore - You cannot publish over the previously published versions: ${file.version}.`, |
| 98 | ].join('\n'), |
| 99 | exitCode: 1, |
| 100 | }); |
| 101 | uploads++; |
| 102 | await record( |
| 103 | file.version, |
| 104 | file.bytes, |
| 105 | args.find(arg => arg.startsWith('--tag='))!.slice('--tag='.length) |
| 106 | ); |
| 107 | } |
| 108 | if (command === 'dist-tag' && target === 'add') tags[args[3]] = args[2].split('@').at(-1)!; |
| 109 | if (command === 'dist-tag' && target === 'rm') delete tags[args[3]]; |
| 110 | return ''; |
| 111 | }).pipe( |
| 112 | Effect.flatMap(result => |
| 113 | typeof result === 'string' ? Effect.succeed(result) : Effect.fail(result) |
| 114 | ) |
| 115 | ), |
| 116 | }); |
| 117 | const cli = Layer.mergeAll(npmCli, actionsEnv); |
| 118 | const fetchAt = |
| 119 | (now: () => number): Fetch => |
| 120 | async input => { |
| 121 | clock = now; |
| 122 | const url = String(input); |
| 123 | const respond = (data: unknown) => new Response(JSON.stringify(data)); |
| 124 | const notFound = new Response('{}', { status: 404 }); |
| 125 | const scanned = (version: string) => |
| 126 | versions.has(version) && now() >= (options.scanUntil ?? 0); |
| 127 | if (url.startsWith(ID_TOKEN_REQUEST_URL)) return respond({ value: 'id-token' }); |
| 128 | if (url === `${NPM}/-/npm/v1/oidc/token/exchange/package/@composio%2fcore`) |
| 129 | return respond({ token: 'exchanged-token' }); |
| 130 | if (url === `${NPM}/-/package/%40composio%2Fcore/dist-tags`) return respond(tags); |
| 131 | const attestation = /\/-\/npm\/v1\/attestations\/%40composio%2Fcore@(.+)$/.exec(url); |
| 132 | if (attestation) |
| 133 | return scanned(attestation[1]) |
| 134 | ? respond({ |
| 135 | attestations: [ |
| 136 | { |
| 137 | predicateType: 'https://slsa.dev/provenance/v1', |
| 138 | bundle: { dsseEnvelope: { payload: provenance.get(attestation[1]) } }, |
| 139 | }, |
| 140 | ], |
| 141 | }) |
| 142 | : notFound; |
| 143 | const tarball = /\/@composio\/core\/-\/core-(.+)\.tgz$/.exec(url); |
| 144 | if (tarball) return new Response((versions.get(tarball[1]) ?? new Uint8Array()).slice()); |
| 145 | const document = /^https:\/\/registry\.npmjs\.org\/%40composio%2Fcore\/(.+)$/.exec(url); |
| 146 | if (document && semver.valid(document[1])) |
| 147 | return scanned(document[1]) |
| 148 | ? respond({ |
| 149 | name: '@composio/core', |
| 150 | version: document[1], |
| 151 | dist: { tarball: `${NPM}/@composio/core/-/core-${document[1]}.tgz` }, |
| 152 | }) |
| 153 | : notFound; |
| 154 | if (document) return document[1] in tags ? respond({ version: tags[document[1]] }) : notFound; |
| 155 | throw new Error(`Unexpected registry request: ${url}`); |
| 156 | }; |
| 157 | const fetcher = fetchAt(() => 0); |
| 158 | const stage = async (file: Distribution) => { |
| 159 | pending.set(file.file, file); |
| 160 | // The earlier run was batch 8's, so its release-only tag is still in place. |
| 161 | if (options.uploaded) await record(file.version, options.uploaded, 'sdk-release-8'); |
| 162 | }; |
| 163 | return { |
| 164 | cli, |
| 165 | fetcher, |
| 166 | fetchAt, |
| 167 | commands, |
| 168 | tags, |
| 169 | stage, |
| 170 | uploads: () => uploads, |
| 171 | elapsed: () => clock(), |
| 172 | }; |
| 173 | } |
| 174 | |
| 175 | const tarballOf = async (release: ReleaseBatch): Promise<Distribution> => { |
| 176 | const version = release.plan.releases[0].newVersion; |
| 177 | const bytes = new TextEncoder().encode(`reviewed tarball ${version}`); |
| 178 | return { |
| 179 | language: 'typescript', |
| 180 | name: '@composio/core', |
| 181 | version, |
| 182 | file: `composio-core-${version}.tgz`, |
| 183 | bytes, |
| 184 | sha256: await runRelease(sha256Hex(bytes)), |
| 185 | }; |
| 186 | }; |
| 187 | |
| 188 | /** One release through every stage, with the production handlers and fakes at the edges. */ |
| 189 | async function rehearse(release: ReleaseBatch, tags: Record<string, string>) { |
| 190 | const beta = release.plan.choices.channel === 'beta'; |
| 191 | const github = fakeController({ beta }); |
| 192 | const ended: ReleaseState[] = []; |
| 193 | const cwd = await resolvePath('.'); |
| 194 | |
| 195 | // 1. request: an active member's request plans the batch. |
| 196 | const planned = await runRelease( |
| 197 | executeReleaseRequest( |
| 198 | Effect.succeed({ |
| 199 | channel: release.plan.choices.channel, |
| 200 | typescript: 'auto', |
| 201 | python: 'skip', |
| 202 | }), |
| 203 | { |
| 204 | actor: 'requester', |
| 205 | runId: release.id, |
| 206 | sourceSha: base, |
| 207 | issueNumber: release.issue, |
| 208 | appSlug: 'sdk-release-app', |
| 209 | }, |
| 210 | () => Effect.succeed(release.plan) |
| 211 | ), |
| 212 | github.fetcher |
| 213 | ); |
| 214 | expect(planned.outcome).toMatchObject({ |
| 215 | _tag: 'Planned', |
| 216 | batch: { id: release.id, plan: release.plan }, |
| 217 | }); |
| 218 | ended.push( |
| 219 | await runRelease( |
| 220 | openReleasePull(cwd, release, { files: beta ? betaFiles : releaseFiles }), |
| 221 | github.fetcher |
| 222 | ) |
| 223 | ); |
| 224 | |
| 225 | // 2. review: the merged, approved release PR is tagged. |
| 226 | ended.push( |
| 227 | await runRelease( |
| 228 | Effect.flatMap(readReleasePull(9), ({ pull, batch: committed }) => |
| 229 | authorizeSourceTag(pull, committed, 'sdk-release-app') |
| 230 | ), |
| 231 | github.fetcher |
| 232 | ) |
| 233 | ); |
| 234 | expect(github.writes.at(-1)).toEqual({ |
| 235 | method: 'POST', |
| 236 | path: '/git/refs', |
| 237 | body: { ref: 'refs/tags/sdk-release/8', sha: head }, |
| 238 | }); |
| 239 | |
| 240 | // 3. publish: the reviewed tarball reaches npm, and a re-run uploads nothing. |
| 241 | const registry = npmRegistry(tags); |
| 242 | const tarball = await tarballOf(release); |
| 243 | await registry.stage(tarball); |
| 244 | const publish = publishNpm(cwd, release, [tarball]).pipe(Effect.provide(registry.cli)); |
| 245 | await runRelease(publish, registry.fetcher); |
| 246 | const firstRun = [...registry.commands]; |
| 247 | await runRelease(publish, registry.fetcher); |
| 248 | ended.push( |
| 249 | await runRelease( |
| 250 | stageOutcome( |
| 251 | 'publish', |
| 252 | { candidates: 'success', 'publish-npm': 'success' }, |
| 253 | Option.none(), |
| 254 | Effect.succeed({ _tag: 'Published', versions: [] } as ReleaseState), |
| 255 | '77' |
| 256 | ) |
| 257 | ) |
| 258 | ); |
| 259 | return { ended, firstRun, registry, tarball }; |
| 260 | } |
| 261 | |
| 262 | describe('release rehearsal', () => { |
| 263 | test('a stable release goes from request to npm latest, and a re-run changes nothing', async () => { |
| 264 | const { ended, firstRun, registry } = await rehearse(batch, { latest: '1.0.0' }); |
| 265 | expect(ended.map(state => state._tag)).toEqual(['InReview', 'Tagged', 'Published']); |
| 266 | expect(firstRun).toEqual([ |
| 267 | `publish ${await resolvePath('sdk-publication/typescript/composio-core-1.0.1.tgz')} --ignore-scripts --provenance --access=public --tag=sdk-release-8`, |
| 268 | 'dist-tag add @composio/core@1.0.1 latest', |
| 269 | 'dist-tag rm @composio/core sdk-release-8', |
| 270 | ]); |
| 271 | expect(registry.commands).toEqual(firstRun); |
| 272 | expect(registry.tags).toEqual({ latest: '1.0.1' }); |
| 273 | }); |
| 274 | |
| 275 | test('a beta reaches npm next and never reads or moves latest', async () => { |
| 276 | const { ended, registry } = await rehearse(betaBatch, { |
| 277 | latest: '1.0.0', |
| 278 | next: '0.1.36-next.13', |
| 279 | }); |
| 280 | expect(ended.map(state => state._tag)).toEqual(['InReview', 'Tagged', 'Published']); |
| 281 | expect(registry.tags).toEqual({ latest: '1.0.0', next: '1.0.1-beta.8' }); |
| 282 | expect(registry.commands.some(command => command.includes('latest'))).toBe(false); |
| 283 | }); |
| 284 | |
| 285 | test('provenance from another ref fails the publication before latest moves', async () => { |
| 286 | const registry = npmRegistry({ latest: '1.0.0' }, { ref: 'refs/heads/next' }); |
| 287 | const tarball = await tarballOf(batch); |
| 288 | await registry.stage(tarball); |
| 289 | await expect( |
| 290 | runRelease( |
| 291 | publishNpm(await resolvePath('.'), batch, [tarball]).pipe(Effect.provide(registry.cli)), |
| 292 | registry.fetcher |
| 293 | ) |
| 294 | ).rejects.toThrow('was built by https://github.com/ComposioHQ/composio'); |
| 295 | expect(registry.tags.latest).toBe('1.0.0'); |
| 296 | expect(registry.commands.some(command => command.startsWith('dist-tag'))).toBe(false); |
| 297 | }); |
| 298 | |
| 299 | test('provenance that describes other bytes fails the publication', async () => { |
| 300 | const registry = npmRegistry({ latest: '1.0.0' }, { otherBytes: true }); |
| 301 | const tarball = await tarballOf(batch); |
| 302 | await registry.stage(tarball); |
| 303 | await expect( |
| 304 | runRelease( |
| 305 | publishNpm(await resolvePath('.'), batch, [tarball]).pipe(Effect.provide(registry.cli)), |
| 306 | registry.fetcher |
| 307 | ) |
| 308 | ).rejects.toThrow('provenance describes other bytes than the reviewed tarball'); |
| 309 | }); |
| 310 | |
| 311 | test('a re-run during npm’s 15-minute scan waits it out and moves latest without a second upload', async () => { |
| 312 | const tarball = await tarballOf(batch); |
| 313 | const registry = npmRegistry( |
| 314 | { latest: '1.0.0' }, |
| 315 | { uploaded: tarball.bytes, scanUntil: 15 * MINUTE } |
| 316 | ); |
| 317 | await registry.stage(tarball); |
| 318 | await runOnTestClock( |
| 319 | publishNpm(await resolvePath('.'), batch, [tarball]).pipe(Effect.provide(registry.cli)), |
| 320 | registry.fetchAt |
| 321 | ); |
| 322 | expect(registry.uploads()).toBe(0); |
| 323 | expect(registry.commands).toEqual([ |
| 324 | `publish ${await resolvePath('sdk-publication/typescript/composio-core-1.0.1.tgz')} --ignore-scripts --provenance --access=public --tag=sdk-release-8`, |
| 325 | 'dist-tag add @composio/core@1.0.1 latest', |
| 326 | 'dist-tag rm @composio/core sdk-release-8', |
| 327 | ]); |
| 328 | expect(registry.tags).toEqual({ latest: '1.0.1' }); |
| 329 | expect(registry.elapsed()).toBeGreaterThanOrEqual(15 * MINUTE); |
| 330 | }); |
| 331 | |
| 332 | test('a re-run whose earlier upload differs from the reviewed tarball fails once npm serves it', async () => { |
| 333 | const tarball = await tarballOf(batch); |
| 334 | const registry = npmRegistry( |
| 335 | { latest: '1.0.0' }, |
| 336 | { uploaded: new TextEncoder().encode('other tarball'), scanUntil: MINUTE } |
| 337 | ); |
| 338 | await registry.stage(tarball); |
| 339 | await expect( |
| 340 | runOnTestClock( |
| 341 | publishNpm(await resolvePath('.'), batch, [tarball]).pipe(Effect.provide(registry.cli)), |
| 342 | registry.fetchAt |
| 343 | ) |
| 344 | ).rejects.toThrow(`Registry content conflict: @composio/core@1.0.1/${tarball.file}`); |
| 345 | expect(registry.elapsed()).toBeLessThan(3 * MINUTE); |
| 346 | expect(registry.tags.latest).toBe('1.0.0'); |
| 347 | expect(registry.commands.some(command => command.startsWith('dist-tag'))).toBe(false); |
| 348 | }); |
| 349 | |
| 350 | test('every state a stage can end in is reached by a production handler', async () => { |
| 351 | const reached = new Set<StateName>(); |
| 352 | for (const { ended } of [ |
| 353 | await rehearse(batch, { latest: '1.0.0' }), |
| 354 | await rehearse(betaBatch, { next: '0.1.36-next.13' }), |
| 355 | ]) |
| 356 | for (const state of ended) reached.add(state._tag); |
| 357 | const review = async (options: Parameters<typeof fakeController>[0]) => { |
| 358 | const { fetcher } = fakeController(options); |
| 359 | return runRelease( |
| 360 | Effect.flatMap(readReleasePull(9), ({ pull, batch: committed }) => |
| 361 | authorizeSourceTag(pull, committed, 'sdk-release-app') |
| 362 | ), |
| 363 | fetcher |
| 364 | ); |
| 365 | }; |
| 366 | reached.add((await review({ closedUnmerged: true }))._tag); |
| 367 | reached.add((await review({ reviewer: 'requester' }))._tag); |
| 368 | const request = async ( |
| 369 | options: Parameters<typeof fakeController>[0], |
| 370 | sourceSha: string, |
| 371 | parse: Effect.Effect<never, string> | undefined, |
| 372 | releases = batch.plan |
| 373 | ) => { |
| 374 | const { fetcher } = fakeController(options); |
| 375 | return runRelease( |
| 376 | Effect.map( |
| 377 | executeReleaseRequest( |
| 378 | parse ?? Effect.succeed({ channel: 'stable', typescript: 'auto', python: 'skip' }), |
| 379 | { |
| 380 | actor: 'requester', |
| 381 | runId: '8', |
| 382 | sourceSha, |
| 383 | issueNumber: 8, |
| 384 | appSlug: 'sdk-release-app', |
| 385 | }, |
| 386 | () => Effect.succeed(releases) |
| 387 | ), |
| 388 | result => result.outcome |
| 389 | ), |
| 390 | fetcher |
| 391 | ); |
| 392 | }; |
| 393 | reached.add((await request({}, base, Effect.fail('Use the form')))._tag as StateName); |
| 394 | reached.add( |
| 395 | (await request({ openRelease: 'sdk-release/batch-7' }, base, undefined))._tag as StateName |
| 396 | ); |
| 397 | reached.add((await request({}, head, undefined))._tag as StateName); |
| 398 | reached.add( |
| 399 | (await request({}, base, undefined, { ...batch.plan, releases: [] }))._tag as StateName |
| 400 | ); |
| 401 | reached.add( |
| 402 | ( |
| 403 | await runRelease( |
| 404 | stageOutcome('request', { plan: 'cancelled' }, Option.none(), Effect.never, '77') |
| 405 | ) |
| 406 | )._tag |
| 407 | ); |
| 408 | const ends = new Set<StateName>([ |
| 409 | ...Object.values(STAGES).flatMap(stage => stage.to as ReadonlyArray<StateName>), |
| 410 | 'Failed', |
| 411 | ]); |
| 412 | expect([...reached].toSorted()).toEqual([...ends].toSorted()); |
| 413 | }); |
| 414 | }); |