From 32333ac7ba6209358b760de0b51d031674abbfac Mon Sep 17 00:00:00 2001 From: wannT-1 <200890097+wannT-1@users.noreply.github.com> Date: Wed, 9 Sep 2026 16:30:00 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix(portfolio-import):=20=E5=8E=BB=E6=8E=89?= =?UTF-8?q?=E8=A1=A8=E5=A4=B4=E5=92=8C=E6=95=B0=E5=AD=97=E9=87=8C=E7=9A=84?= =?UTF-8?q?=E9=9B=B6=E5=AE=BD=E5=AD=97=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 网页粘贴带上的零宽空格会让 Symbol 列表头匹配失败,整表变成没有代码的行。现在表头和数值会先去掉这些不可见字符。 --- packages/shared/src/portfolio-import/parsers.test.ts | 9 +++++++++ packages/shared/src/portfolio-import/parsers.ts | 12 ++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/shared/src/portfolio-import/parsers.test.ts b/packages/shared/src/portfolio-import/parsers.test.ts index 3e0e175b..a1767c39 100644 --- a/packages/shared/src/portfolio-import/parsers.test.ts +++ b/packages/shared/src/portfolio-import/parsers.test.ts @@ -132,6 +132,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) diff --git a/packages/shared/src/portfolio-import/parsers.ts b/packages/shared/src/portfolio-import/parsers.ts index a2a141fd..20d43ffe 100644 --- a/packages/shared/src/portfolio-import/parsers.ts +++ b/packages/shared/src/portfolio-import/parsers.ts @@ -83,9 +83,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 +102,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 +180,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. */ From e3e31da687556085979282fe78102ffd885195ca Mon Sep 17 00:00:00 2001 From: wannT-1 <200890097+wannT-1@users.noreply.github.com> Date: Thu, 17 Sep 2026 11:15:00 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix(portfolio-import):=20=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E7=AC=A6=E5=8F=B7=E9=95=BF=E5=BA=A6=E4=B8=8E=E5=85=A8=E4=BB=93?= =?UTF-8?q?=201=E2=80=936=20=E4=BD=8D=E8=A7=84=E5=88=99=E5=AF=B9=E9=BD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 7 位以上的代码(如 6005191.SH、TOOLONG.US)不再被当成可直接导入的标准符号。A 股 6 位代码仍然有效,校验注释里过时的 1–5 位说明也已改掉。 --- packages/longbridge-tools/src/validator.ts | 2 +- .../src/portfolio-import/parsers.test.ts | 26 +++++++++++++++++++ .../shared/src/portfolio-import/parsers.ts | 10 ++++--- 3 files changed, 34 insertions(+), 4 deletions(-) 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 a1767c39..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', () => { @@ -220,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 20d43ffe..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' From d4f6675a84dfe65a7fcc8aa15f9711f987005418 Mon Sep 17 00:00:00 2001 From: wannT-1 <200890097+wannT-1@users.noreply.github.com> Date: Fri, 25 Sep 2026 15:45:00 +0800 Subject: [PATCH 3/3] =?UTF-8?q?docs:=20=E4=BF=AE=E6=AD=A3=E8=BF=87?= =?UTF-8?q?=E6=97=B6=E7=9A=84=201=E2=80=935=20=E4=BD=8D=E6=A0=87=E7=9A=84?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CLAUDE 和 API 参考里的 5 位上限会拒绝合法的 6 位 A 股代码(如 600519.SH)。文档改为与校验实现一致的 1–6 位。 --- CLAUDE.md | 2 +- docs/api-reference.md | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) 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