Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions docs/guide.org
Original file line number Diff line number Diff line change
Expand Up @@ -2335,17 +2335,15 @@ At narrow widths, the meter keeps the used and window counts and drops the bar.

Use =(compact)= to compact the current conversation immediately,
regardless of the meter. Immediate and automatic compaction use the same
path: a provider-native window where the provider has one, and a typed
checkpoint that every provider can read. During an active turn,
path: a typed checkpoint that every provider can read. During an active turn,
compaction waits in the idle queue. An empty conversation needs no provider
request. Compaction adds a durable checkpoint without adding a user message
or starting a model turn.

A native window and the checkpoint normally take two provider requests. Each
completed request is recorded with its usage before its output is checked,
including rejected submissions and native responses that fall back to the
portable path. A native window includes the full returned item window, in
provider order.
The checkpoint normally takes one provider request. Each completed request is
recorded with its usage before its output is checked, including rejected
submissions. A conversation that holds a provider-native window from an
earlier version still loads, and its provider keeps reading that window.

The model writes the checkpoint by calling =compaction.submit=. A compaction
request repeats the instructions, tools and history of the ordinary requests
Expand Down Expand Up @@ -2750,9 +2748,8 @@ and description words, then appends the =tool_search_output= as a durable
provider item.

The expansion replays intact in every later request. This keeps those tools
loaded and the prompt prefix cacheable. The expansion is replayed empty only
in native compaction requests. Other models and providers receive the complete
eager tool array.
loaded and the prompt prefix cacheable. Other models and providers receive the
complete eager tool array.

Authenticate Gemini with =autolith auth gemini= or
=(auth "gemini")=. Autolith stores its Google OAuth credentials separately at
Expand Down
137 changes: 24 additions & 113 deletions src/agent/runtime.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -1544,22 +1544,20 @@ tool registry reaches the very next provider request."

