The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
/**
* A project's rules record in its own collection (policy.ts), one row per revision, written in the command's transaction.
* Every filter is an equality or a range on the indexed fields (tabId, projectId, revision): never `in` (hotFilters.test.ts).
*/
import type { Prisma } from '@prisma/client'
import { fail, stableId } from './model.js'
import { MAX_RULES_REVISIONS, projectRulesSchema, seedRules, type ProjectRules, type RulesChange, type RulesWrite } from './policy.js'
type Transaction = Prisma.TransactionClient
export const rulesRevisionId = (tabId: string, projectId: string, revision: number): string => stableId(tabId, projectId, 'rules', String(revision))
/** One kept revision as the history lists it. */
export interface RulesRevision {
revision: number
rules: ProjectRules
diff: RulesChange[]
summary: string
writtenBy: { kind: string; id: string }
askedBy: string
askedIn: { conversationId: string; messageId: string } | null
at: Date
}
/** The project's newest revision, or null when none is saved (the seed, revision 0). One indexed read.
* forEdit lets a rules save repair an invalid record; workers always require a valid one. */
export async function readRules(tx: Transaction, tabId: string, projectId: string, forEdit = false): Promise<{ revision: number; rules: ProjectRules } | null> {
const row = await tx.superTabProjectRules.findFirst({ where: { tabId, projectId }, orderBy: { revision: 'desc' }, select: { revision: true, rules: true } })
if (!row) return null
// Normalize legacy dispatch; only the repair path may read an invalid saved record.
const parsed = projectRulesSchema.safeParse(row.rules)
if (!parsed.success && !forEdit) fail('RULES_INVALID', `Project ${projectId} rules revision ${row.revision} does not pass the schema; repair the project rules by saving them again.`, 409)
return { revision: row.revision, rules: seedRules(tabId, projectId, parsed.success ? parsed.data : row.rules as unknown as ProjectRules) }
}
/** The record a project runs on now, for a server worker (the merge train, the release schedule): its newest revision, or
* the seed at revision 0. Takes the client or a transaction. */
export async function currentRules(tx: Transaction, tabId: string, projectId: string): Promise<{ revision: number; rules: ProjectRules }> {
return await readRules(tx, tabId, projectId) ?? { revision: 0, rules: seedRules(tabId, projectId) }
}
/** Every kept revision, newest first: at most MAX_RULES_REVISIONS rows of the same index.
* Invalid history stays readable for repair; no worker runs on it. */
export async function readRulesHistory(tx: Transaction, tabId: string, projectId: string): Promise<RulesRevision[]> {
const rows = await tx.superTabProjectRules.findMany({ where: { tabId, projectId }, orderBy: { revision: 'desc' }, take: MAX_RULES_REVISIONS })
return rows.map(row => {
const parsed = projectRulesSchema.safeParse(row.rules)
return { revision: row.revision, rules: parsed.success ? parsed.data : row.rules as unknown as ProjectRules, diff: row.diff as unknown as RulesChange[],
summary: row.summary, writtenBy: { kind: row.writtenByKind, id: row.writtenBy }, askedBy: row.askedBy,
askedIn: (row.askedIn ?? null) as RulesRevision['askedIn'], at: row.at }
})
}
/** The new revision, created (never upserted: a second writer of the same revision meets the taken key and retries into
* RULES_STALE), and no more than MAX_RULES_REVISIONS of them. */
export async function writeRules(tx: Transaction, tabId: string, write: RulesWrite): Promise<void> {
const { projectId, revision } = write
await tx.superTabProjectRules.create({ data: { id: rulesRevisionId(tabId, projectId, revision), tabId, projectId, revision,
rules: write.rules as unknown as Prisma.InputJsonValue, diff: write.diff as unknown as Prisma.InputJsonValue, summary: write.summary,
writtenByKind: write.writtenBy.kind, writtenBy: write.writtenBy.id, askedBy: write.askedBy,
askedIn: write.askedIn === null ? undefined : write.askedIn as unknown as Prisma.InputJsonValue, at: new Date(write.at) } })
// Growth: the 51st revision deletes the oldest.
await tx.superTabProjectRules.deleteMany({ where: { tabId, projectId, revision: { lte: revision - MAX_RULES_REVISIONS } } })
}
/** Everything a deleted project kept of its rules. */
export async function deleteRules(tx: Transaction, tabId: string, projectId: string): Promise<void> {
await tx.superTabProjectRules.deleteMany({ where: { tabId, projectId } })
}