xtrm-tools 2.4.2 → 2.4.4

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.
@@ -1,138 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- import { execSync } from 'node:child_process';
4
- import { existsSync, readFileSync, writeFileSync } from 'node:fs';
5
- import path from 'node:path';
6
-
7
- export const SESSION_STATE_FILE = '.xtrm-session-state.json';
8
-
9
- export const SESSION_PHASES = [
10
- 'claimed',
11
- 'phase1-done',
12
- 'waiting-merge',
13
- 'conflicting',
14
- 'pending-cleanup',
15
- 'merged',
16
- 'cleanup-done',
17
- ];
18
-
19
- const ALLOWED_TRANSITIONS = {
20
- claimed: ['phase1-done', 'waiting-merge', 'conflicting', 'pending-cleanup', 'cleanup-done'],
21
- 'phase1-done': ['waiting-merge', 'conflicting', 'pending-cleanup', 'cleanup-done'],
22
- 'waiting-merge': ['conflicting', 'pending-cleanup', 'merged', 'cleanup-done'],
23
- conflicting: ['waiting-merge', 'pending-cleanup', 'merged', 'cleanup-done'],
24
- 'pending-cleanup': ['waiting-merge', 'conflicting', 'merged', 'cleanup-done'],
25
- merged: ['cleanup-done'],
26
- 'cleanup-done': [],
27
- };
28
-
29
- function nowIso() {
30
- return new Date().toISOString();
31
- }
32
-
33
- function isValidPhase(phase) {
34
- return typeof phase === 'string' && SESSION_PHASES.includes(phase);
35
- }
36
-
37
- function normalizeState(state) {
38
- if (!state || typeof state !== 'object') throw new Error('Invalid session state payload');
39
- if (!state.issueId || !state.branch || !state.worktreePath) {
40
- throw new Error('Session state requires issueId, branch, and worktreePath');
41
- }
42
- if (!isValidPhase(state.phase)) throw new Error(`Invalid phase: ${String(state.phase)}`);
43
-
44
- return {
45
- issueId: String(state.issueId),
46
- branch: String(state.branch),
47
- worktreePath: String(state.worktreePath),
48
- prNumber: state.prNumber ?? null,
49
- prUrl: state.prUrl ?? null,
50
- phase: state.phase,
51
- conflictFiles: Array.isArray(state.conflictFiles) ? state.conflictFiles.map(String) : [],
52
- startedAt: state.startedAt || nowIso(),
53
- lastChecked: nowIso(),
54
- };
55
- }
56
-
57
- function canTransition(from, to) {
58
- if (!isValidPhase(from) || !isValidPhase(to)) return false;
59
- if (from === to) return true;
60
- return (ALLOWED_TRANSITIONS[from] || []).includes(to);
61
- }
62
-
63
- function findAncestorStateFile(startCwd) {
64
- let current = path.resolve(startCwd || process.cwd());
65
- for (;;) {
66
- const candidate = path.join(current, SESSION_STATE_FILE);
67
- if (existsSync(candidate)) return candidate;
68
- const parent = path.dirname(current);
69
- if (parent === current) return null;
70
- current = parent;
71
- }
72
- }
73
-
74
- function findRepoRoot(cwd) {
75
- try {
76
- return execSync('git rev-parse --show-toplevel', {
77
- encoding: 'utf8',
78
- cwd,
79
- stdio: ['pipe', 'pipe', 'pipe'],
80
- timeout: 5000,
81
- }).trim();
82
- } catch {
83
- return null;
84
- }
85
- }
86
-
87
- export function findSessionStateFile(startCwd) {
88
- return findAncestorStateFile(startCwd);
89
- }
90
-
91
- export function readSessionState(startCwd) {
92
- const filePath = findSessionStateFile(startCwd);
93
- if (!filePath) return null;
94
-
95
- try {
96
- const parsed = JSON.parse(readFileSync(filePath, 'utf8'));
97
- const state = normalizeState(parsed);
98
- return { ...state, _filePath: filePath };
99
- } catch {
100
- return null;
101
- }
102
- }
103
-
104
- export function resolveSessionStatePath(cwd) {
105
- const existing = findSessionStateFile(cwd);
106
- if (existing) return existing;
107
-
108
- const repoRoot = findRepoRoot(cwd);
109
- if (repoRoot) return path.join(repoRoot, SESSION_STATE_FILE);
110
- return path.join(cwd, SESSION_STATE_FILE);
111
- }
112
-
113
- export function writeSessionState(state, opts = {}) {
114
- const cwd = opts.cwd || process.cwd();
115
- const filePath = opts.filePath || resolveSessionStatePath(cwd);
116
- const normalized = normalizeState(state);
117
- writeFileSync(filePath, JSON.stringify(normalized, null, 2) + '\n', 'utf8');
118
- return filePath;
119
- }
120
-
121
- export function updateSessionPhase(startCwd, nextPhase, patch = {}) {
122
- if (!isValidPhase(nextPhase)) throw new Error(`Invalid phase: ${String(nextPhase)}`);
123
- const existing = readSessionState(startCwd);
124
- if (!existing) throw new Error('Session state file not found');
125
- if (!canTransition(existing.phase, nextPhase)) {
126
- throw new Error(`Invalid phase transition: ${existing.phase} -> ${nextPhase}`);
127
- }
128
-
129
- const nextState = {
130
- ...existing,
131
- ...patch,
132
- phase: nextPhase,
133
- };
134
-
135
- delete nextState._filePath;
136
- const filePath = writeSessionState(nextState, { filePath: existing._filePath, cwd: startCwd });
137
- return { ...nextState, _filePath: filePath };
138
- }