turnstamp 0.0.0-stage → 0.2.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.
- package/LICENSE +21 -0
- package/README.md +170 -2
- package/bin/turnstamp.js +284 -0
- package/hooks/claude-code.js +173 -0
- package/opencode.js +31 -0
- package/package.json +55 -4
- package/src/events.js +58 -0
- package/src/index.js +18 -0
- package/src/install.js +114 -0
- package/src/ledger.js +261 -0
- package/src/opencode-plugin.js +115 -0
- package/src/tools.js +87 -0
- package/src/tree.js +91 -0
package/src/tools.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
// Tools that write files, and where each keeps the path in its input.
|
|
6
|
+
// OpenCode uses filePath. Anything new can be added here.
|
|
7
|
+
const PATH_KEYS = ["filePath", "file_path", "path"];
|
|
8
|
+
|
|
9
|
+
export function toolPaths(tool, input) {
|
|
10
|
+
if (!input || typeof input !== "object") {
|
|
11
|
+
return [];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (String(tool).toLowerCase() === "patch") {
|
|
15
|
+
return patchPaths(input);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const found = [];
|
|
19
|
+
for (const key of PATH_KEYS) {
|
|
20
|
+
if (typeof input[key] === "string") {
|
|
21
|
+
found.push(input[key]);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (Array.isArray(input.filePaths)) {
|
|
26
|
+
for (const p of input.filePaths) {
|
|
27
|
+
if (typeof p === "string") found.push(p);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return [...new Set(found)];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// The patch tool takes freeform text with the same headers git apply uses.
|
|
35
|
+
export function patchPaths(input) {
|
|
36
|
+
const text =
|
|
37
|
+
typeof input === "string" ? input : input.patchText || input.patch || input.text || "";
|
|
38
|
+
if (typeof text !== "string") {
|
|
39
|
+
return [];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const out = [];
|
|
43
|
+
for (const line of text.split("\n")) {
|
|
44
|
+
const match = line.match(/^\*\*\* (?:Update|Add|Delete) File: (.+)$/);
|
|
45
|
+
if (match) {
|
|
46
|
+
out.push(match[1].trim());
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return [...new Set(out)];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Only these tools can leave a file behind. Compared case sensitively against
|
|
53
|
+
// lowercase names, because OpenCode says write while Claude Code says Write.
|
|
54
|
+
const FILE_TOOLS = new Set(["write", "edit", "multiedit", "patchedit", "notebookedit", "patch"]);
|
|
55
|
+
|
|
56
|
+
export function isFileTool(tool) {
|
|
57
|
+
return FILE_TOOLS.has(String(tool).toLowerCase());
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function readState(abs) {
|
|
61
|
+
try {
|
|
62
|
+
const stat = fs.statSync(abs);
|
|
63
|
+
if (!stat.isFile()) {
|
|
64
|
+
return stat.isDirectory() ? "dir" : "other";
|
|
65
|
+
}
|
|
66
|
+
return createHash("sha256").update(fs.readFileSync(abs)).digest("hex");
|
|
67
|
+
} catch {
|
|
68
|
+
return "missing";
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// A shell call can write files, but we cannot know which. Record it as a call
|
|
73
|
+
// so the ledger shows it happened, and let the next verify attribute whatever
|
|
74
|
+
// moved.
|
|
75
|
+
export function shellCommand(input) {
|
|
76
|
+
if (typeof input === "string") {
|
|
77
|
+
return input;
|
|
78
|
+
}
|
|
79
|
+
if (input && typeof input.command === "string") {
|
|
80
|
+
return input.command;
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function projectRoot(cwd) {
|
|
86
|
+
return path.resolve(cwd || process.cwd());
|
|
87
|
+
}
|
package/src/tree.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
const SKIP_DIRS = new Set([
|
|
6
|
+
".git",
|
|
7
|
+
"node_modules",
|
|
8
|
+
".turnstamp",
|
|
9
|
+
"dist",
|
|
10
|
+
"build",
|
|
11
|
+
"target",
|
|
12
|
+
".venv",
|
|
13
|
+
"__pycache__",
|
|
14
|
+
]);
|
|
15
|
+
|
|
16
|
+
export function fileHash(file) {
|
|
17
|
+
return createHash("sha256").update(fs.readFileSync(file)).digest("hex");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Walks the directory and returns a target -> digest map. Directories are
|
|
21
|
+
// recorded as "dir" so an empty folder is still accounted for in the ledger.
|
|
22
|
+
export function hashTree(root, options = {}) {
|
|
23
|
+
const prefix = options.prefix || "";
|
|
24
|
+
const out = new Map();
|
|
25
|
+
|
|
26
|
+
function walk(dir, relative) {
|
|
27
|
+
let names;
|
|
28
|
+
try {
|
|
29
|
+
names = fs.readdirSync(dir);
|
|
30
|
+
} catch {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
for (const name of names.sort()) {
|
|
35
|
+
if (SKIP_DIRS.has(name)) {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const full = path.join(dir, name);
|
|
40
|
+
const rel = relative ? `${relative}/${name}` : name;
|
|
41
|
+
let stat;
|
|
42
|
+
try {
|
|
43
|
+
stat = fs.lstatSync(full);
|
|
44
|
+
} catch {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (stat.isSymbolicLink()) {
|
|
49
|
+
out.set(rel, `link:${fs.readlinkSync(full)}`);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (stat.isDirectory()) {
|
|
54
|
+
out.set(rel, "dir");
|
|
55
|
+
walk(full, rel);
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (stat.isFile()) {
|
|
60
|
+
out.set(rel, fileHash(full));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
walk(root, prefix);
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Compares two maps and returns what appeared, changed and disappeared. Used by
|
|
70
|
+
// the snapshot writer so a turn records the delta, not the whole tree twice.
|
|
71
|
+
export function diffTrees(before, after) {
|
|
72
|
+
const added = [];
|
|
73
|
+
const changed = [];
|
|
74
|
+
const removed = [];
|
|
75
|
+
|
|
76
|
+
for (const [target, hash] of after) {
|
|
77
|
+
if (!before.has(target)) {
|
|
78
|
+
added.push(target);
|
|
79
|
+
} else if (before.get(target) !== hash) {
|
|
80
|
+
changed.push(target);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
for (const target of before.keys()) {
|
|
85
|
+
if (!after.has(target)) {
|
|
86
|
+
removed.push(target);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return { added, changed, removed };
|
|
91
|
+
}
|