@muggleai/works 5.22.0-staging.157 → 5.22.0-staging.158
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/.claude-plugin/marketplace.json +1 -1
- package/dist/{chunk-DLBPRPUN.js → chunk-X75TJAYX.js} +8 -1
- package/dist/cli.js +1 -1
- package/dist/index.js +1 -1
- package/dist/plugin/.claude-plugin/plugin.json +1 -1
- package/dist/plugin/.codex-plugin/plugin.json +1 -1
- package/dist/plugin/.cursor-plugin/plugin.json +1 -1
- package/dist/plugin/hooks/README.md +15 -7
- package/dist/plugin/hooks/codex-hooks.json +3 -10
- package/dist/plugin/hooks/hooks.json +14 -20
- package/dist/plugin/scripts/ensure-electron-app.sh +133 -186
- package/dist/plugin/scripts/guardrail-lib.sh +15 -9
- package/dist/plugin/scripts/guardrails.mjs +137 -21
- package/dist/plugin/scripts/session-maintenance.sh +98 -0
- package/dist/release-manifest.json +3 -3
- package/package.json +2 -1
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin/.codex-plugin/plugin.json +1 -1
- package/plugin/.cursor-plugin/plugin.json +1 -1
- package/plugin/hooks/README.md +15 -7
- package/plugin/hooks/codex-hooks.json +3 -10
- package/plugin/hooks/hooks.json +14 -20
- package/plugin/scripts/ensure-electron-app.sh +133 -186
- package/plugin/scripts/guardrail-lib.sh +15 -9
- package/plugin/scripts/guardrails.mjs +137 -21
- package/plugin/scripts/session-maintenance.sh +98 -0
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { readFileSync, existsSync, mkdirSync, writeFileSync, renameSync,
|
|
1
|
+
import { readFileSync, existsSync, mkdirSync, readdirSync, writeFileSync, renameSync, openSync, writeSync, closeSync, unlinkSync } from 'fs';
|
|
2
2
|
import { basename, join, isAbsolute, resolve, dirname } from 'path';
|
|
3
3
|
import { homedir, hostname } from 'os';
|
|
4
|
+
import { execFileSync, spawn } from 'child_process';
|
|
4
5
|
import { fileURLToPath } from 'url';
|
|
5
|
-
import { execFileSync } from 'child_process';
|
|
6
6
|
|
|
7
7
|
// src/guardrails/cli.ts
|
|
8
8
|
|
|
@@ -136,7 +136,14 @@ function withFileLock(lockPath, waitMs, run) {
|
|
|
136
136
|
|
|
137
137
|
// src/guardrails/sessionState.ts
|
|
138
138
|
var baseDir = (override) => override ?? join(homedir(), ".muggle-ai", "guardrails");
|
|
139
|
-
var fileFor = (sessionId2, override) => join(baseDir(override), `${sessionId2.replace(/[^A-Za-z0-9_-]/g, "_")}
|
|
139
|
+
var fileFor = (sessionId2, override, suffix = ".json") => join(baseDir(override), `${sessionId2.replace(/[^A-Za-z0-9_-]/g, "_")}${suffix}`);
|
|
140
|
+
var WALKTHROUGH_SCAN_SUFFIX = ".walkthrough-scan.json";
|
|
141
|
+
function writeAtomically(target, contents, dirOverride) {
|
|
142
|
+
mkdirSync(baseDir(dirOverride), { recursive: true });
|
|
143
|
+
const staging = `${target}.${process.pid}.tmp`;
|
|
144
|
+
writeFileSync(staging, JSON.stringify(contents, null, 2));
|
|
145
|
+
renameSync(staging, target);
|
|
146
|
+
}
|
|
140
147
|
function readState(sessionId2, dirOverride) {
|
|
141
148
|
const f = fileFor(sessionId2, dirOverride);
|
|
142
149
|
if (!existsSync(f)) return { sessionId: sessionId2, prsHandled: [] };
|
|
@@ -148,11 +155,17 @@ function readState(sessionId2, dirOverride) {
|
|
|
148
155
|
}
|
|
149
156
|
}
|
|
150
157
|
function writeState(state, dirOverride) {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
158
|
+
writeAtomically(fileFor(state.sessionId, dirOverride), state, dirOverride);
|
|
159
|
+
}
|
|
160
|
+
function readWalkthroughScan(sessionId2, dirOverride) {
|
|
161
|
+
try {
|
|
162
|
+
return JSON.parse(readFileSync(fileFor(sessionId2, dirOverride, WALKTHROUGH_SCAN_SUFFIX), "utf-8"));
|
|
163
|
+
} catch {
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
function writeWalkthroughScan(sessionId2, scan, dirOverride) {
|
|
168
|
+
writeAtomically(fileFor(sessionId2, dirOverride, WALKTHROUGH_SCAN_SUFFIX), scan, dirOverride);
|
|
156
169
|
}
|
|
157
170
|
function commitState(sessionId2, mutate, dirOverride) {
|
|
158
171
|
mkdirSync(baseDir(dirOverride), { recursive: true });
|
|
@@ -174,6 +187,34 @@ function markPrHandled(sessionId2, prUrl, dirOverride) {
|
|
|
174
187
|
writeState(state, dirOverride);
|
|
175
188
|
}
|
|
176
189
|
|
|
190
|
+
// src/guardrails/types.ts
|
|
191
|
+
var GuardrailBackgroundJob = /* @__PURE__ */ ((GuardrailBackgroundJob2) => {
|
|
192
|
+
GuardrailBackgroundJob2["ScanWalkthroughs"] = "scan-walkthroughs";
|
|
193
|
+
GuardrailBackgroundJob2["ReserveWalkthroughComment"] = "reserve-walkthrough-comment";
|
|
194
|
+
GuardrailBackgroundJob2["SettleWalkthroughSkipped"] = "settle-walkthrough-skipped";
|
|
195
|
+
GuardrailBackgroundJob2["SettleWalkthroughUnreasoned"] = "settle-walkthrough-unreasoned";
|
|
196
|
+
return GuardrailBackgroundJob2;
|
|
197
|
+
})(GuardrailBackgroundJob || {});
|
|
198
|
+
|
|
199
|
+
// src/guardrails/backgroundJob.ts
|
|
200
|
+
function isBackgroundJob(subcommand) {
|
|
201
|
+
return Object.values(GuardrailBackgroundJob).includes(subcommand);
|
|
202
|
+
}
|
|
203
|
+
function startBackgroundJob(job, jobArgs2, cwd) {
|
|
204
|
+
try {
|
|
205
|
+
const child = spawn(process.execPath, [...process.execArgv, process.argv[1], job, ...jobArgs2], {
|
|
206
|
+
cwd,
|
|
207
|
+
detached: true,
|
|
208
|
+
stdio: "ignore",
|
|
209
|
+
windowsHide: true
|
|
210
|
+
});
|
|
211
|
+
child.on("error", () => void 0);
|
|
212
|
+
child.unref();
|
|
213
|
+
} catch {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
177
218
|
// src/guardrails/shellTool.ts
|
|
178
219
|
function isShellToolCall(input2) {
|
|
179
220
|
return SHELL_TOOL_NAMES.includes(input2.tool_name ?? "");
|
|
@@ -809,11 +850,29 @@ function applyWalkthroughSkip(state, skipped) {
|
|
|
809
850
|
if (!skipped || state.walkthroughSkipped === true) return state;
|
|
810
851
|
return { ...state, walkthroughSkipped: true };
|
|
811
852
|
}
|
|
853
|
+
|
|
854
|
+
// src/hook-budget/constants.ts
|
|
855
|
+
var HOOK_CHILD_PROCESS_DEADLINE_MS = 600;
|
|
856
|
+
|
|
857
|
+
// src/hook-budget/childProcessBudget.ts
|
|
858
|
+
var childProcessDeadlineEpochMs = null;
|
|
859
|
+
function enforceHookChildProcessDeadline(gatesStartEpochMs = Date.now()) {
|
|
860
|
+
childProcessDeadlineEpochMs = gatesStartEpochMs + HOOK_CHILD_PROCESS_DEADLINE_MS;
|
|
861
|
+
}
|
|
862
|
+
function boundedChildTimeoutMs(ceilingMs) {
|
|
863
|
+
if (childProcessDeadlineEpochMs === null) return ceilingMs;
|
|
864
|
+
const remainingMs = Math.floor(childProcessDeadlineEpochMs - Date.now());
|
|
865
|
+
return remainingMs > 0 ? Math.min(ceilingMs, remainingMs) : null;
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
// src/guardrails/walkthroughOwed.ts
|
|
812
869
|
function runGh(args) {
|
|
870
|
+
const timeoutMs = boundedChildTimeoutMs(GH_LOOKUP_TIMEOUT_MS);
|
|
871
|
+
if (timeoutMs === null) return null;
|
|
813
872
|
try {
|
|
814
873
|
return execFileSync("gh", args, {
|
|
815
874
|
encoding: "utf-8",
|
|
816
|
-
timeout:
|
|
875
|
+
timeout: timeoutMs,
|
|
817
876
|
stdio: ["ignore", "pipe", "ignore"],
|
|
818
877
|
windowsHide: true
|
|
819
878
|
});
|
|
@@ -847,6 +906,13 @@ function scanForOwedWalkthroughs(state, lookup = defaultPrWalkthroughLookup) {
|
|
|
847
906
|
}
|
|
848
907
|
return { owed, verified: verified2 };
|
|
849
908
|
}
|
|
909
|
+
function isWalkthroughGateArmed(state) {
|
|
910
|
+
return state.e2eRun === true && state.walkthroughPosted !== true && state.walkthroughSkipped !== true;
|
|
911
|
+
}
|
|
912
|
+
function isWalkthroughScanCurrent(scan, state) {
|
|
913
|
+
const scanned = new Set(scan.prsHandled);
|
|
914
|
+
return scanned.size === new Set(state.prsHandled).size && state.prsHandled.every((prUrl) => scanned.has(prUrl));
|
|
915
|
+
}
|
|
850
916
|
function walkthroughGateDecision(state, owedPrUrls, maxBlocks = MAX_WALKTHROUGH_BLOCKS) {
|
|
851
917
|
const blockCount = state.walkthroughBlockCount ?? 0;
|
|
852
918
|
const alreadySettled = state.walkthroughPosted === true || state.walkthroughSkipped === true || state.e2eRun !== true;
|
|
@@ -884,6 +950,7 @@ var E2E_SKIP_CODE_CLAIMS = {
|
|
|
884
950
|
};
|
|
885
951
|
var USER_WAIVE_PHRASE = "SKIP E2E";
|
|
886
952
|
var DEV_SERVER_PROBE_TIMEOUT_MS = 2e3;
|
|
953
|
+
var GIT_PROBE_TIMEOUT_MS = 5e3;
|
|
887
954
|
var WEB_SERVER_COMMAND = /\b(vite|next\s+(dev|start)|react-scripts|ng\s+serve|nuxt|astro\s+dev|remix\s+dev|webpack(-dev)?-server|http-server|serve\s+-s|gatsby\s+develop|vue-cli-service\s+serve)\b/;
|
|
888
955
|
var BROWSER_FRAMEWORK_PACKAGES = [
|
|
889
956
|
"react-dom",
|
|
@@ -942,11 +1009,13 @@ var GITHUB_PR_URL = /^https:\/\/github\.com\/([^/\s]+)\/([^/\s]+)\/pull\/(\d+)/;
|
|
|
942
1009
|
var GH_CALLS_ENV = "MUGGLE_GUARDRAIL_GH_CALLS";
|
|
943
1010
|
var defaultGhRunner = (args, input2) => {
|
|
944
1011
|
if (process.env[GH_CALLS_ENV] === "off") return null;
|
|
1012
|
+
const timeoutMs = boundedChildTimeoutMs(GH_COMMENT_TIMEOUT_MS);
|
|
1013
|
+
if (timeoutMs === null) return null;
|
|
945
1014
|
try {
|
|
946
1015
|
return execFileSync("gh", args, {
|
|
947
1016
|
encoding: "utf-8",
|
|
948
1017
|
input: input2,
|
|
949
|
-
timeout:
|
|
1018
|
+
timeout: timeoutMs,
|
|
950
1019
|
stdio: ["pipe", "pipe", "ignore"],
|
|
951
1020
|
windowsHide: true
|
|
952
1021
|
});
|
|
@@ -1184,10 +1253,13 @@ var listFiles = (dir) => {
|
|
|
1184
1253
|
}
|
|
1185
1254
|
};
|
|
1186
1255
|
var runGit = (args, cwd) => {
|
|
1256
|
+
const timeoutMs = boundedChildTimeoutMs(GIT_PROBE_TIMEOUT_MS);
|
|
1257
|
+
if (timeoutMs === null) return null;
|
|
1187
1258
|
try {
|
|
1188
1259
|
return execFileSync("git", args, {
|
|
1189
1260
|
cwd,
|
|
1190
1261
|
encoding: "utf8",
|
|
1262
|
+
timeout: timeoutMs,
|
|
1191
1263
|
stdio: ["ignore", "pipe", "ignore"],
|
|
1192
1264
|
windowsHide: true
|
|
1193
1265
|
});
|
|
@@ -1208,10 +1280,13 @@ var hostAndPort = (url) => {
|
|
|
1208
1280
|
var isReachable = (url) => {
|
|
1209
1281
|
const target = hostAndPort(url);
|
|
1210
1282
|
if (!target) return false;
|
|
1211
|
-
const
|
|
1283
|
+
const spawnTimeoutMs = boundedChildTimeoutMs(DEV_SERVER_PROBE_TIMEOUT_MS * 2);
|
|
1284
|
+
if (spawnTimeoutMs === null) return false;
|
|
1285
|
+
const socketTimeoutMs = Math.min(DEV_SERVER_PROBE_TIMEOUT_MS, Math.floor(spawnTimeoutMs / 2));
|
|
1286
|
+
const script = `const net=require('net');const s=net.connect(${target.port},${JSON.stringify(target.host)});s.setTimeout(${socketTimeoutMs});s.on('connect',()=>{s.destroy();process.exit(0)});s.on('timeout',()=>{s.destroy();process.exit(1)});s.on('error',()=>process.exit(1));`;
|
|
1212
1287
|
try {
|
|
1213
1288
|
execFileSync(process.execPath, ["-e", script], {
|
|
1214
|
-
timeout:
|
|
1289
|
+
timeout: spawnTimeoutMs,
|
|
1215
1290
|
stdio: "ignore",
|
|
1216
1291
|
windowsHide: true
|
|
1217
1292
|
});
|
|
@@ -1614,6 +1689,7 @@ function readStdin() {
|
|
|
1614
1689
|
}
|
|
1615
1690
|
var host = resolveHookHost();
|
|
1616
1691
|
var sub = process.argv[2];
|
|
1692
|
+
if (!isBackgroundJob(sub)) enforceHookChildProcessDeadline();
|
|
1617
1693
|
var input = readStdin();
|
|
1618
1694
|
var sessionId = input.session_id ?? "unknown";
|
|
1619
1695
|
function releaseGate(field) {
|
|
@@ -1634,7 +1710,10 @@ function prOpened() {
|
|
|
1634
1710
|
if (!url) return "{}";
|
|
1635
1711
|
if (readState(sessionId).prsHandled.includes(url)) return "{}";
|
|
1636
1712
|
markPrHandled(sessionId, url);
|
|
1637
|
-
|
|
1713
|
+
startBackgroundJob("reserve-walkthrough-comment" /* ReserveWalkthroughComment */, [url]);
|
|
1714
|
+
if (isWalkthroughGateArmed(readState(sessionId))) {
|
|
1715
|
+
startBackgroundJob("scan-walkthroughs" /* ScanWalkthroughs */, [sessionId], input.cwd);
|
|
1716
|
+
}
|
|
1638
1717
|
const ctx = `A pull request was just opened: ${url}
|
|
1639
1718
|
Its Muggle AI visual-walkthrough comment is reserved and empty \u2014 settle it by posting the walkthrough there once E2E runs, or by recording why E2E does not apply.
|
|
1640
1719
|
Per the autoWatchPR preference, a muggle-pr-followup watcher should handle its incoming reviews. If autoWatchPR=always, start it now by invoking ${muggleSkill("/muggle:muggle-pr-followup")} with the PR URL; if =ask, offer it to the user; if =never, do nothing.`;
|
|
@@ -1679,7 +1758,8 @@ function terminalGate() {
|
|
|
1679
1758
|
}
|
|
1680
1759
|
function recordTests() {
|
|
1681
1760
|
const cmd = input.tool_input?.command ?? "";
|
|
1682
|
-
const
|
|
1761
|
+
const stateBefore = readState(sessionId);
|
|
1762
|
+
const judgedSkip = judgeE2eSkip(input, stateBefore);
|
|
1683
1763
|
const run = {
|
|
1684
1764
|
unitTestPassed: isTestCommand(cmd) && testsPassed(input),
|
|
1685
1765
|
e2eRan: isE2ERun(input),
|
|
@@ -1696,6 +1776,9 @@ function recordTests() {
|
|
|
1696
1776
|
return failedRunId ? applyFailedRun(withWalkthroughSkip, failedRunId) : withWalkthroughSkip;
|
|
1697
1777
|
};
|
|
1698
1778
|
const next = commitState(sessionId, record) ?? record(readState(sessionId));
|
|
1779
|
+
if (isWalkthroughGateArmed(next) && !isWalkthroughGateArmed(stateBefore)) {
|
|
1780
|
+
startBackgroundJob("scan-walkthroughs" /* ScanWalkthroughs */, [sessionId], input.cwd);
|
|
1781
|
+
}
|
|
1699
1782
|
if (judgedSkip?.accepted) recordSkipReasonOnPrs(next, judgedSkip.skip);
|
|
1700
1783
|
if (judgedSkip && !judgedSkip.accepted) {
|
|
1701
1784
|
return envelope("PostToolUse", explainRejection(judgedSkip), host);
|
|
@@ -1704,7 +1787,7 @@ function recordTests() {
|
|
|
1704
1787
|
}
|
|
1705
1788
|
function recordSkipReasonOnPrs(state, skip) {
|
|
1706
1789
|
for (const prUrl of state.prsHandled) {
|
|
1707
|
-
|
|
1790
|
+
startBackgroundJob("settle-walkthrough-skipped" /* SettleWalkthroughSkipped */, [prUrl, skip.code, skip.detail]);
|
|
1708
1791
|
}
|
|
1709
1792
|
}
|
|
1710
1793
|
function skillStages() {
|
|
@@ -1778,7 +1861,9 @@ function e2eGate() {
|
|
|
1778
1861
|
const state = readState(sessionId);
|
|
1779
1862
|
const decision = e2eGateDecision(state);
|
|
1780
1863
|
if (decision.action === "release" /* Release */) {
|
|
1781
|
-
for (const prUrl of state.prsHandled)
|
|
1864
|
+
for (const prUrl of state.prsHandled) {
|
|
1865
|
+
startBackgroundJob("settle-walkthrough-unreasoned" /* SettleWalkthroughUnreasoned */, [prUrl]);
|
|
1866
|
+
}
|
|
1782
1867
|
return releaseGate("e2eReleased");
|
|
1783
1868
|
}
|
|
1784
1869
|
if (decision.action === "none" /* None */) return "{}";
|
|
@@ -1811,10 +1896,10 @@ function buildFollowthroughGate() {
|
|
|
1811
1896
|
}
|
|
1812
1897
|
function walkthroughGate() {
|
|
1813
1898
|
const state = readState(sessionId);
|
|
1814
|
-
if (state
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1899
|
+
if (!isWalkthroughGateArmed(state)) return "{}";
|
|
1900
|
+
const scan = readWalkthroughScan(sessionId);
|
|
1901
|
+
startBackgroundJob("scan-walkthroughs" /* ScanWalkthroughs */, [sessionId], input.cwd);
|
|
1902
|
+
if (!scan || !isWalkthroughScanCurrent(scan, state)) return "{}";
|
|
1818
1903
|
if (scan.owed.length === 0) {
|
|
1819
1904
|
if (scan.verified.length > 0) writeState({ ...state, walkthroughPosted: true });
|
|
1820
1905
|
return "{}";
|
|
@@ -1965,4 +2050,35 @@ var handlers = {
|
|
|
1965
2050
|
"stage-gate": stageGate,
|
|
1966
2051
|
"debug-path-gate": debugPathGate
|
|
1967
2052
|
};
|
|
1968
|
-
process.
|
|
2053
|
+
var jobArgs = process.argv.slice(3);
|
|
2054
|
+
function scanWalkthroughsJob() {
|
|
2055
|
+
const scannedSessionId = jobArgs[0] ?? "unknown";
|
|
2056
|
+
const state = readState(scannedSessionId);
|
|
2057
|
+
const scan = scanForOwedWalkthroughs(state);
|
|
2058
|
+
writeWalkthroughScan(scannedSessionId, { prsHandled: state.prsHandled, owed: scan.owed, verified: scan.verified });
|
|
2059
|
+
return "{}";
|
|
2060
|
+
}
|
|
2061
|
+
function reserveWalkthroughCommentJob() {
|
|
2062
|
+
const [prUrl] = jobArgs;
|
|
2063
|
+
if (prUrl) reserveWalkthroughComment(prUrl);
|
|
2064
|
+
return "{}";
|
|
2065
|
+
}
|
|
2066
|
+
function settleWalkthroughSkippedJob() {
|
|
2067
|
+
const [prUrl, codeArg, detail] = jobArgs;
|
|
2068
|
+
const code = E2E_SKIP_CODES.find((skipCode) => skipCode === codeArg);
|
|
2069
|
+
if (prUrl && code) settleWalkthroughCommentAsSkipped(prUrl, code, detail ?? "");
|
|
2070
|
+
return "{}";
|
|
2071
|
+
}
|
|
2072
|
+
function settleWalkthroughUnreasonedJob() {
|
|
2073
|
+
const [prUrl] = jobArgs;
|
|
2074
|
+
if (prUrl) settleWalkthroughCommentAsUnreasoned(prUrl);
|
|
2075
|
+
return "{}";
|
|
2076
|
+
}
|
|
2077
|
+
var backgroundJobs = {
|
|
2078
|
+
["scan-walkthroughs" /* ScanWalkthroughs */]: scanWalkthroughsJob,
|
|
2079
|
+
["reserve-walkthrough-comment" /* ReserveWalkthroughComment */]: reserveWalkthroughCommentJob,
|
|
2080
|
+
["settle-walkthrough-skipped" /* SettleWalkthroughSkipped */]: settleWalkthroughSkippedJob,
|
|
2081
|
+
["settle-walkthrough-unreasoned" /* SettleWalkthroughUnreasoned */]: settleWalkthroughUnreasonedJob
|
|
2082
|
+
};
|
|
2083
|
+
var runSubcommand = isBackgroundJob(sub) ? backgroundJobs[sub] : handlers[sub] ?? (() => "{}");
|
|
2084
|
+
process.stdout.write(runSubcommand());
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
|
|
3
|
+
set -uo pipefail
|
|
4
|
+
|
|
5
|
+
# The slow session-start upkeep, launched detached by ensure-electron-app.sh at most
|
|
6
|
+
# once a day so no hook waits on it: state GC and the Electron runner install daily,
|
|
7
|
+
# and the npm version lookup every three days. The due stamp and the install's marker
|
|
8
|
+
# are claimed before the work starts, so sessions opening together run it once rather
|
|
9
|
+
# than once each. The hook payload arrives on stdin and is handed on to gc-state.sh.
|
|
10
|
+
|
|
11
|
+
# Epoch mtime of a file, or 0 if unreadable. GNU coreutils (Linux/WSL/Git Bash)
|
|
12
|
+
# need `stat -c %Y`; BSD/macOS need `stat -f %m`. The wrong form doesn't fail
|
|
13
|
+
# cleanly — GNU `stat -f` prints filesystem info to stdout and exits non-zero —
|
|
14
|
+
# so each form is tried alone and accepted only when all-digits, keeping stray
|
|
15
|
+
# text like `File:` out of the caller's arithmetic.
|
|
16
|
+
file_mtime() {
|
|
17
|
+
local f="$1" m
|
|
18
|
+
m=$(stat -c %Y "$f" 2>/dev/null) || m=""
|
|
19
|
+
case "$m" in ''|*[!0-9]*) ;; *) printf '%s\n' "$m"; return 0 ;; esac
|
|
20
|
+
m=$(stat -f %m "$f" 2>/dev/null) || m=""
|
|
21
|
+
case "$m" in ''|*[!0-9]*) ;; *) printf '%s\n' "$m"; return 0 ;; esac
|
|
22
|
+
printf '0\n'
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
# Sourcing with MUGGLE_SESSION_MAINTENANCE_LIB_ONLY=1 exposes the helper above
|
|
26
|
+
# without running the upkeep — used by the unit test.
|
|
27
|
+
if [ -n "${MUGGLE_SESSION_MAINTENANCE_LIB_ONLY:-}" ]; then
|
|
28
|
+
return 0 2>/dev/null || exit 0
|
|
29
|
+
fi
|
|
30
|
+
|
|
31
|
+
maintenance_dir="${BASH_SOURCE[0]%/*}"
|
|
32
|
+
[ "$maintenance_dir" = "${BASH_SOURCE[0]}" ] && maintenance_dir=.
|
|
33
|
+
cache_dir="${HOME}/.cache/muggle"
|
|
34
|
+
electron_marker="${cache_dir}/electron-app-checked"
|
|
35
|
+
electron_ttl=$((24 * 60 * 60))
|
|
36
|
+
version_cache="${cache_dir}/version-check"
|
|
37
|
+
version_ttl=$((3 * 24 * 60 * 60))
|
|
38
|
+
upgrade_available="${cache_dir}/upgrade-available"
|
|
39
|
+
|
|
40
|
+
maintenance_stamp="${cache_dir}/session-maintenance-due"
|
|
41
|
+
# Matches the shortest TTL below, so the stamp never holds back work that is due.
|
|
42
|
+
maintenance_interval=$((24 * 60 * 60))
|
|
43
|
+
|
|
44
|
+
mkdir -p "$cache_dir" 2>/dev/null || true
|
|
45
|
+
now=$(date +%s)
|
|
46
|
+
printf '%s\n' "$((now + maintenance_interval))" > "$maintenance_stamp" 2>/dev/null || true
|
|
47
|
+
|
|
48
|
+
# Lets the other session-start hooks answer first: on Windows a burst of process
|
|
49
|
+
# starts here would slow theirs.
|
|
50
|
+
sleep 2
|
|
51
|
+
|
|
52
|
+
bash "${maintenance_dir}/gc-state.sh" || true
|
|
53
|
+
|
|
54
|
+
is_due() {
|
|
55
|
+
local marker="$1" ttl="$2"
|
|
56
|
+
[ ! -f "$marker" ] && return 0
|
|
57
|
+
[ $((now - $(file_mtime "$marker"))) -ge "$ttl" ]
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
# A hung `muggle setup` (e.g. one blocked by host security policy on Windows)
|
|
61
|
+
# must not leak a ~100 MB orphan per session start, so the attempt is capped.
|
|
62
|
+
if is_due "$electron_marker" "$electron_ttl"; then
|
|
63
|
+
touch "$electron_marker" 2>/dev/null || true
|
|
64
|
+
setup_timeout=""
|
|
65
|
+
command -v timeout >/dev/null 2>&1 && setup_timeout="timeout -k 5 60"
|
|
66
|
+
if command -v muggle >/dev/null 2>&1; then
|
|
67
|
+
${setup_timeout} muggle setup >/dev/null 2>&1 || true
|
|
68
|
+
else
|
|
69
|
+
${setup_timeout} npx -y @muggleai/works setup >/dev/null 2>&1 || true
|
|
70
|
+
fi
|
|
71
|
+
fi
|
|
72
|
+
|
|
73
|
+
# The cache holds "installed|latest". It is not claimed up front: a failed lookup
|
|
74
|
+
# leaves it due, so the next start retries rather than waiting out the TTL.
|
|
75
|
+
if is_due "$version_cache" "$version_ttl"; then
|
|
76
|
+
installed=$(muggle --version 2>/dev/null | tr -d '[:space:]' || true)
|
|
77
|
+
latest=$(npm view @muggleai/works version 2>/dev/null | tr -d '[:space:]' || true)
|
|
78
|
+
if [ -n "$installed" ] && [ -n "$latest" ]; then
|
|
79
|
+
printf '%s|%s' "$installed" "$latest" > "$version_cache" 2>/dev/null || true
|
|
80
|
+
fi
|
|
81
|
+
fi
|
|
82
|
+
|
|
83
|
+
# ensure-electron-app.sh announces an upgrade only while this file exists, so the
|
|
84
|
+
# version comparison (sort -V) never runs on the hook's own path.
|
|
85
|
+
cached_versions=$(cat "$version_cache" 2>/dev/null || true)
|
|
86
|
+
installed="${cached_versions%%|*}"
|
|
87
|
+
latest="${cached_versions##*|}"
|
|
88
|
+
newest=""
|
|
89
|
+
if [ -n "$installed" ] && [ -n "$latest" ] && [ "$installed" != "$latest" ]; then
|
|
90
|
+
newest=$(printf '%s\n%s\n' "$installed" "$latest" | sort -V | tail -n1)
|
|
91
|
+
fi
|
|
92
|
+
if [ -n "$newest" ] && [ "$newest" = "$latest" ]; then
|
|
93
|
+
printf '%s|%s' "$installed" "$latest" > "$upgrade_available" 2>/dev/null || true
|
|
94
|
+
else
|
|
95
|
+
rm -f "$upgrade_available" 2>/dev/null || true
|
|
96
|
+
fi
|
|
97
|
+
|
|
98
|
+
exit 0
|