Skip to content
Merged
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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ endif
$(TWEAK_NAME)_CFLAGS := -fobjc-arc -Wno-unused-function \
-D$(BUILD_COMMIT_DEFINE)=\"$(BUILD_COMMIT)\" \
-DBUILD_VERSION=\"$(PACKAGE_VERSION)\" \
-I$(TWEAK_SOURCES_DIR) \
-ISources/Chinlan
ifdef FINAL_RELEASE
$(TWEAK_NAME)_CFLAGS += -DFINAL_RELEASE=1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import "Csa_Convert.h"
#import "Csa/Convert.h"

// ===========================================================================
// Csa_Convert — implementation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#import <stdatomic.h>
#import <stdint.h>

#import "Csa_Convert.h"
#import "Csa/Convert.h"

// ===========================================================================
// Csa_Engine — CSA server-side state machine for the TCP transport.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import "Internal.h"
#import "Csa_Engine.h"
#import "Csa/Engine.h"

#import <mach/mach_time.h>
#import <stdatomic.h>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#import "Internal.h"
#import "Csa_Convert.h"
#import "Csa_Engine.h"
#import "Csa/Convert.h"
#import "Csa/Engine.h"

#import <math.h>
#import <mach/mach_time.h>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import "Internal.h"
#import "Settings_Persistence.h"
#import "Settings/Persistence.h"
#import <dlfcn.h>

// ===========================================================================
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import "Internal.h"
#import "Settings_Persistence.h"
#import "Settings/Persistence.h"

// ===========================================================================
// Hook_GameOrchestratorObserve — capture the live GameOrchestrator instance.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import "Internal.h"
#import "Settings_Persistence.h"
#import "Settings/Persistence.h"
#import <dlfcn.h>

// ===========================================================================
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import "Internal.h"
#import "Settings_Persistence.h"
#import "Settings/Persistence.h"

#import <mach/mach_time.h>
#import <UIKit/UIKit.h>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import "Internal.h"
#import "Settings_Persistence.h"
#import "Settings/Persistence.h"

// ===========================================================================
// Hook_MatchingFilterObserve — apply user-configured matching filters.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import "Internal.h"
#import "Settings_Persistence.h"
#import "Settings/Persistence.h"

// ===========================================================================
// Inject_Resign — bridge CSA's %TORYO / %KACHI submissions back to KIOU.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import "Settings_Persistence.h"
#import "Settings/Persistence.h"
#import "logging.h"

// ===========================================================================
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import "Internal.h"
#import "Settings_Persistence.h"
#import "Settings/Persistence.h"
#import <UIKit/UIKit.h>

// ===========================================================================
Expand Down
2 changes: 1 addition & 1 deletion Sources/KiouEngineBridge/Tweak.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import "Internal.h"
#import "Settings_Persistence.h"
#import "Settings/Persistence.h"
#import <mach-o/dyld.h>
#import <dlfcn.h>
#import <string.h>
Expand Down
4 changes: 2 additions & 2 deletions docs/csa_compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ surfaces. KEB writes only the fields it can faithfully fill.
## Initial position

KEB always writes the `BEGIN Position` block in CSA's standard form,
derived from KIOU's live SFEN via `Csa_Convert::CsaPositionFromSfen`.
derived from KIOU's live SFEN via `Csa/Convert::CsaPositionFromSfen`.

| Concept | KEB behaviour | Status |
|---|---|---|
Expand All @@ -81,7 +81,7 @@ arrives. KEB follows the same pattern from both directions.

