Fix/sdk default (#2199)

* feat(plugin-sdk): settings-field default + unknown-attribute warning

* feat(shared): default on the plugin settings-field contract

* feat(server): persist and serve plugin settings-field defaults

* feat(client): pre-fill plugin settings defaults in the admin and user forms

* feat(server): enforce required plugin settings on both save paths

* feat(client): block plugin-settings save on an empty required field

* feat(server): resolve plugin settings defaults at runtime

A settings-field `default` was stored and served to the form but never
reached the plugin: the child's init envelope, `ctx.settings.get()`, the
channel dispatch's readAll and the OAuth provider config all read the
stored blob only. Fold `stored ?? default` in on every path that hands
config to a plugin (settings-defaults.ts), and let a default satisfy
`required` on both save gates — refusing a save the runtime would have
accepted was a contradiction.

Also: one `isFilled` rule shared by the save gate and `hasRequired`
(checkbox exempt, whitespace empty); the install parser drops a
non-boolean checkbox default or a select default outside its options,
matching what the SDK already refuses; export SETTING_FIELD_KEYS so the
SDK's mirror can be parity-tested.

* feat(client): share the plugin settings form rules across all three forms

The mobile user settings screen was a third consumer of the fields/config
contract that never got `default` pre-fill or the required check, and
the seed/patch loops lived in five hand copies. Extract
seedSettingsValues / findMissingRequired / settingsPatch into one module
and use it from the admin instance form, the desktop user tab and the
mobile screen. The user forms now surface a 4xx `{ error }` from the
server (the required-field refusal) instead of the generic toast.

* feat(plugin-sdk): seed dev fixtures from manifest defaults

`trek-plugin dev` handed the plugin only the fixture values, so a plugin
relying on its manifest defaults read undefined in dev and only in dev.
Seed ctx.config / ctx.settings from the declared defaults (a fixture
value still wins) via a new exported settingDefaults(manifest, scope).
SETTING_FIELD_KEYS is now parity-tested against the host's list.

* docs(plugins): document settings defaults as runtime fallbacks

A `default` is the field's effective value wherever nobody set one, not
just a form pre-fill; `required` is enforced on both ends; unknown
attributes are dropped at install and warned on by validate. Add a
Settings section with the attribute table to the SDK README and update
the ctx type comments, the shared schema, and the wiki guide.

* add missing exports
This commit is contained in:
jubnl
2026-09-02 18:42:16 +02:00
committed by GitHub
parent 17668e5ff2
commit 1e6ab069cd
86 changed files with 1022 additions and 110 deletions
+29 -1
View File
@@ -11,7 +11,7 @@
*/
import { createHash } from 'node:crypto';
import path from 'node:path';
import { validateManifest, isSatisfiableRange, isUnboundedRange, KNOWN_ADDONS } from '../../manifest.js';
import { validateManifest, isSatisfiableRange, isUnboundedRange, KNOWN_ADDONS, SETTING_FIELD_KEYS } from '../../manifest.js';
// The hosts a plugin can ACTUALLY reach. See `code.egress-reachable` for why this is the
// only list that counts — and permissions.ts for why dev's egress guard uses the same one.
import { grantedHosts } from '../../permissions.js';
@@ -216,6 +216,33 @@ const manifestAddonsKnown: OfflineCheck = {
},
};
const manifestSettingsKnownKeys: OfflineCheck = {
id: 'manifest.settings-known-keys',
stage: 'manifest',
depth: 'offline',
severity: 'warn',
title: 'settings fields carry only attributes the SDK knows',
run: (c) => {
const settings = c.manifest?.settings;
const fields = (Array.isArray(settings) ? settings : []).filter(
(s): s is Record<string, unknown> => !!s && typeof s === 'object',
);
if (!fields.length) return skip();
const unknown: string[] = [];
for (const f of fields) {
for (const k of Object.keys(f)) {
if (!(SETTING_FIELD_KEYS as readonly string[]).includes(k)) unknown.push(`${String(f.key ?? '?')}.${k}`);
}
}
return unknown.length
? fail(
unknown.join(', '),
`TREK stores only the attributes it knows and SILENTLY DROPS the rest at install — a typo ("defalt")\nor an attribute from a newer SDK simply does nothing, with no diagnostic anywhere.\nKnown: ${SETTING_FIELD_KEYS.join(', ')}.`,
)
: pass(`${fields.length} field(s) clean`);
},
};
const manifestNoEmoji: OfflineCheck = {
id: 'manifest.no-emoji',
stage: 'manifest',
@@ -592,6 +619,7 @@ export const OFFLINE_CHECKS: OfflineCheck[] = [
manifestTrekRange,
manifestIdMatchesDir,
manifestAddonsKnown,
manifestSettingsKnownKeys,
manifestNoEmoji,
codeServerEntry,
codeEgressReachable,
+13 -1
View File
@@ -18,6 +18,7 @@
* Dependency-free: node:http + node:sqlite (when present) + built-ins.
*/
import fs from 'node:fs';
import { settingDefaults } from '../manifest.js';
import path from 'node:path';
import http from 'node:http';
import { createRequire } from 'node:module';
@@ -292,6 +293,14 @@ function loadFixtures(dir: string): Fixtures {
}
/** Watch a directory tree with per-dir fs.watch (works on every platform, unlike recursive). */
function withManifestDefaults(fx: Fixtures, manifest: unknown): Fixtures {
return {
...fx,
config: { ...settingDefaults(manifest, 'instance'), ...(fx.config ?? {}) },
userSettings: { ...settingDefaults(manifest, 'user'), ...(fx.userSettings ?? {}) },
};
}
function watchTree(root: string, onChange: () => void): void {
const skip = new Set(['node_modules', '.git', '.trek-dev']);
const watch = (d: string) => {
@@ -310,7 +319,10 @@ export async function runDev(dir: string, opts: { port?: number } = {}): Promise
const manifest = readJsonFile<Record<string, unknown>>(manifestPath);
const id = String(manifest.id);
const grants = new Set(Array.isArray(manifest.permissions) ? (manifest.permissions as string[]) : []);
const fx = loadFixtures(abs);
// Manifest `default`s are the host's effective value for anything unset, so seed the
// fixtures with them (a fixture value still wins) — otherwise a plugin relying on its
// defaults would read undefined here and only here.
const fx = withManifestDefaults(loadFixtures(abs), manifest);
const { db, note: dbNote, close: closeDb } = createDevDb(path.join(abs, '.trek-dev', 'db.sqlite'));
const broadcasts: unknown[] = [];
installSdkInjection();