Skip to content
  1. Home
  2. workspace
Checking…
Demo view — sanitized data only. Sign in over HTTPS to create projects and manage the workspace.

AI Engineering Workspace

Partial

Chat, files, terminal and preview in one place. Chat sessions run on a durable Postgres-backed queue (chat only: no tools or shell). Live models need an API key; the terminal is unavailable.

Project files

read-only

app/src/lib/audit.ts

1import crypto from "node:crypto";
2import { db, now, asSystem } from "./db";
3
4/** Append-only, hash-chained audit record (tamper-evident; verify with verifyAudit). */
5export const audit = (actor: string, action: string, resource = "", detail: unknown = {}) => asSystem(() => auditInner(actor, action, resource, detail)); // the chain writer is an explicitly authorized system operation
6async function auditInner(actor: string, action: string, resource: string, detail: unknown) {
7 await db().tx(async c => {
8 await c.query("SELECT pg_advisory_xact_lock(7001)"); // serialise appends so the hash chain cannot fork
9 const last = (await c.query("SELECT hash FROM audit_log ORDER BY seq DESC LIMIT 1")).rows[0] as { hash: string } | undefined;
10 const prev = last?.hash ?? "GENESIS";
11 const ts = now();
12 const det = JSON.stringify(detail).slice(0, 2000);
13 const hash = crypto.createHash("sha256").update([prev, ts, actor, action, resource, det].join("|")).digest("hex");
14 await c.query("INSERT INTO audit_log(ts,actor,action,resource,detail,prev_hash,hash,org_id) VALUES($1,$2,$3,$4,$5,$6,$7,$8)", [ts, actor, action, resource, det, prev, hash, (detail as any)?.org ?? null]);
15 });
16}
17
18export async function verifyAudit(): Promise<{ ok: boolean; count: number; brokenAt?: number }> {
19 const rows = await asSystem(() => db().prepare("SELECT * FROM audit_log ORDER BY seq").all()) as any[]; // integrity check spans all tenants (system operation)
20 let prev = "GENESIS";
21 for (const r of rows) {
22 const h = crypto.createHash("sha256").update([prev, r.ts, r.actor, r.action, r.resource, r.detail].join("|")).digest("hex");
23 if (r.prev_hash !== prev || r.hash !== h) return { ok: false, count: rows.length, brokenAt: r.seq };
24 prev = r.hash;
25 }
26 return { ok: true, count: rows.length };
27}
28
29/** org given => only that tenant's entries (stamped by auditT); omitted => platform-wide (super admin only). */
30export async function listAudit(q = "", limit = 100, org?: string) {
31 const like = `%${q}%`; const orgLike = org ? `%"org":"${org.replace(/[^\w-]/g, "")}"%` : "%";
32 return await db().prepare(
33 "SELECT seq,ts,actor,action,resource,detail,hash FROM audit_log WHERE (action LIKE ? OR resource LIKE ? OR actor LIKE ?) AND detail LIKE ? ORDER BY seq DESC LIMIT ?"
34 ).all(like, like, like, orgLike, limit) as any[];
35}
36

Conversation

Partial
Sign in to start AI sessions.

Task checklist

  • Application shell deployed
  • Persistent projects
  • Agent runtime
  • Sandboxed terminal

Execution activity

No agent sessions. Git HEAD fb22adb on master.

Approval requests

None pending.