@tangleai/outcomes 0.24.1

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 (65) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/LICENSE +21 -0
  3. package/README.md +142 -0
  4. package/docs/ADAPTERS.md +215 -0
  5. package/package.json +80 -0
  6. package/schemas/direction-delta.schema.json +55 -0
  7. package/schemas/exact-match.schema.json +88 -0
  8. package/schemas/outcomes.contract.json +4084 -0
  9. package/schemas/outcomes.schema.json +3936 -0
  10. package/src/adapters/direction-delta.d.ts +2 -0
  11. package/src/adapters/direction-delta.gen.d.ts +13 -0
  12. package/src/adapters/direction-delta.gen.js +3 -0
  13. package/src/adapters/direction-delta.js +29 -0
  14. package/src/adapters/exact-match.d.ts +2 -0
  15. package/src/adapters/exact-match.gen.d.ts +39 -0
  16. package/src/adapters/exact-match.gen.js +3 -0
  17. package/src/adapters/exact-match.js +40 -0
  18. package/src/adapters.d.ts +50 -0
  19. package/src/adapters.js +1 -0
  20. package/src/contract.d.ts +3285 -0
  21. package/src/contract.js +6 -0
  22. package/src/domain.d.ts +19 -0
  23. package/src/domain.js +45 -0
  24. package/src/errors.d.ts +23 -0
  25. package/src/errors.js +11 -0
  26. package/src/evaluation.d.ts +49 -0
  27. package/src/evaluation.js +174 -0
  28. package/src/handlers.d.ts +15 -0
  29. package/src/handlers.js +18 -0
  30. package/src/history.d.ts +4 -0
  31. package/src/history.js +56 -0
  32. package/src/identity.d.ts +9 -0
  33. package/src/identity.js +17 -0
  34. package/src/index.d.ts +15 -0
  35. package/src/index.js +9 -0
  36. package/src/operations.d.ts +16 -0
  37. package/src/operations.js +108 -0
  38. package/src/outcomes.contracts.gen.d.ts +1767 -0
  39. package/src/outcomes.contracts.gen.js +3 -0
  40. package/src/persistence.d.ts +10 -0
  41. package/src/persistence.js +88 -0
  42. package/src/projection.d.ts +13 -0
  43. package/src/projection.js +59 -0
  44. package/src/promotion.d.ts +17 -0
  45. package/src/promotion.js +96 -0
  46. package/src/proposal-operation.d.ts +17 -0
  47. package/src/proposal-operation.js +110 -0
  48. package/src/proposer.d.ts +107 -0
  49. package/src/proposer.js +89 -0
  50. package/src/refinement.d.ts +40 -0
  51. package/src/refinement.js +167 -0
  52. package/src/resolution.d.ts +12 -0
  53. package/src/resolution.js +62 -0
  54. package/src/schema.d.ts +8 -0
  55. package/src/schema.js +34 -0
  56. package/src/scoring.d.ts +15 -0
  57. package/src/scoring.js +37 -0
  58. package/src/service-context.d.ts +50 -0
  59. package/src/service-context.js +81 -0
  60. package/src/service.d.ts +22 -0
  61. package/src/service.js +159 -0
  62. package/src/store.d.ts +42 -0
  63. package/src/store.js +70 -0
  64. package/src/transitions.d.ts +7 -0
  65. package/src/transitions.js +56 -0
