-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathstate.mjs
More file actions
191 lines (164 loc) · 4.87 KB
/
Copy pathstate.mjs
File metadata and controls
191 lines (164 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { createHash } from "node:crypto";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { resolveWorkspaceRoot } from "./workspace.mjs";
const STATE_VERSION = 1;
const PLUGIN_DATA_ENV = process.env.ANTIGRAVITY_PLUGIN_DATA ? "ANTIGRAVITY_PLUGIN_DATA" : (process.env.GEMINI_PLUGIN_DATA ? "GEMINI_PLUGIN_DATA" : "CLAUDE_PLUGIN_DATA");
const FALLBACK_STATE_ROOT_DIR = path.join(os.tmpdir(), "codex-companion");
const STATE_FILE_NAME = "state.json";
const JOBS_DIR_NAME = "jobs";
const MAX_JOBS = 50;
function nowIso() {
return new Date().toISOString();
}
function defaultState() {
return {
version: STATE_VERSION,
config: {
stopReviewGate: false
},
jobs: []
};
}
export function resolveStateDir(cwd) {
const workspaceRoot = resolveWorkspaceRoot(cwd);
let canonicalWorkspaceRoot = workspaceRoot;
try {
canonicalWorkspaceRoot = fs.realpathSync.native(workspaceRoot);
} catch {
canonicalWorkspaceRoot = workspaceRoot;
}
const slugSource = path.basename(workspaceRoot) || "workspace";
const slug = slugSource.replace(/[^a-zA-Z0-9._-]+/g, "-").replace(/^-+|-+$/g, "") || "workspace";
const hash = createHash("sha256").update(canonicalWorkspaceRoot).digest("hex").slice(0, 16);
const pluginDataDir = process.env[PLUGIN_DATA_ENV];
const stateRoot = pluginDataDir ? path.join(pluginDataDir, "state") : FALLBACK_STATE_ROOT_DIR;
return path.join(stateRoot, `${slug}-${hash}`);
}
export function resolveStateFile(cwd) {
return path.join(resolveStateDir(cwd), STATE_FILE_NAME);
}
export function resolveJobsDir(cwd) {
return path.join(resolveStateDir(cwd), JOBS_DIR_NAME);
}
export function ensureStateDir(cwd) {
fs.mkdirSync(resolveJobsDir(cwd), { recursive: true });
}
export function loadState(cwd) {
const stateFile = resolveStateFile(cwd);
if (!fs.existsSync(stateFile)) {
return defaultState();
}
try {
const parsed = JSON.parse(fs.readFileSync(stateFile, "utf8"));
return {
...defaultState(),
...parsed,
config: {
...defaultState().config,
...(parsed.config ?? {})
},
jobs: Array.isArray(parsed.jobs) ? parsed.jobs : []
};
} catch {
return defaultState();
}
}
function pruneJobs(jobs) {
return [...jobs]
.sort((left, right) => String(right.updatedAt ?? "").localeCompare(String(left.updatedAt ?? "")))
.slice(0, MAX_JOBS);
}
function removeFileIfExists(filePath) {
if (filePath && fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
}
export function saveState(cwd, state) {
const previousJobs = loadState(cwd).jobs;
ensureStateDir(cwd);
const nextJobs = pruneJobs(state.jobs ?? []);
const nextState = {
version: STATE_VERSION,
config: {
...defaultState().config,
...(state.config ?? {})
},
jobs: nextJobs
};
const retainedIds = new Set(nextJobs.map((job) => job.id));
for (const job of previousJobs) {
if (retainedIds.has(job.id)) {
continue;
}
removeJobFile(resolveJobFile(cwd, job.id));
removeFileIfExists(job.logFile);
}
fs.writeFileSync(resolveStateFile(cwd), `${JSON.stringify(nextState, null, 2)}\n`, "utf8");
return nextState;
}
export function updateState(cwd, mutate) {
const state = loadState(cwd);
mutate(state);
return saveState(cwd, state);
}
export function generateJobId(prefix = "job") {
const random = Math.random().toString(36).slice(2, 8);
return `${prefix}-${Date.now().toString(36)}-${random}`;
}
export function upsertJob(cwd, jobPatch) {
return updateState(cwd, (state) => {
const timestamp = nowIso();
const existingIndex = state.jobs.findIndex((job) => job.id === jobPatch.id);
if (existingIndex === -1) {
state.jobs.unshift({
createdAt: timestamp,
updatedAt: timestamp,
...jobPatch
});
return;
}
state.jobs[existingIndex] = {
...state.jobs[existingIndex],
...jobPatch,
updatedAt: timestamp
};
});
}
export function listJobs(cwd) {
return loadState(cwd).jobs;
}
export function setConfig(cwd, key, value) {
return updateState(cwd, (state) => {
state.config = {
...state.config,
[key]: value
};
});
}
export function getConfig(cwd) {
return loadState(cwd).config;
}
export function writeJobFile(cwd, jobId, payload) {
ensureStateDir(cwd);
const jobFile = resolveJobFile(cwd, jobId);
fs.writeFileSync(jobFile, `${JSON.stringify(payload, null, 2)}\n`, "utf8");
return jobFile;
}
export function readJobFile(jobFile) {
return JSON.parse(fs.readFileSync(jobFile, "utf8"));
}
function removeJobFile(jobFile) {
if (fs.existsSync(jobFile)) {
fs.unlinkSync(jobFile);
}
}
export function resolveJobLogFile(cwd, jobId) {
ensureStateDir(cwd);
return path.join(resolveJobsDir(cwd), `${jobId}.log`);
}
export function resolveJobFile(cwd, jobId) {
ensureStateDir(cwd);
return path.join(resolveJobsDir(cwd), `${jobId}.json`);
}