Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(auto-improve): keep admission outages out of rejections

lhzapatace44904d0ff3
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 59954396..ad4eb189 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,14 @@ version in [`docs/changelog/`](docs/changelog/):
## [Unreleased]
+### Fixed
+- Fixed auto-improve approvals that failed because a `reject`-policy admission
+ webhook was unreachable, timed out or answered 5xx being stored as
+ rejections. The proposal is still marked `failed`, but the outage no longer
+ enters the rejection buffer, so later reviews are not told to avoid a
+ proposal nobody refused and rejection telemetry counts only real
+ refusals. (#1358)
+
## [3.1.0] - 2026-10-16
### Added
diff --git a/crates/ai-memory-store/src/auto_improve.rs b/crates/ai-memory-store/src/auto_improve.rs
index 202bc343..b72f5dc8 100644
--- a/crates/ai-memory-store/src/auto_improve.rs
+++ b/crates/ai-memory-store/src/auto_improve.rs
@@ -470,6 +470,10 @@ pub struct FailAutoImproveProposal {
pub proposal_id: AutoImproveProposalId,
/// What went wrong.
pub reason: String,
+ /// Also record `reason` in `auto_improve_rejections`, which later review
+ /// runs read as patterns to avoid. `false` when the failure says nothing
+ /// about the proposal itself, such as an unreachable admission webhook.
+ pub record_rejection: bool,
/// Actor attribution for the decision event.
pub actor: ActorContext,
/// Deciding user, when identified.
@@ -998,8 +1002,9 @@ fn stage_run_impl(
})
}
-/// Mark a pending proposal `failed`, record the rejection fingerprint, and
-/// append a `failed` event.
+/// Mark a pending proposal `failed`, record the rejection fingerprint when
+/// [`FailAutoImproveProposal::record_rejection`] is set, and append a
+/// `failed` event.
///
/// # Errors
/// Returns [`StoreError::InvalidState`] when the proposal is not pending in
@@ -1028,7 +1033,9 @@ pub fn fail_proposal(conn: &mut Connection, input: &FailAutoImproveProposal) ->
"auto-improve proposal is not pending or not in scope".into(),
));
}
- insert_rejection_for_proposal_in_tx(&tx, input.proposal_id, &input.reason, now)?;
+ if input.record_rejection {
+ insert_rejection_for_proposal_in_tx(&tx, input.proposal_id, &input.reason, now)?;
+ }
insert_event_in_tx(
&tx,
input.proposal_id,
diff --git a/crates/ai-memory-store/src/lib.rs b/crates/ai-memory-store/src/lib.rs
index 5b3c1059..c227a605 100644
--- a/crates/ai-memory-store/src/lib.rs
+++ b/crates/ai-memory-store/src/lib.rs
@@ -1039,6 +1039,7 @@ mod tests {
project_id: proj,
proposal_id: id,
reason: "admission denied".into(),
+ record_rejection: true,
actor: actor.clone(),
author_id: None,
})
@@ -1068,6 +1069,7 @@ mod tests {
project_id: proj,
proposal_id: id,
reason: "again".into(),
+ record_rejection: true,
actor,
author_id: None,
})
@@ -1076,6 +1078,63 @@ mod tests {
);
}
+ #[tokio::test]
+ async fn auto_improve_fail_without_rejection_leaves_rejections_empty() {
+ let tmp = TempDir::new().unwrap();
+ let store = Store::open(tmp.path()).unwrap();
+ let ws = store
+ .writer
+ .get_or_create_workspace("default")
+ .await
+ .unwrap();
+ let proj = store
+ .writer
+ .get_or_create_project(ws, "app", None)
+ .await
+ .unwrap();
+ let id = store
+ .writer
+ .stage_auto_improve_run(stage_input(
+ ws,
+ proj,
+ vec![proposal(
+ "notes/outage.md",
+ AutoImproveProposalOperation::Create,
+ "outage",
+ )],
+ ))
+ .await
+ .unwrap()
+ .proposal_ids[0];
+ store
+ .writer
+ .fail_auto_improve_proposal(FailAutoImproveProposal {
+ workspace_id: ws,
+ project_id: proj,
+ proposal_id: id,
+ reason: "admission webhook policy request failed".into(),
+ record_rejection: false,
+ actor: ActorContext::default(),
+ author_id: None,
+ })
+ .await
+ .unwrap();
+ let detail = store
+ .reader
+ .auto_improve_proposal_detail(ws, proj, id)
+ .await
+ .unwrap()
+ .unwrap();
+ assert_eq!(detail.summary.status, AutoImproveProposalStatus::Failed);
+ assert_eq!(detail.events.last().unwrap().event, "failed");
+ let rejections = store
+ .reader
+ .recent_auto_improve_rejections(ws, proj, 10, None)
+ .await
+ .unwrap();
+ assert!(rejections.is_empty(), "{rejections:?}");
+ }
+
#[tokio::test]
async fn auto_improve_telemetry_aggregate_counts_learning_activity_only() {
let tmp = TempDir::new().unwrap();
@@ -1198,6 +1257,7 @@ mod tests {
project_id: proj,
proposal_id: failed_id,
reason: "admission denied".into(),
+ record_rejection: true,
actor: actor.clone(),
author_id: None,
})
diff --git a/crates/ai-memory-wiki/src/admission.rs b/crates/ai-memory-wiki/src/admission.rs
index 693d9613..2bc0cf0b 100644
--- a/crates/ai-memory-wiki/src/admission.rs
+++ b/crates/ai-memory-wiki/src/admission.rs
@@ -37,6 +37,46 @@ use serde::{Deserialize, Serialize};
use crate::error::WikiError;
use crate::{Markdown, WikiResult};
+/// Why a `reject`-policy webhook stopped [`AdmissionChain::run`]. It travels
+/// as the source of the returned `WikiError::Io`, whose message is this
+/// one's, so callers that only log or display the error see no change.
+#[derive(Debug)]
+pub(crate) struct AdmissionFailure {
+ /// The webhook could not give a verdict (transport error, timeout, 5xx),
+ /// as opposed to answering with a refusal.
+ unavailable: bool,
+ message: String,
+}
+
+impl std::fmt::Display for AdmissionFailure {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.write_str(&self.message)
+ }
+}
+
+impl std::error::Error for AdmissionFailure {}
+
+impl AdmissionFailure {
+ fn into_error(unavailable: bool, message: String) -> WikiError {
+ WikiError::Io(std::io::Error::other(Self {
+ unavailable,
+ message,
+ }))
+ }
+}
+
+/// `true` when `err` came from [`AdmissionChain::run`] because a webhook was
+/// unreachable, timed out or answered 5xx, rather than refusing the page.
+pub(crate) fn is_admission_unavailable(err: &WikiError) -> bool {
+ match err {
+ WikiError::Io(io) => io
+ .get_ref()
+ .and_then(|inner| inner.downcast_ref::<AdmissionFailure>())
+ .is_some_and(|failure| failure.unavailable),
+ _ => false,
+ }
+}
+
/// Lifecycle operation that triggered the chain. Webhooks subscribe via
/// [`WebhookConfig::events`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
@@ -439,16 +479,19 @@ impl AdmissionChain {
);
tracing::warn!(webhook = %hook.name, status = %status, body = %body_txt, "admission error response");
if matches!(hook.failure_policy, FailurePolicy::Reject) {
- return Err(WikiError::Io(std::io::Error::other(err_msg)));
+ return Err(AdmissionFailure::into_error(
+ status.is_server_error(),
+ err_msg,
+ ));
}
}
Err(e) => {
tracing::warn!(webhook = %hook.name, error = %e, "admission request failed");
if matches!(hook.failure_policy, FailurePolicy::Reject) {
- return Err(WikiError::Io(std::io::Error::other(format!(
- "admission webhook {} request failed: {}",
- hook.name, e
- ))));
+ return Err(AdmissionFailure::into_error(
+ true,
+ format!("admission webhook {} request failed: {}", hook.name, e),
+ ));
}
}
}
diff --git a/crates/ai-memory-wiki/src/wiki.rs b/crates/ai-memory-wiki/src/wiki.rs
index d5f46830..e16589cb 100644
--- a/crates/ai-memory-wiki/src/wiki.rs
+++ b/crates/ai-memory-wiki/src/wiki.rs
@@ -17,7 +17,7 @@ use ai_memory_store::{
};
use tokio::sync::RwLock;
-use crate::admission::{AdmissionChain, AdmissionContext, AdmissionOp};
+use crate::admission::{AdmissionChain, AdmissionContext, AdmissionOp, is_admission_unavailable};
use crate::confinement::{
Prepare, project_path, project_root as confined_project_root, tree_path, workspace_path,
};
@@ -2063,6 +2063,9 @@ impl Wiki {
project_id,
proposal_id,
reason,
+ // An outage is not a verdict on the proposal; keep
+ // it out of the reviewer's "avoid repeating" context.
+ record_rejection: !is_admission_unavailable(&e),
actor,
author_id,
})
diff --git a/crates/ai-memory-wiki/tests/suite/auto_improve_admission.rs b/crates/ai-memory-wiki/tests/suite/auto_improve_admission.rs
new file mode 100644
index 00000000..f884db23
--- /dev/null
+++ b/crates/ai-memory-wiki/tests/suite/auto_improve_admission.rs
@@ -0,0 +1,216 @@
+//! Approving an auto-improve proposal through a `reject`-policy admission
+//! chain: which failures land in `auto_improve_rejections`.
+
+use std::time::Duration;
+
+use ai_memory_core::{ActorContext, PagePath, ProjectId, WorkspaceId};
+use ai_memory_store::{
+ AutoImproveProposalOperation, AutoImproveProposalStatus, NewAutoImproveProposal,
+ StageAutoImproveRun, Store,
+};
+use ai_memory_wiki::{AdmissionChain, AdmissionOp, FailurePolicy, WebhookConfig, Wiki};
+use axum::Router;
+use axum::http::StatusCode;
+use axum::routing::post;
+use tempfile::TempDir;
+
+struct Fixture {
+ _tmp: TempDir,
+ store: Store,
+ wiki: Wiki,
+ ws: WorkspaceId,
+ proj: ProjectId,
+}
+
+async fn fixture(chain: AdmissionChain) -> Fixture {
+ let tmp = TempDir::new().unwrap();
+ let store = Store::open(tmp.path()).unwrap();
+ let ws = store
+ .writer
+ .get_or_create_workspace("default")
+ .await
+ .unwrap();
+ let proj = store
+ .writer
+ .get_or_create_project(ws, "a", None)
+ .await
+ .unwrap();
+ let wiki = Wiki::new(tmp.path(), store.writer.clone())
+ .unwrap()
+ .with_store_reader(store.reader.clone())
+ .with_admission_chain(chain);
+ std::fs::create_dir_all(wiki.project_root(ws, proj)).unwrap();
+ Fixture {
+ _tmp: tmp,
+ store,
+ wiki,
+ ws,
+ proj,
+ }
+}
+
+async fn spawn_server(app: Router) -> String {
+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let addr = listener.local_addr().unwrap();
+ tokio::spawn(async move {
+ axum::serve(listener, app).await.unwrap();
+ });
+ format!("http://{addr}")
+}
+
+fn chain(url: String, timeout_ms: u64, failure_policy: FailurePolicy) -> AdmissionChain {
+ AdmissionChain::new(vec![WebhookConfig {
+ name: "policy".into(),
+ url,
+ timeout_ms,
+ failure_policy,
+ events: vec![AdmissionOp::WritePage],
+ blocking: true,
+ }])
+ .unwrap()
+}
+
+async fn stage_one(fx: &Fixture) -> ai_memory_core::AutoImproveProposalId {
+ fx.store
+ .writer
+ .stage_auto_improve_run(StageAutoImproveRun {
+ workspace_id: fx.ws,
+ project_id: fx.proj,
+ session_id: None,
+ provider: None,
+ model: None,
+ summary: None,
+ warnings_json: serde_json::json!([]),
+ rejected_candidates_json: serde_json::json!([]),
+ config_json: serde_json::json!({}),
+ proposal_actor: ActorContext::anonymous(),
+ proposals: vec![NewAutoImproveProposal {
+ operation: AutoImproveProposalOperation::Create,
+ target_path: PagePath::new("decisions/rule.md").unwrap(),
+ kind: "rule".into(),
+ title: "Rule".into(),
+ confidence: 0.9,
+ rationale: "because".into(),
+ evidence_json: serde_json::json!([]),
+ body_markdown: "# Rule\n\nAlways do the thing.\n".into(),
+ artifact_sha256: None,
+ edit_mode: None,
+ patch_json: None,
+ expected_base_body_sha256: None,
+ }],
+ })
+ .await
+ .unwrap()
+ .proposal_ids[0]
+}
+
+/// Approve once and return (approval errored, proposal status, rejection reasons).
+async fn approve(fx: &Fixture) -> (bool, AutoImproveProposalStatus, Vec<String>) {
+ let id = stage_one(fx).await;
+ let res = fx
+ .wiki
+ .approve_auto_improve_proposal(fx.ws, fx.proj, id, ActorContext::anonymous(), None, None)
+ .await;
+ let status = fx
+ .store
+ .reader
+ .auto_improve_proposal_detail(fx.ws, fx.proj, id)
+ .await
+ .unwrap()
+ .unwrap()
+ .summary
+ .status;
+ let reasons = fx
+ .store
+ .reader
+ .recent_auto_improve_rejections(fx.ws, fx.proj, 10, None)
+ .await
+ .unwrap()
+ .into_iter()
+ .map(|r| r.reason)
+ .collect();
+ (res.is_err(), status, reasons)
+}
+
+// Negative: an admission server that times out did not reject anything.
+#[tokio::test]
+async fn reject_policy_timeout_is_not_recorded_as_a_rejection() {
+ let app = Router::new().route(
+ "/policy",
+ post(|| async {
+ tokio::time::sleep(Duration::from_secs(5)).await;
+ StatusCode::OK
+ }),
+ );
+ let base = spawn_server(app).await;
+ let fx = fixture(chain(format!("{base}/policy"), 200, FailurePolicy::Reject)).await;
+ let (errored, status, reasons) = approve(&fx).await;
+ assert!(errored);
+ assert_eq!(status, AutoImproveProposalStatus::Failed);
+ assert!(
+ reasons.is_empty(),
+ "outage recorded as rejection: {reasons:?}"
+ );
+}
+
+// Negative: a 503 from the admission server is an outage, not a verdict.
+#[tokio::test]
+async fn reject_policy_503_is_not_recorded_as_a_rejection() {
+ let app = Router::new().route(
+ "/policy",
+ post(|| async { (StatusCode::SERVICE_UNAVAILABLE, "down for maintenance") }),
+ );
+ let base = spawn_server(app).await;
+ let fx = fixture(chain(
+ format!("{base}/policy"),
+ 1_000,
+ FailurePolicy::Reject,
+ ))
+ .await;
+ let (errored, status, reasons) = approve(&fx).await;
+ assert!(errored);
+ assert_eq!(status, AutoImproveProposalStatus::Failed);
+ assert!(
+ reasons.is_empty(),
+ "outage recorded as rejection: {reasons:?}"
+ );
+}
+
+// Control: an explicit 403 is a policy verdict and must stay a rejection.
+#[tokio::test]
+async fn reject_policy_403_is_recorded_as_a_rejection() {
+ let app = Router::new().route(
+ "/policy",
+ post(|| async { (StatusCode::FORBIDDEN, "no secrets") }),
+ );
+ let base = spawn_server(app).await;
+ let fx = fixture(chain(
+ format!("{base}/policy"),
+ 1_000,
+ FailurePolicy::Reject,
+ ))
+ .await;
+ let (errored, status, reasons) = approve(&fx).await;
+ assert!(errored);
+ assert_eq!(status, AutoImproveProposalStatus::Failed);
+ assert_eq!(reasons.len(), 1, "{reasons:?}");
+ assert!(reasons[0].contains("403"), "{reasons:?}");
+}
+
+// Control: with the ignore policy an outage neither fails nor rejects.
+#[tokio::test]
+async fn ignore_policy_timeout_approves_without_rejection() {
+ let app = Router::new().route(
+ "/policy",
+ post(|| async {
+ tokio::time::sleep(Duration::from_secs(5)).await;
+ StatusCode::OK
+ }),
+ );
+ let base = spawn_server(app).await;
+ let fx = fixture(chain(format!("{base}/policy"), 200, FailurePolicy::Ignore)).await;
+ let (errored, status, reasons) = approve(&fx).await;
+ assert!(!errored);
+ assert_eq!(status, AutoImproveProposalStatus::Approved);
+ assert!(reasons.is_empty(), "{reasons:?}");
+}
diff --git a/crates/ai-memory-wiki/tests/suite/mod.rs b/crates/ai-memory-wiki/tests/suite/mod.rs
index 04ac344d..9f47d2a9 100644
--- a/crates/ai-memory-wiki/tests/suite/mod.rs
+++ b/crates/ai-memory-wiki/tests/suite/mod.rs
@@ -3,5 +3,6 @@
//! no extra binary; a new file must be declared below.
mod admission;
+mod auto_improve_admission;
mod ledger_files;
mod wiki_git_ledgers;
diff --git a/docs/auto-improvement-loop.md b/docs/auto-improvement-loop.md
index 465ef1be..06ec8293 100644
--- a/docs/auto-improvement-loop.md
+++ b/docs/auto-improvement-loop.md
@@ -414,7 +414,9 @@ imply each other.
rejection-buffer summary included in future reviewer prompts. The buffer is
scoped by `workspace_id` + `project_id` and stores human rejects, approval
conflicts/failures, and validator/model rejected candidates when they carry a
-reason.
+reason. An approval that fails because a `reject`-policy admission webhook was
+unreachable, timed out or answered 5xx still marks the proposal `failed`, but
+is not stored: an outage says nothing about the proposal.
## Proposal Format