Automations
Automations
An automation is a named job: a nightly check, an import, a report, or an operation a person starts from a button. It runs as the workspace's system identity, with exactly the policies it declares, whoever started it.
// src/automation/+daily_project_digest.automation.ts (a sketch)
import { automation } from '@norbital-ai/bolt';
export default automation({
description: 'Every morning at 06:00 Singapore time, summarize open critical/high issues and pending claims.',
on: [{ cron: '0 6 * * *', tz: 'Asia/Singapore' }],
policies: ['construction_automation'],
run: function* (_, ctx) {
const { issues, payment_claims } = yield* ctx.query({
issues: {
where: { status: { in: ['open', 'in_progress'] }, severity: { in: ['critical', 'high'] } },
aggregate: { count: true }
},
payment_claims: {
where: { status: { in: ['draft', 'submitted'] } },
aggregate: { count: true }
}
});
return {
generated_at: ctx.now,
critical_high_issues: issues.count ?? 0,
pending_claims: payment_claims.count ?? 0
};
}
}); This sketch runs every morning at six Singapore time and summarizes open critical/high issues and pending payment claims.
What happens when it runs
- At six every morning Singapore time the run starts on its own, as the system identity holding
construction_automation. - It counts open critical/high issues and draft or submitted payment claims. The run, its trigger and its result appear in Command center → Queue.
- Nobody started it, so
ctx.triggeris{ kind: 'system', cause: 'cron' }.
Starting a run
on: [{ cron: '0 6 * * *' }]runs it on a schedule (cause: 'cron'), andon: [{ model, verbs }]fires it on committed model writes (cause: 'event').- A page calls
ctx.run(name, input); server code callsctx.schedule(name, input). A person may start it by hand only if a policy they hold lists it incapabilities.automations. ctx.schedule(name, input, { at, key })queues it after the current write is saved, for example from a model's effect. Inside a run,ctx.waitpauses durably until a duration elapses or a matching event arrives.- The agent may start it when
agentis'confirm', asking the person first.'never'hides it from the agent.
Input and output
input declares typed fields. { kind: 'rows', of: 'bookings' } takes record ids from the caller and gives the body the rows, read under the run's policies and ready to use as write targets. output declares what the run returns. mode: 'call' makes it answer at once instead of queuing a job.
// src/automation/+bookings_cancel.automation.ts (a sketch on scheduling's models)
import { automation } from '@norbital-ai/bolt';
export default automation({
description: 'Cancel bookings for the customer who made them.',
input: { bookings: { kind: 'rows', of: 'bookings' } },
policies: ['operations'],
agent: 'confirm',
run: function* ({ bookings }, ctx) {
// the run holds 'operations'; it acts for a customer only on their own bookings
const by = ctx.trigger;
const other = bookings.findIndex(
(b) => by.kind !== 'person' || by.actor.kind !== 'member' || b.customer !== by.actor.party
);
if (other >= 0) return yield* ctx.refuse('errors.not_your_booking', { row: other });
yield* ctx.act({
'bookings.update': bookings.map((b) => ({ target: b, set: { status: 'cancelled' } }))
});
}
});
// from a page: ctx.run('bookings_cancel', { bookings: [booking.id] }) Whose authority
A run always holds its own policies, never the starter's. ctx.trigger records who started it: a person (with the agent conversation, if any), or the system with its cause: cron, effect or schedule. The trigger grants nothing. When a run acts for its starter, check the trigger yourself, as above. Inside a run, { var: 'actor.id' } names the system, not the starter.
Work that must follow a change to a record belongs in the model's effect, which runs as the writer. A business operation with its own input and authority is an automation.
What a run can do
ctx.queryandctx.act: read and write records.ctx.request(connection, request): call an outside system through an integration.ctx.send(connection, message): send a message on a channel such as WhatsApp or email.ctx.notify([...]): post notifications to members' inboxes and push devices, and to the email and other channels the call names.ctx.ai: structured answers from a language model, quick decisions, and embeddings.ctx.convert: turn uploaded files into readable pages, and HTML or Markdown into documents such as PDF.ctx.web,ctx.files,ctx.geoandctx.browser: read web pages, store files, find places, and drive a browser tab.
Notifications
Call ctx.notify(notice | notices) from a model effect or automation with title, optional description (plain text), to ({ user }, { team } with a team id read from a row, or { policy }, plus 'requester' and 'approvers' in approval effects), optional link ({ model, id }, which opens the record sheet), optional once (deduplication key), optional via ('email' and workspace channels) and optional attachments. Notices queue after the write commits and never fail or delay it.
Every inbox notice fans out to the bell menu, live views and web and APNs push. The call decides email: via: ['email'] also sends transactional email through the host mail provider with zero workspace configuration, a message linking straight to the record for each active member with a deliverable email; reserved placeholder domains are skipped, and attachments stay on the inbox and the conversation channels.
To also send over a supplied channel such as a WhatsApp envoy, Telegram, Slack, Discord or workspace email account, query the active workspace-owned sys_integration (owner: null, under a sys_integration read grant) and pass its instance_id in via. Delivery resolves each staff member’s profile handle (phone, telegram, slack, discord, email) and skips anyone without one there while still sending the inbox notice; replies on an envoy-paired channel continue on that envoy thread. Use ctx.send for external recipients, groups, WeChat or custom payloads. Host transactional SMS is reserved for sign-in and invitation codes; send SMS notifications through an SMS channel or HTTP integration.
Failures and retries
retry sets attempts and backoff, and concurrency.max limits parallel runs (one by default). If the host loses a run, the run starts again from the beginning, so make outside calls safe to repeat. Separate ctx.act calls are separate commits: put writes that belong together in one map. Each run's status, trigger and result appear in the Command center.
Notices land in each member's bell menu, unread first, with read ones folded into their own section. Each run, with its trigger and result, is listed under Command center → Queue.