certal 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/.claude-plugin/plugin.json +11 -0
  2. package/CHANGELOG.md +32 -0
  3. package/LICENSE +21 -0
  4. package/README.md +277 -2
  5. package/SECURITY.md +36 -0
  6. package/bin/certal.mjs +12 -0
  7. package/certal.schema.json +156 -0
  8. package/dist/adapters/approval-channel.d.ts +44 -0
  9. package/dist/adapters/approval-channel.js +74 -0
  10. package/dist/adapters/commands.d.ts +73 -0
  11. package/dist/adapters/commands.js +185 -0
  12. package/dist/adapters/contracts.d.ts +59 -0
  13. package/dist/adapters/contracts.js +1 -0
  14. package/dist/adapters/file-watcher.d.ts +53 -0
  15. package/dist/adapters/file-watcher.js +142 -0
  16. package/dist/adapters/in-memory.d.ts +53 -0
  17. package/dist/adapters/in-memory.js +165 -0
  18. package/dist/adapters/index.d.ts +8 -0
  19. package/dist/adapters/index.js +8 -0
  20. package/dist/adapters/manifest.d.ts +105 -0
  21. package/dist/adapters/manifest.js +438 -0
  22. package/dist/adapters/operator-auth.d.ts +47 -0
  23. package/dist/adapters/operator-auth.js +72 -0
  24. package/dist/adapters/tool-sdk.d.ts +150 -0
  25. package/dist/adapters/tool-sdk.js +336 -0
  26. package/dist/cli/index.d.ts +1 -0
  27. package/dist/cli/index.js +1 -0
  28. package/dist/cli/main.d.ts +1 -0
  29. package/dist/cli/main.js +386 -0
  30. package/dist/kernel/common.d.ts +46 -0
  31. package/dist/kernel/common.js +238 -0
  32. package/dist/kernel/crypto.d.ts +35 -0
  33. package/dist/kernel/crypto.js +54 -0
  34. package/dist/kernel/effects.d.ts +4 -0
  35. package/dist/kernel/effects.js +178 -0
  36. package/dist/kernel/index.d.ts +9 -0
  37. package/dist/kernel/index.js +9 -0
  38. package/dist/kernel/jcs.d.ts +24 -0
  39. package/dist/kernel/jcs.js +93 -0
  40. package/dist/kernel/leases.d.ts +11 -0
  41. package/dist/kernel/leases.js +86 -0
  42. package/dist/kernel/step.d.ts +10 -0
  43. package/dist/kernel/step.js +967 -0
  44. package/dist/kernel/types.d.ts +538 -0
  45. package/dist/kernel/types.js +1 -0
  46. package/dist/kernel/undo.d.ts +4 -0
  47. package/dist/kernel/undo.js +180 -0
  48. package/dist/kernel/validation.d.ts +13 -0
  49. package/dist/kernel/validation.js +307 -0
  50. package/dist/kernel/witness.d.ts +34 -0
  51. package/dist/kernel/witness.js +279 -0
  52. package/dist/mcp/host.d.ts +141 -0
  53. package/dist/mcp/host.js +517 -0
  54. package/dist/mcp/index.d.ts +2 -0
  55. package/dist/mcp/index.js +2 -0
  56. package/dist/mcp/server.d.ts +70 -0
  57. package/dist/mcp/server.js +508 -0
  58. package/dist/runtime/dp1.d.ts +48 -0
  59. package/dist/runtime/dp1.js +229 -0
  60. package/dist/runtime/engine.d.ts +86 -0
  61. package/dist/runtime/engine.js +320 -0
  62. package/dist/runtime/index.d.ts +4 -0
  63. package/dist/runtime/index.js +4 -0
  64. package/dist/runtime/outbox.d.ts +69 -0
  65. package/dist/runtime/outbox.js +238 -0
  66. package/dist/runtime/store.d.ts +204 -0
  67. package/dist/runtime/store.js +746 -0
  68. package/package.json +91 -4
  69. package/presets/exchange-firewall.json +40 -0
  70. package/presets/sdk.json +32 -0
  71. package/presets/solver.json +22 -0
  72. package/presets/vibeops.json +34 -0
  73. package/skills/certal/SKILL.md +49 -0
