Skip to content

Commit f10cb5d

Browse files
fix(cli): patch hyperframes.json in place when seeding the authoring skill
seedProjectAuthoringSkill is the only writer that touches an already existing hyperframes.json — every other writeProjectConfig call site is guarded to write only when the file is absent, which made the whole-file overwrite safe by construction. Round-tripping the seed through normalizeConfig broke that: it rebuilds the object from a field whitelist with no rest-spread, so any key outside the schema was silently dropped, a media block was materialized in projects that never had one, and key order was rewritten. hyperframes.json is normally committed, so a render introduced a diff the user never asked for, and any field added to the schema later would be deleted by a render on an older CLI. Parse the raw JSON, set authoringSkill, write it back, reusing the file's own indentation. Unknown keys and formatting survive; the only delta is the key being added. A corrupt config is now left untouched instead of clobbered. Seed-once semantics are unchanged, still normalized so a hand-edited garbage slug neither reaches telemetry nor wedges the seed. Reported independently by both reviewers on #2762.
1 parent ed1927e commit f10cb5d

2 files changed

Lines changed: 103 additions & 8 deletions

File tree

‎packages/cli/src/utils/projectConfig.test.ts‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,5 +289,79 @@ describe("projectConfig", () => {
289289
rmSync(dir, { recursive: true, force: true });
290290
}
291291
});
292+
293+
// The seed is the only writer that touches an existing hyperframes.json,
294+
// which is normally committed — a render must not diff it beyond the one
295+
// key being added. Guards against round-tripping through normalizeConfig.
296+
it("preserves config keys outside the known schema", () => {
297+
const dir = tmp();
298+
try {
299+
writeFileSync(
300+
projectConfigPath(dir),
301+
JSON.stringify(
302+
{
303+
registry: "https://example.com/my-registry",
304+
myTeamSetting: { reviewer: "wenbo", keep: true },
305+
futureSchemaKey: 42,
306+
},
307+
null,
308+
2,
309+
),
310+
"utf-8",
311+
);
312+
seedProjectAuthoringSkill(dir, "product-launch-video");
313+
const raw = JSON.parse(readFileSync(projectConfigPath(dir), "utf-8"));
314+
expect(raw.authoringSkill).toBe("product-launch-video");
315+
expect(raw.myTeamSetting).toEqual({ reviewer: "wenbo", keep: true });
316+
expect(raw.futureSchemaKey).toBe(42);
317+
expect(raw.registry).toBe("https://example.com/my-registry");
318+
} finally {
319+
rmSync(dir, { recursive: true, force: true });
320+
}
321+
});
322+
323+
it("does not materialize a media block the user never wrote", () => {
324+
const dir = tmp();
325+
try {
326+
writeFileSync(
327+
projectConfigPath(dir),
328+
JSON.stringify({ registry: "https://example.com/r" }, null, 2),
329+
"utf-8",
330+
);
331+
seedProjectAuthoringSkill(dir, "motion-graphics");
332+
const raw = JSON.parse(readFileSync(projectConfigPath(dir), "utf-8"));
333+
expect(raw.media).toBeUndefined();
334+
expect(raw.$schema).toBeUndefined();
335+
expect(Object.keys(raw)).toEqual(["registry", "authoringSkill"]);
336+
} finally {
337+
rmSync(dir, { recursive: true, force: true });
338+
}
339+
});
340+
341+
it("reuses the file's own indentation", () => {
342+
const dir = tmp();
343+
try {
344+
writeFileSync(
345+
projectConfigPath(dir),
346+
JSON.stringify({ registry: "https://example.com/r" }, null, 4),
347+
"utf-8",
348+
);
349+
seedProjectAuthoringSkill(dir, "pr-to-video");
350+
expect(readFileSync(projectConfigPath(dir), "utf-8")).toContain('\n "registry"');
351+
} finally {
352+
rmSync(dir, { recursive: true, force: true });
353+
}
354+
});
355+
356+
it("leaves a corrupt config untouched rather than clobbering it", () => {
357+
const dir = tmp();
358+
try {
359+
writeFileSync(projectConfigPath(dir), "{ not valid json", "utf-8");
360+
seedProjectAuthoringSkill(dir, "faceless-explainer");
361+
expect(readFileSync(projectConfigPath(dir), "utf-8")).toBe("{ not valid json");
362+
} finally {
363+
rmSync(dir, { recursive: true, force: true });
364+
}
365+
});
292366
});
293367
});

‎packages/cli/src/utils/projectConfig.ts‎

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* point at custom registries or reshape their project layout.
88
*/
99

10-
import { readFileSync, writeFileSync } from "node:fs";
10+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
1111
import { join, resolve } from "node:path";
1212
import { DEFAULT_REGISTRY_URL } from "../registry/index.js";
1313
import { normalizeSkillSlug } from "../telemetry/skill.js";
@@ -151,18 +151,39 @@ export function resolveAutoProxy(projectDir: string, flagValue: boolean | undefi
151151
* render's telemetry but does not rewrite the project's owner). An invalid or
152152
* empty slug is ignored. Best effort: a read-only or missing project directory
153153
* never fails the render it rode in on.
154+
*
155+
* This is the only writer that touches an ALREADY EXISTING `hyperframes.json`
156+
* (every other `writeProjectConfig` call site is guarded to write only when the
157+
* file is absent), so it must not round-trip through {@link normalizeConfig}:
158+
* that rebuilds the object from a field whitelist, which would drop keys it
159+
* does not know about and materialize defaults the user never wrote. The file
160+
* is normally committed, so a render must not introduce a diff beyond the one
161+
* key being added. Patch the parsed JSON in place instead, reusing the file's
162+
* own indentation.
154163
*/
155164
export function seedProjectAuthoringSkill(projectDir: string, rawSkill: unknown): void {
156165
const skill = normalizeSkillSlug(rawSkill);
157166
if (!skill) return;
167+
const path = projectConfigPath(projectDir);
158168
try {
159-
const current = readProjectConfig(projectDir);
160-
if (current?.authoringSkill) return;
161-
writeProjectConfig(projectDir, {
162-
...(current ?? DEFAULT_PROJECT_CONFIG),
163-
authoringSkill: skill,
164-
});
169+
if (!existsSync(path)) {
170+
writeProjectConfig(projectDir, { ...DEFAULT_PROJECT_CONFIG, authoringSkill: skill });
171+
return;
172+
}
173+
const text = readFileSync(path, "utf-8");
174+
const parsed: unknown = JSON.parse(text);
175+
// A non-object config is malformed; leave the user's file untouched rather
176+
// than clobbering it from a render.
177+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return;
178+
const raw = parsed as Record<string, unknown>;
179+
// Seed-once. Normalized so a hand-edited garbage slug neither reaches
180+
// telemetry nor wedges the seed — the next `--skill` render heals it.
181+
if (normalizeSkillSlug(raw.authoringSkill)) return;
182+
raw.authoringSkill = skill;
183+
const indent = /\n([ \t]+)"/.exec(text)?.[1] ?? " ";
184+
writeFileSync(path, JSON.stringify(raw, null, indent) + "\n", "utf-8");
165185
} catch {
166-
// Attribution is best-effort telemetry, never a render blocker.
186+
// Corrupt JSON, or a read-only project directory: attribution is
187+
// best-effort telemetry, never a render blocker.
167188
}
168189
}

0 commit comments

Comments
 (0)