graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Refuse desktop bundles without update config before upload (0.0.299 regression)
Cursor Agentbf94eef066c7
diff --git a/apps/native/electron/distribute.sh b/apps/native/electron/distribute.shindex 3e31b928..a937f866 100755--- a/apps/native/electron/distribute.sh+++ b/apps/native/electron/distribute.sh@@ -32,6 +32,10 @@ spctl --assess --type execute --verbose=2 "$app"archive="$out/Codegraff-$version-macos-arm64.zip"ditto -c -k --keepParent "$app" "$archive"bun "$here/update-artifacts.cjs" "$version" "$archive" "$out/latest-mac.yml"+# The 0.0.299 zip passed every gate below yet installed an app that never+# checked for updates: its bundle had no app-update.yml. Prove the upload+# bytes carry the feed config before anything else touches this directory.+bun "$here/update-artifacts.cjs" verify-bundle "$app" "$archive"bash "$here/create-dmg.sh" "$app" "$dmg"codesign --sign "$GRAFF_SIGN_IDENTITY" --timestamp "$dmg"xcrun notarytool submit "$dmg" --keychain-profile "$GRAFF_NOTARY_PROFILE" --wait --output-format json > "$out/dmg-notary.json"diff --git a/apps/native/electron/publish-updates.sh b/apps/native/electron/publish-updates.shindex 31ac0ee1..341302b4 100644--- a/apps/native/electron/publish-updates.sh+++ b/apps/native/electron/publish-updates.sh@@ -1,6 +1,7 @@#!/usr/bin/env bash# Attach the complete desktop update set to a draft release. Publish only after CI.set -euo pipefail+here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"tag="${1:?Usage: publish-updates.sh vVERSION distribution-directory}"out="${2:?Provide the notarized distribution directory}"repo=justrach/codegraff@@ -21,6 +22,11 @@ const f=m.files[0];if(f.url!==`Codegraff-${m.version}-macos-arm64.zip`)throw Errconst bytes=fs.readFileSync(path.join(out,f.url));if(bytes.length!==f.size||crypto.createHash("sha512").update(bytes).digest("base64")!==f.sha512)throw Error("Update checksum mismatch");' "$tag" "$out"+# Last gate before upload: the checks above verify the .app directory and the+# manifest against the zip's bytes, but never that the zip installs an app+# able to check for updates. 0.0.299 passed them all with no app-update.yml+# in the bundle, so its updater never ran. Refuse that shape here.+bun "$here/update-artifacts.cjs" verify-bundle "$out/Codegraff.app" "$out/Codegraff-${tag#v}-macos-arm64.zip"assets=("$out/Codegraff-macos-arm64.dmg" "$out/Codegraff-DMG-SHA256SUMS" "$out/Codegraff-${tag#v}-macos-arm64.zip" "$out/latest-mac.yml")gh release upload "$tag" --repo "$repo" "${assets[@]}"echo 'Desktop assets uploaded. Keep the release in draft until all release checks pass.'diff --git a/apps/native/electron/update-artifacts.cjs b/apps/native/electron/update-artifacts.cjsindex 6489c7b4..26515570 100644--- a/apps/native/electron/update-artifacts.cjs+++ b/apps/native/electron/update-artifacts.cjs@@ -2,6 +2,13 @@ const fs = require('node:fs');const path = require('node:path');const crypto = require('node:crypto');const feed = { provider: 'generic', url: 'https://github.com/justrach/codegraff/releases/latest/download/', updaterCacheDirName: 'codegraff-updater' };+// Where updates.cjs looks for its feed: <resources>/app-update.yml, i.e.+// this path under the .app. distribute.sh writes it before signing so it is+// inside the signature; the 0.0.299 bundle shipped without it and its+// updater never ran (updateAvailability reason 'config').+const bundleConfigRel = path.join('Contents', 'Resources', 'app-update.yml');+// ditto --keepParent zips Codegraff.app itself, so this is the member path.+const archiveMember = 'Codegraff.app/' + bundleConfigRel.split(path.sep).join('/');function writeManifest(version, archive, output) {if (!/^\d+\.\d+\.\d+$/.test(version)) throw Error('Only stable versions can enter the desktop update feed.');const url = path.basename(archive);@@ -13,8 +20,77 @@ function writeManifest(version, archive, output) {fs.writeFileSync(output, JSON.stringify(manifest, null, 2) + '\n');return manifest;}+function writeFeedConfig(output) {+ fs.writeFileSync(output, JSON.stringify(feed, null, 2) + '\n');+ return feed;+}+// Read back a feed config and prove it points at this project's release+// feed. Anything unexpected fails closed: a missing file is the 0.0.299+// shape (no update checks at all), and a wrong URL would update from+// somewhere else, which is worse. JSON only: distribute.sh writes this+// file via writeFeedConfig, so anything unparseable was hand-tampered.+function readFeedConfig(file) {+ let raw;+ try {+ raw = fs.readFileSync(file, 'utf8');+ } catch {+ throw Error(`Distribution bundle has no update configuration (${file}); an app without it never checks for updates.`);+ }+ let config;+ try {+ config = JSON.parse(raw);+ } catch {+ throw Error(`Update configuration is not readable (${file}); refusing to ship an app that cannot check for updates.`);+ }+ for (const key of ['provider', 'url', 'updaterCacheDirName']) {+ if (!config || config[key] !== feed[key]) throw Error(`Update configuration ${key} is ${JSON.stringify(config?.[key])} (expected ${JSON.stringify(feed[key])}); refusing to ship an app pointed at the wrong feed.`);+ }+ return config;+}+// Member names out of a zip's central directory. Names only, no extraction,+// no dependencies: publish-updates.sh runs this on the release Mac against+// the exact bytes about to be uploaded. Anything malformed throws rather+// than reporting "not found" on a file we could not actually read.+function zipMembers(zipPath) {+ const bytes = fs.readFileSync(zipPath);+ const EOCD = 0x06054b50, CEN = 0x02014b50;+ if (bytes.length < 22) throw Error(`Update archive is too small to be a zip (${zipPath}).`);+ let eocd = -1;+ const lo = Math.max(0, bytes.length - 22 - 65535);+ for (let i = bytes.length - 22; i >= lo; i--) {+ if (bytes.readUInt32LE(i) === EOCD) { eocd = i; break; }+ }+ if (eocd < 0) throw Error(`Update archive has no zip end record (${zipPath}).`);+ const entries = bytes.readUInt16LE(eocd + 10);+ const cdSize = bytes.readUInt32LE(eocd + 12);+ const cdOff = bytes.readUInt32LE(eocd + 16);+ if (entries === 0xffff || cdSize === 0xffffffff || cdOff === 0xffffffff) throw Error(`Update archive needs zip64, which the pre-publish check does not read (${zipPath}).`);+ if (cdOff + cdSize > bytes.length) throw Error(`Update archive central directory runs past end of file (${zipPath}).`);+ const names = [];+ let at = cdOff;+ for (let n = 0; n < entries; n++) {+ if (at + 46 > bytes.length || bytes.readUInt32LE(at) !== CEN) throw Error(`Update archive central directory is malformed (${zipPath}).`);+ const nameLen = bytes.readUInt16LE(at + 28), extraLen = bytes.readUInt16LE(at + 30), commentLen = bytes.readUInt16LE(at + 32);+ const start = at + 46, end = start + nameLen;+ if (end > bytes.length) throw Error(`Update archive central directory is malformed (${zipPath}).`);+ names.push(bytes.subarray(start, end).toString('utf8'));+ at = end + extraLen + commentLen;+ }+ return names;+}+// The pre-publish gate publish-updates.sh and distribute.sh both run: the+// .app on disk must carry a valid feed config, and the uploadable zip must+// contain that same config at its installed path. Either half missing is+// the 0.0.299 shape. Returns the parsed feed on success; throws otherwise.+function verifyDistributionBundle(appDir, archive) {+ const config = readFeedConfig(path.join(appDir, bundleConfigRel));+ const members = zipMembers(archive);+ if (!members.includes(archiveMember)) throw Error(`Update archive ${path.basename(archive)} does not contain ${archiveMember}; it would install an app that never checks for updates.`);+ return config;+}if (require.main === module) {- if (process.argv[2] === 'config') fs.writeFileSync(process.argv[3], JSON.stringify(feed, null, 2) + '\n');+ if (process.argv[2] === 'config') writeFeedConfig(process.argv[3]);+ else if (process.argv[2] === 'verify-bundle') verifyDistributionBundle(process.argv[3], process.argv[4]);else writeManifest(...process.argv.slice(2));}-module.exports = { writeManifest, feed };+module.exports = { writeManifest, writeFeedConfig, readFeedConfig, zipMembers, verifyDistributionBundle, bundleConfigRel, archiveMember, feed };diff --git a/apps/native/electron/update-artifacts.test.cjs b/apps/native/electron/update-artifacts.test.cjsindex 32e648af..56c1fbae 100644--- a/apps/native/electron/update-artifacts.test.cjs+++ b/apps/native/electron/update-artifacts.test.cjs@@ -1,7 +1,7 @@const { test, expect } = require('bun:test');const fs = require('node:fs'), os = require('node:os'), path = require('node:path');const { load } = require('js-yaml');-const { writeManifest, feed } = require('./update-artifacts.cjs');+const { writeManifest, writeFeedConfig, readFeedConfig, zipMembers, verifyDistributionBundle, bundleConfigRel, archiveMember, feed } = require('./update-artifacts.cjs');test('release metadata can be read by the updater and identifies the exact archive', () => {const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'graff-update-metadata-'));try {@@ -18,3 +18,112 @@ test('release metadata can be read by the updater and identifies the exact archiexpect(() => writeManifest('1.2.3-beta.1', file, manifest)).toThrow('stable');} finally { fs.rmSync(dir, { recursive: true, force: true }); }});+// Minimal stored (uncompressed) zip writer: local headers, central+// directory, end record. Enough for the pre-publish member check without+// depending on an external zip binary in the test.+function crc32(bytes) {+ let crc = 0xffffffff;+ for (const b of bytes) {+ crc ^= b;+ for (let k = 0; k < 8; k++) crc = (crc & 1) ? (crc >>> 1) ^ 0xedb88320 : crc >>> 1;+ }+ return (crc ^ 0xffffffff) >>> 0;+}+function storedZip(files) {+ const locals = [], centrals = [];+ let offset = 0;+ for (const [name, data] of files) {+ const nameBuf = Buffer.from(name, 'utf8'), payload = Buffer.isBuffer(data) ? data : Buffer.from(data);+ const head = Buffer.alloc(30);+ head.writeUInt32LE(0x04034b50, 0); head.writeUInt16LE(20, 4); head.writeUInt16LE(0, 6); head.writeUInt16LE(0, 8);+ head.writeUInt32LE(crc32(payload), 14); head.writeUInt32LE(payload.length, 18); head.writeUInt32LE(payload.length, 22);+ head.writeUInt16LE(nameBuf.length, 26); head.writeUInt16LE(0, 28);+ locals.push(head, nameBuf, payload);+ const cen = Buffer.alloc(46);+ cen.writeUInt32LE(0x02014b50, 0); cen.writeUInt16LE(20, 4); cen.writeUInt16LE(20, 6);+ cen.writeUInt32LE(crc32(payload), 16); cen.writeUInt32LE(payload.length, 20); cen.writeUInt32LE(payload.length, 24);+ cen.writeUInt16LE(nameBuf.length, 28); cen.writeUInt32LE(offset, 42);+ centrals.push(cen, nameBuf);+ offset += head.length + nameBuf.length + payload.length;+ }+ const cd = Buffer.concat(centrals), end = Buffer.alloc(22);+ end.writeUInt32LE(0x06054b50, 0); end.writeUInt16LE(files.length, 8); end.writeUInt16LE(files.length, 10);+ end.writeUInt32LE(cd.length, 12); end.writeUInt32LE(offset, 16); end.writeUInt16LE(0, 20);+ return Buffer.concat([...locals, cd, end]);+}+function bundleFixture(withConfig) {+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'graff-update-bundle-'));+ const app = path.join(dir, 'Codegraff.app');+ fs.mkdirSync(path.join(app, 'Contents', 'Resources'), { recursive: true });+ if (withConfig) writeFeedConfig(path.join(app, bundleConfigRel));+ return { dir, app };+}+test('the feed config distribute.sh embeds is valid and points at the release feed', () => {+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'graff-update-config-'));+ try {+ const file = path.join(dir, 'app-update.yml');+ writeFeedConfig(file);+ // electron-updater reads YAML; our JSON must parse as YAML too.+ expect(load(fs.readFileSync(file, 'utf8')).url).toBe(feed.url);+ expect(readFeedConfig(file)).toEqual(feed);+ } finally { fs.rmSync(dir, { recursive: true, force: true }); }+});+test('a bundle without update config is refused: the 0.0.299 shape never ships again', () => {+ const { dir, app } = bundleFixture(false);+ try {+ const zip = path.join(dir, 'Codegraff-9.9.9-macos-arm64.zip');+ fs.writeFileSync(zip, storedZip([[archiveMember, '{}']]));+ expect(() => verifyDistributionBundle(app, zip)).toThrow('no update configuration');+ } finally { fs.rmSync(dir, { recursive: true, force: true }); }+});+test('a config pointed at the wrong feed is refused, not shipped', () => {+ const { dir, app } = bundleFixture(false);+ try {+ fs.writeFileSync(path.join(app, bundleConfigRel), JSON.stringify({ ...feed, url: 'https://example.com/evil/' }));+ const zip = path.join(dir, 'Codegraff-9.9.9-macos-arm64.zip');+ fs.writeFileSync(zip, storedZip([[archiveMember, '{}']]));+ expect(() => verifyDistributionBundle(app, zip)).toThrow('wrong feed');+ } finally { fs.rmSync(dir, { recursive: true, force: true }); }+});+test('a zip that does not wrap the configured app is refused', () => {+ const { dir, app } = bundleFixture(true);+ try {+ // A stale or foreign zip: valid config on disk, but the upload bytes+ // would install an app that never checks for updates.+ const zip = path.join(dir, 'Codegraff-9.9.9-macos-arm64.zip');+ fs.writeFileSync(zip, storedZip([['Codegraff.app/Contents/Info.plist', 'plist']]));+ expect(zipMembers(zip)).not.toContain(archiveMember);+ expect(() => verifyDistributionBundle(app, zip)).toThrow('does not contain');+ } finally { fs.rmSync(dir, { recursive: true, force: true }); }+});+test('a configured bundle inside its zip passes the pre-publish gate', () => {+ const { dir, app } = bundleFixture(true);+ try {+ const zip = path.join(dir, 'Codegraff-9.9.9-macos-arm64.zip');+ fs.writeFileSync(zip, storedZip([+ ['Codegraff.app/Contents/Info.plist', 'plist'],+ [archiveMember, fs.readFileSync(path.join(app, bundleConfigRel))],+ ['Codegraff.app/._Contents', 'appledouble'],+ ]));+ expect(zipMembers(zip)).toContain(archiveMember);+ expect(verifyDistributionBundle(app, zip)).toEqual(feed);+ } finally { fs.rmSync(dir, { recursive: true, force: true }); }+});+test('unreadable archives fail closed instead of reporting no update config', () => {+ const { dir, app } = bundleFixture(true);+ try {+ const zip = path.join(dir, 'Codegraff-9.9.9-macos-arm64.zip');+ fs.writeFileSync(zip, 'not a zip at all');+ expect(() => verifyDistributionBundle(app, zip)).toThrow('zip');+ const good = storedZip([[archiveMember, '{}']]);+ fs.writeFileSync(zip, good.subarray(0, good.length - 30)); // truncated end record+ expect(() => verifyDistributionBundle(app, zip)).toThrow('zip');+ } finally { fs.rmSync(dir, { recursive: true, force: true }); }+});+test('distribute.sh and publish-updates.sh both run the pre-publish gate', () => {+ for (const script of ['distribute.sh', 'publish-updates.sh']) {+ const src = fs.readFileSync(path.join(__dirname, script), 'utf8');+ expect(src).toContain('update-artifacts.cjs');+ expect(src).toContain('verify-bundle');+ }+});diff --git a/apps/native/electron/updates.test.cjs b/apps/native/electron/updates.test.cjsindex f7fdac95..2e65ea5a 100644--- a/apps/native/electron/updates.test.cjs+++ b/apps/native/electron/updates.test.cjs@@ -122,3 +122,19 @@ test('in-app settings expose check and the six-hour automatic poll', () => {expect(src).toContain('every six hours');expect(src).toContain('data-desktop-update-automatic');});++test('a packaged app without update config reports config, with it reports available (0.0.299)', () => {+ const fs = require('node:fs'), os = require('node:os'), path = require('node:path');+ const { updateAvailability } = require('./updates.cjs');+ const { writeFeedConfig } = require('./update-artifacts.cjs');+ const resources = fs.mkdtempSync(path.join(os.tmpdir(), 'graff-update-avail-'));+ try {+ const packaged = { app: { isPackaged: true }, resources, env: {}, platform: 'darwin', execPath: '/Applications/Codegraff.app/Contents/MacOS/Codegraff' };+ // The 0.0.299 bundle: packaged and signed, but distribute.sh's config+ // step never landed, so no update check may run.+ expect(updateAvailability(packaged)).toEqual({ available: false, reason: 'config' });+ // The 0.0.298/0.0.300 shape: the same app with the feed file present.+ writeFeedConfig(path.join(resources, 'app-update.yml'));+ expect(updateAvailability(packaged)).toEqual({ available: true, reason: null });+ } finally { fs.rmSync(resources, { recursive: true, force: true }); }+});