# Append Audit Log

> 向安全审计轨迹追加一条不可变记录。

Source: https://docs.sommatic.ai/zh/nodes/audit.append/

- 标识: `audit.append`
- 类别: 运维
- 版本: 1.0.0
- 状态: 部分可用. 写入一行结构化的 `[AUDIT]` 日志。它不会签名、不会构建哈希链，也不会持久化该记录。它在默认的 `out` 端口返回 `{ success: true }`，因此从 `success` 端口连出的边永远不会触发；缺少 `action`、`actor` 或 `resource` 时会直接抛出异常，而不是走 `error` 端口。
- 执行方式: async

向安全审计轨迹追加一条不可变记录。

该算子通过记录关键决策、数据访问或修改来保障合规与可追溯性。记录会被签名，并存储在防篡改的审计日志中。

**字段：**
- **Actor：** 执行操作的主体（用户/服务）。
- **Action：** 发生了什么（例如“Approved Loan”）。
- **Resource：** 目标实体（例如“Loan #123”）。
- **Evidence：** 证明该操作的 JSON 载荷。

**适用场景：**
- 法规合规（GDPR、HIPAA）。
- 排查安全事件。
- 跟踪审批链。

## 配置

- `action` (string, 必填): 具体的操作标识符（例如“ITEM_DELETED”）。

配置中会显示通用执行设置，但该节点目前不执行重试（`retryOnFail`、`maxTries`）。所有节点均未实现 `continueOnFail`。

## 输出端口

- `success` (默认): 审计记录成功。
- `error`: 写入审计日志失败。

## 输入契约

- `actor` (string, 必填): 用户 ID 或服务名称
- `resource` (string, 必填): 目标资源 ID
- `decision` (string, 可选): 结果（例如 Permit/Deny）
- `evidence` (object, 可选): 支撑性上下文/数据

## 输出契约

- `success` (boolean, 可选)

## 在表达式中

后续节点可通过 `$("Append Audit Log").item.json.<路径>` 读取此节点输出中的内容，即工作流编辑器在 Previous Nodes 中提示的路径（输出契约）。

- `$("Append Audit Log").item.json.success` (boolean)


## 工作原理

向编排器日志写入一行 `[AUDIT] <action> by <actor> on <resource>`，并返回 `{ success: true }`。该记录不会签名、不会做哈希链，也不会存储到其他地方。

1. 将输入项与解析后的配置合并（配置优先）。
2. 当其中任一字段为空时，抛出 `Missing 'action'`、`Missing 'actor'` 或 `Missing 'resource'`。
3. 以 info 级别记录 `[AUDIT] <action> by <actor> on <resource>`。随附的 `audit` 对象（`decision`、`evidence`、`timestamp`）不会写入，因为运行时 logger 只输出消息。
4. 返回不带端口的 `{ success: true }`，因此引擎走 `out`。

## 在工作流中

### 节点

```json
{
  "node_id": "node-audit-approval",
  "label": "Audit approval",
  "operator_slug": "audit.append",
  "operator_version": "1.0.0",
  "config": {
    "action": "INVOICE_APPROVED",
    "actor": "{{ $json.approver_id }}",
    "resource": "invoice:{{ $json.invoice_id }}",
    "decision": "Permit"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "port": "out",
  "data": {
    "success": true
  }
}
```

## 输出端口 (代码中的行为)

### `success`

永远不会输出：worker 不返回端口，引擎因此走 `out`，而本节点并未声明该端口。从 `success` 画出的连线永远不会触发。

### `error`

永远不会输出：失败时会直接抛错，导致运行失败。

## 代码中的行为

- `actor`: 不在配置 schema 中（仅出现在输入契约中），但会从节点配置读取；节点上未设置时从输入项读取：`actor`、`resource`、`decision`、`evidence`。

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `Missing 'action'` | `error` | 节点和输入项中都没有设置 `action`。该错误会被抛出：运行失败。 |
| `Missing 'actor'` | `error` | 节点和输入项中都没有设置 `actor`。该错误会被抛出：运行失败。 |
| `Missing 'resource'` | `error` | 节点和输入项中都没有设置 `resource`。该错误会被抛出：运行失败。 |

## 说明与限制

- 部分实现：没有签名、没有哈希链、没有防篡改证据，也不写数据库。唯一的痕迹是编排器的一行日志，内容仅限操作、执行者和资源。
- 请把它作为分支的最后一个节点：连在 `success` 之后的节点永远不会运行。该节点在运行中的输出为 `{ success: true }`。
- 抛出的错误会使整个运行失败，信息为 `Node <node_id> (<slug>) failed: <message>`，`error` 上的连线不会触发。在 `flow.try` 作用域内，引擎会将其转到 catch 节点。
- `execution` 设置（`retryOnFail`、`maxTries`、`waitBetweenTries`、`continueOnFail`、`timeout_ms`）可以填写，但本 worker 和引擎都不会应用。


