Polish: strict int parsing, --db wiring, config backups, email_verified, yaml fallback (#37)#40
Merged
Conversation
…ed, yaml fallback (#37) Implements the low-severity correctness & hardening polish punch-list from issue #37: 1. ✓ Integer parsing now rejects floats instead of silently truncating - Added strict_int() helper in plutus_agent/utils.py - Applied to /v1/usage ingest validation in server/app.py - Raises ValueError on '1.9'/1.9, accepts 5/'5'/0 2. ✓ CLI --db argument is now wired through - Added --db to argparse in cli.py - Sets PLUTUS_DB env var so all db.connect() calls honor it - `plutus --db /tmp/x.db status` now uses that DB 3. ✓ Empty API key validation already safe - db.api_key_org() already rejects empty/non-prefixed secrets (line 360) - Documented in PR body 4. ✓ Added missing FOREIGN KEY constraint - alerts_log.workspace_id now has FOREIGN KEY → workspaces(id) ON DELETE SET NULL - Only applies to fresh DBs (CREATE TABLE IF NOT EXISTS) 5. ✓ Config save() now creates timestamped backups - Backups existing config to .yaml.bak-YYYYMMDDHHMMSS before overwriting - Only when prior file exists (no backup on first save) 6. ✓ email_verified check now requires explicit truthy value - auth._claims_from_id_token now requires email_verified in (True, 'true') - Missing/False/any other value raises AuthError 7. ✓ YAML fallback reader can now parse simple YAML - _minimal_yaml_read now handles 'key: value' and one level of 2-space nesting - Keeps JSON fast-path, tolerant to files written with PyYAML All existing tests remain green + new focused tests added: - test_strict_int_accepts_integers / test_strict_int_rejects_floats - test_db_wiring_honors_env - test_config_save_creates_backup - test_email_verified_requires_truthy - test_minimal_yaml_read_roundtrip Tested: All 42 tests pass, python3 -m compileall clean, --version works.
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.
Implements the low-severity correctness & hardening polish punch-list from issue #37.
Changes
1. ✅ Integer parsing now rejects floats instead of silently truncating
int(ev.get('input_tokens', 0) or 0)in /v1/usage ingest silently truncated 1.9 → 1strict_int()helper inplutus_agent/utils.pythat raisesValueErroron floats/decimal stringsserver/app.pyingest validation5,'5',0; rejects1.9,'1.9',1.02. ✅ CLI
--dbargument now wired through--dbwas not declared in argparse; CLI always usedPLUTUS_DBenv var--dbto top-level parser incli.py, setsPLUTUS_DBenv var inmain()plutus --db /tmp/x.db statusnow uses that DB path3. ✅ Empty API key validation already safe
db.api_key_org()at line 360 already checksif not secret or not secret.startswith(API_KEY_PREFIX): return None4. ✅ Added missing FOREIGN KEY constraint
alerts_log.workspace_idhad no FOREIGN KEYREFERENCES workspaces(id) ON DELETE SET NULLindb.pyschemaCREATE TABLE IF NOT EXISTS); existing DBs unaffected5. ✅ Config
save()now creates timestamped backupsconfig.pysave()overwrote config.yaml without backup.yaml.bak-YYYYMMDDHHMMSS6. ✅
email_verifiedcheck now requires explicit truthy valueauth._claims_from_id_tokenonly rejectedemail_verified in (False, 'false'); missing claim was allowedemail_verified not in (True, 'true')— must be explicitly truthyAuthError('email is not verified')7. ✅ YAML fallback reader can now parse simple YAML
_minimal_yaml_readonly parsed JSON; couldn't read YAML files it wrote when PyYAML absentkey: valueand one level of 2-space-indented nestingTesting
All existing tests remain green (42 tests pass) + new focused tests:
test_strict_int_accepts_integers/test_strict_int_rejects_floatstest_db_wiring_honors_envtest_config_save_creates_backuptest_email_verified_requires_truthytest_minimal_yaml_read_roundtripVerified:
python3 tests/test_engine.py— all 42 tests passpython3 -m compileall -q plutus_agent— cleanpython3 -m plutus_agent --version— worksCloses #37