Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(e2e): accept kubectl's object-per-item dry-run output in approved-tools preparation
Alfredo Lopez594cafb9800a
diff --git a/scripts/e2e/prepare-codex-approved-tools.mjs b/scripts/e2e/prepare-codex-approved-tools.mjsindex e4342694c..4f6df44b1 100644--- a/scripts/e2e/prepare-codex-approved-tools.mjs+++ b/scripts/e2e/prepare-codex-approved-tools.mjs@@ -280,6 +280,46 @@ export function makeResources(scenarios) {})}+// `kubectl create --dry-run=server -f - -o json` on a List prints either one+// List document or, as kubectl v1.36 does, one JSON object per item. Accept+// exactly those two shapes; anything else is a malformed response.+export function parseDryRunItems(output) {+ const objects = []+ let start = -1+ let depth = 0+ let inString = false+ let escaped = false+ for (let index = 0; index < output.length; index++) {+ const char = output[index]+ if (inString) {+ if (escaped) escaped = false+ else if (char === '\\') escaped = true+ else if (char === '"') inString = false+ continue+ }+ if (depth === 0) {+ if (/\s/.test(char)) continue+ if (char !== '{') throw new Error('Dry-run output is not a sequence of JSON objects')+ start = index+ }+ if (char === '"') inString = true+ else if (char === '{') depth+++ else if (char === '}') {+ depth--+ if (depth === 0) objects.push(JSON.parse(output.slice(start, index + 1)))+ }+ }+ if (depth !== 0 || inString) throw new Error('Dry-run output ends inside a JSON object')+ if (objects.length === 0) throw new Error('Dry-run output is empty')+ if (objects.length === 1 && objects[0].kind === 'List') {+ if (!Array.isArray(objects[0].items)) throw new Error('Dry-run List has no items')+ return objects[0].items+ }+ if (objects.some(object => object.kind === 'List'))+ throw new Error('Dry-run output mixes a List with other objects')+ return objects+}+export function assertResourceRoundTrip(expected, observed) {if (Array.isArray(expected)) {if (!Array.isArray(observed) || observed.length !== expected.length)@@ -1293,12 +1333,12 @@ async function main() {const allScenarios = [...scenarios, workflowScenario]const resources = [...makeResources(allScenarios), ...makeWorkflowResources(workflowScenario)]const proposed = { apiVersion: 'v1', kind: 'List', items: resources }- const validated = JSON.parse(+ const validated = parseDryRunItems(kubectl(['create', '--dry-run=server', '-f', '-', '-o', 'json'], {input: JSON.stringify(proposed),}))- assertResourceRoundTrip(resources, validated.items)+ assertResourceRoundTrip(resources, validated)const proxyService = JSON.parse(kubectl(['-n', 'control-plane', 'get', 'service/codex-llm-proxy', '-o', 'json']))diff --git a/scripts/e2e/prepare-codex-approved-tools.test.mjs b/scripts/e2e/prepare-codex-approved-tools.test.mjsindex 4e325cb56..564179a26 100644--- a/scripts/e2e/prepare-codex-approved-tools.test.mjs+++ b/scripts/e2e/prepare-codex-approved-tools.test.mjs@@ -18,6 +18,7 @@ import {makeWorkflowResources,makeWorkflowScenario,openOwnedFile,+ parseDryRunItems,readOwnedDescriptor,readOwnedFile,validateKubectlArgs,@@ -182,6 +183,49 @@ test('roundtrip rejects pruned display/model/empty grant fields while accepting}})+test('dry-run parser accepts a List document and a kubectl v1.36 object-per-item stream', () => {+ const resources = makeResources(scenarios)+ const observed = structuredClone(resources)+ observed.forEach((item, index) => {+ item.metadata.uid = `server-generated-${index}`+ // Braces and escaped quotes inside strings must not split an object.+ item.metadata.annotations = { note: 'a "}{" value \\ with braces' }+ })+ const list = JSON.stringify({ apiVersion: 'v1', kind: 'List', items: observed }, null, 2)+ const stream = observed.map(item => JSON.stringify(item, null, 4)).join('\n') + '\n'+ for (const output of [list, stream]) {+ const items = parseDryRunItems(output)+ assert.equal(items.length, resources.length)+ assert.deepEqual(items, observed)+ assert.doesNotThrow(() => assertResourceRoundTrip(resources, items))+ }+ const pruned = structuredClone(observed)+ pruned.pop()+ const shortStream = pruned.map(item => JSON.stringify(item)).join('\n')+ assert.equal(parseDryRunItems(shortStream).length, resources.length - 1)+ assert.throws(+ () => assertResourceRoundTrip(resources, parseDryRunItems(shortStream)),+ /Fixture array was not preserved/+ )+})++test('dry-run parser rejects empty, truncated, non-object and mixed List output', () => {+ const item = JSON.stringify({ kind: 'Namespace', metadata: { name: 'a' } })+ const list = JSON.stringify({ kind: 'List', items: [] })+ for (const [output, message] of [+ ['', /empty/],+ [' \n', /empty/],+ [item.slice(0, -1), /ends inside a JSON object/],+ [`${item}\ntrailing`, /not a sequence of JSON objects/],+ [`[${item}]`, /not a sequence of JSON objects/],+ [`${item},${item}`, /not a sequence of JSON objects/],+ [`${list}\n${item}`, /mixes a List/],+ [JSON.stringify({ kind: 'List' }), /has no items/],+ ])+ assert.throws(() => parseDryRunItems(output), message)+ assert.deepEqual(parseDryRunItems(list), [])+})+test('evidence accepts only exact canonical run children and rejects symlinks', () => {const root = fs.mkdtempSync(path.join(os.tmpdir(), 'approved-tools-setup-'))try {