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
222 lines
6.6 KiB
Go
222 lines
6.6 KiB
Go
package plugins
|
|
|
|
import "fmt"
|
|
|
|
// GenerateGuardScript returns a self-contained agent status guard.
|
|
func GenerateGuardScript(agentName string, agentID string) string {
|
|
return generateGuardScript(agentName, agentID, "", false, "")
|
|
}
|
|
|
|
func generateGuardScript(
|
|
agentName, agentID, successOutput string,
|
|
failClosed bool,
|
|
denyProtocol string,
|
|
) string {
|
|
return fmt.Sprintf(`#!/usr/bin/env node
|
|
'use strict';
|
|
|
|
// Elydora agent guard for %s. Generated by Elydora SDK.
|
|
const crypto = require('node:crypto');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const os = require('node:os');
|
|
|
|
const AGENT_NAME = %q;
|
|
const AGENT_ID = %q;
|
|
const SUCCESS_OUTPUT = %q;
|
|
const FAIL_CLOSED = %t;
|
|
const DENY_PROTOCOL = %q;
|
|
const ELYDORA_DIR = path.join(os.homedir(), '.elydora');
|
|
const CONFIG_PATH = path.join(ELYDORA_DIR, AGENT_ID, 'config.json');
|
|
const STATUS_CACHE_PATH = path.join(ELYDORA_DIR, AGENT_ID, 'status-cache.json');
|
|
const CACHE_TTL_MS = 60000;
|
|
|
|
%s
|
|
|
|
function validateStatus(value, source) {
|
|
if (!['active', 'frozen', 'revoked'].includes(value)) {
|
|
throw new Error(source + ' contains an invalid agent status');
|
|
}
|
|
return value;
|
|
}
|
|
|
|
function blockAgent(status) {
|
|
const state = status === 'frozen' ? 'is frozen by Elydora' : 'is revoked in Elydora';
|
|
const message = 'Agent "' + AGENT_NAME + '" ' + state + '.';
|
|
if (DENY_PROTOCOL === 'cursor') {
|
|
process.stdout.write(JSON.stringify({
|
|
permission: 'deny',
|
|
userMessage: message,
|
|
agentMessage: message + ' Tool execution is blocked.',
|
|
}) + '\n');
|
|
} else if (DENY_PROTOCOL === 'grok') {
|
|
process.stdout.write(JSON.stringify({
|
|
decision: 'deny',
|
|
reason: message,
|
|
}) + '\n');
|
|
}
|
|
process.stderr.write('[Elydora guard] ' + message + ' Tool execution blocked.\n');
|
|
process.exitCode = 2;
|
|
}
|
|
|
|
async function readInput() {
|
|
const chunks = [];
|
|
for await (const chunk of process.stdin) chunks.push(chunk);
|
|
const raw = Buffer.concat(chunks).toString('utf-8').trim();
|
|
if (!raw) {
|
|
if (FAIL_CLOSED) throw new Error('Hook input is empty');
|
|
process.stderr.write(
|
|
'[Elydora guard] Hook input is empty; enforcement continued in fail-open mode.\n',
|
|
);
|
|
return;
|
|
}
|
|
let value;
|
|
try {
|
|
value = JSON.parse(raw);
|
|
} catch (error) {
|
|
if (FAIL_CLOSED) throw new Error('Hook input is invalid JSON: ' + error.message);
|
|
process.stderr.write(
|
|
'[Elydora guard] Hook input is invalid JSON; ' +
|
|
'enforcement continued in fail-open mode: ' + error.message + '\n',
|
|
);
|
|
return;
|
|
}
|
|
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
if (FAIL_CLOSED) throw new Error('Hook input must contain a JSON object');
|
|
process.stderr.write(
|
|
'[Elydora guard] Hook input must contain a JSON object; ' +
|
|
'enforcement continued in fail-open mode.\n',
|
|
);
|
|
}
|
|
}
|
|
|
|
function readConfig() {
|
|
const raw = readProtectedFile(
|
|
CONFIG_PATH,
|
|
'Agent config',
|
|
MAX_PROTECTED_CONFIG_BYTES,
|
|
).toString('utf-8');
|
|
let config;
|
|
try {
|
|
config = JSON.parse(raw);
|
|
} catch (error) {
|
|
throw new Error('Agent config is invalid JSON: ' + error.message);
|
|
}
|
|
if (!config || typeof config !== 'object' || Array.isArray(config)) {
|
|
throw new Error('Agent config must contain a JSON object');
|
|
}
|
|
if (config.agent_id !== AGENT_ID ||
|
|
(config.agent_name !== undefined && config.agent_name !== AGENT_NAME)) {
|
|
throw new Error('Agent config identity does not match the generated guard');
|
|
}
|
|
return config;
|
|
}
|
|
|
|
function readStatusCache() {
|
|
let raw;
|
|
try {
|
|
raw = readProtectedFile(
|
|
STATUS_CACHE_PATH,
|
|
'Status cache',
|
|
MAX_PROTECTED_CONFIG_BYTES,
|
|
).toString('utf-8');
|
|
} catch (error) {
|
|
if (hasRuntimeErrorCode(error, 'ENOENT')) return undefined;
|
|
throw new Error('Failed to read status cache: ' + error.message);
|
|
}
|
|
let cache;
|
|
try {
|
|
cache = JSON.parse(raw);
|
|
} catch (error) {
|
|
throw new Error('Status cache is invalid JSON: ' + error.message);
|
|
}
|
|
if (!cache || typeof cache !== 'object' || Array.isArray(cache) ||
|
|
!Number.isFinite(cache.cached_at)) {
|
|
throw new Error('Status cache has an invalid shape');
|
|
}
|
|
const age = Date.now() - cache.cached_at;
|
|
if (age < 0) throw new Error('Status cache timestamp is in the future');
|
|
return { status: validateStatus(cache.status, 'Status cache'), age };
|
|
}
|
|
|
|
function writeStatusCache(status) {
|
|
writeProtectedJson(STATUS_CACHE_PATH, 'Status cache', {
|
|
status: status,
|
|
cached_at: Date.now(),
|
|
});
|
|
}
|
|
|
|
async function resolveRemoteStatus(config) {
|
|
const baseURL = resolveConfigBaseURL(config);
|
|
const token = config.token || '';
|
|
if (typeof token !== 'string') throw new Error('Agent config token must be a string');
|
|
const controller = new AbortController();
|
|
const timeout = setTimeout(() => controller.abort(), 3000);
|
|
try {
|
|
const headers = { Accept: 'application/json' };
|
|
if (token) headers.Authorization = 'Bearer ' + token;
|
|
const response = await fetch(baseURL + '/v1/agents/' + encodeURIComponent(AGENT_ID), {
|
|
headers: headers,
|
|
signal: controller.signal,
|
|
});
|
|
if (!response.ok) throw new Error('Agent status API returned HTTP ' + response.status);
|
|
const data = await response.json();
|
|
return validateStatus(data && data.agent && data.agent.status, 'Agent status API');
|
|
} finally {
|
|
clearTimeout(timeout);
|
|
}
|
|
}
|
|
|
|
async function main() {
|
|
await readInput();
|
|
let config;
|
|
try {
|
|
config = readConfig();
|
|
} catch (error) {
|
|
if (FAIL_CLOSED) throw new Error('Failed to read agent config: ' + error.message);
|
|
process.stderr.write('[Elydora guard] Failed to read agent config: ' + error.message + '\n');
|
|
return;
|
|
}
|
|
try {
|
|
const cache = readStatusCache();
|
|
if (cache && cache.age < CACHE_TTL_MS) {
|
|
if (cache.status !== 'active') blockAgent(cache.status);
|
|
return;
|
|
}
|
|
} catch (error) {
|
|
if (FAIL_CLOSED) throw error;
|
|
process.stderr.write('[Elydora guard] ' + error.message + '\n');
|
|
}
|
|
try {
|
|
const status = await resolveRemoteStatus(config);
|
|
writeStatusCache(status);
|
|
if (status !== 'active') blockAgent(status);
|
|
} catch (error) {
|
|
if (FAIL_CLOSED) throw new Error('Failed to resolve agent status: ' + error.message);
|
|
process.stderr.write(
|
|
'[Elydora guard] Failed to resolve agent status: ' + error.message + '\n',
|
|
);
|
|
}
|
|
}
|
|
|
|
main().then(() => {
|
|
if ((!process.exitCode || process.exitCode === 0) && SUCCESS_OUTPUT) {
|
|
process.stdout.write(SUCCESS_OUTPUT);
|
|
}
|
|
}).catch((error) => {
|
|
process.stderr.write(
|
|
'[Elydora guard] ' + (error.stack || error.message || String(error)) + '\n',
|
|
);
|
|
process.exitCode = 1;
|
|
});
|
|
`,
|
|
agentName,
|
|
agentName,
|
|
agentID,
|
|
successOutput,
|
|
failClosed,
|
|
denyProtocol,
|
|
protectedRuntimeFileReader,
|
|
)
|
|
}
|