rewindex 0.0.0-stage → 0.1.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,118 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildMessage = buildMessage;
4
+ exports.listCheckpoints = listCheckpoints;
5
+ exports.listPromptCheckpoints = listPromptCheckpoints;
6
+ exports.resolveCheckpoint = resolveCheckpoint;
7
+ const TRAILER_KEYS = {
8
+ "rewindex-turn": "turnId",
9
+ "rewindex-session": "sessionId",
10
+ "rewindex-event": "event",
11
+ "rewindex-tool": "tool",
12
+ "rewindex-prompt": "prompt",
13
+ };
14
+ function buildMessage(label, meta) {
15
+ const lines = [sanitizeLabel(label), ""];
16
+ const push = (k, v) => {
17
+ if (v)
18
+ lines.push(`${k}: ${oneLine(v)}`);
19
+ };
20
+ push("rewindex-turn", meta.turnId);
21
+ push("rewindex-session", meta.sessionId);
22
+ push("rewindex-event", meta.event);
23
+ push("rewindex-tool", meta.tool);
24
+ push("rewindex-prompt", meta.prompt);
25
+ return lines.join("\n");
26
+ }
27
+ function sanitizeLabel(s) {
28
+ return oneLine(s).slice(0, 120) || "rewindex snapshot";
29
+ }
30
+ function oneLine(s) {
31
+ return s.replace(/\s+/g, " ").trim();
32
+ }
33
+ function parseMeta(body) {
34
+ const lines = body.split("\n");
35
+ const label = lines[0] ?? "";
36
+ const meta = {};
37
+ for (const line of lines.slice(1)) {
38
+ const idx = line.indexOf(":");
39
+ if (idx < 0)
40
+ continue;
41
+ const key = line.slice(0, idx).trim();
42
+ const val = line.slice(idx + 1).trim();
43
+ const field = TRAILER_KEYS[key];
44
+ if (field)
45
+ meta[field] = val;
46
+ }
47
+ return { label, meta };
48
+ }
49
+ /**
50
+ * List snapshots newest-first, skipping the empty root commit. `filesChanged`
51
+ * is the count vs the immediate parent.
52
+ */
53
+ async function listCheckpoints(git) {
54
+ if (!(await git.isInitialized()))
55
+ return [];
56
+ const SEP = "\x1e";
57
+ const REC = "\x1f";
58
+ const fmt = `%H${SEP}%ct${SEP}%B${REC}`;
59
+ const out = (await git.run(["log", `--pretty=format:${fmt}`])).stdout;
60
+ const records = out.split(REC).map((r) => r.trim()).filter(Boolean);
61
+ const checkpoints = [];
62
+ for (const rec of records) {
63
+ const [fullId, ct, body = ""] = rec.split(SEP);
64
+ const { label, meta } = parseMeta(body.trim());
65
+ if (meta.event === "root")
66
+ continue;
67
+ const unixTime = Number(ct) * 1000;
68
+ const filesChanged = await countFilesChanged(git, fullId);
69
+ checkpoints.push({
70
+ id: fullId.slice(0, 10),
71
+ fullId,
72
+ dateISO: new Date(unixTime).toISOString(),
73
+ unixTime,
74
+ label,
75
+ meta,
76
+ filesChanged,
77
+ });
78
+ }
79
+ return checkpoints;
80
+ }
81
+ async function listPromptCheckpoints(git, limit = 50) {
82
+ const checkpoints = await listCheckpoints(git);
83
+ const prompts = [];
84
+ for (const checkpoint of checkpoints) {
85
+ const prompt = checkpoint.meta.prompt?.trim();
86
+ if (checkpoint.meta.event !== "UserPromptSubmit" || !prompt || prompt.startsWith("$rewindex")) {
87
+ continue;
88
+ }
89
+ const changes = await git.changesSince(checkpoint.fullId);
90
+ if (changes.length === 0)
91
+ continue;
92
+ prompts.push({ ...checkpoint, filesChanged: changes.length, changes });
93
+ if (prompts.length === limit)
94
+ break;
95
+ }
96
+ return prompts;
97
+ }
98
+ async function countFilesChanged(git, fullId) {
99
+ const r = await git.run(["diff", "--name-only", `${fullId}~1`, fullId]);
100
+ if (r.code !== 0)
101
+ return 0;
102
+ return r.stdout.split("\n").filter((s) => s.trim()).length;
103
+ }
104
+ /** Resolve a user-supplied id (short sha or "latest") to a full sha. */
105
+ async function resolveCheckpoint(git, id) {
106
+ const all = await listCheckpoints(git);
107
+ if (all.length === 0)
108
+ return null;
109
+ const numbered = /^\d+$/.test(id) ? Number(id) : 0;
110
+ if (numbered >= 1 && numbered <= 50) {
111
+ return (await listPromptCheckpoints(git))[numbered - 1] ?? null;
112
+ }
113
+ if (id === "latest" || id === "last") {
114
+ return (await listPromptCheckpoints(git))[0] ?? null;
115
+ }
116
+ const matches = all.filter((c) => c.fullId.startsWith(id) || c.id === id);
117
+ return matches[0] ?? null;
118
+ }
package/dist/state.js ADDED
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.readState = readState;
37
+ exports.writeState = writeState;
38
+ const fs = __importStar(require("fs"));
39
+ function readState(store) {
40
+ try {
41
+ return JSON.parse(fs.readFileSync(store.stateFile, "utf8"));
42
+ }
43
+ catch {
44
+ return {};
45
+ }
46
+ }
47
+ function writeState(store, state) {
48
+ fs.mkdirSync(store.dir, { recursive: true });
49
+ fs.writeFileSync(store.stateFile, JSON.stringify(state, null, 2));
50
+ }
package/package.json CHANGED
@@ -1,6 +1,49 @@
1
1
  {
2
2
  "name": "rewindex",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "git-free /undo for OpenAI Codex CLI — automatic shadow-git snapshots before every file-mutating tool call, restore with one command. Never touches your project's git history.",
5
+ "keywords": [
6
+ "codex",
7
+ "codex-cli",
8
+ "undo",
9
+ "rewind",
10
+ "checkpoint",
11
+ "snapshot",
12
+ "hooks"
13
+ ],
14
+ "license": "MIT",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/zenonix-studio/rewindex.git"
18
+ },
19
+ "homepage": "https://github.com/zenonix-studio/rewindex#readme",
20
+ "bugs": {
21
+ "url": "https://github.com/zenonix-studio/rewindex/issues"
22
+ },
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "type": "commonjs",
27
+ "bin": {
28
+ "rewindex": "dist/cli.js"
29
+ },
30
+ "files": [
31
+ "dist",
32
+ "README.md"
33
+ ],
34
+ "engines": {
35
+ "node": ">=20"
36
+ },
37
+ "scripts": {
38
+ "build": "tsc -p tsconfig.json",
39
+ "selfcheck": "node selfcheck.js",
40
+ "prepublishOnly": "npm run build",
41
+ "edgecheck": "node edgecheck.js",
42
+ "installcheck": "node installcheck.js",
43
+ "historycheck": "node historycheck.js"
44
+ },
45
+ "devDependencies": {
46
+ "@types/node": "^20.14.0",
47
+ "typescript": "^5.5.0"
48
+ }
49
+ }