diff --git a/apps/api/src/domain/control-plane.ts b/apps/api/src/domain/control-plane.ts index 19ef3ac..4f400a8 100644 --- a/apps/api/src/domain/control-plane.ts +++ b/apps/api/src/domain/control-plane.ts @@ -1056,6 +1056,11 @@ export class ControlPlane { ['awaiting_approval', 'ready', 'deploying', 'unknown'].includes(r.status), ); const deliveryConfig = project.deliveryConfig as { environmentId?: string } | undefined; + const environmentId = release?.environmentId ?? deliveryConfig?.environmentId; + const environment = environmentId ? await tx.get('environment', environmentId) : undefined; + const live = environment?.currentReleaseId + ? await tx.get('release', environment.currentReleaseId) + : undefined; // Test and build evidence count only under the protected policy they were recorded with, as // a release requires; after the policy changes, the run verifies again instead of stalling. const policy = verificationPolicyDigest(project); @@ -1078,8 +1083,12 @@ export class ControlPlane { release, verificationEvidenceId: current('verification'), buildEvidenceId: current('build'), - environmentId: release?.environmentId ?? deliveryConfig?.environmentId, + environmentId, integrationBaseCommit: evidence.find(e => e.type === 'integration')?.targetCommit, + // What the environment runs now: a release's risk covers everything since, including + // commits people pushed to the target branch directly. + deployedCommit: + live?.status === 'healthy' && live.projectId === run.projectId ? live.sourceCommit : undefined, }, }; }); @@ -1112,6 +1121,7 @@ export class ControlPlane { changedPaths: string[]; migration: boolean; permissionsExpanded: boolean; + partial?: boolean; }, ) { if (actor.kind !== 'verifier') throw new DomainError('FORBIDDEN', 'Independent verifier required', 403); @@ -1134,6 +1144,7 @@ export class ControlPlane { changedPaths: [...new Set([...previous.changedPaths, ...classification.changedPaths])], migration: previous.migration || classification.migration, permissionsExpanded: previous.permissionsExpanded || classification.permissionsExpanded, + partial: !!(previous.partial || classification.partial), } : classification; await tx.put('checkpoint', touch(cp)); diff --git a/apps/api/src/domain/policy.ts b/apps/api/src/domain/policy.ts index 67be568..7f25947 100644 --- a/apps/api/src/domain/policy.ts +++ b/apps/api/src/domain/policy.ts @@ -34,12 +34,14 @@ export function releaseRisk(input: { changedPaths?: string[]; migration?: boolean; permissionsExpanded?: boolean; + partial?: boolean; evidenceComplete: boolean; preauthorized: boolean; }) { const reasons: string[] = []; if (!input.evidenceComplete) reasons.push('Missing trusted change classification'); if (!input.preauthorized) reasons.push('Environment is not preauthorized for automatic release'); + if (input.partial) reasons.push('The environment runs code of unknown source; not every change could be classified'); if (input.migration) reasons.push('Database migration'); if (input.permissionsExpanded) reasons.push('Permission expansion'); for (const path of input.changedPaths ?? []) { diff --git a/apps/api/src/domain/releases.ts b/apps/api/src/domain/releases.ts index f165a61..104d6d3 100644 --- a/apps/api/src/domain/releases.ts +++ b/apps/api/src/domain/releases.ts @@ -292,7 +292,13 @@ export class Releases { ? await tx.get('checkpoint', run.checkpointId) : undefined; const classification = checkpoint?.trustedClassification as - | { changedPaths: string[]; migration: boolean; permissionsExpanded: boolean; sourceCommit: string } + | { + changedPaths: string[]; + migration: boolean; + permissionsExpanded: boolean; + partial?: boolean; + sourceCommit: string; + } | undefined; const riskReasons = releaseRisk({ ...classification, diff --git a/apps/api/src/http.ts b/apps/api/src/http.ts index 116642f..23dfdba 100644 --- a/apps/api/src/http.ts +++ b/apps/api/src/http.ts @@ -121,6 +121,7 @@ const classificationBody = z changedPaths: z.array(z.string().min(1).max(4096)).max(10000), migration: z.boolean(), permissionsExpanded: z.boolean(), + partial: z.boolean().optional(), }) .strict(); const meterBody = z diff --git a/apps/worker/src/common.ts b/apps/worker/src/common.ts index 413078c..35e8341 100644 --- a/apps/worker/src/common.ts +++ b/apps/worker/src/common.ts @@ -56,6 +56,8 @@ export interface RunContext { environmentId?: string; /** The target branch commit the run integrated onto. */ integrationBaseCommit?: string; + /** The commit of the release the environment runs now, if any. */ + deployedCommit?: string; }; } export interface ActivityOutcome { diff --git a/apps/worker/src/verifier.ts b/apps/worker/src/verifier.ts index 61f5a55..b7bc48c 100644 --- a/apps/worker/src/verifier.ts +++ b/apps/worker/src/verifier.ts @@ -11,6 +11,8 @@ import { executeCommand, isolatedVerifierExecutor, MergeConflictError, + pocComponentNames, + pocComponentsToBuild, RuntimeError, safeIdentifier, validateVerificationProfile, @@ -343,31 +345,74 @@ async function candidateFor( return { candidate, expected }; } +/** What a release changes where it deploys. */ +interface ReleaseChanges { + paths: string[]; + /** Part of what the environment runs is of unknown source, so `paths` may miss changes. */ + partial: boolean; + /** The files that differ between a commit and the release; undefined when the repository lacks it. */ + since: (commit: string) => Promise; +} + +/** + * Everything the release changes: what the target branch gained with this run, and whatever + * differs from the commits the environment runs now, which takes in commits people pushed to the + * branch directly and integrations that were never deployed. + */ +async function releaseChanges( + session: Session, + integrated: Verified, + deployed: string[], + unknown = false, +): Promise { + const { git } = session; + const diffs = new Map>(); + const since = (commit: string) => { + if (!diffs.has(commit)) + diffs.set( + commit, + git + .hasCommit(integrated.workspace, commit) + .then(known => (known ? git.changedPaths(integrated.workspace, commit, integrated.commit) : undefined)), + ); + return diffs.get(commit)!; + }; + const paths = new Set(await git.changedPaths(integrated.workspace, integrated.baseCommit, integrated.commit)); + let partial = unknown; + for (const commit of new Set(deployed)) { + const changed = await since(commit); + if (!changed) partial = true; + for (const file of changed ?? []) paths.add(file); + } + return { paths: [...paths].sort(), partial, since }; +} + +/** Records what the release changes; the control plane weighs the release's risk from it. */ +async function classify(session: Session, integrated: Verified, changes: ReleaseChanges) { + await session.keeper.ensure(); + await session.api.request(`/internal/runs/${session.input.runId}/classification`, { + leaseToken: session.leaseToken, + sourceCommit: integrated.commit, + changedPaths: changes.paths, + migration: integrated.migrationRequired || changes.paths.some(file => /(^|\/)migrations?(\/|$)/i.test(file)), + permissionsExpanded: changes.paths.some(file => /(^|\/)(security|auth|infra)(\/|$)/i.test(file)), + partial: changes.partial, + }); + await session.keeper.ensure(); +} + /** Builds the integrated commit when the project delivers; otherwise the integration completed the run. */ async function deliver(session: Session, integrated: Verified): Promise { const delivery = session.context.project.deliveryConfig; if (!delivery) return { status: 'complete' }; if (session.run.stage !== 'build') await session.transition('build', `Integrated ${short(integrated.commit)}; building an immutable image`); - const changedPaths = await session.git.changedPaths(integrated.workspace, integrated.baseCommit, integrated.commit); - await session.keeper.ensure(); - await session.api.request(`/internal/runs/${session.input.runId}/classification`, { - leaseToken: session.leaseToken, - sourceCommit: integrated.commit, - changedPaths, - migration: integrated.migrationRequired || changedPaths.some(file => /(^|\/)migrations?(\/|$)/i.test(file)), - permissionsExpanded: changedPaths.some(file => /(^|\/)(security|auth|infra)(\/|$)/i.test(file)), - }); - await session.keeper.ensure(); + const deployed = session.context.delivery?.deployedCommit; const localPoc = localPocForProject(readLocalPocBinding(session.config.localPocBindingFile), session.context.project); if (localPoc) { if (session.config.sandboxMode !== 'host' || delivery.environmentId !== localPoc.environmentId) throw new RuntimeError('LOCAL_POC_POLICY', 'POC delivery requires its bound local execution and environment'); const profile = await loadPocProfile(localPoc.profileFile); - // Shared server/internal changes rebuild the coherent set. Web-only changes retain the two existing consumers. - const apiOnly = changedPaths.every(file => - /^(src\/|tests\/frontend\/|tools\/drosync\/Cargo\.(toml|lock)$)/.test(file), - ); const { release, evidence } = await buildPocRelease( { profile, @@ -375,10 +420,28 @@ async function deliver(session: Session, integrated: Verified): Promise /(^|\/)(migrations?|store|auth|security)(\/|$)/i.test(file)), + // The components can run different commits. The cluster says which, in the snapshot the + // release is later checked against; the risk and what to build follow from them. + plan: async running => { + const commits = Object.values(running).filter((commit): commit is string => !!commit); + const changes = await releaseChanges( + session, + integrated, + deployed ? [deployed, ...commits] : commits, + pocComponentNames.some(component => !running[component]), + ); + await classify(session, integrated, changes); + const { components, reasons } = await pocComponentsToBuild(running, changes.since); + for (const [component, reason] of Object.entries(reasons)) + await session.transition('build', `Building ${component} too: ${reason}`); + return { + components, + rollbackCompatible: + !changes.partial && + !integrated.migrationRequired && + !changes.paths.some(file => /(^|\/)(migrations?|store|auth|security)(\/|$)/i.test(file)), + }; + }, signal: session.keeper.signal, onProgress: message => session.transition('build', message).then(() => undefined), // A flaky link to the cluster shows in the timeline instead of only as a slow build. @@ -401,6 +464,7 @@ async function deliver(session: Session, integrated: Verified): Promise --request-timeout=5s get --raw /readyz` 反复探测这条链路。 -## 有人直接改 `poc` 分支时 +## 有人直接改 `poc` 分支或 POC 集群时 - 运行实现期间有人推了 `poc`:合入时 Claude 的提交合并到最新的 `poc` 上,合并结果重新跑一遍受保护检查后才推送;冲突按修复轮处理。 - 运行合入之后有人推了 `poc`:构建照常进行,构建的是这个运行合入的提交,时间线会说明 `poc` 已经前进;更新的提交由之后的运行部署。只有 `poc` 被强推、不再包含这个提交时,运行才停在「仓库」等待(`POC_SOURCE_REWRITTEN`),这时取消运行,需要的话重新提交愿望。不要强推 `poc`:进行中的运行合入时会把删掉的提交重新带回来。 +- 发布的风险按「POC 当前运行的代码」到这次发布之间的全部变化计算,直接推到 `poc` 的改动同样会触发审批(迁移、鉴权、Dockerfile、依赖等)。 +- 每次发布在 Deployment 上记下组件镜像构建自哪个提交和镜像本身。构建前按这些记录逐个判断:worker、coordinator 运行的代码与本次源码之间只有网页文件不同时沿用原镜像,否则重新构建。没有记录、记录已被回滚覆盖、或镜像被人用 `kubectl set image` 改过的组件视为来源不明:会重新构建,并且这次发布必须人工审批、不自动回滚。 +- 因此升级到这个版本后的第一次 POC 发布会重建全部三个组件,并等待一次人工审批:此前 WishForge 只重建过 api,worker 和 coordinator 仍运行接入 POC 之前的镜像。 diff --git a/packages/contracts/src/entities.ts b/packages/contracts/src/entities.ts index b836862..a14d85b 100644 --- a/packages/contracts/src/entities.ts +++ b/packages/contracts/src/entities.ts @@ -38,6 +38,8 @@ export interface TrustedClassification { changedPaths: string[]; migration: boolean; permissionsExpanded: boolean; + /** Part of what the environment runs is of unknown source, so the changes since are not all known. */ + partial?: boolean; } /** A portable checkpoint as stored for a run; `checkpoint` is the worker's PortableCheckpoint. */ diff --git a/packages/runtime/src/git.ts b/packages/runtime/src/git.ts index f148a8f..b8287e7 100644 --- a/packages/runtime/src/git.ts +++ b/packages/runtime/src/git.ts @@ -279,6 +279,11 @@ export class GitWorkspace { if (ref !== 'HEAD') requireTargetRef(ref); return requireCommit(await this.git(repository, ['rev-parse', '--verify', `${ref}^{commit}`])); } + async hasCommit(repository: string, commit: string): Promise { + requireCommit(commit); + const result = await this.execute(this.command(repository, ['cat-file', '-e', `${commit}^{commit}`])); + return result.exitCode === 0; + } async changedPaths(workspace: string, baseCommit: string, sourceCommit: string): Promise { requireCommit(baseCommit); requireCommit(sourceCommit); diff --git a/packages/runtime/src/poc-build.ts b/packages/runtime/src/poc-build.ts index 9f24df5..2419e3b 100644 --- a/packages/runtime/src/poc-build.ts +++ b/packages/runtime/src/poc-build.ts @@ -14,7 +14,14 @@ import { type PocImages, type PocRelease, } from './poc-release.js'; -import { pocComponents, pocComponentNames, profileDigest, type PocProfile, type PocComponent } from './poc-profile.js'; +import { + pocApiOnlyPath, + pocComponents, + pocComponentNames, + profileDigest, + type PocProfile, + type PocComponent, +} from './poc-profile.js'; import { pocReleaseDigest } from './poc-runner.js'; export interface PocBuildEvidence { @@ -30,6 +37,10 @@ export interface PocBuildEvidence { retainedImages: Partial; bundleDigest: string; } +export interface PocBuildPlan { + components: PocComponent[]; + rollbackCompatible: boolean; +} export interface PocBuildInput { profile: PocProfile; workspace: string; @@ -38,12 +49,57 @@ export interface PocBuildInput { stateDirectory: string; components?: PocComponent[]; rollbackCompatible?: boolean; + /** + * Decides what to build from the commit each component runs now (absent when unknown), read in + * the snapshot the release later checks the cluster against; replaces the two settings above. + */ + plan?: (running: Partial>) => Promise; signal?: AbortSignal; onProgress?: (message: string) => void | Promise; /** How often to repeat requests the cluster API leaves unanswered, and whom to tell. */ patience?: Patience; } +/** + * The api is always built. Worker and coordinator keep their images while the commit those were + * built from differs from the source in web files alone; one of unknown source, or built from a + * commit the repository does not have, is built again. Says why each of them is built. + */ +export async function pocComponentsToBuild( + running: Partial>, + changedSince: (commit: string) => Promise, +): Promise<{ components: PocComponent[]; reasons: Partial> }> { + const components: PocComponent[] = ['api']; + const reasons: Partial> = {}; + for (const component of pocComponentNames) { + if (component === 'api') continue; + const commit = running[component]; + let reason: string | undefined; + if (!commit) reason = 'what it runs is of unknown source'; + else { + const changed = await changedSince(commit); + const file = changed?.find(path => !pocApiOnlyPath(path)); + if (!changed) reason = `it runs ${commit.slice(0, 12)}, which the repository does not have`; + else if (file) reason = `${file} changed since ${commit.slice(0, 12)}, which it runs`; + } + if (reason) { + components.push(component); + reasons[component] = reason; + } + } + return { components, reasons }; +} + +function requireComponents(components: PocComponent[]) { + if ( + !components.length || + new Set(components).size !== components.length || + components.some(c => !pocComponentNames.includes(c)) + ) + throw Error('Invalid POC build components'); + return components; +} + interface BuildPod { metadata: { uid: string; labels?: Record }; status?: { @@ -161,13 +217,7 @@ export async function buildPocRelease( requireCommit(input.sourceCommit); safeIdentifier(input.releaseId); if (!/^[a-z0-9][a-z0-9-]{0,62}$/.test(input.releaseId)) throw Error('Invalid POC release ID'); - const components = input.components ?? [...pocComponentNames]; - if ( - !components.length || - new Set(components).size !== components.length || - components.some(c => !pocComponentNames.includes(c)) - ) - throw Error('Invalid POC build components'); + if (input.components) requireComponents(input.components); const adapter = new PocDeploymentAdapter( input.profile, path.join(input.stateDirectory, 'deployments'), @@ -223,6 +273,18 @@ export async function buildPocRelease( `poc has moved on to ${tip.slice(0, 12)}; building ${input.sourceCommit.slice(0, 12)}, which this run integrated, while the newer commits deploy with a later run`, ); } + const running: Partial> = {}; + for (const component of pocComponentNames) { + const commit = baseline.deployments[component].sourceCommit; + if (commit) running[component] = commit; + } + const plan = input.plan + ? await input.plan(running) + : { + components: input.components ?? [...pocComponentNames], + rollbackCompatible: input.rollbackCompatible ?? false, + }; + const components = requireComponents(plan.components); const archive = path.join(buildDirectory, 'source.tar.gz'); await command('git', ['archive', '--format=tar.gz', `--output=${archive}`, input.sourceCommit]); if ((await stat(archive)).size > 512 * 1024 * 1024) throw Error('POC source archive exceeds 512 MiB'); @@ -360,7 +422,7 @@ export async function buildPocRelease( images, expectedImages: baseline.images, expectedTemplateDigests: baseline.templateDigests, - rollbackCompatible: input.rollbackCompatible ?? false, + rollbackCompatible: plan.rollbackCompatible, }); evidence.completedAt = new Date().toISOString(); evidence.bundleDigest = pocReleaseDigest(release); diff --git a/packages/runtime/src/poc-profile.ts b/packages/runtime/src/poc-profile.ts index 855852d..34a4323 100644 --- a/packages/runtime/src/poc-profile.ts +++ b/packages/runtime/src/poc-profile.ts @@ -24,6 +24,9 @@ export const pocComponents = { repository: 'drosync-coordinator', }, } as const; +/** Web files, which only the api image includes; a change anywhere else may reach every component. */ +export const pocApiOnlyPath = (file: string) => + /^(src\/|tests\/frontend\/|tools\/drosync\/Cargo\.(toml|lock)$)/.test(file); const absolutePath = z .string() diff --git a/packages/runtime/src/poc-release.ts b/packages/runtime/src/poc-release.ts index 5c8b09b..7bab000 100644 --- a/packages/runtime/src/poc-release.ts +++ b/packages/runtime/src/poc-release.ts @@ -61,7 +61,17 @@ interface Deployment { export interface PocSnapshot { images: PocImages; templateDigests: z.infer; - deployments: Record; + deployments: Record< + PocComponent, + { + uid: string; + resourceVersion: string; + generation: number; + ready: boolean; + /** The commit its image was built from, while it runs the image a release put there. */ + sourceCommit?: string; + } + >; } export type PocJournalState = 'prepared' | 'applying' | 'verifying' | 'succeeded' | 'rolling_back' | 'rolled_back' | 'needs_reconciliation'; @@ -238,11 +248,17 @@ export class PocDeploymentAdapter { const item = await this.deployment(component); images[component] = digestImage.parse(this.image(component, item)); templateDigests[component] = pocTemplateDigest(component, item); + const annotations = item.spec.template.metadata?.annotations ?? {}; + const sourceCommit = annotations[`${annotationPrefix}/source-commit`] ?? ''; deployments[component] = { uid: item.metadata.uid, resourceVersion: item.metadata.resourceVersion, generation: item.metadata.generation, ready: this.ready(item), + // A person's `kubectl set image` leaves the annotations as they were; the image tells. + ...(/^[0-9a-f]{40}$/.test(sourceCommit) && annotations[`${annotationPrefix}/image`] === images[component] + ? { sourceCommit } + : {}), }; } return { images, templateDigests, deployments }; @@ -309,6 +325,7 @@ export class PocDeploymentAdapter { for (const [name, value] of Object.entries({ 'release-id': release.id, 'source-commit': rollback ? 'unknown-restored-baseline' : release.sourceCommit, + image: nextImage, 'attempted-source-commit': release.sourceCommit, 'profile-digest': release.profileDigest, operation: rollback ? 'rollback' : 'deploy', diff --git a/packages/runtime/test/poc-release.test.ts b/packages/runtime/test/poc-release.test.ts index c524388..8216533 100644 --- a/packages/runtime/test/poc-release.test.ts +++ b/packages/runtime/test/poc-release.test.ts @@ -3,7 +3,7 @@ import { tmpdir } from 'node:os'; import path from 'node:path'; import { afterEach, describe, expect, it } from 'vitest'; import { KubectlError, PocDeploymentAdapter, unanswered, writePocJson, type PocRelease } from '../src/poc-release.js'; -import { buildPocRelease, createPocBuildPod, type PocBuildInput } from '../src/poc-build.js'; +import { buildPocRelease, createPocBuildPod, pocComponentsToBuild, type PocBuildInput } from '../src/poc-build.js'; import { PocRunnerDeploymentAdapter, pocReleaseDigest } from '../src/poc-runner.js'; import type { ReleaseManifest } from '../src/types.js'; import { @@ -608,7 +608,13 @@ describe('the POC build and release on a flaky link to the cluster', () => { }); }); -describe('what a POC build builds after people pushed to the branch', () => { +describe('what a POC build builds after people changed the branch or the cluster', () => { + /** Leaves a component as a release does: running `image`, built from `commit`. */ + const annotate = (item: any, commit: string, image: string) => + Object.assign(item.spec.template.metadata.annotations, { + 'wishforge.d-robotics.cc/source-commit': commit, + 'wishforge.d-robotics.cc/image': image, + }); it('builds what the run integrated after people pushed onto it, and says so', async () => { const c = await cluster(); c.branch.tip = 'e'.repeat(40); @@ -626,4 +632,62 @@ describe('what a POC build builds after people pushed to the branch', () => { await expect(c.build()).rejects.toMatchObject({ code: 'POC_SOURCE_REWRITTEN' }); expect(c.count('create')).toBe(0); }); + it('knows what each component was built from, until its image changes', async () => { + const f = await fixture(); + expect((await f.adapter.snapshot()).deployments.api.sourceCommit).toBeUndefined(); + await f.adapter.deploy(release()); + for (const c of pocComponentNames) + expect((await f.adapter.snapshot()).deployments[c].sourceCommit).toBe('a'.repeat(40)); + // A person sets an image by hand: the annotations stay as they were, but no longer vouch for it. + f.state.worker.spec.template.spec.containers[0].image = images('c').worker; + expect((await f.adapter.snapshot()).deployments.worker.sourceCommit).toBeUndefined(); + // Restored by a rollback, a component runs an image of unknown source again. + const g = await fixture(); + g.failures.loseReceipt = true; + expect((await g.adapter.deploy(release())).state).toBe('rolled_back'); + expect(g.state.coordinator.spec.template.metadata.annotations['wishforge.d-robotics.cc/image']).toBe( + images('a').coordinator, + ); + expect((await g.adapter.snapshot()).deployments.coordinator.sourceCommit).toBeUndefined(); + }); + it('plans the build from what each component runs, in the snapshot the release is checked against', async () => { + const c = await cluster(); + // The worker runs what a release built from b…; a person set the coordinator's image by hand since. + annotate(c.state.worker, 'b'.repeat(40), images('a').worker); + annotate(c.state.coordinator, 'b'.repeat(40), images('f').coordinator); + let running: Partial> | undefined; + const { release, evidence } = await c.build({ + components: undefined, + plan: async seen => { + running = seen; + return { components: ['api', 'coordinator'], rollbackCompatible: true }; + }, + }); + expect(running).toEqual({ worker: 'b'.repeat(40) }); + expect(Object.keys(evidence.components).sort()).toEqual(['api', 'coordinator']); + expect(evidence.retainedImages).toEqual({ worker: images('a').worker }); + expect(release).toMatchObject({ rollbackCompatible: true, expectedImages: images('a') }); + }); + it('builds worker and coordinator again unless they differ from the source in web files alone', async () => { + const changed: Record = { + ['1'.repeat(40)]: ['src/app.tsx', 'tests/frontend/app.test.ts'], + ['2'.repeat(40)]: ['src/app.tsx', 'internal/worker/run.go'], + }; + const since = async (commit: string) => changed[commit]; + expect(await pocComponentsToBuild({ worker: '1'.repeat(40), coordinator: '1'.repeat(40) }, since)).toEqual({ + components: ['api'], + reasons: {}, + }); + expect(await pocComponentsToBuild({ worker: '2'.repeat(40), coordinator: '3'.repeat(40) }, since)).toEqual({ + components: ['api', 'worker', 'coordinator'], + reasons: { + worker: `internal/worker/run.go changed since ${'2'.repeat(12)}, which it runs`, + coordinator: `it runs ${'3'.repeat(12)}, which the repository does not have`, + }, + }); + expect(await pocComponentsToBuild({}, since)).toEqual({ + components: ['api', 'worker', 'coordinator'], + reasons: { worker: 'what it runs is of unknown source', coordinator: 'what it runs is of unknown source' }, + }); + }); }); diff --git a/tests/control-plane.test.ts b/tests/control-plane.test.ts index 1939ea8..924b09d 100644 --- a/tests/control-plane.test.ts +++ b/tests/control-plane.test.ts @@ -5,7 +5,7 @@ import { ControlPlane } from '../apps/api/src/domain/control-plane.js'; import { Connections } from '../apps/api/src/domain/connections.js'; import { Releases } from '../apps/api/src/domain/releases.js'; import { AgentBundles } from '../apps/api/src/domain/agent-bundles.js'; -import { digest } from '../apps/api/src/domain/policy.js'; +import { digest, releaseRisk } from '../apps/api/src/domain/policy.js'; import { deliverable } from './helpers/delivery.js'; const owner: Actor = { tenantId: 'alpha', userId: 'owner', role: 'owner', kind: 'human' }; @@ -139,13 +139,14 @@ describe('tenant-scoped durable task invariants', () => { await tx.put('checkpoint', checkpoint); await tx.put('run', { ...run, checkpointId: checkpoint.id, verifiedCommit: commit }); }); - const classify = (sourceCommit: string, changedPaths: string[], migration: boolean) => + const classify = (sourceCommit: string, changedPaths: string[], migration: boolean, partial = false) => control.classify(verifier, runId, { leaseToken: leased.lease!.token, sourceCommit, changedPaths, migration, permissionsExpanded: false, + partial, }); const stored = async () => (await db.transaction('alpha', tx => tx.get('checkpoint', 'checkpoint-1')))! @@ -154,6 +155,15 @@ describe('tenant-scoped durable task invariants', () => { // A resumed build that diffed from a later base sees less of what the target branch gained. await classify(commit, ['src/app.ts'], false); expect(await stored()).toMatchObject({ migration: true, changedPaths: ['migrations/001.sql', 'src/app.ts'] }); + // Once part of what the environment ran was of unknown source, the commit's changes stay incomplete. + await classify(commit, ['src/app.ts'], false, true); + await classify(commit, ['src/app.ts'], false); + expect(await stored()).toMatchObject({ migration: true, partial: true }); + expect(releaseRisk({ ...(await stored()), evidenceComplete: true, preauthorized: true })).toEqual([ + 'The environment runs code of unknown source; not every change could be classified', + 'Database migration', + 'Protected change: migrations/001.sql', + ]); // Another commit is classified on its own. await db.transaction('alpha', async tx => { const run = (await tx.get('run', runId))!; diff --git a/tests/local-loop.test.ts b/tests/local-loop.test.ts index 7a7e606..35bae8e 100644 --- a/tests/local-loop.test.ts +++ b/tests/local-loop.test.ts @@ -12,7 +12,16 @@ import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promis import os from 'node:os'; import path from 'node:path'; import { promisify } from 'node:util'; -import type { Actor, CheckpointRecord, Evidence, Lesson, Project, Run } from '@wishforge/contracts'; +import type { + Actor, + CheckpointRecord, + Environment, + Evidence, + Lesson, + Project, + Release, + Run, +} from '@wishforge/contracts'; import { executeCommand, type CommandExecutor, type HostCheckSandbox, type ProviderStart } from '@wishforge/runtime'; import { ManagedProvider } from '../packages/runtime/src/providers/base.js'; import { ControlPlane } from '../apps/api/src/domain/control-plane.js'; @@ -640,6 +649,68 @@ describe('local main loop', () => { expect(reclassified?.trustedClassification).toMatchObject({ migration: true }); expect(reclassified?.trustedClassification?.changedPaths).toContain('migrations/002-tags.sql'); }); + it('weighs the release risk of everything the environment has not run yet, including direct pushes', async () => { + const project = await createProject(); + const deployed = await git(remote, 'rev-parse', 'main'); + await store.transaction(TENANT, async tx => { + const current = (await tx.get('project', project.id))!; + await tx.put('project', { + ...current, + deliveryConfig: { + imageRepository: 'registry.local/greeting', + architecture: 'linux/amd64', + environmentId: 'environment-1', + dockerfile: 'Dockerfile', + containerPort: 3000, + healthPath: '/health', + smokeChecks: [], + }, + }); + // The environment runs the project as it was before the person's push below. + const stamp = new Date().toISOString(); + const entity = { tenantId: TENANT, version: 1, createdAt: stamp, updatedAt: stamp, projectId: project.id }; + await tx.put('release', { + ...entity, + id: 'release-live', + environmentId: 'environment-1', + status: 'healthy', + sourceCommit: deployed, + } as Release); + await tx.put('environment', { ...entity, id: 'environment-1', currentReleaseId: 'release-live' } as Environment); + }); + // A person pushes a migration straight to the target branch; the agent then only touches the greeting. + const person = path.join(root, 'person'); + await git(root, 'clone', remote, person); + await mkdir(path.join(person, 'migrations')); + await writeFile(path.join(person, 'migrations', '001-notes.sql'), 'create table notes (body text);\n'); + await writeFile(path.join(person, 'Dockerfile'), 'FROM scratch\n'); + await git(person, 'add', '.'); + await git(person, 'commit', '-m', 'Add a notes table'); + await git(person, 'push', 'origin', 'main'); + const classification = (runId: string) => + store.transaction(TENANT, async tx => { + const run = (await tx.get('run', runId))!; + return (await tx.get('checkpoint', run.checkpointId!))?.trustedClassification; + }); + const runId = await submit(project.id, 'Make the greeting say hello world', 'wish-direct-0001'); + const session = await provision(runId, writeGreeting('hello world', 'Updated the greeting.')); + expect(await drive(session)).toMatchObject({ status: 'release' }); + expect(await classification(runId)).toMatchObject({ + migration: true, + partial: false, + changedPaths: ['Dockerfile', 'greeting.txt', 'migrations/001-notes.sql'], + }); + + // The environment runs a commit the repository does not have, as after a rewrite of the branch. + await store.transaction(TENANT, async tx => { + const live = (await tx.get('release', 'release-live'))!; + await tx.put('release', { ...live, sourceCommit: 'f'.repeat(40) }); + }); + const second = await submit(project.id, 'Make the greeting say hello world again', 'wish-direct-0002'); + const next = await provision(second, writeGreeting('hello world again', 'Updated the greeting.')); + expect(await drive(next)).toMatchObject({ status: 'release' }); + expect(await classification(second)).toMatchObject({ partial: true, changedPaths: ['greeting.txt'] }); + }); it('repeats the checks when a build continues after the protected policy changed', async () => { const project = await createProject(); await store.transaction(TENANT, async tx => {