Clapilot-Agent Learning Contracts
Contracts and Milestone 5 runtime learning state for durable facts, procedure proposals, hot memory snapshots, approvals, audit state, scopes, and runtime learning.
Learning is how Clapilot turns things it observed during work — a user preference, a client rule, a repeated workflow — into governed objects that can later be injected back into prompts. Safe, evidence-backed facts and preferences use an opt-out activation policy: they become active immediately, while exceptions stay inactive. A configurable model-backed curator later checks only new or changed facts and rejects high-confidence false, unsafe, duplicate, or non-durable content. Every learning object carries explicit visibility, activation/approval state, provenance, and cost attribution, so an admin can always answer what was learned, from what, which policy or model checked it, and what it costs. This page is the canonical contract for that model.
This page defines the learning object model for the native clapilot-agent runtime. Milestone 2 added passive storage and inspection for learning objects, approval decisions, and audit events. Milestone 3 added controlled runtime retrieval for approved learning objects as a separate prompt layer. Milestone 4 added conservative post-response extraction. Milestone 5 adds opt-out activation, agentic curation, and admin review for exceptions. The protected Learning Curator system automation runs with the model selected on /geplante-aufgaben. It evaluates only facts it has not checked at their current content hash, defaults to keeping them, and rejects only high-confidence false, unsafe, duplicate, or non-durable content. Procedures, corrections, conflicts, weak or unsafe evidence, and other exceptions remain review-only. The runtime exposes read-only learning_search / learning_get_object tools for approved, visible objects plus unified read access through context_search / context_get. It still does not add agent-facing learning mutation tools or executable procedure promotion.
The goal is to make future learning features explicit before implementation:
- what can be learned
- who can see it
- who or what approved it
- how it was derived
- how much it cost to create or retrieve
- when it is ready to become an agent-facing tool or autonomous workflow
Milestone 5 Implementation Status
Milestone 2 implemented the control-plane substrate:
- PostgreSQL tables for learning objects, approvals, and audit events
- native internal endpoints under
/internal/learningand/internal/learning-objects - admin app endpoints under
/api/agent-runtime/learning - a ClapilotAICore Learning settings page for inspection and curation
Milestone 3 added the first runtime use path:
- internal retrieval under
POST /internal/learning-objects/retrieve - approved-learning prompt selection inside
sessions.executeRun() - the
approved_learning_objectsprompt token layer in request logs and run metadata learning.used_in_promptaudit events for objects actually injected into a provider prompt- retrieval-focused indexes for prompt-eligible objects, validity windows, text matching, and prompt-use audit history
Milestone 4 adds the first self-learning write path:
memory.storeSharedFacts()returns the already extracted shared-fact text and token attribution to the post-response runtimelearningObjects.runConservativeExtraction()converts shared-fact lines or explicit "remember this" user instructions into canonical Memory v2 assertions and Learning projections- subscription-bridge chat runs can create candidates by default because extraction reuses shared-facts output and records the runtime provider in source refs;
CLAPILOT_AGENT_LEARNING_EXTRACTION_ALLOW_SUBSCRIPTION_RUNTIME=falsedisables that path - the opt-out policy immediately activates evidence-backed facts and preferences with safe scope, sufficient confidence, and no unresolved conflict or correction semantics
CLAPILOT_AGENT_LEARNING_EXTRACTION_AUTO_APPROVE_SAFE_FACTSandCLAPILOT_AGENT_LEARNING_EXTRACTION_AUTO_APPROVE_EXPLICIT_USER_FACTSdefault totrue; policy activation recordsauto_approved_by_policyplus policy provenance instead of pretending a person reviewed the object- the Learning settings queue contains the exceptions that need a decision; policy-activated facts remain visible in the full inspector and audit trail
- policy activation is reversible until a human review adopts the object; rejection changes an active fact to
status=rejected, removing it from future prompt retrieval - low-risk procedure observations start as
status=needs_evidence,proposal_kind=procedure, andreadiness_state=needs_evidence - procedure proposals, corrections, conflicts, weak evidence, and unsafe categories require review before they can become prompt context; safe durable policy/workflow facts can use the same opt-out activation path as other facts
run.postprocess.completedevents report candidate, created, auto-approved, skipped, skipped-reason, and error counts- internal diagnostics can call
POST /internal/learning-objects/extract
Milestone 5 adds the first curation workflow:
- the ClapilotAICore Learning settings page surfaces exception states such as
candidate,draft,needs_evidence,needs_tools, andneeds_approvalin a dedicated exception-review queue; safe policy-activated facts do not require repetitive approval - admins can approve, reject, or request changes for one object, with optional decision notes
- admins can bulk approve or bulk reject selected review-queue objects
- the full object list can be searched and filtered by status, object type, and visibility scope
- all curation actions append approval/audit state through the existing decision endpoint; they do not create agent-facing tools
Read-only agent access is now available:
learning_searchsearches approved, visible, unexpired learning objects using the same prompt-eligible approval and visibility checks as prompt injectionlearning_get_objectreads one approved, visible learning object by idcontext_searchincludes approved, visible Learning objects in a broader internal-context pass alongside Wiki, native memory, exact session history/summaries, and the Knowledge Graphcontext_getcan readlearning:<id>results through the same visibility checks aslearning_get_object- candidates, rejected objects, audit rows, approval decisions, extraction, curation, and promotion remain admin/runtime control-plane capabilities rather than agent tools
Bundled automated curation adds the agentic cleanup loop:
- native installs ship a protected
Learning Curatorsystem automation under/geplante-aufgaben - the job calls
learningObjects.runAgenticCuration()with the provider/model selected on the automation; a blank model fails closed and leaves facts unchanged - candidates are limited to canonical durable facts with evidence and no terminal manual decision, and a content hash prevents rechecking unchanged facts
- the prompt is opt-out: keep is the default, and rejection requires a reason plus configurable minimum confidence (default
0.8) - high-confidence false, contradicted, duplicate, non-durable, runtime-noise, unsafe, or clearly worthless facts are rejected; uncertain facts are kept
- manual admin/user decisions are authoritative and are rechecked immediately before any curator mutation
- every curator result records model, policy version, content hash, confidence, reason code, explanation, and audit events
- Dreaming v2 retains a targeted deterministic safety pass for its newly written assertions. Safe, durable, evidence-backed facts, preferences, decisions, constraints, and procedures are policy-approved immediately; unsupported Dream assertions are policy-rejected instead of entering the exception-review queue. Approved projections can therefore extend recall and the Knowledge Graph before the scheduled agentic quality-control pass.
The active storage families are durable facts, procedure / skill proposals, and hot memory snapshots. Approval state and audit events are stored in dedicated append-friendly tables instead of being mixed into the primary object table. Prompt retrieval uses those tables directly; it does not create user-callable tools.
Still intentionally out of scope:
- no agent-facing learning write, approval, rejection, extraction, curation, or promotion tools
- no separate extraction model call beyond the existing shared-facts postprocess
- no agent-facing curator call; model-backed curation remains a protected scheduled control-plane operation
- no promotion of procedure proposals into executable tools, specialists, or automations
Object Families
The contract separates learning state into five object families.
| Family | Purpose | Expected durability | Current agent-facing behavior |
|---|---|---|---|
| Durable fact | Stable knowledge extracted from work, conversations, channels, documents, or explicit user instruction | Long-lived until superseded, expired, or deleted | Approved, visible, unexpired facts can be injected as context and read through context_search / context_get or learning_search / learning_get_object |
| Procedure / skill proposal | Candidate repeatable workflow, prompt pattern, or operational checklist suggested by observed work | Draft until approved and promoted | Approved ready proposals can be injected only as procedure hints and read through context_search / context_get or learning_search / learning_get_object, not executed as tools |
| Hot memory snapshot | Short-lived, high-salience working set for one session, entity, channel, or task | Minutes to days depending on owner and source | Active, visible, unexpired snapshots can be injected as short-lived context and optionally searched when the tool call requests snapshots |
| Approval state | Human or policy decision that allows, denies, or constrains learned content promotion | Durable audit/control state | Stored as approval rows linked to learning objects |
| Audit event | Immutable trace of extraction, proposal, approval, promotion, use, cost, or deletion | Long-lived operational ledger | Stored as audit rows linked to learning objects |
These families should stay separate even when they share fields. A durable fact is not a procedure. A hot memory snapshot is not a durable fact. An approval decision should never be overwritten by rewriting the learned object.
Common Envelope
Every learned object should carry the following envelope when implemented:
| Field | Meaning |
|---|---|
id | Stable object id |
object_type | durable_fact, procedure_proposal, hot_memory_snapshot, approval_state, or audit_event |
schema_version | Integer version for contract migration |
title | Human-readable short label |
body | Canonical learned content or proposal body |
summary | Optional compact display/retrieval summary |
status | Lifecycle state, family-specific |
visibility_scope | Access boundary described below |
subject_refs | Structured links to users, teams, channels, tasks, documents, modules, routes, clients, providers, or jobs |
source_refs | Raw source anchors such as run id, event id, chat message id, document id, tool call id, file path, or channel thread key |
confidence | Numeric confidence from 0 to 1 when derived by a model or heuristic |
created_by_kind | user, agent, system, import, or admin |
created_by_ref | User id, session key, job id, import id, or system component |
created_at | Creation timestamp |
updated_at | Last object update timestamp |
expires_at | Optional expiry for hot or context-sensitive state |
supersedes_id | Optional previous object id replaced by this object |
metadata | Family-specific JSON payload |
source_refs must be specific enough that an admin can reconstruct why the object exists without replaying an entire workspace.
Durable Facts
Durable facts represent stable learned knowledge. Examples:
- a user's preferred invoice wording
- a client's recurring filing rule
- a team workflow convention
- a channel-specific routing rule
- a verified operational fact from a completed task
Required contract additions beyond the common envelope:
| Field | Meaning |
|---|---|
fact_kind | preference, entity_fact, workflow_fact, policy_fact, operational_fact, or correction |
assertion | The concise fact text used for retrieval and prompt injection |
evidence_refs | Source anchors supporting the fact |
valid_from | Optional start date for time-bound facts |
valid_until | Optional end date for facts that should decay or stop applying |
conflicts_with[] | Object ids that disagree or overlap |
promotion_state | candidate, approved, rejected, superseded, or expired |
Default policy:
- user preference facts start as
privateunless explicitly shared - client, document, task, and team facts start as
teamwhen created in authenticated web/team contexts - unknown or unlinked channel facts start as
channel - operational runtime facts can be
workspaceonly when they are not user- or client-specific
Durable facts should be safe to retrieve repeatedly. If a learned item is only useful for the current run or near-term task, model it as a hot memory snapshot instead.
Procedure And Skill Proposals
Procedure / skill proposals are candidate repeatable workflows. They are not active tools in the current runtime.
Examples:
- "When a German VAT deadline email arrives, create a task, link the client, and draft a reply."
- "For this client, use the three-step monthly bookkeeping checklist."
- "This recurring prompt should become a named specialist or automation."
Required contract additions:
| Field | Meaning |
|---|---|
proposal_kind | procedure, skill, automation, specialist_instruction, or tool_hint |
trigger_pattern | Natural-language or structured pattern describing when the procedure applies |
steps[] | Ordered human-readable steps; must not imply executable code yet |
required_tools[] | Existing tool names or missing future capabilities |
required_auth_scopes[] | App/resource access needed before execution |
risk_level | low, medium, high, or blocked |
approval_required | Boolean; default true for anything that mutates user/business data |
readiness_state | draft, needs_evidence, needs_tools, needs_approval, ready_for_milestone_2, promoted, or rejected |
Promotion rules:
- proposals may reference current tool contracts, but they are not tool contracts themselves
- proposals that require new tool schemas must stay
needs_tools - proposals that mutate data or send external messages must stay blocked until approval and audit paths exist
- future promotion should create or update a separate executable artifact; it should not mutate the proposal into an executable object in place
Hot Memory Snapshots
Hot memory snapshots are compact working sets used to keep near-term context fresh without turning everything into durable memory.
Examples:
- the active task and its last few decisions
- a condensed view of the current channel thread
- current document-editing intent
- active client context across several nearby agent turns
Required contract additions:
| Field | Meaning |
|---|---|
snapshot_kind | session, entity, task, channel, module, or route |
hotness_score | Numeric salience from 0 to 1 |
ttl_seconds | Expected lifetime before refresh or expiry |
source_window | Description of the message/event/run window that produced the snapshot |
refresh_policy | replace, merge, decay, or manual |
retrieval_hint | Optional hint for prompt assembly priority |
Hot memory should default to narrower visibility than durable facts. It can be promoted into a durable fact only through a separate extraction/proposal path with evidence and audit state.
Approval And Audit State
Learning state needs explicit approval and audit contracts before autonomous write or promotion tools are added.
Approval state should answer:
- who approved, rejected, or requested changes
- what exact object version was reviewed
- whether approval came from a user action, admin action, or policy rule
- what scope the approval applies to
- whether approval is reversible
Recommended approval fields:
| Field | Meaning |
|---|---|
target_object_id | Learned object under review |
target_schema_version | Version reviewed |
decision | pending, approved, rejected, changes_requested, revoked, or auto_approved_by_policy |
decided_by_kind | user, admin, system_policy, or agent |
decided_by_ref | User id, policy id, session key, or system component |
decision_reason | Human-readable reason |
decision_scope | Object, subject, user, team, channel, or workspace boundary for the approval |
decided_at | Decision timestamp |
Automatic safe activation uses decided_by_kind=system_policy and records the stable policy id/version in the decision reference or metadata. It is not equivalent to human review: the policy decision can be compensated or revoked until a later user or admin decision adopts the object.
Audit events should be append-only. Recommended audit event names:
learning.extractedlearning.proposedlearning.approval_requestedlearning.approvedlearning.rejectedlearning.promotedlearning.supersededlearning.expiredlearning.used_in_promptlearning.used_by_toollearning.token_cost_attributedlearning.deleted
Audit payloads should include the learned object id, source refs, actor, previous status, next status, visibility scope, and token-cost attribution when available.
Visibility Scopes
Learning objects reuse the current memory visibility language and tighten it for promotion decisions.
| Scope | Intended boundary | Notes |
|---|---|---|
private | One authenticated Clapilot user | Default for personal preferences, drafts, and user-specific corrections |
team | Authenticated users/team contexts in the instance, subject to matching subject_refs | Default for shared business facts created in authenticated web/team contexts |
workspace | Whole Clapilot instance | Only for global operational facts and approved reusable procedures |
channel | One external channel/thread or linked channel context | Default for unknown/unlinked channel learning |
session | One runtime session key | Suitable for hot snapshots and draft facts before promotion |
subject | One or more structured business entities | Use with explicit subject_refs; usually combined with team or private authorization |
Visibility is an access control input, not just a retrieval ranking signal. Retrieval may boost matching subjects, but it must not cross visibility boundaries.
Token-Cost Attribution
Learning should carry cost metadata so admins can understand whether memory improves or bloats runtime work.
Attribution concepts:
| Concept | Meaning |
|---|---|
extraction_input_tokens | Tokens used to derive a learned object from source material |
extraction_output_tokens | Tokens generated while creating the learned object |
embedding_tokens | Tokens used to embed/chunk durable learned content |
retrieval_tokens | Tokens spent retrieving, ranking, or expanding the object |
prompt_injected_tokens | Tokens added to a model prompt because the learned object was selected |
avoided_context_tokens_estimate | Estimated transcript/raw context tokens avoided by using the learned object |
provider_ref | Provider/model used for extraction or embedding |
run_ref | Runtime run id or job id where the cost occurred |
billable_owner_ref | User, team, job, automation, or system owner for cost attribution |
Current learning work does not add billing behavior. It preserves enough metadata to answer:
- which learned objects are expensive to create
- which learned objects are repeatedly injected
- whether a hot snapshot or durable fact reduced prompt size
- whether a procedure proposal came from high-cost or low-confidence evidence
Milestone 1 persists request-level prompt-layer estimates in agent_model_request_logs.prompt_layer_tokens_json. Milestone 2 learning objects added per-object token_cost_json so extraction, retrieval, prompt-injection, and avoided-context estimates can be attached. Milestone 3 adds the approved_learning_objects request layer and learning.used_in_prompt audit events with prompt-injected token estimates. Milestone 4 reuses shared-facts postprocess token estimates when creating candidates, so self-learning does not add another extraction prompt. Subscription-bridge runs use the same reused-output path and attach runtimeProvider in source refs for auditability. Current request attribution covers runtime system prompt, compaction summary, bootstrap prompt files, retrieved memory, approved learning objects, extra system prompt, stored history, current request, tool schemas, message prompt estimate, and provider input estimate.
Runtime Prompt Retrieval
Milestone 3 retrieval is deliberately narrower than generic memory search.
An object is eligible for prompt injection only when:
statusisapproved,active, orpromoted- it is not expired and any
valid_from/valid_untilwindow is current - visibility matches the current user, session, channel, team, workspace, or structured subject refs
- approval-required objects have a latest decision of
approvedorauto_approved_by_policy - procedure proposals have
readiness_stateofready_for_milestone_2orpromotedand are notrisk_level=blocked
The prompt renderer emits a separate Approved learned context system block. That block explicitly tells the model to treat learned items as context data, not higher-priority instructions. The runtime caps this layer to a small share of the prompt budget and drops it before trimming stored conversation history.
Agents can also explicitly query the same approved/visible corpus with read-only tools:
learning_searchreturns a compact list of approved, visible Learning objects. It accepts optionalquery,object_type,include_hot_snapshots,include_metadata, andlimit. The agent-toolobject_typeenum exposes onlydurable_factandprocedure_proposal; snapshots remain supported by storage/read paths and are included withinclude_hot_snapshots=true.learning_get_objectreturns one approved, visible Learning object byid.context_search/context_getexpose the same approved objects as part of broad internal-context lookup; they are preferred when the user asks "what do we know?" rather than specifically asking for the Learning store.
These read-only tools enforce approval state, expiry/validity windows, prompt-readiness for procedure hints, and the same visibility model as prompt injection. They do not expose candidates or rejected objects and cannot approve, reject, extract, edit, delete, or promote learning objects.
Admin And Native API
Milestone 2 exposes admin-only app endpoints:
GET /api/agent-runtime/learninglists recent learning objects, recent audit events, and grouped stats for the Learning settings inspector and exception-review queuePOST /api/agent-runtime/learningcreates a learning object manually or from a controlled backend pathGET /api/agent-runtime/learning/[id]returns one object with linked approvals and audit eventsPOST /api/agent-runtime/learning/[id]/decisionrecords an approval, rejection, changes-requested decision, revocation, or versioned policy approval. The Learning settings exception-review queue uses this endpoint for both single-object decisions and bulk decisions.
The app endpoints proxy to native internal endpoints under /internal/learning and /internal/learning-objects. These are runtime control-plane endpoints, not mutation-capable agent tool names.
Milestone 3 also exposes one internal runtime retrieval endpoint:
POST /internal/learning-objects/retrievereturns prompt-ready approved learning objects for native runtime use. The standard chat path calls the learning module directly rather than through HTTP, but the endpoint exists for internal diagnostics and future runtime callers.
The agent-facing read-only retrieval contract is exposed as native tools rather than public HTTP endpoints:
context_searchsearches approved Learning, Wiki, native memory, session history/summaries, and Knowledge Graph context through a unified read-only facadecontext_getreads one source-qualified result returned bycontext_searchlearning_searchsearches prompt-eligible approved/visible objects through the native tool catalog and/internal/tools/executelearning_get_objectreturns one prompt-eligible approved/visible object through the native tool catalog and/internal/tools/execute
Milestone 4 also exposes one internal extraction diagnostics endpoint:
POST /internal/learning-objects/extractruns the same conservative extractor used by post-response maintenance. It creates canonical candidates/drafts and applies the configured safe fact/preference auto-activation policy. Exceptions remain candidates. It is intended for internal smoke tests and future runtime callers, not agent tool use.POST /internal/learning-objects/curateruns the targeted deterministic safety curator once. OptionalassertionIds/assertion_idsrestrict the pass to Learning projections linked to those canonical assertions; Dreaming v2 uses this targeted form immediately after persistence and gives each targeted Dream assertion an automatic terminal approval/rejection decision. The protected scheduledLearning Curatorinstead calls the model-backedrunAgenticCuration()path with its configuredmodel,batchSize, andminimumRejectConfidencepayload fields.
Promotion Readiness
A learning object is ready for future promotion or prompt use only when the following are true:
- It has a stable
object_type,schema_version, status, scope, source refs, and subject refs. - Its visibility boundary can be enforced before retrieval or execution.
- Its source evidence can be audited without relying on an opaque model summary.
- Any user-visible mutation, external send, workflow automation, or procedure promotion has approval state.
- Token-cost attribution can be attached at extraction, embedding, retrieval, and prompt-injection points.
- Conflicts and supersession can be represented without deleting history.
- The object can be exported in a compact read shape and a full audit shape.
- Existing agent-tool docs can list promoted or read-only behavior without mixing draft proposals with mutation-capable active tools.
Future milestones should add explicit mutation/promotion tool API docs only when executable code exists. Until then, this page is the canonical design contract for learning state and the current read-only retrieval tools.
Integration Points To Preserve
Future implementation should align with existing runtime concepts:
- durable recall, visibility, and subject matching from Memory
- prompt assembly and budget fitting from Request Flows
- provider request logs and token accounting from Providers + Models
- current native memory tools listed in Agent Tool Contracts
- endpoint documentation discipline in API Reference
