alpaca-broker-reconciliation-idempotency-sh-srmi26

专用于 Alpaca 券商对账与幂等操作的 Skill,帮助 AI Agent 高效完成相关对账、幂等性处理与任务执行流程。

安装方式

先用 Prompt 核对来源与依赖。也可下载安装包。

用 AI 助手安装

复制提示词,让助手先核对固定版本和依赖。

查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "alpaca-broker-reconciliation-idempotency-sh-srmi26":https://skillpi.com/zh/skills/deepskill-b7e79c6a0bdc5dc251c52338173cd7a68651f191afb4e40b47d44a96e0d37d6e。安装前核对上游来源和版本:https://skillhub.cn/skills/user_3c6cb52e/alpaca-broker-reconciliation-idempotency-sh-srmi26。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。

来源与配置

skillhub.cn

配置方式待核实

请查看原始来源中的适用工具、依赖和权限要求。

安装实测待补充

专用于 Alpaca 券商对账与幂等操作的 Skill,帮助 AI Agent 高效完成相关对账、幂等性处理与任务执行流程。

0 下载

介绍来源: third_party · user_3c6cb52e · 1.0.0 参考来源

SkillPi 解读

能完成的任务
翻译待完成,请参阅来源文档。
适合谁
来源文档未说明。
输入和输出
来源文档未说明。
软件、账号及付费要求
来源文档未说明。
限制与区别
来源文档未说明。
实测状态
未测试

文件浏览

在原站浏览文件 ↗

原始 SKILL.md

打开原文 ↗
Frontmatter
name: Alpaca券商对账与幂等
slug: alpaca-broker-reconciliation-idempotency
version: 1.0.0
displayName: Alpaca券商对账与幂等
description: >
  Alpaca券商对账与幂等专用技能,帮助AI Agent高效完成相关任务。
summary: "Alpaca券商对账与幂等专用技能,帮助AI Agent高效完成相关任务。"
license: MIT
category: 量化交易
framework:
  - Claude Code
  - Codex
  - Hermes Agent
  - OpenClaw
  - QClaw
  - WorkBuddy
platform: multi-platform
homepage: "https://github.com/1991513ccie-png"
repository: "https://github.com/1991513ccie-png"

Alpaca — Reconciliation & Idempotency

This is the skill that separates a demo from production. Alpaca is an asynchronous, eventually-consistent system: writes settle later, events can be missed or replayed, some rails emit no events at all, and "executed" can still be reversed. Your job is to make your local database a faithful, self-healing mirror of Alpaca's state.

Read alpaca-broker-integration, alpaca-broker-sse-events, and the relevant domain skills first. This skill is the architecture that ties them together.

The core principle

Treat Alpaca as the source of truth and your DB as a cache that must converge to it. Every write is a request, not a fact. Every event is a hint, not a guarantee. Correctness comes from idempotent processing plus a reconciliation loop — never from assuming any single call or event succeeded exactly once.

1. Three layers of defense

Layer 1 — Idempotent writes      : never create a duplicate when you retry
Layer 2 — Idempotent event intake: never double-process a replayed/duplicate event
Layer 3 — Reconciliation sweep   : re-pull authoritative state and fix any drift

You need all three. Layer 1+2 keep you correct in the happy/retry case; Layer 3 catches everything that still slips through (downtime, bugs, missing events, corrections).

2. Layer 1 — Idempotent writes

Every money/order write must be safe to retry, because you can't tell a timeout apart from a success.

  • Orders: set your own client_order_id (≤128 chars) derived from your transaction ID. On a lost response, look the order up via orders:by_client_order_id before retrying.
  • Journals: send an Idempotency-Key header. Same key + same body returns the original journal; same key + different body → 422. (See alpaca-broker-journals.)
  • Local-first ordering: write your intent row (with a generated key) before the network call, so a crash mid-call leaves a record you can reconcile — never an orphaned Alpaca object you can't find.
  • Persist the returned Alpaca ID immediately (account_id, order_id, journal_id, transfer_id). It is your only correlation key for events and reconciliation.

3. Layer 2 — Idempotent event intake

