Skip to content

Commit 81b25bd

Browse files
committed
feat(spec): push channel protocol face — device registration widening, device contract members, push delivery data, APNs/FCM transport config by reference
Claude-Session: https://claude.ai/code/session_01FEkQ5oc56xChD6Wrc4cGSt Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 8dcce8b commit 81b25bd

9 files changed

Lines changed: 357 additions & 4 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
'@objectstack/spec': patch
3+
---
4+
5+
feat(spec): the push channel's protocol face: the widened device registration request, optional `INotificationService` device members, the push delivery data with its same-server `route`, and the APNs / FCM transport configuration by reference (ADR-0012 §6)
6+
7+
Clause-②: yes (widening)
8+
Metadata-protocol: none
9+
10+
- **`RegisterDeviceRequestSchema`** gains three optional members: `vendor` (`apns` | `fcm`; absent, the server derives it from `platform`, `ios` → `apns` and `android` → `fcm`), `appId` and `locale`. `token`, `platform`, `deviceId` and `name` are unchanged, so every body a client sent before still parses as it did. The describes now say that re-registering a `deviceId` refreshes that device, and that a request without one gets a server-minted id the client keeps. A `vendor` outside `apns` / `fcm` is refused, and since the member is new, no body sent before carries one. `RegisterDeviceResponseSchema` and `UnregisterDevice*` are unchanged.
11+
- **`INotificationService`** gains three optional members: `registerDevice(userId, request)`, `unregisterDevice(userId, deviceId)` and `invalidateDeviceToken(token)`. Their docs state the semantics an implementation owes: one live owner per token (a second user's registration takes it over), unregistering a device the user does not hold answers not found, and an invalidated token keeps its device row, disabled, with the invalidation time. Absence means the capability is unavailable, so every existing implementation stays valid.
12+
- **`PushDeliveryDataSchema`** (new, `api/push-delivery.zod.ts`) is the data a push carries to the shell: `notificationId` and an optional `route`. `PushRouteSchema` admits only a same-server relative path (one leading `/`; no scheme, host, backslash, whitespace or control character). The route is derived, never authored: it is the notification's existing `actionUrl` when that passes the schema, and absent otherwise. No `notify` key is added.
13+
- **`PushServiceConfigSchema`** (new, `system/push-config.zod.ts`, beside the email transport configuration): an `apns` block (token auth: `teamId`, `keyId`, `topic`, `environment: sandbox | production`, the `.p8` key by `keyRef`) and an `fcm` block (HTTP v1: `projectId`, the service-account JSON by `serviceAccountRef`). Secrets are references only (ADR-0143 D3). An inline secret is refused by name under the keys an operator would reach for, and refused as the content of a reference (a PEM block or a JSON document). `PushVendorSchema` names the two vendors. Nothing reads this shape until the push channel plugin lands, and it is not a stack-definition key.
14+
15+
No route row is added: the device routes land with their handlers in the server slice, so `DEFAULT_NOTIFICATION_ROUTES` is unchanged. Nothing an author or a client writes today is refused.

‎packages/spec/src/api/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export * from './errors.zod';
6161
export { zodIssuesToFields } from './zod-issues-to-fields';
6262
export * from './error-code-ledger.zod';
6363
export * from './protocol.zod';
64+
export * from './push-delivery.zod';
6465
// [#10235] Per-column sortability projection: the serve-time signal the object
6566
// metadata read publishes so grids never re-derive "virtual ⇒ unsortable" from
6667
// field type (2026-08-23 ruling, option A).

‎packages/spec/src/api/protocol.zod.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { RealtimePresenceSchema, TransportProtocol } from './realtime.zod';
2929
import { ObjectPermissionSchema, EffectiveObjectPermissionSchema, FieldPermissionSchema } from '../security/permission.zod';
3030
import { ActionDescriptorSchema } from '../automation/node-executor.zod';
3131
import { ServedTranslationDataSchema } from '../system/translation.zod';
32+
import { PushVendorSchema } from '../system/push-config.zod';
3233
// #5950 / #5882 — the ADR-0010 read-side protection envelope both metadata-item
3334
// responses publish. Same three vocabularies the resolver filters against, so a
3435
// value this spec cannot name is a value the resolver would have dropped.
@@ -3506,11 +3507,32 @@ export const GetPresenceResponseSchema = lazySchema(() => z.object({
35063507
// Notification Operations
35073508
// ==========================================
35083509

3510+
/**
3511+
* Register or refresh a push device for the signed-in user (ADR-0012 §6).
3512+
*
3513+
* One request does both: registering a `deviceId` the user already has
3514+
* refreshes that device (its token, vendor, app and locale) instead of adding
3515+
* a second one. A request without `deviceId` gets one minted by the server
3516+
* and answered in {@link RegisterDeviceResponseSchema}; the client keeps it
3517+
* and sends it on every later registration and on unregister.
3518+
*
3519+
* The members after `name` (`vendor`, `appId`, `locale`) were added for the
3520+
* push channel, all optional, so the body a client sent before still parses
3521+
* unchanged. The device's owner is the session's user, never a body member.
3522+
*/
35093523
export const RegisterDeviceRequestSchema = lazySchema(() => z.object({
35103524
token: z.string().describe('Device push notification token'),
35113525
platform: z.enum(['ios', 'android', 'web']).describe('Device platform'),
3512-
deviceId: z.string().optional().describe('Unique device identifier'),
3526+
deviceId: z.string().optional().describe(
3527+
'Client-held device id, stable per app install. Re-registering the same deviceId refreshes that device; '
3528+
+ 'absent, the server mints one and answers it, and the client keeps it for later registrations and unregister',
3529+
),
35133530
name: z.string().optional().describe('Device friendly name'),
3531+
vendor: PushVendorSchema.optional().describe(
3532+
'Push vendor the token belongs to: apns or fcm. Absent, the server derives it from platform: ios means apns, android means fcm (web has no core vendor)',
3533+
),
3534+
appId: z.string().optional().describe('App the token was issued to: the iOS bundle ID or the Android application ID'),
3535+
locale: z.string().optional().describe('Device locale (BCP 47, for example zh-CN) that pushes to this device are rendered in'),
35143536
}));
35153537

35163538
export const RegisterDeviceResponseSchema = lazySchema(() => z.object({
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
import { z } from 'zod';
4+
import { lazySchema } from '../shared/lazy-schema';
5+
6+
/**
7+
* Push delivery data — what a push carries to the shell (ADR-0012 §8: the
8+
* `data` member of the push channel's rendered body).
9+
*
10+
* The shell reads it when the user taps the push: `notificationId` names the
11+
* notification (the same id the inbox lists and `markRead` takes), and
12+
* `route` is the Console path the shell opens.
13+
*
14+
* **Where `route` comes from — derived, never authored.** There is no
15+
* `notify` key for it. It is the notification's existing `actionUrl`, the
16+
* value the inbox already serves: an explicit `url` / `actionUrl` in the
17+
* notification's payload, else `/OBJECT/ID` built from the notification's
18+
* source record, else none. When that value passes {@link PushRouteSchema}
19+
* the push carries it as `route`; when it does not (an absolute URL to
20+
* another site, say) or there is none, the push carries no `route` and the
21+
* tap opens the shell's default screen. The delivery is never failed for it.
22+
*
23+
* Not to be confused with the `PushNotificationSchema` that `system/` once
24+
* declared and then removed (ADR-0049): that was an authoring template shape;
25+
* this is the wire data of a delivery, produced by the platform.
26+
*/
27+
28+
/**
29+
* A same-server relative path the shell may open: it starts with exactly one
30+
* `/`, so it carries no scheme and no host, and it contains no backslash,
31+
* whitespace or control character — a URL parser reads `\` as `/` and strips
32+
* tab and newline, which would turn `/\host` — or `/`, a tab, then `/host` —
33+
* into a protocol-relative reference to another site.
34+
*/
35+
export const PushRouteSchema = z.string()
36+
.regex(/^\/(?![/\\])[^\s\\\x00-\x1f\x7f]*$/, {
37+
message: 'A push route is a same-server relative path: it starts with a single "/" and carries no scheme, host, '
38+
+ 'backslash, whitespace or control character.',
39+
})
40+
.describe('Same-server relative path the shell opens on tap (for example /apps/crm/opportunity/123); never an absolute or off-server URL');
41+
42+
/** The data a push delivery carries to the shell. */
43+
export const PushDeliveryDataSchema = lazySchema(() => z.object({
44+
notificationId: z.string().min(1).describe('Notification id — the one the inbox lists and markRead takes'),
45+
route: PushRouteSchema.optional()
46+
.describe('Console route opened on tap: the notification actionUrl when it is a same-server relative path, absent otherwise'),
47+
}));
48+
export type PushDeliveryData = z.input<typeof PushDeliveryDataSchema>;

‎packages/spec/src/contracts/notification-service.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,48 @@ describe('Notification Service Contract', () => {
207207
expect((await service.listInbox!('u-1', { read: false })).notifications).toHaveLength(0);
208208
});
209209
});
210+
211+
describe('push device members (ADR-0012 §6)', () => {
212+
it('are optional: a send-only provider is still a complete implementation', () => {
213+
const service: INotificationService = { send: async () => ({ success: true }) };
214+
expect(service.registerDevice).toBeUndefined();
215+
expect(service.unregisterDevice).toBeUndefined();
216+
expect(service.invalidateDeviceToken).toBeUndefined();
217+
});
218+
219+
it('type the registry an implementation provides: register, unregister, invalidate', async () => {
220+
// A toy registry written to the documented semantics, so the member
221+
// signatures are exercised by a real implementation, not just named.
222+
const rows = new Map<string, { userId: string; token: string; enabled: boolean; invalidatedAt?: string }>();
223+
let minted = 0;
224+
const service: INotificationService = {
225+
send: async () => ({ success: true }),
226+
registerDevice: async (userId, request) => {
227+
const deviceId = request.deviceId ?? `dev-${++minted}`;
228+
for (const [id, row] of rows) if (row.token === request.token && id !== deviceId) rows.delete(id);
229+
rows.set(deviceId, { userId, token: request.token, enabled: true });
230+
return { deviceId, success: true };
231+
},
232+
unregisterDevice: async (userId, deviceId) => {
233+
if (rows.get(deviceId)?.userId !== userId) throw Object.assign(new Error('Device not found'), { code: 'NOT_FOUND' });
234+
rows.delete(deviceId);
235+
return { success: true };
236+
},
237+
invalidateDeviceToken: async (token) => {
238+
for (const row of rows.values()) {
239+
if (row.token === token) Object.assign(row, { enabled: false, invalidatedAt: '2026-01-01T00:00:00.000Z' });
240+
}
241+
},
242+
};
243+
244+
const minted1 = await service.registerDevice!('u-1', { token: 'tok', platform: 'ios', vendor: 'apns' });
245+
expect(minted1).toEqual({ deviceId: 'dev-1', success: true });
246+
// A second user registering the same token takes it over.
247+
await service.registerDevice!('u-2', { token: 'tok', platform: 'ios', deviceId: 'install-2' });
248+
expect([...rows.keys()]).toEqual(['install-2']);
249+
await expect(service.unregisterDevice!('u-1', 'install-2')).rejects.toMatchObject({ code: 'NOT_FOUND' });
250+
await service.invalidateDeviceToken!('tok');
251+
expect(rows.get('install-2')).toMatchObject({ enabled: false, invalidatedAt: '2026-01-01T00:00:00.000Z' });
252+
});
253+
});
210254
});

‎packages/spec/src/contracts/notification-service.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* addressed to an unregistered channel are dead-lettered, not delivered.
2727
*/
2828
import type { NotificationChannel } from '../system/notification.zod';
29+
import type { RegisterDeviceRequest, RegisterDeviceResponse, UnregisterDeviceResponse } from '../api/protocol.zod';
2930
export type { NotificationChannel } from '../system/notification.zod';
3031

3132
/**
@@ -207,4 +208,65 @@ export interface INotificationService {
207208
* @param userId - Owner of the inbox
208209
*/
209210
markAllRead?(userId: string): Promise<MarkReadResult>;
211+
212+
/* ------------------------------------------------------------------ */
213+
/* Push devices — the device registry (ADR-0012 §6) */
214+
/* ------------------------------------------------------------------ */
215+
216+
/*
217+
* All three are OPTIONAL, for the reason the inbox trio is: a provider
218+
* with no push channel (SMTP, an SMS gateway, the dev stub) fills this
219+
* slot legitimately with no device registry. Absence means the
220+
* capability is unavailable — the device routes answer that, not a
221+
* crash — so every implementation written before these members stays
222+
* valid. Shapes are the wire contract the device routes serialize:
223+
* `RegisterDeviceRequestSchema` / `RegisterDeviceResponseSchema` /
224+
* `UnregisterDeviceResponseSchema` (`api/protocol.zod.ts`).
225+
*/
226+
227+
/**
228+
* Register or refresh a push device for this user.
229+
*
230+
* - **Refresh, not duplicate.** A `request.deviceId` this user already
231+
* holds refreshes that device (token, vendor, app, locale, last seen)
232+
* and re-enables it; an absent `deviceId` is minted here and answered
233+
* in the response, which the client keeps.
234+
* - **One live owner per token.** A token is delivered to one user only:
235+
* registering a token another user's device holds moves it to this
236+
* user, and the other user's device stops receiving it. A push meant
237+
* for the previous owner never reaches the phone's new user.
238+
* - An absent `request.vendor` is derived from `request.platform`
239+
* (`ios` → `apns`, `android` → `fcm`).
240+
*
241+
* @param userId - The signed-in user, from the session; never a body member
242+
* @param request - The registration body
243+
*/
244+
registerDevice?(userId: string, request: RegisterDeviceRequest): Promise<RegisterDeviceResponse>;
245+
246+
/**
247+
* Unregister one of this user's devices (sign-out, account switch).
248+
*
249+
* A `deviceId` this user does not hold — unknown, or another user's —
250+
* answers not found (`NOT_FOUND`, 404 at the device route), never a
251+
* success and never a distinct "forbidden": a caller cannot learn that a
252+
* device id exists for somebody else, and cannot remove it.
253+
*
254+
* @param userId - The signed-in user, from the session
255+
* @param deviceId - The id {@link registerDevice} answered
256+
*/
257+
unregisterDevice?(userId: string, deviceId: string): Promise<UnregisterDeviceResponse>;
258+
259+
/**
260+
* Retire a token the vendor reported dead (APNs `BadDeviceToken` /
261+
* `Unregistered`, FCM `UNREGISTERED`), called by the push channel's
262+
* invalid-recipient path (ADR-0012 §6, §12).
263+
*
264+
* The device row is KEPT, disabled, with the time of invalidation, so
265+
* "why did this phone stop getting pushes" stays answerable and a later
266+
* registration of the same device re-enables it. Nothing is delivered to
267+
* a disabled device. A token no device holds is a no-op.
268+
*
269+
* @param token - The vendor token as the vendor reported it
270+
*/
271+
invalidateDeviceToken?(token: string): Promise<void>;
210272
}

‎packages/spec/src/system/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export * from './email-config.zod';
7777
export * from './email-template.zod';
7878
export * from './email-template.form';
7979
export * from './sms-config.zod';
80+
export * from './push-config.zod';
8081
export * from './metadata-form-registry';
8182
// compliance.zod (GDPR/HIPAA/PCI configs) and masking.zod (role-based data
8283
// masking) were REMOVED per ADR-0056 D8 "design+enforce or remove": both were

0 commit comments

Comments
 (0)