{"modulePath":"github.com/memcode-ai/memcode","version":"v0.56.16","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package policy\n\nPackage policy is memcode's user-authored behavior layer: typed settings on named decision points, resolved deterministically at runtime.\n\nThe rule the whole package exists to enforce:\n\n\tPolicy chooses behavior. The model may not synthesize policy.\n\nA user says \"always review plans with grok\"; the model translates that into one Set call; the runtime reads the result. What must never happen is a model deciding on its own that some task deserves a different model — that is the automatic routing memcode deleted, and it is not allowed back in through a settings API.\n\nTwo consequences run through the design. Policies are UNCONDITIONAL: every one applies every time, and nothing inspects or classifies a task to decide whether a policy fires. And fallbacks are OPERATIONAL only: a declared fallback exists for a provider that cannot be reached, never for a result that looked weak.\n\nThis is deliberately NOT internal/prefs. That system infers standing preferences from repeated signals and injects advisory prose into the prompt. This one is explicit, immediate, programmatic, and never inferred. Neither writes to the other.\n\n## Constants\n\n```go\nconst (\n\t// ModeOff / ModeOffer / ModeAlways are the shared vocabulary for a step\n\t// that can be disabled, offered, or run every time. They are an enum rather\n\t// than a bool because \"never review\", \"offer review\" and \"always review\n\t// with X\" are three independent things, and a bool plus a nil model can\n\t// only express two of them.\n\tModeOff    = \"off\"\n\tModeOffer  = \"offer\"\n\tModeAlways = \"always\"\n)\n```\n\n## Variables\n\n```go\nvar PersistedScopes = []Scope{ScopeWorkspace, ScopeUser}\n```\nPersistedScopes are the scopes backed by a file. Session and override are deliberately absent: they are held in memory by whoever owns them.\n\n## Functions\n\n```go\nfunc Save(path string, s Set) error\n```\nSave writes a policy file atomically.\n\n```go\nfunc SetField(path string, t Target, field string, raw any) error\n```\nSetField validates a value and writes it to one scope's file.\n\n```go\nfunc UnsetTarget(path string, t Target) error\n```\nUnsetTarget removes a whole target from one scope, leaving every other scope untouched — resetting \"how explore agents behave\" in this repo must not disturb what was set everywhere.\n\n```go\nfunc UserPath() string\n```\nUserPath is $XDG\\_CONFIG\\_HOME/memcode/policy.json, else ~/.config/memcode/policy.json. \"\" when no home directory can be determined.\n\n```go\nfunc Validate(t Target, field string, raw any) (any, error)\n```\nValidate normalises and checks a value against its field's kind. It runs at SET time so a bad policy is refused where the user can see it, not two turns later inside a failing call.\n\n```go\nfunc WorkspacePath(root string) string\n```\nWorkspacePath is \\\u003croot\u003e/.memcode/policy.json.\n\n## Types\n\n```go\ntype Field struct {\n\tName string\n\tKind Kind\n\tEnum []string // KindEnum / KindStrategy: the permitted values\n\tMin  int      // KindInt\n\tMax  int      // KindInt\n\tDef  any      // the component default — what applies when nothing is set anywhere\n\tDoc  string   // shown in `/policy` and in the tool description\n}\n```\nField is one typed knob on a target.\n\n```go\ntype Kind string\n```\nKind is the closed set of field types. Values are validated against their kind at SET time, so a malformed policy is refused where the user can see the refusal, rather than surfacing as a broken turn later.\n\n```go\nconst (\n\tKindModel     Kind = \"model\"      // a pinnable catalog label\n\tKindModelList Kind = \"model_list\" // an ordered list of pinnable labels\n\tKindEnum      Kind = \"enum\"       // one of Field.Enum\n\tKindInt       Kind = \"int\"        // an integer within [Min,Max]\n\tKindStrategy  Kind = \"strategy\"   // an enum that selects HOW a value is produced\n)\n```\n\n```go\ntype Option func(*resolveOpts)\n```\nOption customises one Resolve call.\n\n```go\nfunc Override(t Target, fields map[string]any) Option\n```\nOverride supplies values scoped to THIS operation. It is passed in by whoever owns the operation and is never stored: \"review this plan with kimi\" belongs to that plan and disappears when the plan does. There is no global consume-on-next-use state that could leak into an unrelated later operation.\n\n```go\ntype Resolved struct {\n\tTarget Target\n\tValues map[string]Value\n}\n```\nResolved is a target's effective policy.\n\n```go\nfunc (r Resolved) Enum(field string) string\n```\nEnum returns an enum/strategy field.\n\n```go\nfunc (r Resolved) Int(field string) int\n```\nInt returns an int field, falling back to the schema default.\n\n```go\nfunc (r Resolved) List(field string) []string\n```\nList returns a model\\_list field.\n\n```go\nfunc (r Resolved) Mode() string\n```\nMode is the conventional name for a target's off/offer/always field.\n\n```go\nfunc (r Resolved) Model(field string) string\n```\nModel returns a model-label field, \"\" when unset all the way down.\n\n```go\nfunc (r Resolved) Source(field string) Scope\n```\nSource reports which scope supplied a field.\n\n```go\ntype Resolver struct {\n\tSession   Set\n\tWorkspace Set\n\tUser      Set\n\n\t// Primary is the session's primary model pin — where a model chain ends\n\t// for a schema that declares InheritsPrimaryModel.\n\tPrimary string\n}\n```\nResolver holds the layers and answers Resolve. One per session.\n\n```go\nfunc (r *Resolver) Resolve(t Target, opts ...Option) Resolved\n```\nResolve computes a target's effective policy, field by field.\n\nPer field: override -\u003e session -\u003e workspace -\u003e user -\u003e parent target -\u003e primary pin (if the schema ends there) -\u003e the schema default. Per-field rather than per-target means a user-level model and a workspace-level fallback list compose instead of one silently replacing the other.\n\n```go\ntype Schema struct {\n\tTarget Target\n\tDoc    string\n\tFields []Field\n\n\t// Parent is the target an unset field falls through to. Inheritance is\n\t// DECLARED here, never special-cased in the resolver: agent.explore names\n\t// agent.delegated as its parent, and a future agent.research or plan.scout\n\t// does the same and needs no resolution code of its own.\n\tParent Target\n\n\t// InheritsPrimaryModel ends a model chain at the session's primary pin.\n\t// This is how \"unset delegated means run everything on the user's own\n\t// model\" is expressed as schema rather than as a hand-written chain.\n\tInheritsPrimaryModel bool\n}\n```\nSchema declares a target's fields and where it inherits from.\n\n```go\nfunc Lookup(t Target) (Schema, bool)\n```\nLookup returns a target's schema.\n\n```go\nfunc (s Schema) Field(name string) (Field, bool)\n```\nField returns a field by name.\n\n```go\ntype Scope string\n```\nScope names where a resolved value came from. Every resolved field reports one, because per-field resolution across four layers is only comprehensible if the user can ask \"why is it that, and who set it?\".\n\n```go\nconst (\n\tScopeOverride  Scope = \"override\"  // this operation only\n\tScopeSession   Scope = \"session\"   // this session\n\tScopeWorkspace Scope = \"workspace\" // this repo\n\tScopeUser      Scope = \"user\"      // everywhere\n\tScopeInherited Scope = \"inherited\" // a parent target, or the primary pin\n\tScopeDefault   Scope = \"default\"   // the schema's declared default\n)\n```\n\n```go\ntype Set map[Target]map[string]any\n```\nSet is one scope's contents: target -\u003e field -\u003e value.\n\n```go\nfunc Load(path string) Set\n```\nLoad reads a policy file. A missing or corrupt file is \"no policy\", never an error: a settings file that fails to parse must not stop a session starting.\n\n```go\nfunc (s Set) Clear(t Target)\n```\nClear removes a whole target — the granularity \"reset how explore agents behave\" needs.\n\n```go\nfunc (s Set) Put(t Target, field string, v any)\n```\nPut stores a validated value. Callers should go through Validate first; SetField does both.\n\n```go\ntype Target string\n```\nTarget names a stable decision point. Adding a controllable behavior means registering a Target with a schema and calling Resolve where the decision is made — there is no other extension mechanism, and no target-specific resolution code anywhere.\n\n```go\nconst (\n\t// AgentDelegated is the model for delegated work: agent-tool workers,\n\t// scouts, plan research. Unset inherits the session's primary pin.\n\tAgentDelegated Target = \"agent.delegated\"\n\t// AgentExplore narrows AgentDelegated for read-only explore/scout agents.\n\tAgentExplore Target = \"agent.explore\"\n\t// PlanReview governs the second-model plan critique.\n\tPlanReview Target = \"plan.review\"\n\t// PlanAdvisor governs the plan-mode advisor.\n\tPlanAdvisor Target = \"plan.advisor\"\n\t// StartupModel governs how a session picks its primary model at launch.\n\tStartupModel Target = \"startup.model\"\n\t// UITheme governs the colour theme.\n\tUITheme Target = \"ui.theme\"\n\t// SessionEffort governs the default thinking depth.\n\tSessionEffort Target = \"session.effort\"\n)\n```\n\n```go\nfunc Targets() []Target\n```\nTargets lists every registered target in a stable order.\n\n```go\ntype Value struct {\n\tRaw    any\n\tSource Scope\n}\n```\nValue is one resolved field plus where it came from.\n\n","path":"github.com/memcode-ai/memcode/internal/policy","name":"policy","synopsis":"Package policy is memcode's user-authored behavior layer: typed settings on named decision points, resolved deterministically at runtime.","isRedistributable":true}
