Skip to content

Commit cca5ca3

Browse files
committed
fix(executor): a bare string reference in a JSON field splices as a JSON string
A reference outside any string literal in JSON text (a Response body, table row data, an api body) spliced a string value raw, so `{"error": <check.message>}` produced invalid JSON for any message that was not itself JSON. Such a string now lands JSON-encoded; a string that already reads as one JSON value (an agent's JSON answer, a numeric env var) still splices as written, and a reference inside a string literal keeps its existing escaping. The api block's request retry params now say what they retry (timeouts, connection resets, 429 and 5xx, for GET/HEAD/PUT/DELETE unless non-idempotent retries are on), what fails at once (an unresolvable or refusing host, other 4xx), and that the block retry setting is separate and reruns the whole block after any failure. Their titles read "Request retries", "Request retry delay (ms)" and "Max request retry delay (ms)".
1 parent f5011af commit cca5ca3

6 files changed

Lines changed: 79 additions & 26 deletions

File tree

‎apps/sim/blocks/blocks/api.ts‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,34 +103,37 @@ Example:
103103
},
104104
{
105105
id: 'retries',
106-
title: 'Retries',
106+
title: 'Request retries',
107107
type: 'short-input',
108108
placeholder: '0',
109109
description:
110-
'Number of retry attempts after a timeout, a connection reset, a 429 or a 5xx response (default: 0, no retries). A host that does not resolve or refuses the connection fails without retrying.',
110+
'Times to resend the HTTP request within one run of this block (default: 0, max: 10), only after a timeout, a connection reset, a 429 or a 5xx response, and only for GET, HEAD, PUT and DELETE unless Retry non-idempotent methods is on. A host that does not resolve or refuses the connection, and any other 4xx, fail at once. Separate from the block retry setting, which reruns the whole block, these request retries included, after any failure.',
111111
mode: 'advanced',
112112
},
113113
{
114114
id: 'retryDelayMs',
115-
title: 'Retry delay (ms)',
115+
title: 'Request retry delay (ms)',
116116
type: 'short-input',
117117
placeholder: '500',
118-
description: 'Initial retry delay in milliseconds (exponential backoff)',
118+
description:
119+
'Wait before the first request retry in milliseconds (default: 500); it doubles each retry, with jitter, up to the max request retry delay. Not the block retry wait between tries.',
119120
mode: 'advanced',
120121
},
121122
{
122123
id: 'retryMaxDelayMs',
123-
title: 'Max retry delay (ms)',
124+
title: 'Max request retry delay (ms)',
124125
type: 'short-input',
125126
placeholder: '30000',
126-
description: 'Maximum delay between retries in milliseconds',
127+
description:
128+
'Longest wait between request retries in milliseconds (default: 30000). A Retry-After header asking for longer ends the retries.',
127129
mode: 'advanced',
128130
},
129131
{
130132
id: 'retryNonIdempotent',
131133
title: 'Retry non-idempotent methods',
132134
type: 'switch',
133-
description: 'Allow retries for POST/PATCH requests (may create duplicate requests)',
135+
description:
136+
'Also resend POST, PATCH and other non-idempotent requests on request retries (may create duplicates).',
134137
mode: 'advanced',
135138
},
136139
{
@@ -169,15 +172,19 @@ Example:
169172
body: { type: 'json', description: 'Request body data' },
170173
params: { type: 'json', description: 'URL query parameters' },
171174
timeout: { type: 'number', description: 'Request timeout in milliseconds' },
172-
retries: { type: 'number', description: 'Number of retry attempts for retryable failures' },
173-
retryDelayMs: { type: 'number', description: 'Initial retry delay in milliseconds' },
175+
retries: {
176+
type: 'number',
177+
description:
178+
'Times to resend the HTTP request after a timeout, connection reset, 429 or 5xx (not the block retry setting)',
179+
},
180+
retryDelayMs: { type: 'number', description: 'Wait before the first request retry in ms' },
174181
retryMaxDelayMs: {
175182
type: 'number',
176-
description: 'Maximum delay between retries in milliseconds',
183+
description: 'Longest wait between request retries in ms',
177184
},
178185
retryNonIdempotent: {
179186
type: 'boolean',
180-
description: 'Allow retries for non-idempotent methods like POST/PATCH',
187+
description: 'Also resend POST, PATCH and other non-idempotent requests on request retries',
181188
},
182189
redirectPolicyVersion: {
183190
type: 'string',

‎apps/sim/executor/variables/resolver.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,24 @@ describe('VariableResolver JSON text inputs', () => {
14031403
})
14041404
})
14051405

