Files
Agent-Workbench/scripts/live-ag/network-observation.mjs
T

48 lines
3.9 KiB
JavaScript

// Independent observation after AG is sealed. TLS only: no HTTP, model request or key access.
import fs from 'node:fs';
import path from 'node:path';
import tls from 'node:tls';
import { lookup, Resolver } from 'node:dns/promises';
import { createHash, randomUUID } from 'node:crypto';
import { fileURLToPath } from 'node:url';
import assert from 'node:assert/strict';
const sealed = JSON.parse(fs.readFileSync('.qa/live-purge-ag/summary.json'));
assert.equal(sealed.originalStateAuditResult.success, true);
assert.ok(fs.existsSync('.qa/live-purge-ag/FINAL-MANIFEST.json'));
const frozen = JSON.parse(fs.readFileSync('.qa/live-purge-ag/current.json'));
const endpoint = new URL(frozen.connection.baseUrl);
assert.equal(endpoint.protocol, 'https:'); assert.equal(endpoint.username, ''); assert.equal(endpoint.password, '');
const hostname = endpoint.hostname, port = Number(endpoint.port || 443);
const output = path.resolve('.qa/ag-network-' + randomUUID()); fs.mkdirSync(output);
const source = fs.readFileSync(fileURLToPath(import.meta.url));
fs.writeFileSync(path.join(output, 'observer.mjs'), source, { flag: 'wx' });
const startedAt = new Date().toISOString(), resolver = new Resolver({ timeout: 5000, tries: 1 });
const errorView = error => ({ name: error.name, code: error.code ?? null, syscall: error.syscall ?? null, message: error.message });
const dnsResults = await Promise.allSettled([lookup(hostname, { all: true }), resolver.resolve4(hostname, { ttl: true }), resolver.resolve6(hostname, { ttl: true })]);
const dns = Object.fromEntries(dnsResults.map((row, index) => [['system', 'ipv4', 'ipv6'][index],
row.status === 'fulfilled' ? { success: true, values: row.value } : { success: false, error: errorView(row.reason) }]));
const addresses = [...new Set([...(dns.system.values ?? []).map(row => row.address),
...(dns.ipv4.values ?? []).map(row => row.address), ...(dns.ipv6.values ?? []).map(row => row.address)])];
const probes = await Promise.all([{ label: 'hostname', host: hostname }, ...addresses.map(host => ({ label: 'resolved-address', host }))].map(target => new Promise(resolve => {
const started = Date.now(); let settled = false, socket;
const finish = value => { if (settled) return; settled = true; clearTimeout(timer); socket?.destroy(); resolve({ ...target, elapsedMs: Date.now() - started, ...value }); };
const timer = setTimeout(() => finish({ success: false, error: { code: 'OBSERVATION_ENDED', message: 'This TLS observation ended after 8000 ms; no product setting was changed.' } }), 8000);
try {
socket = tls.connect({ host: target.host, servername: hostname, port, rejectUnauthorized: true, autoSelectFamily: true }, () => {
const certificate = socket.getPeerCertificate();
finish({ success: socket.authorized, remoteAddress: socket.remoteAddress, remoteFamily: socket.remoteFamily,
protocol: socket.getProtocol(), certificateFingerprint256: certificate.fingerprint256 ?? null });
});
socket.once('error', error => finish({ success: false, error: errorView(error) }));
} catch (error) { finish({ success: false, error: errorView(error) }); }
})));
const result = { schemaVersion: 1, startedAt, finishedAt: new Date().toISOString(), hostname, port,
executable: process.execPath, executableSha256: createHash('sha256').update(fs.readFileSync(process.execPath)).digest('hex'),
observerSha256: createHash('sha256').update(source).digest('hex'), dns, probes,
proxyEnvironmentPresent: { http: !!process.env.HTTP_PROXY, https: !!process.env.HTTPS_PROXY, noProxy: !!process.env.NO_PROXY },
httpRequests: 0, modelRequests: 0, keysReadOrResaved: false, productConfigurationChanged: false,
scope: 'Fresh process DNS/TLS observation only. This does not recreate the host environment or address selection at the failed requests and does not prove sustained connectivity.' };
fs.writeFileSync(path.join(output, 'result.json'), JSON.stringify(result, null, 2) + '\n', { flag: 'wx' });
console.log(JSON.stringify({ output, ...result }));