jason810496 commented on code in PR #76826:
URL: https://github.com/apache/airflow/pull/76826#discussion_r4563114884


##########
airflow-core/src/airflow/api_fastapi/execution_api/routes/xcoms.py:
##########
@@ -553,6 +553,36 @@ def head_xcom(
     response.headers["Content-Range"] = f"map_indexes {count}"
 
 
+def _resolve_requested_map_index(
+    *,
+    dag_id: str,
+    run_id: str,
+    task_id: str,
+    params: GetXcomFilterParams,
+    session: Session,
+    dag_bag: DBDagBag,
+) -> int | None:
+    """
+    Return the map index to read, or None for every instance of a mapped task.
+
+    An omitted map index is resolved by the producing task's definition in the
+    Dag version its run uses: a mapped operator, or a task in a mapped task
+    group, is mapped however many instances it expanded to. A task that cannot
+    be found reads -1, so the read reports the XCom as not found.
+    """
+    if params.map_index is not None:
+        return params.map_index
+    if params.offset is not None:
+        return -1  # None reads a mapped task's list.
+    try:
+        task = TaskCoordinateResolver(dag_bag, session).get_task(dag_id, 
run_id, task_id)
+    except AmbiguousProducerError as error:

Review Comment:
   Found it during the review and it's out of scope of current PR.
   I will track the issue or raise the follow-up for it.
   
   ---
   
   `get_task` without `dag_version_id` goes through `_producer_task`, which 
compares every live TI's Dag version and raises on a mismatch. 
`_build_producer_request` uses the caller's pinned version when the caller 
shares the run, so before this PR a same-run read never hit this 409. Now, if 
the producer changed between mapped and unmapped across versions in one run, a 
plain read with no `map_index` fails. Should this pass the caller's 
`dag_version_id` for a same-run caller, the way `_build_producer_request` does?



##########
airflow-core/src/airflow/api_fastapi/execution_api/routes/xcoms.py:
##########
@@ -553,6 +553,36 @@ def head_xcom(
     response.headers["Content-Range"] = f"map_indexes {count}"
 
 
+def _resolve_requested_map_index(
+    *,
+    dag_id: str,
+    run_id: str,
+    task_id: str,
+    params: GetXcomFilterParams,
+    session: Session,
+    dag_bag: DBDagBag,
+) -> int | None:
+    """
+    Return the map index to read, or None for every instance of a mapped task.
+
+    An omitted map index is resolved by the producing task's definition in the
+    Dag version its run uses: a mapped operator, or a task in a mapped task
+    group, is mapped however many instances it expanded to. A task that cannot
+    be found reads -1, so the read reports the XCom as not found.
+    """
+    if params.map_index is not None:
+        return params.map_index
+    if params.offset is not None:
+        return -1  # None reads a mapped task's list.
+    try:
+        task = TaskCoordinateResolver(dag_bag, session).get_task(dag_id, 
run_id, task_id)
+    except AmbiguousProducerError as error:
+        raise HTTPException(status.HTTP_409_CONFLICT, str(error)) from error
+    except (TaskNotFound, ValueError):
+        return -1
+    return None if task.get_needs_expansion() else -1

Review Comment:
   A caller inside a mapped task group now gets every sibling instance's value 
instead of its own.
   
   ```python
   @task_group
   def group(value):
       @task
       def a(value):
           return value
   
       @task
       def b(ti):
           return ti.xcom_pull(task_ids="group.a", map_indexes=None)
   
       a(value) >> b()
   
   group.expand(value=[1, 2])
   ```
   
   `group.b` at map index 1 reads `group.a` with no `map_index`:
   
   | | Response | `xcom_pull` returns |
   |---|---|---|
   | Before (API 2026-10-30) | 404 | `None` |
   | After (head) | 200, `[1, 2]` | `[1, 2]` |
   | Scoped to the caller (alternative) | 200, `2` | `2` |
   
   Another valid result is the caller's own instance: `2` at map index 1, `1` 
at map index 0.
   That matches how loop producers are already scoped.
   
   Which result do we want here?



##########
airflow-core/src/airflow/api_fastapi/execution_api/routes/xcoms.py:
##########
@@ -607,6 +640,23 @@ def get_xcom(
                 ),
             },
         )
+    if map_index is None:

Review Comment:
   This repeats the ordering and value extraction in `get_mapped_xcom_by_slice`.
   
   Could we have a shared helper  to avoid drift and ensure both case get the 
same result?



##########
airflow-core/src/airflow/api_fastapi/execution_api/routes/xcoms.py:
##########
@@ -575,6 +605,9 @@ def get_xcom(
     token: ExecutionToken = _TriggerAwareToken,
 ) -> XComResponse:
     """Get an Airflow XCom from database - not other XCom Backends."""
+    map_index = _resolve_requested_map_index(

Review Comment:
   This builds its own `TaskCoordinateResolver`, and `_build_xcom_read` builds 
another below, so a mapped read runs the TI version query twice. Could the two 
share one resolver?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to