fix code agent gateway trace timeouts

This commit is contained in:
Codex
2026-05-25 01:14:17 +00:00
parent b2a26d4c81
commit b3d298c3e5
14 changed files with 329 additions and 25 deletions
+33 -4
View File
@@ -9,7 +9,21 @@ const DEFAULT_PROJECT_ID = "prj_mvp_topology";
const DEFAULT_GATEWAY_SESSION_ID = "gws_DESKTOP-1MHOD9I";
const DEFAULT_RESOURCE_ID = "res_windows_host";
const DEFAULT_CAPABILITY_ID = "cap_windows_cmd_exec";
const POWERSHELL_PROLOGUE = "[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false); $OutputEncoding = [Console]::OutputEncoding;";
const DEFAULT_GATEWAY_SHELL_TIMEOUT_MS = 120000;
const MAX_GATEWAY_SHELL_TIMEOUT_MS = 600000;
const REQUEST_TIMEOUT_GRACE_MS = 10000;
const POWERSHELL_PROLOGUE = [
"$ErrorActionPreference = 'Stop';",
"$utf8NoBom = [System.Text.UTF8Encoding]::new($false);",
"[Console]::InputEncoding = $utf8NoBom;",
"[Console]::OutputEncoding = $utf8NoBom;",
"$OutputEncoding = $utf8NoBom;",
"$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8';",
"$PSDefaultParameterValues['Set-Content:Encoding'] = 'utf8';",
"function Read-HwlabText { param([Parameter(Mandatory=$true)][string]$LiteralPath) [System.IO.File]::ReadAllText($LiteralPath, $utf8NoBom) }",
"function Select-HwlabText { param([Parameter(Mandatory=$true)][string]$LiteralPath,[Parameter(Mandatory=$true)][string]$Pattern,[int]$First=20) Select-String -LiteralPath $LiteralPath -Pattern $Pattern -Encoding UTF8 | Select-Object -First $First @{Name='path';Expression={$_.Path}}, @{Name='lineNumber';Expression={$_.LineNumber}}, @{Name='line';Expression={$_.Line}} }",
"function ConvertTo-HwlabJson { param([Parameter(ValueFromPipeline=$true)][object]$InputObject,[int]$Depth=8) begin { $items = @() } process { $items += $InputObject } end { if ($items.Count -eq 1) { $items[0] | ConvertTo-Json -Compress -Depth $Depth } else { $items | ConvertTo-Json -Compress -Depth $Depth } } }"
].join("\n");
async function main() {
const args = parseArgs(process.argv.slice(2));
@@ -22,10 +36,17 @@ async function main() {
const apiBaseUrl = stripTrailingSlash(args.apiBaseUrl || process.env.HWLAB_GATEWAY_SHELL_API_BASE_URL || DEFAULT_API_BASE_URL);
const traceId = args.traceId || `trc_gateway_shell_cli_${Date.now()}`;
const requestId = args.requestId || `req_gateway_shell_cli_${randomUUID()}`;
const timeoutMs = positiveInteger(args.timeoutMs, 30000);
const timeoutMs = boundedPositiveInteger(args.timeoutMs ?? process.env.HWLAB_GATEWAY_SHELL_TIMEOUT_MS, DEFAULT_GATEWAY_SHELL_TIMEOUT_MS, {
min: 1000,
max: MAX_GATEWAY_SHELL_TIMEOUT_MS
});
const requestTimeoutMs = boundedPositiveInteger(args.requestTimeoutMs, timeoutMs + REQUEST_TIMEOUT_GRACE_MS, {
min: timeoutMs,
max: timeoutMs + Math.max(REQUEST_TIMEOUT_GRACE_MS, Math.floor(timeoutMs / 2))
});
const response = await requestJson(`${apiBaseUrl}/v1/rpc/hardware.invoke.shell`, {
method: "POST",
timeoutMs,
timeoutMs: requestTimeoutMs,
headers: {
"content-type": "application/json",
"x-trace-id": traceId,
@@ -100,7 +121,8 @@ function printHelp() {
" --powershell-stdin Read PS1 from stdin, encode it, and run through the gateway",
" --powershell-exe NAME Default powershell.exe",
" --cwd WINDOWS_PATH Gateway-side working directory; prefer this over shell-level cd &&",
" --timeout-ms N Short dispatch timeout in milliseconds",
" --timeout-ms N Gateway command timeout, default 120000ms; can also use HWLAB_GATEWAY_SHELL_TIMEOUT_MS",
" --request-timeout-ms N HTTP wait timeout; defaults to command timeout plus a small response grace",
" --project-id ID Default prj_mvp_topology",
" --gateway-session-id ID Default gws_DESKTOP-1MHOD9I",
" --resource-id ID Default res_windows_host",
@@ -205,6 +227,13 @@ function positiveInteger(value, fallback) {
return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback;
}
function boundedPositiveInteger(value, fallback, { min, max } = {}) {
const parsed = positiveInteger(value, fallback);
const lower = Number.isInteger(min) ? min : 1;
const upper = Number.isInteger(max) ? max : parsed;
return Math.min(Math.max(parsed, lower), upper);
}
function stripTrailingSlash(value) {
return String(value).replace(/\/+$/u, "");
}