mirror of
https://github.com/morluto/rea.git
synced 2026-12-05 19:51:20 -05:00
ab84ba6839
* fix(process): preserve producer-owned inspection cancellation Prototype the explicit inspection origin and retained file/batch owner protocol. Downstream adapter migration and lease binding remain required before integration. * fix(process): type inspection ownership handoffs precisely * fix(process): retain plain inspection preparation cancellation * fix(process): retain explicit inspection cleanup and cancellation facts * fix(process): require explicit inspection facts and cleanup scope * refactor(process): remove redundant cleanup forwarding wrapper * fix(firmware): retain inspection cancellation and cleanup ownership * fix(wakaru): retain typed inspection outcomes and caller scope * fix(ghidra): preserve inspection facts and launch cleanup scope * fix(providers): preserve typed process inspection outcomes * refactor(process): remove obsolete inspection cancellation re-export * fix(native): preserve inspection preparation cleanup and cancellation * fix(registration): retain typed inspection cleanup during Git discovery * fix(process): preserve inspection deadline origin in adapter handoff * fix(process): consume the canonical inspection selector in binding * fix(native): preserve inspection deadlines and cleanup retry completion * test(process): narrow the retained Darwin preparation owner * fix(providers): preserve preparation ownership and timeout policy Narrow preparation facts through their canonical owner, preserve actual cleanup capabilities and recorded cancellation, and keep enclosing command deadlines authoritative. Retain raw inspection records through Java refusal and configuration projections without changing availability fallback policy. * refactor(process): use one inspection failure type owner * fix(providers): retain recorded secondary failures when rebinding * fix(ghidra): preserve normalized inspection facts through public projection * fix(process): normalize native process listing cancellation * docs(development): clarify failure provenance and cleanup verification * fix(process): preserve direct inspection cancellation facts * fix(process): retain inspection owners through termination * fix(process): preserve command inspection cleanup provenance * fix(desktop): preserve inspection facts after cleanup retry * refactor(process)!: migrate desktop capture with inspection producers * fix(desktop)!: preserve capture failure origins and cleanup owners (#10115) * fix(desktop): preserve capture failure origins and cleanup * fix(desktop): type keyring cleanup cancellation * fix(kuna)!: retain copy cleanup before retiring workspaces (#10114) * fix(kuna): own copy cancellation and descriptor cleanup * fix(kuna): close copy descriptors before workspace removal * test(kuna): type copy lifecycle gates for project library * docs(kuna): explain retained copy-file cleanup and staging ownership * docs(kuna): separate cache publication, integrity and cleanup guidance * fix(desktop): retain sampling inspection owners before release * fix(process): preserve capture file read cancellation origin * fix(process): retain capture inspection cancellation and cleanup owners * fix(process): retain sampling inspection failures in capture scope * fix(process): retain inspection failures through mandatory cleanup * fix(process): distinguish sampler shutdown from capture cancellation * fix(browser): retain startup EOF before later cancellation * fix(process): bind capture inspection cleanup to the owning run * test(process): migrate cancellation fixtures to typed owners Record explicit preparation and command cancellation facts, retain exact opaque reasons and physical causes, and guard the mocked version probe from native preparation. Keep the live Wakaru command control while adding a recording equivalent for bounded source verification. * fix(process): own sampling reads and settle inspection peers * fix(errors): retain authored recovery through cleanup composition * fix(process): retain all sampling inspection leaves for final cleanup * test(browser): report recording cleanup signal outcome * fix(process): keep inspection error context out of implicit JSON * fix(desktop): retain normalized sampling cleanup ownership * test(process): assert tagged startup cancellation
169 lines
5.3 KiB
TypeScript
169 lines
5.3 KiB
TypeScript
import { assertArtifactReadActive } from "../artifacts/ArtifactFileOperation.js";
|
|
import type { BigIntStats } from "node:fs";
|
|
import { lstat, readdir } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
|
|
import type {
|
|
DesktopPathState,
|
|
DesktopWalk,
|
|
} from "../domain/desktop/desktopStateDiff.js";
|
|
import { isWithinAny } from "../domain/desktop/desktopStateDiff.js";
|
|
|
|
const LSTAT_CONCURRENCY = 64;
|
|
|
|
/** One stat walk of the observed roots. */
|
|
export interface HomeStateWalk extends DesktopWalk {
|
|
readonly truncated: boolean;
|
|
readonly unreadableDirectories: number;
|
|
/** Paths whose state could not be read, and directories on another device left unentered. */
|
|
readonly uncheckedPaths: number;
|
|
/** Directories on another device than their walk root, recorded but not entered. */
|
|
readonly mountPoints: readonly string[];
|
|
}
|
|
|
|
/** A directory still to list, with the device of the root it was reached from. */
|
|
interface PendingDirectory {
|
|
readonly path: string;
|
|
readonly device: bigint;
|
|
}
|
|
|
|
const pathState = (stats: BigIntStats): DesktopPathState => ({
|
|
type: stats.isFile()
|
|
? "file"
|
|
: stats.isDirectory()
|
|
? "directory"
|
|
: stats.isSymbolicLink()
|
|
? "symlink"
|
|
: "other",
|
|
size: Number(stats.size),
|
|
mode: Number(stats.mode & 0o7777n),
|
|
mtime_ns: stats.mtimeNs.toString(),
|
|
inode: `${stats.dev.toString()}:${stats.ino.toString()}`,
|
|
});
|
|
|
|
/** A path's metadata, null when it is absent, undefined when that is unknown. */
|
|
const statPath = async (
|
|
path: string,
|
|
): Promise<BigIntStats | null | undefined> => {
|
|
try {
|
|
return await lstat(path, { bigint: true });
|
|
} catch (cause: unknown) {
|
|
return (cause as NodeJS.ErrnoException).code === "ENOENT"
|
|
? null
|
|
: undefined;
|
|
}
|
|
};
|
|
|
|
interface WalkProgress {
|
|
readonly entries: Map<string, DesktopPathState>;
|
|
readonly pending: PendingDirectory[];
|
|
readonly mountPoints: string[];
|
|
readonly maxEntries: number;
|
|
truncated: boolean;
|
|
unreadablePaths: number;
|
|
}
|
|
|
|
/** Record one directory's entries; true when every one of them was recorded. */
|
|
const listDirectory = async (
|
|
directory: PendingDirectory,
|
|
names: readonly string[],
|
|
excluded: readonly string[],
|
|
progress: WalkProgress,
|
|
): Promise<boolean> => {
|
|
const paths = names
|
|
.map((name) => join(directory.path, name))
|
|
.filter((path) => !isWithinAny(path, excluded));
|
|
let complete = true;
|
|
for (let offset = 0; offset < paths.length; offset += LSTAT_CONCURRENCY) {
|
|
const batch = paths.slice(offset, offset + LSTAT_CONCURRENCY);
|
|
const stats = await Promise.all(batch.map(statPath));
|
|
for (const [index, stat] of stats.entries()) {
|
|
const path = batch[index];
|
|
if (path === undefined || stat === null) continue;
|
|
if (stat === undefined) {
|
|
progress.unreadablePaths += 1;
|
|
complete = false;
|
|
continue;
|
|
}
|
|
if (progress.entries.size >= progress.maxEntries) {
|
|
progress.truncated = true;
|
|
return false;
|
|
}
|
|
progress.entries.set(path, pathState(stat));
|
|
if (!stat.isDirectory()) continue;
|
|
// A network or FUSE mount can be slow or hang, so the walk stays on its root's device.
|
|
if (stat.dev === directory.device)
|
|
progress.pending.push({ path, device: directory.device });
|
|
else progress.mountPoints.push(path);
|
|
}
|
|
}
|
|
return complete;
|
|
};
|
|
|
|
/**
|
|
* Record every path under the roots without following symbolic links or
|
|
* entering a directory on another device than its root. The walk stops adding
|
|
* entries at `maxEntries` and reports itself truncated. A directory counts as
|
|
* listed only when every one of its entries was recorded.
|
|
*/
|
|
export const walkHomeState = async (
|
|
roots: readonly string[],
|
|
excluded: readonly string[],
|
|
maxEntries: number,
|
|
signal?: AbortSignal,
|
|
): Promise<HomeStateWalk> => {
|
|
const progress: WalkProgress = {
|
|
entries: new Map(),
|
|
pending: [],
|
|
mountPoints: [],
|
|
maxEntries,
|
|
truncated: false,
|
|
unreadablePaths: 0,
|
|
};
|
|
const listed = new Set<string>();
|
|
const checkedRoots = new Set<string>();
|
|
let unreadableDirectories = 0;
|
|
for (const root of roots) {
|
|
if (isWithinAny(root, excluded)) continue;
|
|
const stats = await statPath(root);
|
|
if (stats === undefined) {
|
|
progress.unreadablePaths += 1;
|
|
continue;
|
|
}
|
|
if (stats !== null) {
|
|
// A root counts toward the limit like any entry; one left out is unchecked.
|
|
if (progress.entries.size >= maxEntries) {
|
|
progress.truncated = true;
|
|
continue;
|
|
}
|
|
progress.entries.set(root, pathState(stats));
|
|
if (stats.isDirectory())
|
|
progress.pending.push({ path: root, device: stats.dev });
|
|
}
|
|
checkedRoots.add(root);
|
|
}
|
|
while (progress.pending.length > 0 && !progress.truncated) {
|
|
assertArtifactReadActive(signal);
|
|
const directory = progress.pending.pop();
|
|
if (directory === undefined) break;
|
|
let names: string[];
|
|
try {
|
|
names = await readdir(directory.path);
|
|
} catch {
|
|
unreadableDirectories += 1;
|
|
continue;
|
|
}
|
|
if (await listDirectory(directory, names, excluded, progress))
|
|
listed.add(directory.path);
|
|
}
|
|
return {
|
|
entries: progress.entries,
|
|
listed,
|
|
roots: checkedRoots,
|
|
truncated: progress.truncated,
|
|
unreadableDirectories,
|
|
uncheckedPaths: progress.unreadablePaths + progress.mountPoints.length,
|
|
mountPoints: progress.mountPoints,
|
|
};
|
|
};
|