@asanabrial/estigia 0.2.0 → 0.3.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.
Files changed (2) hide show
  1. package/bin/estigia.js +72 -10
  2. package/package.json +1 -1
package/bin/estigia.js CHANGED
@@ -88,6 +88,25 @@ function installedVersion(binary) {
88
88
  return match ? match[1] : null;
89
89
  }
90
90
 
91
+ function parsedVersion(text) {
92
+ const match = /^(\d+)\.(\d+)\.(\d+)/.exec(text || "");
93
+ return match ? match.slice(1).map(Number) : null;
94
+ }
95
+
96
+ function isNewer(installed, wanted) {
97
+ const found = parsedVersion(installed);
98
+ const asked = parsedVersion(wanted);
99
+ if (!found || !asked) {
100
+ return false;
101
+ }
102
+ for (let index = 0; index < 3; index += 1) {
103
+ if (found[index] !== asked[index]) {
104
+ return found[index] > asked[index];
105
+ }
106
+ }
107
+ return false;
108
+ }
109
+
91
110
  function extract(archivePath, into) {
92
111
  const tar =
93
112
  process.platform === "win32"
@@ -134,13 +153,13 @@ async function install(tag, target, binary) {
134
153
  }
135
154
  fs.chmodSync(candidate, 0o755);
136
155
 
137
- const recorded = spawnSync(candidate, ["__record-install"], {
156
+ const admitted = spawnSync(candidate, ["__record-install", "--check"], {
138
157
  encoding: "utf8",
139
158
  stdio: ["ignore", process.stderr, "pipe"],
140
159
  });
141
- const loaderWords = (recorded.stderr || "").trim();
142
- if (recorded.error || recorded.status === 126 || recorded.status === 127) {
143
- const words = recorded.error ? recorded.error.message : loaderWords;
160
+ const loaderWords = (admitted.stderr || "").trim();
161
+ if (admitted.error || admitted.status === 126 || admitted.status === 127) {
162
+ const words = admitted.error ? admitted.error.message : loaderWords;
144
163
  let detail = `this platform cannot run the ${target.triple} archive, and that is the reason`;
145
164
  if (words) {
146
165
  detail += `; the loader said: ${words}`;
@@ -155,29 +174,72 @@ async function install(tag, target, binary) {
155
174
  " Refusing to replace the installed executable.",
156
175
  );
157
176
  }
158
- process.stderr.write(recorded.stderr || "");
159
- if (recorded.status !== 0) {
177
+ process.stderr.write(admitted.stderr || "");
178
+ if (admitted.status !== 0) {
160
179
  fail("candidate lifecycle admission failed; refusing to replace the installed executable");
161
180
  }
162
181
 
163
182
  fs.mkdirSync(path.dirname(binary), { recursive: true });
164
183
  const placed = `${binary}.${process.pid}.new`;
165
- fs.copyFileSync(candidate, placed);
166
- fs.chmodSync(placed, 0o755);
167
- fs.renameSync(placed, binary);
184
+ try {
185
+ fs.copyFileSync(candidate, placed);
186
+ fs.chmodSync(placed, 0o755);
187
+ fs.renameSync(placed, binary);
188
+ } catch (error) {
189
+ fs.rmSync(placed, { force: true });
190
+ throw error;
191
+ }
192
+
193
+ const recorded = spawnSync(binary, ["__record-install"], {
194
+ encoding: "utf8",
195
+ stdio: ["ignore", process.stderr, "pipe"],
196
+ });
197
+ process.stderr.write(recorded.stderr || "");
198
+ if (recorded.error || recorded.status !== 0) {
199
+ let disposal = "it was removed, so the installer can retry";
200
+ try {
201
+ fs.rmSync(binary, { force: true });
202
+ } catch (error) {
203
+ disposal = `it could not be removed (${error.message}), so delete ${binary} before retrying`;
204
+ }
205
+ const words = recorded.signal
206
+ ? `signal ${recorded.signal}`
207
+ : recorded.error
208
+ ? recorded.error.message
209
+ : `exit ${recorded.status}`;
210
+ fail(`the replaced executable could not record its lifecycle (${words}); ${disposal}`);
211
+ }
168
212
  } finally {
169
213
  fs.rmSync(staging, { recursive: true, force: true });
170
214
  }
171
215
  }
172
216
 
217
+ // What the launcher does about the binary already at the path, decided in one
218
+ // place: run it (`run`), keep it because it is newer (`keep-newer`), or install
219
+ // the release (`install`). A newer binary wins even when `ESTIGIA_VERSION` pins
220
+ // an older release, because the lifecycle refuses a downgrade machine-wide and
221
+ // the pinned archive would be rejected after the download; `docs/lifecycle.md`
222
+ // states that contract.
223
+ function plannedOutcome(installed, wanted) {
224
+ if (installed === wanted) {
225
+ return "run";
226
+ }
227
+ return isNewer(installed, wanted) ? "keep-newer" : "install";
228
+ }
229
+
173
230
  async function main() {
174
231
  const target = resolveTarget();
175
232
  const { version } = require("../package.json");
176
233
  const tag = process.env.ESTIGIA_VERSION || `v${version}`;
177
234
  const binary = path.join(installDirectory(), target.exe);
178
235
 
179
- if (installedVersion(binary) !== tag.replace(/^v/, "")) {
236
+ const installed = installedVersion(binary);
237
+ const wanted = tag.replace(/^v/, "");
238
+ const outcome = plannedOutcome(installed, wanted);
239
+ if (outcome === "install") {
180
240
  await install(tag, target, binary);
241
+ } else if (outcome === "keep-newer") {
242
+ say(`using the newer installed estigia ${installed} at ${binary}`);
181
243
  }
182
244
 
183
245
  const result = spawnSync(binary, process.argv.slice(2), { stdio: "inherit" });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asanabrial/estigia",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "mcpName": "io.github.asanabrial/estigia",
5
5
  "description": "Guardrails for AI coding agents (Claude Code, Codex, OpenCode, Pi): an MCP server and hooks that verify issue claims, bind reviews to exact commits and gate merges",
6
6
  "bin": {