feat(events): add runBackgroundMonitoring driver with correct exit-log ordering#217
Draft
HanSur94 wants to merge 2 commits into
Draft
feat(events): add runBackgroundMonitoring driver with correct exit-log ordering#217HanSur94 wants to merge 2 commits into
HanSur94 wants to merge 2 commits into
Conversation
…g ordering
Adds libs/EventDetection/runBackgroundMonitoring.m, a thin console driver
that runs a LiveEventPipeline for a bounded duration with periodic
heartbeats, then stops it and logs the final status.
The driver was missing from the repository (it was created but never
committed in an earlier ephemeral session — the Phase 1039 live demo).
It is reconstructed here against the real LiveEventPipeline API, with the
exit-log bug already fixed:
- The pipeline is stopped via safeStop_() BEFORE the exit line is
printed, so a normal run logs `status=stopped` instead of the stale
`status=running` that a stop-deferred-to-onCleanup ordering printed.
- safeStop_() is idempotent (guards on Status=='running'), so the
trailing onCleanup safety net (which also covers Ctrl-C and unexpected
errors) never double-stops, and a faulted run keeps its `error` status
in the log.
- safeStop_() guards isvalid() behind exist('isvalid','builtin') and
wraps its body in try/catch, keeping it Octave-portable.
Exit paths (normal / error / Ctrl-C+exception) each stop the pipeline
once and log the correct final status.
Tests (tests/test_run_background_monitoring.m, flat function-based):
- E1-E3 error-ID validation (invalidPipeline/invalidDuration/
invalidHeartbeat) — run on Octave and MATLAB.
- L1 normal exit returns 'stopped', asserts the logged exit status
matches the post-return status, and checks no timer leaks.
- L2 error exit returns 'error' and logs 'status=error', asserting
safeStop_ does not force-stop a faulted pipeline (StopCount==0).
L1/L2 are timer-driven and MATLAB-only (Octave-skipped); they use the
new tests/MockBgPipeline.m test double for the fault path.
MISS_HIT mh_style/mh_lint/mh_metric all clean. Timer-driven L1/L2 must
be run on a MATLAB session (no MATLAB/Octave runtime in CI sandbox).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PFPxzvTUQfaTNCQrRUPrDK
This file holds a transient session/pid lock for the scheduled-tasks system and is rewritten by every Claude session that uses the scheduler, so it does not belong in version control. Keep the working copy (rm --cached) and ignore it going forward. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PFPxzvTUQfaTNCQrRUPrDK
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
Adds
libs/EventDetection/runBackgroundMonitoring.m— a thin, console-only driver that runs aLiveEventPipelinefor a bounded duration with periodic heartbeats, then stops it and logs the final status — with the exit-log ordering bug fixed.The task described a cosmetic logging bug in
libs/EventDetection/runBackgroundMonitoring.m(exit line printingstatus=runningbefore theonCleanup-registeredsafeStop_runs). An exhaustive search found that this file — and its testtests/test_run_background_monitoring.m— exist nowhere: not on this branch, not ingit log --all, and not on any of the repo's ~100 remote branch tips. The quoted symbols (safeStop_with theisvalid/Octave guard, the[BG] runBackgroundMonitoring exitlog, "Phase 1039") are also absent.The most likely explanation is that the file was created but never committed in an earlier ephemeral session (the "Phase 1039 live demo"), so it was lost when the container was re-cloned. Per the author's direction, it is reconstructed here against the real
LiveEventPipelineAPI with the fix already applied, rather than editing lines that don't exist.The fix
The exit line reports the true post-stop status:
safeStop_()before the exitfprintf, so a normal run logsstatus=stoppedinstead of the stalestatus=runningthat a stop-deferred-to-onCleanupordering printed.safeStop_()is idempotent (guards onStatus=='running'), so the trailingonCleanupsafety net — which also covers Ctrl-C and unexpected errors — never double-stops, and a faulted run keeps itserrorstatus in the log.safeStop_()guardsisvalid()behindexist('isvalid','builtin')and wraps its body intry/catch, keeping it Octave-portable.The three heartbeat-loop exit paths each stop the pipeline once and log the correct final status:
safeStop_→running→stoppedstoppedStatus=='error')safeStop_is a no-op (preserveserror)errortry/catch(exceptions) +onCleanupnet (Ctrl-C)Tests
tests/test_run_background_monitoring.m(flat function-based):invalidPipeline/invalidDuration/invalidHeartbeat) — run on Octave and MATLAB (validation precedes any timer start).'stopped', asserts the logged exit status matches the post-return status, and checks no timer leaks.'error'and logsstatus=error, assertingsafeStop_does not force-stop a faulted pipeline (StopCount==0).L1/L2 are timer-driven and MATLAB-only (Octave-skipped); they use the new
tests/MockBgPipeline.mdouble for the fault path.Verification
mh_style,mh_lint,mh_metric --ciall clean on the three new files (MISS_HIT's parser also confirms valid MATLAB syntax).test_run_background_monitoring()on a MATLAB session to exercise L1/L2 (E1–E3 also run under Octave).Note for reviewers
By design (per the task's chosen
Status=='running'guard), the error path preserveserrorin the log and does not callpipeline.stop(), so a real faultedLiveEventPipeline's timer object is left for the caller to clean up (the caller receives'error'as the return value). If guaranteed timer-object cleanup on the error path is preferred over surfacingerrorin the exit line, widen thesafeStop_guard to~strcmp(Status,'stopped')— at the cost of the error path loggingstopped.🤖 Generated with Claude Code
https://claude.ai/code/session_01PFPxzvTUQfaTNCQrRUPrDK
Generated by Claude Code