import { randomUUID } from 'node:crypto' import { readFile } from 'node:fs/promises' import path from 'node:path' import { composeAndCreateGeneratedRun, createGeneratedArtifact, downloadGeneratedArtifact, type ImmutableJsonObject, } from '../packages/application/src/index' import { LocalArtifactStorage } from '../packages/artifacts/src/index' import type { CanonicalPromptRequest, PlaybookMetadata, PlaybookSpecification, } from '../packages/composer/src/index' import { canonicalJson, loadBuiltInPlaybookRecords, sha256, } from '../packages/content/src/index' import { closeDatabase, DrizzleGeneratedArtifactStore, DrizzleGeneratedRunStore, DrizzleWorkspaceAuthorizationLookup, getSqlClient, } from '../packages/db/src/index' import { importRepositoryProfile } from '../packages/repository-intel/src/index' const artifactId = 'cdbe379c-7dce-44bf-bf8b-d6a764d703d1' const idempotencyKey = 'milestone-zero-host-persistence-validation-v2' async function main() { const artifactRoot = process.env.ARTIFACT_ROOT if (!process.env.DATABASE_URL) throw new Error('DATABASE_URL is required') if (!artifactRoot) throw new Error('ARTIFACT_ROOT is required') const repositoryProfile = importRepositoryProfile( await readFile( path.resolve('examples/repository-profiles/example-profile.yaml'), ), 'yaml', ) const sql = getSqlClient() const [identity] = await sql< { ownerId: string; workspaceId: string; playbookVersionId: string }[] >` select u.id as "ownerId", wm.workspace_id as "workspaceId", pv.id as "playbookVersionId" from users u join workspace_memberships wm on wm.user_id = u.id and wm.role = 'owner' join playbooks p on p.slug = 'root-cause-bugfix' join playbook_versions pv on pv.playbook_id = p.id and pv.semantic_version = '1.0.0' where u.status = 'active' order by u.created_at, wm.created_at limit 1 ` if (!identity) throw new Error('A ready owner workspace is required') const records = await loadBuiltInPlaybookRecords() const rootCause = records.find( (record) => record.slug === 'root-cause-bugfix', ) if (!rootCause) throw new Error('The root-cause-bugfix built-in is required') const manifest = rootCause.packageJson as unknown as { metadata: PlaybookMetadata spec: PlaybookSpecification } const inputs = { problemStatement: 'Example value for Problem statement', reproductionClues: '', preserveCompatibility: true, affectedScope: [], } const prompt: CanonicalPromptRequest = { metadata: manifest.metadata, specification: manifest.spec, template: rootCause.templateText, inputs, workMode: 'guided', autonomyLevel: 'verify', repositoryProfile, } const golden = await readFile( path.resolve('examples/rendered-prompts/root-cause-bugfix.md'), 'utf8', ) const dependencies = { store: new DrizzleGeneratedRunStore(), nextId: randomUUID, now: () => new Date(), workspaceAuthorization: new DrizzleWorkspaceAuthorizationLookup(), } const runResult = await composeAndCreateGeneratedRun(dependencies, { prompt, snapshots: { playbook: rootCause.packageJson as unknown as ImmutableJsonObject, repositoryProfile: repositoryProfile as unknown as ImmutableJsonObject, normalizedInput: inputs, policy: { autonomyLevel: 'verify', conditionsResolved: true }, provenance: [], }, lint: { exportReadiness: 'ready', findings: [] }, generatedBy: identity.ownerId, workspaceId: identity.workspaceId, playbookVersionId: identity.playbookVersionId, idempotencyKey, }) if (runResult.run.renderedPrompt !== golden) { throw new Error('Production composition differs from the golden fixture') } const content = new TextEncoder().encode(golden) const artifactResult = await createGeneratedArtifact( { authorization: new DrizzleWorkspaceAuthorizationLookup(), metadata: new DrizzleGeneratedArtifactStore(), storage: new LocalArtifactStorage(artifactRoot), now: () => new Date(), }, { actor: { userId: identity.ownerId }, workspaceId: identity.workspaceId, artifactId, runId: runResult.run.id, artifactType: 'prompt_text', filename: 'root-cause-bugfix.md', mediaType: 'text/markdown; charset=utf-8', content, }, ) const downloaded = await downloadGeneratedArtifact( { authorization: new DrizzleWorkspaceAuthorizationLookup(), metadata: new DrizzleGeneratedArtifactStore(), storage: new LocalArtifactStorage(artifactRoot), }, { actor: { userId: identity.ownerId }, workspaceId: identity.workspaceId, artifactId, }, ) const restored = Buffer.from(downloaded.content).toString('utf8') if (restored !== golden || downloaded.artifact.sha256 !== sha256(golden)) { throw new Error('Persisted artifact integrity validation failed') } process.stdout.write( `${canonicalJson({ artifactCreated: artifactResult.created, artifactDigest: downloaded.artifact.sha256, artifactId: downloaded.artifact.id, bytes: downloaded.content.byteLength, runCreated: runResult.created, runDigest: runResult.run.renderDigest, runId: runResult.run.id, status: 'passed', })}\n`, ) } try { await main() } finally { await closeDatabase() }