interplane.Get Omega
Read in your language

synapse-map

aien-dev/synapse-map

Brought by drakestapleton

GitHub aien-dev/synapse-map#4 (code only)

closed derived 0a53a844549684e5 #4

Imported from GitHub aien-dev/synapse-map#4 (code only)

GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed).

Base c5b084681cd26113cd98023d0764930036144368 · Head 519ba4cfc9d2b2c8c360bcd4cfa8198ce62e3563

Apply it

git checkout -b patch-0a53a844549684e5 c5b084681cd26113cd98023d0764930036144368
curl -fsSL https://interplane.io/drakestapleton/synapse-map/patches/0a53a844549684e5.patch | git am

Raw patch (mbox) · Diff

Provenance

This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record:

github-import 1
aien-dev/synapse-map
4
519ba4cfc9d2b2c8c360bcd4cfa8198ce62e3563
{"kind":"github-import","owner":"aien-dev","repo":"synapse-map","number":4,"url":"https://github.com/aien-dev/synapse-map/pull/4","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:40:00Z","metadata":"unavailable","state_derived":true}

Changes

diff --git a/src/00-base.om b/src/00-base.om
index 3870a5d..e9ef63d 100644
--- a/src/00-base.om
+++ b/src/00-base.om
@@ -256,6 +256,13 @@ put gnum 0 10
 k = write 2 gnum 0 1
 end
 
+text tovf "synapse-map: internal buffer full\n"
+
+note An empty text is an unsized name at omc 2c824805 (nm_len 0 marks `view T *`): its len reads a count
+note register that nothing set, so `text t ""` gives garbage lengths (omega-mv #775, fixed after this pin
+note in e2662fff). The empty view is `part tempty 0 0`; keep it until omega.pin moves past that fix.
+text tempty "."
+
 note sm_die: flush pending stdout, then end the process with code
 fn sm_die code int to void
 if ne get gst SOVF 0
@@ -267,8 +274,6 @@ r = sm_flush 1
 exit code
 end
 
-text tovf "synapse-map: internal buffer full\n"
-
 note ======== string pool ========
 fn sm_pb c uchar to void
 o = get gst SPOOL
@@ -474,6 +479,8 @@ end
 ret 0
 end
 
+text tnoval "synapse-map: missing value for "
+
 note sm_aval: index of the value after flag t, 0 when the flag is absent; a flag without value is usage 64
 fn sm_aval t view uchar * to ulong
 i = sm_aflag t
@@ -489,8 +496,6 @@ end
 ret add i 1
 end
 
-text tnoval "synapse-map: missing value for "
-
 fn sm_apool i ulong to ulong
 o = get gst SPOOL
 n = sm_al i
@@ -502,6 +507,16 @@ put gst SSLEN n
 ret o
 end
 
+note sm_wmatch: argument bytes gab[o, o + n) equal w[s, s + n)
+fn sm_wmatch o ulong n ulong w view uchar * s ulong to bool
+for q ulong n
+if ne get gab add o q get w add s q
+ret false
+end
+end
+ret true
+end
+
 note sm_inlist: argument i equals one of the space-separated words of w
 fn sm_inlist i ulong w view uchar * to bool
 o = sm_ao i
@@ -514,13 +529,7 @@ e = eq get w j 32
 end
 if e
 if eq sub j s n
-m = true
-for q ulong n
-if ne get gab add o q get w add s q
-m = false
-end
-end
-if m
+if sm_wmatch o n w s
 ret true
 end
 end
@@ -530,6 +539,8 @@ end
 ret false
 end
 
+text tunk "synapse-map: unknown flag "
+
 note sm_argscan: every argument after the subcommand is a value flag (vf) with its value, a switch (sw)
 note or a positional; returns the number of positionals (first index in SSLEN), usage 64 on unknown flags
 fn sm_argscan vf view uchar * sw view uchar * to ulong
@@ -540,10 +551,12 @@ while lt i get gst SARGC bound 70
 o = sm_ao i
 isf = false
 if ge sm_al i 2
-if and eq get gab o 45 eq get gab add o 1 45
+if eq get gab o 45
+if eq get gab add o 1 45
 isf = true
 end
 end
+end
 if isf
 if sm_inlist i vf
 i = add i 2
@@ -568,8 +581,6 @@ end
 ret np
 end
 
-text tunk "synapse-map: unknown flag "
-
 note ======== paths ========
 fn sm_pclr to void
 put gst SPLEN 0
@@ -620,6 +631,16 @@ sm_pc 47
 sm_ptx t
 end
 
+note sm_envpre: environment entry b starts with t
+fn sm_envpre b view uchar 8192 t view uchar * to bool
+for j ulong len t
+if ne get b j get t j
+ret false
+end
+end
+ret true
+end
+
 note sm_env: value of the environment entry starting with t (e.g. "HOME=") into the pool, or SNONE
 fn sm_env t view uchar * to ulong
 b = new uchar 8192 0
@@ -627,13 +648,7 @@ n = envcnt
 for i ulong ulong n
 k = envread i b 0 8192
 if ge k long len t
-m = true
-for j ulong len t
-if ne get b j get t j
-m = false
-end
-end
-if m
+if sm_envpre b t
 o = get gst SPOOL
 for j ulong sub ulong k len t
 sm_pb get b add len t j
@@ -646,6 +661,17 @@ end
 ret SNONE
 end
 
+note sm_envkv: the whole environment entry "t<value>" (t ends in '=') into the pool; SNONE when absent
+fn sm_envkv t view uchar * to ulong
+o = sm_pt t
+v = sm_env t
+if eq v SNONE
+ret SNONE
+end
+put gst SSLEN add get gst SSLEN len t
+ret o
+end
+
 note ======== files ========
 note sm_rdfile: gpath into gfile; length, or -errno; -27 when the file is larger than gfile
 fn sm_rdfile to long
