assignment-user-matcher 1.7.0 → 1.9.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 (42) hide show
  1. package/README.md +230 -14
  2. package/dist/index.d.ts +3 -2
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +5 -1
  5. package/dist/learning/auto-weights.d.ts +18 -8
  6. package/dist/learning/auto-weights.d.ts.map +1 -1
  7. package/dist/learning/auto-weights.js +120 -18
  8. package/dist/learning/features.d.ts +2 -2
  9. package/dist/learning/features.d.ts.map +1 -1
  10. package/dist/learning/features.js +10 -2
  11. package/dist/managers/LearningManager.d.ts +7 -0
  12. package/dist/managers/LearningManager.d.ts.map +1 -1
  13. package/dist/managers/LearningManager.js +85 -11
  14. package/dist/managers/WorkflowManager.d.ts +2 -0
  15. package/dist/managers/WorkflowManager.d.ts.map +1 -1
  16. package/dist/managers/WorkflowManager.js +3 -2
  17. package/dist/matcher.class.d.ts +121 -3
  18. package/dist/matcher.class.d.ts.map +1 -1
  19. package/dist/matcher.class.js +967 -16
  20. package/dist/queries/pagination.d.ts +5 -1
  21. package/dist/queries/pagination.d.ts.map +1 -1
  22. package/dist/queries/pagination.js +14 -3
  23. package/dist/schedule/policy.d.ts +35 -0
  24. package/dist/schedule/policy.d.ts.map +1 -0
  25. package/dist/schedule/policy.js +62 -0
  26. package/dist/scoring/match-score.d.ts +6 -0
  27. package/dist/scoring/match-score.d.ts.map +1 -1
  28. package/dist/scoring/match-score.js +15 -2
  29. package/dist/sla/policy.d.ts +49 -0
  30. package/dist/sla/policy.d.ts.map +1 -0
  31. package/dist/sla/policy.js +96 -0
  32. package/dist/tracing/decision-trace.d.ts.map +1 -1
  33. package/dist/tracing/decision-trace.js +4 -2
  34. package/dist/types/matcher.d.ts +422 -2
  35. package/dist/types/matcher.d.ts.map +1 -1
  36. package/dist/utils/keys.d.ts +10 -0
  37. package/dist/utils/keys.d.ts.map +1 -1
  38. package/dist/utils/keys.js +28 -0
  39. package/dist/validation/assignment-lint.d.ts +24 -0
  40. package/dist/validation/assignment-lint.d.ts.map +1 -0
  41. package/dist/validation/assignment-lint.js +132 -0
  42. package/package.json +2 -1
