feat: a "Run now" button for the memory lints — one pass, for whoever asked
Nightly Build / build (push) Successful in 7m33s

The two memory lints run weekly, which is right for maintenance and wrong for
the moment somebody has just reorganised their notes and wants to know what the
lint makes of them. Each agent's tab now carries a button that starts one pass
immediately, for the caller.

It runs as the caller — their pool, their sessions, their hub — so the report
lands with the person who asked. The shared lint is the interesting case: its
scheduled pass runs as the admin because the shared store belongs to nobody, but
a member pressing the button reads the same store and gets the report themselves,
which is coherent with shared memory being readable by every member anyway.

Two settings are treated differently on purpose. Due-ness is skipped, exactly as
manual /compact skips the compactor's token threshold: the interval answers
*when*, and a human asking is a good enough answer to that. The Enabled switch
is honoured: it answers *whether*, and that one is the admin's.

The conversation review gets no button (AgentScope::PerSubject): it is about
somebody else and picks its own subjects, so "run it for me" has no meaning.
The frontend reads that from the agent's scope, not from a list of ids.

A second starter breaks an invariant the scheduler used to hold for free.
system_agent_runs::start sweeps any leftover `running` row of the same agent to
`failed` before inserting, which was safe only because one sequential loop was
the only thing that ever started a pass; a manual run overlapping a scheduled
one would have marked a healthy run as interrupted and duplicated its work. So
the agent list moves out of the scheduler and onto Skald as SystemAgents, which
holds the registry plus an in-flight guard both paths claim through — keyed on
what the pass is *about*, so an instance-wide agent is one slot no matter who
runs it, and a per-subject review is keyed on the subject rather than on the
supervisor lending the runtime.

has_work is answered synchronously, before anything is spawned: it leaves no run
row, so without that the button would say "started" over a log that never gains
a row. Everything after it is spawned — a pass is an LLM turn, and no HTTP
request should be held open for one. The run row exists before the browser is
answered, so the log itself is the progress surface; the page polls it quietly
until the pass leaves `running`.
This commit is contained in:
2026-08-02 21:40:21 +01:00
parent 11f4ba8ed2
commit 85536755ee
12 changed files with 518 additions and 22 deletions
+2
View File
@@ -57,6 +57,8 @@ pub fn router() -> Router<Arc<Skald>> {
// the agent list (settings included only for an admin).
.route("/system-agents", get(system_agents::list_agents))
.route("/system-agents/runs", get(system_agents::list_runs))
// "Run now": one pass for the caller, off-schedule.
.route("/system-agents/{agent_id}/run", post(system_agents::run_now))
// First-run setup
.route("/setup/status", get(setup::status))
.route("/setup/profiles", get(setup::profiles))
+45 -1
View File
@@ -22,13 +22,14 @@ use std::sync::Arc;
use axum::{
Extension, Json,
extract::{Query, State},
extract::{Path, Query, State},
};
use serde::Deserialize;
use serde_json::{Value, json};
use skald_core::db::system_agent_runs;
use skald_core::skald::Skald;
use skald_core::system_agents::{AgentScope, ManualRun, ManualRunError};
use super::guard::AuthUser;
use super::{ApiError, caps, config, require_context};
@@ -50,6 +51,16 @@ pub async fn list_agents(
.filter(|s| s.owner.is_some())
.collect();
// Whether this agent's tab gets a "Run now" button. Read from the agent's own
// scope rather than a list of ids here, so a future agent is classified by
// what it is: a pass about somebody else has no "for me" to run.
let can_run_now = |set: &core_api::ConfigSet| {
set.owner
.as_deref()
.and_then(|id| skald.system_agents().get(id))
.is_some_and(|a| a.scope() != AgentScope::PerSubject)
};
// Values and options are resolved only for an admin. A member gets no
// settings at all rather than read-only ones: there is nothing on this page
// they could do with them, and shipping them would leak the instance's
@@ -66,6 +77,7 @@ pub async fn list_agents(
"name": set.name,
"description": set.description,
"config": view,
"can_run_now": can_run_now(set),
})
})
.collect()
@@ -78,6 +90,7 @@ pub async fn list_agents(
"name": set.name,
"description": set.description,
"config": Value::Null,
"can_run_now": can_run_now(set),
})
})
.collect()
@@ -140,3 +153,34 @@ pub async fn list_runs(
"per_page": per_page,
})))
}
/// `POST /api/system-agents/{agent_id}/run` — run this agent **now**, for the caller.
///
/// **Not admin-gated, and that is the same decision the run log makes.** A pass
/// runs in the caller's own runtime over their own data and reports to them
/// alone, so there is nothing here an admin should have to approve — the person
/// who would read the report is the person asking for it. What stays the admin's
/// is the *schedule* and the on/off switch, which this endpoint does not touch:
/// a disabled agent answers `409` rather than running once for whoever asked.
///
/// Returns as soon as the pass is scheduled; `nothing_to_do` is the honest answer
/// for a store with nothing in it, and leaves no run row behind — exactly what an
/// idle scheduled pass does.
pub async fn run_now(
State(skald): State<Arc<Skald>>,
Extension(auth): Extension<AuthUser>,
Path(agent_id): Path<String>,
) -> Result<Json<Value>, ApiError> {
match skald.run_system_agent_now(&agent_id, &auth.user_id).await {
Ok(ManualRun::Started) => Ok(Json(json!({ "status": "started" }))),
Ok(ManualRun::NothingToDo) => Ok(Json(json!({ "status": "nothing_to_do" }))),
Err(e) => Err(match e {
ManualRunError::UnknownAgent => ApiError::not_found(e.to_string()),
ManualRunError::Unsupported => ApiError::bad_request(e.to_string()),
ManualRunError::Disabled => ApiError::conflict(e.to_string()),
ManualRunError::AlreadyRunning => ApiError::conflict(e.to_string()),
ManualRunError::Locked => ApiError::unauthorized(e.to_string()),
ManualRunError::Failed(err) => ApiError::from(err),
}),
}
}