## 契约（JSON Schema）

### All (`*.definition.js`)

```json
{
  "slug": "audit.append",
  "name": "Append Audit Log",
  "category": "ops",
  "version": "1.0.0",
  "description": "Appends an immutable entry to the secure audit trail.\n\n  This operator ensures compliance and traceability by recording key decisions, data access, or modifications. Entries are signed and stored in a tamper-evident audit log.\n\n  **Fields:**\n  - **Actor:** Who performed the action (User/Service).\n  - **Action:** What happened (e.g., \"Approved Loan\").\n  - **Resource:** Target entity (e.g., \"Loan #123\").\n  - **Evidence:** JSON payload proving the action.\n\n  **Use Cases:**\n  - Regulatory compliance (GDPR, HIPAA).\n  - Debugging security incidents.\n  - Tracking approval chains.",
  "capabilities": [
    "audit",
    "compliance",
    "security",
    "log"
  ],
  "config_schema": {
    "type": "object",
    "required": [
      "action"
    ],
    "properties": {
      "action": {
        "type": "string",
        "title": "Action Name",
        "description": "The specific action identifier (e.g. \"ITEM_DELETED\")."
      },
      "execution": {
        "type": "object",
        "title": "Execution Settings",
        "properties": {
          "alwaysOutputData": {
            "type": "boolean",
            "title": "Always Output Data",
            "description": "Always output data, even on error (if configured).",
            "default": false
          },
          "retryOnFail": {
            "type": "boolean",
            "title": "Retry On Fail",
            "description": "Retry the execution if it fails.",
            "default": false
          },
          "maxTries": {
            "type": "integer",
            "title": "Max Retries",
            "description": "Number of retries.",
            "default": 3,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "waitBetweenTries": {
            "type": "integer",
            "title": "Wait Between Retries (ms)",
            "default": 1000,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "continueOnFail": {
            "type": "boolean",
            "title": "Continue On Fail",
            "description": "Do not crash workflow on error.",
            "default": false
          },
          "timeout_ms": {
            "type": "integer",
            "title": "Timeout (ms)",
            "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
            "default": 300000
          }
        }
      }
    }
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "actor": {
        "type": "string",
        "description": "User ID or Service Name"
      },
      "resource": {
        "type": "string",
        "description": "Target Resource ID"
      },
      "decision": {
        "type": "string",
        "description": "Outcome (e.g. Permit/Deny)"
      },
      "evidence": {
        "type": "object",
        "description": "Supporting context/data"
      }
    },
    "required": [
      "actor",
      "resource"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "description": "Confirmation of audit persistence.",
    "properties": {
      "success": {
        "type": "boolean"
      }
    }
  },
  "ports": [
    {
      "port_id": "success",
      "label": "Success",
      "is_default": true,
      "description": "Audit logged successfully."
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Failed to write audit log."
    }
  ],
  "execution_mode": "async",
  "tags": [
    "audit",
    "compliance",
    "security"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_vectry",
    "profile": {
      "name": "Vectry Analytics",
      "verified": true,
      "url": "https://me.veripass.com/vectry",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "required": [
    "action"
  ],
  "properties": {
    "action": {
      "type": "string",
      "title": "Action Name",
      "description": "The specific action identifier (e.g. \"ITEM_DELETED\")."
    },
    "execution": {
      "type": "object",
      "title": "Execution Settings",
      "properties": {
        "alwaysOutputData": {
          "type": "boolean",
          "title": "Always Output Data",
          "description": "Always output data, even on error (if configured).",
          "default": false
        },
        "retryOnFail": {
          "type": "boolean",
          "title": "Retry On Fail",
          "description": "Retry the execution if it fails.",
          "default": false
        },
        "maxTries": {
          "type": "integer",
          "title": "Max Retries",
          "description": "Number of retries.",
          "default": 3,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "waitBetweenTries": {
          "type": "integer",
          "title": "Wait Between Retries (ms)",
          "default": 1000,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "continueOnFail": {
          "type": "boolean",
          "title": "Continue On Fail",
          "description": "Do not crash workflow on error.",
          "default": false
        },
        "timeout_ms": {
          "type": "integer",
          "title": "Timeout (ms)",
          "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
          "default": 300000
        }
      }
    }
  }
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "actor": {
      "type": "string",
      "description": "User ID or Service Name"
    },
    "resource": {
      "type": "string",
      "description": "Target Resource ID"
    },
    "decision": {
      "type": "string",
      "description": "Outcome (e.g. Permit/Deny)"
    },
    "evidence": {
      "type": "object",
      "description": "Supporting context/data"
    }
  },
  "required": [
    "actor",
    "resource"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "description": "Confirmation of audit persistence.",
  "properties": {
    "success": {
      "type": "boolean"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "success",
    "label": "Success",
    "is_default": true,
    "description": "Audit logged successfully."
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Failed to write audit log."
  }
]
```
