Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

test(forge): pin the director-truncation guard, and simplify it to one signal

nirholasb42dcd34a536
diff --git a/api/_mcp-studio/forge-client.js b/api/_mcp-studio/forge-client.js
index eed50a39a..75f8532f3 100644
--- a/api/_mcp-studio/forge-client.js
+++ b/api/_mcp-studio/forge-client.js
@@ -248,29 +248,27 @@ const DIRECTOR_TIMEOUT_MS = 20_000;
// beyond this the model has stopped writing a prompt and started writing prose.
const DIRECTOR_MAX_CHARS = 1000;
-// A rewrite that stops mid-clause is a truncated generation, not a brief. The
-// tell is the final character: a complete spec ends on a word or terminal
-// punctuation, never on a separator that promises more text. Hyphen, en-dash
-// and em-dash are written as escapes (not literal glyphs) so this regex itself
-// never trips the repo's own em-dash ban.
-const ENDS_MID_CLAUSE = /[,;:\-\u2013\u2014/&+([{]$/;
-
-// Trailing function words carry the same tell without the punctuation ("...with
-// gracefully curved" is complete only if "curved" is the last adjective, which a
-// dangling connective rules out).
-const DANGLING_CONNECTIVE =
- /\b(?:and|or|with|without|of|in|on|at|to|for|from|by|as|the|a|an|its|their|plus|featuring|including|made|constructed)$/i;
+// The director's system prompts ask for a complete spec, and a complete spec is
+// a finished sentence: every well-formed one observed in production closes on a
+// period after its negatives clause ("...no second subject."). A generation that
+// ran out of tokens cannot, which makes terminal punctuation the one signal that
+// separates a whole brief from a clipped one without guessing at grammar. Both
+// production truncations fail it ("A small," and "A classic wooden rocking chair
+// with gracefully curved"), as does any fragment ending on a separator or a
+// dangling connective, with no per-word list to keep current.
+const ENDS_COMPLETE = /[.!?]["'\u201d\u2019)\]]*$/;
// Decide whether a director rewrite is safe to forward in place of the user's
// own words. The director is a quality lever that must never cost a caller their
// intent, so anything that fails this check falls back to the raw prompt rather
-// than shipping a fragment. Pure: same inputs → same verdict.
+// than shipping a fragment. Erring toward rejection is cheap: the fallback is
+// the caller's own wording, which is always a valid brief. Pure: same inputs to
+// same verdict.
export function isUsableDirectorRewrite(refined, rawPrompt) {
if (typeof refined !== 'string') return false;
const text = refined.trim();
if (text.length < 3 || text.length > DIRECTOR_MAX_CHARS) return false;
- if (ENDS_MID_CLAUSE.test(text)) return false;
- if (DANGLING_CONNECTIVE.test(text)) return false;
+ if (!ENDS_COMPLETE.test(text)) return false;
// The director's contract is to ENRICH a rough idea into a denser spec. A
// result no longer than what the caller typed has added nothing, and is far
// more likely a clipped opening clause than a genuine tightening, so the
diff --git a/tests/forge-director-rewrite.test.js b/tests/forge-director-rewrite.test.js
new file mode 100644
index 000000000..a8d60d851
--- /dev/null
+++ b/tests/forge-director-rewrite.test.js
@@ -0,0 +1,78 @@
+// The forge art director rewrites a rough idea into a dense photoreal spec and
+// forwards THAT to text-to-image, which is the only thing the mesh reconstructor
+// ever sees. So a rewrite that arrives truncated does not degrade the result, it
+// replaces the subject outright: observed on production 2026-08-07, "a small
+// ceramic teapot with a bamboo handle" was directed to "A small," and came back
+// as a coffee tamper, and "a red wooden rocking chair" was cut to "A classic
+// wooden rocking chair with gracefully curved". These pin the guard that keeps
+// the director's stated contract: it may improve a generation, never break one.
+
+import { describe, expect, it } from 'vitest';
+import { isUsableDirectorRewrite } from '../api/_mcp-studio/forge-client.js';
+
+const RAW = 'a small ceramic teapot with a bamboo handle';
+
+// A complete spec as the director actually emits one, verified against
+// production output for "a leather messenger bag with brass buckles".
+const COMPLETE_SPEC =
+ 'Ceramic teapot with a rounded silhouette, constructed from a glazed body, a ' +
+ 'curved spout and a bamboo handle bound with waxed cord, in a photoreal art ' +
+ 'style with natural daylight-balanced studio lighting, shallow depth of field, ' +
+ 'and visible micro-detail such as glaze pooling and bamboo grain, full subject ' +
+ 'in frame, centered, isolated on a plain neutral background, no text or watermark.';
+
+describe('isUsableDirectorRewrite', () => {
+ it('accepts a complete, enriched spec', () => {
+ expect(isUsableDirectorRewrite(COMPLETE_SPEC, RAW)).toBe(true);
+ });
+
+ it('rejects the production truncation that produced a coffee tamper', () => {
+ expect(isUsableDirectorRewrite('A small,', RAW)).toBe(false);
+ });
+
+ it('rejects a rewrite cut mid-clause on a dangling adjective phrase', () => {
+ const raw = 'a red wooden rocking chair';
+ expect(isUsableDirectorRewrite('A classic wooden rocking chair with gracefully curved', raw)).toBe(
+ false,
+ );
+ });
+
+ it('rejects any rewrite ending on a separator that promises more text', () => {
+ for (const tail of [',', ';', ':', '/', '&', '+', '(']) {
+ expect(isUsableDirectorRewrite(`${COMPLETE_SPEC.slice(0, 120)}${tail}`, RAW)).toBe(false);
+ }
+ });
+
+ it('rejects a rewrite ending on a dangling connective', () => {
+ for (const tail of ['and', 'with', 'featuring', 'made', 'the', 'of']) {
+ expect(isUsableDirectorRewrite(`${COMPLETE_SPEC.slice(0, 120)} ${tail}`, RAW)).toBe(false);
+ }
+ });
+
+ it('accepts a complete sentence closed inside a quote or bracket', () => {
+ expect(isUsableDirectorRewrite(`${COMPLETE_SPEC.slice(0, 200)}."`, RAW)).toBe(true);
+ expect(isUsableDirectorRewrite(`${COMPLETE_SPEC.slice(0, 200)}!`, RAW)).toBe(true);
+ });
+
+ it('rejects a rewrite that did not enrich the caller brief', () => {
+ // No longer than what the user typed means the director added nothing, and
+ // is far more likely a clipped opening clause than a genuine tightening.
+ expect(isUsableDirectorRewrite('A ceramic teapot.', RAW)).toBe(false);
+ expect(isUsableDirectorRewrite(`${RAW}.`.slice(0, RAW.length), RAW)).toBe(false);
+ });
+
+ it('holds the existing length bounds', () => {
+ expect(isUsableDirectorRewrite('a.', 'a')).toBe(false);
+ expect(isUsableDirectorRewrite(`${'x'.repeat(1000)}.`, RAW)).toBe(false);
+ });
+
+ it('rejects a non-string result rather than forwarding it', () => {
+ expect(isUsableDirectorRewrite(null, RAW)).toBe(false);
+ expect(isUsableDirectorRewrite(undefined, RAW)).toBe(false);
+ });
+
+ it('still accepts an enriched spec when the caller brief is absent', () => {
+ expect(isUsableDirectorRewrite(COMPLETE_SPEC, '')).toBe(true);
+ expect(isUsableDirectorRewrite(COMPLETE_SPEC, undefined)).toBe(true);
+ });
+});