Repository navigation
Dedupe local signed log appends - #519
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces an idempotent “append signed envelope to local messages.jsonl” mechanism to prevent duplicate audit-log rows, and adjusts the monitor/send flows so own-client messages are recorded without being echoed back to the sender.
Changes:
- Added
airc_core.log_append.append_unique_sig()(with a small CLI wrapper) to dedupe appends by envelope signature. - Updated
monitor_formatterto use the idempotent append helper and to mirror own-client messages into the audit log while suppressing display. - Updated
cmd_send.shand the multi-channel monitor supervisor to reduce duplicate local writes and harden bash 3.2 +set -ubehavior.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
test/test_monitor_formatter.py |
Adds a unit test asserting own-client messages are mirrored but not displayed. |
lib/airc_core/monitor_formatter.py |
Switches local mirroring to an idempotent append-by-sig helper and changes own-client filtering order. |
lib/airc_core/log_append.py |
New module implementing lock + tail-scan based deduped appends (and a CLI entrypoint). |
lib/airc_bash/cmd_send.sh |
Uses the deduped append helper for local audit writes and removes a host-side append that raced the monitor mirror. |
airc |
Adjusts multi-channel monitor supervisor PID array assignments to be bash-3.2/set -u-safe. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+26
to
+37
| def _recent_sigs(path: str, limit: int = TAIL_LIMIT) -> set[str]: | ||
| try: | ||
| with open(path, encoding="utf-8", errors="replace") as f: | ||
| lines = f.readlines()[-limit:] | ||
| except (FileNotFoundError, OSError): | ||
| return set() | ||
| sigs: set[str] = set() | ||
| for raw in lines: | ||
| sig = _line_sig(raw) | ||
| if sig: | ||
| sigs.add(sig) | ||
| return sigs |
Comment on lines
+40
to
+88
| def _acquire_lock(path: str) -> str: | ||
| lock_path = f"{path}.lock" | ||
| deadline = time.time() + LOCK_WAIT_SEC | ||
| payload = f"{os.getpid()}\n".encode("ascii", errors="replace") | ||
| while True: | ||
| try: | ||
| fd = os.open(lock_path, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o600) | ||
| try: | ||
| os.write(fd, payload) | ||
| finally: | ||
| os.close(fd) | ||
| return lock_path | ||
| except FileExistsError: | ||
| try: | ||
| age = time.time() - os.path.getmtime(lock_path) | ||
| if age > LOCK_STALE_SEC: | ||
| os.unlink(lock_path) | ||
| continue | ||
| except FileNotFoundError: | ||
| continue | ||
| except OSError: | ||
| pass | ||
| if time.time() >= deadline: | ||
| raise TimeoutError(f"timed out waiting for log lock: {lock_path}") | ||
| time.sleep(LOCK_SLEEP_SEC) | ||
|
|
||
|
|
||
| def append_unique_sig(path: str, line: str) -> str: | ||
| """Append `line`, skipping if its JSON sig already exists in the log tail.""" | ||
| parent = os.path.dirname(path) | ||
| if parent: | ||
| os.makedirs(parent, exist_ok=True) | ||
| framed = line if line.endswith("\n") else line + "\n" | ||
| sig = _line_sig(framed) | ||
| lock_path = _acquire_lock(path) | ||
| try: | ||
| if sig and sig in _recent_sigs(path): | ||
| return "skipped" | ||
| fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0o644) | ||
| try: | ||
| os.write(fd, framed.encode("utf-8")) | ||
| finally: | ||
| os.close(fd) | ||
| return "appended" | ||
| finally: | ||
| try: | ||
| os.unlink(lock_path) | ||
| except OSError: | ||
| pass |
|
|
||
| _airc_append_local_signed() { | ||
| if ! printf '%s\n' "$1" | "$AIRC_PYTHON" -m airc_core.log_append append --path "$MESSAGES" >/dev/null; then | ||
| echo "$1" >> "$MESSAGES" |
| fi | ||
| done | ||
| _pids=("${_alive[@]}") | ||
| _pids=(${_alive[@]+"${_alive[@]}"}) |
| _kept+=("$entry") | ||
| done | ||
| _pids=("${_kept[@]}") | ||
| _pids=(${_kept[@]+"${_kept[@]}"}) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Validation
Not in this PR