(-> agent--note-compaction-request
(agent agent-observer &key (:request-number (integer 1))
(:kind (member :native :summary))
(:usage t)
(:response-id (option string))
(:problem (option string)))
null)
(defun agent--note-compaction-request
(agent observer &key request-number kind usage response-id problem)
(agent observer &key request-number usage response-id problem)
"Persist and report one compaction request's USAGE under REQUEST-NUMBER.

The native checkpoint request re-reads the whole prompt and the summary
request generates output under the configured effort, so both stay visible
to usage accounting instead of hiding behind the compaction record. PROBLEM
says why the request gave no usable checkpoint."
The request re-reads the prompt and generates output under the configured
effort, so it stays visible to usage accounting instead of hiding behind the
compaction record. PROBLEM says why the request gave no usable checkpoint."
(let ((details
(append (list :request-number request-number
:compaction kind
:compaction ':summary
:response-id response-id
:usage (agent--portable-value
(provider-usage-normalize usage)))
Expand All @@ -1569,25 +1567,6 @@ says why the request gave no usable checkpoint."
(agent-observer-status observer :compaction-request-completed details))
nil)

(-> agent--native-compaction-window (t) list)
(defun agent--native-compaction-window (value)
"Normalize a native checkpoint VALUE to its complete ordered output window."
(cond
((null value)
nil)
((json-object-p value)
(list value))
((listp value)
value)
(t
(error 'provider-protocol-error
:message "Native compaction returned a malformed window."
:status nil
:code nil
:request-id nil
:response-id nil
:response nil))))

(-> agent--compaction-submission
(provider-result list list) (values list (option string)))
(defun agent--compaction-submission (result references unsummarized)
Expand Down Expand Up @@ -1719,11 +1698,10 @@ that maximum. A refusal and a terminal failure are signaled."
(agent--note-compaction-request
agent observer
:request-number (+ request-number issued)
:kind ':summary
:usage (provider-result-usage result)
:response-id (provider-result-response-id result)
:problem problem)
(when (some #'provider--native-compaction-refusal-p
(when (some #'provider--output-refusal-p
(provider-result-output-items result))
(error 'provider-protocol-error
:message "The provider refused to write the compaction checkpoint."
Expand All @@ -1740,7 +1718,6 @@ that maximum. A refusal and a terminal failure are signaled."
(agent--note-compaction-request
agent observer
:request-number (+ request-number issued)
:kind ':summary
:problem (princ-to-string result))))
finally (return (values nil issued)))))

Expand Down Expand Up @@ -1888,21 +1865,20 @@ again. A COMPACTION-P request declares compaction.submit in any case."
(agent observer &key tool-allowlist (tool-restriction-p nil) (tools-p t)
goal-context (request-number 0)
(next-request-status-details nil) (next-request-tool-rounds 0))
"Compact AGENT's conversation into a typed checkpoint, with native state when the provider supports it.
"Compact AGENT's conversation into a typed checkpoint.

The model submits the checkpoint over the readable captured history whether
or not a native window exists, so the checkpoint never rests on opaque
provider state and every provider family can read it. When no usable
submission arrives, the checkpoint keeps the previous one's notes and names
the records nobody summarized.
The model submits the checkpoint over the readable captured history, so every
provider family can read it. When no usable submission arrives, the checkpoint
keeps the previous one's notes and names the records nobody summarized.

Each completed provider request is metered before response validation. Capture
the durable cutoff before native or portable requests and retain arrivals at
publication. Return issued request count and optional next-request details."
the durable cutoff before the requests and retain arrivals at publication.
Return issued request count and optional next-request details."
(agent--check-request-budget agent request-number)
(agent--prepare-provider-request agent)
(let* ((conversation (agent-conversation agent))
(compaction (conversation-compaction-capture conversation))
(compaction (conversation-compaction-portable-capture
(conversation-compaction-capture conversation)))
(captured-view (conversation-compaction-view compaction))
(request-context (mission-request-view-context conversation))
(recovery
Expand All @@ -1912,70 +1888,16 @@ publication. Return issued request count and optional next-request details."
conversation (list :compaction-recovery details))
(agent-observer-status observer :compaction-recovery details))))
(unfinished-work nil)
(*request-context-hurry-up-p* (agent-hurry-up-p agent))
(issued 0)
(native-usage nil)
(native-request-accounted-p nil))
(*request-context-hurry-up-p* (agent-hurry-up-p agent)))
;; Announce compaction before reading job storage, which can take a while.
(agent-observer-status
observer
:compaction-started
(list :total-tokens (conversation-last-total-tokens conversation)))
(setf unfinished-work (task-unfinished-work-snapshot agent))
(let* ((*provider-hosted-tools-enabled-p* (not tool-restriction-p))
(provider (agent-provider agent))
(tool-namespaces
(agent--tool-namespaces agent
:tools-p tools-p
:tool-restriction-p tool-restriction-p
:tool-allowlist tool-allowlist))
(native-window
(multiple-value-bind (window usage)
(values-list
(compaction-recovery-call
recovery provider captured-view :dispatch
(lambda (candidate)
(agent--check-request-budget agent (+ request-number issued))
(multiple-value-list
(mission-call-with-native-compaction-view
request-context candidate
(lambda ()
(provider-native-compact-conversation
provider candidate :tool-namespaces tool-namespaces
:event-callback
(lambda (event)
(if (typep event 'native-compaction-completed-event)
(progn
(setf native-request-accounted-p t
native-usage (native-compaction-completed-event-usage event))
(incf issued)
(agent--note-compaction-request
agent observer :request-number (+ request-number issued)
:kind ':native :usage native-usage
:response-id (native-compaction-completed-event-response-id event)))
(agent-observer-status observer :provider-progress nil))))))))
:preflight (lambda (candidate)
(provider-check-request-context
provider (provider-native-compaction-request-object provider candidate tool-namespaces)))
:native-p t))
(unless native-request-accounted-p
(setf native-usage usage))
(agent--native-compaction-window window)))
(compaction (if native-window
compaction
(conversation-compaction-portable-capture compaction)))
(captured-view (conversation-compaction-view compaction))
(family (provider-family provider)))
;; Native providers that cannot emit the completion event still return
;; usage as their second value. Account that fallback exactly once.
(when (and (not native-request-accounted-p) (or native-window native-usage))
(incf issued)
(agent--note-compaction-request
agent observer
:request-number (+ request-number issued)
:kind ':native
:usage native-usage))
(multiple-value-bind (continuation requests)
(let ((*provider-hosted-tools-enabled-p* (not tool-restriction-p))
(family (provider-family (agent-provider agent))))
(multiple-value-bind (continuation issued)
(agent--compaction-checkpoint
agent observer compaction
:view (conversation-compaction-summary-view
Expand All @@ -1991,8 +1913,7 @@ publication. Return issued request count and optional next-request details."
:recovery recovery
:request-context request-context
:unfinished-work unfinished-work
:request-number (+ request-number issued))
(incf issued requests)
:request-number request-number)
(let* ((next-output-tokens
(if next-request-status-details
(let ((value
Expand Down Expand Up @@ -2023,22 +1944,12 @@ publication. Return issued request count and optional next-request details."
:tool-allowlist tool-allowlist)))))
(with-recursive-lock-held ((conversation-append-lock conversation))
(setf unfinished-work (task-unfinished-work-snapshot agent))
(if native-window
(apply #'conversation-append-native-compaction
conversation native-window
:family family
:continuation continuation
:unfinished-work unfinished-work
publication)
(apply #'conversation-append-summary
conversation continuation
:unfinished-work unfinished-work
publication)))
(apply #'conversation-append-summary
conversation continuation
:unfinished-work unfinished-work
publication))
(agent-observer-status
observer
:compaction-completed
(list :native-p (not (null native-window))
:provider-requests issued))
observer :compaction-completed (list :provider-requests issued))
(values issued next-request-details))))))

