Skip to content

Commit 3c329ff

Browse files
feat(storage-lifecycle): one retention contract for audio buckets; lifecycle gate reads it (#24349)
## Summary This PR adds one retention contract for the audio buckets, and the lifecycle release prerequisite now reads it. Before this PR, two checks judged bucket lifecycles by different rules: - `backend/scripts/check_sync_staging_lifecycle.py` failed on any lifecycle Delete. - The workspace drift and retention tools allowed a Delete if it was at least as old as the longest hold. Neither covered the derived, syncing-local, canonical-audio or speech-profile buckets, and neither checked a ceiling. This follows the workspace daily review SP-1102-01 and lead ruling 19540a. **Why it matters:** on 11-01, a whole-bucket 7-day Delete on the batch staging bucket removed 161 `failed_held` sync uploads that had been held 8+ days. They were the only server copy. ## Changes ### `backend/deploy/storage-lifecycle/retention_contract.json`: the single threshold source Each bucket lists its protected prefixes, its holds and a ceiling. The workspace tools read this file through `git show origin/main:`. **Floor:** - Code windows are cited by file and regex. A test fails if a cited constant changes. - These holds are unbounded, so no age Delete may reach them: - accepted sync staging until custody (`staging_custody`); - capture audio of record (`source_audio`; David 10-15, FORWARD.md); - speech profiles. - `batch_finalization_alarm` encodes David's 06:09Z ruling (bus 6be2e1), with `decided_by: David`: no 21-day deadline; most audio within 30 min of finalization (`finalization_target_seconds: 1800`); alarm at 2 h (`finalization_alarm_seconds: 7200`, owned by lane alarm2h); `deadline: null`. **`control_verified: false`:** `failed_held` units never finalize and have no TTL in code, so the hold stays unbounded until an alarm by age since enqueue that covers them is installed and verified (asked alarm2h). Once it is, the floor is 2 h + `margin_days` (7), i.e. 8 d with the 1-day code windows, and the 30-day staging Delete passes. Raising `margin_days` is the knob if more response time is wanted. **Ceiling:** soft delete, versioning, noncurrent cleanup, and cache max ages (`merged/` 3 d, `playback/` 30 d, batch staging 30 d). Each ceiling is cited to the runbook, `artifacts.py verify_voice_retention`, FORWARD.md or prod.json. Ceilings that differ from live today carry `ruling: pending`. **Buckets covered:** - syncing-local - omi-audio-derived-uc1 - based-hardware-batch-pipeline-staging (prod and dev) - omi-private-cloud-sync - speech-profiles - `BUCKET_CANONICAL_AUDIO` (not provisioned) ### `backend/scripts/retention_contract.py` Pure `floor_violations` and `ceiling_findings`. ### `check_sync_staging_lifecycle.py` - A contract bucket is judged by its floor. Noncurrent-only deletes keep the live object, so they are allowed. - Any other bucket keeps the strict rule: any Delete fails. - The gate now also reads `softDeletePolicy` and `versioning`. - The receipt adds `contract` and `ceilings` (reason codes only, never names or prefixes). **Ceilings never change PASS/FAIL.** Promoting a ceiling to enforcing is a separate decision. ### Runbook The `audio-cleanup-custody.md` paragraph is updated to match. ## Verification - `backend/test.sh` with `BACKEND_UNIT_TEST_FILE_LIST` = `tests/unit/test_retention_contract.py` and `tests/unit/test_sync_staging_lifecycle_guard.py`: 13 + 27 passed, run through `bin/omi-heavy` with no ambient GCP credentials. - Live, read-only (ro-prod, metadata only, 11-02), `--bucket syncing-local --bucket omi-private-cloud-sync`: **PASS**. - syncing-local ceilings: `ceiling_soft_delete:pending` (7-day soft delete) and `ceiling_noncurrent_unbounded:pending`. - omi-private-cloud-sync ceilings: `ceiling_noncurrent_unbounded:pending` (14.75 TB of noncurrent generations; the lifecycle is frozen) and `ceiling_max_age:pending` (no `merged/` rule live). - The batch staging, derived and speech-profile buckets are unreadable to ro-prod (no `storage.buckets.get`). The read-only grant is an access change for David. ## Product invariants affected none ## Risk / rollout No deploy, flag, IAM or data change. The gate is run by an operator before promotion. The only behaviour change: a contract bucket with a Delete beyond its floor now passes. Today that is `omi-private-cloud-sync` with `playback/` 30 d, which used to fail as `lifecycle_delete`. The batch staging 30-day rule still fails until the stuck-unit alarm is verified (`control_verified`), as it does today. Unlisted buckets are unchanged. Rollback: revert the PR. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
2 parents d963454 + bd06011 commit 3c329ff

6 files changed

Lines changed: 520 additions & 12 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
{
2+
"schema": "omi.retention/v1",
3+
"_doc": "The one threshold source for audio-bucket retention, both ways. Floor: every lifecycle Delete that can reach a protected prefix has age >= max(hold days) + margin_days; a hold with days=null (or a pending ruling) forbids any age Delete there. Ceiling: soft delete, versioning and cache max ages a bucket may not exceed. Read by backend/scripts/check_sync_staging_lifecycle.py (release prerequisite; floors enforce, ceilings report) and by the workspace's infra/rollout/bin/retention_check.py and drift.sh (git show origin/main:<this file>). 11-01: a whole-bucket 7-day Delete on the batch staging bucket removed 161 failed_held sync uploads held 8+ days, the only server copy.",
4+
"margin_days": 7,
5+
"batch_staging_delete_age_days": 30,
6+
"holds": {
7+
"allowance_hold_retry_max": {
8+
"days": 1,
9+
"code": {"path": "backend/config/batch_pipeline.py", "pattern": "^ALLOWANCE_HOLD_RETRY_MAX_SECONDS = 86400$"}
10+
},
11+
"vendor_failover_held_stale_grace_max": {
12+
"days": 1,
13+
"code": {"path": "backend/config/batch_pipeline_vendor_failover.py", "pattern": "^HELD_STALE_GRACE_SECONDS_MAX = 86400(\\.0)?$"}
14+
},
15+
"scheduler_requeue_delay_max": {
16+
"days": 1,
17+
"code": {"path": "backend/utils/batch_pipeline/scheduler.py", "pattern": "min\\(86400, value\\)"}
18+
},
19+
"artifact_cleanup_after_projection": {
20+
"days": 1,
21+
"code": {"path": "backend/utils/batch_pipeline/retention.py", "pattern": "^ARTIFACT_RETENTION = timedelta\\(hours=24\\)$"}
22+
},
23+
"upload_pending": {
24+
"days": 0.03,
25+
"code": {"path": "backend/utils/batch_pipeline/upload_policy.py", "pattern": "^UPLOAD_PENDING_SECONDS = 30 \\* 60$"}
26+
},
27+
"batch_finalization_alarm": {
28+
"days": 0.0833,
29+
"finalization_target_seconds": 1800,
30+
"finalization_alarm_seconds": 7200,
31+
"deadline": null,
32+
"decided_by": "David",
33+
"ruling": "bus:6be2e1 2026-11-02T06:09Z",
34+
"control_verified": false,
35+
"control": "A 2 h alarm by age since enqueue that also covers units that never finalize (failed_held, quarantined, stuck before transcript), installed and verified (lane alarm2h). Until then this hold is unbounded.",
36+
"note": "David: no 21-day deadline; most audio within 30 min of finalization, alarm at 2 h (lane alarm2h owns the alarm). failed_held/quarantined units still have no TTL in code (utils/batch_pipeline/retention.py: accepted audio must not silently expire) and never finalize, so only an age-since-enqueue alarm reaches them. Once that control is verified, the floor counts this hold until a human is alarmed (days = finalization_alarm_seconds) plus margin_days for the response, so a 30-day staging Delete leaves ~30 days after the alarm."
37+
},
38+
"staging_custody": {
39+
"days": null,
40+
"note": "Accepted sync staging is held until durable custody (docs/runbooks/audio-cleanup-custody.md); no TTL, so no age Delete may reach it."
41+
},
42+
"source_audio": {
43+
"days": null,
44+
"note": "Capture audio of record is retained indefinitely except authorized privacy deletion (David 2026-10-15, storage-lifecycle/FORWARD.md)."
45+
},
46+
"speech_profile": {
47+
"days": null,
48+
"note": "A user's speech profile lives until the user or account deletion removes it."
49+
}
50+
},
51+
"ceiling_sources": {
52+
"erasure_immediate": "docs/runbooks/audio-cleanup-custody.md: privacy erasure must remain possible immediately (no retention lock, holds, or soft-deleted copies)",
53+
"voice_evidence": "utils/batch_pipeline/artifacts.py verify_voice_retention: soft delete must be 0",
54+
"canonical_forward": "storage-lifecycle/FORWARD.md: versioning disabled and soft delete 0 at creation",
55+
"declared_caches": "storage-lifecycle/prod.json: merged/ Delete 3 d, playback/ Delete 30 d"
56+
},
57+
"buckets": {
58+
"syncing-local": {
59+
"env": "BUCKET_TEMPORAL_SYNC_LOCAL",
60+
"project": "based-hardware",
61+
"protected_prefixes": [""],
62+
"holds": ["staging_custody"],
63+
"ceiling": {"soft_delete_seconds_max": 0, "noncurrent_cleanup": true, "ruling": "pending",
64+
"source": ["erasure_immediate"]}
65+
},
66+
"omi-audio-derived-uc1": {
67+
"env": "BUCKET_AUDIO_DERIVED",
68+
"project": "based-hardware",
69+
"protected_prefixes": ["syncing/"],
70+
"holds": ["staging_custody"],
71+
"ceiling": {"soft_delete_seconds_max": 0, "source": ["erasure_immediate", "voice_evidence"]}
72+
},
73+
"based-hardware-batch-pipeline-staging": {
74+
"env": "BUCKET_BATCH_PIPELINE",
75+
"project": "based-hardware",
76+
"protected_prefixes": [""],
77+
"holds": ["allowance_hold_retry_max", "vendor_failover_held_stale_grace_max", "scheduler_requeue_delay_max",
78+
"artifact_cleanup_after_projection", "upload_pending", "batch_finalization_alarm"],
79+
"ceiling": {"soft_delete_seconds_max": 0, "max_age_days": {"": 30}, "source": ["voice_evidence"]}
80+
},
81+
"based-hardware-dev-batch-pipeline-staging": {
82+
"env": "BUCKET_BATCH_PIPELINE",
83+
"project": "based-hardware-dev",
84+
"protected_prefixes": [""],
85+
"holds": ["allowance_hold_retry_max", "vendor_failover_held_stale_grace_max", "scheduler_requeue_delay_max",
86+
"artifact_cleanup_after_projection", "upload_pending", "batch_finalization_alarm"],
87+
"ceiling": {"soft_delete_seconds_max": 0, "max_age_days": {"": 30}, "source": ["voice_evidence"]}
88+
},
89+
"omi-private-cloud-sync": {
90+
"env": "BUCKET_PRIVATE_CLOUD_SYNC",
91+
"project": "based-hardware",
92+
"protected_prefixes": ["chunks/"],
93+
"holds": ["source_audio"],
94+
"ceiling": {"soft_delete_seconds_max": 0, "max_age_days": {"merged/": 3, "playback/": 30}, "noncurrent_cleanup": true,
95+
"ruling": "pending", "source": ["erasure_immediate", "declared_caches"]}
96+
},
97+
"speech-profiles": {
98+
"env": "BUCKET_SPEECH_PROFILES",
99+
"project": "based-hardware",
100+
"protected_prefixes": [""],
101+
"holds": ["speech_profile"],
102+
"ceiling": {"soft_delete_seconds_max": 0, "ruling": "pending", "source": ["erasure_immediate"]}
103+
},
104+
"canonical-audio (BUCKET_CANONICAL_AUDIO, not provisioned)": {
105+
"env": "BUCKET_CANONICAL_AUDIO",
106+
"provisioned": false,
107+
"protected_prefixes": [""],
108+
"holds": ["source_audio"],
109+
"ceiling": {"soft_delete_seconds_max": 0, "versioning": false, "source": ["canonical_forward"]}
110+
}
111+
}
112+
}

‎backend/docs/runbooks/audio-cleanup-custody.md‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,17 @@ backend/.venv/bin/python backend/scripts/check_sync_staging_lifecycle.py --bucke
109109

110110
Keep its timestamped JSON receipt before the lead-owned post-T3b promotion. It reads bucket
111111
metadata only and emits counts, metagenerations and policy digests, with no
112-
bucket/env values, prefixes or provider diagnostics. Missing metadata,
113-
lifecycle `Delete` actions and bucket retention/default holds that could block
114-
privacy erasure fail the guard. It never lists, reads, copies or restores
115-
objects, changes policy, or uses the interactive human identity.
112+
bucket/env values, prefixes or provider diagnostics. Missing metadata and
113+
bucket retention/default holds that could block privacy erasure fail the guard.
114+
Lifecycle `Delete` actions fail it too, except on a bucket named in
115+
`deploy/storage-lifecycle/retention_contract.json`: there a `Delete` may not
116+
reach a protected prefix sooner than the longest hold plus margin (an
117+
unbounded or pending-ruling hold forbids any), and noncurrent-only deletes are
118+
allowed. The receipt's `ceilings` list reports soft delete, versioning and cache
119+
ages beyond the contract's ceiling without changing PASS/FAIL. The same JSON is
120+
the threshold source for the workspace drift and retention checks. It never
121+
lists, reads, copies or restores objects, changes policy, or uses the
122+
interactive human identity.
116123

117124
Do not substitute a GCS retention lock or object hold for this application
118125
hold: privacy erasure must remain possible immediately. The rollout lead owns

‎backend/scripts/check_sync_staging_lifecycle.py‎

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
33
Reads bucket metadata only, never objects, IAM policies or secret payloads.
44
Output contains aggregate policy receipts, never bucket/env values or prefixes.
5+
6+
A bucket named in deploy/storage-lifecycle/retention_contract.json is judged by
7+
its floor there (a Delete may not reach a protected prefix sooner than the
8+
longest hold plus margin; noncurrent-only deletes keep the live object), and its
9+
ceiling findings are reported without changing PASS/FAIL. Any other bucket
10+
keeps the strict rule: any lifecycle Delete fails.
511
"""
612

713
import argparse
@@ -10,13 +16,18 @@
1016
import hashlib
1117
import json
1218
import os
19+
from pathlib import Path
1320
import re
21+
import sys
1422
from typing import Any
1523
from urllib.parse import quote
1624

1725
from google.auth.transport import requests
1826
from google.oauth2 import service_account
1927

28+
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
29+
from scripts import retention_contract # noqa: E402
30+
2031

2132
def read_bucket_metadata(name: str) -> dict[str, Any]:
2233
# No ADC fallback to an interactive human login. The sourced read-only
@@ -27,7 +38,9 @@ def read_bucket_metadata(name: str) -> dict[str, Any]:
2738
with requests.AuthorizedSession(credentials) as session:
2839
response = session.get(
2940
f'https://storage.googleapis.com/storage/v1/b/{quote(name, safe="")}',
30-
params={'fields': 'metageneration,lifecycle,defaultEventBasedHold,retentionPolicy'},
41+
params={
42+
'fields': 'metageneration,lifecycle,defaultEventBasedHold,retentionPolicy,softDeletePolicy,versioning'
43+
},
3144
timeout=20,
3245
)
3346
if response.status_code != 200:
@@ -42,11 +55,16 @@ def read_bucket_metadata(name: str) -> dict[str, Any]:
4255
raise ValueError('invalid retention metadata')
4356
# Keep raw actions: the storage SDK lifecycle iterator drops unknown
4457
# actions with a warning, which could otherwise turn uncertainty into PASS.
58+
soft_delete = payload.get('softDeletePolicy')
59+
versioning = payload.get('versioning')
4560
return {
4661
'metageneration': payload.get('metageneration'),
4762
'lifecycle': payload.get('lifecycle'),
4863
'default_event_based_hold': payload.get('defaultEventBasedHold'),
4964
'retention_period': retention.get('retentionPeriod'),
65+
# Ceiling inputs only; a missing policy reads as unknown, never as zero.
66+
'soft_delete_seconds': soft_delete.get('retentionDurationSeconds') if isinstance(soft_delete, dict) else None,
67+
'versioning': bool(versioning.get('enabled')) if isinstance(versioning, dict) else False,
5068
}
5169

5270

@@ -58,7 +76,9 @@ def _metageneration(metadata: dict[str, Any]) -> str | None:
5876
return value if re.fullmatch(r'[1-9][0-9]*', value) else None
5977

6078

61-
def policy_failure(metadata: dict[str, Any]) -> str | None:
79+
def policy_failure(
80+
metadata: dict[str, Any], bucket: str | None = None, contract: dict[str, Any] | None = None
81+
) -> str | None:
6282
if _metageneration(metadata) is None:
6383
return 'invalid_metadata'
6484
if metadata.get('default_event_based_hold') or metadata.get('retention_period') not in (None, 0):
@@ -68,28 +88,46 @@ def policy_failure(metadata: dict[str, Any]) -> str | None:
6888
lifecycle = {}
6989
if not isinstance(lifecycle, dict) or not isinstance(lifecycle.get('rule', []), list):
7090
return 'invalid_metadata'
91+
governed = _governed(contract, bucket)
7192
for rule in lifecycle.get('rule', []):
7293
if not isinstance(rule, dict) or not isinstance(rule.get('action'), dict):
7394
return 'invalid_metadata'
7495
action = rule['action'].get('type')
75-
if action == 'Delete':
96+
if action == 'Delete' and governed is None:
7697
return 'lifecycle_delete'
77-
if action not in {'SetStorageClass', 'AbortIncompleteMultipartUpload'}:
98+
if action not in {'Delete', 'SetStorageClass', 'AbortIncompleteMultipartUpload'}:
7899
return 'unsupported_lifecycle_action'
100+
if governed is not None and retention_contract.floor_violations(governed[0], governed[1], lifecycle):
101+
return 'lifecycle_delete'
79102
return None
80103

81104

82-
def audit(names: Sequence[str], reader: Callable[[str], dict[str, Any]] = read_bucket_metadata) -> dict[str, Any]:
105+
def _governed(contract: dict[str, Any] | None, bucket: str | None) -> tuple[dict[str, Any], str] | None:
106+
"""(contract, bucket) when the contract names the bucket, else None (strict any-Delete rule)."""
107+
if contract is None or bucket is None or bucket not in contract.get('buckets', {}):
108+
return None
109+
return contract, bucket
110+
111+
112+
def audit(
113+
names: Sequence[str],
114+
reader: Callable[[str], dict[str, Any]] = read_bucket_metadata,
115+
contract: dict[str, Any] | None = None,
116+
) -> dict[str, Any]:
83117
targets = []
84118
for index, name in enumerate(names, start=1):
85119
try:
86120
metadata = reader(name)
87-
reason = policy_failure(metadata)
121+
reason = policy_failure(metadata, name, contract)
122+
governed = _governed(contract, name)
88123
receipt = {
89124
'target': index,
90125
'metageneration': _metageneration(metadata),
91126
'policy_sha256': hashlib.sha256(json.dumps(metadata, sort_keys=True).encode()).hexdigest(),
92127
'reason': reason,
128+
'contract': governed is not None,
129+
# Report-only: ceilings never change PASS/FAIL here.
130+
'ceilings': retention_contract.ceiling_findings(*governed, metadata) if governed is not None else [],
93131
}
94132
except Exception:
95133
# Provider errors can contain URLs or identifiers; never echo them.
@@ -121,7 +159,7 @@ def main(argv: Sequence[str] | None = None, *, reader: Callable[[str], dict[str,
121159
if not names or any(not re.fullmatch(r'[a-z0-9][a-z0-9._-]{1,220}[a-z0-9]', name) for name in names):
122160
print(json.dumps({'status': 'FAIL', 'reason': 'invalid_or_missing_targets'}))
123161
return 2
124-
result = audit(names, reader)
162+
result = audit(names, reader, retention_contract.load())
125163
print(json.dumps(result, sort_keys=True))
126164
return 0 if result['status'] == 'PASS' else 1
127165

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
"""Audio-bucket retention contract: floors (holds) and ceilings, from storage-lifecycle/retention_contract.json.
2+
3+
Pure functions over bucket metadata; no cloud access. The release prerequisite
4+
(check_sync_staging_lifecycle.py) and the workspace drift/retention tools read the
5+
same JSON, so a threshold changes in one reviewed place.
6+
7+
Floor: every lifecycle Delete that can reach a protected prefix has
8+
age >= max(hold days) + margin_days. A hold with days=null, a pending ruling,
9+
or a bound that depends on a control not yet verified (control_verified false)
10+
is unbounded: no age Delete may reach that prefix. Deletes that only remove
11+
noncurrent generations keep the live object and are allowed.
12+
13+
Ceiling: soft delete, versioning, noncurrent cleanup and cache max ages a
14+
bucket may not exceed. A ceiling whose ruling is pending reports with a
15+
`:pending` suffix.
16+
"""
17+
18+
import json
19+
from pathlib import Path
20+
from typing import Any
21+
22+
CONTRACT_PATH = Path(__file__).resolve().parents[1] / 'deploy' / 'storage-lifecycle' / 'retention_contract.json'
23+
24+
25+
def load(path: Path = CONTRACT_PATH) -> dict[str, Any]:
26+
return json.loads(Path(path).read_text())
27+
28+
29+
def _pending(hold: dict[str, Any], accept_pending: bool = False) -> bool:
30+
"""Not yet a bound: the control the bound relies on is unverified, or the ruling is pending. accept_pending asks
31+
"would a pending ruling fit?"; it never waives an unverified control."""
32+
return hold.get('control_verified') is False or (hold.get('ruling') == 'pending' and not accept_pending)
33+
34+
35+
def required_days(contract: dict[str, Any], bucket: str, accept_pending: bool = False) -> float | None:
36+
holds = [contract['holds'][name] for name in contract['buckets'][bucket]['holds']]
37+
if any(h['days'] is None or _pending(h, accept_pending) for h in holds):
38+
return None
39+
return max(h['days'] for h in holds) + contract['margin_days']
40+
41+
42+
def _rules(lifecycle: Any) -> list[dict[str, Any]]:
43+
rules = (lifecycle or {}).get('rule') or [] if isinstance(lifecycle, dict) else []
44+
return [r for r in rules if isinstance(r, dict) and isinstance(r.get('action'), dict)]
45+
46+
47+
def _noncurrent_only(condition: dict[str, Any]) -> bool:
48+
"""True only for conditions GCS can never match on a live object. A live object has zero newer versions, so
49+
numNewerVersions counts only when >= 1; daysSinceNoncurrentTime and noncurrentTimeBefore apply to noncurrent
50+
generations only."""
51+
newer = condition.get('numNewerVersions')
52+
return (
53+
condition.get('isLive') is False
54+
or (isinstance(newer, int) and not isinstance(newer, bool) and newer >= 1)
55+
or 'daysSinceNoncurrentTime' in condition
56+
or 'noncurrentTimeBefore' in condition
57+
)
58+
59+
60+
# A cache max-age rule covers a prefix only when nothing else narrows it: GCS requires every condition to match.
61+
_COVERING_KEYS = frozenset({'age', 'matchesPrefix'})
62+
63+
64+
def _reaches(rule_prefixes: list[str], prefixes: list[str]) -> bool:
65+
"""No rule prefix matches every object; otherwise prefixes overlap when one starts with the other."""
66+
return not rule_prefixes or any(p.startswith(q) or q.startswith(p) for p in rule_prefixes for q in prefixes)
67+
68+
69+
def floor_violations(
70+
contract: dict[str, Any], bucket: str, lifecycle: Any, accept_pending: bool = False
71+
) -> list[dict[str, Any]]:
72+
"""Live-object Delete rules that can reach a protected prefix sooner than the longest hold allows."""
73+
need = required_days(contract, bucket, accept_pending)
74+
spec = contract['buckets'][bucket]
75+
pending = [h for h in spec['holds'] if _pending(contract['holds'][h], accept_pending)]
76+
out = []
77+
for rule in _rules(lifecycle):
78+
condition = rule.get('condition') or {}
79+
if rule['action'].get('type') != 'Delete' or _noncurrent_only(condition):
80+
continue
81+
if not _reaches(condition.get('matchesPrefix') or [], spec['protected_prefixes']):
82+
continue
83+
age = condition.get('age')
84+
if need is None:
85+
out.append({'age': age, 'required': None, 'pending': pending})
86+
elif not isinstance(age, (int, float)) or age < need:
87+
out.append({'age': age, 'required': need, 'pending': []})
88+
return out
89+
90+
91+
def ceiling_findings(contract: dict[str, Any], bucket: str, metadata: dict[str, Any]) -> list[str]:
92+
"""Reason codes (no prefixes or names) where live policy exceeds the bucket's ceiling."""
93+
ceiling = contract['buckets'][bucket].get('ceiling') or {}
94+
suffix = ':pending' if ceiling.get('ruling') == 'pending' else ''
95+
rules = _rules(metadata.get('lifecycle'))
96+
out = []
97+
soft = metadata.get('soft_delete_seconds')
98+
if 'soft_delete_seconds_max' in ceiling:
99+
if soft is None:
100+
out.append('ceiling_soft_delete_unknown' + suffix)
101+
elif int(soft) > ceiling['soft_delete_seconds_max']:
102+
out.append('ceiling_soft_delete' + suffix)
103+
versioning = metadata.get('versioning')
104+
if ceiling.get('versioning') is False and versioning:
105+
out.append('ceiling_versioning' + suffix)
106+
if (
107+
ceiling.get('noncurrent_cleanup')
108+
and versioning
109+
and not any(r['action'].get('type') == 'Delete' and _noncurrent_only(r.get('condition') or {}) for r in rules)
110+
):
111+
out.append('ceiling_noncurrent_unbounded' + suffix)
112+
for prefix, max_age in (ceiling.get('max_age_days') or {}).items():
113+
covered = any(
114+
r['action'].get('type') == 'Delete'
115+
and set(r.get('condition') or {}) <= _COVERING_KEYS
116+
and isinstance((r.get('condition') or {}).get('age'), (int, float))
117+
and r['condition']['age'] <= max_age
118+
and (
119+
not (r['condition'].get('matchesPrefix') or [])
120+
or any(prefix.startswith(p) for p in r['condition']['matchesPrefix'])
121+
)
122+
for r in rules
123+
)
124+
if not covered:
125+
out.append('ceiling_max_age' + suffix)
126+
return out

0 commit comments

Comments
 (0)