Fix five defects found by running Motion rather than reading it
CI / verify (push) Successful in 7m21s
CI / publish (push) Has been skipped

The markdown parser was in the eager entry chunk. `manualChunks` in its
object form does not leave an unlisted vendor package to Vite's async
splitting, so react-markdown was hoisted into the entry even though its only
importers are lazy routes — 327.70 kB gzip against a 314 kB baseline, on the
one download every route pays for. Naming it as its own chunk puts it back
behind the Motion pages and takes the entry to 282.21 kB, below where it was
before Motion existed.

The starter library could never be improved. Seeding was insert-only, so a
deployment seeded in August was frozen on August's wording for ever with no
upgrade path short of editing production rows by hand — for a feature whose
entire premise is that the library gets better. A second run now refreshes a
starter row, but only while it is still ours: `is_system`, `usage_count = 0`
and no owner. That is the same condition §7a already enforces on the API, so
a template an engagement was cut from is left alone and reported by name
rather than silently overwritten.

The refresh was not idempotent, and the seed lied about it. `jsonb` does not
preserve key order — Postgres sorts keys by length then bytewise — so
comparing `JSON.stringify(stored)` against `JSON.stringify(authored)` marked
every template as changed on every run, and the seed rewrote nine rows each
time while reporting itself clean. Comparison is now canonical. Found by
running the seed three times and reading the counts.

`motion-overflow-check.mjs` measured less than it claimed. It seeded
`pig.sidebar` and `pig.piggy.dock`, neither of which anything reads (the keys
are `pig.sidebarOpen` and `pig.piggyDockOpen`), so the layout it pinned was
whatever the last run left. Its dark pass set `colorScheme` only, and the
appearance preference is stored server-side and adopted after hydration, so
the dark pass measured the light palette a beat after first paint. It now
rewrites the profile response as `screenshots.mjs` does, asserts the rendered
`data-theme`, and fails a page that renders almost no text — a page that
throws inside its own body otherwise measures zero overflow and passes.

The stage rail rendered "1 templates", in the visible label and in every
aria-label. Singular and plural are now both passed.

