From 733dcbd854af5d52ebdb17e25a39cf38013d4e87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Hozda?= Date: Mon, 16 Nov 2026 00:17:16 +0100 Subject: [PATCH 1/2] Remove the native compaction request --- src/agent/runtime.lisp | 137 +- src/application/mission.lisp | 37 - src/conversation/compaction-recovery.lisp | 12 +- src/conversation/compaction.lisp | 43 +- src/core/package.lisp | 1 - src/provider/client.lisp | 319 +---- src/provider/context-admission.lisp | 18 - src/provider/wire-protocol.lisp | 7 +- src/telemetry/hooks.lisp | 45 +- tests/agent-tests.lisp | 115 +- tests/application-operation-tests.lisp | 109 +- tests/compaction-acceptance-tests.lisp | 110 +- tests/compaction-recovery-tests.lisp | 1387 +++++++------------ tests/compaction-unfinished-work-tests.lisp | 564 ++++---- tests/context-admission-tests.lisp | 368 +++-- tests/context-tests.lisp | 14 +- tests/conversation-tests.lisp | 8 +- tests/mission-tests.lisp | 25 - tests/native-compaction-window-tests.lisp | 85 +- tests/precompaction-tests.lisp | 106 +- tests/provider-tests.lisp | 338 +---- tests/telemetry-hook-model-tests.lisp | 14 +- tests/telemetry-hook-provider-tests.lisp | 28 +- tests/test-suites.lisp | 56 +- tests/test-support.lisp | 16 + 25 files changed, 1206 insertions(+), 2756 deletions(-) diff --git a/src/agent/runtime.lisp b/src/agent/runtime.lisp index 816a03abc..dc4a5e7e6 100644 --- a/src/agent/runtime.lisp +++ b/src/agent/runtime.lisp @@ -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))) @@ -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) @@ -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." @@ -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))))) @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/application/mission.lisp b/src/application/mission.lisp index dc6dfb2f9..4fe136a3f 100644 --- a/src/application/mission.lisp +++ b/src/application/mission.lisp @@ -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." @@ -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." @@ -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." diff --git a/src/conversation/compaction-recovery.lisp b/src/conversation/compaction-recovery.lisp index 2bfc920e2..ac448d1a9 100644 --- a/src/conversation/compaction-recovery.lisp +++ b/src/conversation/compaction-recovery.lisp @@ -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)) @@ -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)) diff --git a/src/conversation/compaction.lisp b/src/conversation/compaction.lisp index f0f97178c..b24b89c3a 100644 --- a/src/conversation/compaction.lisp +++ b/src/conversation/compaction.lisp @@ -186,13 +186,6 @@ boundary, including arrivals encoded in CARRY-FORWARD, as required by chunks." (conversation-ephemeral-input-entries conversation)))) (conversation-compaction--copy-metadata (conversation-projection conversation) projection items) - ;; Only the native window belongs to one provider family. Every - ;; family reads the checkpoint that follows it. - (when (eq (first record) :native-compaction) - (dolist (item (butlast replacement-items)) - (setf (gethash item - (clinker-transcript:projection-metadata-table projection ':family)) - (getf (rest record) :family)))) (conversation-compaction--mark-checkpoint projection (first (last replacement-items)) (getf (rest record) :continuation)) (setf (getf (rest record) :through-seq) @@ -239,37 +232,6 @@ readers that do not know the typed form." :unfinished-work unfinished-work :validate-projection validate-projection))) -(-> conversation-append-native-compaction - (conversation (or list json-object) &key (:family keyword) (:continuation list) - (:compaction (option conversation-compaction)) - (:unfinished-work list) (:validate-projection (option function)) (:recovery list)) - list) -(defun conversation-append-native-compaction - (conversation window &key family continuation compaction unfinished-work validate-projection recovery) - "Persist native WINDOW, CONTINUATION, and carry-forward state atomically." - (when (json-object-p window) - (setf window (list window))) - (dolist (item window) - (native-compaction-item-canonicalize item)) - (unless (and (keywordp family) (listp window) (not (null window)) - (every #'json-object-p window) - (some #'native-compaction-item-p window)) - (error 'conversation-invariant-error - :message "A native compaction window is invalid." - :pathname (conversation-pathname conversation) - :sequence (conversation-next-sequence conversation))) - (let ((content (continuation-render continuation))) - (conversation-compaction--publish - conversation - :compaction (or compaction (conversation-compaction-capture conversation)) - :replacement-items (append window (list (conversation-summary-item content))) - :record (list :native-compaction :family family - :wire-json (json-encode (coerce window 'vector)) - :summary content :continuation continuation - :recovery recovery) - :unfinished-work unfinished-work - :validate-projection validate-projection))) - ;;;; -- Checkpoint Replay -- @@ -413,8 +375,9 @@ after the cutoff, is left out." ((kind (eql :native-compaction)) conversation properties) "Restore native family privacy, the checkpoint, and carry-forward context. -A record written before checkpoints were typed holds a text handoff, which -the family that owns the native window does not read." +No request produces a native window any more: this reads the ones earlier +versions stored. A record written before checkpoints were typed holds a text +handoff, which the family that owns the native window does not read." (let ((family (getf properties :family)) (wire-json (getf properties :wire-json)) (summary (getf properties :summary))) diff --git a/src/core/package.lisp b/src/core/package.lisp index 7ba8933ae..3611df6ea 100644 --- a/src/core/package.lisp +++ b/src/core/package.lisp @@ -406,7 +406,6 @@ #:provider-family #:provider-item-event #:provider-item-event-item - #:provider-native-compact-conversation #:provider-normalize-output-item #:provider-output-ceiling-p #:provider-progress-event diff --git a/src/provider/client.lisp b/src/provider/client.lisp index c6238e4dc..b2d0a1f59 100644 --- a/src/provider/client.lisp +++ b/src/provider/client.lisp @@ -385,11 +385,6 @@ credentials and model metadata independently of their cached wire routes." (provider request &key credentials conversation) (:documentation "Open an authenticated provider stream and return body, status, and headers.")) -(defgeneric provider-open-native-compaction - (provider request &key credentials conversation) - (:documentation - "POST REQUEST to PROVIDER's native compaction endpoint and return its body.")) - (defparameter *compaction-instructions* (format nil "Stop working on the task. You are now writing the checkpoint that replaces this conversation so far. Call compaction.submit once and write nothing else. Do not call other tools, wait for jobs, or do more work. @@ -484,51 +479,6 @@ root's key, as the Codex reference does at commit 6f51c65958." (when (configuration-codex-fast-mode-active-p configuration) (list "service_tier" "priority"))))) -(defparameter *provider-history-trimming-p* nil - "True while history is projected for a compaction request. - -Consumed tool search expansions are replayed empty there, as the Codex -reference does when trimming, and intact everywhere else so the server keeps -the expanded tools loaded without another search.") - -(-> provider-native-compaction-request-object - (codex-subscription-provider conversation vector) - json-object) -(defun provider-native-compaction-request-object - (provider conversation tool-namespaces) - "Build a standard Responses compaction request for CONVERSATION. - -Durable family-compatible history and top-level instructions participate in the -native checkpoint. Request-local contributions and pending one-response items -stay outside it, and consumed tool search expansions replay empty because the -checkpoint summarizes history rather than continuing it." - (declare (ignore tool-namespaces)) - (let* ((configuration (provider-configuration provider)) - (instructions - (responses-standard-instructions - (list - (let ((*system-prompt-hosted-web-search-p* nil)) - (system-prompt configuration))))) - (input - (let ((*provider-history-trimming-p* t)) - (map 'vector - (lambda (item) - (provider-wire-input-item provider item)) - (conversation-input-items-for-family - conversation - (provider-family provider) - :include-ephemeral-p nil))))) - (apply - #'json-object - (append - (list - "model" (config :model configuration) - "instructions" instructions - "input" input - "prompt_cache_key" (provider--codex-prompt-cache-key provider conversation)) - (when (configuration-codex-fast-mode-active-p configuration) - (list "service_tier" "priority")))))) - (-> provider--codex-request-headers (codex-subscription-provider oauth-credentials conversation &key (:accept string)) @@ -565,17 +515,6 @@ grew to 11392 of 11491 without it." (cons "thread-id" (conversation-identifier conversation)) (cons "x-client-request-id" (conversation-identifier conversation)))) -(-> provider--native-compaction-endpoint (codex-subscription-provider) string) -(defun provider--native-compaction-endpoint (provider) - "Return the native compaction endpoint corresponding to PROVIDER's endpoint." - (let ((endpoint - (string-right-trim - '(#\/) - (config :provider-endpoint (provider-configuration provider))))) - (if (uiop:string-suffix-p "/responses/compact" endpoint) - endpoint - (format nil "~A/compact" endpoint)))) - (defmethod provider-open-response-stream ((provider codex-subscription-provider) (request hash-table) @@ -598,30 +537,6 @@ the requests of a turn for the same reason, at commit 6f51c65958." provider credentials conversation :accept "text/event-stream") :keep-alive-p t))) -(defmethod provider-open-native-compaction - ((provider codex-subscription-provider) - (request hash-table) - &key credentials conversation) - "POST a JSON native compaction REQUEST to the ChatGPT Codex endpoint. - -The pooled connection is shared with the streaming requests so a compaction -reaches the backend holding the conversation's cached prefix." - (declare (type oauth-credentials credentials) - (type conversation conversation)) - (provider-call-with-response-deadline - 300 - (lambda () - (dexador:post - (provider--native-compaction-endpoint provider) - :headers (provider--codex-request-headers - provider credentials conversation :accept "application/json") - :content (json-encode-utf8 request) - :force-string t - :keep-alive t - :use-connection-pool t - :connect-timeout 30 - :read-timeout 300)))) - ;;;; -- Usage Limit Failures -- @@ -870,19 +785,6 @@ and codes follow the Codex reference at commit 6f51c65958." :conversation conversation)) :terminal-errors-p t)) -(-> provider--open-native-compaction - (codex-subscription-provider json-object - &key (:credentials oauth-credentials) (:conversation conversation)) - (values string integer t)) -(defun provider--open-native-compaction - (provider request &key credentials conversation) - "Open an authenticated product request through portable transport normalization." - (provider--call-with-transport-normalization - (lambda () - (provider-open-native-compaction provider request :credentials credentials - :conversation conversation)) - :terminal-errors-p t)) - (define-condition provider-result-rejected (provider-retryable-error) ((result :initarg :result :reader provider-result-rejected-result :type provider-result @@ -1087,231 +989,20 @@ original condition or weakening the streaming safety limit." event-callback)) -;;;; -- Native Responses Compaction -- +;;;; -- Refused Output -- -(-> provider--signal-invalid-native-compaction - (codex-subscription-provider integer t) - null) -(defun provider--signal-invalid-native-compaction (provider status headers) - "Signal that Codex returned an unusable successful compaction response." - (declare (ignore provider)) - (error 'provider-error - :message "The provider returned an invalid native compaction response." - :status status - :request-id (provider--response-request-id headers) - :response nil)) - -(defclass native-compaction-completed-event (provider-event) - ((usage - :initarg :usage - :reader native-compaction-completed-event-usage - :documentation "Reported paid usage, or NIL when the provider omitted it.") - (response-id - :initarg :response-id - :reader native-compaction-completed-event-response-id - :documentation "The response identifier when one was reported.")) - (:documentation "A completion notification emitted before native output validation.")) - -(-> provider--native-compaction-refusal-p (t) boolean) -(defun provider--native-compaction-refusal-p (value) +(-> provider--output-refusal-p (t) boolean) +(defun provider--output-refusal-p (value) "Recognize refusal items, including refusals nested inside message content." (typecase value (hash-table (or (equal (json-get value "type") "refusal") (non-empty-string-p (json-get value "refusal")) (loop for child being the hash-values of value - thereis (provider--native-compaction-refusal-p child)))) + thereis (provider--output-refusal-p child)))) (string nil) (vector - (some #'provider--native-compaction-refusal-p value)) + (some #'provider--output-refusal-p value)) (t nil))) - -(-> provider--native-compaction-item-valid-p (json-object) boolean) -(defun provider--native-compaction-item-valid-p (item) - "Check standard item fields while preserving unfamiliar typed protocol extensions." - (let ((type (json-get item "type")) - (status (json-get item "status"))) - (and (non-empty-string-p type) - (or (null status) (equal status "completed")) - (not (provider--native-compaction-refusal-p item)) - (cond - ((equal type "message") - (and (member (json-get item "role") '("system" "developer" "user" "assistant") - :test #'equal) - (let ((content (json-get item "content"))) - (and (vectorp content) (not (stringp content)) - (every (lambda (part) - (and (json-object-p part) - (non-empty-string-p (json-get part "type")) - (or (not (member (json-get part "type") - '("input_text" "output_text" "text") - :test #'equal)) - (stringp (json-get part "text"))))) - content))) - t)) - ((equal type "function_call") - (and (non-empty-string-p (json-get item "call_id")) - (non-empty-string-p (json-get item "name")) - (stringp (json-get item "arguments")))) - ((equal type "function_call_output") - (and (non-empty-string-p (json-get item "call_id")) - (nth-value 1 (gethash "output" item)))) - ((equal type "reasoning") - (let ((summary (json-get item "summary"))) - (or (null summary) (and (vectorp summary) (not (stringp summary)))))) - ((native-compaction-item-p item) - (non-empty-string-p (json-get item "encrypted_content"))) - (t - t))))) - -(-> provider--native-compaction-window-valid-p (list) boolean) -(defun provider--native-compaction-window-valid-p (items) - "Reject malformed items, orphan results, duplicate correlations and missing results." - (handler-case - (and (every #'provider--native-compaction-item-valid-p items) - (every (lambda (item) - (or (not (clinker-transcript:function-call-output-item-p item)) - (find-if (lambda (call) - (and (clinker-transcript:function-call-item-p call) - (equal (json-get call "call_id") - (json-get item "call_id")))) - items))) - items) - (null (clinker-transcript:reconciliation-repairs - (clinker-transcript:reconcile-items items)))) - (clinker-transcript:projection-error () nil))) - -(-> provider--decode-native-compaction-response - (codex-subscription-provider t &key (:status integer) (:headers t) - (:event-callback (option function))) - (values (option list) (option json-object))) -(defun provider--decode-native-compaction-response - (provider body &key status headers event-callback) - "Decode BODY and return its complete normalized compacted output window. - -Emit one completion event before response validation, including when the HTTP -success body cannot be decoded. Unknown usage and response IDs remain NIL." - (let* ((source (provider--error-body-text body)) - (response - (and (non-empty-string-p source) - (handler-case (json-decode source) - (error () nil)))) - (usage (and (json-object-p response) (json-get response "usage"))) - (response-id - (and (json-object-p response) - (or (json-get response "id") (json-get response "response_id"))))) - (when (functionp event-callback) - (funcall event-callback - (make-instance 'native-compaction-completed-event - :usage (and (json-object-p usage) usage) - :response-id (and (stringp response-id) response-id)))) - (unless (json-object-p response) - (provider--signal-invalid-native-compaction provider status headers)) - (let ((output (json-get response "output"))) - (unless (and (vectorp output) (every #'json-object-p output)) - (provider--signal-invalid-native-compaction provider status headers)) - (handler-case - (let ((items - (map 'list - (lambda (item) - (let ((normalized - (provider-normalize-output-item provider item))) - (when (native-compaction-item-p normalized) - (native-compaction-item-canonicalize normalized)) - normalized)) - output))) - (unless (and (every (lambda (item) - (and (json-object-p item) - (non-empty-string-p (json-get item "type")))) - items) - (provider--native-compaction-window-valid-p items)) - (provider--signal-invalid-native-compaction provider status headers)) - (values (and (some #'native-compaction-item-p items) items) - (and (json-object-p usage) usage))) - (error () - (provider--signal-invalid-native-compaction provider status headers)))))) - -(defgeneric provider-attempt-native-compaction - (provider conversation &key tool-namespaces force-refresh event-callback) - (:documentation - "Perform one authenticated Codex native compaction request. - - Return the complete normalized compacted window and the response usage as - two values. The first value is NIL when the response has no usable opaque - checkpoint, while the usage value is still returned.")) - -(defmethod provider-attempt-native-compaction - ((provider codex-subscription-provider) - (conversation conversation) - &key tool-namespaces force-refresh event-callback) - "Perform one native compaction attempt with optional credential refresh." - (declare (type vector tool-namespaces) - (type boolean force-refresh)) - (with-credentials (credentials (provider-credential-manager provider) - :force-refresh force-refresh) - (let* ((*provider-active-credential-values* - (oauth-credentials-secret-values credentials)) - (*provider-active-credential-redaction-marker* - (safe-redaction-marker - *provider-credential-redaction-marker* - *provider-active-credential-values*))) - (handler-case - (handler-case - (let ((request - (provider-native-compaction-request-object - provider conversation tool-namespaces))) - (multiple-value-bind (body status raw-headers) - (provider--open-native-compaction - provider request :credentials credentials :conversation conversation) - (let ((headers (provider--sanitize-wire-value raw-headers))) - (provider-note-response-headers provider headers) - (unless (= status 200) - (provider--codex-signal-status-failure - provider status :headers headers :raw-body body)) - (provider--decode-native-compaction-response - provider body :status status :headers headers - :event-callback event-callback)))) - ;; Normalized Dexador conditions must reach the attempt's classifier. - (dexador.error:http-request-unauthorized (condition) - (provider-signal-http-failure provider condition)) - (http-request-failed (condition) - (provider-signal-transport-failure provider condition))) - (provider-error (condition) - ;; Classify inside the attempt, before mission supervision wraps failures. - (if (provider--native-compaction-unavailable-p condition) - (values nil (json-object "input_tokens" 0 "output_tokens" 0)) - (error condition))))))) - -(-> provider--native-compaction-unavailable-p (provider-error) boolean) -(defun provider--native-compaction-unavailable-p (condition) - "Return true when CONDITION means this Codex endpoint is not available." - (let ((status (provider-error-status condition))) - (and (integerp status) - (not (null (member status '(404 405 501) :test #'=)))))) - -(defmethod provider-native-compact-conversation - ((provider codex-subscription-provider) - (conversation conversation) - &key tool-namespaces event-callback) - "Compact CONVERSATION through the standard Responses compact endpoint. - - Return the complete compacted window and the request's usage as two values, - or NIL when this endpoint is unavailable." - (declare (type vector tool-namespaces) - (type function event-callback)) - (handler-case - (provider--call-with-bounded-retries - provider - (lambda (force-refresh attempt-callback) - (provider-attempt-native-compaction - provider conversation - :tool-namespaces tool-namespaces - :force-refresh force-refresh - :event-callback attempt-callback)) - event-callback) - (provider-error (condition) - (if (provider--native-compaction-unavailable-p condition) - nil - (error condition))))) diff --git a/src/provider/context-admission.lisp b/src/provider/context-admission.lisp index 6171e4f3c..15af03601 100644 --- a/src/provider/context-admission.lisp +++ b/src/provider/context-admission.lisp @@ -433,15 +433,6 @@ messages that later change fail closed." (setf (first *provider-context-admission-state*) :entered (rest *provider-context-admission-state*) nil))) -(defmethod provider-open-native-compaction :before - ((provider model-provider) (request hash-table) &key credentials conversation) - "Check native compaction requests against the context budget." - (declare (ignore credentials conversation)) - (provider-context--admit-request provider request) - (when *provider-context-admission-state* - (setf (first *provider-context-admission-state*) :entered - (rest *provider-context-admission-state*) nil))) - (define-condition provider-context-budget-error (autolith-error) ((estimated-input-tokens :initarg :estimated-input-tokens @@ -508,12 +499,3 @@ encoded request." ;; No positive output envelope exists for this encoded candidate. (let ((*provider-maximum-output-tokens* ceiling)) (provider-context--admit-request provider candidate))))))) - -(defmethod provider-open-native-compaction :around - ((provider model-provider) (request hash-table) &key credentials conversation) - "Finalize mission output headroom before native compaction admission." - (provider-call-with-mission-output-allowance - provider request - (lambda (final-request) - (call-next-method provider final-request :credentials credentials - :conversation conversation)))) diff --git a/src/provider/wire-protocol.lisp b/src/provider/wire-protocol.lisp index 677c2ec03..db0381034 100644 --- a/src/provider/wire-protocol.lisp +++ b/src/provider/wire-protocol.lisp @@ -552,14 +552,11 @@ same way, so ordinary and compaction requests share one cached prefix." The server remembers which deferred tools are loaded only through the tool_search_output items in the request history, so an expansion must replay with its tools on every later request of the conversation; replaying -it empty made the model search the same namespace again on every round. The -Codex reference at commit 18194bfd3534ca567d886eac454028dafaa68b6c empties the -tools only when trimming history for compaction, which -*provider-history-trimming-p* marks here." +it empty made the model search the same namespace again on every round." (cond ((and (json-object-p item) (json-string= (json-get item "type") "tool_search_output")) - (provider-tool-search-output-replay item :trimming-p *provider-history-trimming-p*)) + (provider-tool-search-output-replay item)) ((and (provider-deferred-tool-loading-p provider) (json-object-p item) (function-call-item-p item) diff --git a/src/telemetry/hooks.lisp b/src/telemetry/hooks.lisp index a4d322cc4..1e155a9b6 100644 --- a/src/telemetry/hooks.lisp +++ b/src/telemetry/hooks.lisp @@ -100,9 +100,8 @@ (t ':internal))) -(-> telemetry--provider-attempt - (model-provider function &key (:native-compaction-p boolean) (:fallback-p boolean)) t) -(defun telemetry--provider-attempt (provider function &key native-compaction-p fallback-p) +(-> telemetry--provider-attempt (model-provider function &key (:fallback-p boolean)) t) +(defun telemetry--provider-attempt (provider function &key fallback-p) "Record terminal outcomes of real attempts, preserving values and nonlocal exits." (when (and (not fallback-p) *telemetry-provider-attempt-count*) (incf *telemetry-provider-attempt-count*)) @@ -123,8 +122,7 @@ (values-list results)) (when (or (not fallback-p) (zerop *telemetry-provider-attempt-count*)) (telemetry--note-provider-result - provider start results :native-compaction-p native-compaction-p - :outcome outcome :error-type error-type))))) + provider start results :outcome outcome :error-type error-type))))) (defmethod provider-attempt-turn :around ((provider model-provider) (conversation conversation) @@ -133,16 +131,8 @@ (declare (ignore conversation arguments)) (telemetry--provider-attempt provider (lambda () (call-next-method)))) -(defmethod provider-attempt-native-compaction :around - ((provider model-provider) (conversation conversation) - &rest arguments &key &allow-other-keys) - "Measure native compaction attempts without exporting checkpoint material." - (declare (ignore conversation arguments)) - (telemetry--provider-attempt provider (lambda () (call-next-method)) - :native-compaction-p t)) - -;; Mission admission specializes CONVERSATION on its class. Use T here so these -;; wrappers compose with that accounting method instead of replacing it. +;; Mission admission specializes CONVERSATION on its class. Use T here so this +;; wrapper composes with that accounting method instead of replacing it. (defmethod provider-stream-turn :around ((provider model-provider) (conversation t) &rest arguments &key &allow-other-keys) @@ -154,22 +144,9 @@ (let ((*telemetry-provider-attempt-count* 0)) (telemetry--provider-attempt provider (lambda () (call-next-method)) :fallback-p t))))) -(defmethod provider-native-compact-conversation :around - ((provider model-provider) (conversation t) - &rest arguments &key &allow-other-keys) - "Cover native compaction outside agent turns, preserving all returned values." - (declare (ignore conversation arguments)) - (telemetry--call-with-provider-run - provider - (lambda () - (let ((*telemetry-provider-attempt-count* 0)) - (telemetry--provider-attempt provider (lambda () (call-next-method)) - :native-compaction-p t :fallback-p t))))) - (-> telemetry--note-provider-result - (model-provider integer list &key (:native-compaction-p boolean) - (:outcome keyword) (:error-type (option keyword))) null) -(defun telemetry--note-provider-result (provider start results &key native-compaction-p outcome error-type) + (model-provider integer list &key (:outcome keyword) (:error-type (option keyword))) null) +(defun telemetry--note-provider-result (provider start results &key outcome error-type) "Emit a usage-only event for an attempt, even if it failed before returning." (when *telemetry-run* (telemetry--call-safely @@ -178,10 +155,8 @@ (metadata (or *telemetry-response-metadata* (when (typep result 'provider-result) (provider-result-metadata result)))) - (usage (if native-compaction-p - (second results) - (when (typep result 'provider-result) - (provider-result-usage result)))) + (usage (when (typep result 'provider-result) + (provider-result-usage result))) (normalized (when usage (provider-usage-normalize usage))) (writes (if metadata @@ -202,7 +177,7 @@ (config :model (provider-configuration provider)))) :response-model (when metadata (provider-response-metadata-model metadata)) :outcome outcome :error-type error-type - :role (if native-compaction-p ':compaction *telemetry-request-role*) + :role *telemetry-request-role* :finish-reason (or (when metadata (provider-response-metadata-finish-reason metadata)) (when (typep result 'provider-result) (if (provider-result-tool-calls result) ':tool-calls diff --git a/tests/agent-tests.lisp b/tests/agent-tests.lisp index e72728bb1..9303db21d 100644 --- a/tests/agent-tests.lisp +++ b/tests/agent-tests.lisp @@ -56,35 +56,14 @@ :documentation "The compaction flag supplied on each request.")) (:documentation "A deterministic provider for exercising repeated agent rounds.")) -(defclass native-scripted-provider (scripted-provider) - ((native-items - :initarg :native-items - :accessor native-scripted-provider-native-items - :type list - :documentation "Opaque native checkpoints returned in request order.") - (native-input-snapshots - :initform nil - :accessor native-scripted-provider-native-input-snapshots - :type list - :documentation "Durable projections observed by native compaction requests.")) - (:documentation "A scripted provider that supports Codex-style native compaction.")) +(defclass codex-scripted-provider (scripted-provider) () + (:documentation "A scripted provider in the Codex model family.")) -(defmethod provider-family ((provider native-scripted-provider)) - "Treat the native scripted provider as the Codex model family." +(defmethod provider-family ((provider codex-scripted-provider)) + "Place the scripted provider in the Codex model family." (declare (ignore provider)) ':codex) -(defmethod provider-native-compact-conversation - ((provider native-scripted-provider) - (conversation conversation) - &key tool-namespaces event-callback) - "Return the next scripted native checkpoint after recording durable input." - (declare (ignore tool-namespaces event-callback)) - (push (conversation-input-items-for-request - conversation :include-ephemeral-p nil) - (native-scripted-provider-native-input-snapshots provider)) - (pop (native-scripted-provider-native-items provider))) - (defmethod provider-stream-turn ((provider scripted-provider) (conversation conversation) @@ -2529,92 +2508,6 @@ REASONING-CONTEXT is the retention mode its response metadata reports." (platform-delete-directory-tree *platform* root :validate t :if-does-not-exist ':ignore))) nil) -(-> test-agent-native-compaction () null) -(defun test-agent-native-compaction () - "Test that an opaque native window is stored beside a checkpoint every provider reads." - (let* ((configuration (test-configuration)) - (root (test-configuration-root configuration))) - (unwind-protect - (let* ((conversation - (conversation-create configuration :identifier "agent-native-compact")) - (summary-item (agent-test-checkpoint-call)) - (earlier-item - (json-object - "type" "message" - "role" "assistant" - "content" (json-array - (json-object "type" "output_text" - "text" "Earlier response.")))) - (answer-item - (json-object - "type" "message" - "role" "assistant" - "content" (json-array - (json-object "type" "output_text" - "text" "Done.")))) - (provider - (make-instance - 'native-scripted-provider - :configuration configuration - :native-items - (list (json-object "type" "compaction" - "encrypted_content" "native-checkpoint")) - :results (list (agent-test-result "compact-native" - (list summary-item) - :turn-completion ':end) - (agent-test-result "turn-native" - (list answer-item) - :turn-completion ':end)))) - (agent (agent-create :configuration configuration - :provider provider - :conversation conversation - :tool-registry (agent-test-registry) - :worker nil))) - (conversation-append-user-message conversation "earlier context") - (conversation-append-provider-item conversation earlier-item) - (conversation-append-provider-metadata - conversation - (list :request-number 1 - :response-id "seed-native" - :usage '(("total_tokens" 999999)))) - (let ((pending t)) - (test-call-with-function-replacements - (list (list 'agent-should-compact-p - (lambda (agent) (declare (ignore agent)) - (prog1 pending (setf pending nil))))) - (lambda () (agent-run-user-turn agent "hello")))) - (test-assert - (= (length - (first - (native-scripted-provider-native-input-snapshots provider))) - 3) - "native compaction receives the full durable projection") - (test-assert - (= (first (reverse (scripted-provider-input-counts provider))) 3) - "the checkpoint is written from the readable history, not from the native window") - (test-assert - (find :native-compaction - (rest (conversation--read-records - (conversation-pathname conversation))) - :key #'first) - "native compaction persists its opaque checkpoint") - (test-assert - (native-compaction-item-p - (first (conversation-input-items conversation))) - "the active provider reuses the opaque checkpoint") - (test-assert - (= (length (conversation-input-items-for-family conversation ':codex)) 3) - "the active provider receives the opaque window, the checkpoint and the new answer") - (test-assert - (= (length (conversation-input-items-for-family conversation ':grok)) 2) - "another provider receives the checkpoint and the new answer") - (test-assert - (equal (agent-tests--request-numbers conversation) - '((1 nil) (1 :native) (2 :summary) (3 nil))) - "both compaction requests are metered ahead of the user request")) - (platform-delete-directory-tree *platform* root :validate t :if-does-not-exist ':ignore))) - nil) - (-> test-agent-parallel-tool-wave () null) (defun test-agent-parallel-tool-wave () "Test independent calls overlap while callbacks and persistence stay ordered." diff --git a/tests/application-operation-tests.lisp b/tests/application-operation-tests.lisp index 15dc25259..47952d649 100644 --- a/tests/application-operation-tests.lisp +++ b/tests/application-operation-tests.lisp @@ -746,57 +746,49 @@ (-> test-compact-operation () null) (defun test-compact-operation () - "Test forced portable and native compaction through the canonical Lisp command." - (dolist (native-p '(nil t)) - (let* ((native-item - (json-object "type" "context_compaction" - "encrypted_content" "command-checkpoint")) - (provider - (apply #'make-instance - (if native-p 'native-scripted-provider 'scripted-provider) - :configuration (test-configuration) - :results (list (agent-test-result - "manual-summary" - (list (agent-test-checkpoint-call)) - :turn-completion ':end)) - (when native-p (list :native-items (list native-item)))))) - (application-operation-tests--call-with-compaction-provider - provider - (lambda (application) - (let* ((conversation (application-conversation application)) - (configuration (application-configuration application))) - (conversation-append-user-message conversation "Earlier work.") - (let* ((sequence-before (conversation-next-sequence conversation)) - (evaluation (application-lisp-evaluate - "(compact)" :application application)) - (records-after (conversation--read-records - (conversation-pathname conversation))) - (reloaded (conversation-load-by-id - configuration (conversation-identifier conversation)))) - (test-assert (eq (application-lisp-evaluation-status evaluation) ':ok) - "the registered Lisp command compacts below the automatic threshold") - (test-assert - (and (equal (scripted-provider-compaction-flags provider) '(t)) - (equal (scripted-provider-tool-schema-counts provider) - (list (length (agent--tool-namespaces (application-agent application) - :tools-p t))))) - "manual compaction makes one request that declares the tools of ordinary requests") - (test-assert - (= (conversation-next-sequence conversation) - (+ sequence-before (if native-p 3 2))) - "manual compaction appends its metered requests and one checkpoint without starting a user turn") - (test-assert - (eq (first (first (last records-after))) - (if native-p ':native-compaction ':summary)) - "the command uses the provider's existing compaction strategy") - (test-assert - (string= (json-encode (coerce (conversation-input-items conversation) 'vector)) - (json-encode (coerce (conversation-input-items reloaded) 'vector))) - "the compacted projection is reproducible from durable history") - (test-assert - (and (not (terminal-ui-compacting-p (application-ui application))) - (null (terminal-ui-status (application-ui application)))) - "manual compaction clears its progress indicators"))))))) + "Test forced compaction through the canonical Lisp command." + (let ((provider + (make-instance 'scripted-provider + :configuration (test-configuration) + :results (list (agent-test-result + "manual-summary" + (list (agent-test-checkpoint-call)) + :turn-completion ':end))))) + (application-operation-tests--call-with-compaction-provider + provider + (lambda (application) + (let* ((conversation (application-conversation application)) + (configuration (application-configuration application))) + (conversation-append-user-message conversation "Earlier work.") + (let* ((sequence-before (conversation-next-sequence conversation)) + (evaluation (application-lisp-evaluate + "(compact)" :application application)) + (records-after (conversation--read-records + (conversation-pathname conversation))) + (reloaded (conversation-load-by-id + configuration (conversation-identifier conversation)))) + (test-assert (eq (application-lisp-evaluation-status evaluation) ':ok) + "the registered Lisp command compacts below the automatic threshold") + (test-assert + (and (equal (scripted-provider-compaction-flags provider) '(t)) + (equal (scripted-provider-tool-schema-counts provider) + (list (length (agent--tool-namespaces (application-agent application) + :tools-p t))))) + "manual compaction makes one request that declares the tools of ordinary requests") + (test-assert + (= (conversation-next-sequence conversation) (+ sequence-before 2)) + "manual compaction appends its metered request and one checkpoint without starting a user turn") + (test-assert + (eq (first (first (last records-after))) ':summary) + "the command publishes a checkpoint") + (test-assert + (string= (json-encode (coerce (conversation-input-items conversation) 'vector)) + (json-encode (coerce (conversation-input-items reloaded) 'vector))) + "the compacted projection is reproducible from durable history") + (test-assert + (and (not (terminal-ui-compacting-p (application-ui application))) + (null (terminal-ui-status (application-ui application)))) + "manual compaction clears its progress indicators")))))) nil) (-> test-fix-skipped-definitions-operation () null) @@ -919,12 +911,9 @@ :status 503 :code nil :request-id nil :response-id nil :response nil))) (let ((provider - (make-instance - 'native-scripted-provider - :configuration (test-configuration) - :native-items (list (json-object "type" "context_compaction" - "encrypted_content" "uncommitted-checkpoint")) - :results (list result)))) + (make-instance 'scripted-provider + :configuration (test-configuration) + :results (list result)))) (application-operation-tests--call-with-compaction-provider provider (lambda (application) @@ -941,9 +930,9 @@ (test-assert (and (string= input-before (json-encode (coerce (conversation-input-items conversation) 'vector))) - (notany (lambda (record) - (member (first record) '(:summary :native-compaction))) - (conversation--read-records (conversation-pathname conversation)))) + (not (find :summary + (conversation--read-records (conversation-pathname conversation)) + :key #'first))) "a failed request preserves its projection and publishes no checkpoint") (test-assert (and (not (terminal-ui-compacting-p (application-ui application))) diff --git a/tests/compaction-acceptance-tests.lisp b/tests/compaction-acceptance-tests.lisp index 077e1c706..83cb9da7b 100644 --- a/tests/compaction-acceptance-tests.lisp +++ b/tests/compaction-acceptance-tests.lisp @@ -22,13 +22,6 @@ (push (json-get request "tool_choice") (compaction-admission-test-encoded-choices provider)) (values request delivery))) -(defmethod provider-native-compact-conversation - ((provider compaction-admission-test-provider) (conversation conversation) - &key tool-namespaces event-callback) - "Use the portable path without credentials or transport." - (declare (ignore provider conversation tool-namespaces event-callback)) - (values nil nil)) - (defmethod provider-stream-turn :before ((provider compaction-admission-test-provider) (conversation conversation) &key tool-namespaces event-callback goal-context compaction-p) @@ -128,39 +121,6 @@ it submits a checkpoint." "a checkpoint keeps the references it cites and no others")))) nil) -(-> test-compaction-native-paid-rejection () null) -(defun test-compaction-native-paid-rejection () - "Meter paid native completion even with no opaque output or rejected content." - (dolist (invalid-p '(nil t)) - (with-test-configuration (configuration) - (let* ((conversation (conversation-create configuration)) - (provider (make-instance 'scripted-provider :configuration configuration - :results (list (agent-test-result "summary" - (list (agent-test-checkpoint-call)) - :turn-completion ':end)))) - (agent (agent-create :configuration configuration :conversation conversation - :provider provider :tool-registry (agent-test-registry) :worker nil))) - (conversation-append-user-message conversation "native source") - (test-call-with-function-replacements - (list (list 'provider-native-compact-conversation - (lambda (provider view &key tool-namespaces event-callback) - (declare (ignore provider view tool-namespaces)) - (funcall event-callback - (make-instance 'native-compaction-completed-event - :usage nil :response-id "native-paid")) - (when invalid-p - (error 'provider-protocol-error :message "Rejected paid native output." - :status nil :code nil :request-id nil - :response-id "native-paid" :response nil)) - (values nil nil)))) - (lambda () - (handler-case (agent-compact-conversation agent (make-instance 'agent-observer)) - (provider-protocol-error () nil)))) - (test-assert (equal (if invalid-p '((1 :native)) '((1 :native) (2 :summary))) - (agent-tests--request-numbers conversation)) - "paid native completion is recorded once, before interpretation")))) - nil) - (-> test-compaction-next-request-admission () null) (defun test-compaction-next-request-admission () "Validate pending caps and MCP contributions before publishing a checkpoint." @@ -479,9 +439,7 @@ it submits a checkpoint." :reader compaction-ownership-test-summary-mode :documentation "Known, unknown, failed or locally refused summary requests.") (views :initform nil :accessor compaction-ownership-test-views - :documentation "Request views and exact mission identity during dispatch.") - (native-p :initarg :native-p :initform nil :reader compaction-ownership-test-native-p - :documentation "Whether to enter an actual native request attempt.")) + :documentation "Request views and exact mission identity during dispatch.")) (:documentation "Observe detached request ownership without credentials or network access.")) (defmethod provider-stream-turn @@ -502,40 +460,9 @@ it submits a checkpoint." :conversation conversation)))) (call-next-method)) -(defmethod provider-native-compact-conversation - ((provider compaction-ownership-test-provider) (conversation conversation) - &key tool-namespaces event-callback) - "Enter the native-attempt boundary only when this fixture supports native requests." - (if (compaction-ownership-test-native-p provider) - (provider-attempt-native-compaction provider conversation - :tool-namespaces tool-namespaces - :force-refresh nil :event-callback event-callback) - (values nil nil))) - -(defmethod provider-attempt-native-compaction - ((provider compaction-ownership-test-provider) (conversation conversation) - &key tool-namespaces force-refresh event-callback) - "Report separately paid native usage without an opaque checkpoint." - (declare (ignore tool-namespaces force-refresh event-callback)) - (push (list conversation (mission-context-find conversation)) - (compaction-ownership-test-views provider)) - (case (compaction-ownership-test-summary-mode provider) - (:native-error - (error 'provider-protocol-error :message "Scripted native failure." - :status nil :code nil :request-id nil :response-id nil :response nil)) - (:native-refuse - (provider-open-response-stream - provider (json-object "input" "short" "max_output_tokens" 6000) - :conversation conversation))) - (values nil (if (eq (compaction-ownership-test-summary-mode provider) :native-unknown) - (json-object) - (json-object "input_tokens" 2 "output_tokens" 3)))) - (-> compaction-ownership-test--run - (&key (:mission-p boolean) (:mode keyword) (:native-p boolean) - (:turns (integer 1)) (:continued-p boolean)) null) -(defun compaction-ownership-test--run - (&key (mission-p t) (mode ':known) native-p (turns 8) continued-p) + (&key (:mission-p boolean) (:mode keyword) (:turns (integer 1)) (:continued-p boolean)) null) +(defun compaction-ownership-test--run (&key (mission-p t) (mode ':known) (turns 8) continued-p) "Verify actual usage, refusal, budget and cleanup on a private mission." (with-test-environment (("AUTOLITH_CONTEXT_WINDOW" "5000") ("AUTOLITH_CONTEXT_OUTPUT_RESERVE" "256")) @@ -544,7 +471,7 @@ it submits a checkpoint." (json-object "input_tokens" 1 "output_tokens" 1))) (provider (make-instance 'compaction-ownership-test-provider - :configuration configuration :summary-mode mode :native-p native-p + :configuration configuration :summary-mode mode :results (append (when continued-p (list (agent-test-result "unfinished" nil :turn-completion ':continue))) @@ -582,7 +509,7 @@ it submits a checkpoint." (test-assert (null (mission-context-find (first entry))) "fresh ownership ends on every unwind")) (when application (let* ((ledger (mission-context-goal context)) - (blocked-p (member mode '(:unknown :error :native-unknown :native-error))) + (blocked-p (member mode '(:unknown :error))) (exhausted-p (= turns 1)) (expected-tokens (cond @@ -592,12 +519,12 @@ it submits a checkpoint." 0) ((eq mode :refuse) 0) - ((eq mode :native-refuse) - 2) + (continued-p + 4) (t - (+ (if native-p 5 0) (if continued-p 4 2)))))) - (format t "~&OWNERSHIP-WITNESS mode=~S native=~S turns=~D error=~S ledger=~S~%" - mode native-p turns (and failure (type-of failure)) ledger) + 2)))) + (format t "~&OWNERSHIP-WITNESS mode=~S turns=~D error=~S ledger=~S~%" + mode turns (and failure (type-of failure)) ledger) (test-assert (= expected-tokens (getf ledger :tokens-used)) "only actual reported usage is charged") (test-assert (= (if blocked-p 1 0) (getf ledger :unknown-usage 0)) "unknown follows ordinary mission policy") (test-assert (zerop (getf ledger :requests-outstanding)) "all request reservations settle") @@ -611,19 +538,16 @@ it submits a checkpoint." ((eq mode :refuse) (test-assert (typep failure 'provider-context-budget-error) "a local refusal retains its type") (test-assert (eq ':active (getf ledger :status)) "a local compaction refusal has known zero usage")) - ((eq mode :native-refuse) - (test-assert (null failure) "exhausted native admission falls back to the admitted portable summary") - (test-assert (eq ':active (getf ledger :status)) "known-zero native refusals do not invent token usage")) (t (test-assert (null failure) "owned compaction succeeds") - (test-assert (= (+ (if native-p 1 0) (if continued-p 2 1)) (getf ledger :turns-used)) - "each actual native or continued summary request reserves one turn")))))) + (test-assert (= (if continued-p 2 1) (getf ledger :turns-used)) + "each summary request reserves one turn")))))) (when application (mission-detach application)))))) nil) (-> test-compaction-summary-mission-ownership () null) (defun test-compaction-summary-mission-ownership () - "Charge known summaries and bound view ownership, including unsupported native probes." + "Charge known summaries and bound view ownership." (compaction-ownership-test--run) (compaction-ownership-test--run :continued-p t) (compaction-ownership-test--run :mission-p nil)) @@ -635,14 +559,6 @@ it submits a checkpoint." (compaction-ownership-test--run :mode mode)) (compaction-ownership-test--run :turns 1)) -(-> test-compaction-native-view-mission-ownership () null) -(defun test-compaction-native-view-mission-ownership () - "Charge the detached native request and summary exactly once each." - (compaction-ownership-test--run :native-p t) - (dolist (mode '(:native-unknown :native-error :native-refuse)) - (compaction-ownership-test--run :native-p t :mode mode)) - (compaction-ownership-test--run :native-p t :turns 1)) - (-> test-compaction-request-view-isolation () null) (defun test-compaction-request-view-isolation () "Bound exact mission aliases without clobbering nested or unrelated request owners." diff --git a/tests/compaction-recovery-tests.lisp b/tests/compaction-recovery-tests.lisp index 2a7a58e91..54d9adce7 100644 --- a/tests/compaction-recovery-tests.lisp +++ b/tests/compaction-recovery-tests.lisp @@ -4,32 +4,13 @@ (defclass compaction-recovery-test-provider (compaction-gap-test-provider) ((window :initarg :window :initform nil :reader compaction-recovery-test-window - :documentation "Deterministic selected-route metadata.") - (native-calls :initform 0 :accessor compaction-recovery-test-native-calls - :documentation "Admitted inert native transports.")) - (:documentation "Exercise both final admission boundaries without credentials.")) + :documentation "Deterministic selected-route metadata.")) + (:documentation "Exercise final admission without credentials.")) (defmethod provider-compaction-recovery-window ((provider compaction-recovery-test-provider)) "Inject existing selected-route metadata deterministically." (compaction-recovery-test-window provider)) -(defmethod provider-open-native-compaction - ((provider compaction-recovery-test-provider) (request hash-table) &key credentials conversation) - "Record actual native dispatch without network traffic." - (declare (ignore request credentials conversation)) - (incf (compaction-recovery-test-native-calls provider)) - (values nil 200 nil)) - -(defmethod provider-native-compact-conversation - ((provider compaction-recovery-test-provider) (conversation conversation) - &key tool-namespaces event-callback) - "Admit a real native projection, then model unsupported portable fallback." - (declare (ignore event-callback)) - (provider-open-native-compaction - provider (provider-native-compaction-request-object provider conversation tool-namespaces) - :conversation conversation) - (values nil nil)) - (-> compaction-recovery-test-small-context (function) t) (defun compaction-recovery-test-small-context (function) "Isolate admission fixtures from the host's prompt and request-local contributors." @@ -43,7 +24,7 @@ (-> test-compaction-recovery-encrypted-history () null) (defun test-compaction-recovery-encrypted-history () - "Recover a refused native compaction using detached old encrypted reasoning." + "Recover a refused compaction request by dropping detached old encrypted reasoning." (with-test-environment (("AUTOLITH_CONTEXT_WINDOW" "5000") ("AUTOLITH_CONTEXT_OUTPUT_RESERVE" "256")) (with-test-configuration (configuration) @@ -61,6 +42,11 @@ conversation (json-object "type" "reasoning" "id" "old-secret" "encrypted_content" (make-string 40000 :initial-element #\e))) (conversation-append-provider-item conversation (agent-test-message "old answer")) + ;; A later exchange keeps the old reasoning out of the retained tail, + ;; so the checkpoint request is the one that has to read it. + (conversation-append-user-message conversation "a later question") + (conversation-append-provider-item + conversation (agent-test-message (make-string 300 :initial-element #\l))) (conversation-append-user-message conversation "continue") (compaction-recovery-test-small-context (lambda () @@ -68,10 +54,8 @@ (provider-context-budget-error (condition) (setf failure condition))))) (test-assert (null failure) (format nil "Emergency compaction must recover the refused request: ~A" failure)) - (test-assert (= 1 (compaction-recovery-test-native-calls provider)) - "Exactly one admitted native attempt reaches transport.") (test-assert (= 1 (context-admission-test-provider-network-calls provider)) - "Exactly one admitted portable summary reaches transport.")))) + "Exactly one admitted checkpoint request reaches transport.")))) nil) (-> test-compaction-recovery-publication-preserves-tail-families () null) @@ -123,15 +107,12 @@ (list (list 'agent--note-compaction-request (lambda (owner observer &rest options) (multiple-value-prog1 (apply note-original owner observer options) - (when (eq :summary (getf options :kind)) - (conversation-append-user-message conversation "Concurrent adopted arrival.") - (setf tail (append tail (list (json-encode (first (last (conversation-input-items conversation)))))))))))) + (conversation-append-user-message conversation "Concurrent adopted arrival.") + (setf tail (append tail (list (json-encode (first (last (conversation-input-items conversation))))))))))) (lambda () (handler-case (agent-compact-conversation agent (make-instance 'agent-observer)) (provider-context-budget-error (condition) (setf failure condition))))))) (test-assert (null failure) "Path: portable recovery successfully publishes.") - (test-assert (= 1 (compaction-recovery-test-native-calls provider)) - "Path: one native attempt exhausts into portable compaction.") (test-assert (= 1 (context-admission-test-provider-network-calls provider)) "Path: exactly one admitted portable summary reaches transport.") (let* ((summary (find :summary (conversation-records-newest conversation 20) :key #'first)) @@ -195,41 +176,38 @@ (oversize (json-object "input" (make-string 80000 :initial-element #\x) "max_output_tokens" 700))) (dolist (window '(nil 4000 5000 20000)) - (dolist (native-p '(nil t)) - (let* ((provider (make-instance 'compaction-recovery-test-provider - :configuration configuration :window window :results nil)) - (view (conversation-create configuration)) - (state (make-compaction-recovery))) - (conversation-append-user-message view "continue") - (flet ((refused-p (candidate) - "Return true when a compaction that sends CANDIDATE is refused locally." - (flet ((check (view) - (declare (ignore view)) - (provider-check-request-context provider candidate)) - - (send (view) - (declare (ignore view)) - (if native-p - (provider-open-native-compaction provider candidate) - (provider-open-response-stream provider candidate)))) - (handler-case - (progn (compaction-recovery-call state provider view - :dispatch #'send :preflight #'check) - nil) - (provider-context-budget-error () t))))) - (if (eql window 20000) - (progn - (test-assert (not (or (refused-p request) (refused-p request))) - "Every request of the compaction is admitted, not only the first.") - (test-assert (null (compaction-recovery-audit state ':completed)) - "A request the model window admits is no recovery.") - (test-assert (refused-p oversize) - "The output reserve and the model window remain hard bounds.")) - (test-assert (refused-p request) - "Only a strictly larger model window admits the request."))) - (test-assert (handler-case (progn (provider-check-request-context provider request) nil) - (provider-context-budget-error () t)) - "An ordinary request sees the configured window."))))))) + (let* ((provider (make-instance 'compaction-recovery-test-provider + :configuration configuration :window window :results nil)) + (view (conversation-create configuration)) + (state (make-compaction-recovery))) + (conversation-append-user-message view "continue") + (flet ((refused-p (candidate) + "Return true when a compaction that sends CANDIDATE is refused locally." + (flet ((check (view) + (declare (ignore view)) + (provider-check-request-context provider candidate)) + + (send (view) + (declare (ignore view)) + (provider-open-response-stream provider candidate))) + (handler-case + (progn (compaction-recovery-call state provider view + :dispatch #'send :preflight #'check) + nil) + (provider-context-budget-error () t))))) + (if (eql window 20000) + (progn + (test-assert (not (or (refused-p request) (refused-p request))) + "Every request of the compaction is admitted, not only the first.") + (test-assert (null (compaction-recovery-audit state ':completed)) + "A request the model window admits is no recovery.") + (test-assert (refused-p oversize) + "The output reserve and the model window remain hard bounds.")) + (test-assert (refused-p request) + "Only a strictly larger model window admits the request."))) + (test-assert (handler-case (progn (provider-check-request-context provider request) nil) + (provider-context-budget-error () t)) + "An ordinary request sees the configured window.")))))) nil) (-> test-compaction-recovery-unrelated-failure () null) @@ -396,39 +374,21 @@ (defclass compaction-recovery-accounting-test-provider (compaction-recovery-test-provider compaction-ownership-test-provider) () - (:documentation "Combine final native admission with C1 ordinary mission ownership.")) - -(defmethod provider-native-compact-conversation - ((provider compaction-recovery-accounting-test-provider) (conversation conversation) - &key tool-namespaces event-callback) - "Exercise the real accounted native attempt boundary." - (provider-attempt-native-compaction provider conversation :tool-namespaces tool-namespaces - :event-callback event-callback :force-refresh nil)) - -(defmethod provider-attempt-native-compaction - ((provider compaction-recovery-accounting-test-provider) (conversation conversation) - &key tool-namespaces force-refresh event-callback) - "Admit a native request, then report known or unknown usage through ordinary accounting." - (declare (ignore force-refresh event-callback)) - (push (list conversation (mission-context-find conversation)) (compaction-ownership-test-views provider)) - (provider-open-native-compaction - provider (provider-native-compaction-request-object provider conversation tool-namespaces) - :conversation conversation) - (values nil (if (eq :unknown (compaction-ownership-test-summary-mode provider)) - (json-object) - (json-object "input_tokens" 2 "output_tokens" 3)))) + (:documentation "Combine final admission with ordinary mission ownership.")) (-> test-compaction-recovery-mission-accounting () null) (defun test-compaction-recovery-mission-accounting () - "Charge actual recovery usage, block UNKNOWN, and refuse exhausted mission budgets." + "Charge actual recovery usage and block the mission on unknown usage." (with-test-environment (("AUTOLITH_CONTEXT_WINDOW" "5000") ("AUTOLITH_CONTEXT_OUTPUT_RESERVE" "256")) (with-test-configuration (configuration) - (dolist (mode '(:known :unknown :budget)) + (dolist (mode '(:known :unknown)) (let* ((provider (make-instance 'compaction-recovery-accounting-test-provider - :configuration configuration :summary-mode mode :native-p t + :configuration configuration :summary-mode mode :results (list (agent-test-result "summary" (list (agent-test-checkpoint-call)) - :usage (json-object "input_tokens" 1 "output_tokens" 1))))) + :usage (if (eq mode :unknown) + (json-object) + (json-object "input_tokens" 1 "output_tokens" 1)))))) (application (mission-inference-test--application configuration provider)) (conversation (application-conversation application)) (agent (agent-create :configuration configuration :conversation conversation @@ -439,30 +399,28 @@ (conversation-append-provider-item conversation (json-object "type" "reasoning" "encrypted_content" (make-string 40000 :initial-element #\e))) (conversation-append-provider-item conversation (agent-test-message "old answer")) + (conversation-append-user-message conversation "a later question") + (conversation-append-provider-item + conversation (agent-test-message (make-string 300 :initial-element #\l))) (conversation-append-user-message conversation "continue") - (application-mission-start application (mission-test--specification :tokens 100000 :turns (if (eq mode :budget) 1 8))) - (let* ((context (mission-context-find conversation)) - (before (mapcar #'json-encode (conversation-input-items conversation)))) + (application-mission-start application (mission-test--specification :tokens 100000 :turns 8)) + (let ((context (mission-context-find conversation))) (compaction-recovery-test-small-context (lambda () (handler-case (agent-compact-conversation agent (make-instance 'agent-observer)) (error (condition) (setf failure condition))))) (let ((ledger (mission-context-goal context))) - (test-assert (if (eq mode :known) (null failure) (typep failure 'mission-error)) - "Only known, budget-admitted recovery succeeds.") - (test-assert (= (if (eq mode :known) 7 0) (getf ledger :tokens-used 0)) + (test-assert (null failure) + (format nil "Recovery with ~(~A~) usage ended with ~A." mode failure)) + (test-assert (= (if (eq mode :known) 2 0) (getf ledger :tokens-used 0)) "Only actual reported usage is charged.") (test-assert (= (if (eq mode :unknown) 1 0) (getf ledger :unknown-usage 0)) "Unknown usage follows existing mission policy.") + (test-assert (eq (if (eq mode :unknown) ':blocked ':active) (getf ledger :status)) + "Unknown usage blocks new mission requests.") (test-assert (zerop (getf ledger :requests-outstanding 0)) "All reservations settle.") - (unless (eq mode :known) - (test-assert (equal before (mapcar #'json-encode (conversation-input-items conversation))) - "Failure preserves original history.")) (dolist (entry (compaction-ownership-test-views provider)) - (test-assert (eq context (second entry)) "Every dispatched copy has the exact captured owner.")) - (when (eq mode :budget) - (test-assert (zerop (compaction-recovery-test-native-calls provider)) - "Hard mission budget refusal reaches no transport.")))))))) + (test-assert (eq context (second entry)) "Every dispatched copy has the exact captured owner.")))))))) nil) (-> test-compaction-recovery-selected-route () null) @@ -508,7 +466,7 @@ (lambda () (flet ((admit (candidate) (provider-check-request-context - provider (provider-native-compaction-request-object provider candidate #())))) + provider (provider-request-object provider candidate #() :compaction-p t)))) (dotimes (attempt 2) (handler-case (compaction-recovery-call state provider conversation :dispatch #'admit :preflight #'admit) @@ -530,184 +488,106 @@ "Every original input item retains its identity.") (test-assert (equal bytes (mapcar #'json-encode (conversation-input-items conversation))) "Exhaustion and publication refusal never rewrite or clip the source.") - (test-assert (and (zerop (compaction-recovery-test-native-calls provider)) - (= 1 (context-admission-test-provider-network-calls provider))) + (test-assert (= 1 (context-admission-test-provider-network-calls provider)) "Only the admitted older-history summary reaches transport."))))) nil) -;;;; -- Unsupported Native Dispatch Accounting -- - -(defclass unsupported-native-test-provider (scripted-provider codex-subscription-provider) - ((status :initarg :status :reader unsupported-native-test-status - :documentation "Synthetic native HTTP status or transport failure.") - (window :initarg :window :initform nil :reader unsupported-native-test-window - :documentation "Existing metadata available to recovery.") - (source :initform nil :accessor unsupported-native-test-source - :documentation "Canonical conversation, never used for native dispatch.") - (context :initform nil :accessor unsupported-native-test-context - :documentation "Exact source mission owner.") - (native-calls :initform 0 :accessor unsupported-native-test-native-calls - :documentation "Actual admitted native dispatches.") - (summary-calls :initform 0 :accessor unsupported-native-test-summary-calls - :documentation "Admitted portable summary requests.") - (settlements :initform nil :accessor unsupported-native-test-settlements - :documentation "Ledger snapshots after native accounting settles.")) - (:documentation "Production native attempts with inert final dispatch and real mission accounting.")) - -(defmethod provider-compaction-recovery-window ((provider unsupported-native-test-provider)) +;;;; -- Mission Accounting of Codex Compaction Requests -- + +(defclass codex-compaction-test-provider (scripted-provider codex-subscription-provider) + ((window :initarg :window :initform nil :reader codex-compaction-test-window + :documentation "The window the registry lists for the model.") + (summary-calls :initform 0 :accessor codex-compaction-test-summary-calls + :documentation "Admitted checkpoint requests.")) + (:documentation "The production Codex encoder with inert dispatch and real mission accounting.")) + +(defmethod provider-compaction-recovery-window ((provider codex-compaction-test-provider)) "Supply existing metadata without discovery or network access." - (unsupported-native-test-window provider)) + (codex-compaction-test-window provider)) (defmethod provider-request-object - ((provider unsupported-native-test-provider) (conversation conversation) + ((provider codex-compaction-test-provider) (conversation conversation) (tool-namespaces vector) &key goal-context compaction-p) "Use the real Codex encoder rather than scripted request encoding." (provider-request-object (make-instance 'codex-subscription-provider :configuration (provider-configuration provider)) conversation tool-namespaces :goal-context goal-context :compaction-p compaction-p)) -(defmethod provider-open-native-compaction - ((provider unsupported-native-test-provider) (request hash-table) &key credentials conversation) - "Refuse only at the final native dispatch boundary, without network traffic." - (declare (ignore request credentials)) - (incf (unsupported-native-test-native-calls provider)) - (test-assert (not (eq conversation (unsupported-native-test-source provider))) - "Native requests use a detached view.") - (test-assert (eq (mission-context-find conversation) (unsupported-native-test-context provider)) - "The detached native dispatch retains its exact mission owner.") - (test-assert (= 1 (getf (mission-context-goal (mission-context-find conversation)) :requests-outstanding)) - "Dispatch occurs under real mission reservation and supervision.") - (case (unsupported-native-test-status provider) - (:transport - (error 'provider-transport-error :message "Inert native transport failure." - :status nil :code nil :request-id nil :response-id nil :response nil)) - (200 - (values (json-encode - (json-object "output" (json-array (json-object "type" "compaction" "encrypted_content" "fixture")) - "usage" (json-object "input_tokens" 2 "output_tokens" 3))) - 200 nil)) - (t - (values "Not found" (unsupported-native-test-status provider) nil)))) - -(defmethod provider-attempt-native-compaction :around - ((provider unsupported-native-test-provider) (conversation conversation) - &key tool-namespaces force-refresh event-callback) - "Observe settlement outside the production accounting around method." - (declare (ignore conversation tool-namespaces force-refresh event-callback)) - (unwind-protect (call-next-method) - (push (copy-tree (mission-context-goal (unsupported-native-test-context provider))) - (unsupported-native-test-settlements provider)))) - (defmethod provider-open-response-stream - ((provider unsupported-native-test-provider) (request hash-table) &key credentials conversation) + ((provider codex-compaction-test-provider) (request hash-table) &key credentials conversation) "Admit an inert stream through the normal context boundary." (declare (ignore request credentials conversation)) (values nil 200 nil)) (defmethod provider-stream-turn - ((provider unsupported-native-test-provider) (conversation conversation) + ((provider codex-compaction-test-provider) (conversation conversation) &key tool-namespaces event-callback goal-context compaction-p) - "Apply final admission, then return deterministic positive portable usage." + "Apply final admission, then return deterministic positive usage." (declare (ignore event-callback)) (provider-open-response-stream provider (provider-request-object provider conversation tool-namespaces :goal-context goal-context :compaction-p compaction-p) :conversation conversation) (when compaction-p - (incf (unsupported-native-test-summary-calls provider))) + (incf (codex-compaction-test-summary-calls provider))) (call-next-method)) -(-> unsupported-native-test--run (&key (:status (or integer keyword)) (:reactive-p boolean) - (:model-window-p boolean)) null) -(defun unsupported-native-test--run (&key status reactive-p model-window-p) - "Exercise actual manual or C1 operations and retain native settlement witnesses." - (with-test-environment (("AUTOLITH_CONTEXT_WINDOW" "5000") - ("AUTOLITH_CONTEXT_OUTPUT_RESERVE" "256")) - (with-test-configuration (configuration) - (let* ((provider (make-instance 'unsupported-native-test-provider - :configuration configuration :status status - :credential-manager (provider-credential-manager (provider-create configuration)) - :window (when model-window-p 50000) - :results (list (agent-test-result - "summary" (list (agent-test-checkpoint-call)) - :usage (json-object "input_tokens" 7 "output_tokens" 3)) - (agent-test-result - "answer" (list (agent-test-message "OK")) - :usage (json-object "input_tokens" 2 "output_tokens" 1))))) - (application (mission-inference-test--application configuration provider)) - (conversation (application-conversation application)) - (agent (agent-create :configuration configuration :conversation conversation - :provider provider :tool-registry (make-instance 'tool-registry))) - (observer (make-instance 'agent-observer)) - (*conversation-compaction-tail-characters* 256) - (failure nil) - (unsupported-p (member status '(404 405 501)))) - (setf (application-agent application) agent - (unsupported-native-test-source provider) conversation) - (conversation-append-user-message conversation "old exchange") - (if model-window-p - (conversation-append-provider-item conversation (agent-test-message (make-string 40000 :initial-element #\h))) - (conversation-append-provider-item - conversation (json-object "type" "reasoning" "encrypted_content" (make-string 40000 :initial-element #\e)))) - (conversation-append-user-message conversation "continue") - (application-mission-start application (mission-test--specification :tokens 100000 :turns 16)) - (setf (unsupported-native-test-context provider) (mission-context-find conversation)) - (unwind-protect - (progn - (compaction-recovery-test-small-context - (lambda () - (test-call-with-function-replacements - (list (list 'credential-manager-credentials - (lambda (&rest arguments) - (declare (ignore arguments)) - (provider-tests--credentials configuration))) - (list 'agent-should-compact-p (lambda (agent) (declare (ignore agent)) nil))) - (lambda () - (handler-case - (if reactive-p - (agent--run-provider-loop agent observer :tools-p nil) - (application-compact application)) - (error (condition) (setf failure condition))))))) - (when failure (format t "~&FIXTURE-FAILURE ~A~%" failure)) - (let* ((ledger (mission-context-goal (unsupported-native-test-context provider))) - (snapshot (first (unsupported-native-test-settlements provider)))) - (format t "~&UNSUPPORTED-WITNESS status=~S reactive=~S model-window=~S failure=~S native=~D summary=~D settlement=~S final=~S~%" - status reactive-p model-window-p (and failure (type-of failure)) - (unsupported-native-test-native-calls provider) - (unsupported-native-test-summary-calls provider) snapshot ledger) - (test-assert (= 1 (unsupported-native-test-native-calls provider)) "One native dispatch reaches transport.") - (test-assert (zerop (getf ledger :requests-outstanding 0)) "All final requests settle.") - (test-assert (zerop (getf ledger :tokens-reserved 0)) "All final allocations settle.") - (when unsupported-p - (test-assert (null failure) "Unsupported native dispatch falls back without mission error.") - (test-assert (eq :active (getf ledger :status)) "Unsupported native dispatch preserves ACTIVE status.") - (test-assert (= 1 (unsupported-native-test-summary-calls provider)) "Portable fallback is admitted exactly once.") - (test-assert (= (if reactive-p 13 10) (getf ledger :tokens-used 0)) "Only legitimate positive summary and resumed usage is charged.") - (dolist (key '(:tokens-used :unknown-usage :reservation-charged-tokens :tokens-reserved :requests-outstanding)) - (test-assert (and snapshot (zerop (getf snapshot key 0))) - (format nil "Native settlement is known zero before summary admission: ~S" key)))) - (unless unsupported-p - (test-assert (typep failure 'mission-error) "Unrelated native failures retain mission handling.") - (test-assert (= 1 (getf ledger :unknown-usage 0)) "Unrelated native usage stays uncertain.") - (test-assert (zerop (unsupported-native-test-summary-calls provider)) "Unrelated failures never take unsupported fallback.")))) - (mission-detach application))))) - nil) - -(-> test-unsupported-native-mission-fallback () null) -(defun test-unsupported-native-mission-fallback () - "Cover all unsupported HTTP statuses and unrelated failures in manual and C1 paths." - (dolist (reactive-p '(nil t)) - (dolist (status '(404 405 501 500 :transport)) - (unsupported-native-test--run :status status :reactive-p reactive-p))) - nil) - -(-> test-unsupported-native-model-window () null) -(defun test-unsupported-native-model-window () - "After an unavailable native attempt the portable checkpoint request keeps the model window." +(-> test-compaction-recovery-mission-model-window () null) +(defun test-compaction-recovery-mission-model-window () + "Admit a mission's checkpoint request against the model window in manual and reactive compaction." (dolist (reactive-p '(nil t)) - (dolist (status '(404 405 501)) - (unsupported-native-test--run :status status :reactive-p reactive-p :model-window-p t))) + (with-test-environment (("AUTOLITH_CONTEXT_WINDOW" "5000") + ("AUTOLITH_CONTEXT_OUTPUT_RESERVE" "256")) + (with-test-configuration (configuration) + (let* ((provider (make-instance 'codex-compaction-test-provider + :configuration configuration + :credential-manager (provider-credential-manager (provider-create configuration)) + :window 50000 + :results (list (agent-test-result + "summary" (list (agent-test-checkpoint-call)) + :usage (json-object "input_tokens" 7 "output_tokens" 3)) + (agent-test-result + "answer" (list (agent-test-message "OK")) + :usage (json-object "input_tokens" 2 "output_tokens" 1))))) + (application (mission-inference-test--application configuration provider)) + (conversation (application-conversation application)) + (agent (agent-create :configuration configuration :conversation conversation + :provider provider :tool-registry (make-instance 'tool-registry))) + (*conversation-compaction-tail-characters* 256) + (failure nil)) + (setf (application-agent application) agent) + (conversation-append-user-message conversation "old exchange") + (conversation-append-provider-item conversation (agent-test-message (make-string 40000 :initial-element #\h))) + (conversation-append-user-message conversation "continue") + (application-mission-start application (mission-test--specification :tokens 100000 :turns 16)) + (unwind-protect + (let ((context (mission-context-find conversation))) + (compaction-recovery-test-small-context + (lambda () + (test-call-with-function-replacements + (list (list 'credential-manager-credentials + (lambda (&rest arguments) + (declare (ignore arguments)) + (provider-tests--credentials configuration))) + (list 'agent-should-compact-p (lambda (agent) (declare (ignore agent)) nil))) + (lambda () + (handler-case + (if reactive-p + (agent--run-provider-loop agent (make-instance 'agent-observer) :tools-p nil) + (application-compact application)) + (error (condition) (setf failure condition))))))) + (let ((ledger (mission-context-goal context))) + (test-assert (null failure) + (format nil "The checkpoint request is admitted without a mission error: ~A" failure)) + (test-assert (eq :active (getf ledger :status)) "The mission stays active.") + (test-assert (= 1 (codex-compaction-test-summary-calls provider)) + "The checkpoint request is admitted exactly once.") + (test-assert (= (if reactive-p 13 10) (getf ledger :tokens-used 0)) + "Only the checkpoint and resumed usage are charged.") + (test-assert (zerop (getf ledger :requests-outstanding 0)) "All final requests settle.") + (test-assert (zerop (getf ledger :tokens-reserved 0)) "All final allocations settle."))) + (mission-detach application)))))) nil) (-> test-compaction-recovery-notice-counts () null) @@ -929,48 +809,46 @@ nil) -;;;; -- Production Streaming Native Fallback -- +;;;; -- Compaction Through Production Streaming -- -(defclass expanded-native-test-provider (codex-subscription-provider) - ((results :initarg :results :accessor expanded-native-results +(defclass compaction-owner-test-provider (codex-subscription-provider) + ((results :initarg :results :accessor compaction-owner-results :documentation "Response data encoded into real completed SSE streams.") - (input-snapshots :initform nil :accessor expanded-native-input-snapshots + (input-snapshots :initform nil :accessor compaction-owner-input-snapshots :documentation "Input identities seen only at admitted opening I/O.") - (compaction-flags :initform nil :accessor expanded-native-compaction-flags + (compaction-flags :initform nil :accessor compaction-owner-compaction-flags :documentation "Summary role observed at admitted opening I/O.") - (native-views :initform nil :accessor expanded-native-test-views - :documentation "Actual native opening conversation and mission context.") - (after-summary :initform nil :accessor expanded-native-test-after-summary + (after-summary :initform nil :accessor compaction-owner-test-after-summary :documentation "Canonical arrival delivered when the summary stream closes.")) (:documentation "Retain production Codex streaming and replace only inert response I/O.")) -(defmethod provider-compaction-recovery-window ((provider expanded-native-test-provider)) +(defmethod provider-compaction-recovery-window ((provider compaction-owner-test-provider)) "Keep the fixture's metadata ceiling equal to its configured window." (config :context-window (provider-configuration provider))) -(defclass expanded-native-input-stream (trivial-gray-streams:fundamental-character-input-stream) - ((source :initarg :source :reader expanded-native-input-source +(defclass compaction-owner-input-stream (trivial-gray-streams:fundamental-character-input-stream) + ((source :initarg :source :reader compaction-owner-input-source :documentation "A standard completed synthetic SSE response.") - (on-close :initarg :on-close :initform nil :accessor expanded-native-input-on-close + (on-close :initarg :on-close :initform nil :accessor compaction-owner-input-on-close :documentation "Optional synthetic concurrent arrival at transport completion.")) (:documentation "Character transport with an independently observable completion boundary.")) -(defmethod trivial-gray-streams:stream-read-char ((stream expanded-native-input-stream)) +(defmethod trivial-gray-streams:stream-read-char ((stream compaction-owner-input-stream)) "Read the inert transport without bypassing the production event decoder." - (read-char (expanded-native-input-source stream) nil :eof)) + (read-char (compaction-owner-input-source stream) nil :eof)) -(defmethod trivial-gray-streams:stream-unread-char ((stream expanded-native-input-stream) character) +(defmethod trivial-gray-streams:stream-unread-char ((stream compaction-owner-input-stream) character) "Return CHARACTER to the inert transport." - (unread-char character (expanded-native-input-source stream))) + (unread-char character (compaction-owner-input-source stream))) -(defmethod close :after ((stream expanded-native-input-stream) &key abort) +(defmethod close :after ((stream compaction-owner-input-stream) &key abort) "Deliver one canonical concurrent arrival after a normally consumed summary stream." (unless abort - (let ((callback (shiftf (expanded-native-input-on-close stream) nil))) + (let ((callback (shiftf (compaction-owner-input-on-close stream) nil))) (when callback (funcall callback))))) -(-> expanded-native-test--sse (provider-result) string) -(defun expanded-native-test--sse (result) +(-> compaction-owner-test--sse (provider-result) string) +(defun compaction-owner-test--sse (result) "Encode fixture data as standard created, output-item and completed response events." (with-output-to-string (stream) (write-string (test-sse-event-string @@ -985,33 +863,27 @@ "usage" (provider-result-usage result)))) stream))) -(defmethod provider-open-native-compaction :before - ((provider expanded-native-test-provider) (request hash-table) &key credentials conversation) - "Observe actual native opening while retaining production POST and final admission." - (declare (ignore request credentials)) - (push (list conversation (mission-context-find conversation)) (expanded-native-test-views provider))) - (defmethod provider-open-response-stream - ((provider expanded-native-test-provider) (request hash-table) &key credentials conversation) + ((provider compaction-owner-test-provider) (request hash-table) &key credentials conversation) "Return inert SSE I/O after production projection, attempt, encoder and final admission." (declare (ignore request credentials)) (let* ((summary-p (eq *telemetry-request-role* :compaction)) - (result (pop (expanded-native-results provider))) - (callback (and summary-p (shiftf (expanded-native-test-after-summary provider) nil)))) + (result (pop (compaction-owner-results provider))) + (callback (and summary-p (shiftf (compaction-owner-test-after-summary provider) nil)))) (assert result) - (push (conversation-input-items conversation) (expanded-native-input-snapshots provider)) - (push summary-p (expanded-native-compaction-flags provider)) + (push (conversation-input-items conversation) (compaction-owner-input-snapshots provider)) + (push summary-p (compaction-owner-compaction-flags provider)) (format t "~&EXPANDED-STREAM role=~S sequence=~D~%" (if summary-p :summary :answer) - (length (expanded-native-input-snapshots provider))) - (values (make-instance 'expanded-native-input-stream - :source (make-string-input-stream (expanded-native-test--sse result)) + (length (compaction-owner-input-snapshots provider))) + (values (make-instance 'compaction-owner-input-stream + :source (make-string-input-stream (compaction-owner-test--sse result)) :on-close callback) 200 nil))) ;; Couple small histories to an earlier soft threshold and a bounded hard window. ;; Keep the actual system instructions and encoded request admission in every cell. -(-> expanded-native-test--soft-pressure (application) null) -(defun expanded-native-test--soft-pressure (application) +(-> compaction-owner-test--soft-pressure (application) null) +(defun compaction-owner-test--soft-pressure (application) "Configure the smaller fixture's soft limit and check actual input pressure." (setf (config :compaction-threshold-percent (application-configuration application)) 35) (let* ((agent (application-agent application)) @@ -1027,8 +899,8 @@ input threshold (config :context-window configuration))) nil) -(-> expanded-native-test--refusal-budget (provider-context-budget-error) null) -(defun expanded-native-test--refusal-budget (condition) +(-> compaction-owner-test--refusal-budget (provider-context-budget-error) null) +(defun compaction-owner-test--refusal-budget (condition) "Require a measured hard overflow from the original final-request guard." (let ((input (provider-context-budget-error-estimated-input-tokens condition)) (reserve (provider-context-budget-error-output-reserve condition)) @@ -1038,13 +910,13 @@ (format t "~&FINAL-REFUSAL input=~D reserve=~D window=~D~%" input reserve window)) nil) -(-> expanded-native-test--fixture (function &key (:owned-p boolean) (:large-p boolean)) null) -(defun expanded-native-test--fixture (function &key owned-p large-p) +(-> compaction-owner-test--fixture (function &key (:owned-p boolean) (:large-p boolean)) null) +(defun compaction-owner-test--fixture (function &key owned-p large-p) "Call FUNCTION with a real application, optional mission, and inert Codex transport." (with-test-environment (("AUTOLITH_CONTEXT_WINDOW" "144000") ("AUTOLITH_CONTEXT_OUTPUT_RESERVE" "8192")) (with-test-configuration (configuration) - (let* ((provider (make-instance 'expanded-native-test-provider + (let* ((provider (make-instance 'compaction-owner-test-provider :configuration configuration :credential-manager (provider-credential-manager (provider-create configuration)) :results (list (agent-test-result @@ -1063,7 +935,7 @@ (conversation-append-provider-item conversation (agent-test-message (if large-p (make-string 190000 :initial-element #\h) "Earlier answer."))) (conversation-append-user-message conversation "Continue the actual work.") - (when large-p (expanded-native-test--soft-pressure application)) + (when large-p (compaction-owner-test--soft-pressure application)) (when owned-p (application-mission-start application (mission-test--specification :tokens 2000000 :turns 32))) (unwind-protect @@ -1073,328 +945,224 @@ (terminal-ui-stop (application-ui application)))))) nil) -(-> expanded-native-test--transport - (application &key (:status integer) (:signaled-p boolean) (:function function) - (:failure (option condition)) (:expected-posts (integer 0))) null) -(defun expanded-native-test--transport - (application &key status signaled-p function failure (expected-posts 1)) - "Invoke FUNCTION with a true Dexador condition or its returned-status control at POST." - (let ((calls 0) - (configuration (application-configuration application))) +(-> compaction-owner-test--call-with-credentials (application function) null) +(defun compaction-owner-test--call-with-credentials (application function) + "Call FUNCTION with the fixture's Codex credentials." + (let ((configuration (application-configuration application))) (test-call-with-function-replacements (list (list 'credential-manager-credentials (lambda (&rest arguments) (declare (ignore arguments)) - (provider-tests--credentials configuration))) - (list 'dexador:post - (lambda (endpoint &rest arguments) - (declare (ignore arguments)) - (incf calls) - (test-assert (search "/responses/compact" endpoint) "The actual native POST boundary is reached.") - (when failure (error failure)) - (if signaled-p - (error 'http-request-failed :status status :body "Native endpoint unavailable." - :headers nil :uri endpoint :method ':post) - (values "Native endpoint unavailable." status nil))))) - function) - (test-assert (= expected-posts calls) "Only admitted native requests reach the actual POST boundary.") - (format t "~&NATIVE-HTTP status=~D signaled=~S POST=~D~%" status signaled-p calls)) - nil) - -(-> test-expanded-native-unavailable-negative-controls () null) -(defun test-expanded-native-unavailable-negative-controls () - "Keep authentication, transient HTTP, and uncertain transport failures outside fallback." - (dolist (status '(401 429 500 503 0)) - (dolist (owned-p '(nil t)) - (expanded-native-test--fixture - (lambda (application provider) - (let ((*provider-maximum-transient-retries* 0) - (failure nil)) - (test-call-with-function-replacements - (list (list 'credential-manager-refreshable-p - (lambda (manager) (declare (ignore manager)) nil))) - (lambda () - (expanded-native-test--transport - application :status status :signaled-p t - :failure (when (zerop status) - (make-condition 'end-of-file :stream (make-string-input-stream ""))) - :function - (lambda () - (handler-case (application-compact application) - (error (condition) (setf failure condition))))))) - (format t "~&NATIVE-NEGATIVE status=~D owned=~S failure=~S~%" status owned-p (type-of failure)) - (test-assert failure "An unrelated native failure must propagate to its existing owner.") - (test-assert (not (typep failure 'provider-context-budget-error)) - "An unrelated native failure is not a local context refusal.") - (test-assert (null (expanded-native-input-snapshots provider)) - "No portable summary or ordinary request follows an unrelated failure.") - (test-assert (not (find :summary - (conversation-records-newest (application-conversation application) 30) - :key #'first)) - "An unrelated failure cannot publish a portable checkpoint.") - (when owned-p - (test-assert (plusp (getf (application-goal application) :unknown-usage 0)) - "Uncertain request usage is never settled as known zero.")))) - :owned-p owned-p :large-p t))) + (provider-tests--credentials configuration)))) + function)) nil) -(-> test-expanded-native-unavailable-representations () null) -(defun test-expanded-native-unavailable-representations () - "Classify both wire representations before bound and detached mission supervision." - (dolist (status '(404 405 501)) - (dolist (signaled-p '(nil t)) - (dolist (ownership '(:unowned :bound :detached)) - (expanded-native-test--fixture - (lambda (application provider) - (let* ((source (application-conversation application)) - (view (if (eq ownership ':detached) (conversation-compaction-view (conversation-compaction-capture source)) source)) - (context (mission-request-view-context source)) - (failure nil) (results nil)) - (expanded-native-test--transport - application :status status :signaled-p signaled-p :function - (lambda () - (handler-case - (setf results - (multiple-value-list - (if (eq ownership ':detached) - (mission-call-with-native-compaction-view - context view - (lambda () - (provider-native-compact-conversation provider view :tool-namespaces #() - :event-callback #'identity))) - (provider-native-compact-conversation provider view :tool-namespaces #() - :event-callback #'identity)))) - (error (condition) (setf failure condition))))) - (format t "~&NATIVE-REPRESENTATION owner=~S failure=~S values=~S~%" - ownership (and failure (type-of failure)) results) - (test-assert (null failure) "An unavailable endpoint returns normally before mission supervision.") - (test-assert (null (first results)) "No opaque native checkpoint is fabricated.") - (when context - (let ((ledger (mission-context-goal context))) - (test-assert (zerop (getf ledger :requests-outstanding 0)) "Every native request settles.") - (test-assert (zerop (getf ledger :tokens-reserved 0)) "Every native reservation settles.") - (test-assert (zerop (getf ledger :unknown-usage 0)) "A definitive unavailable endpoint has known usage.") - (test-assert (zerop (getf ledger :tokens-used 0)) "A definitive unavailable endpoint charges no inference."))) - (when (eq ownership ':detached) - (test-assert (null (mission-context-find view)) "Detached request ownership is released.")))) - :owned-p (not (eq ownership ':unowned)))))) - nil) - -(-> test-expanded-native-unavailable-ordinary () null) -(defun test-expanded-native-unavailable-ordinary () - "Continue real large text-only proactive turns after every native unavailable status." - (dolist (status '(404 405 501)) - (dolist (owned-p '(nil t)) - (expanded-native-test--fixture - (lambda (application provider) - (let* ((agent (application-agent application)) - (conversation (application-conversation application)) - (failure nil) (result nil)) - (test-assert (agent-should-compact-p agent) "The real large-text estimate selects proactive compaction.") - (expanded-native-test--transport - application :status status :signaled-p t :function - (lambda () - (handler-case - (setf result (agent--run-provider-loop agent (make-instance 'agent-observer) :tools-p nil)) - (error (condition) (setf failure condition))))) - (format t "~&NATIVE-ORDINARY owner=~S status=~D failure=~S flags=~S~%" - owned-p status (and failure (type-of failure)) (expanded-native-compaction-flags provider)) - (test-assert (null failure) "Unavailable native compaction must not abort the ordinary turn.") - (test-assert (and result (equal "Resumed answer." (provider-result-assistant-text result))) - "The ordinary answer really continues after the portable summary.") - (test-assert (equal '(nil t) (expanded-native-compaction-flags provider)) - "One admitted summary and one admitted ordinary answer complete.") - (test-assert (find :summary (conversation-records-newest conversation 30) :key #'first) - "The portable checkpoint is durably published.") - (let ((loaded (conversation-load-by-id (application-configuration application) - (conversation-identifier conversation)))) - (test-assert (equal (mapcar #'json-encode (conversation-input-items conversation)) - (mapcar #'json-encode (conversation-input-items loaded))) - "Reload preserves the actual published projection.")) - (when owned-p - (let ((ledger (application-goal application))) - (test-assert (= 180 (getf ledger :tokens-used 0)) "Positive summary and answer usage settle once.") - (test-assert (zerop (getf ledger :unknown-usage 0)) "Unavailable native usage is not quarantined.") - (test-assert (zerop (getf ledger :requests-outstanding 0)) "No request remains outstanding.") - (test-assert (zerop (getf ledger :tokens-reserved 0)) "No allocation remains reserved."))))) - :owned-p owned-p :large-p t))) +(-> test-compaction-owner-proactive () null) +(defun test-compaction-owner-proactive () + "Compact a real large text-only conversation ahead of a proactive turn, then answer." + (dolist (owned-p '(nil t)) + (compaction-owner-test--fixture + (lambda (application provider) + (let* ((agent (application-agent application)) + (conversation (application-conversation application)) + (failure nil) (result nil)) + (test-assert (agent-should-compact-p agent) "The real large-text estimate selects proactive compaction.") + (compaction-owner-test--call-with-credentials + application + (lambda () + (handler-case + (setf result (agent--run-provider-loop agent (make-instance 'agent-observer) :tools-p nil)) + (error (condition) (setf failure condition))))) + (format t "~&OWNER-PROACTIVE owner=~S failure=~S flags=~S~%" + owned-p (and failure (type-of failure)) (compaction-owner-compaction-flags provider)) + (test-assert (null failure) "Compaction must not abort the ordinary turn.") + (test-assert (and result (equal "Resumed answer." (provider-result-assistant-text result))) + "The ordinary answer really continues after the checkpoint.") + (test-assert (equal '(nil t) (compaction-owner-compaction-flags provider)) + "One admitted summary and one admitted ordinary answer complete.") + (test-assert (find :summary (conversation-records-newest conversation 30) :key #'first) + "The checkpoint is durably published.") + (let ((loaded (conversation-load-by-id (application-configuration application) + (conversation-identifier conversation)))) + (test-assert (equal (mapcar #'json-encode (conversation-input-items conversation)) + (mapcar #'json-encode (conversation-input-items loaded))) + "Reload preserves the actual published projection.")) + (when owned-p + (let ((ledger (application-goal application))) + (test-assert (= 180 (getf ledger :tokens-used 0)) "Positive summary and answer usage settle once.") + (test-assert (zerop (getf ledger :unknown-usage 0)) "No usage is quarantined.") + (test-assert (zerop (getf ledger :requests-outstanding 0)) "No request remains outstanding.") + (test-assert (zerop (getf ledger :tokens-reserved 0)) "No allocation remains reserved."))))) + :owned-p owned-p :large-p t)) nil) -;;;; -- Native HTTP Sibling Lifecycles -- +;;;; -- Product Owner Lifecycles -- -(-> expanded-native-test--settled (application integer) null) -(defun expanded-native-test--settled (application tokens) +(-> compaction-owner-test--settled (application integer) null) +(defun compaction-owner-test--settled (application tokens) "Check the actual owner ledger after all admitted work has settled." (let ((goal (application-goal application))) (test-assert (= tokens (getf goal :tokens-used 0)) "Only completed positive paid usage is charged.") - (test-assert (eq :active (getf goal :status)) "A definitive unavailable endpoint preserves the active mission.") + (test-assert (eq :active (getf goal :status)) "The mission stays active.") (dolist (key '(:requests-outstanding :tokens-reserved :unknown-usage)) (test-assert (zerop (getf goal key 0)) (format nil "No residual ~A." key)))) nil) -(-> expanded-native-test--checkpoint (application) null) -(defun expanded-native-test--checkpoint (application) +(-> compaction-owner-test--checkpoint (application) null) +(defun compaction-owner-test--checkpoint (application) "Verify publication from authoritative disk, independently of the active projection." (let ((loaded (conversation-load (conversation-pathname (application-conversation application))))) (test-assert (find :summary (conversation-records-newest loaded 30) :key #'first) - "The portable summary survives canonical reload.")) + "The checkpoint survives canonical reload.")) nil) -(-> test-expanded-native-unavailable-manual () null) -(defun test-expanded-native-unavailable-manual () +(-> test-compaction-owner-manual () null) +(defun test-compaction-owner-manual () "Use manual application compaction with and without a mission owner." - (dolist (status '(404 405 501)) - (dolist (owned-p '(nil t)) - (expanded-native-test--fixture - (lambda (application provider) - (let ((failure nil)) - (expanded-native-test--transport - application :status status :signaled-p t :function - (lambda () - (handler-case (application-compact application) - (error (condition) (setf failure condition))))) - (format t "~&NATIVE-MANUAL status=~D owned=~S failure=~S~%" status owned-p (type-of failure)) - (test-assert (null failure) "Manual compaction returns normally after portable fallback.") - (test-assert (equal '(t) (expanded-native-compaction-flags provider)) "Manual compaction admits one paid summary.") - (expanded-native-test--checkpoint application) - (when owned-p (expanded-native-test--settled application 150)))) - :owned-p owned-p :large-p t))) + (dolist (owned-p '(nil t)) + (compaction-owner-test--fixture + (lambda (application provider) + (let ((failure nil)) + (compaction-owner-test--call-with-credentials + application + (lambda () + (handler-case (application-compact application) + (error (condition) (setf failure condition))))) + (format t "~&OWNER-MANUAL owned=~S failure=~S~%" owned-p (type-of failure)) + (test-assert (null failure) "Manual compaction returns normally.") + (test-assert (equal '(t) (compaction-owner-compaction-flags provider)) "Manual compaction admits one paid summary.") + (compaction-owner-test--checkpoint application) + (when owned-p (compaction-owner-test--settled application 150)))) + :owned-p owned-p :large-p t)) nil) -(defclass expanded-native-output-observer (agent-observer) - ((output-limit :initform 200000 :accessor expanded-native-output-limit +(defclass compaction-owner-output-observer (agent-observer) + ((output-limit :initform 200000 :accessor compaction-owner-output-limit :documentation "One initial ordinary output allowance, consumed at request start.")) (:documentation "A legitimate initial output allowance that unwinds before reactive compaction.")) (defmethod agent-observer-status - ((observer expanded-native-output-observer) (status symbol) details) + ((observer compaction-owner-output-observer) (status symbol) details) "Request a large output allowance once, using the ordinary observer protocol." (declare (ignore details)) (when (eq status :provider-request-started) - (shiftf (expanded-native-output-limit observer) nil))) + (shiftf (compaction-owner-output-limit observer) nil))) -(-> test-expanded-native-unavailable-reactive () null) -(defun test-expanded-native-unavailable-reactive () +(-> test-compaction-owner-reactive () null) +(defun test-compaction-owner-reactive () "Reject the initial wire envelope with a genuine output allowance, then recover." - (dolist (status '(404 405 501)) - (expanded-native-test--fixture - (lambda (application provider) - (let* ((conversation (application-conversation application)) - (agent (application-agent application)) (failure nil) - (observer (make-instance 'expanded-native-output-observer))) - (conversation-append-provider-item conversation (agent-test-message (make-string 400000 :initial-element #\h))) - (conversation-append-user-message conversation "Continue the real text work.") - (test-assert (not (agent-should-compact-p agent)) "The text conversation is below the real soft threshold.") - (expanded-native-test--transport - application :status status :signaled-p t :function - (lambda () - (handler-case (agent--run-provider-loop agent observer :tools-p nil) - (error (condition) (setf failure condition))))) - (format t "~&NATIVE-REACTIVE status=~D failure=~S flags=~S~%" status (type-of failure) - (expanded-native-compaction-flags provider)) - (test-assert (null failure) "Final admission recovery reaches portable summary and resumed answer.") - (test-assert (equal '(nil t) (expanded-native-compaction-flags provider)) "The rejected ordinary wire request is never paid.") - (expanded-native-test--checkpoint application) - (expanded-native-test--settled application 180))) - :owned-p t)) + (compaction-owner-test--fixture + (lambda (application provider) + (let* ((conversation (application-conversation application)) + (agent (application-agent application)) (failure nil) + (observer (make-instance 'compaction-owner-output-observer))) + (conversation-append-provider-item conversation (agent-test-message (make-string 400000 :initial-element #\h))) + (conversation-append-user-message conversation "Continue the real text work.") + (test-assert (not (agent-should-compact-p agent)) "The text conversation is below the real soft threshold.") + (compaction-owner-test--call-with-credentials + application + (lambda () + (handler-case (agent--run-provider-loop agent observer :tools-p nil) + (error (condition) (setf failure condition))))) + (format t "~&OWNER-REACTIVE failure=~S flags=~S~%" (type-of failure) + (compaction-owner-compaction-flags provider)) + (test-assert (null failure) "Final admission recovery reaches the checkpoint and the resumed answer.") + (test-assert (equal '(nil t) (compaction-owner-compaction-flags provider)) "The rejected ordinary wire request is never paid.") + (compaction-owner-test--checkpoint application) + (compaction-owner-test--settled application 180))) + :owned-p t) nil) -(-> test-expanded-native-unavailable-schedule () null) -(defun test-expanded-native-unavailable-schedule () +(-> test-compaction-owner-schedule () null) +(defun test-compaction-owner-schedule () "Consume a genuine claimed occurrence through the default mission runner." - (dolist (status '(404 405 501)) - (expanded-native-test--fixture - (lambda (application provider) - (let* ((now 100) - (service (mission-schedule-service-create application :clock (lambda () now) - :enqueue (lambda (owner command) (declare (ignore owner command)) t)))) - (setf (gethash application *mission-schedule-services*) service) - (application-mission-schedule-add application :id "native" :content "Inspect scheduled work." - :at 100 :interval 10) - (let* ((occurrence (first (application-mission-schedules-wake application))) - (ticket (mission-schedule-tests--ticket occurrence)) (failure nil)) - (test-assert (eq :claimed (getf occurrence :state)) "The real scheduler persists a claimed ticket before execution.") - (expanded-native-test--transport application :status status :signaled-p t :function - (lambda () - (handler-case (application-operation-call application "mission-wakeup" ticket) - (error (condition) (setf failure condition))))) - (format t "~&NATIVE-SCHEDULE status=~D failure=~S~%" status (type-of failure)) - (test-assert (null failure) "The default scheduled runner continues past unavailable native compaction.") - (let ((wakeups (getf (cl-jobpond:scheduler-snapshot (mission-schedule-scheduler service)) :wakeups))) - (test-assert (eq :completed (getf (first wakeups) :state)) "Only the completed answer marks the occurrence completed.")) - (let ((count (length (expanded-native-input-snapshots provider)))) - (handler-case (application-operation-call application "mission-wakeup" ticket) - (mission-error () nil)) - (test-assert (= count (length (expanded-native-input-snapshots provider))) "Reconsuming the claimed ticket issues no request.")) - (expanded-native-test--checkpoint application) - (expanded-native-test--settled application 180) - (setf now 110 - (expanded-native-results provider) - (list (agent-test-result "later" (list (agent-test-message "Later scheduled answer.")) - :usage (json-object "input_tokens" 20 "output_tokens" 10)))) - (let ((next (first (application-mission-schedules-wake application)))) - (test-assert next "The next recurring occurrence is independently eligible.") - (when next - (expanded-native-test--transport - application :status status :signaled-p t :expected-posts 0 - :function (lambda () - (application-operation-call application "mission-wakeup" (mission-schedule-tests--ticket next)))) - (test-assert (= 3 (length (expanded-native-input-snapshots provider))) "A later recurrence actually answers.")))))) - :owned-p t :large-p t)) + (compaction-owner-test--fixture + (lambda (application provider) + (let* ((now 100) + (service (mission-schedule-service-create application :clock (lambda () now) + :enqueue (lambda (owner command) (declare (ignore owner command)) t)))) + (setf (gethash application *mission-schedule-services*) service) + (application-mission-schedule-add application :id "owner" :content "Inspect scheduled work." + :at 100 :interval 10) + (let* ((occurrence (first (application-mission-schedules-wake application))) + (ticket (mission-schedule-tests--ticket occurrence)) (failure nil)) + (test-assert (eq :claimed (getf occurrence :state)) "The real scheduler persists a claimed ticket before execution.") + (compaction-owner-test--call-with-credentials application + (lambda () + (handler-case (application-operation-call application "mission-wakeup" ticket) + (error (condition) (setf failure condition))))) + (format t "~&OWNER-SCHEDULE failure=~S~%" (type-of failure)) + (test-assert (null failure) "The default scheduled runner continues past compaction.") + (let ((wakeups (getf (cl-jobpond:scheduler-snapshot (mission-schedule-scheduler service)) :wakeups))) + (test-assert (eq :completed (getf (first wakeups) :state)) "Only the completed answer marks the occurrence completed.")) + (let ((count (length (compaction-owner-input-snapshots provider)))) + (handler-case (application-operation-call application "mission-wakeup" ticket) + (mission-error () nil)) + (test-assert (= count (length (compaction-owner-input-snapshots provider))) "Reconsuming the claimed ticket issues no request.")) + (compaction-owner-test--checkpoint application) + (compaction-owner-test--settled application 180) + (setf now 110 + (compaction-owner-results provider) + (list (agent-test-result "later" (list (agent-test-message "Later scheduled answer.")) + :usage (json-object "input_tokens" 20 "output_tokens" 10)))) + (let ((next (first (application-mission-schedules-wake application)))) + (test-assert next "The next recurring occurrence is independently eligible.") + (when next + (compaction-owner-test--call-with-credentials + application (lambda () + (application-operation-call application "mission-wakeup" (mission-schedule-tests--ticket next)))) + (test-assert (= 3 (length (compaction-owner-input-snapshots provider))) "A later recurrence actually answers.")))))) + :owned-p t :large-p t) nil) -(-> test-expanded-native-unavailable-terminal-idle () null) -(defun test-expanded-native-unavailable-terminal-idle () +(-> test-compaction-owner-terminal-idle () null) +(defun test-compaction-owner-terminal-idle () "Run due terminal episodes with and without mission ownership, then accept later input." - (dolist (status '(404 405 501)) - (dolist (owned-p '(nil t)) - (expanded-native-test--fixture - (lambda (application provider) - (let* ((controller (make-instance 'application-input-controller :application application :main-thread (current-thread))) - (*idle-compaction-clock-function* (lambda () 1180))) - (change-class (terminal-ui-terminal (application-ui application)) 'waiting-recording-terminal) - (setf (application-input-controller application) controller - (config :prompt-cache-retention-seconds (application-configuration application)) 300) - (idle-compaction-tests--prime application) - (let ((work (application-input-controller--next-work controller))) - (test-assert (eq :idle-compaction (first work)) "A due real episode selects idle work.") - (expanded-native-test--transport application :status status :signaled-p t :function - (lambda () (application-input-controller--run-work controller work))) - (application-input-controller--finish-work controller) - (expanded-native-test--checkpoint application) - (test-assert (null (application-idle-compaction-episode application)) "The idle opportunity is consumed once.") - (test-assert (not (application-input-controller-active-p controller)) "The terminal controller releases activity.") - (test-assert (equal '(t) (expanded-native-compaction-flags provider)) "Idle maintenance completes one paid summary.") - (when owned-p (expanded-native-test--settled application 150)) - (expanded-native-test--transport - application :status status :signaled-p t :expected-posts 0 - :function (lambda () - (application--run-message-input application "Later actual user input." :tools-p nil :goal-continuations-p nil))) - (test-assert (equal '(nil t) (expanded-native-compaction-flags provider)) "Later input gets its ordinary answer.") - (when owned-p (expanded-native-test--settled application 180))) - (setf (application-input-controller application) nil))) - :owned-p owned-p :large-p t))) + (dolist (owned-p '(nil t)) + (compaction-owner-test--fixture + (lambda (application provider) + (let* ((controller (make-instance 'application-input-controller :application application :main-thread (current-thread))) + (*idle-compaction-clock-function* (lambda () 1180))) + (change-class (terminal-ui-terminal (application-ui application)) 'waiting-recording-terminal) + (setf (application-input-controller application) controller + (config :prompt-cache-retention-seconds (application-configuration application)) 300) + (idle-compaction-tests--prime application) + (let ((work (application-input-controller--next-work controller))) + (test-assert (eq :idle-compaction (first work)) "A due real episode selects idle work.") + (compaction-owner-test--call-with-credentials application + (lambda () (application-input-controller--run-work controller work))) + (application-input-controller--finish-work controller) + (compaction-owner-test--checkpoint application) + (test-assert (null (application-idle-compaction-episode application)) "The idle opportunity is consumed once.") + (test-assert (not (application-input-controller-active-p controller)) "The terminal controller releases activity.") + (test-assert (equal '(t) (compaction-owner-compaction-flags provider)) "Idle maintenance completes one paid summary.") + (when owned-p (compaction-owner-test--settled application 150)) + (compaction-owner-test--call-with-credentials + application (lambda () + (application--run-message-input application "Later actual user input." :tools-p nil :goal-continuations-p nil))) + (test-assert (equal '(nil t) (compaction-owner-compaction-flags provider)) "Later input gets its ordinary answer.") + (when owned-p (compaction-owner-test--settled application 180))) + (setf (application-input-controller application) nil))) + :owned-p owned-p :large-p t)) nil) -(-> test-expanded-native-unavailable-acp-idle () null) -(defun test-expanded-native-unavailable-acp-idle () +(-> test-compaction-owner-acp-idle () null) +(defun test-compaction-owner-acp-idle () "Use the shared idle owner and channel-to-ACP outcome adapter, then accept later input." - (dolist (status '(404 405 501)) - (dolist (owned-p '(nil t)) - (expanded-native-test--matrix-checks - (expanded-native-test--matrix-snapshot :entry :acp-idle :status status - :signaled-p t :owned-p owned-p :reuse-p t)))) + (dolist (owned-p '(nil t)) + (compaction-owner-test--matrix-checks + (compaction-owner-test--matrix-snapshot :entry :acp-idle :owned-p owned-p :reuse-p t))) nil) -;;;; -- Unpublishable Portable Compaction -- +;;;; -- Unpublishable Compaction -- -(-> expanded-native-test--no-fit - (application expanded-native-test-provider keyword +(-> compaction-owner-test--no-fit + (application compaction-owner-test-provider keyword &key (:entry keyword) (:session t) (:client t)) null) -(defun expanded-native-test--no-fit (application provider phase &key entry session client) +(defun compaction-owner-test--no-fit (application provider phase &key entry session client) "Exercise pre-dispatch and postpaid refusal through shared channel owners." (declare (ignore client)) (setf (config :compaction-threshold-percent (application-configuration application)) 35) @@ -1432,18 +1200,16 @@ (channel-subscription-submit session "Actual queued ACP input." :source "queue") (channel-subscription-submit session "Remaining queued ACP input." :source "queue")) (when (eq entry :acp-completion) - (setf completion-id (expanded-native-test--ready-completion application))) + (setf completion-id (compaction-owner-test--ready-completion application))) (test-call-with-function-replacements (list (list 'agent--note-compaction-request (lambda (agent observer &rest arguments) (multiple-value-prog1 (apply note-original agent observer arguments) - (when (and arrival (eq :summary (getf arguments :kind))) + (when arrival (funcall (shiftf arrival nil))))))) (lambda () - (expanded-native-test--transport - application :status 404 :signaled-p (eq phase :after) - :expected-posts (if (eq phase :after) 1 0) - :function + (compaction-owner-test--call-with-credentials + application (lambda () (handler-case (setf result @@ -1459,7 +1225,7 @@ (:wakeup (application-operation-call application "mission-wakeup" ticket)) ((:terminal :acp :acp-queue :acp-completion) - (expanded-native-test--run-next controller)) + (compaction-owner-test--run-next controller)) (:acp-prompt (acp-session-prompt session "Actual explicit ACP input.")))) (serious-condition (condition) (setf escaped condition))))))) @@ -1475,9 +1241,9 @@ "Reload preserves the original history and every concurrent arrival.") (when completion-id (test-assert (task-completion--delivered-p loaded completion-id) "The claimed job is delivered durably once.")) - (test-assert (= (if (eq phase :after) 1 0) (length (expanded-native-input-snapshots provider))) + (test-assert (= (if (eq phase :after) 1 0) (length (compaction-owner-input-snapshots provider))) "Only an admitted summary incurs usage.") - (expanded-native-test--settled application (if (eq phase :after) 150 0)) + (compaction-owner-test--settled application (if (eq phase :after) 150 0)) (when (eq entry :acp-prompt) (test-assert (eq :end-turn result) "A local refusal completes the ACP submission without -32603.")) (when (eq entry :turn) @@ -1489,9 +1255,9 @@ (test-assert (zerop (hash-table-count (mission-schedule-executing service))) "Wakeup execution ownership is released.") (test-assert (eq :failed (getf (first (getf (cl-jobpond:scheduler-snapshot (mission-schedule-scheduler restored)) :wakeups)) :state)) "Reload retains the terminal wakeup refusal.") - (let ((count (length (expanded-native-input-snapshots provider)))) + (let ((count (length (compaction-owner-input-snapshots provider)))) (handler-case (application-operation-call application "mission-wakeup" ticket) (mission-error () nil)) - (test-assert (= count (length (expanded-native-input-snapshots provider))) "A settled wakeup cannot repeat paid work."))))) + (test-assert (= count (length (compaction-owner-input-snapshots provider))) "A settled wakeup cannot repeat paid work."))))) (when (eq entry :continuation) (test-assert (= 1 (getf (application-goal application) :continuations 0)) "Refusal stops after one continuation.")) (when (member entry '(:terminal :acp)) @@ -1502,40 +1268,40 @@ (unless session (setf (application-input-controller application) nil))) nil) -(-> test-expanded-native-no-fit-product-boundaries () null) -(defun test-expanded-native-no-fit-product-boundaries () - "Check pre-dispatch exhaustion and true unavailable-HTTP post-paid refusal." +(-> test-compaction-owner-no-fit-product-boundaries () null) +(defun test-compaction-owner-no-fit-product-boundaries () + "Check pre-dispatch exhaustion and post-paid refusal." (dolist (phase '(:before :after)) (dolist (entry '(:manual :turn :message :continuation :wakeup :terminal)) - (expanded-native-test--fixture - (lambda (application provider) (expanded-native-test--no-fit application provider phase :entry entry)) + (compaction-owner-test--fixture + (lambda (application provider) (compaction-owner-test--no-fit application provider phase :entry entry)) :owned-p t))) nil) -(-> test-expanded-native-no-fit-acp () null) -(defun test-expanded-native-no-fit-acp () +(-> test-compaction-owner-no-fit-acp () null) +(defun test-compaction-owner-no-fit-acp () "Explain both no-fit phases through the shared channel idle owner." (dolist (phase '(:before :after)) - (expanded-native-test--matrix-fixture + (compaction-owner-test--matrix-fixture (lambda (application provider session events) (declare (ignore events)) - (expanded-native-test--no-fit application provider phase :entry :acp :session session)) + (compaction-owner-test--no-fit application provider phase :entry :acp :session session)) :acp-p t :owned-p t)) nil) ;;;; -- ACP Prompt and Continuation Refusals -- -(-> expanded-native-test--acp-turn-refusal (keyword) null) -(defun expanded-native-test--acp-turn-refusal (entry) +(-> compaction-owner-test--acp-turn-refusal (keyword) null) +(defun compaction-owner-test--acp-turn-refusal (entry) "Finalize actual channel queue and job owners, and the ACP prompt adapter, once per refusal." (dolist (phase '(:before :after)) - (expanded-native-test--matrix-fixture + (compaction-owner-test--matrix-fixture (lambda (application provider session events) (let ((agent (application-agent application))) (unwind-protect (progn - (expanded-native-test--no-fit application provider phase :entry entry :session session) + (compaction-owner-test--no-fit application provider phase :entry entry :session session) (test-assert (= 1 (count :work-finished (first events) :key #'first)) "Refusal publishes exactly one terminal channel work event.") (test-assert (null (acp-session-turn session)) "The ACP turn closes once.") @@ -1548,73 +1314,33 @@ :acp-p t :owned-p t)) nil) -(-> test-expanded-native-no-fit-acp-prompt () null) -(defun test-expanded-native-no-fit-acp-prompt () +(-> test-compaction-owner-no-fit-acp-prompt () null) +(defun test-compaction-owner-no-fit-acp-prompt () "Return a plain protocol refusal for real explicit ACP prompts." - (expanded-native-test--acp-turn-refusal :acp-prompt) - (expanded-native-test--wire-refusal)) + (compaction-owner-test--acp-turn-refusal :acp-prompt) + (compaction-owner-test--wire-refusal)) -(-> test-expanded-native-no-fit-acp-queue () null) -(defun test-expanded-native-no-fit-acp-queue () - "Consume queued ACP input once when portable recovery cannot fit." - (expanded-native-test--acp-turn-refusal :acp-queue)) +(-> test-compaction-owner-no-fit-acp-queue () null) +(defun test-compaction-owner-no-fit-acp-queue () + "Consume queued ACP input once when recovery cannot fit." + (compaction-owner-test--acp-turn-refusal :acp-queue)) -(-> test-expanded-native-no-fit-acp-completion () null) -(defun test-expanded-native-no-fit-acp-completion () +(-> test-compaction-owner-no-fit-acp-completion () null) +(defun test-compaction-owner-no-fit-acp-completion () "Deliver a real job once without crashing its automatic ACP continuation." - (expanded-native-test--acp-turn-refusal :acp-completion)) - -(-> test-expanded-native-stream-fixture () null) -(defun test-expanded-native-stream-fixture () - "Independently qualify native success and the production stream event decoder." - (expanded-native-test--fixture - (lambda (application provider) - (let ((configuration (application-configuration application)) - (conversation (application-conversation application)) - (posts 0)) - (test-call-with-function-replacements - (list (list 'credential-manager-credentials - (lambda (&rest arguments) - (declare (ignore arguments)) - (provider-tests--credentials configuration))) - (list 'dexador:post - (lambda (endpoint &rest arguments) - (declare (ignore arguments)) - (incf posts) - (test-assert (search "/responses/compact" endpoint) "Native success uses the real native POST.") - (values (json-encode - (json-object "id" "native-fixture" "output" - (json-array (json-object "type" "compaction" "encrypted_content" "synthetic")) - "usage" (json-object "input_tokens" 1 "output_tokens" 1))) - 200 nil)))) - (lambda () - (let ((window (provider-native-compact-conversation provider conversation - :tool-namespaces #() :event-callback #'identity))) - (test-assert window "A genuine native success decodes a complete native window.")) - (let* ((*telemetry-request-role* :compaction) - (result (provider-stream-turn provider conversation :tool-namespaces #() - :event-callback #'identity :compaction-p t))) - (test-assert (equal "summary" (provider-result-response-id result)) "Production decoding returns the completed response identity.") - (test-assert (equal (provider-result-assistant-text result) - (provider-result-assistant-text (agent-test-result "expected" (list (agent-test-checkpoint-call))))) - "Production decoding reconstructs the complete structured summary.") - (test-assert (= 100 (json-get (provider-result-usage result) "input_tokens")) "Production decoding retains metered input usage.") - (test-assert (= 50 (json-get (provider-result-usage result) "output_tokens")) "Production decoding retains metered output usage.")))) - (test-assert (= 1 posts) "Fixture qualification admits one real native POST.") - (test-assert (equal '(t) (expanded-native-compaction-flags provider)) "Fixture qualification admits one production summary stream.")))) - nil) + (compaction-owner-test--acp-turn-refusal :acp-completion)) ;;;; -- Paid Summary Arrival and Terminal Ownership -- -(-> expanded-native-test--owner-snapshot - (&key (:entry keyword) (:owned-p boolean) (:status integer) - (:signaled-p boolean) (:arrival-size (option (integer 1))) (:reuse-p boolean) (:controller-p boolean) (:one-shot-p boolean)) list) -(defun expanded-native-test--owner-snapshot - (&key entry owned-p status signaled-p arrival-size reuse-p controller-p one-shot-p) +(-> compaction-owner-test--owner-snapshot + (&key (:entry keyword) (:owned-p boolean) (:arrival-size (option (integer 1))) + (:reuse-p boolean) (:controller-p boolean) (:one-shot-p boolean)) list) +(defun compaction-owner-test--owner-snapshot + (&key entry owned-p arrival-size reuse-p controller-p one-shot-p) "Observe real owning work and append a canonical arrival after original decoded usage persistence." (let ((snapshot nil)) - (expanded-native-test--fixture + (compaction-owner-test--fixture (lambda (application provider) (let* ((conversation (application-conversation application)) (configuration (application-configuration application)) @@ -1624,7 +1350,7 @@ (application-input-controller-create application :load-pending-p nil :start-reader-p nil) (make-instance 'application-input-controller :application application :main-thread (current-thread)))) - (events nil) (posts 0) (summaries 0) (failure nil) (result nil) + (events nil) (summaries 0) (failure nil) (result nil) (ticket nil) (service nil) (reuse-failure nil) (reuse-result nil)) (change-class (terminal-ui-terminal (application-ui application)) 'waiting-recording-terminal) (when (or controller-p (eq entry :terminal-message)) @@ -1645,45 +1371,25 @@ (lambda (&rest arguments) (declare (ignore arguments)) (provider-tests--credentials configuration))) - (list 'dexador:post - (lambda (endpoint &rest arguments) - (declare (ignore arguments)) - (test-assert (search "/responses/compact" endpoint) "The actual native POST is used.") - (incf posts) - (push (list :native-post status signaled-p) events) - (cond - (signaled-p - (error 'http-request-failed :status status :body "Native endpoint unavailable." - :headers nil :uri endpoint :method ':post)) - ((= status 200) - (values (json-encode - (json-object "id" "native-control" "output" - (json-array (json-object "type" "compaction" - "encrypted_content" "synthetic")) - "usage" (json-object "input_tokens" 1 "output_tokens" 1))) - 200 nil)) - (t - (values "Native endpoint unavailable." status nil))))) (list 'agent--note-compaction-request (lambda (agent observer &rest arguments) (multiple-value-prog1 (apply note-original agent observer arguments) (let ((usage (provider-usage-normalize (getf arguments :usage)))) - (push (list :usage-persisted (getf arguments :kind) + (push (list :usage-persisted :summary (getf arguments :request-number) (json-get usage "input_tokens") (json-get usage "output_tokens")) events)) - (when (eq :summary (getf arguments :kind)) - (incf summaries) - (when (and arrival-size (= summaries 1)) - (conversation-append-user-message - conversation (make-string arrival-size :initial-element #\n)) - (push (list :canonical-arrival arrival-size) events)))))) + (incf summaries) + (when (and arrival-size (= summaries 1)) + (conversation-append-user-message + conversation (make-string arrival-size :initial-element #\n)) + (push (list :canonical-arrival arrival-size) events))))) (list 'agent--validate-compaction-projection (lambda (&rest arguments) (push (list :validation-start) events) (handler-bind ((provider-context-budget-error (lambda (condition) - (expanded-native-test--refusal-budget condition) + (compaction-owner-test--refusal-budget condition) (push (list :validation-refusal (type-of condition)) events)))) (multiple-value-prog1 (apply fit-original arguments) (push (list :validation-returned) events)))))) @@ -1724,15 +1430,12 @@ (setf snapshot (list :entry entry :owned-p owned-p - :status status - :signaled-p signaled-p :arrival-size arrival-size - :posts posts :summaries summaries :events (reverse events) :failure (and failure (type-of failure)) :result (and (keywordp result) result) - :flags (copy-list (expanded-native-compaction-flags provider)) + :flags (copy-list (compaction-owner-compaction-flags provider)) :checkpoint-p (not (null (find :summary records :key #'first))) :aborted-p (not (null (find :turn-aborted records :key #'first))) :notice-p @@ -1758,7 +1461,7 @@ :prompt-status (application-input-controller-prompt-marker-status controller) :input-count (count "Actual terminal user work." (conversation-input-history loaded) :test #'equal)))) (when reuse-p - (setf (expanded-native-results provider) + (setf (compaction-owner-results provider) (list (agent-test-result "reuse-summary" (list (agent-test-checkpoint-call)) :usage (json-object "input_tokens" 100 "output_tokens" 50)) (agent-test-result "reuse-answer" (list (agent-test-message "Later actual answer.")) @@ -1770,7 +1473,7 @@ (setf reuse-failure condition))) (setf (getf snapshot :reuse-failure) (and reuse-failure (type-of reuse-failure)) (getf snapshot :reuse-result) reuse-result - (getf snapshot :reuse-flags) (copy-list (expanded-native-compaction-flags provider)) + (getf snapshot :reuse-flags) (copy-list (compaction-owner-compaction-flags provider)) (getf snapshot :reuse-input-preserved-p) (not (null (member "Later explicit user work." (conversation-input-history conversation) :test #'equal))))))) (setf (getf snapshot :restored-p) @@ -1780,13 +1483,12 @@ :owned-p owned-p :large-p t) snapshot)) -(-> expanded-native-test--paid-snapshot-checks (list) null) -(defun expanded-native-test--paid-snapshot-checks (snapshot) +(-> compaction-owner-test--paid-snapshot-checks (list) null) +(defun compaction-owner-test--paid-snapshot-checks (snapshot) "Check genuine paid completion, canonical arrival and original final guard in order." (let* ((events (getf snapshot :events)) (kinds (mapcar #'first events)) (usage (find :summary events :key #'second))) - (test-assert (= 1 (getf snapshot :posts)) "One genuine unavailable native POST precedes portable recovery.") (test-assert (= 1 (getf snapshot :summaries)) "The production decoder completes exactly one summary.") (test-assert (and usage (= 100 (fourth usage)) (= 50 (fifth usage))) "The original post-decoder event persists metered input100/output50.") @@ -1804,13 +1506,13 @@ (test-assert (zerop (getf snapshot key)) (format nil "The genuine refusal releases ~A." key))))) nil) -(-> test-expanded-native-paid-arrival-fixture () null) -(defun test-expanded-native-paid-arrival-fixture () - "Qualify original-once usage interleaving with native success and portable arrival controls." - (dolist (cell '((200 nil) (404 nil) (404 64))) - (let ((snapshot (expanded-native-test--owner-snapshot - :entry ':manual :owned-p t :status (first cell) :arrival-size (second cell)))) - (test-assert (null (getf snapshot :failure)) "A fitting native or portable control returns normally.") +(-> test-compaction-owner-paid-arrival-fixture () null) +(defun test-compaction-owner-paid-arrival-fixture () + "Qualify original-once usage interleaving with and without a small arrival." + (dolist (arrival-size '(nil 64)) + (let ((snapshot (compaction-owner-test--owner-snapshot + :entry ':manual :owned-p t :arrival-size arrival-size))) + (test-assert (null (getf snapshot :failure)) "A fitting control returns normally.") (test-assert (getf snapshot :checkpoint-p) "Fitting controls publish a real validated checkpoint.") (test-assert (= 1 (getf snapshot :summaries)) "The original stream and decoder complete one summary.") (test-assert (getf snapshot :arrival-preserved-p) "The small concurrent arrival is canonical.") @@ -1819,50 +1521,45 @@ (test-assert (getf snapshot :restored-p) "Qualification restores both original functions."))) nil) -(-> test-expanded-native-paid-owner-refusals () null) -(defun test-expanded-native-paid-owner-refusals () +(-> test-compaction-owner-paid-refusals () null) +(defun test-compaction-owner-paid-refusals () "Reject actual post-paid unpublishable projections without aborting manual or scheduled owners." (dolist (entry '(:manual :terminal-message :wakeup)) - (dolist (status '(404 405 501)) - (let ((snapshot (expanded-native-test--owner-snapshot - :entry entry :owned-p t :status status :arrival-size 600000))) - (expanded-native-test--paid-snapshot-checks snapshot) - (test-assert (null (getf snapshot :failure)) "The real owner presents an explained refusal normally.") - (test-assert (not (getf snapshot :aborted-p)) "A definitive fit refusal is not a crashed turn.") - (test-assert (getf snapshot :notice-p) "The owner durably explains the context refusal.") - (when (eq entry :wakeup) - (test-assert (eq :failed (getf snapshot :wakeup-state)) "A refused occurrence is failed, not executed or unknown.") - (test-assert (eq :runner-failed (getf snapshot :wakeup-result)) "The default runner reports truthful failure.") - (test-assert (zerop (getf snapshot :executing)) "The real executing reservation is released."))))) + (let ((snapshot (compaction-owner-test--owner-snapshot + :entry entry :owned-p t :arrival-size 600000))) + (compaction-owner-test--paid-snapshot-checks snapshot) + (test-assert (null (getf snapshot :failure)) "The real owner presents an explained refusal normally.") + (test-assert (not (getf snapshot :aborted-p)) "A definitive fit refusal is not a crashed turn.") + (test-assert (getf snapshot :notice-p) "The owner durably explains the context refusal.") + (when (eq entry :wakeup) + (test-assert (eq :failed (getf snapshot :wakeup-state)) "A refused occurrence is failed, not executed or unknown.") + (test-assert (eq :runner-failed (getf snapshot :wakeup-result)) "The default runner reports truthful failure.") + (test-assert (zerop (getf snapshot :executing)) "The real executing reservation is released.")))) nil) -(-> test-expanded-native-terminal-message-owners () null) -(defun test-expanded-native-terminal-message-owners () - "Reach real submitted terminal message work for returned and typed unavailable statuses." - (dolist (status '(200 404 405 501)) - (dolist (signaled-p (if (= status 200) '(nil) '(nil t))) - (dolist (owned-p '(nil t)) - (let ((snapshot (expanded-native-test--owner-snapshot - :entry ':terminal-message :owned-p owned-p :status status :signaled-p signaled-p))) - (test-assert (= 1 (getf snapshot :posts)) "The message owner reaches the actual native POST.") - (test-assert (null (getf snapshot :failure)) "Unavailable native compaction does not escape the terminal owner.") - (test-assert (= 1 (getf snapshot :input-count)) "Real submitted user input is persisted exactly once.") - (test-assert (equal '(nil t) (getf snapshot :flags)) "One real summary and the original ordinary answer complete.") - (test-assert (getf snapshot :checkpoint-p) "The actual owner publishes the portable or native checkpoint.") - (test-assert (not (getf snapshot :aborted-p)) "The real message lifecycle never records a native-unavailable abort.") - (test-assert (not (getf snapshot :controller-active-p)) "The terminal work releases controller activity.") - (when owned-p - (test-assert (= (if (= status 200) 182 180) (getf snapshot :paid)) - "Only actual decoded native, summary and answer usage is charged.")))))) +(-> test-compaction-owner-terminal-messages () null) +(defun test-compaction-owner-terminal-messages () + "Reach real submitted terminal message work with and without a mission owner." + (dolist (owned-p '(nil t)) + (let ((snapshot (compaction-owner-test--owner-snapshot :entry ':terminal-message :owned-p owned-p))) + (test-assert (null (getf snapshot :failure)) "Compaction does not escape the terminal owner.") + (test-assert (= 1 (getf snapshot :input-count)) "Real submitted user input is persisted exactly once.") + (test-assert (equal '(nil t) (getf snapshot :flags)) "One real summary and the original ordinary answer complete.") + (test-assert (getf snapshot :checkpoint-p) "The actual owner publishes the checkpoint.") + (test-assert (not (getf snapshot :aborted-p)) "The real message lifecycle never records an abort.") + (test-assert (not (getf snapshot :controller-active-p)) "The terminal work releases controller activity.") + (when owned-p + (test-assert (= 180 (getf snapshot :paid)) + "Only actual decoded summary and answer usage is charged.")))) nil) ;;;; -- Actual ACP and Idle Owner Matrix -- -(-> expanded-native-test--matrix-fixture (function &key (:acp-p boolean) (:owned-p boolean)) null) -(defun expanded-native-test--matrix-fixture (function &key acp-p owned-p) +(-> compaction-owner-test--matrix-fixture (function &key (:acp-p boolean) (:owned-p boolean)) null) +(defun compaction-owner-test--matrix-fixture (function &key acp-p owned-p) "Run shared channel owners and the ACP outcome adapter with inert channel transport." - (expanded-native-test--fixture + (compaction-owner-test--fixture (lambda (application provider) (let* ((configuration (application-configuration application)) (controller (make-instance 'application-input-controller @@ -1921,7 +1618,7 @@ (user-message-input-create :text (getf arguments :text) :submission (getf arguments :submission))) (when (equal "prompt" (getf (getf arguments :submission) :source)) - (expanded-native-test--run-next controller)) + (compaction-owner-test--run-next controller)) (list :busy-p nil)) (:cancel (application-input-controller--request-active-turn-cancellation @@ -1931,14 +1628,14 @@ :owned-p owned-p :large-p t) nil) -(-> expanded-native-test--matrix-snapshot - (&key (:entry keyword) (:owned-p boolean) (:status integer) (:signaled-p boolean) - (:arrival-p boolean) (:image-p boolean) (:reuse-p boolean) (:cancel-before-refusal-p boolean)) list) -(defun expanded-native-test--matrix-snapshot - (&key entry owned-p status signaled-p arrival-p image-p reuse-p cancel-before-refusal-p) +(-> compaction-owner-test--matrix-snapshot + (&key (:entry keyword) (:owned-p boolean) (:arrival-p boolean) (:image-p boolean) + (:reuse-p boolean) (:cancel-before-refusal-p boolean)) list) +(defun compaction-owner-test--matrix-snapshot + (&key entry owned-p arrival-p image-p reuse-p cancel-before-refusal-p) "Observe shared channel work, durable recovery and ACP outcome translation without old ACP owners." (let ((snapshot nil)) - (expanded-native-test--matrix-fixture + (compaction-owner-test--matrix-fixture (lambda (application provider session wire) (let* ((conversation (application-conversation application)) (configuration (application-configuration application)) @@ -1946,8 +1643,8 @@ (note-original (symbol-function 'agent--note-compaction-request)) (fit-original (symbol-function 'agent--validate-compaction-projection)) (*idle-compaction-clock-function* (lambda () 1180)) - (posts 0) (summaries 0) (events nil) (failure nil) (result nil) (completion-id nil)) - (setf (expanded-native-results provider) + (summaries 0) (events nil) (failure nil) (result nil) (completion-id nil)) + (setf (compaction-owner-results provider) (list (agent-test-result "matrix-summary" (list (agent-test-checkpoint-call)) :usage (json-object "input_tokens" 100 "output_tokens" 50)) (agent-test-result "matrix-answer" (list (agent-test-message "Matrix ordinary answer.")) @@ -1964,44 +1661,28 @@ (channel-subscription-submit session "Matrix claimed queued input." :source "queue") (channel-subscription-submit session "Matrix unclaimed queued tail." :source "queue")) (when (eq entry :acp-completion) - (setf completion-id (expanded-native-test--ready-completion application))) + (setf completion-id (compaction-owner-test--ready-completion application))) (unwind-protect (test-call-with-function-replacements (list (list 'credential-manager-credentials (lambda (&rest arguments) (declare (ignore arguments)) (provider-tests--credentials configuration))) - (list 'dexador:post - (lambda (endpoint &rest arguments) - (declare (ignore arguments)) - (incf posts) - (test-assert (search "/responses/compact" endpoint) "Native admission reaches actual POST.") - (cond - (signaled-p - (error 'http-request-failed :status status :body "Native unavailable." - :headers nil :uri endpoint :method :post)) - ((= status 200) - (values (json-encode (json-object "id" "native-control" "output" - (json-array (json-object "type" "compaction" "encrypted_content" "synthetic")) - "usage" (json-object "input_tokens" 1 "output_tokens" 1))) 200 nil)) - (t - (values "Native unavailable." status nil))))) (list 'agent--note-compaction-request (lambda (agent observer &rest arguments) (multiple-value-prog1 (apply note-original agent observer arguments) (let ((usage (provider-usage-normalize (getf arguments :usage)))) - (push (list :usage-persisted (getf arguments :kind) (getf arguments :request-number) + (push (list :usage-persisted :summary (getf arguments :request-number) (json-get usage "input_tokens") (json-get usage "output_tokens")) events)) - (when (eq :summary (getf arguments :kind)) - (incf summaries) - (when (and arrival-p (= summaries 1)) - (conversation-append-user-message conversation (make-string 600000 :initial-element #\n)) - (push (list :canonical-arrival 600000) events)))))) + (incf summaries) + (when (and arrival-p (= summaries 1)) + (conversation-append-user-message conversation (make-string 600000 :initial-element #\n)) + (push (list :canonical-arrival 600000) events))))) (list 'agent--validate-compaction-projection (lambda (&rest arguments) (push (list :validation-start) events) (handler-bind ((provider-context-budget-error (lambda (condition) - (expanded-native-test--refusal-budget condition) + (compaction-owner-test--refusal-budget condition) (push (list :validation-refusal (type-of condition)) events) (when cancel-before-refusal-p (application-input-controller--request-active-turn-cancellation @@ -2011,16 +1692,16 @@ (handler-case (setf result (if (eq entry :acp-prompt) (acp-session-prompt session "Matrix actual client input.") - (expanded-native-test--run-next controller))) + (compaction-owner-test--run-next controller))) (serious-condition (condition) (setf failure condition))) (let* ((records (conversation-records-newest conversation 200)) (loaded (conversation-load (conversation-pathname conversation))) (goal (application-goal application))) (setf snapshot - (list :entry entry :owned-p owned-p :status status :signaled-p signaled-p - :arrival-size (when arrival-p 600000) :image-p image-p :posts posts :summaries summaries + (list :entry entry :owned-p owned-p + :arrival-size (when arrival-p 600000) :image-p image-p :summaries summaries :events (reverse events) :failure (and failure (type-of failure)) :result result - :flags (copy-list (expanded-native-compaction-flags provider)) + :flags (copy-list (compaction-owner-compaction-flags provider)) :checkpoint-p (not (null (find :summary records :key #'first))) :aborted-p (not (null (find :turn-aborted records :key #'first))) :notice-p (not (null (find-if (lambda (record) (and (eq :side-status (first record)) @@ -2054,10 +1735,10 @@ :automatic-work-p (or (application-input-controller-pending-work-p controller) (application-job-completions--take-work controller))))) (when reuse-p - (let ((before (length (expanded-native-input-snapshots provider))) + (let ((before (length (compaction-owner-input-snapshots provider))) (history (conversation-input-history conversation)) (later-failure nil) (later-result nil) (conditions nil)) - (setf (expanded-native-results provider) + (setf (compaction-owner-results provider) (list (agent-test-result "matrix-later-answer" (list (agent-test-message "Matrix later ordinary answer.")) :usage (json-object "input_tokens" 20 "output_tokens" 10)))) ;; Explicit input resumes paused work; automatic dispatch never clears this pause. @@ -2080,8 +1761,8 @@ (getf snapshot :later-failure-message) (and later-failure (princ-to-string later-failure)) (getf snapshot :later-result) later-result (getf snapshot :later-context-conditions) conditions - (getf snapshot :later-opens) (- (length (expanded-native-input-snapshots provider)) before) - (getf snapshot :later-role) (first (expanded-native-compaction-flags provider)) + (getf snapshot :later-opens) (- (length (compaction-owner-input-snapshots provider)) before) + (getf snapshot :later-role) (first (compaction-owner-compaction-flags provider)) (getf snapshot :later-answer-p) (test-object-contains-string-p (conversation-input-items conversation) "Matrix later ordinary answer.") (getf snapshot :prior-history-preserved-p) (equal history (subseq (conversation-input-history conversation) 0 (length history))) (getf snapshot :window) (config :context-window configuration) @@ -2097,10 +1778,9 @@ :acp-p (not (eq entry :terminal-idle)) :owned-p owned-p) snapshot)) -(-> expanded-native-test--matrix-checks (list) null) -(defun expanded-native-test--matrix-checks (snapshot) - "Assert shared owner cleanup, portable publication or local refusal and protocol outcomes." - (test-assert (= 1 (getf snapshot :posts)) "Every cell reaches the original native POST.") +(-> compaction-owner-test--matrix-checks (list) null) +(defun compaction-owner-test--matrix-checks (snapshot) + "Assert shared owner cleanup, publication or local refusal and protocol outcomes." (test-assert (null (getf snapshot :failure)) "A local refusal never escapes as a channel or ACP crash.") (test-assert (not (getf snapshot :aborted-p)) "A local refusal is not recorded as a crashed model turn.") (test-assert (getf snapshot :restored-p) "Original instrumentation bindings are restored.") @@ -2111,19 +1791,12 @@ (test-assert (eq :end-turn (getf snapshot :protocol-outcome)) "A local refusal is a normal ACP outcome, never -32603.")) (if (getf snapshot :arrival-size) (progn - (expanded-native-test--paid-snapshot-checks snapshot) + (compaction-owner-test--paid-snapshot-checks snapshot) (test-assert (getf snapshot :notice-p) "A postpaid refusal has a persisted context explanation.") (test-assert (not (getf snapshot :automatic-work-p)) "Refusal cannot automatically retry queued or completion work.")) (progn (test-assert (getf snapshot :checkpoint-p) "Fitting recovery publishes a durable checkpoint.") - (test-assert (= 1 (getf snapshot :summaries)) - "Native and portable projections each use one structured summary bridge.") - (when (= 200 (getf snapshot :status)) - (test-assert (= 1 (count-if (lambda (event) - (and (eq :usage-persisted (first event)) - (eq :native (second event)))) - (getf snapshot :events))) - "The successful native request is accounted separately from its summary.")) + (test-assert (= 1 (getf snapshot :summaries)) "The projection uses one checkpoint request.") (unless (member (getf snapshot :entry) '(:terminal-idle :acp-idle)) (test-assert (getf snapshot :answer-p) "The original user or completion turn receives its ordinary answer.")))) (when (getf snapshot :owned-p) @@ -2144,7 +1817,7 @@ (test-assert (= 144000 (getf snapshot :window)) "Explicit retry uses the same configured window.") (test-assert (null (getf snapshot :later-failure)) (format nil "Later input failed in ~S: ~A (~A)." - (loop for key in '(:entry :status :owned-p :signaled-p :arrival-size) + (loop for key in '(:entry :owned-p :arrival-size) append (list key (getf snapshot key))) (getf snapshot :later-failure) (getf snapshot :later-failure-message))) @@ -2158,62 +1831,57 @@ (test-assert (getf snapshot :later-answer-p) "The later ordinary answer is durable.")))) nil) -(-> test-expanded-native-acp-owner-matrix () null) -(defun test-expanded-native-acp-owner-matrix () - "Exercise actual client, claimed queue and completed-job owners over all unavailable statuses." +(-> test-compaction-owner-acp-matrix () null) +(defun test-compaction-owner-acp-matrix () + "Exercise actual client, claimed queue and completed-job owners." (dolist (entry '(:acp-prompt :acp-queue :acp-completion)) - (dolist (status '(404 405 501)) - (dolist (owned-p '(nil t)) - (dolist (cell '((nil nil) (nil t) (t nil) (t t))) - (expanded-native-test--matrix-checks - (expanded-native-test--matrix-snapshot :entry entry :status status :owned-p owned-p - :signaled-p (first cell) :arrival-p (second cell))))))) + (dolist (owned-p '(nil t)) + (dolist (arrival-p '(nil t)) + (compaction-owner-test--matrix-checks + (compaction-owner-test--matrix-snapshot :entry entry :owned-p owned-p :arrival-p arrival-p))))) nil) -(-> test-expanded-native-idle-owner-matrix () null) -(defun test-expanded-native-idle-owner-matrix () - "Run terminal and ACP due episodes through real idle owners on all status and ownership cells." +(-> test-compaction-owner-idle-matrix () null) +(defun test-compaction-owner-idle-matrix () + "Run terminal and ACP due episodes through real idle owners on all ownership cells." (dolist (entry '(:terminal-idle :acp-idle)) - (dolist (status '(404 405 501)) - (dolist (owned-p '(nil t)) - (dolist (cell '((nil nil) (nil t) (t nil) (t t))) - (expanded-native-test--matrix-checks - (expanded-native-test--matrix-snapshot :entry entry :status status :owned-p owned-p - :signaled-p (first cell) :arrival-p (second cell) :reuse-p t)))))) + (dolist (owned-p '(nil t)) + (dolist (arrival-p '(nil t)) + (compaction-owner-test--matrix-checks + (compaction-owner-test--matrix-snapshot :entry entry :owned-p owned-p :arrival-p arrival-p :reuse-p t))))) nil) -(-> test-expanded-native-image-owner-and-reuse () null) -(defun test-expanded-native-image-owner-and-reuse () +(-> test-compaction-owner-image-and-reuse () null) +(defun test-compaction-owner-image-and-reuse () "Contrast synthetic image and text owners and role-qualify fitting later ordinary answers." (dolist (image-p '(nil t)) - (dolist (status '(200 404 405 501)) - (dolist (arrival-p (if (= status 200) '(nil) '(nil t))) - (expanded-native-test--matrix-checks - (expanded-native-test--matrix-snapshot :entry ':acp-prompt :owned-p t :status status - :arrival-p arrival-p :image-p image-p :reuse-p t))))) + (dolist (arrival-p '(nil t)) + (compaction-owner-test--matrix-checks + (compaction-owner-test--matrix-snapshot :entry ':acp-prompt :owned-p t + :arrival-p arrival-p :image-p image-p :reuse-p t)))) nil) -(defclass expanded-native-values-agent (agent) - ((return-values :initarg :return-values :reader expanded-native-test-return-values +(defclass compaction-owner-values-agent (agent) + ((return-values :initarg :return-values :reader compaction-owner-test-return-values :documentation "The complete values returned by a test generic extension.")) (:documentation "Agent extension exercising the owner's primary-value contract.")) -(defmethod agent-run-user-turn ((agent expanded-native-values-agent) content &key &allow-other-keys) +(defmethod agent-run-user-turn ((agent compaction-owner-values-agent) content &key &allow-other-keys) "Return the configured value count without replacing the generic function." (declare (ignore content)) - (values-list (expanded-native-test-return-values agent))) + (values-list (compaction-owner-test-return-values agent))) -(-> test-expanded-native-owner-return-values () null) -(defun test-expanded-native-owner-return-values () +(-> test-compaction-owner-return-values () null) +(defun test-compaction-owner-return-values () "Accept generic extensions returning zero, one, or multiple values at the real terminal owner." - (expanded-native-test--fixture + (compaction-owner-test--fixture (lambda (application provider) (declare (ignore provider)) (let* ((agent (application-agent application)) (primary (agent-test-result "extension" (list (agent-test-message "Extension result.")))) (note-original (symbol-function 'application--note-goal-turn)) (title-original (symbol-function 'application--maybe-refresh-conversation-title))) - (change-class agent 'expanded-native-values-agent :return-values nil) + (change-class agent 'compaction-owner-values-agent :return-values nil) (dolist (returned (list nil (list primary) (list primary :secondary))) (setf (slot-value agent 'return-values) returned) (let ((noted :unset) (titles 0) (failure nil) (result nil)) @@ -2233,14 +1901,12 @@ (test-assert (= 1 titles) "Successful input reaches title finalization once.")))))) nil) -(-> test-expanded-native-idle-cancellation-refusal () null) -(defun test-expanded-native-idle-cancellation-refusal () +(-> test-compaction-owner-idle-cancellation-refusal () null) +(defun test-compaction-owner-idle-cancellation-refusal () "Give recorded channel cancellation precedence over final-fit idle refusal." (dolist (cancel-p '(nil t)) - (let ((snapshot (expanded-native-test--matrix-snapshot - :entry :acp-idle :owned-p t :status 404 :signaled-p t - :arrival-p t :cancel-before-refusal-p cancel-p))) - (test-assert (= 1 (getf snapshot :posts)) "Native admission preceded the cancellation.") + (let ((snapshot (compaction-owner-test--matrix-snapshot + :entry :acp-idle :owned-p t :arrival-p t :cancel-before-refusal-p cancel-p))) (test-assert (= 1 (getf snapshot :summaries)) "The paid summary completed before final validation.") (test-assert (= 150 (getf snapshot :paid)) "Paid usage settles once.") (test-assert (eq :active (getf snapshot :mission-status)) "The mission retains its active authority.") @@ -2259,11 +1925,11 @@ "Accounting retains no outstanding reservation or unknown usage."))) nil) -(-> test-expanded-native-owner-finalization-errors () null) -(defun test-expanded-native-owner-finalization-errors () +(-> test-compaction-owner-finalization-errors () null) +(defun test-compaction-owner-finalization-errors () "Keep goal, title and terminal presentation failures outside the inference refusal catch." (dolist (target '(application--note-goal-turn application--maybe-refresh-conversation-title application-render-records)) - (expanded-native-test--fixture + (compaction-owner-test--fixture (lambda (application provider) (declare (ignore provider)) (let* ((agent (application-agent application)) @@ -2271,7 +1937,7 @@ :estimated-input-tokens 300000 :output-reserve 8192 :context-window 272000 :non-text-estimate-p nil)) (caught nil) (presented 0)) - (change-class agent 'expanded-native-values-agent + (change-class agent 'compaction-owner-values-agent :return-values (list (agent-test-result "extension" (list (agent-test-message "Extension result."))))) (test-call-with-function-replacements (list (list target (lambda (&rest arguments) (declare (ignore arguments)) (error condition))) @@ -2284,39 +1950,35 @@ (test-assert (zerop presented) "A finalization failure is not presented as inference context refusal."))))) nil) -(-> test-expanded-native-scheduled-controller () null) -(defun test-expanded-native-scheduled-controller () +(-> test-compaction-owner-scheduled-controller () null) +(defun test-compaction-owner-scheduled-controller () "Consume valid claimed wakeups through the default enqueue, terminal dispatch and default runner." - (dolist (status '(404 405 501)) - (dolist (signaled-p '(nil t)) - (dolist (mode '(:fitting-recurring :refused-recurring :refused-one-shot)) - (let* ((arrival-p (not (eq mode :fitting-recurring))) - (snapshot (expanded-native-test--owner-snapshot - :entry :wakeup :owned-p t :status status :signaled-p signaled-p - :controller-p t :one-shot-p (eq mode :refused-one-shot) - :arrival-size (when arrival-p 600000)))) - - (test-assert (null (getf snapshot :failure)) "Actual scheduled controller dispatch returns without an owner exception.") - (test-assert (member '(:terminal-work :native-lisp) (getf snapshot :events) :test #'equal) - "The valid ticket enters the real noninteractive native Lisp work path.") - (test-assert (= 1 (getf snapshot :posts)) "One original native attempt reaches inert transport.") - (test-assert (= 1 (getf snapshot :summaries)) "The portable summary is paid once.") - (test-assert (equal (if arrival-p '(t) '(nil t)) (getf snapshot :flags)) - "No unrequested paid request follows a final-fit refusal.") - (test-assert (= (if arrival-p 150 180) (getf snapshot :paid)) "Only the actual summary and fitting answer are charged.") - (test-assert (eq (if arrival-p :failed :completed) (getf snapshot :wakeup-state)) - "The default runner durably settles refused or completed work, never UNKNOWN.") - (test-assert (not (getf snapshot :aborted-p)) "The mission turn is not aborted.") - (test-assert (eq :active (getf snapshot :mission-status)) "The scheduled mission remains active.") - (test-assert (getf snapshot :arrival-present-p) "Refusal retains the complete canonical arrival even when operation records follow it.") - (test-assert (not (getf snapshot :controller-active-p)) "The controller releases active work.") - (test-assert (zerop (getf snapshot :executing)) "Scheduler execution reservations are released.") - (test-assert (every #'zerop (mapcar (lambda (key) (getf snapshot key)) '(:outstanding :reserved :unknown))) - "No usage remains outstanding, reserved, or unknown."))))) + (dolist (mode '(:fitting-recurring :refused-recurring :refused-one-shot)) + (let* ((arrival-p (not (eq mode :fitting-recurring))) + (snapshot (compaction-owner-test--owner-snapshot + :entry :wakeup :owned-p t + :controller-p t :one-shot-p (eq mode :refused-one-shot) + :arrival-size (when arrival-p 600000)))) + (test-assert (null (getf snapshot :failure)) "Actual scheduled controller dispatch returns without an owner exception.") + (test-assert (member '(:terminal-work :native-lisp) (getf snapshot :events) :test #'equal) + "The valid ticket enters the real noninteractive native Lisp work path.") + (test-assert (= 1 (getf snapshot :summaries)) "The checkpoint request is paid once.") + (test-assert (equal (if arrival-p '(t) '(nil t)) (getf snapshot :flags)) + "No unrequested paid request follows a final-fit refusal.") + (test-assert (= (if arrival-p 150 180) (getf snapshot :paid)) "Only the actual summary and fitting answer are charged.") + (test-assert (eq (if arrival-p :failed :completed) (getf snapshot :wakeup-state)) + "The default runner durably settles refused or completed work, never UNKNOWN.") + (test-assert (not (getf snapshot :aborted-p)) "The mission turn is not aborted.") + (test-assert (eq :active (getf snapshot :mission-status)) "The scheduled mission remains active.") + (test-assert (getf snapshot :arrival-present-p) "Refusal retains the complete canonical arrival even when operation records follow it.") + (test-assert (not (getf snapshot :controller-active-p)) "The controller releases active work.") + (test-assert (zerop (getf snapshot :executing)) "Scheduler execution reservations are released.") + (test-assert (every #'zerop (mapcar (lambda (key) (getf snapshot key)) '(:outstanding :reserved :unknown))) + "No usage remains outstanding, reserved, or unknown."))) nil) -(-> expanded-native-test--run-next (application-input-controller) null) -(defun expanded-native-test--run-next (controller) +(-> compaction-owner-test--run-next (application-input-controller) null) +(defun compaction-owner-test--run-next (controller) "Dispatch and finalize one genuinely selected channel work item." (let ((work (application-input-controller--next-work controller))) (unwind-protect @@ -2324,8 +1986,8 @@ (application-input-controller--finish-work controller))) nil) -(-> expanded-native-test--ready-completion (application) non-empty-string) -(defun expanded-native-test--ready-completion (application) +(-> compaction-owner-test--ready-completion (application) non-empty-string) +(defun compaction-owner-test--ready-completion (application) "Start a real completed job and wait for its automatic continuation deadline." (let* ((agent (application-agent application)) (job (job-completion-tests--start agent (task-completion--runtime agent))) @@ -2336,14 +1998,14 @@ (sleep 0.01)) (session-job-execution-identifier job))) -(-> expanded-native-test--wire-refusal () null) -(defun expanded-native-test--wire-refusal () +(-> compaction-owner-test--wire-refusal () null) +(defun compaction-owner-test--wire-refusal () "Return local admission refusal over a served ACP connection without an RPC error." (with-test-environment (("AUTOLITH_CONTEXT_WINDOW" "144000") ("AUTOLITH_CONTEXT_OUTPUT_RESERVE" "8192") ("AUTOLITH_AUTOCOMPACT_THRESHOLD" "35")) (with-test-configuration (configuration) - (let ((provider (make-instance 'expanded-native-test-provider + (let ((provider (make-instance 'compaction-owner-test-provider :configuration configuration :credential-manager (provider-credential-manager (provider-create configuration)) :results nil))) @@ -2364,9 +2026,8 @@ "The hosted channel is already visible in the presentation catalogue.") (test-assert (application-world-agent-present-p application) "A hosted-only projection does not refuse agent admission.") - (expanded-native-test--transport - application :status 404 :expected-posts 0 - :function + (compaction-owner-test--call-with-credentials + application (lambda () (let ((result (agentcomms:client-prompt client identifier @@ -2381,7 +2042,7 @@ (test-object-contains-string-p update "Context admission refused")) updates)) "ACP replay includes the persisted refusal exactly once.")) - (test-assert (null (expanded-native-compaction-flags provider)) + (test-assert (null (compaction-owner-compaction-flags provider)) "Neither summary nor answer crosses transport after preflight refusal.") (test-assert (not (find :turn-aborted (conversation-records-newest conversation 100) :key #'first)) "Wire refusal does not append a failed model turn.") diff --git a/tests/compaction-unfinished-work-tests.lisp b/tests/compaction-unfinished-work-tests.lisp index 7f2c11659..3e8b7e251 100644 --- a/tests/compaction-unfinished-work-tests.lisp +++ b/tests/compaction-unfinished-work-tests.lisp @@ -2,28 +2,13 @@ ;;;; -- Unfinished Work Across Compaction -- -(defclass compaction-tests-provider (native-scripted-provider) +(defclass compaction-tests-provider (scripted-provider) ((arrival :initarg :arrival :initform nil :reader compaction-tests-provider-arrival - :documentation "Optional callback appending authoritative work during a request.") - (stage - :initarg :stage - :initform ':summary - :reader compaction-tests-provider-stage - :documentation "The request boundary at which ARRIVAL runs.")) - (:documentation "Scripted native or portable compaction with deterministic concurrent arrivals.")) - -(defmethod provider-native-compact-conversation :around - ((provider compaction-tests-provider) (conversation conversation) - &key tool-namespaces event-callback) - "Append an arrival after the detached native input has been observed." - (declare (ignore tool-namespaces event-callback)) - (prog1 (call-next-method) - (when (and (eq (compaction-tests-provider-stage provider) ':native) - (compaction-tests-provider-arrival provider)) - (funcall (compaction-tests-provider-arrival provider))))) + :documentation "Optional callback appending authoritative work during the compaction request.")) + (:documentation "Scripted compaction with deterministic concurrent arrivals.")) (defmethod provider-stream-turn :around ((provider compaction-tests-provider) (conversation conversation) @@ -31,20 +16,16 @@ "Append an arrival after recording the summarization request snapshot." (declare (ignore tool-namespaces event-callback goal-context)) (prog1 (call-next-method) - (when (and compaction-p - (eq (compaction-tests-provider-stage provider) ':summary) - (compaction-tests-provider-arrival provider)) + (when (and compaction-p (compaction-tests-provider-arrival provider)) (funcall (compaction-tests-provider-arrival provider))))) -(-> compaction-tests--provider (&key (:configuration configuration) (:native-p boolean) (:stage keyword) - (:arrival (option function))) compaction-tests-provider) -(defun compaction-tests--provider (&key configuration native-p (stage ':summary) arrival) - "Return one native/portable compaction followed by a usable portable summary." +(-> compaction-tests--provider + (&key (:configuration configuration) (:arrival (option function))) compaction-tests-provider) +(defun compaction-tests--provider (&key configuration arrival) + "Return a provider that answers one compaction request with a usable checkpoint." (make-instance - 'compaction-tests-provider :configuration configuration :stage stage :arrival arrival - :native-items (list (when native-p - (json-object "type" "compaction" "encrypted_content" "private-checkpoint"))) - :results (list (agent-test-result + 'compaction-tests-provider :configuration configuration :arrival arrival + :results (list (agent-test-result "unfinished-summary" (list (agent-test-checkpoint-call)) :turn-completion ':end)))) @@ -71,57 +52,50 @@ (-> compaction-tests--checkpoint (conversation) list) (defun compaction-tests--checkpoint (conversation) - "Read the latest summary/native checkpoint from durable storage." - (find-if (lambda (record) (member (first record) '(:summary :native-compaction))) - (reverse (rest (conversation--read-records (conversation-pathname conversation)))))) + "Read the latest checkpoint from durable storage." + (find :summary (rest (conversation--read-records (conversation-pathname conversation))) + :key #'first :from-end t)) (-> test-compaction-unfinished-cutoff-replay () null) (defun test-compaction-unfinished-cutoff-replay () "Preserve late correlated outputs, steering and private metadata across restart and family switch." (with-test-configuration (configuration root) (declare (ignore root)) - (dolist (native-p '(nil t)) - (let ((conversation (conversation-create configuration))) - (conversation-append-user-message conversation "captured question") - (conversation-append-provider-item conversation (compaction-tests--call "late-call")) - (let* ((capture (conversation-compaction-capture conversation)) - (cutoff (conversation-compaction-cutoff-sequence capture)) - (view (conversation-compaction-view capture)) - (snapshot (mapcar #'json-encode (conversation-input-items view)))) - (conversation-append-tool-result conversation "late-call" :tool-name "test.effect" - :output "actual late result" :success-p t) - (conversation-append-user-message conversation "late steering instruction") - (conversation-append-provider-item - conversation (json-object "type" "reasoning" "encrypted_content" "private-late-reasoning")) - (test-assert (equal snapshot (mapcar #'json-encode (conversation-input-items view))) - "arrivals do not mutate the provider snapshot") - (let ((commit-sequence (conversation-next-sequence conversation))) - (if native-p - (conversation-append-native-compaction - conversation (list (json-object "type" "compaction" "encrypted_content" "private-checkpoint")) - :family ':codex :continuation (make-continuation :intent "Portable handoff.") :compaction capture) - (conversation-append-summary conversation (make-continuation :intent "Portable handoff.") :compaction capture)) - (let ((record (compaction-tests--checkpoint conversation))) - (test-assert (= cutoff (getf (rest record) :cutoff-seq)) "checkpoint records the request cutoff") - (test-assert (= (1- commit-sequence) (getf (rest record) :through-seq)) - "checkpoint covers arrivals through the publication boundary"))) - (dolist (projection (list conversation (conversation-load (conversation-pathname conversation)))) - (let ((outputs (compaction-tests--outputs projection "late-call"))) - (test-assert (= 1 (length outputs)) "live and restarted history contain one correlated output") - (test-assert (equal "actual late result" (json-get (first outputs) "output")) - "actual output wins over a pure interrupted repair")) - (test-assert (compaction-tests--contains-p (conversation-input-items projection) "late steering instruction") - "steering after the capture survives compaction and replay") - (test-assert (compaction-tests--contains-p - (conversation-input-items-for-family projection ':codex) "private-late-reasoning") - "producing family retains its late private context") - (let ((portable (conversation-input-items-for-family projection ':grok))) - (test-assert (not (compaction-tests--contains-p portable "private-late-reasoning")) - "private late context never leaks across provider families") - (test-assert (not (compaction-tests--contains-p portable "private-checkpoint")) - "opaque native checkpoints never enter portable history") - (test-assert (compaction-tests--contains-p portable "Portable handoff.") - "the other family receives a portable summary"))))))) + (let ((conversation (conversation-create configuration))) + (conversation-append-user-message conversation "captured question") + (conversation-append-provider-item conversation (compaction-tests--call "late-call")) + (let* ((capture (conversation-compaction-capture conversation)) + (cutoff (conversation-compaction-cutoff-sequence capture)) + (view (conversation-compaction-view capture)) + (snapshot (mapcar #'json-encode (conversation-input-items view)))) + (conversation-append-tool-result conversation "late-call" :tool-name "test.effect" + :output "actual late result" :success-p t) + (conversation-append-user-message conversation "late steering instruction") + (conversation-append-provider-item + conversation (json-object "type" "reasoning" "encrypted_content" "private-late-reasoning")) + (test-assert (equal snapshot (mapcar #'json-encode (conversation-input-items view))) + "arrivals do not mutate the provider snapshot") + (let ((commit-sequence (conversation-next-sequence conversation))) + (conversation-append-summary conversation (make-continuation :intent "Portable handoff.") :compaction capture) + (let ((record (compaction-tests--checkpoint conversation))) + (test-assert (= cutoff (getf (rest record) :cutoff-seq)) "checkpoint records the request cutoff") + (test-assert (= (1- commit-sequence) (getf (rest record) :through-seq)) + "checkpoint covers arrivals through the publication boundary"))) + (dolist (projection (list conversation (conversation-load (conversation-pathname conversation)))) + (let ((outputs (compaction-tests--outputs projection "late-call"))) + (test-assert (= 1 (length outputs)) "live and restarted history contain one correlated output") + (test-assert (equal "actual late result" (json-get (first outputs) "output")) + "actual output wins over a pure interrupted repair")) + (test-assert (compaction-tests--contains-p (conversation-input-items projection) "late steering instruction") + "steering after the capture survives compaction and replay") + (test-assert (compaction-tests--contains-p + (conversation-input-items-for-family projection ':codex) "private-late-reasoning") + "producing family retains its late private context") + (let ((portable (conversation-input-items-for-family projection ':grok))) + (test-assert (not (compaction-tests--contains-p portable "private-late-reasoning")) + "private late context never leaks across provider families") + (test-assert (compaction-tests--contains-p portable "Portable handoff.") + "the other family receives a portable summary")))))) nil) (-> test-compaction-unfinished-durable-repair () null) @@ -154,43 +128,37 @@ (-> test-agent-compaction-unfinished-arrivals () null) (defun test-agent-compaction-unfinished-arrivals () - "Capture before both provider stages and preserve results and steering received inside each stage." - (dolist (case '((nil :summary) (t :native) (t :summary))) - (with-test-configuration (configuration root) - (declare (ignore root)) - (let* ((conversation (conversation-create configuration)) - (provider (compaction-tests--provider - :configuration configuration - :native-p (first case) :stage (second case) - :arrival (lambda () - (conversation-append-tool-result - conversation "during-request" :tool-name "test.effect" - :output "result during provider request" :success-p t) - (conversation-append-user-message conversation "steering during provider request")))) - (agent (agent-create :configuration configuration :conversation conversation - :provider provider :tool-registry (agent-test-registry) :worker nil))) - (conversation-append-user-message conversation "before provider request") - (conversation-append-provider-item conversation (compaction-tests--call "during-request")) - (let ((cutoff (1- (conversation-next-sequence conversation)))) - (agent-compact-conversation agent (make-instance 'agent-observer)) - (test-assert (= cutoff (getf (rest (compaction-tests--checkpoint conversation)) :cutoff-seq)) - "agent captures its cutoff before issuing either compaction request")) - (test-assert (not (compaction-tests--contains-p - (first (native-scripted-provider-native-input-snapshots provider)) - "steering during provider request")) - "native input remains the pre-request snapshot") - (test-assert (not (compaction-tests--contains-p - (first (scripted-provider-input-snapshots provider)) - "steering during provider request")) - "summary input remains tied to the original cutoff") - (dolist (projection (list conversation (conversation-load (conversation-pathname conversation)))) - (test-assert (compaction-tests--contains-p (conversation-input-items projection) - "steering during provider request") - "provider-stage steering survives publication and restart") - (let ((outputs (compaction-tests--outputs projection "during-request"))) - (test-assert (= 1 (length outputs)) "one correlated result survives provider-stage arrivals") - (test-assert (equal "result during provider request" (json-get (first outputs) "output")) - "provider-stage actual output supersedes speculative repair")))))) + "Capture before the compaction request and preserve results and steering received during it." + (with-test-configuration (configuration root) + (declare (ignore root)) + (let* ((conversation (conversation-create configuration)) + (provider (compaction-tests--provider + :configuration configuration + :arrival (lambda () + (conversation-append-tool-result + conversation "during-request" :tool-name "test.effect" + :output "result during provider request" :success-p t) + (conversation-append-user-message conversation "steering during provider request")))) + (agent (agent-create :configuration configuration :conversation conversation + :provider provider :tool-registry (agent-test-registry) :worker nil))) + (conversation-append-user-message conversation "before provider request") + (conversation-append-provider-item conversation (compaction-tests--call "during-request")) + (let ((cutoff (1- (conversation-next-sequence conversation)))) + (agent-compact-conversation agent (make-instance 'agent-observer)) + (test-assert (= cutoff (getf (rest (compaction-tests--checkpoint conversation)) :cutoff-seq)) + "agent captures its cutoff before issuing the compaction request")) + (test-assert (not (compaction-tests--contains-p + (first (scripted-provider-input-snapshots provider)) + "steering during provider request")) + "summary input remains tied to the original cutoff") + (dolist (projection (list conversation (conversation-load (conversation-pathname conversation)))) + (test-assert (compaction-tests--contains-p (conversation-input-items projection) + "steering during provider request") + "provider-stage steering survives publication and restart") + (let ((outputs (compaction-tests--outputs projection "during-request"))) + (test-assert (= 1 (length outputs)) "one correlated result survives provider-stage arrivals") + (test-assert (equal "result during provider request" (json-get (first outputs) "output")) + "provider-stage actual output supersedes speculative repair"))))) nil) (-> test-compaction-unfinished-failure-atomicity () null) @@ -243,123 +211,121 @@ (lambda (&rest arguments) (apply generate :timestamp 3994000000 arguments)))) (lambda () - (dolist (native-p '(nil t)) - (job-completion-tests--fixture - (lambda (parent runtime configuration) - (let* ((registry (agent-tool-registry parent)) - (conversation (agent-conversation parent)) - (directory (config :working-directory configuration)) - (release (merge-pathnames "compaction-release" directory)) - (effects (merge-pathnames "compaction-effects" directory)) - (barrier (make-instance 'task-test-blocking-tool - :namespace "test" :name "block" - :description "Wait until compaction has published." - :parameters (tool-object-schema (json-object) nil))) - (child-provider (make-instance 'task-test-provider :mode ':blocking-tool)) - (context (make-instance 'tool-context - :configuration configuration :conversation conversation - :registry registry :agent parent :worker nil - :command-authorization-function - (lambda (command directory) - (declare (ignore command directory)) - ':full-access))) - (child nil) - (shell nil)) - (tool-registry-register registry barrier) - (setf (agent-provider parent) child-provider) - (unwind-protect - (progn - (tool-execute (tool-registry-find registry "child" "spawn") context - (json-object "name" "compaction-child" "agent" "task" - "task" "Enter the blocking ordinary tool." - "completion-policy" "continue")) - (setf child (first (task-orchestrator-list-jobs runtime))) - (test-assert - (task-tests--wait-until - (lambda () - (with-lock-held ((task-test-blocking-tool-lock barrier)) - (task-test-blocking-tool-started-p barrier))) - 5) - "the actual child reaches its running tool before compaction") - (let* ((result - (tool-execute - (tool-registry-find registry "shell" "run") context - (json-object - "command" - (test-fixture-shell-command - *platform* - "while [ ! -f compaction-release ]; do sleep 0.02; done; printf 'shell-once\\n' >> compaction-effects; printf shell-finished" - "while (!(Test-Path compaction-release)) { Start-Sleep -Milliseconds 20 }; Add-Content compaction-effects shell-once; [Console]::Write('shell-finished')") - "async" t "completion-policy" "continue"))) - (record (getf (rest (tool-result-details result)) :job))) - (setf shell (task-orchestrator-find-visible-job runtime (getf record :id) - :viewer parent - :tool-name "job.get"))) - (test-assert (and child shell (not (job-terminal-p child)) (not (job-terminal-p shell))) - "both real jobs remain outstanding before compaction") - (let ((paths (mapcar (lambda (job) (task-continuity-record-job job parent)) - (list child shell)))) - (test-assert - (and (not (equal (session-job-execution-identifier child) - (session-job-execution-identifier shell))) - (not (equal (first paths) (second paths))) - (equal (mapcar (lambda (path) - (getf (task-continuity--read path) :kind)) paths) - '(:task :tool))) - "forced child/tool candidates receive distinct identities and storage")) - (task-completion-watch child parent) - (task-completion-watch shell parent) - (let* ((identifiers (mapcar #'session-job-execution-identifier (list child shell))) - (child-requests (task-test-provider-request-count child-provider))) - (setf (agent-provider parent) - (compaction-tests--provider :configuration configuration - :native-p native-p)) - (agent-compact-conversation parent (make-instance 'agent-observer)) - (let ((snapshot (getf (rest (compaction-tests--checkpoint conversation)) :unfinished-work))) - (test-assert (= 2 (getf snapshot :total)) "checkpoint contains authoritative outstanding-job identities") - (dolist (identifier identifiers) - (test-assert (find identifier (getf snapshot :jobs) - :key (lambda (row) (getf row :execution-id)) :test #'equal) - "both child and shell execution identities survive native/portable compaction"))) - (test-assert (= child-requests (task-test-provider-request-count child-provider)) - "compaction issues no new child request") - (test-assert (not (probe-file effects)) "compaction does not release or reexecute the shell") - (task-tests--release-blocking-tool barrier) - (with-open-file (stream release :direction ':output :if-exists ':supersede) - (write-line "release" stream)) - (dolist (job (list child shell)) - (multiple-value-bind (snapshot terminal-p) (session-job-await job 10) - (declare (ignore snapshot)) - (test-assert terminal-p "the original job completes after test release"))) - (cl-jobpond:completion-subscription-refresh - (task-completion-service-subscription (task-completion--service parent))) - (let ((fresh (job-completion-tests--fresh-agent parent configuration))) - (unwind-protect - (progn - (task-completion-restore fresh) - (test-assert (= 2 (length (task-completion-deliver fresh))) - "restart delivers each original outcome exactly once") - (dolist (identifier identifiers) - (test-assert (task-completion--delivered-p (agent-conversation fresh) identifier) - "both stable execution receipts are durable")) - (test-assert (null (task-completion-deliver fresh)) - "a repeated safe boundary does not redeliver either outcome") - (conversation-append-summary (agent-conversation fresh) (make-continuation :intent "Delivered both original jobs.")) - (let ((again (job-completion-tests--fresh-agent fresh configuration))) - (unwind-protect - (progn - (task-completion-restore again) - (test-assert (null (task-completion-deliver again)) - "recompaction and another restart retain exactly-once receipts")) - (task-orchestrator-close (task-completion--runtime again))))) - (task-orchestrator-close (task-completion--runtime fresh)))) - (test-assert (= 1 (length (uiop:read-file-lines effects))) - "the actual shell side effect ran once across compaction and restarts"))) - (task-tests--release-blocking-tool barrier) - (with-open-file (stream release :direction ':output :if-exists ':supersede) - (write-line "cleanup release" stream)) - (dolist (job (remove nil (list child shell))) - (session-job-await job 10)))))))))) + (job-completion-tests--fixture + (lambda (parent runtime configuration) + (let* ((registry (agent-tool-registry parent)) + (conversation (agent-conversation parent)) + (directory (config :working-directory configuration)) + (release (merge-pathnames "compaction-release" directory)) + (effects (merge-pathnames "compaction-effects" directory)) + (barrier (make-instance 'task-test-blocking-tool + :namespace "test" :name "block" + :description "Wait until compaction has published." + :parameters (tool-object-schema (json-object) nil))) + (child-provider (make-instance 'task-test-provider :mode ':blocking-tool)) + (context (make-instance 'tool-context + :configuration configuration :conversation conversation + :registry registry :agent parent :worker nil + :command-authorization-function + (lambda (command directory) + (declare (ignore command directory)) + ':full-access))) + (child nil) + (shell nil)) + (tool-registry-register registry barrier) + (setf (agent-provider parent) child-provider) + (unwind-protect + (progn + (tool-execute (tool-registry-find registry "child" "spawn") context + (json-object "name" "compaction-child" "agent" "task" + "task" "Enter the blocking ordinary tool." + "completion-policy" "continue")) + (setf child (first (task-orchestrator-list-jobs runtime))) + (test-assert + (task-tests--wait-until + (lambda () + (with-lock-held ((task-test-blocking-tool-lock barrier)) + (task-test-blocking-tool-started-p barrier))) + 5) + "the actual child reaches its running tool before compaction") + (let* ((result + (tool-execute + (tool-registry-find registry "shell" "run") context + (json-object + "command" + (test-fixture-shell-command + *platform* + "while [ ! -f compaction-release ]; do sleep 0.02; done; printf 'shell-once\\n' >> compaction-effects; printf shell-finished" + "while (!(Test-Path compaction-release)) { Start-Sleep -Milliseconds 20 }; Add-Content compaction-effects shell-once; [Console]::Write('shell-finished')") + "async" t "completion-policy" "continue"))) + (record (getf (rest (tool-result-details result)) :job))) + (setf shell (task-orchestrator-find-visible-job runtime (getf record :id) + :viewer parent + :tool-name "job.get"))) + (test-assert (and child shell (not (job-terminal-p child)) (not (job-terminal-p shell))) + "both real jobs remain outstanding before compaction") + (let ((paths (mapcar (lambda (job) (task-continuity-record-job job parent)) + (list child shell)))) + (test-assert + (and (not (equal (session-job-execution-identifier child) + (session-job-execution-identifier shell))) + (not (equal (first paths) (second paths))) + (equal (mapcar (lambda (path) + (getf (task-continuity--read path) :kind)) paths) + '(:task :tool))) + "forced child/tool candidates receive distinct identities and storage")) + (task-completion-watch child parent) + (task-completion-watch shell parent) + (let* ((identifiers (mapcar #'session-job-execution-identifier (list child shell))) + (child-requests (task-test-provider-request-count child-provider))) + (setf (agent-provider parent) + (compaction-tests--provider :configuration configuration)) + (agent-compact-conversation parent (make-instance 'agent-observer)) + (let ((snapshot (getf (rest (compaction-tests--checkpoint conversation)) :unfinished-work))) + (test-assert (= 2 (getf snapshot :total)) "checkpoint contains authoritative outstanding-job identities") + (dolist (identifier identifiers) + (test-assert (find identifier (getf snapshot :jobs) + :key (lambda (row) (getf row :execution-id)) :test #'equal) + "both child and shell execution identities survive compaction"))) + (test-assert (= child-requests (task-test-provider-request-count child-provider)) + "compaction issues no new child request") + (test-assert (not (probe-file effects)) "compaction does not release or reexecute the shell") + (task-tests--release-blocking-tool barrier) + (with-open-file (stream release :direction ':output :if-exists ':supersede) + (write-line "release" stream)) + (dolist (job (list child shell)) + (multiple-value-bind (snapshot terminal-p) (session-job-await job 10) + (declare (ignore snapshot)) + (test-assert terminal-p "the original job completes after test release"))) + (cl-jobpond:completion-subscription-refresh + (task-completion-service-subscription (task-completion--service parent))) + (let ((fresh (job-completion-tests--fresh-agent parent configuration))) + (unwind-protect + (progn + (task-completion-restore fresh) + (test-assert (= 2 (length (task-completion-deliver fresh))) + "restart delivers each original outcome exactly once") + (dolist (identifier identifiers) + (test-assert (task-completion--delivered-p (agent-conversation fresh) identifier) + "both stable execution receipts are durable")) + (test-assert (null (task-completion-deliver fresh)) + "a repeated safe boundary does not redeliver either outcome") + (conversation-append-summary (agent-conversation fresh) (make-continuation :intent "Delivered both original jobs.")) + (let ((again (job-completion-tests--fresh-agent fresh configuration))) + (unwind-protect + (progn + (task-completion-restore again) + (test-assert (null (task-completion-deliver again)) + "recompaction and another restart retain exactly-once receipts")) + (task-orchestrator-close (task-completion--runtime again))))) + (task-orchestrator-close (task-completion--runtime fresh)))) + (test-assert (= 1 (length (uiop:read-file-lines effects))) + "the actual shell side effect ran once across compaction and restarts"))) + (task-tests--release-blocking-tool barrier) + (with-open-file (stream release :direction ':output :if-exists ':supersede) + (write-line "cleanup release" stream)) + (dolist (job (remove nil (list child shell))) + (session-job-await job 10))))))))) nil) (-> test-compaction-unfinished-corrupt-replay () null) @@ -408,78 +374,76 @@ (-> test-agent-compaction-completed-during-request () null) (defun test-agent-compaction-completed-during-request () "Refresh terminal shell state and artifacts at checkpoint publication, then deliver once after restart." - (dolist (case '((nil :summary) (t :native) (t :summary))) - (job-completion-tests--fixture - (lambda (parent runtime configuration) - (let* ((conversation (agent-conversation parent)) - (registry (agent-tool-registry parent)) - (release (merge-pathnames "during-request-release" (config :working-directory configuration))) - (context (make-instance 'tool-context - :configuration configuration :conversation conversation - :registry registry :agent parent :worker nil - :command-authorization-function - (lambda (command directory) - (declare (ignore command directory)) - ':full-access))) - (result (tool-execute - (tool-registry-find registry "shell" "run") context - (json-object - "command" - (test-fixture-shell-command - *platform* - "while [ ! -f during-request-release ]; do sleep 0.02; done; printf completed-during-request" - "while (!(Test-Path during-request-release)) { Start-Sleep -Milliseconds 20 }; [Console]::Write('completed-during-request')") - "async" t "completion-policy" "continue"))) - (record (getf (rest (tool-result-details result)) :job)) - (job (task-orchestrator-find-visible-job runtime (getf record :id) - :viewer parent - :tool-name "job.get")) - (identifier (session-job-execution-identifier job))) - (unwind-protect - (progn - (task-completion-watch job parent) - (test-assert (not (job-terminal-p job)) "the actual shell job is outstanding at capture") - (setf (agent-provider parent) - (compaction-tests--provider - :configuration configuration - :native-p (first case) :stage (second case) - :arrival (lambda () - (with-open-file (stream release :direction ':output :if-exists ':supersede) - (write-line "complete inside provider request" stream)) - (multiple-value-bind (snapshot terminal-p) (session-job-await job 10) - (declare (ignore snapshot)) - (test-assert terminal-p "shell finishes before compaction provider returns")) - (cl-jobpond:completion-subscription-refresh - (task-completion-service-subscription (task-completion--service parent)))))) - (agent-compact-conversation parent (make-instance 'agent-observer)) - (let* ((work (getf (rest (compaction-tests--checkpoint conversation)) :unfinished-work)) - (row (find identifier (getf work :jobs) - :key (lambda (entry) (getf entry :execution-id)) :test #'equal))) - (test-assert (eq ':completed (getf row :status)) "checkpoint refreshes state completed during a provider request") - (test-assert (eq ':undelivered (getf row :delivery)) "compaction does not acknowledge terminal outcomes") - (test-assert (and (getf row :artifact-path) (probe-file (getf row :artifact-path))) - "checkpoint retains an inspectable terminal artifact") - (let ((fresh (job-completion-tests--fresh-agent parent configuration))) - (unwind-protect - (progn - (let* ((snapshot (task-unfinished-work-snapshot fresh)) - (restored (find identifier (getf snapshot :jobs) - :key (lambda (entry) (getf entry :execution-id)) :test #'equal))) - (test-assert (eq ':completed (getf restored :status)) - "post-restart outstanding context reflects authoritative terminal state") - (test-assert (equal (getf row :artifact-path) (getf restored :artifact-path)) - "post-restart context retains the same artifact reference")) - (task-completion-restore fresh) - (test-assert (= 1 (length (task-completion-deliver fresh))) - "the completion during summarization is delivered after restart") - (test-assert (task-completion--delivered-p (agent-conversation fresh) identifier) - "the original execution receives its durable receipt") - (test-assert (null (task-completion-deliver fresh)) - "subsequent boundaries do not redeliver the same completion")) - (task-orchestrator-close (task-completion--runtime fresh)))))) - (with-open-file (stream release :direction ':output :if-exists ':supersede) - (write-line "cleanup release" stream)) - (session-job-await job 10)))))) + (job-completion-tests--fixture + (lambda (parent runtime configuration) + (let* ((conversation (agent-conversation parent)) + (registry (agent-tool-registry parent)) + (release (merge-pathnames "during-request-release" (config :working-directory configuration))) + (context (make-instance 'tool-context + :configuration configuration :conversation conversation + :registry registry :agent parent :worker nil + :command-authorization-function + (lambda (command directory) + (declare (ignore command directory)) + ':full-access))) + (result (tool-execute + (tool-registry-find registry "shell" "run") context + (json-object + "command" + (test-fixture-shell-command + *platform* + "while [ ! -f during-request-release ]; do sleep 0.02; done; printf completed-during-request" + "while (!(Test-Path during-request-release)) { Start-Sleep -Milliseconds 20 }; [Console]::Write('completed-during-request')") + "async" t "completion-policy" "continue"))) + (record (getf (rest (tool-result-details result)) :job)) + (job (task-orchestrator-find-visible-job runtime (getf record :id) + :viewer parent + :tool-name "job.get")) + (identifier (session-job-execution-identifier job))) + (unwind-protect + (progn + (task-completion-watch job parent) + (test-assert (not (job-terminal-p job)) "the actual shell job is outstanding at capture") + (setf (agent-provider parent) + (compaction-tests--provider + :configuration configuration + :arrival (lambda () + (with-open-file (stream release :direction ':output :if-exists ':supersede) + (write-line "complete inside provider request" stream)) + (multiple-value-bind (snapshot terminal-p) (session-job-await job 10) + (declare (ignore snapshot)) + (test-assert terminal-p "shell finishes before compaction provider returns")) + (cl-jobpond:completion-subscription-refresh + (task-completion-service-subscription (task-completion--service parent)))))) + (agent-compact-conversation parent (make-instance 'agent-observer)) + (let* ((work (getf (rest (compaction-tests--checkpoint conversation)) :unfinished-work)) + (row (find identifier (getf work :jobs) + :key (lambda (entry) (getf entry :execution-id)) :test #'equal))) + (test-assert (eq ':completed (getf row :status)) "checkpoint refreshes state completed during a provider request") + (test-assert (eq ':undelivered (getf row :delivery)) "compaction does not acknowledge terminal outcomes") + (test-assert (and (getf row :artifact-path) (probe-file (getf row :artifact-path))) + "checkpoint retains an inspectable terminal artifact") + (let ((fresh (job-completion-tests--fresh-agent parent configuration))) + (unwind-protect + (progn + (let* ((snapshot (task-unfinished-work-snapshot fresh)) + (restored (find identifier (getf snapshot :jobs) + :key (lambda (entry) (getf entry :execution-id)) :test #'equal))) + (test-assert (eq ':completed (getf restored :status)) + "post-restart outstanding context reflects authoritative terminal state") + (test-assert (equal (getf row :artifact-path) (getf restored :artifact-path)) + "post-restart context retains the same artifact reference")) + (task-completion-restore fresh) + (test-assert (= 1 (length (task-completion-deliver fresh))) + "the completion during summarization is delivered after restart") + (test-assert (task-completion--delivered-p (agent-conversation fresh) identifier) + "the original execution receives its durable receipt") + (test-assert (null (task-completion-deliver fresh)) + "subsequent boundaries do not redeliver the same completion")) + (task-orchestrator-close (task-completion--runtime fresh)))))) + (with-open-file (stream release :direction ':output :if-exists ':supersede) + (write-line "cleanup release" stream)) + (session-job-await job 10))))) nil) (-> test-compaction-announced-before-job-state () null) diff --git a/tests/context-admission-tests.lisp b/tests/context-admission-tests.lisp index 208b92c48..92c720e49 100644 --- a/tests/context-admission-tests.lisp +++ b/tests/context-admission-tests.lisp @@ -183,54 +183,47 @@ (-> test-context-admission-checkpoint-transaction () null) (defun test-context-admission-checkpoint-transaction () "Reject oversized final checkpoints before changing durable or projected history." - (dolist (native-p '(nil t)) - (with-test-environment (("AUTOLITH_CONTEXT_WINDOW" "5000") - ("AUTOLITH_CONTEXT_OUTPUT_RESERVE" "256")) - (with-test-configuration (configuration) - (let* ((conversation (conversation-create configuration)) - (provider (make-instance 'context-admission-test-provider - :configuration configuration)) - (window (list (json-object "type" "compaction" - "encrypted_content" "private-checkpoint")))) - (conversation-append-user-message conversation "Original exact request.") - (let ((capture (conversation-compaction-capture conversation))) - (conversation-append-user-message conversation - (make-string 20000 :initial-element #\a)) - (let ((before (copy-list (conversation-input-items conversation))) - (sequence (conversation-next-sequence conversation)) - (checked-p nil)) - (flet ((validate (candidate) - "Admit the full candidate, including arrivals, before publication." - (setf checked-p t) - (test-assert (find (second before) - (conversation-input-items candidate) :test #'eq) - "the validator sees the exact chronological arrival") - (provider-check-request-context - provider (json-object "input" - (coerce (conversation-input-items candidate) 'vector))))) - (test-assert - (handler-case - (progn - (if native-p - (conversation-append-native-compaction - conversation window :family ':responses :continuation (make-continuation :intent "Small handoff.") - :compaction capture :validate-projection #'validate) - (conversation-append-summary - conversation (make-continuation :intent "Small handoff.") :compaction capture - :validate-projection #'validate)) - nil) - (provider-context-budget-error () t)) - "oversized candidate admission aborts publication")) - (test-assert checked-p "publication invoked the admission callback") - (test-assert (and (= sequence (conversation-next-sequence conversation)) - (equal before (conversation-input-items conversation))) - "rejection preserves the full projection and durable sequence") - (let ((loaded (conversation-load-by-id - configuration (conversation-identifier conversation)))) - (test-assert - (equal (mapcar #'json-encode before) - (mapcar #'json-encode (conversation-input-items loaded))) - "reload sees no rejected checkpoint")))))))) + (with-test-environment (("AUTOLITH_CONTEXT_WINDOW" "5000") + ("AUTOLITH_CONTEXT_OUTPUT_RESERVE" "256")) + (with-test-configuration (configuration) + (let* ((conversation (conversation-create configuration)) + (provider (make-instance 'context-admission-test-provider + :configuration configuration))) + (conversation-append-user-message conversation "Original exact request.") + (let ((capture (conversation-compaction-capture conversation))) + (conversation-append-user-message conversation + (make-string 20000 :initial-element #\a)) + (let ((before (copy-list (conversation-input-items conversation))) + (sequence (conversation-next-sequence conversation)) + (checked-p nil)) + (flet ((validate (candidate) + "Admit the full candidate, including arrivals, before publication." + (setf checked-p t) + (test-assert (find (second before) + (conversation-input-items candidate) :test #'eq) + "the validator sees the exact chronological arrival") + (provider-check-request-context + provider (json-object "input" + (coerce (conversation-input-items candidate) 'vector))))) + (test-assert + (handler-case + (progn + (conversation-append-summary + conversation (make-continuation :intent "Small handoff.") :compaction capture + :validate-projection #'validate) + nil) + (provider-context-budget-error () t)) + "oversized candidate admission aborts publication")) + (test-assert checked-p "publication invoked the admission callback") + (test-assert (and (= sequence (conversation-next-sequence conversation)) + (equal before (conversation-input-items conversation))) + "rejection preserves the full projection and durable sequence") + (let ((loaded (conversation-load-by-id + configuration (conversation-identifier conversation)))) + (test-assert + (equal (mapcar #'json-encode before) + (mapcar #'json-encode (conversation-input-items loaded))) + "reload sees no rejected checkpoint"))))))) nil) (-> test-context-admission-mission-large-budget () null) @@ -294,53 +287,40 @@ (declare (ignore provider)) t) -(defmethod provider-open-native-compaction - ((provider context-admission-test-provider) (request hash-table) - &key credentials conversation) - "Capture admitted native compaction without opening a transport." - (declare (ignore credentials conversation)) - (setf (context-admission-test-provider-last-request provider) request - (context-admission-test-provider-last-ceiling provider) *provider-maximum-output-tokens*) - (incf (context-admission-test-provider-network-calls provider)) - (values nil 200 nil)) - (-> test-context-admission-mission-wire-ceiling () null) (defun test-context-admission-mission-wire-ceiling () "Measure serialized cap overhead and finalize ordinary and compaction requests." (with-test-environment (("AUTOLITH_CONTEXT_WINDOW" "5000") ("AUTOLITH_CONTEXT_OUTPUT_RESERVE" "256")) (with-test-configuration (configuration) - (dolist (native-p '(nil t)) - (let* ((provider (make-instance 'context-admission-capped-test-provider - :configuration configuration)) - (request (json-object "input" (make-string 1000 :initial-element #\x))) - (original (json-encode request)) - (*provider-mission-output-allowance* most-positive-fixnum) - (observed nil) - (*provider-cache-request-prefix* '(:fixture t)) - (*provider-cache-prefix-function* - (lambda (prefix) (setf observed prefix)))) - (funcall (if native-p #'provider-open-native-compaction #'provider-open-response-stream) - provider request) - (let* ((final-request (context-admission-test-provider-last-request provider)) - (ceiling (context-admission-test-provider-last-ceiling provider)) - (wire (json-get final-request "max_output_tokens")) - (input (provider-request-input-token-estimate provider final-request))) - (test-assert (= 1 (context-admission-test-provider-network-calls provider)) - "exactly one finalized request reaches the inert transport") - (test-assert (and (plusp ceiling) (= wire ceiling) - (<= (+ input (max 256 ceiling)) 5000)) - "the actual wire ceiling fits including its own serialized overhead") - (test-assert (> input (provider-request-input-token-estimate provider request)) - "the cap field is included in the input estimate") - (test-assert (equal original (json-encode request)) - "the caller's original request is not changed") - (unless native-p - (test-assert (equal (getf observed :wire) - (provider-cache-wire-plan - final-request - :volatile-items (getf observed :volatile-items 0))) - "cache observation fingerprints the finalized wire request"))))))) + (let* ((provider (make-instance 'context-admission-capped-test-provider + :configuration configuration)) + (request (json-object "input" (make-string 1000 :initial-element #\x))) + (original (json-encode request)) + (*provider-mission-output-allowance* most-positive-fixnum) + (observed nil) + (*provider-cache-request-prefix* '(:fixture t)) + (*provider-cache-prefix-function* + (lambda (prefix) (setf observed prefix)))) + (provider-open-response-stream provider request) + (let* ((final-request (context-admission-test-provider-last-request provider)) + (ceiling (context-admission-test-provider-last-ceiling provider)) + (wire (json-get final-request "max_output_tokens")) + (input (provider-request-input-token-estimate provider final-request))) + (test-assert (= 1 (context-admission-test-provider-network-calls provider)) + "exactly one finalized request reaches the inert transport") + (test-assert (and (plusp ceiling) (= wire ceiling) + (<= (+ input (max 256 ceiling)) 5000)) + "the actual wire ceiling fits including its own serialized overhead") + (test-assert (> input (provider-request-input-token-estimate provider request)) + "the cap field is included in the input estimate") + (test-assert (equal original (json-encode request)) + "the caller's original request is not changed") + (test-assert (equal (getf observed :wire) + (provider-cache-wire-plan + final-request + :volatile-items (getf observed :volatile-items 0))) + "cache observation fingerprints the finalized wire request"))))) nil) (-> test-context-admission-mission-explicit-policy () null) @@ -464,53 +444,51 @@ (-> test-context-admission-mission-refusal-settlement () null) (defun test-context-admission-mission-refusal-settlement () - "Settle locally refused stream, compaction and reviewer requests as known zero." + "Settle locally refused stream and reviewer requests as known zero." (with-test-environment (("AUTOLITH_CONTEXT_WINDOW" "5000") ("AUTOLITH_CONTEXT_OUTPUT_RESERVE" "256")) - (dolist (native-p '(nil t)) - (dolist (reviewer-p '(nil t)) - (with-test-configuration (configuration) - (let* ((provider (make-instance 'context-admission-test-provider :configuration configuration)) - (application (mission-inference-test--application configuration provider)) - (conversation (application-conversation application)) - (reviewer (when reviewer-p - (list :turn-limit 4 :token-limit 100 :turns-used 0 :tokens-used 0)))) - (application-mission-start application (mission-test--specification :turns 4 :tokens 100)) - (unwind-protect - (let* ((context (mission-context-find conversation)) - (goal (mission-context-goal context)) - (original-refusal nil)) - (test-assert - (handler-case - (mission--account-inference - context - (lambda () - (handler-bind ((provider-context-budget-error - (lambda (condition) (setf original-refusal condition)))) - (funcall (if native-p #'provider-open-native-compaction #'provider-open-response-stream) - provider (json-object "max_output_tokens" 6000)))) - (lambda (results) (declare (ignore results)) nil) - :allowance reviewer) - (error (condition) - (and (typep condition 'provider-context-budget-error) - (eq condition original-refusal) - (= 6000 (provider-context-budget-error-output-reserve condition)) - (= 5000 (provider-context-budget-error-context-window condition)) - (plusp (provider-context-budget-error-estimated-input-tokens condition))))) - "the accounted request is refused by ordinary admission") - (test-assert (zerop (context-admission-test-provider-network-calls provider)) - "local refusal never enters the transport") - (dolist (limit (remove nil (list goal reviewer))) - (test-assert (zerop (getf limit :unknown-usage 0)) - "the local refusal has known zero usage") - (test-assert (and (zerop (getf limit :tokens-used)) - (= 1 (getf limit :turns-used)) - (zerop (getf limit :tokens-reserved)) - (zerop (getf limit :requests-outstanding))) - "settlement releases once without refunding the admitted turn")) - (test-assert (eq ':active (getf goal :status)) - "known zero usage does not quarantine the mission")) - (mission-detach application))))))) + (dolist (reviewer-p '(nil t)) + (with-test-configuration (configuration) + (let* ((provider (make-instance 'context-admission-test-provider :configuration configuration)) + (application (mission-inference-test--application configuration provider)) + (conversation (application-conversation application)) + (reviewer (when reviewer-p + (list :turn-limit 4 :token-limit 100 :turns-used 0 :tokens-used 0)))) + (application-mission-start application (mission-test--specification :turns 4 :tokens 100)) + (unwind-protect + (let* ((context (mission-context-find conversation)) + (goal (mission-context-goal context)) + (original-refusal nil)) + (test-assert + (handler-case + (mission--account-inference + context + (lambda () + (handler-bind ((provider-context-budget-error + (lambda (condition) (setf original-refusal condition)))) + (provider-open-response-stream provider (json-object "max_output_tokens" 6000)))) + (lambda (results) (declare (ignore results)) nil) + :allowance reviewer) + (error (condition) + (and (typep condition 'provider-context-budget-error) + (eq condition original-refusal) + (= 6000 (provider-context-budget-error-output-reserve condition)) + (= 5000 (provider-context-budget-error-context-window condition)) + (plusp (provider-context-budget-error-estimated-input-tokens condition))))) + "the accounted request is refused by ordinary admission") + (test-assert (zerop (context-admission-test-provider-network-calls provider)) + "local refusal never enters the transport") + (dolist (limit (remove nil (list goal reviewer))) + (test-assert (zerop (getf limit :unknown-usage 0)) + "the local refusal has known zero usage") + (test-assert (and (zerop (getf limit :tokens-used)) + (= 1 (getf limit :turns-used)) + (zerop (getf limit :tokens-reserved)) + (zerop (getf limit :requests-outstanding))) + "settlement releases once without refunding the admitted turn")) + (test-assert (eq ':active (getf goal :status)) + "known zero usage does not quarantine the mission")) + (mission-detach application)))))) nil) (-> test-context-admission-mission-refusal-proof () null) @@ -518,54 +496,53 @@ "Require admission origin and no earlier admitted request before certifying zero." (with-test-environment (("AUTOLITH_CONTEXT_WINDOW" "5000") ("AUTOLITH_CONTEXT_OUTPUT_RESERVE" "256")) - (dolist (native-p '(nil t)) - (dolist (mode '(:synthetic :outside-boundary :after-admission :second-refusal :caught-refusal)) - (with-test-configuration (configuration) - (let* ((provider (make-instance 'context-admission-test-provider :configuration configuration)) - (application (mission-inference-test--application configuration provider)) - (conversation (application-conversation application)) - (open (if native-p #'provider-open-native-compaction #'provider-open-response-stream)) - (large (json-object "max_output_tokens" 6000)) - (entered-p (member mode '(:after-admission :second-refusal)))) - (application-mission-start application (mission-test--specification :turns 4 :tokens 100)) - (unwind-protect - (let* ((context (mission-context-find conversation)) - (goal (mission-context-goal context))) - (test-assert - (handler-case - (mission--account-inference - context - (lambda () - (when entered-p (funcall open provider (json-object))) - (case mode - (:outside-boundary - (provider-check-request-context provider large)) - (:second-refusal - (funcall open provider large)) - (:caught-refusal - (handler-case (funcall open provider large) - (provider-context-budget-error () nil)) - (error "An unrelated later failure.")) - (t - (error 'provider-context-budget-error - :message "Synthetic admission-like failure." - :estimated-input-tokens 6000 :output-reserve 256 - :context-window 5000 :non-text-estimate-p nil)))) - (lambda (results) (declare (ignore results)) nil)) - (mission-error () t)) - "the supervised failure propagates") - (test-assert (= (if entered-p 1 0) - (context-admission-test-provider-network-calls provider)) - "the negative control records its actual transport entries") - (test-assert (and (= 1 (getf goal :unknown-usage)) - (eq ':blocked (getf goal :status))) - "a condition name or later local refusal cannot prove zero usage") - (test-assert (and (zerop (getf goal :tokens-used)) - (= 1 (getf goal :turns-used)) - (zerop (getf goal :tokens-reserved)) - (zerop (getf goal :requests-outstanding))) - "uncertain settlement releases its reservation exactly once")) - (mission-detach application))))))) + (dolist (mode '(:synthetic :outside-boundary :after-admission :second-refusal :caught-refusal)) + (with-test-configuration (configuration) + (let* ((provider (make-instance 'context-admission-test-provider :configuration configuration)) + (application (mission-inference-test--application configuration provider)) + (conversation (application-conversation application)) + (open #'provider-open-response-stream) + (large (json-object "max_output_tokens" 6000)) + (entered-p (member mode '(:after-admission :second-refusal)))) + (application-mission-start application (mission-test--specification :turns 4 :tokens 100)) + (unwind-protect + (let* ((context (mission-context-find conversation)) + (goal (mission-context-goal context))) + (test-assert + (handler-case + (mission--account-inference + context + (lambda () + (when entered-p (funcall open provider (json-object))) + (case mode + (:outside-boundary + (provider-check-request-context provider large)) + (:second-refusal + (funcall open provider large)) + (:caught-refusal + (handler-case (funcall open provider large) + (provider-context-budget-error () nil)) + (error "An unrelated later failure.")) + (t + (error 'provider-context-budget-error + :message "Synthetic admission-like failure." + :estimated-input-tokens 6000 :output-reserve 256 + :context-window 5000 :non-text-estimate-p nil)))) + (lambda (results) (declare (ignore results)) nil)) + (mission-error () t)) + "the supervised failure propagates") + (test-assert (= (if entered-p 1 0) + (context-admission-test-provider-network-calls provider)) + "the negative control records its actual transport entries") + (test-assert (and (= 1 (getf goal :unknown-usage)) + (eq ':blocked (getf goal :status))) + "a condition name or later local refusal cannot prove zero usage") + (test-assert (and (zerop (getf goal :tokens-used)) + (= 1 (getf goal :turns-used)) + (zerop (getf goal :tokens-reserved)) + (zerop (getf goal :requests-outstanding))) + "uncertain settlement releases its reservation exactly once")) + (mission-detach application)))))) nil) (-> test-context-admission-mission-code-assist-envelope () null) @@ -1085,31 +1062,20 @@ (test-assert (conversation-context-usage-sample conversation) "ordinary provider metadata does not discard the latest measured anchor") (let ((sample (copy-tree (conversation-context-usage-sample conversation)))) - (loop for kind in '(:native :summary) - for request-number from 2 - do (agent--note-compaction-request - agent (make-instance 'agent-observer) :request-number request-number - :kind kind :usage (json-object "input_tokens" 1200 "output_tokens" 200)) - (test-assert (equal sample (conversation-context-usage-sample conversation)) - "compaction request metering preserves the live ordinary anchor") - (test-assert (equal sample (conversation-context-usage-sample - (conversation-load (conversation-pathname conversation)))) - "disk replay preserves the ordinary anchor after compaction metering"))) + (agent--note-compaction-request + agent (make-instance 'agent-observer) :request-number 2 + :usage (json-object "input_tokens" 1200 "output_tokens" 200)) + (test-assert (equal sample (conversation-context-usage-sample conversation)) + "compaction request metering preserves the live ordinary anchor") + (test-assert (equal sample (conversation-context-usage-sample + (conversation-load (conversation-pathname conversation)))) + "disk replay preserves the ordinary anchor after compaction metering")) (conversation-append-summary conversation (make-continuation :intent "A replacement context.")) (test-assert (null (conversation-context-usage-sample conversation)) "summary publication invalidates the sample") (test-assert (null (conversation-context-usage-sample (conversation-load (conversation-pathname conversation)))) "disk replay invalidates the sample at summary publication") (context-admission-test--measure agent :envelope-length 64) - (conversation-append-native-compaction - conversation (list (json-object "type" "compaction" "encrypted_content" "checkpoint")) - :family (provider-family provider) :continuation (make-continuation :intent "A native replacement context.")) - (test-assert (null (conversation-context-usage-sample conversation)) - "native checkpoint publication invalidates the live sample") - (test-assert (null (conversation-context-usage-sample - (conversation-load (conversation-pathname conversation)))) - "disk replay invalidates the sample at native checkpoint publication") - (context-admission-test--measure agent :envelope-length 64) (test-assert (conversation-context-usage-sample (conversation-load (conversation-pathname conversation))) "disk resume restores fingerprint evidence for revalidation")))) diff --git a/tests/context-tests.lisp b/tests/context-tests.lisp index 950a10278..54920d0da 100644 --- a/tests/context-tests.lisp +++ b/tests/context-tests.lisp @@ -529,16 +529,16 @@ (defun test-context-estimate-provider-family () "Estimate the native window only for the provider family that can use it." (with-test-configuration (configuration) - (let* ((conversation (conversation-create configuration)) + (let* ((checkpoint (json-object "type" "context_compaction" + "encrypted_content" + (make-string 4000 :initial-element #\c))) + (conversation (test-conversation-with-native-window + configuration (conversation-create configuration) (list checkpoint) + :family ':codex :continuation (make-continuation :intent "Portable handoff."))) (application (make-instance 'application :configuration configuration :conversation conversation - :provider (make-instance 'native-scripted-provider))) - (checkpoint (json-object "type" "context_compaction" - "encrypted_content" - (make-string 4000 :initial-element #\c)))) - (conversation-append-native-compaction - conversation (list checkpoint) :family ':codex :continuation (make-continuation :intent "Portable handoff.")) + :provider (make-instance 'codex-scripted-provider)))) (test-call-with-function-replacements (list (list 'system-prompt (lambda (configuration) diff --git a/tests/conversation-tests.lisp b/tests/conversation-tests.lisp index bb9ebbf2e..d7033afcf 100644 --- a/tests/conversation-tests.lisp +++ b/tests/conversation-tests.lisp @@ -384,7 +384,7 @@ (-> test-conversation-native-compaction () null) (defun test-conversation-native-compaction () - "Test durable opaque compaction with a checkpoint every family reads." + "Test a stored opaque window with a checkpoint every family reads." (let* ((configuration (test-configuration)) (root (test-configuration-root configuration))) (unwind-protect @@ -399,8 +399,10 @@ (list :request-number 1 :response-id "before-native-compact" :usage '(("total_tokens" 4321)))) - (conversation-append-native-compaction - conversation (list item) :family ':codex :continuation (make-continuation :intent "Portable handoff.")) + (setf conversation + (test-conversation-with-native-window + configuration conversation (list item) + :family ':codex :continuation (make-continuation :intent "Portable handoff."))) (test-assert (and (= (length (conversation-input-items conversation)) 2) (native-compaction-item-p diff --git a/tests/mission-tests.lisp b/tests/mission-tests.lisp index ae54f9b03..0e3d4f1fa 100644 --- a/tests/mission-tests.lisp +++ b/tests/mission-tests.lisp @@ -969,31 +969,6 @@ (test-assert (job-terminal-p job) "explicit mission cancellation publishes queued child termination")))) (task-orchestrator-close orchestrator))))) -(defclass mission-test-native-provider (scripted-provider) () - (:documentation "A scripted provider reporting separate native-compaction usage.")) - -(defmethod provider-native-compact-conversation - ((provider mission-test-native-provider) (conversation conversation) - &key tool-namespaces event-callback) - "Return one compacted item and exact native inference usage." - (declare (ignore provider conversation tool-namespaces event-callback)) - (values (json-object "type" "compaction") (json-object "input_tokens" 2 "output_tokens" 3))) - -(-> test-mission-native-compaction-budget () null) -(defun test-mission-native-compaction-budget () - "Include native compaction usage and retain exact reported token exhaustion." - (with-test-configuration (configuration) - (let ((application (mission-test--application configuration))) - (application-mission-start application (mission-test--specification :tokens 4)) - (multiple-value-bind (item usage) - (provider-native-compact-conversation - (make-instance 'mission-test-native-provider :results nil) - (application-conversation application) :tool-namespaces #() :event-callback #'identity) - (test-assert (and item usage) "native compaction preserves both protocol values")) - (test-assert (= 1 (getf (application-goal application) :turns-used)) "native compaction charges an inference admission") - (test-assert (= 5 (getf (application-goal application) :tokens-used)) "reported usage is recorded without clipping it to the limit") - (test-assert (eq ':exhausted (getf (application-goal application) :status)) "reported token exhaustion is a durable terminal state")))) - (-> test-mission-inline-parent-cancellation () null) (defun test-mission-inline-parent-cancellation () "Interrupt a blocked inline mission through its explicit cross-pool parent link." diff --git a/tests/native-compaction-window-tests.lisp b/tests/native-compaction-window-tests.lisp index 1f1c65a6f..fc977dd5c 100644 --- a/tests/native-compaction-window-tests.lisp +++ b/tests/native-compaction-window-tests.lisp @@ -1,79 +1,15 @@ (in-package #:autolith) -;;;; -- Native Compaction Window Contracts -- +;;;; -- Stored Native Compaction Windows -- (-> native-compaction-tests--opaque (string) json-object) (defun native-compaction-tests--opaque (content) "Return a native item containing CONTENT as opaque provider state." (json-object "type" "compaction" "encrypted_content" content)) -(-> test-native-compaction-window-validation () null) -(defun test-native-compaction-window-validation () - "Preserve complete windows and account successful envelopes before rejecting bad items." - (with-test-configuration (configuration) - (let* ((provider (provider-create configuration)) - (usage (json-object "input_tokens" 100 "output_tokens" 10)) - (message (json-object "type" "message" "role" "user" "content" - (json-array (json-object "type" "input_text" "text" "exact request")))) - (call (json-object "type" "function_call" "call_id" "native-call" - "name" "probe" "arguments" "{}")) - (output (function-call-output-item "native-call" "verified result")) - (opaque (native-compaction-tests--opaque "opaque-one")) - (window (list message opaque call output - (native-compaction-tests--opaque "opaque-two"))) - (events nil)) - (flet ((decode (items) - "Decode ITEMS and capture each completed usage notification." - (provider--decode-native-compaction-response - provider (json-encode (json-object "id" "paid-native" "usage" usage - "output" (coerce items 'vector))) - :status 200 :headers nil - :event-callback (lambda (event) (push event events))))) - (multiple-value-bind (decoded returned-usage) (decode window) - (test-assert (= 5 (length decoded)) "the entire native output window survives decoding") - (test-assert (equal '("message" "compaction" "function_call" "function_call_output" "compaction") - (mapcar (lambda (item) (json-get item "type")) decoded)) - "native output order is exact, including both opaque items") - (test-assert (= 100 (json-get returned-usage "input_tokens")) "paid usage is returned")) - (test-assert (= 1 (length events)) "one successful native envelope emits one completion") - (dolist (body '("" "not-json" "[]" "null")) - (let ((before (length events))) - (test-assert - (handler-case - (progn - (provider--decode-native-compaction-response - provider body :status 200 :headers nil - :event-callback (lambda (event) (push event events))) - nil) - (provider-error () t)) - "an undecodable HTTP success body is rejected") - (test-assert (and (= (1+ before) (length events)) - (null (native-compaction-completed-event-usage (first events)))) - "an undecodable completion is metered once with unknown usage"))) - (multiple-value-bind (decoded returned-usage) (decode (list message)) - (test-assert (and (null decoded) (= 100 (json-get returned-usage "input_tokens"))) - "an opaque-free successful response retains usage for portable fallback")) - (dolist (bad (list (list opaque (json-object)) - (list opaque (json-object "type" "message" "role" "user" "content" "bad")) - (list opaque (json-object "type" "message" "role" "assistant" "content" - (json-array (json-object "type" "refusal" "refusal" "no")))) - (list opaque output) - (list opaque call) - (list opaque call call output))) - (let ((before (length events))) - (test-assert - (handler-case (progn (decode bad) nil) (provider-error () t)) - "malformed, refusal and invalid-correlation windows are rejected") - (test-assert (= (1+ before) (length events)) - "the rejected paid response was already metered") - (test-assert (= 100 (json-get (native-compaction-completed-event-usage (first events)) - "input_tokens")) - "the notification preserves reported usage")))))) - nil) - (-> test-native-compaction-window-replay () null) (defun test-native-compaction-window-replay () - "Replay full native windows and project checkpoints across provider families." + "Replay stored native windows and project checkpoints across provider families." (with-test-configuration (configuration) (let* ((conversation (conversation-create configuration)) (window (list (native-compaction-tests--opaque "first") @@ -81,21 +17,18 @@ "encrypted_content" "retained-reasoning") (native-compaction-tests--opaque "last")))) (conversation-append-user-message conversation "original") - (conversation-append-native-compaction conversation window - :family ':responses :continuation (make-continuation :intent "portable handoff")) - (let ((loaded (conversation-load-by-id configuration (conversation-identifier conversation)))) + (let ((loaded (test-conversation-with-native-window + configuration conversation window + :family ':responses :continuation (make-continuation :intent "portable handoff")))) (dolist (family '(:responses :messages)) - (let ((before (conversation-input-items-for-family conversation family)) - (after (conversation-input-items-for-family loaded family))) - (test-assert (equal (mapcar #'json-encode before) (mapcar #'json-encode after)) - "reload reproduces the exact family projection") + (let ((items (conversation-input-items-for-family loaded family))) (test-assert - (and (test-object-contains-string-p (last after) "portable handoff") - (equal (mapcar #'json-encode (butlast after)) + (and (test-object-contains-string-p (last items) "portable handoff") + (equal (mapcar #'json-encode (butlast items)) (and (eq family ':responses) (mapcar #'json-encode window)))) "the source family receives the full ordered window before the checkpoint and other families only the checkpoint") (test-assert (= (if (eq family ':responses) 2 0) - (count-if #'native-compaction-item-p after)) + (count-if #'native-compaction-item-p items)) "all opaque items belong exclusively to their originating family")))) (let ((legacy (conversation-create configuration))) (conversation-append-record diff --git a/tests/precompaction-tests.lisp b/tests/precompaction-tests.lisp index fed5a3837..1869bba39 100644 --- a/tests/precompaction-tests.lisp +++ b/tests/precompaction-tests.lisp @@ -225,62 +225,56 @@ (-> test-precompaction-before-compaction () null) (defun test-precompaction-before-compaction () - "Compact a served, warm prefix under threshold pressure with native or portable summaries." - (dolist (native-p '(nil t)) - (with-test-configuration (configuration) - (let* ((original (conversation-create configuration :identifier "precompaction-full")) - (provider (make-instance - (if native-p 'native-scripted-provider 'scripted-provider) - :configuration configuration - :results (list (agent-test-result "summary" (list (agent-test-checkpoint-call)) - :turn-completion ':end) - (mission-test--result "Done."))))) - (when native-p - (setf (native-scripted-provider-native-items provider) - (list (json-object "type" "compaction" "encrypted_content" "checkpoint")))) - (conversation-append-user-message original "start") - (precompaction-tests--large-exchange original "failed-earlier" nil) - (precompaction-tests--large-exchange original "succeeded" t) - (conversation-append-user-message original "continue") - (precompaction-tests--exchange original "failed-current") - (conversation-append-provider-metadata - original (list :request-number 1 - :request-identity (list (provider-family provider) (config :model configuration)))) - ;; Force threshold pressure independently of token estimation. - (let* ((conversation (conversation-load-by-id configuration "precompaction-full")) - (agent (agent-create :configuration configuration - :provider provider - :conversation conversation - :tool-registry (agent-test-registry) - :worker nil))) - (test-assert (zerop (agent--prepare-provider-request agent)) - "a served unknown-lifetime prefix stays unbounded before threshold compaction") - (test-call-with-function-replacements - (list (list 'agent-should-compact-p - (lambda (agent) - (declare (ignore agent)) - (null (scripted-provider-compaction-flags provider))))) - (lambda () (agent-run-user-turn agent "next question"))) - (test-assert (equal '(t nil) (reverse (scripted-provider-compaction-flags provider))) - "threshold compaction precedes the ordinary request") - (let* ((items (if native-p - (first (native-scripted-provider-native-input-snapshots provider)) - (append (first (reverse (scripted-provider-input-snapshots provider))) - (conversation-input-items conversation)))) - (calls (loop for item in items - when (clinker-transcript:function-call-item-p item) - collect (json-get item "call_id")))) - (test-assert (equal '("failed-earlier" "succeeded" "failed-current") calls) - "compaction receives every failed and successful exchange") - (test-assert (find-if (lambda (item) - (let ((output (json-get item "output"))) - (and (stringp output) (= 10000 (length output))))) - items) - "threshold compaction receives the intact served oversized evidence") - (test-assert (find (if native-p :native-compaction :summary) - (conversation--read-records (conversation-pathname conversation)) - :key #'first) - "threshold compaction publishes a durable native or portable checkpoint")))))) + "Compact a served, warm prefix under threshold pressure." + (with-test-configuration (configuration) + (let* ((original (conversation-create configuration :identifier "precompaction-full")) + (provider (make-instance + 'scripted-provider + :configuration configuration + :results (list (agent-test-result "summary" (list (agent-test-checkpoint-call)) + :turn-completion ':end) + (mission-test--result "Done."))))) + (conversation-append-user-message original "start") + (precompaction-tests--large-exchange original "failed-earlier" nil) + (precompaction-tests--large-exchange original "succeeded" t) + (conversation-append-user-message original "continue") + (precompaction-tests--exchange original "failed-current") + (conversation-append-provider-metadata + original (list :request-number 1 + :request-identity (list (provider-family provider) (config :model configuration)))) + ;; Force threshold pressure independently of token estimation. + (let* ((conversation (conversation-load-by-id configuration "precompaction-full")) + (agent (agent-create :configuration configuration + :provider provider + :conversation conversation + :tool-registry (agent-test-registry) + :worker nil))) + (test-assert (zerop (agent--prepare-provider-request agent)) + "a served unknown-lifetime prefix stays unbounded before threshold compaction") + (test-call-with-function-replacements + (list (list 'agent-should-compact-p + (lambda (agent) + (declare (ignore agent)) + (null (scripted-provider-compaction-flags provider))))) + (lambda () (agent-run-user-turn agent "next question"))) + (test-assert (equal '(t nil) (reverse (scripted-provider-compaction-flags provider))) + "threshold compaction precedes the ordinary request") + (let* ((items (append (first (reverse (scripted-provider-input-snapshots provider))) + (conversation-input-items conversation))) + (calls (loop for item in items + when (clinker-transcript:function-call-item-p item) + collect (json-get item "call_id")))) + (test-assert (equal '("failed-earlier" "succeeded" "failed-current") calls) + "compaction receives every failed and successful exchange") + (test-assert (find-if (lambda (item) + (let ((output (json-get item "output"))) + (and (stringp output) (= 10000 (length output))))) + items) + "threshold compaction receives the intact served oversized evidence") + (test-assert (find :summary + (conversation--read-records (conversation-pathname conversation)) + :key #'first) + "threshold compaction publishes a durable checkpoint"))))) nil) (-> test-precompaction-retention-policy () null) diff --git a/tests/provider-tests.lisp b/tests/provider-tests.lisp index b39104762..02c034584 100644 --- a/tests/provider-tests.lisp +++ b/tests/provider-tests.lisp @@ -738,9 +738,7 @@ "output_schema" nil)))))) (replayed (provider-wire-input-item provider search-output)) (replayed-namespace (aref (json-get replayed "tools") 0)) - (replayed-child (aref (json-get replayed-namespace "tools") 0)) - (trimmed (let ((*provider-history-trimming-p* t)) - (provider-wire-input-item provider search-output)))) + (replayed-child (aref (json-get replayed-namespace "tools") 0))) (test-assert (and (json-string= (json-get replayed "type") "tool_search_output") (= (length (json-get replayed "tools")) 1) @@ -751,11 +749,6 @@ (nth-value 1 (json-get-present replayed-child "strict")) (json-object-p (json-get replayed-child "parameters"))) "tool search expansions replay intact in the deferred wire shape") - (test-assert - (and (json-string= (json-get trimmed "type") "tool_search_output") - (zerop (length (json-get trimmed "tools"))) - (= (length (json-get search-output "tools")) 1)) - "history trimming replays tool search expansions empty") (test-assert (and (family-private-item-p search-output) (family-private-item-p @@ -1032,233 +1025,6 @@ (platform-delete-directory-tree *platform* root :validate t :if-does-not-exist ':ignore))) nil) -(-> test-provider-native-compaction () null) -(defun test-provider-native-compaction () - "Test the Codex native checkpoint request, transport, and fallback boundary." - (let* ((configuration (test-configuration)) - (root (test-configuration-root configuration)) - (conversation - (conversation-create configuration :identifier "native-request")) - (provider (provider-create configuration)) - (credentials (provider-tests--credentials configuration)) - (schemas - (json-array - (json-object "type" "namespace" - "name" "test" - "description" "Test tools." - "tools" (json-array))))) - (unwind-protect - (progn - (conversation-append-user-message conversation "retain this work") - (conversation-append-provider-item - conversation - (json-object "type" "reasoning" - "encrypted_content" "retained-reasoning" - "summary" (json-array))) - (conversation-append-provider-item - conversation - (json-object "type" "function_call" - "call_id" "ephemeral-call" - "name" "test" - "arguments" "{}") - :persistence ':next-response) - (let* ((request - (provider-native-compaction-request-object - provider conversation schemas)) - (input (json-get request "input"))) - (test-assert - (and (= (length input) 2) - (string= (json-get (aref input 0) "role") "user")) - "native compaction carries durable standard Responses input") - (test-assert - (and (reasoning-item-p (aref input 1)) - (string= (json-get (aref input 1) "encrypted_content") - "retained-reasoning")) - "native compaction carries retained encrypted reasoning") - (test-assert - (non-empty-string-p (json-get request "instructions")) - "native compaction uses top-level instructions") - (test-assert - (string= (json-get request "prompt_cache_key") - (conversation-prompt-cache-key conversation)) - "native compaction shares the root conversation cache key") - (test-assert (null (json-get request "service_tier")) - "standard native compaction omits a service tier") - (let* ((fast-configuration - (configuration-copy configuration :codex-fast-mode-p t)) - (fast-provider (provider-create fast-configuration)) - (fast-request - (provider-native-compaction-request-object - fast-provider conversation schemas))) - (test-assert - (string= (json-get fast-request "service_tier") "priority") - "Codex Fast mode applies to native compaction")) - (let* ((unknown-configuration - (configuration-copy configuration - :model "gpt-future-unknown" - :codex-fast-mode-p t - :provider-validation-p nil)) - (unknown-provider (provider-create unknown-configuration)) - (unknown-request - (provider-native-compaction-request-object - unknown-provider conversation schemas))) - (test-assert - (null (json-get unknown-request "service_tier")) - "unknown Codex models omit Fast mode during compaction")) - (dolist (name '("stream" "store" "include" "tool_choice" - "parallel_tool_calls" "reasoning" "text" "tools")) - (multiple-value-bind (value present-p) (gethash name request) - (declare (ignore value)) - (test-assert (not present-p) - (format nil "native compaction omits ~A" name))))) - (let ((captured-url nil) - (captured-headers nil) - (captured-content nil)) - (test-call-with-function-replacements - (list - (list - 'dexador:post - (lambda (url &key headers content &allow-other-keys) - (setf captured-url url - captured-headers headers - captured-content content) - (values "{\"output\":[]}" 200 nil)))) - (lambda () - (provider-open-native-compaction - provider - (json-object "model" *default-model*) - :credentials credentials - :conversation conversation))) - (flet ((header (name) - (rest (assoc name captured-headers :test #'string-equal)))) - (test-assert - (string= captured-url - "https://chatgpt.com/backend-api/codex/responses/compact") - "native compaction derives the Responses compact endpoint") - (test-assert (string= (header "Accept") "application/json") - "native compaction requests one JSON response") - (test-assert - (typep captured-content '(array (unsigned-byte 8) (*))) - "native compaction sends JSON as direct UTF-8 octets") - (test-assert - (string= - (json-get - (json-decode - (sb-ext:octets-to-string - captured-content :external-format ':utf-8)) - "model") - *default-model*) - "direct UTF-8 request content retains its JSON payload") - (test-assert - (string= (header "Authorization") - "Bearer provider-test-access-7f386d") - "native compaction uses the ordinary subscription credentials"))) - (credential-source-save - (credential-manager-primary-source - (provider-credential-manager provider)) - credentials) - (let ((result nil)) - (test-call-with-function-replacements - (list - (list - 'dexador:post - (lambda (&rest arguments) - (declare (ignore arguments)) - (values - (json-encode - (json-object - "output" - (json-array - (json-object "type" "message" - "role" "user" - "content" - (json-array - (json-object "type" "input_text" - "text" "retained user"))) - (json-object "id" "cmp_123" - "type" "compaction" - "encrypted_content" "opaque-checkpoint") - (json-object "type" "message" - "role" "assistant" - "content" - (json-array - (json-object "type" "output_text" - "text" "retained assistant")))))) - 200 - '(("x-request-id" . "native-success")))))) - (lambda () - (setf result - (provider-native-compact-conversation - provider conversation - :tool-namespaces schemas - :event-callback #'identity)))) - (test-assert - (and (listp result) - (= (length result) 3) - (string= (json-get (first result) "role") "user") - (native-compaction-item-p (second result)) - (string= (json-get (third result) "role") "assistant")) - "native compaction returns the complete ordered output window") - (multiple-value-bind (value present-p) (gethash "id" (second result)) - (declare (ignore value)) - (test-assert (not present-p) - "native compaction drops transient server item identifiers"))) - (let ((result - (provider--decode-native-compaction-response - provider - (json-encode - (json-object - "output" - (json-array - (json-object "type" "compaction_summary" - "encrypted_content" "legacy-checkpoint") - (json-object "type" "context_compaction" - "encrypted_content" "current-checkpoint"))))))) - (test-assert - (and (listp result) - (= (length result) 2) - (native-compaction-item-p (second result)) - (string= (json-get (second result) "type") "context_compaction") - (string= (json-get (second result) "encrypted_content") - "current-checkpoint")) - "native compaction keeps the complete window and newest checkpoint encoding")) - (let ((result - (provider--decode-native-compaction-response - provider - (json-encode - (json-object - "output" - (json-array - (json-object "type" "compaction_summary" - "encrypted_content" "legacy-checkpoint"))))))) - (test-assert - (and (listp result) - (= (length result) 1) - (string= (json-get (first result) "type") "compaction")) - "native compaction canonicalizes Codex's legacy checkpoint alias")) - (test-assert - (null - (provider--decode-native-compaction-response - provider (json-encode (json-object "output" (json-array))))) - "an opaque-free compact transcript falls back to the portable summary") - (test-call-with-function-replacements - (list - (list - 'dexador:post - (lambda (&rest arguments) - (declare (ignore arguments)) - (values "not found" 404 nil)))) - (lambda () - (test-assert - (null - (provider-native-compact-conversation - provider conversation - :tool-namespaces schemas - :event-callback #'identity)) - "an unavailable native endpoint falls back without failing compaction")))) - (platform-delete-directory-tree *platform* root :validate t :if-does-not-exist ':ignore))) - nil) - (-> test-sse-event-string (json-object) string) (defun test-sse-event-string (event) "Encode EVENT as one complete server-sent event." @@ -1376,30 +1142,6 @@ (t (values outcome 200 nil))))) -(defmethod provider-open-native-compaction - ((provider test-transport-provider) - (request hash-table) - &key credentials conversation) - "Signal the next scripted transport outcome for native compaction." - (declare (ignore request credentials conversation)) - (incf (test-transport-provider-attempt-count provider)) - (let ((outcome (pop (test-transport-provider-outcomes provider)))) - (cond - ((typep outcome 'condition) - (error outcome)) - ((eq outcome ':name-service) - (error 'sb-bsd-sockets:name-service-error - :errno 1 - :symbol 'getaddrinfo - :syscall 'getaddrinfo)) - ((eq outcome ':tls) - (error 'test-provider-tls-error - :detail "compaction certificate verification failed")) - ((and (consp outcome) (eq (first outcome) ':tls)) - (error 'test-provider-tls-error :detail (second outcome))) - (t - (values outcome 200 nil))))) - (-> provider-tests--tls-eof-condition () cl+ssl::ssl-error-ssl) (defun provider-tests--tls-eof-condition () "Return the pinned cl+ssl condition for an OpenSSL 3+ unclean peer EOF." @@ -1481,7 +1223,7 @@ (-> test-provider-tls-eof-recovery () null) (defun test-provider-tls-eof-recovery () - "Reconnect on TLS EOF across authenticated opens, SSE reads, and native compaction." + "Reconnect on TLS EOF across authenticated opens and SSE reads." (with-test-configuration (configuration) (let* ((conversation (conversation-create configuration)) (credentials (provider-tests--credentials configuration)) @@ -1489,11 +1231,10 @@ (lambda (seconds) (declare (ignore seconds))))) (conversation-append-user-message conversation "continue after reconnecting") - (dolist (boundary '(:open :stream-before-output :stream-after-output :native)) - (let* ((native-p (eq boundary ':native)) - (output-p (eq boundary ':stream-after-output)) + (dolist (boundary '(:open :stream-before-output :stream-after-output)) + (let* ((output-p (eq boundary ':stream-after-output)) (first-outcome - (if (member boundary '(:open :native)) + (if (eq boundary ':open) (provider-tests--tls-eof-condition) (make-instance 'test-tls-eof-stream @@ -1508,15 +1249,9 @@ (json-object "type" "response.output_text.delta" "delta" "partial")) ""))))) (success-outcome - (if native-p - (json-encode - (json-object "output" - (json-array - (json-object "type" "compaction" - "encrypted_content" "tls-checkpoint")))) - (make-instance - 'test-character-input-stream - :source (provider-tests--completed-sse-source "tls-reconnected")))) + (make-instance + 'test-character-input-stream + :source (provider-tests--completed-sse-source "tls-reconnected"))) (provider (provider-tests--transport-provider configuration (list first-outcome success-outcome))) @@ -1525,22 +1260,13 @@ (credential-manager-primary-source (provider-credential-manager provider)) credentials) (let ((result - (if native-p - (provider-native-compact-conversation - provider conversation - :tool-namespaces #() - :event-callback (lambda (event) (push event events))) - (provider-stream-turn - provider conversation - :tool-namespaces #() - :event-callback (lambda (event) (push event events)))))) + (provider-stream-turn + provider conversation + :tool-namespaces #() + :event-callback (lambda (event) (push event events))))) (test-assert (and (= (test-transport-provider-attempt-count provider) 2) - (if native-p - (and (= 1 (length result)) - (native-compaction-item-p (first result)) - (string= (json-get (first result) "encrypted_content") "tls-checkpoint")) - (string= (provider-result-response-id result) "tls-reconnected"))) + (string= (provider-result-response-id result) "tls-reconnected")) "each TLS EOF boundary reconnects and returns the next successful response")) (let ((failures (remove-if-not @@ -1650,40 +1376,6 @@ (autolith-error-message condition) "The provider TLS connection could not be established: certificate rejected [redacted]")))) "TLS failures append useful credential-redacted condition detail")) - (let ((provider - (provider-tests--transport-provider - configuration - (list '(:tls "compaction certificate expired"))))) - (test-assert - (handler-case - (progn - (provider--open-native-compaction - provider - (json-object) - :credentials credentials - :conversation conversation) - nil) - (provider-error (condition) - (string= - (autolith-error-message condition) - "The provider TLS connection could not be established: compaction certificate expired"))) - "native compaction preserves TLS condition detail")) - (let ((provider - (provider-tests--transport-provider - configuration - (list ':name-service)))) - (test-assert - (handler-case - (progn - (provider--open-native-compaction - provider - (json-object) - :credentials credentials - :conversation conversation) - nil) - (provider-transport-error () - t)) - "native compaction normalizes an SBCL name-service error")) (let ((provider (provider-tests--transport-provider configuration @@ -2783,8 +2475,6 @@ (lambda () (provider-open-response-stream codex (json-object) :credentials credentials :conversation conversation) - (provider-open-native-compaction - codex (json-object) :credentials credentials :conversation conversation) (openai-compatible--fetch-models configuration :provider-name "deadline-models" @@ -2802,7 +2492,7 @@ (provider-open-response-stream gemini (json-object) :credentials credentials :conversation conversation))) (test-assert - (equal (nreverse deadlines) '(300 300 30 300 300 300 300 300)) + (equal (nreverse deadlines) '(300 30 300 300 300 300 300)) "every provider response and stream-open path uses its response deadline")) (platform-delete-directory-tree *platform* root :validate t :if-does-not-exist ':ignore))) nil) diff --git a/tests/telemetry-hook-model-tests.lisp b/tests/telemetry-hook-model-tests.lisp index f8399f5c9..cef49b6d9 100644 --- a/tests/telemetry-hook-model-tests.lisp +++ b/tests/telemetry-hook-model-tests.lisp @@ -283,14 +283,12 @@ (-> test-telemetry-compaction-roles () null) (defun test-telemetry-compaction-roles () - "Classify native checkpoint and summary requests at the actual compaction entry point." + "Classify the checkpoint request at the actual compaction entry point." (test-telemetry--call (lambda (configuration) (let* ((conversation (conversation-create configuration)) - (provider (make-instance 'native-scripted-provider + (provider (make-instance 'scripted-provider :configuration configuration - :native-items (list (json-object "type" "compaction" - "encrypted_content" "fixture")) :results (list (agent-test-result "summary" (list (agent-test-checkpoint-call)) :turn-completion ':end)))) @@ -304,10 +302,10 @@ configuration (lambda () (agent-compact-conversation agent (make-instance 'agent-observer))))))) (models (test-telemetry-hooks--kind (first bodies) "model"))) - (test-assert (= (length models) 2) "Native checkpoint and summary each export an attempt.") - (dolist (model models) - (test-assert (equal (test-telemetry--attribute model "autolith.request.role") "compaction") - "Both compaction requests have the compaction purpose.")) + (test-assert (= (length models) 1) "The checkpoint request exports one attempt.") + (test-assert (equal (test-telemetry--attribute (first models) "autolith.request.role") + "compaction") + "The checkpoint request has the compaction purpose.") (test-telemetry--collector-fixture "native-compaction.json" (json-decode (first bodies))))))) nil) diff --git a/tests/telemetry-hook-provider-tests.lisp b/tests/telemetry-hook-provider-tests.lisp index 25ce88a2b..6cc04da70 100644 --- a/tests/telemetry-hook-provider-tests.lisp +++ b/tests/telemetry-hook-provider-tests.lisp @@ -231,24 +231,14 @@ (declare (ignore conversation tool-namespaces event-callback goal-context compaction-p)) (values (pop (scripted-provider-results provider)) :secondary 17)) -(defclass telemetry-test-mission-provider (telemetry-test-values-provider) () - (:documentation "Custom streaming and native-compaction provider for mission accounting.")) - -(defmethod provider-native-compact-conversation - ((provider telemetry-test-mission-provider) (conversation conversation) - &key tool-namespaces event-callback) - "Return a compacted marker, fixed usage and an extra secondary value." - (declare (ignore provider conversation tool-namespaces event-callback)) - (values :compacted (json-object "input_tokens" 2 "output_tokens" 3) :secondary)) - (-> test-telemetry-hooks--mission-accounting () null) (defun test-telemetry-hooks--mission-accounting () - "Compose enabled telemetry with mission admission and both usage protocols." + "Compose enabled telemetry with mission admission." (test-telemetry--call (lambda (configuration) (let* ((result (mission-test--result "done")) - (provider (make-instance 'telemetry-test-mission-provider - :configuration configuration :results (list result result))) + (provider (make-instance 'telemetry-test-values-provider + :configuration configuration :results (list result result result))) (application (mission-test--application configuration)) (conversation (application-conversation application))) (setf (application-provider application) provider) @@ -266,12 +256,8 @@ :tool-namespaces #() :event-callback #'identity)) (list result :secondary 17)) "Mission streaming retains all provider values with telemetry enabled.") - (multiple-value-bind (item usage extra) - (provider-native-compact-conversation - provider conversation :tool-namespaces #() :event-callback #'identity) - (test-assert (and (eq item :compacted) (eq extra :secondary) - (= 2 (json-get usage "input_tokens"))) - "Mission native compaction retains all provider values.")) + (provider-stream-turn provider conversation + :tool-namespaces #() :event-callback #'identity) (test-assert (handler-case (progn (provider-stream-turn provider conversation @@ -282,8 +268,8 @@ (declare (ignore results)) (test-telemetry-hooks--assert-attempts (first bodies) 2)) (let ((goal (application-goal application))) - (test-assert (= 2 (getf goal :turns-used)) "Both protocols charge one mission turn.") - (test-assert (= 10 (getf goal :tokens-used)) "Both protocols settle exact reported usage.") + (test-assert (= 2 (getf goal :turns-used)) "Each request charges one mission turn.") + (test-assert (= 10 (getf goal :tokens-used)) "Each request settles exact reported usage.") (test-assert (eq :exhausted (getf goal :status)) "Mission exhaustion is durable.") (test-assert (= 1 (length (scripted-provider-results provider))) "Rejected inference does not consume a scripted response."))))) diff --git a/tests/test-suites.lisp b/tests/test-suites.lisp index ab8695dda..516f09935 100644 --- a/tests/test-suites.lisp +++ b/tests/test-suites.lisp @@ -54,18 +54,15 @@ test-compaction-request-view-isolation test-compaction-summary-mission-ownership test-compaction-summary-mission-failures - test-compaction-native-view-mission-ownership test-compaction-final-projection-gap test-compaction-final-admission-trigger test-compaction-mission-admission-trigger test-compaction-admission-recovery-bounds test-compaction-submission-acceptance - test-compaction-native-paid-rejection test-compaction-next-request-admission test-compaction-repeated-continuation-accounting) (define-test-suite native-compaction-window - test-native-compaction-window-validation test-native-compaction-window-replay) (define-test-suite continuation @@ -1116,7 +1113,6 @@ test-provider-prompt-cache-ttl test-provider-shared-items-breakpoint test-provider-cost - test-provider-native-compaction test-provider-rate-limits test-provider-context-presentation test-provider-transport-boundary @@ -1409,7 +1405,6 @@ test-agent-compaction-without-submission test-precompaction-before-compaction test-agent-compaction - test-agent-native-compaction test-agent-parallel-tool-wave test-agent-tool-wave-cancellation test-agent-tool-wave-retained-cleanup @@ -1872,8 +1867,7 @@ test-mission-wall-budget test-mission-command-and-compaction test-mission-queued-child-authority - test-mission-inline-parent-cancellation - test-mission-native-compaction-budget) + test-mission-inline-parent-cancellation) (define-test-suite lisp-machine test-application-lisp-evaluation @@ -2107,8 +2101,7 @@ test-compaction-recovery-recent-images test-compaction-recovery-image-text-controls test-compaction-recovery-image-summary-transport - test-unsupported-native-mission-fallback - test-unsupported-native-model-window + test-compaction-recovery-mission-model-window test-compaction-recovery-notice-counts test-compaction-recovery-encrypted-history test-compaction-recovery-publication-preserves-tail-families @@ -2119,27 +2112,24 @@ test-compaction-recovery-mission-accounting test-compaction-recovery-selected-route test-compaction-recovery-exhaustion - test-expanded-native-unavailable-negative-controls - test-expanded-native-unavailable-representations - test-expanded-native-unavailable-ordinary - test-expanded-native-unavailable-manual - test-expanded-native-unavailable-reactive - test-expanded-native-unavailable-schedule - test-expanded-native-unavailable-terminal-idle - test-expanded-native-unavailable-acp-idle - test-expanded-native-no-fit-product-boundaries - test-expanded-native-no-fit-acp - test-expanded-native-no-fit-acp-prompt - test-expanded-native-no-fit-acp-queue - test-expanded-native-no-fit-acp-completion - test-expanded-native-stream-fixture - test-expanded-native-paid-arrival-fixture - test-expanded-native-paid-owner-refusals - test-expanded-native-terminal-message-owners - test-expanded-native-acp-owner-matrix - test-expanded-native-idle-owner-matrix - test-expanded-native-image-owner-and-reuse - test-expanded-native-owner-return-values - test-expanded-native-idle-cancellation-refusal - test-expanded-native-owner-finalization-errors - test-expanded-native-scheduled-controller) + test-compaction-owner-proactive + test-compaction-owner-manual + test-compaction-owner-reactive + test-compaction-owner-schedule + test-compaction-owner-terminal-idle + test-compaction-owner-acp-idle + test-compaction-owner-no-fit-product-boundaries + test-compaction-owner-no-fit-acp + test-compaction-owner-no-fit-acp-prompt + test-compaction-owner-no-fit-acp-queue + test-compaction-owner-no-fit-acp-completion + test-compaction-owner-paid-arrival-fixture + test-compaction-owner-paid-refusals + test-compaction-owner-terminal-messages + test-compaction-owner-acp-matrix + test-compaction-owner-idle-matrix + test-compaction-owner-image-and-reuse + test-compaction-owner-return-values + test-compaction-owner-idle-cancellation-refusal + test-compaction-owner-finalization-errors + test-compaction-owner-scheduled-controller) diff --git a/tests/test-support.lisp b/tests/test-support.lisp index b3d5b4c5e..4329858c5 100644 --- a/tests/test-support.lisp +++ b/tests/test-support.lisp @@ -97,6 +97,22 @@ and history, which lets the provider read them from its cache." "a compaction request repeats the ordinary history before its own instructions"))) compaction)) +(-> test-conversation-with-native-window + (configuration conversation list &key (:family keyword) (:continuation list)) conversation) +(defun test-conversation-with-native-window + (configuration conversation window &key family continuation) + "Store native WINDOW in CONVERSATION as an earlier version did and return it reloaded. + +No request produces a native window any more. A test of a conversation that +still holds one writes the stored record and reads the conversation back." + (conversation-append-record + conversation (list :native-compaction :family family + :through-seq (1- (conversation-next-sequence conversation)) + :wire-json (json-encode (coerce window 'vector)) + :summary (continuation-render continuation) + :continuation continuation)) + (conversation-load-by-id configuration (conversation-identifier conversation))) + (-> test-object-contains-string-p (t string) boolean) (defun test-object-contains-string-p (root needle) "Return true when an ordinary object reachable from ROOT contains NEEDLE." From b232a558bfb2ff63c26512273f1133a08d56ad55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Hozda?= Date: Mon, 16 Nov 2026 00:17:16 +0100 Subject: [PATCH 2/2] Document compaction as one checkpoint request --- docs/guide.org | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/docs/guide.org b/docs/guide.org index f5e63bb26..98578f8c3 100644 --- a/docs/guide.org +++ b/docs/guide.org @@ -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 @@ -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