Skip to content

feat: mlproject run options and chapkit mlproject export (CLIM-1435) - #141

Merged
mortenoh merged 5 commits into
mainfrom
feat/mlproject-run-options
Oct 15, 2026
Merged

mortenoh merged 5 commits into
mainfrom
feat/mlproject-run-options

Conversation

@mortenoh

@mortenoh mortenoh commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Follow-ups from adding an MLproject to chapkit_ghr_model (chap-models/chapkit_ghr_model#10), in two parts.

chapkit mlproject run: settings a hand-written main.py service has

  • meta_data.config_format: chap_core|flat in the MLproject, overridable with --config-format: layout of the config.yml the scripts read. Default stays chap_core (options nested under user_option_values, as chap-core does); flat suits scripts written for a main.py service. It lives in the MLproject so it reaches chap eval --run-config.as-chapkit and the -cli images, which start chapkit mlproject run . with fixed arguments. It goes under meta_data because chap-core's MLproject model is extra="forbid" at the top level (verified: a top-level key is rejected, an unknown meta_data key is ignored). mlproject migrate honours it too.
  • --database PATH: keep configs and trained models (artifacts) in a SQLite file so they survive a restart, and predict works on a model trained before it. Job history is not persisted: the job scheduler is in memory, so /api/v1/jobs starts empty after a restart. Takes a plain file path, no SQLAlchemy URL. The file and its directory are created if missing, migrations run on startup, and a relative path is relative to where the command runs (it is resolved before the service changes into the project directory). Without it, state stays in memory as before. A database file inside the project that the workspace copy would not skip (no .db / .sqlite* extension) gets a startup warning.
  • --keepalive-interval SECONDS: keepalive interval once registered with chap-core. Left to servicekit's default (10s) when not given; a warning is printed when it is set without an orchestrator.
  • --log-level: sets both servicekit's structured logging (LOG_LEVEL) and uvicorn, applied before the service is built so it also covers the build-time log lines. Defaults to LOG_LEVEL, then info.
  • The artifact hierarchy is named after the MLproject name (as a Python identifier, the same helper mlproject migrate uses) instead of always mlproject. State is in memory by default, so no stored artifacts carry the old name.
  • The startup output shows the config layout and where state is stored.

Considered and left out: --workers (the in-memory scheduler cannot be shared between processes), a registration key flag (kept env-only so the secret stays out of the process list), and --max-concurrency (SQLite write locking is why the default is 1).

chapkit mlproject export: generate an MLproject from main.py

The reverse of mlproject migrate, moved into chapkit from chapkit_ghr_model's tools/mlproject.py, which imported chapkit internals (RUNNER_PLACEHOLDERS, translate_to_runner_template, build_config_schema) a model repo should not depend on.

  • Finds the MLServiceInfo, the BaseConfig subclass, the ShellModelRunner and the ArtifactHierarchy in the module by type, so names do not matter (--config NAME when there are several config classes). Function runners are rejected with a clear error.
  • Writes the contract fields, meta_data, user_options (JSON Schema per field, enum $refs inlined, default_factory defaults resolved, BaseConfig's reserved fields only when their default or what they accept changes), adapters and the entry points with runner placeholders turned back into MLproject parameters. Literal braces are escaped for both str.format passes mlproject run applies.
  • --check writes nothing and fails when the file is stale (prints a diff), or when chapkit's own MLproject reader would serve a different /api/v1/info, config options, defaults or accepted values, commands or adapters.
  • Refuses to overwrite an MLproject it did not generate unless --force.
  • The module is compiled from source and run from its own directory (no __pycache__ left behind); what it prints while building the app goes to stderr, so --check output stays clean.
  • When the runner writes a flat config.yml, export writes meta_data.config_format: flat, and --check compares the layout.
  • Warns (export and --check, without failing) about settings an MLproject cannot carry: runner validation hooks, custom artifact level labels, with_auth, a non-default keepalive interval in with_registration (with the --keepalive-interval to restore it), and routes the MLproject service would not serve. Custom health checks cannot be detected and are documented instead. To see the keepalive interval, ServiceBuilder.build() now records the registration options on app.state.registration_options.
  • The docs say plainly that export runs the module, import-time side effects included.

Against chapkit_ghr_model's main.py, the export matches its committed MLproject apart from the header and the position of one description key, and --check passes.

Docs: a Service Options section in mlproject-runner.md and a new mlproject-export.md guide.

Follow-up

  • chap eval --run-config.as-chapkit runs the chapkit installed alongside chap-core (2.3.1 today), which ignores meta_data.config_format. Bump chap-core's chapkit dependency once this is released.

Test plan

  • make lint
  • make test (1085 passed)
  • mkdocs build --strict
  • run options: hierarchy naming, config format reaching the runner, database URL resolution, state surviving an app rebuild on the same database, trained models (not job history) surviving a restart on the same database, keepalive passed only when set, the CLI wiring every option through to the builder and run_app, an unknown --config-format rejected.
  • export: a generated MLproject served by build_mlproject_app gives the same info and config defaults as the module; --check passes fresh, fails stale with a diff, fails on semantic drift (missing author_note) and without a file; --force, --output, MLproject.yaml, --config, a changed prediction_periods default, hierarchy/id fallback, function runner, import failure and missing module; constraints on a reserved field kept and enforced, a description-only change still omitted, --check catching an option that accepts different values, and commands with literal braces producing the same final command after both format passes.
  • Manual: chapkit mlproject run proj --config-format flat --database state/chapkit.db, trained, restarted, artifact still listed with hierarchy smoke_model, nothing written to proj/.
  • Manual: chapkit mlproject export and --check against chapkit_ghr_model's main.py; the export validates with chap-core's own ModelTemplateConfigV2.
  • Manual: chapkit mlproject run --log-level warning prints no info lines.

Jira: CLIM-1435

…-level to mlproject run (CLIM-1435)

Name the artifact hierarchy after the MLproject instead of always mlproject,
matching the services mlproject migrate and init generate.
@codecov

codecov Bot commented Oct 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

….py (CLIM-1435)

The reverse of mlproject migrate: reads the service info, config class, shell
runner and artifact hierarchy from a service module and writes the MLproject
that mlproject run serves as the same service. --check fails when the file is
stale or would be read back as a different service.
@mortenoh mortenoh changed the title feat: add --config-format, --database, --keepalive-interval and --log-level to mlproject run (CLIM-1435) feat: mlproject run options and chapkit mlproject export (CLIM-1435) Oct 15, 2026
- export keeps a reserved config field whose constraints changed, even with
  the default unchanged, and --check compares what each option accepts
- export escapes literal braces for both format passes mlproject run applies
- --database keeps configs and trained models; job history stays in memory,
  so the help and docs no longer promise it

@mortenoh mortenoh left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried this against chapkit_ghr_model (chap-models/chapkit_ghr_model#10). No blocking bugs; the new options work as described.

Verified

  • chapkit mlproject export on chapkit_ghr_model's main.py writes an MLproject that passes --check. Against the committed MLproject the only differences are the header and the position of one description key, as the description says.
  • mlproject run proj --config-format flat --database state/c.db --log-level warning: the startup output shows the layout and storage path, and nothing is written to proj/.
  • --database persistence: a config created with n_samples: 100 is still there after restarting on the same file.
  • Without --database, storage stays in memory, the same as the builder default.

Issues

  1. --config-format cannot be reached from chap eval. chap-core's --run-config.as-chapkit starts chapkit mlproject run . with fixed arguments, and the -cli images start chapkit run . the same way, so anything run that way still gets the nested layout. For that path the layout has to be in the MLproject itself, for example a top-level config_format: flat that the CLI flag can override. Export could then write that key instead of the header comment ("run with --config-format flat"). Until then, scripts written for a flat main.py service have to accept both layouts.
  2. Export silently drops settings an MLproject cannot carry, and --check still passes. The one that changes behaviour is the runner validation hooks (on_validate_train / on_validate_predict): data the main.py service rejects is accepted by the MLproject service. Also dropped: with_auth API keys, custom health checks or extra routes, custom artifact level_labels, and the keepalive interval set in with_registration. Suggest at least a warning for each, and perhaps a --check failure for validation hooks.
  3. --log-level does not cover the first log lines. With --log-level warning, the three lines logged while the app is built (shell_runner_initialized, monitoring.enabled, app.mounted) still print at info, because servicekit only applies LOG_LEVEL once the app starts. Request logs do respect it.
  4. Export executes the module, side effects included. chapkit_ghr_model's main.py creates data/ and builds the whole app with monitoring. The PR describes how the module is loaded; the docs should say plainly that export runs it.
  5. A --database file inside the project stays out of train/predict workspaces only because of its extension. WORKSPACE_EXCLUDE_PATTERNS covers *.db / *.sqlite*, but a path such as state/chapkit with no extension is copied into every workspace. A warning or a note in the docs would cover it.

…ress review (CLIM-1435)

- mlproject run, migrate and export use meta_data.config_format, so the layout
  reaches chap eval and the -cli images; chap-core ignores unknown meta_data
  keys but rejects unknown top-level ones. --config-format still overrides it.
- export warns about settings an MLproject cannot carry: validation hooks,
  custom level labels, API key auth and extra routes
- --log-level applies before the service is built
- warn when a --database file inside the project would be copied into
  every workspace
- docs say plainly that export runs the module

@mortenoh mortenoh left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-tested 98d5253 against chapkit_ghr_model. All five points from the previous review are addressed.

Verified

  • meta_data.config_format: export writes config_format: flat for a flat runner; mlproject run with no flags reports the flat layout, and --config-format chap_core overrides it. chap-core's ModelTemplateConfigV2 loads the exported file and ignores the key, so older chap-core versions are unaffected.
  • Export warnings: chapkit_ghr_model's main.py has no validation hooks, auth, custom level labels or extra routes, and gets no warnings.
  • --log-level warning: no info lines at all now.
  • --database proj/state/chapkit warns about the workspace copy; proj/state/chapkit.db does not.
  • A fresh export of chapkit_ghr_model passes --check.
  • MLproject, export and migrate tests pass locally (222).

Remaining

  • Keepalive interval is still dropped silently. chapkit_ghr_model's main.py uses with_registration(keepalive_interval=15), and export does not warn that mlproject run falls back to the default. Minor, but it would fit with the other dropped_settings warnings (and --keepalive-interval exists to restore it).
  • chap eval --run-config.as-chapkit only benefits once chap-core depends on a chapkit release with this PR. It runs the chapkit installed alongside chap-core (currently 2.3.1, which ignores meta_data.config_format). Worth a chap-core dependency bump after release.

… (CLIM-1435)

ServiceBuilder.build records the registration options on app.state, so export
can compare the keepalive interval with servicekit's default.
@mortenoh
mortenoh merged commit b98e404 into main Oct 15, 2026
5 checks passed
@mortenoh
mortenoh deleted the feat/mlproject-run-options branch October 15, 2026 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant