Skip to content

feat: inject historical reference into MAF - #8

Merged
fabbrik merged 4 commits into
mainfrom
feat/2-3-inject-historical-reference
Sep 22, 2026
Merged

fabbrik merged 4 commits into
mainfrom
feat/2-3-inject-historical-reference

Conversation

@fabbrik

@fabbrik fabbrik commented Sep 21, 2026

Copy link
Copy Markdown
Owner

Summary

Story 2.3, and the last of Epic 2. The loop closes: a run is captured, verified, reflected on, finalized, indexed — and a later agent receives it.

ExperienceContextProvider is a simple-tier AIContextProvider (Story 1.7 rejected TextSearchProvider: no confidence or applicability fields, and no timeout). The host adds it to ChatClientAgentOptions.AIContextProviders; the capture extension never builds those options, so injection is deliberately a separate type.

Per invocation it resolves a retrieval request, retrieves, selects within the record limit, re-checks every candidate against the same eligibility rules retrieval applies — status, confidence floor, expiry and required environment attributes — asks the host's InjectionDecision, then writes one delimited block.

  • The payload names source, confidence, applicability, when the lesson was learned, its environment, and an evidence summary from the reflection. No attempts, tool calls, arguments, results or evidence detail ever appear.
  • Limits drop whole records, never cut one: 8 records and 16 KB by default, both validated, every omission recorded with its reason.
  • Nothing breaks the run. Empty, timed-out or failed retrieval means no context and a reported result; only the caller's own cancellation propagates.

On labeling

Labeling is hygiene, not a control, and the docs say so. The test that matters makes the model obey the injected instruction and emit a guarded tool call: ApprovalRequiredAIFunction means the tool body never runs. It asserts the boundary held, not that the model behaved.

Known limitation, now documented and pinned by a test

Injected blocks accumulate in a session's history: turn 2 carries the fresh block and turn 1's copy. So a record revoked between turns keeps being sent from the earlier block, and the byte budget bounds one invocation rather than a conversation. MAF filters a provider's input to external messages, so the provider cannot reliably detect its own earlier blocks. Use a fresh session per task when that matters.

Review

Three reviewers raised 35 findings; 34 fixed, 1 deferred (a batched by-IDs read for the re-check). The serious ones: the final check was narrower than the eligibility it claimed to re-apply; the session accumulation above; and nothing pinned the injected message's role, so a switch from User to System would have inverted the posture with every test green.

Test plan

  • dotnet build --configuration Release: 0 warnings, 0 errors
  • dotnet test --configuration Release: 680 of 680 pass (MAF 86, Core 382, Postgres 125, Vectors 43). Locally with TESTCONTAINERS_RYUK_DISABLED=true.
  • One test per edge case through a real ChatClientAgent, plus the authorization test, a three-turn session test, cancellation, and a loop-closure test that captures and finalizes a run then receives its own lesson back.
  • CI green

🤖 Generated with Claude Code

fabbrik and others added 4 commits September 18, 2026 21:17
Add ExperienceFinalizationService: one call that loads a completed captured
run, evaluates its own closed verification round, checks host authorization and
the host's storage decision, reflects, creates the record as Candidate, and
commits its initial lifecycle event to Validated or Quarantined. Record,
reflection and event IDs derive from the run, so a retry re-derives them and
converges instead of duplicating.

Required checks can now name the evaluator kind that may satisfy them, and Core
and Storage.Postgres each expose a service-registration extension. The MAF
adapter finalizes a fully captured run through the host's resolver.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Add IExperienceCandidateSource with a PostgreSQL implementation: scope, status
and confidence filtering plus a full-text match over a generated tsvector
column added by migration 0003. Core's ExperienceRetrievalService applies
expiry and environment eligibility, then ranks candidates on relevance,
confidence, recency, status and environment compatibility with configurable
validated weights, exposing every normalized component and effective weight.

Retrieval is bounded by a timeout that returns an empty result with a timeout
signal rather than throwing, caller cancellation stays distinct, and a capped
candidate pool is reported through Truncated.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Add IExperienceEmbeddingIndex and a domain-typed embedding generator port,
with a new AgentExperience.Storage.Postgres.Vectors package implementing them
over plain Npgsql and Pgvector. Embeddings are derived data: indexing runs
after the canonical commit, embeds only the sanitized retrieval summary of an
eligible record, and writes conditionally on the record's exact revision, so a
stale write is rejected and a deleted record is never recreated.

Retrieval gains a vector channel merged with the text channel under the same
eligibility, timeout and ceiling. A model or dimension mismatch, an
unavailable provider, or a vector-channel failure produces an explicit
flagged text-only result rather than an incompatible comparison.

The vectors package owns and applies its own schema, so a text-only
deployment never runs CREATE EXTENSION vector.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Add ExperienceContextProvider, a simple-tier AIContextProvider the host adds
to ChatClientAgentOptions.AIContextProviders. It retrieves ranked experience
before an invocation, re-checks every candidate against the same eligibility
rules retrieval applies, asks the host's injection decision, and injects a
delimited Historical Reference carrying source, confidence, applicability and
an evidence summary -- never raw payload content, never a cut record.

Limits drop whole records and record every omission. Retrieval that is empty,
times out or fails leaves the agent running normally. Labeling marks the
content untrusted; the authorization boundary is what denies an unauthorized
tool call, and a test proves it does when the model obeys injected text.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@fabbrik
fabbrik merged commit 751a544 into main Sep 22, 2026
1 check passed
@fabbrik
fabbrik deleted the feat/2-3-inject-historical-reference branch September 22, 2026 21:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant