Repository navigation
feat(core): run a function's source as an automation's code - #68392
rekram1-node wants to merge 5 commits into
Conversation
There was a problem hiding this comment.
Works as described. I created an automation with an inline arrow function through the real Automation service. The saved program was return await (<fn>)(input), the tool call inside it was authorised at creation, and two ticks ran it with the right input. The new tests and the nearby automation and codemode suites pass (87 tests), and typecheck:tsc passes in core and schema.
Public API: adds an optional codemode.functionSources to the tool Options in the plugin API, and changes the automation.create action.code description. Code actions that are a single function expression are now saved wrapped as return await (<fn>)(input); before, such a program did nothing. Backwards compatible.
Two points, inline: the outer-variable check matches by name rather than scope, so it can miss real outer variables or refuse valid functions; and nothing validates functionSources when a tool registers, so one malformed plugin would break every Code Mode run.
| const declared = declarations(program) | ||
| for (const item of found) { | ||
| const inside = (at: number) => at >= item.fn.start && at < item.fn.end | ||
| const own = new Set(declared.filter((d) => inside(d.at)).map((d) => d.name)) |
There was a problem hiding this comment.
This check compares names, not scopes, so it can be wrong both ways:
- Misses an outer variable:
const x = 1followed bycode: async () => { [1].map((x) => x); return x }is accepted. The inner callback'sxputsxinown. The automation then throws a ReferenceError on every run, not at creation. - Refuses a valid function:
items.map((state) => …)anywhere in the script makescode: async () => ({ state: state ?? 0 })be refused. Herestateis the documented run global (the same applies tonow,runsandargs).
A small scope walk would cover both: resolve each identifier through the function's nested scopes, and look only at bindings on the enclosing chain.
| return new Map( | ||
| Array.from(inventory.tools.values()).flatMap((registration) => { | ||
| const codemode = registration.options?.codemode | ||
| const paths = typeof codemode === "object" && codemode !== null ? codemode.functionSources : undefined |
There was a problem hiding this comment.
functionSources is now part of the plugin API, but the registration check in src/tool.ts (around line 967) doesn't validate it. A plugin passing functionSources: "action.code" (a string) gets past paths.length === 0, and then paths.map throws here. This runs over the whole inventory on every prepare, so that one tool would break every Code Mode run in the session. Could you check for an array of non-empty strings when the tool registers?
There was a problem hiding this comment.
The new version works as described. Both earlier points are resolved: the source rewrite and the functionSources option are gone. I ran a real Code Mode program through the tool-automation harness: (async (input) => {…}).toString() returns the function's exact source (comments, backticks, regexes and escaped quotes unchanged), and automation.create gets it as a plain string. AutomationCode.body is the same as in the previous commit, which I checked end to end against the real Automation service. The automation and automation tool tests pass, and typecheck:tsc passes in core and schema.
Public API: a code action that is a single function expression is now saved as return await (<fn>)(input); before, such code did nothing. The action.code description changed too. Backwards compatible.
Not blocking: nothing checks anymore whether the function uses variables from the surrounding script. That mistake now gets through creation and fails on every run, not when the automation is saved. The description does warn about it.
7a98f9a to
0c497c0
Compare
There was a problem hiding this comment.
Nothing in this pull request changed since my last review: the new commits rebase it onto a newer dev, and its four files are the same as before. On the new head, the automation tests pass and typecheck:tsc passes in core, so my previous review stands. Both earlier threads were about the source rewrite and the functionSources option, which have been removed.
Public API: a code action that is a single function expression is now saved as return await (<fn>)(input); before, such code did nothing. The action.code description changed too. Backwards compatible.
There was a problem hiding this comment.
The new commit only regenerates openapi.json for the new action.code description. check:generated passes in packages/protocol, so the spec matches the schema. My previous review still applies, and the two old threads were about code that has since been removed.
Public API: the OpenAPI description of the automation code action's code field changes. Code actions that are a single function expression are now saved as return await (<fn>)(input); before, such code did nothing. Backwards compatible.
Issue for this PR
N/A
Type of change
What does this PR do?
In Code Mode, a code automation's
codeis JavaScript inside a string inside JavaScript, so quotes and regexes get escaped twice. Now a program can write a real function and pass its source:.toString()runs inside the program, so the tool still gets a string. When an automation saves a code action whose code is a single function expression, it storesreturn await (<fn>)(input), so it's still a plain program and nothing else changes. Theaction.codedescription shows the function form.Creating a code automation now also refuses code that uses a name which won't exist when it runs, such as a variable of the script that created it:
The names come from psychoscript 0.12.0's
script.globals(worked out from the compiled program, cached, nothing extra per run), checked against what a run binds: JavaScript's globals,tools,search,fetch,input,state,now,runs,args, pluseventfor event triggers,Bun/pathwithfull_codemode, andinputswhen the code has input blocks. Only creation checks; existing automations are untouched. psychoscript 0.12.0 also makesString(fn)and template strings give a function's source, as in JavaScript.No flag: a code action that was only a function expression used to do nothing, and code using an undefined name failed on every run.
How did you verify your code works?
test/automation-code-body.test.ts, and unbound-name cases intest/automation.test.ts.bun run script/test.tsfor automation and automation tool tests,bun run check.Screenshots / recordings
N/A, no UI.
Checklist