feat: add thread status documents and safe thread reply handling

This commit is contained in:
kris
2026-04-04 11:50:46 +08:00
parent 010d8eda2d
commit 4d9b8e2976
10 changed files with 487 additions and 57 deletions

View File

@@ -25,17 +25,66 @@ function trimToDefined(value: string | undefined) {
return trimmed ? trimmed : undefined;
}
function looksLikeThreadEnvironmentDiagnostic(value: string | undefined) {
const text = trimToDefined(value);
if (!text) {
return false;
}
const primarySignals = [
"当前会话环境从只读改回可写",
"当前会话环境只读",
"不能直接把当前会话环境",
"cwd 我可以在命令里指向",
"文件系统read-only",
];
const secondarySignals = [
"只读权限",
"切回可写",
"不能继续开发",
"不能写文件",
"不能提交",
];
const primaryMatchCount = primarySignals.filter((fragment) => text.includes(fragment)).length;
const secondaryMatchCount = secondarySignals.filter((fragment) => text.includes(fragment)).length;
return primaryMatchCount >= 2 || (primaryMatchCount >= 1 && secondaryMatchCount >= 1);
}
function buildThreadEnvironmentErrorMessage() {
return "THREAD_ENVIRONMENT_INVALID: 线程返回了内部环境提示,已拦截,请检查线程绑定或工作目录。";
}
export function normalizeRemoteExecutionResult(
input: RemoteExecutionResultInput,
): NormalizedRemoteExecutionResult {
const rawThreadReply = trimToDefined(input.rawThreadReply);
const replyBody = trimToDefined(input.replyBody);
const errorMessage = trimToDefined(input.errorMessage);
const hasEnvironmentDiagnostic =
looksLikeThreadEnvironmentDiagnostic(rawThreadReply) ||
looksLikeThreadEnvironmentDiagnostic(replyBody);
if (hasEnvironmentDiagnostic) {
return {
status: "failed",
dispatchExecutionId: trimToDefined(input.dispatchExecutionId),
targetProjectId: trimToDefined(input.targetProjectId),
targetThreadId: trimToDefined(input.targetThreadId),
errorMessage: errorMessage || buildThreadEnvironmentErrorMessage(),
requestId: trimToDefined(input.requestId),
};
}
return {
status: input.status === "failed" ? "failed" : "completed",
dispatchExecutionId: trimToDefined(input.dispatchExecutionId),
targetProjectId: trimToDefined(input.targetProjectId),
targetThreadId: trimToDefined(input.targetThreadId),
rawThreadReply: trimToDefined(input.rawThreadReply),
replyBody: trimToDefined(input.replyBody),
errorMessage: trimToDefined(input.errorMessage),
rawThreadReply,
replyBody,
errorMessage,
requestId: trimToDefined(input.requestId),
};
}