@misterhuydo/sentinel 1.1.3 → 1.1.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.
- package/.cairn/session.json +2 -2
- package/package.json +1 -1
- package/python/sentinel/fix_engine.py +87 -22
- package/python/sentinel/main.py +4 -3
package/.cairn/session.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
|
-
"message": "Auto-checkpoint at 2026-03-23T08:
|
|
3
|
-
"checkpoint_at": "2026-03-23T08:
|
|
2
|
+
"message": "Auto-checkpoint at 2026-03-23T08:33:20.221Z",
|
|
3
|
+
"checkpoint_at": "2026-03-23T08:33:20.223Z",
|
|
4
4
|
"active_files": [],
|
|
5
5
|
"notes": [],
|
|
6
6
|
"mtime_snapshot": {}
|
package/package.json
CHANGED
|
@@ -99,42 +99,109 @@ def _validate_patch(patch: str) -> tuple[bool, str]:
|
|
|
99
99
|
return True, ""
|
|
100
100
|
|
|
101
101
|
|
|
102
|
+
_AUTH_ERROR_HINTS = (
|
|
103
|
+
"not logged in", "please run claude login", "authentication failed",
|
|
104
|
+
"api key is not set", "invalid x-api-key", "unauthorized", "please authenticate",
|
|
105
|
+
"unauthenticated", "auth_required", "no auth", "login required",
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def _is_auth_error(output: str) -> bool:
|
|
110
|
+
low = output.lower()
|
|
111
|
+
return any(hint in low for hint in _AUTH_ERROR_HINTS)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def _claude_cmd(bin_path: str, prompt: str) -> list[str]:
|
|
115
|
+
import os as _os
|
|
116
|
+
try:
|
|
117
|
+
skip = _os.getuid() != 0
|
|
118
|
+
except AttributeError:
|
|
119
|
+
skip = True # Windows — always pass flag
|
|
120
|
+
if skip:
|
|
121
|
+
return [bin_path, "--dangerously-skip-permissions", "--print", prompt]
|
|
122
|
+
return [bin_path, "--print", prompt]
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _run_claude_attempt(bin_path: str, prompt: str, env: dict) -> tuple[str, bool]:
|
|
126
|
+
"""
|
|
127
|
+
Run claude CLI with the given env. Returns (output, timed_out).
|
|
128
|
+
Raises FileNotFoundError if binary is missing.
|
|
129
|
+
"""
|
|
130
|
+
try:
|
|
131
|
+
result = subprocess.run(
|
|
132
|
+
_claude_cmd(bin_path, prompt),
|
|
133
|
+
capture_output=True, text=True, timeout=SUBPROCESS_TIMEOUT, env=env,
|
|
134
|
+
)
|
|
135
|
+
return (result.stdout or "") + (result.stderr or ""), False
|
|
136
|
+
except subprocess.TimeoutExpired:
|
|
137
|
+
return "", True
|
|
138
|
+
|
|
139
|
+
|
|
102
140
|
def generate_fix(
|
|
103
141
|
event: ErrorEvent,
|
|
104
142
|
repo: RepoConfig,
|
|
105
143
|
cfg: SentinelConfig,
|
|
106
144
|
patches_dir: Path,
|
|
107
|
-
) -> tuple[str, Path | None]:
|
|
145
|
+
) -> tuple[str, Path | None, str]:
|
|
108
146
|
"""
|
|
109
147
|
Generate a fix for the given error event.
|
|
110
148
|
|
|
111
149
|
Returns:
|
|
112
|
-
(status, patch_path)
|
|
150
|
+
(status, patch_path, marker)
|
|
113
151
|
status: "patch" | "skip" | "error"
|
|
152
|
+
|
|
153
|
+
Auth strategy — API key and Claude Pro (OAuth) are interchangeable:
|
|
154
|
+
Primary : Claude Pro (OAuth) if claude_pro_for_tasks=True, else API key
|
|
155
|
+
Fallback : the other method, if primary fails with an auth error
|
|
156
|
+
On total auth failure: notify Slack admins + email report recipients
|
|
114
157
|
"""
|
|
115
|
-
|
|
158
|
+
import os as _os
|
|
159
|
+
|
|
160
|
+
marker = f"sentinel-{event.fingerprint[:8]}"
|
|
116
161
|
log_file = Path(cfg.workspace_dir) / "fetched" / f"{event.source}.log"
|
|
117
162
|
if not log_file.exists():
|
|
118
163
|
log_file = None
|
|
119
|
-
prompt = _build_prompt(event, repo, log_file)
|
|
164
|
+
prompt = _build_prompt(event, repo, log_file, marker)
|
|
120
165
|
|
|
121
166
|
logger.info("Invoking Claude Code for %s (fp=%s)", event.source, event.fingerprint)
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
167
|
+
|
|
168
|
+
base_env = _os.environ.copy()
|
|
169
|
+
api_env = {**base_env, "ANTHROPIC_API_KEY": cfg.anthropic_api_key} if cfg.anthropic_api_key else None
|
|
170
|
+
oauth_env = base_env # relies on cached `claude login` session — no key injected
|
|
171
|
+
|
|
172
|
+
# Choose primary/fallback order based on config
|
|
173
|
+
if cfg.claude_pro_for_tasks and cfg.anthropic_api_key:
|
|
174
|
+
attempts = [("Claude Pro (OAuth)", oauth_env), ("API key", api_env)]
|
|
175
|
+
elif cfg.claude_pro_for_tasks:
|
|
176
|
+
attempts = [("Claude Pro (OAuth)", oauth_env)]
|
|
177
|
+
elif cfg.anthropic_api_key:
|
|
178
|
+
attempts = [("API key", api_env), ("Claude Pro (OAuth)", oauth_env)]
|
|
179
|
+
else:
|
|
180
|
+
attempts = [("Claude Pro (OAuth)", oauth_env)]
|
|
181
|
+
|
|
182
|
+
output = ""
|
|
128
183
|
try:
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
184
|
+
for label, env in attempts:
|
|
185
|
+
if env is None:
|
|
186
|
+
continue
|
|
187
|
+
logger.info("fix_engine: trying %s for %s", label, event.fingerprint)
|
|
188
|
+
output, timed_out = _run_claude_attempt(cfg.claude_code_bin, prompt, env)
|
|
189
|
+
if timed_out:
|
|
190
|
+
logger.error("Claude Code timed out for %s", event.fingerprint)
|
|
191
|
+
return "error", None, ""
|
|
192
|
+
if not _is_auth_error(output):
|
|
193
|
+
break
|
|
194
|
+
logger.warning("fix_engine: %s auth error for %s — trying next method", label, event.fingerprint)
|
|
195
|
+
else:
|
|
196
|
+
# All attempts failed with auth errors
|
|
197
|
+
msg = (
|
|
198
|
+
":warning: *Sentinel — Fix Engine auth failure*\n"
|
|
199
|
+
f"Both API key and Claude Pro (OAuth) failed authentication for `{event.fingerprint}`.\n"
|
|
200
|
+
"• Check that `ANTHROPIC_API_KEY` is valid, or run `claude login` to refresh the OAuth session."
|
|
201
|
+
)
|
|
202
|
+
logger.error("fix_engine: all auth methods failed for %s", event.fingerprint)
|
|
203
|
+
slack_alert(cfg.slack_bot_token, cfg.slack_channel, msg)
|
|
204
|
+
return "error", None, ""
|
|
138
205
|
except FileNotFoundError:
|
|
139
206
|
msg = (
|
|
140
207
|
f":warning: *Sentinel — Claude CLI not found*\n"
|
|
@@ -145,9 +212,7 @@ def generate_fix(
|
|
|
145
212
|
slack_alert(cfg.slack_bot_token, cfg.slack_channel, msg)
|
|
146
213
|
return "error", None, ""
|
|
147
214
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
# Alert Slack immediately on rate-limit / auth failure — never stay silent
|
|
215
|
+
# Alert Slack immediately on rate-limit — never stay silent
|
|
151
216
|
alert_if_rate_limited(
|
|
152
217
|
cfg.slack_bot_token,
|
|
153
218
|
cfg.slack_channel,
|
package/python/sentinel/main.py
CHANGED
|
@@ -549,13 +549,14 @@ def _log_auth_status(cfg: SentinelConfig) -> None:
|
|
|
549
549
|
|
|
550
550
|
if has_api_key and pro_for_tasks:
|
|
551
551
|
logger.info(
|
|
552
|
-
"Claude auth: API key ✓
|
|
552
|
+
"Claude auth: API key ✓ + Claude Pro (OAuth) ✓ — "
|
|
553
|
+
"Fix Engine will try Claude Pro first, falls back to API key on auth error. "
|
|
553
554
|
"Run `claude login` if not already authenticated."
|
|
554
555
|
)
|
|
555
556
|
elif has_api_key and not pro_for_tasks:
|
|
556
557
|
logger.info(
|
|
557
|
-
"Claude auth: API key ✓
|
|
558
|
-
"CLAUDE_PRO_FOR_TASKS=false
|
|
558
|
+
"Claude auth: API key ✓ — Boss + Fix Engine use API key. "
|
|
559
|
+
"CLAUDE_PRO_FOR_TASKS=false; falls back to Claude Pro (OAuth) if key auth fails."
|
|
559
560
|
)
|
|
560
561
|
elif not has_api_key and has_claude_bin:
|
|
561
562
|
logger.warning(
|