@muggleai/works 5.22.0-staging.157 → 5.22.0-staging.159

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 (47) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/README.md +3 -1
  3. package/dist/{chunk-E3T2RN6R.js → chunk-M2PJRVCC.js} +16 -0
  4. package/dist/{chunk-DLBPRPUN.js → chunk-SF75W2SV.js} +217 -47
  5. package/dist/cli.js +2 -2
  6. package/dist/index.js +2 -2
  7. package/dist/plugin/.claude-plugin/plugin.json +1 -1
  8. package/dist/plugin/.codex-plugin/plugin.json +1 -1
  9. package/dist/plugin/.cursor-plugin/plugin.json +1 -1
  10. package/dist/plugin/config/preference-defaults.json +1 -0
  11. package/dist/plugin/hooks/README.md +15 -7
  12. package/dist/plugin/hooks/codex-hooks.json +3 -10
  13. package/dist/plugin/hooks/hooks.json +14 -20
  14. package/dist/plugin/scripts/ensure-electron-app.sh +133 -186
  15. package/dist/plugin/scripts/guardrail-lib.sh +15 -9
  16. package/dist/plugin/scripts/guardrails.mjs +137 -21
  17. package/dist/plugin/scripts/session-maintenance.sh +98 -0
  18. package/dist/plugin/skills/_shared/ci-check-to-command.md +2 -1
  19. package/dist/plugin/skills/_shared/vcs/github/pr-checks.md +9 -0
  20. package/dist/plugin/skills/do/fix-ci.md +14 -4
  21. package/dist/plugin/skills/do/open-prs/forward.md +2 -0
  22. package/dist/plugin/skills/do/pre-flight.md +2 -0
  23. package/dist/plugin/skills/muggle-do/SKILL.md +1 -0
  24. package/dist/plugin/skills/muggle-preferences/ops/configure.md +1 -0
  25. package/dist/plugin/skills/muggle-preferences/preference-gates/autoInstallCiWorkflow.md +13 -0
  26. package/dist/release-manifest.json +3 -3
  27. package/dist/{src-V245RV7L.js → src-5QO7XTIG.js} +1 -1
  28. package/package.json +2 -1
  29. package/plugin/.claude-plugin/plugin.json +1 -1
  30. package/plugin/.codex-plugin/plugin.json +1 -1
  31. package/plugin/.cursor-plugin/plugin.json +1 -1
  32. package/plugin/config/preference-defaults.json +1 -0
  33. package/plugin/hooks/README.md +15 -7
  34. package/plugin/hooks/codex-hooks.json +3 -10
  35. package/plugin/hooks/hooks.json +14 -20
  36. package/plugin/scripts/ensure-electron-app.sh +133 -186
  37. package/plugin/scripts/guardrail-lib.sh +15 -9
  38. package/plugin/scripts/guardrails.mjs +137 -21
  39. package/plugin/scripts/session-maintenance.sh +98 -0
  40. package/plugin/skills/_shared/ci-check-to-command.md +2 -1
  41. package/plugin/skills/_shared/vcs/github/pr-checks.md +9 -0
  42. package/plugin/skills/do/fix-ci.md +14 -4
  43. package/plugin/skills/do/open-prs/forward.md +2 -0
  44. package/plugin/skills/do/pre-flight.md +2 -0
  45. package/plugin/skills/muggle-do/SKILL.md +1 -0
  46. package/plugin/skills/muggle-preferences/ops/configure.md +1 -0
  47. package/plugin/skills/muggle-preferences/preference-gates/autoInstallCiWorkflow.md +13 -0
@@ -9,7 +9,7 @@
9
9
  "type": "command",
10
10
  "command": "env MUGGLE_HOOK_HOST=codex bash \"${CLAUDE_PLUGIN_ROOT}/scripts/ensure-electron-app.sh\"",
11
11
  "async": false,
12
- "timeout": 120,
12
+ "timeout": 10,
13
13
  "commandWindows": "node \"${CLAUDE_PLUGIN_ROOT}\\scripts\\codex-hook-windows.mjs\" \"${CLAUDE_PLUGIN_ROOT}/scripts/ensure-electron-app.sh\""
14
14
  },
15
15
  {
@@ -18,13 +18,6 @@
18
18
  "async": false,
19
19
  "timeout": 10,
20
20
  "commandWindows": "node \"${CLAUDE_PLUGIN_ROOT}\\scripts\\codex-hook-windows.mjs\" \"${CLAUDE_PLUGIN_ROOT}/scripts/reconcile-stale-watchers.sh\""
21
- },
22
- {
23
- "type": "command",
24
- "command": "env MUGGLE_HOOK_HOST=codex bash \"${CLAUDE_PLUGIN_ROOT}/scripts/gc-state.sh\"",
25
- "async": false,
26
- "timeout": 10,
27
- "commandWindows": "node \"${CLAUDE_PLUGIN_ROOT}\\scripts\\codex-hook-windows.mjs\" \"${CLAUDE_PLUGIN_ROOT}/scripts/gc-state.sh\""
28
21
  }
