Skip to content

Commit c9b4659

Browse files
dmealingclaude
andcommitted
refactor+test(ai): #1c review — extract emit locals + cover STI symmetry & non-STI invariant
code-simplifier: hoist the record<Entity> Omit/arg ternaries into recordInputOmit/recordArg locals (emitted output byte-identical). code-reviewer nits: assert the summarize record path symmetrically, and assert the non-STI record helper keeps Omit<LlmCallInput,"llmRequest"> with no callType omission. 8/8 trace tests pass. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 33dd583 commit c9b4659

3 files changed

Lines changed: 16 additions & 5 deletions

File tree

server/typescript/packages/codegen-ts/src/generators/trace-helper-file.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ export const traceHelperFile = function traceHelperFile(opts?: TraceHelperOpts):
9090
const sti = tphPin !== undefined;
9191
const callTypeValue = sti ? tphPin.value : entityName;
9292

93+
// Emitted record<Entity> fragments: the keys Omit'd from the caller input,
94+
// and the first argument passed to recordLlmCall.
95+
const recordInputOmit = sti ? `"llmRequest" | "callType"` : `"llmRequest"`;
96+
const recordArg = sti ? `{ ...input, callType: ${JSON.stringify(callTypeValue)} }` : `input`;
97+
9398
// Derive the parse format from the prompt's @format attr.
9499
// "xml" → Format.XML; absent or any other value → Format.JSON.
95100
const promptFormat = prompt.ownAttr(TEMPLATE_ATTR_FORMAT);
@@ -174,9 +179,9 @@ export const traceHelperFile = function traceHelperFile(opts?: TraceHelperOpts):
174179
`export async function ${fnName}(`,
175180
` om: ObjectManager,`,
176181
` responseMo: MetaObject,`,
177-
` input: Omit<LlmCallInput, ${sti ? `"llmRequest" | "callType"` : `"llmRequest"`}> & { llmRequest: ${requestType} },`,
182+
` input: Omit<LlmCallInput, ${recordInputOmit}> & { llmRequest: ${requestType} },`,
178183
`): Promise<${entityName}TraceResult> {`,
179-
` const result = await recordLlmCall(${sti ? `{ ...input, callType: ${JSON.stringify(callTypeValue)} }` : `input`}, {`,
184+
` const result = await recordLlmCall(${recordArg}, {`,
180185
` recorder: new LlmCallDbRecorder(om, "${entityName}"),`,
181186
` responseMo,`,
182187
` format: ${formatLiteral},`,

server/typescript/packages/codegen-ts/test/ai-trace-sti.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@ async function genTrace(): Promise<{ classify: string; summarize: string }> {
5151

5252
describe("ai-trace #1c — STI callType stamping", () => {
5353
test("record<Entity> omits callType from input and stamps the discriminator value", async () => {
54-
const { classify } = await genTrace();
54+
const { classify, summarize } = await genTrace();
55+
// both subtypes drop callType from the caller input and stamp their own value
5556
expect(classify).toContain('Omit<LlmCallInput, "llmRequest" | "callType">');
56-
expect(classify).toContain('callType: "classify"');
57-
expect(classify).toContain("...input,");
57+
expect(classify).toContain('{ ...input, callType: "classify" }');
58+
expect(summarize).toContain('Omit<LlmCallInput, "llmRequest" | "callType">');
59+
expect(summarize).toContain('{ ...input, callType: "summarize" }');
5860
});
5961

6062
test("call<Entity> stamps the discriminator value (not the entity name)", async () => {

server/typescript/packages/codegen-ts/test/derive-trace-fields.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ describe("derive trace fields", () => {
6060
const h = readFileSync(join(tmp, "ClassifyCall.trace.ts"), "utf-8");
6161
expect(h).toContain("recordClassifyCall");
6262
expect(h).toContain("recordLlmCall");
63+
// non-STI invariant: a trace entity that is NOT a TPH subtype keeps callType
64+
// caller-supplied (no "| callType" omission, no spread-injection).
65+
expect(h).toContain('Omit<LlmCallInput, "llmRequest">');
66+
expect(h).not.toContain('"llmRequest" | "callType"');
6367
});
6468

6569
test("trace-helper emits both record and call helpers for a renderable prompt", async () => {

0 commit comments

Comments
 (0)