-
Notifications
You must be signed in to change notification settings - Fork 69
fix(*): codex usage, log redaction, and two observability gaps #347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
0xKT
wants to merge
2
commits into
main
Choose a base branch
from
fix/codex_usage_and_log_hygiene
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| """Strip credentials out of log records before they reach a sink. | ||
|
|
||
| Some upstreams put the credential in the URL itself -- the Telegram Bot API | ||
| keys every route on ``/bot<token>/``, and several providers take an API key as | ||
| a query parameter -- so any library that logs its request line (httpx does, at | ||
| INFO) writes a working credential into a persisted, retained file. The gateway | ||
| log is the one users attach to a bug report, which is exactly the wrong place | ||
| for it. | ||
|
|
||
| This is the message-body counterpart to ``diagnose=False`` in | ||
| :mod:`raven.cli._log_file`, which already keeps tracebacks from serializing | ||
| locals holding secrets. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| from typing import Any | ||
|
|
||
| REDACTED = "<redacted>" | ||
|
|
||
| _PATTERNS: tuple[tuple[re.Pattern[str], str], ...] = ( | ||
| # Telegram: /bot<id>:<secret>/. The numeric id stays -- it identifies the | ||
| # bot for debugging and is not the secret half. | ||
| (re.compile(r"/bot(\d{5,}):[A-Za-z0-9_-]{15,}"), rf"/bot\1:{REDACTED}"), | ||
| # Credential passed as a query parameter (Gemini's ?key=, and friends). | ||
| ( | ||
| re.compile(r"([?&](?:api[-_]?key|access[-_]?token|auth[-_]?token|token|key|secret)=)[^&\s\"']+", re.I), | ||
| rf"\1{REDACTED}", | ||
| ), | ||
| # Basic-auth credentials embedded in a URL. | ||
| (re.compile(r"(://)[^/\s:@]+:[^/\s@]+@"), rf"\1{REDACTED}@"), | ||
| # Authorization: Bearer <token>. | ||
| (re.compile(r"(Bearer\s+)[A-Za-z0-9._~+/=-]{12,}", re.I), rf"\1{REDACTED}"), | ||
| # Bare vendor-prefixed keys that appear outside any URL. | ||
| (re.compile(r"\b(sk-[A-Za-z0-9_-]{12,}|xox[abprs]-[A-Za-z0-9-]{10,}|gh[pousr]_[A-Za-z0-9]{20,})"), REDACTED), | ||
| ) | ||
|
|
||
|
|
||
| def redact(text: str) -> str: | ||
| """Return ``text`` with every known credential shape masked.""" | ||
| for pattern, replacement in _PATTERNS: | ||
| text = pattern.sub(replacement, text) | ||
| return text | ||
|
|
||
|
|
||
| def redacting_filter(record: Any) -> bool: | ||
| """Loguru sink filter that rewrites the record in place, always keeping it. | ||
|
|
||
| Loguru formats a record *after* its filters run, so mutating | ||
| ``record["message"]`` here is what reaches every sink. | ||
| """ | ||
| message = record.get("message") | ||
| if isinstance(message, str): | ||
| record["message"] = redact(message) | ||
| return True | ||
|
|
||
|
|
||
| def combine_filters(*filters: Any) -> Any: | ||
| """Chain sink filters, dropping the record as soon as one rejects it. | ||
|
|
||
| The redacting filter must run even when a caller supplied its own | ||
| noise-dropping filter, so the two are composed rather than one replacing | ||
| the other. | ||
| """ | ||
| active = [f for f in filters if f is not None] | ||
| if not active: | ||
| return None | ||
| if len(active) == 1: | ||
| return active[0] | ||
|
|
||
| def _chained(record: Any) -> bool: | ||
| return all(f(record) for f in active) | ||
|
|
||
| return _chained | ||
|
|
||
|
|
||
| __all__ = ["REDACTED", "combine_filters", "redact", "redacting_filter"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[blocker] This call still bypasses the new per-process dedup.
_migrate_legacy_context_window'snotify=Truebranch (loader.py:223) logs via a rawlogging.getLogger(__name__).info(...), not through the_MigrationLogwrapper constructed at line 392 — it never sees_log/_logged_migrationsat all.That matters because this path is exactly the one problem #3 in this PR is about:
run_stampedisunstamped = _migration_version(path) < CURRENT_CONFIG_VERSION(loader.py:313), and the stamp write is best-effort (_write_migration_version, loader.py:186-197, swallowsOSError). If the sidecar stamp can't be written (read-only config dir, permission issue, etc.),unstampedstaysTrueforever, so this branch — and itslogging.getLogger(__name__).info("Migrated: dropped agents.defaults.%s ...")line — fires on every singleload_config()call, indefinitely. That's the identical "gateway loads once per cron fire" noise this PR sets out to fix, just for one specific migration that isn't routed through the new dedup.The added tests (
test_migration_logs_once_per_processetc.) only call_migrate_config(data)with the defaultrun_stamped=False, so this path isn't exercised at all.Suggest passing the dedup logger (or
_logged_migrations) into_migrate_legacy_context_windowas well, or moving itsnotifylogging to go through_MigrationLog.