@@ -288,3 +288,35 @@ function bareAgentProcReadPathAllowed(absPath) {
|
||||
const p = bareAgentNormalizeAbsPath(absPath)
|
||||
return Boolean(p && (p === '/proc' || p.startsWith('/proc/')))
|
||||
}
|
||||
|
||||
/**
|
||||
* Kernel-runner evals guest scripts as AsyncFunction('ctx','argv', body).
|
||||
* Agent-authored code that redeclares ctx/argv or uses ESM export throws
|
||||
* "Identifier 'ctx' has already been declared" — same contract as /bin
|
||||
* utilities: define async function run(ctx, argv), never bind ctx yourself.
|
||||
* @param {string} code
|
||||
*/
|
||||
function bareAgentNormalizeGuestScript(code) {
|
||||
let src = String(code || '').replace(/^\uFEFF/, '')
|
||||
src = src.replace(/^#!.*(?:\r?\n|$)/, '')
|
||||
src = src.replace(/^\s*export\s*\{[^}]*\}\s*;?\s*$/gm, '')
|
||||
src = src.replace(/^export\s+default\s+/gm, '')
|
||||
src = src.replace(/^export\s+async\s+function\s+/gm, 'async function ')
|
||||
src = src.replace(/^export\s+function\s+/gm, 'function ')
|
||||
src = src.replace(/^export\s+const\s+/gm, 'const ')
|
||||
src = src.replace(/^export\s+let\s+/gm, 'let ')
|
||||
src = src.replace(/^export\s+var\s+/gm, 'var ')
|
||||
src = src.replace(/^(?:const|let|var)\s+ctx\b[^;\n]*;?\s*$/gm, '')
|
||||
src = src.replace(/^(?:const|let|var)\s+argv\b[^;\n]*;?\s*$/gm, '')
|
||||
src = src.replace(
|
||||
/^(?:const|let|var)\s*\{\s*ctx[\s,}][^;\n]*;?\s*$/gm,
|
||||
''
|
||||
)
|
||||
if (
|
||||
!/\b(?:async\s+)?function\s+run\s*\(/.test(src) &&
|
||||
!/\brun\s*=\s*(?:async\s*)?(?:function\s*)?\(/.test(src)
|
||||
) {
|
||||
src = 'async function run(ctx, argv) {\n' + src.trim() + '\n}\n'
|
||||
}
|
||||
return src.trim() + '\n'
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user