@ran-sh/dsh-crew 1.0.1 → 1.0.3

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.
@@ -1,132 +1,184 @@
1
- // Secret-free inventory projection for Harness providers.
2
- //
3
- // Harness owns the live provider/model catalog; Crew owns the routing policy
4
- // and (where explicitly marked) profile provenance. This module only joins
5
- // those observations into a bounded record set for lifecycle/readiness UIs.
6
-
7
- const LIFECYCLE_ORIGINS = new Set(['builtin', 'profile-managed', 'dynamic', 'unknown']);
8
- const OWNERSHIPS = new Set(['harness', 'crew-managed-profile', 'user-managed-profile', 'dynamic-user', 'unknown']);
9
-
10
- function text(value) {
11
- return typeof value === 'string' && value.trim() ? value.trim() : null;
12
- }
13
-
14
- function normalizeProviderId(value) {
15
- return text(value);
16
- }
17
-
18
- function normalizeCredentialRefs(declaration) {
19
- const raw = declaration?.credential_refs ?? declaration?.credential_ref;
20
- const values = Array.isArray(raw) ? raw : raw === undefined ? [] : [raw];
21
- const seen = new Set();
22
- const refs = [];
23
- for (const candidate of values) {
24
- const name = text(typeof candidate === 'object' ? candidate.name_or_handle ?? candidate.name : candidate);
25
- if (!name || seen.has(name)) continue;
26
- seen.add(name);
27
- const kind = text(typeof candidate === 'object' ? candidate.kind : null) ?? 'env';
28
- const ownership = text(typeof candidate === 'object' ? candidate.ownership : null) ?? 'crew';
29
- refs.push({
30
- kind: ['env', 'crew-store', 'harness-store', 'unknown'].includes(kind) ? kind : 'unknown',
31
- name_or_handle: name,
32
- ownership: ['crew', 'user', 'external', 'unknown'].includes(ownership) ? ownership : 'unknown',
33
- });
34
- }
35
- return refs;
36
- }
37
-
38
- function findPriorityIndex(policy, key, providerId) {
39
- const list = Array.isArray(policy?.[key]) ? policy[key] : [];
40
- const index = list.findIndex((entry) => normalizeProviderId(entry?.provider) === providerId);
41
- return index >= 0 ? index : null;
42
- }
43
-
44
- function declarationFor(declarations, id) {
45
- return declarations.find((entry) => normalizeProviderId(entry?.id) === id) ?? null;
46
- }
47
-
48
- function catalogFor(providers, id) {
49
- return providers.find((entry) => normalizeProviderId(entry?.id) === id) ?? null;
50
- }
51
-
52
- function normalizeOrigin(value) {
53
- const origin = text(value) ?? 'unknown';
54
- return LIFECYCLE_ORIGINS.has(origin) ? origin : 'unknown';
55
- }
56
-
57
- function normalizeOwnership(value) {
58
- const ownership = text(value) ?? 'unknown';
59
- return OWNERSHIPS.has(ownership) ? ownership : 'unknown';
60
- }
61
-
62
- function modelIds(provider) {
63
- if (!Array.isArray(provider?.models)) return [];
64
- return [...new Set(provider.models.map((model) => text(typeof model === 'object' ? model?.id : model)).filter(Boolean))];
65
- }
66
-
67
- /**
68
- * Build a secret-free provider inventory from live Harness and Crew-owned
69
- * observations. No credential value is accepted or copied into the result.
70
- */
71
- export function buildProviderInventory({ catalog = {}, declarations = [], policy = {}, tombstones = {}, activeJobs = [], multimodalRefs = {} } = {}) {
72
- const catalogProviders = Array.isArray(catalog?.providers) ? catalog.providers : [];
73
- const safeDeclarations = Array.isArray(declarations) ? declarations : [];
74
- const ids = [];
75
- const seen = new Set();
76
- for (const source of [catalogProviders, safeDeclarations]) {
77
- for (const entry of source) {
78
- const id = normalizeProviderId(entry?.id);
79
- if (!id || seen.has(id)) continue;
80
- seen.add(id);
81
- ids.push(id);
82
- }
83
- }
84
-
85
- const harnessDefault = normalizeProviderId(catalog?.harness_default?.provider);
86
- const records = ids.map((id) => {
87
- const catalogEntry = catalogFor(catalogProviders, id);
88
- const declaration = declarationFor(safeDeclarations, id);
89
- const declared = declaration !== null;
90
- const catalogued = catalogEntry !== null;
91
- const tombstoned = tombstones?.[id] === 'absent';
92
- const configured = declared;
93
- const active = Array.isArray(activeJobs)
94
- ? activeJobs.filter((job) => normalizeProviderId(job?.provider) === id).length
95
- : Number.isInteger(activeJobs?.[id]) ? Math.max(0, activeJobs[id]) : 0;
96
- const multimodal = Array.isArray(multimodalRefs?.[id]) ? multimodalRefs[id].length : Number.isInteger(multimodalRefs?.[id]) ? Math.max(0, multimodalRefs[id]) : 0;
97
- const declarationFile = text(declaration?.file);
98
- const locator = text(declaration?.locator);
99
-
100
- return {
101
- id,
102
- display_name: text(declaration?.display_name) ?? text(declaration?.name) ?? text(catalogEntry?.name) ?? id,
103
- origin: normalizeOrigin(declaration?.origin ?? (declared ? 'profile-managed' : 'builtin')),
104
- ownership: normalizeOwnership(declaration?.ownership ?? (declared ? 'crew-managed-profile' : 'harness')),
105
- declaration: {
106
- ...(declarationFile ? { file: declarationFile } : {}),
107
- ...(locator ? { locator } : {}),
108
- present: declared,
109
- },
110
- models: modelIds(catalogEntry),
111
- lifecycle: {
112
- installed: declared || catalogued,
113
- configured,
114
- enabled: !tombstoned && declaration?.enabled !== false,
115
- catalogued,
116
- },
117
- credential_refs: normalizeCredentialRefs(declaration),
118
- references: {
119
- harness_default: harnessDefault === id,
120
- worker_priority: findPriorityIndex(policy?.worker, 'priority', id),
121
- worker_escalation: findPriorityIndex(policy?.worker, 'escalation_priority', id),
122
- reviewer_priority: findPriorityIndex(policy?.reviewer ?? policy?.review, 'priority', id),
123
- active_jobs: active,
124
- multimodal_refs: multimodal,
125
- },
126
- desired_state: tombstoned ? 'absent' : 'present',
127
- activation: declared ? 'restart-required' : 'live',
128
- };
129
- });
130
-
131
- return { schema_version: 1, records };
132
- }
1
+ // Secret-free inventory projection for Harness providers.
2
+ //
3
+ // Harness owns the live provider/model catalog; Crew owns the routing policy
4
+ // and (where explicitly marked) profile provenance. This module only joins
5
+ // those observations into a bounded record set for lifecycle/readiness UIs.
6
+
7
+ import { classifyCredentialReference } from './credential-reference.mjs';
8
+
9
+ const LIFECYCLE_ORIGINS = new Set(['builtin', 'profile-managed', 'dynamic', 'unknown']);
10
+ const OWNERSHIPS = new Set(['harness', 'crew-managed-profile', 'user-managed-profile', 'dynamic-user', 'unknown']);
11
+ const MUTABLE_AUTHORITY_KINDS = new Set(['crew-profile', 'harness-settings']);
12
+ export const IMMUTABLE_HARNESS_PROVIDER_IDS = Object.freeze(['deepseek-official']);
13
+
14
+ function text(value) {
15
+ return typeof value === 'string' && value.trim() ? value.trim() : null;
16
+ }
17
+
18
+ function normalizeProviderId(value) {
19
+ return text(value);
20
+ }
21
+
22
+ function normalizeCredentialRefs(declarations) {
23
+ const entries = Array.isArray(declarations) ? declarations : [declarations];
24
+ const seen = new Map();
25
+ const refs = [];
26
+ let redacted = false;
27
+ for (const declaration of entries) {
28
+ const raw = declaration?.credential_refs ?? declaration?.credential_ref;
29
+ const values = Array.isArray(raw) ? raw : raw === undefined ? [] : [raw];
30
+ for (const candidate of values) {
31
+ const kind = text(typeof candidate === 'object' ? candidate.kind : null) ?? 'env';
32
+ const classified = classifyCredentialReference(candidate, { kind });
33
+ if (classified.redacted) redacted = true;
34
+ const name = classified.value;
35
+ const ownership = text(typeof candidate === 'object' ? candidate.ownership : null) ?? 'crew';
36
+ const normalized = {
37
+ kind: ['env', 'crew-store', 'harness-store', 'unknown'].includes(kind) ? kind : 'unknown',
38
+ name_or_handle: name,
39
+ ownership: ['crew', 'user', 'external', 'unknown'].includes(ownership) ? ownership : 'unknown',
40
+ };
41
+ if (!name) continue;
42
+ const key = `${normalized.kind}:${normalized.name_or_handle}`;
43
+ const existing = seen.get(key);
44
+ if (existing) {
45
+ if (existing.ownership !== normalized.ownership) existing.ownership = 'unknown';
46
+ } else {
47
+ seen.set(key, normalized);
48
+ refs.push(normalized);
49
+ }
50
+ }
51
+ }
52
+ return { refs, redacted };
53
+ }
54
+
55
+ function findPriorityIndex(policy, key, providerId) {
56
+ const list = Array.isArray(policy?.[key]) ? policy[key] : [];
57
+ const index = list.findIndex((entry) => normalizeProviderId(entry?.provider) === providerId);
58
+ return index >= 0 ? index : null;
59
+ }
60
+
61
+ function declarationsFor(declarations, id) {
62
+ return declarations.filter((entry) => normalizeProviderId(entry?.id) === id);
63
+ }
64
+
65
+ function normalizeAuthority(value) {
66
+ if (!value || typeof value !== 'object' || Array.isArray(value)) return null;
67
+ const kind = text(value.kind);
68
+ const locator = text(value.locator);
69
+ return kind && locator ? { kind, locator } : null;
70
+ }
71
+
72
+ function catalogFor(providers, id) {
73
+ return providers.find((entry) => normalizeProviderId(entry?.id) === id) ?? null;
74
+ }
75
+
76
+ function normalizeOrigin(value) {
77
+ const origin = text(value) ?? 'unknown';
78
+ return LIFECYCLE_ORIGINS.has(origin) ? origin : 'unknown';
79
+ }
80
+
81
+ function normalizeOwnership(value) {
82
+ const ownership = text(value) ?? 'unknown';
83
+ return OWNERSHIPS.has(ownership) ? ownership : 'unknown';
84
+ }
85
+
86
+ function modelIds(provider) {
87
+ if (!Array.isArray(provider?.models)) return [];
88
+ return [...new Set(provider.models.map((model) => text(typeof model === 'object' ? model?.id : model)).filter(Boolean))];
89
+ }
90
+
91
+ /**
92
+ * Build a secret-free provider inventory from live Harness and Crew-owned
93
+ * observations. No credential value is accepted or copied into the result.
94
+ */
95
+ export function buildProviderInventory({ catalog = {}, declarations = [], policy = {}, tombstones = {}, activeJobs = [], multimodalRefs = {}, additionalProviderIds = [] } = {}) {
96
+ const catalogProviders = Array.isArray(catalog?.providers) ? catalog.providers : [];
97
+ const safeDeclarations = Array.isArray(declarations) ? declarations : [];
98
+ const ids = [];
99
+ const seen = new Set();
100
+ for (const source of [catalogProviders, safeDeclarations, Array.isArray(additionalProviderIds) ? additionalProviderIds.map((id) => ({ id })) : []]) {
101
+ for (const entry of source) {
102
+ const id = normalizeProviderId(entry?.id);
103
+ if (!id || seen.has(id)) continue;
104
+ seen.add(id);
105
+ ids.push(id);
106
+ }
107
+ }
108
+
109
+ const harnessDefault = normalizeProviderId(catalog?.harness_default?.provider);
110
+ const records = ids.map((id) => {
111
+ const catalogEntry = catalogFor(catalogProviders, id);
112
+ const providerDeclarations = declarationsFor(safeDeclarations, id);
113
+ const baseDeclarations = providerDeclarations.filter((entry) => normalizeAuthority(entry?.declaration_authority)?.kind === 'crew-profile');
114
+ const userDeclarations = providerDeclarations.filter((entry) => normalizeAuthority(entry?.declaration_authority)?.kind === 'harness-settings');
115
+ const declaration = providerDeclarations[0] ?? null;
116
+ const declared = declaration !== null;
117
+ const catalogued = catalogEntry !== null;
118
+ const tombstoned = tombstones?.[id] === 'absent';
119
+ const configured = declared;
120
+ const active = Array.isArray(activeJobs)
121
+ ? activeJobs.filter((job) => normalizeProviderId(job?.provider) === id
122
+ && !['done', 'failed', 'cancelled'].includes(job?.status)).length
123
+ : Number.isInteger(activeJobs?.[id]) ? Math.max(0, activeJobs[id]) : 0;
124
+ const multimodal = Array.isArray(multimodalRefs?.[id]) ? multimodalRefs[id].length : Number.isInteger(multimodalRefs?.[id]) ? Math.max(0, multimodalRefs[id]) : 0;
125
+ const declarationFile = text(declaration?.file);
126
+ const locator = text(declaration?.locator);
127
+
128
+ const authorities = providerDeclarations.map((entry) => normalizeAuthority(entry.declaration_authority)).filter(Boolean);
129
+ const uniqueAuthorities = [...new Map(authorities.map((authority) => [`${authority.kind}:${authority.locator}`, authority])).values()];
130
+ const mutableAuthorities = uniqueAuthorities.filter((authority) => MUTABLE_AUTHORITY_KINDS.has(authority.kind));
131
+ const immutable = IMMUTABLE_HARNESS_PROVIDER_IDS.includes(id);
132
+ const allAuthoritiesKnown = providerDeclarations.length > 0
133
+ && uniqueAuthorities.length === providerDeclarations.length
134
+ && uniqueAuthorities.every((authority) => MUTABLE_AUTHORITY_KINDS.has(authority.kind));
135
+ const deleteCapability = immutable ? 'immutable-builtin' : allAuthoritiesKnown && mutableAuthorities.length > 0 ? 'supported' : 'source-unresolved';
136
+ const deleteBlocker = immutable ? 'PROVIDER_BUILTIN_IMMUTABLE' : deleteCapability === 'supported' ? null : 'PROVIDER_DELETE_SOURCE_UNRESOLVED';
137
+ const origin = normalizeOrigin(declaration?.origin ?? (immutable ? 'builtin' : declared ? 'profile-managed' : 'unknown'));
138
+ const ownership = normalizeOwnership(declaration?.ownership ?? (immutable ? 'harness' : declared ? 'crew-managed-profile' : 'unknown'));
139
+ const credentialProjection = normalizeCredentialRefs(providerDeclarations);
140
+ return {
141
+ id,
142
+ display_name: text(declaration?.display_name) ?? text(declaration?.name) ?? text(catalogEntry?.name) ?? id,
143
+ origin,
144
+ ownership,
145
+ declaration: {
146
+ ...(declarationFile ? { file: declarationFile } : {}),
147
+ ...(locator ? { locator } : {}),
148
+ ...(normalizeAuthority(declaration?.declaration_authority) ? { authority: normalizeAuthority(declaration.declaration_authority) } : {}),
149
+ present: declared,
150
+ },
151
+ declaration_authorities: uniqueAuthorities,
152
+ layers: {
153
+ base: [...new Set(baseDeclarations.map((entry) => normalizeAuthority(entry.declaration_authority)?.kind).filter(Boolean))],
154
+ user: [...new Set(userDeclarations.map((entry) => normalizeAuthority(entry.declaration_authority)?.kind).filter(Boolean))],
155
+ },
156
+ native_removable: userDeclarations.length > 0 && baseDeclarations.length === 0,
157
+ delete_capability: deleteCapability,
158
+ ...(deleteBlocker ? { delete_blocker: deleteBlocker } : {}),
159
+ models: modelIds(catalogEntry),
160
+ lifecycle: {
161
+ installed: declared || catalogued,
162
+ configured,
163
+ enabled: !tombstoned && declaration?.enabled !== false,
164
+ catalogued,
165
+ },
166
+ credential_refs: credentialProjection.refs,
167
+ ...(credentialProjection.redacted ? { credential_status: 'present-redacted' } : {}),
168
+ references: {
169
+ harness_default: harnessDefault === id,
170
+ ...(harnessDefault === id && normalizeAuthority(catalog?.harness_default_authority) ? { harness_default_authority: normalizeAuthority(catalog.harness_default_authority) } : {}),
171
+ worker_priority: findPriorityIndex(policy?.worker, 'priority', id),
172
+ worker_escalation: findPriorityIndex(policy?.worker, 'escalation_priority', id),
173
+ reviewer_priority: findPriorityIndex(policy?.reviewer ?? policy?.review, 'priority', id),
174
+ reviewer_escalation: findPriorityIndex(policy?.reviewer ?? policy?.review, 'escalation_priority', id),
175
+ active_jobs: active,
176
+ multimodal_refs: multimodal,
177
+ },
178
+ desired_state: tombstoned ? 'absent' : 'present',
179
+ activation: declared ? 'restart-required' : 'live',
180
+ };
181
+ });
182
+
183
+ return { schema_version: 1, records, harness_default: catalog?.harness_default ?? null };
184
+ }