diff --git a/CLAUDE.md b/CLAUDE.md index 2bf196eb..3296fed4 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -24,7 +24,7 @@ npx skills add longbridge/developers -g -y ### Critical Rules 1. **LongBridge CLI is user-installed** — NEVER bundle it in the app -2. **Symbol validation** — Validate with `/^[A-Z0-9]{1,5}\.(US|HK|SG|SH|SZ|HAS)$/` before exec +2. **Symbol validation** — Validate with `/^[A-Z0-9]{1,6}\.(US|HK|SG|SH|SZ|HAS)$/` before exec. The body is 1–6 characters: A-share codes such as `600519.SH` are 6 digits, so a 5-character limit rejects valid listings. 3. **Use execa array params** — Never string concatenation for commands 4. **Session isolation** — Use `atomFamily` for per-session state diff --git a/docs/api-reference.md b/docs/api-reference.md index 573d68da..43f45d49 100644 --- a/docs/api-reference.md +++ b/docs/api-reference.md @@ -16,6 +16,8 @@ Complete reference for all LongBridge CLI commands used by Finance Agent. | Indices | `.US` | `.VIX.US`, `.SPX.US` | Start with `.` | | Crypto | `.HAS` | `BTCUSD.HAS` | LongBridge crypto suffix | +The code before the suffix is 1–6 letters or digits. Shanghai and Shenzhen listings use 6-digit codes (`600519.SH`); a 5-character limit rejects them. Codes longer than 6 characters (`6005191.SH`, `TOOLONG.US`) are not valid Folio symbols. + --- ## Market Data Commands diff --git a/packages/longbridge-tools/src/validator.ts b/packages/longbridge-tools/src/validator.ts index 39ef1506..b6890f49 100644 --- a/packages/longbridge-tools/src/validator.ts +++ b/packages/longbridge-tools/src/validator.ts @@ -1,7 +1,7 @@ import { LongBridgeError } from './errors.ts'; // Symbol format: AAPL.US, 0700.HK, 600519.SH, 0388.HK, etc. -// Allow 1-5 chars of A-Z and 0-9 +// 1–6 characters. A-share listings are 6 digits; a 5-character limit rejects them. const SYMBOL_REGEX = /^[A-Z0-9]{1,6}\.(US|HK|SG|SH|SZ|HAS)$/; export function validateSymbol(symbol: string): boolean { diff --git a/packages/shared/src/portfolio-import/parsers.test.ts b/packages/shared/src/portfolio-import/parsers.test.ts index 3e0e175b..4315cdd6 100644 --- a/packages/shared/src/portfolio-import/parsers.test.ts +++ b/packages/shared/src/portfolio-import/parsers.test.ts @@ -46,6 +46,24 @@ describe('checkSymbol', () => { it('rejects unknown market suffixes', () => { expect(checkSymbol('AAPL.XX').ambiguous).toBe(true) }) + + it('does not treat over-long codes as canonical symbols', () => { + for (const symbol of ['6005191.SH', 'TOOLONG.US', 'DELISTED.US']) { + const check = checkSymbol(symbol) + expect(check.ambiguous).toBe(true) + expect(check.issue).toContain('not a recognized ticker') + } + const bare = checkSymbol('6005191') + expect(bare.ambiguous).toBe(true) + expect(bare.issue).toContain('not a recognized ticker') + expect(bare.issue).not.toContain('6005191.US') + }) + + it('still hints a market suffix for a 6-character bare ticker', () => { + const check = checkSymbol('600519') + expect(check.ambiguous).toBe(true) + expect(check.issue).toContain('600519.US') + }) }) describe('toFiniteNumber', () => { @@ -132,6 +150,15 @@ describe('parseCsv', () => { expect(mapped).toMatchObject({ symbol: 'AAPL.US', quantity: 1000, costPrice: 180.5, issues: [] }) }) + it('ignores zero-width characters glued to headers and numbers', () => { + const rows = parseCsv('\u200BSymbol,Quantity,Cost\nAAPL.US,1\u200B000,180.5') + expect(rows[0]).toMatchObject({ symbol: 'AAPL.US', quantity: 1000, costPrice: 180.5, confidence: 1, issues: [] }) + const wordJoiner = parseCsv('\u2060代码,数量,成本\n0700.HK,500,320') + expect(wordJoiner[0]).toMatchObject({ symbol: '0700.HK', quantity: 500, costPrice: 320, confidence: 1, issues: [] }) + expect(toFiniteNumber('1\u200B,000')).toBe(1000) + expect(toFiniteNumber('\u200B')).toBeUndefined() + }) + it('flags formatting-only quantities and costs without inventing zeros', () => { const rows = parseCsv('Symbol,Quantity,Cost\nAAPL.US,100,$\nMSFT.US,",",180.5\n0700.HK,500,320') expect(rows).toHaveLength(3) @@ -211,6 +238,14 @@ describe('parseCsv', () => { expect(parseCsv('\n\n')).toEqual([]) }) + it('keeps an over-long symbol for review instead of importing it as clean', () => { + const [row] = parseCsv('Symbol,Quantity,Cost\nTOOLONG.US,100,180.5\n600519.SH,10,1600') + expect(row.confidence).toBe(0.3) + expect(row.issues.some((issue) => issue.includes('not a recognized ticker'))).toBe(true) + const valid = parseCsv('Symbol,Quantity,Cost\n600519.SH,10,1600')[0] + expect(valid).toMatchObject({ symbol: '600519.SH', quantity: 10, costPrice: 1600, confidence: 1, issues: [] }) + }) + it('keeps a row when a required column is missing (review required)', () => { const rows = parseCsv('Symbol,Cost\nAAPL.US,180.5') expect(rows[0]).toMatchObject({ symbol: 'AAPL.US', costPrice: 180.5 }) diff --git a/packages/shared/src/portfolio-import/parsers.ts b/packages/shared/src/portfolio-import/parsers.ts index a2a141fd..e67f5e17 100644 --- a/packages/shared/src/portfolio-import/parsers.ts +++ b/packages/shared/src/portfolio-import/parsers.ts @@ -10,11 +10,15 @@ */ import type { ImportSource, PortfolioImportRow } from '@finagent/core' -/** Canonical symbol: 1–8 alphanumerics plus a known market suffix. */ -export const SYMBOL_RE = /^[A-Z0-9]{1,8}\.(US|HK|SG|SH|SZ|HAS)$/ +/** + * Canonical symbol: 1–6 alphanumerics plus a known market suffix. + * Matches `symbolSchema` / Longbridge validation. A-share codes are 6 digits; + * 7–8 character bodies are not canonical and must not import as clean rows. + */ +export const SYMBOL_RE = /^[A-Z0-9]{1,6}\.(US|HK|SG|SH|SZ|HAS)$/ /** Bare ticker with no market suffix — usable, but ambiguous (review). */ -export const BARE_TICKER_RE = /^[A-Z0-9]{1,8}$/ +export const BARE_TICKER_RE = /^[A-Z0-9]{1,6}$/ export type HeaderField = 'symbol' | 'name' | 'quantity' | 'cost' | 'currency' | 'account' @@ -83,9 +87,17 @@ export function checkSymbol(raw: string): SymbolCheck { } } +/** Characters `trim` does not remove, but spreadsheets and web paste often insert. */ +const INVISIBLE_CHARS = /[\uFEFF\u200B\u200C\u200D\u2060]/g + +function stripInvisible(value: string): string { + return value.replace(INVISIBLE_CHARS, '') +} + /** * Coerce a CSV/paste cell to a finite number. Strings with thousands * separators or a leading `$` are handled; empty/null/non-numeric → undefined. + * Zero-width characters from web paste are removed first. */ export function toFiniteNumber(value: unknown): number | undefined { if (typeof value === 'number') { @@ -94,7 +106,7 @@ export function toFiniteNumber(value: unknown): number | undefined { if (typeof value !== 'string') { return undefined } - const trimmed = value.trim() + const trimmed = stripInvisible(value).trim() if (trimmed === '') { return undefined } @@ -172,7 +184,7 @@ export function splitCsvLine(line: string, delimiter: ',' | '\t' = csvDelimiter( } function normalizeHeaderCell(cell: string): string { - return cell.trim().toLowerCase().replace(/[\s_\-]/g, '') + return stripInvisible(cell).trim().toLowerCase().replace(/[\s_\-]/g, '') } /** Field → resolved column index for one CSV file. */