Skip to content

Commit b199fe2

Browse files
committed
chore: cleanup AI instructions
1 parent 229ca94 commit b199fe2

File tree

3 files changed

+55
-104
lines changed

3 files changed

+55
-104
lines changed

.github/copilot-instructions.md

Lines changed: 20 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,25 @@ This project uses the following exact technology versions:
1616

1717
### Core Technologies
1818

19-
- **Node.js**: `^20.19.0 || >=22.12.0` (prefer 22.12.0+)
20-
- **TypeScript**: `^5.9.2`
19+
- **Node.js** (prefer v24)
20+
- **TypeScript**
2121
- Target: esnext
2222
- Module: NodeNext
2323
- Module Resolution: NodeNext
2424
- Strict mode enabled
25-
- **Package Manager**: npm (10.8.2+)
25+
- **Package Manager**: npm
26+
27+
Refer to [../package.json](../package.json) for versions.
2628

2729
### Development Dependencies
2830

29-
- **@biomejs/biome**: `^2.2.4` (linting and formatting)
30-
- **@types/node**: `^24.5.2`
31-
- **vitest**: `^3.0.8` (testing framework)
32-
- **@vitest/coverage-v8**: `^3.2.4` (coverage provider)
33-
- **serialport**: `^13.0.0` (dev dependency only)
31+
- **@biomejs/biome**: linting and formatting
32+
- **vitest**: testing framework
33+
- **@vitest/coverage-v8**: coverage provider
34+
- **serialport**: dev dependency only
35+
- **@types/node**: typing
36+
37+
Refer to [../package.json](../package.json) for versions.
3438

3539
### Critical Version Constraints
3640

@@ -42,58 +46,7 @@ This project uses the following exact technology versions:
4246

4347
### Layered Architecture
4448

