fix: 释放孤儿 active trace
This commit is contained in:
@@ -906,16 +906,44 @@ async function claimWorkbenchWorkspaceTurn({ params = {}, options = {}, traceId,
|
||||
}
|
||||
const activeTraceId = safeTraceId(workspace.activeTraceId);
|
||||
const activeResult = activeTraceId ? await resolveWorkbenchActiveTraceResult(activeTraceId, options) : null;
|
||||
const activeTraceSnapshot = activeTraceId ? (options.traceStore ?? defaultCodeAgentTraceStore).snapshot(activeTraceId) : null;
|
||||
const activeTraceOrphaned = Boolean(activeTraceId && !activeResult && isWorkbenchOrphanActiveTrace(activeTraceSnapshot));
|
||||
const selectedConversationId = safeConversationId(workspace.selectedConversationId);
|
||||
const shouldInspectSelectedConversation = Boolean(
|
||||
activeTraceId && !activeResult && selectedConversationId && options.accessController?.visibleConversationForActor
|
||||
activeTraceId && !activeResult && !activeTraceOrphaned && selectedConversationId && options.accessController?.visibleConversationForActor
|
||||
);
|
||||
const selectedConversation = shouldInspectSelectedConversation
|
||||
? await options.accessController.visibleConversationForActor(options.actor, selectedConversationId, workspace.projectId)
|
||||
: null;
|
||||
const activeTraceBlocksTurn = Boolean(
|
||||
activeTraceId && (activeResult?.status === "running" || (!activeResult && isActiveWorkbenchConversation(selectedConversation)))
|
||||
activeTraceId && !activeTraceOrphaned && (activeResult?.status === "running" || (!activeResult && isActiveWorkbenchConversation(selectedConversation)))
|
||||
);
|
||||
if (activeTraceOrphaned) {
|
||||
await store.updateWorkspace?.({
|
||||
workspaceId,
|
||||
ownerUserId: options.actor.id,
|
||||
actorRole: options.actor.role,
|
||||
selectedConversationId: workspace.selectedConversationId,
|
||||
selectedAgentSessionId: workspace.selectedAgentSessionId,
|
||||
selectedDevicePodId: workspace.selectedDevicePodId,
|
||||
activeTraceId: null,
|
||||
providerProfile: workspace.providerProfile,
|
||||
patch: {
|
||||
...(workspace.workspace && typeof workspace.workspace === "object" ? workspace.workspace : {}),
|
||||
activeTraceId: null,
|
||||
staleActiveTraceId: activeTraceId,
|
||||
staleActiveTraceReason: "orphan-cancel-session-missing",
|
||||
sessionStatus: "failed",
|
||||
updatedAt: new Date().toISOString(),
|
||||
source: "code-agent-submit-active-trace-repair",
|
||||
valuesRedacted: true,
|
||||
secretMaterialStored: false
|
||||
},
|
||||
updatedBySessionId: options.authSession?.id ?? null,
|
||||
updatedByClient: "code-agent-submit-active-trace-repair",
|
||||
now: new Date().toISOString()
|
||||
});
|
||||
}
|
||||
if (activeTraceBlocksTurn) {
|
||||
sendJson(response, 409, {
|
||||
ok: false,
|
||||
@@ -964,6 +992,14 @@ function isActiveWorkbenchConversation(conversation = null) {
|
||||
return values.some((value) => ["running", "busy", "pending", "active", "queued"].includes(textValue(value).toLowerCase()));
|
||||
}
|
||||
|
||||
function isWorkbenchOrphanActiveTrace(snapshot) {
|
||||
if (!snapshot || typeof snapshot !== "object") return false;
|
||||
const last = snapshot.lastEvent && typeof snapshot.lastEvent === "object" ? snapshot.lastEvent : null;
|
||||
if (!last) return false;
|
||||
if (last.errorCode !== "cancel_session_missing" && last.label !== "cancel:not_cancelable") return false;
|
||||
return !snapshot.sessionId && !snapshot.runId && !snapshot.commandId && !snapshot.sessionStatus && !snapshot.turn;
|
||||
}
|
||||
|
||||
async function resolveWorkbenchActiveTraceResult(traceId, options = {}) {
|
||||
if (!safeTraceId(traceId)) return null;
|
||||
const cached = options.codeAgentChatResults?.get?.(traceId) ?? null;
|
||||
|
||||
Reference in New Issue
Block a user