Files
Genarrative/apps/ai-game-creator-shell/scripts/agent-runtime-real-e2e/harness/process.mjs
T
k88936 f4294660d2 退役AGC项目对话斜杠命令与终端swarm chat入口:真实E2E、脚本与门禁清理
- 删除 scripts/agent-swarm-test-chat.mjs、agent-runtime-deterministic-playable-e2e.mjs、deterministic-lane-defense-provider.mjs
- 删除 user-input、supervisor-autonomous-playable-lane-defense、supervisor-swarm-autonomous-chat、static-isolated 与 collaboration-policy-mixed 套件
- 删除仅服务终端入口的交互式 CLI harness 管道与其进程清理、残留校验逻辑
- 清理 collaboration-assertions、evidence 模板与校验、self-test 中随之失效的混合与静态隔离断言及占位字段
- 移除 check-config.mjs 中钉住已删脚本、命令与套件的断言
- 删除 root 与应用 package.json 的 agc:test*、agc:chat、agc:swarm、各 *-real-e2e 与 test:chat* 脚本
- game-creator-config-wizard.mjs 改为从 channel-identity.mjs 取应用标识
2026-09-23 11:13:03 +08:00

218 lines
6.5 KiB
JavaScript

import {
assert,
codedError,
hashValue,
throwIfShutdownRequested,
} from '../assertions/core.mjs';
import { fs, path, spawn, withLoopbackNoProxy } from '../dependencies.mjs';
import {
activeCommandChildren,
appRoot,
commandOutputLimit,
manifestPath,
state,
} from '../runtime-state.mjs';
import { isSupervisorSwarmTransientRetrySuite } from '../suites/supervisor-swarm.mjs';
import { isIsolatedRunnerSuite } from './reporting.mjs';
export async function prepareCliBinary() {
const cargo = process.platform === 'win32' ? 'cargo.exe' : 'cargo';
await runProcess(
cargo,
['build', '--quiet', '--manifest-path', manifestPath],
{
cwd: appRoot,
timeoutMs: 15 * 60 * 1000,
},
);
const metadata = await runProcess(
cargo,
[
'metadata',
'--format-version',
'1',
'--no-deps',
'--manifest-path',
manifestPath,
],
{ cwd: appRoot, timeoutMs: 120_000 },
);
const parsed = JSON.parse(metadata.stdout);
const executable = path.join(
parsed.target_directory,
'debug',
`genarrative-ai-game-creator-shell${process.platform === 'win32' ? '.exe' : ''}`,
);
const binary = await fs.stat(executable).catch(() => null);
assert(binary?.isFile(), 'cli-binary-missing');
return executable;
}
export async function runCli(args, options = {}) {
assert(Boolean(state.cliBinary), 'cli-binary-not-ready');
assert(Boolean(state.runtimeConfigDir), 'runtime-config-dir-not-ready');
if (
isIsolatedRunnerSuite() &&
state.options?.configDir &&
path.resolve(state.runtimeConfigDir) ===
path.resolve(state.options.configDir)
) {
state.isolatedRunner.sourceConfigCliCallCount += 1;
}
return runProcess(
state.cliBinary,
[...args, '--config-dir', state.runtimeConfigDir],
{
cwd: appRoot,
timeoutMs: options.timeoutMs ?? 60_000,
stdin: options.stdin,
allowNonZero: options.allowNonZero ?? false,
env: buildCliChildEnvironment(),
},
);
}
export function buildCliChildEnvironment() {
const environment = {
...process.env,
NO_COLOR: '1',
RUST_BACKTRACE: '0',
};
// The deterministic playable suite copies its account fixture into the
// sibling isolated AppData directory. Set the path explicitly here so
// every CLI and the Runner it launches use the isolated copy, even if the
// parent harness environment was restored or changed after setup.
if (state.isolatedRunner.platformSessionFixturePath) {
environment.GENARRATIVE_AGC_PLATFORM_SESSION_FIXTURE =
state.isolatedRunner.platformSessionFixturePath;
}
return isSupervisorSwarmTransientRetrySuite()
? withLoopbackNoProxy(environment)
: environment;
}
export function safeProcessFailureDiagnostic({
code = null,
signal = null,
error = null,
stdout = '',
stderr = '',
} = {}) {
const stdoutText = Buffer.isBuffer(stdout)
? stdout.toString('utf8')
: String(stdout);
const stderrText = Buffer.isBuffer(stderr)
? stderr.toString('utf8')
: String(stderr);
const combined = `${error?.message ?? ''}\n${stdoutText}\n${stderrText}`;
const processErrorCode = /^[A-Z0-9_]+$/u.test(error?.code ?? '')
? error.code
: 'none';
const closeSignal = /^[A-Z0-9]+$/u.test(signal ?? '') ? signal : 'none';
let failureKind = 'closed';
if (
processErrorCode === 'ENOSPC' ||
/(?:\bENOSPC\b|No space left on device|os error 28)/iu.test(combined)
) {
failureKind = 'enospc';
} else if (processErrorCode !== 'none') {
failureKind = 'process-error';
} else if (closeSignal !== 'none') {
failureKind = 'signal';
} else if (Number.isInteger(code) && code !== 0) {
failureKind = 'nonzero-exit';
}
return {
failureKind,
exitCode: Number.isInteger(code) ? String(code) : 'none',
signal: closeSignal,
processErrorCode,
stderrChars: [...stderrText].length,
stderrSha256: hashValue(stderrText) ?? 'none',
};
}
export function codedProcessError(code, details) {
const error = codedError(code, details?.error);
error.safeFailureDiagnostic = safeProcessFailureDiagnostic(details);
return error;
}
export async function runProcess(
program,
args,
{ cwd, timeoutMs, stdin, allowNonZero = false, env = null },
) {
throwIfShutdownRequested();
return new Promise((resolve, reject) => {
const child = spawn(program, args, {
cwd,
env: env ?? { ...process.env, NO_COLOR: '1', RUST_BACKTRACE: '0' },
stdio: [stdin === undefined ? 'ignore' : 'pipe', 'pipe', 'pipe'],
});
activeCommandChildren.add(child);
let stdout = Buffer.alloc(0);
let stderr = Buffer.alloc(0);
let timedOut = false;
const timer = setTimeout(() => {
timedOut = true;
child.kill('SIGKILL');
}, timeoutMs);
child.stdout.on('data', (chunk) => {
state.transcriptScanner?.scan('stdout', chunk);
state.projectPathTranscriptScanner?.scan('stdout', chunk);
state.formalConfigPathTranscriptScanner?.scan('stdout', chunk);
stdout = appendBounded(stdout, chunk, commandOutputLimit);
});
child.stderr.on('data', (chunk) => {
state.transcriptScanner?.scan('stderr', chunk);
state.projectPathTranscriptScanner?.scan('stderr', chunk);
state.formalConfigPathTranscriptScanner?.scan('stderr', chunk);
stderr = appendBounded(stderr, chunk, commandOutputLimit);
});
child.on('error', (error) => {
clearTimeout(timer);
activeCommandChildren.delete(child);
reject(
codedProcessError('process-spawn-failed', {
error,
stdout,
stderr,
}),
);
});
child.on('close', (code, signal) => {
clearTimeout(timer);
activeCommandChildren.delete(child);
const result = {
stdout: stdout.toString('utf8'),
stderr: stderr.toString('utf8'),
code,
signal,
};
if (timedOut) {
reject(codedProcessError('process-timeout', result));
} else if (state.shutdownSignal && !state.cleanupInProgress) {
reject(
codedProcessError(
`interrupted-${state.shutdownSignal.toLowerCase()}`,
result,
),
);
} else if (code !== 0 && !allowNonZero) {
reject(codedProcessError('cli-command-failed', result));
} else {
resolve(result);
}
});
if (stdin !== undefined) child.stdin.end(stdin);
});
}
export function appendBounded(current, chunk, limit) {
const combined = Buffer.concat([current, chunk]);
return combined.length <= limit
? combined
: combined.subarray(combined.length - limit);
}