Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9ed58ab96d | ||
|
|
4b6c733afb | ||
|
|
b652e6a6b0 | ||
|
|
5a4eb3e053 | ||
|
|
2b1c1b54ac | ||
|
|
b43438d1a6 | ||
|
|
c9f537c2e3 | ||
|
|
4a2b2fca4f | ||
|
|
994166b20c | ||
|
|
6e833b22d6 | ||
|
|
b5be139fb7 | ||
|
|
4b30f20dcf | ||
|
|
8bbb3e4905 |
30 changed files with 1449 additions and 644 deletions
18
CLAUDE.md
18
CLAUDE.md
|
|
@ -150,6 +150,14 @@ hive-ag3nt/ in-container harness crate; produces TWO binaries
|
|||
src/turn.rs claude --print + stream-json pump; --compact retry;
|
||||
proactive compaction + auto session-reset
|
||||
src/mcp.rs embedded MCP server (rmcp): AgentServer + ManagerServer
|
||||
src/forge_notify.rs Forgejo webhook subscriber: formats new-issue /
|
||||
new-PR / comment / review notifications as
|
||||
broker messages; embeds a body excerpt
|
||||
(up to BODY_TRUNCATE bytes, ATX headings
|
||||
escaped) and surfaces any @mention lines that
|
||||
fell outside the truncation window in a
|
||||
trailing `mentions (truncated from body):`
|
||||
block (#539 / #544).
|
||||
src/login.rs probe /root/.claude/ for a valid session
|
||||
src/login_session.rs drives `claude auth login` over stdio pipes
|
||||
src/prompt.rs system prompt renderer: filters the unified
|
||||
|
|
@ -189,10 +197,18 @@ nix/
|
|||
modules/hive-c0re.nix systemd service + firewall + git wiring;
|
||||
`contextWindowTokens` attrset (per-model,
|
||||
injected as env vars into all containers);
|
||||
imports hive-forge.nix
|
||||
top-level `hyperhive.domain` option
|
||||
(nullable; required when matrix.enable);
|
||||
imports hive-forge.nix + hive-matrix.nix
|
||||
modules/hive-forge.nix optional in-container Forgejo
|
||||
(`hyperhive.forge.enable`, default on);
|
||||
Catppuccin Mocha theme via tmpfiles C+ copy
|
||||
modules/hive-matrix.nix optional in-container matrix-tuwunel
|
||||
homeserver (`hyperhive.matrix.enable`,
|
||||
default off); server_name derives from
|
||||
`hyperhive.domain` (subdomain by default);
|
||||
federation on (empty trusted_servers),
|
||||
registration off, e2ee deferred (#551)
|
||||
templates/harness-base.nix shared scaffolding for sub-agents + manager;
|
||||
`hyperhive.model` option (HIVE_DEFAULT_MODEL)
|
||||
templates/agent-base.nix sub-agent nixosConfiguration
|
||||
|
|
|
|||
|
|
@ -38,6 +38,15 @@ Each agent harness (`hive-ag3nt serve` or `hive-m1nd serve`) runs:
|
|||
the same path used at boot. The operator re-authenticates via
|
||||
the per-agent web UI login flow; on success the sentinel is
|
||||
cleared and the queued message drives the next turn normally.
|
||||
**Mtime-snapshot resumption** (closes #542): `wait_for_login`
|
||||
snapshots the `~/.claude/` dir (newest file mtime + file count)
|
||||
at entry and only resumes when that snapshot advances — not
|
||||
just when credentials exist on disk. This prevents a silent
|
||||
infinite-401 loop: stale credentials already on disk at the
|
||||
time of the 401 no longer cause an immediate false-resume. The
|
||||
`DirSnapshot` struct tracks both axes; either a mtime advance
|
||||
OR a file-count change triggers resume (the count axis handles
|
||||
filesystems where `modified()` errors on every file).
|
||||
7. Emit `LiveEvent::TurnEnd { ok, note }`. Sleep `poll_ms` to avoid
|
||||
tight loops on transient failures.
|
||||
|
||||
|
|
|
|||
|
|
@ -383,7 +383,10 @@ body sits to the right with three stacked lines
|
|||
fetches journald via `GET /api/journal/{name}?unit=&lines=`
|
||||
(`journalctl -M <container> -b --no-pager --output=short-iso`).
|
||||
A unit dropdown (harness service / full machine journal) and
|
||||
a refresh button live in the panel.
|
||||
a refresh button live in the panel. The panel uses a column-flex
|
||||
layout so the `<pre>` log surface fills the full remaining panel
|
||||
height (#541); scroll happens inside the `<pre>`, not the side
|
||||
panel body.
|
||||
- Plain navigation links (config repo, forge profile,
|
||||
`dashboardLinks` extras) now live in the icon-only nav strip
|
||||
on Line 1 — see above (issue #262). The agent's `config` link
|
||||
|
|
|
|||
63
flake.lock
generated
63
flake.lock
generated
|
|
@ -1,45 +1,17 @@
|
|||
{
|
||||
"nodes": {
|
||||
"fenix": {
|
||||
"inputs": {
|
||||
"nixpkgs": [
|
||||
"naersk",
|
||||
"nixpkgs"
|
||||
],
|
||||
"rust-analyzer-src": "rust-analyzer-src"
|
||||
},
|
||||
"crane": {
|
||||
"locked": {
|
||||
"lastModified": 1752475459,
|
||||
"narHash": "sha256-z6QEu4ZFuHiqdOPbYss4/Q8B0BFhacR8ts6jO/F/aOU=",
|
||||
"owner": "nix-community",
|
||||
"repo": "fenix",
|
||||
"rev": "bf0d6f70f4c9a9cf8845f992105652173f4b617f",
|
||||
"lastModified": 1779130139,
|
||||
"narHash": "sha256-BLrtr42azquO7MdGFU5a7KiMl3YpFlTeIXqy1fT5GlQ=",
|
||||
"owner": "ipetkov",
|
||||
"repo": "crane",
|
||||
"rev": "edb38893982a3338972bb4a2ec7ce7c29ba10fd9",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nix-community",
|
||||
"repo": "fenix",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"naersk": {
|
||||
"inputs": {
|
||||
"fenix": "fenix",
|
||||
"nixpkgs": [
|
||||
"nixpkgs"
|
||||
]
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1778151388,
|
||||
"narHash": "sha256-lldMJPUeouEjO8/7aLuwhcsIw29vVihm2ZALzjiqfec=",
|
||||
"owner": "nix-community",
|
||||
"repo": "naersk",
|
||||
"rev": "efdddff9ff4d8e7d0056d57ec67dac50f75ab8f6",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nix-community",
|
||||
"repo": "naersk",
|
||||
"owner": "ipetkov",
|
||||
"repo": "crane",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
|
|
@ -77,29 +49,12 @@
|
|||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"naersk": "naersk",
|
||||
"crane": "crane",
|
||||
"nixpkgs": "nixpkgs",
|
||||
"nixpkgs-unstable": "nixpkgs-unstable",
|
||||
"treefmt-nix": "treefmt-nix"
|
||||
}
|
||||
},
|
||||
"rust-analyzer-src": {
|
||||
"flake": false,
|
||||
"locked": {
|
||||
"lastModified": 1752428706,
|
||||
"narHash": "sha256-EJcdxw3aXfP8Ex1Nm3s0awyH9egQvB2Gu+QEnJn2Sfg=",
|
||||
"owner": "rust-lang",
|
||||
"repo": "rust-analyzer",
|
||||
"rev": "591e3b7624be97e4443ea7b5542c191311aa141d",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "rust-lang",
|
||||
"ref": "nightly",
|
||||
"repo": "rust-analyzer",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"treefmt-nix": {
|
||||
"inputs": {
|
||||
"nixpkgs": [
|
||||
|
|
|
|||
116
flake.nix
116
flake.nix
|
|
@ -4,10 +4,10 @@
|
|||
inputs = {
|
||||
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11";
|
||||
nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
|
||||
naersk = {
|
||||
url = "github:nix-community/naersk";
|
||||
inputs.nixpkgs.follows = "nixpkgs";
|
||||
};
|
||||
# Crane (replaces naersk #538). Stateless — no nixpkgs input to
|
||||
# follow; `crane.mkLib pkgs` returns the lib at whatever pkgs we
|
||||
# pass it (we use the project's pinned nixpkgs).
|
||||
crane.url = "github:ipetkov/crane";
|
||||
treefmt-nix = {
|
||||
url = "github:numtide/treefmt-nix";
|
||||
inputs.nixpkgs.follows = "nixpkgs";
|
||||
|
|
@ -19,7 +19,7 @@
|
|||
self,
|
||||
nixpkgs,
|
||||
nixpkgs-unstable,
|
||||
naersk,
|
||||
crane,
|
||||
treefmt-nix,
|
||||
}:
|
||||
let
|
||||
|
|
@ -45,22 +45,59 @@
|
|||
inherit system;
|
||||
pkgs = nixpkgs.legacyPackages.${system};
|
||||
treefmt-eval = treefmt-nix.lib.evalModule pkgs treefmt-config;
|
||||
naersk-lib = pkgs.callPackage naersk { };
|
||||
craneLib = crane.mkLib pkgs;
|
||||
# Build the workspace's dependency tree once, cached as
|
||||
# its own derivation. `buildPackage` and `cargoClippy`
|
||||
# both reuse this via `inherit cargoArtifacts;` so a
|
||||
# workspace-only edit doesn't rebuild deps. Same
|
||||
# `nativeBuildInputs` as the workspace build itself —
|
||||
# build.rs runs during dep-build too (any deps with a
|
||||
# build.rs need rsvg too if they transitively pull it
|
||||
# in; harmless if they don't).
|
||||
cargoArtifacts = craneLib.buildDepsOnly {
|
||||
src = ./.;
|
||||
# Workspace Cargo.toml is virtual (no `[package].name`),
|
||||
# so crane can't auto-derive a name. Spell it out
|
||||
# explicitly here and below — keeps the derivation name
|
||||
# stable across crane bumps + silences the placeholder
|
||||
# warning. Same `pname` everywhere so dep + workspace +
|
||||
# clippy share a clean naming family.
|
||||
pname = "hyperhive-workspace";
|
||||
version = "0.1.0";
|
||||
inherit nativeBuildInputs;
|
||||
};
|
||||
# Shared between buildDepsOnly + buildPackage + cargoClippy
|
||||
# so the three derivations see the same toolchain shape.
|
||||
# librsvg: hive-c0re/build.rs invokes `rsvg-convert` to
|
||||
# render branding/agent-configs.svg → PNG that the daemon
|
||||
# `include_bytes!`s (#424); SVG stays source-of-truth.
|
||||
# git: naersk used to auto-include it; crane is more
|
||||
# minimal, so we add it explicitly so hive-c0re's
|
||||
# `lifecycle::tests::setup_proposed_*` (which shell out to
|
||||
# `git init` + commit) pass under `cargo test` in the
|
||||
# sandbox.
|
||||
nativeBuildInputs = [
|
||||
pkgs.librsvg
|
||||
pkgs.git
|
||||
];
|
||||
}
|
||||
);
|
||||
in
|
||||
{
|
||||
packages = forAllSystems (
|
||||
{ pkgs, naersk-lib, ... }:
|
||||
{
|
||||
default = naersk-lib.buildPackage {
|
||||
pkgs,
|
||||
craneLib,
|
||||
cargoArtifacts,
|
||||
nativeBuildInputs,
|
||||
...
|
||||
}:
|
||||
{
|
||||
default = craneLib.buildPackage {
|
||||
src = ./.;
|
||||
# librsvg ships `rsvg-convert`, which hive-c0re/build.rs
|
||||
# invokes to render branding/agent-configs.svg into the
|
||||
# PNG it embeds via `include_bytes!` (#424). Keeps the
|
||||
# raster out of git — SVG stays source-of-truth, PNG is
|
||||
# a build artifact in $OUT_DIR.
|
||||
nativeBuildInputs = [ pkgs.librsvg ];
|
||||
inherit cargoArtifacts nativeBuildInputs;
|
||||
pname = "hyperhive-workspace";
|
||||
version = "0.1.0";
|
||||
meta.description = "hyperhive workspace (hive-c0re, hive-ag3nt, hive-m1nd)";
|
||||
};
|
||||
# Bundled browser assets — see ./nix/frontend.nix. Output is
|
||||
|
|
@ -201,43 +238,28 @@
|
|||
checks = forAllSystems (
|
||||
{
|
||||
treefmt-eval,
|
||||
pkgs,
|
||||
naersk-lib,
|
||||
craneLib,
|
||||
cargoArtifacts,
|
||||
nativeBuildInputs,
|
||||
...
|
||||
}:
|
||||
{
|
||||
formatting = treefmt-eval.config.build.check self;
|
||||
# Clippy as a check: reuse naersk's vendored-deps environment but
|
||||
# replace the build phase with `cargo clippy --workspace --all-targets
|
||||
# -- -D warnings`. Naersk's own `mode = "clippy"` mangles the `--`
|
||||
# separator, so we go through overrideAttrs instead.
|
||||
clippy =
|
||||
(naersk-lib.buildPackage {
|
||||
src = ./.;
|
||||
# Skip the actual build; we only care about the clippy lint.
|
||||
doCheck = false;
|
||||
copyTarget = false;
|
||||
# hive-c0re/build.rs needs rsvg-convert on PATH (#424);
|
||||
# mirror the runtime derivation's nativeBuildInputs so
|
||||
# clippy's vendored-deps build phase doesn't break on
|
||||
# the missing tool.
|
||||
nativeBuildInputs = [ pkgs.librsvg ];
|
||||
}).overrideAttrs
|
||||
(old: {
|
||||
name = "${old.name}-clippy";
|
||||
nativeBuildInputs = (old.nativeBuildInputs or [ ]) ++ [ pkgs.clippy ];
|
||||
buildPhase = ''
|
||||
runHook preBuild
|
||||
cargo clippy --workspace --all-targets -- -D warnings
|
||||
runHook postBuild
|
||||
'';
|
||||
installPhase = ''
|
||||
runHook preInstall
|
||||
mkdir -p $out
|
||||
touch $out/.clippy-passed
|
||||
runHook postInstall
|
||||
'';
|
||||
});
|
||||
# Clippy as a check via crane's first-class `cargoClippy`
|
||||
# builder. Reuses the shared `cargoArtifacts` (deps already
|
||||
# built) and runs `cargo clippy --workspace --all-targets
|
||||
# -- -D warnings` directly — no `overrideAttrs` hack needed,
|
||||
# because crane parses `cargoClippyExtraArgs` correctly
|
||||
# (naersk's `mode = "clippy"` used to mangle the `--`
|
||||
# separator, which is why the old wiring went through
|
||||
# overrideAttrs).
|
||||
clippy = craneLib.cargoClippy {
|
||||
src = ./.;
|
||||
inherit cargoArtifacts nativeBuildInputs;
|
||||
pname = "hyperhive-workspace";
|
||||
version = "0.1.0";
|
||||
cargoClippyExtraArgs = "--workspace --all-targets -- -D warnings";
|
||||
};
|
||||
}
|
||||
);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -450,11 +450,21 @@ a:hover {
|
|||
.container-row.tombstone .name { color: var(--muted); }
|
||||
/* Per-container journald viewer + applied-config viewer. Both open
|
||||
in the side panel and lazy-fetch on open; output is monospace
|
||||
inside a bordered <pre>, controls (unit select + refresh) above. */
|
||||
inside a bordered <pre>, controls (unit select + refresh) above.
|
||||
#541: the panel-body wrapper is a column flex container that fills
|
||||
the side-panel-body so the <pre> can flex-grow into a single tall
|
||||
scrollable surface instead of a short box at the top with the rest
|
||||
of the panel empty. */
|
||||
.journal-body {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100%;
|
||||
gap: 0.4em;
|
||||
}
|
||||
.journal-controls {
|
||||
flex: 0 0 auto;
|
||||
display: flex;
|
||||
gap: 0.5em;
|
||||
margin-bottom: 0.4em;
|
||||
align-items: center;
|
||||
}
|
||||
.journal-unit {
|
||||
|
|
@ -472,7 +482,13 @@ a:hover {
|
|||
color: var(--fg);
|
||||
border: 1px solid var(--purple-dim);
|
||||
padding: 0.5em 0.7em;
|
||||
overflow-x: auto;
|
||||
/* #541: take all leftover panel height + scroll inside the pre so
|
||||
long log fetches don't push the controls off-screen. `min-height:
|
||||
0` is the canonical "let me actually flex-shrink for overflow"
|
||||
escape hatch on flex children. */
|
||||
flex: 1 1 0;
|
||||
min-height: 0;
|
||||
overflow: auto;
|
||||
font-size: 0.85em;
|
||||
line-height: 1.4;
|
||||
white-space: pre;
|
||||
|
|
|
|||
|
|
@ -946,9 +946,12 @@ window.marked = marked;
|
|||
pre.textContent = 'error: ' + resp.status + '\n' + text;
|
||||
} else {
|
||||
pre.textContent = text || '(empty)';
|
||||
// Auto-scroll the panel to the newest lines on fresh fetch.
|
||||
const sb = $('side-panel-body');
|
||||
if (sb) sb.scrollTop = sb.scrollHeight;
|
||||
// Auto-scroll to the newest lines on fresh fetch. #541
|
||||
// moved the scroll surface from side-panel-body onto the
|
||||
// <pre> itself (the panel-body now fills the viewport and
|
||||
// the <pre> is the inner overflow container), so scroll
|
||||
// the <pre> instead of the side-panel-body.
|
||||
pre.scrollTop = pre.scrollHeight;
|
||||
}
|
||||
} catch (err) {
|
||||
pre.textContent = 'fetch failed: ' + err;
|
||||
|
|
|
|||
|
|
@ -24,6 +24,9 @@ tower-http.workspace = true
|
|||
tracing.workspace = true
|
||||
tracing-subscriber.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3"
|
||||
|
||||
[[bin]]
|
||||
name = "hive-ag3nt"
|
||||
path = "src/bin/hive-ag3nt.rs"
|
||||
|
|
|
|||
|
|
@ -143,10 +143,7 @@ pub enum LiveEvent {
|
|||
/// compacting). `since_unix` matches `Bus::state_snapshot().1`
|
||||
/// so the client's elapsed-time ticker keeps progressing across
|
||||
/// SSE reconnects without drift.
|
||||
TurnStateChanged {
|
||||
state: TurnState,
|
||||
since_unix: i64,
|
||||
},
|
||||
TurnStateChanged { state: TurnState, since_unix: i64 },
|
||||
}
|
||||
|
||||
/// sqlite-backed event log. Wraps a `Connection` behind a `Mutex` so the
|
||||
|
|
@ -284,10 +281,12 @@ impl TokenUsage {
|
|||
let model_usage = v.get("modelUsage")?;
|
||||
let map = model_usage.as_object()?;
|
||||
for (_model, stats) in map {
|
||||
if let Some(w) = stats.get("contextWindow").and_then(serde_json::Value::as_u64) {
|
||||
if w > 0 {
|
||||
return Some(w);
|
||||
}
|
||||
if let Some(w) = stats
|
||||
.get("contextWindow")
|
||||
.and_then(serde_json::Value::as_u64)
|
||||
&& w > 0
|
||||
{
|
||||
return Some(w);
|
||||
}
|
||||
}
|
||||
None
|
||||
|
|
@ -353,18 +352,21 @@ pub fn context_window_tokens(model: &str) -> u64 {
|
|||
// Per-model env vars set by `hyperhive.contextWindowTokens` in Nix.
|
||||
for (key, val) in std::env::vars() {
|
||||
if let Some(suffix) = key.strip_prefix("HIVE_CONTEXT_WINDOW_TOKENS_")
|
||||
&& !suffix.is_empty() && m.contains(&suffix.to_ascii_lowercase())
|
||||
&& let Ok(v) = val.trim().parse::<u64>()
|
||||
&& v > 0 {
|
||||
return v;
|
||||
}
|
||||
&& !suffix.is_empty()
|
||||
&& m.contains(&suffix.to_ascii_lowercase())
|
||||
&& let Ok(v) = val.trim().parse::<u64>()
|
||||
&& v > 0
|
||||
{
|
||||
return v;
|
||||
}
|
||||
}
|
||||
// Global override (single value, any model).
|
||||
if let Ok(s) = std::env::var("HIVE_CONTEXT_WINDOW_TOKENS")
|
||||
&& let Ok(v) = s.trim().parse::<u64>()
|
||||
&& v > 0 {
|
||||
return v;
|
||||
}
|
||||
&& v > 0
|
||||
{
|
||||
return v;
|
||||
}
|
||||
// Hard fallback for dev/test outside NixOS where env vars aren't set.
|
||||
200_000
|
||||
}
|
||||
|
|
@ -450,9 +452,10 @@ impl Bus {
|
|||
// persisted runtime override > compiled-in DEFAULT_MODEL.
|
||||
// The nix config always wins on rebuild; the persisted file is kept
|
||||
// for within-session tracking only (see persist_model / set_model).
|
||||
let initial_model = configured_model()
|
||||
.map(str::to_owned)
|
||||
.unwrap_or_else(|| load_model().unwrap_or_else(|| DEFAULT_MODEL.to_owned()));
|
||||
let initial_model = configured_model().map_or_else(
|
||||
|| load_model().unwrap_or_else(|| DEFAULT_MODEL.to_owned()),
|
||||
str::to_owned,
|
||||
);
|
||||
// Restore rate_limited from the sentinel file — if the harness
|
||||
// crashed while parked, we should still show the right status on
|
||||
// cold load until the next turn clears it.
|
||||
|
|
@ -560,7 +563,8 @@ impl Bus {
|
|||
pub fn record_turn_usage(&self, ctx: TokenUsage, cost: TokenUsage) {
|
||||
*self.last_ctx_usage.lock().unwrap() = Some(ctx);
|
||||
*self.last_cost_usage.lock().unwrap() = Some(cost);
|
||||
self.last_turn_ended_unix.store(now_unix(), Ordering::Relaxed);
|
||||
self.last_turn_ended_unix
|
||||
.store(now_unix(), Ordering::Relaxed);
|
||||
self.emit(LiveEvent::TokenUsageChanged { ctx, cost });
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -162,11 +162,7 @@ pub async fn run(socket: PathBuf, is_manager: bool) {
|
|||
|
||||
/// Fetch a JSON value from a URL using the agent's forge token. Returns
|
||||
/// `None` on any HTTP or parse error (best-effort enrichment).
|
||||
async fn fetch_json(
|
||||
client: &reqwest::Client,
|
||||
url: &str,
|
||||
token: &str,
|
||||
) -> Option<serde_json::Value> {
|
||||
async fn fetch_json(client: &reqwest::Client, url: &str, token: &str) -> Option<serde_json::Value> {
|
||||
let resp = client
|
||||
.get(url)
|
||||
.header("Authorization", format!("token {token}"))
|
||||
|
|
@ -207,7 +203,7 @@ fn notif_type_label(t: &str) -> &str {
|
|||
/// (`heading\n===`) are not handled here — rarer in practice and
|
||||
/// would need multi-line lookahead; revisit if it actually shows up.
|
||||
///
|
||||
/// **ATX shape strictly:** CommonMark requires a space (or end-of-line)
|
||||
/// **ATX shape strictly:** `CommonMark` requires a space (or end-of-line)
|
||||
/// after the 1-6 leading `#`s to count as an ATX heading. Lines like
|
||||
/// `#tag`, `#123`, `#!/bin/bash` are NOT headings — passing them through
|
||||
/// untouched avoids the cosmetic noise argus flagged on PR #518 (`\#tag`
|
||||
|
|
@ -236,7 +232,7 @@ fn escape_md_headings(body: &str) -> String {
|
|||
out
|
||||
}
|
||||
|
||||
/// Strict CommonMark ATX-heading detector: 1-6 leading `#`s followed
|
||||
/// Strict `CommonMark` ATX-heading detector: 1-6 leading `#`s followed
|
||||
/// by either a space, tab, or end-of-line. Anything tighter (`#tag`,
|
||||
/// `#123`) is a non-heading line that the renderer will not promote.
|
||||
fn is_atx_heading(line: &str) -> bool {
|
||||
|
|
@ -244,11 +240,10 @@ fn is_atx_heading(line: &str) -> bool {
|
|||
if !(1..=6).contains(&hashes) {
|
||||
return false;
|
||||
}
|
||||
match line.as_bytes().get(hashes) {
|
||||
None => true, // bare `#` / `##` / ... on its own line
|
||||
Some(b' ') | Some(b'\t') => true, // proper ATX with space/tab after #s
|
||||
_ => false, // `#tag` / `#123` — not a heading
|
||||
}
|
||||
// Bare `#` / `##` / ... on its own line, or proper ATX with a
|
||||
// space/tab after the run of `#`s; anything else (`#tag` / `#123`)
|
||||
// is not a heading.
|
||||
matches!(line.as_bytes().get(hashes), None | Some(b' ' | b'\t'))
|
||||
}
|
||||
|
||||
fn truncate(s: &str, max: usize) -> String {
|
||||
|
|
@ -264,6 +259,69 @@ fn truncate(s: &str, max: usize) -> String {
|
|||
format!("{}…", &s[..end])
|
||||
}
|
||||
|
||||
/// Detect `@username` mentions on a line. A mention is `@` followed by
|
||||
/// at least one username char (alphanumeric / `_` / `-`) where the `@`
|
||||
/// is at line start or follows a non-username char — so email-style
|
||||
/// `foo@bar.com` does NOT count as a mention.
|
||||
fn contains_mention(line: &str) -> bool {
|
||||
let bytes = line.as_bytes();
|
||||
for (i, &b) in bytes.iter().enumerate() {
|
||||
if b != b'@' {
|
||||
continue;
|
||||
}
|
||||
// Boundary: preceding byte must NOT be a username char.
|
||||
let boundary_ok = match i.checked_sub(1).map(|j| bytes[j]) {
|
||||
None => true,
|
||||
Some(prev) => !is_username_byte(prev),
|
||||
};
|
||||
if !boundary_ok {
|
||||
continue;
|
||||
}
|
||||
// Following byte must be at least one username char.
|
||||
if bytes.get(i + 1).is_some_and(|&c| is_username_byte(c)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn is_username_byte(b: u8) -> bool {
|
||||
b.is_ascii_alphanumeric() || b == b'_' || b == b'-'
|
||||
}
|
||||
|
||||
/// Walk `full_body` line-by-line; return lines that contain an
|
||||
/// `@username` mention AND aren't already present (as a substring) in
|
||||
/// `included_excerpt`. Used to surface mentions that fell outside the
|
||||
/// truncation window so addressed agents see they were tagged even
|
||||
/// when the body is long (closes #539).
|
||||
fn extract_truncated_mention_lines<'a>(
|
||||
full_body: &'a str,
|
||||
included_excerpt: &str,
|
||||
) -> Vec<&'a str> {
|
||||
full_body
|
||||
.lines()
|
||||
.filter(|line| {
|
||||
let trimmed = line.trim();
|
||||
!trimmed.is_empty() && contains_mention(trimmed)
|
||||
})
|
||||
.filter(|line| !included_excerpt.contains(line.trim()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Build the trailing `mentions (truncated from body):\n > …` block.
|
||||
/// Empty string when there's nothing to surface. Caller embeds it
|
||||
/// directly before the meta suffix.
|
||||
fn render_truncated_mentions(lines: &[&str]) -> String {
|
||||
if lines.is_empty() {
|
||||
return String::new();
|
||||
}
|
||||
let mut out = String::from("\n\nmentions (truncated from body):");
|
||||
for line in lines {
|
||||
write!(out, "\n > {}", line.trim()).ok();
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Map a Forgejo review state to a readable action label.
|
||||
/// Returns `None` for non-review states (regular comments have no `state` field;
|
||||
/// `PENDING` means the review was saved but not submitted yet).
|
||||
|
|
@ -327,7 +385,9 @@ async fn format_notification(
|
|||
|
||||
// API URLs for fetching content
|
||||
let subject_api_url = notif["subject"]["url"].as_str().unwrap_or("");
|
||||
let comment_api_url = notif["subject"]["latest_comment_url"].as_str().unwrap_or("");
|
||||
let comment_api_url = notif["subject"]["latest_comment_url"]
|
||||
.as_str()
|
||||
.unwrap_or("");
|
||||
let comment_html_url = notif["subject"]["latest_comment_html_url"]
|
||||
.as_str()
|
||||
.unwrap_or("");
|
||||
|
|
@ -348,9 +408,27 @@ async fn format_notification(
|
|||
// by creation/state-change of the subject itself.
|
||||
let has_comment = !comment_api_url.is_empty() && comment_api_url != subject_api_url;
|
||||
|
||||
let meta = NotifMeta { title, notif_type, html_url, num, repo, meta_suffix, reason, subject, is_pr };
|
||||
let meta = NotifMeta {
|
||||
title,
|
||||
notif_type,
|
||||
html_url,
|
||||
num,
|
||||
repo,
|
||||
meta_suffix,
|
||||
reason,
|
||||
subject,
|
||||
is_pr,
|
||||
};
|
||||
if has_comment {
|
||||
format_comment_notification(client, token, &meta, comment_api_url, comment_html_url, own_login).await
|
||||
format_comment_notification(
|
||||
client,
|
||||
token,
|
||||
&meta,
|
||||
comment_api_url,
|
||||
comment_html_url,
|
||||
own_login,
|
||||
)
|
||||
.await
|
||||
} else {
|
||||
format_state_change_notification(notif, &meta, own_login)
|
||||
}
|
||||
|
|
@ -391,16 +469,28 @@ fn build_meta_suffix(subject: Option<&serde_json::Value>, is_pr: bool, reason: &
|
|||
.and_then(|s| s["requested_reviewers"].as_array())
|
||||
.map(|arr| arr.iter().filter_map(|r| r["login"].as_str()).collect())
|
||||
.unwrap_or_default();
|
||||
if reviewers.is_empty() { None } else { Some(format!("reviewer: {}", reviewers.join(", "))) }
|
||||
if reviewers.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(format!("reviewer: {}", reviewers.join(", ")))
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
// Always include reason so multiple notifications for the same event
|
||||
// (each with a different Forgejo reason) are distinguishable (closes #110).
|
||||
let reason_line = if reason.is_empty() { None } else { Some(format!("reason: {reason}")) };
|
||||
let reason_line = if reason.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(format!("reason: {reason}"))
|
||||
};
|
||||
let mut out = format!("\n{assignee_line}");
|
||||
if let Some(r) = reviewer_line { write!(out, "\n{r}").ok(); }
|
||||
if let Some(r) = reason_line { write!(out, "\n{r}").ok(); }
|
||||
if let Some(r) = reviewer_line {
|
||||
write!(out, "\n{r}").ok();
|
||||
}
|
||||
if let Some(r) = reason_line {
|
||||
write!(out, "\n{r}").ok();
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
|
|
@ -441,17 +531,42 @@ async fn format_comment_notification(
|
|||
.and_then(|c| c["state"].as_str())
|
||||
.and_then(review_state_label);
|
||||
|
||||
let url = if comment_html_url.is_empty() { meta.html_url } else { comment_html_url };
|
||||
let author = if actor_login.is_empty() { "?" } else { actor_login };
|
||||
let NotifMeta { title, notif_type, num, repo, meta_suffix, .. } = meta;
|
||||
let url = if comment_html_url.is_empty() {
|
||||
meta.html_url
|
||||
} else {
|
||||
comment_html_url
|
||||
};
|
||||
let author = if actor_login.is_empty() {
|
||||
"?"
|
||||
} else {
|
||||
actor_login
|
||||
};
|
||||
let NotifMeta {
|
||||
title,
|
||||
notif_type,
|
||||
num,
|
||||
repo,
|
||||
meta_suffix,
|
||||
..
|
||||
} = meta;
|
||||
|
||||
// Truncate the raw body first so the mention-overflow diff compares
|
||||
// like-for-like (escape_md_headings rewrites `# foo` to `\# foo`, so
|
||||
// doing it before the diff would re-surface heading-prefixed mention
|
||||
// lines as fake overflow). Escape happens after for display only.
|
||||
let raw_excerpt = truncate(body_text, BODY_TRUNCATE);
|
||||
// Surface @mentions that fell outside the truncation window so an
|
||||
// addressed agent never silently misses a tag on a long comment
|
||||
// (closes #539). Skipped when the embed wasn't actually truncated.
|
||||
let truncated_mentions = if body_text.len() > BODY_TRUNCATE {
|
||||
render_truncated_mentions(&extract_truncated_mention_lines(body_text, &raw_excerpt))
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
// Escape ATX headings in the user-authored body so the embedded
|
||||
// text doesn't blow into top-level h1/h2 in the wrapper message
|
||||
// when the dashboard renders it (closes #455). Done once here
|
||||
// because both code paths fall through the same truncate+embed
|
||||
// pattern.
|
||||
let escaped = escape_md_headings(body_text);
|
||||
let body_for_embed = truncate(&escaped, BODY_TRUNCATE);
|
||||
// when the dashboard renders it (closes #455).
|
||||
let body_for_embed = escape_md_headings(&raw_excerpt);
|
||||
if let Some(review_label) = review_state {
|
||||
// Review submission on a PR.
|
||||
let kind = format!("PR {review_label}{num}{repo}");
|
||||
|
|
@ -459,14 +574,16 @@ async fn format_comment_notification(
|
|||
if body_text.is_empty() {
|
||||
write!(out, "\n\nreviewer: {author}").ok();
|
||||
} else {
|
||||
write!(out, "\n\n{author}: {body_for_embed}").ok();
|
||||
write!(out, "\n\n{author}: {body_for_embed}{truncated_mentions}").ok();
|
||||
}
|
||||
out.push_str(meta_suffix);
|
||||
Some(out)
|
||||
} else {
|
||||
// Regular comment.
|
||||
let kind = format!("comment on {}{num}{repo}", notif_type_label(notif_type));
|
||||
let mut out = format!("[{kind}] {title}\nurl: {url}\n\n{author}: {body_for_embed}");
|
||||
let mut out = format!(
|
||||
"[{kind}] {title}\nurl: {url}\n\n{author}: {body_for_embed}{truncated_mentions}"
|
||||
);
|
||||
if out.ends_with('\n') {
|
||||
out.pop();
|
||||
}
|
||||
|
|
@ -499,7 +616,17 @@ fn format_state_change_notification(
|
|||
return None;
|
||||
}
|
||||
|
||||
let NotifMeta { title, notif_type, html_url, num, repo, meta_suffix, reason: _, subject, is_pr } = meta;
|
||||
let NotifMeta {
|
||||
title,
|
||||
notif_type,
|
||||
html_url,
|
||||
num,
|
||||
repo,
|
||||
meta_suffix,
|
||||
reason: _,
|
||||
subject,
|
||||
is_pr,
|
||||
} = meta;
|
||||
let label = notif_type_label(notif_type);
|
||||
let kind = match notif_state {
|
||||
"merged" => format!("{label} merged{num}{repo}"),
|
||||
|
|
@ -526,12 +653,41 @@ fn format_state_change_notification(
|
|||
kind
|
||||
};
|
||||
|
||||
let mut out = format!("[{kind}] {title}\nurl: {html_url}");
|
||||
// Include the start of the issue/PR description so the agent gets
|
||||
// context without a follow-up fetch (closes #539). Same escape +
|
||||
// truncate pipeline as comment bodies. Mentions that fell outside
|
||||
// the truncation window are surfaced separately so an addressed
|
||||
// agent never silently misses a long-body @tag.
|
||||
let body_block = subject
|
||||
.as_ref()
|
||||
.and_then(|s| s["body"].as_str())
|
||||
.map(str::trim)
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(|raw| {
|
||||
// Truncate raw first so mention diff sees the same heading
|
||||
// markers as full_body (see comment in
|
||||
// format_comment_notification). Escape happens after for
|
||||
// display only.
|
||||
let raw_excerpt = truncate(raw, BODY_TRUNCATE);
|
||||
let truncated = extract_truncated_mention_lines(raw, &raw_excerpt);
|
||||
let mentions = render_truncated_mentions(&truncated);
|
||||
let excerpt = escape_md_headings(&raw_excerpt);
|
||||
format!("\n\n{excerpt}{mentions}")
|
||||
})
|
||||
.unwrap_or_default();
|
||||
|
||||
let mut out = format!("[{kind}] {title}\nurl: {html_url}{body_block}");
|
||||
out.push_str(meta_suffix);
|
||||
Some(out)
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
#[allow(
|
||||
clippy::too_many_lines,
|
||||
reason = "single-pass notification poll loop — split would obscure the \
|
||||
sequential 'fetch / classify / dispatch' rhythm and add helper \
|
||||
functions for state shared across all three phases"
|
||||
)]
|
||||
async fn poll_once(
|
||||
client: &reqwest::Client,
|
||||
forge_url: &str,
|
||||
|
|
@ -574,10 +730,15 @@ async fn poll_once(
|
|||
return;
|
||||
}
|
||||
|
||||
debug!(count = notifications.len(), "forge_notify: delivering notifications");
|
||||
debug!(
|
||||
count = notifications.len(),
|
||||
"forge_notify: delivering notifications"
|
||||
);
|
||||
|
||||
for notif in ¬ifications {
|
||||
let Some(id) = notif["id"].as_u64() else { continue };
|
||||
let Some(id) = notif["id"].as_u64() else {
|
||||
continue;
|
||||
};
|
||||
|
||||
// Reason drop-list: suppress noisy reasons (subscribed/participating).
|
||||
// null/unknown reasons pass through — directed signals are never
|
||||
|
|
@ -635,28 +796,30 @@ async fn poll_once(
|
|||
// when HIVE_FORGE_KEEP_SUBSCRIPTIONS=1 — triage and other firehose
|
||||
// consumers set this to retain broad repo visibility.
|
||||
let reason = notif["reason"].as_str().unwrap_or("");
|
||||
if !keep_subscriptions && reason == "subscribed"
|
||||
if !keep_subscriptions
|
||||
&& reason == "subscribed"
|
||||
&& let Some(repo) = notif["repository"]["full_name"].as_str()
|
||||
&& !unsubbed_repos.contains(repo) {
|
||||
let unsub_url = format!("{forge_url}/api/v1/repos/{repo}/subscription");
|
||||
match client
|
||||
.delete(&unsub_url)
|
||||
.header("Authorization", format!("token {token}"))
|
||||
.send()
|
||||
.await
|
||||
{
|
||||
Ok(r) if r.status().is_success() || r.status().as_u16() == 404 => {
|
||||
debug!(%repo, "forge_notify: unsubscribed from repo watch");
|
||||
unsubbed_repos.insert(repo.to_owned());
|
||||
}
|
||||
Ok(r) => {
|
||||
debug!(%repo, status = %r.status(), "forge_notify: unsub non-2xx (ignored)");
|
||||
}
|
||||
Err(e) => {
|
||||
debug!(%repo, error = ?e, "forge_notify: unsub request failed (ignored)");
|
||||
}
|
||||
}
|
||||
&& !unsubbed_repos.contains(repo)
|
||||
{
|
||||
let unsub_url = format!("{forge_url}/api/v1/repos/{repo}/subscription");
|
||||
match client
|
||||
.delete(&unsub_url)
|
||||
.header("Authorization", format!("token {token}"))
|
||||
.send()
|
||||
.await
|
||||
{
|
||||
Ok(r) if r.status().is_success() || r.status().as_u16() == 404 => {
|
||||
debug!(%repo, "forge_notify: unsubscribed from repo watch");
|
||||
unsubbed_repos.insert(repo.to_owned());
|
||||
}
|
||||
Ok(r) => {
|
||||
debug!(%repo, status = %r.status(), "forge_notify: unsub non-2xx (ignored)");
|
||||
}
|
||||
Err(e) => {
|
||||
debug!(%repo, error = ?e, "forge_notify: unsub request failed (ignored)");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -772,4 +935,89 @@ mod tests {
|
|||
assert_eq!(escape_md_headings("body\n"), "body\n");
|
||||
assert_eq!(escape_md_headings("no trailing"), "no trailing");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn contains_mention_matches_at_line_start_and_mid_line() {
|
||||
assert!(contains_mention("@damocles take a look"));
|
||||
assert!(contains_mention("cc @argus please"));
|
||||
assert!(contains_mention("see (@mara) for context"));
|
||||
// Hyphens / underscores / digits are valid username chars.
|
||||
assert!(contains_mention("ping @h-m1nd-2"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn contains_mention_rejects_email_and_bare_at() {
|
||||
// Email addresses (`foo@bar.com`) and `@` followed by
|
||||
// whitespace or punctuation are not mentions — boundary check
|
||||
// requires the preceding byte to NOT be a username char.
|
||||
assert!(!contains_mention("foo@bar.com"));
|
||||
assert!(!contains_mention("send to user@example.org"));
|
||||
assert!(!contains_mention("just an @"));
|
||||
assert!(!contains_mention("@ space"));
|
||||
assert!(!contains_mention("plain text"));
|
||||
assert!(!contains_mention(""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extract_truncated_keeps_mention_lines_outside_excerpt() {
|
||||
// Long body where the @mention sits AFTER the excerpt's cutoff
|
||||
// — the truncated extractor must surface it.
|
||||
let full = "first line\nsecond line\n@damocles tagged here\n";
|
||||
let excerpt = "first line\nsecond line\n…"; // mention not present
|
||||
let lines = extract_truncated_mention_lines(full, excerpt);
|
||||
assert_eq!(lines, vec!["@damocles tagged here"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extract_truncated_drops_mentions_already_in_excerpt() {
|
||||
// Mention is inside the embed window already — no need to
|
||||
// re-surface, would be noise.
|
||||
let full = "@damocles read this\nmore body\n";
|
||||
let excerpt = "@damocles read this\nmore body\n…";
|
||||
let lines = extract_truncated_mention_lines(full, excerpt);
|
||||
assert!(lines.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extract_truncated_skips_blank_and_no_mention_lines() {
|
||||
// Only lines with an actual mention survive — random body
|
||||
// text past the cutoff stays dropped.
|
||||
let full = "first\n\nsecond paragraph\n@argus reviewer\nfinal\n";
|
||||
let excerpt = "first";
|
||||
let lines = extract_truncated_mention_lines(full, excerpt);
|
||||
assert_eq!(lines, vec!["@argus reviewer"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extract_truncated_does_not_resurface_heading_mention_inside_window() {
|
||||
// Regression for argus's nit on PR #544: the diff used to
|
||||
// compare full_body against the *escaped* excerpt. Lines like
|
||||
// `# @argus check this` survived as-is in the body but became
|
||||
// `\# @argus check this` in the excerpt, so the `contains`
|
||||
// check failed and the mention was re-surfaced as if it had
|
||||
// fallen outside the window. Pass the unescaped excerpt and
|
||||
// the duplicate disappears.
|
||||
let full = "# @argus check this\nmore body\n";
|
||||
let raw_excerpt = full; // fits entirely
|
||||
let lines = extract_truncated_mention_lines(full, raw_excerpt);
|
||||
assert!(lines.is_empty(), "heading+mention inside window must not be re-surfaced, got {lines:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn render_truncated_mentions_empty_is_empty_string() {
|
||||
// Zero overhead on the healthy short-body path: caller
|
||||
// concatenates this directly so an empty input must produce
|
||||
// no spacing.
|
||||
assert_eq!(render_truncated_mentions(&[]), "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn render_truncated_mentions_formats_block() {
|
||||
let lines = ["cc @damocles", " @argus second mention"];
|
||||
let rendered = render_truncated_mentions(&lines);
|
||||
assert_eq!(
|
||||
rendered,
|
||||
"\n\nmentions (truncated from body):\n > cc @damocles\n > @argus second mention",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ pub enum SocketReply {
|
|||
Recent(Vec<hive_sh4re::InboxRow>),
|
||||
Logs(String),
|
||||
/// `list_schedules` result — used by the manager surface only;
|
||||
/// AgentResponse has no equivalent variant.
|
||||
/// `AgentResponse` has no equivalent variant.
|
||||
Schedules(Vec<hive_sh4re::WireSchedule>),
|
||||
LooseEnds(Vec<hive_sh4re::LooseEnd>),
|
||||
PendingRemindersCount(u64),
|
||||
|
|
@ -185,8 +185,7 @@ pub fn format_recv(resp: Result<SocketReply, anyhow::Error>) -> String {
|
|||
/// resurfaced by `RequeueInflight` on this session's boot. Same
|
||||
/// string surfaces in the wake prompt (see the bin loops) and the
|
||||
/// in-turn `recv` tool result so claude sees the warning either way.
|
||||
pub const REDELIVERY_HINT: &str =
|
||||
"[redelivered after harness restart — may already be handled]\n";
|
||||
pub const REDELIVERY_HINT: &str = "[redelivered after harness restart — may already be handled]\n";
|
||||
|
||||
/// Format helper for `get_loose_ends`: renders a short bulleted list
|
||||
/// of pending approvals + questions + reminders. Empty list collapses
|
||||
|
|
@ -307,17 +306,32 @@ pub fn format_agent_meta(resp: Result<SocketReply, anyhow::Error>) -> String {
|
|||
match status_text {
|
||||
None => out.push_str("\nstatus: <none>"),
|
||||
Some(s) => {
|
||||
use std::fmt::Write as _;
|
||||
let age = status_set_at.and_then(|ts| {
|
||||
let now = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.ok()?
|
||||
.as_secs();
|
||||
let secs = now.saturating_sub(ts as u64);
|
||||
// `ts` is a unix epoch second the agent itself
|
||||
// sourced from `SystemTime` — always positive
|
||||
// in normal operation. Clamp the negative
|
||||
// (clock-skew) edge to 0 before the unsigned
|
||||
// cast so the cast loses no real precision.
|
||||
let ts_secs = u64::try_from(ts).unwrap_or(0);
|
||||
let secs = now.saturating_sub(ts_secs);
|
||||
Some(format_age_secs(secs))
|
||||
});
|
||||
// `write!` into the buffer instead of `push_str(&format!(…))` —
|
||||
// avoids the intermediate allocation clippy::format_push_string
|
||||
// flags. The infallible `String` writer makes this safe to
|
||||
// `let _ =`-ignore.
|
||||
match age {
|
||||
Some(a) => out.push_str(&format!("\nstatus: {s} (set {a} ago)")),
|
||||
None => out.push_str(&format!("\nstatus: {s}")),
|
||||
Some(a) => {
|
||||
let _ = write!(out, "\nstatus: {s} (set {a} ago)");
|
||||
}
|
||||
None => {
|
||||
let _ = write!(out, "\nstatus: {s}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -626,9 +640,13 @@ impl AgentServer {
|
|||
)]
|
||||
async fn set_status(&self, Parameters(args): Parameters<SetStatusArgs>) -> String {
|
||||
run_tool_envelope("set_status", args.text.clone(), async move {
|
||||
let (resp, retries) =
|
||||
self.dispatch(hive_sh4re::AgentRequest::SetStatus { text: args.text }).await;
|
||||
annotate_retries(format_ack(resp, "set_status", "status updated".to_owned()), retries)
|
||||
let (resp, retries) = self
|
||||
.dispatch(hive_sh4re::AgentRequest::SetStatus { text: args.text })
|
||||
.await;
|
||||
annotate_retries(
|
||||
format_ack(resp, "set_status", "status updated".to_owned()),
|
||||
retries,
|
||||
)
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
|
@ -644,10 +662,7 @@ impl AgentServer {
|
|||
where the system-prompt label could be stale. Status reads `<none>` when the \
|
||||
target has never called `set_status` or has cleared it."
|
||||
)]
|
||||
async fn get_agent_meta(
|
||||
&self,
|
||||
Parameters(args): Parameters<GetAgentMetaArgs>,
|
||||
) -> String {
|
||||
async fn get_agent_meta(&self, Parameters(args): Parameters<GetAgentMetaArgs>) -> String {
|
||||
let log = args.name.clone().unwrap_or_else(|| "<self>".to_owned());
|
||||
run_tool_envelope("get_agent_meta", log, async move {
|
||||
let (resp, retries) = self
|
||||
|
|
@ -679,7 +694,11 @@ impl AgentServer {
|
|||
.dispatch(hive_sh4re::AgentRequest::CancelLooseEnd { kind, id })
|
||||
.await;
|
||||
annotate_retries(
|
||||
format_ack(resp, "cancel_loose_end", format!("cancelled {kind_label} {id}")),
|
||||
format_ack(
|
||||
resp,
|
||||
"cancel_loose_end",
|
||||
format!("cancelled {kind_label} {id}"),
|
||||
),
|
||||
retries,
|
||||
)
|
||||
})
|
||||
|
|
@ -721,7 +740,10 @@ impl AgentServer {
|
|||
file_path: args.file_path,
|
||||
})
|
||||
.await;
|
||||
annotate_retries(format_ack(resp, "remind", "reminder scheduled".to_string()), retries)
|
||||
annotate_retries(
|
||||
format_ack(resp, "remind", "reminder scheduled".to_string()),
|
||||
retries,
|
||||
)
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
|
@ -949,7 +971,7 @@ pub struct RequestSchedulePromptArgs {
|
|||
/// `None` / absent = one-shot. `Some(n > 0)` = recurring every
|
||||
/// `n` seconds. The worker clamps catch-up so a long downtime
|
||||
/// fires ONCE on resume (skipped-cycle count surfaces in the
|
||||
/// per-target last_result), not N delayed pulses in a row.
|
||||
/// per-target `last_result`), not N delayed pulses in a row.
|
||||
#[serde(default)]
|
||||
pub interval_seconds: Option<u64>,
|
||||
/// Optional description shown on the dashboard approval card +
|
||||
|
|
@ -1301,10 +1323,7 @@ impl ManagerServer {
|
|||
Authorization mirrors `cancel_schedule`: you can fire your own schedules + any \
|
||||
owned by a sub-agent in your subtree per topology.json."
|
||||
)]
|
||||
async fn fire_schedule_now(
|
||||
&self,
|
||||
Parameters(args): Parameters<FireScheduleNowArgs>,
|
||||
) -> String {
|
||||
async fn fire_schedule_now(&self, Parameters(args): Parameters<FireScheduleNowArgs>) -> String {
|
||||
let log = format!("{args:?}");
|
||||
run_tool_envelope("fire_schedule_now", log, async move {
|
||||
let id = args.id;
|
||||
|
|
@ -1337,7 +1356,10 @@ impl ManagerServer {
|
|||
targets: args.targets,
|
||||
})
|
||||
.await;
|
||||
annotate_retries(format_ack(resp, "cancel_schedule", format!("cancelled #{id}")), retries)
|
||||
annotate_retries(
|
||||
format_ack(resp, "cancel_schedule", format!("cancelled #{id}")),
|
||||
retries,
|
||||
)
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
|
@ -1394,7 +1416,9 @@ impl ManagerServer {
|
|||
)]
|
||||
async fn list_schedules(&self) -> String {
|
||||
run_tool_envelope("list_schedules", String::new(), async move {
|
||||
let (resp, retries) = self.dispatch(hive_sh4re::ManagerRequest::ListSchedules).await;
|
||||
let (resp, retries) = self
|
||||
.dispatch(hive_sh4re::ManagerRequest::ListSchedules)
|
||||
.await;
|
||||
let body = match resp {
|
||||
Ok(SocketReply::Schedules(schedules)) => serde_json::to_string(&schedules)
|
||||
.unwrap_or_else(|e| format!("list_schedules: serialise: {e:#}")),
|
||||
|
|
@ -1541,7 +1565,10 @@ impl ManagerServer {
|
|||
file_path: args.file_path,
|
||||
})
|
||||
.await;
|
||||
annotate_retries(format_ack(resp, "remind", "reminder scheduled".to_string()), retries)
|
||||
annotate_retries(
|
||||
format_ack(resp, "remind", "reminder scheduled".to_string()),
|
||||
retries,
|
||||
)
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
|
@ -1573,9 +1600,13 @@ impl ManagerServer {
|
|||
)]
|
||||
async fn set_status(&self, Parameters(args): Parameters<SetStatusArgs>) -> String {
|
||||
run_tool_envelope("set_status", args.text.clone(), async move {
|
||||
let (resp, retries) =
|
||||
self.dispatch(hive_sh4re::ManagerRequest::SetStatus { text: args.text }).await;
|
||||
annotate_retries(format_ack(resp, "set_status", "status updated".to_owned()), retries)
|
||||
let (resp, retries) = self
|
||||
.dispatch(hive_sh4re::ManagerRequest::SetStatus { text: args.text })
|
||||
.await;
|
||||
annotate_retries(
|
||||
format_ack(resp, "set_status", "status updated".to_owned()),
|
||||
retries,
|
||||
)
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
|
@ -1590,10 +1621,7 @@ impl ManagerServer {
|
|||
drift across renames. Status reads `<none>` when the target has never called \
|
||||
`set_status` or has cleared it."
|
||||
)]
|
||||
async fn get_agent_meta(
|
||||
&self,
|
||||
Parameters(args): Parameters<GetAgentMetaArgs>,
|
||||
) -> String {
|
||||
async fn get_agent_meta(&self, Parameters(args): Parameters<GetAgentMetaArgs>) -> String {
|
||||
let log = args.name.clone().unwrap_or_else(|| "<self>".to_owned());
|
||||
run_tool_envelope("get_agent_meta", log, async move {
|
||||
let (resp, retries) = self
|
||||
|
|
@ -1628,7 +1656,11 @@ impl ManagerServer {
|
|||
.dispatch(hive_sh4re::ManagerRequest::CancelLooseEnd { kind, id })
|
||||
.await;
|
||||
annotate_retries(
|
||||
format_ack(resp, "cancel_loose_end", format!("cancelled {kind_label} {id}")),
|
||||
format_ack(
|
||||
resp,
|
||||
"cancel_loose_end",
|
||||
format!("cancelled {kind_label} {id}"),
|
||||
),
|
||||
retries,
|
||||
)
|
||||
})
|
||||
|
|
@ -1800,10 +1832,7 @@ pub fn allowed_tools_arg(flavor: Flavor) -> String {
|
|||
if patterns.is_empty() {
|
||||
vec!["Bash".to_owned()]
|
||||
} else {
|
||||
patterns
|
||||
.into_iter()
|
||||
.map(|p| format!("Bash({p})"))
|
||||
.collect()
|
||||
patterns.into_iter().map(|p| format!("Bash({p})")).collect()
|
||||
}
|
||||
} else {
|
||||
vec![(*s).to_owned()]
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
|||
use tokio::process::Command;
|
||||
|
||||
use crate::events::{Bus, LiveEvent};
|
||||
use crate::login::{self, LoginState};
|
||||
use crate::login::LoginState;
|
||||
use crate::mcp;
|
||||
|
||||
/// `--settings` JSON applied to every claude invocation. Lives as a
|
||||
|
|
@ -235,6 +235,7 @@ fn effective_context_window(bus: &Bus) -> u64 {
|
|||
/// Resolve the auto-reset watermark. Priority order:
|
||||
/// 1. `HIVE_AUTO_RESET_WATERMARK_TOKENS` env var (explicit override).
|
||||
/// 2. 50% of `effective_context_window(bus)`.
|
||||
///
|
||||
/// `0` disables auto-reset entirely.
|
||||
fn auto_reset_watermark_tokens(bus: &Bus) -> u64 {
|
||||
if let Some(v) = std::env::var("HIVE_AUTO_RESET_WATERMARK_TOKENS")
|
||||
|
|
@ -259,6 +260,7 @@ fn cache_ttl_secs() -> u64 {
|
|||
/// Resolve the proactive-compaction watermark. Priority order:
|
||||
/// 1. `HIVE_COMPACT_WATERMARK_TOKENS` env var (explicit override).
|
||||
/// 2. 75% of `effective_context_window(bus)`.
|
||||
///
|
||||
/// `0` disables proactive compaction (reactive path still applies).
|
||||
fn compact_watermark_tokens(bus: &Bus) -> u64 {
|
||||
if let Some(v) = std::env::var("HIVE_COMPACT_WATERMARK_TOKENS")
|
||||
|
|
@ -299,9 +301,7 @@ pub async fn drive_turn(prompt: &str, files: &TurnFiles, bus: &Bus) -> TurnOutco
|
|||
// /compact itself would be absurd recursion; bubble it up as
|
||||
// a normal failure path.
|
||||
match compact_session(files, bus).await {
|
||||
TurnOutcome::Ok | TurnOutcome::Compacted => {
|
||||
run_turn(prompt, files, bus).await
|
||||
}
|
||||
TurnOutcome::Ok | TurnOutcome::Compacted => run_turn(prompt, files, bus).await,
|
||||
other => return other,
|
||||
}
|
||||
}
|
||||
|
|
@ -315,10 +315,9 @@ pub async fn drive_turn(prompt: &str, files: &TurnFiles, bus: &Bus) -> TurnOutco
|
|||
// turn overflows into the reactive path. Best-effort — never changes
|
||||
// the outcome of the turn that already succeeded, but records it as
|
||||
// `Compacted` so turn stats can distinguish it from a plain `Ok`.
|
||||
if matches!(outcome, TurnOutcome::Ok)
|
||||
&& maybe_checkpoint_and_compact(files, bus).await {
|
||||
return TurnOutcome::Compacted;
|
||||
}
|
||||
if matches!(outcome, TurnOutcome::Ok) && maybe_checkpoint_and_compact(files, bus).await {
|
||||
return TurnOutcome::Compacted;
|
||||
}
|
||||
outcome
|
||||
}
|
||||
|
||||
|
|
@ -464,9 +463,20 @@ pub fn emit_turn_end(bus: &Bus, outcome: &TurnOutcome) {
|
|||
}
|
||||
}
|
||||
|
||||
/// Block until the bound `~/.claude/` dir contains a session, polling
|
||||
/// `claude_dir` on a `poll_ms` interval (min 2s). Flips `state` to
|
||||
/// `Online` when login lands; caller resumes its serve loop.
|
||||
/// Block until the bound `~/.claude/` dir contains a session that
|
||||
/// post-dates this call, polling on a `poll_ms` interval (min 2s).
|
||||
/// Flips `state` to `Online` when login lands; caller resumes its
|
||||
/// serve loop.
|
||||
///
|
||||
/// **Mtime-progress, not bare existence (closes #542)**: an
|
||||
/// existence-only check (the pre-#542 behaviour) immediately returns
|
||||
/// after a 401 because the stale `credentials.json` is still on disk
|
||||
/// — the next turn then 401s on the same tokens and the harness
|
||||
/// loops forever. We snapshot the newest file mtime in `claude_dir`
|
||||
/// at entry and only resume when something has been written since
|
||||
/// that snapshot (the operator's `/login/code` flow lands a refreshed
|
||||
/// credentials file, bumping its mtime). First-time login (empty
|
||||
/// dir → `None` snapshot) still flips on the first file appearing.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
|
|
@ -481,11 +491,12 @@ pub async fn wait_for_login(
|
|||
claude_dir = %claude_dir.display(),
|
||||
"no claude session — staying in partial-run mode (web UI only)"
|
||||
);
|
||||
let snapshot = snapshot_dir(claude_dir);
|
||||
let probe = Duration::from_millis(poll_ms.max(2000));
|
||||
loop {
|
||||
tokio::time::sleep(probe).await;
|
||||
if login::has_session(claude_dir) {
|
||||
tracing::info!("claude session detected — entering turn loop");
|
||||
if session_refreshed(snapshot, snapshot_dir(claude_dir)) {
|
||||
tracing::info!("claude session refreshed — entering turn loop");
|
||||
*state.lock().unwrap() = LoginState::Online;
|
||||
bus.emit_status("online");
|
||||
return;
|
||||
|
|
@ -493,6 +504,61 @@ pub async fn wait_for_login(
|
|||
}
|
||||
}
|
||||
|
||||
/// Snapshot of the credentials dir at a point in time: number of
|
||||
/// regular files + newest `mtime` across them. The two axes are both
|
||||
/// load-bearing for `wait_for_login`'s refresh check (`session_refreshed`):
|
||||
/// mtime catches the common case (re-login overwrites an existing
|
||||
/// credentials file in-place), file_count catches the pathological case
|
||||
/// where `meta.modified()` errors on every file (exotic fs, NFS quirks)
|
||||
/// so the mtime axis stays `None` forever but new files still trigger a
|
||||
/// resume. Defaults to `{0, None}` on read_dir failure (missing or
|
||||
/// unreadable dir) — `wait_for_login` then resumes when files first
|
||||
/// appear.
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
|
||||
struct DirSnapshot {
|
||||
file_count: usize,
|
||||
newest_mtime: Option<std::time::SystemTime>,
|
||||
}
|
||||
|
||||
fn snapshot_dir(dir: &Path) -> DirSnapshot {
|
||||
let Ok(entries) = std::fs::read_dir(dir) else {
|
||||
return DirSnapshot::default();
|
||||
};
|
||||
let mut snap = DirSnapshot::default();
|
||||
for entry in entries.flatten() {
|
||||
if !entry.file_type().is_ok_and(|t| t.is_file()) {
|
||||
continue;
|
||||
}
|
||||
snap.file_count += 1;
|
||||
let Ok(meta) = entry.metadata() else { continue };
|
||||
let Ok(mtime) = meta.modified() else { continue };
|
||||
if snap.newest_mtime.is_none_or(|cur| mtime > cur) {
|
||||
snap.newest_mtime = Some(mtime);
|
||||
}
|
||||
}
|
||||
snap
|
||||
}
|
||||
|
||||
/// Has the credentials dir been written since `prev`? Used as the
|
||||
/// exit condition for `wait_for_login`:
|
||||
///
|
||||
/// - file_count changed → something was added or removed, treat as
|
||||
/// refresh (covers the "all files have unreadable mtime" edge case).
|
||||
/// - newest_mtime advanced → existing file was rewritten in place
|
||||
/// (the common claude re-login path).
|
||||
/// - prev had no mtime (empty or all-unreadable) and now has one →
|
||||
/// first useful signal we've seen, treat as refresh.
|
||||
fn session_refreshed(prev: DirSnapshot, now: DirSnapshot) -> bool {
|
||||
if now.file_count != prev.file_count {
|
||||
return true;
|
||||
}
|
||||
match (prev.newest_mtime, now.newest_mtime) {
|
||||
(None, Some(_)) => true,
|
||||
(Some(p), Some(n)) => n > p,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Spawn `claude` for one turn and pump `stream-json` stdout into the
|
||||
/// live event bus. Prompt goes over stdin (variadic
|
||||
/// `--allowedTools`/`--tools` would otherwise eat a trailing positional
|
||||
|
|
@ -640,53 +706,48 @@ async fn run_claude(prompt: &str, files: &TurnFiles, bus: &Bus) -> Result<(bool,
|
|||
if AUTH_FAIL_MARKERS.iter().any(|m| line.contains(m)) {
|
||||
auth_out.store(true, Ordering::Relaxed);
|
||||
}
|
||||
match serde_json::from_str::<serde_json::Value>(&line) {
|
||||
Ok(v) => {
|
||||
// Rate-limit detection: only fire on JSON `error` events,
|
||||
// not on arbitrary text content. An agent discussing a past
|
||||
// rate limit in its response would otherwise trigger a false
|
||||
// positive (the full conversation flows through stdout as
|
||||
// stream-json, so any text the model outputs is visible here).
|
||||
if v.get("type").and_then(|t| t.as_str()) == Some("error") {
|
||||
let raw = v.to_string();
|
||||
if RATE_LIMIT_MARKERS.iter().any(|m| raw.contains(m)) {
|
||||
rate_out.store(true, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
if let Some(u) = crate::events::TokenUsage::from_assistant_event(&v) {
|
||||
last_inference = Some(u);
|
||||
}
|
||||
if let Some(cost) = crate::events::TokenUsage::from_stream_event(&v) {
|
||||
// Fallback to `cost` if the turn somehow produced
|
||||
// a result without any assistant event — keeps the
|
||||
// ctx badge from going stale on a degenerate turn.
|
||||
let ctx = last_inference.unwrap_or(cost);
|
||||
bus_out.record_turn_usage(ctx, cost);
|
||||
}
|
||||
// Seed the API-reported context-window from the result
|
||||
// event's `modelUsage.*.contextWindow` field. This is
|
||||
// the authoritative per-inference active window used for
|
||||
// compaction watermarks — it reflects what the model
|
||||
// actually enforces, which may differ from the Nix
|
||||
// config (e.g. 200k active window on a 1M cache model).
|
||||
if let Some(w) =
|
||||
crate::events::TokenUsage::context_window_from_result_event(&v)
|
||||
{
|
||||
bus_out.set_api_context_window(w);
|
||||
}
|
||||
bus_out.observe_stream(&v);
|
||||
bus_out.emit(LiveEvent::Stream(v));
|
||||
}
|
||||
Err(_) => {
|
||||
// Non-JSON stdout: raw text check is fine here since these
|
||||
// are claude CLI messages, not conversation content.
|
||||
if RATE_LIMIT_MARKERS.iter().any(|m| line.contains(m)) {
|
||||
if let Ok(v) = serde_json::from_str::<serde_json::Value>(&line) {
|
||||
// Rate-limit detection: only fire on JSON `error` events,
|
||||
// not on arbitrary text content. An agent discussing a past
|
||||
// rate limit in its response would otherwise trigger a false
|
||||
// positive (the full conversation flows through stdout as
|
||||
// stream-json, so any text the model outputs is visible here).
|
||||
if v.get("type").and_then(|t| t.as_str()) == Some("error") {
|
||||
let raw = v.to_string();
|
||||
if RATE_LIMIT_MARKERS.iter().any(|m| raw.contains(m)) {
|
||||
rate_out.store(true, Ordering::Relaxed);
|
||||
}
|
||||
bus_out.emit(LiveEvent::Note {
|
||||
text: format!("(non-json) {line}"),
|
||||
});
|
||||
}
|
||||
if let Some(u) = crate::events::TokenUsage::from_assistant_event(&v) {
|
||||
last_inference = Some(u);
|
||||
}
|
||||
if let Some(cost) = crate::events::TokenUsage::from_stream_event(&v) {
|
||||
// Fallback to `cost` if the turn somehow produced
|
||||
// a result without any assistant event — keeps the
|
||||
// ctx badge from going stale on a degenerate turn.
|
||||
let ctx = last_inference.unwrap_or(cost);
|
||||
bus_out.record_turn_usage(ctx, cost);
|
||||
}
|
||||
// Seed the API-reported context-window from the result
|
||||
// event's `modelUsage.*.contextWindow` field. This is
|
||||
// the authoritative per-inference active window used for
|
||||
// compaction watermarks — it reflects what the model
|
||||
// actually enforces, which may differ from the Nix
|
||||
// config (e.g. 200k active window on a 1M cache model).
|
||||
if let Some(w) = crate::events::TokenUsage::context_window_from_result_event(&v) {
|
||||
bus_out.set_api_context_window(w);
|
||||
}
|
||||
bus_out.observe_stream(&v);
|
||||
bus_out.emit(LiveEvent::Stream(v));
|
||||
} else {
|
||||
// Non-JSON stdout: raw text check is fine here since these
|
||||
// are claude CLI messages, not conversation content.
|
||||
if RATE_LIMIT_MARKERS.iter().any(|m| line.contains(m)) {
|
||||
rate_out.store(true, Ordering::Relaxed);
|
||||
}
|
||||
bus_out.emit(LiveEvent::Note {
|
||||
text: format!("(non-json) {line}"),
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
@ -737,3 +798,114 @@ async fn run_claude(prompt: &str, files: &TurnFiles, bus: &Bus) -> Result<(bool,
|
|||
}
|
||||
Ok((too_long, is_rate_limited, is_auth_failed))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::fs;
|
||||
use std::time::{Duration, SystemTime};
|
||||
|
||||
use super::{DirSnapshot, session_refreshed, snapshot_dir};
|
||||
|
||||
#[test]
|
||||
fn snapshot_dir_empty_dir_is_default() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let snap = snapshot_dir(dir.path());
|
||||
assert_eq!(snap.file_count, 0);
|
||||
assert!(snap.newest_mtime.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snapshot_dir_missing_dir_is_default() {
|
||||
// Defensive: a nonexistent dir must NOT panic. Bind mounts that
|
||||
// disappear mid-poll (host purge during operator intervention)
|
||||
// would otherwise crash the harness.
|
||||
let missing = tempfile::tempdir()
|
||||
.unwrap()
|
||||
.path()
|
||||
.join("never-created-subdir");
|
||||
let snap = snapshot_dir(&missing);
|
||||
assert_eq!(snap, DirSnapshot::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snapshot_dir_picks_latest_mtime_and_counts_files() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
fs::write(dir.path().join("old.json"), b"{}").unwrap();
|
||||
// Sleep so the second file's mtime is strictly greater than
|
||||
// the first on filesystems with low timestamp resolution.
|
||||
std::thread::sleep(Duration::from_millis(20));
|
||||
let newer_path = dir.path().join("newer.json");
|
||||
fs::write(&newer_path, b"{}").unwrap();
|
||||
let snap = snapshot_dir(dir.path());
|
||||
assert_eq!(snap.file_count, 2);
|
||||
let newer_meta = fs::metadata(&newer_path).unwrap().modified().unwrap();
|
||||
assert_eq!(snap.newest_mtime, Some(newer_meta));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn session_refreshed_first_login_flips_on_any_file() {
|
||||
// Empty-dir snapshot → any file appearing means a fresh
|
||||
// login landed. Pre-#542 semantics for first-time login.
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let snapshot = snapshot_dir(dir.path());
|
||||
assert!(!session_refreshed(snapshot, snapshot_dir(dir.path())));
|
||||
fs::write(dir.path().join("credentials.json"), b"{}").unwrap();
|
||||
assert!(session_refreshed(snapshot, snapshot_dir(dir.path())));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn session_refreshed_stale_creds_dont_flip_immediately() {
|
||||
// The #542 repro: stale credentials.json already exists at
|
||||
// entry; wait_for_login must NOT immediately return — it
|
||||
// would loop straight into another 401-failing turn.
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
fs::write(dir.path().join("credentials.json"), b"{}").unwrap();
|
||||
let snapshot = snapshot_dir(dir.path());
|
||||
assert_eq!(snapshot.file_count, 1);
|
||||
// No change to the file → loop must NOT exit.
|
||||
assert!(!session_refreshed(snapshot, snapshot_dir(dir.path())));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn session_refreshed_after_creds_rewrite_flips() {
|
||||
// After the stale-creds snapshot, the operator's `/login/code`
|
||||
// flow lands a refreshed credentials file — its mtime bumps
|
||||
// strictly past the snapshot and wait_for_login resumes.
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
fs::write(dir.path().join("credentials.json"), b"{}").unwrap();
|
||||
let snapshot = snapshot_dir(dir.path());
|
||||
std::thread::sleep(Duration::from_millis(20));
|
||||
fs::write(dir.path().join("credentials.json"), b"{\"v\":2}").unwrap();
|
||||
assert!(session_refreshed(snapshot, snapshot_dir(dir.path())));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn session_refreshed_snapshot_with_future_mtime_doesnt_flip() {
|
||||
// Defensive: a snapshot set to a future timestamp (e.g. clock
|
||||
// skew between snapshot and probe) must keep waiting until a
|
||||
// file's mtime actually exceeds it, not return on first poll.
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
fs::write(dir.path().join("credentials.json"), b"{}").unwrap();
|
||||
let snapshot = DirSnapshot {
|
||||
file_count: 1,
|
||||
newest_mtime: Some(SystemTime::now() + Duration::from_secs(3600)),
|
||||
};
|
||||
assert!(!session_refreshed(snapshot, snapshot_dir(dir.path())));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn session_refreshed_count_change_flips_when_mtime_unreadable() {
|
||||
// Defensive (argus #545 nit): if all files have unreadable
|
||||
// `meta.modified()` (exotic fs / NFS), newest_mtime stays
|
||||
// `None` forever — but file_count axis still catches new
|
||||
// files appearing. Simulated here by forging a snapshot with
|
||||
// file_count=1 + no mtime, then writing a second file.
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
fs::write(dir.path().join("a"), b"{}").unwrap();
|
||||
let forged = DirSnapshot { file_count: 1, newest_mtime: None };
|
||||
fs::write(dir.path().join("b"), b"{}").unwrap();
|
||||
// Real snapshot has file_count=2, so refresh fires even
|
||||
// though the mtime axis would be inconclusive.
|
||||
assert!(session_refreshed(forged, snapshot_dir(dir.path())));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ edition.workspace = true
|
|||
version.workspace = true
|
||||
# Render branding/agent-configs.svg → $OUT_DIR/agent-configs.png at
|
||||
# compile time (#424). build.rs shells out to `rsvg-convert`
|
||||
# (librsvg, pulled in via flake.nix' naersk nativeBuildInputs); the
|
||||
# (librsvg, pulled in via flake.nix' crane nativeBuildInputs); the
|
||||
# baked PNG is included via include_bytes! from forge.rs so no
|
||||
# raster gets checked into git.
|
||||
build = "build.rs"
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
//! artifact.
|
||||
//!
|
||||
//! Uses `rsvg-convert` from PATH (librsvg, already available in
|
||||
//! nixpkgs and added to the naersk derivation's `nativeBuildInputs`
|
||||
//! nixpkgs and added to the crane derivation's `nativeBuildInputs`
|
||||
//! in `flake.nix`). For dev builds outside Nix, install librsvg via
|
||||
//! your system package manager (Debian/Ubuntu: `librsvg2-bin`,
|
||||
//! macOS: `brew install librsvg`).
|
||||
|
|
@ -43,7 +43,7 @@ fn main() {
|
|||
"failed to invoke rsvg-convert: {e}\n\
|
||||
install librsvg (Debian/Ubuntu: librsvg2-bin, macOS: brew install librsvg, \
|
||||
NixOS: pkgs.librsvg). The Nix derivation already pulls it in via \
|
||||
flake.nix → naersk-lib.buildPackage.nativeBuildInputs.",
|
||||
flake.nix → craneLib.buildPackage.nativeBuildInputs.",
|
||||
),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -151,21 +151,18 @@ async fn run_approval_schedule_prompt(
|
|||
approval: hive_sh4re::Approval,
|
||||
) -> Result<()> {
|
||||
let result: Result<()> = async {
|
||||
let payload: hive_sh4re::SchedulePromptPayload =
|
||||
serde_json::from_str(&approval.commit_ref)
|
||||
.context("decode SchedulePromptPayload from approval.commit_ref")?;
|
||||
let payload: hive_sh4re::SchedulePromptPayload = serde_json::from_str(&approval.commit_ref)
|
||||
.context("decode SchedulePromptPayload from approval.commit_ref")?;
|
||||
coord
|
||||
.scheduled_prompts
|
||||
.submit(crate::scheduled_prompts::NewSchedule {
|
||||
.submit(&crate::scheduled_prompts::NewSchedule {
|
||||
owner: approval.agent.clone(),
|
||||
targets: payload.targets,
|
||||
body: payload.body,
|
||||
first_fire_at_unix: payload.first_fire_at_unix,
|
||||
interval_seconds: payload.interval_seconds,
|
||||
description: payload.description,
|
||||
source: crate::scheduled_prompts::ScheduleSource::Approval {
|
||||
id: approval.id,
|
||||
},
|
||||
source: crate::scheduled_prompts::ScheduleSource::Approval { id: approval.id },
|
||||
})
|
||||
.map(|_| ())
|
||||
.context("insert scheduled prompt")
|
||||
|
|
@ -290,9 +287,10 @@ async fn forge_after_first_spawn(coord: &Arc<Coordinator>, agent: &str) {
|
|||
tracing::warn!(%agent, error = ?e, "forge: ensure_config_repo after first spawn failed");
|
||||
}
|
||||
if let Some(core_token) = crate::forge::core_token()
|
||||
&& let Err(e) = crate::forge::meta_read_access(agent, &core_token).await {
|
||||
tracing::warn!(%agent, error = ?e, "forge: meta_read_access after first spawn failed");
|
||||
}
|
||||
&& let Err(e) = crate::forge::meta_read_access(agent, &core_token).await
|
||||
{
|
||||
tracing::warn!(%agent, error = ?e, "forge: meta_read_access after first spawn failed");
|
||||
}
|
||||
if let Err(e) = crate::forge::ensure_meta_remote(agent).await {
|
||||
tracing::warn!(%agent, error = ?e, "forge: ensure_meta_remote after first spawn failed");
|
||||
}
|
||||
|
|
@ -466,7 +464,7 @@ async fn run_apply_commit(
|
|||
Err(anyhow::anyhow!("read applied/main: {e:#}")),
|
||||
None,
|
||||
is_first_spawn,
|
||||
)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -521,8 +519,7 @@ async fn run_apply_commit(
|
|||
Ok(a) => a,
|
||||
Err(e) => {
|
||||
let _ =
|
||||
lifecycle::git_update_ref(applied_dir, "refs/heads/main", &prev_main_sha)
|
||||
.await;
|
||||
lifecycle::git_update_ref(applied_dir, "refs/heads/main", &prev_main_sha).await;
|
||||
let _ = lifecycle::git_read_tree_reset(applied_dir, "refs/heads/main").await;
|
||||
return (
|
||||
Err(anyhow::anyhow!("agents_for_meta_listing_with: {e:#}")),
|
||||
|
|
@ -540,8 +537,7 @@ async fn run_apply_commit(
|
|||
)
|
||||
.await
|
||||
{
|
||||
let _ =
|
||||
lifecycle::git_update_ref(applied_dir, "refs/heads/main", &prev_main_sha).await;
|
||||
let _ = lifecycle::git_update_ref(applied_dir, "refs/heads/main", &prev_main_sha).await;
|
||||
let _ = lifecycle::git_read_tree_reset(applied_dir, "refs/heads/main").await;
|
||||
return (
|
||||
Err(anyhow::anyhow!("meta sync_agents for first spawn: {e:#}")),
|
||||
|
|
|
|||
|
|
@ -251,9 +251,11 @@ impl Approvals {
|
|||
/// kind / agent / sha. Errors if the approval isn't pending — once
|
||||
/// it's approved/denied/failed/cancelled, the resolution is final.
|
||||
pub fn mark_cancelled(&self, id: i64, canceller: &str) -> Result<Approval> {
|
||||
let mut conn = self.conn.lock().unwrap();
|
||||
let tx = conn.transaction()?;
|
||||
let row: Option<(
|
||||
// Row-shape alias for the SELECT below so we don't trip
|
||||
// clippy::type_complexity. Order matches the SELECT projection:
|
||||
// agent, kind, commit_ref, requested_at, status, fetched_sha,
|
||||
// description.
|
||||
type CancelLookupRow = (
|
||||
String,
|
||||
String,
|
||||
String,
|
||||
|
|
@ -261,7 +263,10 @@ impl Approvals {
|
|||
String,
|
||||
Option<String>,
|
||||
Option<String>,
|
||||
)> = tx
|
||||
);
|
||||
let mut conn = self.conn.lock().unwrap();
|
||||
let tx = conn.transaction()?;
|
||||
let row: Option<CancelLookupRow> = tx
|
||||
.query_row(
|
||||
"SELECT agent, kind, commit_ref, requested_at, status, fetched_sha, description
|
||||
FROM approvals WHERE id = ?1",
|
||||
|
|
@ -326,9 +331,7 @@ impl Approvals {
|
|||
/// bad row used to make `pending()` / `recent_resolved()` error out
|
||||
/// wholesale — the dashboard then rendered an empty approvals queue
|
||||
/// (issue #160, an unhandled `init_config` kind poisoning every read).
|
||||
fn collect_lenient(
|
||||
rows: impl Iterator<Item = rusqlite::Result<Approval>>,
|
||||
) -> Vec<Approval> {
|
||||
fn collect_lenient(rows: impl Iterator<Item = rusqlite::Result<Approval>>) -> Vec<Approval> {
|
||||
rows.filter_map(|r| match r {
|
||||
Ok(a) => Some(a),
|
||||
Err(e) => {
|
||||
|
|
@ -467,7 +470,12 @@ mod tests {
|
|||
// status + a "cancelled by <who>" note.
|
||||
let (_dir, _path, db) = open_temp();
|
||||
let id = db
|
||||
.submit_kind("bitburner", ApprovalKind::ApplyCommit, "cafef00d", Some("test"))
|
||||
.submit_kind(
|
||||
"bitburner",
|
||||
ApprovalKind::ApplyCommit,
|
||||
"cafef00d",
|
||||
Some("test"),
|
||||
)
|
||||
.unwrap();
|
||||
let row = db.mark_cancelled(id, "manager").expect("cancel");
|
||||
assert_eq!(row.id, id);
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ pub fn agent_config_pending(name: &str, deployed_sha: Option<&str>) -> bool {
|
|||
/// can't diverge.
|
||||
///
|
||||
/// `queue_entry_id` is `Some(id)` when the rebuild was dispatched from
|
||||
/// the rebuild_queue worker (lets the function annotate its phase via
|
||||
/// the `rebuild_queue` worker (lets the function annotate its phase via
|
||||
/// `coord.set_queue_step`) and `None` when called directly (e.g. the
|
||||
/// manager-migration nudge in `ensure_manager`).
|
||||
pub async fn rebuild_agent(
|
||||
|
|
@ -220,7 +220,10 @@ pub async fn run(coord: Arc<Coordinator>) -> Result<()> {
|
|||
|
||||
let _current_rev = current_flake_rev(&coord.hyperhive_flake).unwrap_or_default();
|
||||
|
||||
tracing::info!(agents = containers.len(), "auto-update: queueing all on startup");
|
||||
tracing::info!(
|
||||
agents = containers.len(),
|
||||
"auto-update: queueing all on startup"
|
||||
);
|
||||
for container in containers {
|
||||
let logical = if container == MANAGER_NAME {
|
||||
Some(MANAGER_NAME.to_owned())
|
||||
|
|
|
|||
|
|
@ -139,6 +139,46 @@ fn is_deliberate_stop(
|
|||
active.is_some_and(is_op_kind) || recently_cleared.is_some_and(is_op_kind)
|
||||
}
|
||||
|
||||
fn emit_login_transitions(
|
||||
coord: &Coordinator,
|
||||
prev: &HashSet<String>,
|
||||
current: &HashSet<String>,
|
||||
sub_agents: &[String],
|
||||
prev_sub_agents: &HashSet<String>,
|
||||
) {
|
||||
for agent in current.difference(prev) {
|
||||
tracing::info!(%agent, "agent logged in");
|
||||
coord.notify_manager(&hive_sh4re::HelperEvent::LoggedIn {
|
||||
agent: agent.clone(),
|
||||
});
|
||||
}
|
||||
// Detect transitions into "needs login": an agent that was previously
|
||||
// logged-in goes unsigned (credentials deleted), OR a brand-new agent
|
||||
// appears without a session.
|
||||
//
|
||||
// prev_needs uses prev_sub_agents (the agent set from the last tick) so
|
||||
// that a newly-spawned agent — which does not appear in prev_sub_agents —
|
||||
// is absent from prev_needs even though it's not in prev_logged_in.
|
||||
// Without this, new agents land in both prev_needs and current_needs and
|
||||
// the set difference is empty, silently dropping the event.
|
||||
let prev_needs: HashSet<&str> = prev_sub_agents
|
||||
.iter()
|
||||
.map(String::as_str)
|
||||
.filter(|n| !prev.contains(*n))
|
||||
.collect();
|
||||
let current_needs: HashSet<&str> = sub_agents
|
||||
.iter()
|
||||
.map(String::as_str)
|
||||
.filter(|n| !current.contains(*n))
|
||||
.collect();
|
||||
for agent in current_needs.difference(&prev_needs) {
|
||||
tracing::info!(%agent, "agent needs login");
|
||||
coord.notify_manager(&hive_sh4re::HelperEvent::NeedsLogin {
|
||||
agent: (*agent).to_owned(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
@ -186,44 +226,3 @@ mod tests {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn emit_login_transitions(
|
||||
coord: &Coordinator,
|
||||
prev: &HashSet<String>,
|
||||
current: &HashSet<String>,
|
||||
sub_agents: &[String],
|
||||
prev_sub_agents: &HashSet<String>,
|
||||
) {
|
||||
for agent in current.difference(prev) {
|
||||
tracing::info!(%agent, "agent logged in");
|
||||
coord.notify_manager(&hive_sh4re::HelperEvent::LoggedIn {
|
||||
agent: agent.clone(),
|
||||
});
|
||||
}
|
||||
// Detect transitions into "needs login": an agent that was previously
|
||||
// logged-in goes unsigned (credentials deleted), OR a brand-new agent
|
||||
// appears without a session.
|
||||
//
|
||||
// prev_needs uses prev_sub_agents (the agent set from the last tick) so
|
||||
// that a newly-spawned agent — which does not appear in prev_sub_agents —
|
||||
// is absent from prev_needs even though it's not in prev_logged_in.
|
||||
// Without this, new agents land in both prev_needs and current_needs and
|
||||
// the set difference is empty, silently dropping the event.
|
||||
let prev_needs: HashSet<&str> = prev_sub_agents
|
||||
.iter()
|
||||
.map(String::as_str)
|
||||
.filter(|n| !prev.contains(*n))
|
||||
.collect();
|
||||
let current_needs: HashSet<&str> = sub_agents
|
||||
.iter()
|
||||
.map(String::as_str)
|
||||
.filter(|n| !current.contains(*n))
|
||||
.collect();
|
||||
for agent in current_needs.difference(&prev_needs) {
|
||||
tracing::info!(%agent, "agent needs login");
|
||||
coord.notify_manager(&hive_sh4re::HelperEvent::NeedsLogin {
|
||||
agent: (*agent).to_owned(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -76,13 +76,13 @@ pub async fn serve(port: u16, coord: Arc<Coordinator>) -> Result<()> {
|
|||
.route("/op-send", post(post_op_send))
|
||||
.route("/meta-update", post(post_meta_update))
|
||||
.route("/api/schedules", get(api_schedules).post(post_schedule_new))
|
||||
.route(
|
||||
"/api/schedules/{id}",
|
||||
axum::routing::patch(patch_schedule),
|
||||
)
|
||||
.route("/api/schedules/{id}", axum::routing::patch(patch_schedule))
|
||||
.route("/api/schedules/{id}/cancel", post(post_schedule_cancel))
|
||||
.route("/api/schedules/{id}/fire-now", post(post_schedule_fire_now))
|
||||
.route("/api/rebuild-queue/{id}/cancel", post(post_rebuild_queue_cancel))
|
||||
.route(
|
||||
"/api/rebuild-queue/{id}/cancel",
|
||||
post(post_rebuild_queue_cancel),
|
||||
)
|
||||
.route("/dashboard/stream", get(dashboard_stream))
|
||||
.route("/dashboard/history", get(dashboard_history))
|
||||
// Anything not matched by the dynamic routes above falls
|
||||
|
|
@ -765,7 +765,14 @@ async fn dashboard_history(State(state): State<AppState>) -> Response {
|
|||
let events: Vec<crate::dashboard_events::DashboardEvent> = messages
|
||||
.into_iter()
|
||||
.map(|m| match m {
|
||||
crate::broker::MessageEvent::Sent { id, from, to, body, at, in_reply_to } => {
|
||||
crate::broker::MessageEvent::Sent {
|
||||
id,
|
||||
from,
|
||||
to,
|
||||
body,
|
||||
at,
|
||||
in_reply_to,
|
||||
} => {
|
||||
let file_refs = scan_validated_paths(&body);
|
||||
crate::dashboard_events::DashboardEvent::Sent {
|
||||
seq: 0,
|
||||
|
|
@ -778,7 +785,14 @@ async fn dashboard_history(State(state): State<AppState>) -> Response {
|
|||
file_refs,
|
||||
}
|
||||
}
|
||||
crate::broker::MessageEvent::Delivered { id, from, to, body, at, in_reply_to } => {
|
||||
crate::broker::MessageEvent::Delivered {
|
||||
id,
|
||||
from,
|
||||
to,
|
||||
body,
|
||||
at,
|
||||
in_reply_to,
|
||||
} => {
|
||||
let file_refs = scan_validated_paths(&body);
|
||||
crate::dashboard_events::DashboardEvent::Delivered {
|
||||
seq: 0,
|
||||
|
|
@ -1155,8 +1169,8 @@ fn resolve_state_path(
|
|||
return Err(format!("path not in allow-list: {raw}"));
|
||||
};
|
||||
reject_symlinks_below(std::path::Path::new(root), &mapped)?;
|
||||
let canonical = std::fs::canonicalize(&mapped)
|
||||
.map_err(|e| format!("{}: {e}", mapped.display()))?;
|
||||
let canonical =
|
||||
std::fs::canonicalize(&mapped).map_err(|e| format!("{}: {e}", mapped.display()))?;
|
||||
if !(canonical.starts_with(AGENTS_ROOT) || canonical.starts_with(SHARED_ROOT)) {
|
||||
return Err(format!(
|
||||
"resolved path escapes allow-list: {}",
|
||||
|
|
@ -1174,8 +1188,8 @@ fn resolve_state_path(
|
|||
));
|
||||
}
|
||||
}
|
||||
let meta = std::fs::metadata(&canonical)
|
||||
.map_err(|e| format!("stat {}: {e}", canonical.display()))?;
|
||||
let meta =
|
||||
std::fs::metadata(&canonical).map_err(|e| format!("stat {}: {e}", canonical.display()))?;
|
||||
if meta.is_file() {
|
||||
let mode = meta.permissions().mode();
|
||||
if mode & 0o004 == 0 {
|
||||
|
|
@ -1313,12 +1327,10 @@ pub(crate) async fn emit_tombstones_snapshot(coord: &Arc<Coordinator>) {
|
|||
let containers = coord.containers_snapshot().await;
|
||||
let transient_snapshot = coord.transient_snapshot();
|
||||
let tombstones = build_tombstone_views(coord, &containers, &transient_snapshot);
|
||||
coord.emit_dashboard_event(
|
||||
crate::dashboard_events::DashboardEvent::TombstonesChanged {
|
||||
seq: coord.next_seq(),
|
||||
tombstones,
|
||||
},
|
||||
);
|
||||
coord.emit_dashboard_event(crate::dashboard_events::DashboardEvent::TombstonesChanged {
|
||||
seq: coord.next_seq(),
|
||||
tombstones,
|
||||
});
|
||||
}
|
||||
|
||||
/// Snapshot meta/flake.lock's root inputs + emit
|
||||
|
|
@ -1326,12 +1338,10 @@ pub(crate) async fn emit_tombstones_snapshot(coord: &Arc<Coordinator>) {
|
|||
/// (`run_meta_update`, `auto_update::rebuild_agent`).
|
||||
pub(crate) fn emit_meta_inputs_snapshot(coord: &Coordinator) {
|
||||
let inputs = read_meta_inputs();
|
||||
coord.emit_dashboard_event(
|
||||
crate::dashboard_events::DashboardEvent::MetaInputsChanged {
|
||||
seq: coord.next_seq(),
|
||||
inputs,
|
||||
},
|
||||
);
|
||||
coord.emit_dashboard_event(crate::dashboard_events::DashboardEvent::MetaInputsChanged {
|
||||
seq: coord.next_seq(),
|
||||
inputs,
|
||||
});
|
||||
}
|
||||
|
||||
/// Scan `body` for path-shaped tokens, validate each against the
|
||||
|
|
@ -1381,9 +1391,7 @@ pub(crate) fn scan_validated_paths(body: &str) -> Vec<String> {
|
|||
out
|
||||
}
|
||||
|
||||
async fn get_state_file(
|
||||
axum::extract::Query(q): axum::extract::Query<StateFileQuery>,
|
||||
) -> Response {
|
||||
async fn get_state_file(axum::extract::Query(q): axum::extract::Query<StateFileQuery>) -> Response {
|
||||
const MAX_BYTES: usize = 1 << 20; // 1 MiB
|
||||
let (canonical, meta) = match resolve_state_path(&q.path) {
|
||||
Ok(pair) => pair,
|
||||
|
|
@ -1415,11 +1423,18 @@ async fn get_state_file(
|
|||
return ([("content-type", ct)], bytes).into_response();
|
||||
}
|
||||
let truncated = bytes.len() > MAX_BYTES;
|
||||
let body_bytes = if truncated { &bytes[..MAX_BYTES] } else { &bytes[..] };
|
||||
let body_bytes = if truncated {
|
||||
&bytes[..MAX_BYTES]
|
||||
} else {
|
||||
&bytes[..]
|
||||
};
|
||||
let mut body = String::from_utf8_lossy(body_bytes).into_owned();
|
||||
if truncated {
|
||||
use std::fmt::Write as _;
|
||||
let _ = write!(body, "\n\n--- truncated at {MAX_BYTES} of {size} bytes ---\n");
|
||||
let _ = write!(
|
||||
body,
|
||||
"\n\n--- truncated at {MAX_BYTES} of {size} bytes ---\n"
|
||||
);
|
||||
}
|
||||
([("content-type", "text/plain; charset=utf-8")], body).into_response()
|
||||
}
|
||||
|
|
@ -1492,7 +1507,7 @@ async fn post_schedule_new(
|
|||
description: payload.description,
|
||||
source: crate::scheduled_prompts::ScheduleSource::Operator,
|
||||
};
|
||||
match state.coord.scheduled_prompts.submit(new) {
|
||||
match state.coord.scheduled_prompts.submit(&new) {
|
||||
Ok(id) => axum::Json(serde_json::json!({"id": id})).into_response(),
|
||||
Err(e) => error_response(&format!("schedule submit: {e:#}")),
|
||||
}
|
||||
|
|
@ -1546,6 +1561,12 @@ struct CancelScheduleForm {
|
|||
}
|
||||
|
||||
#[derive(serde::Deserialize, Default)]
|
||||
#[allow(
|
||||
clippy::option_option,
|
||||
reason = "double-Option carries three-state PATCH semantics on the wire \
|
||||
(missing key = leave alone, JSON null = clear, value = set); \
|
||||
collapsing to a single Option would lose the 'clear' state"
|
||||
)]
|
||||
struct EditScheduleForm {
|
||||
#[serde(default)]
|
||||
body: Option<String>,
|
||||
|
|
@ -1671,7 +1692,10 @@ async fn get_agent_links(AxumPath(name): AxumPath<String>) -> Response {
|
|||
match client.get(&url).send().await {
|
||||
Ok(resp) if resp.status().is_success() => match resp.json::<serde_json::Value>().await {
|
||||
Ok(body) => {
|
||||
let links = body.get("links").cloned().unwrap_or_else(|| serde_json::json!([]));
|
||||
let links = body
|
||||
.get("links")
|
||||
.cloned()
|
||||
.unwrap_or_else(|| serde_json::json!([]));
|
||||
axum::Json(links).into_response()
|
||||
}
|
||||
Err(e) => {
|
||||
|
|
@ -1852,7 +1876,10 @@ async fn post_op_send(State(state): State<AppState>, Form(form): Form<OpSendForm
|
|||
.coord
|
||||
.broadcast_send(hive_sh4re::OPERATOR_RECIPIENT, &body);
|
||||
if !errors.is_empty() {
|
||||
return error_response(&format!("op-send broadcast partial fail: {}", errors.join("; ")));
|
||||
return error_response(&format!(
|
||||
"op-send broadcast partial fail: {}",
|
||||
errors.join("; ")
|
||||
));
|
||||
}
|
||||
} else if let Err(e) = state.coord.broker.send(&hive_sh4re::Message {
|
||||
from: hive_sh4re::OPERATOR_RECIPIENT.to_owned(),
|
||||
|
|
@ -2151,7 +2178,6 @@ fn gc_orphans(coord: &Coordinator, approvals: Vec<Approval>) -> Vec<Approval> {
|
|||
.collect()
|
||||
}
|
||||
|
||||
|
||||
/// Multi-file unified diff between the currently-deployed tree and
|
||||
/// the proposal for this approval. Runs against the applied repo
|
||||
/// since the canonical proposal commit lives there (manager-side
|
||||
|
|
@ -2282,4 +2308,3 @@ async fn get_approval_diff(
|
|||
fn plain_text(body: String) -> Response {
|
||||
(StatusCode::OK, body).into_response()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -165,10 +165,7 @@ pub enum DashboardEvent {
|
|||
/// last one cached on the coordinator. Mutation sites (lifecycle
|
||||
/// endpoints, `actions::destroy` / approve, `crash_watch`'s poll loop)
|
||||
/// call the rescan after their work lands.
|
||||
ContainerStateChanged {
|
||||
seq: u64,
|
||||
container: ContainerView,
|
||||
},
|
||||
ContainerStateChanged { seq: u64, container: ContainerView },
|
||||
/// A container that was in the previous snapshot is gone. Clients
|
||||
/// drop the row by name. Fired alongside any
|
||||
/// `nixos-container destroy` (operator-driven or otherwise) on the
|
||||
|
|
@ -211,12 +208,9 @@ pub enum DashboardEvent {
|
|||
/// snapshot-shape rationale as `TombstonesChanged` /
|
||||
/// `MetaInputsChanged`: the list is small, snapshot semantics avoid
|
||||
/// the add/remove races a per-row event would have, and the
|
||||
/// dashboard's grouping (parent_id) is most naturally re-derived
|
||||
/// dashboard's grouping (`parent_id`) is most naturally re-derived
|
||||
/// from the full list.
|
||||
RebuildQueueChanged {
|
||||
seq: u64,
|
||||
queue: Vec<QueueEntry>,
|
||||
},
|
||||
RebuildQueueChanged { seq: u64, queue: Vec<QueueEntry> },
|
||||
}
|
||||
|
||||
impl DashboardEvent {
|
||||
|
|
@ -259,13 +253,18 @@ mod tests {
|
|||
/// the `kind` JSON field matches `kind_tag()`. The exhaustive
|
||||
/// `match` in `kind_tag` already provides compile-time variant
|
||||
/// coverage — this test is the value-side guard against
|
||||
/// typos in the snake_case strings vs serde's `rename_all`
|
||||
/// typos in the `snake_case` strings vs serde's `rename_all`
|
||||
/// output. `ContainerStateChanged` is omitted from the sample
|
||||
/// list only because `ContainerView` has no `Default` impl and
|
||||
/// constructing one inline here is more boilerplate than the
|
||||
/// test is worth; the variant is still covered by the
|
||||
/// `kind_tag` match arm.
|
||||
#[test]
|
||||
#[allow(
|
||||
clippy::too_many_lines,
|
||||
reason = "exhaustive coverage of every DashboardEvent variant — the \
|
||||
length is the point"
|
||||
)]
|
||||
fn kind_tag_matches_serde_kind_field() {
|
||||
let samples: Vec<DashboardEvent> = vec![
|
||||
DashboardEvent::Sent {
|
||||
|
|
@ -367,11 +366,7 @@ mod tests {
|
|||
.get("kind")
|
||||
.and_then(|k| k.as_str())
|
||||
.expect("kind field present");
|
||||
assert_eq!(
|
||||
ev.kind_tag(),
|
||||
serde_kind,
|
||||
"kind_tag() drift on {ev:?}",
|
||||
);
|
||||
assert_eq!(ev.kind_tag(), serde_kind, "kind_tag() drift on {ev:?}",);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue