@indigoai-us/hq-cli 5.345.219 → 5.345.221
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.
- package/CHANGELOG.md +170 -0
- package/dist/command-catalog.generated.d.ts +169 -3
- package/dist/command-catalog.generated.js +206 -3
- package/dist/commands/auth.js +4 -1
- package/dist/commands/bot-runtime.d.ts +18 -5
- package/dist/commands/bot-runtime.js +29 -12
- package/dist/commands/bot.d.ts +85 -0
- package/dist/commands/bot.js +245 -16
- package/dist/commands/cloud.d.ts +16 -0
- package/dist/commands/cloud.js +112 -102
- package/dist/commands/core-checkpoint-native.d.ts +13 -0
- package/dist/commands/core-checkpoint-native.js +167 -52
- package/dist/commands/core-hook-registration-plan.js +5 -4
- package/dist/commands/core-stop-autosave.d.ts +21 -0
- package/dist/commands/core-stop-autosave.js +300 -0
- package/dist/commands/core.js +2 -0
- package/dist/commands/daemon.d.ts +5 -0
- package/dist/commands/daemon.js +52 -9
- package/dist/commands/doctor-usage.d.ts +5 -0
- package/dist/commands/doctor-usage.js +25 -0
- package/dist/commands/env.d.ts +49 -4
- package/dist/commands/env.js +355 -5
- package/dist/commands/integrations.js +84 -3
- package/dist/commands/lanes-shared.d.ts +3 -0
- package/dist/commands/lanes.d.ts +3 -0
- package/dist/commands/lanes.js +87 -3
- package/dist/commands/packs.js +23 -0
- package/dist/commands/pkg-update.d.ts +6 -0
- package/dist/commands/pkg-update.js +15 -7
- package/dist/commands/run.js +55 -45
- package/dist/commands/sources.js +4 -3
- package/dist/commands/sync-core.d.ts +1 -0
- package/dist/commands/sync-core.js +77 -3
- package/dist/commands/whoami.js +96 -80
- package/dist/lib/bot/config.d.ts +17 -0
- package/dist/lib/bot/config.js +13 -0
- package/dist/lib/bot/fleet-cron.js +29 -4
- package/dist/lib/bot/fleet-local.d.ts +30 -2
- package/dist/lib/bot/fleet-local.js +60 -1
- package/dist/lib/bot/grok-login-scope.d.ts +36 -0
- package/dist/lib/bot/grok-login-scope.js +93 -0
- package/dist/lib/bot/inflight.d.ts +9 -0
- package/dist/lib/bot/jobs/hot-signal-sync.d.ts +51 -0
- package/dist/lib/bot/jobs/hot-signal-sync.js +90 -9
- package/dist/lib/bot/legacy-provider-login.d.ts +32 -9
- package/dist/lib/bot/legacy-provider-login.js +28 -22
- package/dist/lib/bot/run.d.ts +29 -5
- package/dist/lib/bot/run.js +203 -1
- package/dist/lib/bot/runtime-sign-in-check.js +6 -1
- package/dist/lib/bot/runtime-sign-in.d.ts +2 -0
- package/dist/lib/bot/signals-try-now.d.ts +76 -0
- package/dist/lib/bot/signals-try-now.js +111 -0
- package/dist/lib/capacity/collectors/claude-oauth.d.ts +22 -3
- package/dist/lib/capacity/collectors/claude-oauth.js +78 -22
- package/dist/lib/capacity/scheduler.d.ts +8 -0
- package/dist/lib/capacity/scheduler.js +5 -2
- package/dist/lib/core-utils/sentry-report.js +2 -0
- package/dist/lib/daemon/machine-checks.d.ts +22 -1
- package/dist/lib/daemon/machine-checks.js +54 -12
- package/dist/lib/daemon/run.js +71 -8
- package/dist/lib/daemon/runtime-notifications.d.ts +7 -0
- package/dist/lib/daemon/runtime-notifications.js +50 -0
- package/dist/lib/daemon/stop-autosave-lock.d.ts +19 -0
- package/dist/lib/daemon/stop-autosave-lock.js +166 -0
- package/dist/lib/daemon/stop-autosave.d.ts +28 -0
- package/dist/lib/daemon/stop-autosave.js +111 -0
- package/dist/lib/daemon/stop-diagnostics.d.ts +11 -0
- package/dist/lib/daemon/stop-diagnostics.js +136 -0
- package/dist/lib/daemon/stop-job-executor.d.ts +19 -0
- package/dist/lib/daemon/stop-job-executor.js +126 -0
- package/dist/lib/daemon/stop-jobs.d.ts +93 -0
- package/dist/lib/daemon/stop-jobs.js +599 -0
- package/dist/lib/doctor/checks/fleet-hook-core.d.ts +9 -1
- package/dist/lib/doctor/checks/fleet-hook-core.js +39 -15
- package/dist/lib/doctor/checks/host-core.js +1 -1
- package/dist/lib/environment/access-probes.d.ts +234 -0
- package/dist/lib/environment/access-probes.js +918 -0
- package/dist/lib/environment/profile.d.ts +15 -2
- package/dist/lib/environment/profile.js +55 -20
- package/dist/lib/environment/rfc3339.d.ts +8 -0
- package/dist/lib/environment/rfc3339.js +26 -0
- package/dist/lib/environment/work-requirements.d.ts +169 -0
- package/dist/lib/environment/work-requirements.js +389 -0
- package/dist/lib/hook-engine/coverage.d.ts +137 -7
- package/dist/lib/hook-engine/coverage.js +1424 -4
- package/dist/lib/hook-engine/fleet-registration.d.ts +17 -5
- package/dist/lib/hook-engine/fleet-registration.js +53 -17
- package/dist/lib/hook-engine/git-stop-config.json +513 -0
- package/dist/lib/hook-engine/native-stop-compatibility.json +311 -0
- package/dist/lib/hook-engine/reconcile.js +2 -2
- package/dist/lib/hot-channels/alert.d.ts +10 -1
- package/dist/lib/hot-channels/alert.js +38 -8
- package/dist/lib/hot-channels/signals-claude-login.d.ts +53 -0
- package/dist/lib/hot-channels/signals-claude-login.js +96 -0
- package/dist/lib/hot-channels/signals-pass.d.ts +143 -17
- package/dist/lib/hot-channels/signals-pass.js +324 -17
- package/dist/lib/hot-channels/signals-runtime.d.ts +30 -0
- package/dist/lib/hot-channels/signals-runtime.js +82 -0
- package/dist/lib/install/hook-target.js +1 -1
- package/dist/lib/lanes/host-capacity.d.ts +26 -2
- package/dist/lib/lanes/host-capacity.js +17 -2
- package/dist/lib/lanes/host-compute.d.ts +90 -0
- package/dist/lib/lanes/host-compute.js +185 -0
- package/dist/lib/lanes/index.d.ts +2 -0
- package/dist/lib/lanes/index.js +1 -0
- package/dist/lib/lanes/reconcile.js +22 -1
- package/dist/lib/lanes/resource-usage.d.ts +155 -0
- package/dist/lib/lanes/resource-usage.js +589 -0
- package/dist/lib/lanes/run-size.d.ts +46 -0
- package/dist/lib/lanes/run-size.js +197 -0
- package/dist/lib/lanes/store.js +27 -0
- package/dist/lib/lanes/throttle.d.ts +36 -1
- package/dist/lib/lanes/throttle.js +47 -10
- package/dist/lib/lanes/types.d.ts +40 -0
- package/dist/lib/lanes/types.js +2 -2
- package/dist/lib/lanes/watcher.js +13 -4
- package/dist/lib/lanes/worker-process.d.ts +25 -2
- package/dist/lib/lanes/worker-process.js +71 -19
- package/dist/lib/placement/match.d.ts +192 -0
- package/dist/lib/placement/match.js +667 -0
- package/dist/lib/safe-store.d.ts +14 -0
- package/dist/lib/safe-store.js +77 -4
- package/dist/lib/search-index/index.d.ts +88 -4
- package/dist/lib/search-index/index.js +299 -25
- package/dist/lib/search-index/max-doc-bytes.d.ts +17 -6
- package/dist/lib/search-index/max-doc-bytes.js +48 -23
- package/dist/lib/signals/codex-lockdown.d.ts +79 -0
- package/dist/lib/signals/codex-lockdown.js +271 -0
- package/dist/lib/signals/codex-login.d.ts +37 -0
- package/dist/lib/signals/codex-login.js +83 -0
- package/dist/lib/signals/engine.d.ts +28 -0
- package/dist/lib/signals/engine.js +77 -0
- package/dist/lib/signals/reply.d.ts +48 -0
- package/dist/lib/signals/reply.js +94 -12
- package/dist/lib/signals/run-record.js +6 -1
- package/dist/lib/signals/settings.d.ts +5 -0
- package/dist/lib/signals/settings.js +2 -0
- package/dist/lib/signals/steps/adapters.js +6 -0
- package/dist/lib/signals/steps/extract.d.ts +135 -12
- package/dist/lib/signals/steps/extract.js +416 -84
- package/dist/lib/signals/steps/types.d.ts +21 -0
- package/dist/lib/sync-core/engine-home.d.ts +41 -2
- package/dist/lib/sync-core/engine-home.js +132 -19
- package/dist/lib/sync-core/reconcile.js +1 -1
- package/dist/lib/sync-core/release-compare.d.ts +7 -0
- package/dist/lib/sync-core/release-compare.js +16 -0
- package/dist/lib/sync-core/release.d.ts +2 -1
- package/dist/lib/sync-core/release.js +2 -10
- package/dist/lib/sync-core/rollout-state.d.ts +2 -1
- package/dist/lib/sync-core/rollout-state.js +5 -2
- package/dist/lib/sync-core/vault-uid.d.ts +2 -0
- package/dist/lib/sync-core/vault-uid.js +4 -0
- package/dist/run/schema-env-overrides.d.ts +24 -0
- package/dist/run/schema-env-overrides.js +72 -0
- package/dist/schemas/hq-work-requirements.v1.schema.json +145 -0
- package/dist/utils/auth-session-check.d.ts +16 -0
- package/dist/utils/auth-session-check.js +16 -0
- package/dist/utils/client-health-contract.d.ts +6 -0
- package/dist/utils/client-health-contract.js +13 -0
- package/dist/utils/client-health.d.ts +7 -0
- package/dist/utils/client-health.js +22 -1
- package/dist/utils/command-usage.d.ts +40 -0
- package/dist/utils/command-usage.js +61 -0
- package/dist/utils/duplicate-command-usage.d.ts +72 -0
- package/dist/utils/duplicate-command-usage.js +125 -0
- package/dist/utils/host-registration.d.ts +1 -1
- package/dist/utils/host-registration.js +6 -1
- package/dist/utils/resident-package-preload.js +4 -0
- package/dist/utils/sentry-fingerprint.js +9 -0
- package/dist/utils/windows-bash.d.ts +2 -0
- package/dist/utils/windows-bash.js +2 -1
- package/package.json +15 -10
- package/scripts/bench-core-compare.mjs +313 -0
- package/scripts/bench-core.mjs +399 -12
- package/scripts/check-function-manifest.mjs +121 -7
- package/dist/commands/__fixtures__/access-vault.d.ts +0 -95
- package/dist/commands/__fixtures__/access-vault.js +0 -166
- package/dist/commands/__fixtures__/make-tar.d.ts +0 -38
- package/dist/commands/__fixtures__/make-tar.js +0 -102
- package/dist/commands/secrets-public-types.test-d.d.ts +0 -2
- package/dist/commands/secrets-public-types.test-d.js +0 -18
- package/dist/lib/decisions/fixtures/us001.d.ts +0 -137
- package/dist/lib/decisions/fixtures/us001.js +0 -21
- package/dist/lib/doctor/__testing__/fake-hq-tree.d.ts +0 -226
- package/dist/lib/doctor/__testing__/fake-hq-tree.js +0 -408
- package/dist/lib/doctor/__testing__/run-doctor.d.ts +0 -8
- package/dist/lib/doctor/__testing__/run-doctor.js +0 -69
- package/dist/lib/fingerprint/fixtures.d.ts +0 -19
- package/dist/lib/fingerprint/fixtures.js +0 -61
- package/dist/lib/hot-channels/adapters/__fixtures__/dummy.d.ts +0 -33
- package/dist/lib/hot-channels/adapters/__fixtures__/dummy.js +0 -53
- package/dist/lib/hot-channels/adapters/__fixtures__/meeting-providers.d.ts +0 -38
- package/dist/lib/hot-channels/adapters/__fixtures__/meeting-providers.js +0 -64
- package/dist/lib/mesh/live/daemon/mqtt-events.test-d.d.ts +0 -2
- package/dist/lib/mesh/live/daemon/mqtt-events.test-d.js +0 -10
- package/dist/lib/test-utils/isolated-flag-company-context.d.ts +0 -3
- package/dist/lib/test-utils/isolated-flag-company-context.js +0 -18
- package/scripts/hook-engine-manifest.mjs +0 -22
- package/scripts/mesh-daemon-if-stale.mjs +0 -20
|
@@ -0,0 +1,667 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The matcher: which environments can do one piece of work, and a first
|
|
3
|
+
* ranking of the (environment, login) pairs that can.
|
|
4
|
+
*
|
|
5
|
+
* match(requirements, profiles, { now }) -> { feasible, rejected, ranked }
|
|
6
|
+
*
|
|
7
|
+
* Pass 1 applies rules R1 to R9 from the HQ Everywhere match walkthrough
|
|
8
|
+
* (research/environment-profile-v1/match-walkthrough.md). An environment is
|
|
9
|
+
* rejected at the first rule it fails, and a pair at the first account check
|
|
10
|
+
* it fails; each rejection carries exactly one reason naming the rule and the
|
|
11
|
+
* field. Pass 2 scores every feasible pair with the weights in MATCH_WEIGHTS.
|
|
12
|
+
*
|
|
13
|
+
* This is a tool for the conductor engine (owner decision 1): it filters and
|
|
14
|
+
* gives a first ranking. It does not choose; the conductor makes and records
|
|
15
|
+
* the choice.
|
|
16
|
+
*
|
|
17
|
+
* Login policy (owner decision 5): a login held by the owner pairs with work
|
|
18
|
+
* for any of his businesses. owning_company is shown on the ranked pair and
|
|
19
|
+
* breaks ties; it never filters the owner's logins. A login held by another
|
|
20
|
+
* person (readings.foreign_accounts) never pairs.
|
|
21
|
+
*
|
|
22
|
+
* Freshness: every reading is aged against the one `now` passed in. A reading
|
|
23
|
+
* is fresh when it was observed at most ttl_seconds before now (exactly at the
|
|
24
|
+
* ttl is still fresh). A reading with no observed_at, or one observed more than
|
|
25
|
+
* clock_skew_seconds after now, is unknown; one older than its ttl is stale.
|
|
26
|
+
* Stale or unknown room counts as a quarter of the room points, never as fresh
|
|
27
|
+
* room. Stale or unknown compute counts as unknown compute and is not used for
|
|
28
|
+
* the live size check. An access probe that is not fresh fails R5.
|
|
29
|
+
*
|
|
30
|
+
* Company isolation: a profile is a company-scoped view, so it ranks only for
|
|
31
|
+
* the company whose view it is (R1 on view.company).
|
|
32
|
+
*
|
|
33
|
+
* Input: the documents must already be valid (parseEnvironmentProfile and
|
|
34
|
+
* parseWorkRequirements). A missing required array throws an Error that names
|
|
35
|
+
* it. Duplicate environment ids are settled under R0 (input), field
|
|
36
|
+
* declared.id, after the company filter and whatever the input order:
|
|
37
|
+
* identical copies collapse to one, an undated or future-dated copy is
|
|
38
|
+
* rejected without hiding the others, the copy with the latest
|
|
39
|
+
* view.generated_at is kept, and when no single copy is latest every copy is
|
|
40
|
+
* rejected.
|
|
41
|
+
*
|
|
42
|
+
* The module has no I/O. It imports types only, reads no file, environment
|
|
43
|
+
* variable, clock or network, and runs without an HQ root.
|
|
44
|
+
*/
|
|
45
|
+
export const MATCH_RESULT_SCHEMA_VERSION = "hq-env-match.v1";
|
|
46
|
+
/** Printed with every result: this is not a placement. */
|
|
47
|
+
export const MATCH_FIRST_RANKING_NOTICE = "First ranking from the matcher, not a placement. The conductor engine makes and records the choice.";
|
|
48
|
+
/** The config table: pass-2 weights and the clock-skew allowance. */
|
|
49
|
+
export const MATCH_WEIGHTS = Object.freeze({
|
|
50
|
+
room_max: 35,
|
|
51
|
+
room_unknown_share: 0.25,
|
|
52
|
+
compute_max: 20,
|
|
53
|
+
compute_unknown_points: 10,
|
|
54
|
+
locality_points: 15,
|
|
55
|
+
locality_no_needs_points: 0,
|
|
56
|
+
soft_labels_max: 10,
|
|
57
|
+
engine_order_max: 10,
|
|
58
|
+
cost_per_usd: 5,
|
|
59
|
+
cost_cap: 10,
|
|
60
|
+
start_per_minute: 1,
|
|
61
|
+
start_cap: 10,
|
|
62
|
+
start_unknown_penalty: 10,
|
|
63
|
+
taint_prefer_not_penalty: 10,
|
|
64
|
+
clock_skew_seconds: 120,
|
|
65
|
+
});
|
|
66
|
+
function parseNow(now) {
|
|
67
|
+
const ms = now instanceof Date ? now.getTime() : Date.parse(now);
|
|
68
|
+
if (!Number.isFinite(ms))
|
|
69
|
+
throw new Error("match: options.now is not a valid date-time");
|
|
70
|
+
return ms;
|
|
71
|
+
}
|
|
72
|
+
function round2(value) {
|
|
73
|
+
const rounded = Math.round(value * 100) / 100;
|
|
74
|
+
return Object.is(rounded, -0) ? 0 : rounded;
|
|
75
|
+
}
|
|
76
|
+
function clamp(value, low, high) {
|
|
77
|
+
return Math.min(high, Math.max(low, value));
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* fresh: observed at most ttl_seconds before now (a reading up to the skew
|
|
81
|
+
* allowance after now counts as observed at now). stale: older than its ttl.
|
|
82
|
+
* future: observed more than the skew allowance after now. missing: no
|
|
83
|
+
* observed_at, or one that is not a date-time.
|
|
84
|
+
*/
|
|
85
|
+
export function readingFreshness(observedAt, ttlSeconds, clock) {
|
|
86
|
+
if (!observedAt)
|
|
87
|
+
return "missing";
|
|
88
|
+
const at = Date.parse(observedAt);
|
|
89
|
+
if (!Number.isFinite(at))
|
|
90
|
+
return "missing";
|
|
91
|
+
if (at - clock.nowMs > clock.skewMs)
|
|
92
|
+
return "future";
|
|
93
|
+
return clock.nowMs - at <= ttlSeconds * 1000 ? "fresh" : "stale";
|
|
94
|
+
}
|
|
95
|
+
function isFresh(observedAt, ttlSeconds, clock) {
|
|
96
|
+
return readingFreshness(observedAt, ttlSeconds, clock) === "fresh";
|
|
97
|
+
}
|
|
98
|
+
function requireArray(value, what) {
|
|
99
|
+
if (!Array.isArray(value)) {
|
|
100
|
+
throw new Error(`match: ${what} must be an array; validate the document first (parseEnvironmentProfile / parseWorkRequirements)`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/** A clear error, not a TypeError, when a required array or object is missing. */
|
|
104
|
+
function assertInputs(work, profiles) {
|
|
105
|
+
requireArray(profiles, "profiles");
|
|
106
|
+
if (!work || typeof work !== "object")
|
|
107
|
+
throw new Error("match: requirements must be an object");
|
|
108
|
+
if (typeof work.company !== "string")
|
|
109
|
+
throw new Error("match: requirements.company must be a string");
|
|
110
|
+
requireArray(work.engines?.any_of, "requirements.engines.any_of");
|
|
111
|
+
for (const key of ["labels", "repos", "secrets", "integrations"])
|
|
112
|
+
requireArray(work.needs?.[key], `requirements.needs.${key}`);
|
|
113
|
+
if (work.needs.data !== undefined)
|
|
114
|
+
requireArray(work.needs.data, "requirements.needs.data");
|
|
115
|
+
requireArray(work.tolerates, "requirements.tolerates");
|
|
116
|
+
if (!work.size || typeof work.size !== "object")
|
|
117
|
+
throw new Error("match: requirements.size must be an object");
|
|
118
|
+
if (!work.continuity || typeof work.continuity !== "object")
|
|
119
|
+
throw new Error("match: requirements.continuity must be an object");
|
|
120
|
+
profiles.forEach((profile, index) => {
|
|
121
|
+
const at = `profiles[${index}]`;
|
|
122
|
+
if (!profile || typeof profile !== "object")
|
|
123
|
+
throw new Error(`match: ${at} must be an object`);
|
|
124
|
+
if (!profile.declared || typeof profile.declared !== "object")
|
|
125
|
+
throw new Error(`match: ${at}.declared must be an object`);
|
|
126
|
+
if (typeof profile.declared.id !== "string")
|
|
127
|
+
throw new Error(`match: ${at}.declared.id must be a string`);
|
|
128
|
+
const named = `${at} (${profile.declared.id})`;
|
|
129
|
+
if (!profile.view || typeof profile.view.company !== "string")
|
|
130
|
+
throw new Error(`match: ${named}.view.company must be a string`);
|
|
131
|
+
for (const key of ["companies", "labels", "taints"])
|
|
132
|
+
requireArray(profile.declared[key], `${named}.declared.${key}`);
|
|
133
|
+
if (!profile.declared.limits || typeof profile.declared.limits !== "object")
|
|
134
|
+
throw new Error(`match: ${named}.declared.limits must be an object`);
|
|
135
|
+
if (!profile.declared.lifecycle || typeof profile.declared.lifecycle !== "object")
|
|
136
|
+
throw new Error(`match: ${named}.declared.lifecycle must be an object`);
|
|
137
|
+
if (!profile.declared.cost || typeof profile.declared.cost !== "object")
|
|
138
|
+
throw new Error(`match: ${named}.declared.cost must be an object`);
|
|
139
|
+
if (!profile.readings || typeof profile.readings !== "object")
|
|
140
|
+
throw new Error(`match: ${named}.readings must be an object`);
|
|
141
|
+
for (const key of ["accounts", "foreign_accounts", "access"])
|
|
142
|
+
requireArray(profile.readings[key], `${named}.readings.${key}`);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
const NEED_KINDS = [
|
|
146
|
+
{ key: "repos", prefix: "repo" },
|
|
147
|
+
{ key: "secrets", prefix: "secret" },
|
|
148
|
+
{ key: "integrations", prefix: "integration" },
|
|
149
|
+
{ key: "data", prefix: "data" },
|
|
150
|
+
];
|
|
151
|
+
/** The newest probe with this name, or undefined. */
|
|
152
|
+
function newestProbe(probes, name) {
|
|
153
|
+
let best;
|
|
154
|
+
for (const probe of probes) {
|
|
155
|
+
if (probe.name !== name)
|
|
156
|
+
continue;
|
|
157
|
+
if (!best || Date.parse(probe.observed_at) > Date.parse(best.observed_at))
|
|
158
|
+
best = probe;
|
|
159
|
+
}
|
|
160
|
+
return best;
|
|
161
|
+
}
|
|
162
|
+
function probePasses(probes, name, clock) {
|
|
163
|
+
const probe = newestProbe(probes, name);
|
|
164
|
+
return probe !== undefined && probe.result === "pass" && isFresh(probe.observed_at, probe.ttl_seconds, clock);
|
|
165
|
+
}
|
|
166
|
+
function pairRejection(profile, engine, account, reason) {
|
|
167
|
+
return {
|
|
168
|
+
subject: "pair",
|
|
169
|
+
environment_id: profile.declared.id,
|
|
170
|
+
engine,
|
|
171
|
+
account_id: account.account_id,
|
|
172
|
+
holder: account.holder,
|
|
173
|
+
owning_company: account.owning_company,
|
|
174
|
+
reason,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
/** R3 for one account: the first check it fails, or null when it pairs. */
|
|
178
|
+
function accountReason(work, account, index) {
|
|
179
|
+
const at = `readings.accounts[${index}]`;
|
|
180
|
+
if (!work.engines.any_of.includes(account.engine)) {
|
|
181
|
+
return { rule: "R3", field: "engines.any_of", detail: `engine ${account.engine} is not in engines.any_of` };
|
|
182
|
+
}
|
|
183
|
+
const pin = work.engines.account_pin;
|
|
184
|
+
if (pin && account.account_id !== pin) {
|
|
185
|
+
return { rule: "R3", field: "engines.account_pin", detail: `work is pinned to account ${pin}` };
|
|
186
|
+
}
|
|
187
|
+
if (account.enabled !== true) {
|
|
188
|
+
return { rule: "R3", field: `${at}.enabled`, detail: "login is disabled" };
|
|
189
|
+
}
|
|
190
|
+
if (account.signed_in !== true) {
|
|
191
|
+
return {
|
|
192
|
+
rule: "R3",
|
|
193
|
+
field: `${at}.signed_in`,
|
|
194
|
+
detail: account.signed_in === null ? "sign-in is unknown" : "login is not signed in",
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
if (account.holder !== "owner") {
|
|
198
|
+
return {
|
|
199
|
+
rule: "R3",
|
|
200
|
+
field: `${at}.holder`,
|
|
201
|
+
detail: `holder is ${String(account.holder)}; only logins the owner holds pair (owner decision 5)`,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
if (account.usable !== true) {
|
|
205
|
+
return { rule: "R3", field: `${at}.usable`, detail: "the profile marks this login not usable" };
|
|
206
|
+
}
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
/** R3: the pairs this environment offers, and one rejection per login that does not pair. */
|
|
210
|
+
function pairsFor(work, profile) {
|
|
211
|
+
const { declared, readings } = profile;
|
|
212
|
+
const rejections = [];
|
|
213
|
+
const pairs = [];
|
|
214
|
+
if (declared.state === "template") {
|
|
215
|
+
// A template has no logins to check a pin against: the pinned account
|
|
216
|
+
// cannot be shown to exist there before start.
|
|
217
|
+
if (work.engines.account_pin) {
|
|
218
|
+
return {
|
|
219
|
+
pairs,
|
|
220
|
+
rejections,
|
|
221
|
+
envReason: {
|
|
222
|
+
rule: "R3",
|
|
223
|
+
field: "engines.account_pin",
|
|
224
|
+
detail: `work is pinned to account ${work.engines.account_pin}; a template has no logins before start`,
|
|
225
|
+
},
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
// A template pairs through its engine: labels; credentials are injected at start.
|
|
229
|
+
for (const engine of work.engines.any_of) {
|
|
230
|
+
if (declared.labels.includes(`engine:${engine}`))
|
|
231
|
+
pairs.push({ engine, account: null });
|
|
232
|
+
}
|
|
233
|
+
const envReason = pairs.length > 0 ? null : {
|
|
234
|
+
rule: "R3",
|
|
235
|
+
field: "declared.labels",
|
|
236
|
+
detail: `template has no engine label for ${work.engines.any_of.map((e) => `engine:${e}`).join(", ")}`,
|
|
237
|
+
};
|
|
238
|
+
return { pairs, rejections, envReason };
|
|
239
|
+
}
|
|
240
|
+
readings.accounts.forEach((account, index) => {
|
|
241
|
+
const reason = accountReason(work, account, index);
|
|
242
|
+
if (reason) {
|
|
243
|
+
rejections.push(pairRejection(profile, account.engine, {
|
|
244
|
+
account_id: account.account_id,
|
|
245
|
+
holder: account.holder,
|
|
246
|
+
owning_company: account.owning_company,
|
|
247
|
+
}, reason));
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
pairs.push({ engine: account.engine, account });
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
readings.foreign_accounts.forEach((foreign, index) => {
|
|
254
|
+
rejections.push(pairRejection(profile, foreign.engine, { account_id: null, holder: foreign.holder, owning_company: null }, {
|
|
255
|
+
rule: "R3",
|
|
256
|
+
field: `readings.foreign_accounts[${index}].holder`,
|
|
257
|
+
detail: `login held by another person (${foreign.holder}); never paired (owner decision 5)`,
|
|
258
|
+
}));
|
|
259
|
+
});
|
|
260
|
+
const envReason = pairs.length > 0 ? null : {
|
|
261
|
+
rule: "R3",
|
|
262
|
+
field: "readings.accounts",
|
|
263
|
+
detail: "no login pairs with this work",
|
|
264
|
+
};
|
|
265
|
+
return { pairs, rejections, envReason };
|
|
266
|
+
}
|
|
267
|
+
function pass1(work, profile, clock) {
|
|
268
|
+
const { declared, readings } = profile;
|
|
269
|
+
const flags = [];
|
|
270
|
+
const reject = (reason, pairRejections = []) => ({ ok: false, reason, pairRejections });
|
|
271
|
+
const isTemplate = declared.state === "template";
|
|
272
|
+
// R1 company: the profile is this company's view, and the environment may work for it.
|
|
273
|
+
if (profile.view.company !== work.company) {
|
|
274
|
+
return reject({ rule: "R1", field: "view.company", detail: "profile is another company's view" });
|
|
275
|
+
}
|
|
276
|
+
if (!declared.companies.includes(work.company)) {
|
|
277
|
+
return reject({ rule: "R1", field: "declared.companies", detail: `company ${work.company} is not in declared.companies` });
|
|
278
|
+
}
|
|
279
|
+
// R2 state.
|
|
280
|
+
if (isTemplate) {
|
|
281
|
+
flags.push("needs start and post-start preflight");
|
|
282
|
+
}
|
|
283
|
+
else if (declared.state === "stopped") {
|
|
284
|
+
if (work.on_no_fit !== "start") {
|
|
285
|
+
return reject({ rule: "R2", field: "declared.state", detail: "environment is stopped and on_no_fit is not start" });
|
|
286
|
+
}
|
|
287
|
+
flags.push("needs start");
|
|
288
|
+
}
|
|
289
|
+
// R3 engine pair. Pair rejections are kept for a feasible environment and
|
|
290
|
+
// for one rejected here, where they explain the rejection.
|
|
291
|
+
const paired = pairsFor(work, profile);
|
|
292
|
+
if (paired.envReason)
|
|
293
|
+
return reject(paired.envReason, paired.rejections);
|
|
294
|
+
const pairRejections = paired.rejections;
|
|
295
|
+
const rejectLater = (reason) => reject(reason);
|
|
296
|
+
// R4 hard labels.
|
|
297
|
+
const missing = work.needs.labels.filter((label) => !declared.labels.includes(label));
|
|
298
|
+
if (missing.length > 0) {
|
|
299
|
+
return rejectLater({ rule: "R4", field: "needs.labels", detail: `not in declared.labels: ${missing.join(", ")}` });
|
|
300
|
+
}
|
|
301
|
+
// R5 access.
|
|
302
|
+
for (const { key, prefix } of NEED_KINDS) {
|
|
303
|
+
const entries = work.needs[key] ?? [];
|
|
304
|
+
for (let index = 0; index < entries.length; index += 1) {
|
|
305
|
+
const name = `${prefix}:${entries[index]}`;
|
|
306
|
+
const field = `needs.${key}[${index}]`;
|
|
307
|
+
if (isTemplate) {
|
|
308
|
+
if (key === "data") {
|
|
309
|
+
return rejectLater({ rule: "R5", field, detail: `${name}: a template has no probes and a fresh VM has no local data` });
|
|
310
|
+
}
|
|
311
|
+
flags.push(`preflight ${name}`);
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
const probe = newestProbe(readings.access, name);
|
|
315
|
+
if (!probe)
|
|
316
|
+
return rejectLater({ rule: "R5", field, detail: `${name}: no probe` });
|
|
317
|
+
if (probe.result !== "pass")
|
|
318
|
+
return rejectLater({ rule: "R5", field, detail: `${name}: probe result ${probe.result}` });
|
|
319
|
+
const freshness = readingFreshness(probe.observed_at, probe.ttl_seconds, clock);
|
|
320
|
+
if (freshness !== "fresh") {
|
|
321
|
+
const why = freshness === "stale"
|
|
322
|
+
? `probe is older than its ttl_seconds (${probe.ttl_seconds})`
|
|
323
|
+
: freshness === "future"
|
|
324
|
+
? "probe observed_at is later than now plus the clock-skew allowance"
|
|
325
|
+
: "probe has no valid observed_at";
|
|
326
|
+
return rejectLater({ rule: "R5", field, detail: `${name}: ${why}` });
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
// R6 size: declared limits, then the live compute reading when it is fresh.
|
|
331
|
+
const { limits } = declared;
|
|
332
|
+
const sizeChecks = [
|
|
333
|
+
{ field: "size.vcpu", need: work.size.vcpu, limit: limits.vcpu, name: "declared.limits.vcpu" },
|
|
334
|
+
{ field: "size.memory_mb", need: work.size.memory_mb, limit: limits.memory_mb, name: "declared.limits.memory_mb" },
|
|
335
|
+
{ field: "size.disk_gb", need: work.size.disk_gb, limit: limits.disk_gb, name: "declared.limits.disk_gb" },
|
|
336
|
+
];
|
|
337
|
+
for (const check of sizeChecks) {
|
|
338
|
+
if (typeof check.limit === "number" && check.need > check.limit) {
|
|
339
|
+
return rejectLater({ rule: "R6", field: check.field, detail: `${check.need} exceeds ${check.name} ${check.limit}` });
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
const compute = readings.compute;
|
|
343
|
+
if (compute && isFresh(compute.observed_at, compute.ttl_seconds, clock)) {
|
|
344
|
+
if (work.size.memory_mb > compute.mem_available_mb) {
|
|
345
|
+
return rejectLater({ rule: "R6", field: "size.memory_mb", detail: `${work.size.memory_mb} exceeds readings.compute.mem_available_mb ${compute.mem_available_mb}` });
|
|
346
|
+
}
|
|
347
|
+
if (work.size.disk_gb > compute.disk_free_gb) {
|
|
348
|
+
return rejectLater({ rule: "R6", field: "size.disk_gb", detail: `${work.size.disk_gb} exceeds readings.compute.disk_free_gb ${compute.disk_free_gb}` });
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
// R7 run length.
|
|
352
|
+
if (limits.max_run_seconds !== null && work.size.expected_run_seconds > limits.max_run_seconds) {
|
|
353
|
+
return rejectLater({
|
|
354
|
+
rule: "R7",
|
|
355
|
+
field: "size.expected_run_seconds",
|
|
356
|
+
detail: `${work.size.expected_run_seconds} s exceeds declared.limits.max_run_seconds ${limits.max_run_seconds}`,
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
// R8 continuity.
|
|
360
|
+
if ((work.continuity.mode === "workspace-must-survive" || work.continuity.mode === "session-move") && limits.workspace_survives !== true) {
|
|
361
|
+
return rejectLater({ rule: "R8", field: "continuity.mode", detail: `${work.continuity.mode} needs declared.limits.workspace_survives true` });
|
|
362
|
+
}
|
|
363
|
+
// R9 taints.
|
|
364
|
+
const blocking = declared.taints.findIndex((taint) => taint.effect === "no-place" && !work.tolerates.includes(taint.key));
|
|
365
|
+
if (blocking >= 0) {
|
|
366
|
+
return rejectLater({ rule: "R9", field: `declared.taints[${blocking}]`, detail: `${declared.taints[blocking].key} is no-place and not tolerated` });
|
|
367
|
+
}
|
|
368
|
+
return { ok: true, flags, pairs: paired.pairs, pairRejections };
|
|
369
|
+
}
|
|
370
|
+
function scorePair(work, profile, pair, weights, clock) {
|
|
371
|
+
const { declared, readings } = profile;
|
|
372
|
+
const isTemplate = declared.state === "template";
|
|
373
|
+
const account = pair.account;
|
|
374
|
+
// Usage room. Only a fresh reading with a number counts as room; stale,
|
|
375
|
+
// future-dated and missing readings count as unknown room.
|
|
376
|
+
let roomBasis = "unknown";
|
|
377
|
+
let room = weights.room_max * weights.room_unknown_share;
|
|
378
|
+
const roomReading = account?.room;
|
|
379
|
+
if (roomReading) {
|
|
380
|
+
const freshness = readingFreshness(roomReading.observed_at, roomReading.ttl_seconds, clock);
|
|
381
|
+
if (freshness === "stale") {
|
|
382
|
+
roomBasis = "stale";
|
|
383
|
+
}
|
|
384
|
+
else if (freshness === "fresh" && typeof roomReading.left_percent === "number") {
|
|
385
|
+
roomBasis = "fresh";
|
|
386
|
+
room = weights.room_max * clamp(roomReading.left_percent, 0, 100) / 100;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
// Compute headroom.
|
|
390
|
+
let computeBasis = isTemplate ? "template" : "unknown";
|
|
391
|
+
let computePoints = weights.compute_unknown_points;
|
|
392
|
+
let loadPerVcpu = null;
|
|
393
|
+
const compute = readings.compute;
|
|
394
|
+
if (!isTemplate && compute) {
|
|
395
|
+
const freshness = readingFreshness(compute.observed_at, compute.ttl_seconds, clock);
|
|
396
|
+
if (freshness === "stale") {
|
|
397
|
+
computeBasis = "stale";
|
|
398
|
+
}
|
|
399
|
+
else if (freshness === "fresh") {
|
|
400
|
+
const load = typeof compute.load_per_vcpu === "number"
|
|
401
|
+
? compute.load_per_vcpu
|
|
402
|
+
: typeof compute.load_1m === "number" && compute.vcpu_online > 0 ? compute.load_1m / compute.vcpu_online : null;
|
|
403
|
+
if (load !== null) {
|
|
404
|
+
computeBasis = "fresh";
|
|
405
|
+
loadPerVcpu = load;
|
|
406
|
+
computePoints = weights.compute_max * clamp(1 - load, 0, 1);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
// Locality: every repo and data need already has a passing probe here.
|
|
411
|
+
// Work with no repo or data needs gets the same neutral points everywhere.
|
|
412
|
+
const localNeeds = [
|
|
413
|
+
...work.needs.repos.map((repo) => `repo:${repo}`),
|
|
414
|
+
...(work.needs.data ?? []).map((data) => `data:${data}`),
|
|
415
|
+
];
|
|
416
|
+
let locality = 0;
|
|
417
|
+
if (localNeeds.length === 0) {
|
|
418
|
+
locality = weights.locality_no_needs_points;
|
|
419
|
+
}
|
|
420
|
+
else if (!isTemplate && localNeeds.every((name) => probePasses(readings.access, name, clock))) {
|
|
421
|
+
locality = weights.locality_points;
|
|
422
|
+
}
|
|
423
|
+
// Soft labels.
|
|
424
|
+
const prefers = work.prefers?.labels ?? [];
|
|
425
|
+
const soft = prefers.length === 0
|
|
426
|
+
? 0
|
|
427
|
+
: weights.soft_labels_max * prefers.filter((label) => declared.labels.includes(label)).length / prefers.length;
|
|
428
|
+
// Engine order.
|
|
429
|
+
const order = work.engines.any_of;
|
|
430
|
+
const place = order.indexOf(pair.engine);
|
|
431
|
+
const engineOrder = place < 0 ? 0 : weights.engine_order_max * (order.length - place) / order.length;
|
|
432
|
+
// Cost: metered environments only.
|
|
433
|
+
let cost = 0;
|
|
434
|
+
if (declared.cost.model === "metered") {
|
|
435
|
+
const perHour = declared.cost.marginal_usd_per_hour;
|
|
436
|
+
cost = perHour === null
|
|
437
|
+
? -weights.cost_cap
|
|
438
|
+
: -Math.min(weights.cost_cap, weights.cost_per_usd * perHour * work.size.expected_run_seconds / 3600);
|
|
439
|
+
}
|
|
440
|
+
// Start time: only an environment that is not running has to start.
|
|
441
|
+
let start = 0;
|
|
442
|
+
if (declared.state !== "running") {
|
|
443
|
+
const seconds = declared.lifecycle.start_seconds_estimate;
|
|
444
|
+
start = seconds === null
|
|
445
|
+
? -weights.start_unknown_penalty
|
|
446
|
+
: -Math.min(weights.start_cap, weights.start_per_minute * seconds / 60);
|
|
447
|
+
}
|
|
448
|
+
// Taints.
|
|
449
|
+
const preferNot = declared.taints.filter((taint) => taint.effect === "prefer-not" && !work.tolerates.includes(taint.key)).length;
|
|
450
|
+
const taints = -weights.taint_prefer_not_penalty * preferNot;
|
|
451
|
+
const terms = {
|
|
452
|
+
room: round2(room),
|
|
453
|
+
compute: round2(computePoints),
|
|
454
|
+
locality: round2(locality),
|
|
455
|
+
soft_labels: round2(soft),
|
|
456
|
+
engine_order: round2(engineOrder),
|
|
457
|
+
cost: round2(cost),
|
|
458
|
+
start_time: round2(start),
|
|
459
|
+
taints: round2(taints),
|
|
460
|
+
};
|
|
461
|
+
const score = round2(room + computePoints + locality + soft + engineOrder + cost + start + taints);
|
|
462
|
+
return {
|
|
463
|
+
environment_id: declared.id,
|
|
464
|
+
kind: declared.kind,
|
|
465
|
+
engine: pair.engine,
|
|
466
|
+
account_id: account ? account.account_id : null,
|
|
467
|
+
pairing: account ? "account" : "template-label",
|
|
468
|
+
holder: account ? account.holder : null,
|
|
469
|
+
owning_company: account ? account.owning_company : null,
|
|
470
|
+
score,
|
|
471
|
+
terms,
|
|
472
|
+
room: {
|
|
473
|
+
basis: roomBasis,
|
|
474
|
+
left_percent: roomReading && typeof roomReading.left_percent === "number" ? roomReading.left_percent : null,
|
|
475
|
+
observed_at: roomReading?.observed_at ?? null,
|
|
476
|
+
},
|
|
477
|
+
compute: { basis: computeBasis, load_per_vcpu: loadPerVcpu },
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* Tie-break tier: the work's own company first, then logins the owner
|
|
482
|
+
* designated for it, then his other logins and templates. This is where
|
|
483
|
+
* owning_company enters the ranking; it is never a filter.
|
|
484
|
+
*/
|
|
485
|
+
function companyTier(work, account) {
|
|
486
|
+
if (!account)
|
|
487
|
+
return 2;
|
|
488
|
+
if (account.owning_company === work.company)
|
|
489
|
+
return 0;
|
|
490
|
+
if (account.usable_for_companies?.includes(work.company))
|
|
491
|
+
return 1;
|
|
492
|
+
return 2;
|
|
493
|
+
}
|
|
494
|
+
function compareText(a, b) {
|
|
495
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* R0 for duplicate environment ids, decided independently of input order.
|
|
499
|
+
* Only profiles that are this company's view take part: another company's
|
|
500
|
+
* view fails R1 on view.company and never takes an id slot.
|
|
501
|
+
*
|
|
502
|
+
* Among copies of one id:
|
|
503
|
+
* 1. Copies whose canonical JSON is identical collapse to one: the first is
|
|
504
|
+
* the representative and the extras are rejected as its duplicates.
|
|
505
|
+
* 2. A representative with no valid view.generated_at, or one later than
|
|
506
|
+
* now plus the clock-skew allowance, is rejected and takes no part in the
|
|
507
|
+
* comparison, so it cannot hide a good copy.
|
|
508
|
+
* 3. Among the rest, the latest view.generated_at is kept and the others
|
|
509
|
+
* are rejected. When two distinct copies share the latest generated_at,
|
|
510
|
+
* every one of them is rejected rather than guessing.
|
|
511
|
+
* Returns the R0 reason for each rejected profile index.
|
|
512
|
+
*/
|
|
513
|
+
function duplicateVerdicts(work, profiles, clock) {
|
|
514
|
+
const groups = new Map();
|
|
515
|
+
profiles.forEach((profile, index) => {
|
|
516
|
+
if (profile.view.company !== work.company)
|
|
517
|
+
return;
|
|
518
|
+
const list = groups.get(profile.declared.id) ?? [];
|
|
519
|
+
list.push(index);
|
|
520
|
+
groups.set(profile.declared.id, list);
|
|
521
|
+
});
|
|
522
|
+
const verdicts = new Map();
|
|
523
|
+
const reject = (index, detail) => {
|
|
524
|
+
verdicts.set(index, { rule: "R0", field: "declared.id", detail });
|
|
525
|
+
};
|
|
526
|
+
for (const [id, indexes] of groups) {
|
|
527
|
+
if (indexes.length < 2)
|
|
528
|
+
continue;
|
|
529
|
+
// 1. Identical copies collapse to their first occurrence.
|
|
530
|
+
const representatives = [];
|
|
531
|
+
const extras = [];
|
|
532
|
+
const byContent = new Map();
|
|
533
|
+
for (const index of indexes) {
|
|
534
|
+
const key = canonicalJson(profiles[index]);
|
|
535
|
+
const first = byContent.get(key);
|
|
536
|
+
if (first === undefined) {
|
|
537
|
+
byContent.set(key, index);
|
|
538
|
+
representatives.push(index);
|
|
539
|
+
}
|
|
540
|
+
else {
|
|
541
|
+
extras.push({ index, of: first });
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
settleDistinct(id, representatives);
|
|
545
|
+
// An identical extra shares its representative's fate: when the
|
|
546
|
+
// representative is kept, the extra is its duplicate; when it is rejected,
|
|
547
|
+
// the extra is rejected for the same reason.
|
|
548
|
+
for (const extra of extras) {
|
|
549
|
+
const fate = verdicts.get(extra.of);
|
|
550
|
+
verdicts.set(extra.index, fate ?? {
|
|
551
|
+
rule: "R0",
|
|
552
|
+
field: "declared.id",
|
|
553
|
+
detail: `identical copy of the profile kept for environment_id ${id}`,
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
return verdicts;
|
|
558
|
+
function settleDistinct(id, representatives) {
|
|
559
|
+
if (representatives.length < 2)
|
|
560
|
+
return;
|
|
561
|
+
// 2. A missing, invalid or future-dated generated_at takes no part.
|
|
562
|
+
const dated = [];
|
|
563
|
+
for (const index of representatives) {
|
|
564
|
+
const time = Date.parse(profiles[index].view.generated_at ?? "");
|
|
565
|
+
if (!Number.isFinite(time)) {
|
|
566
|
+
reject(index, `duplicate environment_id ${id} with no valid view.generated_at; a dated copy is preferred`);
|
|
567
|
+
}
|
|
568
|
+
else if (time - clock.nowMs > clock.skewMs) {
|
|
569
|
+
reject(index, `duplicate environment_id ${id} with view.generated_at later than now plus the clock-skew allowance`);
|
|
570
|
+
}
|
|
571
|
+
else {
|
|
572
|
+
dated.push({ index, time });
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
if (dated.length === 0)
|
|
576
|
+
return;
|
|
577
|
+
// 3. Latest wins; a tie at the latest rejects every dated copy.
|
|
578
|
+
const latest = Math.max(...dated.map((entry) => entry.time));
|
|
579
|
+
const atLatest = dated.filter((entry) => entry.time === latest);
|
|
580
|
+
if (atLatest.length > 1) {
|
|
581
|
+
for (const entry of dated) {
|
|
582
|
+
reject(entry.index, `${dated.length} distinct profiles of this company have environment_id ${id} and no single latest view.generated_at; every copy is rejected`);
|
|
583
|
+
}
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
const kept = atLatest[0].index;
|
|
587
|
+
for (const entry of dated) {
|
|
588
|
+
if (entry.index === kept)
|
|
589
|
+
continue;
|
|
590
|
+
reject(entry.index, `duplicate environment_id ${id}; the copy with view.generated_at ${profiles[kept].view.generated_at} is used`);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
/** JSON with object keys sorted at every level, so key order does not matter. */
|
|
595
|
+
function canonicalJson(value) {
|
|
596
|
+
if (Array.isArray(value))
|
|
597
|
+
return `[${value.map(canonicalJson).join(",")}]`;
|
|
598
|
+
if (value !== null && typeof value === "object") {
|
|
599
|
+
const record = value;
|
|
600
|
+
return `{${Object.keys(record).sort().map((key) => `${JSON.stringify(key)}:${canonicalJson(record[key])}`).join(",")}}`;
|
|
601
|
+
}
|
|
602
|
+
return JSON.stringify(value) ?? "null";
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* Filter environments for one piece of work and rank the pairs that remain.
|
|
606
|
+
* Pure: the same inputs and `now` always give the same result.
|
|
607
|
+
*/
|
|
608
|
+
export function match(requirements, profiles, options) {
|
|
609
|
+
const nowMs = parseNow(options.now);
|
|
610
|
+
assertInputs(requirements, profiles);
|
|
611
|
+
const weights = { ...MATCH_WEIGHTS, ...(options.weights ?? {}) };
|
|
612
|
+
if (typeof weights.clock_skew_seconds !== "number" || !Number.isFinite(weights.clock_skew_seconds) || weights.clock_skew_seconds < 0) {
|
|
613
|
+
throw new Error("match: weights.clock_skew_seconds must be a non-negative number");
|
|
614
|
+
}
|
|
615
|
+
const clock = { nowMs, skewMs: weights.clock_skew_seconds * 1000 };
|
|
616
|
+
const feasible = [];
|
|
617
|
+
const rejected = [];
|
|
618
|
+
const scored = [];
|
|
619
|
+
const duplicates = duplicateVerdicts(requirements, profiles, clock);
|
|
620
|
+
for (const [index, profile] of profiles.entries()) {
|
|
621
|
+
const { declared } = profile;
|
|
622
|
+
const duplicate = duplicates.get(index);
|
|
623
|
+
if (duplicate) {
|
|
624
|
+
rejected.push({ subject: "environment", environment_id: declared.id, reason: duplicate });
|
|
625
|
+
continue;
|
|
626
|
+
}
|
|
627
|
+
const result = pass1(requirements, profile, clock);
|
|
628
|
+
if (!result.ok) {
|
|
629
|
+
rejected.push({ subject: "environment", environment_id: declared.id, reason: result.reason });
|
|
630
|
+
rejected.push(...result.pairRejections);
|
|
631
|
+
continue;
|
|
632
|
+
}
|
|
633
|
+
rejected.push(...result.pairRejections);
|
|
634
|
+
feasible.push({
|
|
635
|
+
environment_id: declared.id,
|
|
636
|
+
kind: declared.kind,
|
|
637
|
+
state: declared.state,
|
|
638
|
+
flags: result.flags,
|
|
639
|
+
pairs: result.pairs.length,
|
|
640
|
+
});
|
|
641
|
+
for (const pair of result.pairs) {
|
|
642
|
+
scored.push({
|
|
643
|
+
entry: { ...scorePair(requirements, profile, pair, weights, clock), flags: result.flags },
|
|
644
|
+
tier: companyTier(requirements, pair.account),
|
|
645
|
+
place: requirements.engines.any_of.indexOf(pair.engine),
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
scored.sort((a, b) => b.entry.score - a.entry.score
|
|
650
|
+
|| a.tier - b.tier
|
|
651
|
+
|| compareText(a.entry.environment_id, b.entry.environment_id)
|
|
652
|
+
|| a.place - b.place
|
|
653
|
+
|| compareText(a.entry.account_id ?? "", b.entry.account_id ?? ""));
|
|
654
|
+
return {
|
|
655
|
+
schema_version: MATCH_RESULT_SCHEMA_VERSION,
|
|
656
|
+
output: "first-ranking",
|
|
657
|
+
notice: MATCH_FIRST_RANKING_NOTICE,
|
|
658
|
+
work_id: requirements.id,
|
|
659
|
+
company: requirements.company,
|
|
660
|
+
now: new Date(nowMs).toISOString(),
|
|
661
|
+
weights,
|
|
662
|
+
feasible,
|
|
663
|
+
rejected,
|
|
664
|
+
ranked: scored.map((item, index) => ({ rank: index + 1, ...item.entry })),
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
//# sourceMappingURL=match.js.map
|