diff --git a/src/10-json.om b/src/10-json.om
index 1cd5258..9fa7d94 100644
--- a/src/10-json.om
+++ b/src/10-json.om
@@ -423,7 +423,9 @@ end
 note sm_skfind: skill whose name equals the pool slice, or SNONE
 fn sm_skfind o ulong n ulong to ulong
 for s ulong get gst SNSKL
-if sm_peq get gsk mul s 4 get gsk add mul s 4 1 o n
+so = get gsk mul s 4
+sn = get gsk add mul s 4 1
+if sm_peq so sn o n
 ret s
 end
 end
@@ -554,6 +556,13 @@ end
 ret true
 end
 
+note sm_jcopy: gfile[p, p + m) into src[0, m)
+fn sm_jcopy src edit uchar 983040 p ulong m ulong to void
+for i ulong m
+put src i get gfile add p i
+end
+end
+
 note sm_jload: parse gfile[0, SFLEN) as kind (whole document for 1, 5, 6; one object per line otherwise).
 note Returns >= 0, -65 malformed (line number in SBAD for JSONL), -27 too large, -12 table full.
 fn sm_jload kind ulong r ulong to long
@@ -592,9 +601,7 @@ if gt m len src
 put gst SBAD ln
 ret -27
 end
-for i ulong m
-put src i get gfile add p i
-end
+sm_jcopy src p m
 k = js_parse src tape m
 if lt k 0
 put gst SBAD ln
diff --git a/src/20-infer.om b/src/20-infer.om
index 6e2a322..88d59f9 100644
--- a/src/20-infer.om
+++ b/src/20-infer.om
@@ -25,6 +25,28 @@ end
 ret o
 end
 
+note sm_tsvsplit: split the row gfile[p, p + m) at tabs; the first 4 fields go to gtf as pool slices;
+note returns the number of fields
+fn sm_tsvsplit p ulong m ulong to ulong
+nf = ulong 0
+s = p
+for i ulong add m 1
+de = eq i m
+if not de
+de = eq get gfile add p i 9
+end
+if de
+if lt nf 4
+put gtf mul nf 2 sm_pfile s sub add p i s
+put gtf add mul nf 2 1 sub add p i s
+end
+nf = add nf 1
+s = add add p i 1
+end
+end
+ret nf
+end
+
 note sm_tsv: split gfile[0, SFLEN) into tab-separated rows of kind 1 (rules) or 2 (topics)
 fn sm_tsv kind ulong to long
 n = get gst SFLEN
@@ -51,22 +73,7 @@ if not skip
 skip = eq get gfile p 35
 end
 if not skip
-nf = ulong 0
-s = p
-for i ulong add m 1
-de = eq i m
-if not de
-de = eq get gfile add p i 9
-end
-if de
-if lt nf 4
-put gtf mul nf 2 sm_pfile s sub add p i s
-put gtf add mul nf 2 1 sub add p i s
-end
-nf = add nf 1
-s = add add p i 1
-end
-end
+nf = sm_tsvsplit p m
 x = sm_tsvrow kind nf ln
 if lt x 0
 put gst SBAD ln
@@ -240,6 +247,13 @@ sm_fset r FRULE o sub get gst SPOOL o
 sm_ks r KRUL add i 1
 end
 
+note sm_rlayer: record r takes the layer of the rule at grule[b] when it has none
+fn sm_rlayer r ulong b ulong to void
+if and eq sm_fl r FLAY 0 gt get grule add b 7 0
+sm_fset r FLAY get grule add b 6 get grule add b 7
+end
+end
+
 note sm_infer: apply the first matching rule to record r; a crumb-confirmed record only takes the layer
 fn sm_infer r ulong to void
 cf = ne sm_kg r KCONF 0
@@ -254,9 +268,7 @@ o = sm_pfull po pn
 skip = sm_isself r o get gst SSLEN
 end
 if not skip
-if and eq sm_fl r FLAY 0 gt get grule add b 7 0
-sm_fset r FLAY get grule add b 6 get grule add b 7
-end
+sm_rlayer r b
 if cf
 ret
 end
diff --git a/src/30-tok.om b/src/30-tok.om
index 40980f7..442b156 100644
--- a/src/30-tok.om
+++ b/src/30-tok.om
@@ -130,13 +130,7 @@ ret id
 end
 t = sub v 1
 if eq get gtkl t k
-m = true
-for i ulong k
-if ne get gpool add get gtko t i get gtb i
-m = false
-end
-end
-if m
+if sm_tbeq get gtko t k
 ret t
 end
 end
@@ -144,6 +138,16 @@ end
 ret SNONE
 end
 
+note sm_tbeq: pool bytes gpool[o, o + k) equal the token buffer gtb[0, k)
+fn sm_tbeq o ulong k ulong to bool
+for i ulong k
+if ne get gpool add o i get gtb i
+ret false
+end
+end
+ret true
+end
+
 fn sm_tokend id ulong bo ulong to void
 k = get gst STBN
 put gst STBN 0
@@ -163,11 +167,15 @@ if sm_isstop k
 ret
 end
 if gt k 4
-if and eq get gtb sub k 3 105 and eq get gtb sub k 2 101 eq get gtb sub k 1 115
+if eq get gtb sub k 3 105
+if eq get gtb sub k 2 101
+if eq get gtb sub k 1 115
 put gtb sub k 3 121
 k = sub k 2
 end
 end
+end
+end
 if gt k 3
 if eq get gtb sub k 1 115
 c = get gtb sub k 2
diff --git a/src/40-xerj.om b/src/40-xerj.om
index 5f6d7ad..a5b05db 100644
--- a/src/40-xerj.om
+++ b/src/40-xerj.om
@@ -14,7 +14,6 @@ text tctjson "application/json"
 text tctnd "application/x-ndjson"
 text tsrchs "/_search?filter_path=hits.hits._id,hits.hits._score"
 text tbulks "/_bulk?refresh=true&filter_path=errors"