Also normalised `artifact` to `artefact` in the seeded prose, which had
drifted American in the playbook. The `artifacts` field key is untouched:
FieldsView reads it, and already labels it in British.
This commit is contained in:
2026-08-19 00:07:08 -07:00
parent 15c72ade1c
commit 2f32186d22
12 changed files with 187 additions and 38 deletions
+9 -2
View File
@@ -350,9 +350,16 @@ async function seed() {
// ------------------------------------------------ the Motion starter library
const motion = await seedMotionLibrary(db);
console.log(
` ${motion.total} Motion starter template(s) (${motion.added} new) — authored content, ` +
'shared and system-owned, version 1 of their lineages.',
` ${motion.total} Motion starter template(s) (${motion.added} new, ${motion.refreshed} ` +
'refreshed) — authored content, shared and system-owned, version 1 of their lineages.',
);
if (motion.held.length) {
console.log(
` ${motion.held.length} starter template(s) have newer wording here and were LEFT ALONE, ` +
`because they are in use or have been adopted: ${motion.held.join(', ')}. ` +
'Publish a new version rather than editing one an engagement was cut from.',
);
}
for (const bad of motion.rejected) {
console.error(` SKIPPED a starter template with a value outside the ontology: ${bad}`);
}
File diff suppressed because one or more lines are too long
+107 -14
View File
@@ -19,12 +19,22 @@
* against this to confirm the attribute syntax is understood by each, since
* the server runs the TypeScript directly.
*
* Idempotency is `onConflictDoNothing({ target: [slug, version] })`, which
* works **only** because of `motion_templates_slug_version_key`. AGENTS.md §5
* is blunt about what that clause does without a constraint to fire on — it
* silently duplicated seed data here twice — so `assertNoDuplicates` below
* checks the outcome rather than trusting the schema, and CI counts this table
* in its idempotency gate alongside `contacts`.
* Idempotency is keyed on `(slug, version)`, which is enforceable **only**
* because of `motion_templates_slug_version_key`. AGENTS.md §5 is blunt about
* what `onConflictDoNothing` does without a constraint to fire on — it silently
* duplicated seed data here twice — so `assertNoDuplicates` below checks the
* outcome rather than trusting the schema, and CI counts this table in its
* idempotency gate alongside `contacts`.
*
* A second run REFRESHES a starter row rather than skipping it, but only while
* that row is still ours: `is_system`, nobody has forked or instantiated it
* (`usage_count = 0`) and it has no owner. Insert-only was the first version and
* is wrong for the one thing this library is for — the content improves, and a
* deployment seeded in August would otherwise be frozen on August's wording for
* ever, with no upgrade path short of hand-editing production rows. The
* pristine test is what keeps that from becoming a write over somebody's work:
* the moment a template has been used, §7a says it is never edited in place,
* and this respects that with the same condition the API enforces.
*/
import { DEMAND_STAGES, isMotionKind, type DemandStage, type MotionKind } from '@pig/core';
import { and, eq, inArray } from 'drizzle-orm';
@@ -89,6 +99,29 @@ const isDemandStage = (value: string): value is DemandStage =>
* inside `fields` means promotion carries it forward with the rest of the
* structured payload without any special handling in the promote path.
*/
/*
* Key order is not preserved by `jsonb`: Postgres stores an object with its
* keys sorted by length and then bytewise, so the value that comes back is
* rarely the value that went in. A plain `JSON.stringify` comparison therefore
* reported every starter template as changed on every run, and the seed
* rewrote nine rows each time while claiming to be idempotent. Found by
* running it twice and reading the count, not by reading the code.
*/
function canonical(value: unknown): string {
const order = (node: unknown): unknown => {
if (Array.isArray(node)) return node.map(order);
if (node && typeof node === 'object') {
return Object.fromEntries(
Object.keys(node as Record<string, unknown>)
.sort()
.map((key) => [key, order((node as Record<string, unknown>)[key])]),
);
}
return node;
};
return JSON.stringify(order(value));
}
function toRow(
template: StarterTemplate,
kind: MotionKind,
@@ -144,9 +177,17 @@ async function assertNoDuplicates(db: Database, slugs: readonly string[]): Promi
export async function seedMotionLibrary(
db: Database,
): Promise<{ total: number; added: number; rejected: readonly string[] }> {
): Promise<{
total: number;
added: number;
refreshed: number;
held: readonly string[];
rejected: readonly string[];
}> {
const rejected: string[] = [];
const held: string[] = [];
let added = 0;
let refreshed = 0;
for (const template of STARTER_LIBRARY) {
/*
@@ -161,12 +202,64 @@ export async function seedMotionLibrary(
continue;
}
const [created] = await db
.insert(motionTemplates)
.values(toRow(template, template.kind, template.stage))
.onConflictDoNothing({ target: [motionTemplates.slug, motionTemplates.version] })
.returning({ id: motionTemplates.id });
if (created) added += 1;
const row = toRow(template, template.kind, template.stage);
const [existing] = await db
.select({
id: motionTemplates.id,
isSystem: motionTemplates.isSystem,
usageCount: motionTemplates.usageCount,
ownerUserId: motionTemplates.ownerUserId,
title: motionTemplates.title,
summary: motionTemplates.summary,
body: motionTemplates.body,
fields: motionTemplates.fields,
kind: motionTemplates.kind,
stage: motionTemplates.stage,
})
.from(motionTemplates)
.where(and(eq(motionTemplates.slug, template.slug), eq(motionTemplates.version, 1)))
.limit(1);
if (!existing) {
const [created] = await db
.insert(motionTemplates)
.values(row)
.onConflictDoNothing({ target: [motionTemplates.slug, motionTemplates.version] })
.returning({ id: motionTemplates.id });
if (created) added += 1;
continue;
}
const pristine =
existing.isSystem && existing.usageCount === 0 && existing.ownerUserId === null;
const changed =
existing.title !== row.title ||
existing.summary !== row.summary ||
existing.body !== row.body ||
existing.kind !== row.kind ||
existing.stage !== row.stage ||
canonical(existing.fields) !== canonical(row.fields);
if (pristine && changed) {
await db
.update(motionTemplates)
.set({
title: row.title,
summary: row.summary,
body: row.body,
fields: row.fields,
kind: row.kind,
stage: row.stage,
updatedAt: new Date(),
})
.where(eq(motionTemplates.id, existing.id));
refreshed += 1;
} else if (changed) {
// Reported rather than forced: the row has been used or adopted, so
// overwriting it would edit a template a live engagement was cut from.
held.push(template.slug);
}
}
const total = await assertNoDuplicates(
@@ -174,5 +267,5 @@ export async function seedMotionLibrary(
STARTER_LIBRARY.map((template) => template.slug),
);
return { total, added, rejected };
return { total, added, refreshed, held, rejected };
}
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long