29
22
  ]
30
23
  }
@@ -63,7 +56,7 @@
63
56
  "type": "command",
64
57
  "command": "env MUGGLE_HOOK_HOST=codex bash \"${CLAUDE_PLUGIN_ROOT}/scripts/guardrail-dispatch.sh\" guardrail-pr-opened.sh guardrail-record-tests.sh guardrail-pr-terminal.sh guardrail-record-stage-signals.sh guardrail-record-comment-replies.sh guardrail-record-stage-read.sh",
65
58
  "async": false,
66
- "timeout": 25,
59
+ "timeout": 10,
67
60
  "commandWindows": "node \"${CLAUDE_PLUGIN_ROOT}\\scripts\\codex-hook-windows.mjs\" \"${CLAUDE_PLUGIN_ROOT}/scripts/guardrail-dispatch.sh\" guardrail-pr-opened.sh guardrail-record-tests.sh guardrail-pr-terminal.sh guardrail-record-stage-signals.sh guardrail-record-comment-replies.sh guardrail-record-stage-read.sh"
68
61
  }
69
62
  ]
@@ -124,7 +117,7 @@
124
117
  "type": "command",
125
118
  "command": "env MUGGLE_HOOK_HOST=codex bash \"${CLAUDE_PLUGIN_ROOT}/scripts/guardrail-dispatch.sh\" guardrail-e2e-gate.sh guardrail-terminal-gate.sh guardrail-watch-gate.sh guardrail-build-followthrough-gate.sh guardrail-walkthrough-gate.sh guardrail-comment-reply-gate.sh guardrail-stage-gate.sh guardrail-debug-path-gate.sh guardrail-capability-claim.sh",
126
119
  "async": false,
127
- "timeout": 30,
120
+ "timeout": 10,
128
121
  "commandWindows": "node \"${CLAUDE_PLUGIN_ROOT}\\scripts\\codex-hook-windows.mjs\" \"${CLAUDE_PLUGIN_ROOT}/scripts/guardrail-dispatch.sh\" guardrail-e2e-gate.sh guardrail-terminal-gate.sh guardrail-watch-gate.sh guardrail-build-followthrough-gate.sh guardrail-walkthrough-gate.sh guardrail-comment-reply-gate.sh guardrail-stage-gate.sh guardrail-debug-path-gate.sh guardrail-capability-claim.sh"
129
122
  }
130
123
  ]
@@ -8,19 +8,13 @@
8
8
  "type": "command",
9
9
  "command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/ensure-electron-app.sh\"",
10
10
  "async": false,
11
- "timeout": 120
11
+ "timeout": 1
12
12
  },
13
13
  {
14
14
  "type": "command",
15
15
  "command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/reconcile-stale-watchers.sh\"",
16
16
  "async": false,
17
- "timeout": 10
18
- },
19
- {
20
- "type": "command",
21
- "command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/gc-state.sh\"",
22
- "async": false,
23
- "timeout": 10
17
+ "timeout": 1
24
18
  }
25
19
  ]
26
20
  }
@@ -33,7 +27,7 @@
33
27
  "type": "command",
34
28
  "command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/guardrail-dispatch.sh\" guardrail-report-format.sh guardrail-resolve-gate.sh guardrail-walkthrough-heading.sh",
35
29
  "async": false,
36
- "timeout": 10
30
+ "timeout": 1
37
31
  }
38
32
  ]
39
33
  },
@@ -44,7 +38,7 @@
44
38
  "type": "command",
45
39
  "command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/guardrail-classify-gate.sh\"",
46
40
  "async": false,
47
- "timeout": 10
41
+ "timeout": 1
48
42
  }
49
43
  ]
50
44
  }
@@ -57,7 +51,7 @@
57
51
  "type": "command",
58
52
  "command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/guardrail-dispatch.sh\" guardrail-pr-opened.sh guardrail-record-tests.sh guardrail-pr-terminal.sh guardrail-record-stage-signals.sh guardrail-record-comment-replies.sh guardrail-record-stage-read.sh",
59
53
  "async": false,
60
- "timeout": 25
54
+ "timeout": 1
61
55
  }
62
56
  ]
63
57
  },
@@ -68,7 +62,7 @@
68
62
  "type": "command",
69
63
  "command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/guardrail-record-tests.sh\"",
70
64
  "async": false,
71
- "timeout": 10
65
+ "timeout": 1
72
66
  }
73
67
  ]
74
68
  },
@@ -79,7 +73,7 @@
79
73
  "type": "command",
80
74
  "command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/guardrail-record-stage-signals.sh\"",
81
75
  "async": false,
82
- "timeout": 10
76
+ "timeout": 1
83
77
  }
84
78
  ]
85
79
  },
@@ -90,7 +84,7 @@
90
84
  "type": "command",
91
85
  "command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/guardrail-skill-stages.sh\"",
92
86
  "async": false,
93
- "timeout": 10
87
+ "timeout": 1
94
88
  }
95
89
  ]
96
90
  },
@@ -101,7 +95,7 @@
101
95
  "type": "command",
