fix: align v02 gateway evidence environment

This commit is contained in:
Codex
2026-06-01 08:26:21 +08:00
parent da8792046d
commit 923fcf6e92
6 changed files with 49 additions and 15 deletions
+5
View File
@@ -30,6 +30,7 @@ test("gateway outbound transport registers session and keeps shell execution dis
cloud.enqueue(shellRequest({ id: "req_disabled_shell", traceId: "trc_disabled_shell", command: "echo should-not-run" }));
const result = await cloud.waitForResult("req_disabled_shell");
assert.equal(result.response.error.data.reason, "cmd_exec_disabled");
assert.equal(result.response.meta.environment, "v02");
assert.equal(result.gatewaySessionId, "gws_gateway_disabled_test");
} finally {
await gateway.stop();
@@ -61,6 +62,10 @@ test("gateway outbound transport executes cloud requests without blocking poll l
assert.equal(quick.response.result.shellExecuted, true, JSON.stringify(quick.response, null, 2));
assert.equal(quick.response.result.exitCode, 0, JSON.stringify(quick.response, null, 2));
assert.equal(quick.response.meta.environment, "v02");
assert.equal(quick.response.result.environment, "v02");
assert.equal(quick.response.result.audit.environment, "v02");
assert.equal(quick.response.result.evidence.environment, "v02");
assert.match(quick.response.result.stdout, /quick-done/u);
assert.equal(slow.response.result.shellExecuted, true, JSON.stringify(slow.response, null, 2));
assert.equal(slow.response.result.exitCode, 0, JSON.stringify(slow.response, null, 2));
+22 -8
View File
@@ -5,7 +5,7 @@ import { spawn } from "node:child_process";
import { createAuditEvent, createEvidenceRecord, operationContext } from "../../internal/sim/l2-runtime.mjs";
import { createGatewayState, createHealth } from "../../internal/sim/model.mjs";
import { createJsonServer, fetchJson, listen, parsePort } from "../../internal/sim/http.mjs";
import { ERROR_CODES, JSON_RPC_VERSION, createRpcErrorBody } from "../../internal/protocol/index.mjs";
import { ENVIRONMENT_DEV, ERROR_CODES, JSON_RPC_VERSION, createRpcErrorBody, isProtocolEnvironment } from "../../internal/protocol/index.mjs";
const gatewayId = process.env.HWLAB_GATEWAY_ID ?? "gateway_dev_stub";
const gatewaySessionId = process.env.HWLAB_GATEWAY_SESSION_ID ?? `gws_${gatewayId}`;
@@ -15,6 +15,7 @@ const pollIntervalMs = parsePositiveInteger(process.env.HWLAB_GATEWAY_POLL_INTER
const commandTimeoutMs = parsePositiveInteger(process.env.HWLAB_GATEWAY_CMD_TIMEOUT_MS, 120000);
const outputLimitBytes = parsePositiveInteger(process.env.HWLAB_GATEWAY_CMD_OUTPUT_LIMIT_BYTES, 65536);
const maxInflightRequests = parsePositiveInteger(process.env.HWLAB_GATEWAY_MAX_INFLIGHT, 3);
const gatewayEnvironment = normalizeEnvironment(process.env.HWLAB_ENVIRONMENT || process.env.HWLAB_GITOPS_PROFILE || ENVIRONMENT_DEV);
const resourceId = process.env.HWLAB_GATEWAY_RESOURCE_ID ?? "res_windows_host";
const boxId = process.env.HWLAB_GATEWAY_BOX_ID ?? "box_windows_host";
const commandExecutionEnabled = truthy(process.env.HWLAB_GATEWAY_CMD_EXEC_ENABLED) || truthy(process.env.HWLAB_GATEWAY_DEMO_OPEN);
@@ -142,6 +143,7 @@ function startCloudRequest(request) {
}
async function handleCloudRequest(request) {
const environment = requestEnvironment(request?.meta);
const context = operationContext({
operationId: request.params?.operationId,
traceId: request.meta?.traceId,
@@ -159,12 +161,12 @@ async function handleCloudRequest(request) {
method: request.method ?? null
});
}
const result = await invokeShell(request.params ?? {}, context);
const result = await invokeShell(request.params ?? {}, context, environment);
response = {
jsonrpc: JSON_RPC_VERSION,
id: request.id,
result,
meta: responseMeta(context.traceId)
meta: responseMeta(context.traceId, environment)
};
} catch (error) {
const body = error?.body ?? createRpcErrorBody({
@@ -176,7 +178,7 @@ async function handleCloudRequest(request) {
jsonrpc: JSON_RPC_VERSION,
id: request.id,
error: body.error,
meta: responseMeta(context.traceId)
meta: responseMeta(context.traceId, environment)
};
}
@@ -185,6 +187,7 @@ async function handleCloudRequest(request) {
async function postGatewayBusyResult(request) {
const traceId = request?.meta?.traceId ?? `trc_gateway_busy_${Date.now()}`;
const environment = requestEnvironment(request?.meta);
const body = createRpcErrorBody({
code: ERROR_CODES.operationRejected,
message: "Gateway is busy; retry after an in-flight command finishes or increase HWLAB_GATEWAY_MAX_INFLIGHT.",
@@ -199,7 +202,7 @@ async function postGatewayBusyResult(request) {
jsonrpc: JSON_RPC_VERSION,
id: request?.id ?? null,
error: body.error,
meta: responseMeta(traceId)
meta: responseMeta(traceId, environment)
});
}
@@ -219,7 +222,7 @@ async function postGatewayResult(response) {
state.outbound.lastResultError = null;
}
async function invokeShell(params, context) {
async function invokeShell(params, context, environment = gatewayEnvironment) {
if (!commandExecutionEnabled) {
throw rpcError(ERROR_CODES.operationRejected, "Gateway command execution is disabled", {
reason: "cmd_exec_disabled",
@@ -248,6 +251,7 @@ async function invokeShell(params, context) {
actorType: "service",
actorId: "svc_hwlab-gateway",
outcome: status === "succeeded" ? "succeeded" : "failed",
environment,
metadata: {
gatewayId,
resourceId: params.resourceId ?? resourceId,
@@ -266,6 +270,7 @@ async function invokeShell(params, context) {
operationId: context.operationId,
traceId: context.traceId,
kind: "trace",
environment,
payload: {
gatewayId,
gatewaySessionId,
@@ -294,6 +299,7 @@ async function invokeShell(params, context) {
accepted: true,
status,
serviceId: "hwlab-gateway",
environment,
gatewayId,
gatewaySessionId,
operationId: context.operationId,
@@ -516,14 +522,22 @@ function normalizeShellInput(params = {}) {
};
}
function responseMeta(traceId) {
function responseMeta(traceId, environment = gatewayEnvironment) {
return {
traceId,
serviceId: "hwlab-gateway",
environment: "dev"
environment: normalizeEnvironment(environment)
};
}
function requestEnvironment(meta = {}) {
return normalizeEnvironment(meta?.environment || gatewayEnvironment);
}
function normalizeEnvironment(environment) {
return isProtocolEnvironment(environment) ? environment : ENVIRONMENT_DEV;
}
function rpcError(code, message, data = {}) {
const error = new Error(message);
error.code = code;