@@ -0,0 +1,165 @@
1
+ import { computeScopeHash, hashCanonical, } from '../kernel/index.js';
2
+ /**
3
+ * Reference in-memory adapter. It implements slots S1, S2, S4 and the S5 transport (OutboxTransport)
4
+ * and the checkUndoDependencies hook, which finds dependent child objects owned by a human.
5
+ */
6
+ export class InMemoryReferenceAdapter {
7
+ externalRecords = new Map();
8
+ behaviors = new Map();
9
+ witnessOverrides = new Map();
10
+ deliverCalls = 0;
11
+ reconcileCalls = 0;
12
+ compensateCalls = 0;
13
+ setRecipientBehavior(recipientId, behavior) {
14
+ this.behaviors.set(recipientId, behavior);
15
+ }
16
+ setWitnessOverride(stage, override) {
17
+ this.witnessOverrides.set(stage, override);
18
+ }
19
+ clearWitnessOverrides() {
20
+ this.witnessOverrides.clear();
21
+ }
22
+ getExternalRecord(effectId) {
23
+ return this.externalRecords.get(effectId);
24
+ }
25
+ /**
26
+ * Deterministic hook: blocks the undo of object A when an active child object B exists that
27
+ * a human created or modified, with parentObjectId === A.
28
+ */
29
+ hooks = {
30
+ checkUndoDependencies: (state, patch) => {
31
+ for (const slot of Object.values(state.slots)) {
32
+ if (slot.parentObjectId === patch.objectId &&
33
+ slot.owner.kind === 'Human' &&
34
+ slot.value !== null &&
35
+ slot.value !== undefined) {
36
+ return {
37
+ blocked: true,
38
+ reason: `Dependent child object ${slot.objectId}.${slot.field} created by ${slot.owner.actorId}`,
39
+ };
40
+ }
41
+ }
42
+ return { blocked: false };
43
+ },
44
+ };
45
+ readScope(state, scopeId, classId) {
46
+ const values = {};
47
+ let maxRev = 0;
48
+ const prefix = `${scopeId}\0`;
49
+ for (const [k, slot] of Object.entries(state.slots)) {
50
+ if (!k.startsWith(prefix))
51
+ continue;
52
+ if (classId !== '*' && slot.classId !== classId)
53
+ continue;
54
+ values[`${slot.objectId}.${slot.field}`] = slot.value;
55
+ if (slot.rev > maxRev)
56
+ maxRev = slot.rev;
57
+ }
58
+ return {
59
+ scopeId,
60
+ classId,
61
+ revision: maxRev,
62
+ scopeHash: computeScopeHash(state, scopeId, classId),
63
+ values,
64
+ };
65
+ }
66
+ classifyReversibility(op) {
67
+ return op.reversibility;
68
+ }
69
+ freezeInputs(scopeId, rawInputs, confidence, nowMs) {
70
+ return {
71
+ schemaVersion: 1,
72
+ dataRef: `mem://${scopeId}/${nowMs}`,
73
+ hash: hashCanonical('input_snapshot', 1, { scopeId, rawInputs, confidence }),
74
+ capturedAtMs: nowMs,
75
+ confidence,
76
+ };
77
+ }
78
+ evaluate(state, snapshot) {
79
+ return {
80
+ hardViolations: [],
81
+ pinViolations: state.pins.filter((p) => p.kind === 'H').map((p) => p.ruleId),
82
+ objectiveMicroUnits: 0,
83
+ confidence: snapshot.confidence,
84
+ };
85
+ }
86
+ async runStage(intent) {
87
+ const ov = this.witnessOverrides.get(intent.stage);
88
+ return {
89
+ verifyId: intent.verifyId,
90
+ stage: intent.stage,
91
+ verdict: ov?.verdict ?? 'PASS',
92
+ cpuMs: ov?.cpuMs ?? 5,
93
+ nearMissPassed: ov?.nearMissPassed ?? true,
94
+ ...(ov?.optimization !== undefined ? { optimization: ov.optimization } : {}),
95
+ };
96
+ }
97
+ async deliver(record) {
98
+ this.deliverCalls += 1;
99
+ const behavior = this.behaviors.get(record.recipientId);
100
+ if (behavior?.refuseDeliver) {
101
+ return { status: 'REFUSED', reason: 'RECIPIENT_REJECTED' };
102
+ }
103
+ if (behavior?.dropResponseTimes && behavior.dropResponseTimes > 0) {
104
+ behavior.dropResponseTimes -= 1;
105
+ if (behavior.appliedOnTimeout !== false) {
106
+ this.externalRecords.set(record.effectId, {
107
+ effectId: record.effectId,
108
+ payload: JSON.parse(record.payloadJson),
109
+ version: 1,
110
+ compensated: false,
111
+ });
112
+ }
113
+ return { status: 'UNKNOWN', reason: 'RESPONSE_TIMEOUT' };
114
+ }
115
+ this.externalRecords.set(record.effectId, {
116
+ effectId: record.effectId,
117
+ payload: JSON.parse(record.payloadJson),
118
+ version: 1,
119
+ compensated: false,
120
+ });
121
+ return { status: 'APPLIED' };
122
+ }
123
+ async reconcile(record) {
124
+ this.reconcileCalls += 1;
125
+ const behavior = this.behaviors.get(record.recipientId);
126
+ if (behavior?.unknownReconcile) {
127
+ return { status: 'UNKNOWN', reason: 'RECONCILE_TIMEOUT' };
128
+ }
129
+ if (record.effectKind === 'Compensate') {
130
+ const orig = this.externalRecords.get(record.batchId);
131
+ if (!orig) {
132
+ return { status: 'REFUSED', reason: 'ORIGINAL_EFFECT_NOT_FOUND' };
133
+ }
134
+ return orig.compensated
135
+ ? { status: 'APPLIED' }
136
+ : { status: 'REFUSED', reason: 'COMPENSATION_NOT_APPLIED' };
137
+ }
138
+ const existing = this.externalRecords.get(record.effectId);
139
+ if (existing) {
140
+ return { status: 'APPLIED' };
141
+ }
142
+ return { status: 'REFUSED', reason: 'EFFECT_ABSENT_AT_RECIPIENT' };
143
+ }
144
+ async compensate(record) {
145
+ this.compensateCalls += 1;
146
+ const behavior = this.behaviors.get(record.recipientId);
147
+ if (behavior?.refuseCompensate) {
148
+ return { status: 'REFUSED', reason: 'COMPENSATION_REFUSED' };
149
+ }
150
+ const orig = this.externalRecords.get(record.batchId);
151
+ if (!orig) {
152
+ return { status: 'REFUSED', reason: 'ORIGINAL_EFFECT_NOT_FOUND' };
153
+ }
154
+ if (orig.version !== 1) {
155
+ return { status: 'REFUSED', reason: 'CAS_VERSION_MISMATCH' };
156
+ }
157
+ this.externalRecords.set(record.batchId, {
158
+ ...orig,
159
+ version: orig.version + 1,
160
+ compensated: true,
161
+ compensationPayload: JSON.parse(record.payloadJson),
162
+ });
163
+ return { status: 'APPLIED' };
164
+ }
165
+ }
@@ -0,0 +1,8 @@
1
+ export * from './contracts.ts';
2
+ export * from './in-memory.ts';
3
+ export * from './tool-sdk.ts';
4
+ export * from './manifest.ts';
5
+ export * from './commands.ts';
6
+ export * from './operator-auth.ts';
7
+ export * from './approval-channel.ts';
8
+ export * from './file-watcher.ts';
@@ -0,0 +1,8 @@
1
+ export * from './contracts.js';
2
+ export * from './in-memory.js';
3
+ export * from './tool-sdk.js';
4
+ export * from './manifest.js';
5
+ export * from './commands.js';
6
+ export * from './operator-auth.js';
7
+ export * from './approval-channel.js';
8
+ export * from './file-watcher.js';
@@ -0,0 +1,105 @@
1
+ import type { PinKind, PolicyConfig, Reversibility, WitnessStage } from '../kernel/index.ts';
2
+ /**
3
+ * The declarative manifest `certal.json`.
4
+ *
5
+ * A manifest is plain JSON. Manifests found on the way from the working directory up to the
6
+ * filesystem root are merged, outermost first. A manifest in a subdirectory may only tighten
7
+ * what its parents declare: it may add pins and lower budgets, and it may never remove a pin
8
+ * or relax a limit. A merge that would relax anything fails with the list of violations.
9
+ */
10
+ export declare const MANIFEST_FILE = "certal.json";
11
+ export type ManifestProfile = 'sdk' | 'exchange' | 'vibeops' | 'solver' | 'custom';
12
+ export type Command = readonly string[];
13
+ export interface ManifestScope {
14
+ readonly id: string;
15
+ readonly classId: string;
16
+ /** Glob patterns, relative to the manifest directory, of the files that belong to the scope. */
17
+ readonly paths: readonly string[];
18
+ }
19
+ export interface ManifestPin {
20
+ readonly id: string;
21
+ readonly kind: PinKind;
22
+ readonly scopeId: string;
23
+ readonly classId: string;
24
+ readonly rule: string;
25
+ readonly costPenaltyMicroUnits?: number;
26
+ }
27
+ export interface ManifestTool {
28
+ readonly reversibility: Reversibility;
29
+ readonly supportsCas: boolean;
30
+ readonly preflightCommand?: Command;
31
+ readonly deliverCommand?: Command;
32
+ readonly reconcileCommand?: Command;
33
+ readonly compensateCommand?: Command;
34
+ }
35
+ export interface ManifestWitnessStage {
36
+ /** The check to run. Exit code 0 is PASS, 1 is FAIL, anything else is UNKNOWN. */
37
+ readonly command?: Command;
38
+ /** `accept` passes the stage without a check. Use it only where no check exists yet. */
39
+ readonly builtin?: 'accept';
40
+ readonly budgetCpuMs: number;
41
+ readonly requireNearMiss: boolean;
42
+ readonly nearMissProbeCommand?: Command;
43
+ }
44
+ export interface ManifestOperator {
45
+ readonly humanId: string;
46
+ /** Ed25519 public keys in PEM format, by key id. */
47
+ readonly publicKeys: Readonly<Record<string, string>>;
48
+ }
49
+ export interface ManifestApprovals {
50
+ readonly channel: 'local-cli' | 'webhook';
51
+ readonly url?: string;
52
+ }
53
+ export interface CertalManifest {
54
+ readonly version: 1;
55
+ readonly profile: ManifestProfile;
56
+ readonly stateDir?: string;
57
+ readonly operator?: ManifestOperator;
58
+ readonly policy?: Partial<PolicyConfig>;
59
+ readonly scopes?: readonly ManifestScope[];
60
+ readonly pins?: readonly ManifestPin[];
61
+ readonly tools?: Readonly<Record<string, ManifestTool>>;
62
+ readonly witness?: Partial<Readonly<Record<WitnessStage, ManifestWitnessStage>>>;
63
+ readonly approvals?: ManifestApprovals;
64
+ readonly watch?: boolean;
65
+ /** Set in the outermost manifest to let subdirectories declare tools of their own. Default: false. */
66
+ readonly allowSubdirectoryTools?: boolean;
67
+ }
68
+ export interface ResolvedManifest {
69
+ readonly profile: ManifestProfile;
70
+ /** Directory of the innermost manifest. Commands run here and the state directory lives here. */
71
+ readonly baseDir: string;
72
+ readonly stateDir: string;
73
+ /** Manifest files that were merged, outermost first. */
74
+ readonly sources: readonly string[];
75
+ readonly operator: ManifestOperator | null;
76
+ readonly policy: Partial<PolicyConfig>;
77
+ readonly scopes: readonly ManifestScope[];
78
+ readonly pins: readonly ManifestPin[];
79
+ readonly tools: Readonly<Record<string, ManifestTool>>;
80
+ readonly witness: Partial<Readonly<Record<WitnessStage, ManifestWitnessStage>>>;
81
+ readonly approvals: ManifestApprovals;
82
+ readonly watch: boolean;
83
+ readonly allowSubdirectoryTools: boolean;
84
+ }
85
+ export declare class ManifestError extends Error {
86
+ readonly problems: readonly string[];
87
+ constructor(problems: readonly string[]);
88
+ }
89
+ /** Checks the shape of one manifest. Returns the list of problems, empty when the manifest is valid. */
90
+ export declare function validateManifest(raw: unknown, label?: string): string[];
91
+ /** Manifest files from `startDir` up to the filesystem root, outermost first. */
92
+ export declare function findManifestFiles(startDir: string): string[];
93
+ /**
94
+ * Loads and merges the manifests that apply to `startDir`.
95
+ * Throws ManifestError when a file is invalid, when a subdirectory relaxes a parent,
96
+ * or when a required witness stage has no declaration.
97
+ */
98
+ export declare function loadManifest(startDir: string): ResolvedManifest;
99
+ /**
100
+ * Lists the places where `next` would relax the policy `current`. Used when a manifest changes
101
+ * after the log has started: a tighter policy is applied, a looser one is refused.
102
+ */
103
+ export declare function policyRelaxations(current: PolicyConfig, next: Partial<PolicyConfig>): string[];
104
+ /** The part of `next` that differs from `current`. Empty when the manifest policy is already in force. */
105
+ export declare function policyDifference(current: PolicyConfig, next: Partial<PolicyConfig>): Partial<PolicyConfig>;