(-> agent--run-provider-loop
Expand Down
37 changes: 0 additions & 37 deletions src/application/mission.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -340,17 +340,6 @@ preserving the calling thread's provider bindings and authorization callbacks."
:usage-function (lambda (results) (provider-result-usage (first results))))
(call-next-method))))

(defmethod provider-native-compact-conversation :around
((provider model-provider) (conversation conversation)
&key tool-namespaces event-callback)
"Include native compaction inference and its separately reported usage."
(declare (ignore tool-namespaces event-callback))
(let ((context (mission--conversation-context conversation)))
(if (and context (mission--request-owned-p context conversation))
(mission-review--account context conversation
:function #'call-next-method :usage-function #'second)
(call-next-method))))

(-> mission--unknown-usage-blocks-p (list) boolean)
(defun mission--unknown-usage-blocks-p (goal)
"Require a new budget for unknown usage unless GOAL's unknown-usage policy is :record."
Expand Down Expand Up @@ -638,9 +627,6 @@ Report whether the guarded goal is current or replaced; omit arbitrary goal evid
(setf (gethash identity *mission-contexts*) context)))
nil)

(defparameter *mission-native-compaction-view* nil
"Exact detached native request view and owning context during compaction dispatch.")

(-> mission-call-with-request-view ((option mission-context) conversation function) t)
(defun mission-call-with-request-view (context view function)
"Bind a fresh VIEW to CONTEXT during FUNCTION without replacing an existing owner."
Expand All @@ -664,29 +650,6 @@ Report whether the guarded goal is current or replaced; omit arbitrary goal evid
(let ((context (mission--conversation-context conversation)))
(and context (mission--request-owned-p context conversation) context)))

(-> mission-call-with-native-compaction-view ((option mission-context) conversation function) t)
(defun mission-call-with-native-compaction-view (context view function)
"Capture VIEW's owner for actual native attempts, leaving unsupported probes unowned."
(let ((*mission-native-compaction-view* (cons view context)))
(funcall function)))

;; Specialize the native backend so the telemetry MODEL-PROVIDER wrapper composes.
(defmethod provider-attempt-native-compaction :around
((provider codex-subscription-provider) (conversation conversation)
&key tool-namespaces force-refresh event-callback)
"Account for detached native views when native compaction is attempted, not during unsupported capability probes."
(declare (ignore tool-namespaces force-refresh event-callback))
(let ((context (and (eq conversation (first *mission-native-compaction-view*))
(rest *mission-native-compaction-view*))))
;; Already bound conversations are accounted for by the outer native generic.
(if (and context (not (mission-context-find conversation)))
(mission-call-with-request-view
context conversation
(lambda ()
(mission-review--account context conversation
:function #'call-next-method :usage-function #'second)))
(call-next-method))))

(-> mission--agent-context (agent) (option mission-context))
(defun mission--agent-context (agent)
"Return the exact admitted mission, including an explicitly captured absence."
Expand Down
12 changes: 4 additions & 8 deletions src/conversation/compaction-recovery.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,16 @@ as media; image payloads, URLs and incidental metadata are not copied."

(-> compaction-recovery-call
(compaction-recovery model-provider conversation
&key (:dispatch function) (:preflight function) (:native-p boolean))
&key (:dispatch function) (:preflight function))
(values t conversation))
(defun compaction-recovery-call (state provider source &key dispatch preflight native-p)
(defun compaction-recovery-call (state provider source &key dispatch preflight)
"Recover only a local compaction admission refusal, sharing all tier state.

DISPATCH and PREFLIGHT receive the exact detached candidate. Both run against
the window PROVIDER's model takes when that is larger than the configured one,
so a compaction loses history only when the model itself cannot read it.
Unrelated errors abort immediately. Native exhaustion permits portable
fallback; portable exhaustion preserves the original conversation and signals
the final refusal."
Unrelated errors abort immediately. Exhaustion preserves the original
conversation and signals the final refusal."
(let ((view (compaction-recovery-view state source))
(failure nil)
(*compaction-provider* provider))
Expand Down Expand Up @@ -283,9 +282,6 @@ the final refusal."
(multiple-value-bind (result success-p) (try-fitting-candidate)
(when success-p
(return-from compaction-recovery-call (values result view))))))
(when native-p
(compaction-recovery-note state :portable-fallback)
(return-from compaction-recovery-call (values nil view)))
(when (< (compaction-recovery-tier state) 3)
(setf (compaction-recovery-tier state) 3
view (compaction-recovery-text-view state view))
Expand Down
Loading
Loading