fix(chat): show the sandbox container a conversation actually uses

A reporter chat bound to one task ran commands in that task's sandbox, while the chat page kept querying the empty session container. Save the task or session target on the conversation and point the badge, uploads, and later turns at it.
This commit is contained in:
ji ruo committed 2026-12-02 17:45:28 +08:00
1 parent 8bac4960e7
commit b18cf89b77
9 files changed
+531 -57

No files matched your search

@@ -64,15 +64,23 @@ function fmtUptime(startedAt?: string): string {
export function SessionSandboxControl({
conversationId,
sandboxScope,
sandboxOwnerId,
running: chatRunning,
}: {
conversationId: number;
sandboxScope?: string;
sandboxOwnerId?: string;
running: boolean;
}) {
const { isEn } = useLiveTranslation();
const L = (zh: string, en: string) => (isEn ? en : zh);
const ownerID = `conv-${conversationId}`;
const scope = sandboxScope === "task" ? "task" : "session";
const ownerID = sandboxOwnerId ?? `conv-${conversationId}`;
const taskBound = scope === "task";
const boxZh = taskBound ? "任务沙箱" : "会话沙箱";
const boxEn = taskBound ? "Task sandbox" : "Session sandbox";
const [open, setOpen] = React.useState(false);
const [info, setInfo] = React.useState<SandboxContainerInfo | null>(null);
const [loading, setLoading] = React.useState(false);
@@ -88,7 +96,7 @@ export function SessionSandboxControl({
const seq = ++reqSeqRef.current;
if (!silent) setLoading(true);
try {
const res = await api.sandboxContainer("session", ownerID, withStats);
const res = await api.sandboxContainer(scope, ownerID, withStats);
if (seq === reqSeqRef.current) {
setInfo(res);
}
@@ -100,7 +108,7 @@ export function SessionSandboxControl({
}
}
},
[ownerID],
[scope, ownerID],
);
React.useEffect(() => {
@@ -173,10 +181,10 @@ export function SessionSandboxControl({
const handleStart = async () => {
setBusy("start");
try {
const res = await api.startSandboxContainer("session", ownerID);
const res = await api.startSandboxContainer(scope, ownerID);
reqSeqRef.current += 1;
setInfo(res);
toast.success(L("会话沙箱容器已启动", "Session sandbox container started"));
toast.success(L(`${boxZh}容器已启动`, `${boxEn} container started`));
} catch (e) {
toast.error(e instanceof Error ? e.message : String(e));
} finally {
@@ -187,10 +195,10 @@ export function SessionSandboxControl({
const handleStop = async () => {
setBusy("stop");
try {
const res = await api.stopSandboxContainer("session", ownerID);
const res = await api.stopSandboxContainer(scope, ownerID);
reqSeqRef.current += 1;
setInfo(res);
toast.success(L("会话沙箱容器已停止(内存已释放)", "Session sandbox container stopped"));
toast.success(L(`${boxZh}容器已停止(内存已释放)`, `${boxEn} container stopped`));
} catch (e) {
toast.error(e instanceof Error ? e.message : String(e));
} finally {
@@ -204,13 +212,13 @@ export function SessionSandboxControl({
setClearWorkspace(false);
setBusy("recreate");
try {
const res = await api.recreateSandboxContainer("session", ownerID, { clear_workspace: wipe });
const res = await api.recreateSandboxContainer(scope, ownerID, { clear_workspace: wipe });
reqSeqRef.current += 1;
setInfo(res);
toast.success(
wipe
? L("会话沙箱已重建并清空工作区", "Session sandbox recreated with clean workspace")
: L("会话沙箱已重建(工作区文件已保留)", "Session sandbox recreated"),
? L(`${boxZh}已重建并清空工作区`, `${boxEn} recreated with clean workspace`)
: L(`${boxZh}已重建(工作区文件已保留)`, `${boxEn} recreated`),
);
} catch (e) {
toast.error(e instanceof Error ? e.message : String(e));
@@ -223,9 +231,9 @@ export function SessionSandboxControl({
setRemoveOpen(false);
setBusy("remove");
try {
await api.removeSandboxContainer("session", ownerID);
await api.removeSandboxContainer(scope, ownerID);
await fetchInfo(false);
toast.success(L("会话沙箱容器已删除(工作区文件已保留)", "Session sandbox container removed"));
toast.success(L(`${boxZh}容器已删除(工作区文件已保留)`, `${boxEn} container removed`));
} catch (e) {
toast.error(e instanceof Error ? e.message : String(e));
} finally {
@@ -247,21 +255,28 @@ export function SessionSandboxControl({
<PopoverTitle className="flex items-center justify-between text-sm">
<span className="flex items-center gap-1.5">
<BoxIcon className="size-4" />
{L("对话隔离沙箱", "Chat Sandbox")}
{L(taskBound ? "任务沙箱" : "对话隔离沙箱", taskBound ? "Task Sandbox" : "Chat Sandbox")}
</span>
<Badge variant={running ? "default" : "secondary"}>{info.state}</Badge>
</PopoverTitle>
<PopoverDescription className="text-xs">
{L(
"本对话调用命令、脚本或浏览器时将在此独立非 root 容器内执行(按需自动创建,空闲自动停机)。",
"Commands, scripts, and browser tools in this chat run inside this isolated non-root container (created on demand).",
)}
{taskBound
? L(
"本对话调用命令、脚本或浏览器时,实际跑在关联任务的沙箱容器里,不会另开一个对话容器。",
"Commands, scripts, and browser tools in this chat run in the linked task sandbox, not a separate chat container.",
)
: L(
"本对话调用命令、脚本或浏览器时将在此独立非 root 容器内执行(按需自动创建,空闲自动停机)。",
"Commands, scripts, and browser tools in this chat run inside this isolated non-root container (created on demand).",
)}
</PopoverDescription>
</PopoverHeader>
<div className="space-y-1.5 rounded-md border bg-muted/40 p-2.5 font-mono text-[11px]">
<div className="flex justify-between gap-2">
<span className="text-muted-foreground">{L("会话", "Session")}</span>
<span className="text-muted-foreground">
{L(taskBound ? "任务" : "会话", taskBound ? "Task" : "Session")}
</span>
<span className="truncate">{ownerID}</span>
</div>
<div className="flex justify-between gap-2">
@@ -371,7 +386,18 @@ export function SessionSandboxControl({
</Button>
</div>
<div className="flex justify-end border-t pt-2">
<div className="flex items-center justify-between gap-2 border-t pt-2">
{taskBound ? (
<Link
href={`/function/tasks/detail?id=${encodeURIComponent(ownerID)}`}
className="inline-flex items-center gap-1 text-[11px] text-muted-foreground hover:text-foreground"
>
{L("查看关联任务", "Open task")}
<ExternalLinkIcon className="size-3" />
</Link>
) : (
<span />
)}
<Link
href="/system/sandbox"
className="inline-flex items-center gap-1 text-[11px] text-muted-foreground hover:text-foreground"
@@ -386,12 +412,22 @@ export function SessionSandboxControl({
<AlertDialog open={recreateOpen} onOpenChange={setRecreateOpen}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{L("重建对话沙箱容器", "Recreate Chat Sandbox")}</AlertDialogTitle>
<AlertDialogDescription>
<AlertDialogTitle>
{L(
"确定销毁当前对话的旧容器并按最新镜像与资源配额重新创建启动吗?",
"Replace this chat's sandbox container with a fresh one using the latest image and limits?",
taskBound ? "重建任务沙箱容器" : "重建对话沙箱容器",
taskBound ? "Recreate Task Sandbox" : "Recreate Chat Sandbox",
)}
</AlertDialogTitle>
<AlertDialogDescription>
{taskBound
? L(
"确定销毁关联任务的沙箱容器,并按最新镜像与资源配额重新创建吗?这会影响该任务,不会另开对话容器。",
"Replace the linked task's sandbox container with a fresh one? This affects that task.",
)
: L(
"确定销毁当前对话的旧容器并按最新镜像与资源配额重新创建启动吗?",
"Replace this chat's sandbox container with a fresh one using the latest image and limits?",
)}
</AlertDialogDescription>
</AlertDialogHeader>
<div className="flex items-start gap-2.5 rounded-md border bg-muted/30 p-3 text-xs">
@@ -406,10 +442,15 @@ export function SessionSandboxControl({
{L("同时清空工作区文件 (/workspace)", "Also clear workspace files (/workspace)")}
</div>
<div className="text-muted-foreground">
{L(
"默认保留本对话上传与生成的文件;勾选后将清空 /workspace 下所有文件。",
"Session files are preserved by default. Check to wipe /workspace.",
)}
{taskBound
? L(
"默认保留任务工作区;勾选后会清空该任务 /workspace 下的文件,包括已有截图和脚本。",
"Task files are kept by default. Check to wipe this task's /workspace, including screenshots and scripts.",
)
: L(
"默认保留本对话上传与生成的文件;勾选后将清空 /workspace 下所有文件。",
"Session files are preserved by default. Check to wipe /workspace.",
)}
</div>
</Label>
</div>
@@ -425,12 +466,22 @@ export function SessionSandboxControl({
<AlertDialog open={removeOpen} onOpenChange={setRemoveOpen}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{L("删除对话沙箱容器", "Delete Chat Sandbox")}</AlertDialogTitle>
<AlertDialogDescription>
<AlertDialogTitle>
{L(
"确定删除该对话的沙箱容器吗?宿主机工作区文件会保留,后续调用工具时将自动重新创建。",
"Remove this chat's sandbox container? Host workspace files will be preserved.",
taskBound ? "删除任务沙箱容器" : "删除对话沙箱容器",
taskBound ? "Delete Task Sandbox" : "Delete Chat Sandbox",
)}
</AlertDialogTitle>
<AlertDialogDescription>
{taskBound
? L(
"确定删除关联任务的沙箱容器吗?任务工作区文件会保留,下次执行命令时会重新创建。",
"Remove the linked task's sandbox container? Task workspace files will be preserved.",
)
: L(
"确定删除该对话的沙箱容器吗?宿主机工作区文件会保留,后续调用工具时将自动重新创建。",
"Remove this chat's sandbox container? Host workspace files will be preserved.",
)}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
+9 -2
View File
@@ -756,7 +756,9 @@ function ChatView({
if (files.length === 0) return;
setUploading(true);
try {
const r = await api.chatUpload("session", `conv-${conv.id}`, files);
const uploadScope = conv.sandbox_scope === "task" ? "task" : "session";
const uploadOwner = conv.sandbox_owner_id ?? `conv-${conv.id}`;
const r = await api.chatUpload(uploadScope, uploadOwner, files);
setAttachments((prev) => [...prev, ...r.attachments]);
} catch (e) {
toast.error(translate("overview.uploadFailedPrefix") + localizedError(e, translate));
@@ -825,7 +827,12 @@ function ChatView({
)}
{running && <LiveBadge />}
<SideQuestionButton side={side} />
<SessionSandboxControl conversationId={conv.id} running={running} />
<SessionSandboxControl
conversationId={conv.id}
sandboxScope={conv.sandbox_scope}
sandboxOwnerId={conv.sandbox_owner_id}
running={running}
/>
<div className="text-muted-foreground ml-auto flex min-w-0 max-w-full items-center justify-end gap-x-3 gap-y-1 text-xs max-sm:w-full max-sm:flex-wrap">
{tokenTotal.turns > 0 && (
<span title={translate("overview.roundsHint")} className="tabular-nums">
+3
View File
@@ -821,6 +821,9 @@ export interface Conversation {
llm_profile_id?: number;
pinned?: boolean;
pinned_at?: string | null;
// Sandbox the conversation's commands actually use. task → that task's container.
sandbox_scope?: string;
sandbox_owner_id?: string;
created_at: string;
updated_at: string;
}