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
+86
View File
@@ -38,6 +38,7 @@ use crate::provider::ProviderRegistry;
use crate::run_context::RunContextManager;
use crate::secrets::SecretsStore;
use crate::session::manager::ChatSessionManager;
use crate::system_agents::{AgentRunCtx, AgentScope, ManualRun, ManualRunError, SystemAgents};
use crate::tool_catalog::ToolCatalog;
use crate::tools::ToolRegistry;
use crate::transcribe::TranscribeManager;
@@ -303,6 +304,91 @@ impl Skald {
pub fn latex_compiler(&self) -> &LatexCompiler { &self.infra.latex_compiler }
pub fn location_manager(&self) -> &Arc<LocationManager> { &self.infra.location_manager }
pub fn remote(&self) -> &Arc<RwLock<Option<Arc<dyn RemoteAccess>>>> { &self.infra.remote }
// System agents
pub fn system_agents(&self) -> &Arc<SystemAgents> { &self.system_agents }
/// Start one system-agent pass **now**, for `user_id`, because a human asked.
///
/// The schedule answers *when* a pass runs, and the button is a person saying
/// "now" — so due-ness is skipped, exactly as manual `/compact` skips the
/// compactor's token threshold. The instance-wide **Enabled** switch is a
/// different kind of setting and is honoured: it says *whether* the agent runs
/// at all, and that is the admin's answer, not the caller's.
///
/// The pass always runs **as the caller** — their pool, their sessions, their
/// hub — so a member triggering the shared-memory lint gets their own report
/// over the shared store, and their own run row. One consequence worth naming:
/// the attempt is marked in the file the pass ran in, so a member's manual run
/// of an instance-wide agent does not move the admin's scheduled clock. The two
/// clocks were always per file; this only makes it visible.
///
/// Returns as soon as the work is **scheduled**, not when it finishes: a pass is
/// an LLM turn and no HTTP request should be held open for it. The run log is
/// the progress surface — the `running` row exists before this returns to the
/// browser. The one thing answered synchronously is
/// [`SystemAgent::has_work`], which is cheap by contract and whose `false`
/// leaves no row at all: without it the button would report "started" and the
/// log would stay empty forever.
pub async fn run_system_agent_now(
self: &Arc<Self>,
agent_id: &str,
user_id: &str,
) -> Result<ManualRun, ManualRunError> {
let agent = self.system_agents.get(agent_id).ok_or(ManualRunError::UnknownAgent)?.clone();
// A per-subject pass is about somebody else and picks its own subjects;
// "run it for me" has no meaning for it.
if agent.scope() == AgentScope::PerSubject {
return Err(ManualRunError::Unsupported);
}
if !agent.is_enabled().await {
return Err(ManualRunError::Disabled);
}
let ctx = self.user_context(user_id).await.ok_or(ManualRunError::Locked)?;
// Taken before `has_work` so that two quick clicks cannot both look, both
// find work, and both start.
let claim = self
.system_agents
.claim(agent.id(), &SystemAgents::target_of(agent.as_ref(), user_id))
.ok_or(ManualRunError::AlreadyRunning)?;
let run_ctx = AgentRunCtx {
user_id,
pool: &ctx.pool,
sessions: &ctx.sessions,
hub: &ctx.chat_hub,
subject: None,
run_id: None,
};
if !agent.has_work(&run_ctx).await.map_err(ManualRunError::Failed)? {
return Ok(ManualRun::NothingToDo);
}
let user_id = user_id.to_string();
self.rt.supervisor.spawn("system-agent-manual", async move {
// Released when the task ends, whichever way it ends.
let _claim = claim;
let run_ctx = AgentRunCtx {
user_id: &user_id,
pool: &ctx.pool,
sessions: &ctx.sessions,
hub: &ctx.chat_hub,
subject: None,
run_id: None,
};
if let Err(e) = crate::system_agents::run_and_record(agent.as_ref(), &run_ctx).await {
// The failure is already recorded on the run row, which is where
// the person who pressed the button will look for it.
tracing::warn!(agent = agent.id(), user = %user_id, error = %e,
"system-agents: manual pass failed");
}
});
Ok(ManualRun::Started)
}
}
// ── UserChannelApi ────────────────────────────────────────────────────────────