@@ -0,0 +1,2 @@
1
+ import type { OutcomeAdapter } from '../adapters.ts';
2
+ export declare function createDirectionDeltaAdapter(): Promise<OutcomeAdapter>;
@@ -0,0 +1,13 @@
1
+ export interface Input {
2
+ base: number;
3
+ }
4
+ export interface Output {
5
+ predicted: number;
6
+ }
7
+ export interface Resolution {
8
+ actual: number;
9
+ }
10
+ export interface Artifact {
11
+ offset: number;
12
+ }
13
+ export type DirectionDelta = Artifact;
@@ -0,0 +1,3 @@
1
+ // Generated by @jarenjs/emit from packages/outcomes/schemas/direction-delta.schema.json.
2
+ // Do not edit: regenerate instead.
3
+ export {};
@@ -0,0 +1,29 @@
1
+ /** A finite scalar correction policy with a strict, unrounded tolerance. */
2
+ import schema from '../../schemas/direction-delta.schema.json' with { type: 'json' };
3
+ import { adapterIdentity, domainValidator } from "../domain.js";
4
+ import { reject } from "../errors.js";
5
+ export async function createDirectionDeltaAdapter() {
6
+ const schemas = schema.$defs;
7
+ const input = domainValidator(schemas.input), output = domainValidator(schemas.output);
8
+ const resolution = domainValidator(schemas.resolution), artifact = domainValidator(schemas.artifact);
9
+ return Object.freeze({
10
+ identity: await adapterIdentity('direction-delta/v1', schemas, { tolerance: 0.05, comparison: 'strict', zero: 'nonnegative', interpreter: 'base+offset' }),
11
+ schemas, staticPayload: { offset: 0 }, validatePayload: () => [],
12
+ interpret(raw, payload) {
13
+ const { base } = input(raw), { offset } = artifact(payload);
14
+ const predicted = base + offset;
15
+ if (!Number.isFinite(predicted))
16
+ reject('OUTC1010', 'Scalar addition is not finite.');
17
+ return { predicted };
18
+ },
19
+ score(raw, evidence) {
20
+ const { predicted } = output(raw), { actual } = resolution(evidence);
21
+ const sameDirection = (predicted >= 0 && actual >= 0) || (predicted < 0 && actual < 0);
22
+ const difference = Math.abs(predicted - actual);
23
+ return {
24
+ outcome: sameDirection ? difference < 0.05 ? 'success' : 'partial' : 'failure',
25
+ diagnostics: { sameDirection, difference: Number.isFinite(difference) ? difference : null, differenceOverflow: !Number.isFinite(difference) },
26
+ };
27
+ },
28
+ });
29
+ }
@@ -0,0 +1,2 @@
1
+ import type { OutcomeAdapter } from '../adapters.ts';
2
+ export declare function createExactMatchAdapter(): Promise<OutcomeAdapter>;
@@ -0,0 +1,39 @@
1
+ export interface Input {
2
+ /**
3
+ * Schema constraints this type cannot express: minLength=1, pattern="\\S"
4
+ */
5
+ token: string;
6
+ }
7
+ export interface Output {
8
+ /**
9
+ * Schema constraints this type cannot express: minLength=1, pattern="\\S"
10
+ */
11
+ label: string;
12
+ }
13
+ export interface Resolution {
14
+ /**
15
+ * Schema constraints this type cannot express: minLength=1, pattern="\\S"
16
+ */
17
+ label: string;
18
+ }
19
+ export interface ArtifactRulesItem {
20
+ /**
21
+ * Schema constraints this type cannot express: minLength=1, pattern="\\S"
22
+ */
23
+ prefix: string;
24
+ /**
25
+ * Schema constraints this type cannot express: minLength=1, pattern="\\S"
26
+ */
27
+ label: string;
28
+ }
29
+ export interface Artifact {
30
+ /**
31
+ * Schema constraints this type cannot express: minLength=1, pattern="\\S"
32
+ */
33
+ fallbackLabel: string;
34
+ /**
35
+ * Schema constraints this type cannot express: maxItems=16
36
+ */
37
+ rules: Array<ArtifactRulesItem>;
38
+ }
39
+ export type ExactMatch = Artifact;
@@ -0,0 +1,3 @@
1
+ // Generated by @jarenjs/emit from packages/outcomes/schemas/exact-match.schema.json.
2
+ // Do not edit: regenerate instead.
3
+ export {};
@@ -0,0 +1,40 @@
1
+ /** Exact labels and a bounded, deterministic prefix interpreter. */
2
+ import schema from '../../schemas/exact-match.schema.json' with { type: 'json' };
3
+ import { adapterIdentity, domainValidator } from "../domain.js";
4
+ import { issue, reject } from "../errors.js";
5
+ function codePointOrder(a, b) {
6
+ const x = Array.from(a, c => c.codePointAt(0)), y = Array.from(b, c => c.codePointAt(0));
7
+ for (let i = 0; i < Math.min(x.length, y.length); i++)
8
+ if (x[i] !== y[i])
9
+ return x[i] - y[i];
10
+ return x.length - y.length;
11
+ }
12
+ export async function createExactMatchAdapter() {
13
+ const schemas = schema.$defs;
14
+ const input = domainValidator(schemas.input), output = domainValidator(schemas.output);
15
+ const resolution = domainValidator(schemas.resolution), artifact = domainValidator(schemas.artifact);
16
+ const normalize = (raw) => {
17
+ const payload = artifact(raw);
18
+ return { ...payload, rules: [...payload.rules].sort((a, b) => Array.from(b.prefix).length - Array.from(a.prefix).length || codePointOrder(a.prefix, b.prefix)) };
19
+ };
20
+ return Object.freeze({
21
+ identity: await adapterIdentity('exact-match/v1', schemas, { scorer: 'case-sensitive equality', interpreter: 'longest Unicode code-point prefix; code-point tie order', normalization: 'none' }),
22
+ schemas, staticPayload: { fallbackLabel: 'unknown', rules: [] },
23
+ normalizePayload: raw => artifact(normalize(raw)),
24
+ validatePayload(raw) {
25
+ const payload = artifact(raw);
26
+ return new Set(payload.rules.map(r => r.prefix)).size === payload.rules.length ? [] : [issue('OUTC1010', 'Prefixes must be unique.')];
27
+ },
28
+ interpret(raw, payload) {
29
+ const { token } = input(raw), p = normalize(payload);
30
+ if (new Set(p.rules.map(r => r.prefix)).size !== p.rules.length)
31
+ reject('OUTC1010', 'Prefixes must be unique.');
32
+ return { label: p.rules.find(r => token.startsWith(r.prefix))?.label ?? p.fallbackLabel };
33
+ },
34
+ score(raw, evidence) {
35
+ const a = output(raw), b = resolution(evidence);
36
+ const equal = a.label === b.label;
37
+ return { outcome: equal ? 'success' : 'failure', diagnostics: { equal } };
38
+ },
39
+ });
40
+ }
@@ -0,0 +1,50 @@
1
+ /** Host-owned domain schemas and pure evaluators; no provider or domain registry globals. */
2
+ import type { Json, AdapterIdentity, Scope, Source, SourceRef, Issue, EvaluationSlot, ProposalReply } from './outcomes.contracts.gen.ts';
3
+ import type { RunIdentity } from '@tangleai/config';
4
+ export type { EvaluationSlot } from './outcomes.contracts.gen.ts';
5
+ export interface OutcomeAdapter {
6
+ readonly identity: AdapterIdentity;
7
+ readonly schemas: {
8
+ input: object;
9
+ output: object;
10
+ resolution: object;
11
+ artifact: object;
12
+ };
13
+ readonly staticPayload: Json;
14
+ score(output: Json, resolution: Json): {
15
+ outcome: 'success' | 'partial' | 'failure';
16
+ diagnostics: Json;
17
+ };
18
+ interpret(input: Json, payload: Json): Json;
19
+ validatePayload(payload: Json): Issue[];
20
+ normalizePayload?(payload: Json): Json;
21
+ }
22
+ export interface EvidenceResolver {
23
+ readonly revision: string;
24
+ resolve(reference: SourceRef, scope: Scope): Promise<Source | undefined>;
25
+ }
26
+ export interface OutcomePrincipal {
27
+ id: string;
28
+ authorityId: string;
29
+ approve: boolean;
30
+ reconcile: boolean;
31
+ }
32
+ export interface OutcomeProposer {
33
+ readonly identity: RunIdentity;
34
+ propose(input: Json, hooks: {
35
+ onDispatch(requestDigest: string): Promise<void>;
36
+ }): Promise<ProposalReply>;
37
+ }
38
+ export interface OutcomeHost {
39
+ principal?: OutcomePrincipal;
40
+ scope: Scope;
41
+ adapters: readonly OutcomeAdapter[];
42
+ resolver: EvidenceResolver;
43
+ authorizeMemoryIds(ids: readonly string[], scope: Scope): Promise<{
44
+ allowed: boolean;
45
+ authorizationId: string;
46
+ }>;
47
+ evaluationSlot?(slotId: string, versionId: string, scope: Scope): Promise<EvaluationSlot | undefined>;
48
+ proposer?: OutcomeProposer;
49
+ resolveConfiguration?(identityId: string): Promise<RunIdentity | undefined>;
50
+ }
@@ -0,0 +1 @@
1
+ export {};