chore(oss): audit the server package and resync the SDK mirrors
Split the 657-line operation service into eor, submit, and verify modules, add the AGENT_REVOKED and KEY_RETIRED codes the Backend already emits, and condense every multi-line comment. Mirror Node, Python, and Go at 2.1.0 with the deep-health 503, non-idempotent retry, rotation grace, and path-encoding fixes. Document which routes and response fields the self-hosted server does not provide. Claude-Session: https://claude.ai/code/session_01Agoc9bRVE9KL7mmWtey9Vq
This commit is contained in:
1 parent
2010175a82
commit
bc6983a62e
364 files changed
+10180
-23349
No files matched your search
@@ -1,65 +1,49 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { spawn } from 'node:child_process';
|
||||
import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
|
||||
import http from 'node:http';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { pathToFileURL } from 'node:url';
|
||||
import { parse } from 'jsonc-parser';
|
||||
import {
|
||||
createFixtureRoot,
|
||||
distUrl,
|
||||
homeEnvironment,
|
||||
installConfig as baseInstallConfig,
|
||||
optionalEnvironment,
|
||||
runPlugin as basePlugin,
|
||||
runProcess as baseProcess,
|
||||
windowsPowerShellPath,
|
||||
writeOptionalJson,
|
||||
} from './harness.mjs';
|
||||
|
||||
export const VALID_PRIVATE_KEY = Buffer.alloc(32).toString('base64url');
|
||||
export const pluginModuleUrl = pathToFileURL(path.resolve('dist/plugins/gemini.js')).href;
|
||||
export const registryModuleUrl = pathToFileURL(path.resolve('dist/plugins/registry.js')).href;
|
||||
export const ioModuleUrl = pathToFileURL(path.resolve('dist/plugins/gemini-io.js')).href;
|
||||
export const installationModuleUrl = pathToFileURL(
|
||||
path.resolve('dist/plugins/gemini-installation.js'),
|
||||
).href;
|
||||
export const cliPath = path.resolve('dist/cli.js');
|
||||
export {
|
||||
VALID_PRIVATE_KEY,
|
||||
cliPath,
|
||||
parseJsonc as parseSettings,
|
||||
readJsoncSettings as readSettings,
|
||||
registryModuleUrl,
|
||||
startApiServer,
|
||||
writeJson as writeSettings,
|
||||
} from './harness.mjs';
|
||||
|
||||
export const pluginModuleUrl = distUrl('plugins/gemini.js');
|
||||
export const ioModuleUrl = distUrl('plugins/gemini-io.js');
|
||||
export const installationModuleUrl = distUrl('plugins/gemini-installation.js');
|
||||
const PLUGIN = { exportName: 'geminiPlugin', moduleUrl: pluginModuleUrl };
|
||||
|
||||
export function runProcess(command, args, env, cwd, input = '', unset = []) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const childEnv = { ...process.env, ...env };
|
||||
for (const key of unset) delete childEnv[key];
|
||||
const child = spawn(command, args, {
|
||||
cwd,
|
||||
env: childEnv,
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
windowsHide: true,
|
||||
});
|
||||
let stdout = '';
|
||||
let stderr = '';
|
||||
child.stdout.on('data', (chunk) => { stdout += chunk; });
|
||||
child.stderr.on('data', (chunk) => { stderr += chunk; });
|
||||
child.once('error', reject);
|
||||
child.once('close', (code) => resolve({ code, stdout, stderr }));
|
||||
child.stdin.end(input);
|
||||
});
|
||||
return baseProcess(command, args, env, cwd, input, { unset });
|
||||
}
|
||||
|
||||
export function runNode(args, env, cwd, input = '', unset = []) {
|
||||
return runProcess(process.execPath, args, env, cwd, input, unset);
|
||||
}
|
||||
|
||||
function windowsPowerShell() {
|
||||
return path.win32.join(
|
||||
process.env.SystemRoot || 'C:\\Windows',
|
||||
'System32',
|
||||
'WindowsPowerShell',
|
||||
'v1.0',
|
||||
'powershell.exe',
|
||||
);
|
||||
}
|
||||
|
||||
export function runGeminiHook(handler, input, fixture, environment = {}) {
|
||||
const env = {
|
||||
HOME: fixture.homeDir,
|
||||
USERPROFILE: fixture.homeDir,
|
||||
...homeEnvironment(fixture),
|
||||
GEMINI_CLI_HOME: fixture.geminiCliHomeOverride ?? '',
|
||||
...environment,
|
||||
};
|
||||
if (process.platform === 'win32') {
|
||||
return runProcess(
|
||||
windowsPowerShell(),
|
||||
windowsPowerShellPath(),
|
||||
[
|
||||
'-NoLogo',
|
||||
'-NoProfile',
|
||||
@@ -75,66 +59,16 @@ export function runGeminiHook(handler, input, fixture, environment = {}) {
|
||||
return runProcess('/bin/bash', ['-c', handler.command], env, fixture.projectDir, input);
|
||||
}
|
||||
|
||||
export async function writeSettings(filePath, value) {
|
||||
await mkdir(path.dirname(filePath), { recursive: true });
|
||||
const contents = typeof value === 'string' ? value : `${JSON.stringify(value, null, 2)}\n`;
|
||||
await writeFile(filePath, contents, { encoding: 'utf-8', mode: 0o600 });
|
||||
}
|
||||
|
||||
export function parseSettings(raw) {
|
||||
const errors = [];
|
||||
const value = parse(raw, errors, {
|
||||
allowTrailingComma: false,
|
||||
disallowComments: false,
|
||||
});
|
||||
assert.deepEqual(errors, []);
|
||||
return value;
|
||||
}
|
||||
|
||||
export async function readSettings(filePath) {
|
||||
const raw = await readFile(filePath, 'utf-8');
|
||||
return { raw, settings: parseSettings(raw) };
|
||||
}
|
||||
|
||||
export function installConfig(fixture, overrides = {}) {
|
||||
return {
|
||||
agentName: 'gemini',
|
||||
orgId: 'org-1',
|
||||
agentId: 'agent-1',
|
||||
privateKey: VALID_PRIVATE_KEY,
|
||||
kid: 'kid-1',
|
||||
token: 'token-1',
|
||||
baseUrl: fixture.baseUrl,
|
||||
guardScriptPath: fixture.guardScriptPath,
|
||||
hookScriptPath: fixture.hookScriptPath,
|
||||
...overrides,
|
||||
};
|
||||
return baseInstallConfig('gemini', fixture, overrides);
|
||||
}
|
||||
|
||||
export async function runPlugin(fixture, method, argument, environment = {}) {
|
||||
const source = `
|
||||
import { geminiPlugin } from ${JSON.stringify(pluginModuleUrl)};
|
||||
const argument = JSON.parse(process.env.ELYDORA_TEST_ARGUMENT);
|
||||
const result = await geminiPlugin[process.env.ELYDORA_TEST_METHOD](argument);
|
||||
if (result !== undefined) console.log(JSON.stringify(result));
|
||||
`;
|
||||
const env = {
|
||||
HOME: fixture.homeDir,
|
||||
USERPROFILE: fixture.homeDir,
|
||||
ELYDORA_TEST_ARGUMENT: JSON.stringify(argument),
|
||||
ELYDORA_TEST_METHOD: method,
|
||||
...environment,
|
||||
};
|
||||
const unset = [];
|
||||
if (fixture.geminiCliHomeOverride === undefined) unset.push('GEMINI_CLI_HOME');
|
||||
else env.GEMINI_CLI_HOME = fixture.geminiCliHomeOverride;
|
||||
return runNode(
|
||||
['--input-type=module', '--eval', source],
|
||||
env,
|
||||
fixture.projectDir,
|
||||
'',
|
||||
unset,
|
||||
);
|
||||
export function runPlugin(fixture, method, argument, environment = {}) {
|
||||
const home = optionalEnvironment('GEMINI_CLI_HOME', fixture.geminiCliHomeOverride);
|
||||
return basePlugin(PLUGIN, fixture, method, argument, {
|
||||
env: { ...environment, ...home.env },
|
||||
unset: home.unset,
|
||||
});
|
||||
}
|
||||
|
||||
export async function createFixture({
|
||||
@@ -142,38 +76,23 @@ export async function createFixture({
|
||||
settings,
|
||||
explicitGeminiHome = true,
|
||||
} = {}) {
|
||||
const rootDir = await mkdtemp(path.join(os.tmpdir(), 'elydora-gemini-'));
|
||||
const homeDir = path.join(
|
||||
rootDir,
|
||||
const base = await createFixtureRoot(
|
||||
'elydora-gemini-',
|
||||
"home with spaces and 'quote $GEMINI_CWD %GEMINI_CWD%",
|
||||
);
|
||||
const projectDir = path.join(rootDir, 'project with spaces');
|
||||
const geminiCliHome = homeDir;
|
||||
const geminiCliHome = base.homeDir;
|
||||
const settingsPath = path.join(geminiCliHome, '.gemini', 'settings.json');
|
||||
const agentDir = path.join(homeDir, '.elydora', 'agent-1');
|
||||
const guardScriptPath = path.join(agentDir, 'guard.js');
|
||||
const hookScriptPath = path.join(agentDir, 'hook.js');
|
||||
await mkdir(projectDir, { recursive: true });
|
||||
if (settings !== undefined) await writeSettings(settingsPath, settings);
|
||||
const fixture = {
|
||||
agentDir,
|
||||
await writeOptionalJson(settingsPath, settings);
|
||||
return {
|
||||
...base,
|
||||
baseUrl,
|
||||
geminiCliHome,
|
||||
geminiCliHomeOverride: explicitGeminiHome ? geminiCliHome : undefined,
|
||||
guardScriptPath,
|
||||
homeDir,
|
||||
hookScriptPath,
|
||||
projectDir,
|
||||
rootDir,
|
||||
settingsPath,
|
||||
install(overrides = {}, environment = {}) {
|
||||
return runPlugin(this, 'install', installConfig(this, overrides), environment);
|
||||
},
|
||||
async close() {
|
||||
await rm(rootDir, { recursive: true, force: true });
|
||||
},
|
||||
};
|
||||
return fixture;
|
||||
}
|
||||
|
||||
export function managedHandler(settings, event, name) {
|
||||
@@ -202,34 +121,3 @@ export function assertManagedHandler(handler, name) {
|
||||
export function legacyHandler(scriptPath) {
|
||||
return { type: 'command', command: `node "${scriptPath}"` };
|
||||
}
|
||||
|
||||
export async function startApiServer({ status = 'active', operationStatus = 201 } = {}) {
|
||||
const requests = [];
|
||||
const server = http.createServer(async (request, response) => {
|
||||
const chunks = [];
|
||||
for await (const chunk of request) chunks.push(chunk);
|
||||
const raw = Buffer.concat(chunks).toString('utf-8');
|
||||
requests.push({ headers: request.headers, method: request.method, url: request.url, raw });
|
||||
if (request.method === 'GET') {
|
||||
response.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
response.end(JSON.stringify({ agent: { status } }));
|
||||
return;
|
||||
}
|
||||
response.writeHead(operationStatus, { 'Content-Type': 'application/json' });
|
||||
response.end(operationStatus >= 200 && operationStatus < 300
|
||||
? JSON.stringify({ operation: { accepted: true } })
|
||||
: JSON.stringify({ error: { code: 'UPSTREAM_FAILURE', message: 'failed' } }));
|
||||
});
|
||||
await new Promise((resolve, reject) => {
|
||||
server.once('error', reject);
|
||||
server.listen(0, '127.0.0.1', resolve);
|
||||
});
|
||||
const address = server.address();
|
||||
return {
|
||||
baseUrl: `http://127.0.0.1:${address.port}`,
|
||||
requests,
|
||||
close: () => new Promise((resolve, reject) => {
|
||||
server.close((error) => error ? reject(error) : resolve());
|
||||
}),
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user