Give a company a by-hand reason and a date last checked, in migration 15

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Sonnet 5.5 committed 2026-09-29 17:02:18 +02:00
1 parent 0a1d3c67c7
commit a466ea52b7
4 files changed
+120 -19

No files matched your search

+24 -6
View File
@@ -21,7 +21,7 @@ import numpy as np
from desk.models import Item
from app.models import (
AFFINITY, REMOTE, STATUSES, Agent, Application, ApplicationEvent, Candidate,
AFFINITY, HAND_REASONS, REMOTE, STATUSES, Agent, Application, ApplicationEvent, Candidate,
Company, Gate, Limits, Prompt, Role, RoleRow, ScoredRole, Subscription, Weights,
check_prompts, company_key, country_of, excerpt, place_key, posted_at, posted_host,
remote_of, title_key,
@@ -94,7 +94,7 @@ _EXCERPT_SOURCE = "COALESCE(NULLIF(roles.summary, ''), roles.description)"
_EXCERPT_RAW = 240
_COMPANY_FIELDS = ("name", "domain", "status", "careers_url", "notes", "ats", "country", "city",
"ats_slug")
"ats_slug", "hand_reason", "checked_at")
#: A company as the store serves it: the row and the asked-for agent's affinity.
_COMPANY_SELECT = """SELECT c.*, COALESCE(ac.affinity, 0) AS affinity FROM companies c
@@ -1162,18 +1162,22 @@ class Store:
def add_company(self, name: str, *, key: str = "", domain: str = "",
status: str = "researching", careers_url: str = "",
notes: str = "", ats: str = "", country: str = "",
city: str = "", ats_slug: str = "") -> Company:
"""Raises ValueError on a key already taken."""
city: str = "", ats_slug: str = "", hand_reason: str = "",
checked_at: str = "") -> Company:
"""Raises ValueError on a key already taken or a `hand_reason` outside
`HAND_REASONS`."""
name = name.strip()
key = key or company_key(name)
if not key:
raise ValueError("a company needs a name")
_check_hand_reason(hand_reason)
try:
with self.transaction() as conn:
self._insert_company(conn, key, name, {
"domain": domain, "status": status, "careers_url": careers_url,
"notes": notes, "ats": ats, "country": country, "city": city,
"ats_slug": ats_slug})
"ats_slug": ats_slug, "hand_reason": hand_reason,
"checked_at": checked_at})
except sqlite3.IntegrityError as exc:
raise ValueError(f"company {key!r} already exists") from exc
return self._company(key)
@@ -1237,6 +1241,8 @@ class Store:
unknown = set(fields) - set(_COMPANY_FIELDS)
if unknown:
raise ValueError(f"not a company field: {', '.join(sorted(unknown))}")
if "hand_reason" in fields:
_check_hand_reason(fields["hand_reason"])
if fields:
sets = ", ".join(f"{f}=?" for f in fields)
changed = self._write(f"UPDATE companies SET {sets}, updated_at=? WHERE key=?",
@@ -1246,7 +1252,13 @@ class Store:
return self._company(key)
def remove_company(self, key: str) -> bool:
return self._write("DELETE FROM companies WHERE key=?", (key,)) > 0
"""Delete the row and every agent's stance on it, in one transaction:
`agent_companies` holds the key with no foreign key to hold it back, so
a row left behind would give a company added again under the same name
an affinity it never got. Roles and subscriptions are not touched."""
with self.transaction() as conn:
conn.execute("DELETE FROM agent_companies WHERE company_key=?", (key,))
return conn.execute("DELETE FROM companies WHERE key=?", (key,)).rowcount > 0
def _company(self, key: str, agent_id: int | None = None) -> Company:
found = self.company(key, agent_id)
@@ -1541,6 +1553,12 @@ def _check_status(status: str) -> None:
f"({', '.join(STATUSES)})")
def _check_hand_reason(reason: Any) -> None:
if reason not in HAND_REASONS:
raise ValueError(f"hand_reason is one of {', '.join(repr(r) for r in HAND_REASONS)}, "
f"not {reason!r}")
def _check_affinity(affinity: Any) -> None:
if isinstance(affinity, bool) or not isinstance(affinity, int) or affinity not in AFFINITY:
raise ValueError(f"affinity is -1, 0, 1 or 2, not {affinity!r}")