Monetizing TeaQL in Public — Day 7: Remembering a Cancellation After the Process Is Gone
A cancellation request times out. The customer asks what happened. The process that sent it has restarted, and the next provider read shows no scheduled change. Can we safely send it again?
A blank screen and a negative read are not enough to answer that question. We need a durable record of the decision we accepted, what we attempted, and what we actually know about the result.
On Day 6, we built a cancellation screen that can show uncertainty. Today we add the application model behind that memory: a cancellation-command record that survives a fresh runtime and can be read by an independent Node process.
This is local fixture verification, not a real Paddle Sandbox run. We use synthetic accounts, real IAM sessions, actual SQLite, and an injected provider. No real subscription was canceled or refunded. Real Sandbox cancellation and public HTTPS delivery remain pending.
Start with the support question
Sam is Acme's Finance member. Sam confirms the date on the cancellation screen. The service records the intent, sends the request, and loses the response after the simulated provider accepts it.
Finance can now query a local history endpoint and see UNCERTAIN. That query
does not require the provider to be online. When a later provider read shows the
matching cancellation date, the record becomes OBSERVED.
It remains the same command with the same original requesting subject. We did not send another provider cancellation. Plus access remains valid until its recorded paid-through date.
This also makes a different failure visible: an intent can be committed before a provider call ever happens. After an interruption, the record says that an intent exists; it does not pretend to know that it was dispatched.
Give the command its own domain record
We extended the Billing model with two
objects: cancellation_command and the finite command_state vocabulary. The
model preserves the existing Organization, subscription, entitlement, and
payment-receipt relationships.
A command stores its server-generated reference, the original IAM subject, the subscription relationship, the confirmed period end, request time, state, and an allowlisted result code. An operation key identifies the saved provider subscription plus the canonical period-end instant.
These are our application-owned billing concepts, not new built-in TeaQL payment APIs. TeaQL evaluates the model and generates the TypeScript library; our service owns the business transitions.
The current model passed semantic evaluation before regeneration. We compiled the generated library and application against the selected published runtime. Generated source was replaced from the service output, not edited by hand.
Distinguish an answer from an observation
The state vocabulary keeps different kinds of knowledge separate:
| State | What the local record establishes |
|---|---|
PREPARED | The authorized intent was committed before external I/O; dispatch is not established |
CONFIRMED | The cancellation API returned a matching schedule |
UNCERTAIN | The result is still not established |
REJECTED | The provider returned a definitive rejection |
OBSERVED | A later provider read showed the matching requested date |
OBSERVED is deliberately different from CONFIRMED. A later read proves
what the provider currently reports. It does not prove which request caused
that state.
A canceled subscription without a matching date is also insufficient to label the original command confirmed. We retain that uncertainty instead of filling in a result we did not observe.
Commit locally before crossing the network
The service first authorizes the Organization operation and verifies the saved subscription, customer, price, and displayed date. Then it saves the intent in a local transaction:
row.updateCommandStateToPrepared();
row.updateActorSubject(binding.actorSubject);
row.updateExpectedPeriodEnd(new Date(expected).toISOString());
row.updateRequestedAt(new Date(this.now()).toISOString());
row.updateResultCode('AWAITING_PROVIDER');
await row
.auditAs('Record authorized cancellation intent before provider I/O')
.save(context);
Only after that transaction commits does the external request begin. A later
transaction records the response or uncertainty. If the result write fails,
the earlier PREPARED record remains available for recovery.
This is not a transaction spanning SQLite and Paddle. There is a real interval between committing an intent, sending a request, and saving a response. The model describes that interval rather than hiding it behind a success flag.
A database constraint backs the operation key for unresolved or completed intents. Recreating a service object does not erase it. The in-memory guard from Day 5 remains useful for overlapping clicks, but it is no longer the only memory of an accepted intent.
A negative read is not permission to repeat the command
Suppose an uncertain request is followed by a provider read with no matching scheduled change. That could mean the request never arrived. It could also mean that the result is delayed, the state changed again, or we do not have enough information to correlate it.
The record stays UNCERTAIN, with NOT_CONFIRMED_BY_READBACK. A later POST for
the same unresolved operation returns a reconciliation-required result instead
of calling the provider again.
A definitive provider rejection is different. It closes that attempt as
REJECTED; a later explicit user confirmation may create a separate attempt.
Neither path automatically resends a command.
This conservative slice needs an operator-resolution workflow before it can handle every stuck intent conveniently. We have not implemented that workflow or a retry worker yet. Keeping an unresolved record visible is more useful than silently discarding it and pretending the next click is a clean first attempt.
Read the history within one Organization
An authorized Owner or Finance member can read:
GET /billing/v1/organizations/{organizationId}/cancellation-commands
The IAM-side path forwards to Billing, which independently checks
billing.transactions.read. The endpoint is GET-only and returns the latest
twenty records. It does not fetch provider state.
Inside the service, the query declares both its Organization scope and why it is executing:
const rows = await Q.cancellationCommands()
.filterByOrganizationAccount(binding.accountId)
.orderByIdDescending()
.limit(20)
.comment('Read scoped cancellation history')
.purpose('Let authorized Finance inspect the latest twenty durable command outcomes')
.executeForList(this.runtime.context(binding.actorSubject));
Complete E expressions read the modeled values:
const stateId = E.cancellationCommand(row).commandStateId().eval();
const expectedPeriodEnd = E.cancellationCommand(row).expectedPeriodEnd().eval();
const resultCode = E.cancellationCommand(row).resultCode().eval();
An update loads the complete scalar row and its original version before saving. It does not reconstruct a mutable entity from an ID supplied by a browser. Every write has an audit reason and executes with a trusted backend context.
The original requesting subject is preserved when another Finance member checks the result. A readback audit identifies the current reader through its execution context; it does not rewrite who requested the command.
Developer access does not grant access to this financial history. A Finance role in one Organization does not authorize another Organization's records. The response excludes provider customer/price identifiers, raw response bodies, cookies, API keys, and raw exception messages.
History here means the latest stored state of each command. It is not an append-only event log of every transition. Audited saves emit runtime audit events to the configured sink; this slice does not configure a durable sink for that separate event stream.
Follow the observed local result
Our real loopback HTTP scenario used IAM sessions and the same-origin proxy. It produced this sequence:
| Step | Provider-facing result | Durable command record | Paid access |
|---|---|---|---|
| Confirm the date | Simulated provider accepts, response is lost | UNCERTAIN | Plus |
| Read local history | No provider request needed | Same command, UNCERTAIN | Plus |
| Query provider status | Matching date is observed | Same command, OBSERVED | Plus |
| Read history again | No provider request needed | One record, original requester preserved | Plus |
The simulated provider received one cancellation command. Finance/content separation and cross-Organization denials were also exercised through those actual HTTP adapters. The verification excerpt contains the sanitized local responses and exact native test outcomes.
Check upgrade and recovery separately
We retained a small synthetic database from the prior schema, containing its confirmed subscription and paid entitlement. Initializing the new runtime adds the command tables. The existing Plus plan and expiry remain unchanged, and a new cancellation command can be recorded afterward. The source fixture stays byte-identical.
Fault tests also interrupt execution after the intent commit and after provider acceptance, and roll back failures during preparation, finalization, and readback. A fresh runtime opens an independent database snapshot. Another test starts a separate Node process and reads the saved command with no shared JavaScript objects.
These are fault-injection, snapshot/reopen, and process-isolation tests. They do not certify production crash recovery, simultaneous SQLite writers, or cross-system exactly-once execution.
Model regeneration needs to include the emitted runtime adapters and metadata. Our initial integration omitted an adapter update: TypeScript compiled, but bootstrap could not register the new entity. Runtime testing caught it. We replaced the missing emitted adapter bytes from the generator and reran the suite; no generated code was hand-patched.
What another SaaS builder can reuse
Model the command separately from the subscription and its current access. Persist the accepted intent before network I/O. Keep a direct provider answer distinct from a later observation. Scope financial history to the authorized Organization. Preserve uncertainty across service instances instead of turning a negative read into an automatic retry.
The existing cancellation UI continues to work. This entry adds the durable backend record and a read-only API; it does not add a command-history screen.
Real Paddle Sandbox/HTTPS verification, cancellation webhooks, an operator resolution flow, a worker, refunds, and re-subscribe remain pending. History and reconciliation are bounded to twenty records; pagination and recovery of older unresolved records remain separate work. Paid entitlements are still changed only by their existing payment and expiry rules.
