Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 70 additions & 5 deletions docs/api/rest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1982,6 +1982,7 @@ List all bulk-data items in a category for the entity.
"duration_sec": 6.0,
"format": "mcap",
"priority": 900,
"deletable": false,
"recording_id": "fault_MOTOR_OVERHEAT_1738664999000",
"storage_files": 1
}
Expand All @@ -2005,6 +2006,12 @@ manager runs without a valid retention configuration. A client that fetches the
most important recordings first sorts by ``priority``, then by
``creation_date``.

``x-medkit.deletable`` is ``true`` when every fault linked to the recording,
also the faults this entity's list leaves out, is CLEARED or HEALED at the time
of the listing: the status the :ref:`rosbag delete <rest-rosbag-delete>` needs.
It says nothing else: a delete can still answer ``503`` when the fault manager's
store or audit log cannot answer.

**Which entity lists a recording.** An entity lists a recording when one of its
fault sources is exactly a reporting source of one of the recording's faults. A
recording shared by faults of two apps is listed under both, and each list names
Expand Down Expand Up @@ -2181,8 +2188,8 @@ Upload Bulk Data
``POST /api/v1/{entity-path}/bulk-data/{category}``

Upload a new bulk-data file to the specified category. Files are sent as
``multipart/form-data``. The ``rosbags`` category is read-only and cannot be
used for uploads.
``multipart/form-data``. The ``rosbags`` category does not accept uploads: its
recordings are made by the fault manager.

**Supported entity types:** components, apps only. Areas and functions return 405.

Expand Down Expand Up @@ -2246,8 +2253,8 @@ Delete Bulk Data

``DELETE /api/v1/{entity-path}/bulk-data/{category}/{id}``

Delete a specific bulk-data item. The ``rosbags`` category is managed by the
fault manager and cannot be deleted via this endpoint.
Delete a specific bulk-data item. In the ``rosbags`` category it deletes one
recording, see :ref:`rest-rosbag-delete`.

**Supported entity types:** components, apps only. Areas and functions return 405.

Expand All @@ -2260,9 +2267,67 @@ fault manager and cannot be deleted via this endpoint.
**Response Codes:**

- **204 No Content**: Item deleted successfully
- **400 Bad Request**: ``rosbags`` category (managed by fault manager)
- **404 Not Found**: Entity, category, or bulk-data ID not found
- **405 Method Not Allowed**: Delete attempted on areas or functions
- **409 Conflict**: Another client holds the entity's ``bulk-data`` lock
(``lock-broken``), or a fault linked to a rosbag recording is neither CLEARED
nor HEALED
- **503 Service Unavailable**: The fault manager did not answer a rosbag delete

.. _rest-rosbag-delete:

Delete a Rosbag Recording
~~~~~~~~~~~~~~~~~~~~~~~~~

``DELETE /api/v1/{apps|components}/{id}/bulk-data/rosbags/{recording_id}``

Deletes one recording: every storage file of it (a split recording goes whole),
its directory, and every row that links a fault to it, also the faults of other
entities. A client that keeps copies downloads them first, then deletes the
recording once its faults are CLEARED or HEALED. The delete does not depend on
the retention configuration. It needs the OPERATOR role, and a lock another
client holds on the entity's ``bulk-data`` refuses it with ``409``
``lock-broken``.

.. code-block:: bash

curl -X DELETE http://localhost:8080/api/v1/apps/motor_controller/bulk-data/rosbags/fault_MOTOR_OVERHEAT_1738664999000

