Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions apps/web/src/j5/fleet/FleetPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ import {
type ScopedFleetLedgerProject,
} from "./fleetClient";

import { fleetSeatStatesAtom } from "./fleetState";
import { fleetAgentKey } from "./fleetPresentation";
import { fleetSeatStatesAtom, fleetProblemSummaryAtom } from "./fleetState";
import { fleetAgentKey, fleetProblemNotices } from "./fleetPresentation";
import {
FleetFactCells,
FleetFactDetails,
Expand Down Expand Up @@ -111,18 +111,20 @@ export function FleetPage() {
const threads = useThreadShells();
const sources = useAtomValue(fleetDetailSourcesAtom);
const seatStates = useAtomValue(fleetSeatStatesAtom);
const problemSummary = useAtomValue(fleetProblemSummaryAtom);
useFleetDetailRefresh();
const [refreshing, setRefreshing] = useState(false);
const projects = useMemo(() => mergeFleetSources(sources), [sources]);
const showEnvironment = spansMultipleEnvironments(projects);
const loading = !sources.isReady || sources.sources.some((source) => source.status === "loading");
const notices = [
...new Set(
sources.sources.flatMap((source) => {
...new Set([
...fleetProblemNotices(problemSummary),
...sources.sources.flatMap((source) => {
const notice = j5SourceNotice(source);
return notice === null ? [] : [notice];
}),
),
]),
];

const refresh = useCallback(async () => {
Expand Down
25 changes: 9 additions & 16 deletions apps/web/src/j5/fleet/FleetRailEntry.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
import { useAtomValue } from "@effect/atom-react";
import { Link } from "@tanstack/react-router";
import { RadarIcon } from "lucide-react";
import { useCallback, useMemo } from "react";
import { useCallback } from "react";

import { useSidebar } from "../../components/ui/sidebar";
import { cn } from "../../lib/utils";
import { fleetSourcesAtom } from "../state";
import { countFleetAlerts } from "./fleet.logic";
import { mergeFleetSources, useFleetRefresh } from "./fleetClient";
import { useFleetRefresh } from "./fleetClient";
import { fleetBadgeLabel } from "./fleetPresentation";
import { fleetProblemSummaryAtom } from "./fleetState";

/**
* Rail entry for the Roster (SB6). The badge counts agents that owe a reply across every
* connected environment, a measured fact, so hidden Background agents stay visible somewhere
* even though the sidebar no longer lists them.
* The always-mounted rail shares the page's facts and local dispatch deadline.
*/
export function FleetRailEntry({ onBackdrop }: { readonly onBackdrop: boolean }) {
const { isMobile, setOpenMobile } = useSidebar();
const sources = useAtomValue(fleetSourcesAtom);
const summary = useAtomValue(fleetProblemSummaryAtom);
useFleetRefresh();
const alerts = useMemo(() => {
const known = sources.sources.filter((source) => source.data !== null);
return known.length === 0 ? null : countFleetAlerts(mergeFleetSources(sources));
}, [sources]);

const closeMobileSidebar = useCallback(() => {
if (isMobile) setOpenMobile(false);
}, [isMobile, setOpenMobile]);

const label =
alerts === null || alerts === 0 ? "Open fleet" : `Open fleet, ${alerts} agents owe a reply`;
const label = fleetBadgeLabel(summary);
return (
<Link
aria-label={label}
Expand All @@ -45,9 +38,9 @@ export function FleetRailEntry({ onBackdrop }: { readonly onBackdrop: boolean })
to="/fleet"
>
<RadarIcon aria-hidden className="size-4" />
{alerts !== null && alerts > 0 ? (
{summary.agentsWithProblems > 0 ? (
<span className="absolute -end-1 -top-1 flex min-h-4 min-w-4 items-center justify-center rounded-full bg-primary px-1 text-3xs font-semibold leading-none text-primary-foreground tabular-nums ring-2 ring-sidebar">
{alerts}
{summary.agentsWithProblems}
</span>
) : null}
</Link>
Expand Down
4 changes: 1 addition & 3 deletions apps/web/src/j5/fleet/fleet.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from "../crew/crewState";
import {
buildFleetTree,
countFleetAlerts,
countFleetProjects,
createFleetRowRefresher,
fleetInvolvedThreadRefs,
Expand Down Expand Up @@ -88,14 +87,13 @@ describe("fleet tree", () => {
]);
});

it("never loops on a cyclic placement and counts alerts from owed asks only", () => {
it("never loops on a cyclic placement", () => {
const cyclic: FleetLedgerProject = {
...project,
agents: [agent("a", { placementParentId: "b" }), agent("b", { placementParentId: "a" })],
};
const roots = buildFleetTree(cyclic);
expect(roots.map((node) => node.row.agent.participantId).length).toBeGreaterThan(0);
expect(countFleetAlerts([project])).toBe(1);
expect(originLabel({ origin: "unknown" })).toBe("?");
});
});
Expand Down
8 changes: 0 additions & 8 deletions apps/web/src/j5/fleet/fleet.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,6 @@ export function countFleetProjects<
).size;
}

/** Roster alert badge: measured "needs a human" facts only, so nothing here is guessed. */
export const countFleetAlerts = (projects: ReadonlyArray<FleetLedgerProject>) =>
projects.reduce(
(count, ledgerProject) =>
count + ledgerProject.agents.filter((agent) => agent.openAsks > 0).length,
0,
);

/**
* Origin copy for the Roster row; unknown renders as `?` rather than a plausible guess. An
* outside agent that started the agent is named by the label it signed in with, and by its id
Expand Down
57 changes: 56 additions & 1 deletion apps/web/src/j5/fleet/fleetPresentation.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { describe, expect, it, vi } from "vite-plus/test";
import type { FleetDerivedFacts } from "@t3tools/client-runtime/j5/fleetFacts";
import {
summarizeFleetProblems,
type FleetDerivedFacts,
} from "@t3tools/client-runtime/j5/fleetFacts";
import type { FleetRuntimeFact } from "@t3tools/contracts/j5";
import {
fleetCrewSummary,
Expand All @@ -8,6 +11,8 @@ import {
fleetTime,
fleetAgo,
fleetProblemLabel,
fleetBadgeLabel,
fleetProblemNotices,
fleetSeatState,
fleetSourceIsLive,
fleetStatusLabel,
Expand All @@ -27,6 +32,56 @@ const facts = (value: FleetRuntimeFact): FleetDerivedFacts => ({
});

describe("Fleet presentation", () => {
it("qualifies zero and positive badge counts when source or agent measurements are missing", () => {
const complete = {
agentsWithProblems: 0,
unmeasuredAgents: 0,
incompleteSources: false,
hasReadableSource: true,
};
expect(fleetBadgeLabel(complete)).toBe("Open fleet, 0 agents have measured problems.");
expect(fleetProblemNotices(complete)).toEqual([]);
const unmeasured = { ...complete, unmeasuredAgents: 1 };
expect(fleetBadgeLabel(unmeasured)).toContain("1 agent unmeasured.");
expect(fleetBadgeLabel(unmeasured)).toContain("Counts include current measured problems only.");
expect(fleetBadgeLabel(unmeasured)).not.toContain("coverage is incomplete");
const partial = { ...unmeasured, agentsWithProblems: 1, incompleteSources: true };
expect(fleetBadgeLabel(partial)).toContain("1 agent has measured problems.");
expect(fleetBadgeLabel(partial)).toContain("Problem coverage is incomplete");
expect(fleetProblemNotices(partial)).toContain(
"Counts include current measured problems only.",
);
});
it("distinguishes an unavailable count from a measured zero, including an empty live source", () => {
const measured = new Map([
[
"env:agent",
{
...facts(runtime("failed")),
asks: { owed: [], awaited: [] },
problems: [{ kind: "failed-run" as const, runId: "run", at }],
},
],
]);
for (const sourceFacts of [new Map(), measured]) {
const unavailable = summarizeFleetProblems(
sourceFacts,
new Map([["env:agent", false]]),
"unavailable",
);
expect(fleetBadgeLabel(unavailable)).toContain("problem count unavailable");
expect(fleetBadgeLabel(unavailable)).not.toMatch(/\d/);
expect(fleetBadgeLabel(unavailable)).toContain("coverage is incomplete");
}
const emptyLive = summarizeFleetProblems(new Map(), new Map(), "complete");
expect(fleetBadgeLabel(emptyLive)).toBe("Open fleet, 0 agents have measured problems.");
const partialZero = summarizeFleetProblems(new Map(), new Map(), "partial");
expect(fleetBadgeLabel(partialZero)).toContain("0 agents have measured problems");
expect(fleetBadgeLabel(partialZero)).toContain("coverage is incomplete");
const restored = summarizeFleetProblems(measured, new Map([["env:agent", true]]), "partial");
expect(fleetBadgeLabel(restored)).toContain("1 agent has measured problems");
expect(fleetBadgeLabel(restored)).toContain("coverage is incomplete");
});
it("uses runtime vocabulary and makes old or unavailable measurements explicit", () => {
for (const [status, label] of [
["working", "Working"],
Expand Down
25 changes: 24 additions & 1 deletion apps/web/src/j5/fleet/fleetPresentation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FleetDerivedFacts } from "@t3tools/client-runtime/j5/fleetFacts";
import type { FleetDerivedFacts, FleetProblemSummary } from "@t3tools/client-runtime/j5/fleetFacts";
import type { J5ReadSource } from "@t3tools/client-runtime/j5/readSources";
import type { FleetProblem, FleetRuntimeFact } from "@t3tools/contracts/j5";
import type { TimestampFormat } from "@t3tools/contracts/settings";
Expand All @@ -7,6 +7,29 @@ import type { CrewSeatState } from "../crew/crewState";

export { fleetAgentKey } from "@t3tools/client-runtime/j5/fleetFacts";

export const fleetProblemNotices = (summary: FleetProblemSummary): ReadonlyArray<string> => [
...(summary.incompleteSources
? [
"Problem coverage is incomplete: some environments are unavailable or do not report Fleet facts.",
]
: []),
...(summary.unmeasuredAgents > 0
? [
`${summary.unmeasuredAgents} ${summary.unmeasuredAgents === 1 ? "agent" : "agents"} unmeasured.`,
]
: []),
...(summary.incompleteSources || summary.unmeasuredAgents > 0
? ["Counts include current measured problems only."]
: []),
];

export const fleetBadgeLabel = (summary: FleetProblemSummary) =>
`${
summary.hasReadableSource
? `Open fleet, ${summary.agentsWithProblems} ${summary.agentsWithProblems === 1 ? "agent has" : "agents have"} measured problems.`
: "Open fleet, problem count unavailable."
} ${fleetProblemNotices(summary).join(" ")}`.trim();

export const fleetSourceIsLive = (
source: Pick<J5ReadSource<unknown>, "connected" | "status">,
shellStatus: string,
Expand Down
24 changes: 23 additions & 1 deletion apps/web/src/j5/fleet/fleetState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ vi.mock("../../state/threads", async () => {
});

import { fleetAgentKey } from "./fleetPresentation";
import { fleetFactsAtom } from "./fleetState";
import { fleetFactsAtom, fleetProblemSummaryAtom } from "./fleetState";

const at = "2026-10-26T10:00:00.000Z";
const origin = EnvironmentId.make("origin");
Expand Down Expand Up @@ -169,3 +169,25 @@ it("qualifies old receiver reads and creates no phantom sender row", () => {
registry.set(input.sources, sources({ includeOrigin: false }));
expect(registry.get(fleetFactsAtom).size).toBe(0);
});

it("updates badge eligibility and coverage through closure, disconnection, old reads and recovery", () => {
const stopBadge = registry.mount(fleetProblemSummaryAtom);
try {
const badge = () => registry.get(fleetProblemSummaryAtom);
expect(badge()).toMatchObject({ agentsWithProblems: 1, incompleteSources: false });
registry.set(input.sources, sources({ open: false }));
expect(badge()).toMatchObject({ agentsWithProblems: 0, incompleteSources: false });
registry.set(input.sources, sources({ mirrorOpen: false }));
expect(badge()).toMatchObject({ agentsWithProblems: 0, incompleteSources: false });
registry.set(input.sources, sources({ connected: false }));
expect(badge()).toMatchObject({ agentsWithProblems: 0, incompleteSources: true });
registry.set(input.sources, sources({ oldReceiver: true }));
expect(badge()).toMatchObject({ agentsWithProblems: 0, incompleteSources: true });
registry.set(input.sources, sources());
expect(badge()).toMatchObject({ agentsWithProblems: 1, incompleteSources: false });
registry.set(input.sources, sources({ includeOrigin: false }));
expect(badge().agentsWithProblems).toBe(0);
} finally {
stopBadge();
}
});
28 changes: 28 additions & 0 deletions apps/web/src/j5/fleet/fleetState.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
createFleetFactsAtom,
summarizeFleetProblems,
mergeFleetDeliveryFacts,
type FleetFactRow,
} from "@t3tools/client-runtime/j5/fleetFacts";
Expand Down Expand Up @@ -74,6 +75,33 @@ export const fleetLiveAgentAtom = Atom.family((key: string) =>
Atom.make((get) => get(fleetLiveRowsAtom).get(key) === true),
);

export const fleetProblemSummaryAtom = Atom.make((get) => {
const sources = get(fleetSourcesAtom);
const environments = get(fleetEnvironmentLivenessAtom);
const readable = sources.sources.filter(
(source) => environments.get(source.environmentId) === true,
);
const summary = summarizeFleetProblems(
get(fleetFactsAtom),
get(fleetLiveRowsAtom),
readable.length === 0
? "unavailable"
: sources.isReady &&
readable.length === sources.sources.length &&
readable.every((source) => source.data?.asOf !== undefined)
? "complete"
: "partial",
);
const previous = Option.getOrUndefined(get.self<typeof summary>());
return previous !== undefined &&
previous.agentsWithProblems === summary.agentsWithProblems &&
previous.unmeasuredAgents === summary.unmeasuredAgents &&
previous.incompleteSources === summary.incompleteSources &&
previous.hasReadableSource === summary.hasReadableSource
? previous
: summary;
});

/** Only classification changes rebuild the placement sections; activity and elapsed time do not. */
export const fleetSeatStatesAtom = Atom.make((get) => {
const live = get(fleetLiveRowsAtom);
Expand Down
Loading
Loading