Hmbown/CodeWhale · error · Error

Invalid local connection

Error message

Invalid local connection

What it means

Shared-companion twin of the pet-watch token check: `connect()` rejects a URL hash token that is not exactly 64 hex characters before attempting `POST /v1/attach` with it as a Bearer token. This blocks malformed or tampered connection tokens from reaching the local companion. The rejection surfaces in `#message` through `connect().catch(...)`.

Solutions

  1. Reopen the page from Codewhale to get a fresh, valid 64-hex token link.
  2. Re-copy the full link without modifications and confirm the fragment is 64 hex characters.
  3. Clear any extra text after `#` in the address bar and retry with the correct token.
  4. Use preview mode (?preview) when a live attach is not required.

Example fix

// before
open('shared.html#token-from-email')  // not hex
// after
open('shared.html#0f1e2d3c4b5a69788796a5b4c3d2e1f00f1e2d3c4b5a69788796a5b4c3d2e1f0')  // 64 hex chars
Defensive patterns

Strategy: validation

Validate before calling

const token = location.hash.slice(1);
if (!/^[a-f0-9]{64}$/i.test(token)) throw new Error('Token must be 64 hex characters.');

Type guard

const isConnectionToken = (s) => typeof s === 'string' && /^[a-f0-9]{64}$/.test(s);

Try / catch

connect().catch((e) => {
  if (e.message === 'Invalid local connection') showMessage('Link token is malformed; regenerate it from Codewhale.');
  else showMessage(e.message);
});

Prevention

When it happens

Trigger: Opening `shared.html#<token>` with a truncated, non-hex, URL-encoded, or otherwise malformed fragment instead of the 64-character token issued by Codewhale.

Common situations: Copy-paste truncation of the attach link; chat/markdown renderers altering the fragment; stale links after the companion rotated its token; manually typing the URL.

Understand the failure class

Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@433685b202 (2026-09-15). Data as JSON: /api/errors/71d28e03847e26c5. Report an issue: GitHub.

Appendix: source

Thrown at pet/public/shared.html:89

function actionMarks(ctx,kind,t,w,h,ink,parallel){const cx=w*.5,cy=h*.55,r=Math.min(w*.29,h*.26),phase=t*.8;ctx.save();ctx.strokeStyle=ctx.fillStyle=ink;ctx.lineWidth=1;ctx.globalAlpha=.24;const line=(x1,y1,x2,y2)=>{ctx.beginPath();ctx.moveTo(x1,y1);ctx.lineTo(x2,y2);ctx.stroke()},circle=(x,y,r)=>{ctx.beginPath();ctx.arc(x,y,r,0,Math.PI*2);ctx.stroke()};
if(['reading','memory','files'].includes(kind)){for(const side of [-1,1]){const x=cx+side*r*1.35;for(let j=0;j<6;j++){const y=cy-42+j*15;ctx.globalAlpha=.1+.16*(.5+.5*Math.sin(phase-j*.7));line(x-21,y,x+21-(j%3)*6,y)}}}
else if(kind==='searching'||kind==='browsing'||kind==='network'){circle(cx,cy,r*1.3);const a=phase%(Math.PI*2);line(cx+Math.cos(a)*r,cy+Math.sin(a)*r,cx+Math.cos(a)*r*1.42,cy+Math.sin(a)*r*1.42);for(let i=0;i<5;i++){const a=i*Math.PI*2/5;circle(cx+Math.cos(a)*r*1.3,cy+Math.sin(a)*r*1.3,3+i%2)}}
else if(['editing','executing','tool'].includes(kind)){for(const side of [-1,1]){const x=cx+side*r*1.3;line(x,cy-32,x,cy+32);line(x,cy-32,x-side*12,cy-32);line(x,cy+32,x-side*12,cy+32)}ctx.globalAlpha=.2+.2*(.5+.5*Math.sin(phase*3));line(cx+r*.9,cy+44,cx+r*.9+18,cy+44)}
else if(kind==='testing'){for(let i=0;i<12;i++){const a=i*Math.PI/6;ctx.globalAlpha=.08+.35*Math.max(0,Math.cos(a-phase));line(cx+Math.cos(a)*r*1.25,cy+Math.sin(a)*r*1.25,cx+Math.cos(a)*r*1.38,cy+Math.sin(a)*r*1.38)}}
else if(kind==='waiting'){for(let i=0;i<3;i++){ctx.globalAlpha=.08+.05*(.5+.5*Math.sin(phase-i));circle(cx,cy,r*(1.12+i*.14))}}
else if(kind==='responding'){for(let i=0;i<5;i++){const x=cx-r*.7+i*r*.35;ctx.globalAlpha=.12+.22*(.5+.5*Math.sin(phase*2-i));line(x,cy+r*1.3,x+r*.22,cy+r*1.3)}}
if(parallel){for(let i=0;i<Math.min(6,parallel);i++){const x=cx+(i-(parallel-1)/2)*42,y=h*.83;ctx.globalAlpha=.4;circle(x,y,7);circle(x,y,2);if(i)line(x-34,y,x-9,y)}}ctx.restore()}