| CSA concept | Wire | KEB behaviour | Status |
|---|---|---|---|
| Notify move | `<sign><from><to><PIECE>,T<n>` | Emitted from `Hook_GameStateStoreObserve::HookNotifyPieceMoved` for every KIOU move (both sides). `,T<n>` derived from the snapshot-delta on Online/CPUStream; omitted in modes without authoritative clocks. | ✅ |
| Notify move | `<sign><from><to><PIECE>,T<n>` | Emitted from `Hooks/GameStateStoreObserve::HookNotifyPieceMoved` for every KIOU move (both sides). `,T<n>` derived from the snapshot-delta on Online/CPUStream; omitted in modes without authoritative clocks. | ✅ |
| Engine submits move | `<sign><from><to><PIECE>` | Parsed via `MoveBitsFromCsaText`, translated to USI, fed into `inject_apply`. The `,T<n>` suffix is accepted but ignored (KIOU keeps its own clock). | ✅ |
| Engine resigns | `%TORYO` | Sends `#RESIGN` + `#LOSE`, calls `GameOrchestrator.RequestSurrender` for the local seat. The engine controls the local player, so `%TORYO` means the local seat surrenders. | ⚠️ surrenders the local seat |
| Engine nyugyoku win | `%KACHI` | Sends `#JISHOGI` + `#WIN`, advances to GAME_OVER. KIOU side is not signalled. | ⚠️ engine learns; KIOU stays |
Expand Down
18 changes: 9 additions & 9 deletions docs/csa_protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ server protocol v1.2.1: <http://www2.computer-shogi.org/protocol/tcp_ip_server_1

Source of truth pointers:

- `Sources/KiouEngineBridge/Server_CSA.m` — TCP transport.
- `Sources/KiouEngineBridge/Csa_Engine.m` — protocol state machine.
- `Sources/KiouEngineBridge/Csa_GameInfo.m` — `Game_Summary` / result builder.
- `Sources/KiouEngineBridge/Csa_Convert.m` — coordinate / piece / move /
- `Sources/KiouEngineBridge/Csa/Server.m` — TCP transport.
- `Sources/KiouEngineBridge/Csa/Engine.m` — protocol state machine.
- `Sources/KiouEngineBridge/Csa/GameInfo.m` — `Game_Summary` / result builder.
- `Sources/KiouEngineBridge/Csa/Convert.m` — coordinate / piece / move /
position conversion. Pinned regression tests in
`tests/test_csa_convert_expectations.py`.

Expand All @@ -56,7 +56,7 @@ Source of truth pointers:

## Protocol state machine

KEB's state machine sits in `Sources/KiouEngineBridge/Csa_Engine.m`:
KEB's state machine sits in `Sources/KiouEngineBridge/Csa/Engine.m`:

```
BOOT
Expand Down Expand Up @@ -102,7 +102,7 @@ directly.
| `START:<Game_ID>` | inbound `AGREE` after Game_Summary | |
| `REJECT:<Game_ID> by engine` | inbound `REJECT` | Session stays open; engine may issue another LOGIN. |
| `<sign><from><to><PIECE>,T<n>` | KIOU NotifyPieceMoved fires | Both colors are echoed (sign + black, − white). `T<n>` is computed from the snapshot delta and is omitted in modes without authoritative clocks. |
| `#RESIGN`, `#TIME_UP`, `#ILLEGAL_MOVE`, `#SENNICHITE`, `#OUTE_SENNICHITE`, `#JISHOGI`, `#MAX_MOVES`, `#CHUDAN` | match end | KEB picks the reason marker that best matches the inferred outcome (currently `#RESIGN` for win/lose and `#SENNICHITE` for draw — see `CsaBuildMatchResult` in Csa_GameInfo.m). |
| `#RESIGN`, `#TIME_UP`, `#ILLEGAL_MOVE`, `#SENNICHITE`, `#OUTE_SENNICHITE`, `#JISHOGI`, `#MAX_MOVES`, `#CHUDAN` | match end | KEB picks the reason marker that best matches the inferred outcome (currently `#RESIGN` for win/lose and `#SENNICHITE` for draw — see `CsaBuildMatchResult` in Csa/GameInfo.m). |
| `#WIN` / `#LOSE` / `#DRAW` / `#CENSORED` | match end | Outcome from the engine's seat perspective. |