-text tnosfx ""
 text tq1 "{\"size\":"
 text tq2 ",\"_source\":false,\"query\":{\"bool\":{\"must\":{\"semantic\":{\"field\":\"text\",\"query\":"
 text tq3 "}},\"filter\":{\"term\":{\"kind\":\""
@@ -23,19 +22,8 @@ text tkrepov "repo"
 text tkskill "skill"
 text tmapping "{\"mappings\":{\"properties\":{\"kind\":{\"type\":\"keyword\"},\"name\":{\"type\":\"keyword\"},\"text\":{\"type\":\"semantic_text\"}}}}"
 
-note sm_xhttp: method m on "/" index sfx with body gout[bs, SOUT); the response body lands in gfile
-note (length SFLEN). Returns the HTTP status, or a negative code.
-fn sm_xhttp m view uchar * sfx view uchar * bs ulong to long
-cs = new ulong 320 0
-io = new uchar 131072 0
-d = new uchar 1048576 0
-a = new uchar 16 0
-tg = new uchar 1024 0
-n = sub get gst SOUT bs
-put gst SOUT bs
-if eq get gst SXST 2
-ret -111
-end
+note sm_xtarget: "/" index sfx into tg; returns its length, or -36 when it does not fit
+fn sm_xtarget tg edit uchar 1024 sfx view uchar * to long
 io2 = get gst SIDXO
 il = get gst SIDXL
 if gt add add il len sfx 1 len tg
@@ -48,9 +36,62 @@ end
 for i ulong len sfx
 put tg add add il 1 i get sfx i
 end
-tn = add add il 1 len sfx
+ret long add add il 1 len sfx
+end
+
+note sm_xresp: read the response status and body of connection cs; the body lands in gfile (length
+note SFLEN). Returns the HTTP status, or a negative code; the connection is closed either way.
+fn sm_xresp cs edit ulong 320 io edit uchar 131072 to long
+d = new uchar 1048576 0
+st = hrsprd cs io
+if lt st 0
+c = hclose cs
+ret st
+end
+b = hbodyall cs io d
+c = hclose cs
+if lt b 0
+ret b
+end
+for i ulong ulong b
+put gfile i get d i
+end
+put gst SFLEN ulong b
+ret st
+end
+
+note sm_xsend: end the headers, write gout[bs, bs + n) as the body and finish the request on cs
+fn sm_xsend cs edit ulong 320 io edit uchar 131072 r long bs ulong n ulong to long
+q = hfe r hhend cs io 0 n
+for i ulong n
+q = hfe q hputb cs io get gout add bs i
+end
+ret hfe q hwfin cs io
+end
+
+note sm_xdial: connect to SXIP:SXPORT (3 s timeout); the descriptor, or a negative code
+fn sm_xdial to long
+a = new uchar 16 0
 r = haddr4 a uint get gst SXIP get gst SXPORT
-fd = hdial a 3000000000
+ret hdial a 3000000000
+end
+
+note sm_xhttp: method m on "/" index sfx with body gout[bs, SOUT); the response body lands in gfile
+note (length SFLEN). Returns the HTTP status, or a negative code.
+fn sm_xhttp m view uchar * sfx view uchar * bs ulong to long
+cs = new ulong 320 0
+io = new uchar 131072 0
+tg = new uchar 1024 0
+n = sub get gst SOUT bs
+put gst SOUT bs
+if eq get gst SXST 2
+ret -111
+end
+tn = sm_xtarget tg sfx
+if lt tn 0
+ret tn
+end
+fd = sm_xdial
 if lt fd 0
 put gst SXST 2
 ret fd
@@ -58,7 +99,7 @@ end
 r = hinit cs io int fd 0
 hsettmo cs 30000000000 30000000000
 r = hfe r hreqst cs io m
-r = hfe r hreqtg cs io part tg 0 tn
+r = hfe r hreqtg cs io part tg 0 ulong tn
 r = hfe r hhname cs io thost
 r = hfe r hhval cs io thostv
 if gt n 0
@@ -69,32 +110,17 @@ else
 r = hfe r hhval cs io tctjson
 end
 end
-r = hfe r hhend cs io 0 n
-for i ulong n
-r = hfe r hputb cs io get gout add bs i
-end
-r = hfe r hwfin cs io
+r = sm_xsend cs io r bs n
 if lt r 0
 c = hclose cs
 put gst SXST 2
 ret r
 end
-st = hrsprd cs io
+st = sm_xresp cs io
 if lt st 0
-c = hclose cs
 put gst SXST 2
 ret st
 end
-b = hbodyall cs io d
-c = hclose cs
-if lt b 0
-put gst SXST 2
-ret b
-end
-for i ulong ulong b
-put gfile i get d i
-end
-put gst SFLEN ulong b
 put gst SXST 1
 ret st
 end
@@ -126,18 +152,25 @@ end
 if ne st 200
 ret -71
 end
-sm_ot tbx1
-sm_otext sm_fo r FREPO sm_fl r FREPO
-sm_ob 34
-sm_ot tbx2
-end
-ret 0
+ret sm_jload 6 0
 end
 
+text tbx1 "{\"index\":{\"_id\":\"repo:"
+text tbx2 "}}\n{\"kind\":\"repo\",\"name\":"
+text tbx3 ",\"text\":"
+text tbxsep ": "
+text tbxlay ". Layer: "
+text tbxtop ". Topics: "
+text tbs1 "{\"index\":{\"_id\":\"skill:"
+text tbs2 "}}\n{\"kind\":\"skill\",\"name\":"
+
 note sm_xdoc: one bulk action line and document for record r
 fn sm_xdoc r ulong to void
 sm_ot tbx1