function readable(c){return c[0]*.2126+c[1]*.7152+c[2]*.0722>145?'#28383d':'#d4e5e5'}
function paint(c,f,a,now,small=false){const w=c.clientWidth,h=c.clientHeight,d=small?1:Math.min(devicePixelRatio||1,2);if(!w||!h)return;if(c.width!==Math.round(w*d)||c.height!==Math.round(h*d)){c.width=Math.round(w*d);c.height=Math.round(h*d)}const ctx=c.getContext('2d');ctx.setTransform(d,0,0,d,0,0);const gradient=ctx.createRadialGradient(w*.5,0,0,w*.5,h*.18,w*.9);gradient.addColorStop(0,hex(a.backgroundTop));gradient.addColorStop(1,hex(a.background));ctx.fillStyle=gradient;ctx.fillRect(0,0,w,h);if(!f)return;const still=$('still').checked,pose=still?f.still:f,state=pose.state,style=pose.style,stale=!preview&&(now-lastTickAt>800||now-received>800),t=still?0:f.timeMs/1000;
if(a.environment){ctx.strokeStyle=readable(a.background);ctx.globalAlpha=.12;ctx.lineWidth=.7;ctx.beginPath();ctx.moveTo(20,26);ctx.lineTo(w-20,26);ctx.stroke();for(let lane=0;lane<6;lane++){ctx.globalAlpha=.025;ctx.beginPath();for(let x=0;x<=w;x+=8){const y=h*.85+Math.sin(x/110+lane+t*.18)*7+lane*5;x?ctx.lineTo(x,y):ctx.moveTo(x,y)}ctx.stroke()}}ctx.globalAlpha=1;
const complete=demoDone&&!small,bounds=pose.points.reduce((b,p)=>[Math.max(b[0],Math.abs(p[0])),Math.max(b[1],Math.abs(p[1]))],[.7,.7]),scale=Math.min(w*.38/bounds[0],h*(complete?.18:.28)/bounds[1]),ox=w*(.5+(still?0:state.roamX)*.06),oy=h*(complete?.25:.56),flip=state.flip,mix=previous&&!still&&!stale?Math.min(1,(now-lastTickAt)/33.334):1;
if(!small&&!complete&&!stale&&f.activity?.observed)actionMarks(ctx,f.activity.kind,t,w,h,readable(a.background),f.activity.parallel);
const color=a.eventColors?[style.r,style.g,style.b]:a.particle,material='rgb('+color.map(Math.round).join(',')+')',radius=Math.max(.65,Math.min(w,h)*.0026)*a.dotScale,hollow=stale||!f.producerConnected||state.observed<.92;
const alpha=preview?Math.max(style.alpha,hollow?.32:.6)*a.brightness:style.alpha/(f.appearance?.brightness||1)*a.brightness;
for(let i=0;i<980;i++){const q=pose.points[i],p=previous?.points[i],x=ox+(p&&!still?p[0]+(q[0]-p[0])*mix:q[0])*scale*flip,y=oy+(p&&!still?p[1]+(q[1]-p[1])*mix:q[1])*scale,depth=.65+.35*(i*37%101)/100;ctx.fillStyle=ctx.strokeStyle=material;ctx.globalAlpha=Math.min(1,alpha*depth*(stale?.5:1));ctx.lineWidth=Math.max(.65,radius*.5);ctx.beginPath();ctx.arc(x,y,radius*depth,0,Math.PI*2);hollow?ctx.stroke():ctx.fill();if(!hollow&&a.glow>0&&i%5===0){ctx.globalAlpha=.055*a.glow;ctx.beginPath();ctx.arc(x,y,radius*(1+a.glow*5),0,Math.PI*2);ctx.fill()}}ctx.globalAlpha=1;}
function draw(now){requestAnimationFrame(draw);if(document.hidden)return;const dt=lastDraw?Math.min(.1,(now-lastDraw)/1000):0;lastDraw=now;if(preview&&pet){accumulator+=dt;let count=0;while(accumulator>=1/30&&count++<3){advancePreview();accumulator-=1/30}}if(document.body.classList.contains('gallery-mode')){if(++galleryTick%3===0)Array.from($('gallery').querySelectorAll('canvas')).forEach((c,i)=>paint(c,current,presets[i],now,true))}else paint(canvas,current,appearance,now);if(current){const p=$('still').checked?current.still:current,activity=current.activity,known=activity?.observed&&(preview||now-lastTickAt<800),title=demoDone?'Settling back into the habitat.':known?activity.label:'A moment between tasks.',detail=demoDone?'Preview complete':known?activity.tool||(preview?'Simulated lifecycle event':'Observed lifecycle event'):'Activity unobserved',evidence=preview?'Simulated preview':known?'Observed in Codewhale':'Local companion';for(const [id,text] of [['activity-title',title],['activity-detail',detail],['activity-evidence',evidence]])if($(id).textContent!==text)$(id).textContent=text;$('activity-parallel').textContent=known&&activity.parallel?activity.parallel+' parallel agent'+(activity.parallel===1?'':'s')+' active':'';$('caption').textContent=(preview?'Preview · ':'')+p.style.channel+' · '+p.style.arch+(current.state.observed<.92?' · unobserved':'')+(demoDone?' · complete':'');$('clock').textContent=Math.floor(current.timeMs/60000)+':'+String(Math.floor(current.timeMs/1000)%60).padStart(2,'0');canvas.setAttribute('aria-label',title+'; '+detail+'; '+$('caption').textContent+'; '+(selected==='Custom'?'custom':selected)+' appearance')}}
async function connect(){if(preview){await makePreview();return}const token=location.hash.slice(1);history.replaceState(null,'',location.pathname);if(token){if(!/^[a-f0-9]{64}$/i.test(token))throw Error('Invalid local connection');await request('/v1/attach',{}, {Authorization:'Bearer '+token})}$('join').hidden=true;$('message').textContent='One live pet. Appearance changes are shared; preview work stays isolated.';await poll()}
requestAnimationFrame(draw);connect().catch(e=>{$('message').textContent=e.message});
</script></html>

View on GitHub (pinned to 433685b202)