### engine → KEB (inbound)
Expand All @@ -113,8 +113,8 @@ directly.
| `LOGOUT` | Reply with `LOGOUT:completed`, close socket, return to BOOT. |
| `AGREE [<Game_ID>]` | While in AGREE_WAIT: send `START:<Game_ID>` and advance to PLAYING. Otherwise log + drop. |
| `REJECT [<Game_ID>]` | Log; reply with `REJECT:<Game_ID> by engine`; return to LOGIN. KIOU side is not affected. |
| `<sign><from><to><PIECE>[,T<n>]` | Parse via `Csa_Convert::MoveBitsFromCsaText` → translate to USI → call `inject_apply`. The `,T<n>` suffix is logged but otherwise unused (KIOU runs its own clock). |
| `%TORYO` | Send `#RESIGN` + `#WIN`, advance to GAME_OVER, schedule `GameOrchestrator.RequestSurrender` on the main thread (see `Inject_Resign.m`). |
| `<sign><from><to><PIECE>[,T<n>]` | Parse via `Csa/Convert::MoveBitsFromCsaText` → translate to USI → call `inject_apply`. The `,T<n>` suffix is logged but otherwise unused (KIOU runs its own clock). |
| `%TORYO` | Send `#RESIGN` + `#WIN`, advance to GAME_OVER, schedule `GameOrchestrator.RequestSurrender` on the main thread (see `Inject/Resign.m`). |
| `%KACHI` | Send `#JISHOGI` + `#WIN`, advance to GAME_OVER. No corresponding KIOU declaration API has been surfaced yet — see Task 7 of the migration plan. |
| `%CHUDAN` | Send `#CHUDAN`, advance to GAME_OVER. KIOU is not notified. |
| bare `\n` (CSA liveness ping) | No-op; TCP keepalive handles dead-peer detection separately. |
Expand Down Expand Up @@ -191,7 +191,7 @@ CSA's `BEGIN Time` block carries only the fields KIOU actually exposes
`Time_Unit:1msec` either — KIOU works in seconds and that's what we ship.

The `,T<n>` field on move lines is computed from the difference between
consecutive authoritative snapshots (`Hook_OnlineObserve::g_latestBlackTimeSec` /
consecutive authoritative snapshots (`Hooks/OnlineObserve::g_latestBlackTimeSec` /
`g_latestWhiteTimeSec`). VsAI and LocalPvP modes do not surface
authoritative clocks, so the suffix is omitted in those modes.

Expand Down
6 changes: 3 additions & 3 deletions docs/research/cave_kinds.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ logging — stays on `CAVE_OBSERVER`.
Both caves are 21 instructions = 84 bytes, allocated contiguously from
`KIOU_BR_CAVE_REGION_START` in `_BRIDGE_SITES` order. The last two
instructions are identical (`displaced_insn` + `B orig+4`) so the
cave-bypass entry at `cave_va + 0x4C` works for both kinds — `Inject_Move`
cave-bypass entry at `cave_va + 0x4C` works for both kinds — `Inject/Move`
and the entry hooks use that to run orig without re-entering the cave.

### `CAVE_OBSERVER` (observer.cave)
Expand Down Expand Up @@ -198,7 +198,7 @@ void HookNewSiteObserve(SomeArg1, ...); // for observer

### 5. Hook implementation

`Hook_<feature>.m`. For an entry hook, call orig via the bypass entry:
`Hooks/<feature>.m`. For an entry hook, call orig via the bypass entry:

```c
#if KIOU_CHINLAN
Expand All @@ -223,7 +223,7 @@ void HookNewSiteObserve(void *self) {
}
```

### 6. `Sources/KiouEngineBridge/ChinlanDispatcher.m`
### 6. `Sources/KiouEngineBridge/Chinlan/Dispatcher.m`

Observer hooks: add a `case` to `dispatch_one`.

Expand Down
6 changes: 3 additions & 3 deletions tests/test_csa_convert_expectations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Pinned expectations for the CSA conversion library (Csa_Convert.{h,m}).
"""Pinned expectations for the CSA conversion library (Csa/Convert.{h,m}).

The C/ObjC implementation lives in ``Sources/KiouEngineBridge/Csa_Convert.m``
The C/ObjC implementation lives in ``Sources/KiouEngineBridge/Csa/Convert.m``
and is exercised on-device against KIOU. We can't link Foundation against a
Linux CI runner, so this test module ports the algorithms to pure Python and
pins the expected outputs against the same inputs the ObjC code receives.
Expand All @@ -24,7 +24,7 @@


# ---------------------------------------------------------------------------
# Python reference implementation. Mirrors Csa_Convert.m verbatim.
# Python reference implementation. Mirrors Csa/Convert.m verbatim.
# ---------------------------------------------------------------------------

CSA_PIECE_NAMES = [
Expand Down