-sm_oq sm_fo r FREPO sm_fl r FREPO
+ro = sm_fo r FREPO
+rl = sm_fl r FREPO
+sm_otext ro rl
+sm_ob 34
 sm_ot tbx2
 sm_oq sm_fo r FREPO sm_fl r FREPO
 sm_ot tbx3
@@ -170,15 +203,6 @@ sm_oesc get t i
 end
 end
 
-text tbx1 "{\"index\":{\"_id\":\"repo:"
-text tbx2 "}}\n{\"kind\":\"repo\",\"name\":"
-text tbx3 ",\"text\":"
-text tbxsep ": "
-text tbxlay ". Layer: "
-text tbxtop ". Topics: "
-text tbs1 "{\"index\":{\"_id\":\"skill:"
-text tbs2 "}}\n{\"kind\":\"skill\",\"name\":"
-
 fn sm_xskill s ulong to void
 no = get gsk mul s 4
 nl = get gsk add mul s 4 1
@@ -200,13 +224,13 @@ end
 note sm_xindex: drop and recreate the index, then bulk-load records and skills. Returns 0 or negative.
 fn sm_xindex to long
 bs = get gst SOUT
-st = sm_xhttp tdelm tnosfx bs
+st = sm_xhttp tdelm part tempty 0 0 bs
 if lt st 0
 ret st
 end
 sm_ot tmapping
 put gst SCTYP 0
-st = sm_xhttp tputm tnosfx bs
+st = sm_xhttp tputm part tempty 0 0 bs
 if lt st 0
 ret st
 end
@@ -236,15 +260,29 @@ end
 ret 0
 end
 
-text tnoerr "{\"errors\":false}"
+text tnoerr "\"errors\":false"
 
+note sm_xnoerr: the bulk reply's top-level "errors" is false (it precedes "items" in the reply)
 fn sm_xnoerr to bool
 n = get gst SFLEN
-if ne n len tnoerr
+m = len tnoerr
+if lt n m
 ret false
 end
-for i ulong n
-if ne get gfile i get tnoerr i
+for i ulong add sub n m 1
+if sm_xnoat i
+ret true
+end
+if eq get gfile i 91
+ret false
+end
+end
+ret false
+end
+
+fn sm_xnoat i ulong to bool
+for j ulong len tnoerr
+if ne get gfile add i j get tnoerr j
 ret false
 end
 end
diff --git a/src/50-cmds.om b/src/50-cmds.om
index 0b2856c..03d84ad 100644
--- a/src/50-cmds.om
+++ b/src/50-cmds.om
@@ -39,7 +39,6 @@ text tvfwh "--home --limit --topics --skills --xerj --index"
 text tvfhome "--home"
 text tvfchk "--home --min-jaccard --xerj --index"
 text tvfnew "--home --parent --purpose --layer --dir"
-text tswnone ""
 text tswnox "--no-xerj"
 text tswtext "--text"
 text tmapf "map.jsonl"
@@ -344,7 +343,7 @@ end
 end
 
 fn sm_cimport to int
-np = sm_argscan tvfimp tswnone
+np = sm_argscan tvfimp part tempty 0 0
 if ne np 0
 sm_usage
 end
@@ -459,7 +458,7 @@ note ---- index ----
 text txdown "synapse-map: xerj unreachable or refused the request\n"
 
 fn sm_cindex to int
-np = sm_argscan tvfidx tswnone
+np = sm_argscan tvfidx part tempty 0 0
 if ne np 0
 sm_usage
 end
diff --git a/src/60-route.om b/src/60-route.om
index 522fd07..149b9ad 100644
--- a/src/60-route.om
+++ b/src/60-route.om
@@ -84,7 +84,9 @@ ms = get gst SMAXS
 for r ulong get gst SNREC
 l = sm_scale get gsc r ml
 if hs
-put gcb r div add mul l 6 mul sm_scale get gsm r ms 4 10
+sv = sm_scale get gsm r ms
+w = div add mul l 6 mul sv 4 10
+put gcb r w
 else
 put gcb r l
 end
@@ -92,13 +94,16 @@ put gpick r 0
 end
 end
 
-note sm_skscore: task score per skill (lexical over name and description, plus semantic when present)
-fn sm_skscore hs bool ms ulong to void
-mx = ulong 0
-for s ulong get gst SNSKL
+note sm_sklex: lexical task score of skill s (query terms found in its name and description)
+fn sm_sklex s ulong to ulong
 sm_sclr SXSET
-sm_tokz get gsk mul s 4 get gsk add mul s 4 1 SXSET 0
-sm_tokz get gsk add mul s 4 2 get gsk add mul s 4 3 SXSET 0
+b4 = mul s 4
+p0 = get gsk b4
+p1 = get gsk add b4 1
+sm_tokz p0 p1 SXSET 0
+p0 = get gsk add b4 2
+p1 = get gsk add b4 3
+sm_tokz p0 p1 SXSET 0
 v = ulong 0
 for i ulong sm_sn SQSET
 t = and sm_sget SQSET i 4294967295
@@ -106,6 +111,14 @@ if gt sm_shas SXSET t 0
 v = add v sm_idf t
 end
 end
+ret v
+end
+
+note sm_skscore: task score per skill (lexical over name and description, plus semantic when present)
+fn sm_skscore hs bool ms ulong to void
+mx = ulong 0
+for s ulong get gst SNSKL
+v = sm_sklex s
 put gskl s v
 if gt v mx
 mx = v
@@ -114,13 +127,38 @@ end
 for s ulong get gst SNSKL
 l = sm_scale get gskl s mx
 if hs
-put gskc s div add mul l 6 mul sm_scale get gsks s ms 4 10
+sv = sm_scale get gsks s ms
+w = div add mul l 6 mul sv 4 10
+put gskc s w
 else
 put gskc s l
 end
 end
 end
 
