fix(host): keep sidebar_open registered when the settings row is unreadable (#951)
The gate read an absent settings row (the host only lists entries whose fiber is ACTIVE) as the schema default false and unregistered a live tool mid-session. Read the switch tri-state instead: only an explicit boolean is a decision, so an absent row or a thrown read keeps the current registration. Also re-evaluate right after a settings write and after the loader settles, so enabling the feature registers the tool for the first turn instead of waiting for an unrelated settings.get.
This commit is contained in:
1 parent
58310536d6
commit
af0e7b7c9d
2 files changed
+152
-18
No files matched your search
+39
-5
@@ -1473,8 +1473,33 @@ export function apply(ctx: Context, config?: SidebarConfig): void {
|
||||
const value = viewOf().value
|
||||
return value !== null && typeof value === 'object' ? value as SidebarPrefs : SIDEBAR_PREFS_DEFAULTS
|
||||
}
|
||||
/**
|
||||
* The live `agentOpenTools` switch, or `undefined` when the form cannot be
|
||||
* read right now. The host's settings service only lists entries whose
|
||||
* fiber is ACTIVE, so a row missing from `describe()` is a NORMAL state
|
||||
* during (re)mount and live-commit windows; a thrown read is the same
|
||||
* unknown. `prefsOf()` maps that absence to the schema default (`false`),
|
||||
* which is right for consumers but WRONG for this gate: it would read
|
||||
* "unknown" as "the user turned the tool off" and unregister a live tool
|
||||
* mid-session (issue #951). Only an explicit boolean is a user decision.
|
||||
*/
|
||||
const openToolsSetting = (): boolean | undefined => {
|
||||
let value: unknown
|
||||
try {
|
||||
value = viewOf().value
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
if (value === null || typeof value !== 'object') return undefined
|
||||
const flag = (value as { agentOpenTools?: unknown }).agentOpenTools
|
||||
return typeof flag === 'boolean' ? flag : undefined
|
||||
}
|
||||
const syncOpenToolsGate = (): void => {
|
||||
if (prefsOf().agentOpenTools === true) {
|
||||
const enabled = openToolsSetting()
|
||||
// Unknown (absent row / failed read) keeps the current registration.
|
||||
// Explicit false unregisters once: the null guard also makes repeated
|
||||
// syncs idempotent (no double-registration, no second disposal).
|
||||
if (enabled === true) {
|
||||
if (openToolsDisposers === null) {
|
||||
openToolsDisposers = registerOpenTool(
|
||||
ctx,
|
||||
@@ -1483,7 +1508,7 @@ export function apply(ctx: Context, config?: SidebarConfig): void {
|
||||
prefsOf,
|
||||
)
|
||||
}
|
||||
} else if (openToolsDisposers !== null) {
|
||||
} else if (enabled === false && openToolsDisposers !== null) {
|
||||
openToolsDisposers()
|
||||
openToolsDisposers = null
|
||||
agentOpenRegistry.drainAll()
|
||||
@@ -1499,6 +1524,9 @@ export function apply(ctx: Context, config?: SidebarConfig): void {
|
||||
externalDisable,
|
||||
update: async (patch, expectedRevision) => {
|
||||
await sctx.settings.update(ns, patch, expectedRevision)
|
||||
// The write IS the user's decision landing; re-read now instead of
|
||||
// waiting for some later `settings.get` to happen to run (issue #951).
|
||||
syncOpenToolsGate()
|
||||
return viewOf()
|
||||
},
|
||||
}
|
||||
@@ -1512,9 +1540,15 @@ export function apply(ctx: Context, config?: SidebarConfig): void {
|
||||
// row is not in the form list yet and the import would silently find
|
||||
// "no form" and do nothing. Upstream's own settings migration waits the
|
||||
// same way (`ctx.root.loader.await().then(...)`).
|
||||
void Promise.resolve(ctx.loader?.await?.()).then(
|
||||
() => importLegacyPrefs(ctx, sctx.settings, ns),
|
||||
).then((outcome) => {
|
||||
void Promise.resolve(ctx.loader?.await?.()).then(async () => {
|
||||
// The row is addressable only after the loader mounts it, so the
|
||||
// injection-time sync above ran before the user's value could be read
|
||||
// (issue #951: the first turn after a restart missed the tool). Re-read
|
||||
// AFTER the import too: the imported section may carry this very switch.
|
||||
const outcome = await importLegacyPrefs(ctx, sctx.settings, ns)
|
||||
syncOpenToolsGate()
|
||||
return outcome
|
||||
}).then((outcome) => {
|
||||
if (outcome === 'no-profile-home' || outcome === 'no-form') {
|
||||
ctx.logger.warn('dsh-better-sidebar: legacy preference import could not run (%s)', outcome)
|
||||
return
|
||||
|
||||
+113
-13
@@ -1929,19 +1929,27 @@ describe('agent sidebar-open tool gating', () => {
|
||||
/**
|
||||
* DSH 0.1.7 emits its settings change on the settings service's own context,
|
||||
* which is not an ancestor of this plugin's fiber, so the gate cannot watch
|
||||
* an event. It is re-evaluated whenever the client re-reads the form instead
|
||||
* — which is exactly what the fenced `settings.get` route below does, and
|
||||
* what the Side card page does on every `settings/document-updated` push.
|
||||
* an event. It is re-evaluated on every form read instead — the fenced
|
||||
* `settings.get` route below, the Side card page's `settings/document-updated`
|
||||
* push — plus the two decision points of issue #951: the settings write
|
||||
* itself (`settings.update`) and the loader settling (the point where the
|
||||
* plugin's row first becomes addressable).
|
||||
*/
|
||||
const gatingCtx = (enabled: () => boolean, routes: SidebarWebRoute[]) => {
|
||||
const gatingCtx = (
|
||||
read: () => Record<string, unknown> | undefined,
|
||||
routes: SidebarWebRoute[],
|
||||
awaitLoader: () => Promise<void> = () => new Promise<void>(() => {}),
|
||||
) => {
|
||||
let registered = 0
|
||||
let disposed = 0
|
||||
const settings = {
|
||||
describe: () => [{
|
||||
ns: ENTRY_ID,
|
||||
value: { ...SIDEBAR_PREFS_DEFAULTS, agentOpenTools: enabled() },
|
||||
revision: 0,
|
||||
}],
|
||||
// Mirrors the host's settings service: an entry whose fiber is not
|
||||
// ACTIVE is simply ABSENT from describe() (a normal remount window),
|
||||
// and a broken service can throw. Both are "unknown", never "off".
|
||||
describe: () => {
|
||||
const value = read()
|
||||
return value === undefined ? [] : [{ ns: ENTRY_ID, value, revision: 0 }]
|
||||
},
|
||||
async update() {},
|
||||
configure: () => () => {},
|
||||
}
|
||||
@@ -1963,9 +1971,9 @@ describe('agent sidebar-open tool gating', () => {
|
||||
fiber: PLUGIN_FIBER,
|
||||
loader: {
|
||||
entries: () => [{ options: { id: ENTRY_ID, name: 'dsh-better-sidebar' }, fiber: PLUGIN_FIBER }],
|
||||
// The real loader settles before a form is addressable; the fake
|
||||
// resolves immediately so the import runs on the same tick.
|
||||
await: () => Promise.resolve(),
|
||||
// Only a case that pins the post-loader re-evaluation settles it; the
|
||||
// default keeps the injection-time read the sole trigger.
|
||||
await: awaitLoader,
|
||||
},
|
||||
logger: { info: () => {}, warn: () => {}, debug: () => {}, error: () => {} },
|
||||
}
|
||||
@@ -1975,7 +1983,10 @@ describe('agent sidebar-open tool gating', () => {
|
||||
it('injects the one open tool only when the side-card setting is enabled (default off)', async () => {
|
||||
let enabled = false
|
||||
const routes: SidebarWebRoute[] = []
|
||||
const harness = gatingCtx(() => enabled, routes)
|
||||
const harness = gatingCtx(
|
||||
() => ({ ...SIDEBAR_PREFS_DEFAULTS, agentOpenTools: enabled }),
|
||||
routes,
|
||||
)
|
||||
apply(harness.ctx as never)
|
||||
const route = routes.find(candidate => candidate.path === '/sidebar/api')!
|
||||
// Default off: no open tool is registered even though settings is mounted.
|
||||
@@ -1998,4 +2009,93 @@ describe('agent sidebar-open tool gating', () => {
|
||||
expect(harness.live()).toBe(1)
|
||||
expect(harness.registrations()).toBe(2)
|
||||
})
|
||||
|
||||
it('never treats an unreadable form as the setting being off (issue #951)', async () => {
|
||||
let prefs: Record<string, unknown> | undefined = { ...SIDEBAR_PREFS_DEFAULTS, agentOpenTools: true }
|
||||
let readThrows = false
|
||||
const routes: SidebarWebRoute[] = []
|
||||
const harness = gatingCtx(() => {
|
||||
if (readThrows) throw new Error('the settings service is unavailable')
|
||||
return prefs
|
||||
}, routes)
|
||||
apply(harness.ctx as never)
|
||||
const route = routes.find(candidate => candidate.path === '/sidebar/api')!
|
||||
|
||||
// A readable form with the switch on registers the tool.
|
||||
await invoke(route, 'settings.get', {})
|
||||
expect(harness.live()).toBe(1)
|
||||
|
||||
// The host lists an entry only while its fiber is ACTIVE, so the row
|
||||
// vanishing (loader mount / live-commit remount) is normal: keep the
|
||||
// registration instead of reading the absence as "the user turned it off".
|
||||
prefs = undefined
|
||||
await invoke(route, 'settings.get', {})
|
||||
expect(harness.live()).toBe(1)
|
||||
expect(harness.disposals()).toBe(0)
|
||||
|
||||
// A thrown read is the same unknown.
|
||||
readThrows = true
|
||||
await invoke(route, 'settings.get', {})
|
||||
expect(harness.live()).toBe(1)
|
||||
expect(harness.disposals()).toBe(0)
|
||||
|
||||
// A partial form without the boolean field is not an explicit false either.
|
||||
readThrows = false
|
||||
prefs = {}
|
||||
await invoke(route, 'settings.get', {})
|
||||
expect(harness.live()).toBe(1)
|
||||
expect(harness.disposals()).toBe(0)
|
||||
|
||||
// Only an explicit false is a user decision — unregister once, and a
|
||||
// repeated sync is a no-op rather than a second disposal.
|
||||
prefs = { ...SIDEBAR_PREFS_DEFAULTS, agentOpenTools: false }
|
||||
await invoke(route, 'settings.get', {})
|
||||
expect(harness.live()).toBe(0)
|
||||
expect(harness.disposals()).toBe(1)
|
||||
await invoke(route, 'settings.get', {})
|
||||
expect(harness.live()).toBe(0)
|
||||
expect(harness.disposals()).toBe(1)
|
||||
})
|
||||
|
||||
it('registers as soon as the loader lists the form, without a settings.get (issue #951)', async () => {
|
||||
let listed = false
|
||||
let release = (): void => {}
|
||||
const loaderReady = new Promise<void>(resolve => { release = resolve })
|
||||
const routes: SidebarWebRoute[] = []
|
||||
const harness = gatingCtx(
|
||||
() => (listed ? { ...SIDEBAR_PREFS_DEFAULTS, agentOpenTools: true } : undefined),
|
||||
routes,
|
||||
() => loaderReady,
|
||||
)
|
||||
apply(harness.ctx as never)
|
||||
// The inject callback runs while the loader is still mounting rows: the
|
||||
// form is not addressable yet, so the injection-time read must not
|
||||
// unregister anything (there is nothing yet) — and must not decide "off".
|
||||
expect(harness.live()).toBe(0)
|
||||
|
||||
// The loader settles and the row becomes readable; the gate re-evaluates
|
||||
// on its own, so the first model turn already carries the tool.
|
||||
listed = true
|
||||
release()
|
||||
await new Promise<void>(resolve => { setTimeout(resolve, 0) })
|
||||
expect(harness.live()).toBe(1)
|
||||
expect(harness.registrations()).toBe(1)
|
||||
})
|
||||
|
||||
it('re-evaluates the gate after settings.update, not only on the next read (issue #951)', async () => {
|
||||
let prefs: Record<string, unknown> = { ...SIDEBAR_PREFS_DEFAULTS, agentOpenTools: false }
|
||||
const routes: SidebarWebRoute[] = []
|
||||
const harness = gatingCtx(() => prefs, routes)
|
||||
apply(harness.ctx as never)
|
||||
const route = routes.find(candidate => candidate.path === '/sidebar/api')!
|
||||
expect(harness.live()).toBe(0)
|
||||
|
||||
// The write lands (the fake update is a no-op; the read now says true):
|
||||
// the update path itself must re-evaluate, or the tool stays missing
|
||||
// until some unrelated settings.get happens to run.
|
||||
prefs = { ...SIDEBAR_PREFS_DEFAULTS, agentOpenTools: true }
|
||||
const written = await invoke(route, 'settings.update', { patch: { agentOpenTools: true } })
|
||||
expect(written.ok).toBe(true)
|
||||
expect(harness.live()).toBe(1)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user