Files
rea/src/sqlite/SqliteDatabaseFailures.ts
T
morluto 5f718ce30e refactor(kernel): give error-message, byte-hex and SHA-256 hex helpers one owner in a new src/kernel layer (#11542)
* refactor(kernel): give error-to-message text one owner in a new src/kernel layer

* fix(kernel): keep the regex worker's own error text inside its isolated source

* refactor(kernel): exclude copies that run in another realm from the error-message guard by name

The regex worker's source string and the page.evaluate fetch in PlaywrightScenarioRequest cannot import errorMessage. The guard now names them, so the counted baseline does not invite a later slice to rewrite them.

* refactor(kernel): migrate the long tail of inline error-message copies to errorMessage (#11552)

Every provider and adapter folder with six or fewer copies now calls errorMessage from src/kernel. Local helpers whose whole body was the copy (failureMessage, gdbErrorMessage, message, describe, reasonOf, recoveryFailureMessage and four local errorMessage definitions) are removed and their call sites call the owner.

Refs #11548

* refactor(kernel): give byte-to-hex formatting one owner in src/kernel (#11560)

byteToHex returns the two lowercase hexadecimal digits of one byte. All eight inline copies (native, contracts, domain firmware and desktop, pcap) call it, keeping their own 0x, % or \x notation, and a counted guard with an empty baseline keeps new copies out.

Refs #11548

* refactor(kernel): give one-shot SHA-256 hex digests one owner in src/kernel (#11562)

sha256Hex digests one in-memory value. A counted guard records the one-shot createHash("sha256").update(x).digest("hex") chains (115 in 101 files) and the first slice migrates domain, contracts and process (35 copies), removing local wrappers (sha256, sha256Text, hash) and the exported sha256Text. Incremental and streamed hashing is not counted.

Refs #11548

* refactor(kernel): migrate the application layer's inline error-message copies to errorMessage (#11563)

All 69 copies in src/application call errorMessage from src/kernel. Local helpers whose whole body was the copy (errorDetail, causeReason and five local errorMessage definitions) are removed and their call sites call the owner.

Refs #11548

* refactor(kernel): migrate the native provider's inline error-message copies to errorMessage (#11567)

All 49 copies in src/native call errorMessage from src/kernel. The local errorReason helpers in LldbCallTracer and LldbRetainedObservations, whose whole body was the copy, are removed and their call sites call the owner.

Refs #11548

* refactor(kernel): migrate the remaining inline error-message copies to errorMessage (#11569)
2026-11-16 07:05:30 +08:00

279 lines
11 KiB
TypeScript

import { capturedProviderOutput } from "../process/capturedProviderOutput.js";
import { MAX_NODE_TIMER_DELAY_MS } from "../domain/analysisResourceLimits.js";
import {
ArtifactReaderFailure,
stableArtifactInputFailure,
readerIntegrityFailure,
ArtifactFileCleanupFailure,
bindArtifactFileCleanup,
} from "../resources/index.js";
import {
projectAnalysisError,
projectAnalysisErrorWithoutCapturedOutput,
} from "../domain/analysisErrorProjection.js";
import {
AnalysisError,
type AnalysisCapturedOutput,
type AnalysisErrorOptions,
} from "../domain/analysisErrorBase.js";
import type { JsonValue } from "../domain/jsonValue.js";
import {
AnalysisCapabilityUnavailableError,
AnalysisOutputError,
AnalysisResourceConstraintError,
AnalysisTimeoutError,
cancelledBy,
carriedReason,
} from "../domain/analysisErrorCore.js";
import { ProviderAdapterError } from "../domain/providerAdapterError.js";
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
import { OwnedCommandFailure } from "../process/OwnedCommand.js";
import type { ProviderProcessSnapshot } from "../process/ProviderProcess.js";
import { providerOutcomeOf } from "../process/ProviderOutcome.js";
import { runtimeExhaustionReport } from "../process/RuntimeExhaustion.js";
import { DEFAULT_SQLITE_WORKER_TIMEOUT_MS } from "../domain/sqlite/sqliteDatabase.js";
import { SQLITE_PROVIDER_IDENTITY } from "../domain/sqlite/SqliteProviderIdentity.js";
import { SQLITE_DATABASE_LIMITS } from "./SqliteDatabaseLimits.js";
import { SqliteSnapshotAcquisitionFailure } from "./SqliteDatabaseSnapshot.js";
import {
classifyProviderFailure,
type ProviderFailureDialect,
} from "../providers/classifyProviderFailure.js";
import { errorMessage } from "../kernel/errorMessage.js";
/** Keep typed cleanup history and one canonical output owner, including after resolution. */
export const createSqliteCleanupError = (
resources: readonly string[],
diagnostics: Readonly<Record<string, JsonValue>>,
options: Omit<AnalysisErrorOptions, "diagnostics">,
): ProviderCleanupError => {
const capturedOutput =
options.capturedOutput ?? options.cleanupPrimary?.capturedOutput;
const previous =
options.cleanupPrimary === undefined
? null
: capturedOutput === undefined
? projectAnalysisError(options.cleanupPrimary)
: projectAnalysisErrorWithoutCapturedOutput(options.cleanupPrimary);
return new ProviderCleanupError(
SQLITE_PROVIDER_IDENTITY.id,
resources,
{
...diagnostics,
previous_error: previous ?? diagnostics.previous_error ?? null,
},
{
...options,
operation: "inspect_sqlite_database",
...(capturedOutput === undefined ? {} : { capturedOutput }),
},
);
};
/** The file-set member a filesystem error names, if it is the database, its WAL or its journal. */
const busyMember = (cause: Error, path: string): string => {
const failed = "path" in cause ? cause.path : undefined;
return typeof failed === "string" &&
[path, `${path}-wal`, `${path}-journal`].includes(failed)
? failed
: path;
};
const OPERATION = "inspect_sqlite_database";
/**
* The SQLite dialect for the shared provider failure classifier: snapshot
* acquisition, the selected database file set, and the owned worker.
*/
export const sqliteDatabaseDialect = (
path: string,
phase: string,
signal?: AbortSignal,
workerTimeoutMs = DEFAULT_SQLITE_WORKER_TIMEOUT_MS,
): ProviderFailureDialect => {
const dialect: ProviderFailureDialect = {
providerId: SQLITE_PROVIDER_IDENTITY.id,
operation: OPERATION,
recognise: (cause) => {
if (cause instanceof SqliteSnapshotAcquisitionFailure)
return classifyProviderFailure(
cause.operationCause,
dialect,
).retainPrimaryDiagnostics({
snapshot_acquisition: {
consistency: "unverified",
members: cause.completedMembers.map((member) => ({ ...member })),
},
});
if (cause instanceof ArtifactReaderFailure) {
if (cause.reason === "io" && cause.cause !== undefined)
return classifyProviderFailure(cause.cause, dialect);
// The shared owner names the integrity check that failed.
if (cause.reason === "integrity")
return readerIntegrityFailure(OPERATION, path, cause);
// The shared owner reports the read's cancellation.
return cause.reason === "cancelled"
? undefined
: stableArtifactInputFailure(cause, OPERATION);
}
if (cause instanceof Error && cause.name === "AbortError")
return cancelledBy(
OPERATION,
carriedReason(
signal?.aborted === true && cause.cause === signal.reason
? signal.reason
: cause,
),
);
if (cause instanceof OwnedCommandFailure)
return ownedCommandFailure(cause, path, workerTimeoutMs);
// Windows reports a sharing violation as EBUSY: another process holds
// the file without read sharing. The unchanged request succeeds once
// that process releases it. Name the member of the file set that was
// busy (the database, its WAL or its journal) when the error says.
if (
phase === "artifact-read" &&
cause instanceof Error &&
"code" in cause &&
cause.code === "EBUSY"
)
return new AnalysisCapabilityUnavailableError(
SQLITE_PROVIDER_IDENTITY.id,
OPERATION,
`Selected database file set is in use by another process (EBUSY): ${busyMember(cause, path)}`,
{
cause,
retryable: true,
userMessage:
"The selected SQLite database is held open by another process without read sharing. Close that application or inspect a copy, then retry.",
},
);
return undefined;
},
// A database file the read could not open names the selected file set.
...(phase === "artifact-read"
? { file: { path, description: "database file set" } }
: {}),
readCleanup: (read, primary) => {
// A plain read failure carries its physical cause beside the cleanup.
const previous =
primary ??
(read instanceof ArtifactFileCleanupFailure || read.cause === undefined
? undefined
: classifyProviderFailure(read.cause, dialect));
const error = createSqliteCleanupError(
read.cleanup.resources,
{ reason: read.cleanup.reason },
{
cause: read,
...(previous === undefined ? {} : { cleanupPrimary: previous }),
},
);
bindArtifactFileCleanup(read, error);
return error;
},
unclassified: (cause) =>
new ProviderAdapterError(SQLITE_PROVIDER_IDENTITY.id, OPERATION, {
cause,
diagnostics: {
phase,
path,
reason: errorMessage(cause),
...(cause instanceof OwnedCommandFailure && cause.snapshot !== null
? {
failure_kind: cause.reason,
exit_code: cause.snapshot.exitCode ?? null,
signal: cause.snapshot.signal ?? null,
captured_output: { ...capturedProviderOutput(cause.snapshot) },
}
: {}),
},
}),
};
return dialect;
};
const ownedCommandFailure = (
cause: OwnedCommandFailure,
selectedPath: string,
workerTimeoutMs: number,
): AnalysisError | undefined => {
const operation = "inspect_sqlite_database";
const capturedOutput =
cause.snapshot === null
? undefined
: capturedProviderOutput(cause.snapshot);
const optionsWithoutCause =
capturedOutput === undefined ? {} : { capturedOutput };
const options = { cause, ...optionsWithoutCause };
// Preserve the same typed primary whether or not owned cleanup also fails.
const timeout =
cause.reason === "timeout"
? new AnalysisTimeoutError(operation, workerTimeoutMs, {
...options,
userMessage: `The SQLite worker reached its ${String(workerTimeoutMs)} ms server/provider deadline (worker_timeout_ms). This is not a whole-inspection or MCP client deadline.`,
remediationAction: `${
workerTimeoutMs < MAX_NODE_TIMER_DELAY_MS
? `Increase worker_timeout_ms above ${String(workerTimeoutMs)} (integer milliseconds, maximum ${String(MAX_NODE_TIMER_DELAY_MS)}), or use rea inspect-sqlite-database <path> --worker-timeout-ms <milliseconds> on the CLI.`
: `worker_timeout_ms is already at the maximum supported ${String(MAX_NODE_TIMER_DELAY_MS)} ms; no higher worker deadline is supported. Report the blocker or narrow the question with caller approval.`
} This per-call setting needs no server restart. It covers worker startup, queries and reply writing, not snapshot acquisition or final reply reads/cleanup. Allow client headroom for the whole call; increasing only the client deadline does not change the worker budget. Keep the selected database/WAL and requested facts; narrow table/columns/schema_scope/row_limit only if the question permits it. Byte and heap safety limits remain fixed. Resolve incomplete cleanup before retrying; do not repeat the unchanged failed call.`,
})
: undefined;
if (cause.cleanupFailure !== null)
return createSqliteCleanupError(
cause.resources,
{
reason: cause.cleanupFailure,
...(timeout === undefined
? {
previous_error: {
failure_kind: cause.reason,
message: cause.message,
},
}
: {}),
},
{
...options,
...(timeout === undefined ? {} : { cleanupPrimary: timeout }),
},
);
if (cause.reason === "cancelled")
return cancelledBy(
operation,
carriedReason(
cause.cancellation === undefined ? cause : cause.cancellation.reason,
),
optionsWithoutCause,
);
if (timeout !== undefined) return timeout;
// The supervisor observed V8's heap report as output streamed, so a
// retention budget or a stack trace that overflowed it cannot hide it.
const outcome = providerOutcomeOf(cause);
if (outcome.kind === "runtime-exhausted")
return new AnalysisResourceConstraintError(
operation,
"memory",
"Owned SQLite worker reported JavaScript heap exhaustion.",
{
javascript_heap_bytes: 256 * 1024 * 1024,
},
{
...options,
diagnostics: {
selected_path: selectedPath,
runtime_exhaustion_report: runtimeExhaustionReport(
outcome.exhaustion,
),
},
},
);
if (cause.reason === "output-limit")
return new AnalysisOutputError(operation, cause.message, {
...options,
userMessage: `SQLite worker diagnostics exceeded the ${String(SQLITE_DATABASE_LIMITS.diagnosticBytes)}-byte capture allowance. This release has no runtime override.`,
remediationAction: `The SQLite diagnostic capture allowance is ${String(SQLITE_DATABASE_LIMITS.diagnosticBytes)} bytes. This release has no runtime override. worker_timeout_ms does not raise it. Repeating the unchanged request fails the same way.`,
});
return undefined;
};