45-
This project follows a strict layered architecture:
46-
47-
```
48-
┌─────────────────────────────────────────────────┐
49-
│ Drivers (src/drivers/) │
50-
│ - OTRCPDriver (main adapter) │
51-
│ - OTRCPParser (frame parsing) │
52-
│ - OTRCPWriter (frame writing) │
53-
│ - Descriptors (device descriptors) │
54-
└─────────────────────────────────────────────────┘
55-
56-
┌─────────────────────────────────────────────────┐
57-
│ Zigbee Stack Handlers (src/zigbee-stack/) │
58-
│ - MACHandler (MAC layer operations) │
59-
│ - NWKHandler (Network layer operations) │
60-
│ - NWKGPHandler (Green Power operations) │
61-
│ - APSHandler (Application Support layer) │
62-
│ - StackContext (shared state) │
63-
└─────────────────────────────────────────────────┘
64-
65-
┌─────────────────────────────────────────────────┐
66-
│ Zigbee Protocol Utilities (src/zigbee/) │
67-
│ - MAC (IEEE 802.15.4) │
68-
│ - NWK (Network Layer) │
69-
│ - NWK GP (Green Power) │
70-
│ - APS (Application Support) │
71-
│ - Core utilities (zigbee.ts) │
72-
└─────────────────────────────────────────────────┘
73-
74-
┌─────────────────────────────────────────────────┐
75-
│ Spinel Protocol (src/spinel/) │
76-
│ - Core protocol (spinel.ts) │
77-
│ - HDLC framing (hdlc.ts) │
78-
│ - Properties (properties.ts) │
79-
│ - Commands, Statuses │
80-
└─────────────────────────────────────────────────┘
81-
82-
┌─────────────────────────────────────────────────┐
83-
│ Utilities (src/utils/) │
84-
│ - Logger (logger.ts) │
85-
└─────────────────────────────────────────────────┘
86-
```
87-
88-
### Directory Structure Rules
89-
90-
- **src/drivers/**: RCP communication and main adapter logic
91-
- **src/zigbee-stack/**: Zigbee stack handlers (MAC, NWK, APS, GP, context)
92-
- **src/spinel/**: Spinel protocol implementation (OpenThread RCP)
93-
- **src/zigbee/**: Zigbee protocol utilities (frame encoding/decoding)
94-
- **src/utils/**: Shared utilities (minimal)
95-
- **src/dev/**: Development tools (excluded from production builds)
96-
- **test/**: Test files matching source structure
49+
This project follows a strict layered architecture. Refer to [../docs/architecture.md](../docs/architecture.md)
9750

9851
### Build Configurations
9952

@@ -766,6 +719,13 @@ const enum SaveConsts {
766719
}
767720
```
768721

722+
### Routing Feedback Loop (keep MAC/NWK changes in sync)
723+
724+
- `MACHandler.sendFrameDirect()` **must** clear `StackContext.macNoACKs` and call `NWKHandler.markRouteSuccess()` when a unicast ACK succeeds, and increment `macNoACKs` plus call `markRouteFailure()` when a NO_ACK error occurs.
725+
- `StackContext.sourceRouteTable` stores **multiple** `SourceRouteTableEntry` objects per destination; `NWKHandler.findBestSourceRoute()` re-sorts them on every lookup using: path cost + staleness penalty + failure penalty − recency bonus, and filters out relays whose `macNoACKs` entry exceeds `CONFIG_NWK_CONCENTRATOR_DELIVERY_FAILURE_THRESHOLD`.
726+
- Whenever all routes are purged (expired/blacklisted) the NWK handler must immediately trigger `sendPeriodicManyToOneRouteRequest()` so the coordinator refreshes routes.
727+
- Link-status frames reuse the computed path costs so any tweak to the heuristic must stay consistent across `sendPeriodicZigbeeNWKLinkStatus()` and the source-route scorer.
728+
769729
### Wireshark Compatibility
770730

771731
```typescript

AGENTS.md

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,21 @@ Zigbee on Host is an open-source Zigbee stack designed to run on a host and comm
77
**Architecture:**
88
- **Host-based Zigbee stack** with RCP communication
99
- **Language:** TypeScript (~7,000 lines of source code)
10-
- **Runtime:** Node.js ^20.19.0 || >=22.12.0
10+
- **Runtime:** Node.js
1111
- **License:** GPL-3.0-or-later
1212
- **Module system:** NodeNext (ES modules)
1313
- **Zero production dependencies** - lightweight core
14-
- **Protocol Layers:**
15-
- **Spinel Protocol (`src/spinel/`):**
16-
- `spinel.ts` - Core protocol (~400 lines)
17-
- `hdlc.ts` - HDLC framing
18-
- `properties.ts` - Spinel properties (2,800 lines)
19-
- `commands.ts` - Spinel commands
20-
- `statuses.ts` - Status codes
21-
- **Zigbee Protocol Utilities (`src/zigbee/`):**
22-
- `mac.ts` - IEEE 802.15.4 MAC layer utilities
23-
- `zigbee-nwk.ts` - Network layer utilities
24-
- `zigbee-aps.ts` - Application Support layer utilities
25-
- `zigbee-nwkgp.ts` - Green Power utilities
26-
- `zigbee.ts` - Main Zigbee utilities
27-
- **Zigbee Stack Handlers (`src/zigbee-stack/`):**
28-
- `stack-context.ts` - Shared state and context
29-
- `mac-handler.ts` - MAC layer handler
30-
- `nwk-handler.ts` - Network layer handler
31-
- `nwk-gp-handler.ts` - Green Power handler
32-
- `aps-handler.ts` - Application Support layer handler
33-
- **Driver (`src/drivers/`):**
34-
- `ot-rcp-driver.ts` - Main RCP driver (4,700 lines)
35-
- `ot-rcp-parser.ts` - Frame parsing
36-
- `ot-rcp-writer.ts` - Frame writing
37-
- `descriptors.ts` - Device descriptors
38-
- `wip.ts` - Work in progress features** with RCP communication
39-
40-
**Key Components:**
41-
- `src/drivers/` - RCP communication drivers (main: `ot-rcp-driver.ts`)
42-
- `src/zigbee-stack/` - Zigbee protocol stack handlers (MAC, NWK, APS, Green Power, context)
43-
- `src/spinel/` - Spinel protocol implementation (HDLC framing, properties)
44-
- `src/zigbee/` - Zigbee protocol utilities (frame encoding/decoding)
45-
- `src/dev/` - Development tools (excluded from production builds)
46-
- `src/utils/` - Shared utilities (logging framework)
14+
- **Protocol Layers:** Refer to [./docs/architecture.md](./docs/architecture.md)
15+
16+
Refer to [./package.json](./package.json) for versions.
4717

4818
## Setup Commands
4919

5020
**Prerequisites:**
51-
- Node.js version >=22.12.0 (also supports ^20.19.0)
52-
- npm 10.8.2+
21+
- Node.js (prefer v24)
22+
- npm
23+
24+
Refer to [./package.json](./package.json) for versions.
5325

5426
**Initial setup:**
5527
```bash
@@ -126,10 +98,8 @@ npm run test:cov # Run tests with coverage report (~2.5s)
12698
```
12799

128100
**Coverage requirements:**
129-
- Statements: 85%+
130-
- Functions: 85%+
131-
- Branches: 80%+
132-
- Lines: 85%+
101+
102+
Refer to [./test/vitest.config.mts](./test/vitest.config.mts)
133103

134104
**Coverage details:**
135105
- Provider: v8
@@ -372,6 +342,7 @@ docker compose -f docker-dev/compose.yaml down
372342
- ✅ Nested device support
373343
- ✅ Indirect transmission mechanism
374344
- ✅ Source routing
345+
- ✅ Host-guided routing heuristics (multi-path scoring + MAC NO_ACK integration + automatic MTORR refresh)
375346
- ✅ Coordinator LQI/Routing tables
376347
- ✅ LQI reporting
377348
- ✅ Install code validation and Trust Center enforcement for initial joins
@@ -467,7 +438,7 @@ docker compose -f docker-dev/compose.yaml down
467438

468439
- **Repository:** https://github.com/Nerivec/zigbee-on-host
469440
- **npm package:** zigbee-on-host
470-
- **Version:** 0.2.0 (work in progress, expect breaking changes)
441+
- **Version:** 0.2.3 (work in progress, expect breaking changes)
471442
- **Author:** Nerivec
472443

473444
### Key Files

docs/architecture.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Zigbee on Host - Architecture Documentation
22

3-
**Document Version:** 1.1
4-
**Last Updated:** November 2, 2025
3+
**Document Version:** 1.2
4+
**Last Updated:** November 27, 2025
55
**Maintainer:** Nerivec
66
**License:** GPL-3.0-or-later
77

@@ -25,6 +25,7 @@ Zigbee on Host is a host-based Zigbee stack implementation that communicates wit
2525
3. **Single Source of Truth** - StackContext owns all shared state
2626
4. **Zero Production Dependencies** - Only Node.js built-in modules
2727
5. **TypeScript Zero-Cost Abstractions** - Interfaces, const enums, type aliases
28+
6. **Host-Guided Routing Heuristics** - Routing decisions leverage host-only scoring (LQA, staleness, MAC feedback) to keep concentrator behavior deterministic
2829

2930
### Technology Stack
3031

@@ -199,6 +200,7 @@ export async function processFrame(
199200
- Handle data requests
200201
- Maintain indirect transmission queue
201202
- Process MAC commands
203+
- Update per-destination NO_ACK counters (`StackContext.macNoACKs`) to feed NWK routing heuristics
202204

203205
**Key Methods:**
204206
```typescript
@@ -240,6 +242,9 @@ interface MACHandlerCallbacks {
240242
- Implement many-to-one routing
241243
- Process route record frames
242244
- Handle all NWK commands
245+
- Score candidate routes with host-only heuristics (path cost, staleness penalty, MAC NO_ACK counters, recency bonus)
246+
247+
The handler now keeps multiple source-route entries per destination and ranks them every time `findBestSourceRoute` runs. Expired or blacklisted paths are purged eagerly, remaining entries are sorted by a composite score (path cost + penalties + recency bonus), and any relay that has accumulated too many MAC NO_ACK events is skipped entirely. When no usable path is left the handler immediately triggers a concentrator (many-to-one) refresh so the coordinator can rebuild routing knowledge.
243248

244249
**Key Methods:**
245250
```typescript
@@ -345,11 +350,13 @@ interface APSHandlerCallbacks {
345350
- Device table (device entries with capabilities, LQA tracking, authorization)
346351
- Address mappings (16-bit64-bit)
347352
- Routing tables (source routes)
353+
- Source route table entries with timestamps + failure counts (multiple per destination allowed)
348354
- Application link key table (pair-wise TC/app link keys)
349355
- Install code metadata and derived link keys
350356
- Key frame counters
351357
- Pending network key staging (pre-SWITCH_KEY activate)
352358
- End-device timeout metadata and runtime NWK frame counters
359+
- MAC NO_ACK counters (per-destination delivery health used by NWK heuristics)
353360
- Trust center policies (join policies, key policies)
354361
- Coordinator configuration attributes
355362
- Pending associations (awaiting DATA_RQ from device)
@@ -486,7 +493,9 @@ Application API Call
486493
487494
[MACHandler.sendFrameDirect()]
488495
├─ Build Spinel STREAM_RAW property (MAC frame)
489-
└─ Call callbacks.onSendFrame()
496+
├─ Call callbacks.onSendFrame()
497+
├─ ACK success → clear `macNoACKs` entry + `NWKHandler.markRouteSuccess()`
498+
└─ NO_ACK error → increment `macNoACKs` + `NWKHandler.markRouteFailure()`
490499
491500
[OTRCPDriver.setProperty()]
492501
├─ Encode as Spinel frame
@@ -499,6 +508,17 @@ Application API Call
499508
Serial Port → RCP Firmware → Radio Transmission
500509
```
501510
511+
### Routing Feedback Loop (Extra Pass Focus)
512+
513+
Recent refactors introduced a closed-loop routing controller that keeps concentrator behavior stable even when radio quality fluctuates:
514+
515+
1. **MAC delivery feedback** – Every unicast tracks whether the RCP reported an ACK. Success clears the destination entry in `StackContext.macNoACKs`, while a NO_ACK increments it and immediately calls `NWKHandler.markRouteFailure()`.
516+
2. **Source-route scoring** – `NWKHandler.findBestSourceRoute()` re-sorts all candidate paths on every lookup. The score combines hop count, staleness penalty, failure penalty, a recency bonus, and a hard filter that rejects any relay currently above the NO_ACK threshold.
517+
3. **Automatic recovery** – When the handler purges all routes (expired, blacklisted, or unhealthy relays), it schedules a many-to-one route discovery so the coordinator re-advertises itself and rebuilds source routes.
518+
4. **Link status integration** – Periodic link-status frames piggyback the same computed path costs so neighbors receive up-to-date metrics derived from LQA + routing penalties.
519+
520+
Together these steps ensure routing decisions remain deterministic and reflect the host's richer telemetry without waiting for devices to age routes out on their own.
521+
502522
## State Management
503523
504524
### State Persistence

0 commit comments

Comments
 (0)