Skip to content

The agent that grows with you

fix(teams): claim a durable job once before scheduling its pipeline run

Teknium972b398642f1
diff --git a/plugins/teams_pipeline/runtime.py b/plugins/teams_pipeline/runtime.py
index 0c4973dad9d..a3e8a47dc8b 100644
--- a/plugins/teams_pipeline/runtime.py
+++ b/plugins/teams_pipeline/runtime.py
@@ -91,14 +91,19 @@ def bind_gateway_runtime(gateway: Any) -> bool:
# Graph wants a prompt 202 once the work is durable: accept (persist the job) inside the request,
# run the fetch/transcript/summary pipeline detached. An accept failure still fails the request.
background = getattr(adapter, "_background_tasks", set())
+ # Claim a job before scheduling its external effects: a no-id duplicate in one Graph batch
+ # returns the same still-"received" job (run_job persists progress only once it runs).
+ scheduled: set[str] = set()
async def _schedule(notification: dict[str, Any], event: Any) -> None:
job = runtime.create_job_from_notification(notification)
- if job.status != "received": # duplicate of a running or finished job
+ if job.status != "received" or job.job_id in scheduled: # duplicate of a running or finished job
return
+ scheduled.add(job.job_id)
task = asyncio.create_task(runtime.run_job(job.job_id))
background.add(task)
task.add_done_callback(background.discard)
+ task.add_done_callback(lambda _task: scheduled.discard(job.job_id))
adapter.set_notification_scheduler(_schedule)
gateway._teams_pipeline_runtime = runtime
gateway._teams_pipeline_runtime_error = None
diff --git a/tests/plugins/test_teams_duplicate_job_claim.py b/tests/plugins/test_teams_duplicate_job_claim.py
new file mode 100644
index 00000000000..b3bd18753d2
--- /dev/null
+++ b/tests/plugins/test_teams_duplicate_job_claim.py
@@ -0,0 +1,42 @@
+"""One Graph batch carrying the same no-id notification twice runs its durable job once."""
+import asyncio
+from types import SimpleNamespace
+
+import pytest
+
+from gateway.config import GatewayConfig, Platform
+from plugins.teams_pipeline.pipeline import TeamsMeetingPipeline
+from plugins.teams_pipeline.store import TeamsPipelineStore
+
+
+async def test_duplicate_no_id_notifications_in_one_batch_run_the_job_once(monkeypatch, tmp_path):
+ from plugins.teams_pipeline import runtime as runtime_module
+
+ class Adapter:
+ def __init__(self):
+ self._background_tasks = set()
+
+ def set_notification_scheduler(self, scheduler):
+ self.scheduler = scheduler
+
+ pipeline = TeamsMeetingPipeline(graph_client=object(), store=TeamsPipelineStore(tmp_path / 'store.json'))
+ release, runs = asyncio.Event(), []
+
+ async def run_job(job_id): # the sink writes (Notion page creation) happen in here
+ runs.append(job_id)
+ await release.wait()
+ monkeypatch.setattr(pipeline, 'run_job', run_job)
+ monkeypatch.setattr(runtime_module, 'build_pipeline_runtime', lambda gateway: pipeline)
+ adapter = Adapter()
+ gateway = SimpleNamespace(adapters={Platform.MSGRAPH_WEBHOOK: adapter}, config=GatewayConfig(platforms={}),
+ _teams_pipeline_runtime=None, _teams_pipeline_runtime_error=None)
+ assert runtime_module.bind_gateway_runtime(gateway) is True
+ notification = {'changeType': 'created', 'resource': 'communications/onlineMeetings/m1',
+ 'resourceData': {'id': 'm1'}} # Graph's notification ``id`` is optional
+ for _ in range(2): # the adapter schedules each entry of the batch's ``value`` in turn
+ await adapter.scheduler(dict(notification), object())
+ await asyncio.sleep(0)
+ release.set()
+ await asyncio.gather(*list(adapter._background_tasks))
+ assert len(runs) == 1