Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .changeset/36469-comment-doors-hosted.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@objectstack/spec': patch
'@objectstack/runtime': patch
'@objectstack/plugin-audit': patch
---

fix(comments): unreact, resolve and own-comment delete work on a kernel with no `http.server` (a hosted tenant kernel) instead of answering 404

Clause-②: yes (widening)
Metadata-protocol: none

The three comment doors, `DELETE /api/v1/comments/reactions/:id`, `PUT /api/v1/comments/:id/resolution` and `DELETE /api/v1/comments/:id`, were mounted by `AuditPlugin` only on the kernel's `http.server`. A hosted tenant kernel owns no socket and registers no `http.server`, so the host's `createHonoApp` catch-all handed each of them to the runtime dispatcher, which had no domain for them and answered `404 ROUTE_NOT_FOUND`, and removing a reaction, resolving or reopening a thread and deleting your own comment failed there.

- `@objectstack/spec/contracts` declares `ICommentDoorsService` with one optional, transport-neutral member, `handleCommentDoor(request: Request): Promise<Response>`, and types the `comments` service slot with it (`ServiceSlotContracts.comments`). A service without the member still satisfies the contract.
- `AuditPlugin` registers the `comments` service with that member. It runs the same registrars over the same dependencies the `http.server` mount runs, on a private route table that never listens, so every answer is the mount's own. The `http.server` mount is unchanged, and no path is mounted twice. `@objectstack/plugin-audit` now depends on `@objectstack/plugin-hono-server` at runtime (the private table is a `HonoHttpServer`), and the `comments` service name is now taken: a composition that registers its own `comments` service alongside `AuditPlugin` fails to boot with a duplicate-service error.
- The runtime dispatcher gains a `/comments` domain that claims exactly the three method and path shapes and forwards the request, body unread, to the request kernel's `comments` service. Every other `/comments` shape keeps the dispatcher's `404 ROUTE_NOT_FOUND`. With no `comments` service, or one without `handleCommentDoor`, the doors answer `501 NOT_IMPLEMENTED` naming `@objectstack/plugin-audit`. The doors stay behind the dispatcher's authentication and project-membership gates.
1 change: 1 addition & 0 deletions packages/plugins/plugin-audit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@objectstack/metadata-core": "workspace:*",
"@objectstack/objectql": "workspace:*",
"@objectstack/platform-objects": "workspace:*",
"@objectstack/plugin-hono-server": "workspace:*",
"@objectstack/spec": "workspace:*",
"@objectstack/types": "workspace:*"
},
Expand Down
77 changes: 56 additions & 21 deletions packages/plugins/plugin-audit/src/audit-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
IAuditCheckpointSink,
IAuthService,
IClusterService,
ICommentDoorsService,
ICryptoProvider,
IDataEngine,
IHttpServer,
Expand Down Expand Up @@ -70,6 +71,7 @@ import {
type CommentDoorsEngine,
type CommentPostingGate,
} from './comment-routes.js';
import { createCommentDoorMember } from './comment-doors-member.js';
import { admitMentionRecipients } from './mention-recipient-gate.js';
import { installActivityReadVisibility } from './activity-read-visibility.js';
import { installActivityFieldRedaction } from './activity-field-redaction.js';
Expand Down Expand Up @@ -528,6 +530,16 @@ export class AuditPlugin implements Plugin {
...createAppActivityWriter({ getEngine: getLedgerEngine, getActorSnapshot: () => this.actorSnapshotOf }),
});

// [#36469] The comment doors on the `comments` slot, transport-neutrally
// (`ICommentDoorsService.handleCommentDoor`): the runtime dispatcher's
// `/comments` domain forwards to it on a kernel with no `http.server` (a
// hosted tenant kernel), where `mountCommentDoors` has nothing to mount
// on. Same registration as the mount; nothing is read until a request.
const commentDoors: ICommentDoorsService = {
handleCommentDoor: createCommentDoorMember((http) => registerCommentDoors(http, ctx)),
};
ctx.registerService('comments', commentDoors);

