fix: 收敛 trans 远端文本短路径

This commit is contained in:
Codex
2026-07-13 05:20:27 +02:00
parent 18937da9db
commit c04983455e
16 changed files with 498 additions and 70 deletions
+28 -7
View File
@@ -234,6 +234,7 @@ export function sshStdoutTruncationHint(options: {
}): SshStdoutTruncationHint {
const stream = options.stream ?? "stdout";
const policy = readCliOutputPolicy();
const dumpCreated = options.dumpPath !== null;
return {
code: stream === "stdout" ? "ssh-stdout-truncated" : "ssh-stderr-truncated",
level: "warning",
@@ -248,18 +249,26 @@ export function sshStdoutTruncationHint(options: {
dumpPath: options.dumpPath,
dumpError: options.dumpError ?? null,
disclosurePolicy: {
name: "unified-cli-dump-preview",
name: "bounded-stream-explicit-dump",
configPath: policy.configPath,
maxPreviewBytes: policy.maxStdoutBytes,
dumpDir: policy.dumpDir,
defaultCreatesDump: false,
dumpCreated,
},
recommendedRerun: [
"Use rg -m/--max-count, sed -n, head, tail, --limit, --tail-bytes, or an id-specific query.",
"Use --full, --raw, --tail-bytes, --limit, or UNIDESK_*_STREAM_MAX_BYTES only when you intentionally need a wider one-off disclosure.",
"Read the dumpPath for the complete captured stream.",
...(dumpCreated
? ["Read dumpPath only because explicit stream dump capture was requested."]
: ["Default truncation creates no dump; set UNIDESK_TRAN_STREAM_DUMP=1 only for an explicit one-off complete capture."]),
],
message: `ssh ${stream} exceeded the YAML-configured preview budget; ${stream} is bounded and the complete stream is written to a local dump when possible.`,
action: "Inspect dumpPath for the complete stream, or rerun a narrower remote command instead of emitting full logs, huge JSON, or broad search output.",
message: dumpCreated
? `ssh ${stream} exceeded the YAML-configured preview budget; explicit capture wrote the complete stream to a protected local dump.`
: `ssh ${stream} exceeded the YAML-configured preview budget; output is bounded and no temporary dump was created by default.`,
action: dumpCreated
? "Inspect dumpPath for this explicit one-off capture, then prefer a narrower remote command next time."
: "Rerun with cat/head/tail/rg, --limit, --tail-bytes, or an id-specific query instead of recovering through a temporary dump.",
note: "This hint is written to stderr and intentionally does not echo the original remote command.",
};
}
@@ -294,8 +303,10 @@ export function sshTruncationCompletionSummary(options: {
commandOmitted: true,
stdout: options.stdout,
stderr: options.stderr,
message: "SSH output was truncated, but the command has finished; use this completion summary for closeout status and inspect dumpPath only when full output is needed.",
action: "Use exitCode/timedOut plus dumpPath from this summary instead of manually inferring success from a long truncated stream.",
message: "SSH output was truncated, but the command has finished; use exitCode/timedOut for closeout and rerun a semantic narrow query when more evidence is needed.",
action: options.stdout?.dumpPath != null || options.stderr?.dumpPath != null
? "An explicit stream dump was requested; inspect its dumpPath only when the complete one-off payload is required."
: "No dump was created by default; use cat/head/tail/rg, --limit, --tail-bytes, or an id-specific query.",
};
}
@@ -378,6 +389,7 @@ export function createSshStdoutForwarder(options: {
transport: SshStdoutTruncationHint["transport"];
maxBytes?: number;
stdout?: NodeJS.WritableStream;
persistDump?: boolean;
}): SshStreamForwarder {
return createSshStreamForwarder({
invocation: options.invocation,
@@ -385,6 +397,7 @@ export function createSshStdoutForwarder(options: {
stream: "stdout",
maxBytes: options.maxBytes ?? sshStdoutStreamMaxBytes(),
target: options.stdout ?? process.stdout,
persistDump: options.persistDump ?? sshStreamDumpExplicitlyRequested(),
});
}
@@ -393,6 +406,7 @@ export function createSshStderrForwarder(options: {
transport: SshStdoutTruncationHint["transport"];
maxBytes?: number;
stderr?: NodeJS.WritableStream;
persistDump?: boolean;
}): SshStreamForwarder {
return createSshStreamForwarder({
invocation: options.invocation,
@@ -400,6 +414,7 @@ export function createSshStderrForwarder(options: {
stream: "stderr",
maxBytes: options.maxBytes ?? sshStderrStreamMaxBytes(),
target: options.stderr ?? process.stderr,
persistDump: options.persistDump ?? sshStreamDumpExplicitlyRequested(),
});
}
@@ -409,6 +424,7 @@ function createSshStreamForwarder(options: {
stream: SshStdoutTruncationHint["stream"];
maxBytes: number;
target: NodeJS.WritableStream;
persistDump: boolean;
}): SshStreamForwarder {
let observedBytes = 0;
let forwardedBytes = 0;
@@ -419,6 +435,7 @@ function createSshStreamForwarder(options: {
const bufferedChunks: Buffer[] = [];
const appendDump = (chunk: Buffer): void => {
if (!options.persistDump) return;
if (dumpError !== null) return;
try {
if (dumpPath === null) {
@@ -438,7 +455,7 @@ function createSshStreamForwarder(options: {
write(chunk: Buffer): string | null {
observedBytes += chunk.length;
if (!truncated && observedBytes <= options.maxBytes) {
bufferedChunks.push(Buffer.from(chunk));
if (options.persistDump) bufferedChunks.push(Buffer.from(chunk));
options.target.write(chunk);
forwardedBytes += chunk.length;
return null;
@@ -483,6 +500,10 @@ function createSshStreamForwarder(options: {
};
}
export function sshStreamDumpExplicitlyRequested(env: NodeJS.ProcessEnv = process.env): boolean {
return env.UNIDESK_TRAN_STREAM_DUMP === "1";
}
function brokerSource(): string {
return String.raw`
const open = JSON.parse(process.argv[2] || process.argv[1] || "{}");