1406+
it('serializes a bare string reference as a JSON string unless it already is JSON', async () => {
1407+
const { resolve } = createJsonResolver(BlockType.RESPONSE, {
1408+
dataMode: 'json',
1409+
data: '{"error": <producer.text>, "empty": <producer.blank>, "token": {{QUOTED_SECRET}}, "nested": <producer.jsonText>, "port": {{PORT}}}',
1410+
})
1411+
const resolved = await resolve({
1412+
env: { PORT: '8080' },
1413+
output: { blank: '', jsonText: '{"country": "F\\"R"}' },
1414+
})
1415+
expect(JSON.parse(resolved.data)).toEqual({
1416+
error: awkwardText,
1417+
empty: '',
1418+
token: 'p"ss\\word',
1419+
nested: { country: 'F"R' },
1420+
port: 8080,
1421+
})
1422+
})
1423+
14061424
it('escapes environment variables inside JSON strings', async () => {
14071425
await expect(resolveResponseBody('{"auth": "Bearer {{QUOTED_SECRET}}"}')).resolves.toEqual({
14081426
auth: 'Bearer p"ss\\word',

‎apps/sim/executor/variables/resolver.ts‎

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
escapeInertStringContent,
2424
formatInertStringLiteral,
2525
} from '@/executor/utils/code-formatting'
26-
import { isJSONString } from '@/executor/utils/json'
26+
import { isJSONString, mayBeJsonText } from '@/executor/utils/json'
2727
import { createEnvVarPattern, createReferencePattern } from '@/executor/utils/reference-validation'
2828
import { BlockResolver } from '@/executor/variables/resolvers/block'
2929
import { EnvResolver } from '@/executor/variables/resolvers/env'
@@ -146,21 +146,39 @@ function isJsonStringContent(value: string): boolean {
146146
}
147147
}
148148

149+
/**
150+
* Whether string `value` already reads as one JSON value (`42`, `true`, `{"a": 1}`, `"quoted"`).
151+
* A bare reference in JSON text has always spliced its string as written, and workflows rely on a
152+
* string holding JSON (an agent's JSON answer, a numeric env var) landing as that value.
153+
*/
154+
function isJsonValueText(value: string): boolean {
155+
const trimmed = value.trim()
156+
if (!mayBeJsonText(trimmed)) return false
157+
try {
158+
JSON.parse(trimmed)
159+
return true
160+
} catch {
161+
return false
162+
}
163+
}
164+
149165
/**
150166
* Splices placeholder values into `text`. When `text` is JSON, a value landing inside one of its
151167
* string literals that would break the literal becomes escaped string content, so it cannot close
152-
* the string; a value that is already valid string content is spliced as written. Placeholders
168+
* the string; a value that is already valid string content is spliced as written. A string value
169+
* landing outside a string literal becomes a JSON string unless it already reads as one JSON
170+
* value, so `{"error": <check.message>}` stays JSON whatever the message holds. Placeholders
153171
* must arrive in ascending offset order: the lexer resumes where the previous one stopped, then
154172
* skips the placeholder itself, so characters inside its own text never toggle the state.
155173
*/
156174
function placeholderSplicer(
157175
text: string,
158176
jsonText: boolean | undefined
159-
): (value: string, start: number, end: number) => string {
177+
): (value: string, start: number, end: number, isString: boolean) => string {
160178
if (!jsonText) return (value) => value
161179
let position = 0
162180
let inString = false
163-
return (value, start, end) => {
181+
return (value, start, end, isString) => {
164182
while (position < start) {
165183
const char = text[position]
166184
if (inString && char === '\\') {
@@ -171,7 +189,10 @@ function placeholderSplicer(
171189
position++
172190
}
173191
position = Math.max(position, end)
174-
return inString && !isJsonStringContent(value) ? JSON.stringify(value).slice(1, -1) : value
192+
if (inString) {
193+
return isJsonStringContent(value) ? value : JSON.stringify(value).slice(1, -1)
194+
}
195+
return isString && !isJsonValueText(value) ? JSON.stringify(value) : value
175196
}
176197
}
177198

@@ -1872,7 +1893,12 @@ export class VariableResolver {
18721893
}
18731894

18741895
const raw = this.blockResolver.formatValueForBlock(resolved, blockType, language)
1875-
const formatted = spliceReference(raw, index, index + match.length)
1896+
const formatted = spliceReference(
1897+
raw,
1898+
index,
1899+
index + match.length,
1900+
typeof resolved === 'string'
1901+
)
18761902
projectedReferenceResult += containsResolvedSecret ? match : formatted
18771903
return formatted
18781904
} catch (error) {
@@ -1891,7 +1917,7 @@ export class VariableResolver {
18911917
result = await replaceEnvVarsAsync(result, async (match, index) => {
18921918
const resolved = await this.resolveReference(match, resolutionContext)
18931919
if (typeof resolved !== 'string') return match
1894-
return spliceEnvVar(resolved, index, index + match.length)
1920+
return spliceEnvVar(resolved, index, index + match.length, true)
18951921
})
18961922
ctx.resolvedSecretTraceRegistry?.recordResolvedInputProjection(
18971923
options.inputPath,

‎apps/sim/tools/generated/tool-metadata.ts‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎apps/sim/tools/http/request.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,25 @@ export const requestTool: ToolConfig<RequestParams, RequestResponse> = {
7070
type: 'number',
7171
visibility: 'hidden',
7272
description:
73-
'Number of retry attempts after a timeout, a connection reset, a 429 or a 5xx response. A host that does not resolve or refuses the connection fails without retrying. Default: 0 (no retries).',
73+
'Times to resend the request (default: 0, max: 10), only after a timeout, a connection reset, a 429 or a 5xx response, and only for GET, HEAD, PUT and DELETE unless retryNonIdempotent is true. A host that does not resolve or refuses the connection, and any other 4xx, fail at once.',
7474
},
7575
retryDelayMs: {
7676
type: 'number',
7777
visibility: 'hidden',
78-
description: 'Initial retry delay in milliseconds (default: 500)',
78+
description:
79+
'Wait before the first retry in milliseconds (default: 500); it doubles each retry, with jitter, up to retryMaxDelayMs.',
7980
},
8081
retryMaxDelayMs: {
8182
type: 'number',
8283
visibility: 'hidden',
83-
description: 'Maximum delay between retries in milliseconds (default: 30000)',
84+
description:
85+
'Longest wait between retries in milliseconds (default: 30000). A Retry-After header asking for longer ends the retries.',
8486
},
8587
retryNonIdempotent: {
8688
type: 'boolean',
8789
visibility: 'hidden',
8890
description:
89-
'Allow retries for non-idempotent methods like POST/PATCH (may create duplicate requests).',
91+
'Also retry POST, PATCH and other non-idempotent methods (may create duplicate requests).',
9092
},
9193
redirectPolicyVersion: {
9294
type: 'string',

‎packages/workflow-sdk/test/fixtures/catalog.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6730,7 +6730,7 @@
67306730
"valueKind": "string",
67316731
"storage": "native",
67326732
"acceptsNumber": true,
6733-
"title": "Retries",
6733+
"title": "Request retries",
67346734
"mode": "advanced"
67356735
},
67366736
{
@@ -6739,7 +6739,7 @@
67396739
"valueKind": "string",
67406740
"storage": "native",
67416741
"acceptsNumber": true,
6742-
"title": "Retry delay (ms)",
6742+
"title": "Request retry delay (ms)",
67436743
"mode": "advanced"
67446744
},
67456745
{
@@ -6748,7 +6748,7 @@
67486748
"valueKind": "string",
67496749
"storage": "native",
67506750
"acceptsNumber": true,
6751-
"title": "Max retry delay (ms)",
6751+
"title": "Max request retry delay (ms)",
67526752
"mode": "advanced"
67536753
},
67546754
{

0 commit comments

Comments
 (0)