- **204 No Content**: every fault linked to the recording is CLEARED or HEALED.
The rows are gone, so the recording is no longer listed or downloadable, and
the fault manager removes the directory right after, on its retention worker,
as for every other removal. A directory that cannot be removed is logged by
the fault manager.
- **404 Not Found**: the id is not a recording, or the recording is outside
the entity's scope (the rule of the download route), or it was deleted
already. Only an id the fault manager holds no recording for, or a recording
it does not find on an unavailable storage path (below), carries
``vendor_code`` ``x-medkit-recording-not-found``. A fault code is not a
recording id here. While the fault manager's storage path is unavailable (see
``ROSBAG_STORAGE_UNAVAILABLE`` in :doc:`/config/fault-manager`), a recording
whose directory it does not find is not listed and not downloadable, and its
delete answers ``404`` with ``x-medkit-recording-not-found``, although its rows
are kept. Nothing is deleted. Once a listing or a download finds a recorded
directory again, the delete answers as described here.
- **409 Conflict** with ``error_code`` ``precondition-not-fulfilled``: a linked
fault is neither CLEARED nor HEALED: PREFAILED, PREPASSED, CONFIRMED or any
other stored status. ``parameters.fault_codes`` names those faults. Nothing is
deleted. Clear them, or wait until they heal.
- **503 Service Unavailable**: the fault manager, its store or its audit log
could not answer, or the outcome of removing the rows is unknown (a
PostgreSQL connection lost at the commit). The directory then stays. A
``503`` after the gateway's call to the fault manager timed out
(``fault_manager.service_timeout_sec``) leaves the outcome unknown: the fault
manager can still finish the delete. List the recordings again to see whether
the recording is gone.

With the audit log on, the fault manager writes one ``rosbag_deleted`` event per
linked fault before it removes the rows, in every audit mode. The event records
the decision: when the removal then fails (``503``), the event stays and so does
the recording (after a commit of unknown outcome, at least its directory), and a
later delete writes a new event. With
``audit_log.fail_closed: true`` an event that cannot be written stops the
delete: ``503``, nothing deleted.

.. _rest-rosbag-recording-files:

