feat(config): split the master switch into activity/memory gates with legacy inheritance

This commit is contained in:
newbie 2026-11-23 13:13:31 +08:00
parent cb7bc0c19b
commit 9f5daaf266
6 changed files with 83 additions and 13 deletions

View File

@ -6,6 +6,8 @@
/** Defaults written into cordis.patch.yml; also the fallback for missing keys. */ /** Defaults written into cordis.patch.yml; also the fallback for missing keys. */
export const DEFAULT_SETTINGS = { export const DEFAULT_SETTINGS = {
enabled: true, enabled: true,
activityEnabled: true,
memoryEnabled: true,
baseUrl: 'https://your-memory-host/api/longmemory', baseUrl: 'https://your-memory-host/api/longmemory',
clientId: 'dsh-desktop', clientId: 'dsh-desktop',
requestTimeoutMs: 5000, requestTimeoutMs: 5000,
@ -85,6 +87,10 @@ export function settingsFromConfig(raw) {
const result = {}; const result = {};
if (typeof source.enabled === 'boolean') if (typeof source.enabled === 'boolean')
result.enabled = source.enabled; result.enabled = source.enabled;
if (typeof source.activityEnabled === 'boolean')
result.activityEnabled = source.activityEnabled;
if (typeof source.memoryEnabled === 'boolean')
result.memoryEnabled = source.memoryEnabled;
const baseUrl = normalizeBaseUrl(source.baseUrl); const baseUrl = normalizeBaseUrl(source.baseUrl);
if (baseUrl !== undefined) if (baseUrl !== undefined)
result.baseUrl = baseUrl; result.baseUrl = baseUrl;

View File

@ -53,7 +53,7 @@ export function apply(ctx, config) {
}); });
const reporter = new Reporter({ const reporter = new Reporter({
client, client,
settings: () => ({ enabled: settings().enabled, clientId: settings().clientId }), settings: () => ({ enabled: settings().activityEnabled, clientId: settings().clientId }),
activityToken: () => store.activityToken(), activityToken: () => store.activityToken(),
summary: () => ({ enabled: settings().summaryEnabled, maxChars: settings().summaryMaxChars }), summary: () => ({ enabled: settings().summaryEnabled, maxChars: settings().summaryMaxChars }),
summaryToken: () => store.summaryToken(), summaryToken: () => store.summaryToken(),
@ -120,8 +120,8 @@ export function apply(ctx, config) {
const clients = new ClientRegistry(); const clients = new ClientRegistry();
// Announce at most once per activation so a missing token is obvious in logs. // Announce at most once per activation so a missing token is obvious in logs.
const effective = settings(); const effective = settings();
if (!effective.enabled) { if (!effective.activityEnabled) {
logger.info('disabled by settings'); logger.info('turn reporting disabled by settings');
} }
else if (store.activityToken() === '') { else if (store.activityToken() === '') {
logger.warn(`reporting enabled but no activity token; configure one at ${store.path} or open /dsh-brain`); logger.warn(`reporting enabled but no activity token; configure one at ${store.path} or open /dsh-brain`);
@ -151,7 +151,7 @@ export function apply(ctx, config) {
remote: () => memoryRemoteFactory.current(), remote: () => memoryRemoteFactory.current(),
client: () => client, client: () => client,
memoryToken: () => store.memoryToken(), memoryToken: () => store.memoryToken(),
settings: () => ({ enabled: settings().enabled, defaultK: settings().defaultK }), settings: () => ({ enabled: settings().memoryEnabled, defaultK: settings().defaultK }),
}; };
for (const tool of [...createMemoryTools(toolDeps), ...createSkillTools(toolDeps)]) { for (const tool of [...createMemoryTools(toolDeps), ...createSkillTools(toolDeps)]) {
toolsCtx.effect(() => tools.register(tool), `dsh-brain: tool ${tool.name}`); toolsCtx.effect(() => tools.register(tool), `dsh-brain: tool ${tool.name}`);
@ -170,7 +170,7 @@ export function apply(ctx, config) {
} }
ctx.effect(() => { ctx.effect(() => {
const timer = setInterval(() => { const timer = setInterval(() => {
if (store.activityToken() !== '' && settings().enabled) { if (store.activityToken() !== '' && settings().activityEnabled) {
void reporter.flush().catch((error) => { void reporter.flush().catch((error) => {
logger.warn(`retry flush failed: ${error instanceof Error ? error.message : String(error)}`); logger.warn(`retry flush failed: ${error instanceof Error ? error.message : String(error)}`);
}); });

View File

@ -84,6 +84,10 @@ function parseState(raw) {
const state = { version: 1 }; const state = { version: 1 };
if (typeof source.enabled === 'boolean') if (typeof source.enabled === 'boolean')
state.enabled = source.enabled; state.enabled = source.enabled;
if (typeof source.activityEnabled === 'boolean')
state.activityEnabled = source.activityEnabled;
if (typeof source.memoryEnabled === 'boolean')
state.memoryEnabled = source.memoryEnabled;
const baseUrl = normalizeBaseUrl(source.baseUrl); const baseUrl = normalizeBaseUrl(source.baseUrl);
if (baseUrl !== undefined) if (baseUrl !== undefined)
state.baseUrl = baseUrl; state.baseUrl = baseUrl;
@ -127,11 +131,15 @@ export class SettingsStore {
state; state;
file; file;
configDefaults; configDefaults;
/** Config-level explicit values for the split gates (absent = inherit `enabled`). */
configExplicit;
constructor(rawConfig, file = settingsPath()) { constructor(rawConfig, file = settingsPath()) {
this.file = file; this.file = file;
if (file === settingsPath()) if (file === settingsPath())
migrateLegacyFile(file, legacySettingsPath()); migrateLegacyFile(file, legacySettingsPath());
this.configDefaults = { ...DEFAULT_SETTINGS, ...settingsFromConfig(rawConfig) }; const fromConfig = settingsFromConfig(rawConfig);
this.configDefaults = { ...DEFAULT_SETTINGS, ...fromConfig };
this.configExplicit = { activityEnabled: fromConfig.activityEnabled, memoryEnabled: fromConfig.memoryEnabled };
this.state = this.read(); this.state = this.read();
} }
/** Absolute sidecar path (exposed for the status card). */ /** Absolute sidecar path (exposed for the status card). */
@ -164,6 +172,10 @@ export class SettingsStore {
const resolved = { ...this.configDefaults }; const resolved = { ...this.configDefaults };
if (this.state.enabled !== undefined) if (this.state.enabled !== undefined)
resolved.enabled = this.state.enabled; resolved.enabled = this.state.enabled;
if (this.state.activityEnabled !== undefined)
resolved.activityEnabled = this.state.activityEnabled;
if (this.state.memoryEnabled !== undefined)
resolved.memoryEnabled = this.state.memoryEnabled;
if (this.state.baseUrl !== undefined) if (this.state.baseUrl !== undefined)
resolved.baseUrl = this.state.baseUrl; resolved.baseUrl = this.state.baseUrl;
if (this.state.clientId !== undefined) if (this.state.clientId !== undefined)
@ -176,6 +188,14 @@ export class SettingsStore {
resolved.summaryEnabled = this.state.summaryEnabled; resolved.summaryEnabled = this.state.summaryEnabled;
if (this.state.summaryMaxChars !== undefined) if (this.state.summaryMaxChars !== undefined)
resolved.summaryMaxChars = this.state.summaryMaxChars; resolved.summaryMaxChars = this.state.summaryMaxChars;
// The split gates inherit `enabled` unless the config or the sidecar set
// them explicitly, so an old configuration keeps its exact behaviour.
if (this.state.activityEnabled === undefined && this.configExplicit.activityEnabled === undefined) {
resolved.activityEnabled = resolved.enabled;
}
if (this.state.memoryEnabled === undefined && this.configExplicit.memoryEnabled === undefined) {
resolved.memoryEnabled = resolved.enabled;
}
return resolved; return resolved;
} }
/** The activity:write bearer token (sidecar first, env fallback). */ /** The activity:write bearer token (sidecar first, env fallback). */
@ -223,6 +243,16 @@ export class SettingsStore {
return { ok: false, error: 'enabled must be a boolean' }; return { ok: false, error: 'enabled must be a boolean' };
next.enabled = patch.enabled; next.enabled = patch.enabled;
} }
if (patch.activityEnabled !== undefined) {
if (typeof patch.activityEnabled !== 'boolean')
return { ok: false, error: 'activityEnabled must be a boolean' };
next.activityEnabled = patch.activityEnabled;
}
if (patch.memoryEnabled !== undefined) {
if (typeof patch.memoryEnabled !== 'boolean')
return { ok: false, error: 'memoryEnabled must be a boolean' };
next.memoryEnabled = patch.memoryEnabled;
}
if (patch.baseUrl !== undefined) { if (patch.baseUrl !== undefined) {
const baseUrl = normalizeBaseUrl(patch.baseUrl); const baseUrl = normalizeBaseUrl(patch.baseUrl);
if (baseUrl === undefined) if (baseUrl === undefined)

View File

@ -7,6 +7,8 @@
/** Defaults written into cordis.patch.yml; also the fallback for missing keys. */ /** Defaults written into cordis.patch.yml; also the fallback for missing keys. */
export const DEFAULT_SETTINGS: Settings = { export const DEFAULT_SETTINGS: Settings = {
enabled: true, enabled: true,
activityEnabled: true,
memoryEnabled: true,
baseUrl: 'https://your-memory-host/api/longmemory', baseUrl: 'https://your-memory-host/api/longmemory',
clientId: 'dsh-desktop', clientId: 'dsh-desktop',
requestTimeoutMs: 5000, requestTimeoutMs: 5000,
@ -19,8 +21,12 @@ export const DEFAULT_SETTINGS: Settings = {
/** Effective, fully-resolved settings. */ /** Effective, fully-resolved settings. */
export interface Settings { export interface Settings {
/** Master switch for turn reporting AND memory tools. */ /** Master switch: the base gate every split switch inherits when absent. */
enabled: boolean enabled: boolean
/** Turn reporting gate; inherits `enabled` when the config omits it. */
activityEnabled: boolean
/** Memory tool gate; inherits `enabled` when the config omits it. */
memoryEnabled: boolean
/** Base URL of the cloud memory service, without a trailing slash. */ /** Base URL of the cloud memory service, without a trailing slash. */
baseUrl: string baseUrl: string
/** Stable client identifier sent with every activity turn. */ /** Stable client identifier sent with every activity turn. */
@ -107,6 +113,8 @@ export function settingsFromConfig(raw: unknown): Partial<Settings> {
const source = raw as RawConfig const source = raw as RawConfig
const result: Partial<Settings> = {} const result: Partial<Settings> = {}
if (typeof source.enabled === 'boolean') result.enabled = source.enabled if (typeof source.enabled === 'boolean') result.enabled = source.enabled
if (typeof source.activityEnabled === 'boolean') result.activityEnabled = source.activityEnabled
if (typeof source.memoryEnabled === 'boolean') result.memoryEnabled = source.memoryEnabled
const baseUrl = normalizeBaseUrl(source.baseUrl) const baseUrl = normalizeBaseUrl(source.baseUrl)
if (baseUrl !== undefined) result.baseUrl = baseUrl if (baseUrl !== undefined) result.baseUrl = baseUrl
const clientId = shortString(source.clientId) const clientId = shortString(source.clientId)

View File

@ -63,7 +63,7 @@ export function apply(ctx: CloudMemoryContext, config: unknown): void {
const reporter = new Reporter({ const reporter = new Reporter({
client, client,
settings: () => ({ enabled: settings().enabled, clientId: settings().clientId }), settings: () => ({ enabled: settings().activityEnabled, clientId: settings().clientId }),
activityToken: () => store.activityToken(), activityToken: () => store.activityToken(),
summary: () => ({ enabled: settings().summaryEnabled, maxChars: settings().summaryMaxChars }), summary: () => ({ enabled: settings().summaryEnabled, maxChars: settings().summaryMaxChars }),
summaryToken: () => store.summaryToken(), summaryToken: () => store.summaryToken(),
@ -138,8 +138,8 @@ export function apply(ctx: CloudMemoryContext, config: unknown): void {
const clients = new ClientRegistry() const clients = new ClientRegistry()
// Announce at most once per activation so a missing token is obvious in logs. // Announce at most once per activation so a missing token is obvious in logs.
const effective = settings() const effective = settings()
if (!effective.enabled) { if (!effective.activityEnabled) {
logger.info('disabled by settings') logger.info('turn reporting disabled by settings')
} else if (store.activityToken() === '') { } else if (store.activityToken() === '') {
logger.warn(`reporting enabled but no activity token; configure one at ${store.path} or open /dsh-brain`) logger.warn(`reporting enabled but no activity token; configure one at ${store.path} or open /dsh-brain`)
} else { } else {
@ -174,7 +174,7 @@ export function apply(ctx: CloudMemoryContext, config: unknown): void {
remote: () => memoryRemoteFactory.current(), remote: () => memoryRemoteFactory.current(),
client: () => client, client: () => client,
memoryToken: () => store.memoryToken(), memoryToken: () => store.memoryToken(),
settings: () => ({ enabled: settings().enabled, defaultK: settings().defaultK }), settings: () => ({ enabled: settings().memoryEnabled, defaultK: settings().defaultK }),
} }
for (const tool of [...createMemoryTools(toolDeps), ...createSkillTools(toolDeps)]) { for (const tool of [...createMemoryTools(toolDeps), ...createSkillTools(toolDeps)]) {
toolsCtx.effect(() => tools.register(tool), `dsh-brain: tool ${tool.name}`) toolsCtx.effect(() => tools.register(tool), `dsh-brain: tool ${tool.name}`)
@ -194,7 +194,7 @@ export function apply(ctx: CloudMemoryContext, config: unknown): void {
ctx.effect(() => { ctx.effect(() => {
const timer = setInterval(() => { const timer = setInterval(() => {
if (store.activityToken() !== '' && settings().enabled) { if (store.activityToken() !== '' && settings().activityEnabled) {
void reporter.flush().catch((error: unknown) => { void reporter.flush().catch((error: unknown) => {
logger.warn(`retry flush failed: ${error instanceof Error ? error.message : String(error)}`) logger.warn(`retry flush failed: ${error instanceof Error ? error.message : String(error)}`)
}) })

View File

@ -20,6 +20,8 @@ import { DEFAULT_SETTINGS, isPlaceholderBaseUrl, normalizeBaseUrl, originOf, set
export interface PersistedState { export interface PersistedState {
version: 1 version: 1
enabled?: boolean enabled?: boolean
activityEnabled?: boolean
memoryEnabled?: boolean
baseUrl?: string baseUrl?: string
clientId?: string clientId?: string
requestTimeoutMs?: number requestTimeoutMs?: number
@ -117,6 +119,8 @@ function parseState(raw: string): PersistedState {
const source = value as Record<string, unknown> const source = value as Record<string, unknown>
const state: PersistedState = { version: 1 } const state: PersistedState = { version: 1 }
if (typeof source.enabled === 'boolean') state.enabled = source.enabled if (typeof source.enabled === 'boolean') state.enabled = source.enabled
if (typeof source.activityEnabled === 'boolean') state.activityEnabled = source.activityEnabled
if (typeof source.memoryEnabled === 'boolean') state.memoryEnabled = source.memoryEnabled
const baseUrl = normalizeBaseUrl(source.baseUrl) const baseUrl = normalizeBaseUrl(source.baseUrl)
if (baseUrl !== undefined) state.baseUrl = baseUrl if (baseUrl !== undefined) state.baseUrl = baseUrl
const clientId = cleanString(source.clientId) const clientId = cleanString(source.clientId)
@ -151,11 +155,15 @@ export class SettingsStore {
private state: PersistedState private state: PersistedState
private readonly file: string private readonly file: string
private readonly configDefaults: Settings private readonly configDefaults: Settings
/** Config-level explicit values for the split gates (absent = inherit `enabled`). */
private readonly configExplicit: { activityEnabled?: boolean; memoryEnabled?: boolean }
constructor(rawConfig: unknown, file: string = settingsPath()) { constructor(rawConfig: unknown, file: string = settingsPath()) {
this.file = file this.file = file
if (file === settingsPath()) migrateLegacyFile(file, legacySettingsPath()) if (file === settingsPath()) migrateLegacyFile(file, legacySettingsPath())
this.configDefaults = { ...DEFAULT_SETTINGS, ...settingsFromConfig(rawConfig) } const fromConfig = settingsFromConfig(rawConfig)
this.configDefaults = { ...DEFAULT_SETTINGS, ...fromConfig }
this.configExplicit = { activityEnabled: fromConfig.activityEnabled, memoryEnabled: fromConfig.memoryEnabled }
this.state = this.read() this.state = this.read()
} }
@ -189,12 +197,22 @@ export class SettingsStore {
settings(): Settings { settings(): Settings {
const resolved: Settings = { ...this.configDefaults } const resolved: Settings = { ...this.configDefaults }
if (this.state.enabled !== undefined) resolved.enabled = this.state.enabled if (this.state.enabled !== undefined) resolved.enabled = this.state.enabled
if (this.state.activityEnabled !== undefined) resolved.activityEnabled = this.state.activityEnabled
if (this.state.memoryEnabled !== undefined) resolved.memoryEnabled = this.state.memoryEnabled
if (this.state.baseUrl !== undefined) resolved.baseUrl = this.state.baseUrl if (this.state.baseUrl !== undefined) resolved.baseUrl = this.state.baseUrl
if (this.state.clientId !== undefined) resolved.clientId = this.state.clientId if (this.state.clientId !== undefined) resolved.clientId = this.state.clientId
if (this.state.requestTimeoutMs !== undefined) resolved.requestTimeoutMs = this.state.requestTimeoutMs if (this.state.requestTimeoutMs !== undefined) resolved.requestTimeoutMs = this.state.requestTimeoutMs
if (this.state.defaultK !== undefined) resolved.defaultK = this.state.defaultK if (this.state.defaultK !== undefined) resolved.defaultK = this.state.defaultK
if (this.state.summaryEnabled !== undefined) resolved.summaryEnabled = this.state.summaryEnabled if (this.state.summaryEnabled !== undefined) resolved.summaryEnabled = this.state.summaryEnabled
if (this.state.summaryMaxChars !== undefined) resolved.summaryMaxChars = this.state.summaryMaxChars if (this.state.summaryMaxChars !== undefined) resolved.summaryMaxChars = this.state.summaryMaxChars
// The split gates inherit `enabled` unless the config or the sidecar set
// them explicitly, so an old configuration keeps its exact behaviour.
if (this.state.activityEnabled === undefined && this.configExplicit.activityEnabled === undefined) {
resolved.activityEnabled = resolved.enabled
}
if (this.state.memoryEnabled === undefined && this.configExplicit.memoryEnabled === undefined) {
resolved.memoryEnabled = resolved.enabled
}
return resolved return resolved
} }
@ -246,6 +264,14 @@ export class SettingsStore {
if (typeof patch.enabled !== 'boolean') return { ok: false, error: 'enabled must be a boolean' } if (typeof patch.enabled !== 'boolean') return { ok: false, error: 'enabled must be a boolean' }
next.enabled = patch.enabled next.enabled = patch.enabled
} }
if (patch.activityEnabled !== undefined) {
if (typeof patch.activityEnabled !== 'boolean') return { ok: false, error: 'activityEnabled must be a boolean' }
next.activityEnabled = patch.activityEnabled
}
if (patch.memoryEnabled !== undefined) {
if (typeof patch.memoryEnabled !== 'boolean') return { ok: false, error: 'memoryEnabled must be a boolean' }
next.memoryEnabled = patch.memoryEnabled
}
if (patch.baseUrl !== undefined) { if (patch.baseUrl !== undefined) {
const baseUrl = normalizeBaseUrl(patch.baseUrl) const baseUrl = normalizeBaseUrl(patch.baseUrl)
if (baseUrl === undefined) return { ok: false, error: 'baseUrl must be an http(s) URL' } if (baseUrl === undefined) return { ok: false, error: 'baseUrl must be an http(s) URL' }