+note sm_skval: task score of skill s for row r, +300 when its name or description names the repo
+fn sm_skval s ulong r ulong to ulong
+v = get gskc s
+if eq v 0
+ret 0
+end
+b4 = mul s 4
+fo = sm_fo r FNAME
+fl = sm_fl r FNAME
+no = get gsk b4
+nl = get gsk add b4 1
+nm = sm_isub no nl fo fl
+if not nm
+no = get gsk add b4 2
+nl = get gsk add b4 3
+nm = sm_isub no nl fo fl
+end
+if nm
+v = add v 300
+end
+ret v
+end
+
 note sm_skrow: up to 3 skills for row r (task score, +300 when the skill names the repo)
 fn sm_skrow r ulong to void
 sm_okc tkskills
@@ -132,18 +170,7 @@ b = SNONE
 bv = ulong 0
 for s ulong get gst SNSKL
 if and ne s p1 ne s p2
-v = get gskc s
-if gt v 0
-no = get gsk mul s 4
-nl = get gsk add mul s 4 1
-nm = sm_isub no nl sm_fo r FNAME sm_fl r FNAME
-if not nm
-nm = sm_isub get gsk add mul s 4 2 get gsk add mul s 4 3 sm_fo r FNAME sm_fl r FNAME
-end
-if nm
-v = add v 300
-end
-end
+v = sm_skval s r
 if gt v bv
 bv = v
 b = s
@@ -156,7 +183,10 @@ end
 if gt k 0
 sm_ob 44
 end
-sm_oq get gsk mul b 4 get gsk add mul b 4 1
+b4 = mul b 4
+no = get gsk b4
+nl = get gsk add b4 1
+sm_oq no nl
 if eq k 0
 p1 = b
 else
@@ -255,6 +285,22 @@ sm_ob 125
 sm_ob 10
 end
 
+note sm_bestpick: unpicked record with the highest combined score above 0, or SNONE
+fn sm_bestpick to ulong
+b = SNONE
+bv = ulong 0
+for r ulong get gst SNREC
+if eq get gpick r 0
+v = get gcb r
+if gt v bv
+bv = v
+b = r
+end
+end
+end
+ret b
+end
+
 fn sm_cwhere to int
 np = sm_argscan tvfwh tswnox
 if ne np 1
@@ -315,16 +361,7 @@ sm_wcomb hs
 sm_skscore hs ms2
 nrow = ulong 0
 for k ulong lim
-b = SNONE
-bv = ulong 0
-for r ulong get gst SNREC
-if eq get gpick r 0
-if gt get gcb r bv
-bv = get gcb r
-b = r
-end
-end
-end
+b = sm_bestpick
 if eq b SNONE
 break
 end
@@ -358,26 +395,38 @@ sm_ob 125
 sm_ob 10
 end
 
-note sm_semtop: xerj top-3 neighbours (codes + 1) of every live record's purpose into gtop
-fn sm_semtop to bool
-for r ulong get gst SNREC
-put gtop mul r 4 0
-put gtop add mul r 4 1 0
-put gtop add mul r 4 2 0
-if and eq sm_kg r KARCH 0 gt sm_fl r FPUR 0
-if ne get gst SXST 2
-x = sm_xsearch sm_fo r FPUR sm_fl r FPUR 0 4
+note sm_semnb: xerj top-3 neighbours (codes + 1) of record r's purpose into gtop; false when xerj fails
+fn sm_semnb r ulong to bool
+po = sm_fo r FPUR
+pl = sm_fl r FPUR
+x = sm_xsearch po pl 0 4
 if lt x 0
 ret false
 end
+b4 = mul r 4
 k = ulong 0
 for i ulong get gst SNXR
 c = get gxr mul i 2
 if and ne c r lt k 3
-put gtop add mul r 4 k add c 1
+put gtop add b4 k add c 1
 k = add k 1
 end
 end
+ret true
+end
+
+note sm_semtop: xerj top-3 neighbours (codes + 1) of every live record's purpose into gtop
+fn sm_semtop to bool
+for r ulong get gst SNREC
+b4 = mul r 4
+put gtop b4 0
+put gtop add b4 1 0
+put gtop add b4 2 0
+if and eq sm_kg r KARCH 0 gt sm_fl r FPUR 0
+if ne get gst SXST 2
+if not sm_semnb r
+ret false
+end
 end
 end
 end
@@ -393,22 +442,44 @@ end
 ret false
 end
 
-note sm_cdup: duplicate_purpose pairs; returns the number of findings
-fn sm_cdup th ulong hs bool to ulong
-nf = ulong 0
-for a ulong get gst SNREC
-for b ulong get gst SNREC
-live = and lt a b and eq sm_kg a KARCH 0 eq sm_kg b KARCH 0
-if live
-live = and gt sm_fl a FPUR 0 gt sm_fl b FPUR 0
+note sm_dlive: a and b (a before b) are both live and both have a purpose
+fn sm_dlive a ulong b ulong to bool
+if ge a b
+ret false
+end
+if ne sm_kg a KARCH 0
+ret false
+end
+if ne sm_kg b KARCH 0
+ret false
 end
-if live
-j = sm_jac add 1024 a add 1024 b
+if eq sm_fl a FPUR 0
+ret false
+end
+ret gt sm_fl b FPUR 0
+end
+
+note sm_dpair: emit a duplicate_purpose finding for live pair a b when it clears th; true when emitted
+fn sm_dpair a ulong b ulong th ulong hs bool to bool
+ia = add 1024 a
+ib = add 1024 b
+j = sm_jac ia ib
 nb = false
 if hs
-nb = and sm_isnb a b sm_isnb b a
+n1 = sm_isnb a b
+n2 = sm_isnb b a
+nb = and n1 n2
+end
+j2 = mul j 2
+hit = ge j th
+if nb
+if ge j2 th
+hit = true
+end
+end
+if not hit
+ret false
 end
-if or ge j th and nb ge mul j 2 th
 sm_fhead tfdup a
 sm_okc tkother
 sm_fq b FREPO
