pantheon-opencode 2.0.0-beta.3 → 2.0.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,15 +3,14 @@
3
3
  *
4
4
  * TUI-local copy doctrine (same as cost-data.ts): the bundle must stay
5
5
  * self-contained. The behaviors registry mirrors routing.yml's `behaviors:`
6
- * (drift-pinned by tests/pantheon-settings-state.test.mjs); the Gates section
7
- * reads and writes the GLOBAL overrides file — the same one
8
- * `pantheon-opencode`'s installer and doctor consume
9
- * (`<config-dir>/pantheon-overrides.json`).
6
+ * (drift-pinned by tests/pantheon-settings-state.test.mjs).
10
7
  *
11
- * Sections: the window renders one section per page. This module owns the
12
- * section/row model and the Gates section; other sections live in sibling
13
- * TUI-local modules (j1-gate-status.ts, update-status.ts, …) and follow the
14
- * same shape.
8
+ * Model (2.0.0-beta.4, the quota-style full window): the window is a set of
9
+ * TABS; each tab holds GROUPS of setting ROWS; a row carries a display VALUE
10
+ * and — when the setting is editable — the CYCLE of allowed values that
11
+ * ←/→ steps through. Edits stage into a DRAFT (id → pending value); `save`
12
+ * applies the draft through injected writers, `reset` drops it. The disk
13
+ * never changes before save.
15
14
  */
16
15
 
