@reticlehq/terminal 0.0.0-stage → 3.11.0

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,10 @@
1
+ import { EXCLUDED_BY_DEFAULT, takeSnapshot } from './snapshot.js';
2
+ export { ChangeKind, diffSnapshots, takeSnapshot, EXCLUDED_BY_DEFAULT } from './snapshot.js';
3
+ /** The port over a real filesystem. */
4
+ export function nodeWorkspace(roots, options = {}) {
5
+ return {
6
+ roots,
7
+ excluded: options.exclude ?? EXCLUDED_BY_DEFAULT,
8
+ snapshot: () => takeSnapshot(roots, options),
9
+ };
10
+ }
@@ -0,0 +1,73 @@
1
+ /**
2
+ * What was on disk, twice, and what moved between.
3
+ *
4
+ * This is the whole of the artifact channel's observation, and its honesty rests on one idea: the
5
+ * filesystem is asked what is there, rather than the tool being asked what it did. A tool can
6
+ * believe it wrote a file and be wrong; it cannot make `lstat` agree with it.
7
+ *
8
+ * Deliberately a BEFORE and AFTER pair rather than a watcher. A recursive watch was tried and
9
+ * rejected: on macOS a write-to-temp-then-rename emits two rename events with no create and no
10
+ * change, and a deletion is indistinguishable from a creation, so every anomaly built on it fires
11
+ * on the atomic-write pattern that most careful tools use. The pair sees net effect only, which is
12
+ * less, and is true.
13
+ */
14
+ /** What is known about one path. A value object: two of these compare by their fields. */
15
+ export interface FileFact {
16
+ /** Content hash, absent for a thing that has no content: a symlink, a directory entry. */
17
+ readonly hash: string | undefined;
18
+ readonly size: number;
19
+ /** Permission bits only. A mode change leaves every byte identical and is still a change. */
20
+ readonly mode: number;
21
+ /** Where a symlink points. Absent for a regular file. */
22
+ readonly linkTo: string | undefined;
23
+ }
24
+ export interface Snapshot {
25
+ readonly files: ReadonlyMap<string, FileFact>;
26
+ /**
27
+ * Roots that could not be read.
28
+ *
29
+ * Reported rather than treated as empty. Silently reading a missing directory as an empty one
30
+ * makes every `absent` claim over it true and every `present` claim false, for a reason that has
31
+ * nothing to do with the subject -- which is the protocol's own distinction between "nothing was
32
+ * watching" and "it did not happen", arriving as a filesystem error.
33
+ */
34
+ readonly unreadable: readonly string[];
35
+ }
36
+ export declare const ChangeKind: {
37
+ readonly WRITTEN: "written";
38
+ readonly DELETED: "deleted";
39
+ readonly MODIFIED: "modified";
40
+ /** The bytes are identical and what the world may do with them is not. */
41
+ readonly MODE_CHANGED: "mode-changed";
42
+ };
43
+ export type ChangeKind = (typeof ChangeKind)[keyof typeof ChangeKind];
44
+ export interface Change {
45
+ readonly path: string;
46
+ readonly kind: ChangeKind;
47
+ readonly before: FileFact | undefined;
48
+ readonly after: FileFact | undefined;
49
+ }
50
+ /**
51
+ * Directories whose contents are somebody else's, and are hashed at real cost.
52
+ *
53
+ * Measured rather than assumed: hashing one `node_modules` ran to tens of thousands of files and
54
+ * more than a gigabyte, and every window takes TWO snapshots. Excluded by default and overridable,
55
+ * because a tool whose job is to write `dist/` needs `dist/` watched.
56
+ *
57
+ * The exclusion is reported as coverage, never hidden. An empty blind-spot list is a positive
58
+ * claim that nothing was hidden.
59
+ */
60
+ export declare const EXCLUDED_BY_DEFAULT: readonly string[];
61
+ export interface SnapshotOptions {
62
+ /** Directory names skipped wherever they appear. Defaults to `EXCLUDED_BY_DEFAULT`. */
63
+ readonly exclude?: readonly string[];
64
+ }
65
+ /** What is under these roots right now. */
66
+ export declare function takeSnapshot(roots: readonly string[], options?: SnapshotOptions): Snapshot;
67
+ /**
68
+ * What moved between two snapshots.
69
+ *
70
+ * Pure: two snapshots in, a list out, no filesystem and no clock. That is what makes the
71
+ * interesting comparisons testable without arranging for them on a real disk.
72
+ */
73
+ export declare function diffSnapshots(before: Snapshot, after: Snapshot): readonly Change[];
@@ -0,0 +1,145 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { lstatSync, readFileSync, readdirSync, readlinkSync } from 'node:fs';
3
+ import { join } from 'node:path';
4
+ export const ChangeKind = {
5
+ WRITTEN: 'written',
6
+ DELETED: 'deleted',
7
+ MODIFIED: 'modified',
8
+ /** The bytes are identical and what the world may do with them is not. */
9
+ MODE_CHANGED: 'mode-changed',
10
+ };
11
+ /**
12
+ * Directories whose contents are somebody else's, and are hashed at real cost.
13
+ *
14
+ * Measured rather than assumed: hashing one `node_modules` ran to tens of thousands of files and
15
+ * more than a gigabyte, and every window takes TWO snapshots. Excluded by default and overridable,
16
+ * because a tool whose job is to write `dist/` needs `dist/` watched.
17
+ *
18
+ * The exclusion is reported as coverage, never hidden. An empty blind-spot list is a positive
19
+ * claim that nothing was hidden.
20
+ */
21
+ export const EXCLUDED_BY_DEFAULT = ['node_modules', '.git', 'dist', '.next'];
22
+ /** What is under these roots right now. */
23
+ export function takeSnapshot(roots, options = {}) {
24
+ const exclude = new Set(options.exclude ?? EXCLUDED_BY_DEFAULT);
25
+ const files = new Map();
26
+ const unreadable = [];
27
+ for (const root of roots)
28
+ walk(root, exclude, files, unreadable, root);
29
+ return { files, unreadable };
30
+ }
31
+ /**
32
+ * `lstat`, not `stat`, and the difference is the point.
33
+ *
34
+ * `stat` follows a symlink and reports the thing at the other end, so a link created where a file
35
+ * used to be reads as the file, unchanged. `lstat` reports the link itself, which is what actually
36
+ * appeared on disk.
37
+ */
38
+ function walk(dir, exclude, into, unreadable, root) {
39
+ let entries;
40
+ try {
41
+ entries = readdirSync(dir);
42
+ }
43
+ catch {
44
+ // Only the root is worth naming: a directory that vanished mid-walk is a race, and reporting
45
+ // every one of them would bury the case a reader can act on.
46
+ if (dir === root)
47
+ unreadable.push(dir);
48
+ return;
49
+ }
50
+ for (const entry of entries) {
51
+ if (exclude.has(entry))
52
+ continue;
53
+ const path = join(dir, entry);
54
+ let stat;
55
+ try {
56
+ stat = lstatSync(path);
57
+ }
58
+ catch {
59
+ continue;
60
+ }
61
+ if (stat.isDirectory()) {
62
+ walk(path, exclude, into, unreadable, root);
63
+ continue;
64
+ }
65
+ into.set(path, factOf(path, stat.size, stat.mode & 0o777, stat.isSymbolicLink()));
66
+ }
67
+ }
68
+ function factOf(path, size, mode, isLink) {
69
+ if (isLink) {
70
+ // A link has no content of its own. What it POINTS AT is the fact worth holding: a link
71
+ // repointed at a different target is a change, and its size on disk may not move.
72
+ return { hash: undefined, size, mode, linkTo: readTarget(path) };
73
+ }
74
+ return { hash: hashOf(path), size, mode, linkTo: undefined };
75
+ }
76
+ function readTarget(path) {
77
+ try {
78
+ return readlinkSync(path);
79
+ }
80
+ catch {
81
+ return undefined;
82
+ }
83
+ }
84
+ /**
85
+ * The content hash, which is the only thing that decides whether content changed.
86
+ *
87
+ * Size and mtime may decide when to LOOK and never what changed: two writes inside the same second
88
+ * of equal length produce identical mtime, identical size and different bytes, and a comparison
89
+ * that stopped at the cheap fields would report a rewritten file as untouched.
90
+ */
91
+ function hashOf(path) {
92
+ try {
93
+ return createHash('sha256').update(readFileSync(path)).digest('hex');
94
+ }
95
+ catch {
96
+ // Unreadable is not unchanged, and it is not a hash either. `undefined` means nobody could
97
+ // look, which `diffSnapshots` must not read as "the same".
98
+ return undefined;
99
+ }
100
+ }
101
+ /**
102
+ * What moved between two snapshots.
103
+ *
104
+ * Pure: two snapshots in, a list out, no filesystem and no clock. That is what makes the
105
+ * interesting comparisons testable without arranging for them on a real disk.
106
+ */
107
+ export function diffSnapshots(before, after) {
108
+ const changes = [];
109
+ for (const [path, now] of after.files) {
110
+ const then = before.files.get(path);
111
+ if (then === undefined) {
112
+ changes.push({ path, kind: ChangeKind.WRITTEN, before: undefined, after: now });
113
+ continue;
114
+ }
115
+ const kind = changeBetween(then, now);
116
+ if (kind !== undefined)
117
+ changes.push({ path, kind, before: then, after: now });
118
+ }
119
+ for (const [path, then] of before.files) {
120
+ if (!after.files.has(path)) {
121
+ changes.push({ path, kind: ChangeKind.DELETED, before: then, after: undefined });
122
+ }
123
+ }
124
+ return changes;
125
+ }
126
+ /**
127
+ * How one path differs from itself, or undefined when it does not.
128
+ *
129
+ * Content outranks mode: a file whose bytes AND permissions both changed is reported as modified,
130
+ * because that is the change a reader cares about and the mode is a detail of it. The reverse
131
+ * ordering would file a rewritten executable under `mode-changed`.
132
+ */
133
+ function changeBetween(before, after) {
134
+ if (before.linkTo !== after.linkTo)
135
+ return ChangeKind.MODIFIED;
136
+ // An unreadable file on either side is NOT evidence of sameness. Two `undefined` hashes mean
137
+ // nobody could look twice, which must not read as "identical".
138
+ const unknown = before.hash === undefined || after.hash === undefined;
139
+ if (unknown ? before.size !== after.size : before.hash !== after.hash) {
140
+ return ChangeKind.MODIFIED;
141
+ }
142
+ if (before.mode !== after.mode)
143
+ return ChangeKind.MODE_CHANGED;
144
+ return undefined;
145
+ }
package/package.json CHANGED
@@ -1,6 +1,57 @@
1
1
  {
2
2
  "name": "@reticlehq/terminal",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "3.11.0",
4
+ "description": "Reticle's terminal realm: runs a command-line program, watches what it prints and what it does to the filesystem, and reports evidence an adjudicator can reason over. Needs no code inside the program.",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ },
13
+ "./package.json": "./package.json"
14
+ },
15
+ "dependencies": {
16
+ "open-verification": "1.2.0"
17
+ },
18
+ "license": "Apache-2.0",
19
+ "author": "Reticle contributors",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/reticlehq/reticle.git",
23
+ "directory": "adapters/realm/terminal"
24
+ },
25
+ "homepage": "https://github.com/reticlehq/reticle#readme",
26
+ "bugs": "https://github.com/reticlehq/reticle/issues",
27
+ "files": [
28
+ "bin",
29
+ "dist",
30
+ "README.md",
31
+ "NOTICE"
32
+ ],
33
+ "sideEffects": false,
34
+ "keywords": [
35
+ "cli",
36
+ "verification",
37
+ "ai-agents",
38
+ "testing",
39
+ "reticle"
40
+ ],
41
+ "publishConfig": {
42
+ "access": "public"
43
+ },
44
+ "engines": {
45
+ "node": ">=20.0.0"
46
+ },
47
+ "bin": {
48
+ "reticle-terminal": "./bin/drive.mjs"
49
+ },
50
+ "scripts": {
51
+ "build": "tsc -b && node ../../../scripts/alias-dist.mjs",
52
+ "typecheck": "tsc -b",
53
+ "lint": "eslint src",
54
+ "test:unit": "vitest run src --passWithNoTests",
55
+ "test:coverage": "vitest run src --passWithNoTests --coverage.enabled --coverage.provider=v8 --coverage.reporter=text-summary --coverage.include='src/**/*.ts' --coverage.exclude='src/**/*.test.ts'"
56
+ }
6
57
  }