ts function or ts effect declares TypeScript that belongs to the procedure. A declaration is
not a step and never runs by itself. A call runs it once,
addressed as '@local.<id>'. A polling wait can run an
effect repeatedly.
- Use a function for deterministic computation over its input: no network, secrets, clock or randomness. Running it again always gives the same result.
- Use an effect for anything else: reading or writing an outside system, reading a secret, calling an Effective API operation, or reading the current time.
- Use a
valuenode instead when you only need to copy fields, fill a default or build a string.
call of @local.<id> needs exactly one matching declaration, even
when every test mocks it.
Syntax
Function
function id=pro-rata-refund input=refund-input result=refund
Effect
effect id=cancel-policy input=cancel-request result=cancellation uses=fetch,secrets secrets=pas-token retry=pas-write-retry timeout=1m
Fields
Attributes followid on the fence’s info line. Lists are comma-separated, with no spaces, empty
entries or duplicates.
Function attributes
Effect attributes
What the code can use
Both forms use the same TypeScript, with a fixed set of standard objects:
Members not listed, such as
String.prototype.repeat, are not available.
Not available in either form: imports of any kind, eval and other dynamic code, type assertions
(as, <Type>value and the non-null !), Intl, console, timers, the filesystem, processes
and environment variables. Write the code you need inside the declaration. Durable sleeps and
polling don’t belong in code either; use a wait.
A function also has no Date, no Math.random, no Promise and no services. It receives
only its input.
An effect also has Promise.resolve and then for async code, the ambient Date for
wall-clock time, and the context below. Date can differ between attempts; never use it, or
anything random, as an idempotency key.
Code can’t trust outside data. response.json() returns unknown, so check its shape at run time
before you return it, as the effect above does.
Effect context
Capabilities
uses= lists everything an effect may do beyond computing. A capability you don’t declare
doesn’t exist in the effect’s context, and the code fails to compile if it uses it.
fetch
uses=fetch grants requests to the public internet. It excludes private networks, loopback and
link-local addresses, cloud metadata services and other tenants’ networks. You can’t list allowed
hosts per effect.
- Each
fetchsends one request. It never retries on its own; retries come only from the retry policy. - It follows up to 20 redirects. A redirect to another origin fails if it would forward a header
or body you set; use
redirect: "manual"and send the next request yourself. - It doesn’t add an idempotency header. Pass
operation.idempotencyKeyyourself in the header or field the receiving system uses. - It never reaches the Effective API. A relative URL, or a URL on the Effective API’s own origin,
is refused. Use the
effectiveclient instead.
secrets
uses=secrets with secrets=<slot>,... grants the listed slots. Each slot needs a
yaml secret declaration, and an organization binds the
actual value outside the procedure.
- Read a slot with
secrets.getText('<slot>'),secrets.getJson('<slot>')orsecrets.getBytes('<slot>'). The method must match the slot’s declaredformat. - The slot name must be a string literal. Computed names are invalid.
- The effect must read every slot it lists, and list every slot it reads.
uses=fetch,secrets lets your code send secret values to any public host. Review effects that
combine the two with care.
effective:<scope>
uses=effective:<scope> grants Effective API operations through the effective client.
Effective authenticates the call for the run; you don’t store an Effective API token.
- Name one operation exactly, such as
uses=effective:api.email.send, or every read in a group, such asuses=effective:api.chat:read. Separate several with commas. - Name each write individually. Group selectors for writes are rejected, so every write is visible in the source and when the procedure is reviewed.
- One effect may hold at most 64 operations, so very broad read selectors are too large.
- The operation list is fixed when the procedure is published. A later API addition never widens an existing version; republish to use it.
- The call runs with the run’s identity, and the API still applies its own permission checks.
- On writes, Effective sends
Idempotency-Key: operation.idempotencyKeyfor you. Where the operation deduplicates on that key, retries are safe.
'@effective.api.<operation>' in a
call. The effect form is what lets you handle a refusal.
effect id=email-refund-notice input=email-input result=email-receipt uses=effective:api.email.send
Branch on a refused Effective API call
A refusedeffective.api.* call throws an error whose message doesn’t say why. Inside the effect,
effective.apiError(error) tells you:
- It returns
{ status, code, body }when the API answered with a4xx. The call was refused and nothing was applied.bodyis the parsed JSON error body, ornullif it wasn’t a JSON object or was larger than 16 KiB. - It returns
nullfor a5xx, no answer, or any other error. Those may have been applied, so rethrow them and let the retry policy apply.
choice or repeat.
Throw FatalError for any other refusal, so the run stops instead of retrying a call the API
already refused:
effect id=email-refund-notice input=email-input result=email-receipt uses=effective:api.email.send
email-receipt declares outcome as enum: [sent, rejected]. Which status codes an
operation returns for which refusals depends on the operation.
effective.apiError exists only in effects with an effective: capability. A YAML call of an
@effective.api.* operation has no failure routing: a refusal there stops the run with the API’s
reason.
Example
A function works out the pro-rata return premium, then an effect emails the insured:return-premium.procedure.md
The
return-premium email template is illustrative. Use a template your organization has set up.On retry and resume
- Validated at both ends. The input is validated before the code runs, and the result is
validated against
resultbefore it’s recorded. - Three attempts by default. Functions and effects get three attempts: one second before the
first retry, doubling up to ten seconds. A
retrypolicy replaces this. - Effect errors retry; function errors don’t. An uncaught effect error is retried. Throw
FatalErrorto stop at once. A function error is final, because the function would give the same answer again; functions retry only after a timeout or a lost attempt. - Thrown messages aren’t shown. A message your code throws,
FatalErroror any other, could hold a secret value, so Effective doesn’t show it. The run’sfailurenames the step and a code such aslocal-code-failed, not your message. Return a refusal you need to see as a typed result instead, as in Branch on a refused Effective API call, and log the detail in your own system. - A finished call doesn’t run again. When a run resumes, a completed call’s recorded result is used, and the code isn’t called again.
- Effects are not exactly once. A remote write can commit and its answer still be lost, so a
retry can send it again. Send
operation.idempotencyKey, use a natural business key, or select a one-attempt policy. Omittingretry=keeps the three-attempt default. - One write per effect. An effect may do supporting reads and one business write. Put writes that must recover independently in separate effects and separate calls, so each has its own result and identity.
- Polling stops on doubt. In a polling
wait, the next poll starts only after the previous one finished. If a poll attempt’s outcome is unknown, the wait fails for a person to check instead of polling again. - Pinned with the version. Publishing seals the code, its schemas and its capabilities into the version. Changing any of them needs a new version.