@@ -416,11 +487,23 @@ sm_okc tkjac
 sm_omil j
 sm_okc tksem
 if hs
-sm_obool or sm_isnb a b sm_isnb b a
+n1 = sm_isnb a b
+n2 = sm_isnb b a
+sm_obool or n1 n2
 else
 sm_onull
 end
 sm_fend
+ret true
+end
+
+note sm_cdup: duplicate_purpose pairs; returns the number of findings
+fn sm_cdup th ulong hs bool to ulong
+nf = ulong 0
+for a ulong get gst SNREC
+for b ulong get gst SNREC
+if sm_dlive a b
+if sm_dpair a b th hs
 nf = add nf 1
 end
 end
diff --git a/src/70-new.om b/src/70-new.om
index 7cab61e..1170b88 100644
--- a/src/70-new.om
+++ b/src/70-new.om
@@ -30,7 +30,6 @@ text tc3 ",\"parent_status\":\"CONFIRMED\"}}}\n"
 text tg1 "{\"schema\":\"goals/v1\",\"project\":"
 text tg2 ",\"goals\":[{\"id\":\"g1\",\"title\":"
 text tg3 ",\"status\":\"pending\"}]}\n"
-text tgh "https://github.com/"
 text tr1 "Child of ["
 text tr2 "](https://github.com/"
 text tr3 ") at `"
@@ -59,6 +58,81 @@ text tfail "FAIL"
 text tnotrun "NOT_RUN"
 text tunkref "unknown"
 
+note ---- new --publish declarations (functions in 75-publish.om) ----
+
+global garv ulong 128
+
+const SPKN ulong 40
+const SPRO ulong 41
+const SPRL ulong 42
+const SCLO ulong 43
+const SCLL ulong 44
+const SBRO ulong 45
+const SBRL ulong 46
+const SCHO ulong 47
+const SCHL ulong 48
+const SLNO ulong 49
+const SLNL ulong 50
+const SDRY ulong 51
+const SFNO ulong 52
+const SFNL ulong 53
+
+text tfpub "--publish"
+text tfdry "--dry-run"
+text tswnew "--publish --dry-run"
+text tpathsfx "/.local/bin:/usr/bin:/bin"
+text tpathk "PATH="
+text tgit "git"
+text tgh "gh"
+text tdashc "-C"
+text tdashcc "-c"
+text tuname "user.name=Drake Stapleton"
+text tumail "user.email=aien@aienos.com"
+text tinit "init"
+text tdashb "-b"
+text tmain "main"
+text tadd "add"
+text tdasha "-A"
+text tcommit "commit"
+text tdashs "-s"
+text tdashm "-m"
+text tmsg1 "Scaffold "
+text tmsg1b " (synapse-map new)"
+text tmsg2 "docs: connect "
+text trepo "repo"
+text tcreate "create"
+text tpublic "--public"
+text tdescr "--description"
+text tsource "--source"
+text tpush "--push"
+text tclone "clone"
+text tghurl "https://github.com/"
+text tparsfx ".parent"
+text tchkout "checkout"
+text tconn "connected/"
+text treadme "README.md"
+text torigin "origin"
+text tdashu "-u"
+text tpr "pr"
+text tdraft "--draft"
+text tdashr "-R"
+text thead "--head"
+text ttitle "--title"
+text tconnt "Connect "
+text tbodyf "--body-file"
+text tsppr "/PARENT-PR.md"
+text tkargv "argv"
+text tkedit "edit"
+text tkline "line"
+text tkstep "step"
+text tkstat "status"
+text tkpub "published"
+text tkbr "branch"
+text tconnh "## Connected repos"
+text tconnhb "\n## Connected repos\n\n"
+text tsreadme "/README.md"
+text tpubdry "synapse-map: --dry-run needs --publish\n"
+
 fn sm_ndir t view uchar * to void
 sm_pclr
 sm_parg get gst SNDI
@@ -203,17 +277,17 @@ put gpk 0 0
 put gpk 1 0
 put gst SPKN 0
 p = sm_pktx 2 tenv
-h = sm_env thomee
+h = sm_envkv thomee
 if eq h SNONE
 p = sm_pktx p tdefhome
 else
-p = sm_pkadd p sub h 5 add get gst SSLEN 5
+p = sm_pkadd p h get gst SSLEN
 end
-e = sm_env tpathe
+e = sm_envkv tpathe
 if eq e SNONE
 p = sm_pktx p tdefpath
 else
-p = sm_pkadd p sub e 5 add get gst SSLEN 5
+p = sm_pkadd p e get gst SSLEN
 end
 p = sm_pktx p tcrumbc
 p = sm_pktx p tvalid
@@ -233,6 +307,43 @@ end
 ret 1
 end
 
+note sm_nreport: print the new-repo JSON report for directory argument di; exit 1 when validation failed
+fn sm_nreport di ulong to void
+v = sm_nvalid
+sm_ob 123
+sm_ok tkok
+sm_obool ne v 1
+sm_okc tkdir
+dq = sm_apool di
+sm_oq dq get gst SSLEN
+sm_okc tkfiles
+sm_ob 91
+for i ulong get gst SNFI
+if gt i 0
+sm_ob 44
+end
+fo = get gnf i
+fl = get gnf add i 16
+sm_oq fo fl
+end
+sm_ob 93
+sm_okc tkcv
+if eq v 0
+sm_otq tpass
+end
+if eq v 1
+sm_otq tfail
+end
+if eq v 2
+sm_otq tnotrun
+end
+sm_ob 125
+sm_ob 10
+if eq v 1
+sm_die 1
+end
+end
+
 fn sm_cnew to int
 np = sm_argscan tvfnew tswnew
 dry = sm_aflag tfdry
@@ -285,37 +396,7 @@ sm_parg di
 sm_mkdirp
 put gst SNFI 0
 sm_nfiles