17
16
  export interface SettingsBehavior {
@@ -75,84 +74,19 @@ export interface OverrideEntry {
75
74
 
76
75
  export type EffectiveOverrides = Record<string, OverrideEntry>
77
76
 
78
- /** Which behaviors the settings window can toggle (runtime + install skills). */
77
+ /** Which behaviors the settings window can write (runtime + install skills). */
79
78
  export const SETTINGS_TOGGLABLE_IDS = new Set(SETTINGS_BEHAVIORS.map((b) => b.id))
80
79
 
81
80
  export function isBehaviorDisabled(effective: EffectiveOverrides, id: string): boolean {
82
81
  return effective[id]?.action === 'disable'
83
82
  }
84
83
 
85
- // ── Section / row model ────────────────────────────────────────────────
86
-
87
- export interface SettingsRow {
88
- /** Namespaced action key (`gate.<behavior-id>`, `update.check`, …). */
89
- id: string
90
- title: string
91
- description: string
92
- /** Gates rows: the override disables the behavior (`[x]` marker). */
93
- disabled?: boolean
94
- /** Overrides the `[x]`/`[ ]` marker (`→` action, `✓`/`!` status). */
95
- marker?: string
96
- /** false → skipped by up/down and never dispatched by enter (info lines). */
97
- selectable?: boolean
98
- /** The effective entry comes from the PROJECT overrides file (read-only). */
99
- source?: 'global' | 'project'
100
- }
101
-
102
- export interface SettingsSection {
103
- id: string
104
- title: string
105
- /** One muted hint line under the rows. */
106
- hint?: string
107
- rows: SettingsRow[]
108
- }
109
-
110
- /** The rows up/down can reach and enter can dispatch on, in order. */
111
- export function selectableRows(section: SettingsSection): SettingsRow[] {
112
- return section.rows.filter((row) => row.selectable !== false)
113
- }
114
-
115
- /** Flat selection order across every section of the window. */
116
- export function flatSelectableRows(sections: readonly SettingsSection[]): SettingsRow[] {
117
- return sections.flatMap((section) => selectableRows(section))
118
- }
119
-
120
- export function firstSelectableId(sections: readonly SettingsSection[]): string | undefined {
121
- return flatSelectableRows(sections)[0]?.id
122
- }
123
-
124
- // ── Overrides load (global + project files) ────────────────────────────
125
-
126
- export interface OverridesLoad {
127
- global: EffectiveOverrides
128
- project: EffectiveOverrides
129
- /** The GLOBAL file exists but does not parse — writes are refused. */
130
- corruptGlobal: boolean
131
- }
132
-
133
- /**
134
- * Merge for DISPLAY only, mirroring the runtime precedence: the project file
135
- * wins per id (whole entry). The window still writes only the global file.
136
- */
137
- export function mergedOverrides(load: OverridesLoad): EffectiveOverrides {
138
- return { ...load.global, ...load.project }
139
- }
140
-
141
- export function canWriteOverrides(load: OverridesLoad): boolean {
142
- return !load.corruptGlobal
143
- }
144
-
145
- // ── Toggle semantics ───────────────────────────────────────────────────
146
-
147
84
  export const TOGGLE_REASON = 'toggled in /pantheon-settings'
148
85
 
149
86
  /**
150
- * Toggle: disabled -> enabled; enabled -> disabled.
151
- *
152
- * A `replace` entry is never destroyed by a toggle ("never trap user state"):
153
- * disabling keeps its `content` under the disable entry, and re-enabling
154
- * restores the replace. A settings-written disable (no content) is removed
155
- * outright so the overrides file does not accumulate toggle noise.
87
+ * Toggle (a targeted write keeps the toggle semantics: a `replace` entry is
88
+ * never destroyed — disabling keeps its `content`, re-enabling restores the
89
+ * replace; a settings-written disable is removed outright, no toggle noise).
156
90
  */
157
91
  export function toggledOverrides(
158
92
  effective: EffectiveOverrides,
@@ -163,8 +97,6 @@ export function toggledOverrides(
163
97
  const entry = next[id]
164
98
  if (entry?.action === 'disable') {
165
99
  if (entry.content !== undefined) {
166
- // A settings-written disable restores a clean replace; a hand-authored
167
- // disable+content keeps its own provenance reason.
168
100
  next[id] =
169
101
  entry.reason === reason
170
102
  ? { action: 'replace', content: entry.content }
@@ -185,60 +117,227 @@ export function toggledOverrides(
185
117
  return next
186
118
  }
187
119
 
120
+ /** Set an explicit on/off target (the cycler's save path) via the toggle semantics. */
121
+ export function setOverrideState(
122
+ effective: EffectiveOverrides,
123
+ id: string,
124
+ target: 'enabled' | 'disabled',
125
+ reason: string = TOGGLE_REASON,
126
+ ): EffectiveOverrides {
127
+ const disabled = isBehaviorDisabled(effective, id)
128
+ if ((target === 'disabled') === disabled) return effective
129
+ return toggledOverrides(effective, id, reason)
130
+ }
131
+
188
132
  export function serializeOverrides(effective: EffectiveOverrides): string {
189
133
  const ordered: Record<string, unknown> = {}
190
134
  for (const id of Object.keys(effective).sort()) ordered[id] = effective[id]
191
135
  return `${JSON.stringify(ordered, null, 2)}\n`
192
136
  }
193
137
 
194
- // ── Gates section ──────────────────────────────────────────────────────
138
+ // ── Overrides load (global + project files) ────────────────────────────
139
+
140
+ export interface OverridesLoad {
141
+ global: EffectiveOverrides
142
+ project: EffectiveOverrides
143
+ /** The GLOBAL file exists but does not parse — writes are refused. */
144
+ corruptGlobal: boolean
145
+ }
146
+
147
+ /** Merge for DISPLAY only, mirroring the runtime precedence: project wins per id. */
148
+ export function mergedOverrides(load: OverridesLoad): EffectiveOverrides {
149
+ return { ...load.global, ...load.project }
150
+ }
151
+
152
+ export function canWriteOverrides(load: OverridesLoad): boolean {
153
+ return !load.corruptGlobal
154
+ }
155
+
156
+ // ── Tab / group / row model (the cycler window) ────────────────────────
157
+
158
+ export interface SettingsRow {
159
+ id: string
160
+ name: string
161
+ /** Current effective value (display); the staged draft may differ. */
162
+ value: string
163
+ /** Allowed values for ←/→; presence marks the row as editable. */
164
+ cycle?: readonly string[]
165
+ /** Right-column note (writes-where, applies-live, from-where). */
166
+ note?: string
167
+ /** Enter triggers this action instead of cycling (e.g. `update-check`). */
168
+ action?: string
169
+ }
170
+
171
+ export interface SettingsGroup {
172
+ /** Bold group header; undefined renders rows without a header. */
173
+ title?: string
174
+ rows: SettingsRow[]
175
+ }
176
+
177
+ export interface SettingsTab {
178
+ id: string
179
+ title: string
180
+ groups: SettingsGroup[]
181
+ }
182
+
183
+ /** Neutral tab for out-of-range indexes (noUncheckedIndexedAccess guard). */
184
+ export const EMPTY_TAB: SettingsTab = { id: 'empty', title: '', groups: [] }
185
+
186
+ /** Flat, in-order rows of a tab (the up/down navigation space). */
187
+ export function tabRows(tab: SettingsTab): SettingsRow[] {
188
+ return tab.groups.flatMap((group) => group.rows)
189
+ }
190
+
191
+ export function findRow(tab: SettingsTab, id: string): SettingsRow | undefined {
192
+ return tabRows(tab).find((row) => row.id === id)
193
+ }
194
+
195
+ // ── The cycler ─────────────────────────────────────────────────────────
196
+
197
+ /** Step through the cycle with wrap-around; non-cyclable rows return the input. */
198
+ export function cycleValue(value: string, cycle: readonly string[], direction: 1 | -1): string {
199
+ if (cycle.length === 0) return value
200
+ const index = cycle.indexOf(value)
201
+ if (index === -1) return cycle[0] ?? value
202
+ const next = (index + direction + cycle.length) % cycle.length
203
+ return cycle[next] ?? value
204
+ }
205
+
206
+ // ── The draft (staged edits; the disk never changes before save) ───────
207
+
208
+ export type SettingsDraft = Record<string, string>
209
+
210
+ export function stageValue(
211
+ draft: SettingsDraft,
212
+ row: SettingsRow,
213
+ direction: 1 | -1,
214
+ ): SettingsDraft {
215
+ if (row.cycle === undefined) return draft
216
+ const current = draft[row.id] ?? row.value
217
+ const staged = cycleValue(current, row.cycle, direction)
218
+ if (staged === row.value) {
219
+ // staged back onto the disk value — drop the entry (no toggle noise)
220
+ if (draft[row.id] === undefined) return draft
221
+ const next = { ...draft }
222
+ delete next[row.id]
223
+ return next
224
+ }
225
+ return { ...draft, [row.id]: staged }
226
+ }
227
+
228
+ /** Drop every staged entry of one tab (ctrl+r). */
229
+ export function resetTab(draft: SettingsDraft, tab: SettingsTab): SettingsDraft {
230
+ const ids = new Set(tabRows(tab).map((row) => row.id))
231
+ const next: SettingsDraft = {}
232
+ for (const [id, value] of Object.entries(draft)) {
233
+ if (!ids.has(id)) next[id] = value
234
+ }
235
+ return next
236
+ }
237
+
238
+ export function draftCount(draft: SettingsDraft): number {
239
+ return Object.keys(draft).length
240
+ }
241
+
242
+ export function draftDirty(draft: SettingsDraft): boolean {
243
+ return draftCount(draft) > 0
244
+ }
245
+
246
+ export function tabDirty(draft: SettingsDraft, tab: SettingsTab): boolean {
247
+ return tabRows(tab).some((row) => draft[row.id] !== undefined)
248
+ }
249
+
250
+ // ── Applying the draft ─────────────────────────────────────────────────
251
+
252
+ export interface DraftWriterResult {
253
+ ok: boolean
254
+ reason?: string
255
+ }
195
256
 
196
257
  /**
197
- * The Gates & Overrides page: behavior state from BOTH overrides files.
198
- * A project-owned entry wins the runtime merge and the window only writes
199
- * the global file, so project rows are marked and not togglable here.
258
+ * One callback that dispatches a staged row to its real writer (gates →
259
+ * overrides file, decision → decision-router.json, preset → active-preset).
260
+ *Injected by the glue so the disk never runs in tests.
200
261
  */
201
- export function buildGatesSection(load: OverridesLoad): SettingsSection {
262
+ export type DraftWriter = (id: string, value: string) => DraftWriterResult
263
+
264
+ export interface AppliedDraft {
265
+ applied: string[]
266
+ failed: { id: string; reason: string }[]
267
+ }
268
+
269
+ /** Apply every staged entry; failures are collected, never thrown. */
270
+ export function applyDraft(draft: SettingsDraft, writer: DraftWriter): AppliedDraft {
271
+ const applied: string[] = []
272
+ const failed: AppliedDraft['failed'] = []
273
+ for (const [id, value] of Object.entries(draft)) {
274
+ const result = writer(id, value)
275
+ if (result.ok) applied.push(id)
276
+ else failed.push({ id, reason: result.reason ?? 'write failed' })
277
+ }
278
+ return { applied, failed }
279
+ }
280
+
281
+ /** The rows that succeeded and are still in the draft (failed ones stay staged). */
282
+ export function settledDraft(draft: SettingsDraft, applied: readonly string[]): SettingsDraft {
283
+ const next: SettingsDraft = {}
284
+ for (const [id, value] of Object.entries(draft)) {
285
+ if (!applied.includes(id)) next[id] = value
286
+ }
287
+ return next
288
+ }
289
+
290
+ // ── Gates / About tabs (behavior-native; the other tabs live in their modules) ──
291
+
292
+ export const GATES_ENABLED_CYCLE: readonly string[] = Object.freeze(['enabled', 'disabled'])
293
+
294
+ export function buildGatesTab(load: OverridesLoad): SettingsTab {
202
295
  const merged = mergedOverrides(load)
203
- const rows: SettingsRow[] = SETTINGS_BEHAVIORS.map((behavior) => {
296
+ const rowFor = (behavior: SettingsBehavior): SettingsRow => {
204
297
  const fromProject = load.project[behavior.id] !== undefined
298
+ const writable = !fromProject && canWriteOverrides(load)
205
299
  return {
206
300
  id: `gate.${behavior.id}`,
207
- title: fromProject ? `${behavior.title} (project)` : behavior.title,
208
- description: behavior.description,
209
- disabled: isBehaviorDisabled(merged, behavior.id),
210
- selectable: !fromProject,
211
- source: fromProject ? 'project' : 'global',
301
+ name: fromProject ? `${behavior.title} (project)` : behavior.title,
302
+ value: isBehaviorDisabled(merged, behavior.id) ? 'disabled' : 'enabled',
303
+ ...(writable ? { cycle: GATES_ENABLED_CYCLE } : {}),
304
+ note: fromProject
305
+ ? 'project file wins at runtime — edit .pantheon/overrides.json'
306
+ : behavior.description,
212
307
  }
213
- })
308
+ }
309
+ const install = SETTINGS_BEHAVIORS.filter((behavior) => behavior.kind === 'install')
310
+ const runtime = SETTINGS_BEHAVIORS.filter((behavior) => behavior.kind === 'runtime')
214
311
  return {
215
312
  id: 'gates',
216
- title: 'Gates & Overrides',
217
- hint: canWriteOverrides(load)
218
- ? 'enter: disable/enable · writes pantheon-overrides.json (global)'
219
- : 'overrides file unreadable — writes refused; repair with `pantheon-opencode doctor --fix`',
220
- rows,
313
+ title: 'Gates',
314
+ groups: [
315
+ { title: 'Install skills', rows: install.map(rowFor) },
316
+ { title: 'Runtime gates', rows: runtime.map(rowFor) },
317
+ ],
221
318
  }
222
319
  }
223
320
 
224
- // ── About section ──────────────────────────────────────────────────────
225
-
226
- export function buildAboutSection(version: string): SettingsSection {
321
+ export function buildAboutTab(version: string): SettingsTab {
227
322
  return {
228
323
  id: 'about',
229
324
  title: 'About',
230
- rows: [
231
- {
232
- id: 'about.version',
233
- title: `Pantheon v${version}`,
234
- description: 'OpenCode 2.x only — the V1 contract is removed.',
235
- selectable: false,
236
- },
325
+ groups: [
237
326
  {
238
- id: 'about.behaviors',
239
- title: `${SETTINGS_BEHAVIORS.length} togglable behaviors`,
240
- description: 'Docs: docs/design/2.0-settings-and-install.md',
241
- selectable: false,
327
+ rows: [
328
+ {
329
+ id: 'about.version',
330
+ name: 'Version',
331
+ value: `v${version}`,
332
+ note: 'OpenCode 2.x only — the V1 contract is removed',
333
+ },
334
+ {
335
+ id: 'about.behaviors',
336
+ name: 'Behaviors',
337
+ value: String(SETTINGS_BEHAVIORS.length),
338
+ note: 'docs/design/2.0-settings-and-install.md',
339
+ },
340
+ ],
242
341
  },
243
342
  ],
244
343
  }