import { WorkspaceInstructionPolicySnapshot } from "@opengeni/contracts"; import type { WorkspaceInstructionPolicyActivationResponse, WorkspaceInstructionPolicyDiffResponse, WorkspaceInstructionPolicyDraftProvenanceSource, WorkspaceInstructionPolicyHead, WorkspaceInstructionPolicyListQuery, WorkspaceInstructionPolicyListResponse, WorkspaceInstructionPolicyOnboardingProposal, WorkspaceInstructionPolicyOnboardingProposalListQuery, WorkspaceInstructionPolicyOnboardingProposalListResponse, WorkspaceInstructionPolicyProvenanceSource, WorkspaceInstructionPolicyRevision, WorkspaceInstructionPolicyTarget } from "@opengeni/contracts"; import type { Database } from "./database.js"; type DraftRequestInput = WorkspaceInstructionPolicyTarget & { operationId: string; accountId: string; workspaceId: string; content: string; provenanceSource: WorkspaceInstructionPolicyProvenanceSource; provenanceSourceId: string | null; supersedesRevisionId: string | null; createdBySubjectId: string; }; type CallerDraftInput = Omit & { operationId?: string; provenanceSource: WorkspaceInstructionPolicyDraftProvenanceSource; }; type ImportLegacyRequestInput = { operationId: string; accountId: string; workspaceId: string; createdBySubjectId: string; supersedesRevisionId: string | null; }; type OnboardingProposalRequestInput = WorkspaceInstructionPolicyTarget & { operationId: string; accountId: string; workspaceId: string; content: string; sourceId: string; sourceVersion: string; confidenceBps: number; expectedCurrentRevisionId: string | null; expectedActivationVersion: number; createdBySubjectId: string; }; export type WorkspaceInstructionPolicyKnowledgeProposalInput = WorkspaceInstructionPolicyTarget & { operationId: string; accountId: string; workspaceId: string; content: string; knowledgeProposalId: string; knowledgeProposalContentHash: string; confidenceBps: number; expectedCurrentRevisionId: string | null; expectedActivationVersion: number; createdBySubjectId: string; }; export declare class WorkspaceInstructionPolicyConflictError extends Error { readonly currentHead: WorkspaceInstructionPolicyHead | null; readonly name = "WorkspaceInstructionPolicyConflictError"; readonly code = "WORKSPACE_INSTRUCTION_POLICY_CONFLICT"; constructor(currentHead: WorkspaceInstructionPolicyHead | null); } export declare class WorkspaceInstructionPolicyOperationReuseError extends Error { readonly name = "WorkspaceInstructionPolicyOperationReuseError"; readonly code = "WORKSPACE_INSTRUCTION_POLICY_OPERATION_REUSED"; constructor(); } export declare class WorkspaceInstructionPolicyNotFoundError extends Error { readonly name = "WorkspaceInstructionPolicyNotFoundError"; constructor(message?: string); } export declare class WorkspaceInstructionPolicyInvalidOperationError extends Error { readonly name = "WorkspaceInstructionPolicyInvalidOperationError"; } export declare class WorkspaceInstructionPolicyLegacyUnavailableError extends Error { readonly name = "WorkspaceInstructionPolicyLegacyUnavailableError"; constructor(); } export declare class WorkspaceInstructionPolicyOnboardingProposalContentError extends Error { readonly code: "WORKSPACE_INSTRUCTION_POLICY_ONBOARDING_PROPOSAL_EMPTY" | "WORKSPACE_INSTRUCTION_POLICY_ONBOARDING_PROPOSAL_OVERSIZED"; readonly name = "WorkspaceInstructionPolicyOnboardingProposalContentError"; constructor(code: "WORKSPACE_INSTRUCTION_POLICY_ONBOARDING_PROPOSAL_EMPTY" | "WORKSPACE_INSTRUCTION_POLICY_ONBOARDING_PROPOSAL_OVERSIZED"); } /** * The exact diagnostic `activate_human_confirmed_learning_decision` raises when * a proposal's compare-and-set baseline no longer matches the live head. It is * matched by message because the SQLSTATE (40001) is shared with other * conflicts. Kept here so the SQL and the code that keys recovery off it cannot * drift apart silently - the migration test pins the same string. */ export declare const INSTRUCTION_POLICY_STALE_BASELINE_DIAGNOSTIC = "instruction-policy proposal baseline is stale"; export declare class WorkspaceInstructionPolicyOnboardingProposalStaleError extends Error { readonly currentHead: WorkspaceInstructionPolicyHead | null; readonly name = "WorkspaceInstructionPolicyOnboardingProposalStaleError"; readonly code = "WORKSPACE_INSTRUCTION_POLICY_ONBOARDING_PROPOSAL_STALE"; constructor(currentHead: WorkspaceInstructionPolicyHead | null); } export declare class WorkspaceInstructionPolicyOnboardingProposalConflictError extends Error { readonly existingProposalId: string; readonly existingDraftRevisionId: string; readonly name = "WorkspaceInstructionPolicyOnboardingProposalConflictError"; readonly code = "WORKSPACE_INSTRUCTION_POLICY_ONBOARDING_PROPOSAL_CONFLICT"; constructor(existingProposalId: string, existingDraftRevisionId: string); } export declare class WorkspaceInstructionPolicySnapshotAuthorityError extends Error { readonly name = "WorkspaceInstructionPolicySnapshotAuthorityError"; } export type WorkspaceInstructionPolicyAttemptClaims = { accountId: string; workspaceId: string; sessionId: string; turnId: string; attemptId: string; executionGeneration: number; }; export type WorkspaceStateAcceptedAttemptGovernance = { attemptId: string; executionGeneration: number; acceptedAt: string; policySnapshot: WorkspaceInstructionPolicySnapshot | null; preferenceSnapshot: { id: string; descriptorHash: string; descriptors: Array<{ id: string; revisionId: string; contentHash: string; activeVersion: number; scope: "organization" | "workspace" | "user"; }>; truncated: boolean; createdAt: string; } | null; }; /** * Inspect one immutable accepted attempt only when the authenticated caller is * exactly its frozen initiating human. The null result deliberately conflates * absence, cross-account/workspace lookup, and another subject's attempt. */ export declare function getWorkspaceStateAcceptedAttemptGovernance(db: Database, input: { accountId: string; workspaceId: string; subjectId: string; attemptId: string; }): Promise; export declare function createWorkspaceInstructionPolicyDraft(db: Database, input: CallerDraftInput): Promise; /** Import only the stored legacy override; never materialize a deployment default or activate it. */ export declare function importLegacyWorkspaceInstructionPolicyDraft(db: Database, input: Omit & { operationId?: string; }): Promise; export declare function createWorkspaceInstructionPolicyOnboardingProposal(db: Database, input: Omit & { operationId?: string; }): Promise; /** * Materialize an immutable Knowledge change proposal as an inactive instruction * draft. Human activation remains exclusively in the existing lifecycle API. */ export declare function createWorkspaceInstructionPolicyKnowledgeProposal(db: Database, input: WorkspaceInstructionPolicyKnowledgeProposalInput): Promise; /** * Add a successor instruction-policy proposal for a knowledge proposal whose * existing proposal was left behind by a moved head. * * This exists for one case: the human already answered "save", and only then * did the head move. Re-asking them would be the alternative. The successor * reuses the exact immutable source facts of the stranded proposal - same * knowledge proposal id, same content hash, same target, same author - so the * human's confirmation, which is bound to the knowledge proposal id, still * describes precisely what gets activated. Only the compare-and-set baseline * and the draft's place in the revision chain move forward. * * Returns the existing proposal unchanged when it is already current, so a * retry of the same confirmation is idempotent. */ export declare function rebaselineWorkspaceInstructionPolicyKnowledgeProposal(db: Database, input: { accountId: string; workspaceId: string; knowledgeProposalId: string; operationId: string; initiatingHumanSubjectId: string; }): Promise; export declare function listWorkspaceInstructionPolicyOnboardingProposals(db: Database, workspaceId: string, query: WorkspaceInstructionPolicyOnboardingProposalListQuery): Promise; export declare function listWorkspaceInstructionPolicyRevisions(db: Database, workspaceId: string, query: WorkspaceInstructionPolicyListQuery): Promise; export declare function getWorkspaceInstructionPolicyRevision(db: Database, workspaceId: string, revisionId: string): Promise; /** Deterministic, dependency-free unified line diff; large inputs degrade to a full replacement. */ export declare function diffWorkspaceInstructionPolicyContent(from: WorkspaceInstructionPolicyRevision, to: WorkspaceInstructionPolicyRevision): string; export declare function diffWorkspaceInstructionPolicyRevisions(db: Database, workspaceId: string, input: { fromRevisionId: string; toRevisionId: string; }): Promise; export declare function activateWorkspaceInstructionPolicyRevision(db: Database, input: { operationId?: string; accountId: string; workspaceId: string; revisionId: string; expectedCurrentRevisionId: string | null; expectedActivationVersion?: number; actorSubjectId: string; reason: string; }): Promise; export declare function rollbackWorkspaceInstructionPolicyRevision(db: Database, input: { operationId?: string; accountId: string; workspaceId: string; targetRevisionId: string; expectedCurrentRevisionId: string; expectedActivationVersion?: number; actorSubjectId: string; reason: string; }): Promise; /** * Freeze (or replay) the exact active charter/global/role policy set for one * accepted attempt, then resolve only those immutable revisions. The snapshot * stores bounded descriptors/hashes; full policy text remains in the revision * table and cannot be widened by a later head activation. */ export declare function getOrCreateWorkspaceInstructionPolicySnapshot(db: Database, claims: WorkspaceInstructionPolicyAttemptClaims): Promise<{ id: string; workspaceId: string; sessionId: string; turnId: string; attemptId: string; executionGeneration: number; policyRole: string | null; roleSource: "invalid_metadata_fallback" | "metadata_fallback" | "none" | "session_binding"; entryHash: string; createdAt: string; entries: { kind: "charter" | "policy"; scope: "global" | "role"; roleKey: string | null; revisionId: string; revision: number; contentHash: string; activationVersion: number; activatedAt: string; provenance: { source: "agent_learning" | "human" | "knowledge_proposal" | "legacy_import" | "onboarding"; sourceIdHash: string | null; }; content: string; }[]; }>; /** * Current activation baseline for one instruction-policy target: the active * head's revision (or null) and the CAS activation version that a new draft * proposal must expect. A deactivated-to-null target keeps its monotonic * boundary, so the version is the max of the head and latest deactivation. * Read-only under workspace RLS; used by `remember` to bind a user-directed * rule to the exact current baseline instead of assuming an empty workspace. */ export declare function getWorkspaceInstructionPolicyBaseline(db: Database, input: { workspaceId: string; target: WorkspaceInstructionPolicyTarget; }): Promise<{ expectedCurrentRevisionId: string | null; expectedActivationVersion: number; }>; export {};