Events are at-least-once: replay cursors, reconnects, and corrections all cause the same event to arrive more than once.

  • Snapshot-first, keyed on event_id. Insert the raw event with upsert / skip-on-duplicate on event_id (a ULID). A duplicate becomes a no-op. This single unique constraint is your dedup boundary.
  • Key business records on the Alpaca ID, not on a local autoincrement, with upsert semantics.
  • Guard transitions by current status. Before acting, check the record isn't already terminal — so a duplicate "executed"/"filled" doesn't re-fire a payout or notification.
  • Lock the row while mutating (SELECT … FOR UPDATE or your engine's equivalent) so concurrent events for one record serialize.
  • Advance the cursor only after success (at-least-once, made safe by the above).

4. Layer 3 — Reconciliation / heal jobs

A scheduled job that re-pulls authoritative state from Alpaca and upserts it locally. This is what makes the system self-healing.

Canonical nightly heal (lesson):

  1. For each active account, page through trade activities and non-trade activities (GET /v1/accounts/activities, paginated) for the last N days (e.g. 3) — a moving window that re-covers recent days so anything missed by SSE gets backfilled.
  2. Page through journals (GET /v1/journals) and transfers for the same window.
  3. Upsert each into your tables keyed on the Alpaca ID (insert-or-update). Re-running is safe and converges.
  4. Bound concurrency (a small worker pool) and respect rate limits (alpaca-broker-rate-limits-resilience).

Why a window and not just "since last run": it absorbs corrections, late settlements, and any events dropped during a deploy — without rescanning all history every night.

5. Polling the rails that have no events

Not everything emits SSE. Where there's no event, you must poll.

  • Funding-wallet per-transfer status (v1beta) is not pushed — poll GET /v1beta/.../funding_wallet/transfers/{id} on a schedule.
  • Stagger pollers (e.g. one rail at :00, another at :30) to spread API load.
  • Only poll records in a non-terminal state. Filter your query to status IN (pending, processing, …); once a record reaches a terminal status, drop it from the polling set. This bounds the work and prevents re-notifying.
  • Gotcha — no GET-by-id on classic wire transfers: you must GET /v1/accounts/{id}/transfers?direction=OUTGOING (a list) and match the ID client-side; cache the list per account within a run.

Lesson: polling implies latency. Document the expected lag (e.g. "withdrawal status updates within ~1h") so product/support set the right expectations.

6. Status state-machine mapping

Alpaca exposes several status enums (account, order, journal, transfer, funding-wallet) — each with its own vocabulary. Don't scatter raw Alpaca strings through your app.

  • Define one explicit mapping table per domain from Alpaca status → your internal status (e.g. executed/COMPLETE → COMPLETED; rejected/canceled/returned/failed → CANCELLED).
  • Handle unknown statuses gracefully — log and skip, never crash. Alpaca adds values (and has shipped bad ones — e.g. a stray TRD activity type that consumers had to filter out).
  • Know which states are terminal (they differ per enum) so you stop polling/processing them.

7. Eventual-consistency hazards to design for

  • executed/COMPLETE is not always final — journals can be reversed by cashiering; transfers can be RETURNED after appearing done. Keep reconciling past the "happy" terminal state for a window.
  • Corrections create new IDs. A journal correct cancels the original and issues a new journal ID carrying the real funds. Reconciliation keyed on event snapshots + Alpaca IDs handles this; logic that mutates the original record in place does not.
  • 200 means accepted, not settled. Never confirm money moved to a user off the create response — confirm off the terminal event/poll.
  • Out-of-order & duplicate events are normal (see alpaca-broker-sse-events §5). Idempotency absorbs them.

8. Anti-patterns (seen in the wild)

  • ❌ Reconnecting an SSE stream without a since_id cursor → silently drops every event during the gap. (Fix: persist + replay the cursor.)
  • ❌ Treating an SSE stream as your only source → no backstop for missed events. (Fix: add the heal job.)
  • ❌ Blind retry of a journal/order without an idempotency key → double money movement.
  • ❌ Acting on executed as irreversible → broken books when a reversal/correction lands.
  • ❌ Dedup keyed on a confirmed set instead of a seen set → judge/reject churn re-processes forever. Dedup on everything seen, key on the Alpaca/event ID.

9. Putting it together

WRITE:   local intent row (idempotency key) → Alpaca call → store Alpaca ID
LIVE:    SSE consumer (cursor-replay, snapshot-keyed dedup, status-guarded upsert)
POLL:    schedulers for rails with no events (non-terminal records only)
HEAL:    nightly window re-pull of activities/journals/transfers → upsert
MAP:     Alpaca status → internal status, terminal-aware, unknown-tolerant

Related skills: event consumption mechanics → alpaca-broker-sse-events; idempotency keys per domain → alpaca-broker-journals, alpaca-broker-trading-orders; polling rails → alpaca-broker-funding-transfers; backoff & rate limits in heal jobs → alpaca-broker-rate-limits-resilience.

当前收录版本

1.0.0

核对来源版本 ↗

安全与评测信息

质量评测

满分100 · 来自 SkillPi LAB 实验室的评测报告。

场景价值92/100

亮点

  • 针对 Alpaca 券商异步系统的对账与幂等处理,解决金融交易中的核心痛点
  • 提供生产级三层防御架构,具备高实用价值
触发精准95/100

亮点

  • 触发词精准完整,明确指向 Alpaca 对账与幂等场景
  • 关联相关技能,边界清晰,不易误触
结构规范94/100

亮点

  • 文档结构清晰,涵盖核心原则、实现步骤与反模式
  • 配有代码示例与流程图,易于理解与执行
执行可靠93/100

亮点

  • 工具来源明确,参数设计合理
  • 覆盖主要异常场景,具备确认机制与自愈能力
证据可验 (前四项均值)94/100

取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。

正文静态检测

100 / 100 · skillpi-20261123-no-r3-v1

正文未命中当前规则。

分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。

权限与使用前检查

根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。

未从主文档识别到上述常见操作,不能据此判断无需权限或没有风险。

文档引用的域名或地址

这些地址可能用于文档链接或示例,不是已批准的联网清单。使用前核对实际目的地及发送的数据。

  • github.com

本站展示提示不会授予权限。涉及文件改动、密钥、外部发送或费用时,仍由你使用的工具按需确认。

安全评级与访问范围

safe · 静态证据核验,不代表运行安全保证。

来源文档未说明。

使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。