-v = sm_nvalid
-sm_ob 123
-sm_ok tkok
-sm_obool ne v 1
-sm_okc tkdir
-dq = sm_apool di
-sm_oq dq get gst SSLEN
-sm_okc tkfiles
-sm_ob 91
-for i ulong get gst SNFI
-if gt i 0
-sm_ob 44
-end
-sm_oq get gnf i get gnf add i 16
-end
-sm_ob 93
-sm_okc tkcv
-if eq v 0
-sm_otq tpass
-end
-if eq v 1
-sm_otq tfail
-end
-if eq v 2
-sm_otq tnotrun
-end
-sm_ob 125
-sm_ob 10
-if eq v 1
-sm_die 1
-end
+sm_nreport di
 if gt pub 0
 sm_publish
 end
diff --git a/src/75-publish.om b/src/75-publish.om
index dd60400..fd3ec9f 100644
--- a/src/75-publish.om
+++ b/src/75-publish.om
@@ -3,80 +3,8 @@ note Every step is one argv run as /usr/bin/env HOME=$HOME PATH=$HOME/.local/bin
 note (execprog: empty environment, no PATH search), with the child's stdout sent to stderr so stdout
 note stays JSONL. --dry-run prints {"argv":[...]} (and {"edit":..,"line":..} for the README step)
 note instead of running anything. The first failing step stops the run: {"ok":false,...}, exit 70.
-
-global garv ulong 128
-
-const SPKN ulong 40
-const SPRO ulong 41
-const SPRL ulong 42
-const SCLO ulong 43
-const SCLL ulong 44
-const SBRO ulong 45
-const SBRL ulong 46
-const SCHO ulong 47
-const SCHL ulong 48
-const SLNO ulong 49
-const SLNL ulong 50
-const SDRY ulong 51
-const SFNO ulong 52
-const SFNL ulong 53
-
-text tfpub "--publish"
-text tfdry "--dry-run"
-text tswnew "--publish --dry-run"
-text tpathsfx "/.local/bin:/usr/bin:/bin"
-text tpathk "PATH="
-text tgit "git"
-text tgh "gh"
-text tdashc "-C"
-text tdashcc "-c"
-text tuname "user.name=Drake Stapleton"
-text tumail "user.email=aien@aienos.com"
-text tinit "init"
-text tdashb "-b"
-text tmain "main"
-text tadd "add"
-text tdasha "-A"
-text tcommit "commit"
-text tdashs "-s"
-text tdashm "-m"
-text tmsg1 "Scaffold "
-text tmsg1b " (synapse-map new)"
-text tmsg2 "docs: connect "
-text trepo "repo"
-text tcreate "create"
-text tpublic "--public"
-text tdescr "--description"
-text tsource "--source"
-text tpush "--push"
-text tclone "clone"
-text tghurl "https://github.com/"
-text tparsfx ".parent"
-text tcheckout "checkout"
-text tconn "connected/"
-text treadme "README.md"
-text torigin "origin"
-text tdashu "-u"
-text tpr "pr"
-text tdraft "--draft"
-text tdashr "-R"
-text thead "--head"
-text ttitle "--title"
-text tconnt "Connect "
-text tbodyf "--body-file"
-text tsppr "/PARENT-PR.md"
-text tkargv "argv"
-text tkedit "edit"
-text tkline "line"
-text tkstep "step"
-text tkstat "status"
-text tkpub "published"
-text tkbr "branch"
-text tconnh "## Connected repos"
-text tconnhb "\n## Connected repos\n\n"
-text tsreadme "/README.md"
-text tpubdry "synapse-map: --dry-run needs --publish\n"
-text tnone ""
+note Its globals, consts and texts are declared in 70-new.om: sm_new reads them, and omc needs a global,
+note const or text declared before its first use.
 
 note sm_cat: pool slice for text a + slice (o, n) + text b; length in SSLEN
 fn sm_cat a view uchar * o ulong n ulong b view uchar * to ulong
@@ -94,14 +22,16 @@ put gpk 0 0
 put gpk 1 0
 put gst SPKN 0
 p = sm_pktx 2 tenv
-h = sm_env thomee
-if eq h SNONE
+k = sm_envkv thomee
+if eq k SNONE
 p = sm_pktx p tdefhome
 p = sm_pktx p tdefpath
 ret p
 end
-hn = get gst SSLEN
-p = sm_pkadd p sub h 5 add hn 5
+kn = get gst SSLEN
+p = sm_pkadd p k kn
+h = add k len thomee
+hn = sub kn len thomee
 q = sm_cat tpathk h hn tpathsfx
 p = sm_pkadd p q get gst SSLEN
 ret p
@@ -146,7 +76,10 @@ if ge i 3
 if gt i 3
 sm_ob 44
 end
-sm_oq get garv mul i 2 get garv add mul i 2 1
+i2 = mul i 2
+ao = get garv i2
+al = get garv add i2 1
+sm_oq ao al
 end
 end
 sm_ob 93
@@ -204,10 +137,10 @@ po = get gst SNPO
 q = sm_pfind po get gst SNPL 64
 put gst SPRO po
 put gst SPRL q
-c = sm_cat tnone d dn tparsfx
+c = sm_cat part tempty 0 0 d dn tparsfx
 put gst SCLO c
 put gst SCLL get gst SSLEN
-b = sm_cat tconn get gst SNNO get gst SNNL tnone
+b = sm_cat tconn get gst SNNO get gst SNNL part tempty 0 0
 put gst SBRO b
 put gst SBRL get gst SSLEN
 w = sm_pfind po q 47
@@ -243,6 +176,46 @@ end
 ret true
 end
 
