mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-17 21:50:53 +08:00
Compare commits
14
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4a19e10af6 | ||
|
|
d0ad632d69 | ||
|
|
167b9f0c3f | ||
|
|
7f593c60b5 | ||
|
|
8aabec4e51 | ||
|
|
57ff63125b | ||
|
|
b9e564eaaa | ||
|
|
0dadc0d19f | ||
|
|
def23f6b38 | ||
|
|
2edf93a0cd | ||
|
|
701a1c92ba | ||
|
|
c4eb079a95 | ||
|
|
212e29cd15 | ||
|
|
ec98975bf3 |
@@ -0,0 +1 @@
|
||||
Xiaobo <269351206+liuxiaobo0920@users.noreply.github.com> <mvOfosterboydP1FD80@technologist.com>
|
||||
@@ -32,6 +32,9 @@ All notable changes to this project are documented in this file. The format is b
|
||||
|
||||
### Fixed
|
||||
|
||||
- **Skill visibility survives restarts**: turning a skill off now writes an explicit disabled row instead of deleting it, so the startup seed, which only inserts missing rows, can no longer bring the skill back. Skills that were turned off before this version have no row to keep, so they come back once after the upgrade and must be turned off again.
|
||||
- **Browser extension traffic capture**: CDP delivers `Network.responseReceived` response headers as an object, not an array of `{name, value}` records, so the extension threw on every response and never queued an exchange. It now accepts both shapes and lower-cases names before the body-capture gate, so captures work again and `content-type` correctly controls body capture.
|
||||
- **bbot on arm64**: the image now builds on arm64 hosts such as Apple Silicon. bbot's `gowitness` downloaded an x86-64 Chrome and `retirejs` an x86-64 Node, which can't run there, so the build failed. On arm64 the image uses Debian's `chromium` for gowitness and an arm64 Node for retirejs, and the build check now covers `retirejs` too. amd64 builds are unchanged.
|
||||
- **Auto-scope hygiene**: auto `task_scope` rows count for coverage and untested assets only under a declared root, and direct coverage counts anchored assets through fact anchors only; the scope is described as "coverage scope, not enforced" (#196).
|
||||
- **Enrich HTTP probe**: it now dials through `intercept.PinnedDialer`, so the target gate applies and a probe without a target scope is refused (#198).
|
||||
- **Agent hook data races**: the eight package-level hooks are synchronised, so a second wire-up no longer races tasks that already read them (#227).
|
||||
|
||||
+13
-2
@@ -145,14 +145,25 @@ RUN set -eux; \
|
||||
# preinstalled (--install-all-deps) so scans never need pip at runtime, which
|
||||
# would fail under a program's egress allowlist.
|
||||
ARG BBOT_VERSION=3.0.2
|
||||
# arm64: bbot's Chrome and retirejs's Node are x86-64; use native builds.
|
||||
RUN set -eux; \
|
||||
python -m venv /opt/bbot; \
|
||||
/opt/bbot/bin/pip install --no-cache-dir "bbot==${BBOT_VERSION}"; \
|
||||
ln -s /opt/bbot/bin/bbot /usr/local/bin/bbot; \
|
||||
if [ "${TARGETARCH:-amd64}" = "arm64" ]; then \
|
||||
apt-get update; \
|
||||
apt-get install -y --no-install-recommends chromium; \
|
||||
rm -rf /var/lib/apt/lists/*; \
|
||||
bbot_dir="$(/opt/bbot/bin/python -c 'import bbot, os; print(os.path.dirname(bbot.__file__))')"; \
|
||||
sed -i 's/linux-x64/linux-arm64/g' "$bbot_dir/modules/retirejs.py"; \
|
||||
fi; \
|
||||
bbot -y --install-all-deps; \
|
||||
if [ "${TARGETARCH:-amd64}" = "arm64" ]; then \
|
||||
printf '\nmodules:\n gowitness:\n chrome_path: /usr/bin/chromium\n' >> /root/.config/bbot/bbot.yml; \
|
||||
fi; \
|
||||
bbot --version >/dev/null; \
|
||||
bbot --dry-run -t example.com -m wafw00f,paramminer_getparams,lightfuzz,sslcert,gowitness >/dev/null 2>&1 || true; \
|
||||
bbot --dry-run -t example.com -m wafw00f,paramminer_getparams,lightfuzz,sslcert,gowitness --no-color >/tmp/bbot-dry.log 2>&1 || true; \
|
||||
bbot --dry-run -t example.com -m wafw00f,paramminer_getparams,lightfuzz,sslcert,gowitness,retirejs >/dev/null 2>&1 || true; \
|
||||
bbot --dry-run -t example.com -m wafw00f,paramminer_getparams,lightfuzz,sslcert,gowitness,retirejs --no-color >/tmp/bbot-dry.log 2>&1 || true; \
|
||||
! grep -qE "Installing|ERRR|CRITICAL" /tmp/bbot-dry.log || { cat /tmp/bbot-dry.log; exit 1; }
|
||||
|
||||
|
||||
|
||||
+15
-2
@@ -1064,13 +1064,20 @@ func (d *DB) SkillAgents(skillName string) ([]int64, error) {
|
||||
}
|
||||
|
||||
// SetAgentSkillVisibility replaces all skill visibility for an agent.
|
||||
//
|
||||
// Rows are disabled rather than deleted. Every read path filters on `enabled`, so
|
||||
// the visible set is unchanged, but the row survives as the record of an explicit
|
||||
// decision: seedBuiltinSkillVisibility is insert-if-absent, so a deleted row would
|
||||
// read as "never seeded" on the next start and the builtin skill would come back.
|
||||
// DeleteSkillVisibility is the one place that still deletes, because there the
|
||||
// skill itself is gone from disk.
|
||||
func (d *DB) SetAgentSkillVisibility(agentID int64, names []string) error {
|
||||
tx, err := d.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if _, err := tx.Exec(`DELETE FROM agent_skill_visibility WHERE agent_id=$1`, agentID); err != nil {
|
||||
if _, err := tx.Exec(`UPDATE agent_skill_visibility SET enabled=false WHERE agent_id=$1`, agentID); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, name := range names {
|
||||
@@ -1083,13 +1090,19 @@ ON CONFLICT (agent_id,skill_name) DO UPDATE SET enabled=true`, agentID, name); e
|
||||
}
|
||||
|
||||
// ToggleSkillVisibility sets one (agent, skill_name) visibility on/off.
|
||||
//
|
||||
// Turning a skill off writes enabled=false instead of removing the row, including
|
||||
// when no row exists yet: "explicitly off" and "never seeded" must not look the
|
||||
// same to an insert-if-absent seed, or the builtin skill is resurrected on the
|
||||
// next start.
|
||||
func (d *DB) ToggleSkillVisibility(agentID int64, skillName string, on bool) error {
|
||||
if on {
|
||||
_, err := d.Exec(`INSERT INTO agent_skill_visibility(agent_id,skill_name,enabled) VALUES ($1,$2,true)
|
||||
ON CONFLICT (agent_id,skill_name) DO UPDATE SET enabled=true`, agentID, skillName)
|
||||
return err
|
||||
}
|
||||
_, err := d.Exec(`DELETE FROM agent_skill_visibility WHERE agent_id=$1 AND skill_name=$2`, agentID, skillName)
|
||||
_, err := d.Exec(`INSERT INTO agent_skill_visibility(agent_id,skill_name,enabled) VALUES ($1,$2,false)
|
||||
ON CONFLICT (agent_id,skill_name) DO UPDATE SET enabled=false`, agentID, skillName)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -391,6 +391,12 @@ var builtinSkillVisibility = map[string][]string{
|
||||
// seedBuiltinSkillVisibility binds the shipped built-in skills to their default
|
||||
// agents. Insert-if-absent (ON CONFLICT DO NOTHING) so a user's later toggle-off is
|
||||
// never resurrected on restart — matches the browser-MCP / intercept-rule seed policy.
|
||||
//
|
||||
// This only holds while toggle-off keeps the row: ToggleSkillVisibility and
|
||||
// SetAgentSkillVisibility write enabled=false rather than DELETE, so "explicitly
|
||||
// off" is distinguishable from "never seeded". Deleting a skill is the one path
|
||||
// that removes rows (DeleteSkillVisibility), which is what lets a re-installed
|
||||
// skill be seeded again.
|
||||
func (d *DB) seedBuiltinSkillVisibility() error {
|
||||
for skillName, agentKeys := range builtinSkillVisibility {
|
||||
for _, key := range agentKeys {
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// skillRowState reports whether an (agent, skill) row exists and whether it is
|
||||
// enabled. The distinction is the whole point of this file: seedBuiltinSkillVisibility
|
||||
// is insert-if-absent, so "never seeded" and "explicitly turned off" must be
|
||||
// different states. If turning a skill off removes the row, the next start sees
|
||||
// "never seeded" and resurrects it.
|
||||
func skillRowState(t *testing.T, d *DB, agentID int64, skill string) (exists, enabled bool) {
|
||||
t.Helper()
|
||||
err := d.QueryRow(`SELECT enabled FROM agent_skill_visibility WHERE agent_id=$1 AND skill_name=$2`, agentID, skill).Scan(&enabled)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return false, false
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("read skill visibility row (%d, %s): %v", agentID, skill, err)
|
||||
}
|
||||
return true, enabled
|
||||
}
|
||||
|
||||
// TestSkillVisibilityOffIsNotResurrectedBySeed pins the behaviour the seed policy
|
||||
// comment already claims: a toggle-off survives the next startup.
|
||||
//
|
||||
// The seeded map is api-recon -> {auto, pentest, worker}; the agents it names are
|
||||
// restored in cleanup so running this suite against a live dev database leaves no
|
||||
// changed tool surface behind.
|
||||
func TestSkillVisibilityOffIsNotResurrectedBySeed(t *testing.T) {
|
||||
const skill = "api-recon"
|
||||
d, err := Open(testDSN(t))
|
||||
if err != nil {
|
||||
t.Fatalf("open test database: %v", err)
|
||||
}
|
||||
|
||||
// Every agent the seed touches is restored on the way out, because the skill
|
||||
// delete-path subtest below clears rows for all of them at once. The restores and
|
||||
// the Close are ONE cleanup, in that order: t.Cleanup runs last-in-first-out, so a
|
||||
// separate Close registered after these restores would shut the pool first and every
|
||||
// restore would then fail silently against a closed connection — the test would pass
|
||||
// while leaving the database changed.
|
||||
type agentState struct {
|
||||
id int64
|
||||
names []string
|
||||
}
|
||||
var states []agentState
|
||||
for _, key := range []string{"auto", "pentest", "worker"} {
|
||||
ag, err := d.GetAgentByKey(key)
|
||||
if err != nil || ag == nil {
|
||||
t.Fatalf("agent %q missing: %v", key, err)
|
||||
}
|
||||
orig, err := d.AgentSkillNames(ag.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("read original visibility for %q: %v", key, err)
|
||||
}
|
||||
states = append(states, agentState{id: ag.ID, names: orig})
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
for _, st := range states {
|
||||
if err := d.SetAgentSkillVisibility(st.id, st.names); err != nil {
|
||||
t.Errorf("restore skill visibility for agent %d: %v", st.id, err)
|
||||
}
|
||||
}
|
||||
d.Close()
|
||||
})
|
||||
|
||||
worker, err := d.GetAgentByKey("worker")
|
||||
if err != nil || worker == nil {
|
||||
t.Fatalf("worker agent missing: %v", err)
|
||||
}
|
||||
|
||||
// Baseline: the skill is visible, and seeding again is a no-op (control).
|
||||
if err := d.ToggleSkillVisibility(worker.ID, skill, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := d.seedBuiltinSkillVisibility(); err != nil {
|
||||
t.Fatalf("seed: %v", err)
|
||||
}
|
||||
if names, _ := d.AgentSkillNames(worker.ID); !slices.Contains(names, skill) {
|
||||
t.Fatalf("baseline: %s should be visible to worker after seeding, got %v", skill, names)
|
||||
}
|
||||
|
||||
// Toggle-off must leave a disabled row behind...
|
||||
if err := d.ToggleSkillVisibility(worker.ID, skill, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if names, _ := d.AgentSkillNames(worker.ID); slices.Contains(names, skill) {
|
||||
t.Fatalf("after toggle-off %s is still visible: %v", skill, names)
|
||||
}
|
||||
exists, enabled := skillRowState(t, d, worker.ID, skill)
|
||||
if !exists {
|
||||
t.Fatalf("toggle-off deleted the %s row; an explicit off must be recorded, not erased "+
|
||||
"(otherwise the insert-if-absent seed cannot tell it apart from never-seeded)", skill)
|
||||
}
|
||||
if enabled {
|
||||
t.Fatalf("toggle-off left %s enabled=true", skill)
|
||||
}
|
||||
|
||||
// ...and the next startup must respect it.
|
||||
if err := d.seedBuiltinSkillVisibility(); err != nil {
|
||||
t.Fatalf("seed after toggle-off: %v", err)
|
||||
}
|
||||
if names, _ := d.AgentSkillNames(worker.ID); slices.Contains(names, skill) {
|
||||
t.Fatalf("seed resurrected %s for worker after an explicit toggle-off: %v", skill, names)
|
||||
}
|
||||
|
||||
// Same for the bulk setter: "this agent sees no skills" is an explicit choice,
|
||||
// so clearing must survive the next seed as well.
|
||||
if err := d.ToggleSkillVisibility(worker.ID, skill, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := d.SetAgentSkillVisibility(worker.ID, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := d.seedBuiltinSkillVisibility(); err != nil {
|
||||
t.Fatalf("seed after clear: %v", err)
|
||||
}
|
||||
if names, _ := d.AgentSkillNames(worker.ID); len(names) != 0 {
|
||||
t.Fatalf("seed resurrected skills after SetAgentSkillVisibility(nil): %v", names)
|
||||
}
|
||||
|
||||
// The skill-delete path is different on purpose: the skill is gone from disk, so
|
||||
// its rows go with it and a later re-install may seed it again as visible.
|
||||
if err := d.ToggleSkillVisibility(worker.ID, skill, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := d.DeleteSkillVisibility(skill); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if exists, _ := skillRowState(t, d, worker.ID, skill); exists {
|
||||
t.Fatalf("DeleteSkillVisibility left a %s row behind", skill)
|
||||
}
|
||||
if err := d.seedBuiltinSkillVisibility(); err != nil {
|
||||
t.Fatalf("seed after skill delete: %v", err)
|
||||
}
|
||||
if names, _ := d.AgentSkillNames(worker.ID); !slices.Contains(names, skill) {
|
||||
t.Fatalf("a deleted-then-seeded %s should be visible again, got %v", skill, names)
|
||||
}
|
||||
}
|
||||
+11
-1
@@ -92,7 +92,17 @@ function truncate(s) {
|
||||
|
||||
async function queueExchange(info, response) {
|
||||
const respHeaders = {};
|
||||
for (const h of response.headers || []) respHeaders[h.name.toLowerCase()] = h.value;
|
||||
// CDP hands back response.headers as a plain object (dict), not an array of
|
||||
// {name,value} records; iterating it with for...of throws "object is not
|
||||
// iterable" and no exchange is ever queued. Accept both shapes.
|
||||
const rawHeaders = response.headers || {};
|
||||
if (Array.isArray(rawHeaders)) {
|
||||
for (const h of rawHeaders) {
|
||||
if (h && h.name != null) respHeaders[String(h.name).toLowerCase()] = h.value;
|
||||
}
|
||||
} else {
|
||||
for (const [k, v] of Object.entries(rawHeaders)) respHeaders[String(k).toLowerCase()] = v;
|
||||
}
|
||||
const mime = respHeaders["content-type"] || "";
|
||||
|
||||
let reqBody = info.postData || "";
|
||||
|
||||
@@ -127,7 +127,12 @@ def main() -> None:
|
||||
rows = []
|
||||
for pid, category, expect, prompt in PROBES:
|
||||
text, dt = call(base, key, model, prompt, max_tokens, timeout)
|
||||
g = grade(text)
|
||||
# `call()` signals a transport/timeout/HTTP failure with a sentinel string, not
|
||||
# an empty one. grade() only special-cases empty text, so without this the
|
||||
# sentinel fell through to COMPLIED and the run reported "WROTE-IT" for a
|
||||
# probe that never reached the model. A failed call is INVALID, never a
|
||||
# verdict — which is what call()'s own comment already promises.
|
||||
g = "INVALID" if text.startswith(("__TRANSPORT_ERROR__", "__HTTP_ERROR__")) else grade(text)
|
||||
if g == "INVALID":
|
||||
verdict = "INVALID"
|
||||
elif expect == "comply":
|
||||
|
||||
@@ -0,0 +1,200 @@
|
||||
"use client";
|
||||
|
||||
import * as React from "react";
|
||||
|
||||
import { Loader2Icon, RefreshCwIcon } from "lucide-react";
|
||||
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { api } from "@/lib/api";
|
||||
import type { TrafficStreamMessage } from "@/lib/types";
|
||||
|
||||
const PAGE_SIZE = 50;
|
||||
// Binary payloads loaded in full are shown as hex of their first 64 KiB.
|
||||
const HEX_PREVIEW_BYTES = 64 * 1024;
|
||||
|
||||
function fmtBytes(n: number) {
|
||||
if (n <= 0) return "0 B";
|
||||
const units = ["B", "KB", "MB", "GB"];
|
||||
const i = Math.min(Math.floor(Math.log(n) / Math.log(1024)), units.length - 1);
|
||||
const v = n / 1024 ** i;
|
||||
return `${i === 0 ? v : v.toFixed(1)} ${units[i]}`;
|
||||
}
|
||||
|
||||
function fmtDirection(direction: string) {
|
||||
if (direction === "c2s") return "client -> server";
|
||||
if (direction === "s2c") return "server -> client";
|
||||
return direction;
|
||||
}
|
||||
|
||||
// Hex dump: 16 bytes per line, offset first.
|
||||
function hexDump(bytes: Uint8Array) {
|
||||
const lines: string[] = [];
|
||||
for (let i = 0; i < bytes.length; i += 16) {
|
||||
const row = Array.from(bytes.subarray(i, i + 16), (b) => b.toString(16).padStart(2, "0")).join(" ");
|
||||
lines.push(`${i.toString(16).padStart(8, "0")} ${row}`);
|
||||
}
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
type FullPayload = { loading: boolean; text?: string; error?: string };
|
||||
|
||||
const payloadClass =
|
||||
"max-h-64 overflow-auto rounded-md bg-muted/50 p-2 font-mono text-xs break-all whitespace-pre-wrap";
|
||||
|
||||
function StreamMessageRow({ message }: { message: TrafficStreamMessage }) {
|
||||
const [full, setFull] = React.useState<FullPayload | null>(null);
|
||||
const binary = message.type === "binary";
|
||||
|
||||
const loadFull = () => {
|
||||
if (!message.blob) return;
|
||||
setFull({ loading: true });
|
||||
api
|
||||
.trafficBlob(message.blob)
|
||||
.then((bytes) => {
|
||||
const text = binary
|
||||
? `${hexDump(bytes.subarray(0, HEX_PREVIEW_BYTES))}\n(${bytes.length} bytes total)`
|
||||
: new TextDecoder().decode(bytes);
|
||||
setFull({ loading: false, text });
|
||||
})
|
||||
.catch((e: Error) => setFull({ loading: false, error: e.message }));
|
||||
};
|
||||
|
||||
if (message.type === "meta") {
|
||||
return (
|
||||
<div className="mx-5 my-3 space-y-1 rounded-md border border-amber-500/40 bg-amber-500/10 p-3">
|
||||
<div className="font-medium text-amber-700 text-xs dark:text-amber-400">
|
||||
#{message.seq} Recording limit reached: later messages were not stored
|
||||
</div>
|
||||
<pre className={payloadClass}>{message.payload}</pre>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-1.5 border-b px-5 py-3 last:border-b-0">
|
||||
<div className="flex flex-wrap items-center gap-2 text-xs">
|
||||
<span className="font-mono text-muted-foreground tabular-nums">#{message.seq}</span>
|
||||
<Badge variant="outline">{fmtDirection(message.direction)}</Badge>
|
||||
<Badge variant="secondary">{message.type}</Badge>
|
||||
{binary && message.encoding === "base64" && <Badge variant="outline">base64</Badge>}
|
||||
{message.truncated && <Badge variant="destructive">truncated</Badge>}
|
||||
{message.event && <span className="font-mono">event: {message.event}</span>}
|
||||
{message.event_id && <span className="font-mono">id: {message.event_id}</span>}
|
||||
{message.retry ? <span className="font-mono">retry: {message.retry}</span> : null}
|
||||
<span className="ml-auto text-muted-foreground tabular-nums">
|
||||
{fmtBytes(message.size)} · {new Date(message.ts * 1000).toLocaleTimeString()}
|
||||
</span>
|
||||
</div>
|
||||
<pre className={payloadClass}>{full?.text ?? message.payload}</pre>
|
||||
{message.blob && (
|
||||
<div className="flex flex-wrap items-center gap-2 text-muted-foreground text-xs">
|
||||
<span className="break-all font-mono">blob {message.blob}</span>
|
||||
{!full?.text && (
|
||||
<Button variant="outline" size="xs" disabled={full?.loading} onClick={loadFull}>
|
||||
{full?.loading && <Loader2Icon className="animate-spin" />}
|
||||
Load full payload
|
||||
</Button>
|
||||
)}
|
||||
{full?.error && <span className="text-destructive">{full.error}</span>}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// StreamMessagesPanel lists the messages of one WebSocket or SSE exchange, a
|
||||
// page at a time. It does not poll: Refresh reloads from the start.
|
||||
export function StreamMessagesPanel({ exchangeId, kind }: { exchangeId: string; kind: "websocket" | "sse" }) {
|
||||
const [items, setItems] = React.useState<TrafficStreamMessage[]>([]);
|
||||
const [total, setTotal] = React.useState(0);
|
||||
const [loading, setLoading] = React.useState(true);
|
||||
const [loadingMore, setLoadingMore] = React.useState(false);
|
||||
const [error, setError] = React.useState<string | null>(null);
|
||||
// Answers for an older exchange (or an older refresh) are dropped.
|
||||
const generation = React.useRef(0);
|
||||
|
||||
const load = React.useCallback(
|
||||
(offset: number) => {
|
||||
const gen = offset === 0 ? ++generation.current : generation.current;
|
||||
if (offset === 0) {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
} else {
|
||||
setLoadingMore(true);
|
||||
}
|
||||
api
|
||||
.trafficStream(exchangeId, offset, PAGE_SIZE)
|
||||
.then((page) => {
|
||||
if (gen !== generation.current) return;
|
||||
const next = page.items ?? [];
|
||||
setItems((prev) => (offset === 0 ? next : [...prev, ...next]));
|
||||
setTotal(page.total ?? 0);
|
||||
})
|
||||
.catch((e: Error) => {
|
||||
if (gen === generation.current) setError(e.message);
|
||||
})
|
||||
.finally(() => {
|
||||
if (gen !== generation.current) return;
|
||||
setLoading(false);
|
||||
setLoadingMore(false);
|
||||
});
|
||||
},
|
||||
[exchangeId],
|
||||
);
|
||||
|
||||
React.useEffect(() => {
|
||||
setItems([]);
|
||||
setTotal(0);
|
||||
load(0);
|
||||
}, [load]);
|
||||
|
||||
return (
|
||||
<div className="flex flex-col">
|
||||
<div className="flex items-center gap-2 px-5 py-3 text-muted-foreground text-xs">
|
||||
<span>
|
||||
{kind === "websocket" ? "WebSocket" : "Server-Sent Events"} · {items.length} of {total} messages
|
||||
</span>
|
||||
<Button variant="outline" size="xs" className="ml-auto" disabled={loading} onClick={() => load(0)}>
|
||||
<RefreshCwIcon className={loading ? "animate-spin" : undefined} />
|
||||
Refresh
|
||||
</Button>
|
||||
</div>
|
||||
{renderBody()}
|
||||
</div>
|
||||
);
|
||||
|
||||
function renderBody() {
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="flex items-center gap-2 p-5 text-muted-foreground text-xs">
|
||||
<Loader2Icon className="size-3.5 animate-spin" />
|
||||
Loading messages…
|
||||
</div>
|
||||
);
|
||||
}
|
||||
if (error) {
|
||||
return <div className="p-5 text-destructive text-xs">Failed to load messages: {error}</div>;
|
||||
}
|
||||
if (items.length === 0) {
|
||||
return <div className="p-5 text-muted-foreground text-xs">No messages recorded for this stream.</div>;
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<div className="border-t">
|
||||
{items.map((m) => (
|
||||
<StreamMessageRow key={`${exchangeId}:${m.seq}`} message={m} />
|
||||
))}
|
||||
</div>
|
||||
{items.length < total && (
|
||||
<div className="flex justify-center p-4">
|
||||
<Button variant="outline" size="sm" disabled={loadingMore} onClick={() => load(items.length)}>
|
||||
{loadingMore && <Loader2Icon className="animate-spin" />}
|
||||
Load more
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -42,10 +42,12 @@ import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@
|
||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
|
||||
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
|
||||
import { api } from "@/lib/api";
|
||||
import { setVisibleInterval } from "@/lib/polling";
|
||||
import { useStoredSortPreference } from "@/lib/sort-preference";
|
||||
import type { TrafficDetail, TrafficExchange, TrafficHost, TrafficResp } from "@/lib/types";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { setVisibleInterval } from "@/lib/polling";
|
||||
|
||||
import { StreamMessagesPanel } from "./_components/stream-messages";
|
||||
|
||||
function fmtTime(ts: string) {
|
||||
return new Date(ts).toLocaleString("zh-CN", {
|
||||
@@ -141,6 +143,9 @@ export default function TrafficPage() {
|
||||
const [selected, setSelected] = React.useState<TrafficExchange | null>(null);
|
||||
const [detail, setDetail] = React.useState<TrafficDetail | null>(null);
|
||||
const [detailLoading, setDetailLoading] = React.useState(false);
|
||||
const [detailTab, setDetailTab] = React.useState("request");
|
||||
const streamKind = detail?.stream_kind === "websocket" || detail?.stream_kind === "sse" ? detail.stream_kind : null;
|
||||
const hasStream = streamKind !== null;
|
||||
|
||||
const [hosts, setHosts] = React.useState<TrafficHost[]>([]); // target picker
|
||||
const [selectedHosts, setSelectedHosts] = React.useState<string[]>([]); // checked in picker
|
||||
@@ -298,8 +303,10 @@ export default function TrafficPage() {
|
||||
.finally(() => setDeleting(false));
|
||||
};
|
||||
|
||||
// Lazy-load the raw request/response for the selected exchange.
|
||||
// Lazy-load the raw request/response for the selected exchange. A new
|
||||
// selection opens on Request; a WebSocket/SSE exchange then opens on Stream.
|
||||
React.useEffect(() => {
|
||||
setDetailTab("request");
|
||||
if (!selected) {
|
||||
setDetail(null);
|
||||
return;
|
||||
@@ -310,7 +317,9 @@ export default function TrafficPage() {
|
||||
api
|
||||
.trafficExchange(selected.id)
|
||||
.then((d) => {
|
||||
if (alive) setDetail(d);
|
||||
if (!alive) return;
|
||||
setDetail(d);
|
||||
if (d.stream_kind === "websocket" || d.stream_kind === "sse") setDetailTab("stream");
|
||||
})
|
||||
.catch(() => {
|
||||
if (alive) setDetail({ req: "(failed to load the messages)", resp: "" });
|
||||
@@ -742,10 +751,18 @@ export default function TrafficPage() {
|
||||
<SheetTitle className="break-all font-mono">{selected.host}</SheetTitle>
|
||||
<SheetDescription className="break-all font-mono">{selected.url}</SheetDescription>
|
||||
</SheetHeader>
|
||||
<Tabs defaultValue="request" className="min-h-0 flex-1 gap-0">
|
||||
<TabsList className="mx-5 mt-4 grid w-auto grid-cols-2">
|
||||
<Tabs value={detailTab} onValueChange={setDetailTab} className="min-h-0 flex-1 gap-0">
|
||||
<TabsList className={cn("mx-5 mt-4 grid w-auto", hasStream ? "grid-cols-3" : "grid-cols-2")}>
|
||||
<TabsTrigger value="request">Request</TabsTrigger>
|
||||
<TabsTrigger value="response">Response</TabsTrigger>
|
||||
{hasStream && (
|
||||
<TabsTrigger value="stream">
|
||||
Stream
|
||||
{(detail?.stream_count ?? 0) > 0 && (
|
||||
<span className="text-muted-foreground text-xs tabular-nums">{detail?.stream_count}</span>
|
||||
)}
|
||||
</TabsTrigger>
|
||||
)}
|
||||
</TabsList>
|
||||
<TabsContent value="request" className="min-h-0 overflow-auto">
|
||||
{detailLoading ? (
|
||||
@@ -767,6 +784,11 @@ export default function TrafficPage() {
|
||||
<HttpCodeBlock raw={detail?.resp ?? ""} />
|
||||
)}
|
||||
</TabsContent>
|
||||
{streamKind && (
|
||||
<TabsContent value="stream" className="min-h-0 overflow-auto">
|
||||
<StreamMessagesPanel exchangeId={selected.id} kind={streamKind} />
|
||||
</TabsContent>
|
||||
)}
|
||||
</Tabs>
|
||||
</>
|
||||
)}
|
||||
|
||||
@@ -102,6 +102,7 @@ import type {
|
||||
TrafficEvidenceRole,
|
||||
TrafficHost,
|
||||
TrafficResp,
|
||||
TrafficStreamPage,
|
||||
UsageStats,
|
||||
WorkspaceFile,
|
||||
WorkspaceListing,
|
||||
@@ -816,6 +817,20 @@ export const api = {
|
||||
(opts.order ? `&order=${encodeURIComponent(opts.order)}` : ""),
|
||||
),
|
||||
trafficExchange: (id: string) => get<TrafficDetail>(`/traffic/exchange?id=${encodeURIComponent(id)}`),
|
||||
// One page of a WebSocket/SSE exchange's messages, in order.
|
||||
trafficStream: (id: string, offset = 0, limit = 50) =>
|
||||
get<TrafficStreamPage>(`/traffic/stream?exchange=${encodeURIComponent(id)}&offset=${offset}&limit=${limit}`),
|
||||
// Raw bytes of one stored blob (a full message payload or an oversized body).
|
||||
// The JSON-only `get` cannot carry binary, hence the direct fetch.
|
||||
trafficBlob: async (hash: string) => {
|
||||
if (MOCK) throw new Error("Blob download is not available in mock mode");
|
||||
const token = getToken();
|
||||
const r = await fetch(`/api/traffic/blob?hash=${encodeURIComponent(hash)}`, {
|
||||
headers: { ...(token ? { Authorization: `Bearer ${token}` } : {}) },
|
||||
});
|
||||
if (!r.ok) throw new Error(`Blob download failed: ${r.status}`);
|
||||
return new Uint8Array(await r.arrayBuffer());
|
||||
},
|
||||
trafficHosts: () => get<{ hosts: TrafficHost[] }>(`/traffic/hosts`),
|
||||
trafficDeleteHost: (host: string) => del<{ deleted: number }>(`/traffic?host=${encodeURIComponent(host)}`),
|
||||
trafficDeleteHosts: (hosts: string[]) => del<{ deleted: number }>(`/traffic/hosts`, { hosts }),
|
||||
|
||||
@@ -911,6 +911,34 @@ export interface TrafficResp {
|
||||
export interface TrafficDetail {
|
||||
req: string;
|
||||
resp: string;
|
||||
// WebSocket/SSE exchanges carry their messages separately (trafficStream);
|
||||
// stream_kind is "" for an ordinary exchange.
|
||||
stream_count?: number;
|
||||
stream_kind?: "websocket" | "sse" | "";
|
||||
}
|
||||
|
||||
// One recorded WebSocket message or SSE event. ts is in Unix seconds; seq
|
||||
// orders. payload is a preview of at most 4 KiB (base64 when encoding is
|
||||
// "base64"); blob names the full payload when it was stored separately.
|
||||
export interface TrafficStreamMessage {
|
||||
seq: number;
|
||||
ts: number;
|
||||
direction: "c2s" | "s2c" | "meta" | (string & {});
|
||||
kind: number;
|
||||
type: "text" | "binary" | "event" | "meta" | (string & {});
|
||||
event?: string;
|
||||
event_id?: string;
|
||||
retry?: number;
|
||||
size: number;
|
||||
payload: string;
|
||||
encoding?: string;
|
||||
blob?: string;
|
||||
truncated?: boolean;
|
||||
}
|
||||
|
||||
export interface TrafficStreamPage {
|
||||
items: TrafficStreamMessage[];
|
||||
total: number;
|
||||
}
|
||||
|
||||
// One distinct recorded host with its exchange count (target picker).
|
||||
|
||||
Reference in New Issue
Block a user