diff --git a/mint-ensemble-manager/src/api/api-v1/services/unifiedExecutionRestart.test.ts b/mint-ensemble-manager/src/api/api-v1/services/unifiedExecutionRestart.test.ts new file mode 100644 index 00000000..5b095774 --- /dev/null +++ b/mint-ensemble-manager/src/api/api-v1/services/unifiedExecutionRestart.test.ts @@ -0,0 +1,332 @@ +/** + * Running a model again after a failed run: for each kind of plan, planning + * again after the failed run gives a new plan id and so a new run record, while + * submitting one plan twice still returns its one run. The adapter is made to + * answer with the same ids every time, so a new run never depends on it varying. + */ +import { createUnifiedExecutionService, ParentRunResponse } from "./unifiedExecutionService"; +import { + UnifiedExecutionRecord, + UnifiedExecutionStep, + UnifiedExecutionStore +} from "./unifiedExecutionStore"; +import { FlowThreadContext } from "./flowExecution"; + +jest.mock("@/classes/graphql/graphql_functions_v2", () => ({ + getThread: jest.fn().mockResolvedValue({ id: "thread-1" }) +})); + +jest.mock("@/classes/mint/mint-functions", () => ({ + getConfiguration: jest.fn().mockReturnValue({ + execution_engine: "tapis", + svo_adapter_api: "http://adapter.local", + unified_plan_secret: "test-secret" + }) +})); + +const AUTH = "Bearer user-token"; +type LegacyServices = Parameters[0]; +const asParent = (response: unknown) => response as ParentRunResponse; + +describe("running a model again after a failed run", () => { + const legacy = { + local: { submitExecution: jest.fn() }, + wings: { submitExecution: jest.fn() }, + tapis: { + submitExecution: jest.fn(), + buildCompositeWorkflowModelTask: jest.fn(), + compositeModelContext: jest.fn(), + getExecution: jest.fn(), + getJobStatus: jest.fn(), + updateCompositeWorkflowExecution: jest.fn() + } + }; + const flows = { + flowContext: jest.fn(), + startFlowExecution: jest.fn(), + recordFlowEdit: jest.fn(), + finishFlowExecution: jest.fn() + }; + let records: Map; + let fetchMock: jest.Mock; + let planCalls: number; + + function store(): UnifiedExecutionStore { + const steps = new Map(); + return { + async getByPlanId(planId) { + return [...records.values()].find((r) => r.plan_id === planId) || null; + }, + async getById(id) { + return records.get(id) || null; + }, + async listByThread(threadId, modelId) { + return [...records.values()].filter( + (r) => r.thread_id === threadId && r.model_id === modelId + ); + }, + async listSteps(executionId) { + return [...steps.values()].filter((s) => s.execution_id === executionId); + }, + async upsertStep(input) { + const step = { id: `step-${input.step_key}`, ...input } as UnifiedExecutionStep; + steps.set(step.step_key, step); + return step; + }, + async insert(input) { + const record = { + id: `parent-${records.size + 1}`, + ...input + } as UnifiedExecutionRecord; + records.set(record.id, record); + return record; + }, + async update(id, set) { + const record = records.get(id); + if (!record) return null; + Object.assign(record, set); + return record; + }, + async compareAndSet(id, _expected, set) { + const record = records.get(id); + if (!record) return null; + Object.assign(record, set); + return record; + } + } as UnifiedExecutionStore; + } + + const submitsToAdapter = () => + fetchMock.mock.calls.filter(([url]) => String(url).endsWith("/workflows/submit")).length; + + /** Fail the run that exists, as a finished run that did not succeed. */ + function failAll() { + for (const record of records.values()) { + Object.assign(record, { + status: "failed", + failure_code: "MODEL_FAILED", + error_message: "job failed" + }); + } + } + + beforeEach(() => { + jest.clearAllMocks(); + records = new Map(); + planCalls = 0; + fetchMock = jest.fn(async (url: string, init?: { method?: string }) => { + const path = String(url).replace("http://adapter.local", ""); + const ok = (body: unknown) => ({ ok: true, status: 200, json: async () => body }); + if (path.startsWith("/workflows/submit")) { + return ok({ + run_id: "adapter-run-1", + status: "running", + tapis_workflow_id: "wf-1", + tapis_run_id: "tr-1" + }); + } + if (path.startsWith("/plans/flow")) { + planCalls += 1; + // The same adapter plan id every time: it never varies. + return ok({ + plan_id: "adapter-plan-1", + plan_json: { steps: [{ step: 1, name: "ingest", depends_on: [] }] } + }); + } + if (path.startsWith("/plans")) { + // A POST plans; a GET of /plans/ reads the stored one. + if (init?.method === "POST") planCalls += 1; + return ok({ + status: "deferred", + plan_id: "deferred-plan-1", + plan_hash: "same-hash", + plan_json: { parameters: [] } + }); + } + if (path.startsWith("/data-objects")) return ok({ id: "do-1" }); + throw new Error(`unexpected adapter call ${path}`); + }); + global.fetch = fetchMock as unknown as typeof fetch; + legacy.tapis.compositeModelContext.mockResolvedValue({ + configuration_id: "model-1", + region: null, + has_bound_inputs: false, + filled_app_inputs: [], + outputs: [], + unmatched_optional_inputs: [] + }); + legacy.tapis.buildCompositeWorkflowModelTask.mockResolvedValue({ + execution_id: "model-child-1", + output_uri: "tapis://ls6/out/ue_parent-1/model/cbb", + output_name: "cbb", + output_format: "cbc", + job_definition: { name: "job", appId: "modflow6", appVersion: "0.0.1" } + }); + legacy.tapis.submitExecution.mockResolvedValue({ submittedExecutions: ["exec-1"] }); + flows.startFlowExecution.mockResolvedValue("exec-1"); + flows.finishFlowExecution.mockResolvedValue(undefined); + }); + + const service = (withFlows = false) => + createUnifiedExecutionService(legacy as unknown as LegacyServices, { + store: store(), + ...(withFlows ? { flows } : {}) + }); + + const BASE = { + executor: "ensemble_manager", + thread_id: "thread-1", + model_id: "model-1", + execution_engine: "tapis" + }; + + describe("pre-model adapter steps", () => { + const request = { + ...BASE, + adapter_steps: [ + { + adapter_plan_id: "adapter-plan-1", + model_io_id: "input-1", + source_resource_id: "source-1" + } + ] + }; + + it("gives a failed run and a re-plan a new record, and one plan twice one record", async () => { + const svc = service(); + const first = (await svc.createPlan(request)) as { plan_id: string }; + const run1 = asParent(await svc.submit({ plan_id: first.plan_id }, AUTH)); + failAll(); + + // The same plan again: still the failed record, nothing sent. + const sent = submitsToAdapter(); + expect(asParent(await svc.submit({ plan_id: first.plan_id }, AUTH)).run_id).toBe( + run1.run_id + ); + expect(submitsToAdapter()).toBe(sent); + + const second = (await svc.createPlan(request)) as { plan_id: string }; + expect(second.plan_id).not.toBe(first.plan_id); + const run2 = asParent(await svc.submit({ plan_id: second.plan_id }, AUTH)); + expect(run2.run_id).not.toBe(run1.run_id); + expect(records.size).toBe(2); + expect(submitsToAdapter()).toBe(sent + 1); + expect(asParent(await svc.submit({ plan_id: second.plan_id }, AUTH)).run_id).toBe( + run2.run_id + ); + expect(records.size).toBe(2); + }); + }); + + describe("post-model adapter", () => { + const request = { + ...BASE, + post_model_adapter: { + model_io_id: "cbc-output", + model_output_key: "cbb", + source_contract: { standard_variable_uri: "mint:cbc", format: "cbc" }, + target_contract: { standard_variable_uri: "mint:springflow" } + } + }; + const submitBody = (planId: string) => ({ + plan_id: planId, + adapter_parameter_values: { "deferred-plan-1": {} } + }); + + it("gives a failed run and a re-plan a new record even when the adapter repeats its plan id", async () => { + const svc = service(); + const first = (await svc.createPlan(request)) as { plan_id: string }; + const run1 = asParent(await svc.submit(submitBody(first.plan_id), AUTH)); + failAll(); + + const second = (await svc.createPlan(request)) as { plan_id: string }; + expect(planCalls).toBe(2); + expect(second.plan_id).not.toBe(first.plan_id); + const run2 = asParent(await svc.submit(submitBody(second.plan_id), AUTH)); + expect(run2.run_id).not.toBe(run1.run_id); + expect(records.size).toBe(2); + + const again = asParent(await svc.submit(submitBody(second.plan_id), AUTH)); + expect(again.run_id).toBe(run2.run_id); + expect(records.size).toBe(2); + }); + }); + + describe("flow", () => { + const context: FlowThreadContext = { + is_flow: true, + caller: "alice", + thread_bindings: {}, + inputs: [], + parameters: [], + outputs: [{ model_io_id: "out-1", name: "report" }], + dates: {} + } as unknown as FlowThreadContext; + const request = { ...BASE, model_id: "cfg-flow" }; + + beforeEach(() => flows.flowContext.mockResolvedValue(context)); + + it("gives a failed run and a re-plan a new record even when the adapter repeats its plan id", async () => { + const svc = service(true); + const first = (await svc.createPlan(request, AUTH)) as { plan_id: string }; + const run1 = asParent(await svc.submit({ plan_id: first.plan_id }, AUTH)); + failAll(); + + const second = (await svc.createPlan(request, AUTH)) as { plan_id: string }; + expect(planCalls).toBe(2); + expect(second.plan_id).not.toBe(first.plan_id); + const run2 = asParent(await svc.submit({ plan_id: second.plan_id }, AUTH)); + expect(run2.run_id).not.toBe(run1.run_id); + expect(records.size).toBe(2); + expect(flows.startFlowExecution).toHaveBeenCalledTimes(2); + + expect(asParent(await svc.submit({ plan_id: second.plan_id }, AUTH)).run_id).toBe( + run2.run_id + ); + expect(records.size).toBe(2); + expect(flows.startFlowExecution).toHaveBeenCalledTimes(2); + }); + + it("still refuses a second run while one is open", async () => { + const svc = service(true); + const first = (await svc.createPlan(request, AUTH)) as { plan_id: string }; + await svc.submit({ plan_id: first.plan_id }, AUTH); + const second = (await svc.createPlan(request, AUTH)) as { plan_id: string }; + await expect(svc.submit({ plan_id: second.plan_id }, AUTH)).rejects.toMatchObject({ + code: "FLOW_ALREADY_RUNNING" + }); + }); + }); + + describe("plain model job", () => { + it("keeps no run record, so every submit sends the job again", async () => { + const svc = service(); + const first = (await svc.createPlan(BASE)) as { plan_id: string }; + await svc.submit({ plan_id: first.plan_id }, AUTH); + const second = (await svc.createPlan(BASE)) as { plan_id: string }; + await svc.submit({ plan_id: second.plan_id }, AUTH); + expect(legacy.tapis.submitExecution).toHaveBeenCalledTimes(2); + expect(records.size).toBe(0); + }); + }); + + describe("plan id size", () => { + it("still fits with the nonce for a long pre-model plan, and fails with a clear error past the limit", async () => { + const svc = service(); + const long = (n: number) => ({ + ...BASE, + model_id: "m".repeat(120), + adapter_steps: Array.from({ length: n }, (_, i) => ({ + adapter_plan_id: `a${i}`.padEnd(36, "a"), + model_io_id: "i".repeat(36), + source_resource_id: "s".repeat(36) + })) + }); + const ok = (await svc.createPlan(long(4))) as { plan_id: string }; + expect(ok.plan_id.length).toBeLessThanOrEqual(2400); + await expect(svc.createPlan(long(40))).rejects.toMatchObject({ + code: "PLAN_TOO_LARGE" + }); + }); + }); +}); diff --git a/mint-ensemble-manager/src/api/api-v1/services/unifiedExecutionService.test.ts b/mint-ensemble-manager/src/api/api-v1/services/unifiedExecutionService.test.ts index 09eaa4b7..22e38972 100644 --- a/mint-ensemble-manager/src/api/api-v1/services/unifiedExecutionService.test.ts +++ b/mint-ensemble-manager/src/api/api-v1/services/unifiedExecutionService.test.ts @@ -405,6 +405,56 @@ describe("unifiedExecutionService", () => { }); }); + describe("planning pre-model adapter steps again", () => { + const request = { + executor: "ensemble_manager", + thread_id: "thread-1", + model_id: "model-1", + execution_engine: "tapis", + adapter_steps: [ + { + adapter_plan_id: "adapter-plan-1", + model_io_id: "springflow-input", + source_resource_id: "source-1" + } + ] + }; + + it("gives each planning its own plan id, so a failed run can be run again", async () => { + const service = createUnifiedExecutionService(legacy, { store: fakeStore() }); + const first = asPlan(await service.createPlan(request)); + const second = asPlan(await service.createPlan(request)); + expect(second.plan_id).not.toBe(first.plan_id); + }); + + it("still returns the same run when one plan is submitted twice", async () => { + (global.fetch as jest.Mock).mockResolvedValue({ + ok: true, + json: async () => ({ + run_id: "adapter-run-1", + status: "running", + tapis_workflow_id: "workflow-1", + tapis_run_id: "tapis-run-1" + }) + }); + const service = createUnifiedExecutionService(legacy, { store: fakeStore() }); + const plan = asPlan(await service.createPlan(request)); + const first = asParent(await service.submit({ plan_id: plan.plan_id }, "Bearer t")); + const calls = (global.fetch as jest.Mock).mock.calls.length; + const again = asParent(await service.submit({ plan_id: plan.plan_id }, "Bearer t")); + expect(again.run_id).toBe(first.run_id); + expect((global.fetch as jest.Mock).mock.calls.length).toBe(calls); + }); + + it("leaves a plan with no adapter steps unchanged", async () => { + const service = createUnifiedExecutionService(legacy, { store: fakeStore() }); + const plain = { ...request, adapter_steps: undefined }; + const a = asPlan(await service.createPlan(plain)); + const b = asPlan(await service.createPlan(plain)); + expect(b.plan_id).toBe(a.plan_id); + }); + }); + it("rejects a tampered legacy plan identifier", async () => { const service = createUnifiedExecutionService(legacy); const plan = await service.createPlan({ diff --git a/mint-ensemble-manager/src/api/api-v1/services/unifiedExecutionService.ts b/mint-ensemble-manager/src/api/api-v1/services/unifiedExecutionService.ts index 68961134..96258bd9 100644 --- a/mint-ensemble-manager/src/api/api-v1/services/unifiedExecutionService.ts +++ b/mint-ensemble-manager/src/api/api-v1/services/unifiedExecutionService.ts @@ -91,6 +91,13 @@ interface LegacyPlan { model_id: string; execution_engine: string; adapter_steps?: AdapterStep[]; + // Makes each planning its own plan. A plan id is the encoded plan, and a + // submit of a plan id that already has a record returns that record. Pre-model + // steps name saved adapter plans, so their plan id would never change; a flow's + // or post-model plan id changes only if the adapter happens to mint a new plan + // each time. The nonce makes a re-plan after a failure a new run whatever the + // adapter does, while submitting one plan twice still returns one run. + nonce?: string; post_model_adapter?: PostModelAdapter; // Input ETL is planned by the bridge (POST /plans/model-inputs). The plan // records only what submit needs to plan the same steps again: the region @@ -461,6 +468,10 @@ function isRunOwnerMismatch(error: unknown): boolean { ); } +function planNonce(): string { + return crypto.randomBytes(6).toString("hex"); +} + function planHash(plan: LegacyPlan): string { return crypto.createHash("sha256").update(JSON.stringify(plan)).digest("hex"); } @@ -2280,7 +2291,8 @@ export function createUnifiedExecutionService( thread_id: body.thread_id, model_id: body.model_id, execution_engine: "tapis", - flow: plannedFlow.flow + flow: plannedFlow.flow, + nonce: planNonce() }; return { executor, @@ -2534,6 +2546,7 @@ export function createUnifiedExecutionService( }; }); } + if (plan.post_model_adapter || plan.adapter_steps?.length) plan.nonce = planNonce(); return { executor, version: 1, diff --git a/svo-adapter-service/app/hasura.py b/svo-adapter-service/app/hasura.py index e6da38c9..943e6e61 100755 --- a/svo-adapter-service/app/hasura.py +++ b/svo-adapter-service/app/hasura.py @@ -320,6 +320,48 @@ async def execute(self, query: str, variables: dict[str, Any] | None = None) -> # The sync re-inserts a spec's contracts on every run (nested insert under an # upserted spec appends), so it clears them first. Edges cascade and are # recomputed after the sync. +# Replace one registry row and its contracts in one request. Hasura runs the root +# fields of a mutation in order inside one transaction, so the row is never seen +# without contracts: a flow checked during a sync no longer finds a piece with no +# inputs or outputs. `$ids` is the row's own id when its contracts are replaced, +# else empty (an `_in` of nothing deletes nothing). +REPLACE_TRANSFORM_SPEC_MUTATION = """ +mutation ReplaceMintSyncSpec($ids: [String!]!, $obj: adapter_transform_spec_insert_input!) { + delete_adapter_transform_contract(where: { transform_spec_id: { _in: $ids } }) { + affected_rows + } + insert_adapter_transform_spec_one( + object: $obj + on_conflict: { + constraint: transform_spec_mint_model_config_id_key + update_columns: [ + name description transform_type method tapis_app_id app_version + stage env_from_args file_inputs mint_synced_at + ] + } + ) { id mint_model_config_id } +} +""" + +REPLACE_ETL_TRANSFORM_SPEC_MUTATION = """ +mutation ReplaceMintETLSpec($ids: [String!]!, $obj: adapter_transform_spec_insert_input!) { + delete_adapter_transform_contract(where: { transform_spec_id: { _in: $ids } }) { + affected_rows + } + insert_adapter_transform_spec_one( + object: $obj + on_conflict: { + constraint: transform_spec_pkey + update_columns: [ + name description transform_type method tapis_app_id app_version + stage parameters_schema_json env_from_args file_inputs mint_synced_at + owner_username visibility mint_etl_process_id + ] + } + ) { id } +} +""" + DELETE_SPEC_CONTRACTS_MUTATION = """ mutation DeleteSpecContracts($ids: [String!]!) { delete_adapter_transform_contract(where: { transform_spec_id: { _in: $ids } }) { diff --git a/svo-adapter-service/app/mint_sync.py b/svo-adapter-service/app/mint_sync.py index 48d30cf3..48c6ecf1 100755 --- a/svo-adapter-service/app/mint_sync.py +++ b/svo-adapter-service/app/mint_sync.py @@ -440,8 +440,7 @@ async def sync_one_etl_process(hasura_client: Any, process: dict[str, Any]) -> d when the process cannot be a registry row. ``hasura_client`` is the adapter's own (admin) client: it writes the adapter schema. An approval already recorded for the piece is kept; it still has to match the piece to count.""" - from .hasura import ( - DELETE_SPEC_CONTRACTS_MUTATION, GET_APPROVAL_SPEC_QUERY, UPSERT_ETL_TRANSFORM_SPEC_MUTATION) + from .hasura import GET_APPROVAL_SPEC_QUERY, REPLACE_ETL_TRANSFORM_SPEC_MUTATION row = etl_process_to_spec_row(process, []) if row is None: @@ -453,11 +452,12 @@ async def sync_one_etl_process(hasura_client: Any, process: dict[str, Any]) -> d if existing and existing.get("mint_etl_process_id") != row["id"]: raise SpecIdCollision(f"a registry spec with id {row['id']} already exists and is not this piece") contracts = row.pop("contracts", None) - await hasura_client.execute(DELETE_SPEC_CONTRACTS_MUTATION, {"ids": [row["id"]]}) row["mint_synced_at"] = "now()" if contracts: row["contracts"] = contracts - await hasura_client.execute(UPSERT_ETL_TRANSFORM_SPEC_MUTATION, {"obj": row}) + # The old contracts go in the same transaction as the upsert (see + # REPLACE_ETL_TRANSFORM_SPEC_MUTATION), so the piece is never seen without them. + await hasura_client.execute(REPLACE_ETL_TRANSFORM_SPEC_MUTATION, {"ids": [row["id"]], "obj": row}) return row @@ -477,12 +477,11 @@ async def sync_mint_to_adapter( 5. Return a SyncResult with counts + warnings """ from .hasura import ( - UPSERT_TRANSFORM_SPEC_MUTATION, - UPSERT_ETL_TRANSFORM_SPEC_MUTATION, + REPLACE_TRANSFORM_SPEC_MUTATION, + REPLACE_ETL_TRANSFORM_SPEC_MUTATION, DELETE_OBSOLETE_MINT_SPECS_MUTATION, DELETE_OBSOLETE_ETL_SPECS_MUTATION, GET_MINT_SPEC_IDS_QUERY, - DELETE_SPEC_CONTRACTS_MUTATION, ) if mint_client is None: @@ -544,27 +543,22 @@ async def sync_mint_to_adapter( } existing_ids = set(spec_id_by_config) - # Replace, don't append: clear the contracts of every spec this sync is - # about to re-insert them for. - replace_ids = sorted({ - str(row["id"]) if "mint_model_config_id" not in row - else str(spec_id_by_config.get(row["mint_model_config_id"]) or "") - for row in rows_to_upsert if row.get("contracts") - } - {""}) - if replace_ids: - await hasura_client.execute(DELETE_SPEC_CONTRACTS_MUTATION, {"ids": replace_ids}) - for row in rows_to_upsert: contracts = row.pop("contracts", None) is_etl = "mint_model_config_id" not in row is_update = row.get("mint_model_config_id") in existing_ids if not is_etl else False - # Upsert on mint_model_config_id: if the row exists, update all fields; - # contracts are deleted and re-inserted via the nested mutation. + # Replace, don't append: a row whose contracts are re-inserted has its old + # ones deleted in the same transaction as the upsert, so no reader ever + # sees the row without contracts. A row synced without contracts keeps + # the ones it has. + spec_id = (str(row["id"]) if is_etl + else str(spec_id_by_config.get(row["mint_model_config_id"]) or "")) + replace_ids = [spec_id] if contracts and spec_id else [] row["mint_synced_at"] = "now()" if contracts: row["contracts"] = contracts - mutation = UPSERT_ETL_TRANSFORM_SPEC_MUTATION if is_etl else UPSERT_TRANSFORM_SPEC_MUTATION - await hasura_client.execute(mutation, {"obj": row}) + mutation = REPLACE_ETL_TRANSFORM_SPEC_MUTATION if is_etl else REPLACE_TRANSFORM_SPEC_MUTATION + await hasura_client.execute(mutation, {"ids": replace_ids, "obj": row}) if is_update: result.updated += 1 else: diff --git a/svo-adapter-service/app/secrets.py b/svo-adapter-service/app/secrets.py index b40be962..7e9b54d6 100644 --- a/svo-adapter-service/app/secrets.py +++ b/svo-adapter-service/app/secrets.py @@ -110,23 +110,32 @@ def validate_name(name: str) -> str: def validate_fields(fields: dict[str, str]) -> dict[str, str]: - """Field names ``[a-z0-9_]{1,32}``, at most 16, 4 KiB total, no NUL/newline.""" + """Field names ``[a-z0-9_]{1,32}``, at most 16, 4 KiB total, no NUL/newline. + + Returns the fields with spaces at either end of each value removed: they are + never part of a key or an address, and sent along they make a server reject a + key that looks right (a pasted " sk-..."). Store what this returns.""" if not isinstance(fields, dict) or not fields: raise SecretValidationError("fields must be a non-empty object") if len(fields) > MAX_FIELDS: raise SecretValidationError(f"at most {MAX_FIELDS} fields are allowed") total = 0 + cleaned: dict[str, str] = {} for key, value in fields.items(): if not isinstance(key, str) or not FIELD_RE.match(key): raise SecretValidationError("field names must match [a-z0-9_]{1,32}") - if not isinstance(value, str) or not value: + if not isinstance(value, str): raise SecretValidationError(f"field {key!r} must be a non-empty string") if "\x00" in value or "\n" in value or "\r" in value: raise SecretValidationError(f"field {key!r} must not contain NUL or newlines") + value = value.strip() + if not value: + raise SecretValidationError(f"field {key!r} must be a non-empty string") + cleaned[key] = value total += len(key.encode()) + len(value.encode()) if total > MAX_TOTAL_BYTES: raise SecretValidationError(f"fields exceed {MAX_TOTAL_BYTES} bytes in total") - return fields + return cleaned # -------------------------------------------------------------------------- @@ -215,7 +224,7 @@ async def find(self, scope: str, owner: str, name: str, configuration_id: str) - async def put(self, *, scope, owner, name, configuration_id, pinned_app_id, pinned_app_version, fields, actor) -> dict[str, Any]: validate_name(name) - validate_fields(fields) + fields = validate_fields(fields) existing = await self.find(scope, owner, name, configuration_id) now = datetime.now(tz=timezone.utc).isoformat() row = { diff --git a/svo-adapter-service/app/store.py b/svo-adapter-service/app/store.py index 472b35d7..5de955e0 100755 --- a/svo-adapter-service/app/store.py +++ b/svo-adapter-service/app/store.py @@ -437,6 +437,13 @@ def _op_UpsertMintETLSpec(self, v): def _op_DeleteSpecContracts(self, v): return {"delete_adapter_transform_contract": {"affected_rows": 0}} + # The replace mutations: delete the row's contracts, then upsert it, as one. + def _op_ReplaceMintSyncSpec(self, v): + return {**self._op_DeleteSpecContracts(v), **self._op_UpsertMintSyncSpec(v)} + + def _op_ReplaceMintETLSpec(self, v): + return {**self._op_DeleteSpecContracts(v), **self._op_UpsertMintETLSpec(v)} + def _op_GetMintSpecIds(self, v): mint_rows = [ {"mint_model_config_id": r["mint_model_config_id"]} diff --git a/svo-adapter-service/tests/test_etl_piece_ownership.py b/svo-adapter-service/tests/test_etl_piece_ownership.py index 4f400ce8..f56114e7 100644 --- a/svo-adapter-service/tests/test_etl_piece_ownership.py +++ b/svo-adapter-service/tests/test_etl_piece_ownership.py @@ -84,7 +84,7 @@ def test_built_in_process_stays_public_and_ownerless(): def test_sync_upserts_ownership_and_never_writes_approval(): fake = _FakeHasura([_process()]) asyncio.run(sync_mint_to_adapter(fake)) - (upsert,) = fake.variables_of("UpsertMintETLSpec") + (upsert,) = fake.variables_of("ReplaceMintETLSpec") assert upsert["obj"]["owner_username"] == "alice" assert upsert["obj"]["visibility"] == "private" assert "approved_source_sha256" not in upsert["obj"] diff --git a/svo-adapter-service/tests/test_etl_sync_roundtrip.py b/svo-adapter-service/tests/test_etl_sync_roundtrip.py index 5f8180e5..b10bc96b 100644 --- a/svo-adapter-service/tests/test_etl_sync_roundtrip.py +++ b/svo-adapter-service/tests/test_etl_sync_roundtrip.py @@ -16,7 +16,7 @@ ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) -from app.hasura import DELETE_SPEC_CONTRACTS_MUTATION # noqa: E402 +from app.hasura import DELETE_SPEC_CONTRACTS_MUTATION, REPLACE_ETL_TRANSFORM_SPEC_MUTATION # noqa: E402 from app.mint_sync import etl_process_to_spec_row, sync_mint_to_adapter # noqa: E402 from app.planner import build_plan_json # noqa: E402 @@ -95,13 +95,16 @@ async def execute(self, query: str, variables: dict | None = None) -> dict: return {"insert_adapter_transform_spec_one": {"id": "x"}} -def test_resync_clears_contracts_before_reinserting(): +def test_resync_replaces_contracts_in_the_same_request_as_the_upsert(): + # One request per piece deletes its contracts and upserts it, which Hasura runs + # as one transaction: a flow checked mid-sync never sees a piece without them. aoi = next(s for s in _hosted_specs() if s.get("id") == "ts-aoi-from-boundary") fake = _FakeHasura([_as_catalog_row(register.to_object(aoi))], existing=[]) asyncio.run(sync_mint_to_adapter(fake, dry_run=False)) - kinds = [q for q, _ in fake.calls] - delete_at = next(i for i, q in enumerate(kinds) if q == DELETE_SPEC_CONTRACTS_MUTATION) - upsert_at = next(i for i, q in enumerate(kinds) if "UpsertMintETLSpec" in q) - assert delete_at < upsert_at - assert fake.calls[delete_at][1] == {"ids": ["ts-aoi-from-boundary"]} + assert not any(q == DELETE_SPEC_CONTRACTS_MUTATION for q, _ in fake.calls) + (query, variables), = [(q, v) for q, v in fake.calls if "ReplaceMintETLSpec" in q] + assert query == REPLACE_ETL_TRANSFORM_SPEC_MUTATION + assert query.index("delete_adapter_transform_contract") < query.index("insert_adapter_transform_spec_one") + assert variables["ids"] == ["ts-aoi-from-boundary"] + assert variables["obj"]["contracts"] diff --git a/svo-adapter-service/tests/test_secrets_store.py b/svo-adapter-service/tests/test_secrets_store.py index 29618ef8..9ae698be 100644 --- a/svo-adapter-service/tests/test_secrets_store.py +++ b/svo-adapter-service/tests/test_secrets_store.py @@ -124,13 +124,21 @@ def test_bad_secret_names_are_rejected(name): @pytest.mark.parametrize("fields", [ - {}, {"Bad": "x"}, {"a" * 33: "x"}, {"a": ""}, {"a": "x\ny"}, {"a": "x\x00y"}, + {}, {"Bad": "x"}, {"a" * 33: "x"}, {"a": ""}, {"a": " "}, {"a": "x\ny"}, {"a": "x\x00y"}, {f"f{i}": "v" for i in range(17)}, {"a": "x" * 4097}]) def test_bad_field_sets_are_rejected(fields): with pytest.raises(secrets_store.SecretValidationError): secrets_store.validate_fields(fields) +def test_values_are_stored_without_spaces_at_either_end(): + # A pasted " sk-abc " reaches the model server as the key it is, not with the spaces. + assert secrets_store.validate_fields({"api_key": " sk-abc\t", "url": "https://x "}) == { + "api_key": "sk-abc", "url": "https://x"} + # Spaces inside a value are kept. + assert secrets_store.validate_fields({"note": " a b "}) == {"note": "a b"} + + def test_rotation_reencrypts_under_the_new_key_and_the_old_key_can_go(monkeypatch): old = Fernet.generate_key().decode() new = Fernet.generate_key().decode() diff --git a/ui-react/docs/docs/facilitator/results.md b/ui-react/docs/docs/facilitator/results.md index 0119b587..ae10aab8 100644 --- a/ui-react/docs/docs/facilitator/results.md +++ b/ui-react/docs/docs/facilitator/results.md @@ -2,16 +2,39 @@ A run that finishes is not the same as an answer you can rely on, so review the result together with how it was produced. -1. Open the **Results** step of the wizard for each model you ran. Check which outputs are available. A model that has not been run yet shows *Not run yet. Send this model's runs from the Runs step.* -2. Open **Previous runs & provenance** in the Runs step to compare earlier attempts. Compare what changed between runs, such as a dataset or a parameter, to see what caused a different result. -3. Check the context before you interpret a result: the model, the data for each input, the parameter values, and whether the run completed or failed. -4. If an output is missing or a run failed, open the detailed view for that run. It shows the stages, any error message and the files produced. Note the run's name and the message before you ask anyone for help. +1. Open the **Results** step of the wizard. Each model you ran has its own **card**. Select a card's header to minimise it or open it again. The header shows its status. A model that has not been run yet shows *Not run yet. Send this model's runs from the Runs step.* +2. Read the outputs. Each output is its own block with its name, its type, **Download** and **Copy location**. **Refresh** reads the latest results from where the run saved them. +3. Open **Previous runs & provenance** in the Runs step to compare earlier attempts. Compare what changed between runs, such as a dataset or a parameter, to see what caused a different result. +4. Check the context before you interpret a result. Under **How this was made**, collapsed by default, are the data the run read and the settings it used, with those changed from their default first. +5. If an output is missing or a run failed, read the box on the card. Note the run's name and the message before you ask anyone for help. -## Results from a collection +## Preview a result + +Under an output that DYNAMO stored in Tapis, **Preview** shows the start of the file in place: + +- **A Markdown report** (`.md`) is shown formatted. A link in it shows the address it goes to beside its words. +- **A CSV file** shows its header and first rows as a small table. Very wide rows and very long cells are shortened. +- **A JSON file** is shown indented. A file that is cut off, very large or deeply nested is shown as it is, with a note saying why. + +A preview is closed until you open it, except that a Markdown report opens by itself when the file list says it is under 100 KB. A file is read only when its preview is open, and only its first 200 KB is read; where that cuts the file short, the preview says so. If the file cannot be read, the preview says *Preview unavailable* and offers **Download**. Other kinds of file have **Download** only. + +An output that is a web address is not read or previewed by DYNAMO, so that opening a result never sends your browser to another site. It has **Download**, which opens the address, and **Copy location**. + +## A run that has not finished -When an input took a **collection**, several files read together as one input, the run's record for that input shows **N files** rather than a single file name. Select it to list the files that run read. See [Run a flow](run-a-flow.md). +The card says where the run is instead of showing results: + +- **Waiting for your edit.** The flow is paused. Select **Go to Runs to edit and resume**. +- **Running.** The results will appear when it finishes. Use **Refresh** to check. +- **Failed.** The card shows the reason and the same **Restart** control as in the Runs step, with the same run time and publishing choices, and the same confirmation for a model run several times. After you restart, DYNAMO opens the Runs step so you can follow the new run. + +## A model run many times + +When a model ran more than once, its card keeps a table with one row for each run and a column for each output. The table has a column only for the settings that differ between runs; the rest are listed once above it as *Same for every run*. **CSV** downloads the table. + +## Results from a collection -![The Results table for a flow: download buttons for each output, and the Documents input opened to list its 3 files](../assets/images/results-collection-input.png) +When an input took a **collection**, several files read together as one input, the run's record for that input shows **N files** rather than a single file name. Under **How this was made**, select it to list the files that run read. See [Run a flow](run-a-flow.md). ## What a run keeps diff --git a/ui-react/docs/docs/facilitator/run-a-flow.md b/ui-react/docs/docs/facilitator/run-a-flow.md index d74d58aa..6f097c06 100644 --- a/ui-react/docs/docs/facilitator/run-a-flow.md +++ b/ui-react/docs/docs/facilitator/run-a-flow.md @@ -74,17 +74,31 @@ If something blocks the run, DYNAMO shows a message with a title and the details ## Follow a run -The **Flow steps** panel in the Runs step shows the run. +The Runs step shows one **card** for each model or flow in the thread. The header has its name, a status (*Waiting to start*, *Running*, *Waiting for your edit*, *Completed*, *Failed* or *Cancelled*), and either when the run started or how long it took. Select the header to minimise the card or open it again; the choice stays while you are on the page. + +- **Which cards are open.** A card opens by itself when its run needs your attention: running, waiting for your edit, or failed. A completed card is closed when the thread has several models, so a long list stays short. If the thread has only one model, its card is open. +- **One sentence.** Under the header, one line says what to do next, for example *The flow is running. Results will appear when it completes.* +- **The steps.** Each step is one box in the graph, with its state: *Waiting*, *Running*, *Completed*, *Failed*, *Skipped* or *Cancelled*. The steps are drawn once. A step that failed is named, with its message, in the failure box above the graph. +- **Refresh.** Fetches the latest state. DYNAMO keeps checking on the run for a while after you start it. Use **Refresh** at any time to check again. +- **Log and Files.** For a single run, these two buttons are on the card. **Log** shows the run's log. **Files** lists what the run archived. +- **Settings used.** Collapsed by default. It lists the settings the run used, with those changed from their default first. +- **Cancel run.** Asks you to confirm. Select **Yes, cancel run** to stop it, or **Keep waiting** to go back. A cancelled run cannot be resumed, and for a run stopped for your edit the confirmation says so. +- **Technical details.** Collapsed by default. It holds the Tapis run and pipeline IDs and the task ID of each step. Open it only if someone supporting you asks for the IDs. -![A completed flow run: its run counts and the Flow steps graph, each step marked Completed](../assets/images/flow-run-panel.png) +A flow is one run, so its card shows no run counts, no paging and no table of runs. A model run many times, for example one run for each value of a setting, shows a line of counts such as *2 of 3 completed · 1 running* and a table with one row per run. The table has a column only for the settings that differ between runs; the rest are listed once above it as *Same for every run*. Paging appears only when there is more than one page of runs. **Reload** reads the run records again. -- **The step graph.** Each step is one box, with its state: *Waiting*, *Running*, *Completed*, *Failed*, *Skipped* or *Cancelled*. A step that failed shows its message under it. -- **The step list.** The same steps in order, below the graph. A step that has a task reference shows it. -- **Refresh workflow.** Fetches the latest state. DYNAMO keeps checking on the run for a while after you start it. Use **Refresh workflow** at any time to check again. -- **Cancel run.** Asks you to confirm. Select **Yes, cancel run** to stop it, or **Keep waiting** to go back. A cancelled run cannot be resumed, and for a run stopped for your edit the confirmation says so. -- **Technical identifiers.** Collapsed by default. Open it only if someone supporting you asks for the IDs. +When a run finishes, the card says *The flow run is complete. Continue to review its results.* Continue to the **Results** step. + +### When a run fails + +A failed run shows a red box with the reason: the failure message, and a line for each step that failed. Below it is a **Restart** button. **Restart** sends the same model again with the same data and settings, and only that model, as a new run. -When a run finishes, the thread says *The flow run is complete. Continue to review its results.* Continue to the **Results** step. +- **Only when every run is over.** If a model ran several times and one run failed while others are still going, the card says so and offers no Restart until the others have finished. +- **What is carried over.** For a model, the box shows **Maximum run time (minutes)**: the earlier run's time when it was kept, otherwise the default of 60, which you can change. If the earlier run published its outputs to a catalog, **Publish outputs** is already ticked with the same catalogs; check its settings before you restart. A flow has no maximum time and does not publish this way. +- **Several runs.** The button then reads **Restart all N runs**. It asks you to confirm, and says that runs which succeeded are sent again too: DYNAMO starts a model's runs together, so a single run cannot be restarted alone. +- **Never twice.** The button is disabled while the new run is being sent, in the Runs step and in the Results step alike. +- **Who.** Only someone who can send runs on the thread sees it. A run that is still going has **Cancel run** instead. +- **If it is refused**, the reason is shown under the button. For example, a flow already running in the thread is refused with *This flow is already running*. ## When a flow pauses for your edit diff --git a/ui-react/docs/docs/troubleshooting.md b/ui-react/docs/docs/troubleshooting.md index 0a22bc6d..d1e4c2f2 100644 --- a/ui-react/docs/docs/troubleshooting.md +++ b/ui-react/docs/docs/troubleshooting.md @@ -53,7 +53,7 @@ Some settings have no default and no value. Give each one a value in the **Param ## A flow says it is already running -The message gives the name of the run already in progress. Wait for that run to finish, or cancel it in the **Flow steps** panel, then run again. +The message gives the name of the run already in progress. Wait for that run to finish, or cancel it from the flow's card in the Runs step, then run again. ## A step says Not runnable for you diff --git a/ui-react/src/components/common/CollapsibleCard.tsx b/ui-react/src/components/common/CollapsibleCard.tsx new file mode 100644 index 00000000..5e58b4f1 --- /dev/null +++ b/ui-react/src/components/common/CollapsibleCard.tsx @@ -0,0 +1,67 @@ +import * as AccordionPrimitive from '@radix-ui/react-accordion'; +import { ChevronRight } from 'lucide-react'; +import type { ReactNode } from 'react'; + +import { cn } from '@/lib/utils'; + +export interface CollapsibleCardProps { + /** The header's left side: what the card is about. */ + title: ReactNode; + /** The header's right side: status and a short fact such as the duration. */ + summary?: ReactNode; + open: boolean; + onOpenChange: (open: boolean) => void; + testId?: string; + className?: string; + children: ReactNode; +} + +/** + * A card whose header minimises it. The header is one button (`aria-expanded`), + * and the body slides closed with a height-and-opacity animation, the one the + * thread rail's sub-task list uses (Radix Accordion), switched off for people + * who ask for reduced motion. The body is unmounted while closed, so anything it + * loads is loaded only once the card is open. + */ +export function CollapsibleCard({ + title, + summary, + open, + onOpenChange, + testId, + className, + children, +}: CollapsibleCardProps) { + return ( + onOpenChange(value === 'card')} + className={cn('overflow-hidden rounded-md border border-gray-200 bg-white', className)} + data-testid={testId} + > + + + + + + +
{children}
+
+
+
+ ); +} diff --git a/ui-react/src/components/common/MarkdownLite.tsx b/ui-react/src/components/common/MarkdownLite.tsx new file mode 100644 index 00000000..21b8bb9a --- /dev/null +++ b/ui-react/src/components/common/MarkdownLite.tsx @@ -0,0 +1,257 @@ +import { Fragment, type ReactNode } from 'react'; + +/** + * A small, safe Markdown renderer for result reports: headings, paragraphs, + * lists, quotes, fenced code, pipe tables, rules, and the inline `code`, + * **bold**, *italic* and [links](https://…). It builds React elements, never + * HTML, and links only open http(s) addresses. It is not a full CommonMark + * implementation: anything it does not know shows as plain text. + */ + +// Every quantifier is bounded, and no line longer than MAX_LINE reaches a pattern, +// so no input can make a pattern take more than a few million steps. +const INLINE = + /(`[^`]{1,500}`)|(\*\*[^*]{1,500}\*\*)|(\*[^*\s][^*]{0,500}\*)|(\[[^\]]{1,200}\]\([^)\s]{1,2000}\))/g; +/** The longest line, and the most lines, that are read; the rest is left out with a notice. */ +export const MARKDOWN_MAX_LINE = 2000; +export const MARKDOWN_MAX_LINES = 3000; + +function hostOf(href: string): string | null { + try { + return new URL(href).hostname || null; + } catch { + return null; + } +} + +function inline(text: string, keyBase: string): ReactNode[] { + if (text.length > MARKDOWN_MAX_LINE) { + return [...inline(text.slice(0, MARKDOWN_MAX_LINE), keyBase), text.slice(MARKDOWN_MAX_LINE)]; + } + const out: ReactNode[] = []; + let last = 0; + let index = 0; + for (const match of text.matchAll(INLINE)) { + const at = match.index ?? 0; + if (at > last) out.push(text.slice(last, at)); + const token = match[0]; + const key = `${keyBase}-${index++}`; + if (token.startsWith('`')) { + out.push( + + {token.slice(1, -1)} + , + ); + } else if (token.startsWith('**')) { + out.push({token.slice(2, -2)}); + } else if (token.startsWith('*')) { + out.push({token.slice(1, -1)}); + } else { + const link = /^\[([^\]]+)\]\(([^)\s]+)\)$/.exec(token); + const label = link?.[1] ?? token; + const href = link?.[2] ?? ''; + const host = /^https?:\/\//i.test(href) ? hostOf(href) : null; + out.push( + host ? ( + + + {label} + + {/* Where the link goes, so its words cannot hide it. */} + {label.replace(/\/$/, '') !== href.replace(/\/$/, '') && ( + ({host}) + )} + + ) : ( + label + ), + ); + } + last = at + token.length; + } + if (last < text.length) out.push(text.slice(last)); + return out; +} + +const HEADING = /^(#{1,6})\s+(.*)$/; +const BULLET = /^\s*[-*+]\s+(.*)$/; +const NUMBERED = /^\s*\d+[.)]\s+(.*)$/; +const RULE = /^\s*([-*_])(\s*\1){2,}\s*$/; +const TABLE_ROW = /^\s*\|.*\|\s*$/; +const TABLE_DIVIDER = /^\s*\|?\s*:?-{2,}:?\s*(\|\s*:?-{2,}:?\s*)*\|?\s*$/; + +function cells(line: string): string[] { + return line + .trim() + .replace(/^\||\|$/g, '') + .split('|') + .map((cell) => cell.trim()); +} + +const HEADING_CLASS = [ + '', + 'text-lg font-semibold', + 'text-base font-semibold', + 'text-sm font-semibold', + 'text-sm font-medium', + 'text-sm font-medium', + 'text-sm font-medium', +]; + +export function MarkdownLite({ source, testId }: { source: string; testId?: string }) { + let cutLine = false; + const allLines = source.replace(/\r\n?/g, '\n').split('\n'); + const cut = allLines.length > MARKDOWN_MAX_LINES; + const lines = allLines.slice(0, MARKDOWN_MAX_LINES).map((line) => { + if (line.length <= MARKDOWN_MAX_LINE) return line; + cutLine = true; + return line.slice(0, MARKDOWN_MAX_LINE); + }); + const blocks: ReactNode[] = []; + let i = 0; + const startsBlock = (line: string) => + HEADING.test(line) || + BULLET.test(line) || + NUMBERED.test(line) || + RULE.test(line) || + TABLE_ROW.test(line) || + line.startsWith('```') || + line.startsWith('>'); + + while (i < lines.length) { + const line = lines[i] ?? ''; + const key = `b${blocks.length}`; + if (line.trim() === '') { + i += 1; + } else if (line.startsWith('```')) { + const code: string[] = []; + i += 1; + while (i < lines.length && !(lines[i] ?? '').startsWith('```')) { + code.push(lines[i] ?? ''); + i += 1; + } + i += 1; + blocks.push( +
+          {code.join('\n')}
+        
, + ); + } else if (HEADING.test(line)) { + const [, hashes = '#', text = ''] = HEADING.exec(line) ?? []; + const level = Math.min(hashes.length, 6); + const Tag = `h${Math.min(level + 2, 6)}` as 'h3' | 'h4' | 'h5' | 'h6'; + blocks.push( + + {inline(text, key)} + , + ); + i += 1; + } else if (RULE.test(line)) { + blocks.push(
); + i += 1; + } else if (BULLET.test(line) || NUMBERED.test(line)) { + const ordered = NUMBERED.test(line); + const items: string[] = []; + while (i < lines.length) { + const match = (ordered ? NUMBERED : BULLET).exec(lines[i] ?? ''); + if (!match) break; + items.push(match[1] ?? ''); + i += 1; + } + const List = ordered ? 'ol' : 'ul'; + blocks.push( + + {items.map((item, n) => ( +
  • {inline(item, `${key}-${n}`)}
  • + ))} +
    , + ); + } else if (line.startsWith('>')) { + const quote: string[] = []; + while (i < lines.length && (lines[i] ?? '').startsWith('>')) { + quote.push((lines[i] ?? '').replace(/^>\s?/, '')); + i += 1; + } + blocks.push( +
    + {inline(quote.join(' '), key)} +
    , + ); + } else if (TABLE_ROW.test(line)) { + const rows: string[][] = []; + let header = false; + while (i < lines.length && TABLE_ROW.test(lines[i] ?? '')) { + const current = lines[i] ?? ''; + if (TABLE_DIVIDER.test(current)) header = rows.length === 1; + else rows.push(cells(current)); + i += 1; + } + const [head, ...body] = header ? rows : [undefined, ...rows]; + blocks.push( +
    + + {head && ( + + + {head.map((cell, n) => ( + + ))} + + + )} + + {body.map((row = [], r) => ( + + {row.map((cell, n) => ( + + ))} + + ))} + +
    + {inline(cell, `${key}-h${n}`)} +
    + {inline(cell, `${key}-${r}-${n}`)} +
    +
    , + ); + } else { + const paragraph: string[] = []; + while (i < lines.length && (lines[i] ?? '').trim() !== '' && !startsBlock(lines[i] ?? '')) { + paragraph.push(lines[i] ?? ''); + i += 1; + } + if (paragraph.length === 0) { + paragraph.push(line); + i += 1; + } + blocks.push( +

    + {paragraph.map((text, n) => ( + + {n > 0 && ' '} + {inline(text, `${key}-${n}`)} + + ))} +

    , + ); + } + } + + return ( +
    + {blocks} + {(cut || cutLine) && ( +

    + Part of this report is not shown (very long lines or too many lines). Download for the + whole file. +

    + )} +
    + ); +} diff --git a/ui-react/src/components/common/__tests__/CollapsibleCard.test.tsx b/ui-react/src/components/common/__tests__/CollapsibleCard.test.tsx new file mode 100644 index 00000000..341b7a70 --- /dev/null +++ b/ui-react/src/components/common/__tests__/CollapsibleCard.test.tsx @@ -0,0 +1,77 @@ +import { describe, expect, it } from 'vitest'; +import { fireEvent, render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import { useState } from 'react'; + +import { CollapsibleCard } from '../CollapsibleCard'; + +function Harness({ initial = true }: { initial?: boolean }) { + const [open, setOpen] = useState(initial); + return ( + Running} + open={open} + onOpenChange={setOpen} + > +

    card body

    +
    + ); +} + +describe('CollapsibleCard', () => { + it('shows the title and summary in one header button that reports its state', () => { + render(); + const header = screen.getByRole('button', { name: /flood model/i }); + expect(header).toHaveTextContent('Running'); + expect(header).toHaveAttribute('aria-expanded', 'true'); + expect(screen.getByText('card body')).toBeInTheDocument(); + }); + + it('hides the body and flips aria-expanded when the header is clicked, and back', () => { + render(); + const header = screen.getByRole('button', { name: /flood model/i }); + fireEvent.click(header); + expect(header).toHaveAttribute('aria-expanded', 'false'); + expect(screen.queryByText('card body')).not.toBeInTheDocument(); + fireEvent.click(header); + expect(header).toHaveAttribute('aria-expanded', 'true'); + expect(screen.getByText('card body')).toBeInTheDocument(); + }); + + it('starts closed when told to', () => { + render(); + expect(screen.getByRole('button', { name: /flood model/i })).toHaveAttribute( + 'aria-expanded', + 'false', + ); + expect(screen.queryByText('card body')).not.toBeInTheDocument(); + }); + + it('turns its animation off for reduced motion', () => { + render(); + const body = screen.getByText('card body').closest('[data-state]'); + expect(body?.className).toContain('motion-reduce:animate-none'); + }); + + it('is a real button the keyboard reaches, and Enter and Space toggle it', async () => { + const user = userEvent.setup(); + render(); + const header = screen.getByRole('button', { name: /flood model/i }); + expect(header.tagName).toBe('BUTTON'); + await user.tab(); + expect(header).toHaveFocus(); + await user.keyboard('{Enter}'); + expect(header).toHaveAttribute('aria-expanded', 'false'); + expect(screen.queryByText('card body')).not.toBeInTheDocument(); + await user.keyboard(' '); + expect(header).toHaveAttribute('aria-expanded', 'true'); + expect(screen.getByText('card body')).toBeInTheDocument(); + }); + + it('names the region its button controls', () => { + render(); + const header = screen.getByRole('button', { name: /flood model/i }); + expect(header).toHaveAttribute('aria-controls'); + }); +}); diff --git a/ui-react/src/components/common/__tests__/MarkdownLite.test.tsx b/ui-react/src/components/common/__tests__/MarkdownLite.test.tsx new file mode 100644 index 00000000..010c6508 --- /dev/null +++ b/ui-react/src/components/common/__tests__/MarkdownLite.test.tsx @@ -0,0 +1,96 @@ +import { describe, expect, it } from 'vitest'; +import { render, screen } from '@testing-library/react'; + +import { MarkdownLite } from '../MarkdownLite'; + +describe('MarkdownLite', () => { + it('renders headings, paragraphs, lists and inline marks as elements', () => { + render( + , + ); + expect(screen.getByRole('heading', { name: 'Report' })).toBeInTheDocument(); + expect(screen.getByText('bold').tagName).toBe('STRONG'); + expect(screen.getByText('soft').tagName).toBe('EM'); + expect(screen.getByText('code').tagName).toBe('CODE'); + expect(screen.getAllByRole('listitem')).toHaveLength(4); + }); + + it('renders a pipe table with its header', () => { + render(); + expect(screen.getByRole('columnheader', { name: 'name' })).toBeInTheDocument(); + expect(screen.getByRole('cell', { name: 'b' })).toBeInTheDocument(); + }); + + it('links only http(s) addresses, in a new tab', () => { + render(); + const link = screen.getByRole('link', { name: 'ok' }); + expect(link).toHaveAttribute('href', 'https://example.com'); + expect(link).toHaveAttribute('rel', 'noopener noreferrer'); + expect(screen.queryByRole('link', { name: 'bad' })).not.toBeInTheDocument(); + expect(screen.getByText(/bad/)).toBeInTheDocument(); + }); + + it('never turns text into HTML', () => { + const { container } = render( x'} />); + expect(container.querySelector('img')).toBeNull(); + expect(container.querySelector('b')).toBeNull(); + expect(container).toHaveTextContent(''); + }); + + it('keeps fenced code as it was', () => { + const { container } = render(); + expect(container.querySelector('pre')).toHaveTextContent('# not a heading'); + }); + + describe('hostile input', () => { + it('renders a 200 KB line of "[" quickly, and says part was left out', () => { + const started = Date.now(); + render(); + expect(Date.now() - started).toBeLessThan(2000); + expect(screen.getByTestId('markdown-truncated')).toBeInTheDocument(); + }); + + it('renders 200 KB of "[a](" quickly', () => { + const started = Date.now(); + render(); + expect(Date.now() - started).toBeLessThan(2000); + }); + + it('renders a long line of "**a" and of "*" quickly', () => { + const started = Date.now(); + render(); + expect(Date.now() - started).toBeLessThan(3000); + }); + + it('stops after the line limit and says so', () => { + const source = Array.from({ length: 6000 }, (_, i) => `- item ${i}`).join('\n'); + render(); + expect(screen.getAllByRole('listitem').length).toBeLessThanOrEqual(3000); + expect(screen.getByTestId('markdown-truncated')).toBeInTheDocument(); + }); + + it('does not say anything was left out for an ordinary report', () => { + render(); + expect(screen.queryByTestId('markdown-truncated')).not.toBeInTheDocument(); + }); + }); + + describe('links', () => { + it('shows the host beside a link whose words differ from its address', () => { + render(); + const link = screen.getByRole('link', { name: 'your bank' }); + expect(link.parentElement).toHaveTextContent('your bank (evil.example)'); + }); + + it('adds no host for a link whose words are its address', () => { + const { container } = render( + , + ); + expect(container).not.toHaveTextContent('(example.com)'); + }); + }); +}); diff --git a/ui-react/src/components/modeling/runs/RunFailureNotice.tsx b/ui-react/src/components/modeling/runs/RunFailureNotice.tsx new file mode 100644 index 00000000..fec342c6 --- /dev/null +++ b/ui-react/src/components/modeling/runs/RunFailureNotice.tsx @@ -0,0 +1,66 @@ +import type { ReactNode } from 'react'; + +import { EnsembleManagerError, type UnifiedRunSnapshot } from '@/lib/ensemble-manager'; +import { flowErrorMessage } from '@/lib/flow-errors'; + +export interface RunFailureNoticeProps { + /** The run's failure code and message, when the run has a durable record. */ + run?: Pick; + /** What failed inside the run, one line per step, shown under the reason. */ + stepErrors?: string[]; + cancelled?: boolean; + /** What the viewer can do about it: the restart control, when they may run the model. */ + actions?: ReactNode; + testId?: string; +} + +/** + * Why a run did not finish, and the way to run it again. The reason is the + * Ensemble Manager's own failure code and message, in words when the code is a + * known flow refusal. The way to run it again is passed in as `actions`. + */ +export function RunFailureNotice({ + run, + stepErrors = [], + cancelled = false, + actions, + testId = 'run-failure', +}: RunFailureNoticeProps) { + const known = run?.failure_code + ? flowErrorMessage(new EnsembleManagerError(0, run.error_message ?? '', run.failure_code)) + : null; + const hasReason = Boolean(known || run?.failure_code || run?.error_message); + return ( +
    + {known ? ( +
    +
    {known.title}
    + {known.lines.map((line, index) => ( +
    {line}
    + ))} +
    + ) : hasReason ? ( +
    + {run?.failure_code &&
    Failure: {run.failure_code}
    } + {run?.error_message &&
    {run.error_message}
    } +
    + ) : ( +
    + {cancelled ? 'This run was cancelled.' : 'The run did not finish. Open its log for why.'} +
    + )} + {stepErrors.length > 0 && ( +
      + {stepErrors.map((line, index) => ( +
    • {line}
    • + ))} +
    + )} + {actions} +
    + ); +} diff --git a/ui-react/src/components/modeling/runs/SameForEveryRun.tsx b/ui-react/src/components/modeling/runs/SameForEveryRun.tsx new file mode 100644 index 00000000..3fac4d28 --- /dev/null +++ b/ui-react/src/components/modeling/runs/SameForEveryRun.tsx @@ -0,0 +1,42 @@ +import { useState } from 'react'; + +import { humanizeName, type RunParam } from '@/lib/modeling/run-settings'; + +const SAME_FOR_EVERY_RUN_LIMIT = 6; + +/** The settings every listed run shares, summarised once above a table. */ +export function SameForEveryRun({ + items, + testId, +}: { + items: Array<{ param: RunParam; value: string }>; + testId: string; +}) { + const [showAll, setShowAll] = useState(false); + if (items.length === 0) return null; + const truncated = items.length > SAME_FOR_EVERY_RUN_LIMIT && !showAll; + const shown = truncated ? items.slice(0, SAME_FOR_EVERY_RUN_LIMIT) : items; + return ( +

    + Same for every run:{' '} + {shown.map(({ param, value }, i) => ( + + {i > 0 && ' · '} + {humanizeName(param.name)} {value} + + ))} + {items.length > SAME_FOR_EVERY_RUN_LIMIT && ( + <> + {' '} + + + )} +

    + ); +} diff --git a/ui-react/src/components/modeling/runs/SettingsUsed.tsx b/ui-react/src/components/modeling/runs/SettingsUsed.tsx new file mode 100644 index 00000000..b8ac3e6c --- /dev/null +++ b/ui-react/src/components/modeling/runs/SettingsUsed.tsx @@ -0,0 +1,44 @@ +import type { Execution } from '@/graphql/generated/execution'; +import { settingsUsed, type RunParam } from '@/lib/modeling/run-settings'; + +/** + * What one run was given: its settings, those changed from their default first. + * Collapsed, so a run that used only defaults does not take up the page. + */ +export function SettingsUsed({ + params, + execution, + testId, + summary = 'Settings used', +}: { + params: RunParam[]; + execution: Execution | undefined; + testId: string; + summary?: string; +}) { + const settings = execution ? settingsUsed(params, execution) : []; + if (settings.length === 0) return null; + const changed = settings.filter((s) => s.changed).length; + return ( +
    + + {summary} + + ({settings.length} + {changed > 0 ? `, ${changed} changed from default` : ''}) + + +
    + {settings.map((setting) => ( +
    +
    + {setting.label} + {setting.changed && changed} +
    +
    {setting.value}
    +
    + ))} +
    +
    + ); +} diff --git a/ui-react/src/lib/__tests__/ensemble-manager.test.ts b/ui-react/src/lib/__tests__/ensemble-manager.test.ts index 3942f010..a6bee8c6 100644 --- a/ui-react/src/lib/__tests__/ensemble-manager.test.ts +++ b/ui-react/src/lib/__tests__/ensemble-manager.test.ts @@ -25,6 +25,7 @@ import { submitRuns, submitExecutionPlan, isResultDownload, + readExecutionFilePrefix, } from '@/lib/ensemble-manager'; function lastRequest(): [string, RequestInit] { @@ -460,3 +461,79 @@ describe('isResultDownload', () => { expect(isResultDownload('https://example.org/a.csv', [folder])).toBe(false); }); }); + +describe('readExecutionFilePrefix', () => { + const file = { systemId: 'cloud.data', path: 'home/u/out/report.md' }; + + function streamed(chunks: Uint8Array[]) { + let at = 0; + const cancel = vi.fn().mockResolvedValue(undefined); + const read = vi.fn(async () => + at < chunks.length ? { done: false, value: chunks[at++] } : { done: true, value: undefined }, + ); + return { + cancel, + read, + response: { ok: true, status: 200, body: { getReader: () => ({ read, cancel }) } }, + }; + } + + beforeEach(() => { + localStorage.clear(); + storeTokens({ accessToken: 'tok' }); + }); + afterEach(() => { + vi.unstubAllGlobals(); + localStorage.clear(); + }); + + it('asks the download route with the same token, and stops reading at the cap', async () => { + const chunks = [new Uint8Array(60), new Uint8Array(60), new Uint8Array(60)]; + const s = streamed(chunks); + const fetchMock = vi.fn().mockResolvedValue(s.response); + vi.stubGlobal('fetch', fetchMock); + + const out = await readExecutionFilePrefix('http://em', 'exec-1', file, 100); + + expect(String(fetchMock.mock.calls[0]?.[0])).toBe( + 'http://em/executions/exec-1/files/content?path=home%2Fu%2Fout%2Freport.md&systemId=cloud.data', + ); + expect(fetchMock.mock.calls[0]?.[1].headers).toMatchObject({ Authorization: 'Bearer tok' }); + expect(out.bytes).toHaveLength(100); + expect(out.truncated).toBe(true); + // The third chunk was never pulled, and the body was cancelled. + expect(s.read).toHaveBeenCalledTimes(2); + expect(s.cancel).toHaveBeenCalled(); + }); + + it('returns a small file whole and untruncated', async () => { + const s = streamed([new TextEncoder().encode('hello')]); + vi.stubGlobal('fetch', vi.fn().mockResolvedValue(s.response)); + const out = await readExecutionFilePrefix('http://em', 'exec-1', file, 100); + expect(new TextDecoder().decode(out.bytes)).toBe('hello'); + expect(out.truncated).toBe(false); + expect(s.cancel).not.toHaveBeenCalled(); + }); + + it('falls back to a whole read, keeping the start, when the body is not a stream', async () => { + vi.stubGlobal( + 'fetch', + vi + .fn() + .mockResolvedValue({ ok: true, status: 200, blob: async () => new Blob(['abcdefghij']) }), + ); + const out = await readExecutionFilePrefix('http://em', 'exec-1', file, 4); + expect(new TextDecoder().decode(out.bytes)).toBe('abcd'); + expect(out.truncated).toBe(true); + }); + + it('rejects like a download does when the route refuses', async () => { + vi.stubGlobal( + 'fetch', + vi.fn().mockResolvedValue({ ok: false, status: 403, json: async () => ({ message: 'raw' }) }), + ); + await expect(readExecutionFilePrefix('http://em', 'exec-1', file, 100)).rejects.toThrow( + 'Your account cannot read this file in Tapis storage.', + ); + }); +}); diff --git a/ui-react/src/lib/__tests__/publish-outputs.test.ts b/ui-react/src/lib/__tests__/publish-outputs.test.ts new file mode 100644 index 00000000..50aab083 --- /dev/null +++ b/ui-react/src/lib/__tests__/publish-outputs.test.ts @@ -0,0 +1,46 @@ +import { describe, expect, it } from 'vitest'; + +import type { PlanPublisher } from '@/lib/ensemble-manager'; +import { priorPublish, selectionFromPrior } from '../publish-outputs'; + +const encode = (plan: unknown) => + `em_${btoa(JSON.stringify(plan)).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')}.sig`; + +const publisher: PlanPublisher = { + transform_spec_id: 'spec-1', + name: 'Publish', + catalog_tokens: ['ckan', 'stac'], + catalog_options: [['ckan', 'stac'], ['ckan']], + parameters: [{ name: 'title', default: 'My run' }], + can_publish: { ok: true, reason: null }, +}; + +describe('priorPublish', () => { + it('reads the catalogs and output the earlier plan asked to publish', () => { + expect( + priorPublish(encode({ publish: ['ckan'], post_model_adapter: { model_output_key: 'cbb' } })), + ).toEqual({ tokens: ['ckan'], outputKey: 'cbb' }); + }); + + it('is null for a plan that published nothing, or an id it cannot read', () => { + expect(priorPublish(encode({ thread_id: 't' }))).toBeNull(); + expect(priorPublish('em_!!!.sig')).toBeNull(); + expect(priorPublish('svo_abc')).toBeNull(); + expect(priorPublish(undefined)).toBeNull(); + }); +}); + +describe('selectionFromPrior', () => { + it('picks the publisher offering exactly those catalogs, with its parameter defaults', () => { + expect(selectionFromPrior([publisher], { tokens: ['ckan'] })).toEqual({ + transformSpecId: 'spec-1', + values: { title: 'My run' }, + tokens: ['ckan'], + }); + }); + + it('is null when no publisher offers them any more', () => { + expect(selectionFromPrior([publisher], { tokens: ['zenodo'] })).toBeNull(); + expect(selectionFromPrior([publisher], null)).toBeNull(); + }); +}); diff --git a/ui-react/src/lib/__tests__/result-preview.test.ts b/ui-react/src/lib/__tests__/result-preview.test.ts new file mode 100644 index 00000000..d6ca9565 --- /dev/null +++ b/ui-react/src/lib/__tests__/result-preview.test.ts @@ -0,0 +1,113 @@ +import { describe, expect, it } from 'vitest'; + +import { + PREVIEW_CSV_CELL_CHARS, + PREVIEW_CSV_COLUMNS, + decodePreviewText, + parseCsvRows, + prettyJson, + previewKind, +} from '../result-preview'; + +describe('previewKind', () => { + it('reads the kind from the file name', () => { + expect(previewKind('report.md')).toBe('markdown'); + expect(previewKind('REPORT.MARKDOWN')).toBe('markdown'); + expect(previewKind('table.csv')).toBe('csv'); + expect(previewKind('lexicon.json')).toBe('json'); + expect(previewKind('map.tif')).toBeNull(); + expect(previewKind('noextension')).toBeNull(); + }); +}); + +describe('decodePreviewText', () => { + it('decodes UTF-8 and survives a cut multi-byte character', () => { + expect(decodePreviewText(new TextEncoder().encode('héllo'))).toBe('héllo'); + const cut = new TextEncoder().encode('é').slice(0, 1); + expect(() => decodePreviewText(cut)).not.toThrow(); + }); +}); + +describe('parseCsvRows', () => { + it('reads quoted fields, doubled quotes and a newline inside quotes', () => { + const { rows, truncated } = parseCsvRows('a,b\n"x, y","say ""hi"""\n"two\nlines",z\n'); + expect(rows).toEqual([ + ['a', 'b'], + ['x, y', 'say "hi"'], + ['two\nlines', 'z'], + ]); + expect(truncated).toBe(false); + }); + + it('stops after the row limit, says more followed, and handles CRLF', () => { + const text = Array.from({ length: 30 }, (_, i) => `r${i},v${i}`).join('\r\n'); + const { rows, truncated } = parseCsvRows(text, 10); + expect(rows).toHaveLength(10); + expect(rows[9]).toEqual(['r9', 'v9']); + expect(truncated).toBe(true); + }); + + it('is not truncated when it ends exactly at the limit, or has no final newline', () => { + expect(parseCsvRows('a,b\n1,2', 10)).toEqual({ + rows: [ + ['a', 'b'], + ['1', '2'], + ], + truncated: false, + }); + expect(parseCsvRows('a\n1\n', 2).truncated).toBe(false); + }); + + it('caps the columns of a row and the characters of a cell, and says so', () => { + const wide = Array.from({ length: PREVIEW_CSV_COLUMNS + 30 }, (_, i) => `c${i}`).join(','); + const parsed = parseCsvRows(wide); + expect(parsed.rows[0]).toHaveLength(PREVIEW_CSV_COLUMNS); + expect(parsed.truncated).toBe(true); + + const long = parseCsvRows(`${'x'.repeat(100_000)},b`); + expect(long.rows[0]?.[0]).toHaveLength(PREVIEW_CSV_CELL_CHARS); + expect(long.rows[0]?.[1]).toBe('b'); + expect(long.truncated).toBe(true); + }); + + it('is fast on a very large single-line file', () => { + const started = Date.now(); + parseCsvRows(','.repeat(200_000)); + expect(Date.now() - started).toBeLessThan(1000); + }); +}); + +describe('prettyJson', () => { + it('indents valid JSON', () => { + expect(prettyJson('{"a":1}')).toEqual({ text: '{\n "a": 1\n}' }); + }); + + it('shows cut-off JSON as it is and says it was cut off, not that it is malformed', () => { + const cut = prettyJson('{"a":', { truncated: true }); + expect(cut.text).toBe('{"a":'); + expect(cut.note).toMatch(/only the start/i); + expect(cut.note).not.toMatch(/not valid/i); + }); + + it('says so for JSON that does not parse', () => { + expect(prettyJson('{"a":').note).toMatch(/not valid JSON/); + }); + + it('does not indent a file that is too large', () => { + const big = JSON.stringify({ a: 'x'.repeat(200_000) }); + const out = prettyJson(big); + expect(out.text).toBe(big); + expect(out.note).toMatch(/too large/); + }); + + it('does not indent, and does not blow the stack on, deeply nested JSON', () => { + const deep = '['.repeat(50_000) + ']'.repeat(50_000); + const started = Date.now(); + const out = prettyJson(deep); + expect(out.text).toBe(deep); + expect(out.note).toMatch(/nested too deeply/); + expect(Date.now() - started).toBeLessThan(1000); + const ok = '['.repeat(10) + ']'.repeat(10); + expect(prettyJson(ok).note).toBeUndefined(); + }); +}); diff --git a/ui-react/src/lib/__tests__/run-settings.test.ts b/ui-react/src/lib/__tests__/run-settings.test.ts new file mode 100644 index 00000000..45947974 --- /dev/null +++ b/ui-react/src/lib/__tests__/run-settings.test.ts @@ -0,0 +1,49 @@ +import { describe, expect, it } from 'vitest'; + +import type { Execution } from '@/graphql/generated/execution'; +import { runDuration, settingsUsed, splitRunParams } from '../modeling/run-settings'; + +const run = (bindings: Record): Execution => ({ + id: 'e', + modelid: 'm', + status: 'SUCCESS', + bindings, + results: {}, +}); + +const params = [ + { id: 'p1', name: 'flood_depth', default: '10' }, + { id: 'p2', name: 'match_mode', default: 'longest' }, + { id: 'p3', name: 'unset_thing', default: null }, +]; + +describe('settingsUsed', () => { + it('lists changed settings first, leaves out those with no value', () => { + const used = settingsUsed(params, run({ p2: 'shortest' })); + expect(used.map((s) => [s.label, s.value, s.changed])).toEqual([ + ['match mode', 'shortest', true], + ['flood depth', '10', false], + ]); + }); + + it('does not call a binding that equals the default changed', () => { + expect(settingsUsed(params, run({ p1: '10' }))[0]?.changed).toBe(false); + }); +}); + +describe('splitRunParams', () => { + it('gives a column to what differs and summarises the rest', () => { + const { varying, common } = splitRunParams(params, [run({ p1: '1' }), run({ p1: '2' })]); + expect(varying.map((p) => p.id)).toEqual(['p1']); + expect(common.map((c) => c.param.id)).toEqual(['p2']); + }); +}); + +describe('runDuration', () => { + it('words a duration, and is null while unfinished', () => { + expect(runDuration('2026-01-01T00:00:00Z', '2026-01-01T00:00:42Z')).toBe('42 s'); + expect(runDuration('2026-01-01T00:00:00Z', '2026-01-01T00:03:10Z')).toBe('3 min'); + expect(runDuration('2026-01-01T00:00:00Z', '2026-01-01T02:05:00Z')).toBe('2 h 05 min'); + expect(runDuration('2026-01-01T00:00:00Z', null)).toBeNull(); + }); +}); diff --git a/ui-react/src/lib/ensemble-manager.ts b/ui-react/src/lib/ensemble-manager.ts index 0ee71cfe..2bc9eba5 100644 --- a/ui-react/src/lib/ensemble-manager.ts +++ b/ui-react/src/lib/ensemble-manager.ts @@ -497,6 +497,7 @@ export function submitExecutionPlan( plan_id: string; parameter_values?: Record; max_minutes?: number; + queue?: string; run_name?: string; recreate?: boolean; dry_run?: boolean; @@ -810,16 +811,12 @@ function filenameFromDisposition(header: string | null, fallbackPath: string): s return plain ?? fallbackPath.split('/').pop() ?? 'download'; } -/** - * Fetch one result file through the Ensemble Manager, with the user's token. - * The server streams it from Tapis; the browser cannot open a `tapis://` URI. - */ -export async function downloadExecutionFile( +async function requestExecutionFile( ensembleManagerApi: string, executionId: string, file: { systemId: string; path: string }, signal?: AbortSignal, -): Promise<{ blob: Blob; filename: string }> { +): Promise { const query = new URLSearchParams({ path: file.path, systemId: file.systemId }); const url = `${ensembleManagerApi}/executions/${encodeURIComponent(executionId)}/files/content?${query}`; const resp = await fetch(url, { signal, headers: ensembleManagerHeaders() }); @@ -830,12 +827,83 @@ export async function downloadExecutionFile( } throw error; } + return resp; +} + +/** + * Fetch one result file through the Ensemble Manager, with the user's token. + * The server streams it from Tapis; the browser cannot open a `tapis://` URI. + */ +export async function downloadExecutionFile( + ensembleManagerApi: string, + executionId: string, + file: { systemId: string; path: string }, + signal?: AbortSignal, +): Promise<{ blob: Blob; filename: string }> { + const resp = await requestExecutionFile(ensembleManagerApi, executionId, file, signal); return { blob: await resp.blob(), filename: filenameFromDisposition(resp.headers?.get('Content-Disposition') ?? null, file.path), }; } +/** + * Read only the start of a result file, for a preview. The route is the same as + * the download's and it does not answer a `Range` request, so the body is read + * as a stream and cancelled once `maxBytes` have arrived: the rest of a large + * file is never pulled into the browser. + */ +export async function readExecutionFilePrefix( + ensembleManagerApi: string, + executionId: string, + file: { systemId: string; path: string }, + maxBytes: number, + signal?: AbortSignal, +): Promise<{ bytes: Uint8Array; truncated: boolean }> { + const resp = await requestExecutionFile(ensembleManagerApi, executionId, file, signal); + const reader = resp.body?.getReader?.(); + if (!reader) { + // No streaming body (an old browser): read it, then keep the start. + const blob = await resp.blob(); + const start = blob.slice(0, maxBytes); + const buffer = + typeof start.arrayBuffer === 'function' + ? await start.arrayBuffer() + : await new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.onload = () => resolve(reader.result as ArrayBuffer); + reader.onerror = () => reject(reader.error); + reader.readAsArrayBuffer(start); + }); + return { bytes: new Uint8Array(buffer), truncated: blob.size > maxBytes }; + } + const chunks: Uint8Array[] = []; + let received = 0; + let truncated = false; + for (;;) { + const { done, value } = await reader.read(); + if (done) break; + if (value) { + chunks.push(value); + received += value.length; + } + if (received > maxBytes) { + truncated = true; + await reader.cancel().catch(() => undefined); + break; + } + } + const bytes = new Uint8Array(Math.min(received, maxBytes)); + let offset = 0; + for (const chunk of chunks) { + const room = bytes.length - offset; + if (room <= 0) break; + bytes.set(chunk.length > room ? chunk.subarray(0, room) : chunk, offset); + offset += Math.min(chunk.length, room); + } + return { bytes, truncated }; +} + /** Save a blob as a file through a temporary object URL. */ export function saveBlob(blob: Blob, filename: string): void { const objectUrl = URL.createObjectURL(blob); diff --git a/ui-react/src/lib/modeling/run-settings.ts b/ui-react/src/lib/modeling/run-settings.ts new file mode 100644 index 00000000..2eaed5c4 --- /dev/null +++ b/ui-react/src/lib/modeling/run-settings.ts @@ -0,0 +1,89 @@ +/** + * What a run was given: the parameter values it used, and how runs of one model + * compare. Pure functions shared by the Runs and Results steps. + */ +import type { Execution } from '@/graphql/generated/execution'; + +/** The longest a model job may run unless the person chose another time. */ +export const DEFAULT_MAX_MINUTES = 60; + +export interface RunParam { + id?: string | null; + name?: string | null; + default?: string | null; +} + +export function humanizeName(name?: string | null): string { + return (name ?? '').replace(/[-_]/g, ' '); +} + +/** The value a run shows for a parameter: its binding, else the default. */ +export function runParamValue(param: RunParam, execution: Execution): string { + let value = execution.bindings[param.id ?? ''] as unknown; + if (value == null) value = param.default ?? null; + if (typeof value === 'string' && value.startsWith('__region_geojson')) { + return 'Region GeoJSON'; + } + if (value == null || value === '') return '—'; + return String(value); +} + +/** + * Split parameters into those that tell the listed runs apart (they get a + * column) and those every run shares (they are summarised above the table). + */ +export function splitRunParams

    ( + params: P[], + executions: Execution[], +): { varying: P[]; common: Array<{ param: P; value: string }> } { + const varying: P[] = []; + const common: Array<{ param: P; value: string }> = []; + for (const param of params) { + const values = new Set(executions.map((e) => runParamValue(param, e))); + if (values.size > 1) { + varying.push(param); + } else { + const value = executions[0] ? runParamValue(param, executions[0]) : '—'; + if (value !== '—') common.push({ param, value }); + } + } + return { varying, common }; +} + +export interface UsedSetting { + id: string; + label: string; + value: string; + /** The run used a value other than the parameter's default. */ + changed: boolean; +} + +/** + * The settings one run used, those changed from their default first. A setting + * with no value (no binding, no default) is left out. + */ +export function settingsUsed(params: RunParam[], execution: Execution): UsedSetting[] { + const used = params.flatMap((param, index) => { + const value = runParamValue(param, execution); + if (value === '—') return []; + const bound = execution.bindings[param.id ?? ''] as unknown; + const changed = bound != null && bound !== '' && String(bound) !== String(param.default ?? ''); + return [{ id: param.id ?? String(index), label: humanizeName(param.name), value, changed }]; + }); + return [...used.filter((s) => s.changed), ...used.filter((s) => !s.changed)]; +} + +/** How long a run took, or null while it is unfinished or its times are unknown. */ +export function runDuration( + start: string | null | undefined, + end: string | null | undefined, +): string | null { + if (!start || !end) return null; + const ms = Date.parse(end) - Date.parse(start); + if (!Number.isFinite(ms) || ms < 0) return null; + const seconds = Math.round(ms / 1000); + if (seconds < 60) return `${seconds} s`; + const minutes = Math.floor(seconds / 60); + if (minutes < 60) return `${minutes} min`; + return `${Math.floor(minutes / 60)} h ${String(minutes % 60).padStart(2, '0')} min`; +} diff --git a/ui-react/src/lib/publish-outputs.ts b/ui-react/src/lib/publish-outputs.ts index cc30d99f..1913f5d4 100644 --- a/ui-react/src/lib/publish-outputs.ts +++ b/ui-react/src/lib/publish-outputs.ts @@ -103,3 +103,63 @@ export function publisherDefaults(publisher: PlanPublisher): Record char.charCodeAt(0)); + const plan = JSON.parse(new TextDecoder().decode(bytes)) as { + publish?: unknown; + post_model_adapter?: { model_output_key?: unknown }; + }; + const tokens = Array.isArray(plan.publish) + ? plan.publish.filter((token): token is string => typeof token === 'string') + : []; + if (tokens.length === 0) return null; + const outputKey = plan.post_model_adapter?.model_output_key; + return { tokens, ...(typeof outputKey === 'string' ? { outputKey } : {}) }; + } catch { + return null; + } +} + +/** + * The selection that repeats an earlier publish: the publisher offering exactly + * those catalogs, with its parameters at their defaults (the values the earlier + * run used are not kept, so the person checks them before restarting). + */ +export function selectionFromPrior( + publishers: PlanPublisher[], + prior: PriorPublish | null, +): PublishSelection | null { + if (!prior) return null; + const wanted = [...prior.tokens].sort().join('+'); + for (const publisher of publishers) { + const option = publisherCatalogOptions(publisher).find( + (tokens) => [...tokens].sort().join('+') === wanted, + ); + if (option) { + return { + transformSpecId: publisher.transform_spec_id, + values: publisherDefaults(publisher), + tokens: option, + ...(prior.outputKey ? { outputId: prior.outputKey } : {}), + }; + } + } + return null; +} diff --git a/ui-react/src/lib/result-preview.ts b/ui-react/src/lib/result-preview.ts new file mode 100644 index 00000000..73c4e33c --- /dev/null +++ b/ui-react/src/lib/result-preview.ts @@ -0,0 +1,139 @@ +/** + * Helpers for the inline preview of a result file: what kind of text it is, a + * capped decode of its start, and small bounded parsers. Pure. + */ + +/** The most a preview reads of one file. */ +export const PREVIEW_MAX_BYTES = 200 * 1024; +/** A preview opens by itself only for a file known to be this small or smaller. */ +export const PREVIEW_AUTO_OPEN_MAX_BYTES = 100 * 1024; +/** How many CSV rows a preview shows, header included. */ +export const PREVIEW_CSV_ROWS = 10; +/** The widest CSV row, and the longest cell, a preview shows. */ +export const PREVIEW_CSV_COLUMNS = 50; +export const PREVIEW_CSV_CELL_CHARS = 500; +/** JSON is indented only up to this size and nesting depth. */ +export const PREVIEW_JSON_MAX_CHARS = 150_000; +export const PREVIEW_JSON_MAX_DEPTH = 64; + +export type PreviewKind = 'markdown' | 'csv' | 'json'; + +/** What a file can be previewed as, from its name; null when it cannot. */ +export function previewKind(name: string): PreviewKind | null { + const extension = /\.([a-z0-9]+)(?:[?#].*)?$/i.exec(name)?.[1]?.toLowerCase(); + if (extension === 'md' || extension === 'markdown') return 'markdown'; + if (extension === 'csv') return 'csv'; + if (extension === 'json') return 'json'; + return null; +} + +/** Decode the bytes of a file's start as text (a cut multi-byte character becomes a replacement mark). */ +export function decodePreviewText(bytes: Uint8Array): string { + return new TextDecoder('utf-8').decode(bytes); +} + +/** + * The first `maxRows` rows of CSV text, with quoted fields and doubled quotes + * read. `truncated` is true when more rows followed, a row was wider than + * `PREVIEW_CSV_COLUMNS`, or a cell was longer than `PREVIEW_CSV_CELL_CHARS`. + */ +export function parseCsvRows( + text: string, + maxRows = PREVIEW_CSV_ROWS, +): { rows: string[][]; truncated: boolean } { + const rows: string[][] = []; + let row: string[] = []; + let field = ''; + let quoted = false; + let truncated = false; + let more = false; + const addField = () => { + if (row.length < PREVIEW_CSV_COLUMNS) row.push(field); + else truncated = true; + field = ''; + }; + const addChar = (char: string) => { + if (field.length < PREVIEW_CSV_CELL_CHARS) field += char; + else truncated = true; + }; + const endRow = () => { + addField(); + if (row.some((cell) => cell !== '')) rows.push(row); + row = []; + }; + for (let i = 0; i < text.length; i += 1) { + if (rows.length >= maxRows) { + more = text.slice(i).trim() !== ''; + break; + } + const char = text[i] as string; + if (quoted) { + if (char === '"' && text[i + 1] === '"') { + addChar('"'); + i += 1; + } else if (char === '"') { + quoted = false; + } else { + addChar(char); + } + } else if (char === '"') { + quoted = true; + } else if (char === ',') { + addField(); + } else if (char === '\n' || char === '\r') { + if (char === '\r' && text[i + 1] === '\n') i += 1; + endRow(); + } else { + addChar(char); + } + } + if (rows.length < maxRows && (field !== '' || row.length > 0)) endRow(); + return { rows, truncated: truncated || more }; +} + +/** The deepest nesting of `{` and `[` outside strings, stopping early once past `limit`. */ +function jsonDepth(text: string, limit: number): number { + let depth = 0; + let deepest = 0; + let inString = false; + for (let i = 0; i < text.length; i += 1) { + const char = text[i]; + if (inString) { + if (char === '\\') i += 1; + else if (char === '"') inString = false; + } else if (char === '"') { + inString = true; + } else if (char === '{' || char === '[') { + depth += 1; + if (depth > deepest) deepest = depth; + if (deepest > limit) return deepest; + } else if (char === '}' || char === ']') { + depth -= 1; + } + } + return deepest; +} + +/** + * JSON text indented. It is shown as it was, with a note saying why, when it is + * cut off (only its start was read), too large, too deeply nested, or does not parse. + */ +export function prettyJson( + text: string, + options: { truncated?: boolean } = {}, +): { text: string; note?: string } { + if (options.truncated) { + return { text, note: 'Only the start of this file was read, so it is shown as it is.' }; + } + if (text.length > PREVIEW_JSON_MAX_CHARS) { + return { text, note: 'This file is too large to indent, so it is shown as it is.' }; + } + if (jsonDepth(text, PREVIEW_JSON_MAX_DEPTH) > PREVIEW_JSON_MAX_DEPTH) { + return { text, note: 'This file is nested too deeply to indent, so it is shown as it is.' }; + } + try { + return { text: JSON.stringify(JSON.parse(text), null, 2) }; + } catch { + return { text, note: 'This file is not valid JSON, so it is shown as it is.' }; + } +} diff --git a/ui-react/src/lib/user-secrets.ts b/ui-react/src/lib/user-secrets.ts index 56326d99..1d69b545 100644 --- a/ui-react/src/lib/user-secrets.ts +++ b/ui-react/src/lib/user-secrets.ts @@ -107,6 +107,22 @@ export async function deleteUserSecret(name: string): Promise { } /** The first rule a credential breaks, as a sentence, or null when it is fine. */ +/** + * What may be wrong with a typed value, shown under its box, or null. Spaces at + * either end are removed when the value is saved; quote marks are kept (a value + * may hold them), but usually come from copying a key out of a config file. + */ +export function credentialValueWarning(value: string): string | null { + if (value === '') return null; + if (value !== value.trim()) { + return 'This value starts or ends with a space. Spaces at either end are removed when it is saved.'; + } + if (/^["'`]|["'`]$/.test(value)) { + return 'This value starts or ends with a quote mark. Remove it unless the quote is part of the value.'; + } + return null; +} + export function validateUserSecret( name: string, fields: Array<{ name: string; value: string }>, @@ -123,7 +139,7 @@ export function validateUserSecret( } if (seen.has(f.name)) return 'Each field name can be used only once.'; seen.add(f.name); - if (f.value === '') return `Enter a value for "${f.name}".`; + if (f.value.trim() === '') return `Enter a value for "${f.name}".`; } const bytes = fields.reduce( (n, f) => diff --git a/ui-react/src/pages/account/CredentialsPage.tsx b/ui-react/src/pages/account/CredentialsPage.tsx index e1fcaea4..cf08bfd2 100644 --- a/ui-react/src/pages/account/CredentialsPage.tsx +++ b/ui-react/src/pages/account/CredentialsPage.tsx @@ -3,8 +3,11 @@ * * A value is typed into a password field, sent once and never shown again, so * replacing a credential means entering every field again. Values live only in - * this form's state and are dropped as soon as the save succeeds. + * this form's state and are dropped as soon as the save succeeds. "Show" reveals + * only what is typed in the form, so the person can check it before saving; a + * stored value is never read back. */ +import { Eye, EyeOff } from 'lucide-react'; import { useCallback, useEffect, useState } from 'react'; import { ConfirmDialog } from '@/components/common/ConfirmDialog'; @@ -12,6 +15,7 @@ import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { + credentialValueWarning, deleteUserSecret, listUserSecrets, MODEL_SERVER_PRESET, @@ -53,6 +57,8 @@ export function CredentialsPage() { const [notice, setNotice] = useState(null); const [saving, setSaving] = useState(false); const [toDelete, setToDelete] = useState(null); + // Which typed values are shown in clear, by field key. Reset with the form. + const [shown, setShown] = useState>({}); const reload = useCallback(async () => { try { @@ -69,6 +75,7 @@ export function CredentialsPage() { const open = (next: FormState) => { setForm(next); + setShown({}); setFormError(null); setNotice(null); }; @@ -85,6 +92,7 @@ export function CredentialsPage() { const close = () => { setForm(null); + setShown({}); setFormError(null); }; @@ -104,11 +112,14 @@ export function CredentialsPage() { setFormError(null); try { const fields: Record = {}; - for (const f of form.fields) fields[f.name] = f.value; + // Spaces at either end are never part of a key or an address; sent, they make + // a server reject a key that looks right. + for (const f of form.fields) fields[f.name] = f.value.trim(); await saveUserSecret(form.name, fields); const verb = form.mode === 'replace' ? 'replaced' : 'saved'; // Drop every typed value from state before anything else happens. setForm(null); + setShown({}); setNotice(`Credential "${form.name}" ${verb}.`); await reload(); } catch (error) { @@ -237,13 +248,34 @@ export function CredentialsPage() {

    - patchField(f.key, { value: e.target.value })} - /> +
    + patchField(f.key, { value: e.target.value })} + /> + +
    + {credentialValueWarning(f.value) && ( +

    + {credentialValueWarning(f.value)} +

    + )}
    {form.mode === 'add' && form.fields.length > 1 && ( )} @@ -220,48 +215,6 @@ export function FlowRunPanel({ testId="flow-graph-canvas" /> - {run.status === 'awaiting_edit' && run.pause && ( - stage.stage_id === `step-${run.pause?.paused_after_step}`)?.name - } - /> - )} - {run.replacement && } - -
      - {stages.map((stage) => { - const state = flowStepState(stage.status); - const taskId = stage.provider_ids?.task_id; - return ( -
    1. -
      - {flowStepLabel(stage)} - - {STEP_LABEL[state]} - -
      - {stage.error_message && ( -

      {stepErrorText(stage.error_message)}

      - )} - {taskId && ( -

      - Task {taskId} -

      - )} -
    2. - ); - })} -
    - {active && onCancel && (
    {confirmingCancel ? ( @@ -305,10 +258,8 @@ export function FlowRunPanel({
    )} - - -
    - Technical identifiers +
    + Technical details
    Tapis workflow run ID: {tapisWorkflow?.run_id ?? 'not submitted yet'} @@ -323,6 +274,15 @@ export function FlowRunPanel({ Execution ID: {run.run_id}
    )} + {tasks.length > 0 && ( +
      + {tasks.map((stage) => ( +
    • + {flowStepLabel(stage)}: task {stage.provider_ids?.task_id} +
    • + ))} +
    + )}
    @@ -331,9 +291,6 @@ export function FlowRunPanel({ Showing the last saved workflow state. Refresh failed: {refreshError} )} -

    - Application logs and files remain available from the run records below. -

    ); } diff --git a/ui-react/src/pages/modeling/thread/MintResults.tsx b/ui-react/src/pages/modeling/thread/MintResults.tsx index 4f20e63c..38174b06 100644 --- a/ui-react/src/pages/modeling/thread/MintResults.tsx +++ b/ui-react/src/pages/modeling/thread/MintResults.tsx @@ -18,9 +18,21 @@ import { ModelOutputFile, ThreadExecutionData, } from '@/graphql/generated/execution'; +import { CollapsibleCard } from '@/components/common/CollapsibleCard'; import { RunAnswerCard } from '@/components/modeling/runs/RunAnswerCard'; +import { RunFailureNotice } from '@/components/modeling/runs/RunFailureNotice'; +import { SameForEveryRun } from '@/components/modeling/runs/SameForEveryRun'; +import { SettingsUsed } from '@/components/modeling/runs/SettingsUsed'; +import { + aggregateRunStatus, + displayStatusLabel, + statusPillClasses, +} from '@/lib/modeling/run-status'; +import { humanizeName, runParamValue, splitRunParams } from '@/lib/modeling/run-settings'; import { EditedInputNote } from './EditedInputNote'; import { ExecutionFilesDialog } from './ExecutionFilesDialog'; +import { OutputBlock } from './OutputBlock'; +import { RestartRun, type RestartOptions } from './RestartRun'; import { TapisResult } from './TapisResult'; import { EnsembleManagerError, @@ -135,6 +147,15 @@ interface MintResultsProps { * model configuration declares no output. */ onPromoteOutputs?: () => void; + /** Take the user to the Runs step: where a paused run is edited and a failed one restarted. */ + onOpenRuns?: () => void; + /** + * Send a model again after it failed. Rejects with the Ensemble Manager's + * refusal. Absent means the viewer cannot run the model. + */ + onRestartRuns?: (modelId: string, options: RestartOptions) => Promise; + /** Models whose restart is in flight, in either step. */ + restartingModels?: Record; } // ─── Component ──────────────────────────────────────────────────────────────── @@ -152,6 +173,9 @@ export function MintResults({ onIngestResults, onPublishResults, onPromoteOutputs, + onOpenRuns, + onRestartRuns, + restartingModels, unifiedRuns, }: MintResultsProps) { const modelIds = Object.keys(threadData.execution_summary ?? {}); @@ -169,6 +193,8 @@ export function MintResults({ const [filesFor, setFilesFor] = useState<{ executionId: string; modelId: string } | null>(null); const [pages, setPages] = useState>({}); const [showAllOutputs, setShowAllOutputs] = useState(true); + // A card the user closed (or opened) keeps that choice; the rest start open. + const [cardOpen, setCardOpen] = useState>({}); const [publishWaiting, setPublishWaiting] = useState>({}); const [publishError, setPublishError] = useState>({}); // The server's own error code, kept beside the message. NO_OUTPUTS_DECLARED @@ -286,12 +312,7 @@ export function MintResults({ return (
    -

    - This step is for browsing the results of the models that you ran earlier. -

    -

    Results

    - -
      +
        {modelIds.map((mid) => { const summary = threadData.execution_summary[mid]!; const model = threadData.models[mid]; @@ -301,15 +322,16 @@ export function MintResults({ const currentPage = pages[mid] ?? 1; const totalPages = Math.ceil((summary.total_runs || 1) / PAGE_SIZE); - const finishedRuns = (summary.successful_runs ?? 0) + (summary.failed_runs ?? 0); + const successfulRuns = summary.successful_runs ?? 0; + const failedRuns = summary.failed_runs ?? 0; + const submittedRuns = summary.submitted_runs ?? 0; + const finishedRuns = successfulRuns + failedRuns; const submitted = summary.submitted_for_execution || summary.submission_time; const finished = finishedRuns >= summary.total_runs && summary.total_runs > 0; - const runningRuns = (summary.submitted_runs ?? 0) - finishedRuns; - const pendingRuns = summary.total_runs - (summary.submitted_runs ?? 0); - const publishedAll = (summary.published_runs ?? 0) >= (summary.successful_runs ?? 0); const submittedPublishing = !!summary.submitted_for_publishing; const submittedIngestion = !!summary.submitted_for_ingestion; const finishedIngestion = (summary.ingested_runs ?? 0) >= summary.total_runs; + const run = unifiedRuns?.[mid]; if (!submitted) { return ( @@ -322,15 +344,28 @@ export function MintResults({ ); } + const status = aggregateRunStatus({ + totalRuns: summary.total_runs, + submittedRuns, + successfulRuns, + failedRuns, + fallbackStatus: run?.status ?? grouped.executions[0]?.status?.toLowerCase(), + }); + const isSingleRun = summary.total_runs <= 1; + const allOver = + !grouped.executions.some((e) => + ['RUNNING', 'WAITING'].includes(e.status.toUpperCase()), + ) && + (isSingleRun || finishedRuns >= summary.total_runs); + const open = cardOpen[mid] ?? true; + // Determine which outputs to show const allOutputs = model.output_files; - let shownOutputs = allOutputs; - if (!showAllOutputs) { - shownOutputs = allOutputs.filter((o) => - matchVariables(threadData.response_variables, o.variables), - ); - } - const hasExtraOutputs = shownOutputs.length < allOutputs.length; + const matching = allOutputs.filter((o) => + matchVariables(threadData.response_variables, o.variables), + ); + const shownOutputs = showAllOutputs ? allOutputs : matching; + const hasExtraOutputs = matching.length > 0 && matching.length < allOutputs.length; // Filter executions with results const executionsWithResults = grouped.executions.filter( @@ -342,70 +377,94 @@ export function MintResults({ }); const adjustableInputs = model.input_files.filter((f) => !f.value); - const adjustableParams = userParameters(model).filter((p) => !p.value); + const settableParams = userParameters(model).filter((p) => !p.value); + const { varying: varyingParams, common: sameParams } = splitRunParams( + settableParams, + executionsWithResults, + ); + const resultExecution = executionsWithResults[0]; - return ( -
      • -
        {model.name}
        -
        - - {unifiedRuns?.[mid]?.replacement && ( - - )} - {submitted && ( - <> -

        - Below are results of all successfully completed model executions. -

        -

        - {summary.total_runs} runs required. {!finished ? 'So far, ' : ''} - {summary.submitted_runs} submitted, {summary.successful_runs} succeeded,{' '} - 0 ? 'text-red-600' : ''}> - {summary.failed_runs} failed - - . {runningRuns > 0 && `${runningRuns} running`} - {runningRuns > 0 && pendingRuns > 0 && ', '} - {pendingRuns > 0 && `${pendingRuns} waiting`} -

        - - )} + const refresh = + finished && onPublishResults + ? () => void handlePublish(mid) + : () => onFetchRuns(mid, currentPage, PAGE_SIZE); - {/* Action buttons */} -
        - {finished && ( + return ( +
      • + setCardOpen((current) => ({ ...current, [mid]: next }))} + title={model.name} + summary={ + + {displayStatusLabel(status)} + + } + > +
        + + {!isSingleRun && !grouped.loading && Object.keys(groupedByKey).length > 0 && ( + )} + {!isSingleRun && hasExtraOutputs && ( + )} {submittedPublishing && !finished && ( -

        + Publishing… {summary.published_runs ?? 0}/{summary.total_runs} runs. -

        + )} {ingestionApiAvailable && finished && !submittedIngestion && canWrite && ( )} {submittedIngestion && !finishedIngestion && ( -

        + Saving… fetched {summary.fetched_run_outputs ?? 0}/{summary.total_runs}, ingested {summary.ingested_runs ?? 0}/{summary.total_runs}. -

        + )}
        @@ -443,236 +502,175 @@ export function MintResults({ ) )} - {/* Pagination + controls */} -
        - {!grouped.loading && ( - <> - - - Page {currentPage} of {totalPages} - - - - )} - - {(hasExtraOutputs || showAllOutputs) && !grouped.loading && ( - - )} - -
        - {!grouped.loading && Object.keys(groupedByKey).length > 0 && ( + {/* A run that has not finished says where it is, and what to do. */} + {status === 'paused' && ( +
        + Waiting for your edit + {onOpenRuns && ( )} -
        -
        + )} + {(status === 'running' || status === 'completing' || status === 'waiting') && ( +

        + + {status === 'waiting' ? 'Waiting to start' : 'Running'} + {!isSingleRun && ` — ${finishedRuns} of ${summary.total_runs} finished`} +

        + )} + {(status === 'failed' || status === 'cancelled') && ( + 0 + ? [`${failedRuns} of ${summary.total_runs} runs failed.`] + : [] + } + testId={`result-failure-${mid}`} + actions={ + !onRestartRuns ? undefined : allOver ? ( + + ) : ( +

        Restart is offered when every run has finished.

        + ) + } + /> + )} - {/* Results table */} -
        - {grouped.loading ? ( -
        - -
        - ) : ( - - - - {shownOutputs.length > 0 && ( - - )} + + {run?.replacement && } + + {isSingleRun ? ( + <> + {resultExecution ? ( +
          + {shownOutputs.map((out) => { + const result = resultExecution.results[out.id]; + if (!result) return null; + const url = getResultUrl( + result, + resultExecution.execution_engine, + mintConfig, + ); + return ( + + setFilesFor({ executionId, modelId: mid }) + } + /> + ); + })} +
        + ) : grouped.loading ? ( +
        + +
        + ) : ( + status === 'completed' && ( +

        + No results yet. Use Refresh to fetch them. +

        + ) + )} + {resultExecution && ( +
        + + How this was made + +
        {adjustableInputs.length > 0 && ( -
        +
        + {adjustableInputs.map((inp) => ( +
        +
        {humanizeName(inp.name)}
        +
        + +
        +
        + ))} +
        )} - {adjustableParams.length > 0 && ( - - )} - - - {shownOutputs.map((o) => ( - - ))} - {adjustableInputs.map((f) => ( - - ))} - {adjustableParams.map((p) => ( - - ))} - - - - {Object.keys(groupedByKey).length === 0 ? ( - - - - ) : ( - Object.values(groupedByKey).map((execution) => { - const paramDefaults: Record = {}; - userParameters(model).forEach( - (p) => (paramDefaults[p.id ?? ''] = p.default ?? null), - ); - return ( - - {shownOutputs.map((out) => { - const result = execution.results[out.id]; - if (!result) - return ( - - ); - const url = getResultUrl( - result, - execution.execution_engine, - mintConfig, - ); - const fname = getResultName(result); - return ( - - ); - })} - {adjustableInputs.map((inp) => { - return ( - - ); - })} - {adjustableParams.map((param) => { - let pval = execution.bindings[param.id ?? ''] as - | string - | null - | undefined; - if (!pval) pval = paramDefaults[param.id ?? '']; - if ( - typeof pval === 'string' && - pval.startsWith('__region_geojson') - ) { - pval = 'Region GeoJSON'; - } - return ( - - ); - })} - - ); - }) - )} - -
        - Outputs - - Inputs - - Parameters -
        - {(o.name ?? '').replace(/[-_]/g, ' ')} - - {(f.name ?? '').replace(/[-_]/g, ' ')} - - {(p.name ?? '').replace(/[-_]/g, ' ')} -
        - — No results available — -
        - — - - {!url ? ( - {fname || 'No file'} - ) : resultScheme(url) === 'tapis' ? ( - - setFilesFor({ executionId, modelId: mid }) - } - /> - ) : resultScheme(url) === 'web' ? ( - - {fname} - - ) : ( - {fname} - )} - - - - {pval ?? '—'} -
        - )} -
        -
        + +
    +
    + )} + + ) : ( +
    + {totalPages > 1 && !grouped.loading && ( +
    + + + Page {currentPage} of {totalPages} + + +
    + )} + + setFilesFor({ executionId, modelId: mid })} + /> +
    + )} + ); })} @@ -706,3 +704,143 @@ export function MintResults({ ); } + +/** The runs of a model that ran more than once, one row each: its outputs, then what differs. */ +function ResultsTable({ + modelId, + loading, + outputs, + inputs, + params, + executions, + mintConfig, + ensembleManagerApi, + onOpenFolder, +}: { + modelId: string; + loading: boolean; + outputs: ModelOutputFile[]; + inputs: Array<{ id: string; name?: string | null }>; + params: Array<{ id?: string | null; name?: string | null; default?: string | null }>; + executions: Execution[]; + mintConfig: MintResultsProps['mintConfig']; + ensembleManagerApi: string; + onOpenFolder: (executionId: string) => void; +}) { + return ( +
    + {loading ? ( +
    + +
    + ) : ( + + + + {outputs.length > 0 && ( + + )} + {inputs.length > 0 && ( + + )} + {params.length > 0 && ( + + )} + + + {outputs.map((o) => ( + + ))} + {inputs.map((f) => ( + + ))} + {params.map((p) => ( + + ))} + + + + {executions.length === 0 ? ( + + + + ) : ( + executions.map((execution) => ( + + {outputs.map((out) => { + const result = execution.results[out.id]; + if (!result) + return ( + + ); + const url = getResultUrl(result, execution.execution_engine, mintConfig); + const fname = getResultName(result); + return ( + + ); + })} + {inputs.map((inp) => ( + + ))} + {params.map((param) => ( + + ))} + + )) + )} + +
    + Outputs + + Inputs + + Parameters +
    + {humanizeName(o.name)} + + {humanizeName(f.name)} + + {humanizeName(p.name)} +
    + — No results available — +
    + — + + {!url ? ( + {fname || 'No file'} + ) : resultScheme(url) === 'tapis' ? ( + + ) : resultScheme(url) === 'web' ? ( + + {fname} + + ) : ( + {fname} + )} + + + + {runParamValue(param, execution)} +
    + )} +
    + ); +} diff --git a/ui-react/src/pages/modeling/thread/MintRuns.tsx b/ui-react/src/pages/modeling/thread/MintRuns.tsx index 84993e1d..74d0f807 100644 --- a/ui-react/src/pages/modeling/thread/MintRuns.tsx +++ b/ui-react/src/pages/modeling/thread/MintRuns.tsx @@ -8,7 +8,7 @@ * Legacy: ui/src/screens/modeling/thread/mint-runs.ts */ import { ExternalLink, FolderOpen, RefreshCw, X } from 'lucide-react'; -import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; +import { useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from 'react'; import { Link } from 'react-router-dom'; import { @@ -23,10 +23,12 @@ import { type WorkflowCanvasEdge, type WorkflowCanvasPosition, } from '@/components/modeling/WorkflowCanvas'; -import { RunConfigurationSummary } from '@/components/modeling/runs/RunConfigurationSummary'; +import { CollapsibleCard } from '@/components/common/CollapsibleCard'; import { RunAnswerCard } from '@/components/modeling/runs/RunAnswerCard'; +import { RunFailureNotice } from '@/components/modeling/runs/RunFailureNotice'; import { RunStageTimeline } from '@/components/modeling/runs/RunStageTimeline'; -import { RunStatusSummary } from '@/components/modeling/runs/RunStatusSummary'; +import { SameForEveryRun } from '@/components/modeling/runs/SameForEveryRun'; +import { SettingsUsed } from '@/components/modeling/runs/SettingsUsed'; import { fetchExecutionLog, fetchPlanPublishers, @@ -35,13 +37,29 @@ import { type UnifiedRunSnapshot, } from '@/lib/ensemble-manager'; import { adapterParametersComplete, type ThreadAdapterPlan } from '@/lib/adapter-execution'; -import { aggregateRunStatus, stageStatusFromLegacyState } from '@/lib/modeling/run-status'; +import { + aggregateRunStatus, + displayStatusLabel, + formatDateValue, + normalizeRunStatus, + stageStatusFromLegacyState, + statusPillClasses, + type RunDisplayStatus, +} from '@/lib/modeling/run-status'; +import { + DEFAULT_MAX_MINUTES, + humanizeName, + runDuration, + runParamValue, + splitRunParams, +} from '@/lib/modeling/run-settings'; import { flowErrorMessage, type FlowErrorMessage } from '@/lib/flow-errors'; import { inputResourceCount } from '@/lib/thread-execution'; import { secretErrorMessage, userParameters } from '@/lib/secret-parameters'; import { ExecutionFilesDialog } from './ExecutionFilesDialog'; import { FlowRunPanel } from './FlowRunPanel'; import { PublishOutputsControl } from './PublishOutputsControl'; +import { RestartRun, type RestartOptions } from './RestartRun'; import { publishRequestFor, type PublishRequest, @@ -663,6 +681,8 @@ interface UnifiedExecutionPanelProps { refreshError?: string; onCancel?: () => Promise; cancelling?: boolean; + /** The control that sends the model again, once it failed or was cancelled. */ + restartControl?: ReactNode; } /** @@ -705,6 +725,7 @@ function UnifiedExecutionPanel({ refreshError, onCancel, cancelling = false, + restartControl, }: UnifiedExecutionPanelProps) { const adapterRuns = run.adapter_runs ?? []; const health = runHealth(run); @@ -988,20 +1009,18 @@ function UnifiedExecutionPanel({ - {(run.failure_code || run.error_message) && ( -
    - {run.failure_code &&
    Failure: {run.failure_code}
    } - {run.error_message &&
    {run.error_message}
    } -
    + {(run.failure_code || run.error_message || restartControl) && ( + )} {refreshError && (
    Showing the last saved workflow state. Refresh failed: {refreshError}
    )} -

    - Application logs and files remain available from the run records below. -

    ); } @@ -1030,6 +1049,14 @@ interface MintRunsProps { onContinue: () => void; onFetchRuns: (modelId: string, page: number, pageSize: number) => void; onSubmitRuns: (modelId: string, maxMinutes: number, publish?: PublishRequest) => Promise; + /** + * Send a model again after it failed. MintThread owns it so a send started + * from either step shows as in flight in both. Without it, Restart sends + * through `onSubmitRuns`. + */ + onRestartRuns?: (modelId: string, options: RestartOptions) => Promise; + /** Models whose restart is in flight, in either step. */ + restartingModels?: Record; /** Follow a flow run that is already in progress (`FLOW_ALREADY_RUNNING`). */ onOpenFlowRun?: (modelId: string, runId: string) => Promise; onExecutionSummaryChanged?: (summary: ExecutionSummaryMap) => void; @@ -1049,6 +1076,238 @@ interface MintRunsProps { const PAGE_SIZE = 100; +// ─── Run card pieces ────────────────────────────────────────────────────────── + +/** A model's card opens by itself when it needs attention, or when it is the only one. */ +function defaultCardOpen(status: RunDisplayStatus | null, modelCount: number): boolean { + return modelCount === 1 || status !== 'completed'; +} + +/** Why the last send of a model was refused. */ +function SubmitProblems({ + modelId, + refusal, + error, + onOpenFlowRun, +}: { + modelId: string; + refusal?: FlowErrorMessage; + error?: string; + onOpenFlowRun?: (modelId: string, runId: string) => Promise; +}) { + if (!refusal && !error) return null; + return ( + <> + {refusal && ( +
    +

    {refusal.title}

    +
      + {refusal.lines.map((line, index) => ( +
    • {line}
    • + ))} +
    + {refusal.runId && onOpenFlowRun && ( + + )} +
    + )} + {error &&

    {error}

    } + + ); +} + +/** The runs of a model that ran more than once: one row each. */ +function RunsTable({ + modelId, + isFlow, + loading, + executions, + inputs, + params, + onViewLog, + onViewFiles, +}: { + modelId: string; + isFlow: boolean; + loading: boolean; + executions: Execution[]; + inputs: Array<{ id: string; name?: string | null }>; + params: Array<{ id?: string | null; name?: string | null; default?: string | null }>; + onViewLog: (executionId: string) => void; + onViewFiles: (executionId: string) => void; +}) { + const fixedColumns = 5; + return ( +
    + {loading ? ( +
    + +
    + ) : ( + + + + + {inputs.length > 0 && ( + + )} + {params.length > 0 && ( + + )} + + + + + + + + {inputs.length + params.length === 0 && + ))} + {params.map((p) => ( + + ))} + + + + {executions.length === 0 ? ( + + + + ) : ( + executions.map((execution) => ( + + + + + + + {inputs.length + params.length === 0 && ( + + )} + {inputs.map((input) => { + const res = execution.bindings[input.id] as { + name?: string; + url?: string; + location?: string; + } | null; + const url = res ? getResourceUrl(res) : ''; + return ( + + ); + })} + {params.map((param) => ( + + ))} + + )) + )} + +
    + Run + + Inputs + + Parameters +
    StatusStartEndLogFiles} + {inputs.map((f) => ( + + {humanizeName(f.name)} + + {humanizeName(p.name)} +
    + {isFlow ? ( + + No run records yet. Use Reload once the run has started. + + ) : ( +
    + + Loading run records… +
    + )} +
    + + + {toDateTimeString(execution.start_time)} + + {toDateTimeString(execution.end_time)} + + + + + No inputs or parameters + {res && url ? ( + + {res.name ?? url} + + ) : ( + '—' + )} + + {runParamValue(param, execution)} +
    + )} +
    + ); +} + +/** One line of counters for a model that ran more than once. */ +function RunCounters({ + testId, + total, + successful, + failed, + running, + pending, +}: { + testId: string; + total: number; + successful: number; + failed: number; + running: number; + pending: number; +}) { + return ( +

    + + {successful} of {total} completed + + {failed > 0 && · {failed} failed} + {running > 0 && · {running} running} + {pending > 0 && · {pending} waiting} +

    + ); +} + export function MintRuns({ threadId, threadData, @@ -1067,6 +1326,8 @@ export function MintRuns({ onContinue, onFetchRuns, onSubmitRuns, + onRestartRuns, + restartingModels, onOpenFlowRun, onPublishExecution, onOutputsChanged, @@ -1109,6 +1370,9 @@ export function MintRuns({ const [publishSelections, setPublishSelections] = useState< Record >({}); + // A card the user opened or closed keeps that choice; until then it follows + // its model's state (see `defaultCardOpen`). + const [cardOpen, setCardOpen] = useState>({}); const [logDialogOpen, setLogDialogOpen] = useState(false); const [logContent, setLogContent] = useState(null); const logAbortRef = useRef(null); @@ -1169,7 +1433,7 @@ export function MintRuns({ const handleSubmit = useCallback( async (mid: string) => { - const minutes = Number(maxMinutes[mid] ?? '60'); + const minutes = Number(maxMinutes[mid] ?? String(DEFAULT_MAX_MINUTES)); if (!Number.isInteger(minutes) || minutes < 1) { setRuntimeErrors((errors) => ({ ...errors, @@ -1282,9 +1546,8 @@ export function MintRuns({ )} -

    Runs

    -
      +
        {modelIds.map((mid) => { const summary = threadData.execution_summary[mid]!; const model = threadData.models[mid]; @@ -1320,24 +1583,40 @@ export function MintRuns({ fallbackStatus: workflowRun?.status, }); const isFlowRun = Boolean(model.isFlow) || workflowRun?.adapter_stage === 'flow'; + const isSingleRun = summary.total_runs <= 1; + const ended = displayStatus === 'failed' || displayStatus === 'cancelled'; + // Restart only once every run of the model is over: a failed run next to + // runs still going would be sent again on top of them. + const anyActive = grouped.executions.some((e) => + ['RUNNING', 'WAITING'].includes(e.status.toUpperCase()), + ); + const allOver = !anyActive && (isSingleRun || finishedRuns >= summary.total_runs); const nextAction = - displayStatus === 'failed' - ? 'Review the failed run and its diagnostics before trying again.' - : displayStatus === 'completed' - ? isFlowRun - ? 'The flow run is complete. Continue to review its results.' - : 'The model run is complete. Continue to review or publish its results.' - : displayStatus === 'paused' - ? 'The flow is paused for your edit. Download the file below, change it, upload your copy and resume.' - : displayStatus === 'completing' + ended && !allOver + ? 'Some runs have failed. Restart is offered when the others have finished.' + : displayStatus === 'failed' + ? canSubmitRuns + ? 'The run failed. Read the reason below, then restart it.' + : 'The run failed. Read the reason below and open its log.' + : displayStatus === 'cancelled' + ? canSubmitRuns + ? 'The run was cancelled. Restart it to run it again.' + : 'The run was cancelled.' + : displayStatus === 'completed' ? isFlowRun - ? 'The flow finished and its outputs are being prepared.' - : 'The model finished and its output is being prepared for the next stage.' - : displayStatus === 'running' - ? isFlowRun - ? 'The flow is running. Results will appear when it completes.' - : 'The model is running. Results will appear when it completes.' - : 'The run is waiting to start.'; + ? 'The flow run is complete. Continue to review its results.' + : 'The model run is complete. Continue to review or publish its results.' + : displayStatus === 'paused' + ? 'The flow is paused for your edit. Download the file, change it, upload your copy and resume.' + : displayStatus === 'completing' + ? isFlowRun + ? 'The flow finished and its outputs are being prepared.' + : 'The model finished and its output is being prepared for the next stage.' + : displayStatus === 'running' + ? isFlowRun + ? 'The flow is running. Results will appear when it completes.' + : 'The model is running. Results will appear when it completes.' + : 'The run is waiting to start.'; // ── count inputs × params for display ────────────────────────── const nParameters = userParameters(model) @@ -1351,396 +1630,339 @@ export function MintRuns({ // ── adjustable inputs / params for column headers ───────────── const adjustableInputs = model.input_files.filter((f) => !f.value); - const adjustableParams = userParameters(model).filter((p) => !p.value); + const settableParams = userParameters(model).filter((p) => !p.value); + const { varying: adjustableParams, common: sameParams } = splitRunParams( + settableParams, + grouped.executions, + ); + + // ── the card's header ───────────────────────────────────────── + const startedAt = + latestExecution?.start_time ?? (typeof submitted === 'string' ? submitted : undefined); + const finishedAt = grouped.executions + .map((execution) => execution.end_time) + .filter((time): time is string => Boolean(time)) + .sort() + .at(-1); + const took = + displayStatus === 'completed' || ended ? runDuration(startedAt, finishedAt) : null; + const headerStatus: RunDisplayStatus | null = submitted ? displayStatus : null; + const open = cardOpen[mid] ?? defaultCardOpen(headerStatus, modelIds.length); + + // ── Restart: send this model again, only once every run is over ── + const restartControl = + canSubmitRuns && submitted && ended && allOver ? ( + + options.publish + ? onSubmitRuns(modelId, options.maxMinutes, options.publish) + : onSubmitRuns(modelId, options.maxMinutes)) + } + onOpenFlowRun={onOpenFlowRun} + /> + ) : undefined; + const problems = ( + + ); + const refreshRun = + onRefreshWorkflow && + (Boolean(unifiedRuns?.[mid]) || workflowRun?.run_id?.startsWith('ue_')) + ? async () => { + await onRefreshWorkflow(mid); + onFetchRuns(mid, currentPage, PAGE_SIZE); + } + : undefined; + const cancelRun = + onCancelWorkflow && workflowRun?.run_id?.startsWith('ue_') + ? () => onCancelWorkflow(mid) + : undefined; + // A model run many times with no run of its own record is just its table. + const showPanel = + Boolean(workflowRun) && + (isSingleRun || + isFlowRun || + Boolean(unifiedRuns?.[mid]) || + Boolean(workflowRun?.run_id?.startsWith('ue_'))); return (
      • - {summary.total_runs > 0 && submitted && ( - - } - /> - )} - - {!summary.total_runs ? ( -
        - 🚨 No runs configured. Please go back to the Data and Parameters steps. -
        - ) : !submitted ? ( -
        -
        {model.name ?? mid}
        -

        - The parameter settings require {summary.total_runs} runs ({nInputs} input - resources × {nParameters} parameters).{' '} - {model.output_files.length * summary.total_runs} output files will be generated. -

        - {!paramsDone ? ( -

        - {adapterPlanError - ? 'SVO adapter plan discovery failed; return to Parameters and retry.' - : adapterPlansLoading - ? 'Waiting for SVO adapter plan discovery to finish.' - : 'Complete the required model and SVO adapter parameters before sending runs.'} + setCardOpen((current) => ({ ...current, [mid]: next }))} + title={model.name ?? mid} + summary={ + <> + {headerStatus ? ( + + {displayStatusLabel(headerStatus)} + + ) : ( + summary.total_runs > 0 && ( + + Not sent yet + + ) + )} + {took ? ( + took {took} + ) : startedAt && submitted ? ( + Started {formatDateValue(startedAt)} + ) : null} + + } + > + {!summary.total_runs ? ( +

        + 🚨 No runs configured. Please go back to the Data and Parameters steps. +
        + ) : !submitted ? ( +
        +

        + The parameter settings require {summary.total_runs} runs ({nInputs} input + resources × {nParameters} parameters).{' '} + {model.output_files.length * summary.total_runs} output files will be + generated.

        - ) : canExecute && canWrite ? ( -
        -
        + {!paramsDone ? ( +

        + {adapterPlanError + ? 'SVO adapter plan discovery failed; return to Parameters and retry.' + : adapterPlansLoading + ? 'Waiting for SVO adapter plan discovery to finish.' + : 'Complete the required model and SVO adapter parameters before sending runs.'} +

        + ) : canExecute && canWrite ? ( +
        +
        + {!model.isFlow && ( + + )} + +
        {!model.isFlow && ( - + + setPublishSelections((current) => ({ ...current, [mid]: selection })) + } + /> )} - + {problems}
        - {!model.isFlow && ( - - setPublishSelections((current) => ({ ...current, [mid]: selection })) - } + ) : ( +

        + You don't have permission to send runs on this sub-task. +

        + )} +
        + ) : ( + <> +

        {nextAction}

        + + {!isSingleRun && ( + + )} + + {showPanel && workflowRun && isFlowRun ? ( + onResumeWorkflow(mid, edit) + : undefined + } + restartControl={restartControl} + /> + ) : showPanel && workflowRun ? ( + + ) : ended ? ( + + ) : null} + + {isSingleRun ? ( +
        + {latestExecution ? ( +
        + + +
        + ) : grouped.loading ? ( + + + Loading run records… + + ) : ( + + No run records yet. Refresh once the run has started. + + )} + - )} - {flowRefusals[mid] && ( -
        -

        {flowRefusals[mid].title}

        -
          - {flowRefusals[mid].lines.map((line, index) => ( -
        • {line}
        • - ))} -
        - {flowRefusals[mid].runId && - (onOpenFlowRun ? ( +
        + ) : ( +
        + {/* Reload, and a pager only when there is more than one page. */} +
        + {totalPages > 1 && ( + <> + + + Page {currentPage} of {totalPages} + - ) : null)} + + )} +
        - )} - {runtimeErrors[mid] && ( -

        {runtimeErrors[mid]}

        - )} -
        - ) : ( -

        - You don't have permission to send runs on this sub-task. -

        - )} -
        - ) : ( -
        - {workflowRun && isFlowRun ? ( - onRefreshWorkflow(mid) - : undefined - } - refreshing={workflowRefreshing?.[mid]} - refreshError={workflowRefreshErrors?.[mid]} - onCancel={ - onCancelWorkflow && workflowRun.run_id?.startsWith('ue_') - ? () => onCancelWorkflow(mid) - : undefined - } - cancelling={workflowCancelling?.[mid]} - ensembleManagerApi={ensembleManagerApi} - username={currentUsername} - onResume={ - onResumeWorkflow && workflowRun.run_id?.startsWith('ue_') - ? (edit) => onResumeWorkflow(mid, edit) - : undefined - } - /> - ) : workflowRun ? ( - onRefreshWorkflow(mid) - : undefined - } - refreshing={workflowRefreshing?.[mid]} - refreshError={workflowRefreshErrors?.[mid]} - onCancel={ - onCancelWorkflow && workflowRun.run_id?.startsWith('ue_') - ? () => onCancelWorkflow(mid) - : undefined - } - cancelling={workflowCancelling?.[mid]} - /> - ) : null} - - {/* Pagination + Reload bar */} -
        - - - Page {currentPage} of {totalPages} - - -
        - -
        -
        - {/* Runs table */} -
        - {grouped.loading ? ( -
        - + {/* Runs table: paged, so it grows with the step pane instead of + adding a scroll box of its own. */} + + void handleViewLog(id)} + onViewFiles={(id) => setFilesFor({ executionId: id, modelId: mid })} + />
        - ) : ( - - - - - {adjustableInputs.length > 0 && ( - - )} - {adjustableParams.length > 0 && ( - - )} - - - - - - - - {adjustableInputs.length + adjustableParams.length === 0 && ( - - ))} - {adjustableParams.map((p) => ( - - ))} - - - - {grouped.executions.length === 0 ? ( - - - - ) : ( - grouped.executions.map((execution: Execution) => { - const paramDefaults: Record = {}; - userParameters(model).forEach( - (p) => (paramDefaults[p.id ?? ''] = p.default ?? null), - ); - return ( - - - - - - - {adjustableInputs.length + adjustableParams.length === 0 && ( - - )} - {adjustableInputs.map((input) => { - const res = execution.bindings[input.id ?? ''] as { - name?: string; - url?: string; - location?: string; - } | null; - const url = res ? getResourceUrl(res) : ''; - return ( - - ); - })} - {adjustableParams.map((param) => { - let pval = execution.bindings[param.id ?? ''] as - | string - | null - | undefined; - if (pval == null) pval = paramDefaults[param.id ?? '']; - if ( - typeof pval === 'string' && - pval.startsWith('__region_geojson') - ) { - pval = 'Region GeoJSON'; - } - return ( - - ); - })} - - ); - }) - )} - -
        - Run - - Inputs - - Parameters -
        StatusStartEndLogFiles - )} - {adjustableInputs.map((f) => ( - - {(f.name ?? '').replace(/[-_]/g, ' ')} - - {(p.name ?? '').replace(/[-_]/g, ' ')} -
        - {model.isFlow ? ( - - No run records yet. Use Reload once the run has started. - - ) : ( -
        - - Loading run records… -
        - )} -
        - - - {toDateTimeString(execution.start_time)} - - {toDateTimeString(execution.end_time)} - - - - - - No inputs or parameters - - {res && url ? ( - - {res.name ?? url} - - ) : ( - '—' - )} - - {pval ?? '—'} -
        )} -
        -
        - )} + + )} +
      • ); })} diff --git a/ui-react/src/pages/modeling/thread/OutputBlock.tsx b/ui-react/src/pages/modeling/thread/OutputBlock.tsx new file mode 100644 index 00000000..bd401251 --- /dev/null +++ b/ui-react/src/pages/modeling/thread/OutputBlock.tsx @@ -0,0 +1,163 @@ +/** + * OutputBlock — one declared output of a run: its label, type, Download, Copy + * location, and a preview that opens in place. + */ +import { ChevronRight, Copy, Download, FolderOpen } from 'lucide-react'; +import { useState } from 'react'; + +import { PREVIEW_AUTO_OPEN_MAX_BYTES, previewKind } from '@/lib/result-preview'; +import { FileDownloadButton } from './FileDownloadButton'; +import { ResultPreview } from './ResultPreview'; +import { useTapisResultInfo } from './useTapisResultKind'; + +export interface OutputBlockProps { + ensembleManagerApi: string; + executionId: string; + /** What the output is called, as the model declares it. */ + label: string; + /** The declared format, when the model says one. */ + format?: string | null; + /** The file's name. */ + name: string; + /** Where it is: `tapis://`, a web address, or something that is only text. */ + url: string; + scheme: 'tapis' | 'web' | 'text'; + /** Open the run's folder: the answer for a result that is a folder. */ + onOpenFolder: (executionId: string) => void; +} + +function typeLabel(format: string | null | undefined, name: string): string { + if (format) return format; + return /\.([a-z0-9]+)$/i.exec(name)?.[1]?.toUpperCase() ?? ''; +} + +export function OutputBlock({ + ensembleManagerApi, + executionId, + label, + format, + name, + url, + scheme, + onOpenFolder, +}: OutputBlockProps) { + // Asked of Tapis only for a tapis:// location; the others never use it. + const { kind: tapisKind, size } = useTapisResultInfo( + ensembleManagerApi, + executionId, + url, + scheme === 'tapis', + ); + const kind = previewKind(name); + const previewable = Boolean(kind) && scheme === 'tapis' && tapisKind === 'file'; + const [open, setOpen] = useState(null); + // A report is what the run was for, so it opens by itself, but only when the + // listing says it is small; every other preview waits to be asked for. A web + // address is never read from here: it only has Download. + const expanded = + open ?? (kind === 'markdown' && size !== null && size <= PREVIEW_AUTO_OPEN_MAX_BYTES); + + const download = + scheme === 'tapis' && tapisKind === 'file' ? ( + + ) : scheme === 'web' ? ( + + + Download + + ) : null; + + return ( +
      • +
        + {label} + {typeLabel(format, name) && ( + + {typeLabel(format, name)} + + )} + {name} + + {download} + {scheme === 'tapis' && tapisKind === 'folder' && ( + + )} + {scheme === 'tapis' && tapisKind === 'unknown' && ( + … + )} + {scheme === 'tapis' && tapisKind === 'error' && ( + + (location not readable) + + )} + {url && ( + + )} + +
        + {previewable && kind && ( +
        + + {expanded && ( +
        + +
        + )} +
        + )} +
      • + ); +} diff --git a/ui-react/src/pages/modeling/thread/RestartRun.tsx b/ui-react/src/pages/modeling/thread/RestartRun.tsx new file mode 100644 index 00000000..3ec8b851 --- /dev/null +++ b/ui-react/src/pages/modeling/thread/RestartRun.tsx @@ -0,0 +1,235 @@ +/** + * RestartRun — send a model again after its runs ended without success. + * + * What the earlier run used is carried over, never dropped silently: the + * maximum run time and queue come from its record when it kept them (else the + * default is shown, and can be changed), and the catalogs it published to are + * read from its plan and pre-selected, with the publisher's parameters shown to + * check. A model that ran several times is sent again as a whole, so the button + * says how many runs, asks to confirm, and says that runs which succeeded are + * sent again too. + */ +import { RotateCcw } from 'lucide-react'; +import { useEffect, useState } from 'react'; + +import type { ThreadModel } from '@/graphql/generated/execution'; +import { + fetchPlanPublishers, + type PlanPublisher, + type UnifiedRunSnapshot, +} from '@/lib/ensemble-manager'; +import { flowErrorMessage } from '@/lib/flow-errors'; +import { DEFAULT_MAX_MINUTES } from '@/lib/modeling/run-settings'; +import { + priorPublish, + publishRequestFor, + selectionFromPrior, + type PublishRequest, + type PublishSelection, +} from '@/lib/publish-outputs'; +import { secretErrorMessage } from '@/lib/secret-parameters'; +import { PublishOutputsControl } from './PublishOutputsControl'; + +/** What a restart sends; the same as the first send, plus the queue the first run used. */ +export interface RestartOptions { + maxMinutes: number; + queue?: string; + publish?: PublishRequest; +} + +export interface RestartRunProps { + modelId: string; + model: ThreadModel; + /** The earlier run's record: where its maximum time, queue and plan are read from. */ + run?: UnifiedRunSnapshot; + /** How many runs the model has; more than one means the whole set is sent again. */ + totalRuns: number; + successfulRuns: number; + ensembleManagerApi: string; + /** A send of this model is in flight (from either step): the button is disabled. */ + inFlight: boolean; + onRestart: (modelId: string, options: RestartOptions) => Promise; + /** Follow the run already in progress, when a flow refuses because one is. */ + onOpenFlowRun?: (modelId: string, runId: string) => Promise; + /** Called once the new run is sent, e.g. to open the Runs step. */ + onRestarted?: () => void; +} + +function storedJobOptions(run?: UnifiedRunSnapshot): { maxMinutes?: number; queue?: string } { + const result = run?.model_result as + | { __max_minutes?: unknown; __queue?: unknown } + | null + | undefined; + const minutes = Number(result?.__max_minutes); + return { + ...(Number.isInteger(minutes) && minutes >= 1 ? { maxMinutes: minutes } : {}), + ...(typeof result?.__queue === 'string' && result.__queue ? { queue: result.__queue } : {}), + }; +} + +export function RestartRun({ + modelId, + model, + run, + totalRuns, + successfulRuns, + ensembleManagerApi, + inFlight, + onRestart, + onOpenFlowRun, + onRestarted, +}: RestartRunProps) { + const stored = storedJobOptions(run); + const isFlow = Boolean(model.isFlow); + const [minutes, setMinutes] = useState(String(stored.maxMinutes ?? DEFAULT_MAX_MINUTES)); + const [publishers, setPublishers] = useState([]); + // undefined: the person has not chosen, so the earlier run's choice stands. + const [chosen, setChosen] = useState(undefined); + const [confirming, setConfirming] = useState(false); + const [sending, setSending] = useState(false); + const [problem, setProblem] = useState<{ lines: string[]; runId?: string } | null>(null); + + useEffect(() => { + if (isFlow || !ensembleManagerApi) return; + const controller = new AbortController(); + fetchPlanPublishers(ensembleManagerApi, modelId, controller.signal) + .then((response) => setPublishers(response.publishers ?? [])) + .catch(() => undefined); + return () => controller.abort(); + }, [ensembleManagerApi, modelId, isFlow]); + + const selection = + chosen !== undefined ? chosen : selectionFromPrior(publishers, priorPublish(run?.plan_id)); + const many = totalRuns > 1; + const busy = inFlight || sending; + const minutesValue = Number(minutes); + const minutesValid = Number.isInteger(minutesValue) && minutesValue >= 1; + + async function send() { + setConfirming(false); + if (!isFlow && !minutesValid) { + setProblem({ lines: ['Enter a positive whole number of minutes.'] }); + return; + } + setProblem(null); + setSending(true); + try { + const publish = isFlow ? undefined : publishRequestFor(publishers, selection); + await onRestart(modelId, { + maxMinutes: isFlow ? DEFAULT_MAX_MINUTES : minutesValue, + ...(stored.queue ? { queue: stored.queue } : {}), + ...(publish ? { publish } : {}), + }); + onRestarted?.(); + } catch (error) { + const refusal = isFlow ? flowErrorMessage(error) : null; + setProblem( + refusal + ? { lines: [refusal.title, ...refusal.lines], runId: refusal.runId } + : { + lines: [ + secretErrorMessage(error) ?? + (error instanceof Error ? error.message : String(error)), + ], + }, + ); + } finally { + setSending(false); + } + } + + return ( +
        + {!isFlow && ( + + )} + {!isFlow && publishers.length > 0 && ( + + )} + {confirming ? ( +
        +

        + This sends all {totalRuns} runs again + {successfulRuns > 0 ? `, including the ${successfulRuns} that succeeded` : ''}. +

        +
        + + +
        +
        + ) : ( + + )} + {problem && ( +
        + {problem.lines.map((line, index) => ( +

        1 ? 'font-medium' : ''}> + {line} +

        + ))} + {problem.runId && onOpenFlowRun && ( + + )} +
        + )} +
        + ); +} diff --git a/ui-react/src/pages/modeling/thread/ResultPreview.tsx b/ui-react/src/pages/modeling/thread/ResultPreview.tsx new file mode 100644 index 00000000..2ef52571 --- /dev/null +++ b/ui-react/src/pages/modeling/thread/ResultPreview.tsx @@ -0,0 +1,154 @@ +/** + * ResultPreview — the start of one result file, shown inline. + * + * Read only when the block is open (it mounts then): a Markdown report rendered, + * the first rows of a CSV as a small table, JSON indented. A Tapis file is read + * through the same Ensemble Manager route, with the same token, as its Download + * button, and only its first 200 KB is pulled (the body is cancelled at the cap). + * A web address is never read from here. If the file cannot be read, the preview says so quietly and + * offers the Download instead. + */ +import { useEffect, useState, type ReactNode } from 'react'; + +import { MarkdownLite } from '@/components/common/MarkdownLite'; +import { parseTapisUri, readExecutionFilePrefix } from '@/lib/ensemble-manager'; +import { + PREVIEW_CSV_ROWS, + PREVIEW_MAX_BYTES, + decodePreviewText, + parseCsvRows, + prettyJson, + type PreviewKind, +} from '@/lib/result-preview'; + +type State = + | { status: 'loading' } + | { status: 'error' } + | { status: 'ready'; text: string; truncated: boolean }; + +export interface ResultPreviewProps { + kind: PreviewKind; + ensembleManagerApi: string; + executionId: string; + /** The `tapis://` location of the file. Only Tapis files are previewed. */ + url: string; + /** The Download control, repeated beside the fallback message. */ + download?: ReactNode; +} + +export function ResultPreview({ + kind, + ensembleManagerApi, + executionId, + url, + download, +}: ResultPreviewProps) { + const [state, setState] = useState({ status: 'loading' }); + + useEffect(() => { + const controller = new AbortController(); + setState({ status: 'loading' }); + const location = parseTapisUri(url); + if (!location) { + setState({ status: 'error' }); + return; + } + readExecutionFilePrefix( + ensembleManagerApi, + executionId, + location, + PREVIEW_MAX_BYTES, + controller.signal, + ) + .then( + ({ bytes, truncated }) => + !controller.signal.aborted && + setState({ status: 'ready', text: decodePreviewText(bytes), truncated }), + ) + .catch(() => !controller.signal.aborted && setState({ status: 'error' })); + return () => controller.abort(); + }, [ensembleManagerApi, executionId, url]); + + if (state.status === 'loading') { + return ( +
        + + Loading preview… +
        + ); + } + if (state.status === 'error') { + return ( +

        + Preview unavailable — {download ?? 'use Download'} +

        + ); + } + + let body: ReactNode; + let truncatedRows = false; + let jsonNote: string | undefined; + if (kind === 'markdown') { + body = ; + } else if (kind === 'csv') { + const parsed = parseCsvRows(state.text, PREVIEW_CSV_ROWS); + truncatedRows = parsed.truncated; + const [head, ...rows] = parsed.rows; + body = head ? ( +
        + + + + {head.map((cell, n) => ( + + ))} + + + + {rows.map((row, r) => ( + + {row.map((cell, n) => ( + + ))} + + ))} + +
        + {cell} +
        + {cell} +
        +
        + ) : ( +

        The file is empty.

        + ); + } else { + const json = prettyJson(state.text, { truncated: state.truncated }); + jsonNote = json.note; + body = ( +
        +        {json.text}
        +      
        + ); + } + + return ( +
        + {body} + {(state.truncated || truncatedRows || jsonNote) && ( +

        + {jsonNote ? `${jsonNote} ` : ''} + {truncatedRows ? `Only the first ${PREVIEW_CSV_ROWS - 1} rows are shown. ` : ''} + {state.truncated && kind !== 'json' ? 'Only the start of the file is shown. ' : ''} + Download for the whole file. +

        + )} +
        + ); +} diff --git a/ui-react/src/pages/modeling/thread/TapisResult.tsx b/ui-react/src/pages/modeling/thread/TapisResult.tsx index e47b7329..13163ba9 100644 --- a/ui-react/src/pages/modeling/thread/TapisResult.tsx +++ b/ui-react/src/pages/modeling/thread/TapisResult.tsx @@ -7,25 +7,9 @@ * download through the Ensemble Manager, a folder opens the files dialog. */ import { Copy, FolderOpen } from 'lucide-react'; -import { useEffect, useState } from 'react'; -import { fetchExecutionListing, type ExecutionResultLocation } from '@/lib/ensemble-manager'; import { FileDownloadButton } from './FileDownloadButton'; - -// Cells of one run ask for the same listing; share the request while it is in flight. -const inFlight = new Map>(); - -function loadResultKinds(api: string, executionId: string): Promise { - const key = `${api}|${executionId}`; - let pending = inFlight.get(key); - if (!pending) { - pending = fetchExecutionListing(api, executionId) - .then((listing) => listing.results) - .finally(() => inFlight.delete(key)); - inFlight.set(key, pending); - } - return pending; -} +import { useTapisResultKind } from './useTapisResultKind'; export interface TapisResultProps { ensembleManagerApi: string; @@ -42,22 +26,7 @@ export function TapisResult({ url, onOpenFolder, }: TapisResultProps) { - const [kind, setKind] = useState<'file' | 'folder' | 'unknown' | 'error'>('unknown'); - - useEffect(() => { - let cancelled = false; - setKind('unknown'); - loadResultKinds(ensembleManagerApi, executionId) - .then((results) => { - if (cancelled) return; - const match = results.find((result) => result.url === url); - setKind(match ? match.kind : 'error'); - }) - .catch(() => !cancelled && setKind('error')); - return () => { - cancelled = true; - }; - }, [ensembleManagerApi, executionId, url]); + const kind = useTapisResultKind(ensembleManagerApi, executionId, url); return ( diff --git a/ui-react/src/pages/modeling/thread/__tests__/FlowRunPanel.test.tsx b/ui-react/src/pages/modeling/thread/__tests__/FlowRunPanel.test.tsx index de73c91c..4d42bc66 100644 --- a/ui-react/src/pages/modeling/thread/__tests__/FlowRunPanel.test.tsx +++ b/ui-react/src/pages/modeling/thread/__tests__/FlowRunPanel.test.tsx @@ -2,7 +2,7 @@ * Tests for FlowRunPanel and the flow graph it draws from `workflow_stages`. */ import { describe, expect, it, vi } from 'vitest'; -import { fireEvent, render, screen } from '@testing-library/react'; +import { fireEvent, render, screen, within } from '@testing-library/react'; import type { UnifiedRunSnapshot } from '@/lib/ensemble-manager'; import { FlowRunPanel, flowStageGraph, flowStepState } from '../FlowRunPanel'; @@ -96,14 +96,13 @@ describe('gather stages', () => { }), ...gather.slice(1), ]; - render(); - expect(screen.getByTestId('flow-step-step-gather-1')).toHaveTextContent( + render(); + expect(screen.getByTestId('flow-node-step-gather-1')).toHaveTextContent( 'Gather Elevation files', ); - expect(screen.getByTestId('flow-step-step-gather-1')).toHaveTextContent( - 'file 3 on data.example.org', - ); - expect(screen.getByTestId('flow-step-step-1')).toBeInTheDocument(); + expect(screen.getByTestId('run-step-errors')).toHaveTextContent('Gather Elevation files: '); + expect(screen.getByTestId('run-step-errors')).toHaveTextContent('file 3 on data.example.org'); + expect(screen.getByTestId('flow-node-step-1')).toBeInTheDocument(); }); }); @@ -118,15 +117,18 @@ describe('flowStepState', () => { }); describe('FlowRunPanel', () => { - it('lists every step by name with its status', () => { + it('shows every step once, by name with its status', () => { render(); - const list = screen.getByTestId('flow-step-list'); - expect(list.children).toHaveLength(4); - expect(screen.getByTestId('flow-step-step-1')).toHaveTextContent('Sensemaking: ingest corpus'); - expect(screen.getByTestId('flow-step-step-1')).toHaveTextContent('Completed'); - expect(screen.getByTestId('flow-step-step-2')).toHaveTextContent('Running'); - expect(screen.getByTestId('flow-step-step-3')).toHaveTextContent('Waiting'); + expect(screen.getByTestId('flow-node-step-1')).toHaveTextContent('Sensemaking: ingest corpus'); + expect(screen.getByTestId('flow-node-step-1')).toHaveTextContent('Completed'); + expect(screen.getByTestId('flow-node-step-2')).toHaveTextContent('Running'); + expect(screen.getByTestId('flow-node-step-3')).toHaveTextContent('Waiting'); expect(screen.getByTestId('flow-node-step-4')).toBeInTheDocument(); + // No second, visible list of the same steps: the only list is the task ids + // inside the collapsed Technical details. + expect(screen.getByTestId('flow-technical-details')).toContainElement( + screen.getByTestId('flow-step-list'), + ); expect(screen.queryByText(/run model/i)).not.toBeInTheDocument(); expect(screen.queryByText(/pass inputs to model/i)).not.toBeInTheDocument(); }); @@ -148,7 +150,6 @@ describe('FlowRunPanel', () => { ['step-2', 'Completed'], ['step-3', 'Running'], ] as const) { - expect(screen.getByTestId(`flow-step-${id}`)).toHaveTextContent(word); expect(screen.getByTestId(`flow-node-${id}`)).toHaveTextContent(word); } }); @@ -170,8 +171,7 @@ describe('FlowRunPanel', () => { })} />, ); - expect(screen.getAllByText('Starting the flow').length).toBeGreaterThan(0); - expect(screen.getByTestId('flow-step-list').children).toHaveLength(1); + expect(screen.getAllByText('Starting the flow')).toHaveLength(1); }); it('shows a failed step with its error message', () => { @@ -186,8 +186,8 @@ describe('FlowRunPanel', () => { })} />, ); - expect(screen.getByTestId('flow-step-step-2')).toHaveTextContent('Failed'); - expect(screen.getByTestId('flow-step-step-2')).toHaveTextContent('out of memory'); + expect(screen.getByTestId('flow-node-step-2')).toHaveTextContent('Failed'); + expect(screen.getByTestId('run-step-errors')).toHaveTextContent('Embed: out of memory'); }); it('shows a readable run-level message once', () => { @@ -214,13 +214,45 @@ describe('FlowRunPanel', () => { expect(screen.getByText('boom')).toBeInTheDocument(); }); - it('keeps the Tapis ids under Technical identifiers and each step task id', () => { + it('keeps the Tapis ids and each step task id under a collapsed Technical details', () => { render(); - expect(screen.getByText('wr-1')).toBeInTheDocument(); - expect(screen.getByText('wf-1')).toBeInTheDocument(); + const details = screen.getByTestId('flow-technical-details') as HTMLDetailsElement; + expect(details.open).toBe(false); + expect(within(details).getByText('wr-1')).toBeInTheDocument(); + expect(within(details).getByText('wf-1')).toBeInTheDocument(); expect(screen.getByTestId('flow-step-step-2')).toHaveTextContent('task-step-2'); }); + it('shows the restart control in the failure box of a failed run, and not for a running one', () => { + const failed = flowRun({ status: 'failed', failure_code: 'FLOW_FAILED', error_message: 'x' }); + const { rerender } = render( + Restart} />, + ); + const box = screen.getByTestId('run-failure'); + expect(within(box).getByRole('button', { name: 'Restart' })).toBeInTheDocument(); + + rerender(); + expect(screen.queryByRole('button', { name: /restart/i })).not.toBeInTheDocument(); + }); + + it('puts the edit controls first for a paused run, before the step diagram', () => { + const paused = flowRun({ + status: 'awaiting_edit', + started_by: 'alice', + model_child_id: 'exec-1', + pause: { + output_key: '1:1', + file: 'draft.json', + file_location: 'tapis://ls6/scratch/alice/draft.json', + paused_after_step: 1, + paused_at: null, + }, + }); + render(); + const panel = screen.getByTestId('flow-run-panel'); + expect(panel.firstElementChild).toBe(screen.getByTestId('flow-paused')); + }); + it('offers cancel while the run is active and not after it ends', () => { const onCancel = vi.fn().mockResolvedValue(undefined); const { rerender } = render(); diff --git a/ui-react/src/pages/modeling/thread/__tests__/MintResults.test.tsx b/ui-react/src/pages/modeling/thread/__tests__/MintResults.test.tsx index ee594d1b..38fd9295 100644 --- a/ui-react/src/pages/modeling/thread/__tests__/MintResults.test.tsx +++ b/ui-react/src/pages/modeling/thread/__tests__/MintResults.test.tsx @@ -111,7 +111,8 @@ describe('MintResults', () => { onFetchRuns={vi.fn()} />, ); - expect(screen.getByText(/Results/)).toBeInTheDocument(); + expect(screen.getByTestId('mint-results')).toBeInTheDocument(); + expect(screen.getByText('HydroModel')).toBeInTheDocument(); }); it('renders the model name', () => { @@ -228,7 +229,7 @@ describe('MintResults', () => { expect(onContinue).toHaveBeenCalledOnce(); }); - it('shows fetch results button when runs are finished', () => { + it('shows the Refresh button when runs are finished', () => { const threadDataFinished: ThreadExecutionData = { ...mockThreadDataSubmitted, execution_summary: { @@ -253,7 +254,7 @@ describe('MintResults', () => { onPublishResults={vi.fn()} />, ); - expect(screen.getByTestId('fetch-results-model-1')).toBeInTheDocument(); + expect(screen.getByTestId('refresh-results-model-1')).toBeInTheDocument(); }); // ── #110: the button used to make no request, and a failure said nothing ── @@ -289,11 +290,11 @@ describe('MintResults', () => { ); } - it('calls onPublishResults when Fetch results is clicked', async () => { + it('calls onPublishResults when Refresh is clicked', async () => { const onPublishResults = vi.fn().mockResolvedValue(undefined); renderFinished(onPublishResults); - fireEvent.click(screen.getByTestId('fetch-results-model-1')); + fireEvent.click(screen.getByTestId('refresh-results-model-1')); await waitFor(() => expect(onPublishResults).toHaveBeenCalledWith('model-1')); }); @@ -308,7 +309,7 @@ describe('MintResults', () => { // The mount effect loads page 1 once; the publish must load it again. await waitFor(() => expect(onFetchRuns).toHaveBeenCalledTimes(1)); - fireEvent.click(screen.getByTestId('fetch-results-model-1')); + fireEvent.click(screen.getByTestId('refresh-results-model-1')); await waitFor(() => expect(onFetchRuns).toHaveBeenCalledTimes(2)); expect(onFetchRuns).toHaveBeenLastCalledWith('model-1', 1, expect.any(Number)); @@ -320,7 +321,7 @@ describe('MintResults', () => { await waitFor(() => expect(onFetchRuns).toHaveBeenCalledTimes(1)); - fireEvent.click(screen.getByTestId('fetch-results-model-1')); + fireEvent.click(screen.getByTestId('refresh-results-model-1')); expect(await screen.findByTestId('publish-error-model-1')).toBeInTheDocument(); expect(onFetchRuns).toHaveBeenCalledTimes(1); @@ -334,7 +335,7 @@ describe('MintResults', () => { ); renderFinished(onPublishResults); - fireEvent.click(screen.getByTestId('fetch-results-model-1')); + fireEvent.click(screen.getByTestId('refresh-results-model-1')); expect(await screen.findByTestId('publish-error-model-1')).toHaveTextContent( 'No executions found to publish', @@ -367,7 +368,7 @@ describe('MintResults', () => { />, ); - fireEvent.click(screen.getByTestId('fetch-results-model-1')); + fireEvent.click(screen.getByTestId('refresh-results-model-1')); expect(await screen.findByTestId('publish-error-model-1')).toHaveTextContent( /declares no output/i, @@ -383,7 +384,7 @@ describe('MintResults', () => { .mockRejectedValue(new EnsembleManagerError(403, 'Ensemble manager returned 403: nope')), ); - fireEvent.click(screen.getByTestId('fetch-results-model-1')); + fireEvent.click(screen.getByTestId('refresh-results-model-1')); expect(await screen.findByTestId('publish-error-model-1')).toHaveTextContent( 'Ensemble manager returned 403: nope', diff --git a/ui-react/src/pages/modeling/thread/__tests__/MintResultsCards.test.tsx b/ui-react/src/pages/modeling/thread/__tests__/MintResultsCards.test.tsx new file mode 100644 index 00000000..6cf4bf91 --- /dev/null +++ b/ui-react/src/pages/modeling/thread/__tests__/MintResultsCards.test.tsx @@ -0,0 +1,487 @@ +/** + * The Results step's cards: one collapsible card per model, each output as its + * own block with an inline preview, and a plain account of a run that has not + * finished. + */ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { fireEvent, screen, waitFor, within } from '@testing-library/react'; + +import { renderWithProviders } from '@/test/utils/render'; +import { setMintConfig } from '@/test/utils/mint-config'; +import { MintResults } from '../MintResults'; +import type { + Execution, + ExecutionSummary, + ModelExecutionsMap, + ThreadExecutionData, +} from '@/graphql/generated/execution'; +import { EnsembleManagerError, type UnifiedRunSnapshot } from '@/lib/ensemble-manager'; + +// ─── Fixtures ──────────────────────────────────────────────────────────────── + +const REPORT = 'tapis://cloud.data/home/u/out/report.md'; +const TABLE = 'tapis://cloud.data/home/u/out/scores.csv'; +const LEXICON = 'tapis://cloud.data/home/u/out/lexicon.json'; + +const model = { + id: 'm1', + name: 'Sensemaking', + input_parameters: [ + { id: 'p-depth', name: 'flood_depth', type: 'float', default: '10' }, + { id: 'p-mode', name: 'match_mode', type: 'string', default: 'longest' }, + ], + input_files: [{ id: 'in-1', name: 'Documents' }], + output_files: [ + { id: 'o-report', name: 'report', format: 'Markdown' }, + { id: 'o-table', name: 'scores' }, + { id: 'o-lexicon', name: 'lexicon' }, + ], +}; + +function thread(over: Partial = {}): ThreadExecutionData { + return { + id: 'thread-1', + models: { m1: model } as never, + model_ensembles: { + m1: { id: 'ens-1', bindings: { 'p-depth': ['10'], 'p-mode': ['longest'] } }, + }, + execution_summary: { + m1: { + total_runs: 1, + submitted_runs: 1, + successful_runs: 1, + failed_runs: 0, + submitted_for_execution: true, + submission_time: '2024-01-01T00:00:00Z', + ...over, + }, + }, + data: {}, + }; +} + +const run = (id: string, over: Partial = {}): Execution => ({ + id, + modelid: 'm1', + status: 'SUCCESS', + bindings: { 'in-1': { id: 'r1', name: 'a.txt', url: 'https://x/a.txt' }, 'p-mode': 'shortest' }, + results: { + 'o-report': { id: 'o-report', name: 'report.md', url: REPORT }, + 'o-table': { id: 'o-table', name: 'scores.csv', url: TABLE }, + 'o-lexicon': { id: 'o-lexicon', name: 'lexicon.json', url: LEXICON }, + }, + ...over, +}); + +const oneRun: ModelExecutionsMap = { m1: { loading: false, executions: [run('exec-1')] } }; + +/** A listing of the run's files; the size of each is what a preview reads to decide to open. */ +const listingOf = (sizes: Record) => ({ + ok: true, + status: 200, + json: () => + Promise.resolve({ + files: Object.entries(sizes).map(([url, size]) => ({ name: 'x', path: 'p', url, size })), + results: [REPORT, TABLE, LEXICON].map((url) => ({ name: 'x', url, kind: 'file' })), + }), +}); +const listing = listingOf({ [REPORT]: 500, [TABLE]: 2000, [LEXICON]: 100 }); + +/** A response whose body is read as a stream, `chunk` bytes at a time, as the route sends it. */ +let lastCancel: ReturnType; +const content = (text: string, chunk = 64 * 1024) => { + const bytes = new TextEncoder().encode(text); + let at = 0; + lastCancel = vi.fn().mockResolvedValue(undefined); + return { + ok: true, + status: 200, + headers: new Headers(), + body: { + getReader: () => ({ + read: async () => { + if (at >= bytes.length) return { done: true, value: undefined }; + const value = bytes.slice(at, at + chunk); + at += chunk; + return { done: false, value }; + }, + cancel: lastCancel, + }), + }, + blob: () => Promise.resolve(new Blob([bytes])), + }; +}; + +const csv = ['id,score', ...Array.from({ length: 14 }, (_, i) => `row${i + 1},${i}`)].join('\n'); + +function renderResults(props: Partial> = {}) { + return renderWithProviders( + , + ); +} + +let fetchMock: ReturnType; +const contentCalls = () => + fetchMock.mock.calls.filter(([url]) => String(url).includes('/files/content')); + +beforeEach(() => { + setMintConfig({ ENSEMBLE_MANAGER_API: 'http://ensemble' }); + fetchMock = vi.fn(async (url: string) => { + const u = String(url); + if (!u.includes('/files/content')) return listing; + if (u.includes('report.md')) return content('# Findings\n\nThe **corpus** has two themes.\n'); + if (u.includes('scores.csv')) return content(csv); + return content('{"a":{"b":1}}'); + }); + vi.stubGlobal('fetch', fetchMock); +}); +afterEach(() => vi.unstubAllGlobals()); + +// ─── Output blocks and previews ────────────────────────────────────────────── + +describe('a single run with results', () => { + it('shows each output as its own block with label, type, Download and Copy location', async () => { + renderResults(); + const blocks = await screen.findAllByTestId('output-block'); + expect(blocks).toHaveLength(3); + const report = blocks[0]!; + expect(report).toHaveTextContent('report'); + expect(report).toHaveTextContent('Markdown'); + expect(await within(report).findByTestId('download-file')).toBeInTheDocument(); + expect(within(report).getByTestId('copy-location')).toBeInTheDocument(); + expect(blocks[1]).toHaveTextContent('CSV'); + // One table-free block per output, not one column each. + expect(screen.queryByTestId('results-table-m1')).not.toBeInTheDocument(); + }); + + it('renders the Markdown report inline once it is open', async () => { + renderResults(); + expect(await screen.findByRole('heading', { name: 'Findings' })).toBeInTheDocument(); + expect(screen.getByText('corpus').tagName).toBe('STRONG'); + const call = contentCalls()[0]!; + expect(String(call[0])).toContain('path=home%2Fu%2Fout%2Freport.md'); + }); + + it('reads a file only when its block is open, and shows CSV as a small table', async () => { + renderResults(); + await screen.findByRole('heading', { name: 'Findings' }); + // The CSV and JSON blocks are closed: nothing of theirs was fetched yet. + expect(contentCalls().filter(([u]) => String(u).includes('scores.csv'))).toHaveLength(0); + expect(contentCalls().filter(([u]) => String(u).includes('lexicon.json'))).toHaveLength(0); + + const toggles = screen.getAllByTestId('preview-toggle'); + expect(toggles[1]).toHaveAttribute('aria-expanded', 'false'); + fireEvent.click(toggles[1]!); + const table = await screen.findByTestId('preview-csv'); + // Header plus the first rows only, not all fifteen lines. + expect(within(table).getAllByRole('row').length).toBeLessThanOrEqual(10); + expect(within(table).getByText('row1')).toBeInTheDocument(); + expect(within(table).queryByText('row14')).not.toBeInTheDocument(); + expect(toggles[1]).toHaveAttribute('aria-expanded', 'true'); + }); + + it('pretty-prints JSON when it is opened', async () => { + renderResults(); + await screen.findByRole('heading', { name: 'Findings' }); + fireEvent.click(screen.getAllByTestId('preview-toggle')[2]!); + expect(await screen.findByTestId('preview-json')).toHaveTextContent('"b": 1'); + }); + + it('closes a preview again', async () => { + renderResults(); + await screen.findByRole('heading', { name: 'Findings' }); + fireEvent.click(screen.getAllByTestId('preview-toggle')[0]!); + expect(screen.queryByRole('heading', { name: 'Findings' })).not.toBeInTheDocument(); + }); + + it('says the preview is unavailable, with a Download, when the file cannot be read', async () => { + fetchMock.mockImplementation(async (url: string) => + String(url).includes('/files/content') + ? { ok: false, status: 500, json: () => Promise.resolve({ message: 'nope' }) } + : listing, + ); + renderResults(); + const fallback = await screen.findByTestId('preview-unavailable'); + expect(fallback).toHaveTextContent('Preview unavailable'); + expect(within(fallback).getByRole('button', { name: /download/i })).toBeInTheDocument(); + expect(screen.queryByRole('alert')).not.toBeInTheDocument(); + }); + + it('does not open the report by itself when its size is not known or is large', async () => { + fetchMock.mockImplementation(async (url: string) => + String(url).includes('/files/content') + ? content('# Findings\n') + : listingOf({ [REPORT]: 5 * 1024 * 1024 }), + ); + renderResults(); + const toggles = await screen.findAllByTestId('preview-toggle'); + expect(toggles[0]).toHaveAttribute('aria-expanded', 'false'); + expect(contentCalls()).toHaveLength(0); + // Asking for it opens it. + fireEvent.click(toggles[0]!); + expect(await screen.findByRole('heading', { name: 'Findings' })).toBeInTheDocument(); + }); + + it('reads as a stream and cancels at the 200 KB cap, saying only the start is shown', async () => { + const big = '# Big\n\n' + 'word '.repeat(150_000); + fetchMock.mockImplementation(async (url: string) => + String(url).includes('/files/content') + ? content(big, 64 * 1024) + : listingOf({ [REPORT]: 90_000, [TABLE]: 2000, [LEXICON]: 100 }), + ); + renderResults(); + await screen.findByRole('heading', { name: 'Big' }); + expect(lastCancel).toHaveBeenCalled(); + expect(screen.getByTestId('preview-note')).toHaveTextContent('Only the start of the file'); + }); + + it('never previews or fetches a web address, and still offers its Download link', async () => { + renderResults({ + executions: { + m1: { + loading: false, + executions: [ + run('exec-1', { + results: { + 'o-report': { id: 'o-report', name: 'report.md', url: 'https://evil.example/r.md' }, + }, + }), + ], + }, + }, + }); + const link = await screen.findByTestId('download-link'); + expect(link).toHaveAttribute('href', 'https://evil.example/r.md'); + expect(screen.queryByTestId('preview-toggle')).not.toBeInTheDocument(); + expect( + fetchMock.mock.calls.map(([u]) => String(u)).some((u) => u.includes('evil.example')), + ).toBe(false); + }); + + it('says a CSV was cut short only when it was', async () => { + renderResults(); + await screen.findByRole('heading', { name: 'Findings' }); + fireEvent.click(screen.getAllByTestId('preview-toggle')[1]!); + await screen.findByTestId('preview-csv'); + expect(screen.getByTestId('preview-note')).toHaveTextContent('Only the first 9 rows'); + }); + + it('shows a short CSV with no caption', async () => { + fetchMock.mockImplementation(async (url: string) => + String(url).includes('/files/content') ? content('id,score\nr1,1\nr2,2\n') : listing, + ); + renderResults(); + await screen.findAllByTestId('output-block'); + fireEvent.click(screen.getAllByTestId('preview-toggle')[1]!); + await screen.findByTestId('preview-csv'); + expect(screen.queryByTestId('preview-note')).not.toBeInTheDocument(); + }); + + it('falls back quietly when Tapis refuses the file (403) too', async () => { + fetchMock.mockImplementation(async (url: string) => + String(url).includes('/files/content') + ? { ok: false, status: 403, json: () => Promise.resolve({ message: 'raw' }) } + : listing, + ); + renderResults(); + expect(await screen.findByTestId('preview-unavailable')).toBeInTheDocument(); + expect(screen.queryByRole('alert')).not.toBeInTheDocument(); + }); + + it('keeps how it was made collapsed, with the inputs and the settings', async () => { + renderResults(); + await screen.findAllByTestId('output-block'); + const how = screen.getByTestId('how-made-m1') as HTMLDetailsElement; + expect(how.open).toBe(false); + expect(within(how).getByRole('link', { name: 'a.txt' })).toBeInTheDocument(); + expect(within(how).getByTestId('results-settings-m1')).toHaveTextContent('match mode'); + }); + + it('has no boilerplate sentences and one refresh control', async () => { + renderResults(); + await screen.findAllByTestId('output-block'); + expect(screen.queryByText(/below are results/i)).not.toBeInTheDocument(); + expect(screen.queryByText(/runs required/i)).not.toBeInTheDocument(); + expect(screen.queryByText(/fetch results/i)).not.toBeInTheDocument(); + expect(screen.queryByRole('button', { name: /reload/i })).not.toBeInTheDocument(); + expect(screen.getAllByRole('button', { name: /refresh/i })).toHaveLength(1); + }); +}); + +// ─── Card ──────────────────────────────────────────────────────────────────── + +describe('the model card', () => { + it('shows the status in the header and collapses on a click', async () => { + renderResults(); + const head = screen.getByRole('button', { name: /sensemaking/i }); + expect(head).toHaveTextContent('Completed'); + expect(head).toHaveAttribute('aria-expanded', 'true'); + await screen.findAllByTestId('output-block'); + fireEvent.click(head); + expect(head).toHaveAttribute('aria-expanded', 'false'); + expect(screen.queryByTestId('output-block')).not.toBeInTheDocument(); + }); +}); + +// ─── Not finished ──────────────────────────────────────────────────────────── + +describe('a run that has not finished', () => { + const unfinished = { + loading: false, + executions: [run('exec-1', { status: 'RUNNING', results: {} })], + }; + const failedExecutions = { + loading: false, + executions: [run('exec-1', { status: 'FAILURE', results: {} })], + }; + + it('says a paused run waits for the user, with a way to the Runs step', () => { + const onOpenRuns = vi.fn(); + renderResults({ + threadData: thread({ successful_runs: 0 }), + executions: { m1: unfinished }, + unifiedRuns: { m1: { run_id: 'ue_1', status: 'awaiting_edit' } }, + onOpenRuns, + }); + expect(screen.getByTestId('result-paused-m1')).toHaveTextContent('Waiting for your edit'); + expect(screen.queryByText(/no results available/i)).not.toBeInTheDocument(); + expect(screen.queryByText(/1 running/i)).not.toBeInTheDocument(); + fireEvent.click(screen.getByTestId('open-runs-m1')); + expect(onOpenRuns).toHaveBeenCalled(); + }); + + it('says Running for a run in progress', () => { + renderResults({ threadData: thread({ successful_runs: 0 }), executions: { m1: unfinished } }); + expect(screen.getByTestId('result-running-m1')).toHaveTextContent('Running'); + expect(screen.queryByTestId('output-block')).not.toBeInTheDocument(); + }); + + const failedThread = thread({ successful_runs: 0, failed_runs: 1 }); + const failedRun: UnifiedRunSnapshot = { + run_id: 'ue_1', + status: 'failed', + failure_code: 'MODEL_FAILED', + error_message: 'job exited with code 2', + }; + + it('gives a failed run its reason and a Restart that sends it and opens the Runs step', async () => { + const onRestartRuns = vi.fn().mockResolvedValue(undefined); + const onOpenRuns = vi.fn(); + renderResults({ + threadData: failedThread, + executions: { m1: failedExecutions }, + unifiedRuns: { m1: failedRun }, + onRestartRuns, + onOpenRuns, + }); + expect(screen.getByText('job exited with code 2')).toBeInTheDocument(); + fireEvent.click(screen.getByRole('button', { name: 'Restart' })); + await waitFor(() => expect(onRestartRuns).toHaveBeenCalledWith('m1', { maxMinutes: 60 })); + await waitFor(() => expect(onOpenRuns).toHaveBeenCalled()); + }); + + it('carries the earlier run time into the restart', async () => { + const onRestartRuns = vi.fn().mockResolvedValue(undefined); + renderResults({ + threadData: failedThread, + executions: { m1: failedExecutions }, + unifiedRuns: { m1: { ...failedRun, model_result: { __max_minutes: 180, __queue: 'q' } } }, + onRestartRuns, + }); + fireEvent.click(screen.getByRole('button', { name: 'Restart' })); + await waitFor(() => + expect(onRestartRuns).toHaveBeenCalledWith('m1', { maxMinutes: 180, queue: 'q' }), + ); + }); + + it('disables Restart while it sends and shows a refusal', async () => { + let fail: (error: Error) => void = () => undefined; + const onRestartRuns = vi.fn( + () => + new Promise((_, reject) => { + fail = reject; + }), + ); + renderResults({ + threadData: failedThread, + executions: { m1: failedExecutions }, + unifiedRuns: { m1: failedRun }, + onRestartRuns, + }); + fireEvent.click(screen.getByRole('button', { name: 'Restart' })); + expect(await screen.findByRole('button', { name: /restarting/i })).toBeDisabled(); + fail(new EnsembleManagerError(500, 'refused')); + expect(await screen.findByTestId('restart-error-m1')).toHaveTextContent('refused'); + expect(screen.getByRole('button', { name: 'Restart' })).toBeEnabled(); + }); + + it('shows Restart as in flight when the Runs step started it', () => { + renderResults({ + threadData: failedThread, + executions: { m1: failedExecutions }, + unifiedRuns: { m1: failedRun }, + onRestartRuns: vi.fn(), + restartingModels: { m1: true }, + }); + expect(screen.getByRole('button', { name: /restarting/i })).toBeDisabled(); + }); + + it('offers no Restart without permission, or while other runs are still going', () => { + const view = renderResults({ + threadData: failedThread, + executions: { m1: failedExecutions }, + unifiedRuns: { m1: failedRun }, + }); + expect(screen.queryByRole('button', { name: /restart/i })).not.toBeInTheDocument(); + view.unmount(); + renderResults({ + threadData: thread({ total_runs: 2, submitted_runs: 2, successful_runs: 0, failed_runs: 1 }), + executions: { + m1: { + loading: false, + executions: [ + run('exec-1', { status: 'FAILURE', results: {} }), + run('exec-2', { status: 'RUNNING', results: {} }), + ], + }, + }, + unifiedRuns: { m1: failedRun }, + onRestartRuns: vi.fn(), + }); + expect(screen.queryByRole('button', { name: /restart/i })).not.toBeInTheDocument(); + }); +}); + +// ─── Several runs ──────────────────────────────────────────────────────────── + +describe('several runs of one model', () => { + it('keeps one results table, trimmed to the settings that differ', () => { + const sweep = thread({ total_runs: 2, submitted_runs: 2, successful_runs: 2 }); + renderResults({ + threadData: sweep, + executions: { + m1: { + loading: false, + executions: [ + run('exec-1', { bindings: { 'p-depth': '1', 'p-mode': 'x' } }), + run('exec-2', { bindings: { 'p-depth': '2', 'p-mode': 'x' } }), + ], + }, + }, + }); + const table = screen.getByTestId('results-table-m1'); + expect(within(table).getByText('flood depth')).toBeInTheDocument(); + expect(within(table).queryByText('match mode')).not.toBeInTheDocument(); + expect(screen.getByTestId('results-same-m1')).toHaveTextContent('match mode x'); + expect(screen.queryByTestId('output-block')).not.toBeInTheDocument(); + expect(screen.queryByText(/Page \d+ of/)).not.toBeInTheDocument(); + }); +}); diff --git a/ui-react/src/pages/modeling/thread/__tests__/MintRuns.test.tsx b/ui-react/src/pages/modeling/thread/__tests__/MintRuns.test.tsx index 73bf3ccc..ff6fedd3 100644 --- a/ui-react/src/pages/modeling/thread/__tests__/MintRuns.test.tsx +++ b/ui-react/src/pages/modeling/thread/__tests__/MintRuns.test.tsx @@ -2,7 +2,7 @@ * Tests for MintRuns — execution lifecycle step. */ import { afterEach, beforeEach, describe, expect, it, vi, type Mock } from 'vitest'; -import { screen, fireEvent, waitFor } from '@testing-library/react'; +import { screen, fireEvent, waitFor, within } from '@testing-library/react'; import { renderWithProviders } from '@/test/utils/render'; import { storeTokens } from '@/lib/auth/token-store'; import { MintRuns } from '../MintRuns'; @@ -856,7 +856,7 @@ describe('MintRuns log fetch authentication', () => { onSubmitRuns={vi.fn()} />, ); - fireEvent.click(screen.getByRole('button', { name: /view log/i })); + fireEvent.click(screen.getByRole('button', { name: /^log$/i })); } function fetchInit(): RequestInit { @@ -943,3 +943,81 @@ describe('MintRuns archived files', () => { ); }); }); + +// A flow can expose a dozen settings; only the ones that tell runs apart earn +// a column, the rest are summarised once above the table. +describe('MintRuns runs table columns', () => { + const model = { + ...mockModel, + input_parameters: [ + { id: 'p-depth', name: 'flood_depth', type: 'float', default: '10' }, + { id: 'p-mode', name: 'match_mode', type: 'string', default: 'longest_match' }, + { id: 'p-max', name: 'max_files', type: 'int', default: '200' }, + ], + }; + const thread: ThreadExecutionData = { + ...mockThreadDataSubmitted, + models: { 'model-1': model }, + model_ensembles: { + 'model-1': { + id: 'ens-1', + bindings: { 'p-depth': ['15', '25'], 'p-mode': ['longest_match'], 'p-max': ['200'] }, + }, + }, + }; + const run = (id: string, bindings: Record) => ({ + id, + modelid: 'model-1', + status: 'SUCCESS', + run_progress: 1, + bindings, + results: {}, + }); + + function renderRuns(runs: ReturnType[]) { + return renderWithProviders( + , + ); + } + + it('shows no parameter columns for one run and lists every value as shared', () => { + renderRuns([run('exec-1', { 'p-depth': '15' })]); + const table = screen.getByTestId('runs-table-model-1'); + expect(within(table).queryByText('Parameters')).not.toBeInTheDocument(); + expect(within(table).queryByText('flood depth')).not.toBeInTheDocument(); + const line = screen.getByTestId('same-for-every-run-model-1'); + expect(line).toHaveTextContent('Same for every run:'); + expect(line).toHaveTextContent('flood depth 15'); + expect(line).toHaveTextContent('match mode longest_match'); + expect(line).toHaveTextContent('max files 200'); + }); + + it('gives only the parameter that differs a column', () => { + renderRuns([run('exec-1', { 'p-depth': '15' }), run('exec-2', { 'p-depth': '25' })]); + const table = screen.getByTestId('runs-table-model-1'); + expect(within(table).getByText('flood depth')).toBeInTheDocument(); + expect(within(table).queryByText('match mode')).not.toBeInTheDocument(); + expect(within(table).queryByText('max files')).not.toBeInTheDocument(); + expect(within(table).getByText('15')).toBeInTheDocument(); + expect(within(table).getByText('25')).toBeInTheDocument(); + const line = screen.getByTestId('same-for-every-run-model-1'); + expect(line).toHaveTextContent('match mode longest_match'); + expect(line).not.toHaveTextContent('flood depth'); + }); + + it('does not wrap the table in a vertically scrolling box', () => { + renderRuns([run('exec-1', {}), run('exec-2', {})]); + const wrapper = screen.getByTestId('runs-table-model-1').parentElement; + expect(wrapper?.className).not.toContain('max-h-96'); + expect(wrapper?.className).not.toMatch(/\boverflow-(auto|y-)/); + }); +}); diff --git a/ui-react/src/pages/modeling/thread/__tests__/MintRunsCards.test.tsx b/ui-react/src/pages/modeling/thread/__tests__/MintRunsCards.test.tsx new file mode 100644 index 00000000..5e6b42ae --- /dev/null +++ b/ui-react/src/pages/modeling/thread/__tests__/MintRunsCards.test.tsx @@ -0,0 +1,612 @@ +/** + * The Runs step's cards: one collapsible card per model, steps shown once, a + * single run without the ensemble furniture, and Restart for a run that failed. + */ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { fireEvent, screen, waitFor, within } from '@testing-library/react'; + +import { renderWithProviders } from '@/test/utils/render'; +import { MintRuns } from '../MintRuns'; +import type { + Execution, + ExecutionSummary, + ModelExecutionsMap, + ThreadExecutionData, +} from '@/graphql/generated/execution'; +import { EnsembleManagerError, type UnifiedRunSnapshot } from '@/lib/ensemble-manager'; + +// ─── Fixtures ──────────────────────────────────────────────────────────────── + +const params = [ + { id: 'p-depth', name: 'flood_depth', type: 'float', default: '10' }, + { id: 'p-mode', name: 'match_mode', type: 'string', default: 'longest' }, +]; + +function model(id: string, extra: Record = {}) { + return { + id, + name: `Model ${id}`, + input_parameters: params, + input_files: [], + output_files: [{ id: `${id}-out`, name: 'report' }], + ...extra, + }; +} + +function summary(over: Partial = {}): ExecutionSummary { + return { + total_runs: 1, + submitted_runs: 1, + successful_runs: 1, + failed_runs: 0, + submitted_for_execution: true, + submission_time: '2024-01-01T00:00:00Z', + ...over, + }; +} + +function thread( + models: Array<{ + id: string; + model?: Record; + summary?: Partial; + }>, +): ThreadExecutionData { + return { + id: 'thread-1', + models: Object.fromEntries(models.map((m) => [m.id, model(m.id, m.model)])) as never, + model_ensembles: Object.fromEntries( + models.map((m) => [ + m.id, + { id: `ens-${m.id}`, bindings: { 'p-depth': ['10'], 'p-mode': ['longest'] } }, + ]), + ), + execution_summary: Object.fromEntries(models.map((m) => [m.id, summary(m.summary)])), + data: {}, + }; +} + +const execution = (id: string, modelId: string, over: Partial = {}): Execution => ({ + id, + modelid: modelId, + status: 'SUCCESS', + run_progress: 1, + start_time: '2024-01-01T00:00:00Z', + end_time: '2024-01-01T00:04:00Z', + bindings: {}, + results: {}, + ...over, +}); + +const stages = [ + { + stage_id: 'step-1', + type: 'flow_step', + name: 'Ingest corpus', + depends_on: [], + status: 'completed', + provider: 'tapis-workflows', + provider_ids: { task_id: 'step-1-etl_process' }, + }, + { + stage_id: 'step-2', + type: 'flow_step', + name: 'Embed', + depends_on: ['step-1'], + status: 'running', + provider: 'tapis-workflows', + provider_ids: { task_id: 'step-2-etl_process' }, + }, +]; + +const flowRun = (over: Partial = {}): UnifiedRunSnapshot => ({ + run_id: 'ue_flow-1', + status: 'adapter_running', + adapter_stage: 'flow', + execution_mode: 'workflow_pipeline', + tapis_workflow: { + provider: 'tapis-workflows', + workflow_id: 'wf-1', + run_id: 'wr-1', + stage_count: 2, + }, + workflow_stages: stages, + ...over, +}); + +function renderRuns(props: Partial> = {}) { + return renderWithProviders( + , + ); +} + +const header = (name: RegExp | string) => screen.getByRole('button', { name }); + +beforeEach(() => { + vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true, json: () => Promise.resolve({}) })); +}); +afterEach(() => vi.unstubAllGlobals()); + +// ─── A single run ──────────────────────────────────────────────────────────── + +describe('a single run', () => { + const flowThread = thread([ + { + id: 'flow-1', + model: { isFlow: true, input_parameters: [] }, + summary: { successful_runs: 0 }, + }, + ]); + const flowExecutions: ModelExecutionsMap = { + 'flow-1': { + loading: false, + executions: [execution('exec-1', 'flow-1', { status: 'RUNNING' })], + }, + }; + + it('has no counters, run configuration box, pager or run table, and has Log and Files', () => { + renderRuns({ + threadData: flowThread, + executions: flowExecutions, + unifiedRuns: { 'flow-1': flowRun() }, + }); + expect(screen.queryByTestId('run-counters-flow-1')).not.toBeInTheDocument(); + expect(screen.queryByText(/run configuration/i)).not.toBeInTheDocument(); + expect(screen.queryByText(/Page \d+ of/)).not.toBeInTheDocument(); + expect(screen.queryByRole('button', { name: 'Back' })).not.toBeInTheDocument(); + expect(screen.queryByTestId('runs-table-flow-1')).not.toBeInTheDocument(); + expect(screen.queryByText(/technical identifiers/i)).not.toBeInTheDocument(); + expect(screen.getByRole('button', { name: 'Log' })).toBeInTheDocument(); + expect(screen.getByTestId('view-files-exec-1')).toBeInTheDocument(); + }); + + it('shows each step once and keeps task ids under a collapsed Technical details', () => { + renderRuns({ + threadData: flowThread, + executions: flowExecutions, + unifiedRuns: { 'flow-1': flowRun() }, + }); + expect(screen.getAllByTestId(/^flow-node-/)).toHaveLength(2); + const details = screen.getByTestId('flow-technical-details') as HTMLDetailsElement; + expect(details.open).toBe(false); + expect(within(details).getByText('step-2-etl_process')).toBeInTheDocument(); + expect(screen.queryByText(/^Task step-2/)).not.toBeInTheDocument(); + }); + + it('puts the log and files buttons on the card and opens the log', async () => { + (globalThis.fetch as ReturnType).mockResolvedValue({ + ok: true, + text: () => Promise.resolve('log line'), + }); + renderRuns({ + threadData: flowThread, + executions: flowExecutions, + unifiedRuns: { 'flow-1': flowRun() }, + }); + fireEvent.click(screen.getByRole('button', { name: 'Log' })); + expect(await screen.findByRole('dialog', { name: 'Run log' })).toBeInTheDocument(); + await waitFor(() => expect(screen.getByText('log line')).toBeInTheDocument()); + }); + + it('lists the settings used collapsed, those changed from default first', () => { + renderRuns({ + threadData: thread([{ id: 'm1' }]), + executions: { + m1: { + loading: false, + executions: [execution('exec-1', 'm1', { bindings: { 'p-mode': 'shortest' } })], + }, + }, + }); + const used = screen.getByTestId('settings-used-m1') as HTMLDetailsElement; + expect(used.open).toBe(false); + expect(used).toHaveTextContent('1 changed from default'); + const terms = within(used) + .getAllByRole('term') + .map((t) => t.textContent); + expect(terms[0]).toContain('match mode'); + expect(terms[1]).toContain('flood depth'); + }); +}); + +// ─── Several runs ──────────────────────────────────────────────────────────── + +describe('several runs of one model', () => { + const sweep = thread([ + { + id: 'm1', + summary: { total_runs: 3, submitted_runs: 3, successful_runs: 2, failed_runs: 0 }, + }, + ]); + const rows = (...statuses: string[]): ModelExecutionsMap => ({ + m1: { + loading: false, + executions: statuses.map((status, i) => + execution(`exec-${i + 1}`, 'm1', { status, bindings: { 'p-depth': String(i) } }), + ), + }, + }); + + it('shows compact counters and the table, and no pager for one page', () => { + renderRuns({ threadData: sweep, executions: rows('SUCCESS', 'SUCCESS', 'RUNNING') }); + expect(screen.getByTestId('run-counters-m1')).toHaveTextContent('2 of 3 completed'); + expect(screen.getByTestId('run-counters-m1')).toHaveTextContent('1 running'); + expect(screen.getByTestId('runs-table-m1')).toBeInTheDocument(); + expect(screen.queryByText(/Page \d+ of/)).not.toBeInTheDocument(); + expect(screen.queryByTestId('single-run-m1')).not.toBeInTheDocument(); + expect(screen.queryByText(/run configuration/i)).not.toBeInTheDocument(); + // The table still trims: only what differs between runs is a column. + const table = screen.getByTestId('runs-table-m1'); + expect(within(table).getByText('flood depth')).toBeInTheDocument(); + expect(within(table).queryByText('match mode')).not.toBeInTheDocument(); + expect(screen.getByTestId('same-for-every-run-m1')).toHaveTextContent('match mode longest'); + }); + + it('shows a pager only when there is more than one page', () => { + renderRuns({ + threadData: thread([ + { id: 'm1', summary: { total_runs: 250, submitted_runs: 250, successful_runs: 0 } }, + ]), + executions: rows('RUNNING', 'RUNNING'), + }); + expect(screen.getByText('Page 1 of 3')).toBeInTheDocument(); + }); +}); + +// ─── Cards: collapse, defaults ─────────────────────────────────────────────── + +describe('collapsible model cards', () => { + const several = thread([ + { id: 'done', summary: { successful_runs: 1 } }, + { id: 'busy', summary: { successful_runs: 0 } }, + { id: 'broke', summary: { successful_runs: 0, failed_runs: 1 } }, + ]); + const execs: ModelExecutionsMap = { + done: { loading: false, executions: [execution('e-done', 'done')] }, + busy: { loading: false, executions: [execution('e-busy', 'busy', { status: 'RUNNING' })] }, + broke: { loading: false, executions: [execution('e-broke', 'broke', { status: 'FAILURE' })] }, + }; + + it('opens the ones that need attention and closes a completed one when there are several', () => { + renderRuns({ threadData: several, executions: execs }); + expect(header(/Model done/)).toHaveAttribute('aria-expanded', 'false'); + expect(header(/Model busy/)).toHaveAttribute('aria-expanded', 'true'); + expect(header(/Model broke/)).toHaveAttribute('aria-expanded', 'true'); + expect(screen.queryByTestId('view-files-e-done')).not.toBeInTheDocument(); + expect(screen.getByTestId('view-files-e-busy')).toBeInTheDocument(); + }); + + it('opens a completed model when it is the only one', () => { + renderRuns({ + threadData: thread([{ id: 'done', summary: { successful_runs: 1 } }]), + executions: { done: execs.done! }, + }); + expect(header(/Model done/)).toHaveAttribute('aria-expanded', 'true'); + }); + + it('toggles on a click, hides the content, and remembers the choice', () => { + const view = renderRuns({ threadData: several, executions: execs }); + fireEvent.click(header(/Model done/)); + expect(header(/Model done/)).toHaveAttribute('aria-expanded', 'true'); + expect(screen.getByTestId('view-files-e-done')).toBeInTheDocument(); + + fireEvent.click(header(/Model busy/)); + expect(header(/Model busy/)).toHaveAttribute('aria-expanded', 'false'); + expect(screen.queryByTestId('view-files-e-busy')).not.toBeInTheDocument(); + + // A re-render with the same data keeps what the user chose. + view.rerender( + , + ); + expect(header(/Model done/)).toHaveAttribute('aria-expanded', 'true'); + expect(header(/Model busy/)).toHaveAttribute('aria-expanded', 'false'); + }); + + it('shows the status and the time it took in the header', () => { + renderRuns({ threadData: several, executions: execs }); + expect(header(/Model done/)).toHaveTextContent('Completed'); + expect(header(/Model done/)).toHaveTextContent('took 4 min'); + expect(header(/Model busy/)).toHaveTextContent('Running'); + expect(header(/Model busy/)).toHaveTextContent('Started'); + }); +}); + +// ─── Paused ────────────────────────────────────────────────────────────────── + +describe('a run paused for an edit', () => { + it('shows the edit controls directly under the sentence, before the steps', () => { + const paused = flowRun({ + status: 'awaiting_edit', + started_by: 'alice', + model_child_id: 'exec-1', + pause: { + output_key: '1:1', + file: 'lexicon.json', + file_location: 'tapis://ls6/scratch/alice/lexicon.json', + paused_after_step: 1, + paused_at: null, + }, + }); + renderRuns({ + threadData: thread([ + { id: 'flow-1', model: { isFlow: true }, summary: { successful_runs: 0 } }, + ]), + executions: { + 'flow-1': { + loading: false, + executions: [execution('exec-1', 'flow-1', { status: 'RUNNING' })], + }, + }, + unifiedRuns: { 'flow-1': paused }, + currentUsername: 'alice', + onResumeWorkflow: vi.fn(), + }); + const panel = screen.getByTestId('flow-run-panel'); + expect(panel.firstElementChild).toBe(screen.getByTestId('flow-paused')); + expect(screen.getByRole('button', { name: 'Upload the edited file' })).toBeInTheDocument(); + expect(screen.getByTestId('resume-flow')).toBeInTheDocument(); + expect(header(/Model flow-1/)).toHaveAttribute('aria-expanded', 'true'); + }); +}); + +// ─── Restart ───────────────────────────────────────────────────────────────── + +describe('restarting a failed run', () => { + const failedThread = thread([ + { id: 'm1', summary: { successful_runs: 0, failed_runs: 1 } }, + { id: 'm2', summary: { successful_runs: 1 } }, + ]); + const failedRows: ModelExecutionsMap = { + m1: { loading: false, executions: [execution('exec-1', 'm1', { status: 'FAILURE' })] }, + m2: { loading: false, executions: [execution('exec-2', 'm2')] }, + }; + const failure = (over: Partial = {}): UnifiedRunSnapshot => ({ + run_id: 'ue_m1', + status: 'failed', + execution_mode: 'workflow_pipeline', + failure_code: 'MODEL_FAILED', + error_message: 'job exited with code 2', + ...over, + }); + + it('shows the reason and a Restart button on the failed model only', () => { + renderRuns({ + threadData: failedThread, + executions: failedRows, + unifiedRuns: { m1: failure() }, + }); + expect(screen.getByText('Failure: MODEL_FAILED')).toBeInTheDocument(); + expect(screen.getByText('job exited with code 2')).toBeInTheDocument(); + expect(screen.getAllByRole('button', { name: 'Restart' })).toHaveLength(1); + }); + + it('sends that model again, and only that model', async () => { + const onSubmitRuns = vi.fn().mockResolvedValue(undefined); + renderRuns({ + threadData: failedThread, + executions: failedRows, + unifiedRuns: { m1: failure() }, + onSubmitRuns, + }); + fireEvent.click(screen.getByRole('button', { name: 'Restart' })); + await waitFor(() => expect(onSubmitRuns).toHaveBeenCalledTimes(1)); + expect(onSubmitRuns).toHaveBeenCalledWith('m1', 60); + }); + + it('disables Restart while the send is in flight, so it cannot start twice', async () => { + let finish: () => void = () => undefined; + const onSubmitRuns = vi.fn( + () => + new Promise((resolve) => { + finish = resolve; + }), + ); + renderRuns({ + threadData: failedThread, + executions: failedRows, + unifiedRuns: { m1: failure() }, + onSubmitRuns, + }); + fireEvent.click(screen.getByRole('button', { name: 'Restart' })); + const pending = await screen.findByRole('button', { name: /restarting/i }); + expect(pending).toBeDisabled(); + fireEvent.click(pending); + expect(onSubmitRuns).toHaveBeenCalledTimes(1); + finish(); + await waitFor(() => expect(screen.getByRole('button', { name: 'Restart' })).toBeEnabled()); + }); + + it('shows why a restart was refused', async () => { + renderRuns({ + threadData: failedThread, + executions: failedRows, + unifiedRuns: { m1: failure() }, + onSubmitRuns: vi.fn().mockRejectedValue(new EnsembleManagerError(500, 'boom')), + }); + fireEvent.click(screen.getByRole('button', { name: 'Restart' })); + expect(await screen.findByText('boom')).toBeInTheDocument(); + }); + + it('restarts a failed flow too, and not while it is running or without permission', () => { + const flowThread = thread([ + { id: 'flow-1', model: { isFlow: true }, summary: { successful_runs: 0, failed_runs: 1 } }, + ]); + const flowFailed = flowRun({ status: 'failed', failure_code: 'FLOW_FAILED' }); + const view = renderRuns({ threadData: flowThread, unifiedRuns: { 'flow-1': flowFailed } }); + expect(screen.getByRole('button', { name: 'Restart' })).toBeInTheDocument(); + view.unmount(); + + renderRuns({ threadData: flowThread, unifiedRuns: { 'flow-1': flowFailed }, canWrite: false }); + expect(screen.queryByRole('button', { name: 'Restart' })).not.toBeInTheDocument(); + }); + + it('offers no Restart on a run that is still running', () => { + renderRuns({ + threadData: thread([{ id: 'm1', summary: { successful_runs: 0 } }]), + executions: { + m1: { loading: false, executions: [execution('exec-1', 'm1', { status: 'RUNNING' })] }, + }, + }); + expect(screen.queryByRole('button', { name: /restart/i })).not.toBeInTheDocument(); + }); + + const sweepThread = thread([ + { id: 'm1', summary: { total_runs: 3, submitted_runs: 3, successful_runs: 1, failed_runs: 2 } }, + ]); + const sweepRows = (...statuses: string[]): ModelExecutionsMap => ({ + m1: { + loading: false, + executions: statuses.map((status, i) => + execution(`exec-${i + 1}`, 'm1', { status, bindings: { 'p-depth': String(i + 1) } }), + ), + }, + }); + + it('has no per-row Restart in a table of runs; one model-level button asks to confirm first', async () => { + const onSubmitRuns = vi.fn().mockResolvedValue(undefined); + renderRuns({ + threadData: sweepThread, + executions: sweepRows('FAILURE', 'SUCCESS', 'FAILURE'), + onSubmitRuns, + }); + expect(screen.queryByTestId(/^restart-run-exec-/)).not.toBeInTheDocument(); + fireEvent.click(screen.getByRole('button', { name: 'Restart all 3 runs' })); + // Nothing is sent until the person confirms, and the card says what is sent. + expect(onSubmitRuns).not.toHaveBeenCalled(); + expect(screen.getByTestId('restart-confirm-m1')).toHaveTextContent( + 'sends all 3 runs again, including the 1 that succeeded', + ); + fireEvent.click(screen.getByRole('button', { name: 'Cancel' })); + expect(onSubmitRuns).not.toHaveBeenCalled(); + + fireEvent.click(screen.getByRole('button', { name: 'Restart all 3 runs' })); + fireEvent.click(screen.getByTestId('restart-confirm-yes-m1')); + await waitFor(() => expect(onSubmitRuns).toHaveBeenCalledWith('m1', 60)); + }); + + it('offers no Restart while a sibling run is still running, even though one failed', () => { + renderRuns({ + threadData: thread([ + { + id: 'm1', + summary: { total_runs: 3, submitted_runs: 3, successful_runs: 0, failed_runs: 1 }, + }, + ]), + executions: sweepRows('FAILURE', 'RUNNING', 'SUCCESS'), + unifiedRuns: { m1: failure() }, + }); + expect(screen.queryByRole('button', { name: /restart/i })).not.toBeInTheDocument(); + expect( + screen.getByText(/restart is offered when the others have finished/i), + ).toBeInTheDocument(); + }); + + it('carries the earlier run time and queue, and lets the time be changed', async () => { + const onRestartRuns = vi.fn().mockResolvedValue(undefined); + renderRuns({ + threadData: failedThread, + executions: failedRows, + unifiedRuns: { m1: failure({ model_result: { __max_minutes: 240, __queue: 'skx-dev' } }) }, + onRestartRuns, + }); + const minutes = screen.getByTestId('restart-minutes-m1'); + expect(minutes).toHaveValue(240); + fireEvent.change(minutes, { target: { value: '300' } }); + fireEvent.click(screen.getByRole('button', { name: 'Restart' })); + await waitFor(() => + expect(onRestartRuns).toHaveBeenCalledWith('m1', { maxMinutes: 300, queue: 'skx-dev' }), + ); + }); + + it('shows the default time that will be used when the earlier one was not kept', () => { + renderRuns({ + threadData: failedThread, + executions: failedRows, + unifiedRuns: { m1: failure() }, + }); + expect(screen.getByTestId('restart-minutes-m1')).toHaveValue(60); + expect(screen.getByText(/earlier run's time was not kept/i)).toBeInTheDocument(); + }); + + describe('publishing', () => { + const publisher = { + transform_spec_id: 'spec-1', + name: 'Publish', + catalog_tokens: ['ckan', 'stac'], + parameters: [], + can_publish: { ok: true, reason: null }, + }; + const planWithPublish = `em_${btoa(JSON.stringify({ publish: ['ckan', 'stac'] })) + .replace(/\+/g, '-') + .replace(/\//g, '_') + .replace(/=+$/, '')}.sig`; + + beforeEach(() => { + (globalThis.fetch as ReturnType).mockImplementation(async (url: string) => ({ + ok: true, + json: async () => + String(url).includes('/plans/publishers') ? { publishers: [publisher] } : {}, + })); + }); + + it('pre-selects what the earlier run published and sends it again', async () => { + const onRestartRuns = vi.fn().mockResolvedValue(undefined); + renderRuns({ + threadData: failedThread, + executions: failedRows, + unifiedRuns: { m1: failure({ plan_id: planWithPublish }) }, + onRestartRuns, + }); + expect(await screen.findByTestId('publish-catalog-m1')).toBeInTheDocument(); + expect(screen.getByTestId('publish-toggle-m1')).toBeChecked(); + fireEvent.click(screen.getByRole('button', { name: 'Restart' })); + await waitFor(() => expect(onRestartRuns).toHaveBeenCalled()); + expect(onRestartRuns.mock.calls[0]?.[1]).toMatchObject({ + publish: { tokens: ['ckan', 'stac'] }, + }); + }); + + it('sends no publish when the earlier run published nothing and none is chosen', async () => { + const onRestartRuns = vi.fn().mockResolvedValue(undefined); + renderRuns({ + threadData: failedThread, + executions: failedRows, + unifiedRuns: { m1: failure() }, + onRestartRuns, + }); + expect(await screen.findByTestId('publish-toggle-m1')).not.toBeChecked(); + fireEvent.click(screen.getByRole('button', { name: 'Restart' })); + await waitFor(() => expect(onRestartRuns).toHaveBeenCalled()); + expect(onRestartRuns.mock.calls[0]?.[1]).not.toHaveProperty('publish'); + }); + }); + + it('shows Restart as in flight when a send from the other step is running', () => { + renderRuns({ + threadData: failedThread, + executions: failedRows, + unifiedRuns: { m1: failure() }, + restartingModels: { m1: true }, + }); + expect(screen.getByRole('button', { name: /restarting/i })).toBeDisabled(); + }); +}); diff --git a/ui-react/src/pages/modeling/thread/__tests__/MintRunsFlow.test.tsx b/ui-react/src/pages/modeling/thread/__tests__/MintRunsFlow.test.tsx index 2c37ccb3..f2e87426 100644 --- a/ui-react/src/pages/modeling/thread/__tests__/MintRunsFlow.test.tsx +++ b/ui-react/src/pages/modeling/thread/__tests__/MintRunsFlow.test.tsx @@ -103,8 +103,8 @@ describe('MintRuns for a flow', () => { renderRuns(); expect(screen.getByTestId('flow-run-panel')).toBeInTheDocument(); expect(screen.queryByTestId('unified-execution-panel')).not.toBeInTheDocument(); - expect(screen.getByTestId('flow-step-step-1')).toHaveTextContent('Ingest corpus'); - expect(screen.getByTestId('flow-step-step-2')).toHaveTextContent('Running'); + expect(screen.getByTestId('flow-node-step-1')).toHaveTextContent('Ingest corpus'); + expect(screen.getByTestId('flow-node-step-2')).toHaveTextContent('Running'); expect(screen.queryByText(/run model/i)).not.toBeInTheDocument(); expect(screen.queryByText(/pass inputs to model/i)).not.toBeInTheDocument(); expect(screen.queryByText(/the model is running/i)).not.toBeInTheDocument(); @@ -201,7 +201,7 @@ describe('MintRuns for a flow', () => { expect(screen.getByText('Waiting for your edit')).toBeInTheDocument(); expect( screen.getByText( - 'The flow is paused for your edit. Download the file below, change it, upload your copy and resume.', + 'The flow is paused for your edit. Download the file, change it, upload your copy and resume.', ), ).toBeInTheDocument(); expect(screen.queryByText(/the flow is running/i)).not.toBeInTheDocument(); diff --git a/ui-react/src/pages/modeling/thread/__tests__/MintThread.test.tsx b/ui-react/src/pages/modeling/thread/__tests__/MintThread.test.tsx index 23e812f5..1dc8c918 100644 --- a/ui-react/src/pages/modeling/thread/__tests__/MintThread.test.tsx +++ b/ui-react/src/pages/modeling/thread/__tests__/MintThread.test.tsx @@ -194,7 +194,7 @@ const executionMock = { /** * The same thread once its runs have finished, which is the only state that - * renders the Results step's Fetch results button. + * renders the Results step's Refresh button. */ const executionFinishedMock = { request: { query: GetThreadExecutionDocument, variables: { id: 'test-thread-id' } }, @@ -396,7 +396,7 @@ describe('MintThread', () => { }); }); - it('wires Fetch results to the ensemble manager', async () => { + it('wires Refresh to the ensemble manager', async () => { // The regression this guards: MintThread rendered MintResults without // onPublishResults, and MintResults returns on its first line when that // prop is absent — so the button made no request at all (#110). Asserting @@ -412,7 +412,7 @@ describe('MintThread', () => { await waitFor(() => expect(resultsStep).not.toBeDisabled(), { timeout: 3000 }); resultsStep.click(); - const button = await screen.findByTestId('fetch-results-cfgA'); + const button = await screen.findByTestId('refresh-results-cfgA'); button.click(); await waitFor(() => diff --git a/ui-react/src/pages/modeling/thread/__tests__/PausedFlowStep.test.tsx b/ui-react/src/pages/modeling/thread/__tests__/PausedFlowStep.test.tsx index ca20a54a..4a70536d 100644 --- a/ui-react/src/pages/modeling/thread/__tests__/PausedFlowStep.test.tsx +++ b/ui-react/src/pages/modeling/thread/__tests__/PausedFlowStep.test.tsx @@ -254,7 +254,7 @@ describe('FlowRunPanel for a paused run', () => { />, ); expect(screen.getByTestId('flow-paused')).toHaveTextContent('Paused after “Draft the lexicon”'); - expect(screen.getByTestId('flow-step-step-2')).toHaveTextContent('Waiting'); + expect(screen.getByTestId('flow-node-step-2')).toHaveTextContent('Waiting'); await user.click(screen.getByTestId('cancel-workflow')); expect(screen.getByText(/It stops here and cannot be continued/)).toBeInTheDocument(); await user.click(screen.getByTestId('confirm-cancel-workflow')); diff --git a/ui-react/src/pages/modeling/thread/useTapisResultKind.ts b/ui-react/src/pages/modeling/thread/useTapisResultKind.ts new file mode 100644 index 00000000..1a274c6b --- /dev/null +++ b/ui-react/src/pages/modeling/thread/useTapisResultKind.ts @@ -0,0 +1,82 @@ +/** + * useTapisResultKind — whether Tapis says a result location is a file or a + * folder, and how big the file is. + * + * The Ensemble Manager lists a run with each result marked by Tapis; the name of + * the location is never used to guess. The size comes from the same listing. + */ +import { useEffect, useState } from 'react'; + +import { + fetchExecutionListing, + type ExecutionFile, + type ExecutionResultLocation, +} from '@/lib/ensemble-manager'; + +interface Listing { + files: ExecutionFile[]; + results: ExecutionResultLocation[]; +} + +// Cells of one run ask for the same listing; share the request while it is in flight. +const inFlight = new Map>(); + +function loadListing(api: string, executionId: string): Promise { + const key = `${api}|${executionId}`; + let pending = inFlight.get(key); + if (!pending) { + pending = fetchExecutionListing(api, executionId).finally(() => inFlight.delete(key)); + inFlight.set(key, pending); + } + return pending; +} + +export type TapisResultKind = 'file' | 'folder' | 'unknown' | 'error'; + +export interface TapisResultInfo { + kind: TapisResultKind; + /** The file's size in bytes when the listing gives one, else null. */ + size: number | null; +} + +export function useTapisResultInfo( + ensembleManagerApi: string, + executionId: string, + url: string, + enabled = true, +): TapisResultInfo { + const [info, setInfo] = useState({ kind: 'unknown', size: null }); + + useEffect(() => { + if (!enabled) return; + let cancelled = false; + setInfo({ kind: 'unknown', size: null }); + loadListing(ensembleManagerApi, executionId) + .then(({ files, results }) => { + if (cancelled) return; + const match = results.find((result) => result.url === url); + const size = files.find((file) => file.url === url)?.size; + setInfo( + match + ? { kind: match.kind, size: typeof size === 'number' && size > 0 ? size : null } + : { kind: 'error', size: null }, + ); + }) + .catch(() => !cancelled && setInfo({ kind: 'error', size: null })); + return () => { + cancelled = true; + }; + }, [ensembleManagerApi, executionId, url, enabled]); + + return info; +} + +/** Just the kind, for a result that needs no size. */ +export function useTapisResultKind( + ensembleManagerApi: string, + executionId: string, + url: string, + enabled = true, +): TapisResultKind { + return useTapisResultInfo(ensembleManagerApi, executionId, url, enabled).kind; +} diff --git a/ui-react/tailwind.config.ts b/ui-react/tailwind.config.ts index 0139c9e3..e88c8423 100644 --- a/ui-react/tailwind.config.ts +++ b/ui-react/tailwind.config.ts @@ -55,10 +55,20 @@ const config: Config = { from: { height: 'var(--radix-accordion-content-height)' }, to: { height: '0' }, }, + 'card-down': { + from: { height: '0', opacity: '0' }, + to: { height: 'var(--radix-accordion-content-height)', opacity: '1' }, + }, + 'card-up': { + from: { height: 'var(--radix-accordion-content-height)', opacity: '1' }, + to: { height: '0', opacity: '0' }, + }, }, animation: { 'accordion-down': 'accordion-down 0.2s ease-out', 'accordion-up': 'accordion-up 0.2s ease-out', + 'card-down': 'card-down 0.2s ease-out', + 'card-up': 'card-up 0.2s ease-out', }, borderRadius: { lg: 'var(--radius)',