Skip to content

feat: add cdf dev function serve command#2667

Draft
trygveu wants to merge 8 commits intomainfrom
cogfun-cli-migrate
Draft

feat: add cdf dev function serve command#2667
trygveu wants to merge 8 commits intomainfrom
cogfun-cli-migrate

Conversation

@trygveu
Copy link
Copy Markdown
Contributor

@trygveu trygveu commented Mar 10, 2026

Summary

  • Adds cdf dev function serve command that starts a local uvicorn dev server for testing Function App handlers
  • Interactive function discovery — when no path argument is given, scans the project for Function App handlers and prompts the user to pick one
  • Landing page at / with function info, CDF project warning banner, tracing status, link to Swagger docs, and live log viewer with severity toggle (Warnings+ / All)
  • Safety controls — bans prod validation-type configs entirely; prompts user to acknowledge full read/write CDF access for dev/staging
  • Validation-type from config — reads validation-type from config.{env}.yaml (same as build/deploy), not from env vars
  • Classical function guard — rejects plain def handle(client, data) handlers; only Function Apps supported
  • Toolkit auth — monkey-patches cognite_function_apps to use the toolkit's EnvironmentVariables auth path, so all toolkit auth flows work
  • Log streaming via SSE at /api/logs with severity toggle, status JSON at /api/status
  • Tracing detection — detects TracingApp in the handler's middleware chain and displays tracing status on the landing page
  • Auto-opens browser on server start

Changes

  • pyproject.toml — new serve optional dependency group
  • commands/serve.pyServeFunctionCommand with:
    • _discover_function_dirs() / _prompt_function_selection() for interactive handler discovery
    • _load_validation_type() reading from BuildConfigYAML config files
    • _check_build_type() blocking prod, prompting for dev/staging
    • _validate_handler_is_function_app() rejecting classical functions
    • _detect_tracing() inspecting the handler's ASGI middleware chain
    • _patch_cognite_client_factory() for toolkit auth integration
    • Reload/no-reload server startup with graceful shutdown timeout
  • commands/_landing_page.py — Pure ASGI middleware: landing page HTML with warning banner, tracing row, log severity toggle, SSE log streaming, status API
  • apps/_dev_function_app.pyDevFunctionApp typer with optional path argument, --host, --port, --reload/--no-reload, --log-level
  • apps/_dev_app.py — wires DevFunctionApp under cdf dev function
  • tests/test_serve.py — 22 unit tests covering validation, auth patching, build type checks, and all middleware routes

Test plan

  • cdf dev function serve (no args) — discovers handlers, prompts selection
  • cdf dev function serve <path> — starts uvicorn, opens browser to landing page
  • Landing page shows function name, CDF project warning, tracing status, link to /docs, live log stream with toggle
  • /docs link opens Swagger UI
  • With --reload: editing handler.py triggers reload visible in logs
  • Prod config blocked with clear error message
  • Dev/staging config prompts for acknowledgement
  • Classical function handler rejected with clear error
  • Without cognite-function-apps[cli] installed, prints helpful install message
  • Unit tests pass (22/22)

🤖 Generated with Claude Code

Expose the cognite-function-apps local dev server through the toolkit
CLI so users don't need the standalone `fun` CLI.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 10, 2026

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
37581 31979 85% 80% 🟢

New Files

File Coverage Status
cognite_toolkit/_cdf_tk/commands/_landing_page.py 91% 🟢
cognite_toolkit/_cdf_tk/commands/serve.py 32% 🟢
TOTAL 62% 🟢

Modified Files

File Coverage Status
cognite_toolkit/_cdf_tk/commands/init.py 100% 🟢
TOTAL 100% 🟢

updated for commit: 8f04f83 by action🐍

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 10, 2026

Codecov Report

❌ Patch coverage is 49.22118% with 163 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.09%. Comparing base (e0415f3) to head (8f04f83).
⚠️ Report is 15 commits behind head on main.

Files with missing lines Patch % Lines
cognite_toolkit/_cdf_tk/commands/serve.py 32.31% 155 Missing ⚠️
cognite_toolkit/_cdf_tk/commands/_landing_page.py 91.20% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2667      +/-   ##
==========================================
+ Coverage   84.89%   85.09%   +0.19%     
==========================================
  Files         436      440       +4     
  Lines       37014    37581     +567     
==========================================
+ Hits        31423    31979     +556     
- Misses       5591     5602      +11     
Files with missing lines Coverage Δ
cognite_toolkit/_cdf_tk/commands/__init__.py 100.00% <100.00%> (ø)
cognite_toolkit/_cdf_tk/commands/_landing_page.py 91.20% <91.20%> (ø)
cognite_toolkit/_cdf_tk/commands/serve.py 32.31% <32.31%> (ø)

... and 62 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

trygveu and others added 7 commits March 10, 2026 15:32
- Add landing page at `/` with function info, CDF project warning banner, and
  live log viewer via SSE
- Add `/api/logs` (SSE) and `/api/status` (JSON) endpoints
- Use toolkit's EnvironmentVariables auth path instead of cognite_function_apps'
  own env var reader, so all toolkit auth flows work
- Show CDF project/cluster warning at CLI startup
- Auto-open browser on server start
- Add unit tests for validation, auth patching, and all middleware routes

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Default to "Warnings+" (WARNING/ERROR/CRITICAL only), with a toggle
button to switch to "All" (INFO and above). Client-side filtering
so all entries are still captured and available when switching.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Block `cdf dev function serve` when CDF_BUILD_TYPE=prod with clear error
- For all other configs: prompt user to acknowledge full read/write CDF
  access before starting (skipped in non-interactive/CI environments)
- Reject classical functions (def handle(client, data)) with a helpful
  error — only Function Apps using cognite-function-apps are supported
- Detect TracingApp in the handler's middleware chain and show tracing
  status + OTLP endpoint on the landing page
- 22 unit tests (up from 12)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Uvicorn's loggers (uvicorn, uvicorn.access, uvicorn.error) may have
propagate=False, preventing records from reaching the root logger.
Attach the _LogCollector handler directly to these loggers so the
landing page SSE log stream captures all entries reliably.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Read validation-type from config.{env}.yaml instead of CDF_BUILD_TYPE env var.
Discover Function App handlers in the project and prompt user to pick one
when no path argument is given.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Fix type annotations in _LogCollector (dict[str, str] -> dict[str, Any]),
change uvicorn parameter type from object to Any, and update type: ignore
comments to match actual error codes.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ections

The /api/logs SSE connection stays open indefinitely, causing uvicorn
to hang at "Waiting for connections to close" during reload.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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