Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions scripts/coding_discovery_tools/ai_tools_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -3818,7 +3818,7 @@ def generate_single_tool_report(self, tool: Dict, device_id: str, home_user: str
report = {
"home_user": home_user,
"system_user": system_user or home_user,
"device_id": device_id,
"device_discovery_id": device_id,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 · Preserve compatibility with the deployed backend (codex)

New reports contain only device_discovery_id, so updated agents cannot report to the currently deployed backend. The PR description confirms that production reads only device_id and rejects reports without it. If this release reaches devices before the separate backend deployment, discovery reports are dropped fleet-wide.

scripts/coding_discovery_tools/ai_tools_discovery.py:3818-3824 constructs reports with device_discovery_id only; scripts/coding_discovery_tools/utils.py:1518-1522 makes the same swap for scan events. The PR description states that the production gateway currently reads only device_id and that its dual-key handler is not yet deployed.

"is_container": in_container(),
"tools": [tool_for_report]
}
Expand Down Expand Up @@ -3850,7 +3850,7 @@ def generate_report(self) -> Dict:

return {
"system_user": user_info,
"device_id": device_id,
"device_discovery_id": device_id,
"tools": tools_with_projects
}

Expand Down
4 changes: 2 additions & 2 deletions scripts/coding_discovery_tools/memory_fix.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,14 @@ def _post_json(link: _Link, path: str, body: Dict) -> Optional[Dict]:
def _placements(link: _Link, posted: List[Dict]) -> Optional[List[Dict]]:
"""Every answer the backend gave for the posted files, or None when the post failed."""
files = [{key: entry[key] for key in ("home_user", "file_path", "sha256")} for entry in posted]
answer = _post_json(link, FIXES_PATH, {"device_id": link.device_id, "files": files})
answer = _post_json(link, FIXES_PATH, {"device_discovery_id": link.device_id, "files": files})
fixes = answer.get("fixes") if answer else None
return [fix for fix in fixes if isinstance(fix, dict)] if isinstance(fixes, list) else None


def _post_result(link: _Link, result: Dict) -> bool:
"""True when the result reached the backend; a post that fails is tried again, as the edit is already made."""
body = {"device_id": link.device_id, "results": [result]}
body = {"device_discovery_id": link.device_id, "results": [result]}
return any(_post_json(link, RESULTS_PATH, body) is not None for _ in range(RESULT_POST_TRIES))


Expand Down
4 changes: 2 additions & 2 deletions scripts/coding_discovery_tools/s3_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def try_s3_upload(
ctx = sentry_context or {}

# ─── Step 1: get presigned URL ──────────────────────────────────────
meta_body = {"device_id": payload.get("device_id")}
meta_body = {"device_discovery_id": payload.get("device_discovery_id") or payload.get("device_id")}
if payload.get("run_id"):
meta_body["run_id"] = payload["run_id"]

Expand Down Expand Up @@ -209,7 +209,7 @@ def try_s3_upload(
# backend's /from-s3/ handler receives the same context (including
# `app_name` for MDM-tagged scans like JumpCloud).
notify_body = {
"device_id": payload.get("device_id"),
"device_discovery_id": payload.get("device_discovery_id") or payload.get("device_id"),
"object_key": object_key,
"system_user": payload.get("system_user"),
"home_user": payload.get("home_user"),
Expand Down
4 changes: 2 additions & 2 deletions scripts/coding_discovery_tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1516,7 +1516,7 @@ def send_scan_event(
Tuple of (success, retryable): success=True if sent, retryable=True if caller should queue
"""
payload = {
"device_id": device_id,
"device_discovery_id": device_id,
"run_id": run_id,
"scan_event": scan_event,
}
Expand Down Expand Up @@ -3798,7 +3798,7 @@ def send_discovery_metrics(

url = f"{normalize_url(backend_url)}/api/v1/ai-tools/report/"
payload: Dict[str, Any] = {
"device_id": device_id,
"device_discovery_id": device_id,
"tools": [],
"sentry_metrics": sentry_metrics,
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_discovery_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def test_detect_and_report_flow(self):
# Assert required top-level keys
self.assertIn("home_user", report)
self.assertIn("system_user", report)
self.assertIn("device_id", report)
self.assertIn("device_discovery_id", report)
self.assertIn("tools", report)
self.assertEqual(len(report["tools"]), 1)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_memory_fix.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,13 +484,13 @@ def test_a_ready_fix_runs_the_helper_and_posts_the_result_with_its_token(self):
counts = self.run_fixes([held])

posted = self.fix_posts()[0]
self.assertEqual(posted, {"device_id": "dev-1", "files": [
self.assertEqual(posted, {"device_discovery_id": "dev-1", "files": [
{"home_user": ME, "file_path": held["file_path"], "sha256": _sha(text.encode())}]})
self.assertEqual(self.helper_calls[0][0], {
"op": "fix", "file_path": held["file_path"], "memory_root": str(self.root),
"sha256": _sha(text.encode()), "spans": [_span(text, SECRET_A)]})
self.assertEqual(self.helper_calls[0][1:], (pwd.getpwnam(ME).pw_uid, pwd.getpwnam(ME).pw_gid))
self.assertEqual(self.result_posts(), [{"device_id": "dev-1", "results": [
self.assertEqual(self.result_posts(), [{"device_discovery_id": "dev-1", "results": [
{"id": 7, "outcome": "fixed", "token": "T-7"}]}])
self.assertEqual(Path(held["file_path"]).read_text(), "t " + MARKER)
self.assertEqual(counts, {"rounds": 1, "fixed": 1})
Expand Down
28 changes: 26 additions & 2 deletions tests/test_s3_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def _set_path_response(self, path, code, body):
class TestS3UploadFlow(_ServerMixin, unittest.TestCase):
def _payload(self):
return {
"device_id": "DEV-1",
"device_discovery_id": "DEV-1",
"system_user": "alice",
"home_user": "alice",
"run_id": str(uuid.uuid4()),
Expand Down Expand Up @@ -135,7 +135,31 @@ def test_happy_path_three_steps(self):
from_s3_body = json.loads(self.server.requests[2]["body"])
self.assertNotIn("tools", from_s3_body)
self.assertEqual(from_s3_body["object_key"], "org/1/run/abc.json")
self.assertEqual(from_s3_body["device_id"], "DEV-1")
self.assertEqual(from_s3_body["device_discovery_id"], "DEV-1")

@patch.object(utils_mod, "_SENTRY_DSN", "")
def test_queued_payload_with_legacy_key_still_sends_the_new_one(self):
"""A report queued before the rename carries device_id; both bodies must still name the device."""
upload_url = f"{self.base_url}/s3-bucket/object-key.json"
self._set_path_response("/api/v1/ai-tools/report/upload-url/", 200, json.dumps({
"upload_url": upload_url,
"object_key": "org/1/run/legacy.json",
"expires_in": 300,
}).encode())
self._set_path_response("/s3-bucket/object-key.json", 200, b"")
self._set_path_response("/api/v1/ai-tools/report/from-s3/", 202, json.dumps({"status": "queued"}).encode())

legacy = self._payload()
legacy["device_id"] = legacy.pop("device_discovery_id")

success, retryable = try_s3_upload(self.base_url, "k", legacy)

self.assertTrue(success)
self.assertFalse(retryable)
meta_body = json.loads(self.server.requests[0]["body"])
from_s3_body = json.loads(self.server.requests[2]["body"])
self.assertEqual(meta_body["device_discovery_id"], "DEV-1")
self.assertEqual(from_s3_body["device_discovery_id"], "DEV-1")

@patch.object(utils_mod, "_SENTRY_DSN", "")
def test_app_name_forwarded_to_step3(self):
Expand Down
22 changes: 21 additions & 1 deletion tests/test_send_and_persist.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def test_scan_in_progress_event(self, _sleep):

# Verify payload structure
payload = json.loads(self.server.requests[0]["body"])
self.assertEqual(payload["device_id"], "DEVICE123")
self.assertEqual(payload["device_discovery_id"], "DEVICE123")
self.assertEqual(payload["run_id"], "run-uuid-1234")
self.assertEqual(payload["scan_event"], "in_progress")
self.assertEqual(payload["app_name"], "JumpCloud")
Expand Down Expand Up @@ -502,6 +502,26 @@ def test_scan_failed_device_level_error(self, _sleep):
self.assertNotIn("home_user", payload) # No user context for device-level errors
self.assertEqual(payload["scan_error"]["error_type"], "RuntimeError")

@patch("time.sleep")
@patch.object(utils_mod, "_SENTRY_DSN", "")
def test_metrics_payload_names_the_device_with_the_new_key(self, _sleep):
"""Metrics piggyback on the report endpoint, so they must carry the new wire key too."""
sent = utils_mod.send_discovery_metrics(
self.base_url,
"test-key",
"DEVICE123",
{"run_seconds": 1.5},
run_id="run-uuid-1234",
)

self.assertTrue(sent)
self.assertEqual(len(self.server.requests), 1)
payload = json.loads(self.server.requests[0]["body"])
self.assertEqual(payload["device_discovery_id"], "DEVICE123")
self.assertNotIn("device_id", payload)




class TestSentryPriorityBypassesCap(unittest.TestCase):
"""A priority=True event (the terminal no_tools_found summary) bypasses the
Expand Down
Loading