@@ -38,6 +38,31 @@ function createKeyBuilders(config) {
38
38
  // Assignments whose escalation ladder was exhausted under
39
39
  // `onExhausted: 'park'` — held out of matching until unparked.
40
40
  parkedAssignments: () => `${prefix}assignments:parked`,
41
+ // Schedule keys
42
+ // Assignments held by schedule.notBefore: raw JSON, invisible to
43
+ // every matching path until the scheduled sweep enqueues it. The
44
+ // ONLY pre-activation storage — no priority/tag/geo/veto side keys
45
+ // exist until activation.
46
+ scheduledAssignments: () => `${prefix}assignments:scheduled`,
47
+ // Activation index: id scored by schedule.notBefore epoch ms.
48
+ scheduledActivateAt: () => `${prefix}assignments:scheduled:activateAt`,
49
+ // Offer-window deadline index: id scored by schedule.notAfter epoch
50
+ // ms. Lives outside :scheduled because it follows the assignment
51
+ // through scheduled -> queued -> pending; cleared on accept (the
52
+ // offer clock dies there). Cannot share assignmentsSlaExpiry: a zset
53
+ // holds one score per member and an assignment can carry both
54
+ // sla.expireAfterMs and schedule.notAfter.
55
+ scheduleNotAfter: () => `${prefix}assignments:schedule:notAfter`,
56
+ // SLA keys
57
+ // Accepted assignments with a completion deadline (sla.completeWithinMs),
58
+ // scored by deadline epoch ms. Only SLA-bearing assignments appear here.
59
+ acceptedAssignmentsExpiry: () => `${prefix}assignments:accepted:expiry`,
60
+ // Assignments with a freshness TTL (sla.expireAfterMs), scored by
61
+ // first-enqueue + TTL. Entries survive requeues and state moves.
62
+ assignmentsSlaExpiry: () => `${prefix}assignments:sla:expiry`,
63
+ // Aggregate SLO counters (global + per-tag)
64
+ slaStats: () => `${prefix}sla:stats`,
65
+ slaTagStats: (tag) => `${prefix}sla:tag:${tag}:stats`,
41
66
  // Tag keys
42
67
  allTags: () => `${prefix}all:tags`,
43
68
  tagAssignments: (tag) => `${prefix}tag:${tag}:assignments`,
@@ -78,6 +103,9 @@ function createKeyBuilders(config) {
78
103
  learningUsers: () => `${prefix}learning:users`,
79
104
  learningUserTagCounts: (userId) => `${prefix}learning:user:${userId}:tag:counts`,
80
105
  learningUserTagRewards: (userId) => `${prefix}learning:user:${userId}:tag:rewards`,
106
+ learningUserTagRewardSq: (userId) => `${prefix}learning:user:${userId}:tag:rewards:sq`,
107
+ learningUserTagTs: (userId) => `${prefix}learning:user:${userId}:tag:ts`,
108
+ autoWeightsSyncLock: () => `${prefix}autoweights:sync:lock`,
81
109
  // Workflow reliability keys
82
110
  // Reliability and audit keys
83
111
  deadLetterQueue: () => `${prefix}workflow:dlq`,
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Assignment pre-flight lint — pure logic.
3
+ *
4
+ * Detects declarations that the runtime would otherwise handle silently:
5
+ * policies that normalize away to nothing, schedule windows that cannot be
6
+ * satisfied, and clock combinations where one policy shadows another.
7
+ * Everything here is deliberately side-effect free and Redis-free;
8
+ * `AssignmentMatcher.checkAssignmentReadiness()` layers the live checks
9
+ * (tag coverage, duplicate ids, eligible users) on top.
10
+ */
11
+ import type { Assignment, AssignmentLintContext, AssignmentLintIssue, User } from '../types/matcher';
12
+ /**
13
+ * Whether a user can currently serve a tag, mirroring the matching
14
+ * semantics: positive routing weights (wildcards honored, weight 0 vetoes)
15
+ * when the user has any, tag membership (user-side patterns) otherwise.
16
+ */
17
+ export declare function userCoversTag(user: User, tag: string): boolean;
18
+ /**
19
+ * Static pre-flight checks for an assignment declaration. Returns an empty
20
+ * array when nothing is worth flagging. Purely structural — pair with
21
+ * `checkAssignmentReadiness()` for checks against the live user pool.
22
+ */
23
+ export declare function lintAssignment(assignment: Assignment, context?: AssignmentLintContext): AssignmentLintIssue[];
24
+ //# sourceMappingURL=assignment-lint.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assignment-lint.d.ts","sourceRoot":"","sources":["../../src/validation/assignment-lint.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,qBAAqB,EAAE,mBAAmB,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAWrG;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAM9D;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,UAAU,EAAE,UAAU,EAAE,OAAO,GAAE,qBAA0B,GAAG,mBAAmB,EAAE,CAmGjH"}
@@ -0,0 +1,132 @@
1
+ "use strict";
2
+ /**
3
+ * Assignment pre-flight lint — pure logic.
4
+ *
5
+ * Detects declarations that the runtime would otherwise handle silently:
6
+ * policies that normalize away to nothing, schedule windows that cannot be
7
+ * satisfied, and clock combinations where one policy shadows another.
8
+ * Everything here is deliberately side-effect free and Redis-free;
9
+ * `AssignmentMatcher.checkAssignmentReadiness()` layers the live checks
10
+ * (tag coverage, duplicate ids, eligible users) on top.
11
+ */
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.lintAssignment = exports.userCoversTag = void 0;
14
+ const policy_1 = require("../schedule/policy");
15
+ const policy_2 = require("../sla/policy");
16
+ const policy_3 = require("../escalation/policy");
17
+ const match_score_1 = require("../scoring/match-score");
18
+ function epochMs(value) {
19
+ const n = Number(value);
20
+ return Number.isFinite(n) && n > 0 ? Math.floor(n) : undefined;
21
+ }
22
+ /**
23
+ * Whether a user can currently serve a tag, mirroring the matching
24
+ * semantics: positive routing weights (wildcards honored, weight 0 vetoes)
25
+ * when the user has any, tag membership (user-side patterns) otherwise.
26
+ */
27
+ function userCoversTag(user, tag) {
28
+ const weights = user.routingWeights;
29
+ if (weights && Object.keys(weights).length > 0) {
30
+ return (0, match_score_1.getEffectiveWeight)(weights, tag) > 0;
31
+ }
32
+ return (user.tags ?? []).some((userTag) => (0, match_score_1.matchesPattern)(userTag, tag));
33
+ }
34
+ exports.userCoversTag = userCoversTag;
35
+ /**
36
+ * Static pre-flight checks for an assignment declaration. Returns an empty
37
+ * array when nothing is worth flagging. Purely structural — pair with
38
+ * `checkAssignmentReadiness()` for checks against the live user pool.
39
+ */
40
+ function lintAssignment(assignment, context = {}) {
41
+ const now = context.now ?? Date.now();
42
+ const issues = [];
43
+ if (!assignment.tags || assignment.tags.length === 0) {
44
+ issues.push(context.enableDefaultMatching
45
+ ? {
46
+ severity: 'info',
47
+ code: 'no-tags',
48
+ message: 'Assignment has no tags; it will match through the injected default tag only.',
49
+ }
50
+ : {
51
+ severity: 'error',
52
+ code: 'no-tags',
53
+ message: 'Assignment has no tags and default matching is off — no user can ever receive it.',
54
+ });
55
+ }
56
+ const schedule = (0, policy_1.normalizeSchedulePolicy)(assignment.schedule);
57
+ if (assignment.schedule && !schedule) {
58
+ const rawNotBefore = epochMs(assignment.schedule.notBefore);
59
+ const rawNotAfter = epochMs(assignment.schedule.notAfter);
60
+ if (rawNotBefore !== undefined && rawNotAfter !== undefined && rawNotAfter <= rawNotBefore) {
61
+ issues.push({
62
+ severity: 'error',
63
+ code: 'schedule-window-inverted',
64
+ message: `schedule.notAfter (${rawNotAfter}) is not after notBefore (${rawNotBefore}); the whole schedule policy is ignored.`,
65
+ });
66
+ }
67
+ else {
68
+ issues.push({
69
+ severity: 'error',
70
+ code: 'schedule-ignored',
71
+ message: 'schedule declares no usable timestamps; the whole schedule policy is ignored.',
72
+ });
73
+ }
74
+ }
75
+ if (schedule) {
76
+ // Where the clocks would start: a future notBefore anchors at
77
+ // activation, everything else anchors at enqueue (now).
78
+ const activation = schedule.notBefore !== undefined ? Math.max(schedule.notBefore, now) : now;
79
+ if (schedule.notAfter !== undefined && schedule.notAfter <= now) {
80
+ issues.push({
81
+ severity: 'warning',
82
+ code: 'schedule-window-elapsed',
83
+ message: 'schedule.notAfter is already in the past; the assignment will miss on the first sweep.',
84
+ });
85
+ }
86
+ else {
87
+ if (schedule.notBefore !== undefined && schedule.notBefore <= now) {
88
+ issues.push({
89
+ severity: 'info',
90
+ code: 'schedule-notbefore-past',
91
+ message: 'schedule.notBefore is already in the past; the assignment enqueues immediately.',
92
+ });
93
+ }
94
+ if (schedule.notAfter !== undefined) {
95
+ const windowMs = schedule.notAfter - activation;
96
+ const escalation = (0, policy_3.normalizeEscalationPolicy)(assignment.escalation);
97
+ const respondWithinMs = escalation?.respondWithinMs ?? context.matchExpirationMs;
98
+ if (respondWithinMs !== undefined && respondWithinMs > 0 && windowMs < respondWithinMs) {
99
+ issues.push({
100
+ severity: 'warning',
101
+ code: 'offer-window-tight',
102
+ message: `The offer window (${windowMs}ms from activation) is shorter than one response deadline (${respondWithinMs}ms) — a single unanswered offer consumes the whole window.`,
103
+ });
104
+ }
105
+ const sla = (0, policy_2.normalizeSlaPolicy)(assignment.sla);
106
+ if (sla?.expireAfterMs !== undefined && activation + sla.expireAfterMs <= schedule.notAfter) {
107
+ issues.push({
108
+ severity: 'warning',
109
+ code: 'schedule-notafter-shadowed-by-sla-ttl',
110
+ message: `sla.expireAfterMs (${sla.expireAfterMs}ms from activation) elapses before schedule.notAfter — the TTL's onExpire acts first and notAfter never fires.`,
111
+ });
112
+ }
113
+ }
114
+ }
115
+ }
116
+ if (assignment.sla && !(0, policy_2.normalizeSlaPolicy)(assignment.sla)) {
117
+ issues.push({
118
+ severity: 'warning',
119
+ code: 'sla-ignored',
120
+ message: 'sla declares no usable values; the whole SLA policy is ignored.',
121
+ });
122
+ }
123
+ if (assignment.escalation && !(0, policy_3.normalizeEscalationPolicy)(assignment.escalation)) {
124
+ issues.push({
125
+ severity: 'warning',
126
+ code: 'escalation-ignored',
127
+ message: 'escalation declares no usable respondWithinMs; the whole escalation policy is ignored.',
128
+ });
129
+ }
130
+ return issues;
131
+ }
132
+ exports.lintAssignment = lintAssignment;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "assignment-user-matcher",
3
- "version": "1.7.0",
3
+ "version": "1.9.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/ViljarVoidula/assignment-user-matcher"
@@ -28,6 +28,7 @@
28
28
  "scripts": {
29
29
  "test": "nyc mocha tests/ --recursive --extension .ts --exit",
30
30
  "benchmark:learning": "ts-node benchmark-learning.ts",
31
+ "benchmark:weights": "ts-node benchmark-weights.ts",
31
32
  "build:docs": "npx typedoc --plugin typedoc-plugin-markdown",
32
33
  "prettier": "prettier --write .",
33
34
  "build": "rimraf dist && tsc && mkdir -p dist/lua && cp src/lua/*.lua dist/lua/",