Skip to content

fix(hass): log an unparseable apps.yaml to predbat.log with a !secret hint (#5530) - #5642

Merged
springfall2008 merged 7 commits into
mainfrom
fix/apps-yaml-unknown-tag-log-5530
Nov 14, 2026
Merged

springfall2008 merged 7 commits into
mainfrom
fix/apps-yaml-unknown-tag-log-5530

Conversation

@springfall2008

Copy link
Copy Markdown
Owner

This is an automated draft PR generated from issue #5530 — a maintainer should review it before merging.

Fixes #5530

Summary

A mistyped secret reference in apps.yaml (api_key: !openrouter_api_key, or !secrets name) is an unknown YAML tag, so yaml.safe_load() raises ConstructorError. Hass.__init__ caught it with a bare print(exc) and sys.exit(1), so the reason went only to the add-on console and predbat.log showed nothing but the repeating "Loading secrets / Loading apps.yaml" pairs.

The error now goes through Hass.log() (so it lands in predbat.log, and as an Error: line it is still echoed to the console). A new describe_apps_yaml_error() in utils.py makes it actionable:

  • unknown tag: Error: Unable to load apps.yaml - unknown tag '!openrouter_api_key' at line 671 column 18; only !secret is supported, did you mean '!secret openrouter_api_key'?
  • !secrets / other !secret… variants: same, with did you mean '!secret <name>'?
  • any other YAML error: Error: Unable to load apps.yaml - it is not valid YAML: <PyYAML message on one line>

Startup still exits: apps.yaml is the boot config, so there is nothing to continue loading with (per the triage comment). The restart-on-exit loop belongs to the add-on wrapper and is unchanged.

Testing

  • New test_apps_yaml_error_reaches_predbat_log in apps/predbat/tests/test_secrets.py (registered in run_secrets_tests) constructs Hass() against three bad apps.yaml files (unknown tag, !secrets, malformed YAML), asserts it exits, and checks predbat.log holds the Error: line, tag, location and hint.
  • tools/triage_test.sh secrets fails without the fix (source stashed; all 11 assertions fail because predbat.log holds no error text) and passes with it.
  • ./run_pre_commit passes, including the quick suite (All tests passed (4 slow tests skipped)).

Notes

  • GitNexus impact on Hass.__init__: LOW, no upstream callers in the index (only constructed at bootstrap). describe_apps_yaml_error is new and only called from there.
  • describe_apps_yaml_error() repeats load_apps_yaml()'s PREDBAT_APPS_FILE/apps.yaml default to name the file. I left that duplicated rather than change load_apps_yaml()'s signature, which fox.py also uses.
  • The unknown tag text is now written to predbat.log, where before it only went to the console. If someone pasted a raw API key as the tag (api_key: !sk-...), that value would reach the log unredacted, because args and secrets haven't loaded at that point. This seemed an unlikely shape compared with the misnamed-reference case the issue shows, so I didn't add a guard.
  • Background: tools/debug-journal.md already has the "only Loading apps.yaml pairs in predbat.log" symptom row for this issue. Once this merges, that row should say the error now appears in predbat.log.

🤖 Generated with Claude Code

… hint (#5530)

A mistyped secret reference such as 'api_key: !openrouter_api_key' or
'!secrets name' is an unknown YAML tag. Startup printed the PyYAML error
and exited, so it only reached the add-on console and predbat.log showed
nothing but the restart loop. Route it through Hass.log() with the tag,
its line/column and a "did you mean '!secret ...'" hint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@springfall2008 springfall2008 self-assigned this Oct 19, 2026
@springfall2008 springfall2008 added BOT_REVIEW Trigger an autotriage BOT_CLEANUP Trigger: bot should address PR review feedback and CI failures, then commit and push labels Oct 19, 2026

@springfall2008 springfall2008 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated comment from the triage bot.

Reviewed head f433431 (base 697c44c). 3 findings, all posted inline. The fix itself is sound and reads exactly as the triage on #5530 asked for; the blockers are that the PR's own CI is red and that the change appears to interfere with an unrelated later test.

Comment thread apps/predbat/tests/test_secrets.py Resolved
Comment thread apps/predbat/utils.py Resolved
Comment thread apps/predbat/hass.py Resolved
@springfall2008 springfall2008 removed the BOT_REVIEW Trigger an autotriage label Oct 19, 2026
@springfall2008

Copy link
Copy Markdown
Owner Author

Automated cleanup summary from the triage bot. Nothing has been pushed; see the end of this comment for why.

The branch is already up to date with main. All three review threads were evaluated. Fixes for two of them are ready locally but are not pushed, because the local quality gate is failing on an unrelated test.

1. CI failure on test_str2time (thread on test_secrets.py): not caused by this PR. The earlier review blamed it on this PR's new test. That conclusion is wrong. The variable that differs is the Python interpreter. The workflow installs python-version: 3.x, and the runners did not all resolve that to the same version:

Run Branch CPython str2time
39331522635 this PR 3.15.0 FAILED
39327005136 fix/freeze-export-car-hold-5612 3.15.0 FAILED (same naive-string assertion)
39325746644 fix/octopus-slot-cap-per-account-5215 3.15.0 FAILED (same naive-string assertion)
39333835314 fix/compare-existing-tariff-during-events-5180 (the control in the review) 3.14.8 PASSED

Two branches that do not contain this PR's change fail identically on 3.15.0, and the "clean" control ran on 3.14.8. Each row was checked against the job log (Successfully set up CPython (...) and the str2time: result line).

Not verified: the exact 3.15 change. It looks as if datetime.strptime with a trailing %z no longer raises for a string with no offset, but no 3.15 interpreter was run to confirm it. Either way, it belongs in a separate PR, not this one. Two options:

  • make str2time_strptime raise ValueError itself when the parsed result has tzinfo is None, which enforces the existing contract rather than loosening it;
  • pin the workflow's Python version.

A re-run of this PR's CI may pass if it lands on a 3.14 runner.

2. Raw credential echoed as an unknown tag (utils.py): fixed locally. describe_apps_yaml_error() now repeats the tag only when it names a key in secrets.yaml, or when it is the literal !secrets / !secret. On that fatal path, Hass.__init__ re-reads secrets.yaml with a silent logger to get the key names. Any other tag is reported by position only: unknown tag at line 4 column 12 (not shown in case it is a credential); only !secret is supported, did you mean '!secret <name>'?. So api_key: !sk-proj-abc123 no longer reaches predbat.log. The trade-off: a mistyped reference to a name that is not yet in secrets.yaml also loses the name, but it keeps the line, the column and the hint.

3. KeyError / OSError bypassing predbat.log (hass.py): fixed locally. The except is now (yaml.YAMLError, KeyError, OSError), the same set fox.py --config already catches. A file with no pred_bat section logs it has no pred_bat section; Predbat's settings must be nested under a top-level 'pred_bat:' key. A missing file logs the [Errno 2] No such file or directory text. One related case is left as is: an empty apps.yaml makes config["pred_bat"] raise TypeError, which this except still does not catch.

Tests: test_apps_yaml_error_reaches_predbat_log gains four cases:

  • a raw key as a tag;
  • an unlisted name as a tag, where the tag must not appear in the log;
  • no pred_bat section;
  • a missing file.

The test now points PREDBAT_SECRETS_FILE at its own file. tools/triage_test.sh secrets passes, and every hook in ./run_pre_commit passes (ruff, black, cspell and the rest).

Why nothing was pushed: the quick suite inside ./run_pre_commit fails on octopus_url / day_night_rates: Expected 16 rate entries (8 days), got 15. That test is outside this PR's diff and depends on the wall clock (it ran at 00:23 BST on 20 Oct). Tonight's other cleanup runs, which share this clone, hit the same failure on main and on several other branches. The bot does not push past a failed gate, so these fixes are still uncommitted in the bot's clone. They need re-applying once that test is fixed or stops failing.

@springfall2008

Copy link
Copy Markdown
Owner Author

Automated cleanup failed to complete for this PR. Reason: The run exited cleanly but left 3 file(s) with uncommitted changes, so its fixes were never pushed. They are stashed in the bot's clone before the next flow runs. Failed at: triage_daemon.py:2161 in process_bot_cleanup_pr() (daemon 063549db). Not retrying automatically; remove BOT_FAILED and re-add BOT_CLEANUP to try again.

@springfall2008 springfall2008 added BOT_FAILED An automated triage/PR attempt failed; see the comment for details and removed BOT_CLEANUP Trigger: bot should address PR review feedback and CI failures, then commit and push labels Oct 19, 2026
@chalfontchubby chalfontchubby added BOT_CLEANUP Trigger: bot should address PR review feedback and CI failures, then commit and push and removed BOT_FAILED An automated triage/PR attempt failed; see the comment for details labels Oct 20, 2026
CI and others added 2 commits October 20, 2026 10:30
…et; log a missing pred_bat section or file too

describe_apps_yaml_error() now repeats an unknown tag only when it names a
key in secrets.yaml (or is the literal !secret/!secrets), so a raw credential
pasted after a '!' never reaches predbat.log. Hass.__init__ also routes the
KeyError (no pred_bat section) and OSError (missing file) that
load_apps_yaml() raises through the same logged message.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@springfall2008 springfall2008 removed the BOT_CLEANUP Trigger: bot should address PR review feedback and CI failures, then commit and push label Oct 20, 2026
@springfall2008
springfall2008 marked this pull request as ready for review October 20, 2026 19:03
@springfall2008 springfall2008 added the BOT_CLEANUP Trigger: bot should address PR review feedback and CI failures, then commit and push label Oct 23, 2026
@springfall2008

Copy link
Copy Markdown
Owner Author

Automated cleanup check from the triage bot. Nothing here needs a maintainer to act.

One known gap is unchanged from the earlier reply: an empty apps.yaml still raises TypeError, which the new except does not catch.

@springfall2008

Copy link
Copy Markdown
Owner Author

Automated cleanup failed to complete for this PR. Reason: The run exited cleanly but left 6 file(s) with uncommitted changes, so its fixes were never pushed. They are stashed in the bot's clone before the next flow runs. Failed at: triage_daemon.py:2161 in process_bot_cleanup_pr() (daemon 063549db). Not retrying automatically; remove BOT_FAILED and re-add BOT_CLEANUP to try again.

@springfall2008 springfall2008 added BOT_FAILED An automated triage/PR attempt failed; see the comment for details and removed BOT_CLEANUP Trigger: bot should address PR review feedback and CI failures, then commit and push labels Oct 23, 2026
@chalfontchubby chalfontchubby added BOT_CLEANUP Trigger: bot should address PR review feedback and CI failures, then commit and push and removed BOT_FAILED An automated triage/PR attempt failed; see the comment for details labels Oct 25, 2026
@springfall2008

Copy link
Copy Markdown
Owner Author

Automated cleanup check from the triage bot. Nothing here needs a maintainer to act.

One known gap is still unchanged: an empty apps.yaml raises TypeError, which the new except does not catch.

@springfall2008 springfall2008 removed the BOT_CLEANUP Trigger: bot should address PR review feedback and CI failures, then commit and push label Oct 25, 2026
@chalfontchubby chalfontchubby added the BOT_PR_OPENED Bot PR flow opened a draft PR for this ticket label Oct 25, 2026
@chalfontchubby chalfontchubby self-assigned this Oct 25, 2026
…-tag-log-5530

Resolve the hass.py conflict: keep the PR's logged apps.yaml error, and pass main's
per-instance apps_file/secrets_file through to load_apps_yaml, load_secrets and
describe_apps_yaml_error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@springfall2008
springfall2008 enabled auto-merge (squash) November 14, 2026 18:39
@springfall2008
springfall2008 merged commit 7e5336e into main Nov 14, 2026
4 checks passed
@springfall2008
springfall2008 deleted the fix/apps-yaml-unknown-tag-log-5530 branch November 14, 2026 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BOT_PR_OPENED Bot PR flow opened a draft PR for this ticket

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bootloop when secret config incorrectly specified in apps.yaml

2 participants