Implementation:20000419 Fauxnix Output Normalization
| Knowledge Sources | |
|---|---|
| Domains | Encoding, Output_Normalization, Error_Rewriting |
| Last Updated | 2026-10-26 00:00 GMT |
Overview
Concrete set of encoding and error-normalization utilities that decode native tool output (UTF-8/GBK), clip text to caller byte budgets at UTF-8 boundaries, and rewrite PowerShell error messages into bash-style GNU error text.
Description
The Output Normalization module aggregates three pieces of the fauxnix pipeline's final stage:
- decodeOutput (src/encoding.ts): decodes raw Buffer output from the PowerShell host. In UTF-8 mode it sniffs strictly first (genuine UTF-8 never falls back) and only falls back to GBK when the bytes are not valid UTF-8. In GBK mode (FAUXNIX_NATIVE_ENCODING=ansi) it trusts the setting for GBK-native admin tools.
- clipUtf8 (src/encoding.ts): keeps the longest whole-codepoint prefix within a caller's UTF-8 byte budget, never splitting a surrogate pair. Uses a fast backward-trim path when the overshoot is small and a forward iterator otherwise.
- normalizeStderr (src/errors.ts): strips PowerShell-specific noise lines (CategoryInfo, FullyQualifiedErrorId, CLIXML wrappers), unwraps CLIXML-serialized error records, and rewrites common PowerShell error patterns — in both en-US and zh-CN locales — into the bash/GNU style agents expect (command not found, No such file or directory, Permission denied).
Usage
These functions are invoked inside runPlans (src/executor.ts) during and after PowerShell host execution. decodeOutput converts captured host buffers; clipUtf8 enforces per-fd byte budgets on caller-visible output; normalizeStderr rewrites the stderr stream before it reaches the agent. encodeCommand (UTF-16LE base64 for -EncodedCommand) and resolveNativePref are also part of this surface.
Code Reference
Source Location
- Repository: fauxnix
- File: src/encoding.ts (L24-105), src/errors.ts (L73-148)
- Lines: decodeOutput L24-43; clipUtf8 L68-105; normalizeStderr L73-148; encodeCommand L63-65; resolveNativePref L15-17
Signature
export type NativeEncodingPref = 'utf8' | 'gbk';
export function resolveNativePref(): NativeEncodingPref;
export function decodeOutput(buf: Buffer, prefer?: NativeEncodingPref): string;
export function clipUtf8(text: string, limit: number): { text: string; truncated: boolean };
export function encodeCommand(script: string): string;
// src/errors.ts
export function normalizeStderr(stderr: string): string;
Import
import { decodeOutput, clipUtf8, encodeCommand, resolveNativePref } from './encoding.js';
import { normalizeStderr } from './errors.js';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| buf | Buffer | Yes (decodeOutput) | Raw host bytes |
| prefer | 'gbk' | No (decodeOutput) | Encoding preference from resolveNativePref() |
| text | string | Yes (clipUtf8) | Decoded output text |
| limit | number | Yes (clipUtf8) | Caller byte budget |
| stderr | string | Yes (normalizeStderr) | Raw PowerShell stderr text |
Outputs
| Name | Type | Description |
|---|---|---|
| decodeOutput returns | string | Decoded string (BOM stripped) |
| clipUtf8 returns | { text, truncated } | Clipped text plus a truncation flag |
| normalizeStderr returns | string | bash-style error text |
Usage Examples
Decode and Clip Host Output
const buf = Buffer.from(hostStdoutBase64, 'base64');
const text = decodeOutput(buf, resolveNativePref());
const { text: clipped, truncated } = clipUtf8(text, 262144);
Normalize a PowerShell Error
const ps = "cat : Cannot find path 'D:\\missing' because it does not exist.";
const bashStyle = normalizeStderr(ps);
// → "cat: D:/missing: No such file or directory"
Encode a Probe Command
const encoded = encodeCommand('[Console]::Out.WriteLine("hi")');
// UTF-16LE base64 for PowerShell -EncodedCommand
Related Pages
Implements Principle
- Principle:20000419_Fauxnix_Output_Encoding_And_Clipping
- Principle:20000419_Fauxnix_Error_Normalization