Commit 36168b3
feat(sdk): expose user-provided idempotency key and scope in task context (#2903)
## Summary
- Store the original user-provided idempotency key and scope alongside
the hash
- Expose `ctx.run.idempotencyKey` as the user-provided key (not the
hash)
- Add `ctx.run.idempotencyKeyScope` to show the scope ("run", "attempt",
or "global")
<img width="539" height="450" alt="CleanShot 2026-01-19 at 11 40 46"
src="https://github.com/user-attachments/assets/b6f42991-697e-4314-a164-aef77b8fd25c"
/>
## Problem
Idempotency keys were hashed (SHA-256) before storage, making debugging
difficult since users couldn't see the value they originally set or
search for runs by idempotency key.
## Solution
Attach metadata to the `String` object returned by
`idempotencyKeys.create()` using a Symbol, extract it in the SDK before
the API call, and store it in the database alongside the hash.
```typescript
const key = await idempotencyKeys.create("my-key", { scope: "global" });
await childTask.triggerAndWait(payload, { idempotencyKey: key });
// In child task:
ctx.run.idempotencyKey // "my-key" (previously showed the hash)
ctx.run.idempotencyKeyScope // "global"
```
Test plan
- Trigger task with idempotencyKeys.create() using different scopes (run, attempt, global)
- Verify ctx.run.idempotencyKey returns user-provided key
- Verify ctx.run.idempotencyKeyScope returns correct scope
- Verify PostgreSQL stores idempotencyKeyOptions JSON
- Verify ClickHouse receives idempotency_key_user and idempotency_key_scope via replication
---------
Co-authored-by: James Ritchie <james@trigger.dev>1 parent c859be9 commit 36168b3
File tree
29 files changed
+1104
-163
lines changed- .changeset
- apps/webapp
- app
- presenters/v3
- routes
- resources.orgs.$organizationSlug.projects.$projectParam.env.$envParam.runs.$runParam.spans.$spanParam
- runEngine/services
- services
- test
- internal-packages
- clickhouse
- schema
- src
- database/prisma
- migrations/20260116154810_add_idempotency_key_options_to_task_run
- run-engine/src/engine
- systems
- packages
- core/src/v3
- idempotency-key-catalog
- schemas
- serverOnly
- utils
- trigger-sdk/src/v3
- references/hello-world/src/trigger
29 files changed
+1104
-163
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
Lines changed: 3 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| 42 | + | |
41 | 43 | | |
42 | 44 | | |
43 | 45 | | |
| |||
442 | 444 | | |
443 | 445 | | |
444 | 446 | | |
445 | | - | |
| 447 | + | |
446 | 448 | | |
447 | 449 | | |
448 | 450 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
11 | 15 | | |
12 | 16 | | |
13 | 17 | | |
| |||
229 | 233 | | |
230 | 234 | | |
231 | 235 | | |
232 | | - | |
| 236 | + | |
233 | 237 | | |
| 238 | + | |
| 239 | + | |
234 | 240 | | |
235 | 241 | | |
236 | 242 | | |
| |||
276 | 282 | | |
277 | 283 | | |
278 | 284 | | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
279 | 309 | | |
280 | 310 | | |
281 | 311 | | |
| |||
355 | 385 | | |
356 | 386 | | |
357 | 387 | | |
| 388 | + | |
358 | 389 | | |
359 | 390 | | |
360 | 391 | | |
| |||
644 | 675 | | |
645 | 676 | | |
646 | 677 | | |
647 | | - | |
| 678 | + | |
648 | 679 | | |
649 | 680 | | |
650 | 681 | | |
| |||
Lines changed: 9 additions & 43 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | 1 | | |
3 | | - | |
4 | 2 | | |
5 | | - | |
| 3 | + | |
6 | 4 | | |
7 | 5 | | |
8 | 6 | | |
9 | 7 | | |
10 | 8 | | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | 9 | | |
16 | 10 | | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
| 11 | + | |
26 | 12 | | |
27 | 13 | | |
28 | | - | |
29 | | - | |
30 | 14 | | |
31 | 15 | | |
32 | 16 | | |
| |||
54 | 38 | | |
55 | 39 | | |
56 | 40 | | |
57 | | - | |
58 | | - | |
| 41 | + | |
59 | 42 | | |
60 | 43 | | |
61 | 44 | | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
| 45 | + | |
72 | 46 | | |
73 | 47 | | |
74 | 48 | | |
| |||
85 | 59 | | |
86 | 60 | | |
87 | 61 | | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
| 62 | + | |
93 | 63 | | |
94 | 64 | | |
95 | 65 | | |
96 | 66 | | |
97 | | - | |
| 67 | + | |
98 | 68 | | |
99 | 69 | | |
100 | 70 | | |
101 | 71 | | |
102 | 72 | | |
103 | | - | |
| 73 | + | |
104 | 74 | | |
105 | 75 | | |
106 | 76 | | |
| |||
110 | 80 | | |
111 | 81 | | |
112 | 82 | | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
| 83 | + | |
118 | 84 | | |
119 | 85 | | |
120 | 86 | | |
121 | | - | |
| 87 | + | |
122 | 88 | | |
123 | 89 | | |
124 | 90 | | |
| |||
0 commit comments