Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(control-ui): resolve one authoritative SecretResolved condition (R1-M2)

Jose Ramirezff6f21b2fba0
diff --git a/control-ui/components/UpdateConnectorCredentials/resolveCredentialSurface.ts b/control-ui/components/UpdateConnectorCredentials/resolveCredentialSurface.ts
index 1c6ecbac9..43618ac51 100644
--- a/control-ui/components/UpdateConnectorCredentials/resolveCredentialSurface.ts
+++ b/control-ui/components/UpdateConnectorCredentials/resolveCredentialSurface.ts
@@ -10,24 +10,115 @@ import type { McpServerCondition } from '@lib/api'
*/
export type CredentialSurface = 'set' | 'rotate' | 'recipe-owned'
+/**
+ * A WorkflowRecipe-owned connector: `spec.managed === false`.
+ *
+ * This is an OWNERSHIP fact about the connector, and it is deliberately
+ * independent of any observed status condition. The credential screen needs it
+ * that way: a connector whose status has not been written yet (or is stale)
+ * resolves to `rotate`, and only the PUT's own 404 later reveals the Secret is
+ * absent. If ownership were re-derived from that observation, the late 404
+ * would flip a recipe-owned connector into the create form and let the operator
+ * POST an unlabeled Secret outside the recipe flow.
+ *
+ * Exported so the page and the resolver share one definition instead of two
+ * copies of `managed === false` that can drift apart.
+ */
+export function isRecipeOwned(spec: { managed?: boolean } | undefined): boolean {
+ return spec?.managed === false
+}
+
+/**
+ * Returns the ONE authoritative `SecretResolved` condition.
+ *
+ * The McpServer CRD declares `status.conditions` as an ordinary array and does
+ * NOT enforce uniqueness per `type`. HCC rewrites a type in place, but legacy
+ * or hand-edited resources can carry two contradictory `SecretResolved`
+ * entries. Asking `.some()` whether ANY of them proves absence lets an
+ * arbitrarily stale entry win, which forces every key on the operator and
+ * re-enables POST against a Secret that already exists.
+ *
+ * Ordering, from strongest to weakest key:
+ *
+ * 1. Newest parseable `lastTransitionTime` wins — recency is the only real
+ * evidence of which statement the controller made last.
+ * 2. An absent, empty or unparseable timestamp ranks as the OLDEST possible.
+ * It carries no recency proof, so it must never outrank a well-stamped
+ * condition — in particular a malformed absence claim can never beat an
+ * observed resolution.
+ * 3. On an exact tie, the condition that does NOT claim absence wins.
+ * Ambiguity must resolve away from "the Secret is missing": that verdict is
+ * the destructive one (it demands every key and re-opens the create path),
+ * while the rotate verdict is self-correcting — the PUT answers 404 if the
+ * Secret really is gone, and the screen recovers from there.
+ * 4. Still tied — every remaining candidate claims absence and therefore
+ * agrees — so take the last array entry for determinism.
+ */
+function selectAuthoritativeSecretResolved(
+ conditions: McpServerCondition[] | undefined
+): McpServerCondition | undefined {
+ let best: McpServerCondition | undefined
+ let bestTime = Number.NEGATIVE_INFINITY
+
+ for (const candidate of conditions ?? []) {
+ if (candidate.type !== 'SecretResolved') continue
+ const parsed = new Date(candidate.lastTransitionTime ?? '').getTime()
+ const time = Number.isNaN(parsed) ? Number.NEGATIVE_INFINITY : parsed
+
+ if (!best) {
+ best = candidate
+ bestTime = time
+ continue
+ }
+ if (time > bestTime) {
+ best = candidate
+ bestTime = time
+ continue
+ }
+ if (time < bestTime) continue
+ // Exact tie (including two unparseable timestamps): rule 3, then rule 4.
+ if (claimsAbsence(best) && !claimsAbsence(candidate)) {
+ best = candidate
+ bestTime = time
+ continue
+ }
+ if (!claimsAbsence(best) && claimsAbsence(candidate)) continue
+ best = candidate
+ bestTime = time
+ }
+
+ return best
+}
+
/**
* Matching on `reason` is load-bearing. `SecretNotFound` means the Secret does
* not exist and must be created. `SecretMissingKey` means it EXISTS but lacks a
* declared key — the PUT merge-patch already adds it, so that case must stay on
* `rotate`. Matching only type+status would send it to `set`, where POST hits
* AlreadyExists and control-api answers a bare 500 (see spec Non-goals).
+ */
+function claimsAbsence(condition: McpServerCondition): boolean {
+ return condition.status === 'False' && condition.reason === 'SecretNotFound'
+}
+
+/**
+ * Which surface the connector credential screen should render, derived from the
+ * authoritative `SecretResolved` condition (see above) and `spec.managed`.
*
* The `managed: false` check is applied ONLY when the Secret is missing. A
* WRC-owned connector whose Secret resolves keeps today's rotate form; this
* change fixes a dead end and does not restrict rotation that already works.
+ *
+ * NOTE: a `rotate` result here is NOT proof that the connector may create a
+ * Secret. Absent or stale status yields `rotate` for recipe-owned connectors
+ * too, so the create path must be gated on `isRecipeOwned(spec)` separately —
+ * never on this value.
*/
export function resolveCredentialSurface(
conditions: McpServerCondition[] | undefined,
spec: { managed?: boolean } | undefined
): CredentialSurface {
- const missing = (conditions ?? []).some(
- c => c.type === 'SecretResolved' && c.status === 'False' && c.reason === 'SecretNotFound'
- )
- if (!missing) return 'rotate'
- return spec?.managed === false ? 'recipe-owned' : 'set'
+ const authoritative = selectAuthoritativeSecretResolved(conditions)
+ if (!authoritative || !claimsAbsence(authoritative)) return 'rotate'
+ return isRecipeOwned(spec) ? 'recipe-owned' : 'set'
}
diff --git a/control-ui/components/__tests__/resolveCredentialSurface.test.ts b/control-ui/components/__tests__/resolveCredentialSurface.test.ts
index f8b974354..b2a391ca7 100644
--- a/control-ui/components/__tests__/resolveCredentialSurface.test.ts
+++ b/control-ui/components/__tests__/resolveCredentialSurface.test.ts
@@ -1,5 +1,8 @@
import { describe, expect, it } from 'vitest'
-import { resolveCredentialSurface } from '@components/UpdateConnectorCredentials/resolveCredentialSurface'
+import {
+ isRecipeOwned,
+ resolveCredentialSurface,
+} from '@components/UpdateConnectorCredentials/resolveCredentialSurface'
import type { McpServerCondition } from '@lib/api'
function condition(overrides: Partial<McpServerCondition> = {}): McpServerCondition {
@@ -78,3 +81,204 @@ describe('resolveCredentialSurface', () => {
).toBe('rotate')
})
})
+
+// ─── R1-M2: duplicate SecretResolved conditions ────────────────────────────
+//
+// The McpServer CRD declares an ordinary conditions array and does NOT enforce
+// uniqueness per `type`. HCC normally rewrites a type in place, but a legacy or
+// hand-edited resource can carry two SecretResolved entries that contradict
+// each other. A `.some()` predicate lets ANY stale absence claim win, which
+// forces every key and re-enables POST against a Secret that already exists.
+//
+// The resolver therefore selects ONE authoritative SecretResolved condition:
+// 1. newest parseable `lastTransitionTime` wins;
+// 2. an absent/unparseable timestamp ranks as the OLDEST possible, so a
+// malformed absence claim can never outrank a well-stamped resolution;
+// 3. on an exact tie, the condition that does NOT claim absence wins —
+// ambiguity must never resolve to the destructive "the Secret is missing";
+// 4. still tied (they all claim absence, so they agree): last array entry.
+describe('resolveCredentialSurface — duplicate SecretResolved conditions', () => {
+ it('returns "rotate" when a NEWER duplicate says the Secret resolved cleanly', () => {
+ expect(
+ resolveCredentialSurface(
+ [
+ condition({ lastTransitionTime: '2026-08-06T04:00:00.000Z' }),
+ condition({
+ status: 'True',
+ reason: 'SecretResolved',
+ lastTransitionTime: '2026-08-06T05:00:00.000Z',
+ }),
+ ],
+ { managed: true }
+ )
+ ).toBe('rotate')
+ })
+
+ it('returns "rotate" when a NEWER duplicate says SecretMissingKey', () => {
+ expect(
+ resolveCredentialSurface(
+ [
+ condition({ lastTransitionTime: '2026-08-06T04:00:00.000Z' }),
+ condition({ reason: 'SecretMissingKey', lastTransitionTime: '2026-08-06T05:00:00.000Z' }),
+ ],
+ { managed: true }
+ )
+ ).toBe('rotate')
+ })
+
+ // Array order must not decide anything: the same pair, reversed.
+ it('returns "rotate" when the newer clean duplicate is listed FIRST', () => {
+ expect(
+ resolveCredentialSurface(
+ [
+ condition({
+ status: 'True',
+ reason: 'SecretResolved',
+ lastTransitionTime: '2026-08-06T05:00:00.000Z',
+ }),
+ condition({ lastTransitionTime: '2026-08-06T04:00:00.000Z' }),
+ ],
+ { managed: true }
+ )
+ ).toBe('rotate')
+ })
+
+ it('returns "set" when the NEWEST duplicate is the SecretNotFound one', () => {
+ expect(
+ resolveCredentialSurface(
+ [
+ condition({
+ status: 'True',
+ reason: 'SecretResolved',
+ lastTransitionTime: '2026-08-06T04:00:00.000Z',
+ }),
+ condition({ lastTransitionTime: '2026-08-06T05:00:00.000Z' }),
+ ],
+ { managed: true }
+ )
+ ).toBe('set')
+ })
+
+ it('returns "recipe-owned" when the newest duplicate is SecretNotFound on a WRC-owned connector', () => {
+ expect(
+ resolveCredentialSurface(
+ [
+ condition({
+ status: 'True',
+ reason: 'SecretResolved',
+ lastTransitionTime: '2026-08-06T04:00:00.000Z',
+ }),
+ condition({ lastTransitionTime: '2026-08-06T05:00:00.000Z' }),
+ ],
+ { managed: false }
+ )
+ ).toBe('recipe-owned')
+ })
+
+ // Tie-break rule 3, both orders: an exact timestamp tie is ambiguous, and
+ // ambiguity resolves AWAY from absence.
+ it('returns "rotate" when contradictory duplicates share the exact same timestamp', () => {
+ const same = '2026-08-06T04:00:00.000Z'
+ expect(
+ resolveCredentialSurface(
+ [
+ condition({ lastTransitionTime: same }),
+ condition({ status: 'True', reason: 'SecretResolved', lastTransitionTime: same }),
+ ],
+ { managed: true }
+ )
+ ).toBe('rotate')
+ expect(
+ resolveCredentialSurface(
+ [
+ condition({ status: 'True', reason: 'SecretResolved', lastTransitionTime: same }),
+ condition({ lastTransitionTime: same }),
+ ],
+ { managed: true }
+ )
+ ).toBe('rotate')
+ })
+
+ // Tie-break rule 2. An unparseable timestamp carries no recency proof, so it
+ // must not let a stale absence claim outrank a well-stamped resolution.
+ it('returns "rotate" when the SecretNotFound duplicate carries an unparseable timestamp', () => {
+ expect(
+ resolveCredentialSurface(
+ [
+ condition({ lastTransitionTime: 'not-a-timestamp' }),
+ condition({
+ status: 'True',
+ reason: 'SecretResolved',
+ lastTransitionTime: '2026-08-06T04:00:00.000Z',
+ }),
+ ],
+ { managed: true }
+ )
+ ).toBe('rotate')
+ })
+
+ it('returns "rotate" when BOTH contradictory duplicates carry unparseable timestamps', () => {
+ expect(
+ resolveCredentialSurface(
+ [
+ condition({ lastTransitionTime: '' }),
+ condition({
+ status: 'True',
+ reason: 'SecretResolved',
+ lastTransitionTime: 'whenever',
+ }),
+ ],
+ { managed: true }
+ )
+ ).toBe('rotate')
+ })
+
+ // A LONE absence claim with a malformed timestamp still means "missing":
+ // there is nothing contradicting it, so today's behavior is preserved.
+ it('returns "set" for a lone SecretNotFound with an unparseable timestamp', () => {
+ expect(
+ resolveCredentialSurface([condition({ lastTransitionTime: 'not-a-timestamp' })], {
+ managed: true,
+ })
+ ).toBe('set')
+ })
+
+ // Unrelated condition types must not participate in the selection at all —
+ // not as candidates, and not by shifting array order.
+ it('ignores unrelated condition types regardless of their position or recency', () => {
+ const conditions: McpServerCondition[] = [
+ {
+ type: 'DeploymentReady',
+ status: 'False',
+ reason: 'SecretNotFound',
+ message: 'noise',
+ lastTransitionTime: '2026-08-06T09:00:00.000Z',
+ },
+ condition({ lastTransitionTime: '2026-08-06T04:00:00.000Z' }),
+ {
+ type: 'Ready',
+ status: 'True',
+ reason: 'SecretResolved',
+ message: 'noise',
+ lastTransitionTime: '2026-08-06T09:00:00.000Z',
+ },
+ condition({
+ status: 'True',
+ reason: 'SecretResolved',
+ lastTransitionTime: '2026-08-06T05:00:00.000Z',
+ }),
+ ]
+ expect(resolveCredentialSurface(conditions, { managed: true })).toBe('rotate')
+ // Same four conditions, reversed: the verdict is order-independent.
+ expect(resolveCredentialSurface([...conditions].reverse(), { managed: true })).toBe('rotate')
+ })
+})
+
+describe('isRecipeOwned', () => {
+ it('is true only for an explicit managed:false spec', () => {
+ expect(isRecipeOwned({ managed: false })).toBe(true)
+ expect(isRecipeOwned({ managed: true })).toBe(false)
+ expect(isRecipeOwned({})).toBe(false)
+ expect(isRecipeOwned(undefined)).toBe(false)
+ })
+})
diff --git a/control-ui/package-lock.json b/control-ui/package-lock.json
index 5766a89e7..ba2acd63f 100644
--- a/control-ui/package-lock.json
+++ b/control-ui/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-ui",
- "version": "0.1.395",
+ "version": "0.1.396",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-ui",
- "version": "0.1.395",
+ "version": "0.1.396",
"dependencies": {
"@clerum/llm-providers": "file:../packages/llm-providers",
"@clerum/workflow-recipe-capability-policy": "file:../packages/workflow-recipe-capability-policy",
diff --git a/control-ui/package.json b/control-ui/package.json
index 7fbd77fb8..cd30c73a1 100644
--- a/control-ui/package.json
+++ b/control-ui/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-ui",
- "version": "0.1.395",
+ "version": "0.1.396",
"private": true,
"scripts": {
"dev": "NEXT_IGNORE_INCORRECT_LOCKFILE=1 next dev",