102
96
  "command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/guardrail-skill-stages.sh\"",
103
97
  "async": false,
104
- "timeout": 10
98
+ "timeout": 1
105
99
  }
106
100
  ]
107
101
  },
@@ -112,7 +106,7 @@
112
106
  "type": "command",
113
107
  "command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/guardrail-record-stage-read.sh\"",
114
108
  "async": false,
115
- "timeout": 10
109
+ "timeout": 1
116
110
  }
117
111
  ]
118
112
  },
@@ -123,7 +117,7 @@
123
117
  "type": "command",
124
118
  "command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/guardrail-pr-terminal.sh\"",
125
119
  "async": false,
126
- "timeout": 10
120
+ "timeout": 1
127
121
  }
128
122
  ]
129
123
  },
@@ -134,7 +128,7 @@
134
128
  "type": "command",
135
129
  "command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/guardrail-offer-ran.sh\"",
136
130
  "async": false,
137
- "timeout": 10
131
+ "timeout": 1
138
132
  }
139
133
  ]
140
134
  }
@@ -146,7 +140,7 @@
146
140
  "type": "command",
147
141
  "command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/guardrail-dispatch.sh\" guardrail-e2e-gate.sh guardrail-terminal-gate.sh guardrail-watch-gate.sh guardrail-build-followthrough-gate.sh guardrail-walkthrough-gate.sh guardrail-comment-reply-gate.sh guardrail-stage-gate.sh guardrail-debug-path-gate.sh guardrail-capability-claim.sh",
148
142
  "async": false,
149
- "timeout": 30
143
+ "timeout": 1
150
144
  }
151
145
  ]
152
146
  }
@@ -158,7 +152,7 @@
158
152
  "type": "command",
159
153
  "command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/guardrail-build-router.sh\"",
160
154
  "async": false,
161
- "timeout": 10
155
+ "timeout": 1
162
156
  }
163
157
  ]
164
158
  }
@@ -2,62 +2,42 @@
2
2
 
3
3
  set -euo pipefail
4
4
 
5
- # Epoch mtime of a file, or 0 if unreadable. GNU coreutils (Linux/WSL/Git Bash)
6
- # need `stat -c %Y`; BSD/macOS need `stat -f %m`. The wrong form doesn't fail
7
- # cleanly — GNU `stat -f` prints filesystem info to stdout and exits non-zero —
8
- # so each form is tried alone and accepted only when all-digits, keeping stray
9
- # text like `File:` out of the caller's arithmetic.
10
- file_mtime() {
11
- local f="$1" m
12
- m=$(stat -c %Y "$f" 2>/dev/null) || m=""
13
- case "$m" in ''|*[!0-9]*) ;; *) printf '%s\n' "$m"; return 0 ;; esac
14
- m=$(stat -f %m "$f" 2>/dev/null) || m=""
15
- case "$m" in ''|*[!0-9]*) ;; *) printf '%s\n' "$m"; return 0 ;; esac
16
- printf '0\n'
17
- }
18
-
19
- # Sourcing with MUGGLE_ENSURE_ELECTRON_LIB_ONLY=1 exposes the helper above
20
- # without running the hook — used by the unit test.
21
- if [ -n "${MUGGLE_ENSURE_ELECTRON_LIB_ONLY:-}" ]; then
22
- return 0 2>/dev/null || exit 0
5
+ # Injects the session context: when to reach for the muggle tools, the resolved
6
+ # preferences, the last-used project and host, and an upgrade notice. A SessionStart
7
+ # hook must answer inside the hook runtime budget, so the slow upkeep (the Electron
8
+ # runner install, the npm version lookup, state GC) runs detached in
9
+ # session-maintenance.sh and only its cached results are read here.
10
+
11
+ hook_dir="${BASH_SOURCE[0]%/*}"
12
+ [ "$hook_dir" = "${BASH_SOURCE[0]}" ] && hook_dir=.
13
+ . "${hook_dir}/guardrail-lib.sh"
14
+ guardrail_read_payload
15
+
16
+ # Refreshed on every start, so the inactivity sweep in gc-state.sh never collects
17
+ # a resumed session between its daily runs.
18
+ guardrail_resolve_state_file
19
+ if [ -f "$GUARDRAIL_STATE_FILE" ]; then
20
+ touch "$GUARDRAIL_STATE_FILE" 2>/dev/null || true
23
21
  fi
24
22
 