// ADR-0029 D8 — contribute this plugin's object translations to the i18n
// service on kernel:ready (the i18n plugin may register after this one).
if (typeof (ctx as any).hook === 'function') {
Expand Down Expand Up @@ -1083,8 +1095,9 @@ export class AuditPlugin implements Plugin {
* grant.
*
* Every service the doors read is read per request, so a provider that
* registers after this hook is still seen. A kernel with no HTTP server
* mounts nothing, and says so.
* registers after this hook is still seen. A kernel with no HTTP server (a
* hosted tenant kernel) mounts nothing here: the `comments` slot serves the
* same doors to the dispatcher (#36469), and the line says so.
*/
private mountCommentDoors(ctx: PluginContext): void {
const optional = <T>(name: string): T | undefined => {
Expand All @@ -1097,28 +1110,18 @@ export class AuditPlugin implements Plugin {
// `http.server` is the canonical name; `http-server` the deprecated alias.
const http = optional<IHttpServer>('http.server') ?? optional<IHttpServer>('http-server');
if (!http) {
ctx.logger.warn(
`AuditPlugin: no HTTP server is registered, so the comment doors (DELETE ${COMMENT_REACTION_REMOVE_ROUTE}, ` +
`PUT ${COMMENT_RESOLUTION_ROUTE}, DELETE ${COMMENT_REMOVE_ROUTE}) are not mounted: a member whose grants carry ` +
'no delete cannot remove their own reaction or comment, and resolves only the threads they started.',
// [#36469] The hosted shape: a tenant kernel owns no socket. The doors
// are not lost there: the `comments` slot (registered in `init()`) serves
// them to the runtime dispatcher's `/comments` domain, which the host's
// `createHonoApp` reaches.
ctx.logger.info(
`AuditPlugin: comment doors mounted on the 'comments' service for the dispatcher's /comments domain ` +
`(no HTTP server on this kernel): DELETE ${COMMENT_REACTION_REMOVE_ROUTE}, PUT ${COMMENT_RESOLUTION_ROUTE} ` +
`and DELETE ${COMMENT_REMOVE_ROUTE}`,
);
return;
}
const getEngine = () => optional<IDataEngine>('objectql') ?? optional<IDataEngine>('data');
const resolveCaller = platformCallerResolver({
getEngine,
getAuth: () => optional<IAuthService>('auth'),
tenancy: tenancyServiceResolver(ctx),
});
registerCommentReactionRoutes(http, {
getEngine: () => getEngine() as unknown as CommentReactionEngine | undefined,
resolveCaller,
});
registerCommentDoorsRoutes(http, {
getEngine: () => getEngine() as unknown as CommentDoorsEngine | undefined,
resolveCaller,
getSecurity: () => optional<CommentPostingGate>('security'),
});
registerCommentDoors(http, ctx);
ctx.logger.info(
`AuditPlugin: comment doors mounted at DELETE ${COMMENT_REACTION_REMOVE_ROUTE}, PUT ${COMMENT_RESOLUTION_ROUTE} ` +
`and DELETE ${COMMENT_REMOVE_ROUTE}`,
Expand Down Expand Up @@ -1150,6 +1153,38 @@ export class AuditPlugin implements Plugin {
}
}

/**
* Register the three comment doors on `http`, over this kernel's services, each
* read per request. The ONE registration both faces run: the self-hosted mount
* on the kernel's `http.server` (`mountCommentDoors`) and the private table
* behind the `comments` slot's `handleCommentDoor` (`comment-doors-member.ts`),
* so the two cannot answer a request differently.
*/
function registerCommentDoors(http: IHttpServer, ctx: PluginContext): void {
const optional = <T>(name: string): T | undefined => {
try {
return ctx.getService<T>(name) ?? undefined;
} catch {
return undefined;
}
};
const getEngine = () => optional<IDataEngine>('objectql') ?? optional<IDataEngine>('data');
const resolveCaller = platformCallerResolver({
getEngine,
getAuth: () => optional<IAuthService>('auth'),
tenancy: tenancyServiceResolver(ctx),
});
registerCommentReactionRoutes(http, {
getEngine: () => getEngine() as unknown as CommentReactionEngine | undefined,
resolveCaller,
});
registerCommentDoorsRoutes(http, {
getEngine: () => getEngine() as unknown as CommentDoorsEngine | undefined,
resolveCaller,
getSecurity: () => optional<CommentPostingGate>('security'),
});
}

/**
* How the unreact door reaches the `tenancy` service: the kernel's async
* accessor, read per request and classified by the composition
Expand Down
106 changes: 106 additions & 0 deletions packages/plugins/plugin-audit/src/comment-doors-member.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

/**
* [#36469] The comment doors, served transport-neutrally:
* `ICommentDoorsService.handleCommentDoor` (`@objectstack/spec/contracts`).
*
* ## Why this exists
*
* `AuditPlugin` mounts the three comment doors on the kernel's `http.server`
* (`mountCommentDoors`). A hosted tenant kernel owns no socket and registers no
* `http.server`, so on that shape the doors were never mounted. Its host's
* `createHonoApp` catch-all handed each unreact, resolve and own-comment
* delete to the runtime dispatcher, where no domain claimed it, and the
* answer was the dispatcher's `404 ROUTE_NOT_FOUND`. (The card's hosted
* reading of `404 ENDPOINT_NOT_FOUND` is not explained by this tree.) The
* runtime dispatcher reaches that
* kernel instead, through its `/comments` domain, which forwards the request
* to this member on the kernel's `comments` slot (ruling A on #22438, applied
* to this member of the class).
*
* ## One body, two faces
*
* The member runs the SAME registrars the self-hosted mount runs
* (`registerCommentReactionRoutes`, `registerCommentDoorsRoutes`) over the SAME
* deps, on a private route table of its own: a `HonoHttpServer` that never
* listens. So the caller resolution, every refusal, the request-body reading
* (and its refusal) and every answer are byte-for-byte the mount's: there is
* no second copy of a door to drift from the first. The table is built on the
* first request, and every service the doors read is still read per request.
*
* ⛔ The table never listens and is never the kernel's `http.server`: a
* self-hosted kernel keeps its own mount unchanged, and no path is mounted
* twice on a socket.
*
* ## What it answers outside the three routes
*
* A method and path that match none of the three routes answers
* `404 ENDPOINT_NOT_FOUND`, the transport's own unmatched answer, without
* reaching any door. The dispatcher claims exactly the three shapes and never
* forwards one.
*/

import type { IHttpServer } from '@objectstack/spec/contracts';
import { HonoHttpServer } from '@objectstack/plugin-hono-server';

import { COMMENT_REACTION_REMOVE_ROUTE } from './comment-reaction-routes.js';
import { COMMENT_REMOVE_ROUTE, COMMENT_RESOLUTION_ROUTE } from './comment-routes.js';

/** One comment door's method and route pattern. */
export interface CommentDoorRoute {
method: 'DELETE' | 'PUT';
route: string;
}

/**
* The three comment doors, in the self-hosted mount's registration order (the
* order a request is matched in): the unreact door, then the resolution door,
* then the own-comment delete door.
*/
export const COMMENT_DOOR_ROUTES: readonly CommentDoorRoute[] = [
{ method: 'DELETE', route: COMMENT_REACTION_REMOVE_ROUTE },
{ method: 'PUT', route: COMMENT_RESOLUTION_ROUTE },
{ method: 'DELETE', route: COMMENT_REMOVE_ROUTE },
];

/** Does `pathname` match `pattern`, whose `:name` segments match one non-empty segment each? */
function matchesRoute(pattern: string, pathname: string): boolean {
const want = pattern.split('/');
const got = pathname.split('/');
if (want.length !== got.length) return false;
return want.every((segment, i) => (segment.startsWith(':') ? got[i].length > 0 : segment === got[i]));
}

/**
* The comment door `method` and `pathname` name, or `undefined`. Exported so
* the runtime's claim and this member can be pinned to the same three shapes.
*/
export function matchCommentDoor(method: string, pathname: string): CommentDoorRoute | undefined {
const verb = method.toUpperCase();
return COMMENT_DOOR_ROUTES.find((door) => door.method === verb && matchesRoute(door.route, pathname));
}

/** The transport's unmatched answer, as `plugin-hono-server`'s `unmatchedResponse` writes it. */
function unmatched(): Response {
return new Response(JSON.stringify({ success: false, error: { code: 'ENDPOINT_NOT_FOUND', message: 'Not found' } }), {
status: 404,
headers: { 'content-type': 'application/json' },
});
}

/**
* Build `handleCommentDoor` over `mount`, the function that registers the
* three doors on an `IHttpServer` (the one the self-hosted mount calls too).
*/
export function createCommentDoorMember(mount: (http: IHttpServer) => void): (request: Request) => Promise<Response> {
let table: HonoHttpServer | undefined;
return async (request: Request): Promise<Response> => {
if (!matchCommentDoor(request.method, new URL(request.url).pathname)) return unmatched();
if (!table) {
const built = new HonoHttpServer(0);
mount(built);
table = built;
}
return table.getRawApp().fetch(request);
};
}
7 changes: 7 additions & 0 deletions packages/plugins/plugin-audit/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ export default defineConfig({
// One rule for all namespaces cannot go stale that way.
alias: [
{ find: /^@objectstack\/core$/, replacement: path.resolve(__dirname, '../../core/src/index.ts') },
// The never-listening route table `comment-doors-member.ts` serves the
// comment doors on for a kernel with no `http.server`: to source, so the
// suite's verdict is about the checkout (`pnpm check:test-source-alias`).
{
find: /^@objectstack\/plugin-hono-server$/,
replacement: path.resolve(__dirname, '../plugin-hono-server/src/index.ts'),
},
// [#16829] The real driver `read-audit-view-instant-preservation.integration.test.ts`
// boots. That file exists to judge the ledger row AFTER the real
// `sys_stamp_audit_insert` hook and a real driver have both run, so
Expand Down
Loading
Loading