Repository navigation
Feat: Give ibac and sparc the session facts from the store - #1708
Conversation
… store The store now records the Seq of the newest user intent and the newest outbound inference request of each session when it appends an event. An event of neither kind does not change them. Store.ViewFacts returns the view and the two facts under one read lock, so they agree. Each present fact points into the returned view. A fact has 3 states: FactNoSession, FactNone and FactPresent. ViewFacts gives FactNone when the store has no entry, when no event of the kind was appended, and when a session.max_events trim removed the event. The types are in core/model, with aliases in core/pipeline. The intent pin of the trim and the LastIntent fact use one rule, model.IsIntent. Part of rossoctl#1499. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
Context.LastIntent and Context.LastInference give a plugin the newest user intent and the newest outbound inference request of its session. A listener sets the store's facts with SetSessionFacts. The rules, in order: 1. The listener set the facts: return them. 2. Session is nil: FactNoSession. 3. Else read Session.Events: FactPresent or FactNone. Rule 3 keeps the answers for a listener outside this repo that sets only Session, and for the plugin tests that build a view. Part of rossoctl#1499. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
LastIntent and LastInference now give FactNoSession when Session is nil, also when the listener set facts. Before, a nil view from Store.ViewFacts came with FactNone facts, so ibac would read no_intent in place of no_session. SessionView.LastIntent, Intents and InferenceRequests now call model.IsIntent and model.IsInferenceRequest. The conditions are the same as before, so the view, the trim pin and the facts use one rule. Part of rossoctl#1499. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
The forward proxy and the ext_proc server now read the view and the session facts with Store.ViewFacts, under one lock, and set the facts on the pipeline context with SetSessionFacts. - forwardproxy: sessionViewFor returns the view and its facts. The empty view that it makes for a session with no entry comes with FactNone. hydrateSession and each WebSocket turn set the facts. - extproc: both outbound entry points set the facts only with a view, so a request with no view stays FactNoSession. The guard test for the hydration path also finds a two-value assignment to pctx.Session. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
ibac reads pctx.LastIntent(). FactNoSession takes the no_session path. FactNone, or an intent with no user text, takes the no_intent path. The decisions and their sub_reason values do not change. sparc reads pctx.LastInference(). With no present fact, it has no context and skips, as before. The new tests set FactNone with SetSessionFacts on a view that holds the event. They show that the facts from the listener win over the view. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
FactNoSession comes only from a nil Context.Session (rule 1). No code
stores it. So a zero SessionFacts on a context with a session now reads
FactNone ("no intent"), not FactNoSession ("no session").
Nothing persists or prints the numeric value.
Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>
- plugin-reference.md: a new "Session facts" section. It gives the 3 states, the ordered rules of LastIntent and LastInference, and the listener rule: set Session and the facts from one ViewFacts call. - ibac-plugin.md: ibac reads pctx.LastIntent(). FactNoSession gives no_session. FactNone, or an intent with no user text, gives no_intent. - CLAUDE.md: the intent-pin gotcha names pctx.LastIntent() and a restart. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
The step said that no_intent_policy "deny" gives deny/no_intent for both cases. A request with no session gets deny/no_session (403 ibac.no_session). Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 8 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
clawgenti
left a comment
There was a problem hiding this comment.
Summary
This is a clean refactor with no behaviour change. The store tracks the newest intent and inference-request Seq in appendLocked, which every append path goes through (Append, AppendBucket, AppendTrailing, AppendPair*). ViewFacts resolves those Seqs against the copied view under one RLock. factAt uses sort.Search, and that holds because Seq rises within an entry, the pinned intent stays oldest after a trim, and a SeqSeeder seed keeps 0 free as the "none" value. I traced each hydration site: extproc (both phases), forward proxy hydrateSession (HTTPS, tunnel, transparent) and the WebSocket turn context. All of them now pair pctx.Session with SetSessionFacts. I found no other writer of pctx.Session outside tests. ibac's no_session / no_intent split and its sub_reasons are unchanged: a nil view still gives FactNoSession, and the forward proxy's synthesized empty view still gives FactNone → no_intent. sparc's correlateInferenceContext gives the same answer as InferenceRequests()[last].
I verified locally on d4b189a: go vet and go test (GOWORK=off) pass for session, pipeline, model, plugins/ibac, plugins/sparc, listener/extproc and listener/forwardproxy. The property test and the drop/rekey tests pass too. My -race run of forwardproxy was OOM-killed in this sandbox, which is an environment limit. The other packages passed under -race, and CI is green.
Non-blocking:
demos/ibac/agent/main.golines 8 and 563 still say IBAC readspctx.Session.LastIntent(). The PR updates every other such reference (CLAUDE.md,identity.go, both docs). This file is outside the diff, so I can't comment on it inline.- One inline nit on
Context.LastInference.
No new JSON-decoded fields, so no decoder-strictness probe was needed. SessionFact and SessionFacts carry no JSON tags and are never serialized.
Author: huang195 (MEMBER — maintainer)
Areas reviewed: Go (session store, pipeline context, model, ibac, sparc, extproc and forward-proxy listeners), Docs
Agent/IDE config (.claude/.vscode): none. The root CLAUDE.md changes one line of docs text.
Commits: 8 commits, all signed-off: yes
CI status: passing
| if c.factsSet { | ||
| return c.facts.LastInference | ||
| } | ||
| for i := len(c.Session.Events) - 1; i >= 0; i-- { |
There was a problem hiding this comment.
nit: Rule 3 here hand-rolls the newest-inference-request scan, and core/session/facts_test.go has a second copy of it (lastInferenceIn). A SessionView.LastInferenceRequest() beside LastIntent() in core/model/session.go would give both one implementation. This branch would then read the same as LastIntent's: return factOf(c.Session.LastInferenceRequest()). The router-fact follow-up may want it too.
#1708, the PR this branch was stacked on, is merged. Bring the branch up to date with main. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
Part of #1499. Phase 2 of the Praxis prep (#1437).
What this changes
Store.ViewFacts(id)returns the view and the facts under one read lock, so the two describe the same instant.Store.Viewdoes not change.pipeline.ContextgetsLastIntent()andLastInference(), read-only. Listeners set them withSetSessionFacts, from the sameViewFactscall that fillspctx.Session. A fact has 3 states:FactNoSession: the request has no session (pctx.Sessionis nil).FactNone: the session has no such event. It has no entry yet, the entry was dropped, no event of that kind was appended, orsession.max_eventstrimmed it. This is the zero value.FactPresent:Eventpoints intopctx.Session.pctx.Session.Events. ibac keeps "no session" apart from "no intent": theno_sessionandno_intentdecisions and everysub_reasonstay the same.SessionViewaccessors usemodel.IsIntentandmodel.IsInferenceRequest.docs/plugin-reference.md, anddocs/ibac-plugin.md.The facts serve a class of plugins: intent-aware guardrails (ibac), and plugins that judge an action against the agent's newest LLM context (sparc).
pctx.Sessionstays filled as today. The inference router still readspctx.Session. It moves to a fact in a follow-up PR.Behaviour
No change. The existing ibac, sparc, session and pipeline tests pass with no edit. No golden changes, and nothing on the wire changes.
Checks run
max_eventsin {0, 1, 2, 3, 7}. After each append, each fact is the same pointer as the view's own accessor.max_sessions,Delete,Clear,ttl), forRekey, and for "an event of neither kind";Sessionnil →FactNoSession" and "zero facts with a session →FactNone";go vet,go test,gofmt -landgo mod tidy -diffwithGOWORK=offin all 8 workspace modules, and vet in the 3 demos modules.-raceon the listener, session, pipeline, model, ibac and sparc packages.cmd/cortexbuilds with thelocal,fullandliteprofiles.cmd/cortex-envoyvet, build and test withenvoy.corevet with thefullandcpextags.go doc -allofcore/pipelineandcore/session: additions only.pre-commit runon every changed file.Deferred review nits
demos/ibac/agent/main.golines 8 and 563 still say that IBAC readspctx.Session.LastIntent(). It now readspctx.LastIntent().Context.LastInferencerule 3 andsession/facts_test.goeach scan for the newest inference request. ASessionView.LastInferenceRequest()would give them one implementation.SetSessionFacts. Today the view gives the same answer, so a plugin cannot see the difference. The router-fact PR adds a fact that the view cannot give, and it adds those tests.Assisted-By: Claude (Anthropic AI) [email protected]