# Add a Sensor to a Dagster Project

Yes. An agent adds a Dagster sensor that starts your ingestion job when a source file changes, with a cursor, a run key and a test, and opens a pull request.

[Run this on Rush](https://cloud.getrush.ai/run/add-sensors/dagster/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the project's definitions and its existing triggers before adding one. In a workspace like dagster-io/hooli-data-eng-pipelines the `hooli-data-ingest` project registers, in `src/hooli_data_ingest/definitions.py`, one asset group from `assets/sling.py`, one job `daily_sling_job` from `jobs/__init__.py` that selects `RAW_DATA/locations`, and one trigger: `daily_sling_assets` in `schedules/__init__.py`, a `ScheduleDefinition` on `0 0 * * *`. The Sling replication in `resources/__init__.py` reads `locations.csv` from the project directory into DuckDB locally and, on branch and production deployments chosen by `get_env()` from the `DAGSTER_CLOUD_*` variables, from `s3://hooli-demo/embedded-elt/locations.csv` into Snowflake. So a file that lands at noon waits until midnight, and a file that never changes is reloaded every night anyway.

The sibling project already shows the house style for the fix. `hooli-data-eng/src/hooli_data_eng/defs/files/sensors.py` defines `watch_s3_sensor` with `@dg.sensor(job=...)`, takes a `FileSystem` resource, reads the file's modification time, keeps the last one in `context.cursor`, yields a `SkipReason` when nothing changed and otherwise a `RunRequest` with a `run_key` built from the file name and timestamp. `sensor_file_managers.py` has the resource as a `ConfigurableResource` with a `LocalFileSystem` over `os.path.getmtime` and an `s3FileSystem` over boto3, and `resources.py` picks one per environment. The agent follows that shape rather than inventing a second one.

It adds `src/hooli_data_ingest/sensors/__init__.py` with `locations_file_sensor` targeting `daily_sling_job`, a `minimum_interval_seconds` sensible for a file feed, the cursor and `run_key` pattern from the sibling, and a `FileSystem` resource module for this project with the local path derived from the same `hooli_data_ingest_root` the replication config uses and the S3 key `embedded-elt/locations.csv` in the non-local branches. `definitions.py` gains `sensors=[locations_file_sensor]` and the resource under `resources`, beside `sling`. The schedule stays, as a nightly backstop, and the pull request says why. `boto3` is already pulled in through `dagster-cloud`, so `pyproject.toml` changes only if the agent needs a direct pin.

Then it proves the project still loads and the sensor behaves: `dagster definitions validate` from the project directory, which is what the deploy workflow's Dagster Cloud action does before building an image; a `pytest` test in `hooli_data_ingest_tests/test_sensors.py` that calls the sensor with `dg.build_sensor_context(cursor=...)` and a temporary directory holding `locations.csv`, asserting a `SkipReason` when the cursor matches the mtime and a `RunRequest` with the expected `run_key` when the file is newer; and `ruff check`, which `.github/workflows/deploy-dagster-cloud.yml` runs on every pull request.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add a sensor to the hooli-data-ingest project that starts daily_sling_job when locations.csv changes, following the cursor, run_key and FileSystem resource pattern in hooli-data-eng/src/hooli_data_eng/defs/files/sensors.py and sensor_file_managers.py. Resolve the local file from the same project root the replication config uses and the S3 key from the non-local config, selected by get_env(). Register the sensor and its resource in definitions.py and keep the existing schedule. Write a pytest test using build_sensor_context and a temporary directory that checks the skip case and the run request case, run it, run dagster definitions validate in the project directory, and run ruff check. Open a pull request quoting each result and explaining why the schedule stays.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

The pull request adds the sensor module and the resource module under `src/hooli_data_ingest/`, registers both in `definitions.py`, and adds `hooli_data_ingest_tests/test_sensors.py`. Its description quotes the `dagster definitions validate` output, the pytest run with the two sensor cases, and the ruff result, and explains the behaviour: the sensor fires once per new modification time of `locations.csv`, the `run_key` makes a repeated tick for the same file a no-op, and the nightly schedule remains as a backstop for a feed that stalls.

A reviewer reads the sensor once for the cursor logic and the environment switch, confirms the S3 key and bucket match the replication config so the sensor watches the same file Sling reads, and checks that `definitions.py` lists the resource under the key the sensor's parameter name expects. Opening the pull request also runs the repository's deploy workflow, which creates a branch deployment where the sensor can be turned on and watched. The transcript at GET /o/{org}/sessions/{id}/messages holds every command.

Nothing runs against your Dagster deployment from the sandbox. The sensor starts in its default stopped state unless the prompt asks for `DefaultSensorStatus.RUNNING`, so turning it on in production is a deliberate click or a one-line change.

## Run it on every new source file feed

Each new file feed that lands in a bucket or a shared path is the same change again: a sensor keyed on the file's modification time, a resource per environment, a test. Send POST /o/{org}/sessions with a prompt naming the file, the bucket key and the job it should start, and the agent adds a sensor in the same shape beside the first one. A `request_id` that is a UUID v5 of the feed name keeps a retried send from opening two pull requests.

A webhook URL on the API key delivers `run.completed` with the session URL and the pull request, so the data team sees the branch deployment link as soon as the run settles.

## Why run it on Rush?

Writing a sensor is a page of Python; proving it means installing a `uv` workspace with dagster, dagster-sling and dagster-cloud, running the definitions check that the deploy action runs, and exercising the sensor's two branches in a test. That is a slow install and a slower feedback loop on a laptop, and a fast one in a sandbox that keeps the output.

Following the sibling project's pattern rather than a new one keeps the workspace coherent, and a pull request that quotes the validate, test and lint results is one the data engineering team can merge and watch on the branch deployment rather than reason about.

## Frequently asked questions

### Why keep the nightly schedule?

A sensor only fires when the file changes, so a feed that stops updating would never be reloaded, and a load that failed would not be retried on its own. The schedule guarantees one run a day regardless, and the `run_key` keeps the two from doubling up on the same file version.

### What does the sensor need to reach S3?

The same credentials the Sling resource already reads from `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and `AWS_REGION` on branch and production deployments. Locally it reads the file's modification time from disk and needs nothing.

### Will it be running as soon as the pull request merges?

No. A Dagster sensor starts stopped unless its definition sets `DefaultSensorStatus.RUNNING`. The agent leaves the default so you turn it on from the UI after watching it on the branch deployment; ask in the prompt if you want it running by default.

### Could this be an asset sensor or declarative automation instead?

An asset sensor reacts to a Dagster materialization, not to a file in a bucket, and automation conditions schedule assets from other assets. A file that arrives from outside Dagster is exactly the case for a plain sensor with a cursor, which is also what the sibling project chose.

## More Dagster jobs

- [Add Slack Alerts on Failed Runs to a Dagster Project](https://getrush.ai/run/add-slack-failure-alerts/dagster/)
- [Review Pull Requests on a Dagster Project](https://getrush.ai/run/review-pull-requests/dagster/)
- [Upgrade Python Dependencies Weekly for a Dagster Project](https://getrush.ai/run/upgrade-python-dependencies/dagster/)

## More jobs like this: Hand off a long task

- [Add a ServiceMonitor to a Helm Chart](https://getrush.ai/run/add-service-monitors/helm/)
- [Add Shell Completion to a Click CLI](https://getrush.ai/run/add-shell-completion/click/)
- [Add Shell Completions to a Rust CLI](https://getrush.ai/run/add-shell-completion/rust-cli/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
