{"version":3,"file":"workflow.mjs","names":["WorkflowBindingError","message","fields","descriptorBounds","workflowExecution","value","field","descriptor","canonicalConvexBytes","workflowBinding","compatibleVersions","binding","version","snapshot","runWorkflowStep","runner","step","execution","input","versions","nativeOptions","options","output","workflowDecode","decoded","decodeContract","schema","workflowAttempt","authority","configuration","receiptWorkflowMutation","adapter","preparation","workflowReceiptInvocation","captureIdempotencyInvocation","terminalWorkflowAttempt","error","terminal","safeFailure","workflowTransition","generation","current","update","nativeStatus","outcome","status","state","workflowCancel","expectedGeneration","workflowRestart","workflowStatusBindings","apply","transition"],"sources":["../../../src/modules/contracts/workflow.ts"],"sourcesContent":["import type { FunctionReference, FunctionVisibility, FunctionType } from 'convex/server'\nimport type { WorkflowCtx } from '@convex-dev/workflow'\nimport {\n\tdecodeContract,\n\ttype ContractInput,\n\ttype ContractOutput,\n\ttype NeutralSchema,\n} from './contract'\nimport {\n\tcanonicalConvexBytes,\n\tcaptureIdempotencyInvocation,\n\ttype CanonicalConvexBounds,\n\ttype IdempotencyInvocation,\n\ttype IdempotencyPreparation,\n} from './idempotency'\n\nexport class WorkflowBindingError extends Error {\n\treadonly _tag = 'WorkflowBindingError'\n\tconstructor(message: string) {\n\t\tsuper(message)\n\t\tthis.name = 'WorkflowBindingError'\n\t}\n}\nexport interface WorkflowExecution<Args = unknown> {\n\treadonly version: 1\n\treadonly operation: string\n\treadonly operationVersion: string\n\treadonly contractVersion: string\n\treadonly bindingVersion: string\n\t/** Host-issued business-run identity. Recovery preserves it. */\n\treadonly run: string\n\t/** Opaque host capability; never caller-supplied principal or tenant authority. */\n\treadonly capability: string\n\t/** Logical occurrence, distinct for each repeated loop step. */\n\treadonly occurrence: string\n\treadonly args: Args\n}\nexport type WorkflowStepArguments<Args> = { execution: WorkflowExecution<Args> }\nconst fields = [\n\t'version',\n\t'operation',\n\t'operationVersion',\n\t'contractVersion',\n\t'bindingVersion',\n\t'run',\n\t'capability',\n\t'occurrence',\n\t'args',\n] as const\nconst descriptorBounds = {\n\tmaxDepth: 64,\n\tmaxNodes: 100000,\n\tmaxBytes: 1000000,\n\tmaxArrayLength: 100000,\n\tmaxObjectFields: 100000,\n}\n/** Strict plain, versioned transport. Validity does not confer authority; the host resolves capability anew. */\n/* oxlint-disable anti-slop/no-unknown-parameters, anti-slop/no-runtime-typeof -- This decoder validates an untrusted execution descriptor before it enters the native workflow boundary. */\nexport function workflowExecution<Args>(value: WorkflowExecution<Args>): WorkflowExecution<Args>\nexport function workflowExecution(value: unknown): WorkflowExecution\nexport function workflowExecution(value: unknown): WorkflowExecution {\n\tif (\n\t\t!value ||\n\t\ttypeof value !== 'object' ||\n\t\tObject.getPrototypeOf(value) !== Object.prototype ||\n\t\tReflect.ownKeys(value).length !== fields.length ||\n\t\tfields.some((field) => !Object.hasOwn(value, field))\n\t)\n\t\tthrow new WorkflowBindingError('Execution requires exact plain descriptor fields')\n\t// SAFETY: the exact field set is checked above; values and accessors are checked before use below.\n\tconst descriptor = value as WorkflowExecution\n\tfor (const field of fields)\n\t\tif (!('value' in Object.getOwnPropertyDescriptor(value, field)!))\n\t\t\tthrow new WorkflowBindingError('Execution cannot contain accessors')\n\tif (\n\t\tdescriptor.version !== 1 ||\n\t\tfields\n\t\t\t.filter((field) => field !== 'version' && field !== 'args')\n\t\t\t.some((field) => typeof descriptor[field] !== 'string' || descriptor[field].length === 0)\n\t)\n\t\tthrow new WorkflowBindingError('Invalid execution descriptor version or identity')\n\ttry {\n\t\tcanonicalConvexBytes(descriptor.args, descriptorBounds)\n\t} catch {\n\t\tthrow new WorkflowBindingError('Execution args require plain Convex data')\n\t}\n\treturn descriptor\n}\n/* oxlint-enable anti-slop/no-unknown-parameters, anti-slop/no-runtime-typeof */\nexport interface WorkflowBinding<\n\tKind extends FunctionType,\n\tArgs extends NeutralSchema,\n\tResult extends NeutralSchema,\n> {\n\treadonly kind: Kind\n\treadonly operation: string\n\treadonly operationVersion: string\n\treadonly contractVersion: string\n\treadonly bindingVersion: string\n\treadonly args: Args\n\treadonly result: Result\n\treadonly reference: FunctionReference<\n\t\tNoInfer<Kind>,\n\t\tFunctionVisibility,\n\t\tWorkflowStepArguments<NoInfer<ContractInput<Args>>>,\n\t\tNoInfer<ContractInput<Result>>\n\t>\n\t/** Host explicitly lists deployed descriptor versions whose semantics remain compatible. */\n\treadonly compatibleVersions?: readonly {\n\t\toperationVersion: string\n\t\tcontractVersion: string\n\t\tbindingVersion: string\n\t}[]\n}\n/** Both phantom reference args and results must match the explicit wire contracts. */\nexport function workflowBinding<\n\tKind extends FunctionType,\n\tconst Args extends NeutralSchema,\n\tconst Result extends NeutralSchema,\n>(binding: WorkflowBinding<Kind, Args, Result>): WorkflowBinding<Kind, Args, Result> {\n\tconst compatibleVersions = binding.compatibleVersions?.map((version) =>\n\t\tObject.freeze({ ...version }),\n\t)\n\tconst snapshot = { ...binding }\n\tif (compatibleVersions !== undefined)\n\t\tsnapshot.compatibleVersions = Object.freeze(compatibleVersions)\n\treturn Object.freeze(snapshot)\n}\nexport type WorkflowStepOptions = {\n\tname?: string\n\tretry?: NonNullable<Parameters<WorkflowCtx['withOptions']>[0]['retry']>\n} & (\n\t| { inline?: false; runAt?: number; runAfter?: number }\n\t| { inline: true; runAt?: never; runAfter?: never }\n)\n/** A structural seam allows invocation-scoped native WorkflowCtx without creating another durability runtime. */\nexport interface WorkflowStepRunner {\n\t/* oxlint-disable anti-slop/no-unknown-returns -- Native runner results stay opaque here; runWorkflowStep correlates them with the binding's raw result contract without replay decoding. */\n\trunQuery?: (\n\t\treference: FunctionReference<'query', FunctionVisibility>,\n\t\targs: WorkflowStepArguments<unknown>,\n\t\toptions?: WorkflowStepOptions,\n\t) => Promise<unknown>\n\trunMutation?: (\n\t\treference: FunctionReference<'mutation', FunctionVisibility>,\n\t\targs: WorkflowStepArguments<unknown>,\n\t\toptions?: WorkflowStepOptions,\n\t) => Promise<unknown>\n\trunAction?: (\n\t\treference: FunctionReference<'action', FunctionVisibility>,\n\t\targs: WorkflowStepArguments<unknown>,\n\t\toptions?: WorkflowStepOptions,\n\t) => Promise<unknown>\n\t/* oxlint-enable anti-slop/no-unknown-returns */\n}\nexport async function runWorkflowStep<\n\tKind extends FunctionType,\n\tArgs extends NeutralSchema,\n\tResult extends NeutralSchema,\n>(\n\tstep: WorkflowCtx | WorkflowStepRunner,\n\tbinding: WorkflowBinding<Kind, Args, Result>,\n\tinput: WorkflowExecution<ContractInput<Args>>,\n\toptions: WorkflowStepOptions = {},\n): Promise<ContractInput<Result>> {\n\t// SAFETY: each binding has nonempty, checked generated args. Native ArgsAndOptions' empty-args overload is never selected.\n\tconst runner = step as WorkflowStepRunner\n\tconst execution = workflowExecution(input)\n\tconst versions = [binding, ...(binding.compatibleVersions ?? [])]\n\tif (\n\t\texecution.operation !== binding.operation ||\n\t\t!versions.some(\n\t\t\t(version) =>\n\t\t\t\tversion.operationVersion === execution.operationVersion &&\n\t\t\t\tversion.contractVersion === execution.contractVersion &&\n\t\t\t\tversion.bindingVersion === execution.bindingVersion,\n\t\t)\n\t)\n\t\tthrow new WorkflowBindingError('Incompatible workflow operation version')\n\tconst nativeOptions = {\n\t\t...options,\n\t\tname: options.name ?? `${execution.operation}:${execution.occurrence}`,\n\t}\n\tlet output: unknown\n\tswitch (binding.kind) {\n\t\tcase 'query':\n\t\t\tif (!runner.runQuery) throw new WorkflowBindingError('Native query runner missing')\n\t\t\t// SAFETY: the checked binding kind selects this exact native reference kind.\n\t\t\toutput = await runner.runQuery(\n\t\t\t\tbinding.reference as FunctionReference<'query', FunctionVisibility>,\n\t\t\t\t{ execution },\n\t\t\t\tnativeOptions,\n\t\t\t)\n\t\t\tbreak\n\t\tcase 'mutation':\n\t\t\tif (!runner.runMutation) throw new WorkflowBindingError('Native mutation runner missing')\n\t\t\t// SAFETY: the checked binding kind selects this exact native reference kind.\n\t\t\toutput = await runner.runMutation(\n\t\t\t\tbinding.reference as FunctionReference<'mutation', FunctionVisibility>,\n\t\t\t\t{ execution },\n\t\t\t\tnativeOptions,\n\t\t\t)\n\t\t\tbreak\n\t\tcase 'action':\n\t\t\tif (!runner.runAction) throw new WorkflowBindingError('Native action runner missing')\n\t\t\tif (options.inline) throw new WorkflowBindingError('Actions cannot execute inline')\n\t\t\t// SAFETY: the checked binding kind selects this exact native reference kind.\n\t\t\toutput = await runner.runAction(\n\t\t\t\tbinding.reference as FunctionReference<'action', FunctionVisibility>,\n\t\t\t\t{ execution },\n\t\t\t\t{ ...nativeOptions, retry: options.retry ?? false },\n\t\t\t)\n\t\t\tbreak\n\t}\n\t// Native validators own wire validation. Replaying workflow code must not rerun transforms.\n\t// SAFETY: the binding correlates the generated reference's result with this raw wire contract.\n\treturn output as ContractInput<Result>\n}\nexport async function workflowDecode<S extends NeutralSchema>(\n\tschema: S,\n\t// oxlint-disable-next-line anti-slop/no-unknown-parameters -- The schema parses retained or native transport values at this boundary.\n\tvalue: unknown,\n): Promise<ContractOutput<S>> {\n\tconst decoded = await decodeContract(schema, value)\n\tif (decoded.issues) throw new WorkflowBindingError('Workflow contract validation failed')\n\treturn decoded.value\n}\ntype Ordinary<A> = A | PromiseLike<A>\n/** This helper belongs inside a registered native function, never in the replaying workflow handler. */\nexport function workflowAttempt<Args, Authority, Result>(configuration: {\n\tauthorize: (execution: WorkflowExecution<Args>) => Ordinary<Authority>\n\texecute: (execution: WorkflowExecution<Args>, authority: Authority) => Ordinary<Result>\n}) {\n\treturn async (input: WorkflowExecution<Args>): Promise<Result> => {\n\t\tconst execution = workflowExecution(input)\n\t\tconst authority = await configuration.authorize(execution)\n\t\treturn configuration.execute(execution, authority)\n\t}\n}\nexport type WorkflowOutcome<Result = unknown, Failure = unknown> =\n\t| { readonly version: 1; readonly kind: 'succeeded'; readonly value: Result }\n\t| { readonly version: 1; readonly kind: 'failed'; readonly error: Failure }\ndeclare const protectedMutation: unique symbol\nexport interface ReceiptProtectedWorkflowMutation<Args, Result> {\n\treadonly [protectedMutation]: { args: Args; result: Result }\n\treadonly handler: (execution: WorkflowExecution<Args>) => Promise<Result>\n}\nexport interface WorkflowReceiptAdapter<S extends NeutralSchema> {\n\treadonly replayResult: S\n\treadonly prepare: (\n\t\tinvocation: IdempotencyInvocation,\n\t) => Promise<IdempotencyPreparation<ContractOutput<S>>>\n}\n/** Host registers this handler as the inner native mutation. All callbacks share its transaction. */\nexport function receiptWorkflowMutation<\n\tArgs,\n\tAuthority,\n\tconst Replay extends NeutralSchema,\n>(configuration: {\n\tauthorize: (execution: WorkflowExecution<Args>) => Ordinary<Authority>\n\treceipt: (authority: Authority) => WorkflowReceiptAdapter<Replay>\n\tinvocation: (execution: WorkflowExecution<Args>, authority: Authority) => IdempotencyInvocation\n\texecute: (\n\t\texecution: WorkflowExecution<Args>,\n\t\tauthority: Authority,\n\t) => Ordinary<ContractOutput<Replay>>\n}): ReceiptProtectedWorkflowMutation<Args, ContractOutput<Replay>> {\n\tconst handler = workflowAttempt({\n\t\tauthorize: configuration.authorize,\n\t\texecute: async (execution: WorkflowExecution<Args>, authority: Authority) => {\n\t\t\tconst adapter = configuration.receipt(authority)\n\t\t\tconst preparation = await adapter.prepare(configuration.invocation(execution, authority))\n\t\t\tif (preparation.kind === 'replay')\n\t\t\t\treturn workflowDecode(adapter.replayResult, preparation.result)\n\t\t\tconst value = await configuration.execute(execution, authority)\n\t\t\tawait preparation.complete(value)\n\t\t\treturn value\n\t\t},\n\t})\n\t// SAFETY: the private type evidence can only be issued by this receipt-protected helper.\n\treturn { handler } as ReceiptProtectedWorkflowMutation<Args, ContractOutput<Replay>>\n}\nexport function workflowReceiptInvocation(\n\texecution: WorkflowExecution,\n\toptions: { scope: string; fingerprintPolicy: string; bounds: CanonicalConvexBounds },\n): IdempotencyInvocation {\n\tconst descriptor = workflowExecution(execution)\n\treturn captureIdempotencyInvocation({\n\t\tbinding: { kind: 'mutation', atomicity: 'same-mutation' },\n\t\tidentity: {\n\t\t\toperation: descriptor.operation,\n\t\t\tscope: options.scope,\n\t\t\tkey: JSON.stringify([descriptor.run, descriptor.occurrence]),\n\t\t},\n\t\tversions: {\n\t\t\toperation: descriptor.operationVersion,\n\t\t\tcontract: descriptor.contractVersion,\n\t\t\tbinding: descriptor.bindingVersion,\n\t\t\tfingerprintPolicy: options.fingerprintPolicy,\n\t\t},\n\t\trawInput: descriptor.args,\n\t\tcanonical: { bounds: options.bounds },\n\t})\n}\ntype TerminalMode<Args> =\n\t| {\n\t\t\tkind: 'mutation'\n\t\t\t/** Assertion supplied only after host native rollback/journaling acceptance. */ atomicJournalProof: 'host-verified'\n\t  }\n\t| { kind: 'action'; protectedMutation: ReceiptProtectedWorkflowMutation<Args, unknown> }\n/** The native inner mutation rejection must cross its transaction boundary before terminal mapping. */\nexport function terminalWorkflowAttempt<\n\tArgs,\n\tAuthority,\n\tconst Result extends NeutralSchema,\n\tconst Failure extends NeutralSchema,\n>(\n\tconfiguration: TerminalMode<Args> & {\n\t\tauthorize: (execution: WorkflowExecution<Args>) => Ordinary<Authority>\n\t\tinvokeMutation: (\n\t\t\texecution: WorkflowExecution<Args>,\n\t\t\tauthority: Authority,\n\t\t) => Promise<ContractInput<Result>>\n\t\t// oxlint-disable-next-line anti-slop/no-unknown-parameters -- Native thrown values are classified by the host before validating the terminal failure contract.\n\t\tterminal: (error: unknown) => Ordinary<ContractInput<Failure> | undefined>\n\t\tresult: Result\n\t\tfailure: Failure\n\t},\n) {\n\treturn workflowAttempt({\n\t\tauthorize: configuration.authorize,\n\t\texecute: async (\n\t\t\texecution: WorkflowExecution<Args>,\n\t\t\tauthority: Authority,\n\t\t): Promise<WorkflowOutcome<ContractInput<Result>, ContractOutput<Failure>>> => {\n\t\t\tlet value: ContractInput<Result>\n\t\t\ttry {\n\t\t\t\tvalue = await configuration.invokeMutation(execution, authority)\n\t\t\t} catch (error) {\n\t\t\t\tconst terminal = await configuration.terminal(error)\n\t\t\t\tif (terminal === undefined) throw error\n\t\t\t\tconst safeFailure = await workflowDecode(configuration.failure, terminal)\n\t\t\t\tcanonicalConvexBytes(safeFailure, descriptorBounds)\n\t\t\t\treturn { version: 1, kind: 'failed', error: safeFailure }\n\t\t\t}\n\t\t\tawait workflowDecode(configuration.result, value)\n\t\t\tcanonicalConvexBytes(value, descriptorBounds)\n\t\t\treturn { version: 1, kind: 'succeeded', value }\n\t\t},\n\t})\n}\nexport type WorkflowNativeStatus = 'running' | 'succeeded' | 'failed' | 'canceled'\nexport interface WorkflowHostState<Result = unknown, Failure = unknown> {\n\treadonly generation: number\n\treadonly status: WorkflowNativeStatus\n\treadonly nativeStatus: WorkflowNativeStatus\n\treadonly outcome?: WorkflowOutcome<Result, Failure>\n}\nexport interface WorkflowHostUpdate<Result = unknown, Failure = unknown> {\n\treadonly generation: number\n\treadonly nativeStatus: WorkflowNativeStatus\n\treadonly outcome?: WorkflowOutcome<Result, Failure>\n}\nexport type WorkflowTransition<Result = unknown, Failure = unknown> = {\n\treadonly kind: 'applied' | 'ignored'\n\treadonly state: WorkflowHostState<Result, Failure>\n}\n/** Call within a host transaction or compare-and-set. A late operation failure refines host success; cancellation and terminal native status stay fenced. */\nexport function workflowTransition<Result, Failure>(\n\tcurrent: WorkflowHostState<Result, Failure>,\n\tupdate: WorkflowHostUpdate<Result, Failure>,\n): WorkflowTransition<Result, Failure> {\n\tgeneration(current.generation)\n\tgeneration(update.generation)\n\tif (current.generation !== update.generation) return { kind: 'ignored', state: current }\n\tconst nativeStatus =\n\t\tcurrent.nativeStatus !== 'running' ? current.nativeStatus : update.nativeStatus\n\tconst outcome = current.outcome ?? update.outcome\n\tconst status =\n\t\t(current.status === 'running' || current.status === 'succeeded') &&\n\t\tnativeStatus === 'succeeded' &&\n\t\toutcome?.kind === 'failed'\n\t\t\t? 'failed'\n\t\t\t: current.status !== 'running'\n\t\t\t\t? current.status\n\t\t\t\t: nativeStatus\n\tconst state = { generation: current.generation, status, nativeStatus }\n\treturn { kind: 'applied', state: outcome === undefined ? state : { ...state, outcome } }\n}\nexport function workflowCancel<Result, Failure>(\n\tcurrent: WorkflowHostState<Result, Failure>,\n\texpectedGeneration: number,\n): WorkflowTransition<Result, Failure> {\n\tgeneration(expectedGeneration)\n\tif (current.generation !== expectedGeneration) return { kind: 'ignored', state: current }\n\treturn {\n\t\tkind: 'applied',\n\t\tstate: { ...current, status: current.status === 'running' ? 'canceled' : current.status },\n\t}\n}\n/** Host calls after an authorized native restart. Receipt identity remains run plus occurrence. */\nexport function workflowRestart<Result, Failure>(\n\tcurrent: WorkflowHostState<Result, Failure>,\n): WorkflowHostState<Result, Failure> {\n\tgeneration(current.generation)\n\tgeneration(current.generation + 1)\n\tconst state = {\n\t\tgeneration: current.generation + 1,\n\t\tstatus: 'running' as const,\n\t\tnativeStatus: 'running' as const,\n\t}\n\treturn current.outcome?.kind === 'succeeded' ? { ...state, outcome: current.outcome } : state\n}\n/** Host closures capture the trusted run, secured storage and manager. `write` must enforce expected generation atomically. */\nexport function workflowStatusBindings<Result, Failure>(configuration: {\n\tauthorize: () => Ordinary<void>\n\tread: () => Ordinary<WorkflowHostState<Result, Failure>>\n\twrite: (expectedGeneration: number, state: WorkflowHostState<Result, Failure>) => Ordinary<void>\n\tnativeStatus: () => Ordinary<WorkflowNativeStatus>\n\treceiptOutcome: () => Ordinary<WorkflowOutcome<Result, Failure> | undefined>\n\t/** Host calls the manager in the same transaction as its guarded state write. */\n\tcancelNative: () => Ordinary<void>\n\trestartNative: () => Ordinary<void>\n}) {\n\tasync function apply(\n\t\texpectedGeneration: number,\n\t\tnativeStatus: WorkflowNativeStatus,\n\t\toutcome?: WorkflowOutcome<Result, Failure>,\n\t) {\n\t\tconst update = { generation: expectedGeneration, nativeStatus }\n\t\tconst transition = workflowTransition(\n\t\t\tawait configuration.read(),\n\t\t\toutcome === undefined ? update : { ...update, outcome },\n\t\t)\n\t\tif (transition.kind === 'applied')\n\t\t\tawait configuration.write(expectedGeneration, transition.state)\n\t\treturn transition\n\t}\n\treturn {\n\t\tasync callback(\n\t\t\texpectedGeneration: number,\n\t\t\tnativeStatus: WorkflowNativeStatus,\n\t\t\toutcome?: WorkflowOutcome<Result, Failure>,\n\t\t) {\n\t\t\tawait configuration.authorize()\n\t\t\treturn apply(expectedGeneration, nativeStatus, outcome)\n\t\t},\n\t\tasync reconcile(expectedGeneration: number) {\n\t\t\tawait configuration.authorize()\n\t\t\treturn apply(\n\t\t\t\texpectedGeneration,\n\t\t\t\tawait configuration.nativeStatus(),\n\t\t\t\tawait configuration.receiptOutcome(),\n\t\t\t)\n\t\t},\n\t\tasync cancel(expectedGeneration: number) {\n\t\t\tawait configuration.authorize()\n\t\t\tconst current = await configuration.read()\n\t\t\tconst transition = workflowCancel(current, expectedGeneration)\n\t\t\tif (transition.kind === 'applied' && current.status === 'running') {\n\t\t\t\tawait configuration.cancelNative()\n\t\t\t\tawait configuration.write(expectedGeneration, transition.state)\n\t\t\t}\n\t\t\treturn transition\n\t\t},\n\t\tasync restart(expectedGeneration: number) {\n\t\t\tawait configuration.authorize()\n\t\t\tconst current = await configuration.read()\n\t\t\tif (current.generation !== expectedGeneration)\n\t\t\t\treturn { kind: 'ignored' as const, state: current }\n\t\t\tconst state = workflowRestart(current)\n\t\t\tawait configuration.restartNative()\n\t\t\tawait configuration.write(expectedGeneration, state)\n\t\t\treturn { kind: 'applied' as const, state }\n\t\t},\n\t}\n}\nfunction generation(value: number): void {\n\tif (!Number.isSafeInteger(value) || value < 0)\n\t\tthrow new WorkflowBindingError('Invalid host execution generation')\n}\n"],"mappings":"2IAgBA,IAAaA,EAAb,cAA0C,KAAM,CAC/C,KAAgB,uBAChB,YAAY,EAAiB,CAC5B,MAAMC,CAAO,EACb,KAAK,KAAO,sBACb,CACD,EAgBA,MAAMC,EAAS,CACd,UACA,YACA,mBACA,kBACA,iBACA,MACA,aACA,aACA,MACD,EACMC,EAAmB,CACxB,SAAU,GACV,SAAU,IACV,SAAU,IACV,eAAgB,IAChB,gBAAiB,GAClB,EAKA,SAAgBC,EAAkB,EAAmC,CACpE,GACC,CAACC,GACD,OAAOA,GAAU,UACjB,OAAO,eAAeA,CAAK,IAAM,OAAO,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