25
- # Ensure the Electron browser test runner is installed/up to date (silent, best-effort).
26
- #
27
- # Bounded + cached: this script runs from a SessionStart hook on every Claude
28
- # session, so a hung `muggle setup` (e.g. one blocked by host security policy
29
- # on Windows) must not leak a ~100 MB orphan per session start. We cap the
30
- # attempt with `timeout` and skip the call entirely if we already checked
31
- # within the last day.
32
- ensure_marker_dir="${HOME}/.cache/muggle"
33
- ensure_marker="${ensure_marker_dir}/electron-app-checked"
34
- ensure_ttl=$((24 * 60 * 60))
35
-
36
- ensure_now=$(date +%s)
37
- ensure_last=0
38
- if [ -f "${ensure_marker}" ]; then
39
- ensure_last=$(file_mtime "${ensure_marker}")
23
+ # The upkeep stamps the epoch second it next comes due, so a start inside that
24
+ # window launches nothing. Without EPOCHSECONDS (bash before 5) it always
25
+ # launches and the upkeep's own markers decide. Every stream is redirected so the
26
+ # harness sees this hook finish without waiting on it, and it runs at the lowest
27
+ # priority so its npm and install work never starves the hooks of the turn it
28
+ # overlaps.
29
+ upkeep_priority=""
30
+ command -v nice >/dev/null 2>&1 && upkeep_priority="nice -n 19"
31
+ maintenance_due_at=0
32
+ maintenance_stamp="${HOME}/.cache/muggle/session-maintenance-due"
33
+ if [ -f "$maintenance_stamp" ]; then
34
+ read -r maintenance_due_at <"$maintenance_stamp" || true
40
35
  fi
41
-
42
- if [ $((ensure_now - ensure_last)) -ge "${ensure_ttl}" ]; then
43
- if command -v timeout >/dev/null 2>&1; then
44
- ensure_timeout="timeout -k 5 60"
45
- else
46
- ensure_timeout=""
47
- fi
48
- if command -v muggle >/dev/null 2>&1; then
49
- ${ensure_timeout} muggle setup >/dev/null 2>&1 || true
50
- else
51
- ${ensure_timeout} npx -y @muggleai/works setup >/dev/null 2>&1 || true
52
- fi
53
- mkdir -p "${ensure_marker_dir}" 2>/dev/null || true
54
- touch "${ensure_marker}" 2>/dev/null || true
36
+ case "$maintenance_due_at" in ''|*[!0-9]*) maintenance_due_at=0 ;; esac
37
+ if [ -z "${EPOCHSECONDS:-}" ] || [ "$EPOCHSECONDS" -ge "$maintenance_due_at" ]; then
38
+ ${upkeep_priority} bash "${hook_dir}/session-maintenance.sh" <<<"$GUARDRAIL_PAYLOAD" >/dev/null 2>&1 &
55
39
  fi
56
40
 
