diff --git a/README.md b/README.md index 3b65a5a9c..00f21222e 100644 --- a/README.md +++ b/README.md @@ -194,9 +194,13 @@ explicit managed-name refusals with migration compatibility. 1. `scripts/build.sh` builds the release image (`mosaic-poc-agent:-r`, tag derived from `RELEASE` + the pi version `package-lock.json` resolves, by - `scripts/image-tag.mjs`) with Docker Compose. `package.json` gives pi a range, - never a pin. Before the build, `scripts/pi-fit-image.mjs` runs the fit check - on the pi the lock resolves, so a lock that resolves an unfit pi fails it. + `scripts/image-tag.mjs`) with `docker build -f Containerfile`. `package.json` + gives pi a range, never a pin. Before the build, `scripts/pi-fit-image.mjs` + runs the fit check on the pi the lock resolves, so a lock that resolves an + unfit pi fails it. The check runs on the host's node, which can differ from + the image's (the Containerfile's `FROM`). `compose.yaml` has no build section + and `pull_policy: never`, so a compose run never builds or pulls the image; + it refuses a missing one. 2. On each run, `/opt/mosaic/src/load-contracts.sh` reads the four contract files in fixed order (CONSTITUTION, STANDARDS, SOUL, USER), joins them with clear separators, and writes `/var/lib/mosaic/private/system-prompt.md` in the diff --git a/compose.yaml b/compose.yaml index ec8021551..83e520b43 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,8 +1,10 @@ services: mosaic-agent: - build: - context: . - dockerfile: Containerfile + # #1873 S5: no build section, and never pulled. The image is built only by + # build_image (scripts/common.sh) or the gate's prepareImage, which run the + # image fit step first, so a missing image refuses here instead of compose + # building one that skipped the fit check. + pull_policy: never image: ${MOSAIC_IMAGE_TAG:?MOSAIC_IMAGE_TAG must be set by scripts/load_release (run via scripts/*.sh)} user: "1000:1000" environment: diff --git a/packages/cli/tests/gate-lane-image-base.test.mjs b/packages/cli/tests/gate-lane-image-base.test.mjs index b50958157..86a5ddffd 100644 --- a/packages/cli/tests/gate-lane-image-base.test.mjs +++ b/packages/cli/tests/gate-lane-image-base.test.mjs @@ -44,7 +44,7 @@ test('image receipt records the actual pinned FROM rather than a stale mutable t const calls = []; const invoke = args => { calls.push(args); - if (args[0] === 'compose') return ''; + if (args[0] === 'build') return ''; if (args[2] === 'node:24-bookworm-slim') return JSON.stringify({ RepoDigests: ['node@sha256:' + 'b'.repeat(64)] }); return JSON.stringify(actual); }; @@ -52,7 +52,7 @@ test('image receipt records the actual pinned FROM rather than a stale mutable t const result = prepareImage(root, { grant: 'fixture-only', tempRoot: root }, invoke, () => {}); assert.equal(result.baseDigest, selected); assert.equal(calls.some(args => args[2] === 'node:24-bookworm-slim'), false); - assert.equal(calls.filter(args => args[0] === 'compose').length, 1); + assert.equal(calls.filter(args => args[0] === 'build').length, 1); }); test('a well-formed stale base receipt refuses before any image inspection', t => { diff --git a/packages/cli/tests/gate-lane-image.test.mjs b/packages/cli/tests/gate-lane-image.test.mjs index 10c341d95..9305a101b 100644 --- a/packages/cli/tests/gate-lane-image.test.mjs +++ b/packages/cli/tests/gate-lane-image.test.mjs @@ -39,10 +39,10 @@ test('exclusive image preparation records actual image ID and build wall time an for(const f of ['Containerfile','package.json','package-lock.json'])cpSync(new URL('../../../'+f,import.meta.url),join(root,f)); copyProcessingInputs(root); writeFileSync(join(root,'RELEASE'),'0.0.1\n');for(const d of ['contracts','src','adapters'])mkdirSync(join(root,d)); - const calls=[];const invoke=(args)=>{calls.push(args);if(args[0]==='compose')return '';return JSON.stringify({Id:'sha256:'+'a'.repeat(64),Os:'linux',Architecture:'amd64'});}; + const calls=[];const invoke=(args)=>{calls.push(args);if(args[0]==='build')return '';return JSON.stringify({Id:'sha256:'+'a'.repeat(64),Os:'linux',Architecture:'amd64'});}; assert.throws(()=>image.prepareImage(root,{grant:'',tempRoot:root},invoke),/grant/); - const result=image.prepareImage(root,{grant:'fixture',tempRoot:root},invoke,fitStandIn(calls));assert.equal(result.imageId,'sha256:'+'a'.repeat(64));assert.equal(result.builds,1);assert.ok(result.buildWallMs>=0);assert.ok(calls.some(x=>x.join(' ')==='compose build')); - assert.ok(calls.findIndex(x=>x[0]==='fit')x.join(' ')==='compose build'),'the image fit step runs before the build'); + const result=image.prepareImage(root,{grant:'fixture',tempRoot:root},invoke,fitStandIn(calls));assert.equal(result.imageId,'sha256:'+'a'.repeat(64));assert.equal(result.builds,1);assert.ok(result.buildWallMs>=0);assert.ok(calls.some(x=>x.join(' ')==='build -f Containerfile -t mosaic-poc-agent:0.85.1-r0.0.1 .'),'a plain docker build of the Containerfile under the lock tag (compose.yaml has no build section)'); + const fitAt=calls.findIndex(x=>x[0]==='fit');assert.ok(fitAt>=0&&fitAtx[0]==='build'),'the image fit step runs, before the build'); }); test('cleanup refuses a foreign Docker label and removes only an exact owned project',()=>{ const project='mg197-a-abcdef123456',id='123456789abc';let removed=false,label='foreign'; @@ -77,7 +77,7 @@ test('a tag changed between supervised steps fails the next boundary and suppres }); test('pinned-base rebuilds with identical files have distinct image IDs; cold and warm counters stay separate',t=>{ const {root}=imageFixture(t);let builds=0,id='sha256:'+'a'.repeat(64); - const fake=args=>{if(args[0]==='compose'){builds++;return 'fixture build';}return JSON.stringify({Id:id,Os:'linux',Architecture:'amd64'});}; + const fake=args=>{if(args[0]==='build'){builds++;return 'fixture build';}return JSON.stringify({Id:id,Os:'linux',Architecture:'amd64'});}; const cold=image.prepareImage(root,{grant:'fixture-cold',tempRoot:root},fake,fitStandIn([])); assert.equal(cold.builds,1);assert.ok(cold.buildWallMs>=0);assert.equal(builds,1); for(let i=0;i<3;i++)image.verifyImage(cold,root,()=>({Id:id,Os:'linux',Architecture:'amd64'})); diff --git a/packages/cli/tests/pi-unpin-s5.test.mjs b/packages/cli/tests/pi-unpin-s5.test.mjs index 54e7a3101..3a15a2a74 100644 --- a/packages/cli/tests/pi-unpin-s5.test.mjs +++ b/packages/cli/tests/pi-unpin-s5.test.mjs @@ -105,21 +105,25 @@ test("pi-unpin S5: load_release (build.sh, release.sh, verify.sh, test-release.s assert.match(bad.stderr, /image tag: package-lock\.json has no entry/); }); -test("pi-unpin S5: build_image runs the image fit step first, and an unfit pi never reaches docker compose build", t => { +test("pi-unpin S5: build_image runs the image fit step first, and an unfit pi never reaches the image build", t => { const root = shellCheckout(t); mkdirSync(join(root, "bin")); const log = join(root, "docker.log"); writeFileSync(join(root, "bin", "docker"), `#!/bin/sh\nprintf '%s\\n' "$*" >> "${log}"\n`, { mode: 0o755 }); writeFileSync(join(root, "scripts", "pi-fit-image.mjs"), `import { appendFileSync } from "node:fs";\nif (process.env.FIT === "unfit") { process.stderr.write("pi-fit-image: pi 9.9.9 (the lock's pi) lacks --offline\\n"); process.exit(1); }\nappendFileSync(${JSON.stringify(log)}, "fit\\n");\n`); const env = { PATH: `${join(root, "bin")}:${PATH}` }; - const unfit = sh(root, "build_image; echo after", { ...env, FIT: "unfit" }); + const unfit = sh(root, "load_release && build_image; echo after", { ...env, FIT: "unfit" }); assert.notEqual(unfit.status, 0); assert.equal(unfit.stdout, "", "the calling script stops at the fit step"); assert.match(unfit.stderr, /lacks --offline/); assert.equal(existsSync(log), false, "docker is never called for an unfit pi"); - const fit = sh(root, "build_image", env); + const fit = sh(root, "load_release && build_image", env); assert.equal(fit.status, 0, fit.stderr); - assert.equal(readFileSync(log, "utf8"), "fit\ncompose build\n"); + assert.equal(readFileSync(log, "utf8"), "fit\nbuild -f Containerfile -t mosaic-poc-agent:9.9.9-r0.0.13 .\n"); + // Without load_release there is no tag to build under. + const untagged = spawnSync("bash", ["-c", "source scripts/common.sh && build_image"], { cwd: root, encoding: "utf8", env }); + assert.notEqual(untagged.status, 0); + assert.match(untagged.stderr, /build_image needs MOSAIC_IMAGE_TAG from load_release/); }); test("pi-unpin S5: no script builds the image tag from package.json, and every image build goes through build_image", () => { @@ -130,8 +134,13 @@ test("pi-unpin S5: no script builds the image tag from package.json, and every i } for (const f of ["scripts/build.sh", "scripts/release.sh", "scripts/verify.sh"]) { assert.match(read(f), /^\s*build_image\b/m, `${f} builds through build_image`); - assert.doesNotMatch(read(f), /docker compose build/, `${f} builds without the image fit step`); + assert.doesNotMatch(read(f), /docker (compose )?build/, `${f} builds without the image fit step`); } + // compose.yaml can't build or pull the agent image, so a missing image refuses at compose run + // instead of being built without the fit step. + const compose = read("compose.yaml"); + assert.doesNotMatch(compose, /^\s*build:/m, "compose.yaml has no build section"); + assert.match(compose, /^\s*pull_policy: never$/m); for (const f of ["scripts/test-task.sh", "scripts/test-auth.sh"]) assert.match(read(f), /node scripts\/image-tag\.mjs/, `${f} takes the tag from scripts/image-tag.mjs`); assert.match(read("scripts/mosaic-task.mjs"), /MOSAIC_IMAGE_TAG = tag;/); }); @@ -164,15 +173,15 @@ test("pi-unpin S5: verifyImage accepts the lock-derived tag when package.json ho assert.throws(() => image.verifyImage({ ...receipt, inputDigest: image.imageFingerprint(root) }, root, inspect), /image tag: package-lock\.json has no entry/); }); -test("pi-unpin S5: prepareImage tags from the lock, runs the image fit step before compose build, and a refusing step stops the build", t => { +test("pi-unpin S5: prepareImage tags from the lock, runs the image fit step before the build, and a refusing step stops the build", t => { assert.equal(typeof image.prepareImage, "function"); assert.equal(typeof image.imageFitStep, "function", "lane-image.mjs exports the image fit step"); const root = imageCheckout(t); const calls = []; - const invoke = (args) => { calls.push(args.join(" ")); return args[0] === "compose" ? "" : JSON.stringify({ Id: "sha256:" + "a".repeat(64), Os: "linux", Architecture: "amd64" }); }; + const invoke = (args) => { calls.push(args.join(" ")); return args[0] === "build" ? "" : JSON.stringify({ Id: "sha256:" + "a".repeat(64), Os: "linux", Architecture: "amd64" }); }; const receipt = image.prepareImage(root, { grant: "fixture", tempRoot: root }, invoke, (c) => calls.push(`fit ${c === root}`)); assert.equal(receipt.tag, "mosaic-poc-agent:9.9.9-r0.0.1"); - assert.deepEqual(calls, ["fit true", "compose build", "image inspect mosaic-poc-agent:9.9.9-r0.0.1 --format {{json .}}"]); + assert.deepEqual(calls, ["fit true", "build -f Containerfile -t mosaic-poc-agent:9.9.9-r0.0.1 .", "image inspect mosaic-poc-agent:9.9.9-r0.0.1 --format {{json .}}"]); const stopped = []; assert.throws(() => image.prepareImage(root, { grant: "fixture", tempRoot: root }, (a) => stopped.push(a), () => { throw new Error("unfit"); }), /unfit/); assert.deepEqual(stopped, [], "no build after a refusing fit step"); diff --git a/scripts/common.sh b/scripts/common.sh index 897908bef..f92736ed1 100755 --- a/scripts/common.sh +++ b/scripts/common.sh @@ -46,9 +46,15 @@ load_release() { # and refuses with one line when that pi is unfit or the check can't tell, so a # lock that resolves an unfit pi fails the build. scripts/pi-fit-image.mjs # says why the step runs on the host and not inside the image build. +# compose.yaml has no build section, so this is the only way the scripts build +# the image; compose run refuses a missing one. Call load_release first. build_image() { + if [ -z "${MOSAIC_IMAGE_TAG:-}" ]; then + echo "common: build_image needs MOSAIC_IMAGE_TAG from load_release" >&2 + exit 1 + fi node scripts/pi-fit-image.mjs || exit 1 - docker compose build + docker build -f Containerfile -t "$MOSAIC_IMAGE_TAG" . } # Ensure the configured runtime data directory exists and carries this diff --git a/scripts/pi-fit-image.mjs b/scripts/pi-fit-image.mjs index 1cfe8693a..21a1078bc 100755 --- a/scripts/pi-fit-image.mjs +++ b/scripts/pi-fit-image.mjs @@ -10,6 +10,8 @@ // the image's: npm ci installs both from the same lock entry, and this step refuses unless the // checkout's node_modules holds exactly that entry (version, resolved and integrity, as npm recorded // them in node_modules/.package-lock.json, and the installed package.json's version). +// It runs pi on the host's node, which can be a different version from the image's (the +// Containerfile's FROM). Running it on the image's node is left for later. // // node scripts/pi-fit-image.mjs exit 0: passed; exit 1: one line on stderr, unfit or can't tell import { readFileSync, realpathSync } from "node:fs"; diff --git a/scripts/test-auth.sh b/scripts/test-auth.sh index f1bf6436d..48c2c2ea0 100755 --- a/scripts/test-auth.sh +++ b/scripts/test-auth.sh @@ -36,7 +36,7 @@ CFG="MOSAIC_CONFIG=$SANDBOX/mock-config.json" # release pointer so agent.sh's ensure stays on the no-op fast path REL="$(tr -d '[:space:]' < RELEASE)" -TAG="$(node scripts/image-tag.mjs)" # #1873 S5: the tag the lock gives, never a pinned string +TAG="$(node scripts/image-tag.mjs)" || exit 1 # #1873 S5: the tag the lock gives, never a pinned string printf '{"pointerVersion":1,"release":"%s","imageTag":"%s","activatedAt":"2026-01-01T00:00:00Z"}\n' "$REL" "$TAG" > "$SANDBOX/data/state/active.json" # fixtures: a default-harness credential and two mosaic-managed accounts diff --git a/scripts/test-task.sh b/scripts/test-task.sh index 7b86a405c..06fb5b401 100755 --- a/scripts/test-task.sh +++ b/scripts/test-task.sh @@ -266,7 +266,7 @@ EOF EOF REL="$(tr -d '[:space:]' < RELEASE)" mkdir -p "$SANDBOX/data/state" - TAG="$(node scripts/image-tag.mjs)" # #1873 S5: the tag the lock gives, never a pinned string + TAG="$(node scripts/image-tag.mjs)" || exit 1 # #1873 S5: the tag the lock gives, never a pinned string printf '{"pointerVersion":1,"release":"%s","imageTag":"%s","activatedAt":"2026-01-01T00:00:00Z"}\n' "$REL" "$TAG" > "$SANDBOX/data/state/active.json" scripts/skill.sh install ms-tools >/dev/null 2>&1 scripts/skill.sh activate ms-tools >/dev/null 2>&1 diff --git a/scripts/testing/lane-image.mjs b/scripts/testing/lane-image.mjs index b545fa612..1d4571f16 100644 --- a/scripts/testing/lane-image.mjs +++ b/scripts/testing/lane-image.mjs @@ -40,7 +40,8 @@ export function verifyImage(receipt,checkout,inspect=inspectImage){ // Called by the CLI only while both lanes and the image lock are exclusive. // #1873 S5: the image fit step. The checkout's own scripts/pi-fit-image.mjs runs the fit check on the // pi package-lock.json resolves, as npm ci installed it in the checkout, before the build. It exits -// non-zero with one line when that pi is unfit or the check can't tell. +// non-zero with one line when that pi is unfit or the check can't tell. compose.yaml has no build +// section, so the build below is a plain docker build of the Containerfile under the lock's tag. export function imageFitStep(checkout){ try{execFileSync(process.execPath,[join(checkout,'scripts/pi-fit-image.mjs')],{cwd:checkout,encoding:'utf8',stdio:['ignore','pipe','pipe'],env:{PATH:process.env.PATH,HOME:process.env.HOME,...(process.env.TMPDIR?{TMPDIR:process.env.TMPDIR}:{})}});} catch(e){const line=String(e.stderr??'').trim().split('\n').pop();refuse(`image fit step refused: ${line||'it could not run'}`);} @@ -54,7 +55,7 @@ export function prepareImage(checkout,{grant,tempRoot,out},invoke,fit=imageFitSt const tag=lockTag(checkout); fit(checkout); const run=invoke??((args)=>execFileSync('docker',args,{cwd:checkout,encoding:'utf8',stdio:['ignore','pipe','pipe'],env:{PATH:process.env.PATH,HOME:process.env.HOME,...(process.env.DOCKER_HOST?{DOCKER_HOST:process.env.DOCKER_HOST}:{}),COMPOSE_PROJECT_NAME:'mg197-image-prepare',MOSAIC_IMAGE_TAG:tag,MOSAIC_DATA_ROOT:join(tempRoot,'data'),MOSAIC_PROVIDER:'fixture',MOSAIC_MODEL:'fixture',PI_AUTH_FILE:'/dev/null'}})); - const start=performance.now();let buildOutput;try{buildOutput=run(['compose','build']);}catch(e){if(out)writeFileSync(join(out,'image-build.log'),String(e.stdout??'')+String(e.stderr??''),{mode:0o600,flag:'wx'});throw e;}if(out)writeFileSync(join(out,'image-build.log'),buildOutput,{mode:0o600,flag:'wx'});const buildWallMs=performance.now()-start; + const start=performance.now();let buildOutput;try{buildOutput=run(['build','-f','Containerfile','-t',tag,'.']);}catch(e){if(out)writeFileSync(join(out,'image-build.log'),String(e.stdout??'')+String(e.stderr??''),{mode:0o600,flag:'wx'});throw e;}if(out)writeFileSync(join(out,'image-build.log'),buildOutput,{mode:0o600,flag:'wx'});const buildWallMs=performance.now()-start; if(inputDigest!==imageFingerprint(checkout))refuse('image inputs changed while building'); const actual=JSON.parse(run(['image','inspect',tag,'--format','{{json .}}'])); const receipt={inputDigest,imageId:actual.Id,tag,baseDigest,platform:`${actual.Os}/${actual.Architecture}`,grant,builds:1,buildWallMs,at:new Date().toISOString()};