Merge remote-tracking branch 'origin/main'

This commit is contained in:
morluto
2026-10-13 15:12:34 +08:00
47 changed files with 2402 additions and 432 deletions
+27 -18
View File
@@ -36,7 +36,10 @@ export class ArtifactAnalysisResources {
readonly #artifacts = new ArtifactResourceScope();
readonly #pending = new Map<
ArtifactAnalysisRuntime,
ProviderProcessSupervisor | undefined
{
readonly owner: ProviderProcessSupervisor | undefined;
readonly operation: string;
}
>();
#tail: Promise<void> = Promise.resolve();
#busy = false;
@@ -56,14 +59,15 @@ export class ArtifactAnalysisResources {
run<T>(
work: () => Promise<Result<T, AnalysisError>>,
signal?: AbortSignal,
operation = this.operation,
): Promise<Result<T, AnalysisError>> {
if (this.#closed || signal?.aborted)
return Promise.resolve(err(new AnalysisCancelledError(this.operation)));
return Promise.resolve(err(new AnalysisCancelledError(operation)));
if (this.#busy)
return Promise.resolve(
err(
new AnalysisResourceConstraintError(
this.operation,
operation,
"capacity",
this.capacity.message,
this.capacity.details,
@@ -73,7 +77,7 @@ export class ArtifactAnalysisResources {
this.#busy = true;
const running = this.#tail
.then(() => this.#artifacts.run(work))
.catch((cause: unknown) => err(this.#artifactFailure(cause)))
.catch((cause: unknown) => err(this.#artifactFailure(cause, operation)))
.finally(() => {
this.#busy = false;
});
@@ -101,8 +105,9 @@ export class ArtifactAnalysisResources {
retain(
root: ArtifactAnalysisRuntime,
owner?: ProviderProcessSupervisor,
operation = this.operation,
): void {
this.#pending.set(root, owner);
this.#pending.set(root, { owner, operation });
}
get pendingPaths(): readonly string[] {
@@ -110,12 +115,13 @@ export class ArtifactAnalysisResources {
}
async release(): Promise<void> {
for (const [root, owner] of this.#pending) {
for (const [root, retained] of this.#pending) {
try {
const { owner } = retained;
if (owner !== undefined) {
const stopped = await owner.stop();
if (stopped.status === "incomplete") throw new Error(stopped.reason);
this.#pending.set(root, undefined);
this.#pending.set(root, { ...retained, owner: undefined });
}
await root.close();
this.#pending.delete(root);
@@ -124,7 +130,7 @@ export class ArtifactAnalysisResources {
this.providerId,
[root.path],
{ reason: String(cause) },
{ operation: this.operation, cause },
{ operation: retained.operation, cause },
);
}
}
@@ -134,6 +140,7 @@ export class ArtifactAnalysisResources {
observation: NonNullable<ArtifactReaderFailure["cleanup"]>,
cause: unknown,
capturedOutput?: AnalysisCapturedOutput,
operation = this.operation,
): ProviderCleanupError {
return new ProviderCleanupError(
this.providerId,
@@ -144,7 +151,7 @@ export class ArtifactAnalysisResources {
? {}
: { captured_output: { ...capturedOutput } }),
},
{ operation: this.operation, cause },
{ operation, cause },
);
}
@@ -155,6 +162,7 @@ export class ArtifactAnalysisResources {
readonly output_bytes: number;
},
effectiveLimits: LinuxResourceLimits | undefined,
operation = this.operation,
): AnalysisError {
const capturedOutput =
cause.snapshot === null
@@ -171,16 +179,16 @@ export class ArtifactAnalysisResources {
...(capturedOutput === undefined ? {} : { capturedOutput }),
};
if (cause.reason === "cancelled")
return new AnalysisCancelledError(this.operation, detail);
return new AnalysisCancelledError(operation, detail);
if (cause.reason === "timeout")
return new AnalysisTimeoutError(
this.operation,
operation,
limits.command_timeout_ms,
detail,
);
if (cause.reason === "output-limit")
return new AnalysisResourceConstraintError(
this.operation,
operation,
"memory",
cause.message,
{ output_bytes: limits.output_bytes },
@@ -192,18 +200,19 @@ export class ArtifactAnalysisResources {
cause.snapshot?.signal === "SIGXCPU")
)
return new AnalysisResourceConstraintError(
this.operation,
operation,
cause.snapshot.signal === "SIGXFSZ" ? "file-size" : "cpu",
`Producer terminated with ${cause.snapshot.signal}; the signal's exact cause is not independently established.`,
effectiveLimits === undefined ? null : { ...effectiveLimits },
detail,
);
return new AnalysisOutputError(this.operation, cause.message, detail);
return new AnalysisOutputError(operation, cause.message, detail);
}
async finish<T>(
result: Result<T, AnalysisError>,
observation: (value: T) => AnalysisPartialObservation,
operation = this.operation,
): Promise<Result<T, AnalysisError>> {
try {
await this.release();
@@ -223,7 +232,7 @@ export class ArtifactAnalysisResources {
: projectAnalysisError(result.error),
},
{
operation: this.operation,
operation,
cause,
...(partialObservation === undefined ? {} : { partialObservation }),
},
@@ -260,11 +269,11 @@ export class ArtifactAnalysisResources {
if (descriptorFailure !== undefined) throw descriptorFailure;
}
#artifactFailure(cause: unknown): AnalysisError {
#artifactFailure(cause: unknown, operation = this.operation): AnalysisError {
if (cause instanceof AnalysisError) return cause;
if (cause instanceof ArtifactReaderFailure && cause.cleanup !== undefined)
return this.cleanupFailure(cause.cleanup, cause);
return new ProviderAdapterError(this.providerId, this.operation, {
return this.cleanupFailure(cause.cleanup, cause, undefined, operation);
return new ProviderAdapterError(this.providerId, operation, {
cause,
diagnostics: { stage: "artifact-resources", reason: String(cause) },
});
@@ -266,3 +266,47 @@ it("normalizes representative process identities and preserves null, zero, and u
"<working-directory>/program child 42020 parent 41010 group 43030 session 44040 listen:<port> selected",
);
});
it("normalizes nested paths once using the most specific selected identity", () => {
const declared = parseProcessScenario({
executable: "/workspace/project/bin/program",
working_directory: "/workspace/project",
filesystem_observation_paths: [
"/workspace/project/data",
"/workspace/project/data/cache",
],
normalization: { paths: true, pids: false, ports: false, patterns: [] },
});
expect(
normalizeProcessText(
"/workspace/project/bin/program /workspace/project/data/cache/item /workspace/project/data/result /workspace/project/log /workspace/tmp",
declared,
"/workspace",
rootPid,
),
).toBe(
"<executable> <filesystem-root-1>/item <filesystem-root-0>/result <working-directory>/log <temporary-root>/tmp",
);
});
it("treats path metacharacters literally while preferring nested observed roots", () => {
const declared = parseProcessScenario({
executable: "/selected/[tool]+",
working_directory: "/selected",
filesystem_observation_paths: [
"/selected/(data)",
"/selected/(data)/cache",
],
normalization: { paths: true, pids: false, ports: false, patterns: [] },
});
expect(
normalizeProcessText(
"/selected/[tool]+ /selected/(data)/cache/item /selected/(data)/file /selected/log /selected/toolll",
declared,
"/tmp",
rootPid,
),
).toBe(
"<executable> <filesystem-root-1>/item <filesystem-root-0>/file <working-directory>/log <working-directory>/toolll",
);
});
+35 -13
View File
@@ -17,19 +17,8 @@ export const normalizeProcessText = (
pid: number,
): string => {
let normalized = value;
if (scenario.normalization.paths) {
normalized = normalized.replaceAll(temporaryRoot, "<temporary-root>");
normalized = normalized.replaceAll(
scenario.working_directory,
"<working-directory>",
);
normalized = normalized.replaceAll(scenario.executable, "<executable>");
for (const [index, root] of scenario.filesystem_observation_paths.entries())
normalized = normalized.replaceAll(
root,
`<filesystem-root-${String(index)}>`,
);
}
if (scenario.normalization.paths)
normalized = normalizePathTokens(normalized, scenario, temporaryRoot);
if (scenario.normalization.ports)
normalized = normalizePortTokens(normalized);
if (scenario.normalization.pids)
@@ -44,6 +33,39 @@ export const normalizeProcessText = (
return normalized;
};
/** Match original paths once, preferring the most specific selected identity. */
const normalizePathTokens = (
value: string,
scenario: ProcessScenario,
temporaryRoot: string,
): string => {
const aliases = new Map<string, string>();
const selected: readonly (readonly [string, string])[] = [
[temporaryRoot, "<temporary-root>"],
[scenario.working_directory, "<working-directory>"],
[scenario.executable, "<executable>"],
...scenario.filesystem_observation_paths.map(
(root, index): readonly [string, string] => [
root,
`<filesystem-root-${String(index)}>`,
],
),
];
for (const [path, alias] of selected)
if (path !== "" && !aliases.has(path)) aliases.set(path, alias);
const paths = [...aliases.keys()].sort(
(left, right) => right.length - left.length,
);
if (paths.length === 0) return value;
const pattern = paths
.map((path) => path.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&"))
.join("|");
return value.replace(
new RegExp(pattern, "gu"),
(path) => aliases.get(path) ?? path,
);
};
// A port may end a sentence, but a period followed by a letter, digit, or
// underscore in any script continues a decimal, version, or host name and is
// not a port boundary.