fix: identify source archive bytes after Git newline conversion

This commit is contained in:
Agent Workbench Dev
2026-11-06 09:34:36 +08:00
parent 2a6554cdc8
commit 3494ae7ab2
5 changed files with 39 additions and 3 deletions
+26 -1
View File
@@ -3,6 +3,7 @@ import path from 'node:path';
import { execFileSync } from 'node:child_process';
import { createHash, randomUUID } from 'node:crypto';
import { fileURLToPath } from 'node:url';
import { inflateRawSync } from 'node:zlib';
// Export the committed program. No running host, user data directory, installed
// application, credentials, build output or historical evidence is consulted.
@@ -12,6 +13,24 @@ const files = ['package.json', 'package-lock.json', 'vite.config.ts', 'tsconfig.
'Start-Workbench.cmd', 'Stop-Workbench.cmd', 'Start-Desktop.cmd'];
const guide = name => /^docs\/[^/]+\.md$/.test(name) || /^docs\/research\/[^/]+\.(?:md|json)$/.test(name);
function archivedFiles(file) {
const buffer = fs.readFileSync(file), end = buffer.lastIndexOf(Buffer.from([0x50, 0x4b, 0x05, 0x06]));
if (end < 0) throw new Error('Source ZIP directory is missing.');
const count = buffer.readUInt16LE(end + 10), result = new Map(); let offset = buffer.readUInt32LE(end + 16);
for (let index = 0; index < count; index++) {
if (buffer.readUInt32LE(offset) !== 0x02014b50) throw new Error('Invalid source ZIP entry.');
const length = buffer.readUInt16LE(offset + 28), name = buffer.subarray(offset + 46, offset + 46 + length).toString('utf8');
const local = buffer.readUInt32LE(offset + 42), start = local + 30 + buffer.readUInt16LE(local + 26) + buffer.readUInt16LE(local + 28);
const compressed = buffer.subarray(start, start + buffer.readUInt32LE(offset + 20)), method = buffer.readUInt16LE(offset + 10);
if (![0, 8].includes(method) || result.has(name)) throw new Error('Unsupported or duplicate source ZIP entry.');
const bytes = method === 8 ? inflateRawSync(compressed) : compressed;
if (bytes.length !== buffer.readUInt32LE(offset + 24)) throw new Error('Source ZIP entry length mismatch.');
if (!name.endsWith('/')) result.set(name, bytes);
offset += 46 + length + buffer.readUInt16LE(offset + 30) + buffer.readUInt16LE(offset + 32);
}
return result;
}
export function packageSource({ root = process.cwd(), outputRoot = path.join(root, 'releases') } = {}) {
root = path.resolve(root); outputRoot = path.resolve(outputRoot);
const git = args => execFileSync('git', args, { cwd: root, windowsHide: true, maxBuffer: 32 * 1024 * 1024 });
@@ -35,9 +54,15 @@ export function packageSource({ root = process.cwd(), outputRoot = path.join(roo
const archive = path.join(outputRoot, name + '.zip'), fd = fs.openSync(archive, 'wx');
try { execFileSync('git', ['archive', '--format=zip', '--prefix=Agent-Workbench-' + version + '-source/', sourceCommit, '--', ...roots], { cwd: root, windowsHide: true, stdio: ['ignore', fd, 'pipe'] }); }
finally { fs.closeSync(fd); }
const archived = archivedFiles(archive), prefix = 'Agent-Workbench-' + version + '-source/';
if (archived.size !== entries.length) throw new Error('Source ZIP file scope differs from the selected committed tree.');
const exported = entries.map(row => {
const bytes = archived.get(prefix + row.path); if (!bytes) throw new Error('Committed source missing from ZIP: ' + row.path);
return { path: row.path, bytes: bytes.length, sha256: createHash('sha256').update(bytes).digest('hex'), gitBytes: row.bytes, gitBlob: row.gitBlob };
});
const result = { kind: 'source-code', version, sourceCommit, sourceTree: git(['rev-parse', sourceCommit + '^{tree}']).toString().trim(),
path: archive, bytes: fs.statSync(archive).size, sha256: createHash('sha256').update(fs.readFileSync(archive)).digest('hex'),
createdAt: new Date().toISOString(), files: entries.map(({ path, bytes, gitBlob }) => ({ path, bytes, gitBlob })),
createdAt: new Date().toISOString(), files: exported, archiveAttributes: 'git archive applies committed .gitattributes, including CRLF for Windows launch scripts; bytes/sha256 identify exported ZIP bytes, gitBytes/gitBlob identify original committed blobs.',
scope: 'Committed program, build scripts, tests, examples, dependency lock, licenses and text guides. No user projects, sessions, API credentials, installed runtimes or historical evidence archives. Build with Node 24, npm ci and npm run build. Historical acceptance drivers may require separately retained evidence or external engines.' };
fs.writeFileSync(path.join(outputRoot, name + '.json'), JSON.stringify(result, null, 2) + '\n', { flag: 'wx' });
return result;
+1 -1
View File
@@ -45,7 +45,7 @@ with zipfile.ZipFile(archive) as bundle:
assert not relative.is_absolute() and '..' not in relative.parts
contents=bundle.read(top+'/'+item['path'])
assert len(contents)==item['bytes']
if manifest['kind']=='source-code':
if manifest['kind']=='source-code' and 'sha256' not in item:
assert hashlib.sha1(b'blob '+str(len(contents)).encode()+b'\\0'+contents).hexdigest()==item['gitBlob']
item['sha256']=hashlib.sha256(contents).hexdigest()
else: