Skip to content

feat(engine): 現金流 ingestion 地基 — engine 吃得到現金 + 存 state (refs #171)#174

Merged
atomchung merged 2 commits into
mainfrom
claude/fomokernel-cash-ingestion
Jul 12, 2026
Merged

feat(engine): 現金流 ingestion 地基 — engine 吃得到現金 + 存 state (refs #171)#174
atomchung merged 2 commits into
mainfrom
claude/fomokernel-cash-ingestion

Conversation

@atomchung

Copy link
Copy Markdown
Owner

這個 PR 做什麼

fomo-kernel 之前完全吃不到帳戶現金——cash_weight 算不出、入金部署判讀沒有(owner 痛點)。
這個 PR 是現金 ingestion 的地基層:讓 engine 認得現金、算對、存進對帳記憶,測試鎖定。
呈現層(上卡 + SKILL UX)另開 PR,見下。

Scope(地基,非呈現)

  • load_cash_flows:讀 CSV 每一筆現金增減(買賣 + 存提息費股利,靠 Amount 欄),去重骨架同 load()
  • cash_position
    • 錨點優先(對付 is_complete=False 的不完整 CSV:錨點餘額 + 其後現金流)
    • 無錨點 → csv_sumreliable=False(假設開戶現金 0,漏一筆 deposit 就偏,交給 honesty 揭露)
    • cash_weight = 現金 /(持倉市值 + 現金)recent_net_deposit = 本期外部淨流入(入金判讀)
  • 接進 main(讀 TR_CASH 現金餘額錨點 + 混幣按 fx 換聚合幣別)+ 存進 build_state 頂層 cash 欄位(對帳記憶)
  • 契約測試:STATE_KEYS +cash + state.cash 形狀/降級斷言

證據(sample_noisy_broker.csv 實跑)

情境 balance cash_weight reliable
有現金餘額錨點 $19,284 0.29 true
無錨點(只靠 CSV 加總) -$27,411 null(降級) false

無錨點的負現金(買入為主、入金未記全 = csv_sum 假設破裂)weight 曾算出 -1.41 垃圾值 →
已修成降級 None;有錨點的負現金 = 真融資(margin debit)weight 負有意義,照報。

測試

  • run_all 十套全綠
  • test_engine_units +7(讀全 Amount 列 + kind、跨檔去重、錨點 vs csv_sum、weight 分母降級、入金 prev_end 過濾)
  • test_tr_json_contractstate.cash 形狀 + 無錨點降級契約

還沒做(呈現層,另開 session/PR)

honesty_ledger 揭露無錨點不可信 + build_card_data 上卡 + render 顯示 +
SKILL Step 0 收現金餘額錨點的互動 UX(讓功能真正通電的關鍵)+ card-spec 入金判讀講法 +
全 13 個 mock persona 卡面核對。呈現層需視覺核對,交棒乾淨 session 保品質。

refs #171

🤖 Generated with Claude Code

test and others added 2 commits July 12, 2026 10:19
…帳戶級地基 (refs #171)

load() 只留 BUY/SELL 給行為分析;帳戶級現金餘額要每一筆現金增減(Amount 欄:
買=負、賣/存/息=正、費=負,已含手續費淨額)。新增兩個純函式:
- load_cash_flows:讀全 Amount≠0 列 + kind 分類(trade/deposit/withdrawal/dividend/
  interest/fee/other),去重骨架同 load()(跨檔重疊同一筆只算一次)
- cash_position:錨點優先(對付 is_complete=False 的不完整 CSV,錨點+其後現金流),
  無錨點 csv_sum 標 reliable=False(漏 deposit 就偏,交給 honesty_ledger 揭露);
  cash_weight=現金/(持倉市值+現金),分母≤0→None 降級;recent_net_deposit=本期外部
  淨流入(deposit−withdrawal)給「這筆新入金該不該部署」判讀

純函式先落地並測透,接線 build_state/build_card_data/honesty_ledger 留後續 commit。
test_engine_units.py +6(52 綠):讀全列+kind、跨檔去重、錨點 vs csv_sum、weight 分母
降級、入金 prev_end 過濾。run_all 十套全綠。

refs #171
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
main 讀 TR_CASH 現金餘額錨點(JSON {as_of,amount,currency})+ load_cash_flows + 算持倉
市值 → cash_position,結果進 build_state 頂層 cash 欄位(對帳記憶,非上卡)。混幣時現金流按
fx 換聚合幣別(USD)對齊持倉視圖。

順帶修 cash_position 負現金 weight:無錨點的負現金(買入為主、入金未記全 = csv_sum 假設
破裂)weight 曾算出 -1.41 垃圾值 → 改降級 None;有錨點的負現金 = 真融資(margin)weight
負有意義照報。探測 noisy_broker state 時抓到,單元測試原只覆蓋 denom≤0、沒覆蓋此 case。

契約同步:STATE_KEYS +cash;test_tr_json_contract 加 state.cash 形狀 + 無錨點降級斷言。
test_engine_units +1(unanchored 負現金 weight=None)。run_all 十套全綠。

呈現層(honesty_ledger 揭露 + build_card_data 上卡 + render + SKILL/card-spec 入金判讀)
留後續 commit——需跑全 mock persona 卡面核對,適合乾淨 session。

refs #171
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@atomchung atomchung merged commit d9d4ebc into main Jul 12, 2026
6 checks passed
atomchung pushed a commit that referenced this pull request Jul 12, 2026
#174 地基層讓 engine 吃得到現金(load_cash_flows/cash_position/state.cash),
但只進對帳記憶、還沒對用戶「可見可用」。這個 PR 接呈現層(#174 body 的「還沒做」清單):

- build_card_data 加 `cash` 欄(TR_JSON 上卡):Claude 讀 balance/weight/reliable/recent_net_deposit 寫進敘事
- build_honesty_ledger 加 `cash_reliability` key:只在「有可誤導的 weight 但不可信」(reliable=False + weight 非 None)觸發;
  weight=None(算不出、不上卡)不空吠。判定進 engine、文案留 Claude(照 #82 機制,Step 3 gate 無需新增 prose)
- render() Rich 文字卡總覽段:reliable 才報「帳戶現金 $X(佔帳戶 Y%)」+ 本期淨入金;無錨點盲算不上卡
- SKILL Step 0 收現金餘額錨點 UX(TR_CASH JSON,讓 cash_weight 通電)+ Step 1 card.cash 欄位說明
- card-spec 現金與入金判讀段:reliable 講帳戶權重 + 「這筆入金加深還是解集中度」;無錨點誠實邀請補餘額
- EVALS B17 + 契約測試(TR_JSON cash key / HL cash_reliability)+ 單元測試(三情境觸發矩陣)

驗證:run_all 十套全綠;全 13 mock persona 產卡核對 cash 欄位 + cash_reliability 觸發一致(零 INCONSISTENT);
錨點路徑 render 顯示「帳戶現金 $19,284(佔帳戶 37%)」;入金判讀端到端(TR_PREV_END 過濾本期存款)。

呈現層需視覺核對,交棒乾淨 session 完成(#174 交棒理由)。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
atomchung added a commit that referenced this pull request Jul 12, 2026
#174 地基層讓 engine 吃得到現金(load_cash_flows/cash_position/state.cash),
但只進對帳記憶、還沒對用戶「可見可用」。這個 PR 接呈現層(#174 body 的「還沒做」清單):

- build_card_data 加 `cash` 欄(TR_JSON 上卡):Claude 讀 balance/weight/reliable/recent_net_deposit 寫進敘事
- build_honesty_ledger 加 `cash_reliability` key:只在「有可誤導的 weight 但不可信」(reliable=False + weight 非 None)觸發;
  weight=None(算不出、不上卡)不空吠。判定進 engine、文案留 Claude(照 #82 機制,Step 3 gate 無需新增 prose)
- render() Rich 文字卡總覽段:reliable 才報「帳戶現金 $X(佔帳戶 Y%)」+ 本期淨入金;無錨點盲算不上卡
- SKILL Step 0 收現金餘額錨點 UX(TR_CASH JSON,讓 cash_weight 通電)+ Step 1 card.cash 欄位說明
- card-spec 現金與入金判讀段:reliable 講帳戶權重 + 「這筆入金加深還是解集中度」;無錨點誠實邀請補餘額
- EVALS B17 + 契約測試(TR_JSON cash key / HL cash_reliability)+ 單元測試(三情境觸發矩陣)

驗證:run_all 十套全綠;全 13 mock persona 產卡核對 cash 欄位 + cash_reliability 觸發一致(零 INCONSISTENT);
錨點路徑 render 顯示「帳戶現金 $19,284(佔帳戶 37%)」;入金判讀端到端(TR_PREV_END 過濾本期存款)。

呈現層需視覺核對,交棒乾淨 session 完成(#174 交棒理由)。

Co-authored-by: test <test@test.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
@atomchung atomchung deleted the claude/fomokernel-cash-ingestion branch July 12, 2026 06:05
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