mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-09 16:35:27 +02:00
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work
> - Hosting operators (a managed cloud, an internal shared server) tune
the settings surface with `PAPERCLIP_HIDDEN_SETTINGS`, but hiding a
control never changes its value
> - An instance whose stored feedback-sharing preference is still the
schema default ("prompt") keeps prompting users even when the operator
hid the control, leaving them no way to answer
> - More generally, operators have no supported way to change what a
setting defaults to without patching code
> - This pull request adds `PAPERCLIP_SETTING_DEFAULTS`, a generic
operator-supplied read-time default overlay for registry-listed general
settings
> - The benefit is that any hosting operator can pair "hide the control"
with "default the value", while explicit user choices and self-hosted
stock behavior stay untouched
## Linked Issues or Issue Description
No public issue exists; following the enhancement template:
**What existing behavior does this improve?**
Hosting operators need to supply the default value of selected instance
settings (first: `feedbackDataSharingPreference`) via configuration,
without patching code and without a hard-coded, opinionated constant in
the product.
**Subsystem affected**
Server (instance-settings service, feedback service, boot) and
`packages/shared` (settings schemas).
**Current behavior**
Setting defaults are fixed in the shared zod schemas.
`PAPERCLIP_HIDDEN_SETTINGS` can hide the feedback-sharing control and
floor writes, but the stored value stays "prompt", so issue-chat
surfaces keep prompting with no way to answer.
**Proposed behavior**
`PAPERCLIP_SETTING_DEFAULTS` takes a JSON object validated against a
shared registry of defaultable fields. The operator value substitutes
for the schema default at read time: a field whose effective value is
still the schema default resolves to the operator value; an explicit
non-default user choice always wins. Never persisted; unsetting the
variable restores stock behavior. Malformed JSON or an invalid value for
a known field refuses startup (fail closed); unknown field names warn
and are ignored (mixed-version fleet safe).
**Reason and benefit**
Any hosting operator can pair "hide the control" with "default the
value" without forking the product. Explicit user choices and
self-hosted stock behavior stay untouched.
**Breaking changes**
None. With the variable unset, every read path is byte-identical to
before.
## What Changed
- New `packages/shared/src/setting-defaults.ts`:
`SETTING_DEFAULTS_ENV_KEY`, `DEFAULTABLE_GENERAL_SETTINGS` registry
(currently `feedbackDataSharingPreference`), `parseSettingDefaults`
(fail-closed for policy content, warn-ignore unknown fields),
`applyOperatorGeneralDefaults` (pure read-time overlay),
`stripOperatorGeneralEchoes` (persist-time echo strip, see below),
re-exported from the package index.
- New `server/src/services/setting-defaults.ts`: parse-once accessor
mirroring `settings-visibility.ts`.
- `server/src/services/instance-settings.ts`: `toGeneralView` applies
the overlay in `get`/`getGeneral`/update responses; persisted writes
never carry operator values. Because general-settings writes materialize
every field, a stored schema-default value is treated as unchosen —
deliberate, documented, and covered by tests.
- `server/src/services/feedback.ts`: the preference-persistence branch
now checks the effective (overlaid) preference, so a stray prompt answer
cannot overwrite an operator default; its local normalize fallback now
returns full schema defaults.
- `server/src/index.ts`: boot-time fail-fast parse with a log line
naming the defaulted settings, mirroring the managed-config posture.
- The hidden-settings write floor (`assertNoHiddenSettingChanges`) keeps
comparing against effective values, so clients echoing a full GET
response keep working. To keep the overlay strictly read-time,
`updateGeneral` strips such echoes at persist time: a write of the
operator value over a field whose stored value is still the schema
default (unchosen) maps back to the schema default, so an echo cannot
promote the operator value into an explicit stored choice and later
changes to (or removal of) `PAPERCLIP_SETTING_DEFAULTS` still take
effect. A write of any other value, or over an explicit stored choice,
persists as given.
- Docs: `PAPERCLIP_SETTING_DEFAULTS` row + "Operator setting defaults"
section in `docs/deploy/environment-variables.md`.
- Tests: `packages/shared/src/setting-defaults.test.ts` (parse matrix,
overlay precedence, echo-strip matrix, immutability) and
`server/src/__tests__/instance-settings-operator-defaults.test.ts`
(accessor, substitution, explicit-choice wins, unset identity,
never-persisted, full-GET echo stays unchosen, explicit non-default
write persists).
## Verification
- `npx vitest run packages/shared/src/setting-defaults.test.ts
server/src/__tests__/instance-settings-operator-defaults.test.ts
server/src/__tests__/instance-settings-managed-overlay.test.ts` — 33
tests passing.
- `npx vitest run server/src/__tests__/instance-settings-routes.test.ts
server/src/__tests__/instance-settings-service.test.ts` — 57 passing;
`npx vitest run server/src/__tests__/feedback-service.test.ts
server/src/__tests__/issue-feedback-routes.test.ts` — 18 passing.
- `pnpm --filter @paperclipai/shared typecheck` and `pnpm --filter
@paperclipai/server typecheck` — clean.
## Risks
- Low. With the variable unset every read path is byte-identical to
before (identity overlay, covered by tests). The overlay is read-time
only and never persisted, so no migration and no data risk. Fail-closed
parsing means a bad policy value is a loud boot failure rather than
silent drift — consistent with the existing managed-config contract.
## Model Used
Claude (Anthropic), model id `claude-fable-5`, extended thinking,
agentic tool use via Claude Code.
## Checklist
- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge
164 lines
6.4 KiB
TypeScript
164 lines
6.4 KiB
TypeScript
import { instanceGeneralSettingsSchema } from "./validators/instance.js";
|
|
import type { InstanceGeneralSettings } from "./types/instance.js";
|
|
|
|
/**
|
|
* Operator-configurable setting defaults.
|
|
*
|
|
* A hosting operator (a managed cloud, an internal shared server) can replace
|
|
* the schema default of selected instance settings by setting the
|
|
* `PAPERCLIP_SETTING_DEFAULTS` environment variable to a JSON object, e.g.
|
|
* `{"feedbackDataSharingPreference":"allowed"}`. The operator value
|
|
* substitutes for the schema default at read time: any field whose effective
|
|
* value is still the schema default resolves to the operator value, while an
|
|
* explicit non-default user choice always wins. The overlay is never
|
|
* persisted, so unsetting the variable restores stock behavior everywhere a
|
|
* user has not chosen otherwise.
|
|
*
|
|
* Parsing is fail-closed for policy content: malformed JSON or an invalid
|
|
* value for a known field is an error (the server refuses to boot), because a
|
|
* silently dropped policy default is worse than a loud failure. Unknown field
|
|
* names are warned about and ignored, so one value can be rolled across a
|
|
* fleet of mixed app versions where older images predate a field.
|
|
*
|
|
* Pairing note: an operator that also wants the control invisible hides it
|
|
* with `PAPERCLIP_HIDDEN_SETTINGS` (see settings-visibility.ts); the two
|
|
* mechanisms are orthogonal.
|
|
*/
|
|
|
|
export const SETTING_DEFAULTS_ENV_KEY = "PAPERCLIP_SETTING_DEFAULTS";
|
|
|
|
/** Instance → General fields whose schema default an operator may replace. */
|
|
export const DEFAULTABLE_GENERAL_SETTINGS = [
|
|
"feedbackDataSharingPreference",
|
|
] as const;
|
|
|
|
export type DefaultableGeneralSetting = (typeof DEFAULTABLE_GENERAL_SETTINGS)[number];
|
|
|
|
export type OperatorSettingDefaults = Partial<
|
|
Pick<InstanceGeneralSettings, DefaultableGeneralSetting>
|
|
>;
|
|
|
|
export interface ParsedSettingDefaults {
|
|
/** Validated operator defaults for known fields; null when the var is unset. */
|
|
defaults: OperatorSettingDefaults | null;
|
|
/** Unrecognized field names, for the caller to warn about. */
|
|
unknown: string[];
|
|
}
|
|
|
|
const defaultableFieldsSchema = instanceGeneralSettingsSchema
|
|
.pick(
|
|
Object.fromEntries(DEFAULTABLE_GENERAL_SETTINGS.map((key) => [key, true])) as {
|
|
[K in DefaultableGeneralSetting]: true;
|
|
},
|
|
)
|
|
.partial();
|
|
|
|
/** The all-schema-defaults view used to decide whether a value was chosen. */
|
|
const schemaDefaults: InstanceGeneralSettings = instanceGeneralSettingsSchema.parse({});
|
|
|
|
/**
|
|
* Parse a `PAPERCLIP_SETTING_DEFAULTS`-style JSON object.
|
|
*
|
|
* @throws when the JSON is malformed, not an object, or a known field carries
|
|
* an invalid value — policy configuration fails closed.
|
|
*/
|
|
export function parseSettingDefaults(raw: string | undefined): ParsedSettingDefaults {
|
|
if (raw === undefined || raw.trim() === "") return { defaults: null, unknown: [] };
|
|
let parsed: unknown;
|
|
try {
|
|
parsed = JSON.parse(raw);
|
|
} catch (error) {
|
|
throw new Error(
|
|
`${SETTING_DEFAULTS_ENV_KEY} is not valid JSON: ${error instanceof Error ? error.message : String(error)}`,
|
|
);
|
|
}
|
|
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
throw new Error(`${SETTING_DEFAULTS_ENV_KEY} must be a JSON object of setting defaults`);
|
|
}
|
|
const known: Record<string, unknown> = {};
|
|
const unknown: string[] = [];
|
|
const defaultable = new Set<string>(DEFAULTABLE_GENERAL_SETTINGS);
|
|
for (const [key, value] of Object.entries(parsed)) {
|
|
if (defaultable.has(key)) {
|
|
known[key] = value;
|
|
} else {
|
|
unknown.push(key);
|
|
}
|
|
}
|
|
const result = defaultableFieldsSchema.safeParse(known);
|
|
if (!result.success) {
|
|
throw new Error(
|
|
`${SETTING_DEFAULTS_ENV_KEY} carries an invalid value: ${result.error.issues
|
|
.map((issue) => `${issue.path.join(".")}: ${issue.message}`)
|
|
.join("; ")}`,
|
|
);
|
|
}
|
|
return { defaults: result.data as OperatorSettingDefaults, unknown };
|
|
}
|
|
|
|
/**
|
|
* Overlay operator defaults onto normalized general settings at read time.
|
|
*
|
|
* The operator value substitutes for the schema default: a field still at its
|
|
* schema default (absent from storage, or stored equal to it) resolves to the
|
|
* operator value; an explicit non-default choice is untouched. Callers must
|
|
* never persist the result.
|
|
*/
|
|
export function applyOperatorGeneralDefaults(
|
|
general: InstanceGeneralSettings,
|
|
defaults: OperatorSettingDefaults | null,
|
|
): InstanceGeneralSettings {
|
|
if (!defaults) return general;
|
|
let next: InstanceGeneralSettings | null = null;
|
|
for (const key of DEFAULTABLE_GENERAL_SETTINGS) {
|
|
const value = defaults[key];
|
|
if (value === undefined) continue;
|
|
if (general[key] === schemaDefaults[key] && general[key] !== value) {
|
|
next ??= { ...general };
|
|
next[key] = value;
|
|
}
|
|
}
|
|
return next ?? general;
|
|
}
|
|
|
|
/**
|
|
* Strip overlay echoes from a general-settings write at persist time.
|
|
*
|
|
* A client that writes back the full object it read (a full-GET echo) sends
|
|
* the overlaid operator value for a field the user never chose. Persisting
|
|
* that echo would promote the operator value into an explicit stored choice —
|
|
* sticky across later changes to, or removal of, the environment variable.
|
|
* This maps such a write back to the schema default: a field whose stored
|
|
* value is still the schema default (unchosen) and whose incoming value
|
|
* equals the operator default stays unchosen, keeping the overlay
|
|
* strictly read-time.
|
|
*
|
|
* A user cannot be distinguished from an echo when they deliberately pick the
|
|
* value that already shows as the default, so that pick also stays unchosen —
|
|
* the mirror image of the documented "stored schema default is treated as
|
|
* unchosen" rule, with identical effective behavior. Any other write persists
|
|
* as given: an incoming value that differs from the operator default, or a
|
|
* write over an explicit stored choice.
|
|
*/
|
|
export function stripOperatorGeneralEchoes(
|
|
stored: InstanceGeneralSettings,
|
|
next: InstanceGeneralSettings,
|
|
defaults: OperatorSettingDefaults | null,
|
|
): InstanceGeneralSettings {
|
|
if (!defaults) return next;
|
|
let result: InstanceGeneralSettings | null = null;
|
|
for (const key of DEFAULTABLE_GENERAL_SETTINGS) {
|
|
const value = defaults[key];
|
|
if (value === undefined) continue;
|
|
if (
|
|
stored[key] === schemaDefaults[key] &&
|
|
next[key] === value &&
|
|
next[key] !== schemaDefaults[key]
|
|
) {
|
|
result ??= { ...next };
|
|
result[key] = schemaDefaults[key];
|
|
}
|
|
}
|
|
return result ?? next;
|
|
}
|