@cortexkit/common-auth 0.48.0 → 0.49.1

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.
@@ -2,6 +2,7 @@ import type { SettingsDefinition } from './define.js';
2
2
  export type SettingsEdit = {
3
3
  setting: string;
4
4
  path?: readonly string[];
5
+ level?: 'default' | 'opencode' | 'pi';
5
6
  } & ({
6
7
  value: unknown;
7
8
  remove?: never;
@@ -37,7 +37,12 @@ function descriptorAtPath(descriptor, path) {
37
37
  }
38
38
  return current;
39
39
  }
40
- function destination(declaration, setting, value, { host, model }) {
40
+ function destination(declaration, setting, value, { host, model }, level) {
41
+ if (level !== undefined) {
42
+ return level === 'default' && !isJsonObject(value)
43
+ ? [setting]
44
+ : [setting, level];
45
+ }
41
46
  if (declaration.overrides !== undefined && isJsonObject(value)) {
42
47
  if (declaration.overrides.model &&
43
48
  model !== undefined &&
@@ -75,6 +80,20 @@ function checkPath(parsed, path) {
75
80
  invalid();
76
81
  }
77
82
  }
83
+ function preservePlainDefault(parsed, setting) {
84
+ const node = parsed.tree && findNodeAtLocation(parsed.tree, [setting]);
85
+ if (!node)
86
+ invalid();
87
+ // Keep the original value text so comments inside a list survive promotion.
88
+ const value = parsed.text.slice(node.offset, node.offset + node.length);
89
+ return parseForEdit(applyEdits(parsed.text, [
90
+ {
91
+ offset: node.offset,
92
+ length: node.length,
93
+ content: `{ "default": ${value} }`,
94
+ },
95
+ ]));
96
+ }
78
97
  /** Remove only the member's own tokens; intervening comment lines belong to neither neighbor. */
79
98
  function removeMember(text, tree, path) {
80
99
  const property = tree && findNodeAtLocation(tree, path)?.parent;
@@ -157,6 +176,18 @@ export function applySettingsEdits(definition, source, edits, context) {
157
176
  if (!Object.hasOwn(definition.settings, edit.setting))
158
177
  invalid();
159
178
  const declaration = definition.settings[edit.setting];
179
+ if (edit.level !== undefined) {
180
+ if (edit.path !== undefined)
181
+ invalid();
182
+ if (edit.level === 'default') {
183
+ if (declaration.overrides === undefined)
184
+ invalid();
185
+ }
186
+ else if ((edit.level !== 'opencode' && edit.level !== 'pi') ||
187
+ !declaration.overrides?.host) {
188
+ invalid();
189
+ }
190
+ }
160
191
  if (edit.path !== undefined &&
161
192
  declaration.value.kind !== 'object' &&
162
193
  declaration.value.kind !== 'record')
@@ -170,9 +201,24 @@ export function applySettingsEdits(definition, source, edits, context) {
170
201
  const units = [];
171
202
  for (const edit of edits) {
172
203
  const declaration = definition.settings[edit.setting];
204
+ if (edit.level !== undefined &&
205
+ edit.level !== 'default' &&
206
+ !isJsonObject(working.value[edit.setting])) {
207
+ if (edit.remove === true)
208
+ continue;
209
+ if (Object.hasOwn(working.value, edit.setting)) {
210
+ working = preservePlainDefault(working, edit.setting);
211
+ }
212
+ // Earlier root edits now refer to the override's default, if it exists.
213
+ for (const unit of units) {
214
+ if (unit.path.length === 1 && unit.path[0] === edit.setting) {
215
+ unit.path.push('default');
216
+ }
217
+ }
218
+ }
173
219
  const base = edit.path !== undefined
174
220
  ? [edit.setting]
175
- : destination(declaration, edit.setting, working.value[edit.setting], context);
221
+ : destination(declaration, edit.setting, working.value[edit.setting], context, edit.level);
176
222
  const path = [...base, ...(edit.path ?? [])];
177
223
  checkPath(working, path);
178
224
  units.push(resolutionUnit(declaration.value, base, edit.path ?? []));
@@ -4,6 +4,7 @@ import { type ValueOf } from './descriptors.js';
4
4
  import type { SettingsEdit, UpdateResult } from './edit.js';
5
5
  import { type GetOptions } from './resolve.js';
6
6
  import { type ReportEntry } from './snapshot.js';
7
+ import { type SettingsUpdatePlan } from './update.js';
7
8
  export interface LegacyProjectSource {
8
9
  readonly path: string;
9
10
  read(text: string): unknown;
@@ -22,7 +23,7 @@ export interface SettingsHandle<S extends Readonly<Record<string, SettingDeclara
22
23
  }): Promise<void>;
23
24
  get<K extends keyof S & string>(name: K, options: GetOptions): Promise<ValueOf<S[K]['value']>>;
24
25
  report(): ReportEntry[];
25
- update(edits: readonly SettingsEdit[], options: Pick<GetOptions, 'host' | 'model'>): Promise<UpdateResult>;
26
+ update(edits: readonly SettingsEdit[] | SettingsUpdatePlan, options: Pick<GetOptions, 'host' | 'model'>): Promise<UpdateResult>;
26
27
  watch(onChange: () => void): () => void;
27
28
  }
28
29
  /** Internal test hook; not part of the settings package exports. */
@@ -8,3 +8,4 @@ export { SETTINGS_REFUSAL_CODES, SettingsRefusal, type SettingsRefusalCode, } fr
8
8
  export type { GetOptions } from './resolve.js';
9
9
  export { settingsJsonSchema } from './schema.js';
10
10
  export type { ReportEntry, ReportReason } from './snapshot.js';
11
+ export type { SettingsCurrent, SettingsUpdatePlan } from './update.js';
@@ -2,5 +2,13 @@ import type { SettingsDefinition } from './define.js';
2
2
  import { type SettingsEdit, type UpdateResult } from './edit.js';
3
3
  import type { OpenSettingsOptions } from './handle.js';
4
4
  import type { GetOptions } from './resolve.js';
5
+ export type SettingsCurrent = {
6
+ readonly status: 'missing';
7
+ } | {
8
+ readonly status: 'ready';
9
+ readonly document: Readonly<Record<string, unknown>>;
10
+ };
11
+ /** Compute edits synchronously from an immutable copy read under the write lock. */
12
+ export type SettingsUpdatePlan = (current: SettingsCurrent) => readonly SettingsEdit[];
5
13
  /** Lock the canonical user file so symlink aliases serialize the same edits. */
6
- export declare function updateSettings(definition: SettingsDefinition, options: OpenSettingsOptions, edits: readonly SettingsEdit[], context: Pick<GetOptions, 'host' | 'model'>): Promise<UpdateResult>;
14
+ export declare function updateSettings(definition: SettingsDefinition, options: OpenSettingsOptions, edits: readonly SettingsEdit[] | SettingsUpdatePlan, context: Pick<GetOptions, 'host' | 'model'>): Promise<UpdateResult>;
@@ -1,6 +1,7 @@
1
1
  import { basename, dirname, join } from 'node:path';
2
2
  import { ensurePrivateDirectory } from '../fs/private-files.js';
3
3
  import { withLock } from '../fs/with-lock.js';
4
+ import { deepFreeze } from './descriptors.js';
4
5
  import { applySettingsEdits, } from './edit.js';
5
6
  import { SettingsFs } from './fs.js';
6
7
  import { rethrowSettingsLockError } from './lock-errors.js';
@@ -23,21 +24,24 @@ async function writeTarget(file) {
23
24
  catch (error) {
24
25
  if (error.code !== 'ENOENT')
25
26
  unavailable(file, error);
27
+ let isSymlink = false;
26
28
  try {
27
- await SettingsFs.lstat(file);
29
+ isSymlink = (await SettingsFs.lstat(file)).isSymbolicLink();
28
30
  }
29
31
  catch (lstatError) {
30
32
  if (lstatError.code !== 'ENOENT')
31
33
  unavailable(file, lstatError);
32
- try {
33
- return join(await SettingsFs.realpath(dirname(file)), basename(file));
34
- }
35
- catch (parentError) {
36
- unavailable(file, parentError);
37
- }
38
34
  }
39
- // A directory entry whose realpath is missing is a dangling symlink.
40
- unavailable(file, error);
35
+ // Only a symlink whose realpath is missing is a dangling entry.
36
+ if (isSymlink)
37
+ unavailable(file, error);
38
+ // Bun 1.4 can fail realpath for regular files during a concurrent atomic replace.
39
+ try {
40
+ return join(await SettingsFs.realpath(dirname(file)), basename(file));
41
+ }
42
+ catch (parentError) {
43
+ unavailable(file, parentError);
44
+ }
41
45
  }
42
46
  }
43
47
  /** Lock the canonical user file so symlink aliases serialize the same edits. */
@@ -97,6 +101,21 @@ export async function updateSettings(definition, options, edits, context) {
97
101
  if (Number.isInteger(version) && version > 1) {
98
102
  throw new SettingsRefusal('settings-newer-version', 'Settings were written by a newer version');
99
103
  }
104
+ if (typeof edits === 'function') {
105
+ const planned = edits(deepFreeze(missing
106
+ ? { status: 'missing' }
107
+ : {
108
+ status: 'ready',
109
+ document: structuredClone(parsed.value),
110
+ }));
111
+ if (planned !== null &&
112
+ (typeof planned === 'object' || typeof planned === 'function') &&
113
+ 'then' in planned &&
114
+ typeof planned.then === 'function') {
115
+ throw new SettingsRefusal('settings-invalid-value', 'Settings update plans must be synchronous');
116
+ }
117
+ edits = planned;
118
+ }
100
119
  if (!parsed.tree) {
101
120
  source = `${parsed.hasBom ? '\uFEFF' : ''}// ${definition.plugin} settings\n{\n "schema_version": 1\n}\n`;
102
121
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/common-auth",
3
- "version": "0.48.0",
3
+ "version": "0.49.1",
4
4
  "description": "Shared code for the CortexKit auth plugins: account pool, quota and routing, commands and auth menu, OpenCode 2 hooks, Claustrum custody, and plumbing (loopback RPC, file locks, logger, sidebar state, TUI preferences and build).",
5
5
  "license": "MIT",
6
6
  "repository": {