This is an automated email from the ASF dual-hosted git repository.

uranusjr pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 3aac6366554 Add TS ADR-0004 for task mapping (#77310)
3aac6366554 is described below

commit 3aac636655449f3490ec490f554584c683cb7ff6
Author: Tzu-ping Chung <[email protected]>
AuthorDate: Wed Nov 25 03:21:48 2026 +0800

    Add TS ADR-0004 for task mapping (#77310)
    
    * Add TS ADR-0004 for task mapping
    
    ADR-0016 says what every SDK does with a mapped task, but not how a
    TypeScript author writes one. Record the factory methods, the types
    that make a reduce and the rejected shapes fail in tsc, how
    expandKwargs keeps a handler's defaults, how padding stands in for
    Python's fillvalue, how a null or missing result drops out of a
    mapping, and what stays unmapped for now.
    
    ADR-0016's TypeScript example follows the padding spelling and links
    to ADR-0004 for the full form.
    
    ADR-0016's table for a handler's own getXCom read also now says a map
    index on an unmapped task, or one out of range on a mapped task, reads
    as not found, as #76825 and #76826 implement, rather than as an error.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Claude-Session: https://claude.ai/code/session_01GmjkXyAqtBrP7Qi97jgw4C
    
    * Say how a spread binding's map() is found
    
    A Dag definition's runtime finds a map() wrapper's closures by the
    parameter name its binding carries, but a spread binding, which a
    TypeScript task's expandKwargs writes, carries none, so the rule left
    those callables with no key to be found by. A task has at most one
    expand_kwargs() input, so the task alone identifies them. Say so in
    ADR-0016's map() section, and in ADR-0004 decision 6.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Claude-Session: https://claude.ai/code/session_01GmjkXyAqtBrP7Qi97jgw4C
    
    * Keep TaskFactory's call for an arg-less task
    
    ADR-0004 turned TaskFactory into a plain interface whose call always
    takes an inputs object, so a factory for a handler with no arguments,
    such as listFiles in its own example, no longer type-checked as the
    SDK's conditional type has it. Keep that type's no-argument branch,
    and put the call and the mapping methods on MappableTaskFactory, which
    only a handler with arguments gets, as decision 1 already says.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Claude-Session: https://claude.ai/code/session_01GmjkXyAqtBrP7Qi97jgw4C
    
    * Extend TaskInputs instead of the TaskInput type
    
    ADR-0004 declared a TaskInput type for the reduce arm, but the SDK
    already exports TaskInput with another meaning, a typed ref for one
    value. The loose TaskRef arm the ADR builds on is in TaskInputs, which
    a task's call and partial() read. Add the arm there, so the export
    keeps its meaning and a reduce type-checks where inputs are checked.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Claude-Session: https://claude.ai/code/session_01GmjkXyAqtBrP7Qi97jgw4C
    
    * Define the helper types ADR-0004 refers to
    
    The Signature section used ELEMENT_TYPE, HasElements, ElementsOf and
    ElementsOrFill without defining them, so neither a reader nor tsc
    could tell what zip returns. Define them beside ElementOf. ElementsOf
    tells a source apart by its marker before trying ConcatSource, whose
    shape a TaskRef and a MappedTaskRef also have.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Claude-Session: https://claude.ai/code/session_01GmjkXyAqtBrP7Qi97jgw4C
    
    * Fill in TaskRef.zip's signature in ADR-0004
    
    TaskRef's zip returned a placeholder and had no padded overload, so
    the ADR never said what files.zip(sizes, { padding: null }) gives,
    which its own example relies on. Spell out both overloads as
    MappedTaskRef has them, with the reference's element first, and wrap
    zip and concat to the file's 100 columns.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Claude-Session: https://claude.ai/code/session_01GmjkXyAqtBrP7Qi97jgw4C
    
    * Say a concat result has no zip in ADR-0004
    
    Decision 5 listed a concat result among references with all three
    methods, but ConcatSource has only map and concat, and a zip result
    only map, so Python's a.concat(b).zip(c) and a.zip(b).concat(c) do
    not compile here. ADR-0016 rejects neither, so record them as left
    out for now rather than as one of its rejected shapes.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Claude-Session: https://claude.ai/code/session_01GmjkXyAqtBrP7Qi97jgw4C
    
    * Reject a mapped loop body reduced from outside
    
    LoopMappedTaskRef extends MappedTaskRef, so tsc accepts a mapped loop
    body task as a reduce from outside its loop, where core serves only
    the all() read. Say in decision 12 that the Dag read rejects it, as
    ADR-0003 rejects a bare LoopTaskRef, and list it with the other checks
    left to the Dag read, now as bullets.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Claude-Session: https://claude.ai/code/session_01GmjkXyAqtBrP7Qi97jgw4C
    
    * Drop a triggerRule case TypeScript cannot reach
    
    Decision 13 had the triggerRule always check also cover an upstream
    inside a literal expandKwargs list, as Python's does. A TypeScript
    literal list holds only JSON values, and the SDK rejects nested refs,
    so no such upstream can exist, and the check covers a reference only.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Claude-Session: https://claude.ai/code/session_01GmjkXyAqtBrP7Qi97jgw4C
    
    * Make expandedTiCount null for an unmapped task
    
    ADR-0004 had expandedTiCount report -1 for a task that is not mapped,
    a value the TypeScript context would have to make up: the Execution
    API leaves expanded_ti_count out for such a task, and Python's is
    None. Type it number | null, and say in decision 9 that it differs
    from mapIndex there, which keeps the -1 the wire sends.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Claude-Session: https://claude.ai/code/session_01GmjkXyAqtBrP7Qi97jgw4C
    
    * Rewrite ADR-0004's example in the SDK's style
    
    The example declared bare handler signatures and called each task on
    one long line with default ids, unlike the SDK's own examples, and
    named a handler process, which shadows Node's global. Write it as
    #76539 does: explicit ids, inline handlers, one call per line and
    named sources. That also fixes the zip example's argument type, where
    either side can be padded with null.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Claude-Session: https://claude.ai/code/session_01GmjkXyAqtBrP7Qi97jgw4C
    
    * Reformat
    
    * Use the factory in ADR-0004's marker example
    
    The per-argument marker alternative called expand on measure, which
    the rewritten example declares as a handler, not a task factory, so
    the call would not compile. Use measureFile, the factory the example
    registers it under, and split the line to the file's 100 columns.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Claude-Session: https://claude.ai/code/session_01GmjkXyAqtBrP7Qi97jgw4C
    
    * Say which of ADR-0004's additions are exports
    
    Consequences listed setRenderedMapIndex and expandedTiCount as new
    exports, but they are members of the client and the context, and it
    left out the new types. Say that SkipError is the only new value
    export and the rest is types and two members, which is what the
    SDK's public API list will be checked against. Also drop extract()
    from the TaskRef comment, a name this ADR never defines.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Claude-Session: https://claude.ai/code/session_01GmjkXyAqtBrP7Qi97jgw4C
    
    ---------
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../lang-sdk/0016-mapped-tasks-across-lang-sdks.md |  20 +-
 .../adr/0004-task-mapping-authoring-interface.md   | 497 +++++++++++++++++++++
 2 files changed, 508 insertions(+), 9 deletions(-)

diff --git a/airflow-core/adr/lang-sdk/0016-mapped-tasks-across-lang-sdks.md 
b/airflow-core/adr/lang-sdk/0016-mapped-tasks-across-lang-sdks.md
index b44d71cca40..e85f72a2e0e 100644
--- a/airflow-core/adr/lang-sdk/0016-mapped-tasks-across-lang-sdks.md
+++ b/airflow-core/adr/lang-sdk/0016-mapped-tasks-across-lang-sdks.md
@@ -103,8 +103,9 @@ The coordinator only starts the SDK runtime subprocess; the 
supervisor handles e
 - A reduce consumer receives a sequence of the element type, not the element 
type.
 - Expanding over a list of `T` gives each instance a `T`; expanding over a 
dict gives each instance a `[key, value]` pair, as a Python task gets `(key, 
value)`.
 
-The running example, in the TypeScript SDK. Every later section follows this 
same `extract →
-transform → report`.
+The running example, in the TypeScript SDK, whose full spelling is
+[`ts-sdk/adr/0004`](../../../ts-sdk/adr/0004-task-mapping-authoring-interface.md).
 Every later
+section follows this same `extract → transform → report`.
 
 ```ts
 async function extract(): Promise<string[]>; // ["uk ", " fr", "de "]
@@ -143,7 +144,7 @@ run.
     "arg": {"task_id": "extract", "key": "return_value"},
     "callables": ["(row) => row.trim()"]}}}}}
 
-// .expand({ row: rows.zip(lookup, { fillValue: "" }) })
+// .expand({ row: rows.zip(lookup, { padding: "" }) })
 "expand_input": {"type": "dict-of-lists", "value": {"__type": "dict", "__var": 
{
   "row": {"__type": "xcomref", "__var": {
     "type": "zip",
@@ -541,6 +542,7 @@ The supervisor answers `ApplyMap` the way it answers an 
XCom backend request, on
 Only a stub TaskHandler with an `apply_map` binding starts the sidecar for 
this.
 
 With `.expandKwargs(rows.map(toKwargs))` the callables build each instance's 
dict, and each parameter takes its `kwarg` from it.
+A `spread` binding has no parameter name, so a Dag definition's runtime finds 
its callables as its task's one `expand_kwargs()` input.
 `toKwargs` runs once per instance: a runtime reuses the result across 
parameters, and the sidecar does the same across `ApplyMap` requests.
 
 A `map()` callable may skip its instance, as a Python one does by raising 
`AirflowSkipException`.
@@ -568,15 +570,15 @@ still reads as mapped:
 
 | Producer | Map index | The read returns |
 |---|---|---|
-| Not mapped | none | its value |
-| Not mapped | any | an error |
+| Not mapped | none or `-1` | its value |
+| Not mapped | any other | not found |
 | Mapped | none | every instance's value, as a list in map-index order; `[]` 
when it expanded to nothing |
-| Mapped | `0 <= i <` instance count | instance `i`'s value |
-| Mapped | any other, negative included | an error |
+| Mapped | `0 <= i < length` | instance `i`'s value |
+| Mapped | any other | not found |
 
-An error is not a missing value: a read in range with nothing pushed still 
returns null.
+Not found reads as null in every SDK, the same as a value nothing pushed.
 Unlike a reduce binding, the whole-output read is materialized, because the 
handler asked for the value.
-It is an Execution API mode of its own; Python's `XCom.get_one`, where no map 
index still means the `-1` row, is unchanged.
+The server resolves an omitted map index this way on the ordinary read, and a 
Python task reading with no map index, as `XCom.get_one(map_index=None)` does, 
gets the same answers.
 
 ### Python sidecar subprocess for the XCom backend
 
diff --git a/ts-sdk/adr/0004-task-mapping-authoring-interface.md 
b/ts-sdk/adr/0004-task-mapping-authoring-interface.md
new file mode 100644
index 00000000000..dcef5ac73e6
--- /dev/null
+++ b/ts-sdk/adr/0004-task-mapping-authoring-interface.md
@@ -0,0 +1,497 @@
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied.  See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ -->
+
+# ADR-0004: Task Mapping — TypeScript Authoring Interface
+
+## Status
+
+Proposed.
+
+## Context
+
+What a mapped task is, what each process does with it, and the authoring rules 
every SDK follows are in
+[`airflow-core/adr/lang-sdk/0016`](../../airflow-core/adr/lang-sdk/0016-mapped-tasks-across-lang-sdks.md).
+This ADR records the TypeScript spelling of them.
+
+A handler already takes one object of named arguments
+([ADR-0002](0002-native-dag-interface.md), decision 1), so every argument has 
a key, and TypeScript
+can keep Python's keyword spelling where a positional SDK needs a marker 
(ADR-0016, *Authoring
+interface*).
+
+Two rules from outside this ADR shape it. A Dag definition's own task, which 
every task of a
+TypeScript Dag is, may have its `expand_kwargs()` dicts spread into its 
handler's arguments, so the
+handler's own defaults apply (ADR-0016, *Stub-task arg bindings*). And a 
TypeScript task pushes what
+it returns: `undefined` pushes nothing, and `null` is a value it pushes, as 
`setXCom(null)` does.
+
+## Decision
+
+1. **`expand`, `partial` and `expandKwargs` are methods of the task factory.**
+   `transform.expand({...})`, `transform.partial({...}).expand({...})`,
+   `transform.expandKwargs(source)` and 
`transform.partial({...}).expandKwargs(source)` each count as
+   the task's one call (ADR-0002, decision 3). A `partial` that is never 
expanded leaves the task
+   uncalled, so it fails when the Dag is read. A task with no arguments has 
none of the three.
+2. **The keys are the handler's own, and mapping does not change the 
handler.** `transform` takes
+   `{ row: string }` whether it is mapped or not. With `expand`, every 
required key is supplied by
+   `partial` or by `expand`, an optional one may be left to the handler's 
default, no key is
+   supplied by both, and an unknown key is a compile error. The only argument 
types mapping
+   brings are `XComSequence<T>` for a reduce and `[K, V]` for expanding over a 
dict. An argument type
+   with an index signature, such as `Record<string, X>`, has no keys to check, 
so all three methods
+   reject it at compile time.
+3. **`expandKwargs` spreads each instance's object into the handler's 
arguments.** The task writes
+   ADR-0016's `kwargs` entry, so a key the object leaves out takes the 
handler's own default, as it
+   does in an unmapped call that leaves the key out. A key that `partial` also 
sets fails the
+   instance, as Python's `mapping already partial` does. `tsc` checks the 
objects of a literal list
+   against the handler's type. Objects an upstream builds are checked only as 
far as that upstream's
+   return type goes: their keys exist only at run time, and the runtime has no 
parameter list to
+   check them against, so a key the handler does not take is passed on with 
the rest.
+4. **A mapped task returns a `MappedTaskRef<T>`, which is not a `TaskRef`.** 
It carries `before`
+   and `after` like any node, and goes into a later `expand` for one instance 
per upstream instance
+   with a non-`null` result. Passed anywhere else, as a plain input or through 
`partial`, it is
+   accepted only where the argument is `XComSequence<T>`, and serializes as a 
reduce. No call marks
+   the reduce, unlike a loop body's `all()` 
([ADR-0003](0003-task-loop-authoring-interface.md),
+   decision 6): a mapped task has no one-instance reading outside an `expand`, 
so the type alone
+   says which reading is meant. A branch target, in `then`, `else` or `case`, 
also takes a
+   `MappedTaskRef`, since a branch runs or skips a mapped task as a whole, as 
it does in Python.
+5. **`map`, `zip` and `concat` are methods of a reference whose value has 
elements**: a `TaskRef` to
+   an array or a record, or a `MappedTaskRef`. A `concat` result offers `map` 
and `concat`, and a
+   `zip` result only `map`, so this SDK leaves out Python's 
`a.concat(b).zip(c)` and
+   `a.zip(b).concat(c)` for now. Each returns a source that only `expand` and 
`expandKwargs` take,
+   so ADR-0016 decision 4's rejected shapes do not compile:
+   - **`map` stays outermost.** A `map` result offers only `map`, and chained 
calls add callables to
+     one wrapper.
+   - **No `zip` inside a `zip`.** A `zip` result offers only `map`.
+   - **No whole-argument use.** Neither a plain input nor `partial` takes a 
source.
+6. **A `map` callable is a synchronous function of one element**: `(element: 
T) => U`, with `U` not
+   a promise. It runs in the consumer's runtime, found by the argument's name, 
or for `expandKwargs`
+   as the task's one source (ADR-0016, *map()*), so an anonymous arrow 
function is fine. It skips
+   its instance by throwing `SkipError`, a new export, the TypeScript 
equivalent of
+   `AirflowSkipException`. It is serialized as `fn.toString()`.
+7. **`zip` fills with `padding`, JavaScript's name for it.** `{ padding: v }` 
makes the zip as long
+   as its longest source, with `v` standing in for the missing items, as 
`Iterator.zip`'s `padding`
+   does; without it, the zip is as long as its shortest. It is one value for 
every source, as
+   Python's `fillvalue` is, and serialized as `fillvalue`. There is no `mode`: 
giving `padding` asks
+   for the longest zip, as giving `fillvalue` does in Python. `padding` is a 
JSON value, so it
+   cannot be `undefined`: that has no JSON form, and serializing it would drop 
the key and quietly
+   give the shortest zip. A padded item reaches the handler as `null` either 
way, so
+   `{ padding: null }` is the way to pad with nothing. A `padding` key holding 
`undefined` fails
+   when the Dag is read.
+8. **The instance label is a client call, and `mapIndexTemplate` leaves 
`TaskSpec`.**
+   `getClient().setRenderedMapIndex(label)` sends `SetRenderedMapIndex` 
(ADR-0016, decision 7). The
+   generated `TaskSpec` drops `mapIndexTemplate`, since nothing renders its 
Jinja, and a Dag that
+   still sets it fails when it is read, with an error that names 
`setRenderedMapIndex`. It keeps
+   `maxActiveTisPerDagrun`, an ordinary task setting. Jinja templating needs 
an equivalent in each
+   task language, for this field and every other one Python templates; a later 
ADR specifies it for
+   every SDK.
+9. **`getContext()` gains `expandedTiCount`**, read-only beside `mapIndex`, 
for "instance 3 of 10".
+   It is `null` for a task that is not mapped, where `mapIndex` is `-1`: the 
Execution API leaves
+   the count out, and Python's is `None`. It is never used to resolve 
arguments (ADR-0016, *Acting
+   on `expanded_ti_count`*).
+10. **A reduce reads an `XComSequence<T>`, defined here.** ADR-0003 already 
names the type for a
+    loop's `all()`. It reads on demand, as Python's `LazyXComSequence` does 
(ADR-0016, decision 8):
+    `length()`, `at(i)` with a negative `i` counting from the end, 
`slice(start, stop)`, and
+    `for await`. Its elements are in map-index order, and an instance whose 
result is `null`, or
+    that pushed no value, is not one of them. It is valid only in the handler 
call that received it,
+    so it cannot be returned, pushed or passed on.
+11. **A mapped instance that returns `null` or nothing gives no downstream 
instance.** It pushes a
+    `null` or no value, as any task does, and a later `expand` and a reduce 
both leave it out, as
+    they leave out a Python instance that returned `None`. So a mapped task's 
element type is its
+    return type without `null` and `undefined`: 
`MappedTaskRef<NonNullable<TReturn>>`. An unmapped
+    producer that a later `expand` reads must push a list or a record to map 
over (ADR-0016,
+    decision 3).
+12. **A mapped task in a loop body is a `LoopMappedTaskRef<T>`**: a 
`MappedTaskRef<T>` that records
+    its loop. Inside the loop it is a `MappedTaskRef<T>` for one iteration. 
From outside, only `all()`
+    reads it, a `TaskRef<XComSequence<T>>` ordered by iteration, then by map 
index (ADR-0016,
+    *Reduce*). ADR-0003's `LoopTaskRef<T>` stays the type of an unmapped body 
task. An `expand` over
+    a body task from outside its loop fails when the Dag is read, ADR-0017's 
rule (decision 3)
+    checked early. Passed bare as a reduce from outside its loop, it fails 
when the Dag is read too,
+    as a bare `LoopTaskRef` does (ADR-0003, decision 6), since `tsc` accepts 
it there as a
+    `MappedTaskRef`.
+13. **`triggerRule: "always"` is rejected on a task that expands over an 
upstream's output**, when
+    the Dag is read, as Python rejects it: `always` runs the task even when 
that upstream failed and
+    left nothing to expand over. The check covers `expandKwargs` over a 
reference.
+14. **Three things are not mapped, each behind its own capability flag** 
(ADR-0016, decision 4):
+    - **Task groups.** `dag.taskGroup(id)` takes no inputs, so there is 
nothing to expand. A later ADR
+      designs groups that take inputs, together with ADR-0016's rules for 
them: no `expand` inside a
+      mapped group, no loop inside one, and the deeper-nesting rule.
+    - **Operator tasks.** `triggerDagRun` is an `Operator<void, void>`, whose 
factory takes no
+      arguments to expand.
+    - **Deciders.** `if` and `switch`, on a Dag or a task group, take no 
`expand`.
+
+## Example
+
+```ts
+import { Dag, type XComSequence } from "@apache-airflow/ts-sdk";
+
+const dag = new Dag("mapping_example", { schedule: "@daily", catchup: false });
+
+// Shared handlers, each registered under several task ids below.
+const measure = async ({ path, bucket }: { path: string; bucket: string }) =>
+  path.length + bucket.length;
+
+const sizeIfLarge = async ({ path, min = 5 }: { path: string; min?: number }) 
=>
+  path.length >= min ? path.length : undefined;
+
+const summarizeSizes = async ({ sizes }: { sizes: XComSequence<number> }) => {
+  console.log(await sizes.length());
+};
+
+const listFiles = dag.task("list_files", async (): Promise<string[]> => 
["a.csv", "b.csv"]);
+const listOtherFiles = dag.task("list_other_files", async (): 
Promise<string[]> => ["c.csv"]);
+const files = listFiles(); // TaskRef<string[]>
+const otherFiles = listOtherFiles();
+
+// Expand over an upstream's list, a literal, or a mapped upstream.
+const measureFile = dag.task("measure_file", measure);
+// MappedTaskRef<number>
+const sizes = measureFile.partial({ bucket: "raw" }).expand({ path: files });
+
+// A cross product: four instances.
+const measureGrid = dag.task("measure_grid", measure);
+measureGrid.expand({ path: ["a.csv", "b.csv"], bucket: ["raw", "eu"] });
+
+// One audit per measure_file instance.
+const audit = dag.task("audit", async ({ size }: { size: number }) => {
+  console.log(size);
+});
+audit.expand({ size: sizes });
+
+// One [key, value] pair per instance.
+const tag = dag.task("tag", async ({ entry }: { entry: [string, string] }) => {
+  console.log(entry[0], entry[1]);
+});
+tag.expand({ entry: { env: "prod", team: "data" } });
+
+// A reduce: a mapped task where the argument is a sequence.
+const summarize = dag.task("summarize", summarizeSizes);
+summarize({ sizes });
+
+const report = dag.task(
+  "report",
+  async ({ sizes, format }: { sizes: XComSequence<number>; format: string }) 
=> {
+    console.log(format, await sizes.length());
+  },
+);
+report.partial({ sizes }).expand({ format: ["csv", "pdf"] });
+
+// expandKwargs: one argument object per instance, spread into the handler's 
arguments.
+const measureKwargs = dag.task("measure_kwargs", measure);
+const fileArgs = files.map((path) => ({ path, bucket: "raw" }));
+measureKwargs.expandKwargs(fileArgs);
+
+const measureSome = dag.task("measure_some", measure);
+measureSome.partial({ bucket: "raw" }).expandKwargs([{ path: "a.csv" }, { 
path: "b.csv" }]);
+
+// The first instance leaves `min` to the handler's default.
+const sizeSome = dag.task("size_some", sizeIfLarge);
+sizeSome.expandKwargs([{ path: "a.csv" }, { path: "b.csv", min: 10 }]);
+
+// An instance that returns nothing is not in the reduce.
+const sizeLarge = dag.task("size_large", sizeIfLarge);
+const large = sizeLarge.expand({ path: files }); // MappedTaskRef<number>
+const summarizeLarge = dag.task("summarize_large", summarizeSizes);
+summarizeLarge({ sizes: large });
+
+// Sources built with map, zip and concat.
+const measureTrimmed = dag.task("measure_trimmed", measure);
+const trimmed = files.map((path) => path.trim());
+measureTrimmed.partial({ bucket: "raw" }).expand({ path: trimmed });
+
+// Either side may be padded, so both slots can be null.
+const pair = dag.task("pair", async ({ p }: { p: [string | null, number | 
null] }) => {
+  console.log(p[0], p[1]);
+});
+const padded = files.zip(sizes, { padding: null });
+pair.expand({ p: padded });
+
+const measureBoth = dag.task("measure_both", measure);
+const allFiles = files.concat(otherFiles);
+measureBoth.partial({ bucket: "raw" }).expand({ path: allFiles });
+
+// A mapped task as a branch target, and as an ordering node.
+const hasFiles = async ({ files: paths }: { files: string[] }) => paths.length 
> 0;
+const gate = dag.if(hasFiles, { files }, { taskId: "has_files" });
+gate.then(sizes);
+
+const cleanup = dag.task("cleanup", async () => {});
+const cleaned = cleanup();
+sizes.before(cleaned);
+```
+
+## Signature
+
+```ts
+/** A mapped task. It goes on edges and into `expand`; elsewhere only as a 
reduce. */
+interface MappedTaskRef<T = unknown> extends Node {
+  readonly taskId: string;
+  /** @internal Never set; the element type, one instance's return value. */
+  readonly [ELEMENT_TYPE]?: T;
+  before(...downstream: readonly Node[]): MappedTaskRef<T>;
+  after(...upstream: readonly Node[]): MappedTaskRef<T>;
+  map<U>(fn: MapCallable<T, U>): MapSource<U>;
+  zip<const S extends readonly Expandable[]>(...others: S): ZipSource<[T, 
...ElementsOf<S>]>;
+  zip<const S extends readonly Expandable[], F extends JsonValue>(
+    ...others: [...S, ZipOptions<F>]
+  ): ZipSource<[T | F, ...ElementsOrFill<S, F>]>;
+  concat(...others: readonly Expandable<T>[]): ConcatSource<T>;
+}
+
+/** A mapped task in a loop body. Inside the loop a `MappedTaskRef<T>`; from 
outside, only `all()`. */
+interface LoopMappedTaskRef<T = unknown> extends MappedTaskRef<T> {
+  readonly loopId: string;
+  /** Every instance of every iteration, ordered by iteration, then map index. 
*/
+  all(): TaskRef<XComSequence<T>>;
+}
+
+/**
+ * `TaskRef` gains the same three, through a `this` parameter that admits only 
a reference to an
+ * array or a record, so `.map(...)` on a `TaskRef<number>` is a compile error.
+ */
+interface TaskRef<TReturn> {
+  map<U>(this: TaskRef<HasElements>, fn: MapCallable<ElementOf<TReturn>, U>): 
MapSource<U>;
+  zip<const S extends readonly Expandable[]>(
+    this: TaskRef<HasElements>,
+    ...others: S
+  ): ZipSource<[ElementOf<TReturn>, ...ElementsOf<S>]>;
+  zip<const S extends readonly Expandable[], F extends JsonValue>(
+    this: TaskRef<HasElements>,
+    ...others: [...S, ZipOptions<F>]
+  ): ZipSource<[ElementOf<TReturn> | F, ...ElementsOrFill<S, F>]>;
+  concat(
+    this: TaskRef<HasElements>,
+    ...others: readonly Expandable<ElementOf<TReturn>>[]
+  ): ConcatSource<ElementOf<TReturn>>;
+}
+
+/** An array's element, or a record's `[key, value]` pair (ADR-0016, Authoring 
interface). */
+type ElementOf<R> = R extends readonly (infer E)[]
+  ? E
+  : R extends Readonly<Record<infer K extends string, infer V>>
+    ? [K, V]
+    : never;
+
+/** Marks a mapped task's element type, as the SDK's `RETURN_TYPE` marks a 
`TaskRef`'s value. */
+declare const ELEMENT_TYPE: unique symbol;
+
+/** A value with elements: an array or a record. */
+type HasElements = readonly unknown[] | Readonly<Record<string, unknown>>;
+
+/** Each `zip` source's element type, in order. */
+type ElementsOf<S extends readonly unknown[]> = {
+  [I in keyof S]: S[I] extends { readonly [RETURN_TYPE]?: infer R }
+    ? ElementOf<R>
+    : S[I] extends { readonly [ELEMENT_TYPE]?: infer E }
+      ? E
+      : S[I] extends ConcatSource<infer E>
+        ? E
+        : never;
+};
+
+/** Each `zip` source's element type, or the padding type where that source 
runs out. */
+type ElementsOrFill<S extends readonly unknown[], F> = {
+  [I in keyof S]: ElementsOf<[S[I]]>[0] | F;
+};
+
+/** What `zip` and `concat` take: something with elements of `T`. A `zip` or 
`map` result is not. */
+type Expandable<T = unknown> = TaskRef<readonly T[]> | MappedTaskRef<T> | 
ConcatSource<T>;
+
+type MapCallable<T, U> = (element: T) => U extends PromiseLike<unknown> ? 
never : U;
+
+interface MapSource<T> { map<U>(fn: MapCallable<T, U>): MapSource<U>; }
+interface ZipSource<T> { map<U>(fn: MapCallable<T, U>): MapSource<U>; }
+interface ConcatSource<T> {
+  map<U>(fn: MapCallable<T, U>): MapSource<U>;
+  concat(...others: readonly Expandable<T>[]): ConcatSource<T>;
+}
+
+/** `padding` is a JSON value: `undefined` has no JSON form, so pad with 
`null` instead. */
+interface ZipOptions<F extends JsonValue> { padding: F; }
+
+/** What one expanded argument of type `T` can be given. */
+type ExpandSource<T> =
+  | readonly JsonCompatible<T>[]
+  | (T extends readonly [infer K extends string, infer V]
+      ? Readonly<Record<K, JsonCompatible<V>>> | TaskRef<Readonly<Record<K, 
V>>>
+      : never)
+  | TaskRef<readonly T[]>
+  | MappedTaskRef<T>
+  | MapSource<T>
+  | ZipSource<T>
+  | ConcatSource<T>;
+
+type ExpandInputs<TArgs> = { [K in keyof TArgs]: ExpandSource<TArgs[K]> };
+
+/** What `expandKwargs` takes: elements that are each one instance's remaining 
arguments. */
+type KwargsSource<TArgs> =
+  | readonly JsonCompatible<TArgs>[]
+  | TaskRef<readonly TArgs[]>
+  | MappedTaskRef<TArgs>
+  | MapSource<TArgs>;
+
+/** A plain input also takes a mapped task, as a reduce, where the argument is 
a sequence. */
+type TaskInputs<TArgs> = {
+  [K in keyof TArgs]:
+    | TaskRef
+    | JsonCompatible<TArgs[K]>
+    | (TArgs[K] extends XComSequence<infer E> ? MappedTaskRef<E> : never);
+};
+
+/** `never` for an argument type with no known keys, which turns every method 
below off. */
+type Keyed<TArgs> = string extends keyof TArgs ? never : TArgs;
+
+/** A task with no arguments keeps its plain call; there is nothing to expand. 
*/
+type TaskFactory<TArgs extends object | void = void, TReturn = unknown> = 
[TArgs] extends [void]
+  ? () => TaskRef<TReturn>
+  : MappableTaskFactory<TArgs & object, TReturn>;
+
+interface MappableTaskFactory<TArgs extends object, TReturn> {
+  (inputs: TaskInputs<TArgs>): TaskRef<TReturn>;
+  expand(inputs: ExpandInputs<Keyed<TArgs>>): 
MappedTaskRef<NonNullable<TReturn>>;
+  expandKwargs(source: KwargsSource<Keyed<TArgs>>): 
MappedTaskRef<NonNullable<TReturn>>;
+  partial<const P extends Partial<TaskInputs<Keyed<TArgs>>>>(
+    inputs: P & Record<Exclude<keyof P, keyof TArgs>, never>,
+  ): PartialTask<Omit<TArgs, keyof P>, TReturn>;
+}
+
+/** What `partial` returns. Only expanding it calls the task. */
+interface PartialTask<TRest, TReturn> {
+  /** `never` once `partial` has taken every argument, so `expand({})` does 
not compile. */
+  expand(
+    inputs: keyof TRest extends never ? never : ExpandInputs<TRest>,
+  ): MappedTaskRef<NonNullable<TReturn>>;
+  expandKwargs(source: KwargsSource<TRest>): 
MappedTaskRef<NonNullable<TReturn>>;
+}
+
+/** A mapped task's whole output, read on demand. Valid only in the handler 
call that received it. */
+interface XComSequence<T> extends AsyncIterable<T> {
+  length(): Promise<number>;
+  /** A negative `i` counts from the end. Throws `RangeError` outside the 
sequence. */
+  at(i: number): Promise<T>;
+  slice(start?: number, stop?: number): Promise<T[]>;
+}
+
+/** Branch targets take a mapped task too. */
+interface Condition {
+  then(target: TaskRef | MappedTaskRef | Condition | Branch): ConditionElse;
+}
+interface ConditionElse {
+  else(target: TaskRef | MappedTaskRef | Condition | Branch): void;
+}
+interface Branch {
+  case(target: TaskRef | MappedTaskRef): Branch;
+}
+
+/** Thrown from a handler or a `map` callable to skip this instance. */
+class SkipError extends Error {}
+
+interface TaskContext {
+  /** How many instances this expansion has; `null` for a task that is not 
mapped. */
+  readonly expandedTiCount: number | null;
+}
+
+interface TaskClient {
+  /** Label this instance in the UI, as `map_index_template` does for a Python 
task. */
+  setRenderedMapIndex(label: string): Promise<void>;
+}
+```
+
+## Implementation notes
+
+What a mapped task writes, matching what `serialize_mapped_operator` writes 
for a decorated Python
+task, so core reads one shape:
+
+- `_is_mapped: true`, with the task's settings under `partial_kwargs` instead 
of at the top level.
+- The expand input under `op_kwargs_expand_input`: `dict-of-lists` for 
`expand`, `list-of-dicts`
+  for `expandKwargs` (ADR-0016, *What the Dag serialization carries*).
+- The `partial` values under `partial_kwargs.op_kwargs`.
+- `_arg_bindings` in call order, `partial` keys first. An `expand` call writes 
a `param` entry per
+  key. An `expandKwargs` call writes `param` for its `partial` keys and one 
`kwargs` entry last. A
+  key the call does not give has no entry, so the handler's default applies to 
it. No entry carries
+  `default`, since a TypeScript Dag declares defaults only in the handler, or 
`value_schema`, since
+  the handler's types are erased by then.
+- Each `map` callable as `fn.toString()`.
+
+JavaScript orders an object's integer-like keys first, so `{ "2": a, "1": b }` 
expands over `"1"`
+before `"2"`. The payload is written in that order, and that is the expansion 
order.
+
+## Consequences
+
+- **Most mistakes fail in `tsc`.** That covers:
+  - a key given twice, or a required key never given;
+  - a key the handler does not take;
+  - expanding over something that has no elements, or whose elements do not 
fit the argument;
+  - a mapped task passed as one value;
+  - every shape ADR-0016 decision 4 rejects.
+
+  Five are left to the Dag read:
+  - a `partial` never expanded, and an `expand` with no key when every 
remaining argument is
+    optional, as Python raises for both;
+  - `triggerRule: "always"` with an upstream to expand over;
+  - an `expand` over a loop body task from outside its loop;
+  - a `LoopMappedTaskRef` passed as a reduce from outside its loop.
+- **`expandKwargs` over an upstream's objects is checked at run time only for 
a key `partial` also
+  sets.** A key the handler requires but the object leaves out reaches it as 
`undefined`, and an
+  extra one is passed on. Python raises for both. This is what lets the 
handler's own defaults apply.
+- **`TaskInputs` stays as loose as it is.** A plain `TaskRef` input is still 
checked only as a
+  `TaskRef`, as ADR-0003 found. The types above check what mapping adds 
without tightening it:
+  expand sources and `MappedTaskRef` carry their element type, and neither is 
a `TaskRef`.
+- **No new runtime surface beyond ADR-0016's steps.** The handler is called 
with one instance's
+  values or an `XComSequence`. `SkipError` is the only new value export. The 
rest is types
+  (`MappedTaskRef`, `XComSequence` and the source types above) and two members,
+  `getClient().setRenderedMapIndex` and `getContext().expandedTiCount`. 
`SkipError` is the runtime's
+  first general skip; until now only a `triggerDagRun` task ended `skipped`.
+- **Removing `mapIndexTemplate` from `TaskSpec` breaks a Dag that sets it.** 
It no longer
+  compiles, and a Dag read rejects it where the types do not reach. Today that 
Dag serializes a
+  template nothing renders, so the break surfaces a setting that never worked.
+
+## Alternatives
+
+- **A per-argument marker, as a positional SDK uses**,
+  `measureFile.expand({ path: expand(files), bucket: "raw" })`.
+  Rejected: TypeScript can split the arguments by key, which is Python's own 
spelling, so
+  ADR-0016 has it keep that spelling. The marker is the substitute for an SDK 
that cannot.
+- **`MappedTaskRef<T>` as a `TaskRef<T>`, with `all()` for the reduce**, as a 
loop body's ref is.
+  Rejected: a loop body's ref has a one-iteration reading inside the loop, 
which is why the outside
+  reading has to be written. A mapped task has no one-instance reading 
anywhere but `expand`. As a
+  `TaskRef` it would also pass `TaskInputs`' loose check for any argument, so 
passing one as a
+  scalar would compile. The same holds for a mapped body task as a 
`LoopTaskRef<T>`.
+- **An array for a reduce**, `{ sizes: number[] }`. Rejected by ADR-0016 
decision 8: a reduce is a
+  lazy sequence, so its argument is `XComSequence<T>`, as a loop's `all()` 
already gives.
+- **`zip` and `concat` as free functions**, `zip(files, sizes)`. Rejected by 
ADR-0016, *Authoring
+  interface*: methods chain in declaration order.
+- **`expandKwargs` only over a literal list**, whose keys `tsc` sees. 
Rejected: per-instance argument
+  objects an upstream builds are what `expandKwargs` is for, and spreading 
them lets the handler's
+  defaults apply without a key list.
+- **The author lists `expandKwargs`' keys and defaults in the call.** 
Rejected: it repeats the
+  handler's signature, which a TypeScript task's handler already is (ADR-0016, 
*Rejected
+  alternatives*).
+- **Returning `null` pushes nothing, as Python's `None` does.** Rejected: 
TypeScript tells `null`
+  from `undefined`, so `return null` is a value its author chose. Only mapping 
needs a `null` result
+  left out, and it leaves it out without dropping the value.
+- **Mapped task groups now.** Rejected for this ADR: a group would need inputs 
to expand over, which
+  is a design of its own.
+- **Deprecating `mapIndexTemplate` before removing it.** Rejected: nothing has 
ever rendered it, so
+  a warning period would only keep a setting that does nothing.
+- **`fillValue`, Python's name in TypeScript casing.** Rejected: JavaScript 
names the same stand-in
+  `padding` in `Iterator.zip`, and the option name is not on the wire as the 
author writes it.
+- **One `padding` value per source, as `Iterator.zip` takes.** Rejected: 
Python's `fillvalue`, and
+  the wire, take one value for every source, and a per-source one would need a 
wire change for
+  little use.
+- **`mode`, as `Iterator.zip` has.** Rejected: `"longest"` without `padding` 
pads with `undefined`,
+  which serializes as `null`, the same as `{ padding: null }`. Python has no 
`"strict"` zip to map
+  with.

Reply via email to