Expand Down
8 changes: 6 additions & 2 deletions docs/config/fault-manager.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,9 @@ by default: with it off there is no table, no file and no write cost.
* - ``audit_log.transitions``
- ``"all"``
- Which transitions are recorded: ``all`` (occurred, confirmed, healed, cleared) or
``confirmed_only``. Any other value falls back to ``all`` with a warning.
``confirmed_only``. Any other value falls back to ``all`` with a warning. Both modes
also record ``rosbag_deleted``: one event per linked fault when a client deletes a
recording (see :ref:`rest-rosbag-delete`), with the fault's status at the delete.
* - ``audit_log.retention_max_records``
- ``0``
- Seal and prune the oldest segment once the log passes this many records.
Expand All @@ -1305,7 +1307,9 @@ by default: with it off there is no table, no file and no write cost.
actions that follow it (``on_healed``, ``on_cleared``; for the symptom also
the root cause's ``on_cleared``): the stored recordings stay until a space
limit or a later status change removes them, and a recording stored after
the failed append follows the fault's status like any store.
the failed append follows the fault's status like any store. A client
delete of a recording whose ``rosbag_deleted`` events cannot be appended
is refused (``503``) and deletes nothing.
* - ``audit_log.database_path``
- ``""``
- Where the audit database lives. Empty puts it beside the fault database,
Expand Down
5 changes: 3 additions & 2 deletions docs/config/server.rst
Original file line number Diff line number Diff line change
Expand Up @@ -845,8 +845,9 @@ Example:
.. note::

The ``rosbags`` category is available through the Fault Manager and cannot be
used for uploads or deletes. It is included in the category list whenever the Fault
Manager serves rosbags, which is always the case on the ROS 2 node.
used for uploads. A delete removes one recording once its faults are cleared or
healed (see :ref:`rest-rosbag-delete`). It is included in the category list whenever
the Fault Manager serves rosbags, which is always the case on the ROS 2 node.

.. note::

Expand Down
10 changes: 7 additions & 3 deletions docs/requirements/specs/bulkdata.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ BulkData

Returns BulkDataDescriptor array with id, name, mimetype, size, creation_date fields.
For ``rosbags``, includes ``x-medkit`` extensions with fault_codes, duration_sec, format,
recording_id and priority, the recording's retention priority.
recording_id, priority, the recording's retention priority, and deletable, true when every
fault linked to the recording is CLEARED or HEALED.

.. req:: GET /{entity}/bulk-data/{category}/{bulk-data-id}
:id: REQ_INTEROP_073
Expand All @@ -43,7 +44,7 @@ BulkData

Accepts multipart/form-data with a required ``file`` field and optional ``description``
and ``metadata`` fields. Returns 201 Created with a Location header pointing to the new
resource. Supported for components and apps. The ``rosbags`` category is read-only.
resource. Supported for components and apps. The ``rosbags`` category does not accept uploads.

.. req:: DELETE /{entity}/bulk-data/{category}/{bulk-data-id}
:id: REQ_INTEROP_075
Expand All @@ -54,4 +55,7 @@ BulkData
The endpoint shall delete the addressed bulk-data item from the entity, if permitted.

Returns 204 No Content on success. Supported for components and apps.
The ``rosbags`` category is managed by the fault manager and cannot be deleted.
In the ``rosbags`` category the item is one recording: every file of it, its directory and
every row linking a fault to it go once every linked fault is CLEARED or HEALED (204), 409
while a linked fault is active, 404 for an id that is not a recording in the entity's scope,
503 when the fault manager cannot answer.
35 changes: 32 additions & 3 deletions src/ros2_medkit_fault_manager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ ros2 service call /fault_manager/clear_fault ros2_medkit_msgs/srv/ClearFault \
| `~/list_faults` | `ros2_medkit_msgs/srv/ListFaults` | Query faults with filtering |
| `~/clear_fault` | `ros2_medkit_msgs/srv/ClearFault` | Clear/acknowledge a fault |
| `~/get_snapshots` | `ros2_medkit_msgs/srv/GetSnapshots` | Get topic snapshots for a fault |
| `~/list_rosbags` | `ros2_medkit_msgs/srv/ListRosbags` | Rosbag rows of the faults an entity reported (`entity_fqn`), or every row with `all_recordings`; each row with its recording's retention priority (`priorities`) |
| `~/list_rosbags` | `ros2_medkit_msgs/srv/ListRosbags` | Rosbag rows of the faults an entity reported (`entity_fqn`), or every row with `all_recordings`; each row with its recording's retention priority (`priorities`) and whether every fault linked to the recording is CLEARED or HEALED (`deletable`) |
| `~/delete_rosbag` | `ros2_medkit_msgs/srv/DeleteRosbag` | Delete one recording once every linked fault is CLEARED or HEALED; see [Deleting a recording](#deleting-a-recording) |

## Features

Expand Down Expand Up @@ -288,9 +289,37 @@ API (`FaultStorage::get_near_misses`). A database written by an earlier build ga
first open. **There is no service or REST surface for it yet** - reading it means going through the
storage API or the database file.

## Deleting a recording

`~/delete_rosbag` deletes one recording: every row that links a fault to it and, once those are
gone, its directory with every storage file. The gateway calls it for
`DELETE /api/v1/{apps|components}/{id}/bulk-data/rosbags/{recording_id}`. The request names the
recording and `scope_fault_codes`, the linked codes that put the recording in the caller's scope; the
answer is `not_found` when none of them is still linked, so a clear or a sweep that removed the only
link the caller could see does not let it delete the recording. The other answers:

- success: every linked fault was CLEARED or HEALED. The rows are gone, and the directory is queued to
the retention worker, logged `Recording removed: <recording_id> (DELETE)` once removed. The call does
not wait for it: it runs on the node's only executor.
- `fault_active`: a linked fault is neither CLEARED nor HEALED: PREFAILED, PREPASSED, CONFIRMED or any
other stored status. `active_fault_codes` names them; nothing is deleted.
- `storage_unavailable`: the store could not be read, a linked fault is not stored, the recording is on
a storage path the startup check found unavailable, the audit append failed with
`audit_log.fail_closed`, or the removal failed. With a PostgreSQL commit of unknown outcome the rows
may be gone; the directory stays, as on every other removal path.

Over HTTP a recording on an unavailable storage path answers 404, not 503: `~/list_rosbags` leaves it
out and `~/get_rosbag` declines it, so the gateway does not find it and does not call
`~/delete_rosbag`. Nothing is deleted either way.

With the audit log on, the delete appends one `rosbag_deleted` event per linked fault, in one
transaction and in every audit mode, before it removes the rows: the event records the decision. When
the removal then fails, the event stays, and a later delete writes a new one. The delete does not
depend on the retention configuration.

## Advanced: Tamper-Evident Audit Log

An optional append-only, hash-chained audit log records every fault state transition (`occurred`, `confirmed`, `healed`, `cleared`) so the fault history is independently verifiable. Auto-recovery (a fault reaching the healing threshold via PASSED events) is recorded as a distinct `healed` row with source `auto_heal`, so the fault's END is in the timeline and is not confused with a manual `cleared`. The manager has no acknowledge action separate from clearing, so `~/clear_fault` is recorded as `cleared` (clear == ack); there is no `ack` kind. The log also records its own lifecycle with `logging_activated` / `logging_deactivated` markers at start and stop. It is **off by default** because it adds a write and storage cost per transition.
An optional append-only, hash-chained audit log records every fault state transition (`occurred`, `confirmed`, `healed`, `cleared`) so the fault history is independently verifiable. Auto-recovery (a fault reaching the healing threshold via PASSED events) is recorded as a distinct `healed` row with source `auto_heal`, so the fault's END is in the timeline and is not confused with a manual `cleared`. The manager has no acknowledge action separate from clearing, so `~/clear_fault` is recorded as `cleared` (clear == ack); there is no `ack` kind. The log also records its own lifecycle with `logging_activated` / `logging_deactivated` markers at start and stop, and a client delete of a recording as one `rosbag_deleted` row per linked fault, with the fault's status at the delete, source `delete_rosbag` and description `rosbag <recording_id> deleted`. It is **off by default** because it adds a write and storage cost per transition.

Each transition appends one immutable row holding `record_hash = sha256(prev_hash + canonical(event))` (OpenSSL EVP SHA-256), the `prev_hash` it links to, and a monotonic `seq`. The hash is computed once at insert and never recomputed. A persisted chain head lets the chain resume across restarts. The log is stored in its own SQLite database (separate from the fault store) and is treated as append-only: the manager only ever inserts rows, and `BEFORE UPDATE` / `BEFORE DELETE` triggers reject out-of-band edits (the guarded rotation prune excepted).

Expand All @@ -305,7 +334,7 @@ Each transition appends one immutable row holding `record_hash = sha256(prev_has
| Parameter | Type | Default | Description |
| --------------------------------- | ------ | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `audit_log.enabled` | bool | `false` | Enable the tamper-evident audit log |
| `audit_log.transitions` | string | `"all"` | Which transitions to record: `"all"` (occurred/confirmed/healed/cleared) or `"confirmed_only"`. Lifecycle markers are always recorded. |
| `audit_log.transitions` | string | `"all"` | Which transitions to record: `"all"` (occurred/confirmed/healed/cleared) or `"confirmed_only"`. Lifecycle markers and `rosbag_deleted` are always recorded. |
| `audit_log.database_path` | string | `""` | SQLite path. Empty => sibling `fault_audit.db` next to the fault DB (or `:memory:` for in-memory fault stores) |
| `audit_log.retention_max_records` | int | `0` | Seal + prune the oldest segment beyond this many retained records (0 = unlimited) |
| `audit_log.fail_closed` | bool | `false` | When `true`, an audit append failure is re-raised as a fail-FAST error signalling the audit chain is broken and needs operator action. It does **not** roll back the already-committed fault-state change (the fault store is a separate DB - no cross-DB atomicity). When `false`, the failure is logged and counted and fault processing continues. Either way the gap is recorded via the in-process dropped-writes / audit-healthy signals (not operator-observable at runtime yet). |
Expand Down
Loading
Loading