Skip to content

armgit / kiln

Public Imported from GitHub
0

Build and revise procedural 3D assets with your coding agent. Local MCP server, CLI and TypeScript engine with rendered review, editable source and GLB export.

chore(core): expose kiln.refactor + wire hardened validation

Matt Kissingerb37c2017100b
diff --git a/__tests__/refactor-validation.test.ts b/__tests__/refactor-validation.test.ts
new file mode 100644
index 00000000..5735e6ef
--- /dev/null
+++ b/__tests__/refactor-validation.test.ts
@@ -0,0 +1,101 @@
+/**
+ * kiln.refactor + hardened validation wiring (W3b.4)
+ *
+ * Verifies that refactorCode runs the W3b.3 validator over its output and
+ * re-prompts the SDK once when the first attempt fails validation. Mocks
+ * the agent SDK so no network / API key needed.
+ */
+
+import { describe, expect, test, mock } from 'bun:test';
+
+// Track every call so we can verify the re-prompt happened.
+let calls = 0;
+const responses: string[] = [];
+
+mock.module('@anthropic-ai/claude-agent-sdk', () => ({
+ query: () =>
+ (async function* () {
+ const idx = calls;
+ calls += 1;
+ const code = responses[idx] ?? responses[responses.length - 1] ?? '';
+ yield {
+ type: 'result',
+ subtype: 'success',
+ structured_output: { code },
+ session_id: `session-${idx}`,
+ };
+ })(),
+}));
+
+let counter = 0;
+async function freshImport(): Promise<typeof import('../index')> {
+ counter += 1;
+ return (await import(`../index?refactor-validation=${counter}`)) as typeof import('../index');
+}
+
+describe('kiln.refactor — hardened validation', () => {
+ test('returns immediately when refactored code passes validation', async () => {
+ calls = 0;
+ responses.length = 0;
+ responses.push(`
+const meta = { name: 'OK' };
+function build() { return createRoot('OK'); }
+`);
+
+ const kiln = await freshImport();
+ const result = await kiln.refactor({
+ instruction: 'tweak',
+ geometryCode: 'old',
+ target: 'geometry',
+ });
+
+ expect(result.success).toBe(true);
+ expect(result.code).toContain('OK');
+ expect(calls).toBe(1);
+ });
+
+ test('re-prompts once when first attempt fails validation', async () => {
+ calls = 0;
+ responses.length = 0;
+ // First attempt: missing build() — invalid.
+ responses.push(`const meta = { name: 'X' };`);
+ // Retry: valid.
+ responses.push(`
+const meta = { name: 'X' };
+function build() { return createRoot('X'); }
+`);
+
+ const kiln = await freshImport();
+ const result = await kiln.refactor({
+ instruction: 'tweak',
+ geometryCode: 'old',
+ target: 'geometry',
+ });
+
+ expect(result.success).toBe(true);
+ expect(calls).toBe(2);
+ expect(result.code).toContain('build');
+ });
+
+ test('skips validation for effect-only refactors', async () => {
+ calls = 0;
+ responses.length = 0;
+ // Effect refactor returns whatever; not subject to geometry validator.
+ responses.push('// some TSL effect code');
+
+ const kiln = await freshImport();
+ const result = await kiln.refactor({
+ instruction: 'tweak shader',
+ effectCode: 'old shader',
+ target: 'effect',
+ });
+
+ expect(result.success).toBe(true);
+ expect(calls).toBe(1);
+ });
+
+ test('alias `refactor` resolves to refactorCode', async () => {
+ const kiln = await freshImport();
+ expect(kiln.refactor).toBe(kiln.refactorCode);
+ });
+});
diff --git a/generate.ts b/generate.ts
index 068e697f..fe92bd75 100644
--- a/generate.ts
+++ b/generate.ts
@@ -227,6 +227,11 @@ Output minified code only.`,
* Refactor existing code (geometry, effect, or both) against an instruction.
* Preserves asset character; structured output keeps the client's downstream
* rendering path stable.
+ *
+ * For geometry refactors, runs the (W3b.3) hardened validator over the
+ * refactored code and re-prompts once if validation fails. Effect-only
+ * refactors are returned as-is — the geometry validator doesn't apply to
+ * TSL shader code.
*/
export async function refactorCode(
request: RefactorRequest,
@@ -234,11 +239,7 @@ export async function refactorCode(
): Promise<KilnGenerateResult> {
stripClaudeCodeNestingMarkers();
- const abortController = new AbortController();
- const timeoutMs = opts.timeoutMs ?? DEFAULT_QUERY_TIMEOUT_MS;
- const timeout = setTimeout(() => abortController.abort(), timeoutMs);
-
- try {
+ const buildRefactorPrompt = (extraInstruction?: string): string => {
const codeContext: string[] = [];
if (request.geometryCode) {
codeContext.push(
@@ -258,18 +259,55 @@ export async function refactorCode(
? 'Update only the geometry code'
: 'Update only the effect code';
- const prompt = `${codeContext.join('\n\n')}
+ return `${codeContext.join('\n\n')}
## Refactor Instructions
${request.instruction}
-
+${extraInstruction ? `\n${extraInstruction}\n` : ''}
## Task
${outputInstructions}`;
+ };
- const systemPrompt = `You are refactoring existing 3D asset code.
+ const systemPrompt = `You are refactoring existing 3D asset code.
Preserve the asset's character and style while applying the requested changes.
Keep working code - only modify what's necessary for the refactor.`;
+ const initial = await runRefactorQuery(buildRefactorPrompt(), systemPrompt, opts);
+ if (!initial.success) return initial;
+
+ // Skip validation when no geometry was generated (effect-only refactor).
+ if (!initial.code || request.target === 'effect') return initial;
+
+ const v = validate(initial.code);
+ if (v.valid) return initial;
+
+ // Re-prompt once with the structured errors as feedback. Mirrors the
+ // retryWithFeedback pattern used by generateKilnCode.
+ const retryInstruction = `Previous attempt had validation errors:
+${v.issues
+ .map((i) => `- [${i.code}] ${i.message}${i.fixHint ? ` (${i.fixHint})` : ''}`)
+ .join('\n')}
+
+Fix these issues before producing the refactored code.`;
+
+ const retried = await runRefactorQuery(
+ buildRefactorPrompt(retryInstruction),
+ systemPrompt,
+ opts
+ );
+ return retried;
+}
+
+async function runRefactorQuery(
+ prompt: string,
+ systemPrompt: string,
+ opts: KilnGenerateCallOptions
+): Promise<KilnGenerateResult> {
+ const abortController = new AbortController();
+ const timeoutMs = opts.timeoutMs ?? DEFAULT_QUERY_TIMEOUT_MS;
+ const timeout = setTimeout(() => abortController.abort(), timeoutMs);
+
+ try {
const q = query({
prompt,
options: {