fix: add strict TemplateLogic compile validation#165
Conversation
Signed-off-by: Rishabh Jain <rishabhj2005@email.com>
|
@Rishabh060105 This is great work. Thanks for the contribution! I left some comments! |
Signed-off-by: Rishabh Jain <rishabhj2005@email.com>
|
Hi @23harshitkumar have addressed the issues raised above, please go through it. Thanks! |
|
Thanks @Rishabh060105! You have my approval (as the "issue creator"). @sanketshevkar @mttrbrts Could we consider merging it? |
I'm tempted to say that this is a bug, and that the opt-in flag is unnecessary. Let's discuss on the WG call tomorrow? |
If these templates will never work without this TemplateLogic extension, then this is a pure bug fix and we don't need the flag to opt-in. |
Hi @mttrbrts , I checked this against both For the playground/sandbox flow, I agree with the concern: that path expects the compiled output to contain a class extending For That is why I kept this as an opt-in flag: it lets sandbox consumers enforce the stricter contract without changing existing engine behavior by default. If we agree with the fact that plain logic classes are unsupported by contract, I’m happy to simplify the PR and make the validation unconditional. |
Signed-off-by: Rishabh Jain <rishabhj2005@email.com>
The TemplateLogic extension was enforced in the playground as per the US:02 acceptance criteria. This extension helps us in autocomplete and finding out (via regex) which class to instantiate for the sandbox. Therefore, we have an enforcement for this extension in the As for the engine, @Rishabh060105 is right that it currently allows plain classes without failing (could be an accident). To decide if it needs the validation, we'd have to ask ourselves: "do we really want to enforce the TemplateLogic in the engine?". If "yes", then this is a pure bug and we should definately drop the opt-in flag and make the validation unconditional! |
|
The evidence suggests that it's accidental that the engine works for classes that don't extend TemplateLogic class. Let's drop the flag. That greatly simplifies the code. Thanks @Rishabh060105! |
Signed-off-by: Rishabh Jain <rishabhj2005@email.com>
b6331fa to
ab1e662
Compare
|
Hi @mttrbrts, @23harshitkumar I have pushed the changes:
|
| const result = fun(...args); | ||
| process.send({ result }, () => { | ||
| process.exit(); | ||
| process.disconnect(); |
There was a problem hiding this comment.
What is the impact of this change?
There was a problem hiding this comment.
I initially changed this to process.disconnect() because I thought process.exit() was interrupting coverage cleanup.I later realised that process.exit() is viable here since each worker handles one evaluation this means termination must be deterministic after sending the result.
There was a problem hiding this comment.
Pull request overview
This PR aims to add stricter (opt-in) validation during TemplateArchiveProcessor.compileLogic() so callers can require that logic/logic.ts defines a class extending TemplateLogic, helping downstream sandbox consumers fail fast during compilation rather than at execution time.
Changes:
- Adds a TypeScript AST-based check for a
TemplateLogicsubclass during logic compilation. - Refactors the
TemplateArchiveProcessortest suite to load templates via a helper and adds new validation-related tests. - Adjusts the child-process worker shutdown behavior after sending results/errors.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| test/TemplateArchiveProcessor.test.ts | Adds helpers/mocks for loading templates and introduces new compile-time validation/caching tests. |
| src/worker.js | Changes worker termination after process.send() from process.exit() to process.disconnect() (with exit code set on error). |
| src/TemplateArchiveProcessor.ts | Adds a new AST-based TemplateLogic subclass assertion and calls it during TypeScript logic compilation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (logicManager.getLanguage() === 'typescript') { | ||
| const compiledCode: Record<string, TwoSlashReturn> = {}; | ||
| const tsFiles: Array<Script> = logicManager.getScriptManager().getScriptsForTarget('typescript'); | ||
| const logicScript = tsFiles.find((tsFile) => tsFile.getIdentifier() === 'logic/logic.ts'); | ||
| await this.assertTemplateLogicSubclass(logicScript); |
There was a problem hiding this comment.
Validation has been kept unconditional as discussed above.The boolean argument now only controls compilation caching.
| expect(validationSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test('should reject plain default-exported classes', async () => { |
There was a problem hiding this comment.
This test is based on the agreed behavior.compileLogic() should reject classes that don't extend TemplateLogic without the requirement of an opt-in flag.
| process.send({ result }, () => { | ||
| process.exit(); | ||
| process.disconnect(); | ||
| }); |
There was a problem hiding this comment.
changed back process.disconnect() to process.exit() and added a test to confirm the same.
| process.send({ message: err.toString() }, () => { | ||
| process.exit(1); | ||
| process.exitCode = 1; | ||
| process.disconnect(); | ||
| }); |
There was a problem hiding this comment.
The error path now uses process.exit(1),and added a test confirming the original error being returned instead of timeout
Signed-off-by: Rishabh Jain <rishabhj2005@email.com>
|
Hi @mttrbrts, I have addressed the latest review feedback:
|
Summary
Fixes #163 by adding opt-in strict validation to
TemplateArchiveProcessor.compileLogic().Today
compileLogic()returns emitted code without checking whether it contains a class extendingTemplateLogic, so downstream sandbox consumers only discover malformed output later at execution time. This change adds a strict mode for callers that need that guarantee, while keeping the current default behavior unchanged for existing plain-class logic.Changes
CompileLogicOptionstoTemplateArchiveProcessor.compileLogic()requireTemplateLogic: trueto enforce thatlogic/logic.tsdefines a class extendingTemplateLogicCompileLogicOptionsfrom the package rootTemplateLogicTest
Implemented and pushed on branch
Rishabh060105/strict-template-logic-validation.Validation notes:
npm test -- --runTestsByPath test/TemplateArchiveProcessor.test.ts --runInBandcompileLogic()behavior directly with a focused runtime stub check covering:TemplateLogicsubclasses