import { type TaskNoteKind as TaskNoteKindType } from "@opengeni/contracts"; import type { Database } from "./database.js"; export type TaskNoteAttemptClaims = { accountId: string; workspaceId: string; sessionId: string; turnId: string; attemptId: string; executionGeneration: number; }; export type CreateTaskNoteInput = TaskNoteAttemptClaims & { operationId: string; kind: TaskNoteKindType; text: string; expiresInDays: number; }; export type ArchiveTaskNoteInput = TaskNoteAttemptClaims & { operationId: string; noteId: string; expectedVersion: number; reason: string; }; export type ReplaceTaskNoteInput = TaskNoteAttemptClaims & { operationId: string; replacedNoteId: string; expectedReplacedVersion: number; replacementKind: TaskNoteKindType; replacementText: string; replacementExpiresInDays: number; reason: string; }; export type ListTaskNotesInput = TaskNoteAttemptClaims & { includeArchived?: boolean; limit?: number; }; export type TaskNoteOperation = "save" | "archive" | "replace" | "list"; /** * Why the task-note routines refused a call, from the SQLSTATE and fixed * message they raise: * - `active_note_limit`: the root task tree already holds the most active notes (54000). * - `invalid_input`: the routine's own input check failed (22023). * - `operation_conflict`: the operation ID was already used with other input or * by another attempt (23505 raised by the routine). * - `note_not_found`: the note is not in this task tree (P0002). * - `note_not_active`: the note was already archived, replaced or expired (40001 * raised by the lifecycle check). * - `attempt_not_authorized`: the caller is not the exact current attempt of a * visible session in the tree (42501). * - `transient`: the database briefly could not answer (lost connection, * deadlock, serialization or lock timeout, or a concurrent duplicate insert). */ export type TaskNoteRefusalReason = "active_note_limit" | "invalid_input" | "operation_conflict" | "note_not_found" | "note_not_active" | "attempt_not_authorized" | "transient"; /** * A task-note call the database refused, classified so callers can explain it * without the driver error. The message is fixed and never carries SQL, * parameters or note text; the original failure stays only as `cause`. */ export declare class TaskNoteRefusedError extends Error { readonly operation: TaskNoteOperation; readonly reason: TaskNoteRefusalReason; readonly sqlState: string | null; name: string; constructor(operation: TaskNoteOperation, reason: TaskNoteRefusalReason, sqlState: string | null, options?: { cause?: unknown; }); } /** * Classify a failed task-note call, or null when it is not a refusal these * routines are known to raise. Unknown failures keep their original error. */ export declare function classifyTaskNoteFailure(operation: TaskNoteOperation, error: unknown): TaskNoteRefusedError | null; export declare function createTaskNote(db: Database, input: CreateTaskNoteInput): Promise<{ note: { id: string; rootSessionId: string; kind: "artifact" | "blocker" | "decision" | "finding" | "handoff" | "ownership"; text: string; status: "active" | "archived"; version: number; expiresAt: string; createdAt: string; updatedAt: string; archivedAt: string | null; provenance: { actorKind: "human" | "service"; sourceSessionId: string; sourceTurnId: string; }; }; replayed: boolean; }>; export declare function archiveTaskNote(db: Database, input: ArchiveTaskNoteInput): Promise<{ note: { id: string; rootSessionId: string; kind: "artifact" | "blocker" | "decision" | "finding" | "handoff" | "ownership"; text: string; status: "active" | "archived"; version: number; expiresAt: string; createdAt: string; updatedAt: string; archivedAt: string | null; provenance: { actorKind: "human" | "service"; sourceSessionId: string; sourceTurnId: string; }; }; replayed: boolean; }>; /** * Atomically archive one exact active note and create its immutable replacement. * Exact retries return the original linked receipt; changed input or authority fails. */ export declare function replaceTaskNote(db: Database, input: ReplaceTaskNoteInput): Promise<{ operationId: string; inputHash: string; replaces: { noteId: string; archivedVersion: 2; }; replacement: { id: string; rootSessionId: string; kind: "artifact" | "blocker" | "decision" | "finding" | "handoff" | "ownership"; text: string; status: "active" | "archived"; version: number; expiresAt: string; createdAt: string; updatedAt: string; archivedAt: string | null; provenance: { actorKind: "human" | "service"; sourceSessionId: string; sourceTurnId: string; }; }; replayed: boolean; }>; export declare function listTaskNotes(db: Database, input: ListTaskNotesInput): Promise<{ notes: { id: string; rootSessionId: string; kind: "artifact" | "blocker" | "decision" | "finding" | "handoff" | "ownership"; text: string; status: "active" | "archived"; version: number; expiresAt: string; createdAt: string; updatedAt: string; archivedAt: string | null; provenance: { actorKind: "human" | "service"; sourceSessionId: string; sourceTurnId: string; }; }[]; }>;