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
98 changes: 98 additions & 0 deletions docs/SUPPORT_COMPOSITION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Support Desk composition (pegma.dev)

Public host composition notes for authenticated product feedback on
[pegma.dev](https://pegma.dev). This is the ecosystem's Cloudflare reference
environment for Support Desk customer create / list / read / reply.

## Packages

Exact published versions:

- `@pegma/support-desk-application@0.1.0`
- `@pegma/support-desk-contracts@0.1.0`
- `@pegma/support-desk-core@0.1.0`
- `@pegma/support-desk-templates@0.1.0`
- `@pegma/authorization-core@0.1.2` (AccessContext resolution)

Peers already present on the host: `@pegma/storage-core@0.4.0`,
`@pegma/storage-cloudflare-d1@0.4.0`, `@pegma/sessions@0.1.0`,
`@pegma/rate-limit@0.1.0`, `@pegma/identity@0.1.0`, `@pegma/spine@0.1.1`.

## Storage namespace

Support Desk uses the existing `IDENTITY_DB` D1 binding. Isolation is by
collection name, not by sharing Identity mail cursors or session rows:

| Concern | Collection / host key |
| --- | --- |
| Tickets, messages, audit, commands | `support-desk.records.v1` |
| Customer ticket index | `support-desk.customer-ticket-index.v1` |
| Ticket numbers | `support-desk.ticket-numbers.v1` |
| Queue projection | `support-desk.queue-index.v1` |
| Host maintenance cursors | `pegma-dev-support-maintenance` |

Identity keeps its own collections and `pegma-dev-maintenance` cursors. The
two never share a cursor key.

## Authorization

Any authenticated pegma.dev account receives customer permissions through host
AccessContext defaults (no paid entitlement):

- `support.ticket.create`
- `support.ticket.read.own`
- `support.ticket.reply.own`

`principalId` is the Identity principal / account id from the server-side
session (`__Host-pegma_session`). Browser-supplied identity fields are ignored.

## Categories and markers

Allowlist: `feedback`, `bug`, `feature_request`, `documentation`, `question`.

Display marker: `[PEG-{number}]`. Public tracking URL:

`https://pegma.dev/feedback/ticket/?id={ticketId}`

## HTTP boundary

Worker routes (same-origin session + CSRF `X-Pegma-CSRF`):

| Method | Path | Notes |
| --- | --- | --- |
| `GET` | `/api/support/categories` | Auth required |
| `GET` | `/api/support/tickets` | List own tickets |
| `POST` | `/api/support/tickets` | Create (rate-limited) |
| `GET` | `/api/support/tickets/:id` | Read own; missing/non-owned → same 404 |
| `POST` | `/api/support/tickets/:id/replies` | Reply (rate-limited) |

Durable rate-limit policies (separate from Identity):

- `pegma.support.ticket.create`
- `pegma.support.ticket.reply`

Site pages: `/feedback` (create + list), `/feedback/ticket/?id=…` (read + reply; static shell).

## Maintenance and health

Minute cron runs Support Desk queue repair and inactive projection sweep with
independent cursors, plus Support Desk rate-limit sweeps. Mail send for support
notifications is deferred (templates are defined; create/reply omit
notification for this first host PR).

Health always reports Support Desk package detail without message content. A
store probe runs only when `SUPPORT_HEALTH_PROBE` is exactly `"true"`.

## Privacy and retention intent

- Tickets are private conversations between the authenticated requester and
authorized operators.
- Feedback is not automatically published to the roadmap, documentation, or
GitHub Issues.
- Staff UI is deferred.
- Queue projection terminal retention follows Support Desk's default
(30 days) for inactive projection rows; authoritative ticket retention and
export/redaction procedures remain host operational policy and will be
documented further before accepting production-sensitive data at scale.

Built in the open by RetireGolden, LLC.
68 changes: 68 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"dependencies": {
"@pegma/audit": "0.1.0",
"@pegma/authorization-core": "0.1.2",
"@pegma/authorization-identity": "0.1.2",
"@pegma/health": "^0.1.1",
"@pegma/identity": "0.1.0",
Expand All @@ -30,6 +31,10 @@
"@pegma/spine": "^0.1.1",
"@pegma/storage-cloudflare-d1": "0.4.0",
"@pegma/storage-core": "0.4.0",
"@pegma/support-desk-application": "0.1.0",
"@pegma/support-desk-contracts": "0.1.0",
"@pegma/support-desk-core": "0.1.0",
"@pegma/support-desk-templates": "0.1.0",
"@pegma/webhooks": "file:vendor/@pegma/webhooks/pegma-webhooks-0.0.0.tgz"
},
"devDependencies": {
Expand Down
134 changes: 134 additions & 0 deletions public/feedback-ticket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
const statusNode = document.querySelector('#ticket-status');
const signedOut = document.querySelector('#ticket-signed-out');
const detail = document.querySelector('#ticket-detail');
const heading = document.querySelector('#ticket-heading');
const meta = document.querySelector('#ticket-meta');
const messageList = document.querySelector('#message-list');
const replyForm = document.querySelector('#reply-form');
const ticketId = new URL(window.location.href).searchParams.get('id') ?? '';

let csrfToken = '';

function showStatus(message, isError = false) {
statusNode.textContent = message;
statusNode.dataset.tone = isError ? 'error' : 'neutral';
}

async function api(path, init = {}) {
const headers = new Headers(init.headers);
if (init.body !== undefined) {
headers.set('Content-Type', 'application/json');
}
if (csrfToken !== '') {
headers.set('X-Pegma-CSRF', csrfToken);
}
const response = await fetch(path, {
...init,
headers,
credentials: 'same-origin',
});
const body = await response.json();
if (!response.ok) {
const error = new Error(
typeof body.error === 'string' ? body.error : 'request_failed',
);
error.status = response.status;
throw error;
}
return body;
}

function renderMessages(messages) {
messageList.replaceChildren();
for (const message of messages) {
const item = document.createElement('li');
const who = document.createElement('strong');
who.textContent =
message.authorKind === 'customer'
? 'You'
: message.authorKind === 'staff'
? 'Pegma'
: 'System';
const when = document.createElement('span');
when.className = 'form-note';
when.textContent = new Date(message.createdAt).toLocaleString();
const body = document.createElement('p');
body.textContent = message.body;
item.append(who, ' · ', when, body);
messageList.append(item);
}
}

function renderTicket(view) {
heading.textContent = `${view.ticket.marker} ${view.ticket.subject}`;
meta.textContent = `${view.ticket.status} · ${view.ticket.category ?? 'uncategorized'}`;
renderMessages(view.messages);
}

async function loadTicket() {
if (ticketId === '') {
showStatus('Missing ticket id.', true);
return;
}
const idDisplay = document.querySelector('#ticket-id-display');
if (idDisplay) idDisplay.textContent = ticketId;
try {
await api('/api/identity/account');
} catch (error) {
if (error.status === 401) {
signedOut.hidden = false;
detail.hidden = true;
showStatus('Sign in to view this ticket.');
return;
}
throw error;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [warning] Non-401 identity failures abort loadTicket without UI recovery

loadTicket() preflights /api/identity/account and rethrows any error that is not status 401. loadTicket() is invoked without a rejection handler. Identity runtime unavailability (503), network failure, or non-JSON responses therefore become unhandled promise rejections while the page remains on "Loading ticket…". The list page (feedback.js) handles the same class of failure with a user-visible status message; the ticket page does not.

Suggestion: In the identity preflight catch, treat non-401 failures the same way as the ticket-load catch: set detail hidden, call showStatus with a clear unavailable message, and return. Do not rethrow.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Handle identity preflight failures inside the ticket page

When /api/identity/account returns anything other than 401, or its fetch/JSON parsing fails, this rethrow escapes loadTicket(), which is invoked without awaiting or catching its promise. The page consequently remains stuck on “Loading ticket…” with an unhandled rejection instead of showing an availability error; handle these failures here or wrap the entire load flow in the existing status-rendering catch.

Useful? React with 👍 / 👎.

}

try {
const view = await api(
`/api/support/tickets/${encodeURIComponent(ticketId)}`,
);
if (typeof view.csrfToken === 'string') {
csrfToken = view.csrfToken;
}
signedOut.hidden = true;
detail.hidden = false;
renderTicket(view);
showStatus('Ticket loaded.');
} catch (error) {
signedOut.hidden = true;
detail.hidden = true;
if (error.status === 401) {
signedOut.hidden = false;
showStatus('Sign in to view this ticket.');
return;
}
if (error.status === 404) {
showStatus('Ticket not found.', true);
return;
}
showStatus(`Could not load ticket: ${error.message}`, true);
}
}

replyForm.addEventListener('submit', async (event) => {
event.preventDefault();
try {
const form = new FormData(replyForm);
showStatus('Sending reply…');
const view = await api(
`/api/support/tickets/${encodeURIComponent(ticketId)}/replies`,
{
method: 'POST',
body: JSON.stringify({ body: form.get('body') }),
},
);
replyForm.reset();
renderTicket(view);
showStatus('Reply sent.');
} catch (error) {
showStatus(`Could not send reply: ${error.message}`, true);
}
});

loadTicket();
Loading
Loading