the package-authoring round: fork is the verb, the scaffold flag is the ready-to-run gate, the package editor is the owner's surface, and /packages is the launchpad
The design conversation settled the verb question (developer, 2026-11-19): a "create new package" with a base selector is fork under another name, so the SPA gains one list at /packages with three acts per card -- start (the run-create flow, unchanged), fork (any package, system or user, by anyone: the copy lands in state/packages owned by the forker, pc stripped, display name asked inline, the fork landing straight in the editor -- nobody forks to immediately start), and edit (the owner's own user packages only). /new retires to a redirect onto /packages; the source link (code.fleshless.org/hackmaster/tiny-worlds) rides the header.
The template's unplayability became machinery instead of a naming special-case: settings.json carries the "scaffold" flag (the records-bool precedent -- authored content carries the marker, machinery reads it). Create refuses a scaffold loudly on both ends ("fork it to author, then flip scaffold off in the editor"), the listing marks it, the SPA disables its start. The developer's ruling over my clear-the-flag draft: the flag RIDES FORKS UNTOUCHED -- a scaffold fork stays a scaffold until its author flips it off, so the flag is the package's doneness state, and publishing your own scaffold for others to fork is free. The settings editor is raw JSON for now (native UI later, the developer's "ready to run" toggle its eventual shape), with a hint teaching the keys.
The package editor (/packages/:name): every file of a user package is authoring material -- settings included -- with the cheap save lints (memory.json and settings.json must parse as JSON objects; the carryover passes the authored lint, the run surface's exact gate), the deep checks staying at the start gate where they already lived. The gates mirror the run precedent: system packages are repo-side and refuse ("fork to edit"); an ownerless user package is readable, forkable, never editable -- and an ownerless package IS forkable, the copy is yours.
The refactor the editor forced: the setup view's file-editor machinery (nav, textarea + the sigil/var highlight overlay, drafts, dirty marks, the save flow) extracted into widgets.mjs's fileEditor factory, parameterized over DOM ids and a PUT call -- setup keeps its run-side couplings (the phase-gated rw, the addressed session), the package editor is the simpler parameterization, and the editor CSS generalized from id selectors to classes so both surfaces share it.
En-route bug found and fixed: cmdCreate always copied the REPO's handbook.md into the run, so a user package's handbook -- which save-package faithfully copies out -- was silently ignored on re-create; the author's craft edit died in the round-trip. Create now prefers the package's own handbook when it carries one, else the repo's shared copy. The smoke proves the round-trip: a handbook marker authored through the package editor surfaces in the created run's files.
Browser-pass find: the pkgedit header state span and the editor's save-state span shared the id pkgedit-state, so the widget's select() wiped the header line -- the editor's span renamed (pkgedit-edit-state), verified rendering.
Verified: typecheck green both programs; smoke ALL PASS (+20 -- the fork battery: the template fork owner-stamped scaffold-preserved pc-stripped, create refusing the scaffold on both ends, the playable fork carrying no scaffold, fork validation, the editor gates owner-200/non-owner-403/system-repo-side-403/unknown-404, the raw-JSON and carryover save lints, the ready-to-run flip, create from the flipped fork, the handbook round-trip; and the ownerless pair: the editor refuses, the fork succeeds); restart + reminder checks green (6 runs); a browser pass on --mock --debug end to end -- the paste token re-landing after the state wipe, /new redirecting with the URL honest, the template card disabled with the scaffold note, boutique disabled as incomplete, the fork form prefill and submit landing in the editor, the scaffold state line, the settings flip and save making the card startable, the start-form flow intact, zero console errors; check-repo clean; the debug server back on the real state. Docs: README's packages passage (fork, scaffold, the editor, the gates), the booting flow, the layout tree (the clerk's fork-package verb, the view list, widgets.mjs); DICTIONARY's package, scaffold, and fork entries. The developer's queue line ('"create new package" in SPA') is struck by them in the same round. packages/detention stays untracked WIP, untouched.
This commit is contained in:
+116
@@ -667,6 +667,122 @@ route('POST', '/api/packages', async ({ body, user }) => {
|
||||
return { package: JSON.parse(r.out) };
|
||||
});
|
||||
|
||||
// fork a package: the copy-to-author act (developer, 2026-11-19).
|
||||
// Any package, system or user, forkable by anyone -- the copy lands
|
||||
// in state/packages owned by the forker, scaffold flag preserved.
|
||||
route('POST', '/api/packages/fork', async ({ body, user }) => {
|
||||
const from = String(body.from ?? '').trim();
|
||||
const name = String(body.name ?? '').trim();
|
||||
if (!from) throw new HttpError(400, 'from (a package name) is required');
|
||||
if (!name) throw new HttpError(400, 'name (the new package\'s display name) is required');
|
||||
requireUser(user);
|
||||
const r = clerkRaw('--quiet', '--json', '--owner', user.id, 'fork-package', from, name);
|
||||
if (r.code !== 0) throw new HttpError(400, r.err || r.out || 'fork failed');
|
||||
return { package: JSON.parse(r.out) };
|
||||
});
|
||||
|
||||
// --- the package editor ---------------------------------------------------------
|
||||
//
|
||||
// The owner's authoring surface over a user package: the same fixed
|
||||
// whitelist shape as the run files API, every file rw (a package is
|
||||
// pre-run -- all of it is authoring material, settings included).
|
||||
// System packages are repo-side and refuse; ownerless user packages
|
||||
// are the departed's and refuse (the run precedent: an ownerless
|
||||
// thing is readable and forkable, never editable). Save-time gates
|
||||
// are the cheap cousins of the start gate's: memory.json and
|
||||
// settings.json must parse as JSON objects, the carryover must pass
|
||||
// the authored lint -- the deep checks still run at run creation.
|
||||
|
||||
/** One file of the package editor surface. */
|
||||
/** @typedef {{ mode: 'rw', path: string }} PkgFileSpec */
|
||||
|
||||
/** @param {string} name @returns {?string} */
|
||||
function packageDirOf(name) {
|
||||
if (!/^[a-z0-9-]+$/.test(name)) return null;
|
||||
const sys = path.join(ROOT, 'packages', name);
|
||||
if (fs.existsSync(sys)) return sys;
|
||||
const usr = path.join(ROOT, 'state', 'packages', name);
|
||||
if (fs.existsSync(usr)) return usr;
|
||||
return null;
|
||||
}
|
||||
|
||||
/** The editor's gate: resolves the package dir for its owner, or
|
||||
* refuses -- system (repo-side) and ownerless packages are not
|
||||
* editable by anyone.
|
||||
* @param {?UserRow} user @param {string} name @returns {string} */
|
||||
function requirePackageOwner(user, name) {
|
||||
const dir = packageDirOf(name);
|
||||
if (!dir) throw new HttpError(404, `no such package: ${name}`);
|
||||
if (!fs.existsSync(path.join(dir, 'package.json'))) {
|
||||
throw new HttpError(403, 'system packages are repo-side and read-only -- fork to edit');
|
||||
}
|
||||
try {
|
||||
const meta = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8'));
|
||||
if (meta.owner && user && meta.owner === user.id) return dir;
|
||||
} catch { /* unreadable meta -- not editable */ }
|
||||
throw new HttpError(403, 'this package is not yours to edit');
|
||||
}
|
||||
|
||||
/** @param {string} dir @returns {Record<string, PkgFileSpec>} */
|
||||
function packageFileSurface(dir) {
|
||||
/** @type {Record<string, PkgFileSpec>} */
|
||||
const spec = {};
|
||||
for (const f of ['handbook.md', 'backdrop.md', 'character.md', 'memory.json', 'carryover.jsonl', 'settings.json', 'gm.md']) {
|
||||
spec[f] = { mode: 'rw', path: path.join(dir, f) };
|
||||
}
|
||||
return spec;
|
||||
}
|
||||
|
||||
route('GET', '/api/packages/:name/files', async ({ params, user }) => {
|
||||
const dir = requirePackageOwner(user, params.name);
|
||||
const spec = packageFileSurface(dir);
|
||||
/** @type {Record<string, { mode: string, exists: boolean, content?: string }>} */
|
||||
const files = {};
|
||||
for (const [f, s] of Object.entries(spec)) {
|
||||
files[f] = fs.existsSync(s.path)
|
||||
? { mode: s.mode, exists: true, content: fs.readFileSync(s.path, 'utf8') }
|
||||
: { mode: s.mode, exists: false };
|
||||
}
|
||||
const settingsFile = files['settings.json'];
|
||||
const settings = settingsFile?.exists ? JSON.parse(/** @type {string} */ (settingsFile.content)) : {};
|
||||
return { display: settings.name ?? params.name, scaffold: settings.scaffold === true, files };
|
||||
});
|
||||
|
||||
route('PUT', '/api/packages/:name/files/:path', async ({ params, body, user }) => {
|
||||
const dir = requirePackageOwner(user, params.name);
|
||||
const spec = packageFileSurface(dir)[params.path];
|
||||
if (!spec) throw new HttpError(404, `no such package file in the API surface: ${params.path}`);
|
||||
if (typeof body.content !== 'string' || !body.content.trim()) throw new HttpError(400, 'content (a non-empty string) is required');
|
||||
if (params.path === 'memory.json' || params.path === 'settings.json') {
|
||||
// the save-time gate is syntax only: JSON objects, the same cheap
|
||||
// cousin as the run surface's -- the start gate judges the rest
|
||||
try {
|
||||
const o = JSON.parse(body.content);
|
||||
if (typeof o !== 'object' || o === null || Array.isArray(o)) throw new Error();
|
||||
} catch {
|
||||
throw new HttpError(400, `${params.path} must be a JSON object`);
|
||||
}
|
||||
}
|
||||
if (params.path === 'carryover.jsonl') {
|
||||
// only clean openings land: the authored lint, the run surface's
|
||||
// exact save-time gate
|
||||
const lines = [];
|
||||
const raw = body.content.split('\n');
|
||||
for (let i = 0; i < raw.length; i++) {
|
||||
if (!raw[i].trim()) continue;
|
||||
try {
|
||||
lines.push(JSON.parse(raw[i]));
|
||||
} catch {
|
||||
throw new HttpError(400, `not valid JSONL: line ${i + 1} does not parse`);
|
||||
}
|
||||
}
|
||||
const errors = session.lintCarryover(lines, { authored: true });
|
||||
if (errors.length) throw new HttpError(400, `the carryover did not pass the lint: ${errors.join('; ')}`);
|
||||
}
|
||||
fs.writeFileSync(spec.path, body.content);
|
||||
return { ok: true, file: params.path, bytes: Buffer.byteLength(body.content) };
|
||||
});
|
||||
|
||||
// the repo-fed prompts, read-only: per-project opening + guards,
|
||||
// substrate invariants, and the per-turn reminder -- never copied into
|
||||
// a run, so the editor shows them as context, not as something to edit
|
||||
|
||||
+1
-1
@@ -148,7 +148,7 @@ export {};
|
||||
|
||||
/** settings.json: identity (pc/name are the @ sigils), declared vars,
|
||||
* the records flag. */
|
||||
/** @typedef {{ pc?: string, name?: string, records?: boolean, vars?: Record<string, string> }} RunSettings */
|
||||
/** @typedef {{ pc?: string, name?: string, records?: boolean, scaffold?: boolean, vars?: Record<string, string> }} RunSettings */
|
||||
|
||||
/** A housekeeping manifest -- the housekeeper's whole write surface. */
|
||||
/** @typedef {{ ring: 'a'|'b', type: string, label: string, detail?: string, reason: string }} InsertOp */
|
||||
|
||||
Reference in New Issue
Block a user