57
- # --- Context injection ---
58
- # Inject instructions into Claude's context so the agent knows when to use
59
- # muggle tools for E2E acceptance testing, browser testing, and UI validation.
60
-
61
41
  escape_for_json() {
62
42
  local s="$1"
63
43
  s="${s//\\/\\\\}"
@@ -68,162 +48,129 @@ escape_for_json() {
68
48
  printf '%s' "$s"
69
49
  }
70
50
 
71
- # --- Version check (best-effort, 3-day cache) ---
72
- # Writes "installed|latest" to a cache file. On a cache hit we skip the npm
73
- # round-trip entirely. Any failure leaves upgrade_notice empty and we stay silent.
51
+ # --- Upgrade notice ---
52
+ # session-maintenance.sh leaves "installed|latest" here only while latest is newer.
74
53
  upgrade_notice=""
75
- version_check() {
76
- local cache_dir="${HOME}/.cache/muggle"
77
- local cache_file="${cache_dir}/version-check"
78
- local ttl=$((3 * 24 * 60 * 60))
79
- local now installed latest cached mtime age
80
- now=$(date +%s)
81
-
82
- if [ -f "$cache_file" ]; then
83
- mtime=$(file_mtime "$cache_file")
84
- age=$((now - mtime))
85
- if [ "$age" -lt "$ttl" ]; then
86
- cached=$(cat "$cache_file" 2>/dev/null || true)
87
- installed="${cached%%|*}"
88
- latest="${cached##*|}"
89
- fi
54
+ upgrade_available="${HOME}/.cache/muggle/upgrade-available"
55
+ if [ -f "$upgrade_available" ]; then
56
+ installed_version=""
57
+ latest_version=""
58
+ IFS='|' read -r installed_version latest_version <"$upgrade_available" || true
59
+ if [ -n "$installed_version" ] && [ -n "$latest_version" ]; then
60
+ upgrade_notice="\n\nA newer @muggleai/works is available (${installed_version} → ${latest_version}). Tell the user to run \`/muggle:muggle-upgrade\` to update."
90
61
  fi
62
+ fi
91
63
 
92
- if [ -z "${installed:-}" ] || [ -z "${latest:-}" ]; then
93
- installed=$(muggle --version 2>/dev/null | tr -d '[:space:]' || true)
94
- latest=$(npm view @muggleai/works version 2>/dev/null | tr -d '[:space:]' || true)
95
- if [ -n "$installed" ] && [ -n "$latest" ]; then
96
- mkdir -p "$cache_dir" 2>/dev/null || true
97
- printf '%s|%s' "$installed" "$latest" > "$cache_file" 2>/dev/null || true
98
- fi
99
- fi
100
-
101
- if [ -n "${installed:-}" ] && [ -n "${latest:-}" ] && [ "$installed" != "$latest" ]; then
102
- # Only nudge when latest is strictly greater (sort -V puts latest last).
103
- local newest
104
- newest=$(printf '%s\n%s\n' "$installed" "$latest" | sort -V | tail -n1)
105
- if [ "$newest" = "$latest" ]; then
106
- upgrade_notice="\n\nA newer @muggleai/works is available (${installed} → ${latest}). Tell the user to run \`/muggle:muggle-upgrade\` to update."
107
- fi
108
- fi
109
- }
110
- version_check || true
111
-
112
- # --- Preferences injection ---
64
+ # --- Preferences and last-used cache injection ---
113
65
  # Preferences are user-level: defaults overlaid by the global file only. A legacy
114
66
  # <cwd>/.muggle-ai/preferences.json is left on disk but no longer read, so the
115
67
  # keys it can no longer apply are named once — the stamp file suppresses the
116
68
  # repeat until that key set changes.
117
- prefs_line=""
118
- prefs_file_note=""
69
+ #
70
+ # The "last used Muggle Test project" and "last used local dev server URL"
71
+ # caches live in ~/.muggle-ai/, keyed by working directory, and are honored by
72
+ # skills when autoSelectProject / autoSelectLocalHost = always. A cache written
73
+ # before the move to the home directory still sits in <cwd>/.muggle-ai/ and is
74
+ # read as a fallback, so those sessions keep their context lines.
75
+ #
76
+ # Both are resolved in one node process, each section failing on its own.
119
77
 
120
78
  # Defaults ship as data next to this hook so the context line and the MCP tools
121
79
  # resolve a preference the same way. Hardcoding them here drifted once already.
122
- prefs_hook_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
123
- prefs_defaults_file="${prefs_hook_dir}/../config/preference-defaults.json"
124
- prefs_limits_file="${prefs_hook_dir}/../config/onboarding-limits.json"
80
+ prefs_defaults_file="${hook_dir}/../config/preference-defaults.json"
81
+ prefs_limits_file="${hook_dir}/../config/onboarding-limits.json"
125
82
 
126
83
  onboarding_directive='Muggle Test first-run setup has not been run on this machine. Before acting on the user request, acknowledge what they asked for, then offer them the one-time setup walkthrough (the `onboard` operation of the muggle-preferences skill) which explains how Muggle Test works and saves their preferences. Accepting the recommended defaults takes one keystroke. If they decline, record the skip and carry on with their request using defaults.'
127
84
 
128
85
  # A missing preferences file is not a special case: the resolver below falls back to
129
86
  # the shipped defaults, which is exactly what the code will use until setup seeds them.
130
- # Extract preferences object keys and values into a compact one-liner.
131
- # Uses node for reliable JSON parsing (already required for muggle).
132
- prefs_line=$(MUGGLE_PREFERENCE_DEFAULTS_FILE="$prefs_defaults_file" MUGGLE_ONBOARDING_LIMITS_FILE="$prefs_limits_file" MUGGLE_ONBOARDING_DIRECTIVE="$onboarding_directive" node -e "
87
+ session_notes=""
88
+ session_notes=$(MUGGLE_PREFERENCE_DEFAULTS_FILE="$prefs_defaults_file" MUGGLE_ONBOARDING_LIMITS_FILE="$prefs_limits_file" MUGGLE_ONBOARDING_DIRECTIVE="$onboarding_directive" node -e "
133
89
  const fs = require('fs');
134
90
  const os = require('os');
135
91
  const path = require('path');
136
- try {
137
- // Resolved through node, not the shell's \$HOME: under Git Bash the shell
138
- // reports a POSIX path that Windows node cannot open.
139
- const globalFile = path.join(os.homedir(), '.muggle-ai', 'preferences.json');
140
- let file = {};
141
- try { file = JSON.parse(fs.readFileSync(globalFile, 'utf-8')); } catch {}
142
- const g = file.preferences || {};
143
- let defaults = {};
144
- try { defaults = JSON.parse(fs.readFileSync(process.env.MUGGLE_PREFERENCE_DEFAULTS_FILE, 'utf-8')); } catch {}
145
- const resolved = { ...defaults, ...g };
146
- const line = Object.entries(resolved).map(([k,v]) => k+'='+v).join(' ');
147
- const blocks = ['Muggle Test Preferences (~/.muggle-ai/preferences.json):\\\\n' + line];
148
-
149
- const onboardedAt = file.onboardingCompletedAt;
150
- if (typeof onboardedAt !== 'string' || onboardedAt.length === 0) {
151
- // The offer is capped here rather than only in the skip path: a user who never
152
- // engages would otherwise be re-asked every session forever.
153
- let maxOffers = 0;
154
- try { maxOffers = JSON.parse(fs.readFileSync(process.env.MUGGLE_ONBOARDING_LIMITS_FILE, 'utf-8')).maxOffers; } catch {}
155
- const offersDir = path.join(os.homedir(), '.cache', 'muggle');
156
- const offersFile = path.join(offersDir, 'onboarding-offers');
157
- let offers = 0;
158
- try { offers = parseInt(fs.readFileSync(offersFile, 'utf-8'), 10) || 0; } catch {}
159
- if (maxOffers > 0 && offers < maxOffers) {
160
- try { fs.mkdirSync(offersDir, { recursive: true }); fs.writeFileSync(offersFile, String(offers + 1)); } catch {}
161
- blocks.push(process.env.MUGGLE_ONBOARDING_DIRECTIVE);
162
- }
163
- }
164
-
165
- const cwd = process.env.CLAUDE_CWD || process.env.CURSOR_CWD || process.cwd();
166
- const pPath = path.join(cwd, '.muggle-ai', 'preferences.json');
167
- let p = {};
168
- try { p = JSON.parse(fs.readFileSync(pPath, 'utf-8')).preferences || {}; } catch {}
169
- const inertKeys = Object.keys(p).filter((k) => p[k] !== resolved[k]).sort();
170
- if (inertKeys.length > 0) {
171
- const stampDir = path.join(os.homedir(), '.cache', 'muggle');
172
- const stampFile = path.join(stampDir, 'project-prefs-inert');
173
- const stamp = cwd + '|' + inertKeys.join(',');
174
- let lastStamp = '';
175
- try { lastStamp = fs.readFileSync(stampFile, 'utf-8'); } catch {}
176
- if (lastStamp !== stamp) {
177
- try { fs.mkdirSync(stampDir, { recursive: true }); fs.writeFileSync(stampFile, stamp); } catch {}
178
- blocks.push(
179
- 'Muggle Test: per-project preferences were removed — ' + pPath + ' is no longer read, so these keys no longer take effect: ' + inertKeys.join(', ') + '.\\\\n' +
180
- 'Tell the user to re-apply any they want everywhere with \`/muggle-preferences\`; the file is safe to delete.'
181
- );
92
+ const cwd = process.env.CLAUDE_CWD || process.env.CURSOR_CWD || process.cwd();
93
+
94
+ const preferenceNotes = () => {
95
+ try {
96
+ // Resolved through node, not the shell's \$HOME: under Git Bash the shell
97
+ // reports a POSIX path that Windows node cannot open.
98
+ const globalFile = path.join(os.homedir(), '.muggle-ai', 'preferences.json');
99
+ let file = {};
100
+ try { file = JSON.parse(fs.readFileSync(globalFile, 'utf-8')); } catch {}
101
+ const g = file.preferences || {};
102
+ let defaults = {};
103
+ try { defaults = JSON.parse(fs.readFileSync(process.env.MUGGLE_PREFERENCE_DEFAULTS_FILE, 'utf-8')); } catch {}
104
+ const resolved = { ...defaults, ...g };
105
+ const line = Object.entries(resolved).map(([k,v]) => k+'='+v).join(' ');
106
+ const blocks = ['Muggle Test Preferences (~/.muggle-ai/preferences.json):\\\\n' + line];
107
+
108
+ const onboardedAt = file.onboardingCompletedAt;
109
+ if (typeof onboardedAt !== 'string' || onboardedAt.length === 0) {
110
+ // The offer is capped here rather than only in the skip path: a user who never
111
+ // engages would otherwise be re-asked every session forever.
112
+ let maxOffers = 0;
113
+ try { maxOffers = JSON.parse(fs.readFileSync(process.env.MUGGLE_ONBOARDING_LIMITS_FILE, 'utf-8')).maxOffers; } catch {}
114
+ const offersDir = path.join(os.homedir(), '.cache', 'muggle');
115
+ const offersFile = path.join(offersDir, 'onboarding-offers');
116
+ let offers = 0;
117
+ try { offers = parseInt(fs.readFileSync(offersFile, 'utf-8'), 10) || 0; } catch {}
118
+ if (maxOffers > 0 && offers < maxOffers) {
119
+ try { fs.mkdirSync(offersDir, { recursive: true }); fs.writeFileSync(offersFile, String(offers + 1)); } catch {}
120
+ blocks.push(process.env.MUGGLE_ONBOARDING_DIRECTIVE);
121
+ }
182
122
  }
183
- }
184
- console.log(blocks.join('\\\\n\\\\n'));
185
- } catch { console.log(''); }
186
- " 2>/dev/null || true)
187
- if [ -n "$prefs_line" ]; then
188
- prefs_file_note="\\n\\n${prefs_line}"
189
- fi
190
123
 
191
- # --- Last-used cache injection ---
192
- # The "last used Muggle Test project" and "last used local dev server URL"
193
- # caches live in ~/.muggle-ai/, keyed by working directory, and are honored by
194
- # skills when autoSelectProject / autoSelectLocalHost = always. A cache written
195
- # before the move to the home directory still sits in <cwd>/.muggle-ai/ and is
196
- # read as a fallback, so those sessions keep their context lines.
197
- last_cache_notes=""
198
- last_cache_notes=$(node -e "
199
- const fs = require('fs');
200
- const os = require('os');
201
- const path = require('path');
202
- try {
203
- const cwd = process.env.CLAUDE_CWD || process.env.CURSOR_CWD || process.cwd();
204
- const parseFile = (filePath) => {
205
- try { return JSON.parse(fs.readFileSync(filePath, 'utf-8')); } catch { return null; }
124
+ const pPath = path.join(cwd, '.muggle-ai', 'preferences.json');
125
+ let p = {};
126
+ try { p = JSON.parse(fs.readFileSync(pPath, 'utf-8')).preferences || {}; } catch {}
127
+ const inertKeys = Object.keys(p).filter((k) => p[k] !== resolved[k]).sort();
128
+ if (inertKeys.length > 0) {
129
+ const stampDir = path.join(os.homedir(), '.cache', 'muggle');
130
+ const stampFile = path.join(stampDir, 'project-prefs-inert');
131
+ const stamp = cwd + '|' + inertKeys.join(',');
132
+ let lastStamp = '';
133
+ try { lastStamp = fs.readFileSync(stampFile, 'utf-8'); } catch {}
134
+ if (lastStamp !== stamp) {
135
+ try { fs.mkdirSync(stampDir, { recursive: true }); fs.writeFileSync(stampFile, stamp); } catch {}
136
+ blocks.push(
137
+ 'Muggle Test: per-project preferences were removed — ' + pPath + ' is no longer read, so these keys no longer take effect: ' + inertKeys.join(', ') + '.\\\\n' +
138
+ 'Tell the user to re-apply any they want everywhere with \`/muggle-preferences\`; the file is safe to delete.'
139
+ );
140
+ }
141
+ }
142
+ return '\\\\n\\\\n' + blocks.join('\\\\n\\\\n');
143
+ } catch { return ''; }
206
144
  };
207
- const readEntry = (fileName, legacyEntryKey) => {
208
- const home = parseFile(path.join(os.homedir(), '.muggle-ai', fileName));
209
- const homeEntry = home && home.entries && home.entries[path.resolve(cwd)];
210
- if (homeEntry) { return homeEntry; }
211
- const legacy = parseFile(path.join(cwd, '.muggle-ai', fileName));
212
- return (legacy && legacy[legacyEntryKey]) || null;
145
+
146
+ const lastCacheNotes = () => {
147
+ try {
148
+ const parseFile = (filePath) => {
149
+ try { return JSON.parse(fs.readFileSync(filePath, 'utf-8')); } catch { return null; }
150
+ };
151
+ const readEntry = (fileName, legacyEntryKey) => {
152
+ const home = parseFile(path.join(os.homedir(), '.muggle-ai', fileName));
153
+ const homeEntry = home && home.entries && home.entries[path.resolve(cwd)];
154
+ if (homeEntry) { return homeEntry; }
155
+ const legacy = parseFile(path.join(cwd, '.muggle-ai', fileName));
156
+ return (legacy && legacy[legacyEntryKey]) || null;
157
+ };
158
+ const lines = [];
159
+ const lastProject = readEntry('last-project.json', 'lastProject');
160
+ if (lastProject && lastProject.projectId) {
161
+ const safeName = String(lastProject.projectName || '').replace(/\"/g, '\\\\\"');
162
+ lines.push('Muggle Test Last Project: id=' + lastProject.projectId + ' url=' + lastProject.projectUrl + ' name=\"' + safeName + '\"');
163
+ }
164
+ const lastHost = readEntry('last-host.json', 'lastHost');
165
+ if (lastHost && lastHost.host) {
166
+ lines.push('Muggle Test Last Host: ' + lastHost.host);
167
+ }
168
+ return lines.map((line) => '\\\\n\\\\n' + line).join('');
169
+ } catch { return ''; }
213
170
  };
214
- const lines = [];
215
- const lastProject = readEntry('last-project.json', 'lastProject');
216
- if (lastProject && lastProject.projectId) {
217
- const safeName = String(lastProject.projectName || '').replace(/\"/g, '\\\\\"');
218
- lines.push('Muggle Test Last Project: id=' + lastProject.projectId + ' url=' + lastProject.projectUrl + ' name=\"' + safeName + '\"');
219
- }
220
- const lastHost = readEntry('last-host.json', 'lastHost');
221
- if (lastHost && lastHost.host) {
222
- lines.push('Muggle Test Last Host: ' + lastHost.host);
223
- }
224
- console.log(lines.map((line) => '\\\\n\\\\n' + line).join(''));
225
- } catch { console.log(''); }
226
- " 2>/dev/null || true)
171
+
172
+ process.stdout.write(preferenceNotes() + lastCacheNotes());
173
+ " 2>/dev/null || true)
227
174
 
228
175
  # --- Host notes ---
229
176
  # Codex runs this hook from its own manifest, which stamps MUGGLE_HOOK_HOST. The
@@ -240,7 +187,7 @@ Muggle Test is running inside Codex, and its skills were written for Claude Code
240
187
  - Where a skill says AskUserQuestion, ask in your reply with numbered options and wait for the answer, or use request_user_input where it is offered."
241
188
  fi
242
189
 
243
- context="<EXTREMELY_IMPORTANT>\nYou have access to Muggle AI — a real-browser E2E acceptance testing tool.\n\nWhenever the user asks you to test, validate, verify, or check if their web app works — use the muggle MCP tools. This includes:\n- Testing user flows (signup, login, checkout, forms, dashboards)\n- Verifying UI changes didn't break anything\n- Running regression tests after code changes\n- Validating frontend behavior on localhost or a dev server\n- Checking if a feature works before merging a PR\n\nMuggle Test launches a real Electron browser that clicks buttons, fills forms, navigates pages, and captures screenshots. It generates replayable test scripts that persist across sessions.\n\nDo NOT write test code (Playwright, Cypress, Selenium) or try to test UI manually when muggle tools are available. Use the muggle skill or muggle MCP tools instead — they are faster, capture visual evidence, and produce reusable test scripts.\n\nTrigger phrases: 'test my app', 'check if it works', 'run E2E acceptance tests', 'validate the UI', 'verify the flow', 'regression test', 'make sure it still works', 'test before merging'.\n</EXTREMELY_IMPORTANT>${upgrade_notice}${prefs_file_note}${last_cache_notes}${host_notes}"
190
+ context="<EXTREMELY_IMPORTANT>\nYou have access to Muggle AI — a real-browser E2E acceptance testing tool.\n\nWhenever the user asks you to test, validate, verify, or check if their web app works — use the muggle MCP tools. This includes:\n- Testing user flows (signup, login, checkout, forms, dashboards)\n- Verifying UI changes didn't break anything\n- Running regression tests after code changes\n- Validating frontend behavior on localhost or a dev server\n- Checking if a feature works before merging a PR\n\nMuggle Test launches a real Electron browser that clicks buttons, fills forms, navigates pages, and captures screenshots. It generates replayable test scripts that persist across sessions.\n\nDo NOT write test code (Playwright, Cypress, Selenium) or try to test UI manually when muggle tools are available. Use the muggle skill or muggle MCP tools instead — they are faster, capture visual evidence, and produce reusable test scripts.\n\nTrigger phrases: 'test my app', 'check if it works', 'run E2E acceptance tests', 'validate the UI', 'verify the flow', 'regression test', 'make sure it still works', 'test before merging'.\n</EXTREMELY_IMPORTANT>${upgrade_notice}${session_notes}${host_notes}"
244
191
 
245
192
  escaped_context=$(escape_for_json "$context")
246
193
 
@@ -11,6 +11,7 @@
11
11
  GUARDRAIL_LIB_LOADED=1
12
12
 
13
13
  GUARDRAIL_PAYLOAD=""
14
+ GUARDRAIL_STATE_FILE=""
14
15
  GUARDRAIL_STATE=""
15
16
  GUARDRAIL_STATE_LOADED=""
16
17
  GUARDRAIL_HAS_STATE=""
@@ -50,20 +51,25 @@ guardrail_resolve_home() {
50
51
  fi
51
52
  }
52
53
 
54
+ # Sets GUARDRAIL_STATE_FILE to this session's guardrail state file, named as sessionState.ts names it.
55
+ guardrail_resolve_state_file() {
56
+ local session_id_pattern='"session_id"[[:space:]]*:[[:space:]]*"([^"]*)"'
57
+ local raw_session_id="unknown"
58
+ if [[ $GUARDRAIL_PAYLOAD =~ $session_id_pattern ]] && [[ -n ${BASH_REMATCH[1]} ]]; then
59
+ raw_session_id="${BASH_REMATCH[1]}"
60
+ fi
61
+ guardrail_resolve_home
62
+ GUARDRAIL_STATE_FILE="$GUARDRAIL_HOME/.muggle-ai/guardrails/${raw_session_id//[^A-Za-z0-9_-]/_}.json"
63
+ }
64
+
53
65
  # Loads this session's guardrail state once per process; returns 1 when the session has none.
54
66
  guardrail_load_state() {
55
67
  if [[ -z $GUARDRAIL_STATE_LOADED ]]; then
56
68
  GUARDRAIL_STATE_LOADED=1
57
- local session_id_pattern='"session_id"[[:space:]]*:[[:space:]]*"([^"]*)"'
58
- local raw_session_id="unknown"
59
- if [[ $GUARDRAIL_PAYLOAD =~ $session_id_pattern ]] && [[ -n ${BASH_REMATCH[1]} ]]; then
60
- raw_session_id="${BASH_REMATCH[1]}"
61
- fi
62
- guardrail_resolve_home
63
- local state_file="$GUARDRAIL_HOME/.muggle-ai/guardrails/${raw_session_id//[^A-Za-z0-9_-]/_}.json"
64
- if [[ -f $state_file ]]; then
69
+ guardrail_resolve_state_file
70
+ if [[ -f $GUARDRAIL_STATE_FILE ]]; then
65
71
  GUARDRAIL_HAS_STATE=1
66
- IFS= read -r -d '' GUARDRAIL_STATE <"$state_file" || true
72
+ IFS= read -r -d '' GUARDRAIL_STATE <"$GUARDRAIL_STATE_FILE" || true
67
73
  fi
68
74
  fi
69
75
  [[ -n $GUARDRAIL_HAS_STATE ]]