Skip to content

Commit f010017

Browse files
committed
feat(spec): a blueprint field carries a typed defaultValue, so an AI-built boolean flag defaults to false
`BlueprintFieldSchema` and the strict structured-output mirror `StrictField` gain `defaultValue`: a boolean / number / string union, optional on the lenient side and required-but-nullable on the mirror. The lenient field checks the value against its `type` with the field schema's own literal contract (`checkLiteralDefaultValue`): 21 types take a default, the other 28 are refused by name at the key, and a select / radio default must be one of the field's own option values. Claude-Session: https://claude.ai/code/session_01L6bhgUCEUicLCQ4tkXayK2 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 14c67d4 commit f010017

2 files changed

Lines changed: 396 additions & 8 deletions

File tree

‎packages/spec/src/ai/solution-blueprint.test.ts‎

Lines changed: 212 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from './solution-blueprint.zod';
1111
import { ViewFilterRuleSchema } from '../ui/view.zod';
1212
import { SortItemSchema } from '../shared/enums.zod';
13-
import { SelectOptionSchema } from '../data/field.zod';
13+
import { FieldSchema, FieldType, SelectOptionSchema } from '../data/field.zod';
1414
import { ObjectSchema } from '../data/object.zod';
1515

1616
const validBlueprint: SolutionBlueprint = {
@@ -481,7 +481,7 @@ describe('SolutionBlueprintStrictSchema (OpenAI strict mirror)', () => {
481481
nameField: null,
482482
stageField: null,
483483
fields: [
484-
{ name: 'name', label: null, type: 'text', required: null, reference: null, options: null, summaryOperations: null, expression: null },
484+
{ name: 'name', label: null, type: 'text', required: null, reference: null, options: null, summaryOperations: null, expression: null, defaultValue: null },
485485
],
486486
},
487487
],
@@ -520,7 +520,7 @@ describe('SolutionBlueprintStrictSchema (OpenAI strict mirror)', () => {
520520
const badField = {
521521
...strictBp,
522522
objects: [
523-
{ name: 'x', label: null, description: null, sharingModel: null, nameField: null, stageField: null, fields: [{ name: 'f', type: 'text', required: null, reference: null, options: null, summaryOperations: null, expression: null }] },
523+
{ name: 'x', label: null, description: null, sharingModel: null, nameField: null, stageField: null, fields: [{ name: 'f', type: 'text', required: null, reference: null, options: null, summaryOperations: null, expression: null, defaultValue: null }] },
524524
],
525525
};
526526
// `f` is missing the (nullable, required) `label` key.
@@ -546,11 +546,11 @@ describe('SolutionBlueprintStrictSchema (OpenAI strict mirror)', () => {
546546
stageField: null,
547547
fields: [
548548
{
549-
name: 'task_total', label: '任务总数', type: 'summary', required: null, reference: null, options: null, expression: null,
549+
name: 'task_total', label: '任务总数', type: 'summary', required: null, reference: null, options: null, expression: null, defaultValue: null,
550550
summaryOperations: { object: 'task', function: 'count', field: 'id', relationshipField: null, conditions: null },
551551
},
552552
{
553-
name: 'completed_task_count', label: '已完成任务数', type: 'summary', required: null, reference: null, options: null, expression: null,
553+
name: 'completed_task_count', label: '已完成任务数', type: 'summary', required: null, reference: null, options: null, expression: null, defaultValue: null,
554554
summaryOperations: {
555555
object: 'task', function: 'count', field: 'id', relationshipField: null,
556556
conditions: [{ field: 'status', op: 'eq', value: 'completed' }],
@@ -622,7 +622,7 @@ describe('SolutionBlueprintStrictSchema (OpenAI strict mirror)', () => {
622622

623623
describe('stageField + option kind (the model names the stage field and states each option\'s kind, or says null)', () => {
624624
const statusField = (options: Array<Record<string, unknown>>) => ({
625-
name: 'status', label: null, type: 'select', required: null, reference: null, options, summaryOperations: null, expression: null,
625+
name: 'status', label: null, type: 'select', required: null, reference: null, options, summaryOperations: null, expression: null, defaultValue: null,
626626
});
627627
const STAGES = [
628628
{ label: 'New', value: 'new', kind: 'open' },
@@ -1153,6 +1153,7 @@ describe('strict mirror ↔ lenient schema — VALUE parity', () => {
11531153
options: optionsWithLeadingDigit.map((o) => ({ ...o, kind: null })),
11541154
summaryOperations: null,
11551155
expression: null,
1156+
defaultValue: null,
11561157
}],
11571158
}],
11581159
views: null,
@@ -1180,7 +1181,7 @@ describe('blueprint widget `aggregate` and calendar view `startDateField`', () =
11801181
name: 'interview', label: null, description: null, sharingModel: null, nameField: null, stageField: null,
11811182
fields: [{
11821183
name: 'score', label: null, type: 'number', required: null, reference: null, options: null,
1183-
summaryOperations: null, expression: null,
1184+
summaryOperations: null, expression: null, defaultValue: null,
11841185
}],
11851186
}],
11861187
views: null,
@@ -1295,3 +1296,207 @@ describe('blueprint widget `aggregate` and calendar view `startDateField`', () =
12951296
}
12961297
});
12971298
});
1299+
1300+
// ---------------------------------------------------------------------------
1301+
// Field `defaultValue` (ruling B on cloud#3619). Optional on the lenient side
1302+
// and required-but-nullable on the strict mirror; one scalar union on both.
1303+
// Whether a field takes a default, and which value, is the lenient field's
1304+
// refinement: the mirror cannot read `type` while it judges this key.
1305+
// ---------------------------------------------------------------------------
1306+
describe('blueprint field `defaultValue`', () => {
1307+
/**
1308+
* The design call this PR states, restated here so a widening of the admitted
1309+
* set is a diff to this pin: each admitted type with a value it takes, and a
1310+
* value of the wrong type for it.
1311+
*/
1312+
const ADMITTED: Record<string, { ok: unknown; wrong: unknown }> = {
1313+
boolean: { ok: false, wrong: 'false' },
1314+
toggle: { ok: true, wrong: 1 },
1315+
number: { ok: 0, wrong: '0' },
1316+
currency: { ok: 99.5, wrong: '99.5' },
1317+
percent: { ok: 50, wrong: true },
1318+
rating: { ok: 3, wrong: '3' },
1319+
slider: { ok: 10, wrong: '10' },
1320+
progress: { ok: 0, wrong: false },
1321+
text: { ok: '待处理', wrong: 0 },
1322+
textarea: { ok: '', wrong: false },
1323+
email: { ok: 'ops@example.com', wrong: 1 },
1324+
url: { ok: 'https://example.com', wrong: 1 },
1325+
phone: { ok: '+1 555 0100', wrong: 5550100 },
1326+
markdown: { ok: '# Notes', wrong: 1 },
1327+
html: { ok: '<p>Notes</p>', wrong: 1 },
1328+
richtext: { ok: 'Notes', wrong: 1 },
1329+
code: { ok: '{}', wrong: 1 },
1330+
color: { ok: '#3b82f6', wrong: 1 },
1331+
time: { ok: '09:00', wrong: '9am' },
1332+
select: { ok: 'open', wrong: 'Open' },
1333+
radio: { ok: 'done', wrong: 'closed' },
1334+
};
1335+
const OPTIONS = [{ label: 'Open', value: 'open' }, { label: 'Done', value: 'done' }];
1336+
const withOptions = (type: string) => (type === 'select' || type === 'radio' ? { options: OPTIONS } : {});
1337+
const lenientField = (field: Record<string, unknown>) => SolutionBlueprintSchema.safeParse({
1338+
summary: 's',
1339+
objects: [{ name: 'task', fields: [{ name: 'flag', ...field }] }],
1340+
});
1341+
const strictField = (field: Record<string, unknown>) => SolutionBlueprintStrictSchema.safeParse({
1342+
summary: 's',
1343+
assumptions: [],
1344+
questions: null,
1345+
objects: [{
1346+
name: 'task', label: null, description: null, sharingModel: null, nameField: null, stageField: null,
1347+
fields: [{
1348+
name: 'flag', label: null, required: null, reference: null, options: null, summaryOperations: null,
1349+
expression: null, defaultValue: null, ...field,
1350+
}],
1351+
}],
1352+
views: null,
1353+
dashboards: null,
1354+
app: null,
1355+
});
1356+
const PATH = ['objects', 0, 'fields', 0, 'defaultValue'];
1357+
/** The by-name refusals of a failed parse: custom issues at the key whose message names the field type. */
1358+
const namedRefusals = (result: ReturnType<typeof lenientField>, type: string) =>
1359+
(result.error?.issues ?? []).filter((i) => i.code === 'custom'
1360+
&& JSON.stringify(i.path) === JSON.stringify(PATH)
1361+
&& i.message.includes(`"flag" (${type})`));
1362+
1363+
it('carries a boolean flag\'s `defaultValue: false` through the parse, and the key survives', () => {
1364+
const parsed = lenientField({ type: 'boolean', defaultValue: false });
1365+
expect(parsed.success).toBe(true);
1366+
const field = parsed.data!.objects[0].fields[0];
1367+
expect(Object.prototype.hasOwnProperty.call(field, 'defaultValue')).toBe(true);
1368+
expect(field.defaultValue).toBe(false);
1369+
});
1370+
1371+
it('admits exactly the declared types: each takes its value, every other FieldType is refused by name', () => {
1372+
expect(Object.keys(ADMITTED).every((t) => (FieldType.options as readonly string[]).includes(t))).toBe(true);
1373+
for (const type of FieldType.options) {
1374+
const admitted = ADMITTED[type];
1375+
// A value each refused type's own value class would hold, so the refusal is
1376+
// the type's, never the value's.
1377+
const value = admitted ? admitted.ok : (type === 'date' ? '2026-01-01' : type === 'number' ? 0 : 'x');
1378+
const result = lenientField({ type, ...withOptions(type), defaultValue: value });
1379+
if (admitted) {
1380+
expect({ type, success: result.success }).toEqual({ type, success: true });
1381+
expect(result.data!.objects[0].fields[0].defaultValue).toEqual(value);
1382+
} else {
1383+
expect({ type, success: result.success }).toEqual({ type, success: false });
1384+
expect({ type, named: namedRefusals(result, type).length }).toEqual({ type, named: 1 });
1385+
}
1386+
}
1387+
});
1388+
1389+
it('refuses a value of the wrong type for an admitted type, by name at the key', () => {
1390+
for (const [type, { wrong }] of Object.entries(ADMITTED)) {
1391+
const result = lenientField({ type, ...withOptions(type), defaultValue: wrong });
1392+
expect({ type, success: result.success }).toEqual({ type, success: false });
1393+
expect({ type, issues: result.error!.issues.length, named: namedRefusals(result, type).length })
1394+
.toEqual({ type, issues: 1, named: 1 });
1395+
}
1396+
});
1397+
1398+
it('refuses a select default that is not one of the field\'s OWN option values', () => {
1399+
for (const bad of ['Open', 'closed', 'done ']) {
1400+
expect(namedRefusals(lenientField({ type: 'select', options: OPTIONS, defaultValue: bad }), 'select')).toHaveLength(1);
1401+
}
1402+
// A value another field declares is not this field's option.
1403+
const other = SolutionBlueprintSchema.safeParse({
1404+
summary: 's',
1405+
objects: [{
1406+
name: 'task',
1407+
fields: [
1408+
{ name: 'priority', type: 'select', options: [{ label: 'High', value: 'high' }] },
1409+
{ name: 'flag', type: 'select', options: OPTIONS, defaultValue: 'high' },
1410+
],
1411+
}],
1412+
});
1413+
expect(other.error?.issues.map((i) => i.path)).toEqual([['objects', 0, 'fields', 1, 'defaultValue']]);
1414+
// With no options declared there is nothing a default could name.
1415+
expect(namedRefusals(lenientField({ type: 'radio', defaultValue: 'open' }), 'radio')).toHaveLength(1);
1416+
});
1417+
1418+
it('refuses a runtime token on an admitted type: a blueprint default is a literal', () => {
1419+
for (const [type, token] of [['text', 'NOW()'], ['time', 'now()'], ['select', 'current_user']] as const) {
1420+
const options = type === 'select' ? { options: [...OPTIONS, { label: 'Me', value: 'current_user' }] } : {};
1421+
expect(namedRefusals(lenientField({ type, ...options, defaultValue: token }), type)).toHaveLength(1);
1422+
}
1423+
});
1424+
1425+
it('refuses a list default on a multi-value field by name, beside the key\'s own union refusal', () => {
1426+
const result = lenientField({ type: 'multiselect', options: OPTIONS, defaultValue: ['open'] });
1427+
expect(result.success).toBe(false);
1428+
expect(namedRefusals(result, 'multiselect')).toHaveLength(1);
1429+
});
1430+
1431+
it('absent still parses on the lenient side; the strict mirror takes null and refuses the key missing', () => {
1432+
const lenient = lenientField({ type: 'boolean' });
1433+
expect(lenient.success).toBe(true);
1434+
expect('defaultValue' in lenient.data!.objects[0].fields[0]).toBe(false);
1435+
expect(strictField({ type: 'boolean', defaultValue: null }).success).toBe(true);
1436+
// Strict mode needs every key in `required`: a field without the key is refused at it.
1437+
const bare = SolutionBlueprintStrictSchema.safeParse({
1438+
summary: 's', assumptions: [], questions: null, views: null, dashboards: null, app: null,
1439+
objects: [{
1440+
name: 'task', label: null, description: null, sharingModel: null, nameField: null, stageField: null,
1441+
fields: [{
1442+
name: 'flag', label: null, type: 'boolean', required: null, reference: null, options: null,
1443+
summaryOperations: null, expression: null,
1444+
}],
1445+
}],
1446+
});
1447+
expect(bare.error?.issues.map((i) => i.path)).toEqual([PATH]);
1448+
});
1449+
1450+
it('the strict mirror accepts what the lenient schema accepts for each admitted type, and null', () => {
1451+
for (const [type, { ok }] of Object.entries(ADMITTED)) {
1452+
const options = type === 'select' || type === 'radio' ? { options: OPTIONS.map((o) => ({ ...o, kind: null })) } : {};
1453+
const strict = strictField({ type, ...options, defaultValue: ok });
1454+
expect({ type, success: strict.success }).toEqual({ type, success: true });
1455+
expect(strict.data!.objects[0].fields[0].defaultValue).toEqual(ok);
1456+
expect({ type, success: lenientField({ type, ...withOptions(type), defaultValue: ok }).success })
1457+
.toEqual({ type, success: true });
1458+
expect(strictField({ type, ...options, defaultValue: null }).success).toBe(true);
1459+
}
1460+
});
1461+
1462+
it('a value legal to generate is either kept by the lenient schema or refused there BY NAME, never stripped', () => {
1463+
// VALUE parity for a key whose rule spans `type`: the mirror's union cannot
1464+
// say which arm a type takes, so it generates values the lenient field
1465+
// refuses. Every such refusal must name the field's type at this key.
1466+
for (const type of FieldType.options) {
1467+
for (const value of [true, false, 0, 7.5, '', 'x', 'open', '09:00', '2026-01-01']) {
1468+
expect({ type, value, strict: strictField({ type, defaultValue: value }).success })
1469+
.toEqual({ type, value, strict: true });
1470+
const lenient = lenientField({ type, ...withOptions(type), defaultValue: value });
1471+
if (lenient.success) {
1472+
expect({ type, value, kept: lenient.data!.objects[0].fields[0].defaultValue })
1473+
.toEqual({ type, value, kept: value });
1474+
} else {
1475+
expect({ type, value, issues: lenient.error!.issues.length, named: namedRefusals(lenient, type).length })
1476+
.toEqual({ type, value, issues: 1, named: 1 });
1477+
}
1478+
}
1479+
}
1480+
});
1481+
1482+
it('every default the blueprint admits is one the built field\'s own schema admits', () => {
1483+
// The applier carries the value into the object field, where `FieldSchema`
1484+
// judges it again: a value legal to apply must be legal to stage.
1485+
for (const [type, { ok }] of Object.entries(ADMITTED)) {
1486+
const built = FieldSchema.safeParse({ name: 'flag', label: 'Flag', type, ...withOptions(type), defaultValue: ok });
1487+
expect({ type, success: built.success }).toEqual({ type, success: true });
1488+
expect(built.data?.defaultValue).toEqual(ok);
1489+
}
1490+
});
1491+
1492+
it('both describes state the admitted types and the boolean-flag rule', () => {
1493+
const describeOf = (root: any): string =>
1494+
root.shape.objects.element.shape.fields.element.shape.defaultValue.description;
1495+
for (const text of [describeOf(SolutionBlueprintSchema), describeOf(SolutionBlueprintStrictSchema)]) {
1496+
for (const type of Object.keys(ADMITTED)) expect(text).toContain(`"${type}"`);
1497+
expect(text).toContain('never `required`');
1498+
expect(text).toContain('defaultValue false');
1499+
expect(text).toContain('refused by name');
1500+
}
1501+
});
1502+
});

0 commit comments

Comments
 (0)