+note sm_rdapp: copy gfile (n bytes) to output, then append the Connected repos section and the line
+fn sm_rdapp n ulong to void
+for j ulong n
+sm_ob get gfile j
+end
+lastc = uchar 10
+if gt n 0
+lastc = get gfile sub n 1
+end
+if ne lastc 10
+sm_ob 10
+end
+sm_ot tconnhb
+sm_op get gst SLNO get gst SLNL
+sm_ob 10
+end
+
+note sm_rdins: copy gfile with the line inserted at the end of the section starting at h (scan stopped at i)
+fn sm_rdins h ulong i ulong n ulong to void
+jj = i
+if gt jj n
+jj = n
+end
+lo = add h len tconnh
+for it ulong jj
+if not and gt jj lo eq get gfile sub jj 1 10
+break
+end
+jj = sub jj 1
+end
+for k ulong jj
+sm_ob get gfile k
+end
+sm_ob 10
+sm_op get gst SLNO get gst SLNL
+for kk ulong sub n jj
+sm_ob get gfile add jj kk
+end
+end
+
 note sm_rdedit: add the README line under "## Connected repos" in the clone (section created if absent)
 fn sm_rdedit to void
 sm_pclr
@@ -267,7 +240,10 @@ r = sm_flush 1
 n = get gst SFLEN
 h = SNONE
 i = ulong 0
-while lt i n bound 2097152
+for it ulong n
+if ge i n
+break
+end
 if and eq h SNONE sm_ishead i n tconnh
 h = i
 else
@@ -279,35 +255,9 @@ e = sm_pfind2 i n
 i = add e 1
 end
 if eq h SNONE
-for j ulong n
-sm_ob get gfile j
-end
-lastc = uchar 10
-if gt n 0
-lastc = get gfile sub n 1
-end
-if ne lastc 10
-sm_ob 10
-end
-sm_ot tconnhb
-sm_op get gst SLNO get gst SLNL
-sm_ob 10
+sm_rdapp n
 else
-jj = i
-if gt jj n
-jj = n
-end
-while and gt jj add h len tconnh eq get gfile sub jj 1 10 bound 2097152
-jj = sub jj 1
-end
-for k ulong jj
-sm_ob get gfile k
-end
-sm_ob 10
-sm_op get gst SLNO get gst SLNL
-for kk ulong sub n jj
-sm_ob get gfile add jj kk
-end
+sm_rdins h i n
 end
 w = sm_wratom
 if lt w 0
@@ -320,12 +270,14 @@ end
 
 note sm_pfind2: offset of the next '\n' in gfile from i (n when none)
 fn sm_pfind2 i ulong n ulong to ulong
-j = i
-while lt j n bound 2097152
+if ge i n
+ret n
+end
+for k ulong sub n i
+j = add i k
 if eq get gfile j 10
 ret j
 end
-j = add j 1
 end
 ret n
 end
@@ -373,19 +325,19 @@ sm_step 4 p
 p = sm_pkenv
 p = sm_pktx p tgit
 p = sm_pktx p tclone
-u = sm_cat tghurl get gst SPRO get gst SPRL tnone
+u = sm_cat tghurl get gst SPRO get gst SPRL part tempty 0 0
 p = sm_pkadd p u get gst SSLEN
 p = sm_pkg p SCLO
 sm_step 5 p
 p = sm_pkgit SCLO
-p = sm_pktx p tcheckout
+p = sm_pktx p tchkout
 p = sm_pktx p tdashb
 p = sm_pkg p SBRO
 sm_step 6 p
 if ne get gst SDRY 0
 sm_ob 123
 sm_ok tkedit
-e = sm_cat tnone get gst SCLO get gst SCLL tsreadme
+e = sm_cat part tempty 0 0 get gst SCLO get gst SCLL tsreadme
 el = get gst SSLEN
 sm_oq e el
 sm_okc tkline
@@ -404,7 +356,7 @@ p = sm_pkid p
 p = sm_pktx p tcommit
 p = sm_pktx p tdashs
 p = sm_pktx p tdashm
-m = sm_cat tmsg2 get gst SNNO get gst SNNL tnone
+m = sm_cat tmsg2 get gst SNNO get gst SNNL part tempty 0 0
 p = sm_pkadd p m get gst SSLEN
 sm_step 9 p
 p = sm_pkgit SCLO
@@ -423,10 +375,10 @@ p = sm_pkadd p get gst SPRO get gst SPRL
 p = sm_pktx p thead
 p = sm_pkg p SBRO
 p = sm_pktx p ttitle
-t = sm_cat tconnt get gst SNNO get gst SNNL tnone
+t = sm_cat tconnt get gst SNNO get gst SNNL part tempty 0 0
 p = sm_pkadd p t get gst SSLEN
 p = sm_pktx p tbodyf
-b = sm_cat tnone get gst SCHO get gst SCHL tsppr
+b = sm_cat part tempty 0 0 get gst SCHO get gst SCHL tsppr
 p = sm_pkadd p b get gst SSLEN
 sm_step 11 p
 if eq get gst SDRY 0
diff --git a/src/99-main.om b/src/99-main.om
index ab1f915..d0a8eb4 100644
--- a/src/99-main.om
+++ b/src/99-main.om
@@ -30,11 +30,32 @@ put gst SBINO p
 put gst SBINL s
 end
 
+note sm_ahome: "--home DIR COMMAND ..." becomes "COMMAND --home DIR ...", so argument 1 is the command
+fn sm_ahome to void
+if lt get gst SARGC 4
+ret
+end
+if not sm_aeq 1 tfhome
+ret
+end
+ho = get gao 2
+hl = get gao 3
+vo = get gao 4
+vl = get gao 5
+put gao 2 get gao 6
+put gao 3 get gao 7
+put gao 4 ho
+put gao 5 hl
+put gao 6 vo
+put gao 7 vl
+end
+
 fn main to int
 x = sm_args
 if lt x 0
 sm_usage
 end
+sm_ahome
 if lt get gst SARGC 2
 sm_usage
 end