@alphafox/cli 0.3.8 → 0.3.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ export declare function removeCacheRoot(directory: string, env?: NodeJS.ProcessEnv): void;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.removeCacheRoot = removeCacheRoot;
4
+ const node_fs_1 = require("node:fs");
5
+ const paths_1 = require("./paths");
6
+ function removeCacheRoot(directory, env = process.env) {
7
+ (0, paths_1.assertSafeCacheRoot)(directory, env);
8
+ if (!(0, node_fs_1.existsSync)(directory)) {
9
+ return;
10
+ }
11
+ (0, node_fs_1.rmSync)(directory, { recursive: true, force: true });
12
+ }
@@ -0,0 +1,7 @@
1
+ export interface DirectoryUsage {
2
+ readonly path: string;
3
+ readonly exists: boolean;
4
+ readonly bytes: number;
5
+ readonly files: number;
6
+ }
7
+ export declare function inspectDirectory(path: string): DirectoryUsage;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.inspectDirectory = inspectDirectory;
4
+ const node_fs_1 = require("node:fs");
5
+ const node_path_1 = require("node:path");
6
+ function inspectDirectory(path) {
7
+ if (!(0, node_fs_1.existsSync)(path)) {
8
+ return { path, exists: false, bytes: 0, files: 0 };
9
+ }
10
+ const root = (0, node_fs_1.statSync)(path);
11
+ if (root.isFile()) {
12
+ return { path, exists: true, bytes: root.size, files: 1 };
13
+ }
14
+ let bytes = 0;
15
+ let files = 0;
16
+ const walk = (dir) => {
17
+ for (const entry of (0, node_fs_1.readdirSync)(dir, { withFileTypes: true })) {
18
+ const next = (0, node_path_1.join)(dir, entry.name);
19
+ if (entry.isDirectory()) {
20
+ walk(next);
21
+ continue;
22
+ }
23
+ if (entry.isFile()) {
24
+ files += 1;
25
+ bytes += (0, node_fs_1.statSync)(next).size;
26
+ }
27
+ }
28
+ };
29
+ walk(path);
30
+ return { path, exists: true, bytes, files };
31
+ }
@@ -0,0 +1,5 @@
1
+ /** Remind Agents to offer cleanup at or above this tape-cache size. */
2
+ export declare const TAPE_CACHE_REMIND_BYTES: number;
3
+ export declare function resolveTapeCacheDir(env?: NodeJS.ProcessEnv): string;
4
+ export declare function resolveRuntimeCacheRoot(env?: NodeJS.ProcessEnv): string;
5
+ export declare function assertSafeCacheRoot(directory: string, env?: NodeJS.ProcessEnv): void;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TAPE_CACHE_REMIND_BYTES = void 0;
4
+ exports.resolveTapeCacheDir = resolveTapeCacheDir;
5
+ exports.resolveRuntimeCacheRoot = resolveRuntimeCacheRoot;
6
+ exports.assertSafeCacheRoot = assertSafeCacheRoot;
7
+ const node_os_1 = require("node:os");
8
+ const node_path_1 = require("node:path");
9
+ /** Remind Agents to offer cleanup at or above this tape-cache size. */
10
+ exports.TAPE_CACHE_REMIND_BYTES = 512 * 1024 * 1024;
11
+ function resolveTapeCacheDir(env = process.env) {
12
+ const override = env.ALPHAFOX_TAPE_CACHE_DIR?.trim();
13
+ if (override) {
14
+ return (0, node_path_1.resolve)(override);
15
+ }
16
+ return (0, node_path_1.resolve)((0, node_path_1.join)((0, node_os_1.homedir)(), ".alphafox", "cache", "engine-backtest"));
17
+ }
18
+ function resolveRuntimeCacheRoot(env = process.env) {
19
+ const override = env.ALPHAFOX_BACKTEST_RUNTIME_CACHE_DIR?.trim();
20
+ if (override) {
21
+ return (0, node_path_1.resolve)(override);
22
+ }
23
+ const xdg = env.XDG_CACHE_HOME?.trim();
24
+ return (0, node_path_1.resolve)((0, node_path_1.join)(xdg || (0, node_path_1.join)((0, node_os_1.homedir)(), ".cache"), "alphafox", "engine-backtest"));
25
+ }
26
+ function assertSafeCacheRoot(directory, env = process.env) {
27
+ const resolved = (0, node_path_1.resolve)(directory);
28
+ const tape = resolveTapeCacheDir(env);
29
+ const runtime = resolveRuntimeCacheRoot(env);
30
+ if (resolved === tape || resolved === runtime) {
31
+ return;
32
+ }
33
+ if ((0, node_path_1.basename)(resolved) === "engine-backtest") {
34
+ return;
35
+ }
36
+ throw Object.assign(new Error(`Refusing to touch cache directory ${resolved}`), {
37
+ type: "usage",
38
+ subtype: "cache_root_unsafe",
39
+ status: 400,
40
+ });
41
+ }
@@ -0,0 +1,7 @@
1
+ export interface CacheCliFlags {
2
+ readonly format: "json" | "jsonl" | "text";
3
+ readonly yes: boolean;
4
+ readonly dryRun: boolean;
5
+ readonly jq?: string;
6
+ }
7
+ export declare function cmdCache(args: string[], flags: CacheCliFlags, env?: NodeJS.ProcessEnv): Promise<number>;
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cmdCache = cmdCache;
4
+ const envelope_1 = require("../envelope");
5
+ const clean_1 = require("./clean");
6
+ const inspect_1 = require("./inspect");
7
+ const paths_1 = require("./paths");
8
+ async function cmdCache(args, flags, env = process.env) {
9
+ const sub = args[0];
10
+ if (sub === "status" || !sub || sub === "help" || sub === "--help" || sub === "-h") {
11
+ if (sub === "help" || sub === "--help" || sub === "-h") {
12
+ (0, envelope_1.writeSuccess)({
13
+ name: "cache",
14
+ usage: [
15
+ "alphafox cache status",
16
+ "alphafox cache clean [--tape|--runtime|--all] [--yes|--dry-run]",
17
+ ],
18
+ }, { format: flags.format, jq: flags.jq });
19
+ return 0;
20
+ }
21
+ const tape = (0, inspect_1.inspectDirectory)((0, paths_1.resolveTapeCacheDir)(env));
22
+ const runtime = (0, inspect_1.inspectDirectory)((0, paths_1.resolveRuntimeCacheRoot)(env));
23
+ (0, envelope_1.writeSuccess)({
24
+ tape: { ...tape, large: tape.bytes >= paths_1.TAPE_CACHE_REMIND_BYTES },
25
+ runtime,
26
+ remindAfterBytes: paths_1.TAPE_CACHE_REMIND_BYTES,
27
+ totalBytes: tape.bytes + runtime.bytes,
28
+ }, { format: flags.format, jq: flags.jq });
29
+ return 0;
30
+ }
31
+ if (sub === "clean") {
32
+ const rest = args.slice(1);
33
+ const all = rest.includes("--all");
34
+ const runtimeOnly = rest.includes("--runtime");
35
+ const tapeOnly = rest.includes("--tape") || (!all && !runtimeOnly);
36
+ const clearTape = all || tapeOnly;
37
+ const clearRuntime = all || runtimeOnly;
38
+ const tapePath = (0, paths_1.resolveTapeCacheDir)(env);
39
+ const runtimePath = (0, paths_1.resolveRuntimeCacheRoot)(env);
40
+ const tapeBefore = (0, inspect_1.inspectDirectory)(tapePath);
41
+ const runtimeBefore = (0, inspect_1.inspectDirectory)(runtimePath);
42
+ if (flags.dryRun) {
43
+ (0, envelope_1.writeSuccess)({
44
+ dryRun: true,
45
+ cleared: [
46
+ ...(clearTape ? ["tape"] : []),
47
+ ...(clearRuntime ? ["runtime"] : []),
48
+ ],
49
+ tape: tapeBefore,
50
+ runtime: runtimeBefore,
51
+ }, { format: flags.format, jq: flags.jq });
52
+ return 0;
53
+ }
54
+ if (!flags.yes) {
55
+ (0, envelope_1.writeError)({
56
+ type: "confirmation",
57
+ subtype: "yes_required",
58
+ message: "Cleaning local backtest cache requires --yes (or --dry-run).",
59
+ status: 400,
60
+ });
61
+ }
62
+ if (clearTape) {
63
+ (0, clean_1.removeCacheRoot)(tapePath, env);
64
+ }
65
+ if (clearRuntime) {
66
+ (0, clean_1.removeCacheRoot)(runtimePath, env);
67
+ }
68
+ (0, envelope_1.writeSuccess)({
69
+ dryRun: false,
70
+ cleared: [
71
+ ...(clearTape ? ["tape"] : []),
72
+ ...(clearRuntime ? ["runtime"] : []),
73
+ ],
74
+ tape: (0, inspect_1.inspectDirectory)(tapePath),
75
+ runtime: (0, inspect_1.inspectDirectory)(runtimePath),
76
+ bytesFreed: (clearTape ? tapeBefore.bytes : 0) +
77
+ (clearRuntime ? runtimeBefore.bytes : 0),
78
+ }, { format: flags.format, jq: flags.jq });
79
+ return 0;
80
+ }
81
+ (0, envelope_1.writeError)({
82
+ type: "usage",
83
+ message: "Usage: alphafox cache status|clean",
84
+ });
85
+ }