Decide which stale preview environments to delete

Agent buildEasy12 min

Your task

Every branch spins up a preview environment, few get torn down, and the cloud bill shows it. An agent cleans them up, and because deletion cannot be undone, it only deletes what is provably dead.

Write classify_preview_cleanup(env_records): the rule your agent follows for each environment.

  • env_records (list of str): one environment each, written as key=value fields separated by ;, for example "branch_merged=true;idle_days=21;has_data_volume=false;label=none".
  • Return one decision per record, in input order: "keep", "notify" (ping the owner), "confirm" (the owner must approve) or "delete".

Read each record like this:

  1. Split on ;, and each field at its first =. Skip a field with no =. Trim and lowercase both the key and the value. Ignore keys other than the 4 below.
  2. branch_merged: merged only if the value is exactly true. Missing means not merged.
  3. has_data_volume: attached unless the value is exactly false. Missing means not attached.
  4. label and idle_days: the value, or empty if missing.

Then decide. The first rule that matches wins:

  1. idle_days is not 1 to 9 digits (0 to 9 only): "confirm".
  2. Idle for fewer than 7 days: "keep".
  3. A data volume is attached, or the label is keep or demo: "confirm".
  4. Merged, and idle for 14 days or more: "delete".
  5. Otherwise: "notify".

Examples

classify_preview_cleanup(env_records: list[str]) -> list[str]
Example 1
Inputclassify_preview_cleanup(["branch_merged=true;idle_days=21;has_data_volume=false;label=none", "branch_merged=false;idle_days=9;has_data_volume=false;label=none", "branch_merged=true;idle_days=2;has_data_volume=false;label=none"])
Output["delete", "notify", "keep"]
Example 2
Inputclassify_preview_cleanup(["branch_merged=true;idle_days=30;has_data_volume=true;label=none", "branch_merged=true;idle_days=30;has_data_volume=false;label=demo"])
Output["confirm", "confirm"]
Example 3
Inputclassify_preview_cleanup(["branch_merged=true;idle_days=14;has_data_volume=false;label=none", "branch_merged=true;idle_days=13;has_data_volume=false;label=none"])
Output["delete", "notify"]